Coverage for pygeodesy/fmath.py: 87%

327 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2024-09-25 17:40 -0400

1 

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

3 

4u'''Utilities using precision floating point summation. 

5''' 

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

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

8 

9from pygeodesy.basics import _copysign, copysign0, isbool, isint, isscalar, \ 

10 len2, map1, _xiterable 

11from pygeodesy.constants import EPS0, EPS02, EPS1, NAN, PI, PI_2, PI_4, \ 

12 _0_0, _0_125, _1_6th, _0_25, _1_3rd, _0_5, _1_0, \ 

13 _N_1_0, _1_5, _copysign_0_0, isfinite, remainder 

14from pygeodesy.errors import _IsnotError, LenError, _TypeError, _ValueError, \ 

15 _xError, _xkwds_get1, _xkwds_pop2 

16from pygeodesy.fsums import _2float, Fsum, fsum, fsum1_, _isFsumTuple, \ 

17 _1primed, _Psum_, Fmt, unstr 

18from pygeodesy.interns import MISSING, _negative_, _not_scalar_ 

19from pygeodesy.lazily import _ALL_LAZY, _sys_version_info2 

20# from pygeodesy.streprs import Fmt, unstr # from .fsums 

21from pygeodesy.units import Int_, _isHeight, _isRadius, Float_ # PYCHOK for .heights 

22 

23from math import fabs, sqrt # pow 

24import operator as _operator # in .datums, .trf, .utm 

25 

26__all__ = _ALL_LAZY.fmath 

27__version__ = '24.09.19' 

28 

29# sqrt(2) - 1 <https://WikiPedia.org/wiki/Square_root_of_2> 

30_0_4142 = 0.41421356237309504880 # ... ~ 3730904090310553 / 9007199254740992 

31_2_3rd = _1_3rd * 2 

32_h_lt_b_ = 'abs(h) < abs(b)' 

33 

34 

35class Fdot(Fsum): 

36 '''Precision dot product. 

37 ''' 

38 def __init__(self, a, *b, **name_RESIDUAL): 

39 '''New L{Fdot} precision dot product M{sum(a[i] * b[i] for i=0..len(a)-1)}. 

40 

41 @arg a: Iterable of values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

42 @arg b: Other values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all positional. 

43 @kwarg name_RESIDUAL: Optional C{B{name}=NN} (C{str}) and the C{B{RESIDUAL}=0.0} 

44 threshold (C{scalar}) for raising L{ResidualError}s, see class 

45 L{Fsum<Fsum.__init__>}. 

46 

47 @raise LenError: Unequal C{len(B{a})} and C{len(B{b})}. 

48 

49 @raise OverflowError: Partial C{2sum} overflow. 

50 

51 @raise TypeError: Invalid B{C{x}}. 

52 

53 @raise ValueError: Non-finite B{C{x}}. 

54 

55 @see: Function L{fdot} and method L{Fsum.fadd}. 

56 ''' 

57 Fsum.__init__(self, **name_RESIDUAL) 

58 self.fadd(_map_mul(a, b, Fdot)) 

59 

60 

61class Fhorner(Fsum): 

62 '''Precision polynomial evaluation using the Horner form. 

63 ''' 

64 def __init__(self, x, *cs, **incx_name_RESIDUAL): 

65 '''New L{Fhorner} evaluation of polynomial M{sum(cs[i] * x**i for i=0..n)} if 

66 C{B{incx}=False} for decreasing exponent M{sum(... i=n..0)} where C{n = 

67 len(cs) - 1}. 

68 

69 @arg x: Polynomial argument (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

70 @arg cs: Polynomial coeffients (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), 

71 all positional. 

72 @kwarg incx_name_RESIDUAL: Optional C{B{name}=NN} (C{str}), C{B{incx}=True} 

73 for in-/decreasing exponents (C{bool}) and the C{B{RESIDUAL}=0.0} 

74 threshold (C{scalar}) for raising L{ResidualError}s, see class 

75 L{Fsum<Fsum.__init__>}. 

76 

77 @raise OverflowError: Partial C{2sum} overflow. 

78 

79 @raise TypeError: Invalid B{C{x}}. 

80 

81 @raise ValueError: Non-finite B{C{x}}. 

82 

83 @see: Function L{fhorner} and methods L{Fsum.fadd} and L{Fsum.fmul}. 

84 ''' 

85 incx, name_RESIDUAL = _xkwds_pop2(incx_name_RESIDUAL, incx=True) 

86 Fsum.__init__(self, **name_RESIDUAL) 

87 if cs: 

88 self._fhorner(x, cs, Fhorner.__name__, incx=incx) 

89 else: 

90 self._fset_ps(_0_0) 

91 

92 

93class Fhypot(Fsum): 

94 '''Precision summation and hypotenuse, default C{root=2}. 

95 ''' 

96 def __init__(self, *xs, **root_name_RESIDUAL_raiser): 

97 '''New L{Fhypot} hypotenuse of (the I{root} of) several components (raised 

98 to the power I{root}). 

99 

100 @arg xs: Components (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all 

101 positional. 

102 @kwarg root_name_RESIDUAL_raiser: Optional, exponent and C{B{root}=2} order 

103 (C{scalar}), C{B{name}=NN} (C{str}), the C{B{RESIDUAL}=0.0} 

104 threshold (C{scalar}) and C{B{raiser}=True} (C{bool}) for 

105 raising L{ResidualError}s, see class L{Fsum<Fsum.__init__>} and 

106 method L{root<Fsum.root>}. 

107 ''' 

