[Numpy-discussion] Should arr.diagonal() return a copy or a view? (1.7 compatibility issue)

Frédéric Bastien nouiz at nouiz.org
Fri May 11 11:54:29 EDT 2012


In Theano we use a view, but that is not relevant as it is the
compiler that tell what is inplace. So this is invisible to the user.

What about a parameter to diagonal() that default to return a view not
writable as you said. The user can then choose what it want and this
don't break the inferface.

You suggest that in 1.7 we return a view that is not writable, but
tell this is an interface change. I don't see an interface change here
as this change nothing for the user(if the writable flag is respected.
Can we rely on this?). The user will need to update their code in 1.8
if we return a writable view. So I think the interface change is in
1.8.

Why not change the interface in numpy 2.0? Otherwise, we have a high
risk of people just updating numpy without checking the release note
and have bad result. The parameter to diagonal will allow people to
have a view.

Fred

On Fri, May 11, 2012 at 11:42 AM, Nathaniel Smith <njs at pobox.com> wrote:
> I've been trying to sort through the changes that landed in master
> from the missingdata branch to figure out how to separate out changes
> related to NA support from those that aren't, and noticed that one of
> them should probably be flagged to the list. Traditionally,
> arr.diagonal() and np.diagonal(arr) return a *copy* of the diagonal.
> Now, they return a view onto the original array.
>
> On the one hand, this seems like it's clearly the way it should have
> been since the beginning -- I'd expect .diagonal() to be a cheap
> operation, like .transpose() and .reshape(). But, it's a potential
> compatibility break if there is code out there that assumes diagonal()
> returns a copy and can be scribbled on without affecting the original
> array:
>
> # 1.6:
>>>> a = np.ones((2, 2))
>>>> d = a.diagonal()
>>>> d[0] = 3
>>>> a
> array([[ 1.,  1.],
>       [ 1.,  1.]])
>
> # current master/1.7:
>>>> a = np.ones((2, 2))
>>>> d = a.diagonal()
>>>> d[0] = 3
>>>> a
> array([[ 3.,  1.],
>       [ 1.,  1.]])
>
> This is dangerous, obviously, and tricky to handle, since there's no
> clear way to detect it and give a DeprecationWarning.
>
> One option might be to keep the new behavior, but mark the returned
> view as not WRITEABLE, and then flip to WRITEABLE=True in 1.8. Going
> from read-only to writeable would be a compatible change, so that way
> we end up on the behaviour we want eventually (in 1.8), and have only
> one backwards compatibility break (1.6 -> 1.7), but that break is
> clean and obvious.
>
> -- Nathaniel
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion



More information about the NumPy-Discussion mailing list