1# -*- coding: utf-8 -*-

2

3 u'''Class L{AuxDLat} transcoded to Python from I{Karney}'s C++ class U{DAuxLatitude

4<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1DAuxLatitude.html>}

5 in I{GeographicLib version 2.2+}.

6

7 Copyright (C) U{Charles Karney<mailto:Karney@Alum.MIT.edu>} (2022-2023) and licensed

8 under the MIT/X11 License. For more information, see the U{GeographicLib

9<https://GeographicLib.SourceForge.io>} documentation.

10'''

11# make sure int/int division yields float quotient, see .basics

12 from __future__ import division as _; del _ # PYCHOK semicolon

13

14 from pygeodesy.auxilats.auxily import Aux, _Datan, _Dasinh, _Dm, _sc, _sn, \

15 AuxError

16 from pygeodesy.auxilats.auxLat import AuxLat, _ALL_DOCS

17 from pygeodesy.basics import map1, _reverange

18 from pygeodesy.constants import INF, NAN, isfinite, isinf, isnan, _0_0, _0_5, \

19 _1_0, _2_0, _N_2_0, _naninf, _over, _1_over

20 from pygeodesy.elliptic import Elliptic as _Ef, Fsum

21# from pygeodesy.errors import AuxError # from .auxilats.auxily

22# from pygeodesy.fsums import Fsum # from .elliptic

23# from pygeodesy.lazily import _ALL_DOCS # from .auxilats.auxLat

24

25 from math import atan, atan2, cos, sin, sqrt

26

27__all__ = ()

28__version__ = '23.09.18'

29

30

31 class AuxDLat(AuxLat):

32 '''Class to compute C{Divided Differences} of I{Auxiliary}

33 latitudes and other C{Divided Differences} needed for

34 L{RhumbAux} and L{RhumbLineAux} calculations.

35 '''

36

37 def CParametric(self, Zeta1, Zeta2):

38 '''Short for C{.Dconvert(Aux.BETA, B{Zeta1}, B{Zeta2})}.

39 '''

40 return self.Dconvert(Aux.BETA, Zeta1, Zeta2)

41

42 def CRectifying(self, Zeta1, Zeta2):

43 '''Short for C{.Dconvert(Aux.MU, B{Zeta1}, B{Zeta2})}.

44 '''

45 return self.Dconvert(Aux.MU, Zeta1, Zeta2)

46

47 def _Datanhee(self, x, y):

48 # atan(e*sn(tphi))/e:

49 # Datan(e*sn(x),e*sn(y))*Dsn(x,y)/Datan(x,y)

50 # asinh(e1*sn(fm1*tphi)):

51 # Dasinh(e1*sn(fm1*x)), e1*sn(fm1*y)) *

52 # e1 * Dsn(fm1*x, fm1*y) *fm1 / (e * Datan(x,y))

53 # = Dasinh(e1*sn(fm1*x)), e1*sn(fm1*y)) *

54 # Dsn(fm1*x, fm1*y) / Datan(x,y)

55 if self.f < 0:

56 e = self._e

57 r = _Datan(e * _sn(x), e * _sn(y))

58 else:

59 x *= self._fm1

60 y *= self._fm1

61 e1 = self._e1

62 r = _Dasinh(e1 * _sn(x), e1 * _sn(y))

63 return _Dsn(x, y) * r

64

65 def Dconvert(self, auxout, Zeta1, Zeta2):

66 '''I{Divided Difference} of one auxiliary latitude wrt another.

67 '''

68 auxin = Zeta1._AUX

69 # assert Zeta2._AUX == auxin

70 try:

71 if auxout != auxin:

72 cs = self._coeffs(auxout, auxin)

73 # assert len(cs) == self.ALorder

74 r = _DClenshaw(True, Zeta1, Zeta2, cs, self.ALorder)

75 else:

76 r = _1_0

77 except AuxError: # no _coeffs

78 r = NAN

79 return r

80

81 def DE(self, X, Y):

82 # We assume that X and Y are in [-90d, 90d] and

83 # have the same sign. If not we would include

84 # if (Xn.y() * Yn.y() < 0)

85 # return d != 0 ? (E(X) - E(Y)) / d : 1

86 # The general formula fails for x = y = 0d and

87 # x = y = 90d. Probably this is fixable (the

88 # formula works for other x = y. But let's

89 # also stipulate that x != y.

90

91 # Make both y positive, so we can do the swap a <-> b trick

92 sx, cx, x = X._yxr_normalized(True)

