Coverage for pygeodesy/elliptic.py: 96%

480 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-11-12 13:23 -0500

1 

2# -*- coding: utf-8 -*- 

3 

4u'''I{Karney}'s elliptic functions and integrals. 

5 

6Class L{Elliptic} transcoded from I{Charles Karney}'s C++ class U{EllipticFunction 

7<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1EllipticFunction.html>} 

8to pure Python, including symmetric integrals L{Elliptic.fRC}, L{Elliptic.fRD}, 

9L{Elliptic.fRF}, L{Elliptic.fRG} and L{Elliptic.fRJ} as C{static methods}. 

10 

11Python method names follow the C++ member functions, I{except}: 

12 

13 - member functions I{without arguments} are mapped to Python properties 

14 prefixed with C{"c"}, for example C{E()} is property C{cE}, 

15 

16 - member functions with 1 or 3 arguments are renamed to Python methods 

17 starting with an C{"f"}, example C{E(psi)} to C{fE(psi)} and C{E(sn, 

18 cn, dn)} to C{fE(sn, cn, dn)}, 

19 

20 - other Python method names conventionally start with a lower-case 

21 letter or an underscore if private. 

22 

23Following is a copy of I{Karney}'s U{EllipticFunction.hpp 

24<https://GeographicLib.SourceForge.io/C++/doc/EllipticFunction_8hpp_source.html>} 

25file C{Header}. 

26 

27Copyright (C) U{Charles Karney<mailto:Karney@Alum.MIT.edu>} (2008-2023) 

28and licensed under the MIT/X11 License. For more information, see the 

29U{GeographicLib<https://GeographicLib.SourceForge.io>} documentation. 

30 

31B{Elliptic integrals and functions.} 

32 

33This provides the elliptic functions and integrals needed for 

34C{Ellipsoid}, C{GeodesicExact}, and C{TransverseMercatorExact}. Two 

35categories of function are provided: 

36 

37 - functions to compute U{symmetric elliptic integrals 

38 <https://DLMF.NIST.gov/19.16.i>} 

39 

40 - methods to compute U{Legrendre's elliptic integrals 

41 <https://DLMF.NIST.gov/19.2.ii>} and U{Jacobi elliptic 

42 functions<https://DLMF.NIST.gov/22.2>}. 

43 

44In the latter case, an object is constructed giving the modulus 

45C{k} (and optionally the parameter C{alpha}). The modulus (and 

46parameter) are always passed as squares which allows C{k} to be 

47pure imaginary. (Confusingly, Abramowitz and Stegun call C{m = k**2} 

48the "parameter" and C{n = alpha**2} the "characteristic".) 

49 

50In geodesic applications, it is convenient to separate the incomplete 

51integrals into secular and periodic components, e.g. 

52 

53I{C{E(phi, k) = (2 E(k) / pi) [ phi + delta E(phi, k) ]}} 

54 

55where I{C{delta E(phi, k)}} is an odd periodic function with 

56period I{C{pi}}. 

57 

58The computation of the elliptic integrals uses the algorithms given 

59in U{B. C. Carlson, Computation of real or complex elliptic integrals 

60<https://DOI.org/10.1007/BF02198293>} (also available U{here 

61<https://ArXiv.org/pdf/math/9409227.pdf>}), Numerical Algorithms 10, 

6213--26 (1995) with the additional optimizations given U{here 

63<https://DLMF.NIST.gov/19.36.i>}. 

64 

65The computation of the Jacobi elliptic functions uses the algorithm 

66given in U{R. Bulirsch, Numerical Calculation of Elliptic Integrals 

67and Elliptic Functions<https://DOI.org/10.1007/BF01397975>}, 

68Numerische Mathematik 7, 78--90 (1965). 

69 

70The notation follows U{NIST Digital Library of Mathematical Functions 

71<https://DLMF.NIST.gov>} chapters U{19<https://DLMF.NIST.gov/19>} and 

72U{22<https://DLMF.NIST.gov/22>}. 

73''' 

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

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

76 

77from pygeodesy.basics import copysign0, map2, neg, neg_ 

78from pygeodesy.constants import EPS, INF, NAN, PI, PI_2, PI_4, \ 

79 _EPStol as _TolJAC, _0_0, _1_64th, \ 

80 _0_25, _0_5, _1_0, _2_0, _N_2_0, \ 

81 _3_0, _4_0, _6_0, _8_0, _180_0, \ 

82 _360_0, _over 

83from pygeodesy.errors import _ValueError, _xattr, _xkwds_pop 

84from pygeodesy.fmath import fdot, hypot1, zqrt 

85from pygeodesy.fsums import Fsum, _sum 

86from pygeodesy.interns import NN, _delta_, _DOT_, _f_, _invalid_, \ 

87 _invokation_, _negative_, _SPACE_ 

88from pygeodesy.karney import _K_2_0, _norm180, _signBit, _sincos2, \ 

89 _ALL_LAZY 

90# from pygeodesy.lazily import _ALL_LAZY # from .karney 

91from pygeodesy.named import _Named, _NamedTuple, Fmt, unstr 

92from pygeodesy.props import _allPropertiesOf_n, Property_RO, _update_all 

93# from pygeodesy.streprs import Fmt, unstr # from .named 

94from pygeodesy.units import Scalar, Scalar_ 

95# from pygeodesy.utily import sincos2 as _sincos2 # from .karney 

96 

97from math import asinh, atan, atan2, ceil, cosh, fabs, floor, \ 

98 radians, sin, sqrt, tanh 

99 

100__all__ = _ALL_LAZY.elliptic 

101__version__ = '23.09.18' 

102 

103_TolRD = zqrt(EPS * 0.002) 

104_TolRF = zqrt(EPS * 0.030) 

105_TolRG0 = _TolJAC * 2.7 

106_TRIPS = 21 # Max depth, 7 might be sufficient 

107 

108 

109class _Cs(object): 

110 '''(INTERAL) Complete integrals cache. 

111 ''' 

112 def __init__(self, **kwds): 

113 self.__dict__ = kwds 

114 

115 

116class _D(list): 

117 '''(INTERNAL) Deferred C{Fsum}. 

118 ''' 

119 def __call__(self, s): 

120 try: # Fsum *= s 

121 return Fsum(*self).fmul(s) 

122 except ValueError: # Fsum(NAN) exception 

123 return _sum(self) * s 

124 

125 def __iadd__(self, x): 

126 list.append(self, x) 

127 return self 

128 

129 

130class Elliptic(_Named): 

131 '''Elliptic integrals and functions. 

132 

133 @see: I{Karney}'s U{Detailed Description<https://GeographicLib.SourceForge.io/ 

134 html/classGeographicLib_1_1EllipticFunction.html#details>}. 

135 ''' 

136# _alpha2 = 0 

137# _alphap2 = 0 

