How do I create a numpy array containing the values ​​of a multi-variable function?

I want to create an array containing a function f(x,y,z)

. If it were a single variable function I would do, for example:

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

      

to get sin(x)

for x

in[-5,5]

How can I do the same for example sin(x+y+z)

?

+2


source to share


3 answers


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)

      

+5


source


xyz = numpy.mgrid[-5:5,-5:5,-5:5]
sinxyz = numpy.sin(xyz[0]+xyz[1]+xyz[2])

      



+4


source


The numpy.mgrid function will 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 make it work x_num

, y_num

and z_num

must have a clear number, followed by j

, for example,x,y = numpy.mgrid[-1:1:10j, -1:1:10j]

-1


source







All Articles