Coverage for pygeodesy/fmath.py: 90%

323 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2024-05-25 12:04 -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_get, _xkwds_pop2 

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

17 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.05.24' 

28 

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

30_0_4142 = 0.41421356237309504880 # ... sqrt(2) - 1 

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 

40 i=0..len(a)-1)}. 

41 

42 @arg a: Iterable of values (each C{scalar} or an L{Fsum} or L{Fsum2Tuple} 

43 instance). 

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

45 all positional. 

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

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

48 L{Fsum<Fsum.__init__>}. 

49 

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

51 

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

53 

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

55 

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

57 

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

59 ''' 

60 Fsum.__init__(self, **name_RESIDUAL) 

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

62 

63 

64class Fhorner(Fsum): 

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

66 ''' 

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

68 '''New L{Fhorner} evaluation of polynomial M{sum(cs[i] * x**i for 

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

70 

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

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

73 instance), all positional. 

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

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

76 L{Fsum<Fsum.__init__>}. 

77 

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

79 

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

81 

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

83 

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

85 ''' 

86 Fsum.__init__(self, **name_RESIDUAL) 

87 if cs: 

88 if _isFsumTuple(x): 

89 _mul = self._mul_Fsum 

90 else: 

91 _mul = self._mul_scalar 

92 x = _2float(x=x) 

93 op = Fhorner.__name__ 

94 if len(cs) > 1 and x: 

95 for c in reversed(cs): 

96 self._fset_ps(_mul(x, op)) 

97 self._fadd(c, op, up=False) 

98 self._update() 

99 else: # x == 0 

100 self._fadd(cs[0], op) 

101 else: 

102 self._fset_ps(_0_0) 

103 

104 

105class Fhypot(Fsum): 

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

107 ''' 

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

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

110 (raised to the power I{root}). 

111 

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

113 all positional. 

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

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

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

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

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

119 ''' 

120 r = None # _xkwds_pop2 error 

121 try: 

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

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

124 raiser = _Fsum__init__(self, **kwds) 

125 if xs: 

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

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

128 except Exception as X: 

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

130 

131 

132class Fpolynomial(Fsum): 

133 '''Precision polynomial evaluation. 

134 ''' 

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

136 '''New L{Fpolynomial} evaluation of the polynomial 

137 M{sum(cs[i] * x**i for i=0..len(cs)-1)}. 

138 

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

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

141 instance), all positional. 

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

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

144 L{Fsum<Fsum.__init__>}. 

145 

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

147 

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

149 

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

151 

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

153 ''' 

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

155 n = len(cs) - 1 

156 if n > 0: 

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

158 elif n < 0: 

159 self._fset_ps(_0_0) 

160 

161 

162class Fpowers(Fsum): 

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

164 ''' 

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

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

167 

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

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

170 all positional. 

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

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

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

174 L{fpow<Fsum.fpow>}. 

175 ''' 

176 try: 

177 raiser = _Fsum__init__(self, **name_RESIDUAL_raiser) 

178 if xs: 

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

180 except Exception as X: 

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

182 

183 

184class Froot(Fsum): 

185 '''The root of a precision summation. 

186 ''' 

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

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

189 

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

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

192 all positional. 

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

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

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

196 L{fpow<Fsum.fpow>}. 

197 ''' 

198 try: 

199 raiser = _Fsum__init__(self, **name_RESIDUAL_raiser) 

200 if xs: 

201 self.fadd(xs) 

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

203 except Exception as X: 

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

205 

206 

207class Fcbrt(Froot): 

208 '''Cubic root of a precision summation. 

209 ''' 

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

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

212 

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

214 ''' 

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

216 

217 

218class Fsqrt(Froot): 

219 '''Square root of a precision summation. 

220 ''' 

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

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

223 

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

225 ''' 

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

227 

228 

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

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

231 ''' 

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

233 inst._fset_ps(_0_0) 

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

235 

236 

237def bqrt(x): 

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

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

240 

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

242 

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

244 

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

246 

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

248 

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

250 ''' 

251 return _root(x, _0_25, bqrt) 

252 

253 

254try: 

255 from math import cbrt as _cbrt # Python 3.11+ 

256 

257except ImportError: # Python 3.10- 

258 

259 def _cbrt(x): 

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

261 ''' 

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

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

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

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

266 

267 

268def cbrt(x): 

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

270 

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

272 

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

274 

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

276 ''' 

277 if _isFsumTuple(x): 

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

279 if x.signOf() < 0: 

280 r = -r 

281 else: 

282 r = _cbrt(x) 

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

284 

285 

286def cbrt2(x): # PYCHOK attr 

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

288 

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

290 

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

292 

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

294 ''' 

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

