      SUBROUTINE DMAT(Y)
C
C     EVALUATES ELEMENTS OF THE MATRIX DIJ
C
      DIMENSION Y(18)
      INTEGER CODE
      COMMON /COD/  CODE(50,2),KREF,KC,ITYPE
      COMMON/DJK/D11,D12,D13,D22,D23,D33,DTR
      COMMON/GAM/G11,G12,G13,G22,G23,G33
      COMMON/AUXI/ IANI(20),INTR,INT1,IOUT,KRE,IREFR,LAY,NDER,IPRINT,
     1 MPRINT,NTR,ISQRT,NAUX,ISOUR,MAUX,MREG,MDIM,IPOL,MSCON,LOUT,
     2 IAMP,MTRNS,ICOEF,IAD,IRHO,ISHEAR,IAC,IRT,mori
      COMPLEX PS
      COMMON /RAY/   AY(28,400),DS(20,20),KINT(20),HHH(3,3),tmax,
     1               PS(3,7,20),IS(8,20),DINC,DTOLD,N,IREF,IND,IND1
C
      G1=G11-1.
      G2=G22-1.
      G3=G33-1.
      D11=G2*G3-G23*G23
      D22=G1*G3-G13*G13
      D33=G1*G2-G12*G12
      D12=G13*G23-G3*G12
      D13=G12*G23-G2*G13
      D23=G12*G13-G1*G23
      DTR=D11+D22+D33
      IF(ABS(DTR).LT..00001)THEN
        IND=10
      END IF
      RETURN
      END
      SUBROUTINE DDMAT(DG)
C
C     EVALUATES DERIVATIVES OF ELEMENTS OF THE MATRIX DIJ
C
      DIMENSION DG(3,3)
      COMMON/DDJK/DD11,DD12,DD13,DD22,DD23,DD33,DDTR
      COMMON/GAM/G11,G12,G13,G22,G23,G33
C
      DG11=DG(1,1)
      DG22=DG(2,2)
      DG33=DG(3,3)
      DG12=DG(1,2)
      DG13=DG(1,3)
      DG23=DG(2,3)
      G1=G11-1.
      G2=G22-1.
      G3=G33-1.
      DD11=G3*DG22+G2*DG33-2.*G23*DG23
      DD22=G3*DG11+G1*DG33-2.*G13*DG13
      DD33=G2*DG11+G1*DG22-2.*G12*DG12
      DD12=G23*DG13+G13*DG23-DG12*G3-G12*DG33
      DD13=G23*DG12+G12*DG23-DG13*G2-G13*DG22
      DD23=G13*DG12+G12*DG13-DG23*G1-G23*DG11
      DDTR=DD11+DD22+DD33
      RETURN
      END
      FUNCTION TR(G)
C
C     TRACE OF PRODUCT OF MATRICES G AND D
C
      DIMENSION G(3,3)
      COMMON/DJK/ D11,D12,D13,D22,D23,D33,DTR
C
      TR=G(1,1)*D11+G(2,2)*D22+G(3,3)*D33+
     1    2.*(G(1,2)*D12+G(1,3)*D13+G(2,3)*D23)
      RETURN
      END
      FUNCTION TRD(G)
C
C     TRACE OF PRODUCT OF MATRICES G AND DD
C
      DIMENSION G(3,3)
      COMMON/DDJK/ DD11,DD12,DD13,DD22,DD23,DD33,DDTR
C
      TRD=G(1,1)*DD11+G(2,2)*DD22+G(3,3)*DD33+
     1    2.*(G(1,2)*DD12+G(1,3)*DD13+G(2,3)*DD23)
      RETURN
      END
