[Numpy]Can't make fromfunction() work

Scott David Daniels Scott.Daniels at Acm.Org
Mon Oct 6 20:07:48 EDT 2003


Junkie Dolphin wrote:
> Hi, I'm trying this piece of code, but y returns as a 
> float. x and e are created regulary. 
> I'm a beginner with this amazing language, and haven't 
> read (all) the fucking manual yet (shame on me!).
 > ...
 > from Numeric import *;
The semicolon is old magic; lose it except in sentences.

It really helps to read the manual.  Writing it took real
effort on the implementer's part.  Numeric (or numarray)
is an array processing extension to the python language.
Here is the easiest way to determine what is wrong:

> ...
> def ordinate(i):
         print 'Surprise:', i
> 	x = i/N
> 	...


 From the 4.3.1 section of the numarray 0.7 document:

fromfunction( object, shape)

Finally, one may want to create an array with contents which are the 
result of a function evaluation. This is done using the fromfunction 
function, which takes two arguments, a shape and a callable object 
(usually a function). For example:

...

By examining the above examples, one can see that fromfunction creates 
an array of the shape specified by its second argument, and with the 
contents corresponding to the value of the function argument (the first 
argument) evaluated at the indices of the array. Thus the value of m[3, 
4] in the first example above is the value of dist when x=3 and y=4. 
Similarly for the lambda function in the second example, but with a 
rank-3 array. The implementation of fromfunction consists of:
def fromfunction(function, dimensions):
     return apply(function, tuple(indices(dimensions)))

which means that the function function is called with arguments given by 
the sequence indices(dimensions). As described in the definition of 
indices, this consists of arrays of indices which will be of rank one 
less than that specified by dimensions. This means that the function 
argument must accept the same number of arguments as there are 
dimensions in dimensions, and that each argument will be an array of the 
same shape as that specified by dimensions. Furthermore, the array which 
is passed as the first argument corresponds to the indices of each 
element in the resulting array along the first axis, that which is 
passed as the second argument corresponds to the indices of each element 
in the resulting array along the second axis, etc. A consequence of this 
is that the function which is used with fromfunction will work as 
expected only if it performs a separable computation on its arguments, 
and expects its arguments to be indices along each axis. Thus, no 
logical operation on the arguments can be performed, or any non-shape 
preserving operation. Thus, the following will not work as expected:
 >>> def buggy(test):
...     if test > 4: return 1
...     else: return 0
...
 >>> print fromfunction(buggy,(10,))
1

Here is how to do it properly. We add a print statement to the function 
for clarity:

-----
I leave it to you to look up the rest.


-Scott David Daniels
Scott.Daniels at Acm.Org





More information about the Python-list mailing list