      SUBROUTINE WGESL (AR, AI, LDA, N, IPVT, BR, BI, JOB)
      IMPLICIT NONE
C
C SOLVES THE DOUBLE-COMPLEX SYSTEM  A * X = B  OR  CTRANS(A) * X = B
C  USING THE FACTORS COMPUTED BY WGECO OR WGEFA.
C
C ON ENTRY:
C    A     DOUBLE-COMPLEX(LDA, N),  OUTPUT FROM WGECO OR WGEFA
C    LDA   INTEGER,  LEADING DIMENSION OF THE ARRAY A
C    N     INTEGER,  ORDER OF THE MATRIX A
C    IPVT  INTEGER(N),  PIVOT VECTOR FROM WGECO OR WGEFA
C    B     DOUBLE-COMPLEX(N),  RIGHT HAND SIDE VECTOR
C    JOB   INTEGER
C           = 0       TO SOLVE  A*X = B,
C           = NONZERO TO SOLVE  CTRANS(A)*X = B  WHERE
C                      CTRANS(A)  IS THE CONJUGATE TRANSPOSE
C
C ON RETURN:
C    B     SOLUTION VECTOR X
C
C ERROR CONDITION:
C    DIVISION BY ZERO WILL OCCUR IF THE INPUT FACTOR CONTAINS A ZERO ON THE
C     DIAGONAL.  TECHNICALLY THIS INDICATES SINGULARITY BUT IT IS OFTEN CAUSED
C     BY IMPROPER ARGUMENTS OR IMPROPER SETTING OF LDA .  IT WILL NOT OCCUR IF
C     THE SUBROUTINES ARE CALLED CORRECTLY AND IF WGECO HAS SET RCOND .GT. 0.0
C     OR WGEFA HAS SET INFO .EQ. 0
C
C TO COMPUTE  INVERSE(A) * C  WHERE  C  IS A MATRIX WITH  P  COLUMNS:
C    CALL WGECO(A,LDA,N,IPVT,RCOND,Z)
C    IF (RCOND IS TOO SMALL) GO TO ...
C    DO 10 J = 1, P
C      CALL WGESL(A,LDA,N,IPVT,C(1,J),0)
C 10 CONTINUE
C
C VERSION 07/01/79 CLEVE MOLER, UNIVERSITY OF NEW MEXICO, ARGONNE NATIONAL LAB.
C
      INTEGER LDA, N, IPVT(*), JOB
      DOUBLE PRECISION AR(LDA,*), AI(LDA,*), BR(*), BI(*)
C
      DOUBLE PRECISION TR, TI
      INTEGER K, KB, L, NM1
C
      DOUBLE PRECISION WDOTCR, WDOTCI
C
C
      NM1 = N-1
      IF (JOB.NE.0) GO TO 50
C
C ***      SOLVE  A * X = B,  FIRST SOLVE  L*Y = B
      IF (NM1.LT.1) GO TO 30
      DO 20 K = 1, NM1
        L = IPVT(K)
        TR = BR(L)
        TI = BI(L)
        IF (L.EQ.K) GO TO 10
        BR(L) = BR(K)
        BI(L) = BI(K)
        BR(K) = TR
        BI(K) = TI
10      CONTINUE
        CALL WAXPY (N-K, TR, TI, AR(K+1,K), AI(K+1,K),
     .              1, BR(K+1), BI(K+1), 1)
20    CONTINUE
30    CONTINUE
C
C ***      NOW SOLVE  U*X = Y
      DO 40 KB = 1, N
        K = N+1-KB
        CALL WDIV (BR(K), BI(K), AR(K,K), AI(K,K), BR(K), BI(K))
        TR = -BR(K)
        TI = -BI(K)
        CALL WAXPY (K-1, TR, TI, AR(1,K), AI(1,K), 1, BR(1), BI(1), 1)
40    CONTINUE
      GO TO 100
C
C ***      SOLVE  CTRANS(A) * X = B,  FIRST SOLVE  CTRANS(U)*Y = B
50    CONTINUE
      DO 60 K = 1, N
        TR = BR(K)-WDOTCR (K-1, AR(1,K), AI(1,K), 1, BR(1), BI(1), 1)
        TI = BI(K)-WDOTCI (K-1, AR(1,K), AI(1,K), 1, BR(1), BI(1), 1)
        CALL WDIV (TR, TI, AR(K,K), -AI(K,K), BR(K), BI(K))
60    CONTINUE
C
C ***      NOW SOLVE CTRANS(L)*X = Y
      IF (NM1.LT.1) GO TO 90
      DO 80 KB = 1, NM1
        K = N-KB
        BR(K) = BR(K)+WDOTCR (N-K, AR(K+1,K), AI(K+1,K),
     .                        1, BR(K+1), BI(K+1), 1)
        BI(K) = BI(K)+WDOTCI (N-K, AR(K+1,K), AI(K+1,K),
     .                        1, BR(K+1), BI(K+1), 1)
        L = IPVT(K)
        IF (L.EQ.K) GO TO 70
        TR = BR(L)
        TI = BI(L)
        BR(L) = BR(K)
        BI(L) = BI(K)
        BR(K) = TR
        BI(K) = TI
70      CONTINUE
80    CONTINUE
C
90    CONTINUE
100   CONTINUE
C
      RETURN
      END
