Numeric N-dimensional array initialization

2006-06-22 Thread TG
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Numeric N-dimensional array initialization

2006-06-22 Thread TG
I tried to use Numeric.fromfunction, 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 :

 Numeric.fromfunction(myfunc,(5,5))
0

I'm a bit puzzled here

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Numeric N-dimensional array initialization

2006-06-22 Thread Robert Kern
TG wrote:
 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.

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

   http://www.scipy.org/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.

   https://lists.sourceforge.net/lists/listinfo/numpy-discussion

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.flatiter 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Numeric N-dimensional array initialization

2006-06-22 Thread Robert Kern
TG wrote:
 I tried to use Numeric.fromfunction, 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 :
 
 Numeric.fromfunction(myfunc,(5,5))
 0
 
 I'm a bit puzzled here

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

In [26]: fromfunction(myfunc, (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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Numeric N-dimensional array initialization

2006-06-22 Thread TG
Thanks for your precious advices. The flat iterator is definitely what
i need.

-- 
http://mail.python.org/mailman/listinfo/python-list