是否有线性代数库实现迭代Gauss-Seidel来求解线性系统?或者也许是一个预处理的梯度求解器?
谢谢
编辑:最后我使用了一种原始但正确的方法来解决它。因为我必须创建矩阵A(对于Ax = b),我将矩阵分区为
A = M - N
带
M = (D + L) and N = -U
其中D是对角线,L是下三角形截面,U是上三角形截面。然后
Pinv = scipy.linalg.inv(M)
x_k_1 = np.dot(Pinv,np.dot(N,x_k)) + np.dot(Pinv,b)
也进行了一些收敛测试。它有效。
答案 0 :(得分:3)
有一个示例实现,它们在这里进行各种实现:
http://www.scipy.org/PerformancePython
另一个例子:
http://www.dur.ac.uk/physics.astrolab/py_source/kiusalaas/v1_with_numpy/gaussSeidel.py
您可能还想查看petsc
的
答案 1 :(得分:1)
我知道这是旧的,但是,我还没有在 python 中找到任何用于 gauss - seidel 的预先存在的库。相反,我创建了自己的小函数,在我的另一个答案中看到的置换矩阵的帮助下 permutation matrix 将为任何方阵生成解(x 向量),包括对角线上为零的方阵。>
def gauss_seidel(A, b, tolerance, max_iterations, x):
#x is the initial condition
iter1 = 0
#Iterate
for k in range(max_iterations):
iter1 = iter1 + 1
print ("The solution vector in iteration", iter1, "is:", x)
x_old = x.copy()
#Loop over rows
for i in range(A.shape[0]):
x[i] = (b[i] - np.dot(A[i,:i], x[:i]) - np.dot(A[i,(i+1):], x_old[(i+1):])) / A[i ,i]
#Stop condition
#LnormInf corresponds to the absolute value of the greatest element of the vector.
LnormInf = max(abs((x - x_old)))/max(abs(x_old))
print ("The L infinity norm in iteration", iter1,"is:", LnormInf)
if LnormInf < tolerance:
break
return x
经过重新搜索,我发现以下可以用作现成的包,但我自己没有使用过numerica PyPI
答案 2 :(得分:0)
from sage.all import *
a=matrix(QQ,[[12,3,-5],[1,5,3],[3,7,13]])
b=matrix(QQ,[[1],[28],[76]])
x=[]
r=len(a[0])
i=0
while(i<r):
li=raw_input('give approximate solution :')
h=eval(li)
x.append(h)
i=i+1
def gausseidel():
tol=0.00001;l=0;itern=10
fnd=0
while(l<itern and fnd==0):
j=0
while(j<r):
temp=b[j,0]
k=0
while(k<r):
if (j!=k):
temp-=a[j,k]*x[k]
k=k+1
c=temp/a[j,j]
if (abs(x[j]-c)<=tol):
fnd=1
break
x[j]=c
j=j+1
l=l+1
if l==itern:
print "Iterations are over"
for p in range(r):
print 'x',p,'=',float(x[p])
gausseidel()