      subroutine abmat(itest,ncycle,mvalue,nvalue,nmaxcontractions,
     &                 nalpha,ncontractions,coeffa,dint,a,b)

c  this subroutine written by sor koon goh of the
c  department of chemistry, university of ottawa, ottawa, canada.
c  all rights reserved.  this is part of the DeFT project.

      implicit real*8(a-h,o-z)
 
      dimension coeffa(nmaxcontractions,*),a(nalpha,*),b(nalpha,*)

      dimension dint(ncontractions*(ncontractions+1)/2,*)

      data zero,quarter,pi/0.0d0,0.25d0,3.1415926535898d0/

      do 1001 n=1,nalpha
      do 1001 m=1,n
      if(itest.eq.0) then
                           if((ncycle.gt.1).and.
     &                        ((m.ne.mvalue).and.(m.ne.nvalue)).and.
     &                        ((n.ne.mvalue).and.(n.ne.nvalue)))
     &                        goto 1001
                     else
                           if((m.ne.mvalue).or.(n.ne.nvalue)) goto 1001
                     endif
      a(m,n)=zero
      b(m,n)=zero
      if(m.eq.n) goto 1001
      do 1002 l=1,ncontractions
      cm=coeffa(l,m)
      cn=coeffa(l,n)
      do 1002 k=1,ncontractions
      cmm=coeffa(k,m)*cm
      cmn=coeffa(k,m)*cn
      cnn=coeffa(k,n)*cn
      do 1002 j=1,ncontractions
      cmmm=coeffa(j,m)*cmm*quarter
      cmmn=coeffa(j,m)*cmn
      cmnn=coeffa(j,m)*cnn*quarter
      cnmm=coeffa(j,n)*cmm*quarter
      cnmn=coeffa(j,n)*cmn
      cnnn=coeffa(j,n)*cnn*quarter
      fac1=cnmn+cmnn-cmmm
      fac2=cnmm-cnnn
      do 1002 i=1,ncontractions
      ijmin=min(i,j)
      ijmax=max(i,j)
      klmin=min(k,l)
      klmax=max(k,l)
      ij=(ijmax*(ijmax-1)/2)+ijmin
      kl=(klmax*(klmax-1)/2)+klmin
      a(m,n)=a(m,n)+coeffa(i,m)*fac1*dint(ij,kl)
     &             +coeffa(i,n)*fac2*dint(ij,kl)
      b(m,n)=b(m,n)+coeffa(i,m)*cmmn*dint(ij,kl)
     &             -coeffa(i,n)*cnmn*dint(ij,kl)
 1002 continue
 1001 continue

      return
      end
