                 /** Simplification of Gamma functions **/

/*K: Gamma */
/*A: John Gottschalk */
/*S: University of Western Australia */
/*D: September 1985 */
/*P: XDelete, XLexpr and XSets are automatically loaded as needed. */

If[~P[_XLoadonce[Loaded]],<XLoadonce]

Loadonce[XPocV]
_Cmpl[Init] :: Loadonce[XSets]
_DelE[Init] :: Loadonce[XDelete]
_Projfind[Init] :: Loadonce[XLexpr]

/*: CanGamma[$expr] finds the Gamma function in $expr with the lowest argument
    and writes all other Gamma functions in terms of this using the recurrence
    equation for digamma functions. */
CanGamma[$expr] :: (Lcl[%allpsi,%arg,%expr,%likepsi,%Rep,%reps,%Minpsi,%minpsi];\
      %Minpsi[$x] :: (Ar[Len[$x],$x[$1][1]]; {Ap[Min,%%]}; Ap[Gamma,%%]);\
      %Rep[Gamma[$x],Gamma[$z]] :: If[$x~=$z,Gamma[$x] -> Gamma[$z]\
			           Poc[$z,$x-$z]];\
      %expr : S[$expr,Gamma[$x] --> Gamma[Ex[$x]]]; %reps : {};\
      %allpsi : Projfind['Gamma,%expr];\
      Loop[,(%arg : %allpsi[1,1];\
	     %likepsi : Projfind['Gamma,%allpsi,Intp[$1[1]-%arg] & \
				   Numbp[$1[1]-%arg]];\
	     %minpsi : %Minpsi[%likepsi];\
	     %reps : Cat[%reps,Map[%Rep[$1,%minpsi],%likepsi]];\
	     %allpsi : Cmpl[%likepsi,%allpsi]),\
           Len[%allpsi] > 0];\
      S[%expr,DelE[Null,%reps]])

/*: Projfind[$proj,$exp]
	      returns a list of all occurences of the $proj in $exp
	      with the filters. Any numerical coefficients that may
	      have multiplied $proj in $exp are not returned in the list. */

_Projfind[Smp] : 0
Projfind_:Tier

Projfind[$proj,$exp] :: (Pos[$proj[$$a],$exp,$exp]; Union[%%/Map[Nc,%%]])
Projfind[$proj,$exp,$ptemp] :: \
  (Projfind[$proj,$exp]; Cat[Ar[Len[%%],%%,,$ptemp]])