108 r = None # _xkwds_pop2 error 

109 try: 

110 r, kwds = _xkwds_pop2(root_name_RESIDUAL_raiser, root=2) 

111 r, kwds = _xkwds_pop2(kwds, power=r) # for backward compatibility 

112 raiser = _Fsum__init__(self, **kwds) 

113 if xs: 

114 self._facc_power(r, xs, Fhypot, **raiser) 

115 self._fset(self.root(r, **raiser)) 

116 except Exception as X: 

117 raise self._ErrorXs(X, xs, root=r) 

118 

119 

120class Fpolynomial(Fsum): 

121 '''Precision polynomial evaluation. 

122 ''' 

123 def __init__(self, x, *cs, **name_RESIDUAL): 

124 '''New L{Fpolynomial} evaluation of the polynomial M{sum(cs[i] * x**i for 

125 i=0..len(cs)-1)}. 

126 

127 @arg x: Polynomial argument (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

128 @arg cs: Polynomial coeffients (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), 

129 all positional. 

130 @kwarg name_RESIDUAL: Optional C{B{name}=NN} (C{str}) and the C{B{RESIDUAL}=0.0} 

131 threshold (C{scalar}) for raising L{ResidualError}s, see class 

132 L{Fsum<Fsum.__init__>}. 

133 

134 @raise OverflowError: Partial C{2sum} overflow. 

135 

136 @raise TypeError: Invalid B{C{x}}. 

137 

138 @raise ValueError: Non-finite B{C{x}}. 

139 

140 @see: Class L{Fhorner}, function L{fpolynomial} and method L{Fsum.fadd}. 

141 ''' 

142 Fsum.__init__(self, *cs[:1], **name_RESIDUAL) 

143 n = len(cs) - 1 

144 if n > 0: 

145 self.fadd(_1map_mul(cs[1:], _powers(x, n))) 

146 elif n < 0: 

147 self._fset_ps(_0_0) 

148 

149 

150class Fpowers(Fsum): 

151 '''Precision summation of powers, optimized for C{power=2, 3 and 4}. 

152 ''' 

153 def __init__(self, power, *xs, **name_RESIDUAL_raiser): 

154 '''New L{Fpowers} sum of (the I{power} of) several bases. 

155 

156 @arg power: The exponent (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

157 @arg xs: One or more bases (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all 

158 positional. 

159 @kwarg name_RESIDUAL_raiser: Optional C{B{name}=NN} (C{str}), the C{B{RESIDUAL}=0.0} 

160 threshold (C{scalar}) and C{B{raiser}=True} (C{bool}) for raising 

161 L{ResidualError}s, see class L{Fsum<Fsum.__init__>} and method 

162 L{fpow<Fsum.fpow>}. 

163 ''' 

164 try: 

165 raiser = _Fsum__init__(self, **name_RESIDUAL_raiser) 

166 if xs: 

167 self._facc_power(power, xs, Fpowers, **raiser) # x**0 == 1 

168 except Exception as X: 

169 raise self._ErrorXs(X, xs, power=power) 

170 

171 

172class Froot(Fsum): 

173 '''The root of a precision summation. 

174 ''' 

175 def __init__(self, root, *xs, **name_RESIDUAL_raiser): 

176 '''New L{Froot} root of a precision sum. 

177 

178 @arg root: The order (C{scalar}, an L{Fsum} or L{Fsum2Tuple}), non-zero. 

179 @arg xs: Items to summate (each a C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all 

180 positional. 

181 @kwarg name_RESIDUAL_raiser: Optional C{B{name}=NN} (C{str}), the C{B{RESIDUAL}=0.0} 

182 threshold (C{scalar}) and C{B{raiser}=True} (C{bool}) for raising 

183 L{ResidualError}s, see class L{Fsum<Fsum.__init__>} and method 

184 L{fpow<Fsum.fpow>}. 

185 ''' 

186 try: 

187 raiser = _Fsum__init__(self, **name_RESIDUAL_raiser) 

188 if xs: 

189 self.fadd(xs) 

190 self._fset(self.root(root, **raiser)) 

191 except Exception as X: 

192 raise self._ErrorXs(X, xs, root=root) 

193 

194 

195class Fcbrt(Froot): 

196 '''Cubic root of a precision summation. 

197 ''' 

198 def __init__(self, *xs, **name_RESIDUAL_raiser): 

199 '''New L{Fcbrt} cubic root of a precision sum. 

200 

201 @see: Class L{Froot} for further details. 

202 ''' 

203 Froot.__init__(self, 3, *xs, **name_RESIDUAL_raiser) 

204 

205 

206class Fsqrt(Froot): 

207 '''Square root of a precision summation. 

208 ''' 

209 def __init__(self, *xs, **name_RESIDUAL_raiser): 

210 '''New L{Fsqrt} square root of a precision sum. 

211 

212 @see: Class L{Froot} for further details. 

213 ''' 

214 Froot.__init__(self, 2, *xs, **name_RESIDUAL_raiser) 

215 

216 

217def _Fsum__init__(inst, raiser=MISSING, **name_RESIDUAL): 

218 '''(INTERNAL) Init an C{F...} instance above. 

219 ''' 

220 Fsum.__init__(inst, **name_RESIDUAL) # PYCHOK self 

221 inst._fset_ps(_0_0) 

222 return {} if raiser is MISSING else dict(raiser=raiser) 

223 

224 

225def bqrt(x): 

