[Numpy]Can't make fromfunction() work

J

Junkie Dolphin

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!).
If someone used with Numeric could give me an
hint I would be very glad.. Thank you! =)

from Numeric import *;

G = 200.
N = 15.

def abscissa(i):
return i/N

def ordinate(i):
x = i/N
if (0.<=x) & (x<0.3): return 0.
elif (0.3<=x) & (x<=0.35): return 20.*x-6.
elif (0.35<x) & (x<0.65): return 1.
elif (0.65<=x) & (x<=0.7): return 14.-20.*x
elif (0.7<x) & (x<=1.): return 0.
raise ArithmeticError("function is undefined for ",x)

def errors(i):
return i/G

# from command line (unix script style)
if __name__ == '__main__':
x = fromfunction(abscissa,(16,))
y = fromfunction(ordinate,(16,))
e = fromfunction(errors,(16,))
print type(y)

*****************
bash-2.05b$ python ex1.py
<type 'float'>
bash-2.05b$
--
Ciao, Giovanni aka Junkie Dolphin

Political language...is designed to make lies
sound truthful and murder respectable, and to
give an appearance of solidity to pure wind.

George Orwell
 
S

Scott David Daniels

Junkie said:
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:.... if test > 4: return 1
.... else: return 0
....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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top