[Numpy-discussion] Multiple assignment confusion

Benjamin Schwartz bens at MIT.EDU
Sun Nov 16 19:53:02 CST 2003

I am trying to interchange two pieces of an array.  Here is my demo code:

 >>> from Numeric import *
 >>> a=array([.7,.1,.1,.1])
 >>> a=multiply.outer(a,a)
 >>> a=multiply.outer(a,a)
 >>> sum(sum(sum(sum(a))))
 >>> a[1,0],a[1,1]=a[1,1],a[1,0]
 >>> sum(sum(sum(sum(a))))

So a is a four-dimensional array that sums to 1, but when I attempt to 
switch two 2-d sub-arrays of it I just end up duplicating one of them, 
shown by a change in the value of the sum (to .94).  Is this behavior 
deliberate?  It certainly seems to break Python's tuple-assignment model.

What I would ideally like to do is perform this swap without more than 
two-elements'-worth of temporary storage.  I.e. perform it as a pure 
reference swap or switch each pair of elements one by one, reusing 
temporary space.  Is this possible with Numeric (or Numarray?)?


More information about the Numpy-discussion mailing list