简体   繁体   English

共轭梯度模块中的矩阵函数

[英]Matrix function in conjugate gradient module

I am solving simply linear problem A*x=b by using conjugate gradient method. 我正在使用共轭梯度法简单地解决线性问题A * x = b I want to find x unknown. 我想找到x个未知数。

Note that conjGrad calls the function Av that returns the product Av The code is given below: 需要注意的是conjGrad调用函数大道返回产品大道下面的代码给出:

Inputs: 输入:

  • A - sparse matrix. A-稀疏矩阵。 2D array; 2D阵列
  • b - right hand-side vector. b-右侧向量。 1D array; 一维阵列;
  • x - initial guess. x-初始猜测。 Here, it is just 1D array with zero values. 在这里,它只是一维数组,零值。

Code: 码:

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

Is there any simple solution to avoid this message? 有没有简单的解决方案来避免出现此消息? Any answers will be appreciated 任何答案将不胜感激

You have implemented the CG method wrong. 您实施的CG方法错误。 The error message shows you one of the lines where there is a problem. 错误消息向您显示出现问题的行之一。

In particular, your matrix is not square. 特别是,您的矩阵不是正方形。

The conjugate gradients method solves for Ax=b when A is SPD. A为SPD时,共轭梯度法求解Ax = b

If A is not SPD, like in your case, then you can still use conjugate gradients to find the least squares solution for your problem: 如果像您的情况那样A不是SPD,那么您仍然可以使用共轭梯度来找到问题的最小二乘解:

A^t A x = A^tb A ^ t A x = A ^ tb

The matrix A^t A is SPD and well suited for your method. 矩阵A ^ t A是SPD,非常适合您的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM