[SciPy-user] Numpy : in place moving data strange behaviour

Robert Kern robert.kern at gmail.com
Fri Aug 1 05:06:14 EDT 2008


On Fri, Aug 1, 2008 at 03:47, Samuel GARCIA <sgarcia at olfac.univ-lyon1.fr> wrote:
> Hi list,
> I have a strange behaviour of numpy if I try to move in place
> a part of my array in an other place of the array.
> For 1D or for 2D there is not the same behaviour.

Basically, you're treading into areas where we make no guarantees. The
exact order we iterate over the arrays should be considered an
implementation detail, and you should not rely on it. This should only
affect your results in cases where you are modifying an array inplace
where the source is an overlapping view of that very array.

Without diving into the source, I think what's happening is that the
1D version happens to deal with contiguous slices on both the LHS and
the RHS, so memcpy() is used or something similar which handles
overlaps specially. In the 2D case, neither is contiguous, so we
iterate manually in the naive way. For example, we can construct a 1D
non-contiguous case which demonstrates the same behavior:

>>> from numpy import *
>>> a = arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a[2:9:2] = a[0:7:2]
>>> a
array([0, 1, 0, 3, 0, 5, 0, 7, 0, 9])

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco



More information about the SciPy-User mailing list