      function RAPmatx(beta,l,m,m2)

C*****************************************************
C
C This little function calculates an element
C of the irreducible matrix representation of
C the infinite rotation group. Basically, a
C spherical harmonic Yml which has been functionally
C rotated so that it's new polar axis lies in the xz
C plane at an angle of beta to the coordinate z-axis
C may be represented by a sum over original spherical
C harmonics:
C
C     Ylm(rotated) = Sum(m'=-l,m'=l) (dlmm' Ym'l)
C
C where dlmm' is returned by RAPmatx. m' is represented
C by m2; the other parameters are obvious (I hope!)
C
C See p110 of 'Group Theory & QM', M. Tinkham, 1st Ed 
C McGraw-Hill
C
C Started: 1/12/95
C
C (c) Richard Townsend 1995
C
C
C******************************************************

C Turn off implicit typing

      implicit none

C Declare the passed parameters

      double precision RAPmatx		! The returned matrix element
      double precision beta		! The rotation angle
      integer l				! The l-value of the harmonics
      integer m				! The m-value of the rotated harmonic
      integer m2			! The m-value of the 'basis' harmonic

C Declare the in-program parameters

      double precision gterm		! General term in working out the summation
      double precision rapfact		! The factorial function
      double precision tfact		! Temporary variable for storing rapfact
      double precision sbeta2		! sin(beta/2)
      double precision cbeta2		! cos(beta/2)
      integer k				! A summation counter to loop over terms

C Initialise the matrix element to zero

      RAPmatx=0.D0

C Set up sbeta2 and cbeta2

      sbeta2=sin(beta/2.D0)
      cbeta2=cos(beta/2.D0)

C Loop over k to add in each part of the matrix element

      do k=0,(2*l+1),1

C Initialise the general term

        gterm=(-1.D0)**k*dsqrt(
     :    rapfact(l+m)*rapfact(l-m)*
     :    rapfact(l+m2)*rapfact(l-m2))/
     :    rapfact(k)

C Add in the bits dependent on beta, checking for sin(beta/2)=0
C and cos(beta/2)=0.

        if(sbeta2.eq.0.D0) then
          if((2*k+m2-m).ne.0) gterm=0.D0
        else
          gterm=gterm*
     :    (-sbeta2)**(2*k+m2-m)
        endif

        if(cbeta2.eq.0.D0) then
          if((2*l-2*k-m2+m).ne.0) gterm=0.D0
        else
          gterm=gterm*
     :    (cbeta2)**(2*l-2*k-m2+m)
        endif

C Multiply by the other bits
                                                                             
        tfact=rapfact(l+m-k)
        if(tfact.eq.0.D0) then
          gterm=0.D0
        else
          gterm=gterm/tfact
        endif

        tfact=rapfact(l-k-m2)
        if(tfact.eq.0.D0) then
          gterm=0.D0
        else
          gterm=gterm/tfact
        endif

        tfact=rapfact(k+m2-m)
        if(tfact.eq.0.D0) then
          gterm=0.D0
        else
          gterm=gterm/tfact
        endif

C Add this to the matrix element and loop back around

        RAPmatx=RAPmatx+gterm

      enddo

C Return

      return

      end                         

