GeographicLib 2.5.2

Loading...
Searching...
No Matches
EllipticFunction.cpp
Go to the documentation of this file.
1/**
2 * \file EllipticFunction.cpp
3 * \brief Implementation for GeographicLib::EllipticFunction class
4 *
5 * Copyright (c) Charles Karney (2008-2024) <karney@alum.mit.edu> and licensed
6 * under the MIT/X11 License. For more information, see
7 * https://geographiclib.sourceforge.io/
8 **********************************************************************/
9
10#include <GeographicLib/EllipticFunction.hpp>
11
12 namespace GeographicLib {
13
14 using namespace std;
15
16 /*
17 * Implementation of methods given in
18 *
19 * B. C. Carlson
20 * Computation of elliptic integrals
21 * Numerical Algorithms 10, 13-26 (1995)
22 */
23
24 Math::real EllipticFunction::RF(real x, real y, real z) {
25 // Carlson, eqs 2.2 - 2.7
26 static const real tolRF =
27 pow(3 * numeric_limits<real>::epsilon() * real(0.01), 1/real(8));
28 real
29 A0 = (x + y + z)/3,
30 An = A0,
31 Q = fmax(fmax(fabs(A0-x), fabs(A0-y)), fabs(A0-z)) / tolRF,
32 x0 = x,
33 y0 = y,
34 z0 = z,
35 mul = 1;
36 while (Q >= mul * fabs(An)) {
37 // Max 6 trips
38 real lam = sqrt(x0)*sqrt(y0) + sqrt(y0)*sqrt(z0) + sqrt(z0)*sqrt(x0);
39 An = (An + lam)/4;
40 x0 = (x0 + lam)/4;
41 y0 = (y0 + lam)/4;
42 z0 = (z0 + lam)/4;
43 mul *= 4;
44 }
45 real
46 X = (A0 - x) / (mul * An),
47 Y = (A0 - y) / (mul * An),
48 Z = - (X + Y),
49 E2 = X*Y - Z*Z,
50 E3 = X*Y*Z;
51 // https://dlmf.nist.gov/19.36.E1
52 // Polynomial is
53 // (1 - E2/10 + E3/14 + E2^2/24 - 3*E2*E3/44
54 // - 5*E2^3/208 + 3*E3^2/104 + E2^2*E3/16)
55 // convert to Horner form...
56 return (E3 * (6930 * E3 + E2 * (15015 * E2 - 16380) + 17160) +
57 E2 * ((10010 - 5775 * E2) * E2 - 24024) + 240240) /
58 (240240 * sqrt(An));
59 }
60
61 Math::real EllipticFunction::RF(real x, real y) {
62 // Carlson, eqs 2.36 - 2.38
63 static const real tolRG0 =
64 real(2.7) * sqrt((numeric_limits<real>::epsilon() * real(0.01)));
65 real xn = sqrt(x), yn = sqrt(y);
66 if (xn < yn) swap(xn, yn);
67 while (fabs(xn-yn) > tolRG0 * xn) {
68 // Max 4 trips
69 real t = (xn + yn) /2;
70 yn = sqrt(xn * yn);
71 xn = t;
72 }
73 return Math::pi() / (xn + yn);
74 }
75
76 Math::real EllipticFunction::RC(real x, real y) {
77 // Defined only for y != 0 and x >= 0.
78 return ( !(x >= y) ? // x < y and catch nans
79 // https://dlmf.nist.gov/19.2.E18
80 atan(sqrt((y - x) / x)) / sqrt(y - x) :
81 ( x == y ? 1 / sqrt(y) :
82 asinh( y > 0 ?
83 // https://dlmf.nist.gov/19.2.E19
84 // atanh(sqrt((x - y) / x))
85 sqrt((x - y) / y) :
86 // https://dlmf.nist.gov/19.2.E20
87 // atanh(sqrt(x / (x - y)))
88 sqrt(-x / y) ) / sqrt(x - y) ) );
89 }
90
91 Math::real EllipticFunction::RG(real x, real y, real z) {
92 return (x == 0 ? RG(y, z) :
93 (y == 0 ? RG(z, x) :
94 (z == 0 ? RG(x, y) :
95 // Carlson, eq 1.7
96 (z * RF(x, y, z) - (x-z) * (y-z) * RD(x, y, z) / 3
97 + sqrt(x * y / z)) / 2 )));
98 }
99
100 Math::real EllipticFunction::RG(real x, real y) {
101 // Carlson, eqs 2.36 - 2.39
102 static const real tolRG0 =
103 real(2.7) * sqrt((numeric_limits<real>::epsilon() * real(0.01)));
104 real
105 x0 = sqrt(fmax(x, y)),
106 y0 = sqrt(fmin(x, y)),
107 xn = x0,
108 yn = y0,
109 s = 0,
110 mul = real(0.25);
111 while (fabs(xn-yn) > tolRG0 * xn) {
112 // Max 4 trips
113 real t = (xn + yn) /2;
114 yn = sqrt(xn * yn);
115 xn = t;
116 mul *= 2;
117 t = xn - yn;
118 s += mul * t * t;
119 }
120 return (Math::sq( (x0 + y0)/2 ) - s) * Math::pi() / (2 * (xn + yn));
121 }
122
123 Math::real EllipticFunction::RJ(real x, real y, real z, real p) {
124 // Carlson, eqs 2.17 - 2.25
125 static const real
126 tolRD = pow(real(0.2) * (numeric_limits<real>::epsilon() * real(0.01)),
127 1/real(8));
128 real
129 A0 = (x + y + z + 2*p)/5,
130 An = A0,
131 delta = (p-x) * (p-y) * (p-z),
132 Q = fmax(fmax(fabs(A0-x), fabs(A0-y)),
133 fmax(fabs(A0-z), fabs(A0-p))) / tolRD,
134 x0 = x,
135 y0 = y,
136 z0 = z,
137 p0 = p,
138 mul = 1,
139 mul3 = 1,
140 s = 0;
141 while (Q >= mul * fabs(An)) {
142 // Max 7 trips
143 real
144 lam = sqrt(x0)*sqrt(y0) + sqrt(y0)*sqrt(z0) + sqrt(z0)*sqrt(x0),
145 d0 = (sqrt(p0)+sqrt(x0)) * (sqrt(p0)+sqrt(y0)) * (sqrt(p0)+sqrt(z0)),
146 e0 = delta/(mul3 * Math::sq(d0));
147 s += RC(1, 1 + e0)/(mul * d0);
148 An = (An + lam)/4;
149 x0 = (x0 + lam)/4;
150 y0 = (y0 + lam)/4;
151 z0 = (z0 + lam)/4;
152 p0 = (p0 + lam)/4;
153 mul *= 4;
154 mul3 *= 64;
155 }
156 real
157 X = (A0 - x) / (mul * An),
158 Y = (A0 - y) / (mul * An),
159 Z = (A0 - z) / (mul * An),
160 P = -(X + Y + Z) / 2,
161 E2 = X*Y + X*Z + Y*Z - 3*P*P,
162 E3 = X*Y*Z + 2*P * (E2 + 2*P*P),
163 E4 = (2*X*Y*Z + P * (E2 + 3*P*P)) * P,
164 E5 = X*Y*Z*P*P;
165 // https://dlmf.nist.gov/19.36.E2
166 // Polynomial is
167 // (1 - 3*E2/14 + E3/6 + 9*E2^2/88 - 3*E4/22 - 9*E2*E3/52 + 3*E5/26
168 // - E2^3/16 + 3*E3^2/40 + 3*E2*E4/20 + 45*E2^2*E3/272
169 // - 9*(E3*E4+E2*E5)/68)
170 return ((471240 - 540540 * E2) * E5 +
171 (612612 * E2 - 540540 * E3 - 556920) * E4 +
172 E3 * (306306 * E3 + E2 * (675675 * E2 - 706860) + 680680) +
173 E2 * ((417690 - 255255 * E2) * E2 - 875160) + 4084080) /
174 (4084080 * mul * An * sqrt(An)) + 6 * s;
175 }
176
177 Math::real EllipticFunction::RD(real x, real y, real z) {
178 // Carlson, eqs 2.28 - 2.34
179 static const real
180 tolRD = pow(real(0.2) * (numeric_limits<real>::epsilon() * real(0.01)),
181 1/real(8));
182 real
183 A0 = (x + y + 3*z)/5,
184 An = A0,
185 Q = fmax(fmax(fabs(A0-x), fabs(A0-y)), fabs(A0-z)) / tolRD,
186 x0 = x,
187 y0 = y,
188 z0 = z,
189 mul = 1,
190 s = 0;
191 while (Q >= mul * fabs(An)) {
192 // Max 7 trips
193 real lam = sqrt(x0)*sqrt(y0) + sqrt(y0)*sqrt(z0) + sqrt(z0)*sqrt(x0);
194 s += 1/(mul * sqrt(z0) * (z0 + lam));
195 An = (An + lam)/4;
196 x0 = (x0 + lam)/4;
197 y0 = (y0 + lam)/4;
198 z0 = (z0 + lam)/4;
199 mul *= 4;
200 }
201 real
202 X = (A0 - x) / (mul * An),
203 Y = (A0 - y) / (mul * An),
204 Z = -(X + Y) / 3,
205 E2 = X*Y - 6*Z*Z,
206 E3 = (3*X*Y - 8*Z*Z)*Z,
207 E4 = 3 * (X*Y - Z*Z) * Z*Z,
208 E5 = X*Y*Z*Z*Z;
209 // https://dlmf.nist.gov/19.36.E2
210 // Polynomial is
211 // (1 - 3*E2/14 + E3/6 + 9*E2^2/88 - 3*E4/22 - 9*E2*E3/52 + 3*E5/26
212 // - E2^3/16 + 3*E3^2/40 + 3*E2*E4/20 + 45*E2^2*E3/272
213 // - 9*(E3*E4+E2*E5)/68)
214 return ((471240 - 540540 * E2) * E5 +
215 (612612 * E2 - 540540 * E3 - 556920) * E4 +
216 E3 * (306306 * E3 + E2 * (675675 * E2 - 706860) + 680680) +
217 E2 * ((417690 - 255255 * E2) * E2 - 875160) + 4084080) /
218 (4084080 * mul * An * sqrt(An)) + 3 * s;
219 }
220
221 void EllipticFunction::Reset(real k2, real alpha2,
222 real kp2, real alphap2) {
223 // Accept nans here (needed for GeodesicExact)
224 if (k2 > 1)
225 throw GeographicErr("Parameter k2 is not in (-inf, 1]");
226 if (alpha2 > 1)
227 throw GeographicErr("Parameter alpha2 is not in (-inf, 1]");
228 if (kp2 < 0)
229 throw GeographicErr("Parameter kp2 is not in [0, inf)");
230 if (alphap2 < 0)
231 throw GeographicErr("Parameter alphap2 is not in [0, inf)");
232 _k2 = k2;
233 _kp2 = kp2;
234 _alpha2 = alpha2;
235 _alphap2 = alphap2;
236 _eps = _k2/Math::sq(sqrt(_kp2) + 1);
237 // Values of complete elliptic integrals for k = 0,1 and alpha = 0,1
238 // K E D
239 // k = 0: pi/2 pi/2 pi/4
240 // k = 1: inf 1 inf
241 // Pi G H
242 // k = 0, alpha = 0: pi/2 pi/2 pi/4
243 // k = 1, alpha = 0: inf 1 1
244 // k = 0, alpha = 1: inf inf pi/2
245 // k = 1, alpha = 1: inf inf inf
246 //
247 // Pi(0, k) = K(k)
248 // G(0, k) = E(k)
249 // H(0, k) = K(k) - D(k)
250 // Pi(0, k) = K(k)
251 // G(0, k) = E(k)
252 // H(0, k) = K(k) - D(k)
253 // Pi(alpha2, 0) = pi/(2*sqrt(1-alpha2))
254 // G(alpha2, 0) = pi/(2*sqrt(1-alpha2))
255 // H(alpha2, 0) = pi/(2*(1 + sqrt(1-alpha2)))
256 // Pi(alpha2, 1) = inf
257 // H(1, k) = K(k)
258 // G(alpha2, 1) = H(alpha2, 1) = RC(1, alphap2)
259 if (_k2 != 0) {
260 // Complete elliptic integral K(k), Carlson eq. 4.1
261 // https://dlmf.nist.gov/19.25.E1
262 _kKc = _kp2 != 0 ? RF(_kp2, 1) : Math::infinity();
263 // Complete elliptic integral E(k), Carlson eq. 4.2
264 // https://dlmf.nist.gov/19.25.E1
265 _eEc = _kp2 != 0 ? 2 * RG(_kp2, 1) : 1;
266 // D(k) = (K(k) - E(k))/k^2, Carlson eq.4.3
267 // https://dlmf.nist.gov/19.25.E1
268 _dDc = _kp2 != 0 ? RD(0, _kp2, 1) / 3 : Math::infinity();
269 } else {
270 _kKc = _eEc = Math::pi()/2; _dDc = _kKc/2;
271 }
272 if (_alpha2 != 0) {
273 // https://dlmf.nist.gov/19.25.E2
274 real rj = (_kp2 != 0 && _alphap2 != 0) ? RJ(0, _kp2, 1, _alphap2) :
275 Math::infinity(),
276 // Only use rc if _kp2 = 0.
277 rc = _kp2 != 0 ? 0 :
278 (_alphap2 != 0 ? RC(1, _alphap2) : Math::infinity());
279 // Pi(alpha^2, k)
280 _pPic = _kp2 != 0 ? _kKc + _alpha2 * rj / 3 : Math::infinity();
281 // G(alpha^2, k)
282 _gGc = _kp2 != 0 ? _kKc + (_alpha2 - _k2) * rj / 3 : rc;
283 // H(alpha^2, k)
284 _hHc = _kp2 != 0 ? _kKc - (_alphap2 != 0 ? _alphap2 * rj : 0) / 3 : rc;
285 } else {
286 _pPic = _kKc; _gGc = _eEc;
287 // Hc = Kc - Dc but this involves large cancellations if k2 is close to
288 // 1. So write (for alpha2 = 0)
289 // Hc = int(cos(phi)^2/sqrt(1-k2*sin(phi)^2),phi,0,pi/2)
290 // = 1/sqrt(1-k2) * int(sin(phi)^2/sqrt(1-k2/kp2*sin(phi)^2,...)
291 // = 1/kp * D(i*k/kp)
292 // and use D(k) = RD(0, kp2, 1) / 3
293 // so Hc = 1/kp * RD(0, 1/kp2, 1) / 3
294 // = kp2 * RD(0, 1, kp2) / 3
295 // using https://dlmf.nist.gov/19.20.E18
296 // Equivalently
297 // RF(x, 1) - RD(0, x, 1)/3 = x * RD(0, 1, x)/3 for x > 0
298 // For k2 = 0 and alpha2 = 0, we have
299 // Hc = int(cos(phi)^2,...) = pi/4
300 // For k2 = 1 and alpha2 = 0, we have
301 // Hc = int(cos(phi),...) = 1
302 _hHc = _kp2 == 1 ? Math::pi()/4 :
303 (_kp2 == 0 ? 1 : _kp2 * RD(0, 1, _kp2) / 3);
304 }
305 }
306
307 /*
308 * Implementation of methods given in
309 *
310 * R. Bulirsch
311 * Numerical Calculation of Elliptic Integrals and Elliptic Functions
312 * Numericshe Mathematik 7, 78-90 (1965)
313 */
314
315 void EllipticFunction::sncndn(real x, real& sn, real& cn, real& dn) const {
316 // Bulirsch's sncndn routine, p 89.
317 static const real tolJAC =
318 sqrt(numeric_limits<real>::epsilon() * real(0.01));
319 if (_kp2 != 0) {
320 real mc = _kp2, d = 0;
321 if (signbit(_kp2)) {
322 // This implements DLMF Eqs 22.17.2 - 22.17.4. But this only
323 // accomodates kp2 < 0 or k2 > 1 and these are outside the advertized
324 // ranges for the contructor for this class.
325 d = 1 - mc;
326 mc /= -d;
327 d = sqrt(d);
328 x *= d;
329 }
330 real c = 0; // To suppress warning about uninitialized variable
331 real m[num_], n[num_];
332 unsigned l = 0;
333 for (real a = 1;
334 l < num_ ||
335 GEOGRAPHICLIB_PANIC("Convergence failure in EllipticFunction");
336 ++l) {
337 // This converges quadratically. Max 5 trips
338 m[l] = a;
339 n[l] = mc = sqrt(mc);
340 c = (a + mc) / 2;
341 if (!(fabs(a - mc) > tolJAC * a)) {
342 ++l;
343 break;
344 }
345 mc *= a;
346 a = c;
347 }
348 x *= c;
349 sn = sin(x);
350 cn = cos(x);
351 dn = 1;
352 if (sn != 0) {
353 real a = cn / sn;
354 c *= a;
355 while (l--) {
356 real b = m[l];
357 a *= c;
358 c *= dn;
359 dn = (n[l] + a) / (b + a);
360 a = c / b;
361 }
362 a = 1 / sqrt(c*c + 1);
363 sn = signbit(sn) ? -a : a;
364 cn = c * sn;
365 if (signbit(_kp2)) {
366 // See DLMF Eqs 22.17.2 - 22.17.4
367 swap(cn, dn);
368 sn /= d;
369 }
370 }
371 } else {
372 sn = tanh(x);
373 dn = cn = 1 / cosh(x);
374 }
375 }
376
377 Math::real EllipticFunction::am(real x) const {
378 // This implements DLMF Sec 22.20(ii).
379 // See also Sala (1989), https://doi.org/10.1137/0520100, Sec 5.
380 static const real tolJAC =
381 pow(numeric_limits<real>::epsilon(), real(0.75));
382 real k2 = _k2, kp2 = _kp2;
383 if (_k2 == 0)
384 return x;
385 else if (_kp2 == 0) {
386 return atan(sinh(x)); // gd(x)
387 } else if (_k2 < 0) {
388 // Sala Eq. 5.8
389 k2 = -_k2 / _kp2; kp2 = 1 / _kp2;
390 x *= sqrt(_kp2);
391 }
392 real a[num_], b, c[num_];
393 a[0] = 1; b = sqrt(kp2); c[0] = sqrt(k2);
394 int l = 1;
395 for (; l < num_ ||
396 GEOGRAPHICLIB_PANIC("Convergence failure in EllipticFunction");) {
397 a[l] = (a[l-1] + b) / 2;
398 c[l] = (a[l-1] - b) / 2;
399 b = sqrt(a[l-1] * b);
400 if (!(c[l] > tolJAC * a[l])) break;
401 ++l;
402 }
403 // Now a[l] = pi/(2*K)
404 // Need to initialize phi1 to stop Visual Studio complaining
405 real phi = a[l] * x * real(1 << l), phi1 = 0;
406 for (; l > 0; --l) {
407 phi1 = phi;
408 phi = (phi + asin(c[l] * sin(phi) / a[l])) / 2;
409 }
410 // For k2 < 0, see Sala Eq. 5.8
411 return _k2 < 0 ? phi1 - phi : phi;
412 }
413
414 Math::real EllipticFunction::am(real x, real& sn, real& cn, real& dn) const {
415 real phi = am(x);
416 if (_kp2 == 0) {
417 // Could rely on sin(gd(x)) = tanh(x) and cos(gd(x)) = 1 / cosh(x). But
418 // this is more accurate for large |x|.
419 sn = tanh(x); cn = dn = 1 / cosh(x);
420 } else {
421 sn = sin(phi); cn = cos(phi);
422 // See comment following DLMF Eq. 22.20.5
423 // dn = cn / cos(phi1 - phi)
424 dn = Delta(sn, cn);
425 }
426 return phi;
427 }
428
429 Math::real EllipticFunction::F(real sn, real cn, real dn) const {
430 // Carlson, eq. 4.5 and
431 // https://dlmf.nist.gov/19.25.E5
432 real cn2 = cn*cn, dn2 = dn*dn,
433 fi = cn2 != 0 ? fabs(sn) * RF(cn2, dn2, 1) : K();
434 // Enforce usual trig-like symmetries
435 if (signbit(cn))
436 fi = 2 * K() - fi;
437 return copysign(fi, sn);
438 }
439
440 Math::real EllipticFunction::E(real sn, real cn, real dn) const {
441 real
442 cn2 = cn*cn, dn2 = dn*dn, sn2 = sn*sn,
443 ei = cn2 != 0 ?
444 fabs(sn) * ( _k2 <= 0 ?
445 // Carlson, eq. 4.6 and
446 // https://dlmf.nist.gov/19.25.E9
447 RF(cn2, dn2, 1) - _k2 * sn2 * RD(cn2, dn2, 1) / 3 :
448 ( _kp2 >= 0 ?
449 // https://dlmf.nist.gov/19.25.E10
450 _kp2 * RF(cn2, dn2, 1) +
451 _k2 * _kp2 * sn2 * RD(cn2, 1, dn2) / 3 +
452 _k2 * fabs(cn) / dn :
453 // https://dlmf.nist.gov/19.25.E11
454 - _kp2 * sn2 * RD(dn2, 1, cn2) / 3 +
455 dn / fabs(cn) ) ) :
456 E();
457 // Enforce usual trig-like symmetries
458 if (signbit(cn))
459 ei = 2 * E() - ei;
460 return copysign(ei, sn);
461 }
462
463 Math::real EllipticFunction::D(real sn, real cn, real dn) const {
464 // Carlson, eq. 4.8 and
465 // https://dlmf.nist.gov/19.25.E13
466 real
467 cn2 = cn*cn, dn2 = dn*dn, sn2 = sn*sn,
468 di = cn2 != 0 ? fabs(sn) * sn2 * RD(cn2, dn2, 1) / 3 : D();
469 // Enforce usual trig-like symmetries
470 if (signbit(cn))
471 di = 2 * D() - di;
472 return copysign(di, sn);
473 }
474
475 Math::real EllipticFunction::Pi(real sn, real cn, real dn) const {
476 // Carlson, eq. 4.7 and
477 // https://dlmf.nist.gov/19.25.E14
478 real
479 cn2 = cn*cn, dn2 = dn*dn, sn2 = sn*sn,
480 pii = cn2 != 0 ? fabs(sn) * (RF(cn2, dn2, 1) +
481 _alpha2 * sn2 *
482 RJ(cn2, dn2, 1, cn2 + _alphap2 * sn2) / 3) :
483 Pi();
484 // Enforce usual trig-like symmetries
485 if (signbit(cn))
486 pii = 2 * Pi() - pii;
487 return copysign(pii, sn);
488 }
489
490 Math::real EllipticFunction::G(real sn, real cn, real dn) const {
491 real
492 cn2 = cn*cn, dn2 = dn*dn, sn2 = sn*sn,
493 gi = cn2 != 0 ? fabs(sn) * (RF(cn2, dn2, 1) +
494 (_alpha2 - _k2) * sn2 *
495 RJ(cn2, dn2, 1, cn2 + _alphap2 * sn2) / 3) :
496 G();
497 // Enforce usual trig-like symmetries
498 if (signbit(cn))
499 gi = 2 * G() - gi;
500 return copysign(gi, sn);
501 }
502
503 Math::real EllipticFunction::H(real sn, real cn, real dn) const {
504 real
505 cn2 = cn*cn, dn2 = dn*dn, sn2 = sn*sn,
506 // WARNING: large cancellation if k2 = 1, alpha2 = 0, and phi near pi/2
507 hi = cn2 != 0 ? fabs(sn) * (RF(cn2, dn2, 1) -
508 _alphap2 * sn2 *
509 RJ(cn2, dn2, 1, cn2 + _alphap2 * sn2) / 3) :
510 H();
511 // Enforce usual trig-like symmetries
512 if (signbit(cn))
513 hi = 2 * H() - hi;
514 return copysign(hi, sn);
515 }
516
517 Math::real EllipticFunction::deltaF(real sn, real cn, real dn) const {
518 // Function is periodic with period pi
519 if (signbit(cn)) { cn = -cn; sn = -sn; }
520 return F(sn, cn, dn) * (Math::pi()/2) / K() - atan2(sn, cn);
521 }
522
523 Math::real EllipticFunction::deltaE(real sn, real cn, real dn) const {
524 // Function is periodic with period pi
525 if (signbit(cn)) { cn = -cn; sn = -sn; }
526 return E(sn, cn, dn) * (Math::pi()/2) / E() - atan2(sn, cn);
527 }
528
529 Math::real EllipticFunction::deltaPi(real sn, real cn, real dn) const {
530 // Function is periodic with period pi
531 if (signbit(cn)) { cn = -cn; sn = -sn; }
532 return Pi(sn, cn, dn) * (Math::pi()/2) / Pi() - atan2(sn, cn);
533 }
534
535 Math::real EllipticFunction::deltaD(real sn, real cn, real dn) const {
536 // Function is periodic with period pi
537 if (signbit(cn)) { cn = -cn; sn = -sn; }
538 return D(sn, cn, dn) * (Math::pi()/2) / D() - atan2(sn, cn);
539 }
540
541 Math::real EllipticFunction::deltaG(real sn, real cn, real dn) const {
542 // Function is periodic with period pi
543 if (signbit(cn)) { cn = -cn; sn = -sn; }
544 return G(sn, cn, dn) * (Math::pi()/2) / G() - atan2(sn, cn);
545 }
546
547 Math::real EllipticFunction::deltaH(real sn, real cn, real dn) const {
548 // Function is periodic with period pi
549 if (signbit(cn)) { cn = -cn; sn = -sn; }
550 return H(sn, cn, dn) * (Math::pi()/2) / H() - atan2(sn, cn);
551 }
552
553 Math::real EllipticFunction::F(real phi) const {
554 if (_k2 == 0)
555 return phi;
556 else if (_kp2 == 0)
557 return asinh(tan(phi));
558 real sn = sin(phi), cn = cos(phi), dn = Delta(sn, cn);
559 return fabs(phi) < Math::pi() ? F(sn, cn, dn) :
560 (deltaF(sn, cn, dn) + phi) * K() / (Math::pi()/2);
561 }
562
563 Math::real EllipticFunction::E(real phi) const {
564 if (_k2 == 0)
565 return phi;
566 // else if (_kp2 == 0)
567 // Despite DLMF Eq 19.6.9 this is probably wrong, since
568 // sqrt(1 - k^2*sin(phi)^2) -> abs(cos(phi)) in the limit k -> 1.
569 // return sin(phi);
570 real sn = sin(phi), cn = cos(phi), dn = Delta(sn, cn);
571 return fabs(phi) < Math::pi() ? E(sn, cn, dn) :
572 (deltaE(sn, cn, dn) + phi) * E() / (Math::pi()/2);
573 }
574
575 Math::real EllipticFunction::Ed(real ang) const {
576 // ang - Math::AngNormalize(ang) is (nearly) an exact multiple of 360
577 real n = round((ang - Math::AngNormalize(ang))/Math::td);
578 real sn, cn;
579 Math::sincosd(ang, sn, cn);
580 return E(sn, cn, Delta(sn, cn)) + 4 * E() * n;
581 }
582
583 Math::real EllipticFunction::Pi(real phi) const {
584 real sn = sin(phi), cn = cos(phi), dn = Delta(sn, cn);
585 return fabs(phi) < Math::pi() ? Pi(sn, cn, dn) :
586 (deltaPi(sn, cn, dn) + phi) * Pi() / (Math::pi()/2);
587 }
588
589 Math::real EllipticFunction::D(real phi) const {
590 real sn = sin(phi), cn = cos(phi), dn = Delta(sn, cn);
591 return fabs(phi) < Math::pi() ? D(sn, cn, dn) :
592 (deltaD(sn, cn, dn) + phi) * D() / (Math::pi()/2);
593 }
594
595 Math::real EllipticFunction::G(real phi) const {
596 real sn = sin(phi), cn = cos(phi), dn = Delta(sn, cn);
597 return fabs(phi) < Math::pi() ? G(sn, cn, dn) :
598 (deltaG(sn, cn, dn) + phi) * G() / (Math::pi()/2);
599 }
600
601 Math::real EllipticFunction::H(real phi) const {
602 real sn = sin(phi), cn = cos(phi), dn = Delta(sn, cn);
603 return fabs(phi) < Math::pi() ? H(sn, cn, dn) :
604 (deltaH(sn, cn, dn) + phi) * H() / (Math::pi()/2);
605 }
606
607 Math::real EllipticFunction::Einv(real x) const {
608 static const real tolJAC =
609 sqrt(numeric_limits<real>::epsilon() * real(0.01));
610 real n = floor(x / (2 * _eEc) + real(0.5));
611 x -= 2 * _eEc * n; // x now in [-ec, ec)
612 // Linear approximation
613 real phi = Math::pi() * x / (2 * _eEc); // phi in [-pi/2, pi/2)
614 // First order correction
615 phi -= _eps * sin(2 * phi) / 2;
616 // For kp2 close to zero use asin(x/_eEc) or
617 // J. P. Boyd, Applied Math. and Computation 218, 7005-7013 (2012)
618 // https://doi.org/10.1016/j.amc.2011.12.021
619 for (int i = 0;
620 i < num_ ||
621 GEOGRAPHICLIB_PANIC("Convergence failure in EllipticFunction");
622 ++i) {
623 real
624 sn = sin(phi),
625 cn = cos(phi),
626 dn = Delta(sn, cn),
627 err = (E(sn, cn, dn) - x)/dn;
628 phi -= err;
629 if (!(fabs(err) > tolJAC))
630 break;
631 }
632 return n * Math::pi() + phi;
633 }
634
635 Math::real EllipticFunction::deltaEinv(real stau, real ctau) const {
636 // Function is periodic with period pi
637 if (signbit(ctau)) { ctau = -ctau; stau = -stau; }
638 real tau = atan2(stau, ctau);
639 return Einv( tau * E() / (Math::pi()/2) ) - tau;
640 }
641
642} // namespace GeographicLib
EllipticFunction.hpp
Header for GeographicLib::EllipticFunction class.
GEOGRAPHICLIB_PANIC
#define GEOGRAPHICLIB_PANIC(msg)
Definition Math.hpp:67
GeographicLib::EllipticFunction::G
Math::real G() const
Definition EllipticFunction.hpp:251
GeographicLib::EllipticFunction::sncndn
void sncndn(real x, real &sn, real &cn, real &dn) const
Definition EllipticFunction.cpp:315
GeographicLib::EllipticFunction::RJ
static real RJ(real x, real y, real z, real p)
Definition EllipticFunction.cpp:123
GeographicLib::EllipticFunction::H
Math::real H() const
Definition EllipticFunction.hpp:265
GeographicLib::EllipticFunction::deltaG
Math::real deltaG(real sn, real cn, real dn) const
Definition EllipticFunction.cpp:541
GeographicLib::EllipticFunction::RG
static real RG(real x, real y, real z)
Definition EllipticFunction.cpp:91
GeographicLib::EllipticFunction::deltaE
Math::real deltaE(real sn, real cn, real dn) const
Definition EllipticFunction.cpp:523
GeographicLib::EllipticFunction::F
Math::real F(real phi) const
Definition EllipticFunction.cpp:553
GeographicLib::EllipticFunction::RC
static real RC(real x, real y)
Definition EllipticFunction.cpp:76
GeographicLib::EllipticFunction::Einv
Math::real Einv(real x) const
Definition EllipticFunction.cpp:607
GeographicLib::EllipticFunction::RD
static real RD(real x, real y, real z)
Definition EllipticFunction.cpp:177
GeographicLib::EllipticFunction::Pi
Math::real Pi() const
Definition EllipticFunction.hpp:238
GeographicLib::EllipticFunction::E
Math::real E() const
Definition EllipticFunction.hpp:203
GeographicLib::EllipticFunction::k2
Math::real k2() const
Definition EllipticFunction.hpp:158
GeographicLib::EllipticFunction::alphap2
Math::real alphap2() const
Definition EllipticFunction.hpp:175
GeographicLib::EllipticFunction::Reset
void Reset(real k2=0, real alpha2=0)
Definition EllipticFunction.hpp:126
GeographicLib::EllipticFunction::am
Math::real am(real x) const
Definition EllipticFunction.cpp:377
GeographicLib::EllipticFunction::Delta
Math::real Delta(real sn, real cn) const
Definition EllipticFunction.hpp:607
GeographicLib::EllipticFunction::deltaD
Math::real deltaD(real sn, real cn, real dn) const
Definition EllipticFunction.cpp:535
GeographicLib::EllipticFunction::Ed
Math::real Ed(real ang) const
Definition EllipticFunction.cpp:575
GeographicLib::EllipticFunction::K
Math::real K() const
Definition EllipticFunction.hpp:191
GeographicLib::EllipticFunction::kp2
Math::real kp2() const
Definition EllipticFunction.hpp:164
GeographicLib::EllipticFunction::deltaH
Math::real deltaH(real sn, real cn, real dn) const
Definition EllipticFunction.cpp:547
GeographicLib::EllipticFunction::deltaF
Math::real deltaF(real sn, real cn, real dn) const
Definition EllipticFunction.cpp:517
GeographicLib::EllipticFunction::RF
static real RF(real x, real y, real z)
Definition EllipticFunction.cpp:24
GeographicLib::EllipticFunction::deltaPi
Math::real deltaPi(real sn, real cn, real dn) const
Definition EllipticFunction.cpp:529
GeographicLib::EllipticFunction::deltaEinv
Math::real deltaEinv(real stau, real ctau) const
Definition EllipticFunction.cpp:635
GeographicLib::EllipticFunction::alpha2
Math::real alpha2() const
Definition EllipticFunction.hpp:169
GeographicLib::EllipticFunction::D
Math::real D() const
Definition EllipticFunction.hpp:216
GeographicLib::GeographicErr
Exception handling for GeographicLib.
Definition Constants.hpp:316
GeographicLib::Math::sincosd
static void sincosd(T x, T &sinx, T &cosx)
Definition Math.cpp:104
GeographicLib::Math::sq
static T sq(T x)
Definition Math.hpp:227
GeographicLib::Math::AngNormalize
static T AngNormalize(T x)
Definition Math.cpp:69
GeographicLib::Math::infinity
static T infinity()
Definition Math.cpp:295
GeographicLib::Math::td
static constexpr int td
degrees per turn
Definition Math.hpp:154
GeographicLib::Math::pi
static T pi()
Definition Math.hpp:205
GeographicLib::Math::real
double real
Definition Math.hpp:115
GeographicLib
Namespace for GeographicLib.
Definition Accumulator.cpp:12
std
Definition NearestNeighbor.hpp:806
std::swap
void swap(GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &a, GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &b)
Definition NearestNeighbor.hpp:819