      SUBROUTINE CHLRD(W,N)
*     Cholesky decomposition of symmetric N*N matrix W, 
*     overwritten by result of decomposition.
*     W(1)=0 returned in case of non-positive diagonal element  
      REAL W(*)
      II=0
      DO I=1,N
       II=II+I
       IF(W(II).LE.0.0) THEN
*         non-positive diagonal element          
          W(1)=0.0
          RETURN           
       END IF
       W(II)=SQRT(W(II))      ! diagonal element of R 
       DO J=I+1,N 
        IJ=I+(J*J-J)/2
        W(IJ)=W(IJ)/W(II)     ! other elements of R
       END DO  
       DO J=I+1,N
        IJ=I+(J*J-J)/2
        DO K=J,N   
         JK=J+(K*K-K)/2
         IK=I+(K*K-K)/2
         W(JK)=W(JK)-W(IJ)*W(IK)
        END DO        
       END DO
      END DO
      END
