/** Generalized hypergeometric functions: relations between 3F2(1)'s **/

/* Relations between 3F2(1) series */
/*K: hypergeometric functions; two term relations; three term relations; 
     finite series relations; Ghg; sums; summation */
/*A: John Gottschalk */
/*S: University of Western Australia */
/*D: October 1984 */

/*P: XCvgt, XSets, XGammaV are automatically loaded.
	The inclusion of XGammaV is not strictly necessary but may greatly 
	reduce the complexity of the output. */
If[~P[_XLoadonce[Loaded]],<XLoadonce]
_Gamma[Init]    :: Loadonce[XGammaV]
_AllCvgtp[Init] :: Loadonce[XCvgt]
_Cmpl[Init]     :: Loadonce[XSets]

/* This is to prevent automatic evaluation of the trig functions, which is
 undesirable in expressions such as Sin[Pi] Gamma[0]. */
Pi : pi

/* Reductions for a 3F2 of unit argument may be obtained by applying 
 transformations relating the functions to other 3F2(1)'s.  The transformations
 described in the references have been written as functions Twoterm, Threeterm
 and Finiterel in this file. When the argument of these functions is a 3F2(1)
 a list of equivalent series is returned. */

/* The lists returned by functions in this file may contain divergent
 hypergeometric series. The well behaved elements of the lists are all 
 equivalent. The divergent terms are deleted if detected otherwise they
 should be ignored. */

#_: Comm
Ghg_:Fpn_:Twoterm_: Tier

/*: Twoterm[$Ghg]
	returns a list whose elements are equivalent to $Ghg using the two-term
	relations for generalized hypergeometric functions described in 
	"Generalized Hypergeometric Functions" by L J Slater. $Ghg must be a 
	function of the form Ghg[3,2,#[...],#[...],1]. */
