      SUBROUTINE FRMRIJ3(ICORE,MAXCOR,IUHF,RLECYC,MAXORD,IBOT,I,J,
     &                  SINGLE)
C
C THIS ROUTINE IS USED TO FORM THE R MATRIX USED IN THE RLE SOLUTION
C  OF THE CC EQUATIONS.  RLECYC IS THE NUMBER OF CYCLES WHICH HAVE BEEN
C  COMPLETED, I IS THE ROW NUMBER OF THE MATRIX ELEMENT WHICH IS TO BE 
C  COMPUTED AND J IS THE COLUMN NUMBER.  IF I IS NONZERO AND J IS ZERO,
C  THE THE ENTIRE Ith ROW WILL BE CONSTRUCTED.  SIMILARLY, IF I IS ZERO AND
C  J IS NONZERO, THE Jth COLUMN WILL BE FORMED.
C
C THE FORMULA FOR AN R MATRIX ELEMENT IS:
C
C                [i]     [j]   [i]     (j+1)      (i)
C      R(I,J) = T   D   T   - T   D   T       -  Q
C
C WHERE Q IS THE T2 CONTRIBUTION TO THE ENERGY FOR HF CASES AND
C  E<-T2 + E<-T1 FOR NON-HF CASES.
C
CEND
      IMPLICIT INTEGER (A-Z)
      DIMENSION ICORE(MAXCOR)
      DOUBLE PRECISION R,FACTOR,SDOT,ECORR,ONEM
      LOGICAL SINGLE,GETT1V
      COMMON /SYMPOP/ IRPDPD(8,22),ISYTYP(2,500),ID(18)
      COMMON /MACHSP/ IINTLN,IFLTLN,IINTFP,IALONE,IBITWD
      COMMON /RMAT/ R(400)
cAP - 500 is the maximum number of CC iterations (not basis functions)
      COMMON /ENERGY/ ECORR(500,2),IXTRLE(500)
      COMMON /SYM/ POP(8,2),VRT(8,2),NT(2),NF1(2),NF2(2)
      COMMON /RLEIO/ IOLDEST
      DATA ONEM /-1.0/
      INDX(I,J)=I+(J-1)*MAXORD
      IGET(I)  =1+MOD(IOLDEST+MAXORD-I,MAXORD+1)
      RSIZE=MIN(RLECYC-1,MAXORD)
C
C CASE ONE.  ONLY ONE ELEMENT WILL BE FORMED.
C
      IF(I.NE.0.AND.J.NE.0)THEN
       IROW=I
       ICOL=J
       IOFF=INDX(IROW,ICOL)
       R(IOFF)=0.0
       NLSTI=1+IROW
       NLSTJ=1+ICOL
       NLSTJP1=NLSTJ-1
       DO 100 ISPIN=3,1,IUHF-2
        IADD=0
        GETT1V=.FALSE.
        IF(ISPIN.LT.3.AND.IUHF.EQ.0)THEN
         FACTOR=2.0
        ELSE
         FACTOR=1.0
        ENDIF
        IF(SINGLE.AND.ISPIN.LT.3)THEN
         IADD=NT(ISPIN) 
         GETT1V=.TRUE.
        ENDIF
        SIZET2=ISYMSZ(ISYTYP(1,60+ISPIN),ISYTYP(2,60+ISPIN))
        SIZET=SIZET2+IADD
        IOFFT1=IINTFP*SIZET2
        I000=1
        I010=I000+SIZET*IINTFP
        I020=I010+SIZET*IINTFP
        CALL GETLST(ICORE(I000),IGET(NLSTI),1,1,ISPIN+3,70)
        CALL GETALL(ICORE(I010),SIZET,1,63+ISPIN)
        IF(GETT1V)CALL GETLST(ICORE(I010+IOFFT1),1,1,2,9,63+ISPIN)
        CALL VECPRD(ICORE(I000),ICORE(I010),ICORE(I000),SIZET)
        CALL GETLST(ICORE(I010),IGET(NLSTJ),1,1,ISPIN+3,70)
        R(IOFF)=R(IOFF)+FACTOR*SDOT(SIZET,ICORE(I000),1,ICORE(I010),1)
        CALL GETLST(ICORE(I010),IGET(NLSTJP1),1,1,ISPIN,70)
        R(IOFF)=R(IOFF)-FACTOR*SDOT(SIZET,ICORE(I000),1,ICORE(I010),1)
100    CONTINUE
       R(IOFF)=R(IOFF)+ECORR(RLECYC-IROW,2)
      ELSEIF(I.NE.0.AND.J.EQ.0)THEN
       IROW=I
       NLSTI=1+IROW
       DO 200 ICOL=1,RSIZE
        R(INDX(IROW,ICOL))=0.0
