C === TO CALCULATE AND INVERT MATRICES R, S  =====
C==================================
      SUBROUTINE INVMANFUL(M,M1,XM0,DELXM,XMAXM,LAML,
     *       IMIN,OM,LV,JJ,NC,NC0,N1,C,Q,DV1,DV2,FF,
     *       MA,DET,R,S,B,B1,B2)
C==============================================
      INTEGER Z1,Z2,Z3,
     *        M(*),M1(*),IMIN(*)
      REAL*8 EPS,MIN,KAP,LAM,DELX,XMAX,X0,OM,DET,
     *       XM0(*),DELXM(*),XMAXM(*),LAML(*),
     *       C(*),Q(*),DV1(*),DV2(*),FF(*),MA(N1,*),
     *       R(N1,*),S(N1,*),B(N1,*),B1(N1,*),B2(*)

         Z1=0
      DO 10 JN=1,NC
         K=M(JN)
         K1=M1(JN)
         XMAX=XMAXM(JN)
         X0=XM0(JN)
        IMIN(JN)=0
         Z2=2*Z1
         Z3=Z2+K
         LAM=LAML(JN)
         DELX=DELXM(JN)
         DO 20 I=1,N1
            B(I,JN)=0.0D 00
            DO 30 J=1,K
               R(I,J+Z2)=-MA(I,J+Z2)*(C(J+Z1)-Q(J+Z1))*
     *                   FF(J+Z1)*FF(J+Z1)*
     *                   DV1(J+Z1)/DFLOAT(2*LV+1)
               S(I,J+Z2)=R(I,J+Z2)
               R(I,J+Z3)=-MA(I,J+Z3)*C(J+Z1)*
     *                   FF(J+Z1)*FF(J+Z1)*
     *                   DV2(J+Z1)/DFLOAT(2*LV+1)
   30          S(I,J+Z3)=R(I,J+Z3)
   20       CONTINUE
         Z1=Z1+K
         IF(OM.GT.-LAM.AND.OM.LT.XMAX**2-LAM) THEN
           KAP=X0
           MIN=DABS(OM-KAP**2+LAM)
           DO 4 I=K1+1,K
              IF(DABS(OM-KAP**2+LAM).LE.MIN) THEN
                I1=I
                MIN=DABS(OM-KAP**2+LAM)
                END IF
    4         KAP=KAP+DELX
           K4=I1+Z2
           IMIN(JN)=K4
C          WRITE(6,100)K4
C100      FORMAT('invman  k4',I5)
           IF(K4.NE.0) THEN
           DO 5 I=1,N1
             B(I,JN)=3.1416*MA(I,K4)/DFLOAT(2*LV+1)
C     IF (JJ.EQ.5) THEN
C     WRITE(6,53) JN,K4
C  53  FORMAT('JN imin1     ',2I3        )
C      END IF
    5 CONTINUE
          END IF
             ELSE
             IF(JN.EQ.NC0) IMIN(JN)=JJ+Z2
           END IF
   10    CONTINUE
      DO 6 I=1,N1
         S(I,I)=S(I,I)+1.D 00
    6    R(I,I)=R(I,I)+1.D 00
C          WRITE(6,53) JN,K4
C 53  FORMAT('invman JN imin1     ',2I3)
       EPS=1.D-16
C      IF(JN.EQ.1.AND.JJ.EQ.5) THEN
C      DO 52 I=1,N1
C      DO 52 J=1,N1
C      PRINT 51,S(I,J)
C  51  FORMAT('matrix s='/
C     *      81(4E12.5)
C  52  CONTINUE
C     END IF
      CALL INVERT(S,EPS,DET,N1,*310)
      CALL MULMA1(NC,N1,S,B,B1)
      CALL MULMA2(NC,NC,N1,IMIN,B,B1,B2)
      DO 7 JN=1,NC
         J=IMIN(JN)
      IF(J.NE.0)THEN
         DO 8 I=1,N1
          R(I,J)=R(I,J)+B1(I,JN)
  8   CONTINUE
      END IF
    7    CONTINUE
      CALL INVERT(R,EPS,DET,N1,*310)
      CALL MULMA1(NC,N1,R,B,B1)
      CALL MULMA2(N1,NC,N1,IMIN,B1,S,B2)
  310 CONTINUE
C      WRITE(6,53)  IMIN(NC0),NC0,JJ
C 53  FORMAT('invman  imin(nc0) nc0 jj ',3I3)

      RETURN
      END