226 '''Return the 4-th, I{bi-quadratic} or I{quartic} root, M{x**(1 / 4)}, 

227 preserving C{type(B{x})}. 

228 

229 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

230 

231 @return: I{Quartic} root (C{float} or an L{Fsum}). 

232 

233 @raise TypeeError: Invalid B{C{x}}. 

234 

235 @raise ValueError: Negative B{C{x}}. 

236 

237 @see: Functions L{zcrt} and L{zqrt}. 

238 ''' 

239 return _root(x, _0_25, bqrt) 

240 

241 

242try: 

243 from math import cbrt as _cbrt # Python 3.11+ 

244 

245except ImportError: # Python 3.10- 

246 

247 def _cbrt(x): 

248 '''(INTERNAL) Compute the I{signed}, cube root M{x**(1/3)}. 

249 ''' 

250 # <https://archive.lib.MSU.edu/crcmath/math/math/r/r021.htm> 

251 # simpler and more accurate than Ken Turkowski's CubeRoot, see 

252 # <https://People.FreeBSD.org/~lstewart/references/apple_tr_kt32_cuberoot.pdf> 

253 return _copysign(pow(fabs(x), _1_3rd), x) # to avoid complex 

254 

255 

256def cbrt(x): 

257 '''Compute the cube root M{x**(1/3)}, preserving C{type(B{x})}. 

258 

259 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

260 

261 @return: Cubic root (C{float} or L{Fsum}). 

262 

263 @see: Functions L{cbrt2} and L{sqrt3}. 

264 ''' 

265 if _isFsumTuple(x): 

266 r = abs(x).fpow(_1_3rd) 

267 if x.signOf() < 0: 

268 r = -r 

269 else: 

270 r = _cbrt(x) 

271 return r # cbrt(-0.0) == -0.0 

272 

273 

274def cbrt2(x): # PYCHOK attr 

275 '''Compute the cube root I{squared} M{x**(2/3)}, preserving C{type(B{x})}. 

276 

277 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

278 

279 @return: Cube root I{squared} (C{float} or L{Fsum}). 

280 

281 @see: Functions L{cbrt} and L{sqrt3}. 

282 ''' 

283 return abs(x).fpow(_2_3rd) if _isFsumTuple(x) else _cbrt(x**2) 

284 

285 

286def euclid(x, y): 

287 '''I{Appoximate} the norm M{sqrt(x**2 + y**2)} by M{max(abs(x), 

288 abs(y)) + min(abs(x), abs(y)) * 0.4142...}. 

289 

290 @arg x: X component (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

291 @arg y: Y component (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

292 

293 @return: Appoximate norm (C{float} or L{Fsum}). 

294 

295 @see: Function L{euclid_}. 

296 ''' 

297 x, y = abs(x), abs(y) # NOT fabs! 

298 if y > x: 

299 x, y = y, x 

300 return x + y * _0_4142 # XXX * _0_5 before 20.10.02 

301 

302 

303def euclid_(*xs): 

304 '''I{Appoximate} the norm M{sqrt(sum(x**2 for x in xs))} by cascaded 

305 L{euclid}. 

306 

307 @arg xs: X arguments (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), 

308 all positional. 

309 

310 @return: Appoximate norm (C{float} or L{Fsum}). 

311 

312 @see: Function L{euclid}. 

313 ''' 

314 e = _0_0 

315 for x in sorted(map(abs, xs)): # NOT fabs, reverse=True! 

316 # e = euclid(x, e) 

317 if e < x: 

318 e, x = x, e 

319 if x: 

320 e += x * _0_4142 

321 return e 

322 

323 

324def facos1(x): 

325 '''Fast approximation of L{pygeodesy.acos1}C{(B{x})}. 

326 

327 @see: U{ShaderFastLibs.h<https://GitHub.com/michaldrobot/ 

328 ShaderFastLibs/blob/master/ShaderFastMathLib.h>}. 

329 ''' 

330 a = fabs(x) 

331 if a < EPS0: 

332 r = PI_2 

333 elif a < EPS1: 

334 H = Fhorner(-a, 1.5707288, 0.2121144, 0.0742610, 0.0187293) 

335 H *= Fsqrt(_1_0, -a) 

336 r = float(H) 

337 if x < 0: 

338 r = PI - r 

339 else: 

340 r = PI if x < 0 else _0_0 

341 return r 

342 

343 

344def fasin1(x): # PYCHOK no cover 

345 '''Fast approximation of L{pygeodesy.asin1}C{(B{x})}. 

346 

347 @see: L{facos1}. 

348 ''' 

349 return PI_2 - facos1(x) 

350 

351 

352def fatan(x): 

353 '''Fast approximation of C{atan(B{x})}. 

354 ''' 

355 a = fabs(x) 

356 if a < _1_0: 

357 r = fatan1(a) if a else _0_0 

358 elif a > _1_0: 

359 r = PI_2 - fatan1(_1_0 / a) # == fatan2(a, _1_0) 

360 else: 

361 r = PI_4 

362 if x < 0: # copysign0(r, x) 

363 r = -r 

364 return r 

365 

366 

367def fatan1(x): 

368 '''Fast approximation of C{atan(B{x})} for C{0 <= B{x} < 1}, I{unchecked}. 

369 

370 @see: U{ShaderFastLibs.h<https://GitHub.com/michaldrobot/ShaderFastLibs/ 

371 blob/master/ShaderFastMathLib.h>} and U{Efficient approximations 

372 for the arctangent function<http://www-Labs.IRO.UMontreal.CA/ 

373 ~mignotte/IFT2425/Documents/EfficientApproximationArctgFunction.pdf>}, 

374 IEEE Signal Processing Magazine, 111, May 2006. 

375 ''' 

