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