Why are the inverse results not equal?

393 Views Asked by At

I mat a problem when solving inverse of a matrix. Firstly, I use python numpy library to make it, by coding below:

import numpy as np
mtx_str = '1 0.05336904  1.03164031  0.05505765;1 0.05248641  3.0928260 0.16233134;1 2.16503202  1.03197617  2.23426146;1 0.05347855 -1.02633768 -0.05488705'
A = np.matrix(mtx_str)
np.rank(A)

it return 2; but if I use octave software by entering:

  A = [1 0.05336904 1.03164031 0.05505765; 1 0.05248641 3.09282607 0.16233134; 1 2.16503202 1.03197617 2.23426146; 1 0.05347855 -1.02633768 -0.05488705]
inv(A)

it return 4.

I wonder why the inverse result is different?

2

There are 2 best solutions below

0
On

Although I doubt this question belongs here

numpy.rank(A) -> Gives number of dimensions of $A$ (1 for array or 2 for matrix or 3 for 3D array etc.)

the function you need to use is numpy.linalg.matrix_rank(A) for mathematical rank.

And in second code it should be rank(A)

0
On

You are using numpy wrong. The rank function returns the "tensor rank" of the array object, since your matrix is a 2D array, it returns 2. The function you are looking for is shown below.

In [1]: import numpy as np

In [2]: mtx_str = '1 0.05336904  1.03164031  0.05505765;1 0.05248641  3.0928260 0.16233134;1 2.16503202  1.03197617  2.23426146;1 0.05347855 -1.02633768 -0.05488705'

In [3]: A = np.matrix(mtx_str)

In [4]: np.linalg.matrix_rank(A)
Out[4]: 4