import numpy as np
mat_1=[
[2,4,1,54],
[23,42,4,4],
[3,1,4,56],
[6,3,2,6],
]
mat_2=[
[100,2,3],
[4,5,6],
[7,8,9]
]
def det(mat,n):
A=0
if n==1:
A=mat[0][0]
return A
if n==2:
A=mat[0][0]*mat[1][1]-mat[0][1]*mat[1][0]
return A
b=[0 for x in xrange(n) ]
t1=[ [0 for x in xrange(n)] for y in xrange(n)]
for i in xrange(n):
b[i]=mat[i][0]*(-1)**(i+2)
for i in xrange(n):
for j in xrange(n-1):
t1[i][j]=mat[i][j+1]
for i in xrange(n):
t2 = [[0 for x in xrange(n-1)] for y in xrange(n-1)]
for j in xrange(n-1):
for k in xrange(n-1):
if j<i:
t2[j][k]=t1[j][k]
else:
t2[j][k]=t1[j+1][k]
A += b[i]*det(t2,n-1)
return A
print det(mat_1,len(mat_1))
print np.linalg.det(mat_1)
求矩阵的行列式
最新推荐文章于 2023-05-15 23:28:34 发布