# [R] a bug with LAPACK ? non orthogonal vectors obtained with eigen of a symmetric matrix

Stephane DRAY stephane.dray at umontreal.ca
Tue Jul 20 16:44:14 CEST 2004

```Hello,
I have obtained strange results using eigen on a symmetric matrix:

# this function perform a double centering of a matrix
(xij-rowmean(i)-colmean(j)+meantot)
dbcenter=function(mat){
rmean=apply(mat,1,mean)
cmean=apply(mat,2,mean)
newmat=sweep(mat,1,rmean,"-")
newmat=sweep(newmat,2,cmean,"-")
newmat=newmat+mean(mat)
newmat}

# i use spdep package to create a spatial contiguity matrix
library(spdep)
x=dbcenter(nb2mat(cell2nb(3,3),style="B"))

#compute eigenvalues of a 9 by 9 matrix
res=eigen(x)

# some eigenvalues are equal to 0
eq0 <- apply(as.matrix(res\$values),1,function(x) identical(all.equal(x, 0),
TRUE))

# I remove the corresponding eigenvectors
res0=res\$vec[,-which(eq0)]

# then I compute the Froebenius norm with the identity matrix
sum((diag(1,ncol(res0))-crossprod(res0))^2)

[1] 1.515139e-30

# The results are correct,
# then I do it again with a biggest matrix(100 by 100)

x=dbcenter(nb2mat(cell2nb(10,10),style="B"))
res=eigen(x)
eq0 <- apply(as.matrix(res\$values),1,function(x) identical(all.equal(x, 0),
TRUE))
res0=res\$vec[,-which(eq0)]
sum((diag(1,ncol(res0))-crossprod(res0))^2)

[1] 3.986387

I have try the same with res=eigen(x,EISPACK=T):

x=dbcenter(nb2mat(cell2nb(10,10),style="B"))
res=eigen(x,EISPACK=T)
eq0 <- apply(as.matrix(res\$values),1,function(x) identical(all.equal(x, 0),
TRUE))
res0=res\$vec[,-which(eq0)]
sum((diag(1,ncol(res0))-crossprod(res0))^2)
[1] 1.315542e-27

So I wonder I there is a bug in the LAPACK algorithm or if there are some
things that I have not understood. Note that my matrix has some pairs of
equal eigenvalues.

Stéphane DRAY
--------------------------------------------------------------------------------------------------

Département des Sciences Biologiques
Université de Montréal, C.P. 6128, succursale centre-ville