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