简体   繁体   中英

Matrix function in conjugate gradient module

I am solving simply linear problem A*x=b by using conjugate gradient method. I want to find x unknown.

Note that conjGrad calls the function Av that returns the product Av The code is given below:

Inputs:

  • A - sparse matrix. 2D array;
  • b - right hand-side vector. 1D array;
  • x - initial guess. 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. 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.

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^t A x = A^tb

The matrix A^t A is SPD and well suited for your method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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