      SUBROUTINE IEGS(KERNEL,RHFCN,A,B,EP,IFLAG,X,T,NT,NUP,MUP,IER,
     *   CUTOFF,ROOTRT,UNITRD,NHALF,LUFACT,KMM,KMN,KNM,RHS,R,RH,
     *   DELN,TM,TN,XM,XMZ,WM,WN,OLDX,SAVE,XN,SAVE2,ASIDE,ASIDE3,
     *   IMKNN)
C
C   THIS ROUTINE CONTROLS THE SOLUTION OF THE INTEGRAL EQUATION.
C
      REAL KERNEL,LUFACT,KMM,KMN,KNM,IMKNN,NORMK,NUMR1,NUMR2
      INTEGER FLAG,OLDM,PIVOT(128)
      DIMENSION X(1),T(1),LUFACT(NUP,NUP),KMM(NUP,NUP),RHS(MUP),
     *   KNM(NHALF,NUP),KMN(NUP,NHALF),R(MUP),RH(NUP),DELN(NUP),
     *   TM(MUP),TN(NUP),XM(MUP),XMZ(MUP),WM(MUP),WN(NUP),OLDX(MUP),
     *   SAVE(MUP),XN(NUP),SAVE2(MUP),ASIDE(NUP,4),ASIDE2(5),
     *   ASIDE3(NUP,NUP),IMKNN(NUP,NUP)
      COMMON/XXLIN/ELINSY,RELRSD,PIVOT
      COMMON/XXINFO/R1,R2,FINLEP,NORMK,NFINAL,MFINAL
      EXTERNAL KERNEL,RHFCN
C
C   INITIALIZATION
C
      LOOP=1
      N=2
      R2=0.5
      M=2*N
      R1RAT=ROOTRT
      COND=1.0
      PASTC=1.0
      PASTRE=0.0
      EPS=EP
C
C   STAGE A. DIRECT SOLUTION OF LINEAR SYSTEM (I-KN)*XN=RHS, WHILE
C   TRYING TO FIND A GOOD APPROXIMATE INVERSE TO IMPLEMENT
C   ITERATIVE METHOD OF SOLUTION.
C
C   CREATE THE NODES AND WEIGHTS TN(I) AND WN(I), I=1,...,N
      CALL WANDT(WN,TN,N,A,B)
C   SET UP MATRIX FOR (I-KN)*XN=RHFCN
      DO 2 J=1,N
      DO 1 I=1,N
1     IMKNN(I,J)=-WN(J)*KERNEL(TN(I),TN(J))
      XMZ(J)=RHFCN(TN(J))
2     IMKNN(J,J)=IMKNN(J,J)+1.0
      GO TO 6
C   THIS IS ENTRANCE FOR AN INCREASED VALUE OF N, USING PREVIOUSLY
C   STORED VALUES IN KMM TO DEFINE MATRIX FOR (I-KN)*XN=RHFCN WITH
C   NEW VALUE OF N.
3     DO 5 J=1,N
      DO 4 I=1,N
4     IMKNN(I,J)=-KMM(I,J)
      WN(J)=WM(J)
      TN(J)=TM(J)
      XMZ(J)=RHS(J)
5     IMKNN(J,J)=IMKNN(J,J)+1.0
C   THIS IS THE ENTRANCE WHEN ITERATION IN STAGE B FAILS AND WE NEED
C   TO INCREASE N TO OBTAIN A BETTER ITERATIVE RATE.
6     CONTINUE
C   SOLVE (I-KN)*XN=RHFCN AT ALL TN(I).ALSO OBTAIN THE LU
C   DECOMPOSITION FOR LATER USE IN THE STAGE B ITERATIVE METHOD.
C
C*******************************************************************
C                                                                  *
      CALL LNSYS(IMKNN,LUFACT,NUP,N,XMZ,XN,2,IND)
C                                                                  *
C   LNSYS IS A GENERAL LINEAR EQUATION SOLVER. IT HAS SPECIAL      *
C   OPTIONS WHICH ARE USED IN THE FOLLOWING PROGRAM. THUS IT       *
C   SHOULD NOT BE REPLACED WITH ANOTHER SOLVER. LNSYS IS ALSO      *
C   USED IN THE SUBROUTINE ITERT.                                  *
C                                                                  *
C*******************************************************************
C
      COND=CONEW(COND,ELINSY,RELRSD,AVERR,PASTC,PASTRE)
      RELMIN=RMIN(N,N,COND,UNITRD,AVERR)
      IF(LOOP .EQ. 1) GO TO 11
      IF(LOOP .EQ. 2) GO TO 9
