      SUBROUTINE WGEFA (AR, AI, LDA, N, IPVT, INFO)
      IMPLICIT NONE
C
C FACTORS A DOUBLE-COMPLEX MATRIX BY GAUSSIAN ELIMINATION
C  USUALLY CALLED BY WGECO, BUT IT CAN BE CALLED DIRECTLY WITH A SAVING IN
C  TIME IF RCOND IS NOT NEEDED. (TIME FOR WGECO) = (1 + 9/N)*(TIME FOR WGEFA)
C
C ON ENTRY:
C    A    DOUBLE-COMPLEX(LDA, N),  MATRIX TO BE FACTORED
C    LDA  INTEGER,  LEADING DIMENSION OF THE ARRAY  A
C    N    INTEGER,  ORDER OF THE MATRIX  A
C
C ON RETURN:
C    A     AN UPPER TRIANGULAR MATRIX AND THE MULTIPLIERS USED TO OBTAIN IT.
C           THE FACTORIZATION CAN BE WRITTEN  A = L*U  WHERE L  IS A PRODUCT
C           OF PERMUTATION AND UNIT LOWER TRIANGULAR MATRICES AND  U  IS
C           UPPER TRIANGULAR.
C    IPVT  INTEGER(N),  AN INTEGER VECTOR OF PIVOT INDICES
C    INFO  INTEGER
C           = 0  NORMAL VALUE.
C           = K  IF  U(K,K) .EQ. 0.0 .  THIS IS NOT AN ERROR CONDITION FOR
C                 THIS SUBROUTINE, BUT IT DOES INDICATE THAT WGESL OR WGEDI
C                 WILL DIVIDE BY ZERO IF CALLED.  USE RCOND IN WGECO FOR A
C                 RELIABLE INDICATION OF SINGULARITY
C
C VERSION 07/01/79 CLEVE MOLER, UNIVERSITY OF NEW MEXICO, ARGONNE NATIONAL LAB.
C
      INTEGER LDA, N, IPVT(*), INFO
      DOUBLE PRECISION AR(LDA,*), AI(LDA,*)
C
      DOUBLE PRECISION TR, TI
      INTEGER J, K, KP1, L, NM1
      DOUBLE PRECISION ZDUMR, ZDUMI
C
      INTEGER IWAMAX
      DOUBLE PRECISION CABS1
C
C
      CABS1 (ZDUMR, ZDUMI) = DABS (ZDUMR)+DABS (ZDUMI)
C
C
C ***      GAUSSIAN ELIMINATION WITH PARTIAL PIVOTING
      INFO = 0
      NM1 = N-1
      IF (NM1.LT.1) GO TO 70
      DO 60 K = 1, NM1
        KP1 = K+1
C
C ***      FIND L = PIVOT INDEX
        L = IWAMAX (N-K+1, AR(K,K), AI(K,K), 1)+K-1
        IPVT(K) = L
C
C ***      ZERO PIVOT IMPLIES THIS COLUMN ALREADY TRIANGULARIZED
        IF (CABS1 (AR(L,K), AI(L,K)).EQ.0.0D0) GO TO 40
C
C ***      INTERCHANGE IF NECESSARY
        IF (L.EQ.K) GO TO 10
        TR = AR(L,K)
        TI = AI(L,K)
        AR(L,K) = AR(K,K)
        AI(L,K) = AI(K,K)
        AR(K,K) = TR
        AI(K,K) = TI
10      CONTINUE
C
C ***      COMPUTE MULTIPLIERS
        CALL WDIV (-1.0D0, 0.0D0, AR(K,K), AI(K,K), TR, TI)
        CALL WSCAL (N-K, TR, TI, AR(K+1,K), AI(K+1,K), 1)
C
C ***      ROW ELIMINATION WITH COLUMN INDEXING
        DO 30 J = KP1, N
          TR = AR(L,J)
          TI = AI(L,J)
          IF (L.EQ.K) GO TO 20
          AR(L,J) = AR(K,J)
          AI(L,J) = AI(K,J)
          AR(K,J) = TR
          AI(K,J) = TI
20        CONTINUE
          CALL WAXPY (N-K, TR, TI, AR(K+1,K), AI(K+1,K),
     .                1, AR(K+1,J), AI(K+1,J), 1)
30      CONTINUE
        GO TO 50
C
40      CONTINUE
        INFO = K
50      CONTINUE
60    CONTINUE
C
70    CONTINUE
      IPVT(N) = N
      IF (CABS1 (AR(N,N), AI(N,N)).EQ.0.0D0) INFO = N
C
      RETURN
      END
