/** XGammaSimp **/

/*K: Gamma; reduction; canonical form; simplify */
/*A: John Gottschalk */
/*S: University of Western Australia */
/*D: March 1985 */
/*P: XGammaConv, XGammaV, XSEmbed and XSumM are automatically loaded as needed. */
/*F: Extend GammaTidy */

If[~P[_XLoadonce[Loaded]],<XLoadonce]
_SGammaConv[Init]  :: Loadonce[XGammaConv]
_SiEmbed[Init]     :: Loadonce[XSEmbed]
_SEmbed[Init]      :: Loadonce[XSEmbed]
_SumCan[Init]      :: Loadonce[XSumM]
_SSumM[Init]       :: Loadonce[XSumM]
_Poc[Init]	   :: Loadonce[XPocV]

Loadonce[XGammaV]

_Sum[Smp] : Inf

SumTidy[$expr] :: (Lcl[%e]; %e : SumCan[$expr]; S[%e,SSumTidy])

SSumTidy : Sum[$x,{$i,$min,$max}] --> (Lcl[%e]; \
    %e : Sum[GammaTidy[$x,$i],{$i,$min,$max}]; S[%e,SSumM[2]])

/*: GammaTidy[$expr,$r]
	is a heuristic procedure to reduce $expr which contains
	ratios of Gamma functions in $r. $r must be a nonnegative
	integer. */
