15 Apr 21:52 2009

## Ill-defined in-place operations (#1085)

Pauli Virtanen <pav <at> iki.fi>

2009-04-15 19:52:54 GMT

2009-04-15 19:52:54 GMT

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? Some alternatives: a) Raise warning for all arrays, even 1-D. b) Raise warning only for multidimensional arrays. (Assume the user can predict what happens in 1-D.) c) Don't raise any warnings, just educate users in the documentation. This is not the first time this has been discussed on this ML, but the last discussion did not (IIRC) lead to any conclusions. (But I think it was established that detecting when views really overlap is a problem with no cheap solution. So walking up ->base pointers or comparing ->data pointers seemed nearly the only feasible way.) -- -- Pauli Virtanen