200    CONTINUE
       DO 201 ISPIN=3,1,IUHF-2
        IADD=0
        GETT1V=.FALSE.
        IF(ISPIN.LT.3.AND.IUHF.EQ.0)THEN
         FACTOR=2.0
        ELSE
         FACTOR=1.0
        ENDIF
        IF(SINGLE.AND.ISPIN.LT.3)THEN
         IADD=NT(ISPIN) 
         GETT1V=.TRUE.
        ENDIF
        SIZET2=ISYMSZ(ISYTYP(1,60+ISPIN),ISYTYP(2,60+ISPIN))
        SIZET=SIZET2+IADD
        IOFFT1=IINTFP*SIZET2
        I000=1
        I010=I000+SIZET*IINTFP
        I020=I010+SIZET*IINTFP
        CALL GETLST(ICORE(I000),IGET(NLSTI),1,1,ISPIN+3,70)
        CALL GETALL(ICORE(I010),SIZET,1,63+ISPIN)
        IF(GETT1V)CALL GETLST(ICORE(I010+IOFFT1),1,1,2,9,63+ISPIN)
        CALL VECPRD(ICORE(I000),ICORE(I010),ICORE(I000),SIZET)
        DO 202 ICOL=1,RSIZE  
         IOFF=INDX(IROW,ICOL)
         NLSTJ=1+ICOL
         NLSTJP1=NLSTJ-1
         CALL GETLST(ICORE(I010),IGET(NLSTJ),1,1,ISPIN+3,70)
         R(IOFF)=R(IOFF)+FACTOR*SDOT(SIZET,ICORE(I000),1,ICORE(I010),1)
         CALL GETLST(ICORE(I010),IGET(NLSTJP1),1,1,ISPIN,70)
         R(IOFF)=R(IOFF)-FACTOR*SDOT(SIZET,ICORE(I000),1,ICORE(I010),1)
202     CONTINUE
201    CONTINUE
       DO 203 ICOL=1,RSIZE
        IOFF=INDX(IROW,ICOL)
        R(IOFF)=R(IOFF)+ECORR(RLECYC-IROW,2)
203    CONTINUE
      ELSEIF(I.EQ.0.AND.J.NE.0)THEN
       ICOL=J
       NLSTJ=1+ICOL
       DO 300 IROW=1,RSIZE
        R(INDX(IROW,ICOL))=0.0
300    CONTINUE
       DO 301 ISPIN=3,1,IUHF-2
        IADD=0
        GETT1V=.FALSE.
        IF(ISPIN.LT.3.AND.IUHF.EQ.0)THEN
         FACTOR=2.0
        ELSE
         FACTOR=1.0
        ENDIF
        IF(SINGLE.AND.ISPIN.LT.3)THEN
         IADD=NT(ISPIN) 
         GETT1V=.TRUE.
        ENDIF
        SIZET2=ISYMSZ(ISYTYP(1,60+ISPIN),ISYTYP(2,60+ISPIN))
        SIZET=SIZET2+IADD
        IOFFT1=IINTFP*SIZET2
        I000=1
        I010=I000+SIZET*IINTFP
        I020=I010+SIZET*IINTFP
        CALL GETLST(ICORE(I010),IGET(NLSTJ),1,1,ISPIN+3,70)
        CALL GETLST(ICORE(I000),IGET(NLSTJ-1),1,1,ISPIN,70)
        CALL SAXPY(SIZET,ONEM,ICORE(I000),1,ICORE(I010),1)
        CALL GETALL(ICORE(I000),SIZET,1,63+ISPIN)
        IF(GETT1V)CALL GETLST(ICORE(I000+IOFFT1),1,1,2,9,63+ISPIN)
        CALL VECPRD(ICORE(I010),ICORE(I000),ICORE(I010),SIZET)
        DO 302 IROW=1,RSIZE
         NLSTI=1+IROW
         CALL GETLST(ICORE(I000),IGET(NLSTI),1,1,ISPIN+3,70)
         IOFF=INDX(IROW,ICOL)
         R(IOFF)=R(IOFF)+FACTOR*SDOT(SIZET,ICORE(I000),1,ICORE(I010),1)
302     CONTINUE
301    CONTINUE
       DO 303 IROW=1,RSIZE
        IOFF=INDX(IROW,ICOL)
cYAU - inlining the offset crashes the Sun Forte 6 optimizer
        IOFF2=RLECYC-IROW
        R(IOFF)=R(IOFF)+ECORR(IOFF2,2)
303    CONTINUE
      ELSE
       RETURN
      ENDIF
      RETURN
      END
