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

Happyman bahtiyor_zohidov at mail.ru
Wed Sep 18 16:19:34 EDT 2013

``` I understand ,sometimes, it is normal that number of equations are less or more than number of unknowns that means non square matrix appearance. If so, how  would you compose Av(v) function, because I have only constant dimensional values such as A matrix(MxN) and array b array (M)???

Среда, 18 сентября 2013, 11:50 -07:00 от "Bradley M. Froehle" <brad.froehle at gmail.com>:
>In [2]: %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...
>> --
>> happy man
>>
>> _______________________________________________
>> NumPy-Discussion mailing list
>>  NumPy-Discussion at scipy.org
>>  http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>_______________________________________________
>NumPy-Discussion mailing list
>NumPy-Discussion at scipy.org
>http://mail.scipy.org/mailman/listinfo/numpy-discussion

--
happy Man
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130919/5bdd54a8/attachment.html>
```