Twoterm[Ghg[3,2,#[$a,$b,$c],#[$d,$e],1]] :: \
  (Lcl[%list]; %list : Fpn[Ghg[3,2,#[$a,$b,$c],#[$d,$e],1],0,0];\
                       DelDiv[Union[Ar[9,%list]/S[%list[10],Ghg[$$x]->1]]])

/*: Threeterm[$Ghg,$n,($number:10)]
	returns a list of expressions equivalent to the generalized 
	hypergeometric function $Ghg. The expressions are the three-term
	relations for functions of the form Ghg[3,2,#[...],#[...],1] 
	described in "Generalized Hypergeometric Functions" by L J Slater.
		$n = 1 corresponds to equation 4.3.2.1 in that book,
		$n = 2 corresponds to equation 4.3.2.3 and
		$n = 3 corresponds to equation 4.3.2.5.
	Applying each of these formulae gives a list of no more than 100 
	elements (equal elements and divergent elements are deleted).
	To evaluate this full list $number = 100. The more intelligent
	approach is first calculate the Fn(2)'s, Fp(4)'s or Fp(5)'s, choose
	the most useful element of these lists, then combine that with the
	Fn(3)'s, Fp(5)'s or Fn(0)'s respectively to produce a list of no more
	than 10 elements. This approach may be adopted by choosing $number = 10.
	For this option SMP needs to interact with the user, the previous 
	option ($number = 100) is not interactive but produces an extremely 
	large list. */
Threeterm_: Tier
Threeterm[Ghg[3,2,#[$a,$b,$c],#[$d,$e],1],$n_=In[$n,{1,2,3}]] :: \
		      Threeterm[Ghg[3,2,#[$a,$b,$c],#[$d,$e],1],$n,10]
  
Threeterm[Ghg[3,2,#[$a,$b,$c],#[$d,$e],1],$n_=In[$n,{1,2,3}],$number] :: \
 (Lcl[%a,%ans,%b,%Ghg,%num,%r,%rel,%x,%y];\
  %Ghg : Ghg[3,2,#[$a,$b,$c],#[$d,$e],1];\
  %a[$x,$y,$z] : 1/2+%r[$x]+%r[$y]+%r[$z]; %b[$x,$y] : 1+%r[$x]-%r[$y];\
      %r[0] : ($a+$b+$c-2$d-2$e)/3+5/6;\
      %r[1] : ($a-2$b-2$c+$d+$e)/3-1/6;\
      %r[2] : (-2$a+$b-2$c+$d+$e)/3-1/6;\
      %r[3] : (-2$a-2$b+$c+$d+$e)/3-1/6;\
      %r[4] : ($a+$b+$c+$d-2$e)/3-1/6;\
      %r[5] : ($a+$b+$c-2$d+$e)/3-1/6;\
  Sel[$n=1, %x : Gamma[%a[1,2,3]] Gamma[%b[4,0]]   Gamma[%b[5,0]]/\
                (Gamma[%a[1,3,4]] Gamma[%a[1,3,5]] Gamma[%a[3,4,5]])\
                 Gamma[%a[0,2,3]] Pi/Sin[Pi %b[2,3]];\
            %y : Gamma[%a[1,2,3]] Gamma[%b[4,0]]   Gamma[%b[5,0]]/\
	        (Gamma[%a[1,2,4]] Gamma[%a[1,2,5]] Gamma[%a[2,4,5]])\
                 Gamma[%a[0,2,3]] Pi/Sin[Pi %b[2,3]],\
      $n=2, %x : Gamma[%a[1,2,3]] Gamma[%b[4,0]]   Gamma[%b[5,0]]/\
                (Gamma[%a[1,2,4]] Gamma[%a[1,3,4]] Gamma[%a[2,3,4]])\
	         Gamma[%a[0,1,2]] Gamma[%a[0,1,3]] Gamma[%a[0,2,3]]\
		 Sin[Pi %b[5,0]]/Sin[Pi %b[4,5]];\
            %y : Gamma[%a[1,2,3]] Gamma[%b[4,0]]   Gamma[%b[5,0]]/\
	        (Gamma[%a[1,2,5]] Gamma[%a[1,3,5]] Gamma[%a[2,3,5]])\
                 Gamma[%a[0,1,2]] Gamma[%a[0,1,3]] Gamma[%a[0,2,3]]\
		 Sin[Pi %b[0,4]]/Sin[Pi %b[4,5]],\
      $n=3, %x : Gamma[%a[1,2,3]] Gamma[%b[4,0]]   Gamma[%b[5,0]] \
	        (Sin[Pi %a[1,4,5]] Sin[Pi %a[2,4,5]] Sin[Pi %a[3,4,5]]/Pi^3+\
                 Sin[Pi %a[1,2,3]] Sin[Pi %b[4,0]]   Sin[Pi %b[5,0]]/Pi^3)\
	         Gamma[%a[1,2,0]] Gamma[%a[1,3,0]] Gamma[%a[2,3,0]]\
                 Gamma[%a[2,4,0]] Gamma[%a[1,4,0]] Gamma[%a[3,4,0]];\
            %y : Gamma[%a[1,2,3]] Gamma[%b[4,0]]   Gamma[%b[5,0]] \
	         Gamma[%a[1,2,0]] Gamma[%a[1,3,0]] Gamma[%a[2,3,0]]\
                 Gamma[%a[2,4,0]] Gamma[%a[1,4,0]] Gamma[%a[3,4,0]]\
		 Sin[Pi %b[0,5]]/(Gamma[%a[1,2,3]] Gamma[%a[1,2,4]] \
	                          Gamma[%a[1,3,4]] Gamma[%a[2,3,4]] Pi)];\
  Sel[$n=1, %x :  Fpn[%Ghg,1,2] %x; %y : -Fpn[%Ghg,1,3] %y,\
      $n=2, %x : -Fpn[%Ghg,0,4] %x; %y : -Fpn[%Ghg,0,5] %y,\
      $n=3, %x :  Fpn[%Ghg,0,5] %x; %y : -Fpn[%Ghg,1,0] %y];\
  %x : DelDiv[Union[%x]]; %y : DelDiv[Union[%y]];\
  If[P[$number=100], %rel : Flat[Ar[Len[%x],%x[$1]+%y]],\
     Loop[,Pr["Choose the most useful element of the following list."];\
       Do[i,1,Len[%x],,Pr[i,":",%x[i]]];\
       %num : Rd["What is the number of your choice? "];\
       %rel : %y+%x[%num];\
       Pr["The set generated by your choice is:"]; Pr[%rel];\
       %ans : Rd["Do you want a different set calculated? (yes or no ) "];\
       %ans : Impl[Expl[%ans][1]],(%ans=y)|(%ans=Y)]]; %rel)

/*: Finiterel[$Ghg] 
	returns a list of terminating hypergeometric series
	equivalent to $Ghg as described in "Generalized Hypergeometric 
	Functions" by L J Slater. $Ghg must be a function of the form 
	Ghg[3,2,#[...],#[...],1] where one of the numerator parameters of 
	$Ghg, c say, must have the property Natp[1-c]=1. All seventeen 
	equivalent series have (c+1) terms, where the
	first term corresponds to the summation index being zero. */
Finiterel[Ghg[3,2,#[$a,$b,$c_=Natp[1-$c]],#[$d,$e],1]] :: \
 (Lcl[%a,%b,%e,%list,%nterms,%part1,%part2,%part2coef,%r];\
  %a[$x,$y,$z] : 1/2+%r[$x]+%r[$y]+%r[$z]; %b[$x,$y] : 1+%r[$x]-%r[$y];\
        %r[0] : ($a+$b+$c-2$d-2$e)/3+5/6;\
        %r[1] : ($a-2$b-2$c+$d+$e)/3-1/6;\
        %r[2] : (-2$a+$b-2$c+$d+$e)/3-1/6;\
        %r[3] : (-2$a-2$b+$c+$d+$e)/3-1/6;\
        %r[4] : ($a+$b+$c+$d-2$e)/3-1/6;\
        %r[5] : ($a+$b+$c-2$d+$e)/3-1/6;\
  %e[$x,$y,$z] :: Ap[Ghg[3,2,#[%a[$1,$y,$z],%a[$2,$y,$z],%a[$3,$y,$z]],\
    #[%b[$y,$x],%b[$z,$x]],1]/(Gamma[%a[$1,$2,$3]] Gamma[%b[$y,$x]]\
    Gamma[%b[$z,$x]]),Cmpl[{$x,$y,$z},{0,1,2,3,4,5}]];\
  %part1 : Flat[{Gamma[%a[1,2,3]] Gamma[%a[1,2,4]] Gamma[%a[1,2,5]] \
   		     {%e[0,4,5],%e[0,3,4],%e[0,3,5]},\
                Gamma[%a[0,2,3]] Gamma[%a[0,2,4]] Gamma[%a[0,2,5]] \
		     {%e[1,4,5],%e[1,3,4],%e[1,3,5]},\
                Gamma[%a[0,1,3]] Gamma[%a[0,1,4]] Gamma[%a[0,1,5]] \
		     {%e[2,4,5],%e[2,3,4],%e[2,3,5]}}];\
  %part2coef : Ar[3,\
    Gamma[%a[1,2,$1+2]] Gamma[%a[0,2,$1+2]] Gamma[%a[0,1,$1+2]] (-1)^$c];\
  %r : -%r;\
  %part2 : Flat[%part2coef Ar[3,{%e[$1+2,0,2],%e[$1+2,0,1],%e[$1+2,1,2]}]];\
  %list : Cat[%part1,%part2]; %nterms : 1-$c;\
  Pr["All series in the list have",%nterms,"terms"];\
  Union[Ar[17,%list[$1+1]]/S[%list[1],Ghg[$$x] -> 1]])

/*: Fpn[$Ghg,$n,$l]
	calculates the Fp($l)'s for $n : 0 and it calculates the Fn($l)'s for
	$n : 1, when $Ghg is a generalized hypergeometric function of the
	form Ghg[3,2,#[...],#[...],1]. The last element of the list generated
	by Fpn[$Ghg,0,0] contains $Ghg. */
Fpn[Ghg[3,2,#[$a,$b,$c],#[$d,$e],1],$n,$l] :: (Lcl[%a,%b,%e,%r,%Se];\
  %a[$x,$y,$z] : 1/2+%r[$x]+%r[$y]+%r[$z]; %b[$x,$y] : 1+%r[$x]-%r[$y];\
     %r[0] : ($a+$b+$c-2$d-2$e)/3+5/6;\
     %r[1] : ($a-2$b-2$c+$d+$e)/3-1/6;\
     %r[2] : (-2$a+$b-2$c+$d+$e)/3-1/6;\
     %r[3] : (-2$a-2$b+$c+$d+$e)/3-1/6;\
     %r[4] : ($a+$b+$c+$d-2$e)/3-1/6;\
     %r[5] : ($a+$b+$c-2$d+$e)/3-1/6;\
  If[$n=1,%r : -%r];\
  %Se : %e[$x,$y,$z] --> Ap[Ghg[3,2,#[%a[$1,$y,$z],%a[$2,$y,$z],%a[$3,$y,$z]],\
    #[%b[$y,$x],%b[$z,$x]],1]/(Gamma[%a[$1,$2,$3]] Gamma[%b[$y,$x]]\
    Gamma[%b[$z,$x]]),Cmpl[{$x,$y,$z},{0,1,2,3,4,5}]];\
  S[Flat[Cat[Ar[5,Ar[6-$1,%e[$l,$1-1,6-$2],(6-$2)~=$l],($1-1)~=$l]]],%Se])

/*: DelDiv[$list] removes elements of $list containing divergent
	generalized hypergeometric functions. */
DelDiv[$x_=Listp[$x]] :: Cat[Ar[Len[$x],$x,,AllCvgtp]]

_XGhg3[Loaded] : 1

/*R: L. J. Slater 1966 "Generalized Hypergeometric Functions",
   Cambridge University Press. See also F. J. W. Whipple,
   Proc. London Math. Soc.(2), Vol.23, 104-14, 1925. Note that both
   works contain errors. */

/*E:
SMP 1.5.0

#I[1]::  <XGhg3

#I[2]::  Natp[1+n]:1;

#I[3]::  e  : Ghg[3,2,#[-n,1/2,a],#[9/2,b],1];

#I[4]::  eq : Finiterel[e]

All series in the list have     1 + n   terms

	  Gamma[b] Gamma[4 - a + b + n]

		  * Ghg[3,2,#[4,-n,9/2 - a],#[9/2,4 - a + b],1]
#O[4]:   {-----------------------------------------------------,
		      Gamma[b + n] Gamma[4 - a + b]

		    -n
		(-1)   Gamma[b] Gamma[3/2 - b]

		      * Ghg[3,2,#[1/2,-n,9/2 - a],#[9/2,3/2 - b - n],1]
		-------------------------------------------------------,
			    Gamma[b + n] Gamma[3/2 - b - n]

		    -n
		(-1)   Gamma[b] Gamma[1 + a - b]

		      * Ghg[3,2,#[4,a,-n],#[9/2,1 + a - b - n],1]
		-------------------------------------------------,
			Gamma[b + n] Gamma[1 + a - b - n]

		      1/2
		105 Pi    Gamma[4 - a + b + n]

			 * Ghg[3,2,#[-n,-1/2 + b,-a + b],#[b,4 - a + b],1]
		----------------------------------------------------------,
			    16Gamma[9/2 + n] Gamma[4 - a + b]

		  105Pi Gamma[b] Gamma[-7/2 + a]

		       * Ghg[3,2,#[-n,-7/2 - n,-1/2 + b],

			     #[1/2 - n,-7/2 + a - n],1]
		--------------------------------------------,
		16Gamma[1/2 - n] Gamma[9/2 + n] Gamma[b + n]

			  * Gamma[-7/2 + a - n]

		105Pi Gamma[b] Gamma[1 + a - b]

		     * Ghg[3,2,#[4,-n,1 - b - n],#[1/2 - n,1 + a - b - n],1]
		------------------------------------------------------------,
			16Gamma[1/2 - n] Gamma[9/2 + n] Gamma[b + n]

				  * Gamma[1 + a - b - n]

                      /*The remainder of the list has been ommitted.*/ 

#I[5]::  S[e,{n->2,a->1,b->4}]

#O[5]:   Ghg[3,2,#[-2,1/2,1],#[4,9/2],1]

#I[6]::  S[eq,{n->2,a->1,b->4}]

	  14Ghg[3,2,#[-2,7/2,4],#[9/2,7],1]
#O[6]:   {---------------------------------,
			  5

		63Ghg[3,2,#[-2,1/2,7/2],#[-9/2,9/2],1]
		--------------------------------------,
				  80

		3Ghg[3,2,#[-2,1,4],#[-4,9/2],1]
		-------------------------------,
			       5

		224Ghg[3,2,#[-2,3,7/2],#[4,7],1]
		--------------------------------,
			       99

		21Ghg[3,2,#[-11/2,-2,7/2],#[-9/2,-3/2],1]
		-----------------------------------------,
				   880

		Ghg[3,2,#[-5,-2,4],#[-4,-3/2],1]
		--------------------------------,
			       55

   
                      /*The remainder of the list has been ommitted.*/ 

#I[7]::  <XPocV

#I[8]::  sub : Ghg[3,2,#[$a1,$a2,$a3],#[$b1,$b2],1] --> \
	       Sum[(Poc[$a1,r] Poc[$a2,r] Poc[$a3,r])/\
	           (Poc[$b1,r] Poc[$b2,r] r!),{r,0,2}]

#O[8]:   Ghg[3,2,#[$a1,$a2,$a3],#[$b1,$b2],1]

			 Poc[$a1,r] Poc[$a2,r] Poc[$a3,r]
		 --> Sum[--------------------------------,{r,0,2}]
			     Poc[$b1,r] Poc[$b2,r] r!

#I[9]::  S[@5,sub]

#O[9]:   469/495

#I[10]::  S[@6,sub]

#O[10]:   {469/495,469/495,469/495,469/495,469/495,469/495,469/495,469/495,

		 469/495,469/495,469/495,469/495,469/495,469/495,469/495,

		 469/495,469/495}

*/
