2
votes

I am trying to learn how to use LaPACK by diagonalizing this simple matrix:

0.8147    0.9058    0.1270    0.9134
0.6324    0.0975    0.2785    0.5469
0.9575    0.9649    0.1576    0.9706
0.9572    0.4854    0.8003    0.1419

In matlab, I just use the command, eig(mat), and get the output:

ans =

    2.4021
   -0.0346
   -0.7158
   -0.4400

However, when I try to write a simple fortran program to diagonalize the same matrix, I get different eigenvalues:

      implicit none

  real*8, allocatable::dataMat(:,:),dataMatP(:,:),corMat(:,:),
 $   tempMat(:,:),corMat2(:,:)
  real*8, allocatable::matList(:),rawData(:)
  real*8, allocatable ::eig(:),diag(:),offdiag(:),tau(:),work(:)
  real*8 avg1,avg2,SD1,SD2,geneCorSum,genei,genej,temp
  integer i,j,k,numElements,info,lwork,numGenes,n,
 $   numExperiments,readsize,numAbsent,count,geneTolerance

  real*8 mean,std

  n=4

  allocate(corMat(4,4))

corMat(1,1)=0.8147
corMat(1,2)=0.9058
corMat(1,3)=0.1270
corMat(1,4)=0.9134
corMat(2,1)=0.6234
corMat(2,2)=0.0975
corMat(2,3)=0.2785
corMat(2,4)=0.5469
corMat(3,1)=0.9575
corMat(3,2)=0.9649
corMat(3,3)=0.1576
corMat(3,4)=0.9706
corMat(4,1)=0.9572
corMat(4,2)=0.4854
corMat(4,3)=0.8003
corMat(4,4)=0.1419



  allocate(diag(n))
  allocate(offdiag(n-1))
  allocate(tau(n-1))
  allocate(work(1))

  call dsytrd('U',n,corMat,n,diag,offdiag,tau,
 $ work,-1,info)
  print*,"Returning from Blocksize calculation"
  if(info.eq.0) then
  print*,"Work value successfully calculated:",work(1)
  endif
  lwork=work(1)
  deallocate(work)
  allocate(work(max(1,lwork)))

  call dsytrd('U',n,corMat,n,diag,offdiag,tau,
 $ work,lwork,info)
  print*,"Returning from full SSYTRD"
  if(info.EQ.0) then
  print*,"Tridiagonal matrix calculated"
  endif



  call dsterf(n,diag,offdiag,info)
  if(info.EQ.0) then
    print*,"Matrix Diagonalized"
  endif


  do i=1,n
  print*,"lam=",i,diag(i)
  enddo

  deallocate(offdiag)
  deallocate(work)
  deallocate(tau)

  end

This gives me:

 lam= 1,  -1.0228376083545221
 lam= 2,  -0.48858533844019592
 lam= 3,  0.43828991894506536
 lam= 4,  2.2848330351691031

Did I do something wrong to get different eigenvalues?

3

3 Answers

3
votes

The LAPACK routines you have used assume a symmetric matrix whereas the original matrix is not.

To prove this, create a symmetric matrix from your original matrix, using the upper right triangular part and run MATLAB's eig function:

for i=1:4
  for j=i:4; 
    xx(i,j) = x(i,j); 
    xx(j,i)=x(i,j);
  end
end

The resulting matrix (x was the original matrix you had):

xx =

0.8147    0.9058    0.1270    0.9134
0.9058    0.0975    0.2785    0.5469
0.1270    0.2785    0.1576    0.9706
0.9134    0.5469    0.9706    0.1419

And the eigenvalues of the original x and the symmetric xx matrices:

>> eig(x)
  ans =    2.4022    -0.0346   -0.7158   -0.4400

>> eig(xx)
  ans =   -1.0228    -0.4886     0.4383     2.2848
1
votes

To begin with, I hope that you're not just copy/pasting the four decimal places that Matlab prints out the command window by default. Second, corMat(2,1)=0.6234 is different from the corresponding value in your first matrix. Thirdly, the documentation for dsytrd states:

DSYTRD reduces a real symmetric matrix A to real symmetric tridiagonal form T by an orthogonal similarity transformation ...

Your matrix is definitely not symmetric (isequal(A,A')). There are are variety of routines that handle non symmetric matrices. You might try dgeev, for example.

0
votes

SSYTRD/DSYTRD only works for the symmetric matrix.