Error in linalg.inv ??

Nick Craig-Wood nick at craig-wood.com
Sat Jun 6 03:29:40 EDT 2009


Ajith Kumar <ajith at iuac.res.in> wrote:
>   I ran the following code (Using Debian 5.0)
> 
>  from numpy import *
>  a = arange(1.,10.)
>  b = reshape(a, [3,3])
>  c = linalg.inv(b)
>  print b
>  print c
>  print dot(b,c)
>  print dot(c,b)
> 
>  And the result is
> 
>  [[ 1.  2.  3.]
>   [ 4.  5.  6.]
>   [ 7.  8.  9.]]
> 
>  [[  3.15221191e+15  -6.30442381e+15   3.15221191e+15]
>   [ -6.30442381e+15   1.26088476e+16  -6.30442381e+15]
>   [  3.15221191e+15  -6.30442381e+15   3.15221191e+15]]
> 
>  [[-0.5 -1.  -1. ]
>   [-1.  -2.   2. ]
>   [-1.5 -3.   1. ]]
> 
>  [[  5.5   8.   10.5]
>   [  3.    0.   -3. ]
>   [ -1.    0.   -3. ]]
> 
>  NOT the identity matrix. Any help ?

The matrix you are trying to invert is singular (can't be inverted),
ie its determinant is zero.

>> a = arange(1.,10.)
>>> b = reshape(a, [3,3])
>>> linalg.det(b)
-9.5171266700777579e-16
>>>

Which is zero but with a bit of rounding errors which I guess numpy
doesn't notice.

Double checking like this

>>> a,b,c,d,e,f,g,h,i=range(1,10)
>>> a*e*i - a*f*h - b*d*i + b*f*g + c*d*h - c*e*g
0
>>>

So I guess it is a bug that numpy didn't throw

numpy.linalg.linalg.LinAlgError("Singular matrix")

Like it does normally

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list