我正在使用共轭梯度法解决简单的线性问题A*x=b 。我想找到x未知。
注意 conjGrad 调用函数Av返回乘积 Av 代码如下:
输入:
- A - 稀疏矩阵。二维数组;
- b - 右手边向量。一维数组;
- x - 初始猜测。在这里,它只是一个零值的一维数组。
代码:
import numpy as np
import math
A = np.array([[ 0.56244579, 0. , 0. , 0. , 0.52936075,
0.59553084, 0. , 0. , 0. , 1.1248915 ,
0. , 0. , 0. , 0.46319065, 0.43672262,
0. ],
[ 0.5 , 1. , 1. , 0.5 , 0. ,
0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. ,
0. ],
[ 0. , 0. , 0. , 0.58009067, 0. ,
0. , 0.75411788, 0.40606347, 0. , 0. ,
0.23203627, 0. , 0. , 0. , 0. ,
0. ]])
x = np.array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0.])
b = np.array([ 3.99464617, 1.81663614, 1.86413003])
def Av(v):
return np.dot(A,v)
def conjGrad(Av, x, b, tol=1.0e-9):
n = len(b)
r = b - Av(x)
s = r.copy()
for i in range(n):
u = Av(s)
alpha = np.dot(s,r)/np.dot(s,u)
x = x + aplha*s
r = b - Av(x)
if(math.sqrt(np.dot(r,r))) < tol:
break
else:
beta = - np.dot(r,u)/np.dot(s,u)
s = r + beta * s
return x,i
if __name__ == '__main__':
x, iter_number = conjGrad(Av, x, b)
Traceback (most recent call last):
File "C:\Python27\Conjugate_Gradient.py", line 59, in <module>
x, iter_number = conjGrad(Av, x, b)
File "C:\Python27\Conjugate_Gradient.py", line 47, in conjGrad
u = Av(s)
File "C:\Python27\Conjugate_Gradient.py", line 40, in Av
return np.dot(A,v)
ValueError: matrices are not aligned
是否有任何简单的解决方案来避免此消息?任何答案将不胜感激