376 # Eq (9): PI_4 * x - x * (abs(x) - 1) * (0.2447 + 0.0663 * abs(x)), for -1 < x < 1 

377 # PI_4 * x - (x**2 - x) * (0.2447 + 0.0663 * x), for 0 < x < 1 

378 # x * (1.0300981633974482 + x * (-0.1784 - x * 0.0663)) 

379 H = Fhorner(x, _0_0, 1.0300981634, -0.1784, -0.0663) 

380 return float(H) 

381 

382 

383def fatan2(y, x): 

384 '''Fast approximation of C{atan2(B{y}, B{x})}. 

385 

386 @see: U{fastApproximateAtan(x, y)<https://GitHub.com/CesiumGS/cesium/blob/ 

387 master/Source/Shaders/Builtin/Functions/fastApproximateAtan.glsl>} 

388 and L{fatan1}. 

389 ''' 

390 a, b = fabs(x), fabs(y) 

391 if b > a: 

392 r = (PI_2 - fatan1(a / b)) if a else PI_2 

393 elif a > b: 

394 r = fatan1(b / a) if b else _0_0 

395 elif a: # a == b != 0 

396 r = PI_4 

397 else: # a == b == 0 

398 return _0_0 

399 if x < 0: 

400 r = PI - r 

401 if y < 0: # copysign0(r, y) 

402 r = -r 

403 return r 

404 

405 

406def favg(a, b, f=_0_5): 

407 '''Return the precise average of two values. 

408 

409 @arg a: One (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

410 @arg b: Other (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

411 @kwarg f: Optional fraction (C{float}). 

412 

413 @return: M{a + f * (b - a)} (C{float}). 

414 ''' 

415# @raise ValueError: Fraction out of range. 

416# ''' 

417# if not 0 <= f <= 1: # XXX restrict fraction? 

418# raise _ValueError(fraction=f) 

419 # a + f * (b - a) == a * (1 - f) + b * f 

420 return fsum1_(a, a * (-f), b * f) 

421 

422 

423def fdot(a, *b): 

424 '''Return the precision dot product M{sum(a[i] * b[i] for ni=0..len(a))}. 

425 

426 @arg a: Iterable of values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

427 @arg b: Other values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all 

428 positional. 

429 

430 @return: Dot product (C{float}). 

431 

432 @raise LenError: Unequal C{len(B{a})} and C{len(B{b})}. 

433 

434 @see: Class L{Fdot}, U{Algorithm 5.10 B{DotK} 

435 <https://www.TUHH.De/ti3/paper/rump/OgRuOi05.pdf>} and function 

436 C{math.sumprod} in Python 3.12 and later. 

437 ''' 

438 return fsum(_map_mul(a, b, fdot)) 

439 

440 

441def fdot3(xs, ys, zs, start=0): 

442 '''Return the precision dot product M{start + sum(a[i] * b[i] * c[i] 

443 for i=0..len(a)-1)}. 

444 

445 @arg xs: Iterable (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

446 @arg ys: Iterable (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

447 @arg zs: Iterable (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

448 @kwarg start: Optional bias (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

449 

450 @return: Dot product (C{float}). 

451 

452 @raise LenError: Unequal C{len(B{xs})}, C{len(B{ys})} and/or C{len(B{zs})}. 

453 

454 @raise OverflowError: Partial C{2sum} overflow. 

455 ''' 

456 def _mul3(xs, ys, zs, s, p): 

457 if s: 

458 yield s 

459 if p: 

460 yield _1_0 

461 for x, y, z in zip(xs, ys, zs): 

462 yield (Fsum(x) * y) * z 

463 if p: 

464 yield _N_1_0 

465 

466 n = len(xs) 

467 if not n == len(ys) == len(zs): 

468 raise LenError(fdot3, xs=n, ys=len(ys), zs=len(zs)) 

469 

470 return fsum(_mul3(xs, ys, zs, start, n < 4)) 

471 

472 

473def fhorner(x, *cs, **incx): 

474 '''Evaluate a polynomial using the Horner form M{sum(cs[i] * x**i 

475 for i=0..n)} or if C{B{incx}=False} for decreasing exponent 

476 M{sum(... i=n..0)} where C{n = len(cs) - 1}. 

477 

478 @return: Horner sum (C{float}). 

479 

480 @see: Class L{Fhorner} for further details. 

481 ''' 

482 H = Fhorner(x, *cs, **incx) 

483 return float(H) 

484 

485 

486def fidw(xs, ds, beta=2): 

487 '''Interpolate using U{Inverse Distance Weighting 

488 <https://WikiPedia.org/wiki/Inverse_distance_weighting>} (IDW). 

489 

490 @arg xs: Known values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

491 @arg ds: Non-negative distances (each C{scalar}, an L{Fsum} or 

492 L{Fsum2Tuple}). 

493 @kwarg beta: Inverse distance power (C{int}, 0, 1, 2, or 3). 

494 

495 @return: Interpolated value C{x} (C{float}). 

496 

497 @raise LenError: Unequal or zero C{len(B{ds})} and C{len(B{xs})}. 

498 

499 @raise TypeError: An invalid B{C{ds}} or B{C{xs}}. 

500 

501 @raise ValueError: Invalid B{C{beta}}, negative B{C{ds}} or 

502 weighted B{C{ds}} below L{EPS}. 

503 

504 @note: Using C{B{beta}=0} returns the mean of B{C{xs}}. 

505 ''' 

506 n, xs = len2(xs) 

507 if n > 1: 

