c LINEAR ALGEBRA ROUTINES FOR DMV10CONJ.F
c Solve linear system A X = B 
c with A close to identity (no pivot research).
      SUBROUTINE LINSOLVE (AMAT,BMAT)
c  Input: 3x3 matrices A=AMAT, X=BMAT
c Output: 3x3 matrix   B=BMAT
      IMPLICIT REAL*8 (A-H,O-Z) 
      PARAMETER (N=3)
      DOUBLE PRECISION AMAT(N,N),BMAT(N,N)
c down
      DO IR=1,N
      APIV=1.0D0/AMAT(IR,IR)
      DO J=1,N
      AMAT(IR,J)=AMAT(IR,J)*APIV
      BMAT(IR,J)=BMAT(IR,J)*APIV
      END DO
      DO I=IR+1,N
      SCAL=AMAT(I,IR)
      DO J=1,N
      AMAT(I,J)=AMAT(I,J)-SCAL*AMAT(IR,J)
      BMAT(I,J)=BMAT(I,J)-SCAL*BMAT(IR,J)
      END DO
      END DO
      END DO
c up
      DO IR=N,2,-1
      DO I=1,IR-1
      SCAL=AMAT(I,IR)
      DO J=1,N
      AMAT(I,J)=AMAT(I,J)-SCAL*AMAT(IR,J)
      BMAT(I,J)=BMAT(I,J)-SCAL*BMAT(IR,J)
      END DO
      END DO
      END DO
      RETURN
      END
C
c Compute determinant of a 3x3 matrix (global pivot search)
c note: matrix AMAT is not modified
      FUNCTION DETMAT (AMAT)
      IMPLICIT REAL*8 (A-H,O-Z) 
      PARAMETER (N=3)
      DOUBLE PRECISION AMAT(N,N)
c pivot global research 
      I0=1
      J0=1
      DO I=1,N
      DO J=1,N
      IF (ABS(AMAT(I,J)).GT.ABS(AMAT(I0,J0))) THEN
      I0=I
      J0=J
      END IF
      END DO
      END DO
c      
      I1=I0+1
      I2=I0+2
      IF (I1.GT.3) I1=I1-3
      IF (I2.GT.3) I2=I2-3
      J1=J0+1
      J2=J0+2
      IF (J1.GT.3) J1=J1-3
      IF (J2.GT.3) J2=J2-3
c
      PIV=AMAT(I0,J0)     
      IF (PIV.EQ.0.0D0) THEN
      DETMAT=0.0D0
      ELSE
      A=PIV*AMAT(I1,J1)-AMAT(I1,J0)*AMAT(I0,J1)
      B=PIV*AMAT(I1,J2)-AMAT(I1,J0)*AMAT(I0,J2)
      C=PIV*AMAT(I2,J1)-AMAT(I2,J0)*AMAT(I0,J1)
      D=PIV*AMAT(I2,J2)-AMAT(I2,J0)*AMAT(I0,J2)
      DETMAT=(A*D-B*C)/PIV
      END IF
      RETURN
      END
C
      SUBROUTINE PRINTMAT (AMAT)
      IMPLICIT REAL*8 (A-H,O-Z) 
      PARAMETER (N=3)
      DOUBLE PRECISION AMAT(N,N)
        DO I=1,N
        write (6,*) (AMAT(I,J),J=1,N)
        END DO
        write (6,*) ''
      RETURN
      END
