内部函数改变外部函数的变量值
inner function changing the variable value of outer function
def swap(i,r,c,mat):
for j in range(i+1,c):
if(abs(mat[j][j])>0):
mat[[j,i]] = mat[[i,j]]
break
return mat
def upper_triMat(matA,r,c):
np.set_printoptions(precision=4)
# forward elimination
for i in range(0,c-1):
if matA[i][i] == 0:
matA = swap(i,r,c,matA)
for j in range(i+1,r):
multiplier = matA[j][i]/matA[i][i]
for k in range(0,c):
matA[j][k] = matA[j][k] - multiplier*matA[i][k]
return matA
def dolittle(A):
A = np.array(A)
r,c = np.shape(A)
print(A)
U = upper_triMat(A,r,c) # Here the value of A is changed U.
print(A)
l = np.eye(r,c)
for i in range(0,r-1):
for j in range(i+1,r):
sum = 0
for k in range(0,r):
if i != k:
sum = sum + U[k][i]*l[j][k]
l[j][i] = (A[j][i]-sum)/U[i][i]
return l,U
A = [[3,-0.1,-0.2],
[0.1,7,-0.3],
[0.3,-0.2,10]]
dolittle(A)
当我调用 upper_triMat 函数“A”时 dolittle 功能。为什么?? A 是 A 并且 upper_triMat 函数将其分配给 U 。但是 A 也得到了 U 的值。使用 Jupyter 笔记本。我在做 LU 分解
upper_triMat 改变其参数 matA。由于 matA 是对 A 的引用,因此它正在被修改。
也许你可以这样解决
U = upper_triMat(A.copy(),r,c) # pass a copy of the list instead of the reference of the original one.
def swap(i,r,c,mat):
for j in range(i+1,c):
if(abs(mat[j][j])>0):
mat[[j,i]] = mat[[i,j]]
break
return mat
def upper_triMat(matA,r,c):
np.set_printoptions(precision=4)
# forward elimination
for i in range(0,c-1):
if matA[i][i] == 0:
matA = swap(i,r,c,matA)
for j in range(i+1,r):
multiplier = matA[j][i]/matA[i][i]
for k in range(0,c):
matA[j][k] = matA[j][k] - multiplier*matA[i][k]
return matA
def dolittle(A):
A = np.array(A)
r,c = np.shape(A)
print(A)
U = upper_triMat(A,r,c) # Here the value of A is changed U.
print(A)
l = np.eye(r,c)
for i in range(0,r-1):
for j in range(i+1,r):
sum = 0
for k in range(0,r):
if i != k:
sum = sum + U[k][i]*l[j][k]
l[j][i] = (A[j][i]-sum)/U[i][i]
return l,U
A = [[3,-0.1,-0.2],
[0.1,7,-0.3],
[0.3,-0.2,10]]
dolittle(A)
当我调用 upper_triMat 函数“A”时 dolittle 功能。为什么?? A 是 A 并且 upper_triMat 函数将其分配给 U 。但是 A 也得到了 U 的值。使用 Jupyter 笔记本。我在做 LU 分解
upper_triMat 改变其参数 matA。由于 matA 是对 A 的引用,因此它正在被修改。 也许你可以这样解决
U = upper_triMat(A.copy(),r,c) # pass a copy of the list instead of the reference of the original one.