Coverage for pygeodesy/elliptic.py: 96%

475 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-09-01 13:41 -0400

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.08.31' 

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 _CIs(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._reset_cDEKEeps.cD 

177 

178 @Property_RO 

179 def cE(self): 

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

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

182 ''' 

183 return self._reset_cDEKEeps.cE 

184 

185 @Property_RO 

186 def cG(self): 

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

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

189 ''' 

190 return self._reset_cGHPi.cG 

191 

192 @Property_RO 

193 def cH(self): 

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

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

196 ''' 

197 return self._reset_cGHPi.cH 

198 

199 @Property_RO 

200 def cK(self): 

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

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

203 ''' 

204 return self._reset_cDEKEeps.cK 

205 

206 @Property_RO 

207 def cKE(self): 

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

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

210 ''' 

211 return self._reset_cDEKEeps.cKE 

212 

213 @Property_RO 

214 def cPi(self): 

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

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

217 ''' 

218 return self._reset_cGHPi.cPi 

219 

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

221 '''The periodic Jahnke's incomplete elliptic integral. 

222 

223 @arg sn: sin(φ). 

224 @arg cn: cos(φ). 

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

226 

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

228 

229 @raise EllipticError: Invalid invokation or no convergence. 

230 ''' 

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

232 

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

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

235 

236 @arg sn: sin(φ). 

237 @arg cn: cos(φ). 

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

239 

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

241 

242 @raise EllipticError: Invalid invokation or no convergence. 

243 ''' 

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

245 

246 def deltaEinv(self, stau, ctau): 

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

248 

249 @arg stau: sin(τ) 

250 @arg ctau: cos(τ) 

251 

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

253 

254 @raise EllipticError: No convergence. 

255 ''' 

256 try: 

257 if _signBit(ctau): # pi periodic 

258 stau, ctau = neg_(stau, ctau) 

259 t = atan2(stau, ctau) 

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

261 

262 except Exception as e: 

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

264 

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

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

267 

268 @arg sn: sin(φ). 

269 @arg cn: cos(φ). 

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

271 

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

273 

274 @raise EllipticError: Invalid invokation or no convergence. 

275 ''' 

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

277 

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

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

280 

281 @arg sn: sin(φ). 

282 @arg cn: cos(φ). 

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

284 

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

286 

287 @raise EllipticError: Invalid invokation or no convergence. 

288 ''' 

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

290 

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

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

293 

294 @arg sn: sin(φ). 

295 @arg cn: cos(φ). 

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

297 

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

299 

300 @raise EllipticError: Invalid invokation or no convergence. 

301 ''' 

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

303 

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

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

306 

307 @arg sn: sin(φ). 

308 @arg cn: cos(φ). 

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

310 

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

312 (C{float}). 

313 

314 @raise EllipticError: Invalid invokation or no convergence. 

315 ''' 

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

317 

318 def _Einv(self, x): 

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

320 ''' 

321 E2 = self.cE * _2_0 

322 n = floor(x / E2 + _0_5) 

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

324 # linear approximation 

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

326 Phi = Fsum(phi) 

327 # first order correction 

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

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

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

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

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

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

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

335 if dn: 

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

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

338 else: # PYCHOK no cover 

339 d = _0_0 # XXX continue? 

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

341 _iterations(self, i) 

342 break 

343 else: # PYCHOK no cover 

344 raise _convergenceError(d, _TolJAC) 

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

346 

347 @Property_RO 

348 def eps(self): 

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

350 ''' 

351 return self._reset_cDEKEeps.eps 

352 

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

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

355 Jacobi elliptic functions. 

356 

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

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

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

360 

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

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

363 

364 @raise EllipticError: Invalid invokation or no convergence. 

365 ''' 

366 def _fD(sn, cn, dn): 

367 r = fabs(sn)**3 

368 if r: 

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

370 return r 

371 

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

373 self.deltaD, _fD) 

374 

375 def fDelta(self, sn, cn): 

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

377 

378 @arg sn: sin(φ). 

379 @arg cn: cos(φ). 

380 

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

382 ''' 

383 try: 

384 k2 = self.k2 

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

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

387 return sqrt(s) if s else _0_0 

388 

389 except Exception as e: 

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

391 

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

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

394 Jacobi elliptic functions. 

395 

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

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

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

399 

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

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

402 

403 @raise EllipticError: Invalid invokation or no convergence. 

404 ''' 

405 def _fE(sn, cn, dn): 

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

407 ''' 

408 if sn: 

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

410 kp2, k2 = self.kp2, self.k2 

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

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

413 if k2: 

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

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

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

417 if kp2: 

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

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

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

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

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

423 Ei *= fabs(sn) 

424 ei = float(Ei) 

425 else: # PYCHOK no cover 

426 ei = _0_0 

427 return ei 

428 

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

430 self.deltaE, _fE) 

