How to check if a matrix has an inverse in the R language

hamsternik picture hamsternik · Jul 25, 2014 · Viewed 13.3k times · Source

How do you determine if a matrix has an inverse in R?

So is there in R a function that with a matrix input, will return somethin like:

"TRUE" (this matrix has inverse)/"FALSE"(it hasn't ...).

Answer

MAB picture MAB · Jul 25, 2014

Using abs(det(M)) > threshold as a way of determining if a matrix is invertible is a very bad idea. Here's an example: consider the class of matrices cI, where I is the identity matrix and c is a constant. If c = 0.01 and I is 10 x 10, then det(cI) = 10^-20, but (cI)^-1 most definitely exists and is simply 100I. If c is small enough, det() will underflow and return 0 even though the matrix is invertible. If you want to use determinants to check invertibility, check instead if the modulus of the log determinant is finite using determinant().