138# _eps = EPS 

139# _k2 = 0 

140# _kp2 = 0 

141 

142 def __init__(self, k2=0, alpha2=0, kp2=None, alphap2=None, name=NN): 

143 '''Constructor, specifying the C{modulus} and C{parameter}. 

144 

145 @kwarg name: Optional name (C{str}). 

146 

147 @see: Method L{Elliptic.reset} for further details. 

148 

149 @note: If only elliptic integrals of the first and second kinds 

150 are needed, use C{B{alpha2}=0}, the default value. In 

151 that case, we have C{Π(φ, 0, k) = F(φ, k), G(φ, 0, k) = 

152 E(φ, k)} and C{H(φ, 0, k) = F(φ, k) - D(φ, k)}. 

153 ''' 

154 self.reset(k2=k2, alpha2=alpha2, kp2=kp2, alphap2=alphap2) 

155 

156 if name: 

157 self.name = name 

158 

159 @Property_RO 

160 def alpha2(self): 

161 '''Get α^2, the square of the parameter (C{float}). 

162 ''' 

163 return self._alpha2 

164 

165 @Property_RO 

166 def alphap2(self): 

167 '''Get α'^2, the square of the complementary parameter (C{float}). 

168 ''' 

169 return self._alphap2 

170 

171 @Property_RO 

172 def cD(self): 

173 '''Get Jahnke's complete integral C{D(k)} (C{float}), 

174 U{defined<https://DLMF.NIST.gov/19.2.E6>}. 

175 ''' 

176 return self._cDEKEeps.cD 

177 

178 @Property_RO 

179 def _cDEKEeps(self): 

180 '''(INTERNAL) Get the complete integrals D, E, K and KE plus C{eps}. 

181 ''' 

182 k2, kp2 = self.k2, self.kp2 

183 if k2: 

184 if kp2: 

185 try: 

186 self._iteration = 0 

187 # D(k) = (K(k) - E(k))/k2, Carlson eq.4.3 

188 # <https://DLMF.NIST.gov/19.25.E1> 

189 D = _RD(self, _0_0, kp2, _1_0, _3_0) 

190 cD = float(D) 

191 # Complete elliptic integral E(k), Carlson eq. 4.2 

192 # <https://DLMF.NIST.gov/19.25.E1> 

193 cE = _rG2(self, kp2, _1_0, PI_=PI_2) 

194 # Complete elliptic integral K(k), Carlson eq. 4.1 

195 # <https://DLMF.NIST.gov/19.25.E1> 

196 cK = _rF2(self, kp2, _1_0) 

197 cKE = float(D.fmul(k2)) 

198 eps = k2 / (sqrt(kp2) + _1_0)**2 

199 

200 except Exception as e: 

201 raise _ellipticError(self.reset, k2=k2, kp2=kp2, cause=e) 

202 else: 

203 cD = cK = cKE = INF 

204 cE = _1_0 

205 eps = k2 

206 else: 

207 cD = PI_4 

208 cE = cK = PI_2 

209 cKE = _0_0 # k2 * cD 

210 eps = EPS 

211 

212 return _Cs(cD=cD, cE=cE, cK=cK, cKE=cKE, eps=eps) 

213 

214 @Property_RO 

215 def cE(self): 

216 '''Get the complete integral of the second kind C{E(k)} 

217 (C{float}), U{defined<https://DLMF.NIST.gov/19.2.E5>}. 

218 ''' 

219 return self._cDEKEeps.cE 

220 

221 @Property_RO 

222 def cG(self): 

223 '''Get Legendre's complete geodesic longitude integral 

224 C{G(α^2, k)} (C{float}). 

225 ''' 

226 return self._cGHPi.cG 

227 

228 @Property_RO 

229 def _cGHPi(self): 

230 '''(INTERNAL) Get the complete integrals G, H and Pi. 

231 ''' 

232 alpha2, alphap2, kp2 = self.alpha2, self.alphap2, self.kp2 

233 try: 

234 self._iteration = 0 

235 if alpha2: 

236 if alphap2: 

237 if kp2: # <https://DLMF.NIST.gov/19.25.E2> 

238 cK = self.cK 

239 Rj = _RJ(self, _0_0, kp2, _1_0, alphap2, _3_0) 

240 cG = float(Rj * (alpha2 - self.k2) + cK) # G(alpha2, k) 

241 cH = -float(Rj * alphap2 - cK) # H(alpha2, k) 

242 cPi = float(Rj * alpha2 + cK) # Pi(alpha2, k) 

243 else: 

244 cG = cH = _rC(self, _1_0, alphap2) 

245 cPi = INF # XXX or NAN? 

246 else: 

247 cG = cH = cPi = INF # XXX or NAN? 

248 else: 

249 cG, cPi = self.cE, self.cK 

250 # H = K - D but this involves large cancellations if k2 is near 1. 

251 # So write (for alpha2 = 0) 

252 # H = int(cos(phi)**2 / sqrt(1-k2 * sin(phi)**2), phi, 0, pi/2) 

253 # = 1 / sqrt(1-k2) * int(sin(phi)**2 / sqrt(1-k2/kp2 * sin(phi)**2,...) 

254 # = 1 / kp * D(i * k/kp) 

255 # and use D(k) = RD(0, kp2, 1) / 3, so 

256 # H = 1/kp * RD(0, 1/kp2, 1) / 3 

257 # = kp2 * RD(0, 1, kp2) / 3 

258 # using <https://DLMF.NIST.gov/19.20.E18>. Equivalently 

259 # RF(x, 1) - RD(0, x, 1) / 3 = x * RD(0, 1, x) / 3 for x > 0 

260 # For k2 = 1 and alpha2 = 0, we have 

261 # H = int(cos(phi),...) = 1 

262 cH = float(_RD(self, _0_0, _1_0, kp2, _3_0 / kp2)) if kp2 else _1_0 

263 

264 except Exception as e: 

265 raise _ellipticError(self.reset, kp2=kp2, alpha2 =alpha2, 

266 alphap2=alphap2, cause=e) 

267 return _Cs(cG=cG, cH=cH, cPi=cPi) 

268 

269 @Property_RO 

270 def cH(self): 

271 '''Get Cayley's complete geodesic longitude difference integral 

272 C{H(α^2, k)} (C{float}). 

273 ''' 

274 return self._cGHPi.cH 

275 

276 @Property_RO 

277 def cK(self): 

278 '''Get the complete integral of the first kind C{K(k)} 

279 (C{float}), U{defined<https://DLMF.NIST.gov/19.2.E4>}. 

280 ''' 

281 return self._cDEKEeps.cK 

282 

283 @Property_RO 

284 def cKE(self): 

285 '''Get the difference between the complete integrals of the 

286 first and second kinds, C{K(k) − E(k)} (C{float}). 

287 ''' 

