Mannix
Mannix

Reputation: 431

Python numpy matrix inverse gives incorrect values

What I am getting:

>>> Pp
matrix([[ 0.01011  ,  0.0050535,  0.0010005],
        [ 0.0050535,  0.002526 ,  0.0005001],
        [ 0.0010005,  0.0005001,  0.0001   ]])
>>> Pp.I
matrix([[  4.73894021e+17,  -9.47740572e+17,  -1.65931645e+15],
        [ -9.47740572e+17,   1.89538621e+18,   3.31846669e+15],
        [ -1.65931645e+15,   3.31846669e+15,   5.81001542e+12]])

What I think I should be getting:

matrix([[ -1.11110667e+09,   2.22220000e+09,   3.40000000e+06],
        [  2.22220000e+09,  -4.44433334e+09,  -7.00000001e+06],
        [  3.40000000e+06,  -7.00000001e+06,   1.00000000e+06]])

Am I using the inverse incorrectly?

By the way, this is incorrect, too:

>>> np.linalg.inv(Pp)
matrix([[  4.73894021e+17,  -9.47740572e+17,  -1.65931645e+15],
        [ -9.47740572e+17,   1.89538621e+18,   3.31846669e+15],
        [ -1.65931645e+15,   3.31846669e+15,   5.81001542e+12]])

I saw another post about this, but it had no clear solution.

Upvotes: 1

Views: 2064

Answers (3)

Mannix
Mannix

Reputation: 431

I made a goof in defining the matrix.

I defined it as such:

Pp=np.matrix([(x11,x12,x13),(x21,x22,x23),(x31,x32,x33)])

instead of the correct way, which is:

Pp=np.matrix([[x11,x12,x13],[x21,x22,x23],[x31,x32,x33]])

Interesting that everything pretty much worked correctly except the inverse.

Upvotes: 0

meph
meph

Reputation: 692

First look at the determinant of the matrix with np.linalg.det(m). The result is -2.249999999256419e-18 which is close to 0.
In theory you could say that this matrix is invertible, but because of the high condition number (use np.linalg.cond(m)) you might get very bad results.
Try constructing your matrix like this:

m = np.matrix([[ 0.01011  ,  0.0050535,  0.0010005],
        [ 0.0050535,  0.002526 ,  0.0005001],
        [ 0.0010005,  0.0005001,  0.0001   ]], dtype=np.float64)

This indeed will get better results for m.I:

matrix([[-1.11110667e+09,  2.22220000e+09,  3.40000000e+06],
        [ 2.22220000e+09, -4.44433333e+09, -7.00000000e+06],
        [ 3.40000000e+06, -7.00000000e+06,  1.00000000e+06]])

Checking results with m * m.I:

matrix([[ 1.00000000e+00,  8.19379276e-10,  6.10730759e-12],
        [-1.18821050e-10,  1.00000000e+00,  3.87522234e-12],
        [ 1.48254137e-11, -6.72848576e-11,  1.00000000e+00]])

Upvotes: 0

hpaulj
hpaulj

Reputation: 231655

With a simple copy-n-paste I get the correct value - both ways:

In [82]: [[ 0.01011  ,  0.0050535,  0.0010005], 
    ...:         [ 0.0050535,  0.002526 ,  0.0005001], 
    ...:         [ 0.0010005,  0.0005001,  0.0001   ]]                                                    
Out[82]: 
[[0.01011, 0.0050535, 0.0010005],
 [0.0050535, 0.002526, 0.0005001],
 [0.0010005, 0.0005001, 0.0001]]
In [83]: np.linalg.inv(_)                                                                                 
Out[83]: 
array([[-1.11110667e+09,  2.22220000e+09,  3.40000000e+06],
       [ 2.22220000e+09, -4.44433333e+09, -7.00000000e+06],
       [ 3.40000000e+06, -7.00000000e+06,  1.00000000e+06]])
In [84]: mm = np.matrix(_82)                                                                              
In [85]: mm.I                                                                                             
Out[85]: 
matrix([[-1.11110667e+09,  2.22220000e+09,  3.40000000e+06],
        [ 2.22220000e+09, -4.44433333e+09, -7.00000000e+06],
        [ 3.40000000e+06, -7.00000000e+06,  1.00000000e+06]])

Try your Pp in a clean session; something's wrong your session. Or the Pp you show isn't the one that you are actually using.

Upvotes: 1

Related Questions