<div dir="ltr"><div>Hey,<br><br>here is an ipython notebook with benchmarks of all implementations (scroll to the bottom for plots):<br> <a href="https://github.com/sotte/ipynb_snippets/blob/master/2015-05%20gridspace%20-%20cartesian.ipynb">https://github.com/sotte/ipynb_snippets/blob/master/2015-05%20gridspace%20-%20cartesian.ipynb</a><br><br></div>Overall, Jaime's version is the fastest.<br><br><br><br><br><div><br><br></div></div><br><div class="gmail_quote">On Tue, May 12, 2015 at 2:01 PM Jaime Fernández del Río <<a href="mailto:jaime.frio@gmail.com">jaime.frio@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, May 12, 2015 at 1:17 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"><div>Hello,<br><br></div>indeed I was looking for the cartesian product.<br><br>I timed the two stackoverflow answers and the winner is not quite as clear:<br><div><div><br>n_elements: 10 cartesian 0.00427 cartesian2 0.00172<br>n_elements: 100 cartesian 0.02758 cartesian2 0.01044<br>n_elements: 1000 cartesian 0.97628 cartesian2 1.12145<br>n_elements: 5000 cartesian 17.14133 cartesian2 31.12241<br><br></div><div>(This is for two arrays as parameters: np.linspace(0, 1, n_elements))<br>cartesian2 seems to be slower for bigger.<br></div></div></div></blockquote><div><br></div></div></div></div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>On my system, the following variation on Pauli's answer is 2-4x faster than his for your test cases:</div><div><br></div><div><div><font face="monospace, monospace">def cartesian4(arrays, out=None):</font></div><div><font face="monospace, monospace"> arrays = [np.asarray(x).ravel() for x in arrays]</font></div><div><font face="monospace, monospace"> dtype = np.result_type(*arrays)</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> n = np.prod([arr.size for arr in arrays])</font></div><div><font face="monospace, monospace"> if out is None:</font></div><div><font face="monospace, monospace"> out = np.empty((len(arrays), n), dtype=dtype)</font></div><div><font face="monospace, monospace"> else:</font></div><div><font face="monospace, monospace"> out = out.T</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> for j, arr in enumerate(arrays):</font></div><div><font face="monospace, monospace"> n /= arr.size</font></div><div><font face="monospace, monospace"> out.shape = (len(arrays), -1, arr.size, n)</font></div><div><font face="monospace, monospace"> out[j] = arr[np.newaxis, :, np.newaxis]</font></div><div><font face="monospace, monospace"> out.shape = (len(arrays), -1)</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> return out.T</font></div></div></div></div></div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br></div><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"><div><div><br></div><div>I'd really appreciate if this was be part of numpy. Should I create a pull request?<br></div></div></div></blockquote><div><br></div></div></div></div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>There hasn't been any opposition, quite the contrary, so yes, I would go ahead an create that PR. I somehow feel this belongs with the set operations, rather than with the indexing ones. Other thoughts?</div><div><br></div><div>Also for consideration: should it work on flattened arrays? or should we give it an axis argument, and then "broadcast on the rest", a la generalized ufunc?</div></div></div></div><div dir="ltr"><div class="gmail_extra"><br>Jaime<br clear="all"><div><br></div>-- <br><div>(\__/)<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>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org" target="_blank">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote></div>