288 return self._cDEKEeps.cKE 

289 

290 @Property_RO 

291 def cPi(self): 

292 '''Get the complete integral of the third kind C{Pi(α^2, k)} 

293 (C{float}), U{defined<https://DLMF.NIST.gov/19.2.E7>}. 

294 ''' 

295 return self._cGHPi.cPi 

296 

297 def deltaD(self, sn, cn, dn): 

298 '''Jahnke's periodic incomplete elliptic integral. 

299 

300 @arg sn: sin(φ). 

301 @arg cn: cos(φ). 

302 @arg dn: sqrt(1 − k2 * sin(2φ)). 

303 

304 @return: Periodic function π D(φ, k) / (2 D(k)) - φ (C{float}). 

305 

306 @raise EllipticError: Invalid invokation or no convergence. 

307 ''' 

308 return _deltaX(sn, cn, dn, self.cD, self.fD) 

309 

310 def deltaE(self, sn, cn, dn): 

311 '''The periodic incomplete integral of the second kind. 

312 

313 @arg sn: sin(φ). 

314 @arg cn: cos(φ). 

315 @arg dn: sqrt(1 − k2 * sin(2φ)). 

316 

317 @return: Periodic function π E(φ, k) / (2 E(k)) - φ (C{float}). 

318 

319 @raise EllipticError: Invalid invokation or no convergence. 

320 ''' 

321 return _deltaX(sn, cn, dn, self.cE, self.fE) 

322 

323 def deltaEinv(self, stau, ctau): 

324 '''The periodic inverse of the incomplete integral of the second kind. 

325 

326 @arg stau: sin(τ) 

327 @arg ctau: cos(τ) 

328 

329 @return: Periodic function E^−1(τ (2 E(k)/π), k) - τ (C{float}). 

330 

331 @raise EllipticError: No convergence. 

332 ''' 

333 try: 

334 if _signBit(ctau): # pi periodic 

335 stau, ctau = neg_(stau, ctau) 

336 t = atan2(stau, ctau) 

337 return self._Einv(t * self.cE / PI_2) - t 

338 

339 except Exception as e: 

340 raise _ellipticError(self.deltaEinv, stau, ctau, cause=e) 

341 

342 def deltaF(self, sn, cn, dn): 

343 '''The periodic incomplete integral of the first kind. 

344 

345 @arg sn: sin(φ). 

346 @arg cn: cos(φ). 

347 @arg dn: sqrt(1 − k2 * sin(2φ)). 

348 

349 @return: Periodic function π F(φ, k) / (2 K(k)) - φ (C{float}). 

350 

351 @raise EllipticError: Invalid invokation or no convergence. 

352 ''' 

353 return _deltaX(sn, cn, dn, self.cK, self.fF) 

354 

355 def deltaG(self, sn, cn, dn): 

356 '''Legendre's periodic geodesic longitude integral. 

357 

358 @arg sn: sin(φ). 

359 @arg cn: cos(φ). 

360 @arg dn: sqrt(1 − k2 * sin(2φ)). 

361 

362 @return: Periodic function π G(φ, k) / (2 G(k)) - φ (C{float}). 

363 

364 @raise EllipticError: Invalid invokation or no convergence. 

365 ''' 

366 return _deltaX(sn, cn, dn, self.cG, self.fG) 

367 

368 def deltaH(self, sn, cn, dn): 

369 '''Cayley's periodic geodesic longitude difference integral. 

370 

371 @arg sn: sin(φ). 

372 @arg cn: cos(φ). 

373 @arg dn: sqrt(1 − k2 * sin(2φ)). 

374 

375 @return: Periodic function π H(φ, k) / (2 H(k)) - φ (C{float}). 

376 

377 @raise EllipticError: Invalid invokation or no convergence. 

378 ''' 

379 return _deltaX(sn, cn, dn, self.cH, self.fH) 

380 

381 def deltaPi(self, sn, cn, dn): 

382 '''The periodic incomplete integral of the third kind. 

383 

384 @arg sn: sin(φ). 

385 @arg cn: cos(φ). 

386 @arg dn: sqrt(1 − k2 * sin(2φ)). 

387 

388 @return: Periodic function π Π(φ, α2, k) / (2 Π(α2, k)) - φ 

389 (C{float}). 

390 

391 @raise EllipticError: Invalid invokation or no convergence. 

392 ''' 

393 return _deltaX(sn, cn, dn, self.cPi, self.fPi) 

394 

395 def _Einv(self, x): 

396 '''(INTERNAL) Helper for C{.deltaEinv} and C{.fEinv}. 

397 ''' 

398 E2 = self.cE * _2_0 

399 n = floor(x / E2 + _0_5) 

400 r = x - E2 * n # r in [-cE, cE) 

401 # linear approximation 

402 phi = PI * r / E2 # phi in [-PI_2, PI_2) 

403 Phi = Fsum(phi) 

404 # first order correction 

405 phi = Phi.fsum_(self.eps * sin(phi * _2_0) / _N_2_0) 

406 # For kp2 close to zero use asin(r / cE) or J. P. Boyd, 

407 # Applied Math. and Computation 218, 7005-7013 (2012) 

408 # <https://DOI.org/10.1016/j.amc.2011.12.021> 

409 _Phi2, self._iteration = Phi.fsum2_, 0 # aggregate 

410 for i in range(1, _TRIPS): # GEOGRAPHICLIB_PANIC 

411 sn, cn, dn = self._sncndn3(phi) 

412 if dn: 

413 sn = self.fE(sn, cn, dn) 

414 phi, d = _Phi2((r - sn) / dn) 

415 else: # PYCHOK no cover 

416 d = _0_0 # XXX continue? 

417 if fabs(d) < _TolJAC: # 3-4 trips 

418 _iterations(self, i) 

419 break 

420 else: # PYCHOK no cover 

421 raise _convergenceError(d, _TolJAC) 

422 return Phi.fsum_(n * PI) if n else phi 

423 

424 @Property_RO 

425 def eps(self): 

426 '''Get epsilon (C{float}). 

427 ''' 

428 return self._cDEKEeps.eps 

429 

430 def fD(self, phi_or_sn, cn=None, dn=None): 

431 '''Jahnke's incomplete elliptic integral in terms of 

432 Jacobi elliptic functions. 

433 

434 @arg phi_or_sn: φ or sin(φ). 

435 @kwarg cn: C{None} or cos(φ). 

436 @kwarg dn: C{None} or sqrt(1 − k2 * sin(2φ)). 

437 

438 @return: D(φ, k) as though φ ∈ (−π, π] (C{float}), 

439 U{defined<https://DLMF.NIST.gov/19.2.E4>}. 

440 

441 @raise EllipticError: Invalid invokation or no convergence. 

442 ''' 

443 def _fD(sn, cn, dn): 

