[Numpy-discussion] Why ndarray provides four ways to flatten?

Nathaniel Smith njs at pobox.com
Tue Oct 28 22:11:49 EDT 2014

On 29 Oct 2014 01:47, "Alexander Belopolsky" <ndarray at mac.com> wrote:
> On Tue, Oct 28, 2014 at 9:23 PM, Nathaniel Smith <njs at pobox.com> wrote:
>> OTOH trying to make .flat into a full duck-compatible ndarray-like
>> type is a non-starter; it would take a tremendous amount of work for
>> no clear gain.
> I don't think so - I think all the heavy lifting is already done in
flatiter.  The missing parts are mostly trivial things like .size or .shape
or can be fudged by coercing to true ndarray using existing
flatiter.__array__ method.

Now try .resize()... The full ndarray api is vast, and niggling problems
would create endless maintenance issues. If your api is going to be that
leaky then it's better not to have it at all.

> It would be more interesting however if we could always return a true
ndarray view.  How is ndarray.diagonal() view implemented in 1.9?  Can
something similar be used to create a flat view?

.diagonal has no magic, it just turns out that the diagonal of any strided
array is also expressible as a strided array. (Specifically, new_strides =
(sum(old_strides),).) There is no analogous theorem for flattening.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20141029/10630e15/attachment.html>

More information about the NumPy-Discussion mailing list