gsl_specfunc__mathieu_angfunc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* specfunc/mathieu_angfunc.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 <stdio.h>
  23. #include <math.h>
  24. #include "gsl_math.h"
  25. #include "gsl_sf_mathieu.h"
  26. int gsl_sf_mathieu_ce(int order, double qq, double zz, gsl_sf_result *result)
  27. {
  28. int even_odd, ii, status;
  29. double coeff[GSL_SF_MATHIEU_COEFF], norm, fn, factor;
  30. gsl_sf_result aa;
  31. norm = 0.0;
  32. even_odd = 0;
  33. if (order % 2 != 0)
  34. even_odd = 1;
  35. /* Handle the trivial case where q = 0. */
  36. if (qq == 0.0)
  37. {
  38. norm = 1.0;
  39. if (order == 0)
  40. norm = sqrt(2.0);
  41. fn = cos(order*zz)/norm;
  42. result->val = fn;
  43. result->err = 2.0*GSL_DBL_EPSILON;
  44. factor = fabs(fn);
  45. if (factor > 1.0)
  46. result->err *= factor;
  47. return GSL_SUCCESS;
  48. }
  49. /* Use symmetry characteristics of the functions to handle cases with
  50. negative order. */
  51. if (order < 0)
  52. order *= -1;
  53. /* Compute the characteristic value. */
  54. status = gsl_sf_mathieu_a(order, qq, &aa);
  55. if (status != GSL_SUCCESS)
  56. {
  57. return status;
  58. }
  59. /* Compute the series coefficients. */
  60. status = gsl_sf_mathieu_a_coeff(order, qq, aa.val, coeff);
  61. if (status != GSL_SUCCESS)
  62. {
  63. return status;
  64. }
  65. if (even_odd == 0)
  66. {
  67. fn = 0.0;
  68. norm = coeff[0]*coeff[0];
  69. for (ii=0; ii<GSL_SF_MATHIEU_COEFF; ii++)
  70. {
  71. fn += coeff[ii]*cos(2.0*ii*zz);
  72. norm += coeff[ii]*coeff[ii];
  73. }
  74. }
  75. else
  76. {
  77. fn = 0.0;
  78. for (ii=0; ii<GSL_SF_MATHIEU_COEFF; ii++)
  79. {
  80. fn += coeff[ii]*cos((2.0*ii + 1.0)*zz);
  81. norm += coeff[ii]*coeff[ii];
  82. }
  83. }
  84. norm = sqrt(norm);
  85. fn /= norm;
  86. result->val = fn;
  87. result->err = 2.0*GSL_DBL_EPSILON;
  88. factor = fabs(fn);
  89. if (factor > 1.0)
  90. result->err *= factor;
  91. return GSL_SUCCESS;
  92. }
  93. int gsl_sf_mathieu_se(int order, double qq, double zz, gsl_sf_result *result)
  94. {
  95. int even_odd, ii, status;
  96. double coeff[GSL_SF_MATHIEU_COEFF], norm, fn, factor;
  97. gsl_sf_result aa;
  98. norm = 0.0;
  99. even_odd = 0;
  100. if (order % 2 != 0)
  101. even_odd = 1;
  102. /* Handle the trivial cases where order = 0 and/or q = 0. */
  103. if (order == 0)
  104. {
  105. result->val = 0.0;
  106. result->err = 0.0;
  107. return GSL_SUCCESS;
  108. }
  109. if (qq == 0.0)
  110. {
  111. norm = 1.0;
  112. fn = sin(order*zz);
  113. result->val = fn;
  114. result->err = 2.0*GSL_DBL_EPSILON;
  115. factor = fabs(fn);
  116. if (factor > 1.0)
  117. result->err *= factor;
  118. return GSL_SUCCESS;
  119. }
  120. /* Use symmetry characteristics of the functions to handle cases with
  121. negative order. */
  122. if (order < 0)
  123. order *= -1;
  124. /* Compute the characteristic value. */
  125. status = gsl_sf_mathieu_b(order, qq, &aa);
  126. if (status != GSL_SUCCESS)
  127. {
  128. return status;
  129. }
  130. /* Compute the series coefficients. */
  131. status = gsl_sf_mathieu_b_coeff(order, qq, aa.val, coeff);
  132. if (status != GSL_SUCCESS)
  133. {
  134. return status;
  135. }
  136. if (even_odd == 0)
  137. {
  138. fn = 0.0;
  139. for (ii=0; ii<GSL_SF_MATHIEU_COEFF; ii++)
  140. {
  141. norm += coeff[ii]*coeff[ii];
  142. fn += coeff[ii]*sin(2.0*(ii + 1)*zz);
  143. }
  144. }
  145. else
  146. {
  147. fn = 0.0;
  148. for (ii=0; ii<GSL_SF_MATHIEU_COEFF; ii++)
  149. {
  150. norm += coeff[ii]*coeff[ii];
  151. fn += coeff[ii]*sin((2.0*ii + 1)*zz);
  152. }
  153. }
  154. norm = sqrt(norm);
  155. fn /= norm;
  156. result->val = fn;
  157. result->err = 2.0*GSL_DBL_EPSILON;
  158. factor = fabs(fn);
  159. if (factor > 1.0)
  160. result->err *= factor;
  161. return GSL_SUCCESS;
  162. }
  163. int gsl_sf_mathieu_ce_array(int nmin, int nmax, double qq, double zz,
  164. gsl_sf_mathieu_workspace *work,
  165. double result_array[])
  166. {
  167. int even_odd, order, ii, jj, status;
  168. double coeff[GSL_SF_MATHIEU_COEFF], *aa = work->aa, norm;
  169. /* Initialize the result array to zeroes. */
  170. for (ii=0; ii<nmax-nmin+1; ii++)
  171. result_array[ii] = 0.0;
  172. /* Ensure that the workspace is large enough to accomodate. */
  173. if (work->size < (unsigned int)nmax)
  174. {
  175. GSL_ERROR("Work space not large enough", GSL_EINVAL);
  176. }
  177. if (nmin < 0 || nmax < nmin)
  178. {
  179. GSL_ERROR("domain error", GSL_EDOM);
  180. }
  181. /* Compute all of the eigenvalues up to nmax. */
  182. gsl_sf_mathieu_a_array(0, nmax, qq, work, aa);
  183. for (ii=0, order=nmin; order<=nmax; ii++, order++)
  184. {
  185. norm = 0.0;
  186. even_odd = 0;
  187. if (order % 2 != 0)
  188. even_odd = 1;
  189. /* Handle the trivial case where q = 0. */
  190. if (qq == 0.0)
  191. {
  192. norm = 1.0;
  193. if (order == 0)
  194. norm = sqrt(2.0);
  195. result_array[ii] = cos(order*zz)/norm;
  196. continue;
  197. }
  198. /* Compute the series coefficients. */
  199. status = gsl_sf_mathieu_a_coeff(order, qq, aa[order], coeff);
  200. if (status != GSL_SUCCESS)
  201. return status;
  202. if (even_odd == 0)
  203. {
  204. norm = coeff[0]*coeff[0];
  205. for (jj=0; jj<GSL_SF_MATHIEU_COEFF; jj++)
  206. {
  207. result_array[ii] += coeff[jj]*cos(2.0*jj*zz);
  208. norm += coeff[jj]*coeff[jj];
  209. }
  210. }
  211. else
  212. {
  213. for (jj=0; jj<GSL_SF_MATHIEU_COEFF; jj++)
  214. {
  215. result_array[ii] += coeff[jj]*cos((2.0*jj + 1.0)*zz);
  216. norm += coeff[jj]*coeff[jj];
  217. }
  218. }
  219. norm = sqrt(norm);
  220. result_array[ii] /= norm;
  221. }
  222. return GSL_SUCCESS;
  223. }
  224. int gsl_sf_mathieu_se_array(int nmin, int nmax, double qq, double zz,
  225. gsl_sf_mathieu_workspace *work,
  226. double result_array[])
  227. {
  228. int even_odd, order, ii, jj, status;
  229. double coeff[GSL_SF_MATHIEU_COEFF], *bb = work->bb, norm;
  230. /* Initialize the result array to zeroes. */
  231. for (ii=0; ii<nmax-nmin+1; ii++)
  232. result_array[ii] = 0.0;
  233. /* Ensure that the workspace is large enough to accomodate. */
  234. if (work->size < (unsigned int)nmax)
  235. {
  236. GSL_ERROR("Work space not large enough", GSL_EINVAL);
  237. }
  238. if (nmin < 0 || nmax < nmin)
  239. {
  240. GSL_ERROR("domain error", GSL_EDOM);
  241. }
  242. /* Compute all of the eigenvalues up to nmax. */
  243. gsl_sf_mathieu_b_array(0, nmax, qq, work, bb);
  244. for (ii=0, order=nmin; order<=nmax; ii++, order++)
  245. {
  246. norm = 0.0;
  247. even_odd = 0;
  248. if (order % 2 != 0)
  249. even_odd = 1;
  250. /* Handle the trivial case where q = 0. */
  251. if (qq == 0.0)
  252. {
  253. norm = 1.0;
  254. result_array[ii] = sin(order*zz);
  255. continue;
  256. }
  257. /* Compute the series coefficients. */
  258. status = gsl_sf_mathieu_b_coeff(order, qq, bb[order], coeff);
  259. if (status != GSL_SUCCESS)
  260. {
  261. return status;
  262. }
  263. if (even_odd == 0)
  264. {
  265. for (jj=0; jj<GSL_SF_MATHIEU_COEFF; jj++)
  266. {
  267. result_array[ii] += coeff[jj]*sin(2.0*(jj + 1)*zz);
  268. norm += coeff[jj]*coeff[jj];
  269. }
  270. }
  271. else
  272. {
  273. for (jj=0; jj<GSL_SF_MATHIEU_COEFF; jj++)
  274. {
  275. result_array[ii] += coeff[jj]*sin((2.0*jj + 1.0)*zz);
  276. norm += coeff[jj]*coeff[jj];
  277. }
  278. }
  279. norm = sqrt(norm);
  280. result_array[ii] /= norm;
  281. }
  282. return GSL_SUCCESS;
  283. }