      SUBROUTINE Y12MCF(N,Z,A,SNR,NN,RNR,NN1,PIVOT,B,HA,IHA, AFLAG,IFLAG
     *,IFAIL)
C
C  SYSTEMS OF LINEAR EQUATIONS ARE SOLVED BY USE OF SPARSE MATRIX TECH-
C  NIQUE AND BY GAUSSIAN ELIMINATION.
C
      IMPLICIT DOUBLE PRECISION(A-B,G,P,T-Y),INTEGER(C,F,H-N,R-S,Z)
      DOUBLE PRECISION A(NN),B(N),PIVOT(N),AFLAG(8)
C
C  INFORMATION WHICH IS NECESSARY TO BEGIN THE ELIMINATION IS STORED.
C
      INTEGER SNR(NN),RNR(NN1),HA(IHA,11), IFLAG(10)
      IFAIL=0
      IF(IFLAG(1).NE.-1)IFAIL=2
      IF(AFLAG(1).LT.1.0D0)AFLAG(1)=1.0005 D0
      IF(AFLAG(3).LT.1.0D+5)AFLAG(3)=1.0D+5
      IF(AFLAG(4).LT.0.0D0)AFLAG(4)=-AFLAG(4)
      IF(IFLAG(2).LT.1)IFAIL=19
      IF(IFLAG(3).LT.0.OR.IFLAG(3).GT.2)IFAIL=20
      IF(IFLAG(5).LT.1.OR.IFLAG(5).GT.3)IFAIL=21
      IF(IFLAG(5).EQ.3)IFAIL=22
      IF(IFAIL.GT.0)GO TO 1110
      SNR(Z+1)=0
      RNR(Z+1)=0
      N8=N+1
      N7=N-1
      U=AFLAG(1)
      GRMIN=AFLAG(4)*AFLAG(6)
C
C  USE THE INFORMATION ABOUT FILL-INS IF IT IS POSSIBLE.
C
      ZZ=Z
      NR=N*N
      IF(IFLAG(4).NE.2)GO TO 100
      IF(IFLAG(10).GT.NN)GO TO 50
      L1=IFLAG(10)
      L5=L1+1
      IF(L5.LE.NN)SNR(L5)=0
      DO 40 I=1,N
      L=N8-I
      L2=HA(L,3)+1
      L3=L2-HA(L,1)
      DO 10 J=1,L3
      SNR(L5-J)=SNR(L2-J)
   10 A(L5-J)=A(L2-J)
      HA(L,3)=L1
      HA(L,1)=L5-L3
      L6=L1-L3
      L5=L5-HA(L,9)
      IF(L5.GT.L6)GO TO 30
      DO 20 J=L5,L6
   20 SNR(J)=0
   30 CONTINUE
   40 L1=L5-1
   50 IF(IFLAG(9).GT.NN1)GO TO 100
      L2=IFLAG(9)
      L5=L2+1
      IF(L5.LE.NN1)RNR(L5)=0
      DO 90 I=1,N
      L=N8-I
      L1=HA(L,6)+1
      L4=L1-HA(L,4)
      DO 60 J=1,L4
   60 RNR(L5-J)=RNR(L1-J)
      HA(L,4)=L5-L4
      HA(L,6)=L2
      L6=L2-L4
      L5=L5-HA(L,10)
      IF(L5.GT.L6)GO TO 80
      DO 70 J=L5,L6
   70 RNR(J)=0
   80 CONTINUE
   90 L2=L5-1
  100 R4=HA(N,3)
      R5=HA(N,6)
      AFLAG(7)=AFLAG(6)
      AFLAG(8)=AFLAG(6)
      DO 110 I=1,N
      PIVOT(I)=0.0 D0
      HA(I,2)=HA(I,1)
  110 HA(I,5)=HA(I,4)
      INDEX=HA(N,8)