444 r = fabs(sn)**3 

445 if r: 

446 r = float(_RD(self, cn**2, dn**2, _1_0, _3_0 / r)) 

447 return r 

448 

449 return self._fXf(phi_or_sn, cn, dn, self.cD, 

450 self.deltaD, _fD) 

451 

452 def fDelta(self, sn, cn): 

453 '''The C{Delta} amplitude function. 

454 

455 @arg sn: sin(φ). 

456 @arg cn: cos(φ). 

457 

458 @return: sqrt(1 − k2 * sin(2φ)) (C{float}). 

459 ''' 

460 try: 

461 k2 = self.k2 

462 s = (self.kp2 + cn**2 * k2) if k2 > 0 else ( 

463 (_1_0 - sn**2 * k2) if k2 < 0 else self.kp2) 

464 return sqrt(s) if s else _0_0 

465 

466 except Exception as e: 

467 raise _ellipticError(self.fDelta, sn, cn, k2=k2, cause=e) 

468 

469 def fE(self, phi_or_sn, cn=None, dn=None): 

470 '''The incomplete integral of the second kind in terms of 

471 Jacobi elliptic functions. 

472 

473 @arg phi_or_sn: φ or sin(φ). 

474 @kwarg cn: C{None} or cos(φ). 

475 @kwarg dn: C{None} or sqrt(1 − k2 * sin(2φ)). 

476 

477 @return: E(φ, k) as though φ ∈ (−π, π] (C{float}), 

478 U{defined<https://DLMF.NIST.gov/19.2.E5>}. 

479 

480 @raise EllipticError: Invalid invokation or no convergence. 

481 ''' 

482 def _fE(sn, cn, dn): 

483 '''(INTERNAL) Core of C{.fE}. 

484 ''' 

485 if sn: 

486 sn2, cn2, dn2 = sn**2, cn**2, dn**2 

487 kp2, k2 = self.kp2, self.k2 

488 if k2 <= 0: # Carlson, eq. 4.6, <https://DLMF.NIST.gov/19.25.E9> 

489 Ei = _RF3(self, cn2, dn2, _1_0) 

490 if k2: 

491 Ei -= _RD(self, cn2, dn2, _1_0, _3over(k2, sn2)) 

492 elif kp2 >= 0: # k2 > 0, <https://DLMF.NIST.gov/19.25.E10> 

493 Ei = _over(k2 * fabs(cn), dn) # float 

494 if kp2: 

495 Ei += (_RD( self, cn2, _1_0, dn2, _3over(k2, sn2)) + 

496 _RF3(self, cn2, dn2, _1_0)) * kp2 

497 else: # kp2 < 0, <https://DLMF.NIST.gov/19.25.E11> 

498 Ei = _over(dn, fabs(cn)) 

499 Ei -= _RD(self, dn2, _1_0, cn2, _3over(kp2, sn2)) 

500 Ei *= fabs(sn) 

501 ei = float(Ei) 

502 else: # PYCHOK no cover 

503 ei = _0_0 

504 return ei 

505 

506 return self._fXf(phi_or_sn, cn, dn, self.cE, 

507 self.deltaE, _fE) 

508 

509 def fEd(self, deg): 

510 '''The incomplete integral of the second kind with 

511 the argument given in C{degrees}. 

512 

513 @arg deg: Angle (C{degrees}). 

514 

515 @return: E(π B{C{deg}} / 180, k) (C{float}). 

516 

517 @raise EllipticError: No convergence. 

518 ''' 

519 if _K_2_0: 

520 e = round((deg - _norm180(deg)) / _360_0) 

521 elif fabs(deg) < _180_0: 

522 e = _0_0 

523 else: 

524 e = ceil(deg / _360_0 - _0_5) 

525 deg -= e * _360_0 

526 return self.fE(radians(deg)) + e * self.cE * _4_0 

527 

528 def fEinv(self, x): 

529 '''The inverse of the incomplete integral of the second kind. 

530 

531 @arg x: Argument (C{float}). 

532 

533 @return: φ = 1 / E(B{C{x}}, k), such that E(φ, k) = B{C{x}} 

534 (C{float}). 

535 

536 @raise EllipticError: No convergence. 

537 ''' 

538 try: 

539 return self._Einv(x) 

540 except Exception as e: 

541 raise _ellipticError(self.fEinv, x, cause=e) 

542 

543 def fF(self, phi_or_sn, cn=None, dn=None): 

544 '''The incomplete integral of the first kind in terms of 

545 Jacobi elliptic functions. 

546 

547 @arg phi_or_sn: φ or sin(φ). 

548 @kwarg cn: C{None} or cos(φ). 

549 @kwarg dn: C{None} or sqrt(1 − k2 * sin(2φ)). 

550 

551 @return: F(φ, k) as though φ ∈ (−π, π] (C{float}), 

552 U{defined<https://DLMF.NIST.gov/19.2.E4>}. 

553 

554 @raise EllipticError: Invalid invokation or no convergence. 

555 ''' 

556 def _fF(sn, cn, dn): 

557 r = fabs(sn) 

558 if r: 

559 r = float(_RF3(self, cn**2, dn**2, _1_0).fmul(r)) 

560 return r 

561 

562 return self._fXf(phi_or_sn, cn, dn, self.cK, 

563 self.deltaF, _fF) 

564 

565 def fG(self, phi_or_sn, cn=None, dn=None): 

566 '''Legendre's geodesic longitude integral in terms of 

567 Jacobi elliptic functions. 

568 

569 @arg phi_or_sn: φ or sin(φ). 

570 @kwarg cn: C{None} or cos(φ). 

571 @kwarg dn: C{None} or sqrt(1 − k2 * sin(2φ)). 

572 

573 @return: G(φ, k) as though φ ∈ (−π, π] (C{float}). 

574 

575 @raise EllipticError: Invalid invokation or no convergence. 

576 

577 @note: Legendre expresses the longitude of a point on the 

578 geodesic in terms of this combination of elliptic 

579 integrals in U{Exercices de Calcul Intégral, Vol 1 

580 (1811), p 181<https://Books.Google.com/books?id= 

581 riIOAAAAQAAJ&pg=PA181>}. 

582 

583 @see: U{Geodesics in terms of elliptic integrals<https:// 

584 GeographicLib.SourceForge.io/html/geodesic.html#geodellip>} 

585 for the expression for the longitude in terms of this function. 

586 ''' 

587 return self._fXa(phi_or_sn, cn, dn, self.alpha2 - self.k2, 

588 self.cG, self.deltaG) 

589 

590 def fH(self, phi_or_sn, cn=None, dn=None): 

