Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining if a matrix is diagonalizable in the R Programming Language

Tags:

r

matrix

I have a matrix and I would like to know if it is diagonalizable. How do I do this in the R programming language?

like image 447
medriscoll Avatar asked Jul 25 '09 02:07

medriscoll


People also ask

How do you know if a matrix is diagonalizable over R?

If the characteristic polynomial of A has n distinct linear factors then A is diagonalizable over C. Let A be an n × n matrix with real entries. If the characteristic polynomial of A has n distinct linear real factors then A is diagonalizable over R.

How do you Diagonalize a matrix in R?

To convert a vector into a diagonal matrix in R, we can use diag function along with matrix function and use ncol argument where we can put the number of columns equal to the number of values in the vector.


2 Answers

If you have a given matrix, m, then one way is the take the eigen vectors times the diagonal of the eigen values times the inverse of the original matrix. That should give us back the original matrix. In R that looks like:

m <- matrix( c(1:16), nrow = 4)
p <- eigen(m)$vectors
d <- diag(eigen(m)$values)
p %*% d %*% solve(p)
m

so in that example p %*% d %*% solve(p) should be the same as m

like image 187
JD Long Avatar answered Sep 28 '22 12:09

JD Long


You can implement the full algorithm to check if the matrix reduces to a Jordan form or a diagonal one (see e.g., this document). Or you can take the quick and dirty way: for an n-dimensional square matrix, use eigen(M)$values and check that they are n distinct values. For random matrices, this always suffices: degeneracy has prob.0.

P.S.: based on a simple observation by JD Long below, I recalled that a necessary and sufficient condition for diagonalizability is that the eigenvectors span the original space. To check this, just see that eigenvector matrix has full rank (no zero eigenvalue). So here is the code:

diagflag = function(m,tol=1e-10){
    x = eigen(m)$vectors
    y = min(abs(eigen(x)$values))
    return(y>tol)
}
# nondiagonalizable matrix 
m1 = matrix(c(1,1,0,1),nrow=2) 
# diagonalizable matrix
m2 = matrix(c(-1,1,0,1),nrow=2) 

> m1
     [,1] [,2]
[1,]    1    0
[2,]    1    1

> diagflag(m1)
[1] FALSE

> m2
     [,1] [,2]
[1,]   -1    0
[2,]    1    1

> diagflag(m2)
[1] TRUE
like image 24
gappy Avatar answered Sep 28 '22 12:09

gappy