C
C  START OF GAUSSIAN ELIMINATION.
C
      SLUT=HA(INDEX,3)-HA(INDEX,2)+1
      DO 950 I=1,N7
      RR3=HA(I,2)
      RR4=HA(I,3)
      C1=HA(I,4)
      CR4=HA(I,6)
      IF(IFLAG(3).EQ.0)GO TO 350
      IF(IFLAG(4).NE.2)GO TO 120
      RROW=HA(I,7)
      RCOLL=HA(I,8)
      GO TO 220
  120 L4=HA(I,8)
      IF(IFLAG(3).EQ.1)GO TO 130
      RROW=L4
      RCOLL=RROW
      RPIVOT=I
      GO TO 170
  130 R=NR
      V=0.0 D0
      INDEX=IFLAG(2)
      DO 160 KK=1,INDEX
      L1=I-1+KK
      IF(L1.GT.N)GO TO 170
      J=HA(L1,8)
      R7=HA(J,2)
      R8=HA(J,3)
      R9=R8-R7
      T=0.0 D0
      DO 140 K=R7,R8
      TD=DABS(A(K))
  140 IF(T.LT.TD)T=TD
      T=T/U
      DO 160 K=R7,R8
      TD=DABS(A(K))
      IF(TD.LT.T)GO TO 150
      R6=SNR(K)
      R3=R9*(HA(R6,6)-HA(R6,5))
      IF(R3.GT.R)GO TO 150
      IF(R3.LT.R)GO TO 151
      IF(V.GE.TD)GO TO 150
  151 V=TD
      RROW=J
      RCOLL=R6
      R=R3
      RPIVOT=L1
  150 CONTINUE
  160 CONTINUE
  170 R3=HA(RCOLL,10)
      HA(RCOLL,10)=HA(I,10)
      HA(I,10)=R3
      R3=HA(RROW,9)
      HA(RROW,9)=HA(I,9)
C
C  REMOVE THE PIVOT ROW OF THE LIST WHERE THE ROWS ARE ORDERED BY
C  INCREASING NUMBERS OF NON-ZERO ELEMENTS.
C
      HA(I,9)=R3
      L1=0
      L=I
      L2=HA(L4,3)-HA(L4,2)+1
  180 L=L+1
      IF(L2.GT.L1)HA(L2,11)=L
      IF(L.GT.N)GO TO 190
      L5=HA(L,8)
      L3=HA(L5,3)-HA(L5,2)+1
      IF(RPIVOT.LT.L)GO TO 190
      HA(L4,7)=L
      HA(L,8)=L4
      L4=L5
      L1=L2
      L2=L3
      L3=N8
      GO TO 180
  190 IF(L2.EQ.L1)GO TO 200
      IF(L3.EQ.L2)GO TO 200
      HA(L2,11)=0
  200 L5=HA(I,7)
      IF(RROW.EQ.I)GO TO 210
      HA(L5,8)=RROW
      HA(RROW,7)=L5
  210 HA(I,7)=RROW
C
C  ROW INTERCHANGES.
C
      HA(I,8)=RCOLL
  220 IF(RROW.EQ.I)GO TO 290
      T=B(RROW)
      B(RROW)=B(I)
      B(I)=T
      DO 250 J=RR3,RR4
      L1=SNR(J)
      R=HA(L1,5)-1
      R10=HA(L1,6)
  240 R=R+1
      IF(RNR(R).NE.I)GO TO 240
      RNR(R)=RNR(R10)
  250 RNR(R10)=RROW
      RR3=HA(RROW,2)
      RR4=HA(RROW,3)
      DO 270 J=RR3,RR4
      L1=SNR(J)
      R=HA(L1,5)-1
  260 R=R+1
      IF(RNR(R).NE.RROW)GO TO 260
  270 RNR(R)=I
      DO 280 J=1,3
      R3=HA(RROW,J)
      HA(RROW,J)=HA(I,J)
