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