高斯消元的Python源码规模排旋转
发表于:2024-11-26 作者:热门IT资讯网编辑
编辑最后更新 2024年11月26日,如下的资料是关于高斯消元的Python规模排旋转的内容,希望能对大家有些好处。''' x = gaussPivot(a,b,tol=1.0e-9). Solves [a]{x} = {b} by
如下的资料是关于高斯消元的Python规模排旋转的内容,希望能对大家有些好处。
''' x = gaussPivot(a,b,tol=1.0e-9). Solves [a]{x} = {b} by Gauss elimination with scaled row pivoting''' from numpy import zeros,argmax,dotimport swap import errordef gaussPivot(a,b,tol=1.0e-12): n = len(b) # Set up scale factors s = zeros(n) for i in range(n): s[i] = max(abs(a[i,:])) for k in range(0,n-1): # Row interchange, if needed p = argmax(abs(a[k:n,k])/s[k:n]) + k if abs(a[p,k]) < tol: error.err('Matrix is singular') if p != k: swap.swapRows(b,k,p) swap.swapRows(s,k,p) swap.swapRows(a,k,p) # Elimination for i in range(k+1,n): if a[i,k] != 0.0: lam = a[i,k]/a[k,k] if abs(a[n-1,n-1]) < tol: error.err('Matrix is singular') # Back substitution b[n-1] = b[n-1]/a[n-1,n-1] for k in range(n-2,-1,-1): b[k] = (b[k] - dot(a[k,k+1:n],b[k+1:n]))/a[k,k] return b