C     MATRIX INVERSION WITH ACCOMPANYING SOLUTION OF LINEAR EQUATIONS   ANF40201
C                                                                       F4020002
      SUBROUTINE MATINV(A,N,B,M,DETERM)                                 F4020003
                                                                        F4020004
      DIMENSION IPIVOT(25), A(25,25), B(25,1), INDEX(25,2), PIVOT(25)   F4020005
      COMMON PIVOT, INDEX, IPIVOT                                       F4020006
      EQUIVALENCE (IROW,JROW), (ICOLUM,JCOLUM), (AMAX, T, SWAP)         F4020007
C                                                                       F4020008
C     INITIALIZATION                                                    F4020009
C                                                                       F4020010
   10 DETERM=1.0                                                        F4020011
   15 DO 20 J=1,N                                                       F4020012
   20 IPIVOT(J)=0                                                       F4020013
   30 DO 550 I=1,N                                                      F4020014
C                                                                       F4020015
C     SEARCH FOR PIVOT ELEMENT                                          F4020016
C                                                                       F4020017
   40 AMAX=0.0                                                          F4020018
   45 DO 105 J=1,N                                                      F4020019
   50 IF (IPIVOT(J)-1) 60, 105, 60                                      F4020020
   60 DO 100 K=1,N                                                      F4020021
   70 IF (IPIVOT(K)-1) 80, 100, 740                                     F4020022
   80 IF (ABS(AMAX)-ABS(A(J,K))) 85, 100, 100                           F4020023
   85 IROW=J                                                            F4020024
   90 ICOLUM=K                                                          F4020025
   95 AMAX=A(J,K)                                                       F4020026
  100 CONTINUE                                                          F4020027
  105 CONTINUE                                                          F4020028
  110 IPIVOT(ICOLUM)=IPIVOT(ICOLUM)+1                                   F4020029
                                                                        F4020030
C     INTERCHANGE ROWS TO PUT PIVOT ELEMENT ON DIAGONAL                 F4020031
                                                                        F4020032
  130 IF (IROW-ICOLUM) 140, 260, 140                                    F4020033
  140 DETERM=-DETERM                                                    F4020034
  150 DO 200 L=1,N                                                      F4020035
  160 SWAP=A(IROW,L)                                                    F4020036
  170 A(IROW,L)=A(ICOLUM,L)                                             F4020037
  200 A(ICOLUM,L)=SWAP                                                  F4020038
  205 IF(M) 260, 260, 210                                               F4020039
  210 DO 250 L=1, M                                                     F4020040
  220 SWAP=B(IROW,L)                                                    F4020041
  230 B(IROW,L)=B(ICOLUM,L)                                             F4020042
  250 B(ICOLUM,L)=SWAP                                                  F4020043
  260 INDEX(I,1)=IROW                                                   F4020044
  270 INDEX(I,2)=ICOLUM                                                 F4020045
  310 PIVOT(I)=A(ICOLUM,ICOLUM)                                         F4020046
  320 DETERM=DETERM*PIVOT(I)                                            F4020047
C                                                                       F4020048
C     DIVIDE PIVOT ROW BY PIVOT ELEMENT                                 F4020049
C                                                                       F4020050
  330 A(ICOLUM,ICOLUM)=1.0                                              F4020051
  340 DO 350 L=1,N                                                      F4020052
  350 A(ICOLUM,L)=A(ICOLUM,L)/PIVOT(I)                                  F4020053
  355 IF(M) 380, 380, 360                                               F4020054
  360 DO 370 L=1,M                                                      F4020055
  370 B(ICOLUM,L)=B(ICOLUM,L)/PIVOT(I)                                  F4020056
C                                                                       F4020057
C     REDUCE NON-PIVOT ROWS                                             F4020058
C                                                                       F4020059
  380 DO 550 L1=1,N                                                     F4020060
  390 IF(L1-ICOLUM) 400, 550, 400                                       F4020061
  400 T=A(L1,ICOLUM)                                                    F4020062
  420 A(L1,ICOLUM)=0.0                                                  F4020063
  430 DO 450 L=1,N                                                      F4020064
  450 A(L1,L)=A(L1,L)-A(ICOLUM,L)*T                                     F4020065
  455 IF(M) 550, 550, 460                                               F4020066
  460 DO 500 L=1,M                                                      F4020067
  500 B(L1,L)=B(L1,L)-B(ICOLUM,L)*T                                     F4020068
  550 CONTINUE                                                          F4020069
C                                                                       F4020070
C     INTERCHANGE COLUMNS                                               F4020071
C                                                                       F4020072
  600 DO 710 I=1,N                                                      F4020073
  610 L=N+1-I                                                           F4020074
  620 IF (INDEX(L,1)-INDEX(L,2)) 630, 710, 630                          F4020075
  630 JROW=INDEX(L,1)                                                   F4020076
  640 JCOLUM=INDEX(L,2)                                                 F4020077
  650 DO 705 K=1,N                                                      F4020078
  660 SWAP=A(K,JROW)                                                    F4020079
  670 A(K,JROW)=A(K,JCOLUM)                                             F4020080
  700 A(K,JCOLUM)=SWAP                                                  F4020081
  705 CONTINUE                                                          F4020082
  710 CONTINUE                                                          F4020083
  740 RETURN                                                            F4022084
  750 END                                                               F4020085
