def determinant(M):
"""
Finds the determinant of matrix M.
"""
if dimension(M)[0]!=dimension(M)[1]:
print("This matrix is not a square matrix and therefore cannot have a determinant!")
return
elif dimension(M)[0]==dimension(M)[1]:
if dimension(M)==(2,2):
return (M[0][0]*M[1][1])-(M[0][1]*M[1][0])
else:
return (M[0][0]*determinant(reduce_matrix(M,1,1))) - (M[0][1]*determinant(reduce_matrix(M,1,2))) + (M[0][2]*determinant(reduce_matrix(M,1,3)))
РЕДАКТИРОВАТЬ: Этот код способен найти определитель матриц 3x3, но ТОЛЬКО матриц 3x3. Как я могу отредактировать это, чтобы найти определитель квадратной матрицы ЛЮБОГО размера?
[code]def determinant(M): """ Finds the determinant of matrix M. """ if dimension(M)[0]!=dimension(M)[1]: print("This matrix is not a square matrix and therefore cannot have a determinant!") return elif dimension(M)[0]==dimension(M)[1]: if dimension(M)==(2,2): return (M[0][0]*M[1][1])-(M[0][1]*M[1][0]) else: return (M[0][0]*determinant(reduce_matrix(M,1,1))) - (M[0][1]*determinant(reduce_matrix(M,1,2))) + (M[0][2]*determinant(reduce_matrix(M,1,3))) [/code]
РЕДАКТИРОВАТЬ: Этот код способен найти определитель матриц 3x3, но ТОЛЬКО матриц 3x3. Как я могу отредактировать это, чтобы найти определитель квадратной матрицы ЛЮБОГО размера?