anchor.diff:        INTEGER,        intent(in   ) :: LDB               ! The leading dimension of B.  LDB >= max(1,N).
         INTEGER,        intent(in   ) :: LDVL              ! The leading dimension of the matrix VL. LDVL >= 1, and if JOBVL = 'V', LDVL >= N
         INTEGER,        intent(in   ) :: LDVR              ! The leading dimension of the matrix VR. LDVR >= 1, and if JOBVR = 'V', LDVR >= N.
  -      INTEGER,        intent(in   ) :: LWORK             ! The dimension of the array WORK.  LWORK >= max(1,8*N). For good performance, LWORK must generally be larger. 
  +      INTEGER,        intent(in   ) :: LWORK             ! The dimension of the array WORK.  LWORK >= max(1,8*N). For good performance, LWORK must generally be larger.
                                                            !   If LWORK = -1, then a workspace query is assumed; the routine only calculates the optimal size of the WORK array, returns
                                                            !   this value as the first entry of the WORK array, and no error message related to LWORK is issued by XERBLA.
  -      
  -      
  +
  +
         REAL(SiKi),     intent(inout) :: A( LDA, * )       ! dimension (LDA, N). On entry, the matrix A in the pair (A,B). On exit, A has been overwritten.
         REAL,           intent(inout) :: B( LDB, * )       ! dimension (LDB, N). On entry, the matrix B in the pair (A,B). On exit, B has been overwritten.
  -      
  -      
  +
  +
         REAL(SiKi),     intent(  out) :: ALPHAR( * )       ! dimension (N). See comments for variable "Beta"
         REAL(SiKi),     intent(  out) :: ALPHAI( * )       ! dimension (N). See comments for variable "Beta".
         REAL(SiKi),     intent(  out) :: BETA( * )         ! On exit, (ALPHAR(j) + ALPHAI(j)*i)/BETA(j), j=1,...,N, will be the generalized eigenvalues.  If ALPHAI(j) is zero, then
                                                            !   the j-th eigenvalue is real; if positive, then the j-th and (j+1)-st eigenvalues are a complex conjugate pair, with
                                                            !   ALPHAI(j+1) negative.
  -                                                         !   
  +                                                         !
                                                            !   Note: the quotients ALPHAR(j)/BETA(j) and ALPHAI(j)/BETA(j) may easily over- or underflow, and BETA(j) may even be zero.
                                                            !   Thus, the user should avoid naively computing the ratio alpha/beta.  However, ALPHAR and ALPHAI will be always less
  -                                                         !   than and usually comparable with norm(A) in magnitude, and BETA always less than and usually comparable with norm(B).      
  -      
  -                                                         
  +                                                         !   than and usually comparable with norm(A) in magnitude, and BETA always less than and usually comparable with norm(B).
  +
  +
         REAL(SiKi),     intent(  out) :: VL( LDVL, * )     ! dimension (LDVL,N). If JOBVL = 'V', the left eigenvectors u(j) are stored one after another in the columns of VL, in the same
                                                            !   order as their eigenvalues. If the j-th eigenvalue is real, then u(j) = VL(:,j), the j-th column of VL. If the j-th and
                                                            !   (j+1)-th eigenvalues form a complex conjugate pair, then u(j) = VL(:,j)+i*VL(:,j+1) and u(j+1) = VL(:,j)-i*VL(:,j+1).
                                                            !   Each eigenvector is scaled so the largest component has abs(real part)+abs(imag. part)=1. Not referenced if JOBVL = 'N'.
  -      REAL(SiKi),     intent(  out) :: VR( LDVR, * )     ! dimension (LDVR,N). If JOBVR = 'V', the right eigenvectors v(j) are stored one after another in the columns of VR, in the same 
  +      REAL(SiKi),     intent(  out) :: VR( LDVR, * )     ! dimension (LDVR,N). If JOBVR = 'V', the right eigenvectors v(j) are stored one after another in the columns of VR, in the same
                                                            !   order as their eigenvalues. If the j-th eigenvalue is real, then v(j) = VR(:,j), the j-th column of VR. If the j-th and
                                                            !   (j+1)-th eigenvalues form a complex conjugate pair, then v(j) = VR(:,j)+i*VR(:,j+1) and v(j+1) = VR(:,j)-i*VR(:,j+1).
                                                            !   Each eigenvector is scaled so the largest component has abs(real part)+abs(imag. part)=1. Not referenced if JOBVR = 'N'.
  -      
  +
         REAL(SiKi),     intent(  out) :: WORK( * )         ! dimension (MAX(1,LWORK)). On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
   
  -      
  -      
  -      INTEGER(IntKi), intent(  out) :: ErrStat           ! Error level 
  +
  +
  +      INTEGER(IntKi), intent(  out) :: ErrStat           ! Error level
         CHARACTER(*),   intent(  out) :: ErrMsg            ! Message describing error
  -      
  -         ! local variables      
  -      INTEGER                       :: INFO              ! = 0:  successful exit; 
  -                                                         ! < 0:  
  +
  +         ! local variables
  +      INTEGER                       :: INFO              ! = 0:  successful exit;
  +                                                         ! < 0:
                                                            !   = -i, the i-th argument had an illegal value;
                                                            ! > 0:
                                                            !   = 1,...,N: The QZ iteration failed.  No eigenvectors have been calculated, but ALPHAR(j), ALPHAI(j), and BETA(j) should be correct for j=INFO+1,...,N.
  -                                                         !   = N+1: other than QZ iteration failed in SHGEQZ. 
  +                                                         !   = N+1: other than QZ iteration failed in SHGEQZ.
                                                            !   = N+2: error return from STGEVC.
  -      CHARACTER(20)                 ::  n_str                                                        
  -                                                         
  -                              
  +      CHARACTER(20)                 ::  n_str
  +
  +
         ErrStat = ErrID_None
         ErrMsg  = ""
   
         CALL SGGEV( JOBVL, JOBVR, N, A, LDA, B, LDB, ALPHAR, ALPHAI, BETA, VL, LDVL, VR, LDVR, WORK, LWORK, INFO )
  -                
  -      IF (INFO /= 0) THEN
  -         ErrStat = ErrID_FATAL
  -         WRITE( ErrMsg, * ) INFO         
  -         IF (INFO < 0) THEN
  -            ErrMsg  = "LAPACK_SGGEV: illegal value in argument "//TRIM(ErrMsg)//"."         
  +
  +      IF (INFO /= 0) THEN
  +         ErrStat = ErrID_FATAL
  +         WRITE( ErrMsg, * ) INFO
  +         IF (INFO < 0) THEN
  +            ErrMsg  = "LAPACK_SGGEV: illegal value in argument "//TRIM(ErrMsg)//"."
            ELSEIF (INFO <=N) THEN
               !ErrStat = ErrID_Severe
  -            WRITE( ErrMsg, * ) INFO + 1            
  +            WRITE( ErrMsg, * ) INFO + 1
               WRITE( n_str, * ) n
               ErrMsg  = "LAPACK_SGGEV: The QZ iteration failed. No eigenvectors have been calculated, but ALPHAR(j), ALPHAI(j), and BETA(j) should be correct for j="&
                          //TRIM(ErrMsg)//",...,"//TRIM(n_str)//"."
  
