# Gauss消元法(特解与通解)

#### (3)$(3)$当方程的解只有特解时,得到的是只关于常数的一系列多项式。通解是关于后n−R(A)$n-R(A)$个未知数的线性组合;


def gauss(a):
ep = 0.1**(15)
n = len(a)
num = len(a[0])
array = []
for i in range(0,n):
p = -1
for k in range(i,n):
if abs(a[k][i])>ep:p=k;break
if p==-1:continue
a[i],a[p] = a[p],a[i]
if a[i]!=[0]*num:array.append(a[i])
x = a[i][i]
for j in range(i+1,n):
if abs(a[j][i])<ep:continue
k = -a[j][i]/(x)
for m in range(i,num):a[j][m]+= k*a[i][m]
a = array;n = len(a);num = len(a[0])
for index in range(0,num-1):
if a[-1][index]!=0:break
last = deque()
for k in range(0,num-index-2):last.append([1 if m==k else 0 for m in range(num-index-2)]+[0])
for i in range(n-1,-1,-1):
sum_1 = [0]*(num-index-2)
constant = 0
for item in zip(a[i][i+1:num-1],last):
constant += item[0]*item[1][-1]#compute the constant
for ind in range(len(item[1])-1):
sum_1[ind] -= item[0]*item[1][ind]/a[i][i]
sum_1.append((a[i][-1]-constant)/a[i][i])
last.appendleft(sum_1)
print(a,last)
#gauss([[1,1,2,3,3],[2,1,2,4,5],[1,1,4,5,3],[1,3,7,4,6]])


• 本文已收录于以下专栏：

举报原因： 您举报文章：Gauss消元法(特解与通解) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)