GammaTidy_:Tier
GammaTidy[$expr_=($expr[0]='Plus),$r] :: Map[GammaTidy[$1,$r],$expr]
GammaTidy[$expr,$r] :: (Lcl[%e]; Pr[]; \
    %e : S[Ncfac[%e],SGammaEx]; \
    Pr[" GammaTidy: applying SGammaConv"]; \
    %e : Si[$expr,SGammaConv[$r]]; \
    Pr[" GammaTidy: applying SGammared[1]"]; \
    %e : SiEmbed[%e,SGammared[1]]; \
    Pr[" GammaTidy: applying SGammaDup"]; \
    %e : Si[%e,SGammaDup]; \
    S[Ncfac[%e],SGammaEx])

SGammaEx : Gamma[$x] --> Gamma[Ex[$x]]

GenR[$x] :: S[$x,$y_=$z->$y]

/*: GammaDup[$expr] 
	applies the duplication formula for gamma functions to
	reduce products of gamma functions in $expr. */
GammaDup[$expr] :: Si[$expr,MultSub[SGammaDup]]

/*W: SGammaDup is not valid for expressions such as Gamma[-k] Gamma[-1/2-k]
     when k is a positive integer. */
SGammaDup : Gamma[$a] Gamma[$b_=(Intp[Ex[$b-$a-1/2]]&Numbp[Ex[$b-$a-1/2]])]--> \
		Gamma[Ex[2$a,,,1]] Poc[$a+1/2,$b-$a-1/2] 2^(1-2$a) Pi^(1/2)

/*: GammaRef[$expr] 
	applies the reflection  formula for gamma functions to
	reduce products of gamma functions in $expr. */
GammaRef[$expr] :: S[Si[$expr,MultSub[SGammaRef]],SGammaEx]

SGammaRef : Gamma[$a] Gamma[$b_=Intp[Ex[$b+$a-1]]] -> \
		 Poc[1-$a,$b+$a-1] Pi/Sin[Pi $a]

MultSub_:Ldist
MultSub[$x $y -> $sub] :: {$x $y       -> $sub,\
			   $x^$n $y    -> $sub GenR[$x^($n-1)],\
			   $x $y^$m    -> $sub GenR[$y^($m-1)],\
			   $x^$n $y^$n -> $sub^$n,\
			   $x^$n $y^($m_=(Numbp[$n] & $n>0) | \
		   (Numbp[$m] & $m>0)) -> $sub GenR[$x^($n-1) $y^($m-1)]}

DivSub_:Ldist
DivSub[$x/$y -> $sub] :: {$x/$y       -> $sub,\
                          $x^$n/$y    -> $sub GenR[$x^($n-1)],\
			  $x/$y^$m    -> $sub/GenR[$y^($m-1)],\
			  $x^$n/$y^$n -> $sub^$n,\
                          $x^$n/$y^($m_=(Numbp[$m] & $m>0) | \
		  (Numbp[$n] & $n>0)) -> $sub GenR[$x^($n-$1)/$y^($m-$1)]}

/* Gammared rewrites ratios of Gamma functions as polynomials. */
Gammared[$expr_=$expr[0] = 'Plus] :: Map[Gammared,$expr]
Gammared[$expr] :: S[Ncfac[SiEmbed[$expr,SGammared]],SGammaEx]

SGammared :: Gamma[$x]/Gamma[$y_=Intp[Ex[$x-$y]] & Numbp[Ex[$x-$y]]] -> \
		                 Poc[$y,Ex[$x-$y]]

/* The following are special cases of SGammacb and Gammacb */

Gammacb[$expr] :: (Lcl[%e]; %e : $expr; Pr[]; \
   Pr[" Gammacb: applying SGammacb[1]"]; \
   %e : Si[%e,MultSub[SGammacb[1]]]; \
   Pr[" Gammacb: applying SGammacb[2]"]; \
   %e : Si[%e,MultSub[SGammacb[2]]]; \
   Pr[" Gammacb: applying SGammacb[3]"]; \
   %e : Si[%e,MultSub[SGammacb[3]]])
Gammacb[$expr_= ($expr[0] = 'Plus) | ($expr[0] = 'Div)] :: Map[Gammacb,$expr]

SGammacb[1] : ($x*Gamma[$x] -> Gamma[1 + $x])
SGammacb[2] : (Gamma[$x]*($y _= $y = Ex[2*$x]) -> 2Gamma[1 + $x])
SGammacb[3] : (Gamma[$x]*($y _= $y = Ex[-2*$x]) -> -2Gamma[1 + $x])

/* The following substitutions have not been completed so are
   commented out for now. 

/* Gammacb combines monomials with gamma functions and gamma
         functions with other gamma functions where possible. */
Gammacb[$exp] :: Si[SiEmbed[Map[Fac,$exp],SGammacb],SGammaDup]

SGammacb[$var][1] :: Gamma[$x] ($y_=Pmod[$y,$x,$var]=0)  -> \
					Gamma[$x+1] Pdiv[$y,$x,$var]
SGammacb[$var][2] :: \
     Gamma[$x]^($n_=Numbp[$n]&Intp[$n]) ($y_=Pmod[$y,$x,$var]=0)  -> \
					Gamma[$x+1]^($n-1) Pdiv[$y,$x,$var]
SGammacb[$var][3] :: Gamma[$x]/($y_=Pmod[$y,$x-1,$var]=0)  -> \
					Gamma[$x-1]/Pdiv[$y,$x-1,$var]
SGammacb[$var][4] :: \
     Gamma[$x]^($n_=Numbp[$n]&Intp[$n])/($y_=Pmod[$y,$x-1,$var]=0)  -> \
					Gamma[$x-1]^($n-1)/Pdiv[$y,$x-1,$var]
SGammacb[$var][5] :: ($y_=Pmod[$y,$x-1,$var]=0)/Gamma[$x]     -> \
	    Pdiv[$y,$x-1,$var]/Gamma[$x-1]
SGammacb[$var][6] :: \
     ($y_=Pmod[$y,$x-1,$var]=0)/Gamma[$x]^($n_=Numbp[$n]&Intp[$n])     -> \
	    Pdiv[$y,$x-1,$var]/Gamma[$x-1]^($n-1)
SGammacb[$var][7] :: Gamma[$x]/Gamma[$y_=Ratp[Pdiv[$x-1,$y-1,$var]]] -> \
			  Gamma[$x-1]/Gamma[$y-1] Pdiv[$x-1,$y-1,$var]
SGammacb[$var][8] :: Gamma[$x]/Gamma[$y_=Ratp[Pdiv[$x-1,$y-1,$var]]] -> \
			  Gamma[$x-1]/Gamma[$y-1] Pdiv[$x-1,$y-1,$var]

SGammacb[$var][7] :: Gamma[$x]/Gamma[$y_=Ratp[Pdiv[$x-1,$y-1,$var]]] -> \
			  Gamma[$x-1]/Gamma[$y-1] Pdiv[$x-1,$y-1,$var]
SGammacb[$var][7] :: Gamma[$x]/Gamma[$y_=Ratp[Pdiv[$x-1,$y-1,$var]]] -> \
			  Gamma[$x-1]/Gamma[$y-1] Pdiv[$x-1,$y-1,$var]
*/

_XGammaSimp[Loaded] : 1

/*E:
SMP 1.5.0   (May 14 1986)

#I[1]::  <XGammaSimp

#I[2]::  Gamma[b] Gamma[b+1/2]^r Gamma[b+1] Gamma[b+5]/Gamma[b+4]

				r
	 Gamma[b] Gamma[1/2 + b]  Gamma[1 + b] Gamma[5 + b]
#O[2]:   --------------------------------------------------
			    Gamma[4 + b]

#I[3]::  GammaDup[%]

	  -6b   3/2     	  -3 + r		  3
	 2    Pi    Gamma[1/2 + b]       Gamma[2(1/2 + b)]  (1 + b) (2 + b)

	     * (3 + b) (4 + b)
#O[3]:   ------------------------------------------------------------------
				   b Gamma[4 + b]

#I[4]::  <XGammaSimp

#I[5]::  (a+4) Gamma[a+5] Gamma[a+6] Gamma[a+7/2]/\
	 	 	     ((8+2a) Gamma[a+3] Gamma[a+4] Gamma[a+9/2])

	 Gamma[7/2 + a] Gamma[5 + a] Gamma[6 + a] (4 + a)
#O[5]:   -------------------------------------------------
	 Gamma[3 + a] Gamma[4 + a] Gamma[9/2 + a] (8 + 2a)

#I[6]::  Gammared[%]

			2
	 (3 + a) (4 + a)  (5 + a)
#O[6]:   ------------------------
		  7 + 2a

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