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