[SciPy-user] += has strange behavior with multidimensional arrays

Erik Volz erik.volz@gmail....
Wed Dec 5 18:56:07 CST 2007

Sorry if this issue is already well-known.

It seems that += and perhaps *= operators do not have correct behavior
with multidimensional arrays.

Let's say I want to make a simple matrix symmetric across the diagonal.

from pylab import *
y = ones((10,10)) * arange(1,11)
print y
yy = y
print 'This is incorrect:'
y+=transpose(y); print y
print 'But this still works:'
yy = yy+transpose(yy); print yy

Clearly python is modifying the array element by element, not
simultaneously. You might not consider this a bug, but the behavior is
so unintuitive (and unlike matlab) that it would trip up most people.

More information about the SciPy-user mailing list