/** Generalized hypergeometric functions: Sum, Ghg interconversion **/

/*K: Ghg; generalized hypergeometric functions; hypergeometric; sum;
     summation */
/*A: John Gottschalk */
/*S: University of Western Australia */
/*D: December 1986 */

/*W: Sum[Smp] is defined to be Inf. 
	There is no test for convergence applied to the hypergeometrics. */
/*P: XGammaConv, XCvgt, XGammaV and XWarning are automatically 
	loaded as needed. */ 

/* Explanations of failure to convert to the hypergeometric form : 
#1: The summand must be able to be expressed in terms of
	Gamma[r+$$a] $z^r, where r is the summation index and
	$$a and $z are independent of r,
#2: Some gamma functions in the converted summand where
	not of the form Gamma[r+$$a] where r is the summation index and
	$$a is independent of r. */

If[~P[_XLoadonce[Loaded]],<XLoadonce]
_GammaConv[Init] :: Loadonce[XGammaConv]
_Nterms[Init]    :: Loadonce[XCvgt]
_SymbT[Init]     :: Loadonce[XWarning]
_SumCan[Init]    :: Loadonce[XSumM]

#_:Comm
Convtoghg_:Ghg_:Tier
Convtoghg_:Ldist

/*W: Sum[Smp] is defined to be Inf. */
_Sum[Smp] : Inf

/*: Convtoghg[$expr]
	attempts to write all the summations in $expr in terms of
	generalised hypergeometric functions. The summations are 
	put into a canonical form, so if the conversion fails they
	will be left in this form. All the functions in the summand are
	converted to Gamma functions of the form Gamma[r+a] where
	r is the summation index. The list of parameters in the Gamma
	functions are collected to form the numerator and denominator
	parameter lists of the hypergeometric functions. Finite summations 
	are examined to see if there is a terminating parameter in the 
	summand, for example Sum[Poc[-m,r],{r,0,m}] contains Poc[-m,r] which 
	vanishes for r>m, so this is suitable. Finite summations such as
	Sum[Poc[-m,r],{r,0,m-2}] are expressed as a Ghg minus
	Sum[Poc[-m,r],{r,m-1,m}]. If there is no terminating parameter
	in the numerator of the summand but there is a factorial in the
	denominator, eg Sum[1/r!,{r,0,m}], the sum is reversed. 
	This is then converted to hypergeometric form. If $expr
	contains a multiple summation the innermost sum is transformed
	to a hypergeometric function. If use of Convtoghg[$expr] does 
	not produce the desired result, the substitution SSumtoghg 
	should be applied separately. */
