Lindo Systems

! Find the smallest eigenvalue, D, of a symmetric matrix; 
!  Keywords: Positive definite, Eigenvalue, POSD, SDP;
 SETS:
  VAR;
  VXV( VAR, VAR): Q, R;
 ENDSETS
 DATA:
   VAR = 1..3;
! The matrix;
   Q  = .01080754 .01240721 .01307513    
        .01240721 .05839170 .05542639    
        .01307513 .05542639 .09422681 ;
 ENDDATA
!---------------------------------------------------------------;
! Maximize amount deducted, D, from each diagonal element, but
still leaving the matrix positive semi-definite, 
i.e, so Q - D*I remains Positive Semi-definite.
Note, subtracting the same constant D from all elements of
the diagonal decreases all the eigenvalues by D.
If all eigenvalues are >= 0, then the matrix is Positive Semi-definite;
 MAX = D;
 @FREE( D);

! R is the adjusted matrix.
 Off diagonal elements are left unchanged;
 @FOR( VXV( i,j) | i #NE# j:
   R(i,j) = Q(i,j) ;
   @FREE( R(i,j));
     );

! Diagonal elements may be decreased;
 @FOR( VAR(j): 
    R(j,j) = Q(j,j) - D;
    @FREE( R(j,j));
     );

 ! and we want R = Q - D*I to be positive definite;
 @POSD( R);

 DATA:
   @TEXT() = ' The smallest eigenvalue is ', D;
   @TEXT() = ' ';
   @TEXT() = ' The Original matrix';
   @TEXT() = @TABLE(Q);
   @TEXT() =  '  ';
   @TEXT() = ' The Adjusted matrix';
   @TEXT() = @TABLE(R);
   @TEXT() =  '  ';
 ENDDATA