/**Numerical evaluation of hypergeometric functions using an asymptotic form**/

/*K: Ghg; hypergeometric; series; summation; numerical evaluation */
/*A: John Gottschalk */
/*S: University of Western Australia */
/*D: May 1985 */
/*P: XCvgt and XNGhg are automatcially loaded when needed. */

If[~P[_XLoadonce[Loaded]],<XLoadonce]
_Negparm[Init] :: Loadonce[XCvgt]
_n[Init]       :: Loadonce[XNGhg]

/*: Tail[$Ghg,$max] 
	returns the asymptotic form of the hypergeometric function $Ghg
	when the argument is +1 or -1 and zero otherwise. */
	
Tail_:Tier
Tail[$$x] : 0

Tail[Ghg[$p_=$p>2,$p-1,#[$$a_=~Negparm[{$$a}]],#[$$b],1],$max] :: \
 (Lcl[%a,%b,%m,%p];\
  %a : {$$a}; %b : {$$b};\
  %m : Ap[Plus,%b]-Ap[Plus,%a];\
  %p : 1 + (Ap[Plus,Map[$1^2,%a]]-Ap[Plus,Map[$1^2,%b]]-1)/(2(%m+1));\
  Ap[Mult,Map[Gamma,%b]]/Ap[Mult,Map[Gamma,%a]] \
    (1/(%m ($max+1)^%m) + %p/($max+1)^(%m+1)))

Tail[Ghg[$p_=$p>1,$p-1,#[$$a_=~Negparm[{$$a}]],#[$$b],-1],$max] :: \
 (Lcl[%a,%b,%m,%p];\
  %a : {$$a}; %b : {$$b};\
  %m : Ap[Plus,%b]-Ap[Plus,%a];\
  %p : 3%m + 2 + Ap[Plus,Map[$1^2,%a]] - Ap[Plus,Map[$1^2,%b]];\
  (-1)^($max+1) Ap[Mult,Map[Gamma,%b]]/Ap[Mult,Map[Gamma,%a]] \
    (1/(2($max+1)^(%m+1)) + %p/(4($max+1)^(%m+2))))

/*: GhgN[$Ghg,($max:250)]
	returns an estimate of the numercial value of $Ghg. The series
	is evaluated to $max terms. In the cases of the argument of $Ghg 
	being +1 or -1 the asymptotic form (see Paul Abbott's notes)
	is added to this truncated series, greatly improving the estimate of
	the numerical value. At present n[$Ghg,$max] is called then 
	Tail[$Ghg,$max] is added, so if n truncates the series at some
	value less than $max the value returned from GhgN may be badly 
	affected. */
GhgN_:Tier
GhgN[$Ghg] :: GhgN[$Ghg,250]
GhgN[$Ghg,$max] :: N[n[$Ghg,,$max] + Tail[$Ghg,$max]]

_XGhgN[Loaded] : 1