508 b = -Int_(beta=beta, low=0, high=3) 

509 if b < 0: 

510 try: # weighted 

511 _F = Fsum 

512 W = _F() 

513 X = _F() 

514 for i, d in enumerate(_xiterable(ds)): 

515 x = xs[i] 

516 D = _F(d) 

517 if D < EPS0: 

518 if D < 0: 

519 raise ValueError(_negative_) 

520 x = float(x) 

521 i = n 

522 break 

523 if D.fpow(b): 

524 W += D 

525 X += D.fmul(x) 

526 else: 

527 x = X.fover(W, raiser=False) 

528 i += 1 # len(xs) >= len(ds) 

529 except IndexError: 

530 i += 1 # len(xs) < i < len(ds) 

531 except Exception as X: 

532 _I = Fmt.INDEX 

533 raise _xError(X, _I(xs=i), x, _I(ds=i), d) 

534 else: # b == 0 

535 x = fsum(xs) / n # fmean(xs) 

536 i = n 

537 elif n: 

538 x = float(xs[0]) 

539 i = n 

540 else: 

541 x = _0_0 

542 i, _ = len2(ds) 

543 if i != n: 

544 raise LenError(fidw, xs=n, ds=i) 

545 return x 

546 

547 

548def fma(x, y, z, **raiser): 

549 '''Fused-multiply-add, using C{math.fma(x, y, z)} from Python 3.13+ 

550 or an equivalent implementation. 

551 

552 @arg x: Multiplicand (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

553 @arg y: Multiplier (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

554 @arg z: Addend (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

555 @kwarg raiser: Keyword argument C{B{raiser}=False}, if C{True}, 

556 throw an exception, otherwise pass any non-finite 

557 result (C{bool}). 

558 

559 @return: C{(x * y) + z} (L{Fsum} or C{float}). 

560 ''' 

561 return _Psum_(x).fma(y, z, raiser=raiser).as_iscalar 

562 

563 

564def fmean(xs): 

565 '''Compute the accurate mean M{sum(xs) / len(xs)}. 

566 

567 @arg xs: Values (each C{scalar}, or L{Fsum} or L{Fsum2Tuple}). 

568 

569 @return: Mean value (C{float}). 

570 

571 @raise LenError: No B{C{xs}} values. 

572 

573 @raise OverflowError: Partial C{2sum} overflow. 

574 ''' 

575 n, xs = len2(xs) 

576 if n < 1: 

577 raise LenError(fmean, xs=xs) 

578 return Fsum(*xs).fover(n) if n > 1 else _2float(index=0, xs=xs[0]) 

579 

580 

581def fmean_(*xs): 

582 '''Compute the accurate mean M{sum(xs) / len(xs)}. 

583 

584 @see: Function L{fmean} for further details. 

585 ''' 

586 return fmean(xs) 

587 

588 

589def f2mul_(x, *ys, **raiser): 

590 '''Cascaded, accurate multiplication C{B{x} * B{y} * B{y} ...} for all B{C{ys}}. 

591 

592 @arg x: Multiplicand (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

593 @arg ys: Multipliers (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all 

594 positional. 

595 @kwarg raiser: Keyword argument C{B{raiser}=False}, if C{True}, throw an 

596 exception, otherwise pass any non-finite result (C{bool}). 

597 

598 @return: The cascaded I{TwoProduct} (L{Fsum}, C{float} or C{int}). 

599 

600 @see: U{Equations 2.3<https://www.TUHH.De/ti3/paper/rump/OzOgRuOi06.pdf>} 

601 ''' 

602 return _Psum_(x).f2mul_(*ys, **raiser).as_iscalar 

603 

604 

605def fpolynomial(x, *cs, **over): 

606 '''Evaluate the polynomial M{sum(cs[i] * x**i for i=0..len(cs)) 

607 [/ over]}. 

608 

609 @kwarg over: Optional final, I{non-zero} divisor (C{scalar}). 

610 

611 @return: Polynomial value (C{float}). 

612 

613 @see: Class L{Fpolynomial} for further details. 

614 ''' 

615 P = Fpolynomial(x, *cs) 

616 d = _xkwds_get1(over, over=0) if over else 0 

617 return P.fover(d) if d else float(P) 

618 

619 

620def fpowers(x, n, alts=0): 

621 '''Return a series of powers M{[x**i for i=1..n]}. 

622 

623 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

624 @arg n: Highest exponent (C{int}). 

625 @kwarg alts: Only alternating powers, starting with this 

626 exponent (C{int}). 

627 

628 @return: Tuple of powers of B{C{x}} (each C{type(B{x})}). 

629 

630 @raise TypeError: Invalid B{C{x}} or B{C{n}} not C{int}. 

631 

632 @raise ValueError: Non-finite B{C{x}} or invalid B{C{n}}. 

633 ''' 

634 if not isint(n): 

635 raise _IsnotError(int.__name__, n=n) 

636 elif n < 1: 

637 raise _ValueError(n=n) 

638 

639 p = x if isscalar(x) or _isFsumTuple(x) else _2float(x=x) 

640 ps = tuple(_powers(p, n)) 

641 

642 if alts > 0: # x**2, x**4, ... 

643 # ps[alts-1::2] chokes PyChecker 

644 ps = ps[slice(alts-1, None, 2)] 

645 

646 return ps 

647 

648 

649try: 

650 from math import prod as fprod # Python 3.8 

651except ImportError: 

652 

653 def fprod(xs, start=1): 

