2021-07-27

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)
def det_calculation(arr):
    if arr.shape[0] == 1:
        return arr[0][0]
    S = 0
    for i in range(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 formula
    return S
def cof(M,index):
    result = np.zeros((M.shape[0]-1,M.shape[1]-1))
    for i in range(M.shape[0]):
        temp = copy.deepcopy(M[i])
        if i==index[0]-1:
            continue
        if i >= index[0]:
            Ri = i-1
        else:
            Ri = i
        result[Ri] = np.append(temp[:index[1]-1],temp[index[1]:])
    return det_calculation(result)

def alcof(M,index):
    return pow(-1,index[0]+index[1])*cof(M,index)
def adj(M):
    result = np.zeros((M.shape[0],M.shape[1]))
    for i in range(1,M.shape[0]+1):
        for j in range(1,M.shape[1]+1):
            result[j-1][i-1] = alcof(copy.deepcopy(M),[i,j])
    return result

def invmat(M):
    if det_calculation(M)!=0:
    	return 1/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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值