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