      subroutine matinv(isol,idsol,nr,nc,a,mra,kwa,det)                 MATI001
c --------------------------------------------------------              MATI002
c      this subroutine finds the inverse and/or solves                  MATI003
c      simultaneous equations, or neither, and                          MATI004
c      calculates a determinant of a real matrix.                       MATI005
c      Called from STPLRG and XLAT(main)                                MATI006
c --------------------------------------------------------              MATI007
c      generic version, last revised 23.2.89 ETH-LNS                    MATI008
c --------------------------------------------------------              MATI009
      dimension a(1), kwa(1)                                            MATI010
      ir = nr                                                           MATI011
      isol = 1                                                          MATI012
      idsol = 1                                                         MATI013
      if(nr.le.0) goto 330                                              MATI014
      if((ir-mra).gt.0) goto 330                                        MATI015
      ic = iabs(nc)                                                     MATI016
      if ((ic - ir).lt.0) ic = ir                                       MATI017
      ibmp = 1                                                          MATI018
      jbmp = mra                                                        MATI019
      kbmp = jbmp + ibmp                                                MATI020
      nes = ir*jbmp                                                     MATI021
      net = ic*jbmp                                                     MATI022
      if(nc) 10,330,20                                                  MATI023
 10   mdiv = jbmp + 1                                                   MATI024
      iric = ir - ic                                                    MATI025
      goto 30                                                           MATI026
 20   mdiv = 1                                                          MATI027
 30   mad = mdiv                                                        MATI028
      mser = 1                                                          MATI029
      kser = ir                                                         MATI030
      mz = 1                                                            MATI031
      det = 1.0                                                         MATI032
 40   piv = 0.                                                          MATI033
      i = mser                                                          MATI034
 50   if (( i - kser).gt.0) goto 70                                     MATI035
      if((abs(a(i))-piv).le.0.) goto 60                                 MATI036
      piv = abs(a(i))                                                   MATI037
      ip = i                                                            MATI038
 60   i = i + ibmp                                                      MATI039
      goto 50                                                           MATI040
 70   if(piv.eq.0.) goto 340                                            MATI041
      if(nc.lt.0) goto 80                                               MATI042
      i = ip-((ip - 1)/jbmp)*jbmp                                       MATI043
      j = mser - ((mser - 1)/jbmp)*jbmp                                 MATI044
      jj = mser/kbmp + 1                                                MATI045
      ii = jj + (ip -mser)                                              MATI046
      kwa(jj) = ii                                                      MATI047
      goto 90                                                           MATI048
 80   i = ip                                                            MATI049
      j = mser                                                          MATI050
 90   if (ip - mser) 330,120,100                                        MATI051
 100  if ((j - net).gt.0) goto 110                                      MATI052
      psto = a(i)                                                       MATI053
      a(i) = a(j)                                                       MATI054
      a(j) = psto                                                       MATI055
      i = i + jbmp                                                      MATI056
      j = j + jbmp                                                      MATI057
      goto 100                                                          MATI058
 110  det = - det                                                       MATI059
 120  psto = a(mser)                                                    MATI060
      det = det*psto                                                    MATI061
 130  if (det.eq.0.) goto 150                                           MATI062
 140  psto = 1./psto                                                    MATI063
      goto 160                                                          MATI064
 150  idsol = 1                                                         MATI065
      isol = 2                                                          MATI066
      return                                                            MATI067
 160  continue                                                          MATI068
      a(mser) = 1.0                                                     MATI069
      i = mdiv                                                          MATI070
 170  if((i - net).gt.0) goto 180                                       MATI071
      a(i) = a(i)*psto                                                  MATI072
      i = i + jbmp                                                      MATI073
      goto 170                                                          MATI074
 180  if((mz - kser).gt.0) goto 210                                     MATI075
      if((mz-mser).eq.0) goto 200                                       MATI076
      i = mad                                                           MATI077
      j = mdiv                                                          MATI078
      psto = a(mz)                                                      MATI079
      if(psto.eq.0.) goto 200                                           MATI080
      a(mz) = 0.                                                        MATI081
 190  if((j-net).gt.0) goto 200                                         MATI082
      a(i) = a(i) - a(j)*psto                                           MATI083
      j = j + jbmp                                                      MATI084
      i = i + jbmp                                                      MATI085
      goto 190                                                          MATI086
 200  mad = mad + ibmp                                                  MATI087
      mz = mz + ibmp                                                    MATI088
      goto 180                                                          MATI089
 210  continue                                                          MATI090
 220  kser = kser + jbmp                                                MATI091
      if ((kser-nes).gt.0) goto 260                                     MATI092
      mser = mser + kbmp                                                MATI093
      if(nc.lt.0) goto 230                                              MATI094
      mdiv = mdiv + ibmp                                                MATI095
      mz = ((mser - 1)/jbmp)*jbmp + 1                                   MATI096
      mad = 1                                                           MATI097
      goto 40                                                           MATI098
 230  mdiv = mdiv + kbmp                                                MATI099
      if(iric.ne.0) goto 240                                            MATI100
      mz = mser + ibmp                                                  MATI101
      goto 250                                                          MATI102
 240  mz = ((mser - 1)/jbmp)*jbmp + 1                                   MATI103
 250  mad = mz + jbmp                                                   MATI104
      goto 40                                                           MATI105
 260  if(nc.lt.0) return                                                MATI106
      jr = ir                                                           MATI107
 270  if(jr) 330,360,280                                                MATI108
 280  if(kwa(jr) - jr) 330,320,290                                      MATI109
 290  k = (jr - 1)*jbmp                                                 MATI110
      j = k + ir                                                        MATI111
      l = (kwa(jr) - 1)*jbmp + ir                                       MATI112
 300  if(j - k) 330,320,310                                             MATI113
 310  psto = a(l)                                                       MATI114
      a(l) = a(j)                                                       MATI115
      a(j) = psto                                                       MATI116
      j = j - ibmp                                                      MATI117
      l = l - ibmp                                                      MATI118
      goto 300                                                          MATI119
 320  jr = jr - 1                                                       MATI120
      goto 270                                                          MATI121
 330  isol = 3                                                          MATI122
      return                                                            MATI123
 340  det = 0.                                                          MATI124
      isol = 2                                                          MATI125
      idsol = 1                                                         MATI126
      return                                                            MATI127
 350  isol = 2                                                          MATI128
      idsol = 2                                                         MATI129
 360  return                                                            MATI130
      end                                                               MATI131
                                                                        MATI132
