numarray question

S

SunX

I tried to initialize a float point array by:

import numarray
xur = numarray.fromfunction(lambda x,y,z:x*y*z, (2, 2, 2))

but I ended up with an integer array even though x, y, and z are all floats.

BTW, how do you unzip NumTut in windows? And is there a newer version?

Thank you
Howard
 
C

Colin J. Williams

SunX said:
I tried to initialize a float point array by:

import numarray
xur = numarray.fromfunction(lambda x,y,z:x*y*z, (2, 2, 2))

but I ended up with an integer array even though x, y, and z are all floats.

BTW, how do you unzip NumTut in windows? And is there a newer version?

Thank you
Howard
You might try something like:

import numarray
xyz= lambda x,y,z:x*y*z
xur = numarray.fromfunction(xyz, (6, 6, 3))
print xur
 
J

Jean Brouwers

You probably meant

xyz = lambda x,y,z: float(x*y*z)

or maybe even

xyz = lambda x,y,z: float(x)*float(y)*float(z)


/Jean Brouwers
 
C

Colin J. Williams

Jean said:
You probably meant

xyz = lambda x,y,z: float(x*y*z)

or maybe even

xyz = lambda x,y,z: float(x)*float(y)*float(z)


/Jean Brouwers
Jean,

You are right, I overlooked SunX's wish to have a Float array.

Unfortunately, numarray assumes that the source is based in
the function indices, which delivers an Int array.

Thus, your suggestion raises an exception. An alternative
is given below.

Colin W.

import numarray
xyz= lambda x,y,z: x*y*z
# This raises an exception
##xur = numarray.fromfunction(xyz, (6, 4, 3))
## File "C:\Python23\Lib\site-packages\numarray\numarraycore.py", line
715, in __float__
## raise TypeError, "Only rank-0 numarray can be cast to floats."
# This gets around that problem
ind= numarray.indices(shape= (6, 4, 3), type= numarray.Float)
xur= apply(xyz, ind)
print xur
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top