I\'m having some strange behavior in my python code related to -
and -=
. I\'m writing a QR decomposition using numpy, and have the following line o
+1 to both other answers to this questions. They cover two important differences between =
and -=
but I wanted to highlight one more. Most of the time x -= y
is the same as x[:] = x - y
, but not when x
and y
are slices of the same array. For example:
x = np.ones(10)
y = np.ones(10)
x[1:] += x[:-1]
print x
[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
y[1:] = y[1:] + y[:-1]
print y
[ 1. 2. 2. 2. 2. 2. 2. 2. 2. 2.]