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