[Numpy-discussion] Unexpected behavior with numpy array
Damian Eads
eads@soe.ucsc....
Sun Feb 3 13:25:56 CST 2008
Good day,
Reversing a 1-dimensional array in numpy is simple,
A = A[:,:,-1] .
However A is a new array referring to the old one and is no longer
contiguous.
While trying to reverse an array in place and keep it contiguous, I
encountered some weird behavior. The reason for keeping it contiguous is
the array must be passed to an old C function I have, which expects the
buffer to be in row major order and contiguous. I am using lots of
memory so I want to minimize copying and allocation of new arrays.
>>> A=numpy.arange(0,10)
>>> A
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> A[::-1]
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
>>> A[:] = A[::-1]
>>> A
array([9, 8, 7, 6, 5, 5, 6, 7, 8, 9])
>>>
Is there any way to perform assignments of the following form
X[sliceI] = expression involving X
with dimensions that are
compatible with the left
hand side
without causing the odd behavior I mentioned. If not, it might be
helpful to throw an exception when both the LHS and the RHS of an
assignment reference an array slice of the same variable?
On similar note, does the assignment
A = A * B
create a new array with a new buffer to hold the result of A * B, and
assign A to refer to the new array?
Thanks very much!
Damian
More information about the Numpy-discussion
mailing list