positive.code:       INTEGER,         intent(in   ) :: NRHS              ! The number of right hand sides, i.e., the number of columns of the matrix B.  NRHS >= 0.
        INTEGER,         intent(in   ) :: LDA               ! The leading dimension of the array A.  LDA >= max(1,N).
        INTEGER,         intent(in   ) :: LDB               ! The leading dimension of the array B.  LDB >= max(1,N).
                                                  
        !     .. Array Arguments ..
        INTEGER,         intent(in   ) :: IPIV( * )         ! The pivot indices from DGETRF; for 1<=i<=N, row i of the matrix was interchanged with row IPIV(i).                
        REAL(R8Ki)      ,intent(in   ) :: A( LDA, * )       ! The factors L and U from the factorization A = P*L*U as computed by DGETRF. 
        REAL(R8Ki)      ,intent(inout) :: B( *      )       ! On entry, the right hand side matrix B. On exit, the solution matrix X.
        
        INTEGER(IntKi),  intent(  out) :: ErrStat           ! Error level 
        CHARACTER(*),    intent(  out) :: ErrMsg            ! Message describing error
        
           ! local variables      
        INTEGER                        :: INFO              ! = 0:  successful exit; < 0:  if INFO = -i, the i-th argument had an illegal value; 
        
        
        ErrStat = ErrID_None
        ErrMsg  = ""
        
        CALL DGETRS( TRANS, N, NRHS, A, LDA, IPIV, B, LDB, INFO )
                  
        IF (INFO /= 0) THEN
           ErrStat = ErrID_FATAL
           WRITE( ErrMsg, * ) INFO
           IF (INFO < 0) THEN
              ErrMsg  = "LAPACK_DGETRS1: illegal value in argument "//TRIM(ErrMsg)//"."         
           ELSE
              ErrMsg = 'LAPACK_DGETRS1: unknown error '//TRIM(ErrMsg)//'.'
           END IF
        END IF      
   
        
     RETURN
     END SUBROUTINE LAPACK_DGETRS1
  !=======================================================================
     SUBROUTINE LAPACK_SGETRS( TRANS, N, NRHS, A, LDA, IPIV, B, LDB, ErrStat, ErrMsg )
                                  
        ! passed parameters
   
        CHARACTER(1),   intent(in   ) :: TRANS             ! Specifies the form of the system of equations: = 'N':  A * X = B  (No transpose)
                                                           !                                                = 'T':  A**T* X = B  (Transpose)
                                                           !                                                = 'C':  A**T* X = B  (Conjugate transpose = Transpose)
  
