<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Dec 12, 2014 at 11:28 AM, Stephan Hoyer <span dir="ltr"><<a href="mailto:shoyer@gmail.com" target="_blank">shoyer@gmail.com</a>></span> wrote:<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 class="gmail_extra"><div class="gmail_quote"><span class="">On Fri, Dec 12, 2014 at 5:48 AM, Jaime Fernández del Río <span dir="ltr"><<a href="mailto:jaime.frio@gmail.com" target="_blank">jaime.frio@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 class="gmail_extra"><div class="gmail_quote"><div><div><span style="color:rgb(34,34,34)">np.broadcast is the Python object of the old iterator. It may be a better idea to write all of these functions using the new one, np.nditer:</span><br></div></div><div><br></div><div>def common_shape(*args):</div><div>    return np.nditer(args).shape[::-1]  # Yes, you do need to reverse it!</div></div></div></div></blockquote><div><br></div></span><div>Unfortunately, that version does not seem to do what I'm looking for:</div><div><br></div><div><span class=""><div>def common_shape(*args):</div><div>    return np.nditer(args).shape[::-1]</div><div><br></div></span><div>x = np.empty((4,))</div><span class=""><div>y = np.empty((2, 3, 4))</div></span><div>print(common_shape(x, y))</div></div><div><br></div><div>Outputs: (6, 4)</div></div></div></div></blockquote><div><br></div><div>Yes, the iterator is a smart beast. I think this is what you need then, with no reversing involved:</div><div><br></div><div><div>>>> np.nditer((x,y), flags=['multi_index']).shape</div><div>(2, 3, 4)</div></div><div><br></div><div> </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 class="gmail_extra"><div class="gmail_quote"><span class=""><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 class="gmail_extra"><div class="gmail_quote"><div></div><div>And in writing 'broadcast_to', rather than rewriting the broadcasting logic, you could check the compatibility of the shape with something like:</div><div><br></div><div>np.nditer((arr,), itershape=shape)  # will raise ValueError if shapes incompatible<br></div><div><br></div><div>After that, all that would be left is some prepending of zero strides, and some zeroing of strides of shape 1 dimensions before calling as_strided</div></div></div></div></blockquote><div><br></div></span><div>Yes, that is a good idea.</div><div><br></div><div>Here is a gist with the latest version of this code (shortly to be turned into a PR):</div><div><a href="https://gist.github.com/shoyer/3e36af0a8196c82d4b42" target="_blank">https://gist.github.com/shoyer/3e36af0a8196c82d4b42</a><br></div></div></div></div>
<br>_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">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>
<br></blockquote></div><br clear="all"><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>