      SUBROUTINE OSINV (A,N,D)
      implicit double precision (A-H,O-Z)
      include 'SIZES'
      include 'MOP6FILE'
      dimension A(*)
************************************************************************
*
*    OSINV INVERTS A GENERAL SQUARE MATRIX OF ORDER UP TO MAXORB. SEE
*          dimension STATEMENTS BELOW.
*
*   ON INPUT       A = GENERAL SQUARE MATRIX STORED LINEARLY.
*                  N = dimension OF MATRIX A.
*                  D = VARIABLE, NOT DEFINED ON INPUT.
*
*   ON OUTPUT      A = INVERSE OF ORIGINAL A.
*                  D = DETERMINANT OF ORIGINAL A, UNLESS A WAS SINGULAR,
*                      IN WHICH CASE D = 0.0
*
************************************************************************
      dimension L(MAXORB), M(MAXORB)
************************************************************************
*
*    IF THE VALUE OF TOL GIVEN HERE IS UNSUITABLE, IT CAN BE CHANGED.
      TOL=1.D-8
*
*
************************************************************************
      D=1.0
      NK=-N
      do 180 K=1,N
         NK=NK+N
         L(K)=K
         M(K)=K
         KK=NK+K
         BIGA=A(KK)
         do 20 J=K,N
            IZ=N*(J-1)
            do 20 I=K,N
               IJ=IZ+I
C
C     10 FOLLOWS
C
               if (ABS(BIGA)-ABS(A(IJ))) 10,20,20
   10          BIGA=A(IJ)
               L(K)=I
               M(K)=J
   20    continue
         J=L(K)
         if (J-K) 50,50,30
   30    KI=K-N
         do 40 I=1,N
            KI=KI+N
            HOLO=-A(KI)
            JI=KI-K+J
            A(KI)=A(JI)
   40    A(JI)=HOLO
   50    I=M(K)
         if (I-K) 80,80,60
   60    JP=N*(I-1)
         do 70 J=1,N
            JK=NK+J
            JI=JP+J
            HOLO=-A(JK)
            A(JK)=A(JI)
   70    A(JI)=HOLO
   80    if (ABS(BIGA)-TOL) 90,100,100
   90    D=0.0
         return
  100    do 120 I=1,N
            if (I-K) 110,120,110
  110       IK=NK+I
            A(IK)=A(IK)/(-BIGA)
  120    continue
         do 150 I=1,N
            IK=NK+I
            IJ=I-N
            do 150 J=1,N
               IJ=IJ+N
               if (I-K) 130,150,130
  130          if (J-K) 140,150,140
  140          KJ=IJ-I+K
               A(IJ)=A(IK)*A(KJ)+A(IJ)
  150    continue
         KJ=K-N
         do 170 J=1,N
            KJ=KJ+N
            if (J-K) 160,170,160
  160       A(KJ)=A(KJ)/BIGA
  170    continue
         D=MIN(D*BIGA,1.D10)
         A(KK)=1.0/BIGA
  180 continue
      K=N
  190 K=K-1
      if (K) 260,260,200
  200 I=L(K)
      if (I-K) 230,230,210
  210 JQ=N*(K-1)
      JR=N*(I-1)
      do 220 J=1,N
         JK=JQ+J
         HOLO=A(JK)
         JI=JR+J
         A(JK)=-A(JI)
  220 A(JI)=HOLO
  230 J=M(K)
      if (J-K) 190,190,240
  240 KI=K-N
      do 250 I=1,N
         KI=KI+N
         HOLO=A(KI)
         JI=KI+J-K
         A(KI)=-A(JI)
  250 A(JI)=HOLO
      go to 190
  260 return
C
      end