591 '''Cayley's geodesic longitude difference integral in terms of 

592 Jacobi elliptic functions. 

593 

594 @arg phi_or_sn: φ or sin(φ). 

595 @kwarg cn: C{None} or cos(φ). 

596 @kwarg dn: C{None} or sqrt(1 − k2 * sin(2φ)). 

597 

598 @return: H(φ, k) as though φ ∈ (−π, π] (C{float}). 

599 

600 @raise EllipticError: Invalid invokation or no convergence. 

601 

602 @note: Cayley expresses the longitude difference of a point 

603 on the geodesic in terms of this combination of 

604 elliptic integrals in U{Phil. Mag. B{40} (1870), p 333 

605 <https://Books.Google.com/books?id=Zk0wAAAAIAAJ&pg=PA333>}. 

606 

607 @see: U{Geodesics in terms of elliptic integrals<https:// 

608 GeographicLib.SourceForge.io/html/geodesic.html#geodellip>} 

609 for the expression for the longitude in terms of this function. 

610 ''' 

611 return self._fXa(phi_or_sn, cn, dn, -self.alphap2, 

612 self.cH, self.deltaH) 

613 

614 def fPi(self, phi_or_sn, cn=None, dn=None): 

615 '''The incomplete integral of the third kind in terms of 

616 Jacobi elliptic functions. 

617 

618 @arg phi_or_sn: φ or sin(φ). 

619 @kwarg cn: C{None} or cos(φ). 

620 @kwarg dn: C{None} or sqrt(1 − k2 * sin(2φ)). 

621 

622 @return: Π(φ, α2, k) as though φ ∈ (−π, π] (C{float}). 

623 

624 @raise EllipticError: Invalid invokation or no convergence. 

625 ''' 

626 if dn is None and cn is not None: # and isscalar(phi_or_sn) 

627 dn = self.fDelta(phi_or_sn, cn) # in .triaxial 

628 return self._fXa(phi_or_sn, cn, dn, self.alpha2, 

629 self.cPi, self.deltaPi) 

630 

631 def _fXa(self, phi_or_sn, cn, dn, aX, cX, deltaX): 

632 '''(INTERNAL) Helper for C{.fG}, C{.fH} and C{.fPi}. 

633 ''' 

634 def _fX(sn, cn, dn): 

635 if sn: 

636 cn2, dn2 = cn**2, dn**2 

637 R = _RF3(self, cn2, dn2, _1_0) 

638 if aX: 

639 sn2 = sn**2 

640 p = sn2 * self.alphap2 + cn2 

641 R += _RJ(self, cn2, dn2, _1_0, p, _3over(aX, sn2)) 

642 R *= fabs(sn) 

643 r = float(R) 

644 else: # PYCHOK no cover 

645 r = _0_0 

646 return r 

647 

648 return self._fXf(phi_or_sn, cn, dn, cX, deltaX, _fX) 

649 

650 def _fXf(self, phi_or_sn, cn, dn, cX, deltaX, fX): 

651 '''(INTERNAL) Helper for C{.fD}, C{.fE}, C{.fF} and C{._fXa}. 

652 ''' 

653 self._iteration = 0 # aggregate 

654 phi = sn = phi_or_sn 

655 if cn is dn is None: # fX(phi) call 

656 sn, cn, dn = self._sncndn3(phi) 

657 if fabs(phi) >= PI: 

658 return (deltaX(sn, cn, dn) + phi) * cX / PI_2 

659 # fall through 

660 elif cn is None or dn is None: 

661 n = NN(_f_, deltaX.__name__[5:]) 

662 raise _ellipticError(n, sn, cn, dn) 

663 

664 if _signBit(cn): # enforce usual trig-like symmetries 

665 xi = cX * _2_0 - fX(sn, cn, dn) 

666 else: 

667 xi = fX(sn, cn, dn) if cn > 0 else cX 

668 return copysign0(xi, sn) 

669 

670 @Property_RO 

671 def k2(self): 

672 '''Get k^2, the square of the modulus (C{float}). 

673 ''' 

674 return self._k2 

675 

676 @Property_RO 

677 def kp2(self): 

678 '''Get k'^2, the square of the complementary modulus (C{float}). 

679 ''' 

680 return self._kp2 

681 

682 def reset(self, k2=0, alpha2=0, kp2=None, alphap2=None): # MCCABE 13 

683 '''Reset the modulus, parameter and the complementaries. 

684 

685 @kwarg k2: Modulus squared (C{float}, NINF <= k^2 <= 1). 

686 @kwarg alpha2: Parameter squared (C{float}, NINF <= α^2 <= 1). 

687 @kwarg kp2: Complementary modulus squared (C{float}, k'^2 >= 0). 

688 @kwarg alphap2: Complementary parameter squared (C{float}, α'^2 >= 0). 

689 

690 @raise EllipticError: Invalid B{C{k2}}, B{C{alpha2}}, B{C{kp2}} 

691 or B{C{alphap2}}. 

692 

693 @note: The arguments must satisfy C{B{k2} + B{kp2} = 1} and 

694 C{B{alpha2} + B{alphap2} = 1}. No checking is done 

695 that these conditions are met to enable accuracy to be 

696 maintained, e.g., when C{k} is very close to unity. 

697 ''' 

698 if self.__dict__: 

699 _update_all(self, _Named.iteration._uname, Base=Property_RO) 

700 

701 self._k2 = Scalar_(k2=k2, Error=EllipticError, low=None, high=_1_0) 

702 self._kp2 = Scalar_(kp2=((_1_0 - k2) if kp2 is None else kp2), Error=EllipticError) 

703 

704 self._alpha2 = Scalar_(alpha2=alpha2, Error=EllipticError, low=None, high=_1_0) 

705 self._alphap2 = Scalar_(alphap2=((_1_0 - alpha2) if alphap2 is None else alphap2), 

706 Error=EllipticError) 

707 

708 # Values of complete elliptic integrals for k = 0,1 and alpha = 0,1 

709 # K E D 

710 # k = 0: pi/2 pi/2 pi/4 

711 # k = 1: inf 1 inf 

712 # Pi G H 

713 # k = 0, alpha = 0: pi/2 pi/2 pi/4 

714 # k = 1, alpha = 0: inf 1 1 

715 # k = 0, alpha = 1: inf inf pi/2 

716 # k = 1, alpha = 1: inf inf inf 

717 # 

718 # G(0, k) = Pi(0, k) = H(1, k) = E(k) 

719 # H(0, k) = K(k) - D(k) 

720 # Pi(alpha2, 0) = G(alpha2, 0) = pi / (2 * sqrt(1 - alpha2)) 

721 # H( alpha2, 0) = pi / (2 * (sqrt(1 - alpha2) + 1)) 

722 # Pi(alpha2, 1) = inf 

723 # G( alpha2, 1) = H(alpha2, 1) = RC(1, alphap2) 

724 

725 def sncndn(self, x): 

