用于 Gauss-Seidel 迭代求解器的 Python 库?

标签 python numpy scipy numeric

有没有线性代数库实现迭代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/

相关文章:

python - 使用 Tensorflow Serving 和 SavedModel Estimators 获取模型解释

python - 在 python 虚拟环境中使用 pip 安装 Pandas 时出现“bz2 模块不可用”

numpy - 多边形内的点

python - Theano 中的逐元素矩阵乘法

c - 在 numpy vector 上使用标量 C 函数

Python统计包: difference between statsmodel and scipy. stats

python - 如何让我的程序根据 key.is_pressed 语句播放声音?

python - 如何启用 MySQL 客户端自动重新连接 MySQLdb?

python - 如何在 scipy 中集成一个 numpy 数组?

python - Jupyter Lab交互式绘图: unsupported operand type(s) for *: 'FloatSlider' and 'float'