654 '''Iterable product, like C{math.prod} or C{numpy.prod}. 

655 

656 @arg xs: Iterable of values to be multiplied (each 

657 C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

658 @kwarg start: Initial value, also the value returned 

659 for an empty B{C{xs}} (C{scalar}). 

660 

661 @return: The product (C{float} or an L{Fsum}). 

662 

663 @see: U{NumPy.prod<https://docs.SciPy.org/doc/ 

664 numpy/reference/generated/numpy.prod.html>}. 

665 ''' 

666 return freduce(_operator.mul, xs, start) 

667 

668 

669def frandoms(n, seeded=None): 

670 '''Generate C{n} (long) lists of random C{floats}. 

671 

672 @arg n: Number of lists to generate (C{int}, non-negative). 

673 @kwarg seeded: If C{scalar}, use C{random.seed(B{seeded})} or 

674 if C{True}, seed using today's C{year-day}. 

675 

676 @see: U{Hettinger<https://GitHub.com/ActiveState/code/tree/master/recipes/ 

677 Python/393090_Binary_floating_point_summatiaccurate_full/recipe-393090.py>}. 

678 ''' 

679 from random import gauss, random, seed, shuffle 

680 

681 if seeded is None: 

682 pass 

683 elif seeded and isbool(seeded): 

684 from time import localtime 

685 seed(localtime().tm_yday) 

686 elif isscalar(seeded): 

687 seed(seeded) 

688 

689 c = (7, 1e100, -7, -1e100, -9e-20, 8e-20) * 7 

690 for _ in range(n): 

691 s = 0 

692 t = list(c) 

693 _a = t.append 

694 for _ in range(n * 8): 

695 v = gauss(0, random())**7 - s 

696 _a(v) 

697 s += v 

698 shuffle(t) 

699 yield t 

700 

701 

702def frange(start, number, step=1): 

703 '''Generate a range of C{float}s. 

704 

705 @arg start: First value (C{float}). 

706 @arg number: The number of C{float}s to generate (C{int}). 

707 @kwarg step: Increment value (C{float}). 

708 

709 @return: A generator (C{float}s). 

710 

711 @see: U{NumPy.prod<https://docs.SciPy.org/doc/ 

712 numpy/reference/generated/numpy.arange.html>}. 

713 ''' 

714 if not isint(number): 

715 raise _IsnotError(int.__name__, number=number) 

716 for i in range(number): 

717 yield start + (step * i) 

718 

719 

720try: 

721 from functools import reduce as freduce 

722except ImportError: 

723 try: 

724 freduce = reduce # PYCHOK expected 

725 except NameError: # Python 3+ 

726 

727 def freduce(f, xs, *start): 

728 '''For missing C{functools.reduce}. 

729 ''' 

730 if start: 

731 r = v = start[0] 

732 else: 

733 r, v = 0, MISSING 

734 for v in xs: 

735 r = f(r, v) 

736 if v is MISSING: 

737 raise _TypeError(xs=(), start=MISSING) 

738 return r 

739 

740 

741def fremainder(x, y): 

742 '''Remainder in range C{[-B{y / 2}, B{y / 2}]}. 

743 

744 @arg x: Numerator (C{scalar}). 

745 @arg y: Modulus, denominator (C{scalar}). 

746 

747 @return: Remainder (C{scalar}, preserving signed 

748 0.0) or C{NAN} for any non-finite B{C{x}}. 

749 

750 @raise ValueError: Infinite or near-zero B{C{y}}. 

751 

752 @see: I{Karney}'s U{Math.remainder<https://PyPI.org/ 

753 project/geographiclib/>} and Python 3.7+ 

754 U{math.remainder<https://docs.Python.org/3/ 

755 library/math.html#math.remainder>}. 

756 ''' 

757 # with Python 2.7.16 and 3.7.3 on macOS 10.13.6 and 

758 # with Python 3.10.2 on macOS 12.2.1 M1 arm64 native 

759 # fmod( 0, 360) == 0.0 

760 # fmod( 360, 360) == 0.0 

761 # fmod(-0, 360) == 0.0 

762 # fmod(-0.0, 360) == -0.0 

763 # fmod(-360, 360) == -0.0 

764 # however, using the % operator ... 

765 # 0 % 360 == 0 

766 # 360 % 360 == 0 

767 # 360.0 % 360 == 0.0 

768 # -0 % 360 == 0 

769 # -360 % 360 == 0 == (-360) % 360 

770 # -0.0 % 360 == 0.0 == (-0.0) % 360 

771 # -360.0 % 360 == 0.0 == (-360.0) % 360 

772 

773 # On Windows 32-bit with python 2.7, math.fmod(-0.0, 360) 

774 # == +0.0. This fixes this bug. See also Math::AngNormalize 

775 # in the C++ library, Math.sincosd has a similar fix. 

776 if isfinite(x): 

777 try: 

778 r = remainder(x, y) if x else x 

779 except Exception as e: 

780 raise _xError(e, unstr(fremainder, x, y)) 

781 else: # handle x INF and NINF as NAN 

782 r = NAN 

783 return r 

784 

785 

786if _sys_version_info2 < (3, 8): # PYCHOK no cover 

787 from math import hypot # OK in Python 3.7- 

788 

789 def hypot_(*xs): 

