[Numpy-discussion] Index array confusion...
Todd Miller
jmiller at stsci.edu
Mon Dec 16 06:57:01 EST 2002
Magnus Lie Hetland wrote:
>Todd Miller <jmiller at stsci.edu>:
>
>
>[snip]
>
>
>>What I'm saying is that in the future it will work correctly. :)
>>
>>
>
>OK :)
>
>
>
>>Most likely as you suggest, but I haven't really looked at the code,
>>just the rather sickening results.
>>
>>
>
>Well, what I was asking about was really what you meant by "correct".
>My interpretation of "correct" here was that only tuples and slices
>would be allowed as indexes.
>
This actually worked as you expected in numarray-0.3.6. The current
behavior is a casualty of optimization to C.
>
>BTW: I found something else that I think is rather odd:
>
>
>
>>>>x = [0, 0, 0]
>>>>y = [1, 1, 1]
>>>>p = 1
>>>>x[p:], y[p:] = y[p:], x[p:]
>>>>x, y
>>>>
>>>>
>([0, 1, 1], [1, 0, 0])
>
>
>>>>x = array([0, 0, 0])
>>>>y = array([1, 1, 1])
>>>>x[p:], y[p:] = y[p:], x[p:]
>>>>x, y
>>>>
>>>>
>(array([0, 1, 1]), array([1, 1, 1]))
>
>This seems like a bug to me. The assignment ought to swap the tails of
>
Numeric does this as well, so I would not call it a bug, but I agree
that it is unfortunate.
>the sequences, as is the case with lists, but with numeric arrays,
>some weird form of overwriting occurs. I guess this may be an
>optimization (i.e. to avoid making copies), but it is rather
>
I think you're correct here. This behavior is a consequence of the
fact that array slices are views and not copies. To fix it, just say:
x[p:], y[p:] = y[p:].copy(), x[p:].copy()
>confusing. What do you think?
>
The manual should probably document this as a gotcha, and we might want
to consider adding an exchange ufunc which can do the swap without a
temporary. I doubt the cost of exchange is worth it though.
Thanks for the feedback,
Todd
More information about the NumPy-Discussion
mailing list