from .Matrix import Matrix from .Vector import Vector
classLinearSystem: def__init__(self, A, b): assert A.row_num() == len(b),\ "row number of A must be equal to the length of b" self._m = A.row_num() self._n = A.col_num()
self.Ab = [Vector(A.row_vector(i).underlying_list() + [b[i]]) for i inrange(self._m)] def_max_row(self, index, n): best, ret= self.Ab[index][index], index for i inrange(index+1, n): ifself.Ab[i][index] > best: best, ret = self.Ab[i][index], i return ret
def_forward(self): '''前向过程:化成上三角矩阵''' n = self._m for i inrange(n): #Ab[i][i]为主元 max_row = self._max_row(i, n) self.Ab[i], self.Ab[max_row] = self.Ab[max_row], self.Ab[i]
self.Ab[i] = self.Ab[i]/self.Ab[i][i] for j inrange(i+1, n): self.Ab[j] = self.Ab[j] - self.Ab[j][i]*self.Ab[i] def_backward(self): '''后向过程:化成行最简阶梯形''' n = self._m for i inrange(n-1, 0, -1): for j inrange(i-1, 0, -1): self.Ab[j] = self.Ab[j] - self.Ab[j][i]*self.Ab[i]
defgauss_jordan_elimination(self):
self._forward() # 前向过程 self._backward() # 后向过程
deffancy_print(self): for i inrange(self._m): print(" ".join(str(self.Ab[i][j])for j inrange(self._n)),end=" ") print("|", self.Ab[i][-1])
测试代码:
1 2 3 4 5 6 7 8 9 10 11 12 13
from LA.LinearSystem import LinearSystem from LA.Matrix import Matrix from LA.Vector import Vector
if __name__ == "__main__": A = Matrix([[1,2,4],[3,7,2],[2,3,3]]) b = Vector([7,-11,1])
ls = LinearSystem(A, b) ls.fancy_print() ls.gauss_jordan_elimination() ls.fancy_print()
from .Matrix import Matrix from .Vector import Vector from ._globalimport is_zero
classLinearSystem: def__init__(self, A, b): assert A.row_num() == len(b),\ "row number of A must be equal to the length of b" self._m = A.row_num() self._n = A.col_num()
self.Ab = [Vector(A.row_vector(i).underlying_list() + [b[i]]) for i inrange(self._m)] self.pivots = [] def_max_row(self, index_i, index_j, n): best, ret= self.Ab[index_i][index_j], index_i for i inrange(index_i+1, n): ifself.Ab[i][index_j] > best: best, ret = self.Ab[i][index_j], i return ret
def_forward(self): i, k = 0, 0 while i < self._m and k < self._n: # 看Ab[i][k]是否为主元 max_row = self._max_row(i, k, self._m) self.Ab[i], self.Ab[max_row] = self.Ab[max_row], self.Ab[i]
if is_zero(self.Ab[i][k]) : k += 1 else: self.Ab[i] = self.Ab[i]/self.Ab[i][k] for j inrange(i+1, self._m): self.Ab[j] = self.Ab[j] - self.Ab[j][k]*self.Ab[i] self.pivots.append(k) i+=1 def_backward(self): n = len(self.pivots) for i inrange(n-1, -1, -1): k = self.pivots(i) for j inrange(i-1, -1, -1): self.Ab[j] = self.Ab[j] - self.Ab[j][k]*self.Ab[i]
defgauss_jordan_elimination(self):
self._forward() self._backward()
deffancy_print(self): for i inrange(self._m): print(" ".join(str(self.Ab[i][j])for j inrange(self._n)),end=" ") print("|", self.Ab[i][-1])
测试代码,测试用例就是上面的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13
from LA.LinearSystem import LinearSystem from LA.Matrix import Matrix from LA.Vector import Vector
if __name__ == "__main__": A = Matrix([[1,-1,2,0,3],[-1,1,0,2,-5],[1,-1,4,2,4],[-2,2,-5,-1,-3]]) b = Vector([1,5,13,-1])
ls = LinearSystem(A, b) ls.fancy_print() ls.gauss_jordan_elimination() ls.fancy_print()