高斯-赛德尔迭代求解器的 Python 库?

发布于 2024-10-31 11:22:26 字数 404 浏览 1 评论 0原文

是否有一个线性代数库可以实现迭代 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)

还做了一些收敛测试。有用。

Is there a linear algebra library that implements iterative Gauss-Seidel to solve linear systems? Or maybe a preconditioned gradient solver?

Thanks

EDIT: In the end I used a kind of crude but correct way to solve it. As i had to create the matrix A (for Ax=b) anyway, I partitioned the matrix as

A = M - N

with

 M = (D + L) and N = -U

where D is the diagonal, L is the lower triangular section, and U the upper triangular section. Then

Pinv = scipy.linalg.inv(M)
x_k_1 = np.dot(Pinv,np.dot(N,x_k)) + np.dot(Pinv,b)

Also did some convergence tests. It works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮云落日 2024-11-07 11:22:26

我知道这已经很旧了,但是我还没有在 python 中找到任何预先存在的 gauss - seidel 库。相反,我在排列矩阵的帮助下创建了自己的小函数,如我的另一个答案置换矩阵将产生任何方阵的解(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

I know this is old but, I haven't found any pre existing library in python for gauss - seidel. Instead I created my own little function that with the help of a permutation matrix as seen in another answer of mine permutation matrix will produce the solution (x vector) for any square matrix, including those with zeros on the diagonal.

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

After re searching I have found the following that could be used as a ready to go package, but haven't used it myself numerica PyPI

梦纸 2024-11-07 11:22:26
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()
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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文