296 

297 

298def euclid(x, y): 

299 '''I{Appoximate} the norm M{sqrt(x**2 + y**2)} by 

300 M{max(abs(x), abs(y)) + min(abs(x), abs(y)) * 0.4142...}. 

301 

302 @arg x: X component (C{scalar} or L{Fsum} instance). 

303 @arg y: Y component (C{scalar} or L{Fsum} instance). 

304 

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

306 

307 @see: Function L{euclid_}. 

308 ''' 

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

310 if y > x: 

311 x, y = y, x 

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

313 

314 

315def euclid_(*xs): 

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

317 cascaded L{euclid}. 

318 

319 @arg xs: X arguments (each C{scalar} or an L{Fsum} 

320 instance), all positional. 

321 

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

323 

324 @see: Function L{euclid}. 

325 ''' 

326 e = _0_0 

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

328 # e = euclid(x, e) 

329 if e < x: 

330 e, x = x, e 

331 if x: 

332 e += x * _0_4142 

333 return e 

334 

335 

336def facos1(x): 

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

338 

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

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

341 ''' 

342 a = fabs(x) 

343 if a < EPS0: 

344 r = PI_2 

345 elif a < EPS1: 

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

347 r = float(H * sqrt(_1_0 - a)) 

348 if x < 0: 

349 r = PI - r 

350 else: 

351 r = PI if x < 0 else _0_0 

352 return r 

353 

354 

355def fasin1(x): # PYCHOK no cover 

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

357 

358 @see: L{facos1}. 

359 ''' 

360 return PI_2 - facos1(x) 

361 

362 

363def fatan(x): 

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

365 ''' 

366 a = fabs(x) 

367 if a < _1_0: 

368 r = fatan1(a) if a else _0_0 

369 elif a > _1_0: 

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

371 else: 

372 r = PI_4 

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

374 r = -r 

375 return r 

376 

377 

378def fatan1(x): 

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

380 

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

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

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

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

385 IEEE Signal Processing Magazine, 111, May 2006. 

386 ''' 

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

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

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

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

391 return float(H) 

392 

393 

394def fatan2(y, x): 

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

396 

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

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

399 and L{fatan1}. 

400 ''' 

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

402 if b > a: 

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

404 elif a > b: 

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

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

407 r = PI_4 

408 else: # a == b == 0 

409 return _0_0 

410 if x < 0: 

411 r = PI - r 

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

413 r = -r 

414 return r 

415 

416 

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

418 '''Return the precision average of two values. 

419 

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

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

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

423 

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

425 ''' 

426# @raise ValueError: Fraction out of range. 

427# ''' 

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

429# raise _ValueError(fraction=f) 

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

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

432 

433 

434def fdot(a, *b): 

435 '''Return the precision dot product M{sum(a[i] * b[i] for 

436 i=0..len(a))}. 

437 

438 @arg a: Iterable of values (each C{scalar}). 

439 @arg b: Other values (each C{scalar}), all positional. 

440 

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

442 

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

444 

445 @see: Class L{Fdot} and U{Algorithm 5.10 B{DotK} 

446 <https://www.TUHH.De/ti3/paper/rump/OgRuOi05.pdf>}. 

447 ''' 

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

449 

450 

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

452 '''Return the precision dot product M{start + 

453 sum(a[i] * b[i] * c[i] for i=0..len(a)-1)}. 

454 

455 @arg xs: Iterable (each C{scalar} or an L{Fsum} or 

456 L{Fsum2Tuple} instance). 

457 @arg ys: Iterable (each C{scalar} or an L{Fsum} or 

458 L{Fsum2Tuple} instance). 

459 @arg zs: Iterable (each C{scalar} or an L{Fsum} or 

460 L{Fsum2Tuple} instance). 

461 @kwarg start: Optional bias (C{scalar} or an L{Fsum} 

462 or L{Fsum2Tuple}). 

463 

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

465 

466 @raise LenError: Unequal C{len(B{xs})}, C{len(B{ys})} 

467 and/or C{len(B{zs})}. 

468 

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

470 ''' 

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

472 if s: 

473 yield s 

474 if p: 

475 yield _1_0 

476 _F = Fsum 

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

478 yield (_F(x) * y) * z 

479 if p: 

480 yield _N_1_0 

481 

482 n = len(xs) 

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

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

485 

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

487 

488 

489def fhorner(x, *cs): 

490 '''Evaluate the polynomial M{sum(cs[i] * x**i for 

491 i=0..len(cs)-1)} using the Horner form. 

492 

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

494 

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

496 ''' 

497 H = Fhorner(x, *cs) 

