- vs -= operators with numpy

前端 未结 3 1078
闹比i
闹比i 2021-01-04 02:55

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

3条回答
  •  心在旅途
    2021-01-04 03:04

    +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.]
    

提交回复
热议问题