      SUBROUTINE CLUIMP(A, KA, N, Q, KQ, IPVT, B, X, R, IND)
C ----------------------------------------------------------------------
C     PURPOSE
C       GIVEN AN APPROXIMATE SOLUTION X OF A COMPLEX SYSTEM AX = B
C       OBTAINED USING CGECO OR CGEFA. CLUIMP ATTEMPTS TO COMPUTE
C       AN IMPROVED SOLUTION CORRECT TO MACHINE PRECISION.
C
C     PARAMETERS
C
C       A    A COMPLEX ARRAY OF DIMENSION (KA,N) CONTAINING THE
C            MATRIX A OF ORDER N.
C       Q    A COMPLEX ARRAY OF DIMENSION (KQ,N) CONTAINING THE
C            LU DECOMPOSITION OF A PRODUCED BY CGECO OR CGEFA.
C       IPVT AN ARRAY OF DIMENSION N CONTAINING THE PERMUTATION
C            INFORMATION GIVEN BY CGECO OR CGEFA.
C       B    THE RIGHT HAND SIDE OF THE EQUATION AX = B.
C       X    ON INPUT X IS THE APPROXIMATE SOLUTION OF AX = B TO
C            BE IMPROVED. ON OUTPUT X IS THE SOLUTION OBTAINED.
C       R    A COMPLEX ARRAY FOR INTERNAL USE BY THE ROUTINE.
C       IND  VARIABLE THAT REPORTS THE STATUS OF THE RESULTS.
C            IND = 0 IF IMPROVEMENT OF X IS SUCCESSFUL WITH A
C            GAIN IN ACCURACY OF AT LEAST 50 PER CENT EACH
C            ITERATION. OTHERWISE IND = 1.
C
C ----------------------------------------------------------------------
      COMPLEX A(KA,N), Q(KQ,N), B(N), X(N), R(N)
      INTEGER IPVT(N)
      DOUBLE PRECISION RA, IA, RX, IX, RSUM, ISUM
C
C     ********** EPS IS A MACHINE DEPENDENT PARAMETER. ASSIGN EPS
C                THE VALUE U WHERE U IS THE SMALLEST FLOATING POINT
C                NUMBER SUCH THAT 1.0 + U .GT. 1.0.
C
      EPS = SPMPAR(1)
C
      IND = 0
      XNRM = 0.0
      DO 10 I = 1,N
   10 XNRM = XNRM + (REAL(X(I))**2 + AIMAG(X(I))**2)
      IF (XNRM .EQ. 0.0) RETURN
      EPS2 = EPS*EPS
      RATIO = 1.0
C
C                  COMPUTE THE RESIDUAL VECTOR
C
   20 DO 22 I = 1,N
      RSUM = DBLE(REAL(B(I)))
      ISUM = DBLE(AIMAG(B(I)))
        DO 21 J = 1,N
        RA = DBLE(REAL(A(I,J)))
        IA = DBLE(AIMAG(A(I,J)))
        RX = DBLE(REAL(X(J)))
        IX = DBLE(AIMAG(X(J)))
        RSUM = RSUM - RA*RX + IA*IX
   21   ISUM = ISUM - RA*IX - IA*RX
   22 R(I) = CMPLX(SNGL(RSUM),SNGL(ISUM))
C
C                  FIND THE CORRECTION VECTOR
C
      CALL CGESL(Q, KQ, N, IPVT, R, 0)
      RNRM = 0.0
      DO 30 I = 1,N
   30 RNRM = RNRM + (REAL(R(I))**2 + AIMAG(R(I))**2)
      IF (RNRM .LE. EPS2*XNRM) RETURN
C
C                FORM A NEW APPROXIMATE SOLUTION
C
      DO 40 I = 1,N
   40 X(I) = X(I) + R(I)
      XNRM = 0.0
      DO 41 I = 1,N
   41 XNRM = XNRM + (REAL(X(I))**2 + AIMAG(X(I))**2)
C
      IF (XNRM .EQ. 0.0) RETURN
      RAT = RATIO
      RATIO = RNRM/XNRM
      IF (RATIO .LE. 0.25*RAT) GO TO 20
C
      IF (RATIO .GT. AMIN1(RAT,4.0*EPS2)) IND = 1
      RETURN
      END