726 '''The Jacobi elliptic function. 

727 

728 @arg x: The argument (C{float}). 

729 

730 @return: An L{Elliptic3Tuple}C{(sn, cn, dn)} with 

731 C{*n(B{x}, k)}. 

732 

733 @raise EllipticError: No convergence. 

734 ''' 

735 self._iteration = 0 # reset 

736 try: # Bulirsch's sncndn routine, p 89. 

737 if self.kp2: 

738 c, d, cd, mn = self._sncndn4 

739 dn = _1_0 

740 sn, cn = _sincos2(x * cd) 

741 if sn: 

742 a = cn / sn 

743 c *= a 

744 for m, n in reversed(mn): 

745 a *= c 

746 c *= dn 

747 dn = (n + a) / (m + a) 

748 a = c / m 

749 a = _1_0 / hypot1(c) 

750 sn = neg(a) if _signBit(sn) else a 

751 cn = c * sn 

752 if d and _signBit(self.kp2): 

753 cn, dn = dn, cn 

754 sn = sn / d # /= chokes PyChecker 

755 else: 

756 sn = tanh(x) 

757 cn = dn = _1_0 / cosh(x) 

758 

759 except Exception as e: 

760 raise _ellipticError(self.sncndn, x, kp2=self.kp2, cause=e) 

761 

762 return Elliptic3Tuple(sn, cn, dn, iteration=self._iteration) 

763 

764 def _sncndn3(self, phi): 

765 '''(INTERNAL) Helper for C{.fEinv} and C{._fXf}. 

766 ''' 

767 sn, cn = _sincos2(phi) 

768 return sn, cn, self.fDelta(sn, cn) 

769 

770 @Property_RO 

771 def _sncndn4(self): 

772 '''(INTERNAL) Get Bulirsch' 4-tuple C{(c, d, cd, mn)}. 

773 ''' 

774 # Bulirsch's sncndn routine, p 89. 

775 d, mc = 0, self.kp2 

776 if _signBit(mc): 

777 d = _1_0 - mc 

778 mc = neg(mc / d) 

779 d = sqrt(d) 

780 

781 mn, a = [], _1_0 

782 for i in range(1, _TRIPS): # GEOGRAPHICLIB_PANIC 

783 mc = sqrt(mc) 

784 mn.append((a, mc)) 

785 c = (a + mc) * _0_5 

786 r = fabs(mc - a) 

787 t = _TolJAC * a 

788 if r <= t: # 6 trips, quadratic 

789 _iterations(self, i) 

790 break 

791 mc *= a 

792 a = c 

793 else: # PYCHOK no cover 

794 raise _convergenceError(r, t) 

795 cd = (c * d) if d else c 

796 return c, d, cd, mn 

797 

798 @staticmethod 

799 def fRC(x, y): 

800 '''Degenerate symmetric integral of the first kind C{RC(x, y)}. 

801 

802 @return: C{RC(x, y)}, equivalent to C{RF(x, y, y)}. 

803 

804 @see: U{C{RC} definition<https://DLMF.NIST.gov/19.2.E17>} and 

805 U{Carlson<https://ArXiv.org/pdf/math/9409227.pdf>}. 

806 ''' 

807 return _rC(None, x, y) 

808 

809 @staticmethod 

810 def fRD(x, y, z, *over): 

811 '''Degenerate symmetric integral of the third kind C{RD(x, y, z)}. 

812 

813 @return: C{RD(x, y, z) / over}, equivalent to C{RJ(x, y, z, z) 

814 / over} with C{over} typically 3. 

815 

816 @see: U{C{RD} definition<https://DLMF.NIST.gov/19.16.E5>} and 

817 U{Carlson<https://ArXiv.org/pdf/math/9409227.pdf>}. 

818 ''' 

819 try: 

820 return float(_RD(None, x, y, z, *over)) 

821 except Exception as e: 

822 raise _ellipticError(Elliptic.fRD, x, y, z, *over, cause=e) 

823 

824 @staticmethod 

825 def fRF(x, y, z=0): 

826 '''Symmetric or complete symmetric integral of the first kind 

827 C{RF(x, y, z)} respectively C{RF(x, y)}. 

828 

829 @return: C{RF(x, y, z)} or C{RF(x, y)} for missing or zero B{C{z}}. 

830 

831 @see: U{C{RF} definition<https://DLMF.NIST.gov/19.16.E1>} and 

832 U{Carlson<https://ArXiv.org/pdf/math/9409227.pdf>}. 

833 ''' 

834 try: 

835 return float(_RF3(None, x, y, z)) if z else _rF2(None, x, y) 

836 except Exception as e: 

837 raise _ellipticError(Elliptic.fRF, x, y, z, cause=e) 

838 

839 @staticmethod 

840 def fRG(x, y, z=0): 

841 '''Symmetric or complete symmetric integral of the second kind 

842 C{RG(x, y, z)} respectively C{RG(x, y)}. 

843 

844 @return: C{RG(x, y, z)} or C{RG(x, y)} for missing or zero B{C{z}}. 

845 

846 @see: U{C{RG} definition<https://DLMF.NIST.gov/19.16.E3>}, 

847 U{Carlson<https://ArXiv.org/pdf/math/9409227.pdf>} and 

848 U{RG<https://GeographicLib.SourceForge.io/C++/doc/ 

849 EllipticFunction_8cpp_source.html#l00096>} version 2.3. 

850 ''' 

851 try: 

852 return _rG2(None, x, y) if z == 0 else ( 

853 _rG2(None, z, x) if y == 0 else ( 

854 _rG2(None, y, z) if x == 0 else _rG3(None, x, y, z))) 

855 except Exception as e: 

856 t = _negative_ if min(x, y, z) < 0 else NN 

857 raise _ellipticError(Elliptic.fRG, x, y, z, cause=e, txt=t) 

858 

859 @staticmethod 

860 def fRJ(x, y, z, p): 

861 '''Symmetric integral of the third kind C{RJ(x, y, z, p)}. 

862 

863 @return: C{RJ(x, y, z, p)}. 

864 

865 @see: U{C{RJ} definition<https://DLMF.NIST.gov/19.16.E2>} and 

866 U{Carlson<https://ArXiv.org/pdf/math/9409227.pdf>}. 

867 ''' 

868 try: 

869 return float(_RJ(None, x, y, z, p)) 

870 except Exception as e: 

871 raise _ellipticError(Elliptic.fRJ, x, y, z, p, cause=e) 

872 

873 @staticmethod 

874 def _RFRD(x, y, z, m): 

875 # in .auxilats.AuxDLat.DE, .auxilats.AuxLat.Rectifying 

876 try: # float(RF(x, y, z) - RD(x, y, z, 3 / m)) 

877 R = _RF3(None, x, y, z) 

878 if m: 

879 R -= _RD(None, x, y, z, _3_0 / m) 