C
C  COLUMN INTERCHANGES.
C
  280 HA(I,J)=R3
  290 IF(RCOLL.EQ.I)GO TO 350
      DO 310 J=C1,CR4
      L1=RNR(J)
      R=HA(L1,2)-1
      R10=HA(L1,3)
  300 R=R+1
      IF(SNR(R).NE.I)GO TO 300
      T=A(R10)
      A(R10)=A(R)
      A(R)=T
      SNR(R)=SNR(R10)
  310 SNR(R10)=RCOLL
      C1=HA(RCOLL,4)
      CR4=HA(RCOLL,6)
      DO 330 J=C1,CR4
      L1=RNR(J)
      R=HA(L1,2)-1
  320 R=R+1
      IF(SNR(R).NE.RCOLL)GO TO 320
  330 SNR(R)=I
      DO 340 J=4,6
      R3=HA(RCOLL,J)
      HA(RCOLL,J)=HA(I,J)
C
C END OF THE INTERCHANGES.
C THE ROW ORDERED LIST AND THE COLUMN ORDERED LIST ARE PREPARED TO
C BEGIN STEP I OF THE ELIMINATION.
C
  340 HA(I,J)=R3
  350 R9=RR4-RR3
      DO 360 RR=RR3,RR4
      IF(SNR(RR).EQ.I)GO TO 370
  360 CONTINUE
      IFAIL=9
      GO TO 1110
  370 V=A(RR)
      PIVOT(I)=V
      TD=DABS(V)
      IF(TD.LT.AFLAG(8))AFLAG(8)=TD
      IF(TD.GE.GRMIN)GO TO 380
      IFAIL=3
      GO TO 1110
  380 R2=HA(I,1)
      A(RR)=A(RR3)
      SNR(RR)=SNR(RR3)
      A(RR3)=A(R2)
      SNR(RR3)=SNR(R2)
      SNR(R2)=0
      Z=Z-1
      RR3=RR3+1
      HA(I,2)=RR3
      HA(I,1)=R2+1
      CR3=HA(I,5)
      IF(R9.LE.0)GO TO 431
      DO 430 J=RR3,RR4
      INDEX=SNR(J)
  430 PIVOT(INDEX)=A(J)
  431 R7=CR4-CR3+1
      DO 880 K=1,R7
      R1=RNR(CR3-1+K)
      IF(R1.EQ.I)GO TO 870
      I1=HA(R1,1)
      RR1=HA(R1,2)
      RR2=HA(R1,3)
      L2=RR2-RR1+1
      L=RR1-1
  390 L=L+1
      IF(SNR(L).NE.I)GO TO 390
      T=A(L)/V
      IF(IFLAG(5).EQ.2)GO TO 400
      A(L)=A(I1)
      SNR(L)=SNR(I1)
      SNR(I1)=0
      I1=I1+1
      HA(R1,1)=I1
      Z=Z-1
      GO TO 410
  400 A(L)=A(RR1)
      A(RR1)=T
      R3=SNR(RR1)
      SNR(RR1)=SNR(L)
      SNR(L)=R3
  410 RR1=RR1+1
      HA(R1,2)=RR1
      B(R1)=B(R1)-B(I)*T
      IF(R9.LE.0)GO TO 669
      R=RR1
      IF(R.GT.RR2)GO TO 470
      DO 460 L=R,RR2
      L1=SNR(L)
      TD=PIVOT(L1)
      IF(TD.EQ.0.0D0)GO TO 450
      PIVOT(L1)=0.0 D0
      TD=A(L)-TD*T
      A(L)=TD
      TD1=DABS(TD)
      IF(TD1.GT.AFLAG(7))AFLAG(7)=TD1
