我有这段代码:
import scipy
import scipy.linalg
def LU(A):
n = scipy.shape(A)[0]
U = A
L = scipy.identity(n)
for j in range(1,n):
for i in range(j+1,n+1):
L[i-1,j-1] = U[i-1,j-1]/U[j-1,j-1]
for k in range(j,n+1):
U[i-1,k-1] = U[i-1,k-1] - L[i-1,j-1] * U[j-1,k-1]
return L,U
a = scipy.matrix([[1.0,1.0],[2.0,1.0]])
L,U = LU(a)
print L
print U
print a
print scipy.dot(L,U)
如何在LU()中通过引用修复不需要的传递,其中U指向A所在的位置,而更改为U也会更改A.
答案 0 :(得分:1)
您可以通过立即复制来阻止对A
的修改:
import copy
def LU(A):
n = scipy.shape(A)[0]
U = copy.deepcopy(A)
L = scipy.identity(n)
# ...