当前位置: 代码迷 >> 综合 >> Algorithm :Gaussian Elimination for Matrix
  详细解决方案

Algorithm :Gaussian Elimination for Matrix

热度:58   发布时间:2024-01-20 02:07:45.0

最近在review linear Algebra, 高斯消元法来求梯形矩阵感觉很有用。就自己写了一个代码,实现如下:

 

by python:

# by sesiria 2019
# algorithm for Gaussian Elimination for square matrix.
import numpy as npclass MatrixIsSingular(Exception): pass# assume the input is a square matrix with the dimension m x n
# return the echelon-form matrix of the input
# otherwise throw a 'MatrixIsSingular' exception.
def GaussianElimination(A):B = np.array(A, dtype=np.float_) # Make B as a copy of A, since we're going to alter it's values.# m = rows# n = colsm, n = A.shapeif (m != n):raise MatrixIsSingular# try to fix each rowfor row in range(m):# set all elements of the current rwo to 0 until the diagonal element.fixPivot(B, row)next = row + 1while(B[row, row] == 0 and next < m):if (B[next,