C
C  TOO SMALL ELEMENT IS CREATED.REMOVE IT FROM THE LISTS.
C
      IF(TD1.GT.AFLAG(2))GO TO 450
      Z=Z-1
      A(L)=A(RR1)
      SNR(L)=SNR(RR1)
      A(RR1)=A(I1)
      SNR(RR1)=SNR(I1)
      SNR(I1)=0
      RR1=RR1+1
      I1=I1+1
      HA(R1,2)=RR1
      HA(R1,1)=I1
      R3=HA(L1,5)
      R2=R3-1
      L4=HA(L1,4)
      L5=RNR(L4)
      L6=RNR(R3)
  440 R2=R2+1
      IF(RNR(R2).NE.R1)GO TO 440
      RNR(R2)=L6
      RNR(R3)=L5
      RNR(L4)=0
      HA(L1,5)=R3+1
      HA(L1,4)=L4+1
  450 CONTINUE
  460 CONTINUE
  470 CONTINUE
      DO 750 J=1,R9
      R=RR3-1+J
      R2=SNR(R)
      TOL2=PIVOT(R2)
      PIVOT(R2)=A(R)
      IF(TOL2.EQ.0.0D0)GO TO 740
      TOL3=-TOL2*T
      TOL1=DABS(TOL3)
      IF(TOL1.LT.AFLAG(2))GO TO 740
      C2=HA(R2,4)
      CR2=HA(R2,6)
      CR1=HA(R2,5)
      LFR=RR2-I1+2
      LFC=CR2-C2+2
      IF(IFLAG(4).NE.1)GO TO 480
      IF(LFR.GT.HA(R1,9))HA(R1,9)=LFR
      IF(LFC.GT.HA(R2,10))HA(R2,10)=LFC
  480 IF(I1.EQ.1)GO TO 490
      IF(SNR(I1-1).EQ.0)GO TO 600
  490 IF(RR2.EQ.NN)GO TO 500
      IF(SNR(RR2+1).EQ.0)GO TO 580
C
C  COLLECTION IN ROW ORDERED LIST.
C
  500 R10=NN-LFR
      IF(R10.GE.R4)GO TO 560
      IFLAG(6)=IFLAG(6)+1
      DO 520 JJ=1,N
      L1=HA(JJ,3)
      IF(L1.LT.HA(JJ,1))GO TO 510
      HA(JJ,3)=SNR(L1)
      SNR(L1)=-JJ
  510 CONTINUE
  520 CONTINUE
      L3=0
      L4=1
      DO 550 JJ=1,R4
      IF(SNR(JJ).EQ.0)GO TO 540
      L3=L3+1
      IF(SNR(JJ).GT.0)GO TO 530
      L5=-SNR(JJ)
      SNR(JJ)=HA(L5,3)
      HA(L5,3)=L3
      L6=L4+HA(L5,2)-HA(L5,1)
      HA(L5,2)=L6
      HA(L5,1)=L4
      L4=L3+1
  530 A(L3)=A(JJ)
      SNR(L3)=SNR(JJ)
  540 CONTINUE
  550 CONTINUE
      R4=L3
      SNR(L3+1)=0
      RR3=HA(I,2)
      RR4=HA(I,3)
      I1=HA(R1,1)
      RR1=HA(R1,2)
      R=RR3-1+J
      IF(R10.GE.R4)GO TO 560
      IFAIL=5
C
C FILL-IN TAKES PLACE IN THE ROW ORDERED LIST.
C
      GO TO 1110
  560 R8=LFR-1
      RR2=R4+LFR
      IF(R8.LE.0)GO TO 579
      L3=I1-1
      DO 570 LL=1,R8
      L4=R4+LL
      L5=L3+LL
      A(L4)=A(L5)
      SNR(L4)=SNR(L5)
  570 SNR(L5)=0
  579 RR1=R4+RR1-I1+1
      HA(R1,3)=RR2
      HA(R1,2)=RR1
      I1=R4+1
      HA(R1,1)=I1
      L1=RR2
      GO TO 590
  580 RR2=RR2+1
      HA(R1,3)=RR2
      L1=RR2
      IF(RR2.LE.R4)GO TO 610
  590 R4=RR2
      IF(R4.LT.NN)SNR(R4+1)=0
      GO TO 610
  600 RR1=RR1-1
      I1=I1-1
      HA(R1,1)=I1
      HA(R1,2)=RR1
      L1=RR1
      SNR(I1)=SNR(L1)
      A(I1)=A(L1)
  610 A(L1)=TOL3
      SNR(L1)=SNR(R)
      TD=DABS(A(L1))
      IF(TD.GT.AFLAG(7))AFLAG(7)=TD
      Z=Z+1
      IF(IFLAG(8).LT.Z) IFLAG(8)=Z
      IF(C2.EQ.1)GO TO 620
      IF(RNR(C2-1).EQ.0)GO TO 720
  620 IF(CR2.EQ.NN1)GO TO 630
      IF(RNR(CR2+1).EQ.0)GO TO 700
