Inverse of singular matrix. Should be error but in fact not.
See original GitHub issueHello. I’ve got strange behavior for experiments. I’m working with matrix (for example b) that in result of multiplying b.T * b should be singular matrix and for inverse method should be arisen error like numpy.linalg.linalg.LinAlgError: Singular matrix. But result was high/low values. Code below:
>>> b = np.matrix([[1,1,0], [1,0,1], [1,1,0]])
>>> b
matrix([[1, 1, 0],
[1, 0, 1],
[1, 1, 0]])
>>> np.linalg.inv(b.T * b)
matrix([[ 4.50359963e+15, -4.50359963e+15, -4.50359963e+15],
[-4.50359963e+15, 4.50359963e+15, 4.50359963e+15],
[-4.50359963e+15, 4.50359963e+15, 4.50359963e+15]])
How can be avoided this behavior? Tests on: win10, Python 3.5.4, numpy version ‘1.14.0’. ubuntu 16.04, Python 3.5.2, numpy version ‘1.13.3’ and ‘1.14.0’.
PS. I’ve checked via wolfram and R it’s real singular matrix.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:11 (7 by maintainers)
Top Results From Across the Web
Making a singular matrix non-singular
But if the matrix has a large condition number, the result may be very inaccurate.
Read more >numpy.linalg.inv returns inverse for a singular matrix
Since GESV first performs a LU decomposition and then checks the diagonal of U matrix for exact zeros, it is very difficult to...
Read more >If $A$ is a non- singular matrix and $AB= I$ , can we comment ...
The complete definition of inverse of a matrix is- if AB=BA=I, then B is inverse of A.
Read more >Near Singular Matrix - EViews.com
Assuming this is the matrix you emailed into us, the determinant is not just "very small", it is essentially zero. 1.05e-101 is not...
Read more >Unique A -1
... trial and error a few pages ago. If A is non-singular (has an inverse) and Ap = q, then p = A...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The condition number (
np.linalg.cond
) might be a more appropriate tool than the determinant here.@noob-saibot You could use
scipy.linalg.solve
, which raises a warning when it detects an ill-conditioned matrix (since version 0.19.0, if I’m not mistaken):The issue in
numpy.linalg.inv
is that the LU decomposition doesn’t give the exact solution, which you can see from:Since all diagonal elements in
U
are nonzero,numpy.linalg.inv
continues without raising an error. If it used GESVX, asscipy.linalg.solve
does, it could relatively cheaply compute an estimate of a condition number.But, do you really need
inv
for what you are doing? If, e.g., you need to solve a least squares problem, there are better methods then using normal equations.