790 '''Compute the norm M{sqrt(sum(x**2 for x in xs))}. 

791 

792 Similar to Python 3.8+ n-dimension U{math.hypot 

793 <https://docs.Python.org/3.8/library/math.html#math.hypot>}, 

794 but exceptions, C{nan} and C{infinite} values are 

795 handled differently. 

796 

797 @arg xs: X arguments (C{scalar}s), all positional. 

798 

799 @return: Norm (C{float}). 

800 

801 @raise OverflowError: Partial C{2sum} overflow. 

802 

803 @raise ValueError: Invalid or no B{C{xs}} values. 

804 

805 @note: The Python 3.8+ Euclidian distance U{math.dist 

806 <https://docs.Python.org/3.8/library/math.html#math.dist>} 

807 between 2 I{n}-dimensional points I{p1} and I{p2} can be 

808 computed as M{hypot_(*((c1 - c2) for c1, c2 in zip(p1, p2)))}, 

809 provided I{p1} and I{p2} have the same, non-zero length I{n}. 

810 ''' 

811 return float(Fhypot(*xs, raiser=False)) 

812 

813elif _sys_version_info2 < (3, 10): 

814 # In Python 3.8 and 3.9 C{math.hypot} is inaccurate, see 

815 # U{agdhruv<https://GitHub.com/geopy/geopy/issues/466>}, 

816 # U{cffk<https://Bugs.Python.org/issue43088>} and module 

817 # U{geomath.py<https://PyPI.org/project/geographiclib/1.52>} 

818 

819 def hypot(x, y): 

820 '''Compute the norm M{sqrt(x**2 + y**2)}. 

821 

822 @arg x: X argument (C{scalar}). 

823 @arg y: Y argument (C{scalar}). 

824 

825 @return: C{sqrt(B{x}**2 + B{y}**2)} (C{float}). 

826 ''' 

827 return float(Fhypot(x, y, raiser=False)) 

828 

829 from math import hypot as hypot_ # PYCHOK in Python 3.8 and 3.9 

830else: 

831 from math import hypot # PYCHOK in Python 3.10+ 

832 hypot_ = hypot 

833 

834 

835def hypot1(x): 

836 '''Compute the norm M{sqrt(1 + x**2)}. 

837 

838 @arg x: Argument (C{scalar} or L{Fsum} or L{Fsum2Tuple}). 

839 

840 @return: Norm (C{float}). 

841 ''' 

842 if _isFsumTuple(x): 

843 h = float(Fhypot(_1_0, x)) if x else _1_0 

844 else: 

845 h = hypot(_1_0, x) if x else _1_0 

846 return h 

847 

848 

849def hypot2(x, y): 

850 '''Compute the I{squared} norm M{x**2 + y**2}. 

851 

852 @arg x: X (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

853 @arg y: Y (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

854 

855 @return: C{B{x}**2 + B{y}**2} (C{float}). 

856 ''' 

857 x, y = map1(abs, x, y) # NOT fabs! 

858 if y > x: 

859 x, y = y, x 

860 if x: 

861 h2 = x**2 

862 if y: 

863 h2 *= (y / x)**2 + _1_0 

864 h2 = float(h2) 

865 else: 

866 h2 = _0_0 

867 return h2 

868 

869 

870def hypot2_(*xs): 

871 '''Compute the I{squared} norm C{fsum(x**2 for x in B{xs})}. 

872 

873 @arg xs: Components (each C{scalar}, an L{Fsum} or 

874 L{Fsum2Tuple}), all positional. 

875 

876 @return: Squared norm (C{float}). 

877 

878 @see: Class L{Fpowers} for further details. 

879 ''' 

880 h2 = float(max(map(abs, xs))) if xs else _0_0 

881 if h2: 

882 _h = _1_0 / h2 

883 h2 = Fpowers(2, *((x * _h) for x in xs)) 

884 h2 = h2.fover(_h**2) 

885 return h2 

886 

887 

888def _map_mul(xs, ys, where): 

889 '''(INTERNAL) Yield each B{C{x * y}}. 

890 ''' 

891 n = len(ys) 

892 if len(xs) != n: # PYCHOK no cover 

893 raise LenError(where, xs=len(xs), ys=n) 

894 return _1map_mul(xs, ys) if n < 4 else map( 

895 _operator.mul, map(Fsum, xs), ys) 

896 

897 

898def _1map_mul(xs, ys): 

899 '''(INTERNAL) Yield each B{C{x * y}}, 1-primed. 

900 ''' 

901 return _1primed(map(_operator.mul, map(Fsum, xs), ys)) 

902 

903 

904def norm2(x, y): 

905 '''Normalize a 2-dimensional vector. 

906 

907 @arg x: X component (C{scalar}). 

908 @arg y: Y component (C{scalar}). 

909 

910 @return: 2-Tuple C{(x, y)}, normalized. 

911 

912 @raise ValueError: Invalid B{C{x}} or B{C{y}} 

913 or zero norm. 

914 ''' 

915 try: 

916 h = None 

917 h = hypot(x, y) 

918 if h: 

919 x, y = (x / h), (y / h) 

920 else: 

921 x = _copysign_0_0(x) # pass? 

922 y = _copysign_0_0(y) 

923 except Exception as e: 

924 raise _xError(e, x=x, y=y, h=h) 

925 return x, y 

926 

927 

928def norm_(*xs): 

929 '''Normalize the components of an n-dimensional vector. 

930 

931 @arg xs: Components (each C{scalar}, an L{Fsum} or 

932 L{Fsum2Tuple}), all positional. 

933 

934 @return: Yield each component, normalized. 

935 

936 @raise ValueError: Invalid or insufficent B{C{xs}} 

937 or zero norm. 

938 ''' 

939 try: 

940 i = x = h = None 

941 h = hypot_(*xs) 

942 _h = (_1_0 / h) if h else _0_0 

943 for i, x in enumerate(xs): 

944 yield x * _h 