431 

432 def fEd(self, deg): 

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

434 the argument given in C{degrees}. 

435 

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

437 

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

439 

440 @raise EllipticError: No convergence. 

441 ''' 

442 if _K_2_0: 

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

444 elif fabs(deg) < _180_0: 

445 e = _0_0 

446 else: 

447 e = ceil(deg / _360_0 - _0_5) 

448 deg -= e * _360_0 

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

450 

451 def fEinv(self, x): 

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

453 

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

455 

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

457 (C{float}). 

458 

459 @raise EllipticError: No convergence. 

460 ''' 

461 try: 

462 return self._Einv(x) 

463 except Exception as e: 

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

465 

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

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

468 Jacobi elliptic functions. 

469 

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

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

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

473 

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

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

476 

477 @raise EllipticError: Invalid invokation or no convergence. 

478 ''' 

479 def _fF(sn, cn, dn): 

480 r = fabs(sn) 

481 if r: 

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

483 return r 

484 

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

486 self.deltaF, _fF) 

487 

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

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

490 Jacobi elliptic functions. 

491 

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

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

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

495 

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

497 

498 @raise EllipticError: Invalid invokation or no convergence. 

499 

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

501 geodesic in terms of this combination of elliptic 

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

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

504 riIOAAAAQAAJ&pg=PA181>}. 

505 

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

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

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

509 ''' 

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

511 self.cG, self.deltaG) 

512 

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

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

515 Jacobi elliptic functions. 

516 

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

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

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

520 

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

522 

523 @raise EllipticError: Invalid invokation or no convergence. 

524 

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

526 on the geodesic in terms of this combination of 

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

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

529 

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

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

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

533 ''' 

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

535 self.cH, self.deltaH) 

536 

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

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

539 Jacobi elliptic functions. 

540 

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

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

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

544 

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

546 

547 @raise EllipticError: Invalid invokation or no convergence. 

548 ''' 

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

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

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

552 self.cPi, self.deltaPi) 

553 

554 def _fXa(self, phi_or_sn, cn, dn, aX, cX, _deltaX): 

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

556 ''' 

557 def _fX(sn, cn, dn): 

558 if sn: 

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

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

561 if aX: 

562 sn2 = sn**2 

563 p = sn2 * self.alphap2 + cn2 

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

565 r = float(R.fmul(fabs(sn))) 

566 else: # PYCHOK no cover 

567 r = _0_0 

568 return r 

569 

570 return self._fXf(phi_or_sn, cn, dn, cX, _deltaX, _fX) 

571 

572 def _fXf(self, phi_or_sn, cn, dn, cX, _deltaX, _fX): 

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

574 ''' 

575 self._iteration = 0 # aggregate 

576 phi = sn = phi_or_sn 

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

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

579 if fabs(phi) >= PI: 

580 if cX: 

581 cX *= (_deltaX(sn, cn, dn) + phi) / PI_2 

582 return cX 

583 # fall through 

584 elif cn is None or dn is None: 

585 n = NN(_f_, _deltaX.__name__[5:]) 

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

587 

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

589 xi = cX * _2_0 - _fX(sn, cn, dn) 

590 elif cn > 0: 

591 xi = _fX(sn, cn, dn) 

592 else: 

593 xi = cX 

594 return copysign0(xi, sn) 

595 

596 @Property_RO 

597 def k2(self): 

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

599 ''' 

600 return self._k2 

601 

602 @Property_RO 

603 def kp2(self): 

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

605 ''' 

606 return self._kp2 

607 

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

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

610 

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

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

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

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

615 

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

617 or B{C{alphap2}}. 

618 

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

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

621 that these conditions are met to enable accuracy to be 

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

623 ''' 

624 if self.__dict__: 

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

626 

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

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

629 

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

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

632 Error=EllipticError) 

633 

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

635 # K E D 

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

637 # k = 1: inf 1 inf 

638 # Pi G H 

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

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

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

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

643 # 

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

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

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

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

648 # Pi(alpha2, 1) = inf 

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

650 

651 @Property_RO 

652 def _reset_cDEKEeps(self): 

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

654 ''' 

655 k2, kp2 = self.k2, self.kp2 

656 if k2: 

657 if kp2: 

658 try: 

659 self._iteration = 0 

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

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

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

663 cD = float(D) 

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

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

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

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

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

669 cK = _rF2(self, kp2, _1_0) 

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

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

672 

673 except Exception as e: 

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

675 else: 

676 cD = cK = cKE = INF 

677 cE = _1_0 

678 eps = k2 

679 else: 

680 cD = PI_4 

681 cE = cK = PI_2 

682 cKE = _0_0 # k2 * cD 

683 eps = EPS 

684 

