我正在尝试使用递归函数来计算矩阵的行列式。 我有一个具有 getitem , setitem ,...的Matrix类。.eliminated(i,j)方法将返回一个新的矩阵,该矩阵将从第i行,第j列中删除。
这是Matrix类,我之前也创建了Array,Array_2D类,它们都很好:
class Matrix:
def __init__(self, numRows, numCols):
self.grid = Array_2D(numRows,numCols)
self.grid.clear(0)
def __getitem__(self, tuple):
return self.grid[tuple[0],tuple[1]]
def numRows(self):
return self.grid.numRows()
def numCols(self):
return self.grid.numCols()
def __add__(self, other):
assert other.numRows() == self.numRows() and\
other.numCols == self.numCols() , "Matrix sizes not compatible"
newMatrix = Matrix(self.numRows(),self.numCols())
for r in range(self.numRows()):
for c in range(self.numCols()):
newMatrix[r,c]=self[r,c]+other[r,c]
return newMatrix
def __setitem__(self, tuple, value):
self.grid[tuple[0],tuple[1]]=value
def scaleBy(self,scalar):
for r in range(self.numRows()):
for c in range(self.numCols()):
self[r,c]=self[r,c]*scalar
def tranpose(self):
newMatrix = Matrix(self.numCols(),self.numRows())
for r in range(newMatrix.numRows()):
for c in range(newMatrix.numCols()):
newMatrix[r,c]=self[c,r]
return newMatrix
def __sub__(self, other):
assert other.numRows() == self.numRows() and \
other.numCols == self.numCols(), "Matrix sizes not compatible"
newMatrix = Matrix(self.numRows(), self.numCols())
for r in range(self.numRows()):
for c in range(self.numCols()):
newMatrix[r, c] = self[r, c] - other[r, c]
return newMatrix
def __mul__(self, other):
assert other.numRows() == self.numCols(), "Matrix sizes not compatible"
newMatrix = Matrix(self.numRows(),other.numCols())
for r in range(newMatrix.numRows()):
for c in range(newMatrix.numCols()):
newMatrix[r,c] = 0
for i in range(self.numCols()):
newMatrix[r,c]+=self[r,i]*other[i,c]
return newMatrix
def determinant(self):
assert self.numCols()==self.numRows(), "Must be a square matrix"
assert self.numCols() > 0
if self.numCols() == 1:
return self[0,0]
if self.numCols() == 2:
return self[0,0]*self[1,1]-self[0,1]*self[1,0]
det = 0
if self.numCols() >=2:
for c in range(self.numCols()):
det+=((-1) ** c) * self[0, c] * self.eliminated(0, c).determinant()
return det
def eliminated(self,row,col):
assert row >=0 and row < self.numRows(), "Invalid row"
assert col >=0 and col < self.numCols(), "Invalid column"
assert self.numCols() >1 and self.numRows()>1
entry_list = []
for r in range(self.numRows()):
for c in range(self.numCols()):
self[r, col] = None
self[row,c]=None
if self[r,c] != None:
entry_list.append(self[r,c])
new_matrix = Matrix(self.numRows()-1, self.numCols()-1)
for r in range(new_matrix.numRows()):
for c in range(new_matrix.numCols()):
new_matrix[r,c] = entry_list[c + r*new_matrix.numCols()]
return new_matrix
对于3x3矩阵,我一直收到此错误,2x2和1x1很好:
Traceback (most recent call last):
File "E:/DataStructures/matrix.py", line 100, in <module>
print(ma4.determinant())
File "E:/DataStructures/matrix.py", line 67, in determinant
det+=((-1) ** c) * self[0, c] * self.eliminated(0, c).determinant()
TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
我知道我的递归函数会一直删除行和列,直到到达NoneType为止,但是不知道如何解决
答案 0 :(得分:0)
您好,请查看以下内容:
matrix=[]
def det(IC,i):
global matrix,row
determinent=0
m=1
for j in range(len(matrix)):
if j not in IC:
m+=1
if i == row-1:
determinent = matrix[i][j]
else:
determinent+=(-1)**(m)*matrix[i][j]*det(IC+[j],i+1)
return determinent
row=int(input("enter order:"))
for i in range(row):
rowEntry=[int(x) for x in input().split(' ')]
matrix.append(rowEntry)
print(matrix)
print(det([],0))
请仔细输入信息,例如: 输入订单:3
2 3 4
1 2 3
3 4 5