945 except Exception as X: 

946 raise _xError(X, Fmt.SQUARE(xs=i), x, h=h) 

947 

948 

949def _powers(x, n): 

950 '''(INTERNAL) Yield C{x**i for i=1..n}. 

951 ''' 

952 p = 1 # type(p) == type(x) 

953 for _ in range(n): 

954 p *= x 

955 yield p 

956 

957 

958def _root(x, p, where): 

959 '''(INTERNAL) Raise C{x} to power C{0 < p < 1}. 

960 ''' 

961 try: 

962 if x > 0: 

963 return Fsum(x).fpow(p).as_iscalar 

964 elif x < 0: 

965 raise ValueError(_negative_) 

966 except Exception as X: 

967 raise _xError(X, unstr(where, x)) 

968 return _0_0 

969 

970 

971def sqrt0(x, Error=None): 

972 '''Return the square root C{sqrt(B{x})} iff C{B{x} > }L{EPS02}, 

973 preserving C{type(B{x})}. 

974 

975 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

976 @kwarg Error: Error to raise for negative B{C{x}}. 

977 

978 @return: Square root (C{float} or L{Fsum}) or C{0.0}. 

979 

980 @raise TypeeError: Invalid B{C{x}}. 

981 

982 @note: Any C{B{x} < }L{EPS02} I{including} C{B{x} < 0} 

983 returns C{0.0}. 

984 ''' 

985 if Error and x < 0: 

986 raise Error(unstr(sqrt0, x)) 

987 return _root(x, _0_5, sqrt0) if x > EPS02 else ( 

988 _0_0 if x < EPS02 else EPS0) 

989 

990 

991def sqrt3(x): 

992 '''Return the square root, I{cubed} M{sqrt(x)**3} or M{sqrt(x**3)}, 

993 preserving C{type(B{x})}. 

994 

995 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

996 

997 @return: Square root I{cubed} (C{float} or L{Fsum}). 

998 

999 @raise TypeeError: Invalid B{C{x}}. 

1000 

1001 @raise ValueError: Negative B{C{x}}. 

1002 

1003 @see: Functions L{cbrt} and L{cbrt2}. 

1004 ''' 

1005 return _root(x, _1_5, sqrt3) 

1006 

1007 

1008def sqrt_a(h, b): 

1009 '''Compute C{I{a}} side of a right-angled triangle from 

1010 C{sqrt(B{h}**2 - B{b}**2)}. 

1011 

1012 @arg h: Hypotenuse or outer annulus radius (C{scalar}). 

1013 @arg b: Triangle side or inner annulus radius (C{scalar}). 

1014 

1015 @return: C{copysign(I{a}, B{h})} or C{unsigned 0.0} (C{float}). 

1016 

1017 @raise TypeError: Non-scalar B{C{h}} or B{C{b}}. 

1018 

1019 @raise ValueError: If C{abs(B{h}) < abs(B{b})}. 

1020 

1021 @see: Inner tangent chord B{I{d}} of an U{annulus 

1022 <https://WikiPedia.org/wiki/Annulus_(mathematics)>} 

1023 and function U{annulus_area<https://People.SC.FSU.edu/ 

1024 ~jburkardt/py_src/geometry/geometry.py>}. 

1025 ''' 

1026 try: 

1027 if not (_isHeight(h) and _isRadius(b)): 

1028 raise TypeError(_not_scalar_) 

1029 c = fabs(h) 

1030 if c > EPS0: 

1031 s = _1_0 - (b / c)**2 

1032 if s < 0: 

1033 raise ValueError(_h_lt_b_) 

1034 a = (sqrt(s) * c) if 0 < s < 1 else (c if s else _0_0) 

1035 else: # PYCHOK no cover 

1036 b = fabs(b) 

1037 d = c - b 

1038 if d < 0: 

1039 raise ValueError(_h_lt_b_) 

1040 d *= c + b 

1041 a = sqrt(d) if d else _0_0 

1042 except Exception as x: 

1043 raise _xError(x, h=h, b=b) 

1044 return copysign0(a, h) 

1045 

1046 

1047def zcrt(x): 

1048 '''Return the 6-th, I{zenzi-cubic} root, M{x**(1 / 6)}, 

1049 preserving C{type(B{x})}. 

1050 

1051 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

1052 

1053 @return: I{Zenzi-cubic} root (C{float} or L{Fsum}). 

1054 

1055 @see: Functions L{bqrt} and L{zqrt}. 

1056 

1057 @raise TypeeError: Invalid B{C{x}}. 

1058 

1059 @raise ValueError: Negative B{C{x}}. 

1060 ''' 

1061 return _root(x, _1_6th, zcrt) 

1062 

1063 

1064def zqrt(x): 

1065 '''Return the 8-th, I{zenzi-quartic} or I{squared-quartic} root, 

1066 M{x**(1 / 8)}, preserving C{type(B{x})}. 

1067 

1068 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}). 

1069 

1070 @return: I{Zenzi-quartic} root (C{float} or L{Fsum}). 

1071 

1072 @see: Functions L{bqrt} and L{zcrt}. 

1073 

1074 @raise TypeeError: Invalid B{C{x}}. 

1075 

1076 @raise ValueError: Negative B{C{x}}. 

1077 ''' 

1078 return _root(x, _0_125, zqrt) 

1079 

1080# **) MIT License 

1081# 

1082# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved. 

1083# 

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

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

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

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

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

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

1090# 

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

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

1093# 

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

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

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

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

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

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

1100# OTHER DEALINGS IN THE SOFTWARE.