GeographicLib  1.43
Geodesic.cpp
Go to the documentation of this file.
1 /**
2  * \file Geodesic.cpp
3  * \brief Implementation for GeographicLib::Geodesic class
4  *
5  * Copyright (c) Charles Karney (2009-2015) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
8  *
9  * This is a reformulation of the geodesic problem. The notation is as
10  * follows:
11  * - at a general point (no suffix or 1 or 2 as suffix)
12  * - phi = latitude
13  * - beta = latitude on auxiliary sphere
14  * - omega = longitude on auxiliary sphere
15  * - lambda = longitude
16  * - alpha = azimuth of great circle
17  * - sigma = arc length along great circle
18  * - s = distance
19  * - tau = scaled distance (= sigma at multiples of pi/2)
20  * - at northwards equator crossing
21  * - beta = phi = 0
22  * - omega = lambda = 0
23  * - alpha = alpha0
24  * - sigma = s = 0
25  * - a 12 suffix means a difference, e.g., s12 = s2 - s1.
26  * - s and c prefixes mean sin and cos
27  **********************************************************************/
28 
31 
32 #if defined(_MSC_VER)
33 // Squelch warnings about potentially uninitialized local variables and
34 // constant conditional expressions
35 # pragma warning (disable: 4701 4127)
36 #endif
37 
38 namespace GeographicLib {
39 
40  using namespace std;
41 
42  Geodesic::Geodesic(real a, real f)
43  : maxit2_(maxit1_ + Math::digits() + 10)
44  // Underflow guard. We require
45  // tiny_ * epsilon() > 0
46  // tiny_ + epsilon() == epsilon()
47  , tiny_(sqrt(numeric_limits<real>::min()))
48  , tol0_(numeric_limits<real>::epsilon())
49  // Increase multiplier in defn of tol1_ from 100 to 200 to fix inverse
50  // case 52.784459512564 0 -52.784459512563990912 179.634407464943777557
51  // which otherwise failed for Visual Studio 10 (Release and Debug)
52  , tol1_(200 * tol0_)
53  , tol2_(sqrt(tol0_))
54  // Check on bisection interval
55  , tolb_(tol0_ * tol2_)
56  , xthresh_(1000 * tol2_)
57  , _a(a)
58  , _f(f <= 1 ? f : 1/f)
59  , _f1(1 - _f)
60  , _e2(_f * (2 - _f))
61  , _ep2(_e2 / Math::sq(_f1)) // e2 / (1 - e2)
62  , _n(_f / ( 2 - _f))
63  , _b(_a * _f1)
64  , _c2((Math::sq(_a) + Math::sq(_b) *
65  Math::eatanhe(real(1), (_f < 0 ? -1 : 1) * sqrt(abs(_e2))) / _e2)
66  / 2) // authalic radius squared
67  // The sig12 threshold for "really short". Using the auxiliary sphere
68  // solution with dnm computed at (bet1 + bet2) / 2, the relative error in
69  // the azimuth consistency check is sig12^2 * abs(f) * min(1, 1-f/2) / 2.
70  // (Error measured for 1/100 < b/a < 100 and abs(f) >= 1/1000. For a
71  // given f and sig12, the max error occurs for lines near the pole. If
72  // the old rule for computing dnm = (dn1 + dn2)/2 is used, then the error
73  // increases by a factor of 2.) Setting this equal to epsilon gives
74  // sig12 = etol2. Here 0.1 is a safety factor (error decreased by 100)
75  // and max(0.001, abs(f)) stops etol2 getting too large in the nearly
76  // spherical case.
77  , _etol2(0.1 * tol2_ /
78  sqrt( max(real(0.001), abs(_f)) * min(real(1), 1 - _f/2) / 2 ))
79  {
80  if (!(Math::isfinite(_a) && _a > 0))
81  throw GeographicErr("Major radius is not positive");
82  if (!(Math::isfinite(_b) && _b > 0))
83  throw GeographicErr("Minor radius is not positive");
84  A3coeff();
85  C3coeff();
86  C4coeff();
87  }
88 
90  static const Geodesic wgs84(Constants::WGS84_a(), Constants::WGS84_f());
91  return wgs84;
92  }
93 
94  Math::real Geodesic::SinCosSeries(bool sinp,
95  real sinx, real cosx,
96  const real c[], int n) {
97  // Evaluate
98  // y = sinp ? sum(c[i] * sin( 2*i * x), i, 1, n) :
99  // sum(c[i] * cos((2*i+1) * x), i, 0, n-1)
100  // using Clenshaw summation. N.B. c[0] is unused for sin series
101  // Approx operation count = (n + 5) mult and (2 * n + 2) add
102  c += (n + sinp); // Point to one beyond last element
103  real
104  ar = 2 * (cosx - sinx) * (cosx + sinx), // 2 * cos(2 * x)
105  y0 = n & 1 ? *--c : 0, y1 = 0; // accumulators for sum
106  // Now n is even
107  n /= 2;
108  while (n--) {
109  // Unroll loop x 2, so accumulators return to their original role
110  y1 = ar * y0 - y1 + *--c;
111  y0 = ar * y1 - y0 + *--c;
112  }
113  return sinp
114  ? 2 * sinx * cosx * y0 // sin(2 * x) * y0
115  : cosx * (y0 - y1); // cos(x) * (y0 - y1)
116  }
117 
118  GeodesicLine Geodesic::Line(real lat1, real lon1, real azi1, unsigned caps)
119  const {
120  return GeodesicLine(*this, lat1, lon1, azi1, caps);
121  }
122 
123  Math::real Geodesic::GenDirect(real lat1, real lon1, real azi1,
124  bool arcmode, real s12_a12, unsigned outmask,
125  real& lat2, real& lon2, real& azi2,
126  real& s12, real& m12, real& M12, real& M21,
127  real& S12) const {
128  return GeodesicLine(*this, lat1, lon1, azi1,
129  // Automatically supply DISTANCE_IN if necessary
130  outmask | (arcmode ? NONE : DISTANCE_IN))
131  . // Note the dot!
132  GenPosition(arcmode, s12_a12, outmask,
133  lat2, lon2, azi2, s12, m12, M12, M21, S12);
134  }
135 
136  Math::real Geodesic::GenInverse(real lat1, real lon1, real lat2, real lon2,
137  unsigned outmask,
138  real& s12, real& azi1, real& azi2,
139  real& m12, real& M12, real& M21, real& S12)
140  const {
141  outmask &= OUT_MASK;
142  // Compute longitude difference (AngDiff does this carefully). Result is
143  // in [-180, 180] but -180 is only for west-going geodesics. 180 is for
144  // east-going and meridional geodesics.
145  real lon12 = Math::AngDiff(Math::AngNormalize(lon1),
146  Math::AngNormalize(lon2));
147  // If very close to being on the same half-meridian, then make it so.
148  lon12 = Math::AngRound(lon12);
149  // Make longitude difference positive.
150  int lonsign = lon12 >= 0 ? 1 : -1;
151  lon12 *= lonsign;
152  // If really close to the equator, treat as on equator.
153  lat1 = Math::AngRound(lat1);
154  lat2 = Math::AngRound(lat2);
155  // Swap points so that point with higher (abs) latitude is point 1
156  int swapp = abs(lat1) >= abs(lat2) ? 1 : -1;
157  if (swapp < 0) {
158  lonsign *= -1;
159  swap(lat1, lat2);
160  }
161  // Make lat1 <= 0
162  int latsign = lat1 < 0 ? 1 : -1;
163  lat1 *= latsign;
164  lat2 *= latsign;
165  // Now we have
166  //
167  // 0 <= lon12 <= 180
168  // -90 <= lat1 <= 0
169  // lat1 <= lat2 <= -lat1
170  //
171  // longsign, swapp, latsign register the transformation to bring the
172  // coordinates to this canonical form. In all cases, 1 means no change was
173  // made. We make these transformations so that there are few cases to
174  // check, e.g., on verifying quadrants in atan2. In addition, this
175  // enforces some symmetries in the results returned.
176 
177  real phi, sbet1, cbet1, sbet2, cbet2, s12x, m12x;
178 
179  phi = lat1 * Math::degree();
180  // Ensure cbet1 = +epsilon at poles
181  sbet1 = _f1 * sin(phi);
182  cbet1 = lat1 == -90 ? tiny_ : cos(phi);
183  Math::norm(sbet1, cbet1);
184 
185  phi = lat2 * Math::degree();
186  // Ensure cbet2 = +epsilon at poles
187  sbet2 = _f1 * sin(phi);
188  cbet2 = abs(lat2) == 90 ? tiny_ : cos(phi);
189  Math::norm(sbet2, cbet2);
190 
191  // If cbet1 < -sbet1, then cbet2 - cbet1 is a sensitive measure of the
192  // |bet1| - |bet2|. Alternatively (cbet1 >= -sbet1), abs(sbet2) + sbet1 is
193  // a better measure. This logic is used in assigning calp2 in Lambda12.
194  // Sometimes these quantities vanish and in that case we force bet2 = +/-
195  // bet1 exactly. An example where is is necessary is the inverse problem
196  // 48.522876735459 0 -48.52287673545898293 179.599720456223079643
197  // which failed with Visual Studio 10 (Release and Debug)
198 
199  if (cbet1 < -sbet1) {
200  if (cbet2 == cbet1)
201  sbet2 = sbet2 < 0 ? sbet1 : -sbet1;
202  } else {
203  if (abs(sbet2) == -sbet1)
204  cbet2 = cbet1;
205  }
206 
207  real
208  dn1 = sqrt(1 + _ep2 * Math::sq(sbet1)),
209  dn2 = sqrt(1 + _ep2 * Math::sq(sbet2));
210 
211  real
212  lam12 = lon12 * Math::degree(),
213  slam12 = abs(lon12) == 180 ? 0 : sin(lam12),
214  clam12 = cos(lam12); // lon12 == 90 isn't interesting
215 
216  // initial values to suppress warning
217  real a12, sig12, calp1, salp1, calp2 = 0, salp2 = 0;
218  // index zero elements of these arrays are unused
219  real C1a[nC1_ + 1], C2a[nC2_ + 1], C3a[nC3_];
220 
221  bool meridian = lat1 == -90 || slam12 == 0;
222 
223  if (meridian) {
224 
225  // Endpoints are on a single full meridian, so the geodesic might lie on
226  // a meridian.
227 
228  calp1 = clam12; salp1 = slam12; // Head to the target longitude
229  calp2 = 1; salp2 = 0; // At the target we're heading north
230 
231  real
232  // tan(bet) = tan(sig) * cos(alp)
233  ssig1 = sbet1, csig1 = calp1 * cbet1,
234  ssig2 = sbet2, csig2 = calp2 * cbet2;
235 
236  // sig12 = sig2 - sig1
237  sig12 = atan2(max(csig1 * ssig2 - ssig1 * csig2, real(0)),
238  csig1 * csig2 + ssig1 * ssig2);
239  {
240  real dummy;
241  Lengths(_n, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
242  cbet1, cbet2, s12x, m12x, dummy,
243  (outmask & GEODESICSCALE) != 0U, M12, M21, C1a, C2a);
244  }
245  // Add the check for sig12 since zero length geodesics might yield m12 <
246  // 0. Test case was
247  //
248  // echo 20.001 0 20.001 0 | GeodSolve -i
249  //
250  // In fact, we will have sig12 > pi/2 for meridional geodesic which is
251  // not a shortest path.
252  if (sig12 < 1 || m12x >= 0) {
253  m12x *= _b;
254  s12x *= _b;
255  a12 = sig12 / Math::degree();
256  } else
257  // m12 < 0, i.e., prolate and too close to anti-podal
258  meridian = false;
259  }
260 
261  real omg12 = 0; // initial value to suppress warning
262  if (!meridian &&
263  sbet1 == 0 && // and sbet2 == 0
264  // Mimic the way Lambda12 works with calp1 = 0
265  (_f <= 0 || lam12 <= Math::pi() - _f * Math::pi())) {
266 
267  // Geodesic runs along equator
268  calp1 = calp2 = 0; salp1 = salp2 = 1;
269  s12x = _a * lam12;
270  sig12 = omg12 = lam12 / _f1;
271  m12x = _b * sin(sig12);
272  if (outmask & GEODESICSCALE)
273  M12 = M21 = cos(sig12);
274  a12 = lon12 / _f1;
275 
276  } else if (!meridian) {
277 
278  // Now point1 and point2 belong within a hemisphere bounded by a
279  // meridian and geodesic is neither meridional or equatorial.
280 
281  // Figure a starting point for Newton's method
282  real dnm;
283  sig12 = InverseStart(sbet1, cbet1, dn1, sbet2, cbet2, dn2,
284  lam12,
285  salp1, calp1, salp2, calp2, dnm,
286  C1a, C2a);
287 
288  if (sig12 >= 0) {
289  // Short lines (InverseStart sets salp2, calp2, dnm)
290  s12x = sig12 * _b * dnm;
291  m12x = Math::sq(dnm) * _b * sin(sig12 / dnm);
292  if (outmask & GEODESICSCALE)
293  M12 = M21 = cos(sig12 / dnm);
294  a12 = sig12 / Math::degree();
295  omg12 = lam12 / (_f1 * dnm);
296  } else {
297 
298  // Newton's method. This is a straightforward solution of f(alp1) =
299  // lambda12(alp1) - lam12 = 0 with one wrinkle. f(alp) has exactly one
300  // root in the interval (0, pi) and its derivative is positive at the
301  // root. Thus f(alp) is positive for alp > alp1 and negative for alp <
302  // alp1. During the course of the iteration, a range (alp1a, alp1b) is
303  // maintained which brackets the root and with each evaluation of
304  // f(alp) the range is shrunk, if possible. Newton's method is
305  // restarted whenever the derivative of f is negative (because the new
306  // value of alp1 is then further from the solution) or if the new
307  // estimate of alp1 lies outside (0,pi); in this case, the new starting
308  // guess is taken to be (alp1a + alp1b) / 2.
309  //
310  // initial values to suppress warnings (if loop is executed 0 times)
311  real ssig1 = 0, csig1 = 0, ssig2 = 0, csig2 = 0, eps = 0;
312  unsigned numit = 0;
313  // Bracketing range
314  real salp1a = tiny_, calp1a = 1, salp1b = tiny_, calp1b = -1;
315  for (bool tripn = false, tripb = false;
316  numit < maxit2_ || GEOGRAPHICLIB_PANIC;
317  ++numit) {
318  // the WGS84 test set: mean = 1.47, sd = 1.25, max = 16
319  // WGS84 and random input: mean = 2.85, sd = 0.60
320  real dv;
321  real v = Lambda12(sbet1, cbet1, dn1, sbet2, cbet2, dn2, salp1, calp1,
322  salp2, calp2, sig12, ssig1, csig1, ssig2, csig2,
323  eps, omg12, numit < maxit1_, dv, C1a, C2a, C3a)
324  - lam12;
325  // 2 * tol0 is approximately 1 ulp for a number in [0, pi].
326  // Reversed test to allow escape with NaNs
327  if (tripb || !(abs(v) >= (tripn ? 8 : 2) * tol0_)) break;
328  // Update bracketing values
329  if (v > 0 && (numit > maxit1_ || calp1/salp1 > calp1b/salp1b))
330  { salp1b = salp1; calp1b = calp1; }
331  else if (v < 0 && (numit > maxit1_ || calp1/salp1 < calp1a/salp1a))
332  { salp1a = salp1; calp1a = calp1; }
333  if (numit < maxit1_ && dv > 0) {
334  real
335  dalp1 = -v/dv;
336  real
337  sdalp1 = sin(dalp1), cdalp1 = cos(dalp1),
338  nsalp1 = salp1 * cdalp1 + calp1 * sdalp1;
339  if (nsalp1 > 0 && abs(dalp1) < Math::pi()) {
340  calp1 = calp1 * cdalp1 - salp1 * sdalp1;
341  salp1 = nsalp1;
342  Math::norm(salp1, calp1);
343  // In some regimes we don't get quadratic convergence because
344  // slope -> 0. So use convergence conditions based on epsilon
345  // instead of sqrt(epsilon).
346  tripn = abs(v) <= 16 * tol0_;
347  continue;
348  }
349  }
350  // Either dv was not postive or updated value was outside legal
351  // range. Use the midpoint of the bracket as the next estimate.
352  // This mechanism is not needed for the WGS84 ellipsoid, but it does
353  // catch problems with more eccentric ellipsoids. Its efficacy is
354  // such for the WGS84 test set with the starting guess set to alp1 =
355  // 90deg:
356  // the WGS84 test set: mean = 5.21, sd = 3.93, max = 24
357  // WGS84 and random input: mean = 4.74, sd = 0.99
358  salp1 = (salp1a + salp1b)/2;
359  calp1 = (calp1a + calp1b)/2;
360  Math::norm(salp1, calp1);
361  tripn = false;
362  tripb = (abs(salp1a - salp1) + (calp1a - calp1) < tolb_ ||
363  abs(salp1 - salp1b) + (calp1 - calp1b) < tolb_);
364  }
365  {
366  real dummy;
367  Lengths(eps, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
368  cbet1, cbet2, s12x, m12x, dummy,
369  (outmask & GEODESICSCALE) != 0U, M12, M21, C1a, C2a);
370  }
371  m12x *= _b;
372  s12x *= _b;
373  a12 = sig12 / Math::degree();
374  omg12 = lam12 - omg12;
375  }
376  }
377 
378  if (outmask & DISTANCE)
379  s12 = 0 + s12x; // Convert -0 to 0
380 
381  if (outmask & REDUCEDLENGTH)
382  m12 = 0 + m12x; // Convert -0 to 0
383 
384  if (outmask & AREA) {
385  real
386  // From Lambda12: sin(alp1) * cos(bet1) = sin(alp0)
387  salp0 = salp1 * cbet1,
388  calp0 = Math::hypot(calp1, salp1 * sbet1); // calp0 > 0
389  real alp12;
390  if (calp0 != 0 && salp0 != 0) {
391  real
392  // From Lambda12: tan(bet) = tan(sig) * cos(alp)
393  ssig1 = sbet1, csig1 = calp1 * cbet1,
394  ssig2 = sbet2, csig2 = calp2 * cbet2,
395  k2 = Math::sq(calp0) * _ep2,
396  eps = k2 / (2 * (1 + sqrt(1 + k2)) + k2),
397  // Multiplier = a^2 * e^2 * cos(alpha0) * sin(alpha0).
398  A4 = Math::sq(_a) * calp0 * salp0 * _e2;
399  Math::norm(ssig1, csig1);
400  Math::norm(ssig2, csig2);
401  real C4a[nC4_];
402  C4f(eps, C4a);
403  real
404  B41 = SinCosSeries(false, ssig1, csig1, C4a, nC4_),
405  B42 = SinCosSeries(false, ssig2, csig2, C4a, nC4_);
406  S12 = A4 * (B42 - B41);
407  } else
408  // Avoid problems with indeterminate sig1, sig2 on equator
409  S12 = 0;
410 
411  if (!meridian &&
412  omg12 < real(0.75) * Math::pi() && // Long difference too big
413  sbet2 - sbet1 < real(1.75)) { // Lat difference too big
414  // Use tan(Gamma/2) = tan(omg12/2)
415  // * (tan(bet1/2)+tan(bet2/2))/(1+tan(bet1/2)*tan(bet2/2))
416  // with tan(x/2) = sin(x)/(1+cos(x))
417  real
418  somg12 = sin(omg12), domg12 = 1 + cos(omg12),
419  dbet1 = 1 + cbet1, dbet2 = 1 + cbet2;
420  alp12 = 2 * atan2( somg12 * ( sbet1 * dbet2 + sbet2 * dbet1 ),
421  domg12 * ( sbet1 * sbet2 + dbet1 * dbet2 ) );
422  } else {
423  // alp12 = alp2 - alp1, used in atan2 so no need to normalize
424  real
425  salp12 = salp2 * calp1 - calp2 * salp1,
426  calp12 = calp2 * calp1 + salp2 * salp1;
427  // The right thing appears to happen if alp1 = +/-180 and alp2 = 0, viz
428  // salp12 = -0 and alp12 = -180. However this depends on the sign
429  // being attached to 0 correctly. The following ensures the correct
430  // behavior.
431  if (salp12 == 0 && calp12 < 0) {
432  salp12 = tiny_ * calp1;
433  calp12 = -1;
434  }
435  alp12 = atan2(salp12, calp12);
436  }
437  S12 += _c2 * alp12;
438  S12 *= swapp * lonsign * latsign;
439  // Convert -0 to 0
440  S12 += 0;
441  }
442 
443  // Convert calp, salp to azimuth accounting for lonsign, swapp, latsign.
444  if (swapp < 0) {
445  swap(salp1, salp2);
446  swap(calp1, calp2);
447  if (outmask & GEODESICSCALE)
448  swap(M12, M21);
449  }
450 
451  salp1 *= swapp * lonsign; calp1 *= swapp * latsign;
452  salp2 *= swapp * lonsign; calp2 *= swapp * latsign;
453 
454  if (outmask & AZIMUTH) {
455  azi1 = Math::atan2d(salp1, calp1);
456  azi2 = Math::atan2d(salp2, calp2);
457  }
458 
459  // Returned value in [0, 180]
460  return a12;
461  }
462 
463  void Geodesic::Lengths(real eps, real sig12,
464  real ssig1, real csig1, real dn1,
465  real ssig2, real csig2, real dn2,
466  real cbet1, real cbet2,
467  real& s12b, real& m12b, real& m0,
468  bool scalep, real& M12, real& M21,
469  // Scratch areas of the right size
470  real C1a[], real C2a[]) const {
471  // Return m12b = (reduced length)/_b; also calculate s12b = distance/_b,
472  // and m0 = coefficient of secular term in expression for reduced length.
473  C1f(eps, C1a);
474  C2f(eps, C2a);
475  real
476  A1m1 = A1m1f(eps),
477  AB1 = (1 + A1m1) * (SinCosSeries(true, ssig2, csig2, C1a, nC1_) -
478  SinCosSeries(true, ssig1, csig1, C1a, nC1_)),
479  A2m1 = A2m1f(eps),
480  AB2 = (1 + A2m1) * (SinCosSeries(true, ssig2, csig2, C2a, nC2_) -
481  SinCosSeries(true, ssig1, csig1, C2a, nC2_));
482  m0 = A1m1 - A2m1;
483  real J12 = m0 * sig12 + (AB1 - AB2);
484  // Missing a factor of _b.
485  // Add parens around (csig1 * ssig2) and (ssig1 * csig2) to ensure accurate
486  // cancellation in the case of coincident points.
487  m12b = dn2 * (csig1 * ssig2) - dn1 * (ssig1 * csig2) - csig1 * csig2 * J12;
488  // Missing a factor of _b
489  s12b = (1 + A1m1) * sig12 + AB1;
490  if (scalep) {
491  real csig12 = csig1 * csig2 + ssig1 * ssig2;
492  real t = _ep2 * (cbet1 - cbet2) * (cbet1 + cbet2) / (dn1 + dn2);
493  M12 = csig12 + (t * ssig2 - csig2 * J12) * ssig1 / dn1;
494  M21 = csig12 - (t * ssig1 - csig1 * J12) * ssig2 / dn2;
495  }
496  }
497 
498  Math::real Geodesic::Astroid(real x, real y) {
499  // Solve k^4+2*k^3-(x^2+y^2-1)*k^2-2*y^2*k-y^2 = 0 for positive root k.
500  // This solution is adapted from Geocentric::Reverse.
501  real k;
502  real
503  p = Math::sq(x),
504  q = Math::sq(y),
505  r = (p + q - 1) / 6;
506  if ( !(q == 0 && r <= 0) ) {
507  real
508  // Avoid possible division by zero when r = 0 by multiplying equations
509  // for s and t by r^3 and r, resp.
510  S = p * q / 4, // S = r^3 * s
511  r2 = Math::sq(r),
512  r3 = r * r2,
513  // The discrimant of the quadratic equation for T3. This is zero on
514  // the evolute curve p^(1/3)+q^(1/3) = 1
515  disc = S * (S + 2 * r3);
516  real u = r;
517  if (disc >= 0) {
518  real T3 = S + r3;
519  // Pick the sign on the sqrt to maximize abs(T3). This minimizes loss
520  // of precision due to cancellation. The result is unchanged because
521  // of the way the T is used in definition of u.
522  T3 += T3 < 0 ? -sqrt(disc) : sqrt(disc); // T3 = (r * t)^3
523  // N.B. cbrt always returns the real root. cbrt(-8) = -2.
524  real T = Math::cbrt(T3); // T = r * t
525  // T can be zero; but then r2 / T -> 0.
526  u += T + (T ? r2 / T : 0);
527  } else {
528  // T is complex, but the way u is defined the result is real.
529  real ang = atan2(sqrt(-disc), -(S + r3));
530  // There are three possible cube roots. We choose the root which
531  // avoids cancellation. Note that disc < 0 implies that r < 0.
532  u += 2 * r * cos(ang / 3);
533  }
534  real
535  v = sqrt(Math::sq(u) + q), // guaranteed positive
536  // Avoid loss of accuracy when u < 0.
537  uv = u < 0 ? q / (v - u) : u + v, // u+v, guaranteed positive
538  w = (uv - q) / (2 * v); // positive?
539  // Rearrange expression for k to avoid loss of accuracy due to
540  // subtraction. Division by 0 not possible because uv > 0, w >= 0.
541  k = uv / (sqrt(uv + Math::sq(w)) + w); // guaranteed positive
542  } else { // q == 0 && r <= 0
543  // y = 0 with |x| <= 1. Handle this case directly.
544  // for y small, positive root is k = abs(y)/sqrt(1-x^2)
545  k = 0;
546  }
547  return k;
548  }
549 
550  Math::real Geodesic::InverseStart(real sbet1, real cbet1, real dn1,
551  real sbet2, real cbet2, real dn2,
552  real lam12,
553  real& salp1, real& calp1,
554  // Only updated if return val >= 0
555  real& salp2, real& calp2,
556  // Only updated for short lines
557  real& dnm,
558  // Scratch areas of the right size
559  real C1a[], real C2a[]) const {
560  // Return a starting point for Newton's method in salp1 and calp1 (function
561  // value is -1). If Newton's method doesn't need to be used, return also
562  // salp2 and calp2 and function value is sig12.
563  real
564  sig12 = -1, // Return value
565  // bet12 = bet2 - bet1 in [0, pi); bet12a = bet2 + bet1 in (-pi, 0]
566  sbet12 = sbet2 * cbet1 - cbet2 * sbet1,
567  cbet12 = cbet2 * cbet1 + sbet2 * sbet1;
568 #if defined(__GNUC__) && __GNUC__ == 4 && \
569  (__GNUC_MINOR__ < 6 || defined(__MINGW32__))
570  // Volatile declaration needed to fix inverse cases
571  // 88.202499451857 0 -88.202499451857 179.981022032992859592
572  // 89.262080389218 0 -89.262080389218 179.992207982775375662
573  // 89.333123580033 0 -89.333123580032997687 179.99295812360148422
574  // which otherwise fail with g++ 4.4.4 x86 -O3 (Linux)
575  // and g++ 4.4.0 (mingw) and g++ 4.6.1 (tdm mingw).
576  real sbet12a;
577  {
578  GEOGRAPHICLIB_VOLATILE real xx1 = sbet2 * cbet1;
579  GEOGRAPHICLIB_VOLATILE real xx2 = cbet2 * sbet1;
580  sbet12a = xx1 + xx2;
581  }
582 #else
583  real sbet12a = sbet2 * cbet1 + cbet2 * sbet1;
584 #endif
585  bool shortline = cbet12 >= 0 && sbet12 < real(0.5) &&
586  cbet2 * lam12 < real(0.5);
587  real omg12 = lam12;
588  if (shortline) {
589  real sbetm2 = Math::sq(sbet1 + sbet2);
590  // sin((bet1+bet2)/2)^2
591  // = (sbet1 + sbet2)^2 / ((sbet1 + sbet2)^2 + (cbet1 + cbet2)^2)
592  sbetm2 /= sbetm2 + Math::sq(cbet1 + cbet2);
593  dnm = sqrt(1 + _ep2 * sbetm2);
594  omg12 /= _f1 * dnm;
595  }
596  real somg12 = sin(omg12), comg12 = cos(omg12);
597 
598  salp1 = cbet2 * somg12;
599  calp1 = comg12 >= 0 ?
600  sbet12 + cbet2 * sbet1 * Math::sq(somg12) / (1 + comg12) :
601  sbet12a - cbet2 * sbet1 * Math::sq(somg12) / (1 - comg12);
602 
603  real
604  ssig12 = Math::hypot(salp1, calp1),
605  csig12 = sbet1 * sbet2 + cbet1 * cbet2 * comg12;
606 
607  if (shortline && ssig12 < _etol2) {
608  // really short lines
609  salp2 = cbet1 * somg12;
610  calp2 = sbet12 - cbet1 * sbet2 *
611  (comg12 >= 0 ? Math::sq(somg12) / (1 + comg12) : 1 - comg12);
612  Math::norm(salp2, calp2);
613  // Set return value
614  sig12 = atan2(ssig12, csig12);
615  } else if (abs(_n) > real(0.1) || // Skip astroid calc if too eccentric
616  csig12 >= 0 ||
617  ssig12 >= 6 * abs(_n) * Math::pi() * Math::sq(cbet1)) {
618  // Nothing to do, zeroth order spherical approximation is OK
619  } else {
620  // Scale lam12 and bet2 to x, y coordinate system where antipodal point
621  // is at origin and singular point is at y = 0, x = -1.
622  real y, lamscale, betscale;
623  // Volatile declaration needed to fix inverse case
624  // 56.320923501171 0 -56.320923501171 179.664747671772880215
625  // which otherwise fails with g++ 4.4.4 x86 -O3
627  if (_f >= 0) { // In fact f == 0 does not get here
628  // x = dlong, y = dlat
629  {
630  real
631  k2 = Math::sq(sbet1) * _ep2,
632  eps = k2 / (2 * (1 + sqrt(1 + k2)) + k2);
633  lamscale = _f * cbet1 * A3f(eps) * Math::pi();
634  }
635  betscale = lamscale * cbet1;
636 
637  x = (lam12 - Math::pi()) / lamscale;
638  y = sbet12a / betscale;
639  } else { // _f < 0
640  // x = dlat, y = dlong
641  real
642  cbet12a = cbet2 * cbet1 - sbet2 * sbet1,
643  bet12a = atan2(sbet12a, cbet12a);
644  real m12b, m0, dummy;
645  // In the case of lon12 = 180, this repeats a calculation made in
646  // Inverse.
647  Lengths(_n, Math::pi() + bet12a,
648  sbet1, -cbet1, dn1, sbet2, cbet2, dn2,
649  cbet1, cbet2, dummy, m12b, m0, false,
650  dummy, dummy, C1a, C2a);
651  x = -1 + m12b / (cbet1 * cbet2 * m0 * Math::pi());
652  betscale = x < -real(0.01) ? sbet12a / x :
653  -_f * Math::sq(cbet1) * Math::pi();
654  lamscale = betscale / cbet1;
655  y = (lam12 - Math::pi()) / lamscale;
656  }
657 
658  if (y > -tol1_ && x > -1 - xthresh_) {
659  // strip near cut
660  // Need real(x) here to cast away the volatility of x for min/max
661  if (_f >= 0) {
662  salp1 = min(real(1), -real(x)); calp1 = - sqrt(1 - Math::sq(salp1));
663  } else {
664  calp1 = max(real(x > -tol1_ ? 0 : -1), real(x));
665  salp1 = sqrt(1 - Math::sq(calp1));
666  }
667  } else {
668  // Estimate alp1, by solving the astroid problem.
669  //
670  // Could estimate alpha1 = theta + pi/2, directly, i.e.,
671  // calp1 = y/k; salp1 = -x/(1+k); for _f >= 0
672  // calp1 = x/(1+k); salp1 = -y/k; for _f < 0 (need to check)
673  //
674  // However, it's better to estimate omg12 from astroid and use
675  // spherical formula to compute alp1. This reduces the mean number of
676  // Newton iterations for astroid cases from 2.24 (min 0, max 6) to 2.12
677  // (min 0 max 5). The changes in the number of iterations are as
678  // follows:
679  //
680  // change percent
681  // 1 5
682  // 0 78
683  // -1 16
684  // -2 0.6
685  // -3 0.04
686  // -4 0.002
687  //
688  // The histogram of iterations is (m = number of iterations estimating
689  // alp1 directly, n = number of iterations estimating via omg12, total
690  // number of trials = 148605):
691  //
692  // iter m n
693  // 0 148 186
694  // 1 13046 13845
695  // 2 93315 102225
696  // 3 36189 32341
697  // 4 5396 7
698  // 5 455 1
699  // 6 56 0
700  //
701  // Because omg12 is near pi, estimate work with omg12a = pi - omg12
702  real k = Astroid(x, y);
703  real
704  omg12a = lamscale * ( _f >= 0 ? -x * k/(1 + k) : -y * (1 + k)/k );
705  somg12 = sin(omg12a); comg12 = -cos(omg12a);
706  // Update spherical estimate of alp1 using omg12 instead of lam12
707  salp1 = cbet2 * somg12;
708  calp1 = sbet12a - cbet2 * sbet1 * Math::sq(somg12) / (1 - comg12);
709  }
710  }
711  // Sanity check on starting guess. Backwards check allows NaN through.
712  if (!(salp1 <= 0))
713  Math::norm(salp1, calp1);
714  else {
715  salp1 = 1; calp1 = 0;
716  }
717  return sig12;
718  }
719 
720  Math::real Geodesic::Lambda12(real sbet1, real cbet1, real dn1,
721  real sbet2, real cbet2, real dn2,
722  real salp1, real calp1,
723  real& salp2, real& calp2,
724  real& sig12,
725  real& ssig1, real& csig1,
726  real& ssig2, real& csig2,
727  real& eps, real& domg12,
728  bool diffp, real& dlam12,
729  // Scratch areas of the right size
730  real C1a[], real C2a[], real C3a[]) const {
731 
732  if (sbet1 == 0 && calp1 == 0)
733  // Break degeneracy of equatorial line. This case has already been
734  // handled.
735  calp1 = -tiny_;
736 
737  real
738  // sin(alp1) * cos(bet1) = sin(alp0)
739  salp0 = salp1 * cbet1,
740  calp0 = Math::hypot(calp1, salp1 * sbet1); // calp0 > 0
741 
742  real somg1, comg1, somg2, comg2, omg12, lam12;
743  // tan(bet1) = tan(sig1) * cos(alp1)
744  // tan(omg1) = sin(alp0) * tan(sig1) = tan(omg1)=tan(alp1)*sin(bet1)
745  ssig1 = sbet1; somg1 = salp0 * sbet1;
746  csig1 = comg1 = calp1 * cbet1;
747  Math::norm(ssig1, csig1);
748  // Math::norm(somg1, comg1); -- don't need to normalize!
749 
750  // Enforce symmetries in the case abs(bet2) = -bet1. Need to be careful
751  // about this case, since this can yield singularities in the Newton
752  // iteration.
753  // sin(alp2) * cos(bet2) = sin(alp0)
754  salp2 = cbet2 != cbet1 ? salp0 / cbet2 : salp1;
755  // calp2 = sqrt(1 - sq(salp2))
756  // = sqrt(sq(calp0) - sq(sbet2)) / cbet2
757  // and subst for calp0 and rearrange to give (choose positive sqrt
758  // to give alp2 in [0, pi/2]).
759  calp2 = cbet2 != cbet1 || abs(sbet2) != -sbet1 ?
760  sqrt(Math::sq(calp1 * cbet1) +
761  (cbet1 < -sbet1 ?
762  (cbet2 - cbet1) * (cbet1 + cbet2) :
763  (sbet1 - sbet2) * (sbet1 + sbet2))) / cbet2 :
764  abs(calp1);
765  // tan(bet2) = tan(sig2) * cos(alp2)
766  // tan(omg2) = sin(alp0) * tan(sig2).
767  ssig2 = sbet2; somg2 = salp0 * sbet2;
768  csig2 = comg2 = calp2 * cbet2;
769  Math::norm(ssig2, csig2);
770  // Math::norm(somg2, comg2); -- don't need to normalize!
771 
772  // sig12 = sig2 - sig1, limit to [0, pi]
773  sig12 = atan2(max(csig1 * ssig2 - ssig1 * csig2, real(0)),
774  csig1 * csig2 + ssig1 * ssig2);
775 
776  // omg12 = omg2 - omg1, limit to [0, pi]
777  omg12 = atan2(max(comg1 * somg2 - somg1 * comg2, real(0)),
778  comg1 * comg2 + somg1 * somg2);
779  real B312, h0;
780  real k2 = Math::sq(calp0) * _ep2;
781  eps = k2 / (2 * (1 + sqrt(1 + k2)) + k2);
782  C3f(eps, C3a);
783  B312 = (SinCosSeries(true, ssig2, csig2, C3a, nC3_-1) -
784  SinCosSeries(true, ssig1, csig1, C3a, nC3_-1));
785  h0 = -_f * A3f(eps);
786  domg12 = salp0 * h0 * (sig12 + B312);
787  lam12 = omg12 + domg12;
788 
789  if (diffp) {
790  if (calp2 == 0)
791  dlam12 = - 2 * _f1 * dn1 / sbet1;
792  else {
793  real dummy;
794  Lengths(eps, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
795  cbet1, cbet2, dummy, dlam12, dummy,
796  false, dummy, dummy, C1a, C2a);
797  dlam12 *= _f1 / (calp2 * cbet2);
798  }
799  }
800 
801  return lam12;
802  }
803 
804  Math::real Geodesic::A3f(real eps) const {
805  // Evaluate A3
806  return Math::polyval(nA3_ - 1, _A3x, eps);
807  }
808 
809  void Geodesic::C3f(real eps, real c[]) const {
810  // Evaluate C3 coeffs
811  // Elements c[1] thru c[nC3_ - 1] are set
812  real mult = 1;
813  int o = 0;
814  for (int l = 1; l < nC3_; ++l) { // l is index of C3[l]
815  int m = nC3_ - l - 1; // order of polynomial in eps
816  mult *= eps;
817  c[l] = mult * Math::polyval(m, _C3x + o, eps);
818  o += m + 1;
819  }
820  // Post condition: o == nC3x_
821  }
822 
823  void Geodesic::C4f(real eps, real c[]) const {
824  // Evaluate C4 coeffs
825  // Elements c[0] thru c[nC4_ - 1] are set
826  real mult = 1;
827  int o = 0;
828  for (int l = 0; l < nC4_; ++l) { // l is index of C4[l]
829  int m = nC4_ - l - 1; // order of polynomial in eps
830  c[l] = mult * Math::polyval(m, _C4x + o, eps);
831  o += m + 1;
832  mult *= eps;
833  }
834  // Post condition: o == nC4x_
835  }
836 
837  // The static const coefficient arrays in the following functions are
838  // generated by Maxima and give the coefficients of the Taylor expansions for
839  // the geodesics. The convention on the order of these coefficients is as
840  // follows:
841  //
842  // ascending order in the trigonometric expansion,
843  // then powers of eps in descending order,
844  // finally powers of n in descending order.
845  //
846  // (For some expansions, only a subset of levels occur.) For each polynomial
847  // of order n at the lowest level, the (n+1) coefficients of the polynomial
848  // are followed by a divisor which is applied to the whole polynomial. In
849  // this way, the coefficients are expressible with no round off error. The
850  // sizes of the coefficient arrays are:
851  //
852  // A1m1f, A2m1f = floor(N/2) + 2
853  // C1f, C1pf, C2f, A3coeff = (N^2 + 7*N - 2*floor(N/2)) / 4
854  // C3coeff = (N - 1) * (N^2 + 7*N - 2*floor(N/2)) / 8
855  // C4coeff = N * (N + 1) * (N + 5) / 6
856  //
857  // where N = GEOGRAPHICLIB_GEODESIC_ORDER
858  // = nA1 = nA2 = nC1 = nC1p = nA3 = nC4
859 
860  // The scale factor A1-1 = mean value of (d/dsigma)I1 - 1
861  Math::real Geodesic::A1m1f(real eps) {
862  // Generated by Maxima on 2015-05-05 18:08:12-04:00
863 #if GEOGRAPHICLIB_GEODESIC_ORDER/2 == 1
864  static const real coeff[] = {
865  // (1-eps)*A1-1, polynomial in eps2 of order 1
866  1, 0, 4,
867  };
868 #elif GEOGRAPHICLIB_GEODESIC_ORDER/2 == 2
869  static const real coeff[] = {
870  // (1-eps)*A1-1, polynomial in eps2 of order 2
871  1, 16, 0, 64,
872  };
873 #elif GEOGRAPHICLIB_GEODESIC_ORDER/2 == 3
874  static const real coeff[] = {
875  // (1-eps)*A1-1, polynomial in eps2 of order 3
876  1, 4, 64, 0, 256,
877  };
878 #elif GEOGRAPHICLIB_GEODESIC_ORDER/2 == 4
879  static const real coeff[] = {
880  // (1-eps)*A1-1, polynomial in eps2 of order 4
881  25, 64, 256, 4096, 0, 16384,
882  };
883 #else
884 #error "Bad value for GEOGRAPHICLIB_GEODESIC_ORDER"
885 #endif
886  GEOGRAPHICLIB_STATIC_ASSERT(sizeof(coeff) / sizeof(real) == nA1_/2 + 2,
887  "Coefficient array size mismatch in A1m1f");
888  int m = nA1_/2;
889  real t = Math::polyval(m, coeff, Math::sq(eps)) / coeff[m + 1];
890  return (t + eps) / (1 - eps);
891  }
892 
893  // The coefficients C1[l] in the Fourier expansion of B1
894  void Geodesic::C1f(real eps, real c[]) {
895  // Generated by Maxima on 2015-05-05 18:08:12-04:00
896 #if GEOGRAPHICLIB_GEODESIC_ORDER == 3
897  static const real coeff[] = {
898  // C1[1]/eps^1, polynomial in eps2 of order 1
899  3, -8, 16,
900  // C1[2]/eps^2, polynomial in eps2 of order 0
901  -1, 16,
902  // C1[3]/eps^3, polynomial in eps2 of order 0
903  -1, 48,
904  };
905 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 4
906  static const real coeff[] = {
907  // C1[1]/eps^1, polynomial in eps2 of order 1
908  3, -8, 16,
909  // C1[2]/eps^2, polynomial in eps2 of order 1
910  1, -2, 32,
911  // C1[3]/eps^3, polynomial in eps2 of order 0
912  -1, 48,
913  // C1[4]/eps^4, polynomial in eps2 of order 0
914  -5, 512,
915  };
916 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 5
917  static const real coeff[] = {
918  // C1[1]/eps^1, polynomial in eps2 of order 2
919  -1, 6, -16, 32,
920  // C1[2]/eps^2, polynomial in eps2 of order 1
921  1, -2, 32,
922  // C1[3]/eps^3, polynomial in eps2 of order 1
923  9, -16, 768,
924  // C1[4]/eps^4, polynomial in eps2 of order 0
925  -5, 512,
926  // C1[5]/eps^5, polynomial in eps2 of order 0
927  -7, 1280,
928  };
929 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 6
930  static const real coeff[] = {
931  // C1[1]/eps^1, polynomial in eps2 of order 2
932  -1, 6, -16, 32,
933  // C1[2]/eps^2, polynomial in eps2 of order 2
934  -9, 64, -128, 2048,
935  // C1[3]/eps^3, polynomial in eps2 of order 1
936  9, -16, 768,
937  // C1[4]/eps^4, polynomial in eps2 of order 1
938  3, -5, 512,
939  // C1[5]/eps^5, polynomial in eps2 of order 0
940  -7, 1280,
941  // C1[6]/eps^6, polynomial in eps2 of order 0
942  -7, 2048,
943  };
944 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 7
945  static const real coeff[] = {
946  // C1[1]/eps^1, polynomial in eps2 of order 3
947  19, -64, 384, -1024, 2048,
948  // C1[2]/eps^2, polynomial in eps2 of order 2
949  -9, 64, -128, 2048,
950  // C1[3]/eps^3, polynomial in eps2 of order 2
951  -9, 72, -128, 6144,
952  // C1[4]/eps^4, polynomial in eps2 of order 1
953  3, -5, 512,
954  // C1[5]/eps^5, polynomial in eps2 of order 1
955  35, -56, 10240,
956  // C1[6]/eps^6, polynomial in eps2 of order 0
957  -7, 2048,
958  // C1[7]/eps^7, polynomial in eps2 of order 0
959  -33, 14336,
960  };
961 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 8
962  static const real coeff[] = {
963  // C1[1]/eps^1, polynomial in eps2 of order 3
964  19, -64, 384, -1024, 2048,
965  // C1[2]/eps^2, polynomial in eps2 of order 3
966  7, -18, 128, -256, 4096,
967  // C1[3]/eps^3, polynomial in eps2 of order 2
968  -9, 72, -128, 6144,
969  // C1[4]/eps^4, polynomial in eps2 of order 2
970  -11, 96, -160, 16384,
971  // C1[5]/eps^5, polynomial in eps2 of order 1
972  35, -56, 10240,
973  // C1[6]/eps^6, polynomial in eps2 of order 1
974  9, -14, 4096,
975  // C1[7]/eps^7, polynomial in eps2 of order 0
976  -33, 14336,
977  // C1[8]/eps^8, polynomial in eps2 of order 0
978  -429, 262144,
979  };
980 #else
981 #error "Bad value for GEOGRAPHICLIB_GEODESIC_ORDER"
982 #endif
983  GEOGRAPHICLIB_STATIC_ASSERT(sizeof(coeff) / sizeof(real) ==
984  (nC1_*nC1_ + 7*nC1_ - 2*(nC1_/2)) / 4,
985  "Coefficient array size mismatch in C1f");
986  real
987  eps2 = Math::sq(eps),
988  d = eps;
989  int o = 0;
990  for (int l = 1; l <= nC1_; ++l) { // l is index of C1p[l]
991  int m = (nC1_ - l) / 2; // order of polynomial in eps^2
992  c[l] = d * Math::polyval(m, coeff + o, eps2) / coeff[o + m + 1];
993  o += m + 2;
994  d *= eps;
995  }
996  // Post condition: o == sizeof(coeff) / sizeof(real)
997  }
998 
999  // The coefficients C1p[l] in the Fourier expansion of B1p
1000  void Geodesic::C1pf(real eps, real c[]) {
1001  // Generated by Maxima on 2015-05-05 18:08:12-04:00
1002 #if GEOGRAPHICLIB_GEODESIC_ORDER == 3
1003  static const real coeff[] = {
1004  // C1p[1]/eps^1, polynomial in eps2 of order 1
1005  -9, 16, 32,
1006  // C1p[2]/eps^2, polynomial in eps2 of order 0
1007  5, 16,
1008  // C1p[3]/eps^3, polynomial in eps2 of order 0
1009  29, 96,
1010  };
1011 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 4
1012  static const real coeff[] = {
1013  // C1p[1]/eps^1, polynomial in eps2 of order 1
1014  -9, 16, 32,
1015  // C1p[2]/eps^2, polynomial in eps2 of order 1
1016  -37, 30, 96,
1017  // C1p[3]/eps^3, polynomial in eps2 of order 0
1018  29, 96,
1019  // C1p[4]/eps^4, polynomial in eps2 of order 0
1020  539, 1536,
1021  };
1022 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 5
1023  static const real coeff[] = {
1024  // C1p[1]/eps^1, polynomial in eps2 of order 2
1025  205, -432, 768, 1536,
1026  // C1p[2]/eps^2, polynomial in eps2 of order 1
1027  -37, 30, 96,
1028  // C1p[3]/eps^3, polynomial in eps2 of order 1
1029  -225, 116, 384,
1030  // C1p[4]/eps^4, polynomial in eps2 of order 0
1031  539, 1536,
1032  // C1p[5]/eps^5, polynomial in eps2 of order 0
1033  3467, 7680,
1034  };
1035 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 6
1036  static const real coeff[] = {
1037  // C1p[1]/eps^1, polynomial in eps2 of order 2
1038  205, -432, 768, 1536,
1039  // C1p[2]/eps^2, polynomial in eps2 of order 2
1040  4005, -4736, 3840, 12288,
1041  // C1p[3]/eps^3, polynomial in eps2 of order 1
1042  -225, 116, 384,
1043  // C1p[4]/eps^4, polynomial in eps2 of order 1
1044  -7173, 2695, 7680,
1045  // C1p[5]/eps^5, polynomial in eps2 of order 0
1046  3467, 7680,
1047  // C1p[6]/eps^6, polynomial in eps2 of order 0
1048  38081, 61440,
1049  };
1050 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 7
1051  static const real coeff[] = {
1052  // C1p[1]/eps^1, polynomial in eps2 of order 3
1053  -4879, 9840, -20736, 36864, 73728,
1054  // C1p[2]/eps^2, polynomial in eps2 of order 2
1055  4005, -4736, 3840, 12288,
1056  // C1p[3]/eps^3, polynomial in eps2 of order 2
1057  8703, -7200, 3712, 12288,
1058  // C1p[4]/eps^4, polynomial in eps2 of order 1
1059  -7173, 2695, 7680,
1060  // C1p[5]/eps^5, polynomial in eps2 of order 1
1061  -141115, 41604, 92160,
1062  // C1p[6]/eps^6, polynomial in eps2 of order 0
1063  38081, 61440,
1064  // C1p[7]/eps^7, polynomial in eps2 of order 0
1065  459485, 516096,
1066  };
1067 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 8
1068  static const real coeff[] = {
1069  // C1p[1]/eps^1, polynomial in eps2 of order 3
1070  -4879, 9840, -20736, 36864, 73728,
1071  // C1p[2]/eps^2, polynomial in eps2 of order 3
1072  -86171, 120150, -142080, 115200, 368640,
1073  // C1p[3]/eps^3, polynomial in eps2 of order 2
1074  8703, -7200, 3712, 12288,
1075  // C1p[4]/eps^4, polynomial in eps2 of order 2
1076  1082857, -688608, 258720, 737280,
1077  // C1p[5]/eps^5, polynomial in eps2 of order 1
1078  -141115, 41604, 92160,
1079  // C1p[6]/eps^6, polynomial in eps2 of order 1
1080  -2200311, 533134, 860160,
1081  // C1p[7]/eps^7, polynomial in eps2 of order 0
1082  459485, 516096,
1083  // C1p[8]/eps^8, polynomial in eps2 of order 0
1084  109167851, 82575360,
1085  };
1086 #else
1087 #error "Bad value for GEOGRAPHICLIB_GEODESIC_ORDER"
1088 #endif
1089  GEOGRAPHICLIB_STATIC_ASSERT(sizeof(coeff) / sizeof(real) ==
1090  (nC1p_*nC1p_ + 7*nC1p_ - 2*(nC1p_/2)) / 4,
1091  "Coefficient array size mismatch in C1pf");
1092  real
1093  eps2 = Math::sq(eps),
1094  d = eps;
1095  int o = 0;
1096  for (int l = 1; l <= nC1p_; ++l) { // l is index of C1p[l]
1097  int m = (nC1p_ - l) / 2; // order of polynomial in eps^2
1098  c[l] = d * Math::polyval(m, coeff + o, eps2) / coeff[o + m + 1];
1099  o += m + 2;
1100  d *= eps;
1101  }
1102  // Post condition: o == sizeof(coeff) / sizeof(real)
1103  }
1104 
1105  // The scale factor A2-1 = mean value of (d/dsigma)I2 - 1
1106  Math::real Geodesic::A2m1f(real eps) {
1107  // Generated by Maxima on 2015-05-05 18:08:12-04:00
1108 #if GEOGRAPHICLIB_GEODESIC_ORDER/2 == 1
1109  static const real coeff[] = {
1110  // A2/(1-eps)-1, polynomial in eps2 of order 1
1111  1, 0, 4,
1112  };
1113 #elif GEOGRAPHICLIB_GEODESIC_ORDER/2 == 2
1114  static const real coeff[] = {
1115  // A2/(1-eps)-1, polynomial in eps2 of order 2
1116  9, 16, 0, 64,
1117  };
1118 #elif GEOGRAPHICLIB_GEODESIC_ORDER/2 == 3
1119  static const real coeff[] = {
1120  // A2/(1-eps)-1, polynomial in eps2 of order 3
1121  25, 36, 64, 0, 256,
1122  };
1123 #elif GEOGRAPHICLIB_GEODESIC_ORDER/2 == 4
1124  static const real coeff[] = {
1125  // A2/(1-eps)-1, polynomial in eps2 of order 4
1126  1225, 1600, 2304, 4096, 0, 16384,
1127  };
1128 #else
1129 #error "Bad value for GEOGRAPHICLIB_GEODESIC_ORDER"
1130 #endif
1131  GEOGRAPHICLIB_STATIC_ASSERT(sizeof(coeff) / sizeof(real) == nA2_/2 + 2,
1132  "Coefficient array size mismatch in A2m1f");
1133  int m = nA2_/2;
1134  real t = Math::polyval(m, coeff, Math::sq(eps)) / coeff[m + 1];
1135  return t * (1 - eps) - eps;
1136  }
1137 
1138  // The coefficients C2[l] in the Fourier expansion of B2
1139  void Geodesic::C2f(real eps, real c[]) {
1140  // Generated by Maxima on 2015-05-05 18:08:12-04:00
1141 #if GEOGRAPHICLIB_GEODESIC_ORDER == 3
1142  static const real coeff[] = {
1143  // C2[1]/eps^1, polynomial in eps2 of order 1
1144  1, 8, 16,
1145  // C2[2]/eps^2, polynomial in eps2 of order 0
1146  3, 16,
1147  // C2[3]/eps^3, polynomial in eps2 of order 0
1148  5, 48,
1149  };
1150 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 4
1151  static const real coeff[] = {
1152  // C2[1]/eps^1, polynomial in eps2 of order 1
1153  1, 8, 16,
1154  // C2[2]/eps^2, polynomial in eps2 of order 1
1155  1, 6, 32,
1156  // C2[3]/eps^3, polynomial in eps2 of order 0
1157  5, 48,
1158  // C2[4]/eps^4, polynomial in eps2 of order 0
1159  35, 512,
1160  };
1161 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 5
1162  static const real coeff[] = {
1163  // C2[1]/eps^1, polynomial in eps2 of order 2
1164  1, 2, 16, 32,
1165  // C2[2]/eps^2, polynomial in eps2 of order 1
1166  1, 6, 32,
1167  // C2[3]/eps^3, polynomial in eps2 of order 1
1168  15, 80, 768,
1169  // C2[4]/eps^4, polynomial in eps2 of order 0
1170  35, 512,
1171  // C2[5]/eps^5, polynomial in eps2 of order 0
1172  63, 1280,
1173  };
1174 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 6
1175  static const real coeff[] = {
1176  // C2[1]/eps^1, polynomial in eps2 of order 2
1177  1, 2, 16, 32,
1178  // C2[2]/eps^2, polynomial in eps2 of order 2
1179  35, 64, 384, 2048,
1180  // C2[3]/eps^3, polynomial in eps2 of order 1
1181  15, 80, 768,
1182  // C2[4]/eps^4, polynomial in eps2 of order 1
1183  7, 35, 512,
1184  // C2[5]/eps^5, polynomial in eps2 of order 0
1185  63, 1280,
1186  // C2[6]/eps^6, polynomial in eps2 of order 0
1187  77, 2048,
1188  };
1189 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 7
1190  static const real coeff[] = {
1191  // C2[1]/eps^1, polynomial in eps2 of order 3
1192  41, 64, 128, 1024, 2048,
1193  // C2[2]/eps^2, polynomial in eps2 of order 2
1194  35, 64, 384, 2048,
1195  // C2[3]/eps^3, polynomial in eps2 of order 2
1196  69, 120, 640, 6144,
1197  // C2[4]/eps^4, polynomial in eps2 of order 1
1198  7, 35, 512,
1199  // C2[5]/eps^5, polynomial in eps2 of order 1
1200  105, 504, 10240,
1201  // C2[6]/eps^6, polynomial in eps2 of order 0
1202  77, 2048,
1203  // C2[7]/eps^7, polynomial in eps2 of order 0
1204  429, 14336,
1205  };
1206 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 8
1207  static const real coeff[] = {
1208  // C2[1]/eps^1, polynomial in eps2 of order 3
1209  41, 64, 128, 1024, 2048,
1210  // C2[2]/eps^2, polynomial in eps2 of order 3
1211  47, 70, 128, 768, 4096,
1212  // C2[3]/eps^3, polynomial in eps2 of order 2
1213  69, 120, 640, 6144,
1214  // C2[4]/eps^4, polynomial in eps2 of order 2
1215  133, 224, 1120, 16384,
1216  // C2[5]/eps^5, polynomial in eps2 of order 1
1217  105, 504, 10240,
1218  // C2[6]/eps^6, polynomial in eps2 of order 1
1219  33, 154, 4096,
1220  // C2[7]/eps^7, polynomial in eps2 of order 0
1221  429, 14336,
1222  // C2[8]/eps^8, polynomial in eps2 of order 0
1223  6435, 262144,
1224  };
1225 #else
1226 #error "Bad value for GEOGRAPHICLIB_GEODESIC_ORDER"
1227 #endif
1228  GEOGRAPHICLIB_STATIC_ASSERT(sizeof(coeff) / sizeof(real) ==
1229  (nC2_*nC2_ + 7*nC2_ - 2*(nC2_/2)) / 4,
1230  "Coefficient array size mismatch in C2f");
1231  real
1232  eps2 = Math::sq(eps),
1233  d = eps;
1234  int o = 0;
1235  for (int l = 1; l <= nC2_; ++l) { // l is index of C2[l]
1236  int m = (nC2_ - l) / 2; // order of polynomial in eps^2
1237  c[l] = d * Math::polyval(m, coeff + o, eps2) / coeff[o + m + 1];
1238  o += m + 2;
1239  d *= eps;
1240  }
1241  // Post condition: o == sizeof(coeff) / sizeof(real)
1242  }
1243 
1244  // The scale factor A3 = mean value of (d/dsigma)I3
1245  void Geodesic::A3coeff() {
1246  // Generated by Maxima on 2015-05-05 18:08:13-04:00
1247 #if GEOGRAPHICLIB_GEODESIC_ORDER == 3
1248  static const real coeff[] = {
1249  // A3, coeff of eps^2, polynomial in n of order 0
1250  -1, 4,
1251  // A3, coeff of eps^1, polynomial in n of order 1
1252  1, -1, 2,
1253  // A3, coeff of eps^0, polynomial in n of order 0
1254  1, 1,
1255  };
1256 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 4
1257  static const real coeff[] = {
1258  // A3, coeff of eps^3, polynomial in n of order 0
1259  -1, 16,
1260  // A3, coeff of eps^2, polynomial in n of order 1
1261  -1, -2, 8,
1262  // A3, coeff of eps^1, polynomial in n of order 1
1263  1, -1, 2,
1264  // A3, coeff of eps^0, polynomial in n of order 0
1265  1, 1,
1266  };
1267 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 5
1268  static const real coeff[] = {
1269  // A3, coeff of eps^4, polynomial in n of order 0
1270  -3, 64,
1271  // A3, coeff of eps^3, polynomial in n of order 1
1272  -3, -1, 16,
1273  // A3, coeff of eps^2, polynomial in n of order 2
1274  3, -1, -2, 8,
1275  // A3, coeff of eps^1, polynomial in n of order 1
1276  1, -1, 2,
1277  // A3, coeff of eps^0, polynomial in n of order 0
1278  1, 1,
1279  };
1280 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 6
1281  static const real coeff[] = {
1282  // A3, coeff of eps^5, polynomial in n of order 0
1283  -3, 128,
1284  // A3, coeff of eps^4, polynomial in n of order 1
1285  -2, -3, 64,
1286  // A3, coeff of eps^3, polynomial in n of order 2
1287  -1, -3, -1, 16,
1288  // A3, coeff of eps^2, polynomial in n of order 2
1289  3, -1, -2, 8,
1290  // A3, coeff of eps^1, polynomial in n of order 1
1291  1, -1, 2,
1292  // A3, coeff of eps^0, polynomial in n of order 0
1293  1, 1,
1294  };
1295 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 7
1296  static const real coeff[] = {
1297  // A3, coeff of eps^6, polynomial in n of order 0
1298  -5, 256,
1299  // A3, coeff of eps^5, polynomial in n of order 1
1300  -5, -3, 128,
1301  // A3, coeff of eps^4, polynomial in n of order 2
1302  -10, -2, -3, 64,
1303  // A3, coeff of eps^3, polynomial in n of order 3
1304  5, -1, -3, -1, 16,
1305  // A3, coeff of eps^2, polynomial in n of order 2
1306  3, -1, -2, 8,
1307  // A3, coeff of eps^1, polynomial in n of order 1
1308  1, -1, 2,
1309  // A3, coeff of eps^0, polynomial in n of order 0
1310  1, 1,
1311  };
1312 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 8
1313  static const real coeff[] = {
1314  // A3, coeff of eps^7, polynomial in n of order 0
1315  -25, 2048,
1316  // A3, coeff of eps^6, polynomial in n of order 1
1317  -15, -20, 1024,
1318  // A3, coeff of eps^5, polynomial in n of order 2
1319  -5, -10, -6, 256,
1320  // A3, coeff of eps^4, polynomial in n of order 3
1321  -5, -20, -4, -6, 128,
1322  // A3, coeff of eps^3, polynomial in n of order 3
1323  5, -1, -3, -1, 16,
1324  // A3, coeff of eps^2, polynomial in n of order 2
1325  3, -1, -2, 8,
1326  // A3, coeff of eps^1, polynomial in n of order 1
1327  1, -1, 2,
1328  // A3, coeff of eps^0, polynomial in n of order 0
1329  1, 1,
1330  };
1331 #else
1332 #error "Bad value for GEOGRAPHICLIB_GEODESIC_ORDER"
1333 #endif
1334  GEOGRAPHICLIB_STATIC_ASSERT(sizeof(coeff) / sizeof(real) ==
1335  (nA3_*nA3_ + 7*nA3_ - 2*(nA3_/2)) / 4,
1336  "Coefficient array size mismatch in A3f");
1337  int o = 0, k = 0;
1338  for (int j = nA3_ - 1; j >= 0; --j) { // coeff of eps^j
1339  int m = min(nA3_ - j - 1, j); // order of polynomial in n
1340  _A3x[k++] = Math::polyval(m, coeff + o, _n) / coeff[o + m + 1];
1341  o += m + 2;
1342  }
1343  // Post condition: o == sizeof(coeff) / sizeof(real) && k == nA3x_
1344  }
1345 
1346  // The coefficients C3[l] in the Fourier expansion of B3
1347  void Geodesic::C3coeff() {
1348  // Generated by Maxima on 2015-05-05 18:08:13-04:00
1349 #if GEOGRAPHICLIB_GEODESIC_ORDER == 3
1350  static const real coeff[] = {
1351  // C3[1], coeff of eps^2, polynomial in n of order 0
1352  1, 8,
1353  // C3[1], coeff of eps^1, polynomial in n of order 1
1354  -1, 1, 4,
1355  // C3[2], coeff of eps^2, polynomial in n of order 0
1356  1, 16,
1357  };
1358 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 4
1359  static const real coeff[] = {
1360  // C3[1], coeff of eps^3, polynomial in n of order 0
1361  3, 64,
1362  // C3[1], coeff of eps^2, polynomial in n of order 1
1363  // This is a case where a leading 0 term has been inserted to maintain the
1364  // pattern in the orders of the polynomials.
1365  0, 1, 8,
1366  // C3[1], coeff of eps^1, polynomial in n of order 1
1367  -1, 1, 4,
1368  // C3[2], coeff of eps^3, polynomial in n of order 0
1369  3, 64,
1370  // C3[2], coeff of eps^2, polynomial in n of order 1
1371  -3, 2, 32,
1372  // C3[3], coeff of eps^3, polynomial in n of order 0
1373  5, 192,
1374  };
1375 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 5
1376  static const real coeff[] = {
1377  // C3[1], coeff of eps^4, polynomial in n of order 0
1378  5, 128,
1379  // C3[1], coeff of eps^3, polynomial in n of order 1
1380  3, 3, 64,
1381  // C3[1], coeff of eps^2, polynomial in n of order 2
1382  -1, 0, 1, 8,
1383  // C3[1], coeff of eps^1, polynomial in n of order 1
1384  -1, 1, 4,
1385  // C3[2], coeff of eps^4, polynomial in n of order 0
1386  3, 128,
1387  // C3[2], coeff of eps^3, polynomial in n of order 1
1388  -2, 3, 64,
1389  // C3[2], coeff of eps^2, polynomial in n of order 2
1390  1, -3, 2, 32,
1391  // C3[3], coeff of eps^4, polynomial in n of order 0
1392  3, 128,
1393  // C3[3], coeff of eps^3, polynomial in n of order 1
1394  -9, 5, 192,
1395  // C3[4], coeff of eps^4, polynomial in n of order 0
1396  7, 512,
1397  };
1398 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 6
1399  static const real coeff[] = {
1400  // C3[1], coeff of eps^5, polynomial in n of order 0
1401  3, 128,
1402  // C3[1], coeff of eps^4, polynomial in n of order 1
1403  2, 5, 128,
1404  // C3[1], coeff of eps^3, polynomial in n of order 2
1405  -1, 3, 3, 64,
1406  // C3[1], coeff of eps^2, polynomial in n of order 2
1407  -1, 0, 1, 8,
1408  // C3[1], coeff of eps^1, polynomial in n of order 1
1409  -1, 1, 4,
1410  // C3[2], coeff of eps^5, polynomial in n of order 0
1411  5, 256,
1412  // C3[2], coeff of eps^4, polynomial in n of order 1
1413  1, 3, 128,
1414  // C3[2], coeff of eps^3, polynomial in n of order 2
1415  -3, -2, 3, 64,
1416  // C3[2], coeff of eps^2, polynomial in n of order 2
1417  1, -3, 2, 32,
1418  // C3[3], coeff of eps^5, polynomial in n of order 0
1419  7, 512,
1420  // C3[3], coeff of eps^4, polynomial in n of order 1
1421  -10, 9, 384,
1422  // C3[3], coeff of eps^3, polynomial in n of order 2
1423  5, -9, 5, 192,
1424  // C3[4], coeff of eps^5, polynomial in n of order 0
1425  7, 512,
1426  // C3[4], coeff of eps^4, polynomial in n of order 1
1427  -14, 7, 512,
1428  // C3[5], coeff of eps^5, polynomial in n of order 0
1429  21, 2560,
1430  };
1431 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 7
1432  static const real coeff[] = {
1433  // C3[1], coeff of eps^6, polynomial in n of order 0
1434  21, 1024,
1435  // C3[1], coeff of eps^5, polynomial in n of order 1
1436  11, 12, 512,
1437  // C3[1], coeff of eps^4, polynomial in n of order 2
1438  2, 2, 5, 128,
1439  // C3[1], coeff of eps^3, polynomial in n of order 3
1440  -5, -1, 3, 3, 64,
1441  // C3[1], coeff of eps^2, polynomial in n of order 2
1442  -1, 0, 1, 8,
1443  // C3[1], coeff of eps^1, polynomial in n of order 1
1444  -1, 1, 4,
1445  // C3[2], coeff of eps^6, polynomial in n of order 0
1446  27, 2048,
1447  // C3[2], coeff of eps^5, polynomial in n of order 1
1448  1, 5, 256,
1449  // C3[2], coeff of eps^4, polynomial in n of order 2
1450  -9, 2, 6, 256,
1451  // C3[2], coeff of eps^3, polynomial in n of order 3
1452  2, -3, -2, 3, 64,
1453  // C3[2], coeff of eps^2, polynomial in n of order 2
1454  1, -3, 2, 32,
1455  // C3[3], coeff of eps^6, polynomial in n of order 0
1456  3, 256,
1457  // C3[3], coeff of eps^5, polynomial in n of order 1
1458  -4, 21, 1536,
1459  // C3[3], coeff of eps^4, polynomial in n of order 2
1460  -6, -10, 9, 384,
1461  // C3[3], coeff of eps^3, polynomial in n of order 3
1462  -1, 5, -9, 5, 192,
1463  // C3[4], coeff of eps^6, polynomial in n of order 0
1464  9, 1024,
1465  // C3[4], coeff of eps^5, polynomial in n of order 1
1466  -10, 7, 512,
1467  // C3[4], coeff of eps^4, polynomial in n of order 2
1468  10, -14, 7, 512,
1469  // C3[5], coeff of eps^6, polynomial in n of order 0
1470  9, 1024,
1471  // C3[5], coeff of eps^5, polynomial in n of order 1
1472  -45, 21, 2560,
1473  // C3[6], coeff of eps^6, polynomial in n of order 0
1474  11, 2048,
1475  };
1476 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 8
1477  static const real coeff[] = {
1478  // C3[1], coeff of eps^7, polynomial in n of order 0
1479  243, 16384,
1480  // C3[1], coeff of eps^6, polynomial in n of order 1
1481  10, 21, 1024,
1482  // C3[1], coeff of eps^5, polynomial in n of order 2
1483  3, 11, 12, 512,
1484  // C3[1], coeff of eps^4, polynomial in n of order 3
1485  -2, 2, 2, 5, 128,
1486  // C3[1], coeff of eps^3, polynomial in n of order 3
1487  -5, -1, 3, 3, 64,
1488  // C3[1], coeff of eps^2, polynomial in n of order 2
1489  -1, 0, 1, 8,
1490  // C3[1], coeff of eps^1, polynomial in n of order 1
1491  -1, 1, 4,
1492  // C3[2], coeff of eps^7, polynomial in n of order 0
1493  187, 16384,
1494  // C3[2], coeff of eps^6, polynomial in n of order 1
1495  69, 108, 8192,
1496  // C3[2], coeff of eps^5, polynomial in n of order 2
1497  -2, 1, 5, 256,
1498  // C3[2], coeff of eps^4, polynomial in n of order 3
1499  -6, -9, 2, 6, 256,
1500  // C3[2], coeff of eps^3, polynomial in n of order 3
1501  2, -3, -2, 3, 64,
1502  // C3[2], coeff of eps^2, polynomial in n of order 2
1503  1, -3, 2, 32,
1504  // C3[3], coeff of eps^7, polynomial in n of order 0
1505  139, 16384,
1506  // C3[3], coeff of eps^6, polynomial in n of order 1
1507  -1, 12, 1024,
1508  // C3[3], coeff of eps^5, polynomial in n of order 2
1509  -77, -8, 42, 3072,
1510  // C3[3], coeff of eps^4, polynomial in n of order 3
1511  10, -6, -10, 9, 384,
1512  // C3[3], coeff of eps^3, polynomial in n of order 3
1513  -1, 5, -9, 5, 192,
1514  // C3[4], coeff of eps^7, polynomial in n of order 0
1515  127, 16384,
1516  // C3[4], coeff of eps^6, polynomial in n of order 1
1517  -43, 72, 8192,
1518  // C3[4], coeff of eps^5, polynomial in n of order 2
1519  -7, -40, 28, 2048,
1520  // C3[4], coeff of eps^4, polynomial in n of order 3
1521  -7, 20, -28, 14, 1024,
1522  // C3[5], coeff of eps^7, polynomial in n of order 0
1523  99, 16384,
1524  // C3[5], coeff of eps^6, polynomial in n of order 1
1525  -15, 9, 1024,
1526  // C3[5], coeff of eps^5, polynomial in n of order 2
1527  75, -90, 42, 5120,
1528  // C3[6], coeff of eps^7, polynomial in n of order 0
1529  99, 16384,
1530  // C3[6], coeff of eps^6, polynomial in n of order 1
1531  -99, 44, 8192,
1532  // C3[7], coeff of eps^7, polynomial in n of order 0
1533  429, 114688,
1534  };
1535 #else
1536 #error "Bad value for GEOGRAPHICLIB_GEODESIC_ORDER"
1537 #endif
1538  GEOGRAPHICLIB_STATIC_ASSERT(sizeof(coeff) / sizeof(real) ==
1539  ((nC3_-1)*(nC3_*nC3_ + 7*nC3_ - 2*(nC3_/2)))/8,
1540  "Coefficient array size mismatch in C3coeff");
1541  int o = 0, k = 0;
1542  for (int l = 1; l < nC3_; ++l) { // l is index of C3[l]
1543  for (int j = nC3_ - 1; j >= l; --j) { // coeff of eps^j
1544  int m = min(nC3_ - j - 1, j); // order of polynomial in n
1545  _C3x[k++] = Math::polyval(m, coeff + o, _n) / coeff[o + m + 1];
1546  o += m + 2;
1547  }
1548  }
1549  // Post condition: o == sizeof(coeff) / sizeof(real) && k == nC3x_
1550  }
1551 
1552  void Geodesic::C4coeff() {
1553  // Generated by Maxima on 2015-05-05 18:08:13-04:00
1554 #if GEOGRAPHICLIB_GEODESIC_ORDER == 3
1555  static const real coeff[] = {
1556  // C4[0], coeff of eps^2, polynomial in n of order 0
1557  -2, 105,
1558  // C4[0], coeff of eps^1, polynomial in n of order 1
1559  16, -7, 35,
1560  // C4[0], coeff of eps^0, polynomial in n of order 2
1561  8, -28, 70, 105,
1562  // C4[1], coeff of eps^2, polynomial in n of order 0
1563  -2, 105,
1564  // C4[1], coeff of eps^1, polynomial in n of order 1
1565  -16, 7, 315,
1566  // C4[2], coeff of eps^2, polynomial in n of order 0
1567  4, 525,
1568  };
1569 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 4
1570  static const real coeff[] = {
1571  // C4[0], coeff of eps^3, polynomial in n of order 0
1572  11, 315,
1573  // C4[0], coeff of eps^2, polynomial in n of order 1
1574  -32, -6, 315,
1575  // C4[0], coeff of eps^1, polynomial in n of order 2
1576  -32, 48, -21, 105,
1577  // C4[0], coeff of eps^0, polynomial in n of order 3
1578  4, 24, -84, 210, 315,
1579  // C4[1], coeff of eps^3, polynomial in n of order 0
1580  -1, 105,
1581  // C4[1], coeff of eps^2, polynomial in n of order 1
1582  64, -18, 945,
1583  // C4[1], coeff of eps^1, polynomial in n of order 2
1584  32, -48, 21, 945,
1585  // C4[2], coeff of eps^3, polynomial in n of order 0
1586  -8, 1575,
1587  // C4[2], coeff of eps^2, polynomial in n of order 1
1588  -32, 12, 1575,
1589  // C4[3], coeff of eps^3, polynomial in n of order 0
1590  8, 2205,
1591  };
1592 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 5
1593  static const real coeff[] = {
1594  // C4[0], coeff of eps^4, polynomial in n of order 0
1595  4, 1155,
1596  // C4[0], coeff of eps^3, polynomial in n of order 1
1597  -368, 121, 3465,
1598  // C4[0], coeff of eps^2, polynomial in n of order 2
1599  1088, -352, -66, 3465,
1600  // C4[0], coeff of eps^1, polynomial in n of order 3
1601  48, -352, 528, -231, 1155,
1602  // C4[0], coeff of eps^0, polynomial in n of order 4
1603  16, 44, 264, -924, 2310, 3465,
1604  // C4[1], coeff of eps^4, polynomial in n of order 0
1605  4, 1155,
1606  // C4[1], coeff of eps^3, polynomial in n of order 1
1607  80, -99, 10395,
1608  // C4[1], coeff of eps^2, polynomial in n of order 2
1609  -896, 704, -198, 10395,
1610  // C4[1], coeff of eps^1, polynomial in n of order 3
1611  -48, 352, -528, 231, 10395,
1612  // C4[2], coeff of eps^4, polynomial in n of order 0
1613  -8, 1925,
1614  // C4[2], coeff of eps^3, polynomial in n of order 1
1615  384, -88, 17325,
1616  // C4[2], coeff of eps^2, polynomial in n of order 2
1617  320, -352, 132, 17325,
1618  // C4[3], coeff of eps^4, polynomial in n of order 0
1619  -16, 8085,
1620  // C4[3], coeff of eps^3, polynomial in n of order 1
1621  -256, 88, 24255,
1622  // C4[4], coeff of eps^4, polynomial in n of order 0
1623  64, 31185,
1624  };
1625 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 6
1626  static const real coeff[] = {
1627  // C4[0], coeff of eps^5, polynomial in n of order 0
1628  97, 15015,
1629  // C4[0], coeff of eps^4, polynomial in n of order 1
1630  1088, 156, 45045,
1631  // C4[0], coeff of eps^3, polynomial in n of order 2
1632  -224, -4784, 1573, 45045,
1633  // C4[0], coeff of eps^2, polynomial in n of order 3
1634  -10656, 14144, -4576, -858, 45045,
1635  // C4[0], coeff of eps^1, polynomial in n of order 4
1636  64, 624, -4576, 6864, -3003, 15015,
1637  // C4[0], coeff of eps^0, polynomial in n of order 5
1638  100, 208, 572, 3432, -12012, 30030, 45045,
1639  // C4[1], coeff of eps^5, polynomial in n of order 0
1640  1, 9009,
1641  // C4[1], coeff of eps^4, polynomial in n of order 1
1642  -2944, 468, 135135,
1643  // C4[1], coeff of eps^3, polynomial in n of order 2
1644  5792, 1040, -1287, 135135,
1645  // C4[1], coeff of eps^2, polynomial in n of order 3
1646  5952, -11648, 9152, -2574, 135135,
1647  // C4[1], coeff of eps^1, polynomial in n of order 4
1648  -64, -624, 4576, -6864, 3003, 135135,
1649  // C4[2], coeff of eps^5, polynomial in n of order 0
1650  8, 10725,
1651  // C4[2], coeff of eps^4, polynomial in n of order 1
1652  1856, -936, 225225,
1653  // C4[2], coeff of eps^3, polynomial in n of order 2
1654  -8448, 4992, -1144, 225225,
1655  // C4[2], coeff of eps^2, polynomial in n of order 3
1656  -1440, 4160, -4576, 1716, 225225,
1657  // C4[3], coeff of eps^5, polynomial in n of order 0
1658  -136, 63063,
1659  // C4[3], coeff of eps^4, polynomial in n of order 1
1660  1024, -208, 105105,
1661  // C4[3], coeff of eps^3, polynomial in n of order 2
1662  3584, -3328, 1144, 315315,
1663  // C4[4], coeff of eps^5, polynomial in n of order 0
1664  -128, 135135,
1665  // C4[4], coeff of eps^4, polynomial in n of order 1
1666  -2560, 832, 405405,
1667  // C4[5], coeff of eps^5, polynomial in n of order 0
1668  128, 99099,
1669  };
1670 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 7
1671  static const real coeff[] = {
1672  // C4[0], coeff of eps^6, polynomial in n of order 0
1673  10, 9009,
1674  // C4[0], coeff of eps^5, polynomial in n of order 1
1675  -464, 291, 45045,
1676  // C4[0], coeff of eps^4, polynomial in n of order 2
1677  -4480, 1088, 156, 45045,
1678  // C4[0], coeff of eps^3, polynomial in n of order 3
1679  10736, -224, -4784, 1573, 45045,
1680  // C4[0], coeff of eps^2, polynomial in n of order 4
1681  1664, -10656, 14144, -4576, -858, 45045,
1682  // C4[0], coeff of eps^1, polynomial in n of order 5
1683  16, 64, 624, -4576, 6864, -3003, 15015,
1684  // C4[0], coeff of eps^0, polynomial in n of order 6
1685  56, 100, 208, 572, 3432, -12012, 30030, 45045,
1686  // C4[1], coeff of eps^6, polynomial in n of order 0
1687  10, 9009,
1688  // C4[1], coeff of eps^5, polynomial in n of order 1
1689  112, 15, 135135,
1690  // C4[1], coeff of eps^4, polynomial in n of order 2
1691  3840, -2944, 468, 135135,
1692  // C4[1], coeff of eps^3, polynomial in n of order 3
1693  -10704, 5792, 1040, -1287, 135135,
1694  // C4[1], coeff of eps^2, polynomial in n of order 4
1695  -768, 5952, -11648, 9152, -2574, 135135,
1696  // C4[1], coeff of eps^1, polynomial in n of order 5
1697  -16, -64, -624, 4576, -6864, 3003, 135135,
1698  // C4[2], coeff of eps^6, polynomial in n of order 0
1699  -4, 25025,
1700  // C4[2], coeff of eps^5, polynomial in n of order 1
1701  -1664, 168, 225225,
1702  // C4[2], coeff of eps^4, polynomial in n of order 2
1703  1664, 1856, -936, 225225,
1704  // C4[2], coeff of eps^3, polynomial in n of order 3
1705  6784, -8448, 4992, -1144, 225225,
1706  // C4[2], coeff of eps^2, polynomial in n of order 4
1707  128, -1440, 4160, -4576, 1716, 225225,
1708  // C4[3], coeff of eps^6, polynomial in n of order 0
1709  64, 315315,
1710  // C4[3], coeff of eps^5, polynomial in n of order 1
1711  1792, -680, 315315,
1712  // C4[3], coeff of eps^4, polynomial in n of order 2
1713  -2048, 1024, -208, 105105,
1714  // C4[3], coeff of eps^3, polynomial in n of order 3
1715  -1792, 3584, -3328, 1144, 315315,
1716  // C4[4], coeff of eps^6, polynomial in n of order 0
1717  -512, 405405,
1718  // C4[4], coeff of eps^5, polynomial in n of order 1
1719  2048, -384, 405405,
1720  // C4[4], coeff of eps^4, polynomial in n of order 2
1721  3072, -2560, 832, 405405,
1722  // C4[5], coeff of eps^6, polynomial in n of order 0
1723  -256, 495495,
1724  // C4[5], coeff of eps^5, polynomial in n of order 1
1725  -2048, 640, 495495,
1726  // C4[6], coeff of eps^6, polynomial in n of order 0
1727  512, 585585,
1728  };
1729 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 8
1730  static const real coeff[] = {
1731  // C4[0], coeff of eps^7, polynomial in n of order 0
1732  193, 85085,
1733  // C4[0], coeff of eps^6, polynomial in n of order 1
1734  4192, 850, 765765,
1735  // C4[0], coeff of eps^5, polynomial in n of order 2
1736  20960, -7888, 4947, 765765,
1737  // C4[0], coeff of eps^4, polynomial in n of order 3
1738  12480, -76160, 18496, 2652, 765765,
1739  // C4[0], coeff of eps^3, polynomial in n of order 4
1740  -154048, 182512, -3808, -81328, 26741, 765765,
1741  // C4[0], coeff of eps^2, polynomial in n of order 5
1742  3232, 28288, -181152, 240448, -77792, -14586, 765765,
1743  // C4[0], coeff of eps^1, polynomial in n of order 6
1744  96, 272, 1088, 10608, -77792, 116688, -51051, 255255,
1745  // C4[0], coeff of eps^0, polynomial in n of order 7
1746  588, 952, 1700, 3536, 9724, 58344, -204204, 510510, 765765,
1747  // C4[1], coeff of eps^7, polynomial in n of order 0
1748  349, 2297295,
1749  // C4[1], coeff of eps^6, polynomial in n of order 1
1750  -1472, 510, 459459,
1751  // C4[1], coeff of eps^5, polynomial in n of order 2
1752  -39840, 1904, 255, 2297295,
1753  // C4[1], coeff of eps^4, polynomial in n of order 3
1754  52608, 65280, -50048, 7956, 2297295,
1755  // C4[1], coeff of eps^3, polynomial in n of order 4
1756  103744, -181968, 98464, 17680, -21879, 2297295,
1757  // C4[1], coeff of eps^2, polynomial in n of order 5
1758  -1344, -13056, 101184, -198016, 155584, -43758, 2297295,
1759  // C4[1], coeff of eps^1, polynomial in n of order 6
1760  -96, -272, -1088, -10608, 77792, -116688, 51051, 2297295,
1761  // C4[2], coeff of eps^7, polynomial in n of order 0
1762  464, 1276275,
1763  // C4[2], coeff of eps^6, polynomial in n of order 1
1764  -928, -612, 3828825,
1765  // C4[2], coeff of eps^5, polynomial in n of order 2
1766  64256, -28288, 2856, 3828825,
1767  // C4[2], coeff of eps^4, polynomial in n of order 3
1768  -126528, 28288, 31552, -15912, 3828825,
1769  // C4[2], coeff of eps^3, polynomial in n of order 4
1770  -41472, 115328, -143616, 84864, -19448, 3828825,
1771  // C4[2], coeff of eps^2, polynomial in n of order 5
1772  160, 2176, -24480, 70720, -77792, 29172, 3828825,
1773  // C4[3], coeff of eps^7, polynomial in n of order 0
1774  -16, 97461,
1775  // C4[3], coeff of eps^6, polynomial in n of order 1
1776  -16384, 1088, 5360355,
1777  // C4[3], coeff of eps^5, polynomial in n of order 2
1778  -2560, 30464, -11560, 5360355,
1779  // C4[3], coeff of eps^4, polynomial in n of order 3
1780  35840, -34816, 17408, -3536, 1786785,
1781  // C4[3], coeff of eps^3, polynomial in n of order 4
1782  7168, -30464, 60928, -56576, 19448, 5360355,
1783  // C4[4], coeff of eps^7, polynomial in n of order 0
1784  128, 2297295,
1785  // C4[4], coeff of eps^6, polynomial in n of order 1
1786  26624, -8704, 6891885,
1787  // C4[4], coeff of eps^5, polynomial in n of order 2
1788  -77824, 34816, -6528, 6891885,
1789  // C4[4], coeff of eps^4, polynomial in n of order 3
1790  -32256, 52224, -43520, 14144, 6891885,
1791  // C4[5], coeff of eps^7, polynomial in n of order 0
1792  -6784, 8423415,
1793  // C4[5], coeff of eps^6, polynomial in n of order 1
1794  24576, -4352, 8423415,
1795  // C4[5], coeff of eps^5, polynomial in n of order 2
1796  45056, -34816, 10880, 8423415,
1797  // C4[6], coeff of eps^7, polynomial in n of order 0
1798  -1024, 3318315,
1799  // C4[6], coeff of eps^6, polynomial in n of order 1
1800  -28672, 8704, 9954945,
1801  // C4[7], coeff of eps^7, polynomial in n of order 0
1802  1024, 1640925,
1803  };
1804 #else
1805 #error "Bad value for GEOGRAPHICLIB_GEODESIC_ORDER"
1806 #endif
1807  GEOGRAPHICLIB_STATIC_ASSERT(sizeof(coeff) / sizeof(real) ==
1808  (nC4_ * (nC4_ + 1) * (nC4_ + 5)) / 6,
1809  "Coefficient array size mismatch in C4coeff");
1810  int o = 0, k = 0;
1811  for (int l = 0; l < nC4_; ++l) { // l is index of C4[l]
1812  for (int j = nC4_ - 1; j >= l; --j) { // coeff of eps^j
1813  int m = nC4_ - j - 1; // order of polynomial in n
1814  _C4x[k++] = Math::polyval(m, coeff + o, _n) / coeff[o + m + 1];
1815  o += m + 2;
1816  }
1817  }
1818  // Post condition: o == sizeof(coeff) / sizeof(real) && k == nC4x_
1819  }
1820 
1821 } // namespace GeographicLib
static T AngNormalize(T x)
Definition: Math.hpp:445
Geodesic(real a, real f)
Definition: Geodesic.cpp:42
Header for GeographicLib::GeodesicLine class.
static T pi()
Definition: Math.hpp:214
GeodesicLine Line(real lat1, real lon1, real azi1, unsigned caps=ALL) const
Definition: Geodesic.cpp:118
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
static T cbrt(T x)
Definition: Math.hpp:357
static const Geodesic & WGS84()
Definition: Geodesic.cpp:89
Math::real GenInverse(real lat1, real lon1, real lat2, real lon2, unsigned outmask, real &s12, real &azi1, real &azi2, real &m12, real &M12, real &M21, real &S12) const
Definition: Geodesic.cpp:136
static bool isfinite(T x)
Definition: Math.hpp:614
Mathematical functions needed by GeographicLib.
Definition: Math.hpp:102
static void norm(T &x, T &y)
Definition: Math.hpp:392
#define GEOGRAPHICLIB_VOLATILE
Definition: Math.hpp:84
Header for GeographicLib::Geodesic class.
friend class GeodesicLine
Definition: Geodesic.hpp:174
static T hypot(T x, T y)
Definition: Math.hpp:255
Math::real GenDirect(real lat1, real lon1, real azi1, bool arcmode, real s12_a12, unsigned outmask, real &lat2, real &lon2, real &azi2, real &s12, real &m12, real &M12, real &M21, real &S12) const
Definition: Geodesic.cpp:123
static T sq(T x)
Definition: Math.hpp:244
static T atan2d(T y, T x)
Definition: Math.hpp:551
static T polyval(int N, const T p[], T x)
Definition: Math.hpp:433
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static T degree()
Definition: Math.hpp:228
static T AngDiff(T x, T y)
Definition: Math.hpp:475
Exception handling for GeographicLib.
Definition: Constants.hpp:382
Geodesic calculations
Definition: Geodesic.hpp:171
static T AngRound(T x)
Definition: Math.hpp:498
#define GEOGRAPHICLIB_PANIC
Definition: Math.hpp:87