498 return float(H) 

499 

500 

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

502 '''Interpolate using U{Inverse Distance Weighting 

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

504 

505 @arg xs: Known values (each C{scalar} or an L{Fsum} or 

506 L{Fsum2Tuple} instance). 

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

508 or L{Fsum2Tuple} instance). 

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

510 

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

512 

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

514 

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

516 

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

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

519 

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

521 ''' 

522 n, xs = len2(xs) 

523 if n > 1: 

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

525 if b < 0: 

526 try: # weighted 

527 _F = Fsum 

528 W = _F() 

529 X = _F() 

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

531 x = xs[i] 

532 D = _F(d) 

533 if D < EPS0: 

534 if D < 0: 

535 raise ValueError(_negative_) 

536 x = float(x) 

537 i = n 

538 break 

539 if D.fpow(b): 

540 W += D 

541 X += D.fmul(x) 

542 else: 

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

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

545 except IndexError: 

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

547 except Exception as X: 

548 _I = Fmt.INDEX 

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

550 else: # b == 0 

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

552 i = n 

553 elif n: 

554 x = float(xs[0]) 

555 i = n 

556 else: 

557 x = _0_0 

558 i, _ = len2(ds) 

559 if i != n: 

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

561 return x 

562 

563 

564def fmean(xs): 

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

566 

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

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 fpolynomial(x, *cs, **over): 

590 '''Evaluate the polynomial M{sum(cs[i] * x**i for 

591 i=0..len(cs)) [/ over]}. 

592 

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

594 

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

596 

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

598 ''' 

599 P = Fpolynomial(x, *cs) 

600 d = _xkwds_get(over, over=0) if over else 0 

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

602 

603 

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

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

606 

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

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

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

610 exponent (C{int}). 

611 

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

613 

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

615 

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

617 ''' 

618 if not isint(n): 

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

620 elif n < 1: 

621 raise _ValueError(n=n) 

622 

623 p = x if isint(x) or _isFsumTuple(x) else _2float(x=x) 

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

625 

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

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

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

629 

630 return ps 

631 

632 

633try: 

634 from math import prod as fprod # Python 3.8 

635except ImportError: 

636 

637 def fprod(xs, start=1): 

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

639 

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

641 C{scalar} or an L{Fsum}). 

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

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

644 

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

646 

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

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

649 ''' 

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

651 

652 

653def frandoms(n, seeded=None): 

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

655 

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

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

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

659 

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

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

662 ''' 

663 from random import gauss, random, seed, shuffle 

664 

665 if seeded is None: 

666 pass 

667 elif seeded and isbool(seeded): 

668 from time import localtime 

669 seed(localtime().tm_yday) 

670 elif isscalar(seeded): 

671 seed(seeded) 

672 

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

674 for _ in range(n): 

675 s = 0 

676 t = list(c) 

677 _a = t.append 

678 for _ in range(n * 8): 

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

680 _a(v) 

681 s += v 

682 shuffle(t) 

683 yield t 

684 

685 

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

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

688 

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

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

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

692 

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

694 

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

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

697 ''' 

698 if not isint(number): 

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

700 for i in range(number): 

701 yield start + (step * i) 

702 

703 

704try: 

705 from functools import reduce as freduce 

706except ImportError: 

707 try: 

708 freduce = reduce # PYCHOK expected 

709 except NameError: # Python 3+ 

710 

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

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

713 ''' 

714 if start: 

715 r = v = start[0] 

716 else: 

717 r, v = 0, MISSING 

718 for v in xs: 

719 r = f(r, v) 

720 if v is MISSING: 

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

722 return r 

723 

724 

725def fremainder(x, y): 

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

727 

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

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

730 

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

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

733 

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

735 

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

737 project/geographiclib/>} and Python 3.7+ 

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

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

740 ''' 

741 # with Python 2.7.16 and 3.7.3 on macOS 10.13.6 and 

742 # with Python 3.10.2 on macOS 12.2.1 M1 arm64 native 

743 # fmod( 0, 360) == 0.0 

744 # fmod( 360, 360) == 0.0 

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

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

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

748 # however, using the % operator ... 

749 # 0 % 360 == 0 

750 # 360 % 360 == 0 

751 # 360.0 % 360 == 0.0 

752 # -0 % 360 == 0 

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

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

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

756 

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

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

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

760 if _isfinite(x): 

761 try: 

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

763 except Exception as e: 

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

765 else: # handle x INF and NINF as NAN 

766 r = NAN 

767 return r 

768 

769 

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

771 from math import hypot # OK in Python 3.7- 

772 

773 def hypot_(*xs): 

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