negative.code:             ErrMsg  = "LAPACK_SGGEV: other than QZ iteration failed in SHGEQZ."
           ELSEIF (INFO == N+2) THEN
              ErrMsg  = "LAPACK_SGGEV: error return from STGEVC."
           ELSE
              ErrMsg = 'LAPACK_SGGEV: unknown error '//TRIM(ErrMsg)//'.'
           END IF
        END IF
  
  
     RETURN
     END SUBROUTINE LAPACK_SGGEV
  !=======================================================================
     SUBROUTINE LAPACK_DPOSV (UPLO, N, NRHS, A, LDA, B, LDB, ErrStat, ErrMsg)
  
  
        ! passed parameters
  
        INTEGER,         intent(in   ) :: N                 ! The number of linear equations, i.e., the order of the matrix A.  N >= 0.
        INTEGER,         intent(in   ) :: NRHS              ! The number of right hand sides, i.e., the number of columns of the matrix B.  NRHS >= 0.
        INTEGER,         intent(in   ) :: LDA               ! The leading dimension of the array A.  LDA >= max(1,N).
        INTEGER,         intent(in   ) :: LDB               ! The leading dimension of the array B.  LDB >= max(1,N).
  
        !     .. Array Arguments ..
        REAL(R8Ki)      ,intent(inout) :: A( LDA, * )       ! On entry, the symmetric matrix A.  If UPLO = 'U', the leading N-by-N upper triangular part of A contains the upper
                                                            ! triangular part of the matrix A, and the strictly lower triangular part of A is not referenced.  If UPLO = 'L', the
                                                            ! leading N-by-N lower triangular part of A contains the lower triangular part of the matrix A, and the strictly upper
                                                            ! triangular part of A is not referenced.
                                                            ! On exit, if INFO = 0, the factor U or L from the Cholesky factorization A = U**T*U or A = L*L**T.
        REAL(R8Ki)      ,intent(inout) :: B( LDA, * )       ! On entry, the N-by-NRHS matrix of right hand side matrix B.  On exit, if INFO = 0, the N-by-NRHS solution matrix X.
  
        INTEGER(IntKi),  intent(  out) :: ErrStat           ! Error level
        CHARACTER(*),    intent(  out) :: ErrMsg            ! Message describing error
        CHARACTER(1),    intent(in   ) :: UPLO              ! 'U':  Upper triangle of A is stored; 'L':  Lower triangle of A is stored.
  
           ! local variables
        INTEGER                        :: INFO              ! = 0:  successful exit; < 0:  if INFO = -i, the i-th argument had an illegal value; > 0: if INFO = i, U(i,i) is exactly zero.
                                                            ! > 0: if INFO = i, the leading minor of order i of A is not positive definite, so the factorization could not be
                                                            ! completed, and the solution has not been computed.
  
  
  
        ErrStat = ErrID_None
        ErrMsg  = ""
  
        CALL DPOSV (UPLO, N, NRHS, A, LDA, B, LDB, INFO)
  
        IF (INFO /= 0) THEN
           ErrStat = ErrID_FATAL
           WRITE( ErrMsg, * ) INFO
           IF (INFO < 0) THEN
              ErrMsg  = "LAPACK_DPOSV: illegal value in argument "//TRIM(ErrMsg)//"."
           ELSE
              ErrMsg = 'LAPACK_DPOSV: Leading minor order '//TRIM(ErrMsg)//' of A is not positive definite, so factorization could not be completed, and the solution has not been computed.'
           END IF
        END IF
  
  
     RETURN
     END SUBROUTINE LAPACK_DPOSV
  !=======================================================================
     SUBROUTINE LAPACK_SPOSV (UPLO, N, NRHS, A, LDA, B, LDB, ErrStat, ErrMsg)
  
  
        ! passed parameters
  
        INTEGER,         intent(in   ) :: N                 ! The number of linear equations, i.e., the order of the matrix A.  N >= 0.
        INTEGER,         intent(in   ) :: NRHS              ! The number of right hand sides, i.e., the number of columns of the matrix B.  NRHS >= 0.
        INTEGER,         intent(in   ) :: LDA               ! The leading dimension of the array A.  LDA >= max(1,N).
        INTEGER,         intent(in   ) :: LDB               ! The leading dimension of the array B.  LDB >= max(1,N).
  
        !     .. Array Arguments ..
        REAL(SiKi)      ,intent(inout) :: A( LDA, * )       ! On entry, the symmetric matrix A.  If UPLO = 'U', the leading N-by-N upper triangular part of A contains the upper
                                                            ! triangular part of the matrix A, and the strictly lower triangular part of A is not referenced.  If UPLO = 'L', the
                                                            ! leading N-by-N lower triangular part of A contains the lower triangular part of the matrix A, and the strictly upper
                                                            ! triangular part of A is not referenced.
                                                            ! On exit, if INFO = 0, the factor U or L from the Cholesky factorization A = U**T*U or A = L*L**T.
        REAL(SiKi)      ,intent(inout) :: B( LDA, * )       ! On entry, the N-by-NRHS matrix of right hand side matrix B.  On exit, if INFO = 0, the N-by-NRHS solution matrix X.
  
        INTEGER(IntKi),  intent(  out) :: ErrStat           ! Error level
        CHARACTER(*),    intent(  out) :: ErrMsg            ! Message describing error
        CHARACTER(1),    intent(in   ) :: UPLO              ! 'U':  Upper triangle of A is stored; 'L':  Lower triangle of A is stored.
  
           ! local variables
        INTEGER                        :: INFO              ! = 0:  successful exit; < 0:  if INFO = -i, the i-th argument had an illegal value; > 0: if INFO = i, U(i,i) is exactly zero.
                                                            ! > 0: if INFO = i, the leading minor of order i of A is not positive definite, so the factorization could not be
                                                            ! completed, and the solution has not been computed.
  
  
  
        ErrStat = ErrID_None
        ErrMsg  = ""
  
        CALL SPOSV (UPLO, N, NRHS, A, LDA, B, LDB, INFO)
  
        IF (INFO /= 0) THEN
           ErrStat = ErrID_FATAL
           WRITE( ErrMsg, * ) INFO
           IF (INFO < 0) THEN
              ErrMsg  = "LAPACK_SPOSV: illegal value in argument "//TRIM(ErrMsg)//"."
           ELSE
              ErrMsg = 'LAPACK_SPOSV: Leading minor order '//TRIM(ErrMsg)//' of A is not positive definite, so factorization could not be completed, and the solution has not been computed.'
           END IF
        END IF
  
  
     RETURN
     END SUBROUTINE LAPACK_SPOSV
  !=======================================================================
  END MODULE NWTC_LAPACK
  