Numarray newbie question

C

ChinStrap

I know there are probably alternatives for this with the standard
library, but I think that would kill the speed I get with numarray:

Say I have two 2-dimensional numarrays (x_mat and y_mat, say), and a
function f(x,y) that I would like to evaluate at every index.
Basically I want to be able to say f(x_mat,y_mat) and have it return a
numarray with the same shape and element wise evaluation of f. I know,
I want a ufunc, but they look scary to write my own. Are there any
functions that do this? Or is writing ufuncs easier than it seems?

Thanks,
-Chris Neff
 
R

Robert Kern

ChinStrap said:
I know there are probably alternatives for this with the standard
library, but I think that would kill the speed I get with numarray:

Say I have two 2-dimensional numarrays (x_mat and y_mat, say), and a
function f(x,y) that I would like to evaluate at every index.
Basically I want to be able to say f(x_mat,y_mat) and have it return a
numarray with the same shape and element wise evaluation of f. I know,
I want a ufunc, but they look scary to write my own. Are there any
functions that do this? Or is writing ufuncs easier than it seems?

It depends. If you write f(x,y) such that it operates on arrays as a
whole (meaning you can't use if statements, etc.), then it may Just Work.

If you need to do something where you can't express it like that, then
take a look at Scipy's[1] vectorize[2] function.

[1] http://www.scipy.org
[2] http://oliphant.ee.byu.edu:81/scipy_base/vectorize/

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
C

ChinStrap

Are there no windows binaries for SciPy for python 2.4 yet? I try to
run the installer and it complains that it can't find python 2.3.

Besides that, vectorize is exactly what i want.
 
R

Robert Kern

ChinStrap said:
Are there no windows binaries for SciPy for python 2.4 yet? I try to
run the installer and it complains that it can't find python 2.3.

No, not yet.
Besides that, vectorize is exactly what i want.

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
C

ChinStrap

Oh well. I am downloading all the things to build it, but in the mean
time I just did:

def get_y_mat(x_ind,y_ind):

return self.y_min + y_ind*self.dy

def get_x_mat(x_ind,y_ind):

return self.x_min + x_ind*self.dx

self.x_mat=fromfunction(get_x_mat,matshape)
self.y_mat=fromfunction(get_y_mat,matshape)

def fxy(x_ind,y_ind):
x=self.x_min + x_ind*self.dx
y=self.y_min + y_ind*self.dx
return f(x,y)

def vxy(x_ind,y_ind):
x=self.x_min + x_ind*self.dx
y=self.y_min + y_ind*self.dx
return v(x,y)

self.f_mat=fromfunction(fxy,matshape)
self.v_mat=fromfunction(vxy,matshape)

As you can see I am just repeating calculations in fxy and vxy that I
have already done for x_mat and y_mat. This is still faster than
saying:

self.f_mat = array([f(x,y) for x in x_mat for y in y_mat],matshape)

by a noticable amount.
 
C

Colin J. Williams

ChinStrap said:
I know there are probably alternatives for this with the standard
library, but I think that would kill the speed I get with numarray:

Say I have two 2-dimensional numarrays (x_mat and y_mat, say), and a
function f(x,y) that I would like to evaluate at every index.
Basically I want to be able to say f(x_mat,y_mat) and have it return a
numarray with the same shape and element wise evaluation of f. I know,
I want a ufunc, but they look scary to write my own. Are there any
functions that do this? Or is writing ufuncs easier than it seems?

Thanks,
-Chris Neff
numarray has a fixed set of ufuncs. Section 5.1 of the docs.

Section 5.3 points to a method for writing the f(i, j).


Unfortunately, it appears to require that f be written in C and assumes
that the user operating system has a compiler, which windows, in
general, does not.

It would be good if f could be written in Python.

Colin W.
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top