views:

295

answers:

2

I want to create an array holding a function f(x,y,z). If it were a function of one variable I'd do, for instance:

sinx = numpy.sin(numpy.linspace(-5,5,100))

to get sin(x) for x in [-5,5]

How can I do the same to get, for instance sin(x+y+z)?

+2  A: 

I seem to have found a way:

# define the range of x,y,z
x_range = numpy.linspace(x_min,x_max,x_num)
y_range = numpy.linspace(y_min,y_max,y_num)
z_range = numpy.linspace(z_min,z_max,z_num)

# create arrays x,y,z in the correct dimensions
# so that they create the grid
x,y,z = numpy.ix_(x_range,y_range,z_range)

# calculate the function of x, y and z
sinxyz = numpy.sin(x+y+z)
Nathan Fellman
+1  A: 
xyz = numpy.mgrid[-5:5,-5:5,-5:5]
sinxyz = numpy.sin(xyz[0]+xyz[1]+xyz[2])
Andrew