685 return _CIs(cD=cD, cE=cE, cK=cK, cKE=cKE, eps=eps) 

686 

687 @Property_RO 

688 def _reset_cGHPi(self): 

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

690 ''' 

691 alpha2, alphap2, kp2 = self.alpha2, self.alphap2, self.kp2 

692 try: 

693 self._iteration = 0 

694 if alpha2: 

695 if alphap2: 

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

697 cK = self.cK 

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

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

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

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

702 else: 

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

704 cPi = INF # XXX or NAN? 

705 else: 

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

707 else: 

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

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

710 # So write (for alpha2 = 0) 

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

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

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

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

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

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

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

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

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

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

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

722 

723 except Exception as e: 

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

725 alphap2=alphap2, cause=e) 

726 return _CIs(cG=cG, cH=cH, cPi=cPi) 

727 

728 def sncndn(self, x): 

729 '''The Jacobi elliptic function. 

730 

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

732 

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

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

735 

736 @raise EllipticError: No convergence. 

737 ''' 

738 self._iteration = 0 # reset 

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

740 if self.kp2: 

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

742 dn = _1_0 

743 sn, cn = _sincos2(x * cd) 

744 if sn: 

745 a = cn / sn 

746 c *= a 

747 for m, n in reversed(mn): 

748 a *= c 

749 c *= dn 

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

751 a = c / m 

752 a = _1_0 / hypot1(c) 

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

754 cn = c * sn 

755 if d and _signBit(self.kp2): 

756 cn, dn = dn, cn 

757 sn = sn / d # /= chokes PyChecker 

758 else: 

759 sn = tanh(x) 

760 cn = dn = _1_0 / cosh(x) 

761 

762 except Exception as e: 

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

764 

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

766 

767 def _sncndn3(self, phi): 

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

769 ''' 

770 sn, cn = _sincos2(phi) 

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

772 

773 @Property_RO 

774 def _sncndn4(self): 

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

776 ''' 

777 # Bulirsch's sncndn routine, p 89. 

778 d, mc = 0, self.kp2 

779 if _signBit(mc): 

780 d = _1_0 - mc 

781 mc = neg(mc / d) 

782 d = sqrt(d) 

783 

784 mn, a = [], _1_0 

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

786 mc = sqrt(mc) 

787 mn.append((a, mc)) 

788 c = (a + mc) * _0_5 

789 r = fabs(mc - a) 

790 t = _TolJAC * a 

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

792 _iterations(self, i) 

793 break 

794 mc *= a 

795 a = c 

796 else: # PYCHOK no cover 

797 raise _convergenceError(r, t) 

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

799 return c, d, cd, mn 

800 

801 @staticmethod 

802 def fRC(x, y): 

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

804 

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

806 

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

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

809 ''' 

810 return _rC(None, x, y) 

811 

812 @staticmethod 

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

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

815 

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

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

818 

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

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

821 ''' 

822 try: 

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

824 except Exception as e: 

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

826 

827 @staticmethod 

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

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

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

831 

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

833 

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

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

836 ''' 

837 try: 

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

839 except Exception as e: 

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

841 

842 @staticmethod 

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

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

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

846 

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

848 

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

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

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

852 EllipticFunction_8cpp_source.html#l00096>} version 2.3. 

853 ''' 

854 try: 

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

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

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

858 

859 except Exception as e: 

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

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

862 

863 @staticmethod 

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

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

866 

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

868 

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

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

871 ''' 

872 try: 

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

874 except Exception as e: 

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

876 

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

878 

879 

880class EllipticError(_ValueError): 

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

882 ''' 

883 pass 

884 

885 

886class Elliptic3Tuple(_NamedTuple): 

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

888 ''' 

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

890 _Units_ = ( Scalar, Scalar, Scalar) 

891 

892 

893class _L(list): 

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

895 ''' 

896 _a0 = None 

897# _xyzp = () 

898 

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

900 list.__init__(self, xyzp) 

901 self._xyzp = xyzp 

902 

903 def a0(self, n): 

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

905 ''' 

906 t = tuple(self) 

907 m = n - len(t) 

908 if m > 0: 

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

910 try: 

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

912 except ValueError: # Fsum(NAN) exception 

913 a = _sum(t) / n 

914 self._a0 = a 

915 return a 

916 

917 def amrs4(self, inst, n, Tol): 

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

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

920 ''' 

921 L = self 

922 a = L.a0(n) 

923 m = 1 

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

925 for i in range(_TRIPS): 

926 d = fabs(a * m) 

927 if d > t: # 5-6 trips 

928 _iterations(inst, i) 

929 break 

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

931 try: 

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

933 except ValueError: # Fsum(NAN) exception 

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

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

936 a = (r + a) * _0_25 

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

938 m *= 4 

939 else: # PYCHOK no cover 

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

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

942 

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

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

945 ''' 

