      SUBROUTINE MINV (A,N,D,L,M)
      IMPLICIT  REAL*8   (A-H,O-Z)
C *** SEE R H 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))) GO TO 10
               BIGA = A(IJ)
               L(K) = I
               M(K) = J
 10      CONTINUE
C
C     INTERCHANGE  ROWS
C
         J = L(K)
         IF (J.LE.K) GO TO 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) GO TO 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.) GO TO 60
         D = 0.
         RETURN
 60      DO 70 I=1,N
            IF (I.EQ.K) GO TO 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) GO TO 80
               IF (J.EQ.K) GO TO 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) GO TO 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) GO TO 150
      I = L(K)
      IF (I.LE.K) GO TO 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) GO TO 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
      GO TO 110
 150  RETURN
      END
