Reputation: 127428
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)
?
Upvotes: 2
Views: 4196
Reputation: 1
The numpy.mgrid function would work equally well:
x,y,z = numpy.mgrid[x_min:x_max:x_num, y_min:y_max:y_num, z_min:z_max:z_num]
sinxyz = numpy.sin(x+y+z)
edit: to get it to work x_num
, y_num
and z_num
have to be explicit numbers followed by j
, e.g., x,y = numpy.mgrid[-1:1:10j, -1:1:10j]
Upvotes: -1
Reputation: 1569
xyz = numpy.mgrid[-5:5,-5:5,-5:5]
sinxyz = numpy.sin(xyz[0]+xyz[1]+xyz[2])
Upvotes: 4
Reputation: 127428
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)
Upvotes: 5