[Numpy-discussion] Ill-defined in-place operations (#1085)

Charles R Harris charlesr.harris@gmail....
Wed Apr 15 15:10:57 CDT 2009


On Wed, Apr 15, 2009 at 1:52 PM, Pauli Virtanen <pav@iki.fi> wrote:

>
> Mixing views and mutating (eg. in-place) operations can cause surprising
> and ill-defined results. Consider
> http://projects.scipy.org/numpy/ticket/1085:
>
> >>> import numpy as np
> >>> x = np.array([[1,2], [3,4]])
> >>> x
> array([[1, 2],
>       [3, 4]])
> >>> x += x.T
> >>> x
> array([[2, 5],
>       [8, 8]])
> >>> y = np.array([[1,2], [3,4]], order='F')
> >>> y
> array([[1, 2],
>       [3, 4]])
> >>> y += y.T
> >>> y
> array([[2, 7],
>       [5, 8]])
>
> The result depends on the order in which the elements happen to lie in
> the memory. Predicting the outcome is nearly impossible. (Also, I think
> Numpy tries to optimize the order of the loops, making it even more
> difficult?)
>
> This is a sort of a pitfall. Should Numpy issue a warning every time a
> mutating operation is performed on an array, with input data that is a
> view on the same array?
>

My first thought is yes, just because the result is hard to predict.


>
> Some alternatives:
>
>  a) Raise warning for all arrays, even 1-D.
>

Yes again. Are there examples where one would not want a warning? Examples
that couldn't be implemented otherwise?

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090415/f54088f1/attachment.html 


More information about the Numpy-discussion mailing list