C   SET UP APPROXIMATE RATE OF CONVERGENCE OF SOLUTIONS XN TO TRUE
C   SOLUTION X. ALSO SET UP DESIRED RATIO FOR ITERATIVE METHOD.
      NUMR2=RNRM(XN,OLDX,N,1)
      R2=AMIN1(0.5,AMAX1(NUMR2/DENR2,1.0E-4))
      R1RAT=AMIN1(ROOTRT,SQRT(R2))
C   CHECK FOR ERROR IN XN USING TEST INVOLVING R2 AND OLDX,ACCORDING
C   TO THEORY FOR ASYMPTOTIC ERROR BOUNDS. MODIFY ERROR IF IT IS
C   OUTSIDE PRECISION RANGE OF COMPUTER, POSSIBLY DUE TO
C   ILL-CONDITIONING.
8     ERROR=(R2/(1.0-R2))*NUMR2
      XNORM=RNRM(XN,XN,N,0)
      RELERR=ERROR/XNORM
      IF(IFLAG .EQ. 0) EPS=AMAX1(EP,XNORM*RELMIN)
      IF(IFLAG .EQ. 1) EPS=AMAX1(EP,RELMIN)
      IF(IFLAG .EQ. 1)ERROR=AMAX1(RELERR,RELMIN)
      IF((IFLAG .EQ. 0) .AND. (RELERR .LT. RELMIN))
     *   ERROR=RELMIN*XNORM
      IF(ERROR .LE. EPS) GO TO 10
      DENR2=NUMR2
      GO TO 11
C   ENTRANCE FOR LOOP=2.
9     NUMR2=RNRM(XN,OLDX,N,1)
      DENR2=0.0
      GO TO 8
C   EXIT FOR SUCCESSFUL RETURN. ITERATION WAS NOT NECESSARY.
10    CALL LEAVE(0,N,N,XN,TN,WN,ERROR,
     *   KERNEL,RHFCN,EP,IFLAG,X,T,NT,IER,EPS,ELINSY,TN,WN,TM,WM,XM,
     *   XMZ,KMM,KMN,KNM,RHS,IMKNN,LUFACT,R,RH,DELN,NUP,NHALF,XNORM)
      RETURN
C
C   ATTEMPT TO SOLVE (I-KM)*XM=RHFCN ITERATIVELY, CHECKING TO SEE IF
C   THE RATE OF CONVERGENCE IS SUFFICIENTLY FAST SO AS TO ENTER
C   STAGE B.
C
C   CALCULATE TM(I) AND WM(I), I=1,...,M.
11    CALL WANDT(WM,TM,M,A,B)
      FLAG=0
C   CALCULATE INITIAL GUESS XMZ FOR ITERATION METHOD.
      CALL NSTERP(TM,WM,XMZ,M,TN,WN,XN,N,
     *   KERNEL,RHFCN,RHS,KMN,NHALF,NUP)
      DO 12 I=1,M
12    OLDX(I)=XMZ(I)
C   CALCULATE FIRST ITERATE.
      CALL ITERT(KERNEL,RHFCN,N,TN,WN,M,TM,WM,XM,XMZ,KMM,KMN,KNM,
     *   RHS,IMKNN,LUFACT,R,RH,DELN,NUP,NHALF,FLAG)
      COND=CONEW(COND,ELINSY,RELRSD,AVERR,PASTC,PASTRE)
      DENR1=RNRM(XM,XMZ,M,1)
      FLAG=1
      DO 13 I=1,M
13    XMZ(I)=XM(I)
C   CALCULATE SECOND ITERATE.
      CALL ITERT(KERNEL,RHFCN,N,TN,WN,M,TM,WM,XM,XMZ,KMM,KMN,KNM,
     *   RHS,IMKNN,LUFACT,R,RH,DELN,NUP,NHALF,FLAG)
      COND=CONEW(COND,ELINSY,RELRSD,AVERR,PASTC,PASTRE)
      NUMR1=RNRM(XM,XMZ,M,1)
C   CHECK ON THE SPEED OF CONVERGENCE OF ITERATIVE METHOD. IF IT IS
C   SUFFICIENTLY RAPID, THEN FIX N AND GO TO STAGE B.
      R1=AMAX1(NUMR1/DENR1,.0001)
      RATE=R1
      IF(M .GT. NUP) GO TO 19
      IF(R1 .LE. R1RAT) GO TO 15
C   THE ITERATION DID NOT WORK WELL ENOUGH, AND STAGE A IS TO BE
C   REPEATED. RE-INITIALIZE FOR SOLVING (I-KN)*XN=RHFCN AGAIN
C   WITH A LARGER N.
14    N=M
      LOOP=LOOP+1
      M=2*N
      GO TO 3