880 except Exception as e: 

881 raise _ellipticError(Elliptic._RFRD, x, y, z, m, cause=e) 

882 return float(R) 

883 

884_allPropertiesOf_n(15, Elliptic) # # PYCHOK assert, see Elliptic.reset 

885 

886 

887class EllipticError(_ValueError): 

888 '''Elliptic function, integral, convergence or other L{Elliptic} issue. 

889 ''' 

890 pass 

891 

892 

893class Elliptic3Tuple(_NamedTuple): 

894 '''3-Tuple C{(sn, cn, dn)} all C{scalar}. 

895 ''' 

896 _Names_ = ('sn', 'cn', 'dn') 

897 _Units_ = ( Scalar, Scalar, Scalar) 

898 

899 

900class _L(list): 

901 '''(INTERNAL) Helper for C{_RD}, C{_RF3} and C{_RJ}. 

902 ''' 

903 _a0 = None 

904# _xyzp = () 

905 

906 def __init__(self, *xyzp): # x, y, z [, p] 

907 list.__init__(self, xyzp) 

908 self._xyzp = xyzp 

909 

910 def a0(self, n): 

911 '''Compute the initial C{a}. 

912 ''' 

913 t = tuple(self) 

914 m = n - len(t) 

915 if m > 0: 

916 t += t[-1:] * m 

917 try: 

918 a = Fsum(*t).fover(n) 

919 except ValueError: # Fsum(NAN) exception 

920 a = _sum(t) / n 

921 self._a0 = a 

922 return a 

923 

924 def amrs4(self, inst, y, Tol): 

925 '''Yield Carlson 4-tuples C{(An, mul, lam, s)} plus sentinel, with 

926 C{lam = fdot(sqrt(x), ... (z))} and C{s = (sqrt(x), ... (p))}. 

927 ''' 

928 L = self 

929 a = L.a0(5 if y else 3) 

930 m = 1 

931 t = max(fabs(a - _) for _ in L) / Tol 

932 for i in range(_TRIPS): 

933 d = fabs(a * m) 

934 if d > t: # 3-6 trips 

935 _iterations(inst, i) 

936 break 

937 s = map2(sqrt, L) # sqrt(x), srqt(y), sqrt(z) [, sqrt(p)] 

938 try: 

939 r = fdot(s[:3], s[1], s[2], s[0]) # sqrt(x) * sqrt(y) + ... 

940 except ValueError: # Fsum(NAN) exception 

941 r = _sum(s[i] * s[(i + 1) % 3] for i in range(3)) 

942 L[:] = [(r + _) * _0_25 for _ in L] 

943 a = (r + a) * _0_25 

944 if y: # yield only if used 

945 yield a, m, r, s # L[2] is next z 

946 m *= 4 

947 else: # PYCHOK no cover 

948 raise _convergenceError(d, t, thresh=True) 

949 yield a, m, None, () # sentinel: same a, next m, no r and s 

950 

951 def rescale(self, am, *xs): 

952 '''Rescale C{x}, C{y}, ... 

953 ''' 

954 # assert am 

955 a0 = self._a0 

956 for x in xs: 

957 yield (a0 - x) / am 

958 

959 

960def _ab2(inst, x, y): 

961 '''(INTERNAL) Yield Carlson 2-tuples C{(xn, yn)}. 

962 ''' 

963 a, b = sqrt(x), sqrt(y) 

964 if b > a: 

965 a, b = b, a 

966 yield a, b # initial x0, y0 

967 for i in range(_TRIPS): 

968 d = fabs(a - b) 

969 t = _TolRG0 * a 

970 if d <= t: # 3-4 trips 

971 _iterations(inst, i) 

972 break 

973 a, b = ((a + b) * _0_5), sqrt(a * b) 

974 yield a, b # xn, yn 

975 else: # PYCHOK no cover 

976 raise _convergenceError(d, t) 

977 

978 

979def _convergenceError(d, tol, **thresh): 

980 '''(INTERNAL) Format a no-convergence Error. 

981 ''' 

982 t = Fmt.no_convergence(d, tol, **thresh) 

983 return ValueError(t) # txt only 

984 

985 

986def _deltaX(sn, cn, dn, cX, fX): 

987 '''(INTERNAL) Helper for C{Elliptic.deltaD} thru C{.deltaPi}. 

988 ''' 

989 try: 

990 if cn is None or dn is None: 

991 raise ValueError(_invalid_) 

992 

993 if _signBit(cn): 

994 sn, cn = neg_(sn, cn) 

995 r = fX(sn, cn, dn) * PI_2 / cX 

996 return r - atan2(sn, cn) 

997 

998 except Exception as e: 

999 n = NN(_delta_, fX.__name__[1:]) 

1000 raise _ellipticError(n, sn, cn, dn, cause=e) 

1001 

1002 

1003def _ellipticError(where, *args, **kwds_cause_txt): 

1004 '''(INTERNAL) Format an L{EllipticError}. 

1005 ''' 

1006 c = _xkwds_pop(kwds_cause_txt, cause=None) 

1007 t = _xkwds_pop(kwds_cause_txt, txt=NN) 

1008 n = _xattr(where, __name__=where) # _dunder_nameof(where, where) 

1009 n = _DOT_(Elliptic.__name__, n) 

1010 n = _SPACE_(_invokation_, n) 

1011 u = unstr(n, *args, **kwds_cause_txt) 

1012 return EllipticError(u, cause=c, txt=t) 

1013 

1014 

1015def _Horner(S, e1, E2, E3, E4, E5, *over): 

1016 '''(INTERNAL) Horner form for C{_RD} and C{_RJ} below. 

1017 ''' 

1018 E22 = E2**2 

1019 # Polynomial is <https://DLMF.NIST.gov/19.36.E2> 

1020 # (1 - 3*E2/14 + E3/6 + 9*E2**2/88 - 3*E4/22 - 9*E2*E3/52 

1021 # + 3*E5/26 - E2**3/16 + 3*E3**2/40 + 3*E2*E4/20 

1022 # + 45*E2**2*E3/272 - 9*(E3*E4+E2*E5)/68) 

1023 # converted to Horner-like form ... 

1024 F = Fsum 

1025 e = e1 * 4084080 

1026 S *= e 

1027 S += F(E2 * -540540, 471240).fmul(E5) 

1028 S += F(E2 * 612612, E3 * -540540, -556920).fmul(E4) 

1029 S += F(E2 * -706860, E22 * 675675, E3 * 306306, 680680).fmul(E3) 

1030 S += F(E2 * 417690, E22 * -255255, -875160).fmul(E2) 

1031 S += 4084080 

1032 return S.fdiv((over[0] * e) if over else e) # Fsum 

1033 

1034 

1035def _iterations(inst, i): 

