0

assuming I have any function such as

f(x, y, z) = xyz

what's the fastest way of calculating every value for f given three linear input arrays x, y, and z?

Of course I can do something along the lines of,

import numpy as np

x = np.linspace(-1., 1., 11)
y = np.linspace(-1., 1., 11)
z = np.linspace(-1., 1., 11)

for xi, xx in enumerate(x):
    for yi, yy in enumerate(y):
        for zi, zz in enumerate(z):
            f(xi, yi, zz) = xx*yy*zz

but this is probably not the best way to do it, especially if the input arrays become larger. Is there a better way of doing it, such as with a list comprehension (but multidimensional) or any other way? Help is appreciated.

4
  • What's the problem with x * y * z? Commented Jun 12, 2021 at 16:48
  • Doing x * y * z will give me a resulting array of shape (11, ) instead of every combination of x, y, z, which will be an array of shape (11, 11, 11) for the above case. Commented Jun 12, 2021 at 17:16
  • f(xi, yi, zz) = xx*yy*zz doesn't work, does it? Python doesn't allow you to assign to a function call! Commented Jun 13, 2021 at 2:28
  • How about x[:,None,None] * y[None,:,None]*z[None,None,:]? This uses broadcasting - it's a key concept when working with numpy arrays. Commented Jun 13, 2021 at 2:30

1 Answer 1

2

First create the grid mesh array (x,y,z) as 3-tuples using meshgrid then you can apply the function in sequence:

import numpy as np
x = y = z = np.linspace(-1.0,1.0,11)
grid_coordinates = np.array(np.meshgrid(x,y,z)).reshape(-1,3)
f = lambda x,y,z : x*y*z
f_values = [ f(x,y,z) for x,y,z in grid_coordinates ]

A unit test for the f_values,

f_values[10] == grid_coordinates[10][0]*grid_coordinates[10][1]*grid_coordinates[10][2]
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much, I was not familiar with how meshgrids worked but this seems to be the way to do it. Going from the documentation for anybody else reading this question, 'meshgrid is very useful to evaluate functions on a grid.'.
Why not just multiply the 3 arrays produced by meshgrid, without further reshaping and manipulating? You are doing as much python iteration as the OP this way!
@hpaulj In this simple function, that would work but if we have nonlinear function, we would need to iterate over.
If we have to iterate, feeding scalar values to a function, there's little point to using numpy. Python iteration on list is faster.
Yes we used list comprehension from plain python above [ f(x,y,z) for x,y,z in grid_coordinates]. Sure, if function can be vectorised the better, as I said earlier.

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.