946 # assert am 

947 a0 = self._a0 

948 for x in xs: 

949 yield (a0 - x) / am 

950 

951 

952def _ab2(inst, x, y): 

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

954 ''' 

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

956 if b > a: 

957 a, b = b, a 

958 yield a, b # initial x0, y0 

959 for i in range(1, _TRIPS): 

960 d = fabs(a - b) 

961 t = _TolRG0 * a 

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

963 _iterations(inst, i - 1) 

964 break 

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

966 yield a, b # xn, yn 

967 else: # PYCHOK no cover 

968 raise _convergenceError(d, t) 

969 

970 

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

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

973 ''' 

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

975 return ValueError(t) # txt only 

976 

977 

978def _deltaX(sn, cn, dn, cX, _fX): 

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

980 ''' 

981 try: 

982 if cn is None or dn is None: 

983 raise ValueError(_invalid_) 

984 

985 if _signBit(cn): 

986 sn, cn = neg_(sn, cn) 

987 r = _fX(sn, cn, dn) * PI_2 / cX 

988 return r - atan2(sn, cn) 

989 

990 except Exception as e: 

991 n = NN(_delta_, _fX.__name__[1:]) 

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

993 

994 

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

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

997 ''' 

998 c = _xkwds_pop(kwds_cause_txt, cause=None) 

999 t = _xkwds_pop(kwds_cause_txt, txt=NN) 

1000 n = _xattr(where, __name__=where) 

1001 n = _DOT_(Elliptic.__name__, n) 

1002 n = _SPACE_(_invokation_, n) 

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

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

1005 

1006 

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

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

1009 ''' 

1010 E22 = E2**2 

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

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

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

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

1015 # converted to Horner-like form ... 

1016 F = Fsum 

1017 e = e1 * 4084080 

1018 S *= e 

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

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

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

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

1023 S += 4084080 

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

1025 

1026 

1027def _iterations(inst, i): 

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

1029 ''' 

1030 if inst and i > 0: 

1031 inst._iteration += i 

1032 

1033 

1034def _3over(a, b): 

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

1036 ''' 

1037 return _over(_3_0, a * b) 

1038 

1039 

1040def _rC(unused, x, y): 

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

1042 ''' 

1043 d = x - y 

1044 if d < 0: # catch NaN 

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

1046 d = -d 

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

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

1049 d, r = y, _1_0 

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

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

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

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

1054 else: # PYCHOK no cover 

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

1056 return r / sqrt(d) # float 

1057 

1058 

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

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

1061 ''' 

1062 L = _L(x, y, z) 

1063 S = _D() 

1064 for a, m, r, s in L.amrs4(inst, 5, _TolRF): 

1065 if s: 

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

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

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

1069 xy = x * y 

1070 z = (x + y) / _3_0 

1071 z2 = z**2 

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

1073 xy - _6_0 * z2, 

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

1075 (xy - z2) * _3_0 * z2, 

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

1077 

1078 

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

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

1081 ''' 

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

1083 pass 

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

1085 

1086 

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

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

1089 ''' 

1090 L = _L(x, y, z) 

1091 for a, m, _, _ in L.amrs4(inst, 3, _TolRF): 

1092 pass 

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

1094 z = neg(x + y) 

1095 xy = x * y 

1096 e2 = xy - z**2 

1097 e3 = xy * z 

1098 e4 = e2**2 

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

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

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

1102 # converted to Horner-like form ... 

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

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

1105 S += 240240 

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

1107 

1108 

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

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

1111 ''' 

1112 m = -1 # neg! 

1113 S = _D() 

1114 # assert not S 

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

1116 if S: 

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

1118 m *= 2 

1119 else: # initial 

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

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

1122 

1123 

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

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

1126 ''' 

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

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

1129 if rd: # Carlson, eq 1.7 

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

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

1132 return R.fover(_2_0) 

1133 

1134 

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

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

1137 ''' 

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

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

1140 

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

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

1143 S = _D() 

1144 for a, m, _, s in L.amrs4(inst, 5, _TolRD): 

1145 if s: 

1146 d = _xyzp(*s) 

1147 if d: 

1148 if n: 

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

1150 n *= _1_64th # /= chokes PyChecker 

1151 else: 

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

1153 S += rc / (d * m) 

1154 else: # PYCHOK no cover 

1155 return NAN 

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

1157 xyz = x * y * z 

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

1159 p2 = p**2 

1160 p3 = p2 * p 

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

1162 E2p = E2 * p 

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

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

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

1166 xyz * p2, *over) # Fsum 

1167 

1168# **) MIT License 

1169# 

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

1171# 

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

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

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

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

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

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

1178# 

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

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

1181# 

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

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

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

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

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

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

1188# OTHER DEALINGS IN THE SOFTWARE.