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