1036 '''(INTERNAL) Aggregate iterations B{C{i}}. 

1037 ''' 

1038 if inst and i > 0: 

1039 inst._iteration += i 

1040 

1041 

1042def _3over(a, b): 

1043 '''(INTERNAL) Return C{3 / (a * b)}. 

1044 ''' 

1045 return _over(_3_0, a * b) 

1046 

1047 

1048def _rC(unused, x, y): 

1049 '''(INTERNAL) Defined only for C{y != 0} and C{x >= 0}. 

1050 ''' 

1051 d = x - y 

1052 if d < 0: # catch NaN 

1053 # <https://DLMF.NIST.gov/19.2.E18> 

1054 d = -d 

1055 r = atan(sqrt(d / x)) if x > 0 else PI_2 

1056 elif d == 0: # XXX d < EPS0? or EPS02 or _EPSmin 

1057 d, r = y, _1_0 

1058 elif y > 0: # <https://DLMF.NIST.gov/19.2.E19> 

1059 r = asinh(sqrt(d / y)) # atanh(sqrt((x - y) / x)) 

1060 elif y < 0: # <https://DLMF.NIST.gov/19.2.E20> 

1061 r = asinh(sqrt(-x / y)) # atanh(sqrt(x / (x - y))) 

1062 else: # PYCHOK no cover 

1063 raise _ellipticError(Elliptic.fRC, x, y) 

1064 return r / sqrt(d) # float 

1065 

1066 

1067def _RD(inst, x, y, z, *over): 

1068 '''(INTERNAL) Carlson, eqs 2.28 - 2.34. 

1069 ''' 

1070 L = _L(x, y, z) 

1071 S = _D() 

1072 for a, m, r, s in L.amrs4(inst, True, _TolRF): 

1073 if s: 

1074 S += _over(_3_0, (r + z) * s[2] * m) 

1075 z = L[2] # s[2] = sqrt(z) 

1076 x, y = L.rescale(-a * m, x, y) 

1077 xy = x * y 

1078 z = (x + y) / _3_0 

1079 z2 = z**2 

1080 return _Horner(S(_1_0), sqrt(a) * a * m, 

1081 xy - _6_0 * z2, 

1082 (xy * _3_0 - _8_0 * z2) * z, 

1083 (xy - z2) * _3_0 * z2, 

1084 xy * z2 * z, *over) # Fsum 

1085 

1086 

1087def _rF2(inst, x, y): # 2-arg version, z=0 

1088 '''(INTERNAL) Carlson, eqs 2.36 - 2.38. 

1089 ''' 

1090 for a, b in _ab2(inst, x, y): # PYCHOK yield 

1091 pass 

1092 return _over(PI, a + b) # float 

1093 

1094 

1095def _RF3(inst, x, y, z): # 3-arg version 

1096 '''(INTERNAL) Carlson, eqs 2.2 - 2.7. 

1097 ''' 

1098 L = _L(x, y, z) 

1099 for a, m, _, _ in L.amrs4(inst, False, _TolRF): 

1100 pass 

1101 x, y = L.rescale(a * m, x, y) 

1102 z = neg(x + y) 

1103 xy = x * y 

1104 e2 = xy - z**2 

1105 e3 = xy * z 

1106 e4 = e2**2 

1107 # Polynomial is <https://DLMF.NIST.gov/19.36.E1> 

1108 # (1 - E2/10 + E3/14 + E2**2/24 - 3*E2*E3/44 

1109 # - 5*E2**3/208 + 3*E3**2/104 + E2**2*E3/16) 

1110 # converted to Horner-like form ... 

1111 S = Fsum(e4 * 15015, e3 * 6930, e2 * -16380, 17160).fmul(e3) 

1112 S += Fsum(e4 * -5775, e2 * 10010, -24024).fmul(e2) 

1113 S += 240240 

1114 return S.fdiv(sqrt(a) * 240240) # Fsum 

1115 

1116 

1117def _rG2(inst, x, y, PI_=PI_4): # 2-args 

1118 '''(INTERNAL) Carlson, eqs 2.36 - 2.39. 

1119 ''' 

1120 m = -1 # neg! 

1121 S = _D() 

1122 # assert not S 

1123 for a, b in _ab2(inst, x, y): # PYCHOK yield 

1124 if S: 

1125 S += (a - b)**2 * m 

1126 m *= 2 

1127 else: # initial 

1128 S += (a + b)**2 * _0_5 

1129 return S(PI_).fover(a + b) 

1130 

1131 

1132def _rG3(inst, x, y, z): # 3-arg version 

1133 '''(INTERNAL) C{x}, C{y} and C{z} all non-zero, see C{.fRG}. 

1134 ''' 

1135 R = _RF3(inst, x, y, z) * z 

1136 rd = (x - z) * (z - y) # - (y - z) 

1137 if rd: # Carlson, eq 1.7 

1138 R += _RD(inst, x, y, z, _3_0 / rd) 

1139 R += sqrt(x * y / z) 

1140 return R.fover(_2_0) 

1141 

1142 

1143def _RJ(inst, x, y, z, p, *over): 

1144 '''(INTERNAL) Carlson, eqs 2.17 - 2.25. 

1145 ''' 

1146 def _xyzp(x, y, z, p): 

1147 return (x + p) * (y + p) * (z + p) 

1148 

1149 L = _L(x, y, z, p) 

1150 n = neg(_xyzp(x, y, z, -p)) 

1151 S = _D() 

1152 for a, m, _, s in L.amrs4(inst, True, _TolRD): 

1153 if s: 

1154 d = _xyzp(*s) 

1155 if d: 

1156 if n: 

1157 rc = _rC(inst, _1_0, n / d**2 + _1_0) 

1158 n *= _1_64th # /= chokes PyChecker 

1159 else: 

1160 rc = _1_0 # == _rC(None, _1_0, _1_0) 

1161 S += rc / (d * m) 

1162 else: # PYCHOK no cover 

1163 return NAN 

1164 x, y, z = L.rescale(a * m, x, y, z) 

1165 p = Fsum(x, y, z).fover(_N_2_0) 

1166 p2 = p**2 

1167 p3 = p2 * p 

1168 E2 = Fsum(x * y, x * z, y * z, -p2 * _3_0) 

1169 E2p = E2 * p 

1170 xyz = x * y * z 

1171 return _Horner(S(_6_0), sqrt(a) * a * m, E2, 

1172 Fsum(p3 * _4_0, xyz, E2p * _2_0), 

1173 Fsum(p3 * _3_0, E2p, xyz * _2_0).fmul(p), 

1174 xyz * p2, *over) # Fsum 

1175 

1176# **) MIT License 

1177# 

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

1179# 

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

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

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

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

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

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

1186# 

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

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

1189# 

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

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

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

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

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

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

1196# OTHER DEALINGS IN THE SOFTWARE.