Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy.linalg.inv returns inverse for a singular matrix

The matrix below is singular, and AFAIK attempting to invert it should result in

numpy.linalg.linalg.LinAlgError: Singular matrix

but instead, I do get some output matrix. Note that output matrix is a non-sensical result, because it has a row of 0's (which is impossible, since an inverse of a matrix should itself be invertible)!

Am I missing something here related to floating point precision, or the computation of a pseudoinverse as opposed to a true inverse?

$ np.__version__ 
'1.13.1'
$ np.linalg.inv(np.array([[2,7,7],[7,7,7],[8,7,7]]))
array([[  0.00000000e+00,   0.00000000e+00,   0.00000000e+00],
       [  3.43131400e+15,  -2.05878840e+16,   1.71565700e+16],
       [ -3.43131400e+15,   2.05878840e+16,  -1.71565700e+16]])```
like image 947
niklas Avatar asked Sep 09 '17 03:09

niklas


People also ask

Can you find the inverse of a singular matrix?

A singular matrix does not have an inverse. To find the inverse of a square matrix A , you need to find a matrix A−1 such that the product of A and A−1 is the identity matrix.

How does Numpy calculate the inverse of a matrix?

We use numpy. linalg. inv() function to calculate the inverse of a matrix. The inverse of a matrix is such that if it is multiplied by the original matrix, it results in identity matrix.

What does Numpy Linalg Det do?

The numpy. linalg. det() function calculates the determinant of the input matrix.

What is LinAlgError singular matrix?

One error you may encounter in Python is: numpy.linalg.LinAlgError: Singular matrix. This error occurs when you attempt to invert a singular matrix, which by definition is a matrix that has a determinant of zero and cannot be inverted.

How to inverse the NumPy matrix in Python?

So let’s see how to inverse the numpy Matrix in Python. The np.linalg.inv () is a numpy library function that computes a matrix’s (multiplicative) inverse. The inverse of a matrix is a reciprocal of a matrix.

What is the difference between NumPy linalg () and NumPy inv ()?

If the given input is a numpy matrix, then inv () also returns a matrix. If the input array consists of multiple matrices, the numpy linalg.inv () method computes the inverse of them at once. DelftStack is a collective effort contributed by software geeks like you.

What is inv in NumPy with example?

Example Codes: numpy.linalg.inv () Method With matrix Input If the given input is a numpy matrix, then inv () also returns a matrix. import numpy as np arr = np.matrix() arr_inv = np.linalg.inv(arr) print(arr_inv, type(arr_inv))

Why does inv() raise a linalgerror if a is not a square matrix?

The inv () function raises a LinAlgError if A is not a square matrix because if A is not a square matrix, inversion fails. Inversion of 4*4 Matrix.


2 Answers

Behind the scenes, NumPy and SciPy (and many other software) fall back to LAPACK implementations (or C translations) of linear equation solvers (in this case GESV).

Since GESV first performs a LU decomposition and then checks the diagonal of U matrix for exact zeros, it is very difficult to hit perfect zeros in the decompositions. That's why you don't get a singular matrix error.

Apart from that you should never ever invert a matrix if you are multiplying with other matrices but instead solve for AX=B.

In SciPy since version 0.19, scipy.linalg.solve uses the "expert" driver GESVX of GESV which also reports back condition number and a warning is emitted. This is similar to matlab behavior in case the singularity is missed.

In [7]: sp.linalg.solve(np.array([[2,7,7],[7,7,7],[8,7,7]]), np.eye(3))
...\lib\site-packages\scipy\linalg\basic.py:223: RuntimeWarning: scipy.linalg.solve
Ill-conditioned matrix detected. Result is not guaranteed to be accurate.
Reciprocal condition number: 1.1564823173178713e-18
  ' condition number: {}'.format(rcond), RuntimeWarning)
Out[7]: 
array([[  0.00000000e+00,  -1.00000000e+00,   1.50000000e+00],
       [  3.43131400e+15,  -2.05878840e+16,   1.71565700e+16],
       [ -3.43131400e+15,   2.05878840e+16,  -1.71565700e+16]])
like image 76
percusse Avatar answered Oct 19 '22 09:10

percusse


One note from the numpy team:

The de-facto convention in the field is that errors in matrix inversion are mostly silently ignored --- it is assumed that the user knows if this is something that needs to be checked for (implying that a more controlled approximate inversion method needs to be used --- the regularization is problem-dependent).

https://github.com/numpy/numpy/issues/2074

Seems to give an error on 1.13.0 however

like image 2
ionox0 Avatar answered Oct 19 '22 07:10

ionox0