93 sy, cy, y = Y._yxr_normalized(True)

94 k2, d = -self._e12, (y - x)

95 # Switch prolate to oblate, then use formulas for k2 < 0

96 if self.f < 0: # XXX and False?

97 sx, cx = cx, sx

98 sy, cy = cy, sy

99 d, k2 = -d, self._e2

100 # See DLMF: Eqs (19.11.2) and (19.11.4) letting

101 Dt = _Dsin(x, y) * (sx + sy)

102 if Dt:

103 t = _sxk2y(sx, sy, k2) + _sxk2y(sy, sx, k2)

104 Dt = _over(Dt, t * (cx + cy))

105 t = d * Dt

106 t2 = _1_0 + t**2

107 Dt *= _2_0 / t2

108 sk2 = (d * Dt)**2 * k2

109 d2 = _1_0 - sk2

110 c2 = ((_1_0 - t) * (_1_0 + t) / t2)**2 if t else _1_0

111 # E(z)/sin(z)

112 Dt *= _Ef._RFRD(c2, d2, _1_0, sk2) - k2 * sx * sy

113 return Dt

114

115 def DIsometric(self, Phi1, Phi2):

116 '''I{Divided Difference} of the isometric wrt the geographic latitude.

117 '''

118 tx, ty = Phi1.tan, Phi2.tan

119 if isnan(ty) or isnan(tx): # PYCHOK no cover

120 r = NAN

121 elif isinf(ty) or isinf(tx): # PYCHOK no cover

122 r = INF

123 else: # psi = asinh(tan(Phi)) - e^2 * atanhee(tan(Phi))

124 r = self._Datanhee(tx, ty) * self._e2

125 r = _over(_Dasinh(tx, ty) - r, _Datan(tx, ty))

126 return r

127

128 def DParametric(self, Phi1, Phi2):

129 '''I{Divided Difference} of the parametric wrt the geographic latitude.

130 '''

131 fm1, e2m1 = self._fm1, self._e2m1

132 tx, ty = Phi1.tan, Phi2.tan

133 # DbetaDphi = Datan(fm1*tx, fm1*ty) * fm1 / Datan(tx, ty)

134 # Datan(x, y) = 1 / (1 + x^2), if x == y

135 # = (atan(y) - atan(x)) / (y-x), if x*y < 0

136 # = atan((y-x) / (1 + x*y)) / (y-x), if x*y > 0

137 txy = tx * ty

138 if txy < 0 or (isinf(ty) and not tx):

139 _a = atan

140 r = _over(_a(fm1 * ty) - _a(fm1 * tx), _a(ty) - _a(tx))

141 elif tx == ty: # includes tx = ty = inf

142 if txy > 1: # == tx**2

143 txy = _1_over(txy)

144 r = txy + e2m1

145 else:

146 r = txy * e2m1 + _1_0

147 r = _over(fm1 * (txy + _1_0), r)

148 else:

149 if txy > 1:

150 tx = _1_over(tx)

151 ty = _1_over(ty)

152 txy = tx * ty

153 t = txy + e2m1

154 else:

155 t = txy * e2m1 + _1_0

156 r = ty - tx

157 r = _over(atan2(r * fm1, t), atan2(r, _1_0 + txy))

158 return r

159

160 def DRectifying(self, Phi1, Phi2):

161 '''I{Divided Difference} of the rectifying wrt the geographic latitude.

162 '''

163 # Stipulate that Phi1 and Phi2 are in [-90d, 90d]

164 x, y = Phi1.toRadians, Phi2.toRadians

165 if y == x: # isnear0

166 Mu1 = self.Rectifying(Phi1, diff=True)

167 tphi1, r = Phi1.tan, Mu1.diff

168 if isfinite(tphi1):

169 r *= _over(_sc(tphi1), _sc(Mu1.tan))**2

170 else: # PYCHOK no cover

171 r = _1_over(r)

172 elif (x * y) < 0:

173 r = _over(self.Rectifying(Phi2).toRadians -

174 self.Rectifying(Phi1).toRadians, y - x)

175 else:

176 r = _over(self.b, self.RectifyingRadius(True))

177 r *= self.DE(*map1(self.Parametric, Phi1, Phi2))

178 r *= self.DParametric(Phi1, Phi2)

179 return r # or INF or NAN

180

181

182 def _DClenshaw(sinp, Zeta1, Zeta2, cs, K):