C   THE ITERATIVE RATE IS SUFFICIENTLY RAPID, AND CONTROL WILL GO TO
C   STAGE B. SAVE INFORMATION IN CASE STAGE B ABORTS AT A LARGER
C   VALUE OF M AND STAGE A HAS TO BE RETURNED TO.
15    DO 16 I=1,M
      ASIDE(I,1)=OLDX(I)
      ASIDE(I,2)=WM(I)
      ASIDE(I,3)=TM(I)
16    ASIDE(I,4)=RHS(I)
      ASIDE2(1)=LOOP
      ASIDE2(3)=R2
      ASIDE2(4)=DENR2
      ASIDE2(5)=R1RAT
      DO 17 J=1,M
      DO 17 I=1,M
17    ASIDE3(I,J)=KMM(I,J)
C
C   STAGE B. ITERATIVE METHOD OF SOLUTION OF (I-KM)*XM=RHS.
C
19    OLDM=N
      ASIDE2(2)=M
      IF(R1 .LE. CUTOFF) GO TO 22
C   THE ITERATES ARE CONVERGING VERY SLOWLY OR NOT AT ALL. THUS
C   RETURN WITHOUT FURTHER ATTEMPTS TO LESSEN THE ERROR.
      IF(LOOP .NE. 1) GO TO 21
20    CALL LEAVE(2,N,N,XN,TN,WN,0.0,
     *   KERNEL,RHFCN,EP,IFLAG,X,T,NT,IER,EPS,ELINSY,TN,WN,TM,WM,XM,
     *   XMZ,KMM,KMN,KNM,RHS,IMKNN,LUFACT,R,RH,DELN,NUP,NHALF,XNORM)
      RETURN
21    CALL LEAVE(1,N,N,XN,TN,WN,ERROR,
     *   KERNEL,RHFCN,EP,IFLAG,X,T,NT,IER,EPS,ELINSY,TN,WN,TM,WM,XM,
     *   XMZ,KMM,KMN,KNM,RHS,IMKNN,LUFACT,R,RH,DELN,NUP,NHALF,XNORM)
      RETURN
C   TEST TO SEE IF THE CURRENT ITERATE XM IS SUFFICIENTLY ACCURATE
C   COMPARED TO THE TRUE XM.
22    RATE=R1*RATE
      TEMP=RNRM(XM,OLDX,M,1)
      IF(LOOP .EQ. 1) TEMP2=0.5
      IF(LOOP .GT. 1) TEMP2=TEMP/DENR2
      RT=  AMIN1(0.01,AMAX1(TEMP2,0.0001))/2.0
      XNORM=RNRM(XM,XM,M,0)
      ESTERR=(RT/(1.0-RT))*TEMP/XNORM
      IF(ESTERR .LT. RELMIN) ESTERR=RELMIN
      ESTERR=ESTERR*XNORM
      TEST=((1.0-R1)/R1)*ESTERR
      IF(NUMR1 .LE. TEST) GO TO 33
C   ITERATE NOT SUFFICIENTLY ACCURATE. INITIALIZE FOR COMPUTATION
C   OF ANOTHER ITERATE.
25    DENR1=NUMR1
      DO 26 I=1,M
26    XMZ(I)=XM(I)
      CALL ITERT(KERNEL,RHFCN,N,TN,WN,M,TM,WM,XM,XMZ,KMM,KMN,KNM,
     *   RHS,IMKNN,LUFACT,R,RH,DELN,NUP,NHALF,FLAG)
      COND=CONEW(COND,ELINSY,RELRSD,AVERR,PASTC,PASTRE)
      NUMR1=RNRM(XM,XMZ,M,1)
      R1=AMAX1(NUMR1/DENR1,.0001)
      IF(R1 .LE. CUTOFF) GO TO 22
C   THIS IS ENTRANCE FOR CASE WHERE ITERATION FAILS IN STAGE B.
C   PARAMETERS MUST BE RESET FOR A RETURN TO STAGE A OR FOR AN
C   ABORTIVE EXIT IF N CANNOT BE INCREASED ANY FURTHER.
27    MNEW=ASIDE2(2)
      IF(MNEW .GT. NUP) GO TO 30
      N=MNEW
      DO 29 J=1,N
      DO 28 I=1,N
28    IMKNN(I,J)=-ASIDE3(I,J)
      OLDX(J)=ASIDE(J,1)
      WN(J)=ASIDE(J,2)
      TN(J)=ASIDE(J,3)
      XMZ(J)=ASIDE(J,4)
29    IMKNN(J,J)=IMKNN(J,J)+1.0
      M=2*N
      LOOP=ASIDE2(1)+1.0
      R2=ASIDE2(3)
      DENR2=ASIDE2(4)
      R1RAT=ASIDE2(5)
      GO TO 6