Convtoghg[$expr_=~P[$expr[0]='Sum]]      :: Map[Convtoghg,$expr]
Convtoghg[$expr_=~In['Sum,$expr]]        :: $expr
Convtoghg[$expr_=Nc[$expr]~=1]           :: Nc[$expr] Convtoghg[$expr/Nc[$expr]]
Convtoghg[Sum[$x_=In['Sum,$x],{$r,0,$m}]]:: Sum[Convtoghg[$x],{$r,0,$m}]
Convtoghg[Sum[$$x]]                      :: Convtoghg[SumCan[Sum[$$x]]]
Convtoghg[Sum[$x,{$r,0,Inf}]] :: S[Sum[$x,{$r,0,Inf}],SSumtoghg] 
Convtoghg[Sum[$x,{$r,0,$m}]]  :: (Lcl[%expr,%ghg,%term,%top]; \
  %expr : S[Sum[$x,{$r,0,$m}],SSumtoghg]; \
  %ghg : Findghg[%expr]; \
  If[Valp[%ghg], \
     Sel[In[$_=$=-$m,%ghg[3],2],SymbT[natp[1+$m]];%expr, \
       In[$_=(Intp[$+$m]&Numbp[$+$m]),%ghg[3],2],(%top : S[%ghg[3],'#->'List]; \
         %term : Ap['Max,Del[0,Map[Intp[$+$m],%top] %top]]; \
         SymbT[natp[1-%term]]; \
         %expr-Sum[$x,{$r,$m+1,-%term}]), \
       ~In[1,%ghg[3],2],SymbT[natp[1+$m]]; \
         S[Sum[S[$x,$r->$m-$r],{$r,0,$m}],SSumtoghg], \
       In[1,%ghg[3],2],SymbT[natp[1+$m]]; \
         S[Sum[$x,{$r,0,Inf}]-Sum[S[$x,$r->$r+$m+1],{$r,0,Inf}],SSumtoghg], \
       1,Sum[$x,{$r,0,$m}]], \
  Sum[$x,{$r,0,$m}]])
Convtoghg[Sum[1,{$r,$k,$m}]] : Sum[1,{$r,$k,$m}]

/*: Findghg[$expr] 
	returns a hypergeometric present in $expr, or a Null if there are
	no hypergeometrics. */
Findghg_:Tier
Findghg[$expr] :: (Lcl[%e]; \ 
	%e : Pos[Ghg[$$x],$expr,$expr]; \
	If[Len[%e]>0, %e[1]/Nc[%e[1]]])

/*: natp[$expr]
	returns Natp[$expr] if $expr is a number. It is used to prevent
	problems with Natp[$expr] yielding 0 if $expr is not a number. */
natp[$m_=Numbp[$m]] :: Natp[$m]
natp[$m_=Natp[$m]]  : 1

/* SSumtoghg is a substitution used to express summations as hypergeometric
	functions. It is used by Convtoghg[$expr] but may be used alone. */
SSumtoghg : Sum[$x,{$r,0,$m}] --> \
  (Lcl[%p,%sum,%summand];\
   %sum : Sum[$x,{$r,0,$m}];\
   %summand : GammaConv[$x, $r];\
   %p : Ghgargs[%summand Gamma[1+$r], $r];\
   If[~Valp[%p],Ret[%sum]];\
   S[$x,$r->0] Ghg[Len[%p[1]],Len[%p[2]],%fn%[%p[1]],%fn%[%p[2]],%p[3]])

%fn%[$x] :: If[Len[$x]=0, #[],Ap['#,$x]]

/*: Ghgargs[$exp,$r]
	returns a list {$top,$bot,$z} which is the argument list of the 
	hypergeometric function corresponding to the sum with summand $exp 
	and summation index $r. $top are the numerator parameters, $bot are
	the denominator parameters and z is the variable of the 
	hypergeometric. */
Ghgargs_:Tier;
Ghgargs[$exp,$r] :: \
  (Lcl[%o,%top,%z];\
   {%o,%top} : Gamsel[Num[$exp],$r,{}];\
   If[In[$r,%top], Ghgerr["#2"]]; \
   %z:S[%o,$r->$r+1]/%o;\
   If[In[$r,%z],Ghgerr["#1"]]; \
   {%o,%bot} : Gamsel[Den[$exp],$r,{}];\
   If[In[$r,%bot], Ghgerr["#2"]]; \
   %z: %z/(S[%o,$r->$r+1]/%o);\
   If[In[$r,%z],Ghgerr["#1"]]; \
   {%top, %bot, %z})

/*: Ghgerr[$s]
	is used to print the messages stating that a sum cannot be converted
	to hypergeometric form. */
Ghgerr[$s]::\
      (Pr[Fmt[,"The hypergeometric conversion failed, see explanation ",$s]]; \
      Ret[,2]) 	/*Return to SSumtoghg*/

/*: Gamsel[$expr,$r,$list]
	returns a list of the form {$a,$b} where $b is a list of parameters
	of the Gamma functions contained in $expr other than the index $r, 
	and $a is a list of symbols other than the Gamma functions. */
Gamsel_:Tier
 
Gamsel[$z,$r,$list]                :  {$z,$list}
Gamsel[Gamma[$z+$r] $$b,$r,$list] :: Gamsel[$$b,$r,Cat[$list,{$z}]]
Gamsel[Gamma[$z+$r]    ,$r,$list] :: {1,Cat[$list,{$z}]}
Gamsel[Gamma[$z+$r]^($n_=Numbp[$n] & Natp[$n]) $$b,$r,$list] :: \
	                           Gamsel[$$b,$r,Cat[$list,{Repl[$z,$n]}]]
Gamsel[Gamma[$z+$r]^($n_=Numbp[$n] & Natp[$n])    ,$r,$list] :: \
				   {1,Cat[$list,{Repl[$z,$n]}]}


/* SGhgtosum is a substitution transforming a hypergeometric into a sum */
SGhgtosum : Sghgtosum[%#r]
Sghgtosum[$r,1] : Ghg[$p,$q_=~P[$q=0],#[$$a],#[$$b],$z] --> \
  (Lcl[$r]; Loadonce[XGammaV]; \
    Ap['Sum,{Ap['Mult,Map[Gamma[$1+$r]/Gamma[$1],{$$a}]] $z^$r/\
    (Ap['Mult,Map[Gamma[$1+$r]/Gamma[$1],{$$b}]] Gamma[1+$r]),\
    {$r,0,Ap[If[P[$1=Inf],Inf,$1-1],{Nterms[Ghg[$p,$q,#[$$a],#[$$b],$z]]}]}}])

Sghgtosum[$r,2] : Ghg[$p,0,#[$$a],#[],$z] --> (Lcl[$r]; Loadonce[XGammaV]; \
  Ap['Sum,{Ap['Mult,Map[Gamma[$1+$r]/Gamma[$1],{$$a}]] $z^$r/Gamma[1+$r], \
  {$r,0,Ap[If[P[$1 = Inf],Inf,$1-1],{Nterms[Ghg[$p,0,#[$$a],#[],$z]]}]}}])

/*B: Convtoghg will not convert certain sums as hypergeometrics even though 
	this may be possible. For example Sum[r (r+3) z^r/(r+4),{r,0,Inf}] 
	and Sum[Poc[-n,r]/Gamma[r],{r,0,n}]. The problem is that these have
	summands which are zero for a value of the summation index. The user
	can manipulate these special cases using functions in the external file
	XSumM. */

_XGhgC[Loaded] : 1

/*E:
SMP 1.5.0   (May 14 1986)
Thu Apr 23 14:25:36 1987


#I[1]::  <XGhgC

#I[2]::  Sum[33Sum[Gamma[r+3+m]/Gamma[r+3/2],{r,0,s+2}],{s,0,m}]

		   Gamma[3 + m + r]
#O[2]:   Sum[33Sum[----------------,{r,0,2 + s}],{s,0,m}]
		    Gamma[3/2 + r]

#I[3]::  Convtoghg[%]

I will assume   natp[3 + s]

		Gamma[3 + m] Ghg[2,1,#[1,3 + m],#[3/2],1]
#O[3]:   Sum[33(-----------------------------------------
			       Gamma[3/2]

			  Gamma[6 + m + s] Ghg[2,1,#[1,6 + m + s],#[9/2 + s],

					   1]
			- ---------------------------------------------------),
					    Gamma[9/2 + s]

	     {s,0,m}]

#I[4]::  Sum[33Sum[Gamma[r+3+m]/Gamma[r+3/2],{r,0,Inf}],{s,0,m}]

		   Gamma[3 + m + r]
#O[4]:   Sum[33Sum[----------------,{r,0,Inf}],{s,0,m}]
		    Gamma[3/2 + r]

#I[5]::  Convtoghg[%]

	     33Gamma[3 + m] Ghg[2,1,#[1,3 + m],#[3/2],1]
#O[5]:   Sum[-------------------------------------------,{s,0,m}]
			     Gamma[3/2]

#I[6]::  5 x^i Sum[1/4((-1)^r/(g*(4/3 + r)*(3 + r))),{r,2,Inf,2}]/4

			     r
	    i   	 (-1)
	 5 x  Sum[--------------------,{r,2,Inf,2}]
		  4g (4/3 + r) (3 + r)
#O[6]:   ------------------------------------------
			     4

#I[7]::  Convtoghg[%]

	    i
	 3 x  Ghg[3,2,#[1,5/3,5/2],#[8/3,7/2],1]
#O[7]:   ---------------------------------------
			  160g

#I[8]::  Sum[Poc[-n,r] z^r,{r,0,n}]-Sum[Poc[-m,r] z^r/(r+4),{r,0,m+2}]+\
	 	 	        Sum[Poc[-p/2-1/2,r] z^r/(r+2),{r,0,(p-5)/2}]

	       r
	      z  Poc[-m,r]
#O[8]:   -Sum[------------,{r,0,2 + m}]
		 4 + r

			r
		       z  Poc[-1/2 - p/2,r]      -5 + p
		 + Sum[--------------------,{r,0,------}]
			      2 + r     	   2

			r
		 + Sum[z  Poc[-n,r],{r,0,n}]

#I[9]::  Convtoghg[%]

I will assume   natp[1 + m]
I will assume   natp[3/2 + p/2]
I will assume   natp[1 + n]

	    -3/2 + p/2
	 - z           Poc[-1/2 - p/2,-3/2 + p/2]
#O[9]:   ----------------------------------------
			1/2 + p/2

		    -1/2 + p/2
		   z           Poc[-1/2 - p/2,-1/2 + p/2]
		 - --------------------------------------
				 3/2 + p/2

		    1/2 + p/2
		   z          Poc[-1/2 - p/2,1/2 + p/2]
		 - ------------------------------------
				5/2 + p/2

		 + Ghg[2,0,#[1,-n],#[],z] Poc[-n,0]

		   Ghg[3,1,#[1,2,-1/2 - p/2],#[3],z] Poc[-1/2 - p/2,0]
		 + ---------------------------------------------------
					    2

		   Ghg[3,1,#[1,4,-m],#[5],z] Poc[-m,0]
		 - -----------------------------------
				    4

#I[10]::  Sum[(z^r*x[r])/Gamma[3 + r],{r,0,Inf}]

		 r
		z  x[r]
#O[10]:   Sum[------------,{r,0,Inf}]
	      Gamma[3 + r]

#I[11]::  Convtoghg[%]

The hypergeometric conversion failed, see explanation #1

		 r
		z  x[r]
#O[11]:   Sum[------------,{r,0,Inf}]
	      Gamma[3 + r]

#I[12]::  sum : Sum[(r+3) Gamma[r+1/2]/r!,{r,0,m}]

	      Gamma[1/2 + r] (3 + r)
#O[12]:   Sum[----------------------,{r,0,m}]
			r!

#I[13]::  Natp[m+1] : 1 /* this prevents printing of the warning */

#O[13]:   1

#I[14]::  Convtoghg[sum]

	  Gamma[1/2 + m] Ghg[3,2,#[1,-m,-2 - m],#[-3 - m,1/2 - m],1] (3 + m)
#O[14]:   ------------------------------------------------------------------
					  m!

#I[15]::  x/Sum[1/4((a*z^r*(1 + r))/((3 + r)*(4 + r))),{r,0,Inf}]

			 x
#O[15]:   -------------------------------
		   r
		a z  (1 + r)
	  Sum[----------------,{r,0,Inf}]
	      4(3 + r) (4 + r)

#I[16]::  Convtoghg[%]

		    48x
#O[16]:   ------------------------
	  a Ghg[2,1,#[2,3],#[5],z]

#I[17]::  Sum[-2((a*z^r)/((3 + r)*(4 + r))),{r,0,Inf}] + Sum[(a*z^r)/(4+r),{r,0,Inf}]

		       r			  r
		  -2a z 		       a z
#O[17]:   Sum[---------------,{r,0,Inf}] + Sum[-----,{r,0,Inf}]
	      (3 + r) (4 + r)   	       4 + r

#I[18]::  Convtoghg[%]

	  -a Ghg[2,1,#[1,3],#[5],z]   a Ghg[2,1,#[1,4],#[5],z]
#O[18]:   ------------------------- + ------------------------
		      6 			 4

#I[19]::  Sum[(z^r*(1 + r + r^2))/((b + r)*(cc + r)),{r,0,Inf}]

	       r	   2
	      z  (1 + r + r )
#O[19]:   Sum[----------------,{r,0,Inf}]
	      (b + r) (cc + r)

#I[20]::  Convtoghg[%]

The hypergeometric conversion failed, see explanation #2

	       r	   2
	      z  (1 + r + r )
#O[20]:   Sum[----------------,{r,0,Inf}]
	      (b + r) (cc + r)

#I[21]::  Sum[(5 + 2r)/(r*(7 + 2r)),{r,0,Inf}]

		5 + 2r
#O[21]:   Sum[----------,{r,0,Inf}]
	      r (7 + 2r)

#I[22]::  Convtoghg[%]

The hypergeometric conversion failed, see explanation #1

		5 + 2r
#O[22]:   Sum[----------,{r,0,Inf}]
	      r (7 + 2r)

#I[23]::  Sum[(z^r*(3 + r)^(-r))/(2 + r),{r,0,Inf}]

	       r	-r
	      z  (3 + r)
#O[23]:   Sum[------------,{r,0,Inf}]
		 2 + r

#I[24]::  Convtoghg[%]

The hypergeometric conversion failed, see explanation #1

	       r	-r
	      z  (3 + r)
#O[24]:   Sum[------------,{r,0,Inf}]
		 2 + r

#I[25]::  Sum[Gamma[r+3]/Gamma[r+5/2],{r,0,n}]

	       Gamma[3 + r]
#O[25]:   Sum[--------------,{r,0,n}]
	      Gamma[5/2 + r]

#I[26]::  Convtoghg[%]

I will assume   natp[1 + n]

	  Gamma[3] Ghg[2,1,#[1,3],#[5/2],1]
#O[26]:   ---------------------------------
		     Gamma[5/2]

		    Gamma[4 + n] Ghg[2,1,#[1,4 + n],#[7/2 + n],1]
		  - ---------------------------------------------
				   Gamma[7/2 + n]

#I[27]::  sum13 : Sum[z^r/(3+r),{r,-2,Inf,2}]

		r
	       z
#O[27]:   Sum[-----,{r,-2,Inf,2}]
	      3 + r

#I[28]::  Convtoghg[%]

				   2
	  Ghg[2,1,#[1/2,1],#[3/2],z ]
#O[28]:   ---------------------------
		       2
		      z

#I[29]::  Sum[Poc[5-m,r] Poc[-2-m,r] Gamma[r+3]/(r!*Gamma[2r+5]),{r,0,m}]

	      Gamma[3 + r] Poc[-2 - m,r] Poc[5 - m,r]
#O[29]:   Sum[---------------------------------------,{r,0,m}]
			 r! Gamma[5 + 2r]

#I[30]::  Convtoghg[%]

I will assume   natp[-4 + m]

	  Gamma[3] Ghg[2,1,#[-2 - m,5 - m],#[5/2],1/4] Poc[-2 - m,0]

		  * Poc[5 - m,0]
#O[30]:   ----------------------------------------------------------
				   Gamma[5]

#I[31]::  Sum[1/r!,{r,0,m}]

	      1
#O[31]:   Sum[--,{r,0,m}]
	      r!

#I[32]::  Convtoghg[%]

	  Ghg[2,0,#[1,-m],#[],-1]
#O[32]:   -----------------------
		    m!

#I[33]::  S[%,SGhgtosum]

		  %#r
	      (-1)    Gamma[-m + %#r]
	  Sum[-----------------------,{%#r,0,m}]
		     Gamma[-m]
#O[33]:   --------------------------------------
			    m!

#I[34]::  S[%,Nterms[$x]->m+1]

		  %#r
	      (-1)    Gamma[-m + %#r]
	  Sum[-----------------------,{%#r,0,m}]
		     Gamma[-m]
#O[34]:   --------------------------------------
			    m!

#I[35]::  Sum[z^r/(r+a),{r,0,m,2}]/2

		r
	       z
	  Sum[-----,{r,0,m,2}]
	      a + r
#O[35]:   --------------------
		   2

#I[36]::  Convtoghg[%]

I will assume   natp[1 + m/2]

				       2
	  Ghg[2,1,#[1,a/2],#[1 + a/2],z ]
	  -------------------------------
			 a

			2(1 + m/2)
		       z	   Ghg[2,1,#[1,1 + a/2 + m/2],#[2 + a/2 + m/2],

					2
				       z ]
		     - --------------------------------------------------------
					      2 + a + m
#O[36]:   ---------------------------------------------------------------------
					    2

#I[37]::  N[#T]

#O[37]:   {[36]: 18.1833, [35]: 0.0166667, [34]: 0.1, [33]: 5.65,

		 [32]: 3.31667, [31]: 0.0166667, [30]: 5.15, [29]: 0.0333333,

		 [28]: 2.8, [27]: 0.0166667, [26]: 5.16667, [25]: 0.0166667,

		 [24]: 2.88333, [23]: 0.0166667, [22]: 1.63333,

		 [21]: 0.0166667, [20]: 2.75, [19]: 0.0166667, [18]: 4.23333,

		 [17]: 0.05, [16]: 2.8, [15]: 0.0333333, [14]: 6.95, [13]: 0,

		 [12]: 0.0166667, [11]: 1.38333, [10]: 0.0166667, [9]: 7.55,

		 [8]: 0.0833333, [7]: 8.73333, [6]: 0.05, [5]: 1.63333,

		 [4]: 0.0166667, [3]: 8.28333, [2]: 0.0333333, [1]: 5.4,

		 [0]: 0}

#I[38]::  <end>
*/
