C====to calculate real and imag.self-energy matrix  for scat======
      SUBROUTINE  INVMA1(W,K2,SR,DET,RRR,RRM,
     *        CC,QQ,DD1,RE1,IM1)
C =================================================
      INTEGER K2,W,I,J
      REAL*8 RE1,IM1,DET,
     *       RRR(K2,*),RRM(K2,*),SR(2*K2,2*K2),
     *       CC(*),QQ(*),DD1(*)

      DO 10 J=1,2*K2
      DO 15 I=1,2*K2
      IF(I.EQ.J) THEN
      SR(I,J)=1.D 00
      ELSE
      SR(I,J)=0.D 00
      END IF
C      IF(I.EQ.J) WRITE(6,16)SR(I,J)
C 16   FORMAT(' diag ',E15.8)
 15   CONTINUE
 10   CONTINUE
C----replace to RRR(J,I)----
      DO 30 J=1,K2
      DO 35 I=1,K2
      SR(I,J)=SR(I,J)-RRR(I,J)*(CC(J)-QQ(J))*DD1(J)
      SR(J+K2,I+K2)=SR(J,I)
C      IF(I.EQ.J) WRITE(6,36)SR(J,I)
C 36   FORMAT(' diag after chang ',E15.8)
 35   CONTINUE
 30   CONTINUE
C----- replace  rrm(i,j) to rrm(j,i)!!!
      DO 50 J=1,K2
      DO 55 I=1,K2
      SR(I+K2,J)=SR(I+K2,J)-RRM(I,J)*(CC(J)-QQ(J))*DD1(J)
      SR(I,J+K2)=-SR(I+K2,J)
C      IF(I.EQ.J) WRITE(6,56) SR(J,I)
C 56   FORMAT(' nondiag ',E15.8)
 55   CONTINUE
 50   CONTINUE
      CALL INVERT(SR,0.00001,DET,2*K2,*100)
      WRITE(6,56) DEt
 56   FORMAT(' DET ',E15.8)
      RE1=0.0
      IM1=0.0
C---- replace to rrr(w,i),rrm(w,i)!!!
      DO 70 J=1,K2
      RE1=RE1+SR(W,   J)*RRR(J,W)+SR(W,   J+K2)*RRM(J,W)
      IM1=IM1+SR(W+K2,J)*RRR(J,W)+SR(W+K2,J+K2)*RRM(J,W)
C      WRITE(6,57) J,W,RRR(W,J),RRM(J,W),SR(W,J),SR(J+K2,W),
C    *         SR(J,W+K2),SR(J+K2,W+K2)
C 57   FORMAT('J,W,RRR,RRM,SR ',2I3,6F10.5)
C      WRITE(6, 80)J,RE1,IM1
 80   FORMAT(' K ',I3,' RE ',E15.8,' IM ',E15.8)
 70   CONTINUE
100   RETURN
      END
