Bilal
Bilal

Reputation: 3272

Calculate the inverse of a matrix : system is computationally singular [error]

I have a matrix m :

(m <- matrix(c(26,14,24,14,20,42,24,42,90), 3))

#      [,1] [,2] [,3]
# [1,]   26   14   24
# [2,]   14   20   42
# [3,]   24   42   90

When i run solve(m) to calculate the inverse of the matrix, i get this error message :

solve(m)

Error in solve.default(m) : system is computationally singular: reciprocal condition number = 6.21104e-18

Upvotes: 2

Views: 5148

Answers (2)

G. Grothendieck
G. Grothendieck

Reputation: 269501

We can see that this must be so in several ways each of which implies non-invertability:

1) The determinant of m is zero:

> det(m)
[1] -2.685852e-12

2) m has a zero eigenvalue, i.e. eigen(m)$values[3]. Equivalently the nullspace of m is non-null -- it equals the 1 dimensional space spanned by eigen(m)$vectors[, 3]

> e <- eigen(m); e
$values
[1]  1.180000e+02  1.800000e+01 -6.446353e-15

$vectors
           [,1]          [,2]       [,3]
[1,] -0.2881854  9.486833e-01  0.1301889
[2,] -0.4116935  1.110223e-16 -0.9113224
[3,] -0.8645563 -3.162278e-01  0.3905667

> N <- e$vector[, 3]  # nullspace
> m %*% N  # shows that N is indeed mapped to zero
             [,1]
[1,] 5.329071e-15
[2,] 0.000000e+00
[3,] 0.000000e+00

3) The columns of m are not linearly independent. In particular regressing m[,1] on the other columns gives a perfect fit (i.e. the fitted values equal m[, 1]) so from the coefficients of the linear model we have 7 * m[,2] - 3 * m[, 3] equals m[, 1].

> fm <- lm(m[, 1] ~ m[, 2] + m[, 3] + 0)

> all.equal(fitted(fm), m[, 1]) # perfect fit
[1] TRUE

> coef(fm)
m[, 2] m[, 3] 
     7     -3 

> all.equal(7 * m[, 2] - 3 * m[, 3], m[, 1])
[1] TRUE

4) The cholesky decomposition has a zero on its diagonal:

> chol(m, pivot = TRUE)
         [,1]     [,2]      [,3]
[1,] 9.486833 2.529822 4.4271887
[2,] 0.000000 4.427189 0.6324555
[3,] 0.000000 0.000000 0.0000000
attr(,"pivot")
[1] 3 1 2
attr(,"rank")
[1] 2
Warning message:
In chol.default(m, pivot = TRUE) :
  the matrix is either rank-deficient or indefinite

5) m is not of full rank, i.e. the rank is less than 3:

> attr(chol(m, pivot = TRUE), "rank")
[1] 2
Warning message:
In chol.default(m, pivot = TRUE) :
  the matrix is either rank-deficient or indefinite

Note: The input is given reproducibly by:

m <- matrix(c(26, 14, 24, 14, 20, 42, 24, 42, 90), 3)

Upvotes: 10

David Maust
David Maust

Reputation: 8270

The problem is the columns are not linearly independent.

The first column * -1/3 + second column * 7/3 is equal to the third column.

-m[, 1] * 1/3 + 7/3 * m[, 2]

# [1] 24 42 90

Upvotes: 8

Related Questions