Python 求矩阵误差的行列式
我不断地遇到这个错误,我不知道出了什么问题Python 求矩阵误差的行列式,python,matrix,Python,Matrix,我不断地遇到这个错误,我不知道出了什么问题 line 81, in determinant determinant += newMatrix[0][x] * (-1)**(2+x) * determinant(reduce_matrix(newMatrix,1,x+1)) TypeError: 'int' object is not callable 以下是两个功能的代码: def reduce_matrix(M, i, j): newMatrix= [[x for x in
line 81, in determinant
determinant += newMatrix[0][x] * (-1)**(2+x) * determinant(reduce_matrix(newMatrix,1,x+1))
TypeError: 'int' object is not callable
以下是两个功能的代码:
def reduce_matrix(M, i, j):
newMatrix= [[x for x in y] for y in M]
try:
for k in range(len(newMatrix)):
del(newMatrix[k][j-1])
newMatrix.remove(newMatrix[i-1])
return newMatrix
except:
print ("Row or column not in matrix")
return M
def determinant(M):
newMatrix= [[x for x in y] for y in M]
if len(newMatrix) != len((newMatrix)[0]):
print ("Not a square matrix")
if (dimension(newMatrix))==(1,1):
return newMatrix
else:
determinant = 0
for x in range(len(newMatrix)):
determinant += newMatrix[0][x] * (-1)**(2+x) * determinant(reduce_matrix(newMatrix,1,x+1))
return determinant
您正在使用
行列式
作为函数名和该函数中的局部变量。坏主意,因为局部变量行列式
会隐藏函数名,并且您正试图递归调用函数
当Python解释器看到行列式(reduce_matrix(newMatrix,1,x+1))
时,它知道的唯一无阴影的行列式
是您在为其赋值0
时创建的int
,因此错误消息有些神秘
对局部变量使用其他变量,例如det
:
det = 0
#rest of code, suitably adjusted
numpy.linalg.det内置函数