# [Numpy-discussion] ValueError: matrices are not aligned!!!

Wed Sep 18 14:50:19 EDT 2013

```In : %debug
> <ipython-input-1-2084f8a2223e>(5)Av()
4 def Av(A,v):
----> 5      return np.dot(A,v)
6

ipdb> !A.shape
(4, 8)
ipdb> !v.shape
(4,)

In your code it looks like you are essentially computing A.dot(v)
where A is a 4-by-8 matrix and v is vector of length 4.  That's what
the error is telling you --- that the matrix and vector have
incompatible dimensions.

I've never seen the conjugate gradient method used on non-square
matrices... are you sure this is what you want to be doing?

On Wed, Sep 18, 2013 at 11:11 AM, Happyman <bahtiyor_zohidov at mail.ru> wrote:
> Hello,
>
> I am trying to solve linear Ax=b problem, but some error occurs in the
> process like:
> ----------------------------------------------
>
> Traceback (most recent call last):
> File "C:\Python27\Conjugate_Gradient.py", line 66, in <module>
> x, iter_number = conjGrad(Av,A, x, b)
> u = Av(A,s)
> File "C:\Python27\Conjugate_Gradient.py", line 41, in Av
> return np.dot(A,v)
> ValueError: matrices are not aligned
> -----------------------------------------------
>
> I put the code below to check it.
>
> import numpy as np
> import math
>
> def Av(A,v):
>      return np.dot(A,v)
>
> def conjGrad(Av,A, x, b, tol=1.0e-9):
>     n = len(x)
>     r = b - Av(A,x)
>     s = r.copy()
>     for i in range(n):
>          u = Av(A,s)
>          alpha = np.dot(s,r)/np.dot(s,u)
>          x = x + aplha*s
>          r = b - Av(A,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__':
>      # Input values
>      A = np.arange(32, dtype=float).reshape((4,8))
>      x = np.zeros(8)
>      b = np.array([2.5, 4.5, 6.5, 8.0])
>      x, iter_number = conjGrad(Av,A, x, b)
>
> I would appreciate any solution to this problem...