183 '''(INTERNAL) I{Divided Difference} of L{AuxLat._Clenshaw}.

184

185 @return: C{float} if B{C{sinp}} otherwise a C{Fsum}.

186 '''

187 s1, c1, r1 = Zeta1._yxr_normalized(False)

188 s2, c2, r2 = Zeta2._yxr_normalized(False)

189 Delta = r2 - r1

190 # Evaluate (Clenshaw(sinp, szeta2, czeta2, cs, K) -

191 # Clenshaw(sinp, szeta1, czeta1, cs, K)) / Delta

192 # or f = sin if sinp else cos

193 # sum(cs[k] * (f((2*k+2) * Zeta2) -

194 # f((2*k+2) * Zeta2))) / Delta

195 #

196 # Delta is EITHER 1, giving the plain difference OR (Zeta2 - Zeta1)

197 # in radians, giving the I{Divided Difference}. Other values will

198 # produce nonsense.

199 #

200 # Suffices a and b denote [1,1], [2,1] elements of matrix/vector

201 cp = cm = c2 * c1

202 t = s2 * s1

203 cp -= t # not +

204 cm += t # not -

205

206 sp = s2 * c1

207 t = c2 * s1

208 smd = ((sin(Delta) / Delta) if Delta != _1_0 else

209 (sp - t)) if Delta else _1_0

210 sp += t

211

212 xa = cp * cm * _2_0

213 xb = sp * smd * _N_2_0

214 xD = xb * Delta**2

215

216 if isfinite(xD) and isfinite(xb) and isfinite(xa):

217 U0a, U1a = Fsum(), Fsum()

218 U0b, U1b = Fsum(), Fsum()

219 for k in _reverange(K): # assert len(cs) == K

220 # t = x . U0 - U1 + cs[k] * I

221 U1a -= U0a * xa + U0b * xD + cs[k]

222 U1b -= U0a * xb + U0b * xa

223 U1a, U0a = U0a, -U1a

224 U1b, U0b = U0b, -U1b

225 # F0a = (sp if sinp else cp) * cm

226 # F0b = (cp if sinp else -sp) * smd

227 # Fm1a = 0 if sinp else 1 # Fm1b = 0

228 # return (U0b * F0a + U0a * F0b - U1b * Fm1a) * 2

229 if sinp:

230 U1b = _0_0

231 else:

232 sp, cp = cp, -sp

233 U0b *= sp * cm

234 U0a *= cp * smd

235 U0a += U0b

236 U0a = _Dm(U0a, U1b, _2_0)

237 r = float(U0a) if sinp else U0a # Fsum

238 else:

239 r = _naninf(xD, xb, xa)

240 return r

241

242

243 def _Dsin(x, y): # see also .rhumbx._Dsin

244 r = cos((x + y) * _0_5)

245 d = (x - y) * _0_5

246 if d:

247 r *= sin(d) / d

248 return r

249

250

251 def _Dsn(x, y):

252 # (sn(y) - sn(x)) / (y - x)

253 if x != y:

254 snx, sny = _sn(x), _sn(y)

255 if (x * y) > 0:

256 scx, scy = _sc(x), _sc(y)

257 r = _over((snx / scy) + (sny / scx),

258 (snx + sny) * scy * scx)

259 else:

260 r = (sny - snx) / (y - x)

261 elif x:

262 r = _1_over(_sc(x) * (x**2 + _1_0)) # == 1 / sqrt3(x**2 + 1)

263 else:

264 r = _1_0

265 return r

266

267

268 def _sxk2y(sx, sy, k2):

269 # .DE helper

270 sy *= sy * k2

271 if sy:

272 try:

273 sx *= sqrt(_1_0 - sy)

274 except ValueError: # domain error

275 sx = NAN

276 return sx

277

278

279__all__ += _ALL_DOCS(AuxDLat)

280

281# **) MIT License

282#

283# Copyright (C) 2023-2023 -- mrJean1 at Gmail -- All Rights Reserved.

284#

285# Permission is hereby granted, free of charge, to any person obtaining a

286# copy of this software and associated documentation files (the "Software"),

287# to deal in the Software without restriction, including without limitation

288# the rights to use, copy, modify, merge, publish, distribute, sublicense,

289# and/or sell copies of the Software, and to permit persons to whom the

290# Software is furnished to do so, subject to the following conditions:

291#

292# The above copyright notice and this permission notice shall be included

293# in all copies or substantial portions of the Software.

294#

295# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS

296# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

297# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL

298# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR

299# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,

300# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR

301# OTHER DEALINGS IN THE SOFTWARE.