Iva*_*van 6 python numpy numeric scipy
是否有一个线性代数库可以实现迭代 Gauss-Seidel 来求解线性系统?或者也许是一个预条件梯度求解器?
谢谢
编辑:最后我使用了一种粗略但正确的方法来解决它。因为无论如何我都必须创建矩阵 A(对于 Ax=b),所以我将矩阵划分为
A = M - N
Run Code Online (Sandbox Code Playgroud)
和
M = (D + L) and N = -U
Run Code Online (Sandbox Code Playgroud)
其中 D 是对角线,L 是下三角部分,U 是上三角部分。然后
Pinv = scipy.linalg.inv(M)
x_k_1 = np.dot(Pinv,np.dot(N,x_k)) + np.dot(Pinv,b)
Run Code Online (Sandbox Code Playgroud)
还做了一些收敛测试。有用。
小智 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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20856 次 |
| 最近记录: |