      Subroutine minv (a,n,d,l,m)
      Implicit  Real*8   (a-h,o-z)
c *** see RH Landau, Program lpott, 1981
      Dimension a(1), l(1), m(1)
      d = 1.
      nk = -n
      Do 100 k=1,n
         nk = nk+n
         l(k) = k
         m(k) = k
         kk = nk+k
         biga = a(kk)
         Do 10 j=k,n
            iz = n*(j-1)
            Do 10 i=k,n
               ij = iz+i
               If (abs(biga).ge.abs(a(ij))) GoTo 10
               biga = a(ij)
               l(k) = i
               m(k) = j
 10      Continue
c
c     interchange  rows
c
         j = l(k)
         If (j.le.k) GoTo 30
         ki = k-n
         Do 20 i=1,n
            ki = ki+n
            hold = -a(ki)
            ji = ki-k+j
            a(ki) = a(ji)
            a(ji) = hold
 20      Continue
c
c     interchange   colomns
c
 30      i = m(k)
         If (i.le.k) GoTo 50
         jp = n*(i-1)
         Do 40 j=1,n
            jk = nk+j
            ji = jp+j
            hold = -a(jk)
            a(jk) = a(ji)
            a(ji) = hold
 40      Continue
c
c     divide  colonn  by  -  pivot  (  value  of  pivot  element  is
c     contained  in  biga  )
c
 50      If (biga.ne.0.) GoTo 60
         d = 0.
         Return
 60      Do 70 i=1,n
            If (i.eq.k) GoTo 70
            ik = nk+i
            a(ik) = a(ik)/(-biga)
 70      Continue
c
c      reduce  matrix
c
         Do 80 i=1,n
            ik = nk+i
            hold = a(ik)
            ij = i-n
            Do 80 j=1,n
               ij = ij+n
               If (i.eq.k) GoTo 80
               If (j.eq.k) GoTo 80
               kj = ij-i+k
               a(ij) = a(kj)*hold+a(ij)
 80      Continue
c
c     divide  row  by  pivot
c
         kj = k-n
         Do 90 j=1,n
            kj = kj+n
            If (j.eq.k) GoTo 90
            a(kj) = a(kj)/biga
 90      Continue
c
c     product  of  pivots
c
         d = d*biga
c
c     replace  pivot  by  reciprocal
c
         a(kk) = 1./biga
 100  Continue
c
c     final  row  and  colomn  intercainge
c
      k = n
 110  k = k-1
      If (k.le.0) GoTo 150
      i = l(k)
      If (i.le.k) GoTo 130
      jq = n*(k-1)
      jr = n*(i-1)
      Do 120 j=1,n
         jk = jq+j
         hold = a(jk)
         ji = jr+j
         a(jk) = -a(ji)
         a(ji) = hold
 120  Continue
 130  j = m(k)
      If (j.le.k) GoTo 110
      ki = k-n
      Do 140 i=1,n
         ki = ki+n
         hold = a(ki)
         ji = ki-k+j
         a(ki) = -a(ji)
         a(ji) = hold
 140  Continue
      GoTo 110
 150  Return
      End
