3 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
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.
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.
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.
20 /* Author: G. Jungman */
23 #include <gsl/gsl_math.h>
24 #include <gsl/gsl_errno.h>
25 #include <gsl/gsl_sf_pow_int.h>
26 #include <gsl/gsl_sf_elljac.h>
29 /* GJ: See [Thompson, Atlas for Computing Mathematical Functions] */
31 /* BJG 2005-07: New algorithm based on Algorithm 5 from Numerische
32 Mathematik 7, 78-90 (1965) "Numerical Calculation of Elliptic
33 Integrals and Elliptic Functions" R. Bulirsch.
35 Minor tweak is to avoid division by zero when sin(x u_l) = 0 by
36 computing reflected values sn(K-u) cn(K-u) dn(K-u) and using
37 transformation from Abramowitz & Stegun table 16.8 column "K-u"*/
40 gsl_sf_elljac_e(double u, double m, double * sn, double * cn, double * dn)
46 GSL_ERROR ("|m| > 1.0", GSL_EDOM);
48 else if(fabs(m) < 2.0*GSL_DBL_EPSILON) {
54 else if(fabs(m - 1.0) < 2.0*GSL_DBL_EPSILON) {
61 int status = GSL_SUCCESS;
67 double sin_umu, cos_umu, t, r;
71 nu[0] = sqrt(1.0 - m);
73 while( fabs(mu[n] - nu[n]) > 4.0 * GSL_DBL_EPSILON * fabs(mu[n]+nu[n])) {
74 mu[n+1] = 0.5 * (mu[n] + nu[n]);
75 nu[n+1] = sqrt(mu[n] * nu[n]);
78 status = GSL_EMAXITER;
83 sin_umu = sin(u * mu[n]);
84 cos_umu = cos(u * mu[n]);
86 /* Since sin(u*mu(n)) can be zero we switch to computing sn(K-u),
87 cn(K-u), dn(K-u) when |sin| < |cos| */
89 if (fabs(sin_umu) < fabs(cos_umu))
91 t = sin_umu / cos_umu;
98 c[n] = d[n+1] * c[n+1];
99 r = (c[n+1] * c[n+1]) / mu[n+1];
100 d[n] = (r + nu[n]) / (r + mu[n]);
103 *dn = sqrt(1.0-m) / d[n];
104 *cn = (*dn) * GSL_SIGN(cos_umu) / gsl_hypot(1.0, c[n]);
105 *sn = (*cn) * c[n] /sqrt(1.0-m);
109 t = cos_umu / sin_umu;
116 c[n] = d[n+1] * c[n+1];
117 r = (c[n+1] * c[n+1]) / mu[n+1];
118 d[n] = (r + nu[n]) / (r + mu[n]);
122 *sn = GSL_SIGN(sin_umu) / gsl_hypot(1.0, c[n]);