gsl_specfunc__elljac.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* specfunc/elljac.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_pow_int.h"
  24. #include "gsl_sf_elljac.h"
  25. /* GJ: See [Thompson, Atlas for Computing Mathematical Functions] */
  26. /* BJG 2005-07: New algorithm based on Algorithm 5 from Numerische
  27. Mathematik 7, 78-90 (1965) "Numerical Calculation of Elliptic
  28. Integrals and Elliptic Functions" R. Bulirsch.
  29. Minor tweak is to avoid division by zero when sin(x u_l) = 0 by
  30. computing reflected values sn(K-u) cn(K-u) dn(K-u) and using
  31. transformation from Abramowitz & Stegun table 16.8 column "K-u"*/
  32. int
  33. gsl_sf_elljac_e(double u, double m, double * sn, double * cn, double * dn)
  34. {
  35. if(fabs(m) > 1.0) {
  36. *sn = 0.0;
  37. *cn = 0.0;
  38. *dn = 0.0;
  39. GSL_ERROR ("|m| > 1.0", GSL_EDOM);
  40. }
  41. else if(fabs(m) < 2.0*GSL_DBL_EPSILON) {
  42. *sn = sin(u);
  43. *cn = cos(u);
  44. *dn = 1.0;
  45. return GSL_SUCCESS;
  46. }
  47. else if(fabs(m - 1.0) < 2.0*GSL_DBL_EPSILON) {
  48. *sn = tanh(u);
  49. *cn = 1.0/cosh(u);
  50. *dn = *cn;
  51. return GSL_SUCCESS;
  52. }
  53. else {
  54. int status = GSL_SUCCESS;
  55. const int N = 16;
  56. double mu[16];
  57. double nu[16];
  58. double c[16];
  59. double d[16];
  60. double sin_umu, cos_umu, t, r;
  61. int n = 0;
  62. mu[0] = 1.0;
  63. nu[0] = sqrt(1.0 - m);
  64. while( fabs(mu[n] - nu[n]) > 4.0 * GSL_DBL_EPSILON * fabs(mu[n]+nu[n])) {
  65. mu[n+1] = 0.5 * (mu[n] + nu[n]);
  66. nu[n+1] = sqrt(mu[n] * nu[n]);
  67. ++n;
  68. if(n >= N - 1) {
  69. status = GSL_EMAXITER;
  70. break;
  71. }
  72. }
  73. sin_umu = sin(u * mu[n]);
  74. cos_umu = cos(u * mu[n]);
  75. /* Since sin(u*mu(n)) can be zero we switch to computing sn(K-u),
  76. cn(K-u), dn(K-u) when |sin| < |cos| */
  77. if (fabs(sin_umu) < fabs(cos_umu))
  78. {
  79. t = sin_umu / cos_umu;
  80. c[n] = mu[n] * t;
  81. d[n] = 1.0;
  82. while(n > 0) {
  83. n--;
  84. c[n] = d[n+1] * c[n+1];
  85. r = (c[n+1] * c[n+1]) / mu[n+1];
  86. d[n] = (r + nu[n]) / (r + mu[n]);
  87. }
  88. *dn = sqrt(1.0-m) / d[n];
  89. *cn = (*dn) * GSL_SIGN(cos_umu) / gsl_hypot(1.0, c[n]);
  90. *sn = (*cn) * c[n] /sqrt(1.0-m);
  91. }
  92. else
  93. {
  94. t = cos_umu / sin_umu;
  95. c[n] = mu[n] * t;
  96. d[n] = 1.0;
  97. while(n > 0) {
  98. --n;
  99. c[n] = d[n+1] * c[n+1];
  100. r = (c[n+1] * c[n+1]) / mu[n+1];
  101. d[n] = (r + nu[n]) / (r + mu[n]);
  102. }
  103. *dn = d[n];
  104. *sn = GSL_SIGN(sin_umu) / gsl_hypot(1.0, c[n]);
  105. *cn = c[n] * (*sn);
  106. }
  107. return status;
  108. }
  109. }