有没有线性代数库实现迭代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)
还做了一些收敛测试。它有效。
最佳答案
我知道这是旧的但是,我还没有在 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
关于用于 Gauss-Seidel 迭代求解器的 Python 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5622656/