GeographicLib  1.43
Math.cpp
Go to the documentation of this file.
1 /**
2  * \file Math.cpp
3  * \brief Implementation for GeographicLib::Math class
4  *
5  * Copyright (c) Charles Karney (2015) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
8  **********************************************************************/
9 
10 #include <GeographicLib/Math.hpp>
11 
12 #if defined(_MSC_VER)
13 // Squelch warnings about constant conditional expressions
14 # pragma warning (disable: 4127)
15 #endif
16 
17 namespace GeographicLib {
18 
19  /// \cond SKIP
20 
21  using namespace std;
22 
23  template<typename T> T Math::eatanhe(T x, T es) {
24  return es > T(0) ? es * atanh(es * x) : -es * atan(es * x);
25  }
26 
27  template<typename T> T Math::taupf(T tau, T es) {
28  T tau1 = hypot(T(1), tau),
29  sig = sinh( eatanhe(tau / tau1, es ) );
30  return hypot(T(1), sig) * tau - sig * tau1;
31  }
32 
33  template<typename T> T Math::tauf(T taup, T es) {
34  static const int numit = 5;
35  static const T tol = sqrt(numeric_limits<T>::epsilon()) / T(10);
36  T e2m = T(1) - sq(es),
37  // To lowest order in e^2, taup = (1 - e^2) * tau = _e2m * tau; so use
38  // tau = taup/_e2m as a starting guess. (This starting guess is the
39  // geocentric latitude which, to first order in the flattening, is equal
40  // to the conformal latitude.) Only 1 iteration is needed for |lat| <
41  // 3.35 deg, otherwise 2 iterations are needed. If, instead, tau = taup
42  // is used the mean number of iterations increases to 1.99 (2 iterations
43  // are needed except near tau = 0).
44  tau = taup/e2m,
45  stol = tol * max(T(1), abs(taup));
46  // min iterations = 1, max iterations = 2; mean = 1.94
47  for (int i = 0; i < numit || GEOGRAPHICLIB_PANIC; ++i) {
48  T taupa = taupf(tau, es),
49  dtau = (taup - taupa) * (1 + e2m * sq(tau)) /
50  ( e2m * hypot(T(1), tau) * hypot(T(1), taupa) );
51  tau += dtau;
52  if (!(abs(dtau) >= stol))
53  break;
54  }
55  return tau;
56  }
57 
58  // Instantiate
59  template Math::real Math::eatanhe<Math::real>(Math::real, Math::real);
60  template Math::real Math::taupf<Math::real>(Math::real, Math::real);
61  template Math::real Math::tauf<Math::real>(Math::real, Math::real);
62 
63  /// \endcond
64 
65 } // namespace GeographicLib
static T eatanhe(T x, T es)
Header for GeographicLib::Math class.
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static T tauf(T taup, T es)
static T taupf(T tau, T es)
#define GEOGRAPHICLIB_PANIC
Definition: Math.hpp:87