775 

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

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

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

779 handled differently. 

780 

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

782 

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

784 

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

786 

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

788 

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

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

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

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

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

794 ''' 

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

796 

797elif _sys_version_info2 < (3, 10): 

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

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

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

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

802 

803 def hypot(x, y): 

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

805 

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

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

808 

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

810 ''' 

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

812 

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

814else: 

815 from math import hypot # PYCHOK in Python 3.10+ 

816 hypot_ = hypot 

817 

818 

819def hypot1(x): 

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

821 

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

823 

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

825 ''' 

826 if _isFsumTuple(x): 

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

828 else: 

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

830 return h 

831 

832 

833def hypot2(x, y): 

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

835 

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

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

838 

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

840 ''' 

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

842 if y > x: 

843 x, y = y, x 

844 if x: 

845 h2 = x**2 

846 if y: 

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

848 h2 = float(h2) 

849 else: 

850 h2 = _0_0 

851 return h2 

852 

853 

854def hypot2_(*xs): 

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

856 

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

858 L{Fsum2Tuple} instance), all positional. 

859 

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

861 

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

863 ''' 

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

865 if h2: 

866 _h = _1_0 / h2 

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

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

869 return h2 

870 

871 

872def _map_mul(xs, ys, where): 

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

874 ''' 

875 n = len(ys) 

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

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

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

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

880 

881 

882def _1map_mul(xs, ys): 

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

884 ''' 

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

886 

887 

888def norm2(x, y): 

889 '''Normalize a 2-dimensional vector. 

890 

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

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

893 

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

895 

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

897 or zero norm. 

898 ''' 

899 try: 

900 h = hypot(x, y) 

901 if h: 

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

903 else: 

904 x = _copysign_0_0(x) # pass? 

905 y = _copysign_0_0(y) 

906 except Exception as e: 

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

908 return x, y 

909 

910 

911def norm_(*xs): 

912 '''Normalize all n-dimensional vector components. 

913 

914 @arg xs: Components (C{scalar}s), all positional. 

915 

916 @return: Yield each component, normalized. 

917 

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

919 or zero norm. 

920 ''' 

921 try: 

922 i = x = h = None 

923 h = hypot_(*xs) 

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

925 for i, x in enumerate(xs): 

926 yield x * _h 

927 except Exception as X: 

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

929 

930 

931def _powers(x, n): 

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

933 ''' 

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

935 for _ in range(n): 

936 p *= x 

937 yield p 

938 

939 

940def _root(x, p, where): 

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

942 ''' 

943 try: 

944 if x > 0: 

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

946 elif x < 0: 

947 raise ValueError(_negative_) 

948 except Exception as X: 

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

950 return _0_0 

951 

952 

953def sqrt0(x, Error=None): 

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

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

956 

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

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

959 

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

961 

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

963 

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

965 returns C{0.0}. 

966 ''' 

967 if Error and x < 0: 

968 raise Error(unstr(sqrt0, x)) 

969 return _root(x, _0_5, sqrt0) if x > EPS02 else (_0_0 if x < EPS02 else EPS0) 

970 

971 

972def sqrt3(x): 

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

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

975 

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

977 

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

979 

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

981 

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

983 

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

985 ''' 

986 return _root(x, _1_5, sqrt3) 

987 

988 

989def sqrt_a(h, b): 

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

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

992 

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

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

995 

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

997 

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

999 

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

1001 

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

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

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

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

1006 ''' 

1007 try: 

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

1009 raise TypeError(_not_scalar_) 

1010 c = fabs(h) 

1011 if c > EPS0: 

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

1013 if s < 0: 

1014 raise ValueError(_h_lt_b_) 

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

1016 else: # PYCHOK no cover 

1017 b = fabs(b) 

1018 d = c - b 

1019 if d < 0: 

1020 raise ValueError(_h_lt_b_) 

1021 d *= c + b 

1022 a = sqrt(d) if d else _0_0 

1023 except Exception as x: 

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

1025 return copysign0(a, h) 

1026 

1027 

1028def zcrt(x): 

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

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

1031 

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

1033 

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

1035 

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

1037 

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

1039 

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

1041 ''' 

1042 return _root(x, _1_6th, zcrt) 

1043 

1044 

1045def zqrt(x): 

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

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

1048 

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

1050 

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

1052 

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

1054 

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

1056 

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

1058 ''' 

1059 return _root(x, _0_125, zqrt) 

1060 

1061# **) MIT License 

1062# 

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

1064# 

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

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

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

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

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

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

1071# 

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

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

1074# 

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

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

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

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

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

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

1081# OTHER DEALINGS IN THE SOFTWARE.