C   ABORTIVE EXIT FROM STAGE B. N CANNOT BE INCREASED FURTHER, AND
C   R1 IS NOT SUFFICIENTLY SMALL.
30    IF(LOOP .EQ. 1) GO TO 20
      CALL WANDT(WM,TM,OLDM,A,B)
      CALL LEAVE(1,N,OLDM,SAVE,TM,WM,ERROR,
     *   KERNEL,RHFCN,EP,IFLAG,X,T,NT,IER,EPS,ELINSY,TN,WN,TM,WM,XM,
     *   XMZ,KMM,KMN,KNM,RHS,IMKNN,LUFACT,R,RH,DELN,NUP,NHALF,XNORM)
      RETURN
C   AN ACCURATE VALUE OF XM HAS BEEN OBTAINED. R2 IS TO BE TESTED AS
C   TO WHETHER IT SHOULD BE RESET. THEN CHECK ERROR IN XM COMPARED
C   WITH THE TRUE SOLUTION X.
33    IF(LOOP .EQ. 1) GO TO 37
      NUMR2=TEMP
      R2=AMAX1(1.0E-4,RATE,AMIN1(NUMR2/DENR2,0.5))
      DENR2=NUMR2
34    ERROR=(R2/(1.0-R2))*TEMP
      XNORM=RNRM(XM,XM,M,0)
      RELERR=ERROR/XNORM
      RELMIN=RMIN(N,M,COND,UNITRD,AVERR)
      IF(IFLAG .EQ. 0) EPS=AMAX1(EP,XNORM*RELMIN)
      IF(IFLAG .EQ. 1) EPS=AMAX1(EP,RELMIN)
      IF(IFLAG .EQ. 1)ERROR=AMAX1(RELERR,RELMIN)
      IF((IFLAG .EQ. 0) .AND. (RELERR .LT. RELMIN))
     *   ERROR=RELMIN*XNORM
      IF(ERROR .GT. EPS) GO TO 35
      CALL LEAVE(0,N,M,XM,TM,WM,ERROR,
     *   KERNEL,RHFCN,EP,IFLAG,X,T,NT,IER,EPS,ELINSY,TN,WN,TM,WM,XM,
     *   XMZ,KMM,KMN,KNM,RHS,IMKNN,LUFACT,R,RH,DELN,NUP,NHALF,XNORM)
      RETURN
35    MNEW=2*M
      IF(MNEW .LE. MUP) GO TO 39
C   M CANNOT BE INCREASED ANY FURTHER.
      CALL LEAVE(1,N,M,XM,TM,WM,ERROR,
     *   KERNEL,RHFCN,EP,IFLAG,X,T,NT,IER,EPS,ELINSY,TN,WN,TM,WM,XM,
     *   XMZ,KMM,KMN,KNM,RHS,IMKNN,LUFACT,R,RH,DELN,NUP,NHALF,XNORM)
      RETURN
37    DENR2=TEMP
      LOOP=2
      GO TO 34
C   ERROR NOT SUFFICIENTLY SMALL. M IS INCREASED AND TWO MORE
C   MORE ITERATES ARE COMPUTED WITH THE NEW M.
39    OLDM=M
      M=MNEW
      DO 41 I=1,OLDM
      SAVE2(I)=WM(I)
41    SAVE(I)=TM(I)
      CALL WANDT(WM,TM,M,A,B)
      FLAG=0
      CALL NSTERP(TM,WM,XMZ,M,SAVE,SAVE2,XM,OLDM,
     *   KERNEL,RHFCN,RHS,KMN,NHALF,NUP)
      DO 43 I=1,OLDM
43    SAVE(I)=XM(I)
      DO 45 I=1,M
45    OLDX(I)=XMZ(I)
      CALL ITERT(KERNEL,RHFCN,N,TN,WN,M,TM,WM,XM,XMZ,KMM,KMN,KNM,
     *   RHS,IMKNN,LUFACT,R,RH,DELN,NUP,NHALF,FLAG)
      COND=CONEW(COND,ELINSY,RELRSD,AVERR,PASTC,PASTRE)
      DENR1=RNRM(XM,XMZ,M,1)
      FLAG=1
      DO 47 I=1,M
47    XMZ(I)=XM(I)
      CALL ITERT(KERNEL,RHFCN,N,TN,WN,M,TM,WM,XM,XMZ,KMM,KMN,KNM,
     *   RHS,IMKNN,LUFACT,R,RH,DELN,NUP,NHALF,FLAG)
      COND=CONEW(COND,ELINSY,RELRSD,AVERR,PASTC,PASTRE)
      NUMR1=RNRM(XM,XMZ,M,1)
      R1=AMAX1(NUMR1/DENR1,.0001)
      RATE=R1
      IF(R1 .LE. CUTOFF) GO TO 22
      GO TO 27
      END
