Numeric N-dimensional array initialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TG

    Numeric N-dimensional array initialization

    Hi there !

    I'm just starting to use Numeric here, and I'm wondering : how can I
    efficiently initialize every values of a N-dimensional array, given I
    don't know the number of dimensions ?

    I'm looking for something like a map function, or a way to conveniently
    iterate through the whole N-array, but I didn't find anything ... yet.
    If anyone has a clue, I'm listening.

    Thanks

  • TG

    #2
    Re: Numeric N-dimensional array initialization

    I tried to use Numeric.fromfun ction, but there seems to be a problem :

    the function called must have the right number of args (hint : the
    number of dimensions, which I don't know). So i tried to use a function
    like :

    def myfunc(*args, **kw):
    return 0

    and then i get :
    [color=blue][color=green]
    >> Numeric.fromfun ction(myfunc,(5 ,5))[/color][/color]
    0

    I'm a bit puzzled here

    Comment

    • Robert Kern

      #3
      Re: Numeric N-dimensional array initialization

      TG wrote:[color=blue]
      > Hi there !
      >
      > I'm just starting to use Numeric here, and I'm wondering : how can I
      > efficiently initialize every values of a N-dimensional array, given I
      > don't know the number of dimensions ?
      >
      > I'm looking for something like a map function, or a way to conveniently
      > iterate through the whole N-array, but I didn't find anything ... yet.
      > If anyone has a clue, I'm listening.[/color]

      Since you're just starting, you should know that Numeric is no longer being
      developed. The actively developed version is numpy:



      You will want to ask numpy questions on the numpy-discussion mailing list. The
      answers one gets here tend to be hit or miss.



      As to your actual question, I'm not entirely sure what you are asking for, but
      you should look at the .flat attribute.


      In [5]: from numpy import *

      In [6]: a = empty((2, 3))

      In [7]: a.flat[:] = 10

      In [8]: a
      Out[8]:
      array([[10, 10, 10],
      [10, 10, 10]])

      # Note, in numpy, .flat is not a real array although it should be usable in most
      # places that need an array. However, unlike Numeric, it can always be used
      # even if the array is not contiguous. If you need a real array, use the
      # .ravel() method, but know that it will make a copy if the array is not
      # contiguous (for example, if it is the result of a .transpose() call).
      In [9]: a.flat
      Out[9]: <numpy.flatit er object at 0x196a800>

      In [10]: a.flat = arange(10)

      In [11]: a
      Out[11]:
      array([[0, 1, 2],
      [3, 4, 5]])

      In [12]: for i in a.flat:
      ....: print i
      ....:
      ....:
      0
      1
      2
      3
      4
      5

      --
      Robert Kern

      "I have come to believe that the whole world is an enigma, a harmless enigma
      that is made terrible by our own mad attempt to interpret it as though it had
      an underlying truth."
      -- Umberto Eco

      Comment

      • Robert Kern

        #4
        Re: Numeric N-dimensional array initialization

        TG wrote:[color=blue]
        > I tried to use Numeric.fromfun ction, but there seems to be a problem :
        >
        > the function called must have the right number of args (hint : the
        > number of dimensions, which I don't know). So i tried to use a function
        > like :
        >
        > def myfunc(*args, **kw):
        > return 0
        >
        > and then i get :
        >[color=green][color=darkred]
        >>> Numeric.fromfun ction(myfunc,(5 ,5))[/color][/color]
        > 0
        >
        > I'm a bit puzzled here[/color]

        In [24]: def myfunc(*args):
        ....: print args
        ....:
        ....:

        In [26]: fromfunction(my func, (2, 2))
        (array([[0, 0],
        [1, 1]]),
        array([[0, 1],
        [0, 1]]))

        fromfunction() does not iterate over the possible indices and call the function
        with scalar arguments to get a scalar return value. It generates N arrays with
        index values in them and calls the function once with those arrays. The return
        value should be another array.

        If you actually just want 0s:

        In [27]: zeros((5, 5))
        Out[27]:
        array([[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]])

        If you want 1s:

        In [28]: ones((5, 5))
        Out[28]:
        array([[1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1]])

        If you just want an array as fast as possible because you are going to fill in
        values later:

        In [29]: empty((5, 5))
        Out[29]:
        array([[ 13691, 0, 0, 2883587, 3],
        [ 3, 0, 828189706, 6, 0],
        [ 0, 9, 10, 828202281, 0],
        [ 7, 0, 0, 0, 0],
        [ 0, 0, 0, 0, 0]])


        If you have more complicated needs, we can talk about them on numpy-discussion.

        --
        Robert Kern

        "I have come to believe that the whole world is an enigma, a harmless enigma
        that is made terrible by our own mad attempt to interpret it as though it had
        an underlying truth."
        -- Umberto Eco

        Comment

        • TG

          #5
          Re: Numeric N-dimensional array initialization

          Thanks for your precious advices. The flat iterator is definitely what
          i need.

          Comment

          Working...