gsl_specfunc__mathieu_radfunc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /* specfunc/mathieu_radfunc.c
  2. *
  3. * Copyright (C) 2002 Lowell Johnson
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. /* Author: L. Johnson */
  20. #include "gsl__config.h"
  21. #include <stdlib.h>
  22. #include <math.h>
  23. #include "gsl_math.h"
  24. #include "gsl_sf.h"
  25. #include "gsl_sf_mathieu.h"
  26. int gsl_sf_mathieu_Mc(int kind, int order, double qq, double zz,
  27. gsl_sf_result *result)
  28. {
  29. int even_odd, kk, mm, status;
  30. double maxerr = 1e-14, amax, pi = M_PI, fn, factor;
  31. double coeff[GSL_SF_MATHIEU_COEFF], fc;
  32. double j1c, z2c, j1pc, z2pc;
  33. double u1, u2;
  34. gsl_sf_result aa;
  35. /* Check for out of bounds parameters. */
  36. if (qq <= 0.0)
  37. {
  38. GSL_ERROR("q must be greater than zero", GSL_EINVAL);
  39. }
  40. if (kind < 1 || kind > 2)
  41. {
  42. GSL_ERROR("kind must be 1 or 2", GSL_EINVAL);
  43. }
  44. mm = 0;
  45. amax = 0.0;
  46. fn = 0.0;
  47. u1 = sqrt(qq)*exp(-1.0*zz);
  48. u2 = sqrt(qq)*exp(zz);
  49. even_odd = 0;
  50. if (order % 2 != 0)
  51. even_odd = 1;
  52. /* Compute the characteristic value. */
  53. status = gsl_sf_mathieu_a(order, qq, &aa);
  54. if (status != GSL_SUCCESS)
  55. {
  56. return status;
  57. }
  58. /* Compute the series coefficients. */
  59. status = gsl_sf_mathieu_a_coeff(order, qq, aa.val, coeff);
  60. if (status != GSL_SUCCESS)
  61. {
  62. return status;
  63. }
  64. if (even_odd == 0)
  65. {
  66. for (kk=0; kk<GSL_SF_MATHIEU_COEFF; kk++)
  67. {
  68. amax = GSL_MAX(amax, fabs(coeff[kk]));
  69. if (fabs(coeff[kk])/amax < maxerr)
  70. break;
  71. j1c = gsl_sf_bessel_Jn(kk, u1);
  72. if (kind == 1)
  73. {
  74. z2c = gsl_sf_bessel_Jn(kk, u2);
  75. }
  76. else /* kind = 2 */
  77. {
  78. z2c = gsl_sf_bessel_Yn(kk, u2);
  79. }
  80. fc = pow(-1.0, 0.5*order+kk)*coeff[kk];
  81. fn += fc*j1c*z2c;
  82. }
  83. fn *= sqrt(pi/2.0)/coeff[0];
  84. }
  85. else
  86. {
  87. for (kk=0; kk<GSL_SF_MATHIEU_COEFF; kk++)
  88. {
  89. amax = GSL_MAX(amax, fabs(coeff[kk]));
  90. if (fabs(coeff[kk])/amax < maxerr)
  91. break;
  92. j1c = gsl_sf_bessel_Jn(kk, u1);
  93. j1pc = gsl_sf_bessel_Jn(kk+1, u1);
  94. if (kind == 1)
  95. {
  96. z2c = gsl_sf_bessel_Jn(kk, u2);
  97. z2pc = gsl_sf_bessel_Jn(kk+1, u2);
  98. }
  99. else /* kind = 2 */
  100. {
  101. z2c = gsl_sf_bessel_Yn(kk, u2);
  102. z2pc = gsl_sf_bessel_Yn(kk+1, u2);
  103. }
  104. fc = pow(-1.0, 0.5*(order-1)+kk)*coeff[kk];
  105. fn += fc*(j1c*z2pc + j1pc*z2c);
  106. }
  107. fn *= sqrt(pi/2.0)/coeff[0];
  108. }
  109. result->val = fn;
  110. result->err = 2.0*GSL_DBL_EPSILON;
  111. factor = fabs(fn);
  112. if (factor > 1.0)
  113. result->err *= factor;
  114. return GSL_SUCCESS;
  115. }
  116. int gsl_sf_mathieu_Ms(int kind, int order, double qq, double zz,
  117. gsl_sf_result *result)
  118. {
  119. int even_odd, kk, mm, status;
  120. double maxerr = 1e-14, amax, pi = M_PI, fn, factor;
  121. double coeff[GSL_SF_MATHIEU_COEFF], fc;
  122. double j1c, z2c, j1mc, z2mc, j1pc, z2pc;
  123. double u1, u2;
  124. gsl_sf_result aa;
  125. /* Check for out of bounds parameters. */
  126. if (qq <= 0.0)
  127. {
  128. GSL_ERROR("q must be greater than zero", GSL_EINVAL);
  129. }
  130. if (kind < 1 || kind > 2)
  131. {
  132. GSL_ERROR("kind must be 1 or 2", GSL_EINVAL);
  133. }
  134. mm = 0;
  135. amax = 0.0;
  136. fn = 0.0;
  137. u1 = sqrt(qq)*exp(-1.0*zz);
  138. u2 = sqrt(qq)*exp(zz);
  139. even_odd = 0;
  140. if (order % 2 != 0)
  141. even_odd = 1;
  142. /* Compute the characteristic value. */
  143. status = gsl_sf_mathieu_b(order, qq, &aa);
  144. if (status != GSL_SUCCESS)
  145. {
  146. return status;
  147. }
  148. /* Compute the series coefficients. */
  149. status = gsl_sf_mathieu_b_coeff(order, qq, aa.val, coeff);
  150. if (status != GSL_SUCCESS)
  151. {
  152. return status;
  153. }
  154. if (even_odd == 0)
  155. {
  156. for (kk=0; kk<GSL_SF_MATHIEU_COEFF; kk++)
  157. {
  158. amax = GSL_MAX(amax, fabs(coeff[kk]));
  159. if (fabs(coeff[kk])/amax < maxerr)
  160. break;
  161. j1mc = gsl_sf_bessel_Jn(kk, u1);
  162. j1pc = gsl_sf_bessel_Jn(kk+2, u1);
  163. if (kind == 1)
  164. {
  165. z2mc = gsl_sf_bessel_Jn(kk, u2);
  166. z2pc = gsl_sf_bessel_Jn(kk+2, u2);
  167. }
  168. else /* kind = 2 */
  169. {
  170. z2mc = gsl_sf_bessel_Yn(kk, u2);
  171. z2pc = gsl_sf_bessel_Yn(kk+2, u2);
  172. }
  173. fc = pow(-1.0, 0.5*order+kk+1)*coeff[kk];
  174. fn += fc*(j1mc*z2pc - j1pc*z2mc);
  175. }
  176. fn *= sqrt(pi/2.0)/coeff[0];
  177. }
  178. else
  179. {
  180. for (kk=0; kk<GSL_SF_MATHIEU_COEFF; kk++)
  181. {
  182. amax = GSL_MAX(amax, fabs(coeff[kk]));
  183. if (fabs(coeff[kk])/amax < maxerr)
  184. break;
  185. j1c = gsl_sf_bessel_Jn(kk, u1);
  186. j1pc = gsl_sf_bessel_Jn(kk+1, u1);
  187. if (kind == 1)
  188. {
  189. z2c = gsl_sf_bessel_Jn(kk, u2);
  190. z2pc = gsl_sf_bessel_Jn(kk+1, u2);
  191. }
  192. else /* kind = 2 */
  193. {
  194. z2c = gsl_sf_bessel_Yn(kk, u2);
  195. z2pc = gsl_sf_bessel_Yn(kk+1, u2);
  196. }
  197. fc = pow(-1.0, 0.5*(order-1)+kk)*coeff[kk];
  198. fn += fc*(j1c*z2pc - j1pc*z2c);
  199. }
  200. fn *= sqrt(pi/2.0)/coeff[0];
  201. }
  202. result->val = fn;
  203. result->err = 2.0*GSL_DBL_EPSILON;
  204. factor = fabs(fn);
  205. if (factor > 1.0)
  206. result->err *= factor;
  207. return GSL_SUCCESS;
  208. }
  209. int gsl_sf_mathieu_Mc_array(int kind, int nmin, int nmax, double qq,
  210. double zz, gsl_sf_mathieu_workspace *work,
  211. double result_array[])
  212. {
  213. int even_odd, order, ii, kk, mm, status;
  214. double maxerr = 1e-14, amax, pi = M_PI, fn;
  215. double coeff[GSL_SF_MATHIEU_COEFF], fc;
  216. double j1c, z2c, j1pc, z2pc;
  217. double u1, u2;
  218. double *aa = work->aa;
  219. /* Initialize the result array to zeroes. */
  220. for (ii=0; ii<nmax-nmin+1; ii++)
  221. result_array[ii] = 0.0;
  222. /* Check for out of bounds parameters. */
  223. if (qq <= 0.0)
  224. {
  225. GSL_ERROR("q must be greater than zero", GSL_EINVAL);
  226. }
  227. if (kind < 1 || kind > 2)
  228. {
  229. GSL_ERROR("kind must be 1 or 2", GSL_EINVAL);
  230. }
  231. mm = 0;
  232. amax = 0.0;
  233. fn = 0.0;
  234. u1 = sqrt(qq)*exp(-1.0*zz);
  235. u2 = sqrt(qq)*exp(zz);
  236. /* Compute all eigenvalues up to nmax. */
  237. gsl_sf_mathieu_a_array(0, nmax, qq, work, aa);
  238. for (ii=0, order=nmin; order<=nmax; ii++, order++)
  239. {
  240. even_odd = 0;
  241. if (order % 2 != 0)
  242. even_odd = 1;
  243. /* Compute the series coefficients. */
  244. status = gsl_sf_mathieu_a_coeff(order, qq, aa[order], coeff);
  245. if (status != GSL_SUCCESS)
  246. {
  247. return status;
  248. }
  249. if (even_odd == 0)
  250. {
  251. for (kk=0; kk<GSL_SF_MATHIEU_COEFF; kk++)
  252. {
  253. amax = GSL_MAX(amax, fabs(coeff[kk]));
  254. if (fabs(coeff[kk])/amax < maxerr)
  255. break;
  256. j1c = gsl_sf_bessel_Jn(kk, u1);
  257. if (kind == 1)
  258. {
  259. z2c = gsl_sf_bessel_Jn(kk, u2);
  260. }
  261. else /* kind = 2 */
  262. {
  263. z2c = gsl_sf_bessel_Yn(kk, u2);
  264. }
  265. fc = pow(-1.0, 0.5*order+kk)*coeff[kk];
  266. fn += fc*j1c*z2c;
  267. }
  268. fn *= sqrt(pi/2.0)/coeff[0];
  269. }
  270. else
  271. {
  272. for (kk=0; kk<GSL_SF_MATHIEU_COEFF; kk++)
  273. {
  274. amax = GSL_MAX(amax, fabs(coeff[kk]));
  275. if (fabs(coeff[kk])/amax < maxerr)
  276. break;
  277. j1c = gsl_sf_bessel_Jn(kk, u1);
  278. j1pc = gsl_sf_bessel_Jn(kk+1, u1);
  279. if (kind == 1)
  280. {
  281. z2c = gsl_sf_bessel_Jn(kk, u2);
  282. z2pc = gsl_sf_bessel_Jn(kk+1, u2);
  283. }
  284. else /* kind = 2 */
  285. {
  286. z2c = gsl_sf_bessel_Yn(kk, u2);
  287. z2pc = gsl_sf_bessel_Yn(kk+1, u2);
  288. }
  289. fc = pow(-1.0, 0.5*(order-1)+kk)*coeff[kk];
  290. fn += fc*(j1c*z2pc + j1pc*z2c);
  291. }
  292. fn *= sqrt(pi/2.0)/coeff[0];
  293. }
  294. result_array[ii] = fn;
  295. } /* order loop */
  296. return GSL_SUCCESS;
  297. }
  298. int gsl_sf_mathieu_Ms_array(int kind, int nmin, int nmax, double qq,
  299. double zz, gsl_sf_mathieu_workspace *work,
  300. double result_array[])
  301. {
  302. int even_odd, order, ii, kk, mm, status;
  303. double maxerr = 1e-14, amax, pi = M_PI, fn;
  304. double coeff[GSL_SF_MATHIEU_COEFF], fc;
  305. double j1c, z2c, j1mc, z2mc, j1pc, z2pc;
  306. double u1, u2;
  307. double *bb = work->bb;
  308. /* Initialize the result array to zeroes. */
  309. for (ii=0; ii<nmax-nmin+1; ii++)
  310. result_array[ii] = 0.0;
  311. /* Check for out of bounds parameters. */
  312. if (qq <= 0.0)
  313. {
  314. GSL_ERROR("q must be greater than zero", GSL_EINVAL);
  315. }
  316. if (kind < 1 || kind > 2)
  317. {
  318. GSL_ERROR("kind must be 1 or 2", GSL_EINVAL);
  319. }
  320. mm = 0;
  321. amax = 0.0;
  322. fn = 0.0;
  323. u1 = sqrt(qq)*exp(-1.0*zz);
  324. u2 = sqrt(qq)*exp(zz);
  325. /* Compute all eigenvalues up to nmax. */
  326. gsl_sf_mathieu_b_array(0, nmax, qq, work, bb);
  327. for (ii=0, order=nmin; order<=nmax; ii++, order++)
  328. {
  329. even_odd = 0;
  330. if (order % 2 != 0)
  331. even_odd = 1;
  332. /* Compute the series coefficients. */
  333. status = gsl_sf_mathieu_b_coeff(order, qq, bb[order], coeff);
  334. if (status != GSL_SUCCESS)
  335. {
  336. return status;
  337. }
  338. if (even_odd == 0)
  339. {
  340. for (kk=0; kk<GSL_SF_MATHIEU_COEFF; kk++)
  341. {
  342. amax = GSL_MAX(amax, fabs(coeff[kk]));
  343. if (fabs(coeff[kk])/amax < maxerr)
  344. break;
  345. j1mc = gsl_sf_bessel_Jn(kk, u1);
  346. j1pc = gsl_sf_bessel_Jn(kk+2, u1);
  347. if (kind == 1)
  348. {
  349. z2mc = gsl_sf_bessel_Jn(kk, u2);
  350. z2pc = gsl_sf_bessel_Jn(kk+2, u2);
  351. }
  352. else /* kind = 2 */
  353. {
  354. z2mc = gsl_sf_bessel_Yn(kk, u2);
  355. z2pc = gsl_sf_bessel_Yn(kk+2, u2);
  356. }
  357. fc = pow(-1.0, 0.5*order+kk+1)*coeff[kk];
  358. fn += fc*(j1mc*z2pc - j1pc*z2mc);
  359. }
  360. fn *= sqrt(pi/2.0)/coeff[0];
  361. }
  362. else
  363. {
  364. for (kk=0; kk<GSL_SF_MATHIEU_COEFF; kk++)
  365. {
  366. amax = GSL_MAX(amax, fabs(coeff[kk]));
  367. if (fabs(coeff[kk])/amax < maxerr)
  368. break;
  369. j1c = gsl_sf_bessel_Jn(kk, u1);
  370. j1pc = gsl_sf_bessel_Jn(kk+1, u1);
  371. if (kind == 1)
  372. {
  373. z2c = gsl_sf_bessel_Jn(kk, u2);
  374. z2pc = gsl_sf_bessel_Jn(kk+1, u2);
  375. }
  376. else /* kind = 2 */
  377. {
  378. z2c = gsl_sf_bessel_Yn(kk, u2);
  379. z2pc = gsl_sf_bessel_Yn(kk+1, u2);
  380. }
  381. fc = pow(-1.0, 0.5*(order-1)+kk)*coeff[kk];
  382. fn += fc*(j1c*z2pc - j1pc*z2c);
  383. }
  384. fn *= sqrt(pi/2.0)/coeff[0];
  385. }
  386. result_array[ii] = fn;
  387. } /* order loop */
  388. return GSL_SUCCESS;
  389. }