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