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