C
C  COLLECTION IN COLUMN ORDERED LIST.
C
  630 R10=NN1-LFC
      IF(R10.GE.R5)GO TO 680
      IFLAG(7)=IFLAG(7)+1
      DO 640 JJ=I,N
      L1=HA(JJ,6)
      HA(JJ,6)=RNR(L1)
  640 RNR(L1)=-JJ
      L3=0
      L4=1
      DO 670 JJ=1,R5
      IF(RNR(JJ).EQ.0)GO TO 660
      L3=L3+1
      IF(RNR(JJ).GT.0)GO TO 650
      L5=-RNR(JJ)
      RNR(JJ)=HA(L5,6)
      HA(L5,6)=L3
      L6=L4+HA(L5,5)-HA(L5,4)
      HA(L5,5)=L6
      HA(L5,4)=L4
      L4=L3+1
  650 RNR(L3)=RNR(JJ)
  660 CONTINUE
  670 CONTINUE
      R5=L3
      RNR(R5+1)=0
      C2=HA(R2,4)
      CR3=HA(I,5)
      CR4=HA(I,6)
      CR1=HA(R2,5)
      IF(R10.GE.R5)GO TO 680
      IFAIL=6
C
C FILL-IN TAKES PLACE IN THE COLUMN ORDERED LIST.
C
      GO TO 1110
  680 R8=LFC-1
      CR2=R5+LFC
      IF(R8.LE.0)GO TO 699
      L3=C2-1
      DO 690 L=1,R8
      L4=R5+L
      L5=L3+L
      RNR(L4)=RNR(L5)
  690 RNR(L5)=0
  699 CR1=R5+CR1-C2+1
      C2=R5+1
      HA(R2,6)=CR2
      HA(R2,4)=C2
      HA(R2,5)=CR1
      R=CR2
      GO TO 710
  700 CR2=CR2+1
      HA(R2,6)=CR2
      R=CR2
      IF(CR2.LE.R5)GO TO 730
  710 R5=CR2
      IF(R5.LT.NN1)RNR(R5+1)=0
      GO TO 730
  720 CR1=CR1-1
      C2=C2-1
      HA(R2,4)=C2
      HA(R2,5)=CR1
      R=CR1
      RNR(C2)=RNR(R)
  730 RNR(R)=R1
  740 CONTINUE
  750 CONTINUE
  669 IF(RR1.LE.RR2)GO TO 760
      IFAIL=7
