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