<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sun, May 10, 2015 at 4:40 AM, Stefan Otte <span dir="ltr"><<a href="mailto:stefan.otte@gmail.com" target="_blank">stefan.otte@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">Hey,<br><br>quite often I want to evaluate a function on a grid in a n-D space.<br>What I end up doing (and what I really dislike) looks something like this:<br><br>  x = np.linspace(0, 5, 20)<br>  M1, M2 = np.meshgrid(x, x)<br>  X = np.column_stack([M1.flatten(), M2.flatten()])<br>  X.shape  # (400, 2)<br><br>  fancy_function(X)<br><br>I don't think I ever used `meshgrid` in any other way.<br>Is there a better way to create such a grid space?<br><br>I wrote myself a little helper function:<br><br>  def gridspace(linspaces):<br>      return np.column_stack([space.flatten()<br>                              for space in np.meshgrid(*linspaces)])<br><br>But maybe something like this should be part of numpy?<br></div></blockquote><div><br></div><div>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.:</div><div><br></div><div><a href="http://stackoverflow.com/questions/1208118/using-numpy-to-build-an-array-of-all-combinations-of-two-arrays/1235363#1235363">http://stackoverflow.com/questions/1208118/using-numpy-to-build-an-array-of-all-combinations-of-two-arrays/1235363#1235363</a><br></div><div><br></div><div>Perhaps we could make this part of numpy.lib.arraysetops? Isthere room for other combinatoric generators, i.e. combinations, permutations... as in itertools?</div><div><br></div><div>Jaime</div></div><div><br></div>-- <br><div class="gmail_signature">(\__/)<br>( O.o)<br>( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial.</div>
</div></div>