C
C  UPDATE THE INFORMATION IN THE LIST WHERE THE ROWS ARE ORDERED BY
C  INCREASING NUMBERS OF THE NON-ZERO ELEMENTS.
C
      GO TO 1110
  760 IF(IFLAG(4).EQ.2)GO TO 870
      IF(IFLAG(3).EQ.0)GO TO 870
      L1=RR2-RR1+1
      IF(L1.EQ.L2)GO TO 870
      L6=HA(R1,7)
      L4=HA(L2,11)
      IF(L1.GT.L2)GO TO 820
      IF(L6.GT.L4)GO TO 780
      IF(L4.EQ.N)GO TO 770
      L=HA(L4+1,8)
      L5=HA(L,3)-HA(L,2)+1
      IF(L5.EQ.L2)GO TO 790
  770 HA(L2,11)=0
      GO TO 800
  780 L5=HA(L4,8)
      L3=HA(L6,8)
      HA(L4,8)=L3
      HA(L6,8)=L5
      HA(L5,7)=L6
      HA(L3,7)=L4
      L6=L4
  790 HA(L2,11)=L4+1
  800 IF(L4.EQ.I+1)GO TO 810
      L=HA(L6-1,8)
      L2=HA(L,3)-HA(L,2)+1
      L4=HA(L2,11)
      IF(L1.LT.L2)GO TO 780
  810 IF(L1.NE.L2)HA(L1,11)=L6
      GO TO 870
  820 IF(L6.GT.L4)GO TO 840
      IF(L4.EQ.N)GO TO 830
      L=HA(L4+1,8)
      L5=HA(L,3)-HA(L,2)+1
      IF(L5.EQ.L2)GO TO 840
  830 HA(L2,11)=0
  840 L2=L2+1
      IF(L2.LE.SLUT)GO TO 850
      L3=N
      SLUT=L1
      L2=L1
      GO TO 860
  850 L3=HA(L2,11)-1
      IF(L3.EQ.-1)GO TO 840
      IF(L2.GT.L1)L2=L1
  860 HA(L2,11)=L3
      L4=HA(L3,8)
      L7=HA(L6,8)
      HA(L3,8)=L7
      HA(L6,8)=L4
      HA(L7,7)=L3
      HA(L4,7)=L6
      L6=L3
      IF(L2.LT.L1)GO TO 840
  870 CONTINUE
  880 CONTINUE
      IF(R9.LE.0)GO TO 882
      DO 881 J=RR3,RR4
      INDEX=SNR(J)
  881 PIVOT(INDEX)=0.0 D0
  882 CONTINUE
      CR3=HA(I,4)
      DO 890 J=CR3,CR4
  890 RNR(J)=0
      IF(R9.LE.0)GO TO 930
      L2=HA(I,2)-1
      DO 920 LL=1,R9
      R=SNR(L2+LL)
      R1=HA(R,5)
      R2=HA(R,6)
      IF(R2.GT.R1)GO TO 900
      IFAIL=8
      GO TO 1110
  900 HA(R,5)=R1+1
      R3=R1-1
  910 R3=R3+1
      IF(RNR(R3).NE.I)GO TO 910
      RNR(R3)=RNR(R1)
  920 RNR(R1)=I
  930 AFLAG(5)=AFLAG(7)/AFLAG(6)
      IF(AFLAG(5).LT.AFLAG(3))GO TO 940
      IFAIL=4
      GO TO 1110
  940 CONTINUE
C
C  PREPARATION TO BEGIN THE BACK SUBSTITUTION.
C
  950 CONTINUE
      INDEX=HA(N,2)
      PIVOT(N)=A(INDEX)
      A(INDEX)=0.0 D0
      TD=DABS(PIVOT(N))
      IF(TD.GT.AFLAG(7))AFLAG(7)=TD
      IF(TD.LT.AFLAG(8))AFLAG(8)=TD
      IF(TD.GT.GRMIN)GO TO 960
      IFAIL=3
      GO TO 1110
  960 IF(IFLAG(4).NE.1)GO TO 1060
      IFLAG(10)=HA(N,9)
      IFLAG(9)=HA(N,10)
      DO 990 I=1,N7
      R1=N-I
      IFLAG(10)=IFLAG(10)+HA(R1,9)
      IFLAG(9)=IFLAG(9)+HA(R1,10)
      IF(IFLAG(3).EQ.0)GO TO 980
      DO 970 J=9,10
      R2=HA(R1,J-2)
      R6=HA(R2,J)
      HA(R2,J)=HA(R1,J)
  970 HA(R1,J)=R6
  980 CONTINUE
  990 CONTINUE
1060  CONTINUE
      AFLAG(5)=AFLAG(7)/AFLAG(6)
      IFLAG(1)=-2
 1110 Z=ZZ
      RETURN
      END
