[Numpy-discussion] Create a n-D grid; meshgrid alternative

Jaime Fernández del Río jaime.frio at gmail.com
Sun May 10 17:46:12 EDT 2015

On Sun, May 10, 2015 at 4:40 AM, Stefan Otte <stefan.otte at gmail.com> wrote:

> Hey,
> quite often I want to evaluate a function on a grid in a n-D space.
> What I end up doing (and what I really dislike) looks something like this:
>   x = np.linspace(0, 5, 20)
>   M1, M2 = np.meshgrid(x, x)
>   X = np.column_stack([M1.flatten(), M2.flatten()])
>   X.shape  # (400, 2)
>   fancy_function(X)
> I don't think I ever used `meshgrid` in any other way.
> Is there a better way to create such a grid space?
> I wrote myself a little helper function:
>   def gridspace(linspaces):
>       return np.column_stack([space.flatten()
>                               for space in np.meshgrid(*linspaces)])
> But maybe something like this should be part of numpy?

Isn't what you are trying to build a cartesian product function? There is a
neat, efficient implementation of such a function in StackOverflow, by our
own pv.:


Perhaps we could make this part of numpy.lib.arraysetops? Isthere room for
other combinatoric generators, i.e. combinations, permutations... as in


( O.o)
( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes
de dominación mundial.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150510/83230539/attachment.html>

More information about the NumPy-Discussion mailing list