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
  • What would be the values for x,y,z and/or how do you plan to generate them? Commented Sep 5, 2009 at 10:33
  • x,y,z would be the cartesian product of numpy.linspace(-5,5,100) over all three dimensions. I don't know the best way to generate them. I guess that's a pre-requisite for the question. Commented Sep 5, 2009 at 10:58

3 Answers 3

5

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)
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

-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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.