      SUBROUTINE TQL2(NM,N,D,E,Z,IERR)
C
C     .. Scalar Arguments ..
      INTEGER         IERR, N, NM
C     .. Array Arguments ..
      REAL D(N), E(N), Z(NM,N)
C     .. Local Scalars ..
      REAL C, C2, C3, DL1, EL1, F, G, H, P, R, S, S2, TST1,
     *                 TST2
      INTEGER         I, II, J, K, L, L1, L2, M, MML
C     .. External Functions ..
      REAL PYTHAG
      EXTERNAL        PYTHAG
C     .. Intrinsic Functions ..
      INTRINSIC       ABS, SIGN
C     .. Executable Statements ..
C
C     THIS SUBROUTINE IS A TRANSLATION OF THE ALGOL PROCEDURE TQL2,
C     NUM. MATH. 11, 293-306(1968) BY BOWDLER, MARTIN, REINSCH, AND
C     WILKINSON.
C     HANDBOOK FOR AUTO. COMP., VOL.II-LINEAR ALGEBRA, 227-240(1971).
C
C     THIS SUBROUTINE FINDS THE EIGENVALUES AND EIGENVECTORS
C     OF A SYMMETRIC TRIDIAGONAL MATRIX BY THE QL METHOD.
C     THE EIGENVECTORS OF A FULL SYMMETRIC MATRIX CAN ALSO
C     BE FOUND IF  TRED2  HAS BEEN USED TO REDUCE THIS
C     FULL MATRIX TO TRIDIAGONAL FORM.
C
C     ON INPUT
C
C        NM MUST BE SET TO THE ROW DIMENSION OF TWO-DIMENSIONAL
C          ARRAY PARAMETERS AS DECLARED IN THE CALLING PROGRAM
C          DIMENSION STATEMENT.
C
C        N IS THE ORDER OF THE MATRIX.
C
C        D CONTAINS THE DIAGONAL ELEMENTS OF THE INPUT MATRIX.
C
C        E CONTAINS THE SUBDIAGONAL ELEMENTS OF THE INPUT MATRIX
C          IN ITS LAST N-1 POSITIONS.  E(1) IS ARBITRARY.
C
C        Z CONTAINS THE TRANSFORMATION MATRIX PRODUCED IN THE
C          REDUCTION BY  TRED2, IF PERFORMED.  IF THE EIGENVECTORS
C          OF THE TRIDIAGONAL MATRIX ARE DESIRED, Z MUST CONTAIN
C          THE IDENTITY MATRIX.
C
C      ON OUTPUT
C
C        D CONTAINS THE EIGENVALUES IN ASCENDING ORDER.  IF AN
C          ERROR EXIT IS MADE, THE EIGENVALUES ARE CORRECT BUT
C          UNORDERED FOR INDICES 1,2,...,IERR-1.
C
C        E HAS BEEN DESTROYED.
C
C        Z CONTAINS ORTHONORMAL EIGENVECTORS OF THE SYMMETRIC
C          TRIDIAGONAL (OR FULL) MATRIX.  IF AN ERROR EXIT IS MADE,
C          Z CONTAINS THE EIGENVECTORS ASSOCIATED WITH THE STORED
C          EIGENVALUES.
C
C        IERR IS SET TO
C          ZERO       FOR NORMAL RETURN,
C          J          IF THE J-TH EIGENVALUE HAS NOT BEEN
C                     DETERMINED AFTER 30 ITERATIONS.
C
C     CALLS PYTHAG FOR  DSQRT(A*A + B*B) .
C
C     QUESTIONS AND COMMENTS SHOULD BE DIRECTED TO BURTON S. GARBOW,
C     MATHEMATICS AND COMPUTER SCIENCE DIV, ARGONNE NATIONAL LABORATORY
C
C     THIS VERSION DATED AUGUST 1983.
C
C     ------------------------------------------------------------------
C
      IERR = 0
      IF (N.EQ.1) GO TO 300
C
      DO 20 I = 2, N
         E(I-1) = E(I)
   20 CONTINUE
C
      F = 0.0E0
      TST1 = 0.0E0
      E(N) = 0.0E0
C
      DO 200 L = 1, N
         J = 0
         H = ABS(D(L)) + ABS(E(L))
         IF (TST1.LT.H) TST1 = H
C        .......... LOOK FOR SMALL SUB-DIAGONAL ELEMENT ..........
         DO 40 M = L, N
            TST2 = TST1 + ABS(E(M))
            IF (TST2.EQ.TST1) GO TO 60
C           .......... E(N) IS ALWAYS ZERO, SO THERE IS NO EXIT
C                THROUGH THE BOTTOM OF THE LOOP ..........
   40    CONTINUE
C
   60    IF (M.EQ.L) GO TO 180
   80    IF (J.EQ.30) GO TO 280
         J = J + 1
C        .......... FORM SHIFT ..........
         L1 = L + 1
         L2 = L1 + 1
         G = D(L)
         P = (D(L1)-G)/(2.0E0*E(L))
         R = PYTHAG(P,1.0E0)
         D(L) = E(L)/(P+SIGN(R,P))
         D(L1) = E(L)*(P+SIGN(R,P))
         DL1 = D(L1)
         H = G - D(L)
         IF (L2.GT.N) GO TO 120
C
         DO 100 I = L2, N
            D(I) = D(I) - H
  100    CONTINUE
C
  120    F = F + H
C        .......... QL TRANSFORMATION ..........
         P = D(M)
         C = 1.0E0
         C2 = C
         EL1 = E(L1)
         S = 0.0E0
         MML = M - L
C        .......... FOR I=M-1 STEP -1 UNTIL L DO -- ..........
         DO 160 II = 1, MML
            C3 = C2
            C2 = C
            S2 = S
            I = M - II
            G = C*E(I)
            H = C*P
            R = PYTHAG(P,E(I))
            E(I+1) = S*R
            S = E(I)/R
            C = P/R
            P = C*D(I) - S*G
            D(I+1) = H + S*(C*G+S*D(I))
C           .......... FORM VECTOR ..........
            DO 140 K = 1, N
               H = Z(K,I+1)
               Z(K,I+1) = S*Z(K,I) + C*H
               Z(K,I) = C*Z(K,I) - S*H
  140       CONTINUE
C
  160    CONTINUE
C
         P = -S*S2*C3*EL1*E(L)/DL1
         E(L) = S*P
         D(L) = C*P
         TST2 = TST1 + ABS(E(L))
         IF (TST2.GT.TST1) GO TO 80
  180    D(L) = D(L) + F
  200 CONTINUE
C     .......... ORDER EIGENVALUES AND EIGENVECTORS ..........
      DO 260 II = 2, N
         I = II - 1
         K = I
         P = D(I)
C
         DO 220 J = II, N
            IF (D(J).GE.P) GO TO 220
            K = J
            P = D(J)
  220    CONTINUE
C
         IF (K.EQ.I) GO TO 260
         D(K) = D(I)
         D(I) = P
C
         DO 240 J = 1, N
            P = Z(J,I)
            Z(J,I) = Z(J,K)
            Z(J,K) = P
  240    CONTINUE
C
  260 CONTINUE
C
      GO TO 300
C     .......... SET ERROR -- NO CONVERGENCE TO AN
C                EIGENVALUE AFTER 30 ITERATIONS ..........
  280 IERR = L
  300 RETURN
      END
