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