gsl_specfunc__hyperg_1F1.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065
  1. /* specfunc/hyperg_1F1.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. /* Author: G. Jungman */
  20. #include "gsl__config.h"
  21. #include "gsl_math.h"
  22. #include "gsl_errno.h"
  23. #include "gsl_sf_elementary.h"
  24. #include "gsl_sf_exp.h"
  25. #include "gsl_sf_bessel.h"
  26. #include "gsl_sf_gamma.h"
  27. #include "gsl_sf_laguerre.h"
  28. #include "gsl_sf_hyperg.h"
  29. #include "gsl_specfunc__error.h"
  30. #include "gsl_specfunc__hyperg.h"
  31. #define _1F1_INT_THRESHOLD (100.0*GSL_DBL_EPSILON)
  32. /* Asymptotic result for 1F1(a, b, x) x -> -Infinity.
  33. * Assumes b-a != neg integer and b != neg integer.
  34. */
  35. static
  36. int
  37. hyperg_1F1_asymp_negx(const double a, const double b, const double x,
  38. gsl_sf_result * result)
  39. {
  40. gsl_sf_result lg_b;
  41. gsl_sf_result lg_bma;
  42. double sgn_b;
  43. double sgn_bma;
  44. int stat_b = gsl_sf_lngamma_sgn_e(b, &lg_b, &sgn_b);
  45. int stat_bma = gsl_sf_lngamma_sgn_e(b-a, &lg_bma, &sgn_bma);
  46. if(stat_b == GSL_SUCCESS && stat_bma == GSL_SUCCESS) {
  47. gsl_sf_result F;
  48. int stat_F = gsl_sf_hyperg_2F0_series_e(a, 1.0+a-b, -1.0/x, -1, &F);
  49. if(F.val != 0) {
  50. double ln_term_val = a*log(-x);
  51. double ln_term_err = 2.0 * GSL_DBL_EPSILON * (fabs(a) + fabs(ln_term_val));
  52. double ln_pre_val = lg_b.val - lg_bma.val - ln_term_val;
  53. double ln_pre_err = lg_b.err + lg_bma.err + ln_term_err;
  54. int stat_e = gsl_sf_exp_mult_err_e(ln_pre_val, ln_pre_err,
  55. sgn_bma*sgn_b*F.val, F.err,
  56. result);
  57. return GSL_ERROR_SELECT_2(stat_e, stat_F);
  58. }
  59. else {
  60. result->val = 0.0;
  61. result->err = 0.0;
  62. return stat_F;
  63. }
  64. }
  65. else {
  66. DOMAIN_ERROR(result);
  67. }
  68. }
  69. /* Asymptotic result for 1F1(a, b, x) x -> +Infinity
  70. * Assumes b != neg integer and a != neg integer
  71. */
  72. static
  73. int
  74. hyperg_1F1_asymp_posx(const double a, const double b, const double x,
  75. gsl_sf_result * result)
  76. {
  77. gsl_sf_result lg_b;
  78. gsl_sf_result lg_a;
  79. double sgn_b;
  80. double sgn_a;
  81. int stat_b = gsl_sf_lngamma_sgn_e(b, &lg_b, &sgn_b);
  82. int stat_a = gsl_sf_lngamma_sgn_e(a, &lg_a, &sgn_a);
  83. if(stat_a == GSL_SUCCESS && stat_b == GSL_SUCCESS) {
  84. gsl_sf_result F;
  85. int stat_F = gsl_sf_hyperg_2F0_series_e(b-a, 1.0-a, 1.0/x, -1, &F);
  86. if(stat_F == GSL_SUCCESS && F.val != 0) {
  87. double lnx = log(x);
  88. double ln_term_val = (a-b)*lnx;
  89. double ln_term_err = 2.0 * GSL_DBL_EPSILON * (fabs(a) + fabs(b)) * fabs(lnx)
  90. + 2.0 * GSL_DBL_EPSILON * fabs(a-b);
  91. double ln_pre_val = lg_b.val - lg_a.val + ln_term_val + x;
  92. double ln_pre_err = lg_b.err + lg_a.err + ln_term_err + 2.0 * GSL_DBL_EPSILON * fabs(x);
  93. int stat_e = gsl_sf_exp_mult_err_e(ln_pre_val, ln_pre_err,
  94. sgn_a*sgn_b*F.val, F.err,
  95. result);
  96. return GSL_ERROR_SELECT_2(stat_e, stat_F);
  97. }
  98. else {
  99. result->val = 0.0;
  100. result->err = 0.0;
  101. return stat_F;
  102. }
  103. }
  104. else {
  105. DOMAIN_ERROR(result);
  106. }
  107. }
  108. /* Asymptotic result from Slater 4.3.7
  109. *
  110. * To get the general series, write M(a,b,x) as
  111. *
  112. * M(a,b,x)=sum ((a)_n/(b)_n) (x^n / n!)
  113. *
  114. * and expand (b)_n in inverse powers of b as follows
  115. *
  116. * -log(1/(b)_n) = sum_(k=0)^(n-1) log(b+k)
  117. * = n log(b) + sum_(k=0)^(n-1) log(1+k/b)
  118. *
  119. * Do a taylor expansion of the log in 1/b and sum the resulting terms
  120. * using the standard algebraic formulas for finite sums of powers of
  121. * k. This should then give
  122. *
  123. * M(a,b,x) = sum_(n=0)^(inf) (a_n/n!) (x/b)^n * (1 - n(n-1)/(2b)
  124. * + (n-1)n(n+1)(3n-2)/(24b^2) + ...
  125. *
  126. * which can be summed explicitly. The trick for summing it is to take
  127. * derivatives of sum_(i=0)^(inf) a_n*y^n/n! = (1-y)^(-a);
  128. *
  129. * [BJG 16/01/2007]
  130. */
  131. static
  132. int
  133. hyperg_1F1_largebx(const double a, const double b, const double x, gsl_sf_result * result)
  134. {
  135. double y = x/b;
  136. double f = exp(-a*log1p(-y));
  137. double t1 = -((a*(a+1.0))/(2*b))*pow((y/(1.0-y)),2.0);
  138. double t2 = (1/(24*b*b))*((a*(a+1)*y*y)/pow(1-y,4))*(12+8*(2*a+1)*y+(3*a*a-a-2)*y*y);
  139. double t3 = (-1/(48*b*b*b*pow(1-y,6)))*a*((a + 1)*((y*((a + 1)*(a*(y*(y*((y*(a - 2) + 16)*(a - 1)) + 72)) + 96)) + 24)*pow(y, 2)));
  140. result->val = f * (1 + t1 + t2 + t3);
  141. result->err = 2*fabs(f*t3) + 2*GSL_DBL_EPSILON*fabs(result->val);
  142. return GSL_SUCCESS;
  143. }
  144. /* Asymptotic result for x < 2b-4a, 2b-4a large.
  145. * [Abramowitz+Stegun, 13.5.21]
  146. *
  147. * assumes 0 <= x/(2b-4a) <= 1
  148. */
  149. static
  150. int
  151. hyperg_1F1_large2bm4a(const double a, const double b, const double x, gsl_sf_result * result)
  152. {
  153. double eta = 2.0*b - 4.0*a;
  154. double cos2th = x/eta;
  155. double sin2th = 1.0 - cos2th;
  156. double th = acos(sqrt(cos2th));
  157. double pre_h = 0.25*M_PI*M_PI*eta*eta*cos2th*sin2th;
  158. gsl_sf_result lg_b;
  159. int stat_lg = gsl_sf_lngamma_e(b, &lg_b);
  160. double t1 = 0.5*(1.0-b)*log(0.25*x*eta);
  161. double t2 = 0.25*log(pre_h);
  162. double lnpre_val = lg_b.val + 0.5*x + t1 - t2;
  163. double lnpre_err = lg_b.err + 2.0 * GSL_DBL_EPSILON * (fabs(0.5*x) + fabs(t1) + fabs(t2));
  164. #if SMALL_ANGLE
  165. const double eps = asin(sqrt(cos2th)); /* theta = pi/2 - eps */
  166. double s1 = (fmod(a, 1.0) == 0.0) ? 0.0 : sin(a*M_PI);
  167. double eta_reduc = (fmod(eta + 1, 4.0) == 0.0) ? 0.0 : fmod(eta + 1, 8.0);
  168. double phi1 = 0.25*eta_reduc*M_PI;
  169. double phi2 = 0.25*eta*(2*eps + sin(2.0*eps));
  170. double s2 = sin(phi1 - phi2);
  171. #else
  172. double s1 = sin(a*M_PI);
  173. double s2 = sin(0.25*eta*(2.0*th - sin(2.0*th)) + 0.25*M_PI);
  174. #endif
  175. double ser_val = s1 + s2;
  176. double ser_err = 2.0 * GSL_DBL_EPSILON * (fabs(s1) + fabs(s2));
  177. int stat_e = gsl_sf_exp_mult_err_e(lnpre_val, lnpre_err,
  178. ser_val, ser_err,
  179. result);
  180. return GSL_ERROR_SELECT_2(stat_e, stat_lg);
  181. }
  182. /* Luke's rational approximation.
  183. * See [Luke, Algorithms for the Computation of Mathematical Functions, p.182]
  184. *
  185. * Like the case of the 2F1 rational approximations, these are
  186. * probably guaranteed to converge for x < 0, barring gross
  187. * numerical instability in the pre-asymptotic regime.
  188. */
  189. static
  190. int
  191. hyperg_1F1_luke(const double a, const double c, const double xin,
  192. gsl_sf_result * result)
  193. {
  194. const double RECUR_BIG = 1.0e+50;
  195. const int nmax = 5000;
  196. int n = 3;
  197. const double x = -xin;
  198. const double x3 = x*x*x;
  199. const double t0 = a/c;
  200. const double t1 = (a+1.0)/(2.0*c);
  201. const double t2 = (a+2.0)/(2.0*(c+1.0));
  202. double F = 1.0;
  203. double prec;
  204. double Bnm3 = 1.0; /* B0 */
  205. double Bnm2 = 1.0 + t1 * x; /* B1 */
  206. double Bnm1 = 1.0 + t2 * x * (1.0 + t1/3.0 * x); /* B2 */
  207. double Anm3 = 1.0; /* A0 */
  208. double Anm2 = Bnm2 - t0 * x; /* A1 */
  209. double Anm1 = Bnm1 - t0*(1.0 + t2*x)*x + t0 * t1 * (c/(c+1.0)) * x*x; /* A2 */
  210. while(1) {
  211. double npam1 = n + a - 1;
  212. double npcm1 = n + c - 1;
  213. double npam2 = n + a - 2;
  214. double npcm2 = n + c - 2;
  215. double tnm1 = 2*n - 1;
  216. double tnm3 = 2*n - 3;
  217. double tnm5 = 2*n - 5;
  218. double F1 = (n-a-2) / (2*tnm3*npcm1);
  219. double F2 = (n+a)*npam1 / (4*tnm1*tnm3*npcm2*npcm1);
  220. double F3 = -npam2*npam1*(n-a-2) / (8*tnm3*tnm3*tnm5*(n+c-3)*npcm2*npcm1);
  221. double E = -npam1*(n-c-1) / (2*tnm3*npcm2*npcm1);
  222. double An = (1.0+F1*x)*Anm1 + (E + F2*x)*x*Anm2 + F3*x3*Anm3;
  223. double Bn = (1.0+F1*x)*Bnm1 + (E + F2*x)*x*Bnm2 + F3*x3*Bnm3;
  224. double r = An/Bn;
  225. prec = fabs((F - r)/F);
  226. F = r;
  227. if(prec < GSL_DBL_EPSILON || n > nmax) break;
  228. if(fabs(An) > RECUR_BIG || fabs(Bn) > RECUR_BIG) {
  229. An /= RECUR_BIG;
  230. Bn /= RECUR_BIG;
  231. Anm1 /= RECUR_BIG;
  232. Bnm1 /= RECUR_BIG;
  233. Anm2 /= RECUR_BIG;
  234. Bnm2 /= RECUR_BIG;
  235. Anm3 /= RECUR_BIG;
  236. Bnm3 /= RECUR_BIG;
  237. }
  238. else if(fabs(An) < 1.0/RECUR_BIG || fabs(Bn) < 1.0/RECUR_BIG) {
  239. An *= RECUR_BIG;
  240. Bn *= RECUR_BIG;
  241. Anm1 *= RECUR_BIG;
  242. Bnm1 *= RECUR_BIG;
  243. Anm2 *= RECUR_BIG;
  244. Bnm2 *= RECUR_BIG;
  245. Anm3 *= RECUR_BIG;
  246. Bnm3 *= RECUR_BIG;
  247. }
  248. n++;
  249. Bnm3 = Bnm2;
  250. Bnm2 = Bnm1;
  251. Bnm1 = Bn;
  252. Anm3 = Anm2;
  253. Anm2 = Anm1;
  254. Anm1 = An;
  255. }
  256. result->val = F;
  257. result->err = 2.0 * fabs(F * prec);
  258. result->err += 2.0 * GSL_DBL_EPSILON * (n-1.0) * fabs(F);
  259. return GSL_SUCCESS;
  260. }
  261. /* Series for 1F1(1,b,x)
  262. * b > 0
  263. */
  264. static
  265. int
  266. hyperg_1F1_1_series(const double b, const double x, gsl_sf_result * result)
  267. {
  268. double sum_val = 1.0;
  269. double sum_err = 0.0;
  270. double term = 1.0;
  271. double n = 1.0;
  272. while(fabs(term/sum_val) > 0.25*GSL_DBL_EPSILON) {
  273. term *= x/(b+n-1);
  274. sum_val += term;
  275. sum_err += 8.0*GSL_DBL_EPSILON*fabs(term) + GSL_DBL_EPSILON*fabs(sum_val);
  276. n += 1.0;
  277. }
  278. result->val = sum_val;
  279. result->err = sum_err;
  280. result->err += 2.0 * fabs(term);
  281. return GSL_SUCCESS;
  282. }
  283. /* 1F1(1,b,x)
  284. * b >= 1, b integer
  285. */
  286. static
  287. int
  288. hyperg_1F1_1_int(const int b, const double x, gsl_sf_result * result)
  289. {
  290. if(b < 1) {
  291. DOMAIN_ERROR(result);
  292. }
  293. else if(b == 1) {
  294. return gsl_sf_exp_e(x, result);
  295. }
  296. else if(b == 2) {
  297. return gsl_sf_exprel_e(x, result);
  298. }
  299. else if(b == 3) {
  300. return gsl_sf_exprel_2_e(x, result);
  301. }
  302. else {
  303. return gsl_sf_exprel_n_e(b-1, x, result);
  304. }
  305. }
  306. /* 1F1(1,b,x)
  307. * b >=1, b real
  308. *
  309. * checked OK: [GJ] Thu Oct 1 16:46:35 MDT 1998
  310. */
  311. static
  312. int
  313. hyperg_1F1_1(const double b, const double x, gsl_sf_result * result)
  314. {
  315. double ax = fabs(x);
  316. double ib = floor(b + 0.1);
  317. if(b < 1.0) {
  318. DOMAIN_ERROR(result);
  319. }
  320. else if(b == 1.0) {
  321. return gsl_sf_exp_e(x, result);
  322. }
  323. else if(b >= 1.4*ax) {
  324. return hyperg_1F1_1_series(b, x, result);
  325. }
  326. else if(fabs(b - ib) < _1F1_INT_THRESHOLD && ib < INT_MAX) {
  327. return hyperg_1F1_1_int((int)ib, x, result);
  328. }
  329. else if(x > 0.0) {
  330. if(x > 100.0 && b < 0.75*x) {
  331. return hyperg_1F1_asymp_posx(1.0, b, x, result);
  332. }
  333. else if(b < 1.0e+05) {
  334. /* Recurse backward on b, from a
  335. * chosen offset point. For x > 0,
  336. * which holds here, this should
  337. * be a stable direction.
  338. */
  339. const double off = ceil(1.4*x-b) + 1.0;
  340. double bp = b + off;
  341. gsl_sf_result M;
  342. int stat_s = hyperg_1F1_1_series(bp, x, &M);
  343. const double err_rat = M.err / fabs(M.val);
  344. while(bp > b+0.1) {
  345. /* M(1,b-1) = x/(b-1) M(1,b) + 1 */
  346. bp -= 1.0;
  347. M.val = 1.0 + x/bp * M.val;
  348. }
  349. result->val = M.val;
  350. result->err = err_rat * fabs(M.val);
  351. result->err += 2.0 * GSL_DBL_EPSILON * (fabs(off)+1.0) * fabs(M.val);
  352. return stat_s;
  353. } else if (fabs(x) < fabs(b) && fabs(x) < sqrt(fabs(b)) * fabs(b-x)) {
  354. return hyperg_1F1_largebx(1.0, b, x, result);
  355. } else if (fabs(x) > fabs(b)) {
  356. return hyperg_1F1_1_series(b, x, result);
  357. } else {
  358. return hyperg_1F1_large2bm4a(1.0, b, x, result);
  359. }
  360. }
  361. else {
  362. /* x <= 0 and b not large compared to |x|
  363. */
  364. if(ax < 10.0 && b < 10.0) {
  365. return hyperg_1F1_1_series(b, x, result);
  366. }
  367. else if(ax >= 100.0 && GSL_MAX_DBL(fabs(2.0-b),1.0) < 0.99*ax) {
  368. return hyperg_1F1_asymp_negx(1.0, b, x, result);
  369. }
  370. else {
  371. return hyperg_1F1_luke(1.0, b, x, result);
  372. }
  373. }
  374. }
  375. /* 1F1(a,b,x)/Gamma(b) for b->0
  376. * [limit of Abramowitz+Stegun 13.3.7]
  377. */
  378. static
  379. int
  380. hyperg_1F1_renorm_b0(const double a, const double x, gsl_sf_result * result)
  381. {
  382. double eta = a*x;
  383. if(eta > 0.0) {
  384. double root_eta = sqrt(eta);
  385. gsl_sf_result I1_scaled;
  386. int stat_I = gsl_sf_bessel_I1_scaled_e(2.0*root_eta, &I1_scaled);
  387. if(I1_scaled.val <= 0.0) {
  388. result->val = 0.0;
  389. result->err = 0.0;
  390. return GSL_ERROR_SELECT_2(stat_I, GSL_EDOM);
  391. }
  392. else {
  393. /* Note that 13.3.7 contains higher terms which are zeroth order
  394. in b. These make a non-negligible contribution to the sum.
  395. With the first correction term, the I1 above is replaced by
  396. I1 + (2/3)*a*(x/(4a))**(3/2)*I2(2*root_eta). We will add
  397. this as part of the result and error estimate. */
  398. const double corr1 =(2.0/3.0)*a*pow(x/(4.0*a),1.5)*gsl_sf_bessel_In_scaled(2, 2.0*root_eta)
  399. ;
  400. const double lnr_val = 0.5*x + 0.5*log(eta) + fabs(2.0*root_eta) + log(I1_scaled.val+corr1);
  401. const double lnr_err = GSL_DBL_EPSILON * (1.5*fabs(x) + 1.0) + fabs((I1_scaled.err+corr1)/I1_scaled.val);
  402. return gsl_sf_exp_err_e(lnr_val, lnr_err, result);
  403. }
  404. }
  405. else if(eta == 0.0) {
  406. result->val = 0.0;
  407. result->err = 0.0;
  408. return GSL_SUCCESS;
  409. }
  410. else {
  411. /* eta < 0 */
  412. double root_eta = sqrt(-eta);
  413. gsl_sf_result J1;
  414. int stat_J = gsl_sf_bessel_J1_e(2.0*root_eta, &J1);
  415. if(J1.val <= 0.0) {
  416. result->val = 0.0;
  417. result->err = 0.0;
  418. return GSL_ERROR_SELECT_2(stat_J, GSL_EDOM);
  419. }
  420. else {
  421. const double t1 = 0.5*x;
  422. const double t2 = 0.5*log(-eta);
  423. const double t3 = fabs(x);
  424. const double t4 = log(J1.val);
  425. const double lnr_val = t1 + t2 + t3 + t4;
  426. const double lnr_err = GSL_DBL_EPSILON * (1.5*fabs(x) + 1.0) + fabs(J1.err/J1.val);
  427. gsl_sf_result ex;
  428. int stat_e = gsl_sf_exp_err_e(lnr_val, lnr_err, &ex);
  429. result->val = -ex.val;
  430. result->err = ex.err;
  431. return stat_e;
  432. }
  433. }
  434. }
  435. /* 1F1'(a,b,x)/1F1(a,b,x)
  436. * Uses Gautschi's version of the CF.
  437. * [Gautschi, Math. Comp. 31, 994 (1977)]
  438. *
  439. * Supposedly this suffers from the "anomalous convergence"
  440. * problem when b < x. I have seen anomalous convergence
  441. * in several of the continued fractions associated with
  442. * 1F1(a,b,x). This particular CF formulation seems stable
  443. * for b > x. However, it does display a painful artifact
  444. * of the anomalous convergence; the convergence plateaus
  445. * unless b >>> x. For example, even for b=1000, x=1, this
  446. * method locks onto a ratio which is only good to about
  447. * 4 digits. Apparently the rest of the digits are hiding
  448. * way out on the plateau, but finite-precision lossage
  449. * means you will never get them.
  450. */
  451. #if 0
  452. static
  453. int
  454. hyperg_1F1_CF1_p(const double a, const double b, const double x, double * result)
  455. {
  456. const double RECUR_BIG = GSL_SQRT_DBL_MAX;
  457. const int maxiter = 5000;
  458. int n = 1;
  459. double Anm2 = 1.0;
  460. double Bnm2 = 0.0;
  461. double Anm1 = 0.0;
  462. double Bnm1 = 1.0;
  463. double a1 = 1.0;
  464. double b1 = 1.0;
  465. double An = b1*Anm1 + a1*Anm2;
  466. double Bn = b1*Bnm1 + a1*Bnm2;
  467. double an, bn;
  468. double fn = An/Bn;
  469. while(n < maxiter) {
  470. double old_fn;
  471. double del;
  472. n++;
  473. Anm2 = Anm1;
  474. Bnm2 = Bnm1;
  475. Anm1 = An;
  476. Bnm1 = Bn;
  477. an = (a+n)*x/((b-x+n-1)*(b-x+n));
  478. bn = 1.0;
  479. An = bn*Anm1 + an*Anm2;
  480. Bn = bn*Bnm1 + an*Bnm2;
  481. if(fabs(An) > RECUR_BIG || fabs(Bn) > RECUR_BIG) {
  482. An /= RECUR_BIG;
  483. Bn /= RECUR_BIG;
  484. Anm1 /= RECUR_BIG;
  485. Bnm1 /= RECUR_BIG;
  486. Anm2 /= RECUR_BIG;
  487. Bnm2 /= RECUR_BIG;
  488. }
  489. old_fn = fn;
  490. fn = An/Bn;
  491. del = old_fn/fn;
  492. if(fabs(del - 1.0) < 10.0*GSL_DBL_EPSILON) break;
  493. }
  494. *result = a/(b-x) * fn;
  495. if(n == maxiter)
  496. GSL_ERROR ("error", GSL_EMAXITER);
  497. else
  498. return GSL_SUCCESS;
  499. }
  500. #endif /* 0 */
  501. /* 1F1'(a,b,x)/1F1(a,b,x)
  502. * Uses Gautschi's series transformation of the
  503. * continued fraction. This is apparently the best
  504. * method for getting this ratio in the stable region.
  505. * The convergence is monotone and supergeometric
  506. * when b > x.
  507. * Assumes a >= -1.
  508. */
  509. static
  510. int
  511. hyperg_1F1_CF1_p_ser(const double a, const double b, const double x, double * result)
  512. {
  513. if(a == 0.0) {
  514. *result = 0.0;
  515. return GSL_SUCCESS;
  516. }
  517. else {
  518. const int maxiter = 5000;
  519. double sum = 1.0;
  520. double pk = 1.0;
  521. double rhok = 0.0;
  522. int k;
  523. for(k=1; k<maxiter; k++) {
  524. double ak = (a + k)*x/((b-x+k-1.0)*(b-x+k));
  525. rhok = -ak*(1.0 + rhok)/(1.0 + ak*(1.0+rhok));
  526. pk *= rhok;
  527. sum += pk;
  528. if(fabs(pk/sum) < 2.0*GSL_DBL_EPSILON) break;
  529. }
  530. *result = a/(b-x) * sum;
  531. if(k == maxiter)
  532. GSL_ERROR ("error", GSL_EMAXITER);
  533. else
  534. return GSL_SUCCESS;
  535. }
  536. }
  537. /* 1F1(a+1,b,x)/1F1(a,b,x)
  538. *
  539. * I think this suffers from typical "anomalous convergence".
  540. * I could not find a region where it was truly useful.
  541. */
  542. #if 0
  543. static
  544. int
  545. hyperg_1F1_CF1(const double a, const double b, const double x, double * result)
  546. {
  547. const double RECUR_BIG = GSL_SQRT_DBL_MAX;
  548. const int maxiter = 5000;
  549. int n = 1;
  550. double Anm2 = 1.0;
  551. double Bnm2 = 0.0;
  552. double Anm1 = 0.0;
  553. double Bnm1 = 1.0;
  554. double a1 = b - a - 1.0;
  555. double b1 = b - x - 2.0*(a+1.0);
  556. double An = b1*Anm1 + a1*Anm2;
  557. double Bn = b1*Bnm1 + a1*Bnm2;
  558. double an, bn;
  559. double fn = An/Bn;
  560. while(n < maxiter) {
  561. double old_fn;
  562. double del;
  563. n++;
  564. Anm2 = Anm1;
  565. Bnm2 = Bnm1;
  566. Anm1 = An;
  567. Bnm1 = Bn;
  568. an = (a + n - 1.0) * (b - a - n);
  569. bn = b - x - 2.0*(a+n);
  570. An = bn*Anm1 + an*Anm2;
  571. Bn = bn*Bnm1 + an*Bnm2;
  572. if(fabs(An) > RECUR_BIG || fabs(Bn) > RECUR_BIG) {
  573. An /= RECUR_BIG;
  574. Bn /= RECUR_BIG;
  575. Anm1 /= RECUR_BIG;
  576. Bnm1 /= RECUR_BIG;
  577. Anm2 /= RECUR_BIG;
  578. Bnm2 /= RECUR_BIG;
  579. }
  580. old_fn = fn;
  581. fn = An/Bn;
  582. del = old_fn/fn;
  583. if(fabs(del - 1.0) < 10.0*GSL_DBL_EPSILON) break;
  584. }
  585. *result = fn;
  586. if(n == maxiter)
  587. GSL_ERROR ("error", GSL_EMAXITER);
  588. else
  589. return GSL_SUCCESS;
  590. }
  591. #endif /* 0 */
  592. /* 1F1(a,b+1,x)/1F1(a,b,x)
  593. *
  594. * This seemed to suffer from "anomalous convergence".
  595. * However, I have no theory for this recurrence.
  596. */
  597. #if 0
  598. static
  599. int
  600. hyperg_1F1_CF1_b(const double a, const double b, const double x, double * result)
  601. {
  602. const double RECUR_BIG = GSL_SQRT_DBL_MAX;
  603. const int maxiter = 5000;
  604. int n = 1;
  605. double Anm2 = 1.0;
  606. double Bnm2 = 0.0;
  607. double Anm1 = 0.0;
  608. double Bnm1 = 1.0;
  609. double a1 = b + 1.0;
  610. double b1 = (b + 1.0) * (b - x);
  611. double An = b1*Anm1 + a1*Anm2;
  612. double Bn = b1*Bnm1 + a1*Bnm2;
  613. double an, bn;
  614. double fn = An/Bn;
  615. while(n < maxiter) {
  616. double old_fn;
  617. double del;
  618. n++;
  619. Anm2 = Anm1;
  620. Bnm2 = Bnm1;
  621. Anm1 = An;
  622. Bnm1 = Bn;
  623. an = (b + n) * (b + n - 1.0 - a) * x;
  624. bn = (b + n) * (b + n - 1.0 - x);
  625. An = bn*Anm1 + an*Anm2;
  626. Bn = bn*Bnm1 + an*Bnm2;
  627. if(fabs(An) > RECUR_BIG || fabs(Bn) > RECUR_BIG) {
  628. An /= RECUR_BIG;
  629. Bn /= RECUR_BIG;
  630. Anm1 /= RECUR_BIG;
  631. Bnm1 /= RECUR_BIG;
  632. Anm2 /= RECUR_BIG;
  633. Bnm2 /= RECUR_BIG;
  634. }
  635. old_fn = fn;
  636. fn = An/Bn;
  637. del = old_fn/fn;
  638. if(fabs(del - 1.0) < 10.0*GSL_DBL_EPSILON) break;
  639. }
  640. *result = fn;
  641. if(n == maxiter)
  642. GSL_ERROR ("error", GSL_EMAXITER);
  643. else
  644. return GSL_SUCCESS;
  645. }
  646. #endif /* 0 */
  647. /* 1F1(a,b,x)
  648. * |a| <= 1, b > 0
  649. */
  650. static
  651. int
  652. hyperg_1F1_small_a_bgt0(const double a, const double b, const double x, gsl_sf_result * result)
  653. {
  654. const double bma = b-a;
  655. const double oma = 1.0-a;
  656. const double ap1mb = 1.0+a-b;
  657. const double abs_bma = fabs(bma);
  658. const double abs_oma = fabs(oma);
  659. const double abs_ap1mb = fabs(ap1mb);
  660. const double ax = fabs(x);
  661. if(a == 0.0) {
  662. result->val = 1.0;
  663. result->err = 0.0;
  664. return GSL_SUCCESS;
  665. }
  666. else if(a == 1.0 && b >= 1.0) {
  667. return hyperg_1F1_1(b, x, result);
  668. }
  669. else if(a == -1.0) {
  670. result->val = 1.0 + a/b * x;
  671. result->err = GSL_DBL_EPSILON * (1.0 + fabs(a/b * x));
  672. result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  673. return GSL_SUCCESS;
  674. }
  675. else if(b >= 1.4*ax) {
  676. return gsl_sf_hyperg_1F1_series_e(a, b, x, result);
  677. }
  678. else if(x > 0.0) {
  679. if(x > 100.0 && abs_bma*abs_oma < 0.5*x) {
  680. return hyperg_1F1_asymp_posx(a, b, x, result);
  681. }
  682. else if(b < 5.0e+06) {
  683. /* Recurse backward on b from
  684. * a suitably high point.
  685. */
  686. const double b_del = ceil(1.4*x-b) + 1.0;
  687. double bp = b + b_del;
  688. gsl_sf_result r_Mbp1;
  689. gsl_sf_result r_Mb;
  690. double Mbp1;
  691. double Mb;
  692. double Mbm1;
  693. int stat_0 = gsl_sf_hyperg_1F1_series_e(a, bp+1.0, x, &r_Mbp1);
  694. int stat_1 = gsl_sf_hyperg_1F1_series_e(a, bp, x, &r_Mb);
  695. const double err_rat = fabs(r_Mbp1.err/r_Mbp1.val) + fabs(r_Mb.err/r_Mb.val);
  696. Mbp1 = r_Mbp1.val;
  697. Mb = r_Mb.val;
  698. while(bp > b+0.1) {
  699. /* Do backward recursion. */
  700. Mbm1 = ((x+bp-1.0)*Mb - x*(bp-a)/bp*Mbp1)/(bp-1.0);
  701. bp -= 1.0;
  702. Mbp1 = Mb;
  703. Mb = Mbm1;
  704. }
  705. result->val = Mb;
  706. result->err = err_rat * (fabs(b_del)+1.0) * fabs(Mb);
  707. result->err += 2.0 * GSL_DBL_EPSILON * fabs(Mb);
  708. return GSL_ERROR_SELECT_2(stat_0, stat_1);
  709. }
  710. else if (fabs(x) < fabs(b) && fabs(a*x) < sqrt(fabs(b)) * fabs(b-x)) {
  711. return hyperg_1F1_largebx(a, b, x, result);
  712. } else {
  713. return hyperg_1F1_large2bm4a(a, b, x, result);
  714. }
  715. }
  716. else {
  717. /* x < 0 and b not large compared to |x|
  718. */
  719. if(ax < 10.0 && b < 10.0) {
  720. return gsl_sf_hyperg_1F1_series_e(a, b, x, result);
  721. }
  722. else if(ax >= 100.0 && GSL_MAX(abs_ap1mb,1.0) < 0.99*ax) {
  723. return hyperg_1F1_asymp_negx(a, b, x, result);
  724. }
  725. else {
  726. return hyperg_1F1_luke(a, b, x, result);
  727. }
  728. }
  729. }
  730. /* 1F1(b+eps,b,x)
  731. * |eps|<=1, b > 0
  732. */
  733. static
  734. int
  735. hyperg_1F1_beps_bgt0(const double eps, const double b, const double x, gsl_sf_result * result)
  736. {
  737. if(b > fabs(x) && fabs(eps) < GSL_SQRT_DBL_EPSILON) {
  738. /* If b-a is very small and x/b is not too large we can
  739. * use this explicit approximation.
  740. *
  741. * 1F1(b+eps,b,x) = exp(ax/b) (1 - eps x^2 (v2 + v3 x + ...) + ...)
  742. *
  743. * v2 = a/(2b^2(b+1))
  744. * v3 = a(b-2a)/(3b^3(b+1)(b+2))
  745. * ...
  746. *
  747. * See [Luke, Mathematical Functions and Their Approximations, p.292]
  748. *
  749. * This cannot be used for b near a negative integer or zero.
  750. * Also, if x/b is large the deviation from exp(x) behaviour grows.
  751. */
  752. double a = b + eps;
  753. gsl_sf_result exab;
  754. int stat_e = gsl_sf_exp_e(a*x/b, &exab);
  755. double v2 = a/(2.0*b*b*(b+1.0));
  756. double v3 = a*(b-2.0*a)/(3.0*b*b*b*(b+1.0)*(b+2.0));
  757. double v = v2 + v3 * x;
  758. double f = (1.0 - eps*x*x*v);
  759. result->val = exab.val * f;
  760. result->err = exab.err * fabs(f);
  761. result->err += fabs(exab.val) * GSL_DBL_EPSILON * (1.0 + fabs(eps*x*x*v));
  762. result->err += 4.0 * GSL_DBL_EPSILON * fabs(result->val);
  763. return stat_e;
  764. }
  765. else {
  766. /* Otherwise use a Kummer transformation to reduce
  767. * it to the small a case.
  768. */
  769. gsl_sf_result Kummer_1F1;
  770. int stat_K = hyperg_1F1_small_a_bgt0(-eps, b, -x, &Kummer_1F1);
  771. if(Kummer_1F1.val != 0.0) {
  772. int stat_e = gsl_sf_exp_mult_err_e(x, 2.0*GSL_DBL_EPSILON*fabs(x),
  773. Kummer_1F1.val, Kummer_1F1.err,
  774. result);
  775. return GSL_ERROR_SELECT_2(stat_e, stat_K);
  776. }
  777. else {
  778. result->val = 0.0;
  779. result->err = 0.0;
  780. return stat_K;
  781. }
  782. }
  783. }
  784. /* 1F1(a,2a,x) = Gamma(a + 1/2) E(x) (|x|/4)^(-a+1/2) scaled_I(a-1/2,|x|/2)
  785. *
  786. * E(x) = exp(x) x > 0
  787. * = 1 x < 0
  788. *
  789. * a >= 1/2
  790. */
  791. static
  792. int
  793. hyperg_1F1_beq2a_pos(const double a, const double x, gsl_sf_result * result)
  794. {
  795. if(x == 0.0) {
  796. result->val = 1.0;
  797. result->err = 0.0;
  798. return GSL_SUCCESS;
  799. }
  800. else {
  801. gsl_sf_result I;
  802. int stat_I = gsl_sf_bessel_Inu_scaled_e(a-0.5, 0.5*fabs(x), &I);
  803. gsl_sf_result lg;
  804. int stat_g = gsl_sf_lngamma_e(a + 0.5, &lg);
  805. double ln_term = (0.5-a)*log(0.25*fabs(x));
  806. double lnpre_val = lg.val + GSL_MAX_DBL(x,0.0) + ln_term;
  807. double lnpre_err = lg.err + GSL_DBL_EPSILON * (fabs(ln_term) + fabs(x));
  808. int stat_e = gsl_sf_exp_mult_err_e(lnpre_val, lnpre_err,
  809. I.val, I.err,
  810. result);
  811. return GSL_ERROR_SELECT_3(stat_e, stat_g, stat_I);
  812. }
  813. }
  814. /* Determine middle parts of diagonal recursion along b=2a
  815. * from two endpoints, i.e.
  816. *
  817. * given: M(a,b) and M(a+1,b+2)
  818. * get: M(a+1,b+1) and M(a,b+1)
  819. */
  820. #if 0
  821. inline
  822. static
  823. int
  824. hyperg_1F1_diag_step(const double a, const double b, const double x,
  825. const double Mab, const double Map1bp2,
  826. double * Map1bp1, double * Mabp1)
  827. {
  828. if(a == b) {
  829. *Map1bp1 = Mab;
  830. *Mabp1 = Mab - x/(b+1.0) * Map1bp2;
  831. }
  832. else {
  833. *Map1bp1 = Mab - x * (a-b)/(b*(b+1.0)) * Map1bp2;
  834. *Mabp1 = (a * *Map1bp1 - b * Mab)/(a-b);
  835. }
  836. return GSL_SUCCESS;
  837. }
  838. #endif /* 0 */
  839. /* Determine endpoint of diagonal recursion.
  840. *
  841. * given: M(a,b) and M(a+1,b+2)
  842. * get: M(a+1,b) and M(a+1,b+1)
  843. */
  844. #if 0
  845. inline
  846. static
  847. int
  848. hyperg_1F1_diag_end_step(const double a, const double b, const double x,
  849. const double Mab, const double Map1bp2,
  850. double * Map1b, double * Map1bp1)
  851. {
  852. *Map1bp1 = Mab - x * (a-b)/(b*(b+1.0)) * Map1bp2;
  853. *Map1b = Mab + x/b * *Map1bp1;
  854. return GSL_SUCCESS;
  855. }
  856. #endif /* 0 */
  857. /* Handle the case of a and b both positive integers.
  858. * Assumes a > 0 and b > 0.
  859. */
  860. static
  861. int
  862. hyperg_1F1_ab_posint(const int a, const int b, const double x, gsl_sf_result * result)
  863. {
  864. double ax = fabs(x);
  865. if(a == b) {
  866. return gsl_sf_exp_e(x, result); /* 1F1(a,a,x) */
  867. }
  868. else if(a == 1) {
  869. return gsl_sf_exprel_n_e(b-1, x, result); /* 1F1(1,b,x) */
  870. }
  871. else if(b == a + 1) {
  872. gsl_sf_result K;
  873. int stat_K = gsl_sf_exprel_n_e(a, -x, &K); /* 1F1(1,1+a,-x) */
  874. int stat_e = gsl_sf_exp_mult_err_e(x, 2.0 * GSL_DBL_EPSILON * fabs(x),
  875. K.val, K.err,
  876. result);
  877. return GSL_ERROR_SELECT_2(stat_e, stat_K);
  878. }
  879. else if(a == b + 1) {
  880. gsl_sf_result ex;
  881. int stat_e = gsl_sf_exp_e(x, &ex);
  882. result->val = ex.val * (1.0 + x/b);
  883. result->err = ex.err * (1.0 + x/b);
  884. result->err += ex.val * GSL_DBL_EPSILON * (1.0 + fabs(x/b));
  885. result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  886. return stat_e;
  887. }
  888. else if(a == b + 2) {
  889. gsl_sf_result ex;
  890. int stat_e = gsl_sf_exp_e(x, &ex);
  891. double poly = (1.0 + x/b*(2.0 + x/(b+1.0)));
  892. result->val = ex.val * poly;
  893. result->err = ex.err * fabs(poly);
  894. result->err += ex.val * GSL_DBL_EPSILON * (1.0 + fabs(x/b) * (2.0 + fabs(x/(b+1.0))));
  895. result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  896. return stat_e;
  897. }
  898. else if(b == 2*a) {
  899. return hyperg_1F1_beq2a_pos(a, x, result); /* 1F1(a,2a,x) */
  900. }
  901. else if( ( b < 10 && a < 10 && ax < 5.0 )
  902. || ( b > a*ax )
  903. || ( b > a && ax < 5.0 )
  904. ) {
  905. return gsl_sf_hyperg_1F1_series_e(a, b, x, result);
  906. }
  907. else if(b > a && b >= 2*a + x) {
  908. /* Use the Gautschi CF series, then
  909. * recurse backward to a=0 for normalization.
  910. * This will work for either sign of x.
  911. */
  912. double rap;
  913. int stat_CF1 = hyperg_1F1_CF1_p_ser(a, b, x, &rap);
  914. double ra = 1.0 + x/a * rap;
  915. double Ma = GSL_SQRT_DBL_MIN;
  916. double Map1 = ra * Ma;
  917. double Mnp1 = Map1;
  918. double Mn = Ma;
  919. double Mnm1;
  920. int n;
  921. for(n=a; n>0; n--) {
  922. Mnm1 = (n * Mnp1 - (2*n-b+x) * Mn) / (b-n);
  923. Mnp1 = Mn;
  924. Mn = Mnm1;
  925. }
  926. result->val = Ma/Mn;
  927. result->err = 2.0 * GSL_DBL_EPSILON * (fabs(a) + 1.0) * fabs(Ma/Mn);
  928. return stat_CF1;
  929. }
  930. else if(b > a && b < 2*a + x && b > x) {
  931. /* Use the Gautschi series representation of
  932. * the continued fraction. Then recurse forward
  933. * to the a=b line for normalization. This will
  934. * work for either sign of x, although we do need
  935. * to check for b > x, for when x is positive.
  936. */
  937. double rap;
  938. int stat_CF1 = hyperg_1F1_CF1_p_ser(a, b, x, &rap);
  939. double ra = 1.0 + x/a * rap;
  940. gsl_sf_result ex;
  941. int stat_ex;
  942. double Ma = GSL_SQRT_DBL_MIN;
  943. double Map1 = ra * Ma;
  944. double Mnm1 = Ma;
  945. double Mn = Map1;
  946. double Mnp1;
  947. int n;
  948. for(n=a+1; n<b; n++) {
  949. Mnp1 = ((b-n)*Mnm1 + (2*n-b+x)*Mn)/n;
  950. Mnm1 = Mn;
  951. Mn = Mnp1;
  952. }
  953. stat_ex = gsl_sf_exp_e(x, &ex); /* 1F1(b,b,x) */
  954. result->val = ex.val * Ma/Mn;
  955. result->err = ex.err * fabs(Ma/Mn);
  956. result->err += 4.0 * GSL_DBL_EPSILON * (fabs(b-a)+1.0) * fabs(result->val);
  957. return GSL_ERROR_SELECT_2(stat_ex, stat_CF1);
  958. }
  959. else if(x >= 0.0) {
  960. if(b < a) {
  961. /* The point b,b is below the b=2a+x line.
  962. * Forward recursion on a from b,b+1 is possible.
  963. * Note that a > b + 1 as well, since we already tried a = b + 1.
  964. */
  965. if(x + log(fabs(x/b)) < GSL_LOG_DBL_MAX-2.0) {
  966. double ex = exp(x);
  967. int n;
  968. double Mnm1 = ex; /* 1F1(b,b,x) */
  969. double Mn = ex * (1.0 + x/b); /* 1F1(b+1,b,x) */
  970. double Mnp1;
  971. for(n=b+1; n<a; n++) {
  972. Mnp1 = ((b-n)*Mnm1 + (2*n-b+x)*Mn)/n;
  973. Mnm1 = Mn;
  974. Mn = Mnp1;
  975. }
  976. result->val = Mn;
  977. result->err = (x + 1.0) * GSL_DBL_EPSILON * fabs(Mn);
  978. result->err *= fabs(a-b)+1.0;
  979. return GSL_SUCCESS;
  980. }
  981. else {
  982. OVERFLOW_ERROR(result);
  983. }
  984. }
  985. else {
  986. /* b > a
  987. * b < 2a + x
  988. * b <= x (otherwise we would have finished above)
  989. *
  990. * Gautschi anomalous convergence region. However, we can
  991. * recurse forward all the way from a=0,1 because we are
  992. * always underneath the b=2a+x line.
  993. */
  994. gsl_sf_result r_Mn;
  995. double Mnm1 = 1.0; /* 1F1(0,b,x) */
  996. double Mn; /* 1F1(1,b,x) */
  997. double Mnp1;
  998. int n;
  999. gsl_sf_exprel_n_e(b-1, x, &r_Mn);
  1000. Mn = r_Mn.val;
  1001. for(n=1; n<a; n++) {
  1002. Mnp1 = ((b-n)*Mnm1 + (2*n-b+x)*Mn)/n;
  1003. Mnm1 = Mn;
  1004. Mn = Mnp1;
  1005. }
  1006. result->val = Mn;
  1007. result->err = fabs(Mn) * (1.0 + fabs(a)) * fabs(r_Mn.err / r_Mn.val);
  1008. result->err += 2.0 * GSL_DBL_EPSILON * fabs(Mn);
  1009. return GSL_SUCCESS;
  1010. }
  1011. }
  1012. else {
  1013. /* x < 0
  1014. * b < a (otherwise we would have tripped one of the above)
  1015. */
  1016. if(a <= 0.5*(b-x) || a >= -x) {
  1017. /* Gautschi continued fraction is in the anomalous region,
  1018. * so we must find another way. We recurse down in b,
  1019. * from the a=b line.
  1020. */
  1021. double ex = exp(x);
  1022. double Manp1 = ex;
  1023. double Man = ex * (1.0 + x/(a-1.0));
  1024. double Manm1;
  1025. int n;
  1026. for(n=a-1; n>b; n--) {
  1027. Manm1 = (-n*(1-n-x)*Man - x*(n-a)*Manp1)/(n*(n-1.0));
  1028. Manp1 = Man;
  1029. Man = Manm1;
  1030. }
  1031. result->val = Man;
  1032. result->err = (fabs(x) + 1.0) * GSL_DBL_EPSILON * fabs(Man);
  1033. result->err *= fabs(b-a)+1.0;
  1034. return GSL_SUCCESS;
  1035. }
  1036. else {
  1037. /* Pick a0 such that b ~= 2a0 + x, then
  1038. * recurse down in b from a0,a0 to determine
  1039. * the values near the line b=2a+x. Then recurse
  1040. * forward on a from a0.
  1041. */
  1042. int a0 = ceil(0.5*(b-x));
  1043. double Ma0b; /* M(a0,b) */
  1044. double Ma0bp1; /* M(a0,b+1) */
  1045. double Ma0p1b; /* M(a0+1,b) */
  1046. double Mnm1;
  1047. double Mn;
  1048. double Mnp1;
  1049. int n;
  1050. {
  1051. double ex = exp(x);
  1052. double Ma0np1 = ex;
  1053. double Ma0n = ex * (1.0 + x/(a0-1.0));
  1054. double Ma0nm1;
  1055. for(n=a0-1; n>b; n--) {
  1056. Ma0nm1 = (-n*(1-n-x)*Ma0n - x*(n-a0)*Ma0np1)/(n*(n-1.0));
  1057. Ma0np1 = Ma0n;
  1058. Ma0n = Ma0nm1;
  1059. }
  1060. Ma0bp1 = Ma0np1;
  1061. Ma0b = Ma0n;
  1062. Ma0p1b = (b*(a0+x)*Ma0b + x*(a0-b)*Ma0bp1)/(a0*b);
  1063. }
  1064. /* Initialise the recurrence correctly BJG */
  1065. if (a0 >= a)
  1066. {
  1067. Mn = Ma0b;
  1068. }
  1069. else if (a0 + 1>= a)
  1070. {
  1071. Mn = Ma0p1b;
  1072. }
  1073. else
  1074. {
  1075. Mnm1 = Ma0b;
  1076. Mn = Ma0p1b;
  1077. for(n=a0+1; n<a; n++) {
  1078. Mnp1 = ((b-n)*Mnm1 + (2*n-b+x)*Mn)/n;
  1079. Mnm1 = Mn;
  1080. Mn = Mnp1;
  1081. }
  1082. }
  1083. result->val = Mn;
  1084. result->err = (fabs(x) + 1.0) * GSL_DBL_EPSILON * fabs(Mn);
  1085. result->err *= fabs(b-a)+1.0;
  1086. return GSL_SUCCESS;
  1087. }
  1088. }
  1089. }
  1090. /* Evaluate a <= 0, a integer, cases directly. (Polynomial; Horner)
  1091. * When the terms are all positive, this
  1092. * must work. We will assume this here.
  1093. */
  1094. static
  1095. int
  1096. hyperg_1F1_a_negint_poly(const int a, const double b, const double x, gsl_sf_result * result)
  1097. {
  1098. if(a == 0) {
  1099. result->val = 1.0;
  1100. result->err = 1.0;
  1101. return GSL_SUCCESS;
  1102. }
  1103. else {
  1104. int N = -a;
  1105. double poly = 1.0;
  1106. int k;
  1107. for(k=N-1; k>=0; k--) {
  1108. double t = (a+k)/(b+k) * (x/(k+1));
  1109. double r = t + 1.0/poly;
  1110. if(r > 0.9*GSL_DBL_MAX/poly) {
  1111. OVERFLOW_ERROR(result);
  1112. }
  1113. else {
  1114. poly *= r; /* P_n = 1 + t_n P_{n-1} */
  1115. }
  1116. }
  1117. result->val = poly;
  1118. result->err = 2.0 * (sqrt(N) + 1.0) * GSL_DBL_EPSILON * fabs(poly);
  1119. return GSL_SUCCESS;
  1120. }
  1121. }
  1122. /* Evaluate negative integer a case by relation
  1123. * to Laguerre polynomials. This is more general than
  1124. * the direct polynomial evaluation, but is safe
  1125. * for all values of x.
  1126. *
  1127. * 1F1(-n,b,x) = n!/(b)_n Laguerre[n,b-1,x]
  1128. * = n B(b,n) Laguerre[n,b-1,x]
  1129. *
  1130. * assumes b is not a negative integer
  1131. */
  1132. static
  1133. int
  1134. hyperg_1F1_a_negint_lag(const int a, const double b, const double x, gsl_sf_result * result)
  1135. {
  1136. const int n = -a;
  1137. gsl_sf_result lag;
  1138. const int stat_l = gsl_sf_laguerre_n_e(n, b-1.0, x, &lag);
  1139. if(b < 0.0) {
  1140. gsl_sf_result lnfact;
  1141. gsl_sf_result lng1;
  1142. gsl_sf_result lng2;
  1143. double s1, s2;
  1144. const int stat_f = gsl_sf_lnfact_e(n, &lnfact);
  1145. const int stat_g1 = gsl_sf_lngamma_sgn_e(b + n, &lng1, &s1);
  1146. const int stat_g2 = gsl_sf_lngamma_sgn_e(b, &lng2, &s2);
  1147. const double lnpre_val = lnfact.val - (lng1.val - lng2.val);
  1148. const double lnpre_err = lnfact.err + lng1.err + lng2.err
  1149. + 2.0 * GSL_DBL_EPSILON * fabs(lnpre_val);
  1150. const int stat_e = gsl_sf_exp_mult_err_e(lnpre_val, lnpre_err,
  1151. s1*s2*lag.val, lag.err,
  1152. result);
  1153. return GSL_ERROR_SELECT_5(stat_e, stat_l, stat_g1, stat_g2, stat_f);
  1154. }
  1155. else {
  1156. gsl_sf_result lnbeta;
  1157. gsl_sf_lnbeta_e(b, n, &lnbeta);
  1158. if(fabs(lnbeta.val) < 0.1) {
  1159. /* As we have noted, when B(x,y) is near 1,
  1160. * evaluating log(B(x,y)) is not accurate.
  1161. * Instead we evaluate B(x,y) directly.
  1162. */
  1163. const double ln_term_val = log(1.25*n);
  1164. const double ln_term_err = 2.0 * GSL_DBL_EPSILON * ln_term_val;
  1165. gsl_sf_result beta;
  1166. int stat_b = gsl_sf_beta_e(b, n, &beta);
  1167. int stat_e = gsl_sf_exp_mult_err_e(ln_term_val, ln_term_err,
  1168. lag.val, lag.err,
  1169. result);
  1170. result->val *= beta.val/1.25;
  1171. result->err *= beta.val/1.25;
  1172. return GSL_ERROR_SELECT_3(stat_e, stat_l, stat_b);
  1173. }
  1174. else {
  1175. /* B(x,y) was not near 1, so it is safe to use
  1176. * the logarithmic values.
  1177. */
  1178. const double ln_n = log(n);
  1179. const double ln_term_val = lnbeta.val + ln_n;
  1180. const double ln_term_err = lnbeta.err + 2.0 * GSL_DBL_EPSILON * fabs(ln_n);
  1181. int stat_e = gsl_sf_exp_mult_err_e(ln_term_val, ln_term_err,
  1182. lag.val, lag.err,
  1183. result);
  1184. return GSL_ERROR_SELECT_2(stat_e, stat_l);
  1185. }
  1186. }
  1187. }
  1188. /* Handle negative integer a case for x > 0 and
  1189. * generic b.
  1190. *
  1191. * Combine [Abramowitz+Stegun, 13.6.9 + 13.6.27]
  1192. * M(-n,b,x) = (-1)^n / (b)_n U(-n,b,x) = n! / (b)_n Laguerre^(b-1)_n(x)
  1193. */
  1194. #if 0
  1195. static
  1196. int
  1197. hyperg_1F1_a_negint_U(const int a, const double b, const double x, gsl_sf_result * result)
  1198. {
  1199. const int n = -a;
  1200. const double sgn = ( GSL_IS_ODD(n) ? -1.0 : 1.0 );
  1201. double sgpoch;
  1202. gsl_sf_result lnpoch;
  1203. gsl_sf_result U;
  1204. const int stat_p = gsl_sf_lnpoch_sgn_e(b, n, &lnpoch, &sgpoch);
  1205. const int stat_U = gsl_sf_hyperg_U_e(-n, b, x, &U);
  1206. const int stat_e = gsl_sf_exp_mult_err_e(-lnpoch.val, lnpoch.err,
  1207. sgn * sgpoch * U.val, U.err,
  1208. result);
  1209. return GSL_ERROR_SELECT_3(stat_e, stat_U, stat_p);
  1210. }
  1211. #endif
  1212. /* Assumes a <= -1, b <= -1, and b <= a.
  1213. */
  1214. static
  1215. int
  1216. hyperg_1F1_ab_negint(const int a, const int b, const double x, gsl_sf_result * result)
  1217. {
  1218. if(x == 0.0) {
  1219. result->val = 1.0;
  1220. result->err = 0.0;
  1221. return GSL_SUCCESS;
  1222. }
  1223. else if(x > 0.0) {
  1224. return hyperg_1F1_a_negint_poly(a, b, x, result);
  1225. }
  1226. else {
  1227. /* Apply a Kummer transformation to make x > 0 so
  1228. * we can evaluate the polynomial safely. Of course,
  1229. * this assumes b <= a, which must be true for
  1230. * a<0 and b<0, since otherwise the thing is undefined.
  1231. */
  1232. gsl_sf_result K;
  1233. int stat_K = hyperg_1F1_a_negint_poly(b-a, b, -x, &K);
  1234. int stat_e = gsl_sf_exp_mult_err_e(x, 2.0 * GSL_DBL_EPSILON * fabs(x),
  1235. K.val, K.err,
  1236. result);
  1237. return GSL_ERROR_SELECT_2(stat_e, stat_K);
  1238. }
  1239. }
  1240. /* [Abramowitz+Stegun, 13.1.3]
  1241. *
  1242. * M(a,b,x) = Gamma(1+a-b)/Gamma(2-b) x^(1-b) *
  1243. * { Gamma(b)/Gamma(a) M(1+a-b,2-b,x) - (b-1) U(1+a-b,2-b,x) }
  1244. *
  1245. * b not an integer >= 2
  1246. * a-b not a negative integer
  1247. */
  1248. static
  1249. int
  1250. hyperg_1F1_U(const double a, const double b, const double x, gsl_sf_result * result)
  1251. {
  1252. const double bp = 2.0 - b;
  1253. const double ap = a - b + 1.0;
  1254. gsl_sf_result lg_ap, lg_bp;
  1255. double sg_ap;
  1256. int stat_lg0 = gsl_sf_lngamma_sgn_e(ap, &lg_ap, &sg_ap);
  1257. int stat_lg1 = gsl_sf_lngamma_e(bp, &lg_bp);
  1258. int stat_lg2 = GSL_ERROR_SELECT_2(stat_lg0, stat_lg1);
  1259. double t1 = (bp-1.0) * log(x);
  1260. double lnpre_val = lg_ap.val - lg_bp.val + t1;
  1261. double lnpre_err = lg_ap.err + lg_bp.err + 2.0 * GSL_DBL_EPSILON * fabs(t1);
  1262. gsl_sf_result lg_2mbp, lg_1papmbp;
  1263. double sg_2mbp, sg_1papmbp;
  1264. int stat_lg3 = gsl_sf_lngamma_sgn_e(2.0-bp, &lg_2mbp, &sg_2mbp);
  1265. int stat_lg4 = gsl_sf_lngamma_sgn_e(1.0+ap-bp, &lg_1papmbp, &sg_1papmbp);
  1266. int stat_lg5 = GSL_ERROR_SELECT_2(stat_lg3, stat_lg4);
  1267. double lnc1_val = lg_2mbp.val - lg_1papmbp.val;
  1268. double lnc1_err = lg_2mbp.err + lg_1papmbp.err
  1269. + GSL_DBL_EPSILON * (fabs(lg_2mbp.val) + fabs(lg_1papmbp.val));
  1270. gsl_sf_result M;
  1271. gsl_sf_result_e10 U;
  1272. int stat_F = gsl_sf_hyperg_1F1_e(ap, bp, x, &M);
  1273. int stat_U = gsl_sf_hyperg_U_e10_e(ap, bp, x, &U);
  1274. int stat_FU = GSL_ERROR_SELECT_2(stat_F, stat_U);
  1275. gsl_sf_result_e10 term_M;
  1276. int stat_e0 = gsl_sf_exp_mult_err_e10_e(lnc1_val, lnc1_err,
  1277. sg_2mbp*sg_1papmbp*M.val, M.err,
  1278. &term_M);
  1279. const double ombp = 1.0 - bp;
  1280. const double Uee_val = U.e10*M_LN10;
  1281. const double Uee_err = 2.0 * GSL_DBL_EPSILON * fabs(Uee_val);
  1282. const double Mee_val = term_M.e10*M_LN10;
  1283. const double Mee_err = 2.0 * GSL_DBL_EPSILON * fabs(Mee_val);
  1284. int stat_e1;
  1285. /* Do a little dance with the exponential prefactors
  1286. * to avoid overflows in intermediate results.
  1287. */
  1288. if(Uee_val > Mee_val) {
  1289. const double factorM_val = exp(Mee_val-Uee_val);
  1290. const double factorM_err = 2.0 * GSL_DBL_EPSILON * (fabs(Mee_val-Uee_val)+1.0) * factorM_val;
  1291. const double inner_val = term_M.val*factorM_val - ombp*U.val;
  1292. const double inner_err =
  1293. term_M.err*factorM_val + fabs(ombp) * U.err
  1294. + fabs(term_M.val) * factorM_err
  1295. + GSL_DBL_EPSILON * (fabs(term_M.val*factorM_val) + fabs(ombp*U.val));
  1296. stat_e1 = gsl_sf_exp_mult_err_e(lnpre_val+Uee_val, lnpre_err+Uee_err,
  1297. sg_ap*inner_val, inner_err,
  1298. result);
  1299. }
  1300. else {
  1301. const double factorU_val = exp(Uee_val - Mee_val);
  1302. const double factorU_err = 2.0 * GSL_DBL_EPSILON * (fabs(Mee_val-Uee_val)+1.0) * factorU_val;
  1303. const double inner_val = term_M.val - ombp*factorU_val*U.val;
  1304. const double inner_err =
  1305. term_M.err + fabs(ombp*factorU_val*U.err)
  1306. + fabs(ombp*factorU_err*U.val)
  1307. + GSL_DBL_EPSILON * (fabs(term_M.val) + fabs(ombp*factorU_val*U.val));
  1308. stat_e1 = gsl_sf_exp_mult_err_e(lnpre_val+Mee_val, lnpre_err+Mee_err,
  1309. sg_ap*inner_val, inner_err,
  1310. result);
  1311. }
  1312. return GSL_ERROR_SELECT_5(stat_e1, stat_e0, stat_FU, stat_lg5, stat_lg2);
  1313. }
  1314. /* Handle case of generic positive a, b.
  1315. * Assumes b-a is not a negative integer.
  1316. */
  1317. static
  1318. int
  1319. hyperg_1F1_ab_pos(const double a, const double b,
  1320. const double x,
  1321. gsl_sf_result * result)
  1322. {
  1323. const double ax = fabs(x);
  1324. if( ( b < 10.0 && a < 10.0 && ax < 5.0 )
  1325. || ( b > a*ax )
  1326. || ( b > a && ax < 5.0 )
  1327. ) {
  1328. return gsl_sf_hyperg_1F1_series_e(a, b, x, result);
  1329. }
  1330. else if( x < -100.0
  1331. && GSL_MAX_DBL(fabs(a),1.0)*GSL_MAX_DBL(fabs(1.0+a-b),1.0) < 0.7*fabs(x)
  1332. ) {
  1333. /* Large negative x asymptotic.
  1334. */
  1335. return hyperg_1F1_asymp_negx(a, b, x, result);
  1336. }
  1337. else if( x > 100.0
  1338. && GSL_MAX_DBL(fabs(b-a),1.0)*GSL_MAX_DBL(fabs(1.0-a),1.0) < 0.7*fabs(x)
  1339. ) {
  1340. /* Large positive x asymptotic.
  1341. */
  1342. return hyperg_1F1_asymp_posx(a, b, x, result);
  1343. }
  1344. else if(fabs(b-a) <= 1.0) {
  1345. /* Directly handle b near a.
  1346. */
  1347. return hyperg_1F1_beps_bgt0(a-b, b, x, result); /* a = b + eps */
  1348. }
  1349. else if(b > a && b >= 2*a + x) {
  1350. /* Use the Gautschi CF series, then
  1351. * recurse backward to a near 0 for normalization.
  1352. * This will work for either sign of x.
  1353. */
  1354. double rap;
  1355. int stat_CF1 = hyperg_1F1_CF1_p_ser(a, b, x, &rap);
  1356. double ra = 1.0 + x/a * rap;
  1357. double Ma = GSL_SQRT_DBL_MIN;
  1358. double Map1 = ra * Ma;
  1359. double Mnp1 = Map1;
  1360. double Mn = Ma;
  1361. double Mnm1;
  1362. gsl_sf_result Mn_true;
  1363. int stat_Mt;
  1364. double n;
  1365. for(n=a; n>0.5; n -= 1.0) {
  1366. Mnm1 = (n * Mnp1 - (2.0*n-b+x) * Mn) / (b-n);
  1367. Mnp1 = Mn;
  1368. Mn = Mnm1;
  1369. }
  1370. stat_Mt = hyperg_1F1_small_a_bgt0(n, b, x, &Mn_true);
  1371. result->val = (Ma/Mn) * Mn_true.val;
  1372. result->err = fabs(Ma/Mn) * Mn_true.err;
  1373. result->err += 2.0 * GSL_DBL_EPSILON * (fabs(a)+1.0) * fabs(result->val);
  1374. return GSL_ERROR_SELECT_2(stat_Mt, stat_CF1);
  1375. }
  1376. else if(b > a && b < 2*a + x && b > x) {
  1377. /* Use the Gautschi series representation of
  1378. * the continued fraction. Then recurse forward
  1379. * to near the a=b line for normalization. This will
  1380. * work for either sign of x, although we do need
  1381. * to check for b > x, which is relevant when x is positive.
  1382. */
  1383. gsl_sf_result Mn_true;
  1384. int stat_Mt;
  1385. double rap;
  1386. int stat_CF1 = hyperg_1F1_CF1_p_ser(a, b, x, &rap);
  1387. double ra = 1.0 + x/a * rap;
  1388. double Ma = GSL_SQRT_DBL_MIN;
  1389. double Mnm1 = Ma;
  1390. double Mn = ra * Mnm1;
  1391. double Mnp1;
  1392. double n;
  1393. for(n=a+1.0; n<b-0.5; n += 1.0) {
  1394. Mnp1 = ((b-n)*Mnm1 + (2*n-b+x)*Mn)/n;
  1395. Mnm1 = Mn;
  1396. Mn = Mnp1;
  1397. }
  1398. stat_Mt = hyperg_1F1_beps_bgt0(n-b, b, x, &Mn_true);
  1399. result->val = Ma/Mn * Mn_true.val;
  1400. result->err = fabs(Ma/Mn) * Mn_true.err;
  1401. result->err += 2.0 * GSL_DBL_EPSILON * (fabs(b-a)+1.0) * fabs(result->val);
  1402. return GSL_ERROR_SELECT_2(stat_Mt, stat_CF1);
  1403. }
  1404. else if(x >= 0.0) {
  1405. if(b < a) {
  1406. /* Forward recursion on a from a=b+eps-1,b+eps.
  1407. */
  1408. double N = floor(a-b);
  1409. double eps = a - b - N;
  1410. gsl_sf_result r_M0;
  1411. gsl_sf_result r_M1;
  1412. int stat_0 = hyperg_1F1_beps_bgt0(eps-1.0, b, x, &r_M0);
  1413. int stat_1 = hyperg_1F1_beps_bgt0(eps, b, x, &r_M1);
  1414. double M0 = r_M0.val;
  1415. double M1 = r_M1.val;
  1416. double Mam1 = M0;
  1417. double Ma = M1;
  1418. double Map1;
  1419. double ap;
  1420. double start_pair = fabs(M0) + fabs(M1);
  1421. double minim_pair = GSL_DBL_MAX;
  1422. double pair_ratio;
  1423. double rat_0 = fabs(r_M0.err/r_M0.val);
  1424. double rat_1 = fabs(r_M1.err/r_M1.val);
  1425. for(ap=b+eps; ap<a-0.1; ap += 1.0) {
  1426. Map1 = ((b-ap)*Mam1 + (2.0*ap-b+x)*Ma)/ap;
  1427. Mam1 = Ma;
  1428. Ma = Map1;
  1429. minim_pair = GSL_MIN_DBL(fabs(Mam1) + fabs(Ma), minim_pair);
  1430. }
  1431. pair_ratio = start_pair/minim_pair;
  1432. result->val = Ma;
  1433. result->err = 2.0 * (rat_0 + rat_1 + GSL_DBL_EPSILON) * (fabs(b-a)+1.0) * fabs(Ma);
  1434. result->err += 2.0 * (rat_0 + rat_1) * pair_ratio*pair_ratio * fabs(Ma);
  1435. result->err += 2.0 * GSL_DBL_EPSILON * fabs(Ma);
  1436. return GSL_ERROR_SELECT_2(stat_0, stat_1);
  1437. }
  1438. else {
  1439. /* b > a
  1440. * b < 2a + x
  1441. * b <= x
  1442. *
  1443. * Recurse forward on a from a=eps,eps+1.
  1444. */
  1445. double eps = a - floor(a);
  1446. gsl_sf_result r_Mnm1;
  1447. gsl_sf_result r_Mn;
  1448. int stat_0 = hyperg_1F1_small_a_bgt0(eps, b, x, &r_Mnm1);
  1449. int stat_1 = hyperg_1F1_small_a_bgt0(eps+1.0, b, x, &r_Mn);
  1450. double Mnm1 = r_Mnm1.val;
  1451. double Mn = r_Mn.val;
  1452. double Mnp1;
  1453. double n;
  1454. double start_pair = fabs(Mn) + fabs(Mnm1);
  1455. double minim_pair = GSL_DBL_MAX;
  1456. double pair_ratio;
  1457. double rat_0 = fabs(r_Mnm1.err/r_Mnm1.val);
  1458. double rat_1 = fabs(r_Mn.err/r_Mn.val);
  1459. for(n=eps+1.0; n<a-0.1; n++) {
  1460. Mnp1 = ((b-n)*Mnm1 + (2*n-b+x)*Mn)/n;
  1461. Mnm1 = Mn;
  1462. Mn = Mnp1;
  1463. minim_pair = GSL_MIN_DBL(fabs(Mn) + fabs(Mnm1), minim_pair);
  1464. }
  1465. pair_ratio = start_pair/minim_pair;
  1466. result->val = Mn;
  1467. result->err = 2.0 * (rat_0 + rat_1 + GSL_DBL_EPSILON) * (fabs(a)+1.0) * fabs(Mn);
  1468. result->err += 2.0 * (rat_0 + rat_1) * pair_ratio*pair_ratio * fabs(Mn);
  1469. result->err += 2.0 * GSL_DBL_EPSILON * fabs(Mn);
  1470. return GSL_ERROR_SELECT_2(stat_0, stat_1);
  1471. }
  1472. }
  1473. else {
  1474. /* x < 0
  1475. * b < a
  1476. */
  1477. if(a <= 0.5*(b-x) || a >= -x) {
  1478. /* Recurse down in b, from near the a=b line, b=a+eps,a+eps-1.
  1479. */
  1480. double N = floor(a - b);
  1481. double eps = 1.0 + N - a + b;
  1482. gsl_sf_result r_Manp1;
  1483. gsl_sf_result r_Man;
  1484. int stat_0 = hyperg_1F1_beps_bgt0(-eps, a+eps, x, &r_Manp1);
  1485. int stat_1 = hyperg_1F1_beps_bgt0(1.0-eps, a+eps-1.0, x, &r_Man);
  1486. double Manp1 = r_Manp1.val;
  1487. double Man = r_Man.val;
  1488. double Manm1;
  1489. double n;
  1490. double start_pair = fabs(Manp1) + fabs(Man);
  1491. double minim_pair = GSL_DBL_MAX;
  1492. double pair_ratio;
  1493. double rat_0 = fabs(r_Manp1.err/r_Manp1.val);
  1494. double rat_1 = fabs(r_Man.err/r_Man.val);
  1495. for(n=a+eps-1.0; n>b+0.1; n -= 1.0) {
  1496. Manm1 = (-n*(1-n-x)*Man - x*(n-a)*Manp1)/(n*(n-1.0));
  1497. Manp1 = Man;
  1498. Man = Manm1;
  1499. minim_pair = GSL_MIN_DBL(fabs(Manp1) + fabs(Man), minim_pair);
  1500. }
  1501. /* FIXME: this is a nasty little hack; there is some
  1502. (transient?) instability in this recurrence for some
  1503. values. I can tell when it happens, which is when
  1504. this pair_ratio is large. But I do not know how to
  1505. measure the error in terms of it. I guessed quadratic
  1506. below, but it is probably worse than that.
  1507. */
  1508. pair_ratio = start_pair/minim_pair;
  1509. result->val = Man;
  1510. result->err = 2.0 * (rat_0 + rat_1 + GSL_DBL_EPSILON) * (fabs(b-a)+1.0) * fabs(Man);
  1511. result->err *= pair_ratio*pair_ratio + 1.0;
  1512. return GSL_ERROR_SELECT_2(stat_0, stat_1);
  1513. }
  1514. else {
  1515. /* Pick a0 such that b ~= 2a0 + x, then
  1516. * recurse down in b from a0,a0 to determine
  1517. * the values near the line b=2a+x. Then recurse
  1518. * forward on a from a0.
  1519. */
  1520. double epsa = a - floor(a);
  1521. double a0 = floor(0.5*(b-x)) + epsa;
  1522. double N = floor(a0 - b);
  1523. double epsb = 1.0 + N - a0 + b;
  1524. double Ma0b;
  1525. double Ma0bp1;
  1526. double Ma0p1b;
  1527. int stat_a0;
  1528. double Mnm1;
  1529. double Mn;
  1530. double Mnp1;
  1531. double n;
  1532. double err_rat;
  1533. {
  1534. gsl_sf_result r_Ma0np1;
  1535. gsl_sf_result r_Ma0n;
  1536. int stat_0 = hyperg_1F1_beps_bgt0(-epsb, a0+epsb, x, &r_Ma0np1);
  1537. int stat_1 = hyperg_1F1_beps_bgt0(1.0-epsb, a0+epsb-1.0, x, &r_Ma0n);
  1538. double Ma0np1 = r_Ma0np1.val;
  1539. double Ma0n = r_Ma0n.val;
  1540. double Ma0nm1;
  1541. err_rat = fabs(r_Ma0np1.err/r_Ma0np1.val) + fabs(r_Ma0n.err/r_Ma0n.val);
  1542. for(n=a0+epsb-1.0; n>b+0.1; n -= 1.0) {
  1543. Ma0nm1 = (-n*(1-n-x)*Ma0n - x*(n-a0)*Ma0np1)/(n*(n-1.0));
  1544. Ma0np1 = Ma0n;
  1545. Ma0n = Ma0nm1;
  1546. }
  1547. Ma0bp1 = Ma0np1;
  1548. Ma0b = Ma0n;
  1549. Ma0p1b = (b*(a0+x)*Ma0b+x*(a0-b)*Ma0bp1)/(a0*b); /* right-down hook */
  1550. stat_a0 = GSL_ERROR_SELECT_2(stat_0, stat_1);
  1551. }
  1552. /* Initialise the recurrence correctly BJG */
  1553. if (a0 >= a - 0.1)
  1554. {
  1555. Mn = Ma0b;
  1556. }
  1557. else if (a0 + 1>= a - 0.1)
  1558. {
  1559. Mn = Ma0p1b;
  1560. }
  1561. else
  1562. {
  1563. Mnm1 = Ma0b;
  1564. Mn = Ma0p1b;
  1565. for(n=a0+1.0; n<a-0.1; n += 1.0) {
  1566. Mnp1 = ((b-n)*Mnm1 + (2*n-b+x)*Mn)/n;
  1567. Mnm1 = Mn;
  1568. Mn = Mnp1;
  1569. }
  1570. }
  1571. result->val = Mn;
  1572. result->err = (err_rat + GSL_DBL_EPSILON) * (fabs(b-a)+1.0) * fabs(Mn);
  1573. return stat_a0;
  1574. }
  1575. }
  1576. }
  1577. /* Assumes b != integer
  1578. * Assumes a != integer when x > 0
  1579. * Assumes b-a != neg integer when x < 0
  1580. */
  1581. static
  1582. int
  1583. hyperg_1F1_ab_neg(const double a, const double b, const double x,
  1584. gsl_sf_result * result)
  1585. {
  1586. const double bma = b - a;
  1587. const double abs_x = fabs(x);
  1588. const double abs_a = fabs(a);
  1589. const double abs_b = fabs(b);
  1590. const double size_a = GSL_MAX(abs_a, 1.0);
  1591. const double size_b = GSL_MAX(abs_b, 1.0);
  1592. const int bma_integer = ( bma - floor(bma+0.5) < _1F1_INT_THRESHOLD );
  1593. if( (abs_a < 10.0 && abs_b < 10.0 && abs_x < 5.0)
  1594. || (b > 0.8*GSL_MAX(fabs(a),1.0)*fabs(x))
  1595. ) {
  1596. return gsl_sf_hyperg_1F1_series_e(a, b, x, result);
  1597. }
  1598. else if( x > 0.0
  1599. && size_b > size_a
  1600. && size_a*log(M_E*x/size_b) < GSL_LOG_DBL_EPSILON+7.0
  1601. ) {
  1602. /* Series terms are positive definite up until
  1603. * there is a sign change. But by then the
  1604. * terms are small due to the last condition.
  1605. */
  1606. return gsl_sf_hyperg_1F1_series_e(a, b, x, result);
  1607. }
  1608. else if( (abs_x < 5.0 && fabs(bma) < 10.0 && abs_b < 10.0)
  1609. || (b > 0.8*GSL_MAX_DBL(fabs(bma),1.0)*abs_x)
  1610. ) {
  1611. /* Use Kummer transformation to render series safe.
  1612. */
  1613. gsl_sf_result Kummer_1F1;
  1614. int stat_K = gsl_sf_hyperg_1F1_series_e(bma, b, -x, &Kummer_1F1);
  1615. int stat_e = gsl_sf_exp_mult_err_e(x, GSL_DBL_EPSILON * fabs(x),
  1616. Kummer_1F1.val, Kummer_1F1.err,
  1617. result);
  1618. return GSL_ERROR_SELECT_2(stat_e, stat_K);
  1619. }
  1620. else if( x < -30.0
  1621. && GSL_MAX_DBL(fabs(a),1.0)*GSL_MAX_DBL(fabs(1.0+a-b),1.0) < 0.99*fabs(x)
  1622. ) {
  1623. /* Large negative x asymptotic.
  1624. * Note that we do not check if b-a is a negative integer.
  1625. */
  1626. return hyperg_1F1_asymp_negx(a, b, x, result);
  1627. }
  1628. else if( x > 100.0
  1629. && GSL_MAX_DBL(fabs(bma),1.0)*GSL_MAX_DBL(fabs(1.0-a),1.0) < 0.99*fabs(x)
  1630. ) {
  1631. /* Large positive x asymptotic.
  1632. * Note that we do not check if a is a negative integer.
  1633. */
  1634. return hyperg_1F1_asymp_posx(a, b, x, result);
  1635. }
  1636. else if(x > 0.0 && !(bma_integer && bma > 0.0)) {
  1637. return hyperg_1F1_U(a, b, x, result);
  1638. }
  1639. else {
  1640. /* FIXME: if all else fails, try the series... BJG */
  1641. if (x < 0.0) {
  1642. /* Apply Kummer Transformation */
  1643. int status = gsl_sf_hyperg_1F1_series_e(b-a, b, -x, result);
  1644. double K_factor = exp(x);
  1645. result->val *= K_factor;
  1646. result->err *= K_factor;
  1647. return status;
  1648. } else {
  1649. int status = gsl_sf_hyperg_1F1_series_e(a, b, x, result);
  1650. return status;
  1651. }
  1652. /* Sadness... */
  1653. /* result->val = 0.0; */
  1654. /* result->err = 0.0; */
  1655. /* GSL_ERROR ("error", GSL_EUNIMPL); */
  1656. }
  1657. }
  1658. /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
  1659. int
  1660. gsl_sf_hyperg_1F1_int_e(const int a, const int b, const double x, gsl_sf_result * result)
  1661. {
  1662. /* CHECK_POINTER(result) */
  1663. if(x == 0.0) {
  1664. result->val = 1.0;
  1665. result->err = 0.0;
  1666. return GSL_SUCCESS;
  1667. }
  1668. else if(a == b) {
  1669. return gsl_sf_exp_e(x, result);
  1670. }
  1671. else if(b == 0) {
  1672. DOMAIN_ERROR(result);
  1673. }
  1674. else if(a == 0) {
  1675. result->val = 1.0;
  1676. result->err = 0.0;
  1677. return GSL_SUCCESS;
  1678. }
  1679. else if(b < 0 && (a < b || a > 0)) {
  1680. /* Standard domain error due to singularity. */
  1681. DOMAIN_ERROR(result);
  1682. }
  1683. else if(x > 100.0 && GSL_MAX_DBL(1.0,fabs(b-a))*GSL_MAX_DBL(1.0,fabs(1-a)) < 0.5 * x) {
  1684. /* x -> +Inf asymptotic */
  1685. return hyperg_1F1_asymp_posx(a, b, x, result);
  1686. }
  1687. else if(x < -100.0 && GSL_MAX_DBL(1.0,fabs(a))*GSL_MAX_DBL(1.0,fabs(1+a-b)) < 0.5 * fabs(x)) {
  1688. /* x -> -Inf asymptotic */
  1689. return hyperg_1F1_asymp_negx(a, b, x, result);
  1690. }
  1691. else if(a < 0 && b < 0) {
  1692. return hyperg_1F1_ab_negint(a, b, x, result);
  1693. }
  1694. else if(a < 0 && b > 0) {
  1695. /* Use Kummer to reduce it to the positive integer case.
  1696. * Note that b > a, strictly, since we already trapped b = a.
  1697. */
  1698. gsl_sf_result Kummer_1F1;
  1699. int stat_K = hyperg_1F1_ab_posint(b-a, b, -x, &Kummer_1F1);
  1700. int stat_e = gsl_sf_exp_mult_err_e(x, GSL_DBL_EPSILON * fabs(x),
  1701. Kummer_1F1.val, Kummer_1F1.err,
  1702. result);
  1703. return GSL_ERROR_SELECT_2(stat_e, stat_K);
  1704. }
  1705. else {
  1706. /* a > 0 and b > 0 */
  1707. return hyperg_1F1_ab_posint(a, b, x, result);
  1708. }
  1709. }
  1710. int
  1711. gsl_sf_hyperg_1F1_e(const double a, const double b, const double x,
  1712. gsl_sf_result * result
  1713. )
  1714. {
  1715. const double bma = b - a;
  1716. const double rinta = floor(a + 0.5);
  1717. const double rintb = floor(b + 0.5);
  1718. const double rintbma = floor(bma + 0.5);
  1719. const int a_integer = ( fabs(a-rinta) < _1F1_INT_THRESHOLD && rinta > INT_MIN && rinta < INT_MAX );
  1720. const int b_integer = ( fabs(b-rintb) < _1F1_INT_THRESHOLD && rintb > INT_MIN && rintb < INT_MAX );
  1721. const int bma_integer = ( fabs(bma-rintbma) < _1F1_INT_THRESHOLD && rintbma > INT_MIN && rintbma < INT_MAX );
  1722. const int b_neg_integer = ( b < -0.1 && b_integer );
  1723. const int a_neg_integer = ( a < -0.1 && a_integer );
  1724. const int bma_neg_integer = ( bma < -0.1 && bma_integer );
  1725. /* CHECK_POINTER(result) */
  1726. if(x == 0.0) {
  1727. /* Testing for this before testing a and b
  1728. * is somewhat arbitrary. The result is that
  1729. * we have 1F1(a,0,0) = 1.
  1730. */
  1731. result->val = 1.0;
  1732. result->err = 0.0;
  1733. return GSL_SUCCESS;
  1734. }
  1735. else if(b == 0.0) {
  1736. DOMAIN_ERROR(result);
  1737. }
  1738. else if(a == 0.0) {
  1739. result->val = 1.0;
  1740. result->err = 0.0;
  1741. return GSL_SUCCESS;
  1742. }
  1743. else if(a == b) {
  1744. /* case: a==b; exp(x)
  1745. * It's good to test exact equality now.
  1746. * We also test approximate equality later.
  1747. */
  1748. return gsl_sf_exp_e(x, result);
  1749. } else if(fabs(b) < _1F1_INT_THRESHOLD && fabs(a) < _1F1_INT_THRESHOLD) {
  1750. /* a and b near zero: 1 + a/b (exp(x)-1)
  1751. */
  1752. /* Note that neither a nor b is zero, since
  1753. * we eliminated that with the above tests.
  1754. */
  1755. gsl_sf_result exm1;
  1756. int stat_e = gsl_sf_expm1_e(x, &exm1);
  1757. double sa = ( a > 0.0 ? 1.0 : -1.0 );
  1758. double sb = ( b > 0.0 ? 1.0 : -1.0 );
  1759. double lnab = log(fabs(a/b)); /* safe */
  1760. gsl_sf_result hx;
  1761. int stat_hx = gsl_sf_exp_mult_err_e(lnab, GSL_DBL_EPSILON * fabs(lnab),
  1762. sa * sb * exm1.val, exm1.err,
  1763. &hx);
  1764. result->val = (hx.val == GSL_DBL_MAX ? hx.val : 1.0 + hx.val); /* FIXME: excessive paranoia ? what is DBL_MAX+1 ?*/
  1765. result->err = hx.err;
  1766. return GSL_ERROR_SELECT_2(stat_hx, stat_e);
  1767. } else if (fabs(b) < _1F1_INT_THRESHOLD && fabs(x*a) < 1) {
  1768. /* b near zero and a not near zero
  1769. */
  1770. const double m_arg = 1.0/(0.5*b);
  1771. gsl_sf_result F_renorm;
  1772. int stat_F = hyperg_1F1_renorm_b0(a, x, &F_renorm);
  1773. int stat_m = gsl_sf_multiply_err_e(m_arg, 2.0 * GSL_DBL_EPSILON * m_arg,
  1774. 0.5*F_renorm.val, 0.5*F_renorm.err,
  1775. result);
  1776. return GSL_ERROR_SELECT_2(stat_m, stat_F);
  1777. }
  1778. else if(a_integer && b_integer) {
  1779. /* Check for reduction to the integer case.
  1780. * Relies on the arbitrary "near an integer" test.
  1781. */
  1782. return gsl_sf_hyperg_1F1_int_e((int)rinta, (int)rintb, x, result);
  1783. }
  1784. else if(b_neg_integer && !(a_neg_integer && a > b)) {
  1785. /* Standard domain error due to
  1786. * uncancelled singularity.
  1787. */
  1788. DOMAIN_ERROR(result);
  1789. }
  1790. else if(a_neg_integer) {
  1791. return hyperg_1F1_a_negint_lag((int)rinta, b, x, result);
  1792. }
  1793. else if(b > 0.0) {
  1794. if(-1.0 <= a && a <= 1.0) {
  1795. /* Handle small a explicitly.
  1796. */
  1797. return hyperg_1F1_small_a_bgt0(a, b, x, result);
  1798. }
  1799. else if(bma_neg_integer) {
  1800. /* Catch this now, to avoid problems in the
  1801. * generic evaluation code.
  1802. */
  1803. gsl_sf_result Kummer_1F1;
  1804. int stat_K = hyperg_1F1_a_negint_lag((int)rintbma, b, -x, &Kummer_1F1);
  1805. int stat_e = gsl_sf_exp_mult_err_e(x, GSL_DBL_EPSILON * fabs(x),
  1806. Kummer_1F1.val, Kummer_1F1.err,
  1807. result);
  1808. return GSL_ERROR_SELECT_2(stat_e, stat_K);
  1809. }
  1810. else if(a < 0.0 && fabs(x) < 100.0) {
  1811. /* Use Kummer to reduce it to the generic positive case.
  1812. * Note that b > a, strictly, since we already trapped b = a.
  1813. * Also b-(b-a)=a, and a is not a negative integer here,
  1814. * so the generic evaluation is safe.
  1815. */
  1816. gsl_sf_result Kummer_1F1;
  1817. int stat_K = hyperg_1F1_ab_pos(b-a, b, -x, &Kummer_1F1);
  1818. int stat_e = gsl_sf_exp_mult_err_e(x, GSL_DBL_EPSILON * fabs(x),
  1819. Kummer_1F1.val, Kummer_1F1.err,
  1820. result);
  1821. return GSL_ERROR_SELECT_2(stat_e, stat_K);
  1822. }
  1823. else if (a > 0) {
  1824. /* a > 0.0 */
  1825. return hyperg_1F1_ab_pos(a, b, x, result);
  1826. } else {
  1827. return gsl_sf_hyperg_1F1_series_e(a, b, x, result);
  1828. }
  1829. }
  1830. else {
  1831. /* b < 0.0 */
  1832. if(bma_neg_integer && x < 0.0) {
  1833. /* Handle this now to prevent problems
  1834. * in the generic evaluation.
  1835. */
  1836. gsl_sf_result K;
  1837. int stat_K;
  1838. int stat_e;
  1839. if(a < 0.0) {
  1840. /* Kummer transformed version of safe polynomial.
  1841. * The condition a < 0 is equivalent to b < b-a,
  1842. * which is the condition required for the series
  1843. * to be positive definite here.
  1844. */
  1845. stat_K = hyperg_1F1_a_negint_poly((int)rintbma, b, -x, &K);
  1846. }
  1847. else {
  1848. /* Generic eval for negative integer a. */
  1849. stat_K = hyperg_1F1_a_negint_lag((int)rintbma, b, -x, &K);
  1850. }
  1851. stat_e = gsl_sf_exp_mult_err_e(x, GSL_DBL_EPSILON * fabs(x),
  1852. K.val, K.err,
  1853. result);
  1854. return GSL_ERROR_SELECT_2(stat_e, stat_K);
  1855. }
  1856. else if(a > 0.0) {
  1857. /* Use Kummer to reduce it to the generic negative case.
  1858. */
  1859. gsl_sf_result K;
  1860. int stat_K = hyperg_1F1_ab_neg(b-a, b, -x, &K);
  1861. int stat_e = gsl_sf_exp_mult_err_e(x, GSL_DBL_EPSILON * fabs(x),
  1862. K.val, K.err,
  1863. result);
  1864. return GSL_ERROR_SELECT_2(stat_e, stat_K);
  1865. }
  1866. else {
  1867. return hyperg_1F1_ab_neg(a, b, x, result);
  1868. }
  1869. }
  1870. }
  1871. #if 0
  1872. /* Luke in the canonical case.
  1873. */
  1874. if(x < 0.0 && !a_neg_integer && !bma_neg_integer) {
  1875. double prec;
  1876. return hyperg_1F1_luke(a, b, x, result, &prec);
  1877. }
  1878. /* Luke with Kummer transformation.
  1879. */
  1880. if(x > 0.0 && !a_neg_integer && !bma_neg_integer) {
  1881. double prec;
  1882. double Kummer_1F1;
  1883. double ex;
  1884. int stat_F = hyperg_1F1_luke(b-a, b, -x, &Kummer_1F1, &prec);
  1885. int stat_e = gsl_sf_exp_e(x, &ex);
  1886. if(stat_F == GSL_SUCCESS && stat_e == GSL_SUCCESS) {
  1887. double lnr = log(fabs(Kummer_1F1)) + x;
  1888. if(lnr < GSL_LOG_DBL_MAX) {
  1889. *result = ex * Kummer_1F1;
  1890. return GSL_SUCCESS;
  1891. }
  1892. else {
  1893. *result = GSL_POSINF;
  1894. GSL_ERROR ("overflow", GSL_EOVRFLW);
  1895. }
  1896. }
  1897. else if(stat_F != GSL_SUCCESS) {
  1898. *result = 0.0;
  1899. return stat_F;
  1900. }
  1901. else {
  1902. *result = 0.0;
  1903. return stat_e;
  1904. }
  1905. }
  1906. #endif
  1907. /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
  1908. #include "gsl_specfunc__eval.h"
  1909. double gsl_sf_hyperg_1F1_int(const int m, const int n, double x)
  1910. {
  1911. EVAL_RESULT(gsl_sf_hyperg_1F1_int_e(m, n, x, &result));
  1912. }
  1913. double gsl_sf_hyperg_1F1(double a, double b, double x)
  1914. {
  1915. EVAL_RESULT(gsl_sf_hyperg_1F1_e(a, b, x, &result));
  1916. }