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 of code in a double loop:
v = v - r[i,j] * q[:,i]
where q
and r
are both numpy.array
, and v
is a slice of another numpy.array
taken as v = x[:,j]
.
The above code doesn't work as expected in all cases. However, if I make the following change:
v -= r[i,j] * q[:,i]
Then everything works flawlessly.
I was under the impression that those two lines should be identical. To test whether -=
and _ = _ -
were working differently, I created the following snippet
import numpy
x = numpy.array(range(0,6))
y = numpy.array(range(0,6))
u = x[3:5]
v = y[3:5]
print u,v
u = u - [1,1]
v -= [1,1]
print u,v
which again works as expected, producing [2 3] [2 3]
at both print statements.
So I'm entirely confused why those two lines perform differently. The only possible thing I can think of is that I am dealing with extremely small numbers sometimes (on the order of 10^-8 or smaller) and there is some precision issue that -=
is better at? The first line performs increasingly worse as the elements of x
get smaller.
I apologize if there's any other posts about this similar issue, I can't search for -
and -=
and I don't know if there's any correct terms for these besides assignment/operators.
Thanks for any help!