Python Forum
Singular error with numpy.linalg.inv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Singular error with numpy.linalg.inv
#1
Hi there Smile ,

I have a problem with calculating the inverse of a Matrix A. I use the numpy library.

Inverse = numpy.linalg(A)

This worked fine so far. However, there was a problem when I tried to compute the Inverse of the following Matrix:

A [[1778.224561 1123.972526 ]
[1123.972526 710.43571601]]
(this is the output of print('A', A))

The output window stated the error: numpy.linalg.LinAlgError: singular matrix.
If the determinant of a matrix A is zero, the matrix is called a Singular Matrix and the Inverse of A does not exist. But when I calculate the determinant of A with Wolfram Alpha I get the value

det(A) = 0.00001778224561.

If I use the command linalg.det(A) in Python, I get the following output:

Det A 0.0

I assume that the linalg.inv function checks, wether the Inverse of a Matrix exists by calculating the determinant first. But the value seems to be rounded. I tried the scipy library as well but is gives the same error output.

If I am correct with my assumption is there a way to avoid the rounding of the determinate or to use a different function to calculate the Inverse of a matrix?

(I hope this the right thread for this kind of question)
Reply
#2
Everything works fine for me:

In [4]: A = np.array( [[1778.224561, 1123.972526 ], [1123.972526,710.43571601]])

In [5]: np.linalg.det(A)
Out[5]: 1.778225971635503e-05

In [6]: np.linalg.inv(A)
Out[6]:
array([[ 39951936.78093591, -63207519.40014904],
       [-63207519.40014903,  99999920.67175226]])

In [7]: np.linalg.pinv(A)
Out[7]:
array([[ 3.99521351e+07, -6.32078332e+07],
       [-6.32078332e+07,  1.00000417e+08]])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Spyder: ndarray object of numpy module error python_newbie09 6 17,866 Jul-31-2019, 06:21 AM
Last Post: rohit_w

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020