!
Subroutine Normalization(max)
use Advertisement
implicit none
integer*4, intent(in) :: max
integer*4 k, j
nu(0)=dsqrt(3.d00/8.d00)
if (m.ne.0) then
  nu(0)=(2.d00*m+1.d00)/m/(m+1)/2.d00
  do k=2, 2*m, 1
    nu(0)=nu(0)/k
  end do
  nu(0)=dsqrt(nu(0)) !!!
  do k=3, 2*m-1, 2
    nu(0)=nu(0)*k
  end do
end if
if (m.eq.0) then
  do j=1, max
    nu(j)=dsqrt((2.d00*j+1.d00)/(j+1.d00)/j/4.d00) !!!
  end do
else
  do j=m, max
    nu(j)=(2.d00*j+1.d00)/(j+1.d00)/j/2.d00
    do k=1, 2*m
      nu(j)=nu(j)/(j-m+k)
    end do
    nu(j)=dsqrt(nu(j)) !!!
  end do
end if
End subroutine Normalization
!
Subroutine Normalization_
use Advertisement
implicit none
integer*4 k, j
nu(0)=dsqrt(3.d00/8.d00)
if (m.ne.0) then
  nu(0)=(2.d00*m+1.d00)/m/(m+1)/2.d00
  do k=2, 2*m, 1
    nu(0)=nu(0)/k
  end do
  nu(0)=dsqrt(nu(0)) !!!
  do k=3, 2*m-1, 2
    nu(0)=nu(0)*k
  end do
end if
if (m.eq.0) then
  do j=1, n_max
    nu(j)=dsqrt((2.d00*j+1.d00)/(j+1.d00)/j/4.d00) !!!
  end do
else
  do j=m, n_max+m-1
    nu(j-m+1)=(2.d00*j+1.d00)/(j+1.d00)/j/2.d00
    do k=1, 2*m
      nu(j-m+1)=nu(j-m+1)/(j-m+k)
    end do
    nu(j-m+1)=dsqrt(nu(j-m+1)) !!!
  end do
end if
End subroutine Normalization_
!
Subroutine Legendre(zz, p, t)
use Advertisement
implicit none
complex*16, intent(in) :: zz
complex*16, intent(out) :: p, t
if (m.eq.0) then
 p=nu(0)*zz; t=-nu(0)*cdsqrt(1.d00-zz*zz)
else
 p=nu(0)*cdsqrt(1.d00-zz*zz)**(m-1); t=m*zz*p
end if
End subroutine Legendre
!
Subroutine nLegendre(max, zz, aa, plg, tlg)
use Advertisement
implicit none
integer*4, intent(in) :: max
real*8, intent(in) :: zz, aa
real*8, intent(out) :: plg(max+1), tlg(max+1)
real*8 p, q
integer*4 nn, mm
Plg(1)=zz; Tlg(1)=-aa ! Initial values
Plg(2)=.5d00*(3*zz*zz-1); Tlg(2)=-3*zz*aa
do nn=3, max! Recursion for m=0
 Plg(nn)=((2*nn-1)*zz*Plg(nn-1)-(nn-1)*Plg(nn-2))/nn
 Tlg(nn)=zz*Tlg(nn-1)-nn*aa*Plg(nn-1)
end do
if (m.gt.0) then  ! Recursion for m=1            
  p=zz; q=1.d00
  Plg(1)=q
  do nn=1, max          
    Tlg(nn)=nn*(nn+1)*p-zz*q
    if (nn.lt.max) then 
      q=zz*q+(nn+1)*p
      p=Plg(nn+1)
      Plg(nn+1)=q
    end if
  end do                             
  do mm=2, m
    q=(2*mm-1)*aa*Plg(mm-1)
	p=Plg(mm)
	Plg(mm)=q
	do nn=mm, max
	  Tlg(nn)=(nn-mm+1)*(nn+mm)*aa*p-mm*zz*q
	  if (nn.lt.max+1) then
	    q=zz*q+(nn+mm)*aa*p
		p=Plg(nn+1)
		Plg(nn+1)=q
	  end if
	end do
  end do
end if
if (m.gt.1) then
  Plg(1:m-1)=0.d00; Tlg(1:m-1)=0.d00
end if
End subroutine nLegendre
