GeographicLib 2.3

DAuxLatitude.cpp
Go to the documentation of this file.
1/**
2 * \file DAuxLatitude.cpp
3 * \brief Implementation for the GeographicLib::DAuxLatitude class.
4 *
5 * \note This is just sample code. It is not part of GeographicLib itself.
6 *
7 * This file is an implementation of the methods described in
8 * - C. F. F. Karney,
9 * <a href="https://doi.org/10.1080/00396265.2023.2217604">
10 * On auxiliary latitudes,</a>
11 * Survey Review (2023);
12 * preprint
13 * <a href="https://arxiv.org/abs/2212.05818">arXiv:2212.05818</a>.
14 * .
15 * Copyright (c) Charles Karney (2022-2023) <karney@alum.mit.edu> and licensed
16 * under the MIT/X11 License. For more information, see
17 * https://geographiclib.sourceforge.io/
18 **********************************************************************/
19
20#include <GeographicLib/DAuxLatitude.hpp>
21#include <GeographicLib/EllipticFunction.hpp>
22
23#if defined(_MSC_VER)
24// Squelch warnings about constant conditional expressions
25# pragma warning (disable: 4127)
26#endif
27
28 namespace GeographicLib {
29
30 using namespace std;
31
32 Math::real DAuxLatitude::DRectifying(const AuxAngle& phi1,
33 const AuxAngle& phi2)
34 const {
35 // Stipulate that phi1 and phi2 are in [-90d, 90d]
36 real x = phi1.radians(), y = phi2.radians();
37 if (x == y) {
38 real d;
39 AuxAngle mu1(base::Rectifying(phi1, &d));
40 real tphi1 = phi1.tan(), tmu1 = mu1.tan();
41 return
42 isfinite(tphi1) ? d * Math::sq(base::sc(tphi1)/base::sc(tmu1)) : 1/d;
43 } else if (x * y < 0)
44 return (base::Rectifying(phi2).radians() -
45 base::Rectifying(phi1).radians()) / (y - x);
46 else {
47 AuxAngle bet1(base::Parametric(phi1)), bet2(base::Parametric(phi2));
48 real dEdbet = DE(bet1, bet2), dbetdphi = DParametric(phi1, phi2);
49 return base::_b * dEdbet / base::RectifyingRadius(true) * dbetdphi;
50 }
51 }
52
53 Math::real DAuxLatitude::DParametric(const AuxAngle& phi1,
54 const AuxAngle& phi2)
55 const {
56 real tx = phi1.tan(), ty = phi2.tan(), r;
57 // DbetaDphi = Datan(fm1*tx, fm1*ty) * fm1 / Datan(tx, ty)
58 // Datan(x, y) = 1/(1 + x^2), for x = y
59 // = (atan(y) - atan(x)) / (y-x), for x*y < 0
60 // = atan( (y-x) / (1 + x*y) ) / (y-x), for x*y > 0
61 if (!(tx * ty >= 0)) // This includes, e.g., tx = 0, ty = inf
62 r = (atan(base::_fm1 * ty) - atan(base::_fm1 * tx)) /
63 (atan(ty) - atan(tx));
64 else if (tx == ty) { // This includes the case tx = ty = inf
65 tx *= tx;
66 if (tx <= 1)
67 r = base::_fm1 * (1 + tx) / (1 + base::_e2m1 * tx);
68 else {
69 tx = 1/tx;
70 r = base::_fm1 * (1 + tx) / (base::_e2m1 + tx);
71 }
72 } else {
73 if (tx * ty <= 1)
74 r = atan2(base::_fm1 * (ty - tx), 1 + base::_e2m1 * tx * ty)
75 / atan2( ty - tx , 1 + tx * ty);
76 else {
77 tx = 1/tx; ty = 1/ty;
78 r = atan2(base::_fm1 * (ty - tx), base::_e2m1 + tx * ty)
79 / atan2( ty - tx , 1 + tx * ty);
80 }
81 }
82 return r;
83 }
84
85 Math::real DAuxLatitude::DE(const AuxAngle& X, const AuxAngle& Y) const {
86 AuxAngle Xn(X.normalized()), Yn(Y.normalized());
87 // We assume that X and Y are in [-90d, 90d] and have the same sign
88 // If not we would include
89 // if (Xn.y() * Yn.y() < 0)
90 // return d != 0 ? (E(X) - E(Y)) / d : 1;
91
92 // The general formula fails for x = y = 0d and x = y = 90d. Probably this
93 // is fixable (the formula works for other x = y. But let's also stipulate
94 // that x != y .
95
96 // Make both positive, so we can do the swap a <-> b trick
97 Xn.y() = fabs(Xn.y()); Yn.y() = fabs(Yn.y());
98 real x = Xn.radians(), y = Yn.radians(), d = y - x,
99 sx = Xn.y(), sy = Yn.y(), cx = Xn.x(), cy = Yn.x(),
100 k2;
101 // Switch prolate to oblate; we then can use the formulas for k2 < 0
102 if (false && base::_f < 0) {
103 d = -d; swap(sx, cx); swap(sy, cy);
104 k2 = base::_e2;
105 } else {
106 k2 = -base::_e12;
107 }
108 // See DLMF: Eqs (19.11.2) and (19.11.4) letting
109 // theta -> x, phi -> -y, psi -> z
110 //
111 // (E(y) - E(x)) / d = E(z)/d - k2 * sin(x) * sin(y) * sin(z)/d
112 // = (E(z)/sin(z) - k2 * sin(x) * sin(y)) * sin(z)/d
113 // tan(z/2) = (sin(x)*Delta(y) - sin(y)*Delta(x)) / (cos(x) + cos(y))
114 // = d * Dsin(x,y) * (sin(x) + sin(y))/(cos(x) + cos(y)) /
115 // (sin(x)*Delta(y) + sin(y)*Delta(x))
116 // = t = d * Dt
117 // Delta(x) = sqrt(1 - k2 * sin(x)^2)
118 // sin(z) = 2*t/(1+t^2); cos(z) = (1-t^2)/(1+t^2)
119 real Dt = Dsin(x, y) * (sx + sy) /
120 ((cx + cy) * (sx * sqrt(1 - k2 * sy*sy) + sy * sqrt(1 - k2 * sx*sx))),
121 t = d * Dt, Dsz = 2 * Dt / (1 + t*t),
122 sz = d * Dsz, cz = (1 - t) * (1 + t) / (1 + t*t),
123 sz2 = sz*sz, cz2 = cz*cz, dz2 = 1 - k2 * sz2,
124 // E(z)/sin(z)
125 Ezbsz = (EllipticFunction::RF(cz2, dz2, 1)
126 - k2 * sz2 * EllipticFunction::RD(cz2, dz2, 1) / 3);
127 return (Ezbsz - k2 * sx * sy) * Dsz;
128 }
129
130 /// \cond SKIP
131 Math::real DAuxLatitude::Dsn(real x, real y) {
132 real sc1 = base::sc(x);
133 if (x == y) return 1 / (sc1 * (1 + x*x));
134 real sc2 = base::sc(y), sn1 = base::sn(x), sn2 = base::sn(y);
135 return x * y > 0 ?
136 (sn1/sc2 + sn2/sc1) / ((sn1 + sn2) * sc1 * sc2) :
137 (sn2 - sn1) / (y - x);
138 }
139 Math::real DAuxLatitude::Datan(real x, real y) {
140 using std::isinf; // Needed for Centos 7, ubuntu 14
141 real d = y - x, xy = x*y;
142 return x == y ? 1 / (1 + xy) :
143 (isinf(xy) && xy > 0 ? 0 :
144 (2 * xy > -1 ? atan( d / (1 + xy) ) : atan(y) - atan(x)) / d);
145 }
146 Math::real DAuxLatitude::Dasinh(real x, real y) {
147 using std::isinf; // Needed for Centos 7, ubuntu 14
148 real d = y - x, xy = x*y, hx = base::sc(x), hy = base::sc(y);
149 // KF formula for x*y < 0 is asinh(y*hx - x*hy) / (y - x)
150 // but this has problem if x*y overflows to -inf
151 return x == y ? 1 / hx :
152 (isinf(d) ? 0 :
153 (xy > 0 ? asinh(d * (x*y < 1 ? (x + y) / (x*hy + y*hx) :
154 (1/x + 1/y) / (hy/y + hx/x))) :
155 asinh(y) - asinh(x)) / d);
156 }
157 Math::real DAuxLatitude::Dh(real x, real y) {
158 using std::isnan; using std::isinf; // Needed for Centos 7, ubuntu 14
159 if (isnan(x + y))
160 return x + y; // N.B. nan for inf-inf
161 if (isinf(x))
162 return copysign(1/real(2), x);
163 if (isinf(y))
164 return copysign(1/real(2), y);
165 real sx = base::sn(x), sy = base::sn(y), d = sx*x + sy*y;
166 if (d / 2 == 0)
167 return (x + y) / 2; // Handle underflow
168 if (x * y <= 0)
169 return (h(y) - h(x)) / (y - x); // Does not include x = y = 0
170 real scx = base::sc(x), scy = base::sc(y);
171 return ((x + y) / (2 * d)) *
172 (Math::sq(sx*sy) + Math::sq(sy/scx) + Math::sq(sx/scy));
173 }
174 Math::real DAuxLatitude::Datanhee(real x, real y) const {
175 // atan(e*sn(tphi))/e:
176 // Datan(e*sn(x),e*sn(y))*Dsn(x,y)/Datan(x,y)
177 // asinh(e1*sn(fm1*tphi)):
178 // Dasinh(e1*sn(fm1*x)), e1*sn(fm1*y)) *
179 // e1 * Dsn(fm1*x, fm1*y) *fm1 / (e * Datan(x,y))
180 // = Dasinh(e1*sn(fm1*x)), e1*sn(fm1*y)) *
181 // Dsn(fm1*x, fm1*y) / Datan(x,y)
182 return base::_f < 0 ?
183 Datan(base::_e * base::sn(x), base::_e * base::sn(y)) * Dsn(x, y) :
184 Dasinh(base::_e1 * base::sn(base::_fm1 * x),
185 base::_e1 * base::sn(base::_fm1 * y)) *
186 Dsn(base::_fm1 * x, base::_fm1 * y);
187 }
188 /// \endcond
189
190 Math::real DAuxLatitude::DIsometric(const AuxAngle& phi1,
191 const AuxAngle& phi2)
192 const {
193 // psi = asinh(tan(phi)) - e^2 * atanhee(tan(phi))
194 using std::isnan; using std::isinf; // Needed for Centos 7, ubuntu 14
195 real tphi1 = phi1.tan(), tphi2 = phi2.tan();
196 return isnan(tphi1) || isnan(tphi2) ? numeric_limits<real>::quiet_NaN() :
197 (isinf(tphi1) || isinf(tphi2) ? numeric_limits<real>::infinity() :
198 (Dasinh(tphi1, tphi2) - base::_e2 * Datanhee(tphi1, tphi2)) /
199 Datan(tphi1, tphi2));
200 }
201
202 Math::real DAuxLatitude::DConvert(int auxin, int auxout,
203 const AuxAngle& zeta1,
204 const AuxAngle& zeta2)
205 const {
206 using std::isnan; // Needed for Centos 7, ubuntu 14
207 int k = base::ind(auxout, auxin);
208 if (k < 0) return numeric_limits<real>::quiet_NaN();
209 if (auxin == auxout) return 1;
210 if ( isnan(base::_c[base::Lmax * (k + 1) - 1]) )
211 base::fillcoeff(auxin, auxout, k);
212 AuxAngle zeta1n(zeta1.normalized()), zeta2n(zeta2.normalized());
213 return 1 + DClenshaw(true, zeta2n.radians() - zeta1n.radians(),
214 zeta1n.y(), zeta1n.x(), zeta2n.y(), zeta2n.x(),
215 base::_c + base::Lmax * k, base::Lmax);
216 }
217
218 Math::real DAuxLatitude::DClenshaw(bool sinp, real Delta,
219 real szeta1, real czeta1,
220 real szeta2, real czeta2,
221 const real c[], int K) {
222 // Evaluate
223 // (Clenshaw(sinp, szeta2, czeta2, c, K) -
224 // Clenshaw(sinp, szeta1, czeta1, c, K)) / Delta
225 // or
226 // sum(c[k] * (sin( (2*k+2) * zeta2) - sin( (2*k+2) * zeta2)), i, 0, K-1)
227 // / Delta
228 // (if !sinp, then change sin->cos here.)
229 //
230 // Delta is EITHER 1, giving the plain difference OR (zeta2 - zeta1) in
231 // radians, giving the divided difference. Other values will give
232 // nonsense.
233 //
234 int k = K;
235 // suffices a b denote [1,1], [2,1] elements of matrix/vector
236 real D2 = Delta * Delta,
237 czetap = czeta2 * czeta1 - szeta2 * szeta1,
238 szetap = szeta2 * czeta1 + czeta2 * szeta1,
239 czetam = czeta2 * czeta1 + szeta2 * szeta1,
240 // sin(zetam) / Delta
241 szetamd = (Delta == 1 ? szeta2 * czeta1 - czeta2 * szeta1 :
242 (Delta != 0 ? sin(Delta) / Delta : 1)),
243 Xa = 2 * czetap * czetam,
244 Xb = -2 * szetap * szetamd,
245 u0a = 0, u0b = 0, u1a = 0, u1b = 0; // accumulators for sum
246 for (--k; k >= 0; --k) {
247 // temporary real = X . U0 - U1 + c[k] * I
248 real ta = Xa * u0a + D2 * Xb * u0b - u1a + c[k],
249 tb = Xb * u0a + Xa * u0b - u1b;
250 // U1 = U0; U0 = real
251 u1a = u0a; u0a = ta;
252 u1b = u0b; u0b = tb;
253 }
254 // P = U0 . F[0] - U1 . F[-1]
255 // if sinp:
256 // F[0] = [ sin(2*zeta2) + sin(2*zeta1),
257 // (sin(2*zeta2) - sin(2*zeta1)) / Delta]
258 // = 2 * [ szetap * czetam, czetap * szetamd ]
259 // F[-1] = [0, 0]
260 // else:
261 // F[0] = [ cos(2*zeta2) + cos(2*zeta1),
262 // (cos(2*zeta2) - cos(2*zeta1)) / Delta]
263 // = 2 * [ czetap * czetam, -szetap * szetamd ]
264 // F[-1] = [2, 0]
265 real F0a = (sinp ? szetap : czetap) * czetam,
266 F0b = (sinp ? czetap : -szetap) * szetamd,
267 Fm1a = sinp ? 0 : 1; // Fm1b = 0;
268 // Don't both to compute sum...
269 // divided difference (or difference if Delta == 1)
270 return 2 * (F0a * u0b + F0b * u0a - Fm1a * u1b);
271 }
272
273} // namespace GeographicLib
DAuxLatitude.hpp
Header for the GeographicLib::DAuxLatitude class.
EllipticFunction.hpp
Header for GeographicLib::EllipticFunction class.
real
GeographicLib::Math::real real
Definition: GeodSolve.cpp:29
GeographicLib::AuxAngle
An accurate representation of angles.
Definition: AuxAngle.hpp:47
GeographicLib::AuxAngle::y
Math::real y() const
Definition: AuxAngle.hpp:70
GeographicLib::AuxAngle::x
Math::real x() const
Definition: AuxAngle.hpp:75
GeographicLib::AuxAngle::radians
Math::real radians() const
Definition: AuxAngle.hpp:228
GeographicLib::AuxAngle::normalized
AuxAngle normalized() const
Definition: AuxAngle.cpp:31
GeographicLib::AuxAngle::tan
Math::real tan() const
Definition: AuxAngle.hpp:113
GeographicLib::AuxLatitude::Lmax
static const int Lmax
Definition: AuxLatitude.hpp:316
GeographicLib::AuxLatitude::RectifyingRadius
Math::real RectifyingRadius(bool exact=false) const
Definition: AuxLatitude.cpp:332
GeographicLib::AuxLatitude::Parametric
AuxAngle Parametric(const AuxAngle &phi, real *diff=nullptr) const
Definition: AuxLatitude.cpp:91
GeographicLib::AuxLatitude::Rectifying
AuxAngle Rectifying(const AuxAngle &phi, real *diff=nullptr) const
Definition: AuxLatitude.cpp:101
GeographicLib::DAuxLatitude::DParametric
Math::real DParametric(const AuxAngle &phi1, const AuxAngle &phi2) const
Definition: DAuxLatitude.cpp:53
GeographicLib::DAuxLatitude::DConvert
Math::real DConvert(int auxin, int auxout, const AuxAngle &zeta1, const AuxAngle &zeta2) const
Definition: DAuxLatitude.cpp:202
GeographicLib::DAuxLatitude::DIsometric
Math::real DIsometric(const AuxAngle &phi1, const AuxAngle &phi2) const
Definition: DAuxLatitude.cpp:190
GeographicLib::DAuxLatitude::DClenshaw
static Math::real DClenshaw(bool sinp, real Delta, real szeta1, real czeta1, real szeta2, real czeta2, const real c[], int K)
Definition: DAuxLatitude.cpp:218
GeographicLib::DAuxLatitude::DRectifying
Math::real DRectifying(const AuxAngle &phi1, const AuxAngle &phi2) const
Definition: DAuxLatitude.cpp:32
GeographicLib::EllipticFunction::RD
static real RD(real x, real y, real z)
Definition: EllipticFunction.cpp:182
GeographicLib::EllipticFunction::RF
static real RF(real x, real y, real z)
Definition: EllipticFunction.cpp:29
GeographicLib::Math::sq
static T sq(T x)
Definition: Math.hpp:205
GeographicLib::Math::real
double real
Definition: Math.hpp:100
GeographicLib
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
std
Definition: NearestNeighbor.hpp:812
std::swap
void swap(GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &a, GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &b)
Definition: NearestNeighbor.hpp:825