gsl_specfunc__legendre_Qn.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* specfunc/legendre_Qn.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_bessel.h"
  24. #include "gsl_sf_elementary.h"
  25. #include "gsl_sf_exp.h"
  26. #include "gsl_sf_pow_int.h"
  27. #include "gsl_sf_legendre.h"
  28. #include "gsl_specfunc__error.h"
  29. /* Evaluate f_{ell+1}/f_ell
  30. * f_ell := Q^{b}_{a+ell}(x)
  31. * x > 1
  32. */
  33. static
  34. int
  35. legendreQ_CF1_xgt1(int ell, double a, double b, double x, double * result)
  36. {
  37. const double RECUR_BIG = GSL_SQRT_DBL_MAX;
  38. const int maxiter = 5000;
  39. int n = 1;
  40. double Anm2 = 1.0;
  41. double Bnm2 = 0.0;
  42. double Anm1 = 0.0;
  43. double Bnm1 = 1.0;
  44. double a1 = ell + 1.0 + a + b;
  45. double b1 = (2.0*(ell+1.0+a) + 1.0) * x;
  46. double An = b1*Anm1 + a1*Anm2;
  47. double Bn = b1*Bnm1 + a1*Bnm2;
  48. double an, bn;
  49. double fn = An/Bn;
  50. while(n < maxiter) {
  51. double old_fn;
  52. double del;
  53. double lna;
  54. n++;
  55. Anm2 = Anm1;
  56. Bnm2 = Bnm1;
  57. Anm1 = An;
  58. Bnm1 = Bn;
  59. lna = ell + n + a;
  60. an = b*b - lna*lna;
  61. bn = (2.0*lna + 1.0) * x;
  62. An = bn*Anm1 + an*Anm2;
  63. Bn = bn*Bnm1 + an*Bnm2;
  64. if(fabs(An) > RECUR_BIG || fabs(Bn) > RECUR_BIG) {
  65. An /= RECUR_BIG;
  66. Bn /= RECUR_BIG;
  67. Anm1 /= RECUR_BIG;
  68. Bnm1 /= RECUR_BIG;
  69. Anm2 /= RECUR_BIG;
  70. Bnm2 /= RECUR_BIG;
  71. }
  72. old_fn = fn;
  73. fn = An/Bn;
  74. del = old_fn/fn;
  75. if(fabs(del - 1.0) < 4.0*GSL_DBL_EPSILON) break;
  76. }
  77. *result = fn;
  78. if(n == maxiter)
  79. GSL_ERROR ("error", GSL_EMAXITER);
  80. else
  81. return GSL_SUCCESS;
  82. }
  83. /* Uniform asymptotic for Q_l(x).
  84. * Assumes x > -1.0 and x != 1.0.
  85. * Discards second order and higher terms.
  86. */
  87. static
  88. int
  89. legendre_Ql_asymp_unif(const double ell, const double x, gsl_sf_result * result)
  90. {
  91. if(x < 1.0) {
  92. double u = ell + 0.5;
  93. double th = acos(x);
  94. gsl_sf_result Y0, Y1;
  95. int stat_Y0, stat_Y1;
  96. int stat_m;
  97. double pre;
  98. double B00;
  99. double sum;
  100. /* B00 = 1/8 (1 - th cot(th) / th^2
  101. * pre = sqrt(th/sin(th))
  102. */
  103. if(th < GSL_ROOT4_DBL_EPSILON) {
  104. B00 = (1.0 + th*th/15.0)/24.0;
  105. pre = 1.0 + th*th/12.0;
  106. }
  107. else {
  108. double sin_th = sqrt(1.0 - x*x);
  109. double cot_th = x / sin_th;
  110. B00 = 1.0/8.0 * (1.0 - th * cot_th) / (th*th);
  111. pre = sqrt(th/sin_th);
  112. }
  113. stat_Y0 = gsl_sf_bessel_Y0_e(u*th, &Y0);
  114. stat_Y1 = gsl_sf_bessel_Y1_e(u*th, &Y1);
  115. sum = -0.5*M_PI * (Y0.val + th/u * Y1.val * B00);
  116. stat_m = gsl_sf_multiply_e(pre, sum, result);
  117. result->err += 0.5*M_PI * fabs(pre) * (Y0.err + fabs(th/u*B00)*Y1.err);
  118. result->err += GSL_DBL_EPSILON * fabs(result->val);
  119. return GSL_ERROR_SELECT_3(stat_m, stat_Y0, stat_Y1);
  120. }
  121. else {
  122. double u = ell + 0.5;
  123. double xi = acosh(x);
  124. gsl_sf_result K0_scaled, K1_scaled;
  125. int stat_K0, stat_K1;
  126. int stat_e;
  127. double pre;
  128. double B00;
  129. double sum;
  130. /* B00 = -1/8 (1 - xi coth(xi) / xi^2
  131. * pre = sqrt(xi/sinh(xi))
  132. */
  133. if(xi < GSL_ROOT4_DBL_EPSILON) {
  134. B00 = (1.0-xi*xi/15.0)/24.0;
  135. pre = 1.0 - xi*xi/12.0;
  136. }
  137. else {
  138. double sinh_xi = sqrt(x*x - 1.0);
  139. double coth_xi = x / sinh_xi;
  140. B00 = -1.0/8.0 * (1.0 - xi * coth_xi) / (xi*xi);
  141. pre = sqrt(xi/sinh_xi);
  142. }
  143. stat_K0 = gsl_sf_bessel_K0_scaled_e(u*xi, &K0_scaled);
  144. stat_K1 = gsl_sf_bessel_K1_scaled_e(u*xi, &K1_scaled);
  145. sum = K0_scaled.val - xi/u * K1_scaled.val * B00;
  146. stat_e = gsl_sf_exp_mult_e(-u*xi, pre * sum, result);
  147. result->err = GSL_DBL_EPSILON * fabs(result->val) * fabs(u*xi);
  148. result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  149. return GSL_ERROR_SELECT_3(stat_e, stat_K0, stat_K1);
  150. }
  151. }
  152. /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
  153. int
  154. gsl_sf_legendre_Q0_e(const double x, gsl_sf_result * result)
  155. {
  156. /* CHECK_POINTER(result) */
  157. if(x <= -1.0 || x == 1.0) {
  158. DOMAIN_ERROR(result);
  159. }
  160. else if(x*x < GSL_ROOT6_DBL_EPSILON) { /* |x| <~ 0.05 */
  161. const double c3 = 1.0/3.0;
  162. const double c5 = 1.0/5.0;
  163. const double c7 = 1.0/7.0;
  164. const double c9 = 1.0/9.0;
  165. const double c11 = 1.0/11.0;
  166. const double y = x * x;
  167. const double series = 1.0 + y*(c3 + y*(c5 + y*(c7 + y*(c9 + y*c11))));
  168. result->val = x * series;
  169. result->err = 2.0 * GSL_DBL_EPSILON * fabs(x);
  170. return GSL_SUCCESS;
  171. }
  172. else if(x < 1.0) {
  173. result->val = 0.5 * log((1.0+x)/(1.0-x));
  174. result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  175. return GSL_SUCCESS;
  176. }
  177. else if(x < 10.0) {
  178. result->val = 0.5 * log((x+1.0)/(x-1.0));
  179. result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  180. return GSL_SUCCESS;
  181. }
  182. else if(x*GSL_DBL_MIN < 2.0) {
  183. const double y = 1.0/(x*x);
  184. const double c1 = 1.0/3.0;
  185. const double c2 = 1.0/5.0;
  186. const double c3 = 1.0/7.0;
  187. const double c4 = 1.0/9.0;
  188. const double c5 = 1.0/11.0;
  189. const double c6 = 1.0/13.0;
  190. const double c7 = 1.0/15.0;
  191. result->val = (1.0/x) * (1.0 + y*(c1 + y*(c2 + y*(c3 + y*(c4 + y*(c5 + y*(c6 + y*c7)))))));
  192. result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  193. return GSL_SUCCESS;
  194. }
  195. else {
  196. UNDERFLOW_ERROR(result);
  197. }
  198. }
  199. int
  200. gsl_sf_legendre_Q1_e(const double x, gsl_sf_result * result)
  201. {
  202. /* CHECK_POINTER(result) */
  203. if(x <= -1.0 || x == 1.0) {
  204. DOMAIN_ERROR(result);
  205. }
  206. else if(x*x < GSL_ROOT6_DBL_EPSILON) { /* |x| <~ 0.05 */
  207. const double c3 = 1.0/3.0;
  208. const double c5 = 1.0/5.0;
  209. const double c7 = 1.0/7.0;
  210. const double c9 = 1.0/9.0;
  211. const double c11 = 1.0/11.0;
  212. const double y = x * x;
  213. const double series = 1.0 + y*(c3 + y*(c5 + y*(c7 + y*(c9 + y*c11))));
  214. result->val = x * x * series - 1.0;
  215. result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  216. return GSL_SUCCESS;
  217. }
  218. else if(x < 1.0){
  219. result->val = 0.5 * x * (log((1.0+x)/(1.0-x))) - 1.0;
  220. result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  221. return GSL_SUCCESS;
  222. }
  223. else if(x < 6.0) {
  224. result->val = 0.5 * x * log((x+1.0)/(x-1.0)) - 1.0;
  225. result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  226. return GSL_SUCCESS;
  227. }
  228. else if(x*GSL_SQRT_DBL_MIN < 0.99/M_SQRT3) {
  229. const double y = 1/(x*x);
  230. const double c1 = 3.0/5.0;
  231. const double c2 = 3.0/7.0;
  232. const double c3 = 3.0/9.0;
  233. const double c4 = 3.0/11.0;
  234. const double c5 = 3.0/13.0;
  235. const double c6 = 3.0/15.0;
  236. const double c7 = 3.0/17.0;
  237. const double c8 = 3.0/19.0;
  238. const double sum = 1.0 + y*(c1 + y*(c2 + y*(c3 + y*(c4 + y*(c5 + y*(c6 + y*(c7 + y*c8)))))));
  239. result->val = sum / (3.0*x*x);
  240. result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  241. return GSL_SUCCESS;
  242. }
  243. else {
  244. UNDERFLOW_ERROR(result);
  245. }
  246. }
  247. int
  248. gsl_sf_legendre_Ql_e(const int l, const double x, gsl_sf_result * result)
  249. {
  250. /* CHECK_POINTER(result) */
  251. if(x <= -1.0 || x == 1.0 || l < 0) {
  252. DOMAIN_ERROR(result);
  253. }
  254. else if(l == 0) {
  255. return gsl_sf_legendre_Q0_e(x, result);
  256. }
  257. else if(l == 1) {
  258. return gsl_sf_legendre_Q1_e(x, result);
  259. }
  260. else if(l > 100000) {
  261. return legendre_Ql_asymp_unif(l, x, result);
  262. }
  263. else if(x < 1.0){
  264. /* Forward recurrence.
  265. */
  266. gsl_sf_result Q0, Q1;
  267. int stat_Q0 = gsl_sf_legendre_Q0_e(x, &Q0);
  268. int stat_Q1 = gsl_sf_legendre_Q1_e(x, &Q1);
  269. double Qellm1 = Q0.val;
  270. double Qell = Q1.val;
  271. double Qellp1;
  272. int ell;
  273. for(ell=1; ell<l; ell++) {
  274. Qellp1 = (x*(2.0*ell + 1.0) * Qell - ell * Qellm1) / (ell + 1.0);
  275. Qellm1 = Qell;
  276. Qell = Qellp1;
  277. }
  278. result->val = Qell;
  279. result->err = GSL_DBL_EPSILON * l * fabs(result->val);
  280. return GSL_ERROR_SELECT_2(stat_Q0, stat_Q1);
  281. }
  282. else {
  283. /* x > 1.0 */
  284. double rat;
  285. int stat_CF1 = legendreQ_CF1_xgt1(l, 0.0, 0.0, x, &rat);
  286. int stat_Q;
  287. double Qellp1 = rat * GSL_SQRT_DBL_MIN;
  288. double Qell = GSL_SQRT_DBL_MIN;
  289. double Qellm1;
  290. int ell;
  291. for(ell=l; ell>0; ell--) {
  292. Qellm1 = (x * (2.0*ell + 1.0) * Qell - (ell+1.0) * Qellp1) / ell;
  293. Qellp1 = Qell;
  294. Qell = Qellm1;
  295. }
  296. if(fabs(Qell) > fabs(Qellp1)) {
  297. gsl_sf_result Q0;
  298. stat_Q = gsl_sf_legendre_Q0_e(x, &Q0);
  299. result->val = GSL_SQRT_DBL_MIN * Q0.val / Qell;
  300. result->err = l * GSL_DBL_EPSILON * fabs(result->val);
  301. }
  302. else {
  303. gsl_sf_result Q1;
  304. stat_Q = gsl_sf_legendre_Q1_e(x, &Q1);
  305. result->val = GSL_SQRT_DBL_MIN * Q1.val / Qellp1;
  306. result->err = l * GSL_DBL_EPSILON * fabs(result->val);
  307. }
  308. return GSL_ERROR_SELECT_2(stat_Q, stat_CF1);
  309. }
  310. }
  311. /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
  312. #include "gsl_specfunc__eval.h"
  313. double gsl_sf_legendre_Q0(const double x)
  314. {
  315. EVAL_RESULT(gsl_sf_legendre_Q0_e(x, &result));
  316. }
  317. double gsl_sf_legendre_Q1(const double x)
  318. {
  319. EVAL_RESULT(gsl_sf_legendre_Q1_e(x, &result));
  320. }
  321. double gsl_sf_legendre_Ql(const int l, const double x)
  322. {
  323. EVAL_RESULT(gsl_sf_legendre_Ql_e(l, x, &result));
  324. }