from scipy import linalg
import numpy as np
#M_temp代表系数矩阵
x1=10
y1=20
x2=30
y2=40
M_temp =[[x1, y1,1,0],[x2, y2,1,0],[y1,-x1,0,1],[y2,-x2,0,1]]
M_temp=np.array(M_temp)# #一定要转换为矩阵形式# #C为常数列# C=[x1,x2,y1,y2]
C=[100,300,200,400]
C = np.array(C)# b代表常数列
M = linalg.solve(M_temp,C)print('m',M)
import numpy as np
import copy
from numpy.linalg import inv
x1=10
y1=20
x2=30
y2=40
M_temp =[[x1, y1,1,0],[x2, y2,1,0],[y1,-x1,0,1],[y2,-x2,0,1]]
M_temp=np.array(M_temp)
C=[100,300,200,400]
B=[[100,300,200,400],[100,300,200,400]]
C = np.array(C)
B = np.array(B)print(B.shape,(B.T).shape)defdet_calculation(arr):if arr.shape[0]==1:return arr[0][0]
S =0for i inrange(arr.shape[0]):
arr1 = np.delete(arr,0,0)# delete first row
arr1 = np.delete(arr1,i,1)# delete ith column
S +=(-1)**(1+i+1)*arr[0,i]*det_calculation(arr1)# Laplace formulareturn S
defcof(M,index):
result = np.zeros((M.shape[0]-1,M.shape[1]-1))for i inrange(M.shape[0]):
temp = copy.deepcopy(M[i])if i==index[0]-1:continueif i >= index[0]:
Ri = i-1else:
Ri = i
result[Ri]= np.append(temp[:index[1]-1],temp[index[1]:])return det_calculation(result)defalcof(M,index):returnpow(-1,index[0]+index[1])*cof(M,index)defadj(M):
result = np.zeros((M.shape[0],M.shape[1]))for i inrange(1,M.shape[0]+1):for j inrange(1,M.shape[1]+1):
result[j-1][i-1]= alcof(copy.deepcopy(M),[i,j])return result
definvmat(M):if det_calculation(M)!=0:return1/det_calculation(M)*adj(M)else:print('原始矩阵不能为0')
x =invmat((M_temp.T@(M_temp)))@(M_temp.T)@(C.T)print(((M_temp.T)@(C.T)).shape)print('x',x)