# [R] How to compute eigenvectors and eigenvalues?

Martin Maechler maechler at stat.math.ethz.ch
Mon Nov 14 13:06:46 CET 2011

``` > Consider the following matrix:

> > mp <- matrix(c(0,1/4,1/4,3/4,0,1/4,1/4,3/4,1/2),3,3,byrow=T)

> > mp
>      [,1] [,2] [,3]
> [1,] 0.00 0.25 0.25
> [2,] 0.75 0.00 0.25
> [3,] 0.25 0.75 0.50

> The eigenvectors of the previous matrix are 1, 0.25 and 0.25 and it is not a diagonalizable matrix.

> When you try to find the eigenvalues and eigenvectors with R, R responses:

> > eigen(mp)

> \$values
> [1]  1.00 -0.25 -0.25

> \$vectors
>           [,1]          [,2]          [,3]
> [1,] 0.3207501  1.068531e-08 -1.068531e-08
> [2,] 0.4490502 -7.071068e-01 -7.071068e-01
> [3,] 0.8339504  7.071068e-01  7.071068e-01

> The eigenvalues are correct but the eigenvectors aren't.

Well, let's look at   4*mp  which is an integer matrix:

> (M <- matrix(c(rep(c(0, 3, 1, 1), 2),2), 3,3)); em <- eigen(M); V <- em\$vectors; lam <- em\$values
[,1] [,2] [,3]
[1,]    0    1    1
[2,]    3    0    1
[3,]    1    3    2
> all.equal(M,  V %*% diag(lam) %*% solve(V))
[1] TRUE
> zapsmall(V %*% diag(lam) %*% solve(V))
[,1] [,2] [,3]
[1,]    0    1    1
[2,]    3    0    1
[3,]    1    3    2
>

So, as you see  V is not singular,
and  the basic property of the eigenvalue decomposition is
fulfilled:

M   = V Lambda V^{-1}
or
M V = V Lambda

i.e. each column of V  *is* eigenvector to the corresponding
eigenvalue.

> Moreover, if you try to compute the inverse of the matrix of eigenvectors, R is not aware that this matrix is singular:

> > solve(eigen(mp)\$vectors)
>               [,1]          [,2]          [,3]
> [1,]  6.235383e-01  6.235383e-01  6.235383e-01
> [2,]  3.743456e+07 -9.358641e+06 -9.358640e+06
> [3,] -3.743456e+07  9.358640e+06  9.358641e+06

it isn't...

> My question is: how can I fix it?

No need to fix anything, as nothing is broken ;-)

Martin Maechler, ETH Zurich

```