(*The foundation and development of this code can be found in Manuel Forner's book:<<Desarrollos en serie de los productos de algunas funciones especiales>>paper support:http://www.tenda.uji.es/pls/iglu/!GCPPA00.GCPPR0002?lg=CA&id_art=1110 e-Book-PDF:http://www.tenda.uji.es/pls/iglu/!GCPPA00.GCPPR0002?lg=CA&id_art=1171*)

(*INTEGRAL CALCULATION OF THE PRODUCT OF THREE ASSOCIATED ESPHERICAL HARMONICS IN COMPLEX FORM*)

(* The integral to be calculated is(∫_(λ=0)^(2π))(∫_(θ=-(π/2))^(π/2)) (Subscript[Y, n,m])(θ,λ) (Subscript[Y, r,s])(θ,λ) (Subscript[Y, j,zz]) Cos[θ] \[DifferentialD]θ \[DifferentialD]λ*)

(* (∫_(-1)^1)(x^α) (((1-(x^2)))^(z/2))\[DifferentialD]x =If[Mod[z,2]==0,((1+(((-1))^α))/2)(((α-1)!! ((z/2))! (2^((z/2)+1)))/((α+z+1)!!)),((1+(((-1))^α))/2)(((α-1)!! z!! π)/((α+z+1)!!))]*)

solInt[α_,z_]:=If[Mod[z,2]==0,((1+(((-1))^α))/2)(((α-1)!! ((z/2))! (2^((z/2)+1)))/((α+z+1)!!)),((1+(((-1))^α))/2)(((α-1)!! z!! π)/((α+z+1)!!))]

η[m_]:=If[m<0,(((-1))^(Abs[m])),1]

(*Construction of the "n+m"-th derivative of ((((x^2)-1))^n)*)
(*((d^(n+m))/(d (x^(n+m))))((((x^2)-1))^n) = (n+m)! (((n!))^2) (∑_(α=0)^(n-m))((∑_(j=0)^(n-m))((1/(j!(n-m-j)!(m+j)!(n-j)!))((∑_(t=β)^j)(((-1))^t)(({{j}, {t}}))(({{n-m-j}, {n-m-α-t}})))))(x^α) , where β=If[j-α<0,0,j-α], "n" and "m" belong to the natural numbers including zero and being "m≤n"*)

(*suma1[α_,n_,m_,j_]:=(∑_(t=β)^j)(((-1))^t)(({{j}, {t}}))(({{n-m-j}, {n-m-α-t}}))*)

suma1[α_,n_,m_,j_]:=Module[{t,β,γ},γ=j;β=If[j-α<0,0,j-α];Sum[(((-1))^t)((j!)/(t!(j-t)!))(((n-m-j)!)/((n-m-α-t)!(α+t-j)!)),{t,β,γ}]]

(*suma2[α_,n_,m_]:=(∑_(j=0)^(n-m))((1/(j!(n-m-j)!(m+j)!(n-j)!))((∑_(t=β)^j)(((-1))^t)(({{j}, {t}}))(({{n-m-j}, {n-m-α-t}}))))*)

suma2[α_,n_,m_]:=Expand[Sum[((suma1[α,n,m,j])/(j!(n-m-j)!(m+j)!(n-j)!)),{j,0,n-m}]]

(*χ[ξ_,n_,m_,r_,s_]:=(∑_(u=δ)^g)((Subscript[Λ, u]) (Subscript[E, ξ-u])),where δ=If[r-s-ξ<0,Abs[r-s-ξ],0],g=If[ξ>n-m,n-m,ξ],(Subscript[Λ, u])=suma2[u,n,m],(Subscript[E, ξ-u])=suma2[ξ-u,r,s]*)

χ[ξ_,n_,m_,r_,s_]:=Module[{u,δ,l},δ=If[r-s-ξ<0,Abs[r-s-ξ],0];l=If[ξ>n-m,n-m,ξ];Sum[suma2[u,n,m] suma2[ξ-u,r,s],{u,δ,l}]]

(*χχ[ζ_,n_,m_,r_,s_,i_,z_]:=(∑_(uu=δδ)^gg)((Subscript[X, uu]) (Subscript[H, ζ-uu])), where δδ=If[i-z-ζ<0,Abs[i-z-ζ],0],gg=If[ζ>n+r-m-s,n+r-m-s,ζ],(Subscript[X, uu])=χ[uu,n,m,r,s],(Subscript[H, ζ-uu])= suma2[ζ-uu,i,z]*)

χχ[ζ_,n_,m_,r_,s_,i_,z_]:=Module[{uu,δδ,ll},δδ=If[i-z-ζ<0,Abs[i-z-ζ],0];ll=If[ζ>n+r-m-s,n+r-m-s,ζ];Sum[χ[uu,n,m,r,s] suma2[ζ-uu,i,z],{uu,δδ,ll}]]

(*If we assign cC[n_,m_]:=(Sqrt[(((2n+1)(n-m)!)/(4π(n+m)!))]), spherical harmonics in complex form can be denoted as:*)
(*(Subscript[Y, n,m])(θ,λ)=cC[n,m] (E^(I m λ)) (Subscript[P, n,m])(Sin[θ]) *)

cC[n_,m_]:=(Sqrt[(((2n+1)(n-m)!)/(4π(n+m)!))])

coef[m_,s_,z_]:=If[m+s+z==0,2π,0]

(*intFornerC[j_,zz_,n_,m_,r_,s_]:=(∫_(λ=0)^(2π))(∫_(θ=-(π/2))^(π/2)) (Subscript[Y, n,m])(θ,λ) (Subscript[Y, r,s])(θ,λ) (Subscript[Y, j,zz]) Cos[θ] \[DifferentialD]θ \[DifferentialD]λ*)

intFornerC[j_Integer,zz_Integer,n_Integer,m_Integer,r_Integer,s_Integer]:=coef[m,s,zz](((n+Abs[m])!n!(r+Abs[s])!r!(j+Abs[zz])!j!)/(2^(n+r+j)))cC[n,Abs[m]] cC[r,Abs[s]] cC[j,Abs[zz]] η[m]η[s]η[zz]Sum[χχ[ζ,n,Abs[m],r,Abs[s],j,Abs[zz]]solInt[ζ,Abs[m]+Abs[s]+Abs[zz]] ,{ζ,0,n+r+j-(Abs[m]+Abs[s]+Abs[zz])}]

(*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*)

(*ASCERTAINMENT OF THE INTEGRAL CALCULATION*)

(*Definition of associated Legendre functions*)
(*(Subscript[P, n,m])(x)=(((1-(x^2)))^(m/2)) ((d^m)/(d (x^m)))((Subscript[P, n])(x))=(1/((2^n) n!)) (((1-(x^2)))^(m/2)) ((d^(n+m))/(d (x^(n+m))))((((x^2)-1))^n)*)

funAsLe[n_,m_,θ_]:=(((-1))^m)LegendreP[n,m,Sin[θ]]

(*Definition of spherical harmònics in complex form*)
(*(Subscript[Y, n,m])(θ,λ)=(Sqrt[(((2n+1)(n-m)!)/(4π(n+m)!))])(E^(I m λ)) (Subscript[P, n,m])(Sin[θ]) *)

YFC[n_,m_,θ_,λ_]:=(Sqrt[(((2n+1)(n-m)!)/(4π(n+m)!))])(E^(I m λ)) (funAsLe[n,m,θ]/.{Sqrt[1-Cos[x_]^2]->Sin[x],Sqrt[1-Sin[x_]^2]->Cos[x]})

(*intProd[j_,z_,n_,m_,r_,s_]:=(∫_(λ=0)^(2π))(∫_(θ=-(π/2))^(π/2)) (Subscript[Y, n,m])(θ,λ) (Subscript[Y, r,s])(θ,λ) (Subscript[Y, j,zz]) Cos[θ] \[DifferentialD]θ \[DifferentialD]λ*)

intProd[j_,z_,n_,m_,r_,s_]:=Integrate[Integrate[YFC[n,m,θ,λ] YFC[r,s,θ,λ] YFC[j,z,θ,λ] Cos[θ],{θ,-(π/2),(π/2)}],{λ,0,2π}]

(*Examples*)

intProd[7,-5,6,1,5,4]

intFornerC[7,-5,6,1,5,4]

intProd[7,-7,6,5,5,2]

intFornerC[7,-7,6,5,5,2]

(*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*)
(*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*)

(*GENERALIZATION OF DEVELOPMENT,IN SPHERICAL HARMONICS SERIES IN COMPLEX FORM,OF THE PRODUCT OF ESPHERICAL HARMONICS IN COMPLEX FORM*)

(*aC[j_,n_,m_,r_,s_]:=(∫_(λ=0)^(2π))(∫_(θ=-(π/2))^(π/2)) (Subscript[Y, n,m])(θ,λ) (Subscript[Y, r,s])(θ,λ) (Subscript[Y, j,m+s]^( * ))(θ,λ) Cos[θ]\[DifferentialD]θ\[DifferentialD]λ*)
(*Otherwise (Subscript[Y, j,z]^( * ))(θ,λ)=(((-1))^(Abs[z] ))(Subscript[Y, j,-z])(θ,λ)*)
(*With which aC[j_,n_,m_,r_,s_]:=(∫_(λ=0)^(2π))(∫_(θ=-(π/2))^(π/2)) (((-1))^(Abs[z] ))(Subscript[Y, n,m])(θ,λ) (Subscript[Y, r,s])(θ,λ) (Subscript[Y, j,-(m+s)])(θ,λ) Cos[θ]\[DifferentialD]θ\[DifferentialD]λ*)
(*That is to say, aC[j_,n_,m_,r_,s_]:=(((-1))^(Abs[z]))intFornerC[j,-z,n,m,r,s],amb z=m+s*)

aC[j_,n_,m_,r_,s_]:=Module[{z},z=m+s;(((-1))^(Abs[z]))intFornerC[j,-z,n,m,r,s]]

(*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*)

(*prodC[n_,m_,r_,s_]:=(∑_(j=|m+s|)^(n+r))aC[j,n,m,r,s] (Subscript[Y, j,m+s])*)

prodC[n_,m_,r_,s_]:=Sum[aC[j,n,m,r,s]*FFC[j,m+s],{j,Abs[m+s],n+r}]

(*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*)

prodArmFCc[n_Integer,m_Integer,r_Integer,s_Integer]:=prodArmFCc[n,m,r,s]=Which[(n≥Abs[m])&&(r≥Abs[s]),prodC[n,m,r,s],n<0||r<0,Print["Neither n nor r can be negative numbers"],n<Abs[m]||r<Abs[s],Print["Neither n nor r can be smaller than |m| and |s|, respectively"]]