Coverage for pygeodesy/fmath.py: 89%
319 statements
« prev ^ index » next coverage.py v7.6.0, created at 2024-09-30 14:00 -0400
« prev ^ index » next coverage.py v7.6.0, created at 2024-09-30 14:00 -0400
2# -*- coding: utf-8 -*-
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
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, _xsError
16from pygeodesy.fsums import _2float, Fsum, fsum, fsum1_, _isFsum_2Tuple, \
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
23from math import fabs, sqrt # pow
24import operator as _operator # in .datums, .trf, .utm
26__all__ = _ALL_LAZY.fmath
27__version__ = '24.09.29'
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)'
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)}.
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__>}.
47 @raise LenError: Unequal C{len(B{a})} and C{len(B{b})}.
49 @raise OverflowError: Partial C{2sum} overflow.
51 @raise TypeError: Invalid B{C{x}}.
53 @raise ValueError: Non-finite B{C{x}}.
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))
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}.
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__>}.
77 @raise OverflowError: Partial C{2sum} overflow.
79 @raise TypeError: Invalid B{C{x}}.
81 @raise ValueError: Non-finite B{C{x}}.
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)
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}).
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)
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)}.
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__>}.
134 @raise OverflowError: Partial C{2sum} overflow.
136 @raise TypeError: Invalid B{C{x}}.
138 @raise ValueError: Non-finite B{C{x}}.
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)
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.
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)
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.
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)
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.
201 @see: Class L{Froot} for further details.
202 '''
203 Froot.__init__(self, 3, *xs, **name_RESIDUAL_raiser)
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.
212 @see: Class L{Froot} for further details.
213 '''
214 Froot.__init__(self, 2, *xs, **name_RESIDUAL_raiser)
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)
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})}.
229 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
231 @return: I{Quartic} root (C{float} or an L{Fsum}).
233 @raise TypeeError: Invalid B{C{x}}.
235 @raise ValueError: Negative B{C{x}}.
237 @see: Functions L{zcrt} and L{zqrt}.
238 '''
239 return _root(x, _0_25, bqrt)
242try:
243 from math import cbrt as _cbrt # Python 3.11+
245except ImportError: # Python 3.10-
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
256def cbrt(x):
257 '''Compute the cube root M{x**(1/3)}, preserving C{type(B{x})}.
259 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
261 @return: Cubic root (C{float} or L{Fsum}).
263 @see: Functions L{cbrt2} and L{sqrt3}.
264 '''
265 if _isFsum_2Tuple(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
274def cbrt2(x): # PYCHOK attr
275 '''Compute the cube root I{squared} M{x**(2/3)}, preserving C{type(B{x})}.
277 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
279 @return: Cube root I{squared} (C{float} or L{Fsum}).
281 @see: Functions L{cbrt} and L{sqrt3}.
282 '''
283 return abs(x).fpow(_2_3rd) if _isFsum_2Tuple(x) else _cbrt(x**2)
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...}.
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}).
293 @return: Appoximate norm (C{float} or L{Fsum}).
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
303def euclid_(*xs):
304 '''I{Appoximate} the norm M{sqrt(sum(x**2 for x in xs))} by cascaded
305 L{euclid}.
307 @arg xs: X arguments (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}),
308 all positional.
310 @return: Appoximate norm (C{float} or L{Fsum}).
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
324def facos1(x):
325 '''Fast approximation of L{pygeodesy.acos1}C{(B{x})}.
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
344def fasin1(x): # PYCHOK no cover
345 '''Fast approximation of L{pygeodesy.asin1}C{(B{x})}.
347 @see: L{facos1}.
348 '''
349 return PI_2 - facos1(x)
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
367def fatan1(x):
368 '''Fast approximation of C{atan(B{x})} for C{0 <= B{x} < 1}, I{unchecked}.
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)
383def fatan2(y, x):
384 '''Fast approximation of C{atan2(B{y}, B{x})}.
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
406def favg(a, b, f=_0_5):
407 '''Return the precise average of two values.
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}).
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)
423def fdot(a, *b):
424 '''Return the precision dot product M{sum(a[i] * b[i] for ni=0..len(a))}.
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.
430 @return: Dot product (C{float}).
432 @raise LenError: Unequal C{len(B{a})} and C{len(B{b})}.
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))
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)}.
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}).
450 @return: Dot product (C{float}).
452 @raise LenError: Unequal C{len(B{xs})}, C{len(B{ys})} and/or C{len(B{zs})}.
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
466 n = len(xs)
467 if not n == len(ys) == len(zs):
468 raise LenError(fdot3, xs=n, ys=len(ys), zs=len(zs))
470 return fsum(_mul3(xs, ys, zs, start, n < 4))
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}.
478 @return: Horner sum (C{float}).
480 @see: Class L{Fhorner} for further details.
481 '''
482 H = Fhorner(x, *cs, **incx)
483 return float(H)
486def fidw(xs, ds, beta=2):
487 '''Interpolate using U{Inverse Distance Weighting
488 <https://WikiPedia.org/wiki/Inverse_distance_weighting>} (IDW).
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).
495 @return: Interpolated value C{x} (C{float}).
497 @raise LenError: Unequal or zero C{len(B{ds})} and C{len(B{xs})}.
499 @raise TypeError: An invalid B{C{ds}} or B{C{xs}}.
501 @raise ValueError: Invalid B{C{beta}}, negative B{C{ds}} or
502 weighted B{C{ds}} below L{EPS}.
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
548def fma(x, y, z, **nonfinites):
549 '''Fused-multiply-add, using C{math.fma(x, y, z)} in Python 3.13+
550 or an equivalent implementation.
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 nonfinites: Use C{B{nonfinites}=True} or C{=False}, to
556 override default L{nonfiniterrors} (C{bool}),
557 see L{Fsum<Fsum.__init__>},
559 @return: C{(x * y) + z} (L{Fsum} or C{float}).
560 '''
561 return _Psum_(x).fma(y, z, **nonfinites).as_iscalar
564def fmean(xs):
565 '''Compute the accurate mean M{sum(xs) / len(xs)}.
567 @arg xs: Values (each C{scalar}, or L{Fsum} or L{Fsum2Tuple}).
569 @return: Mean value (C{float}).
571 @raise LenError: No B{C{xs}} values.
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])
581def fmean_(*xs):
582 '''Compute the accurate mean M{sum(xs) / len(xs)}.
584 @see: Function L{fmean} for further details.
585 '''
586 return fmean(xs)
589def f2mul_(x, *ys, **raiser):
590 '''Cascaded, accurate multiplication C{B{x} * B{y} * B{y} ...} for all B{C{ys}}.
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 nonfinites: Use C{B{nonfinites}=True} or C{=False}, to override default
596 L{nonfiniterrors} (C{bool}), see L{Fsum<Fsum.__init__>}.
598 @return: The cascaded I{TwoProduct} (L{Fsum}, C{float} or C{int}).
600 @see: U{Equations 2.3<https://www.TUHH.De/ti3/paper/rump/OzOgRuOi06.pdf>}
601 '''
602 return _Psum_(x).f2mul_(*ys, **nonfinites).as_iscalar
605def fpolynomial(x, *cs, **over):
606 '''Evaluate the polynomial M{sum(cs[i] * x**i for i=0..len(cs))
607 [/ over]}.
609 @kwarg over: Optional final, I{non-zero} divisor (C{scalar}).
611 @return: Polynomial value (C{float}).
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)
620def fpowers(x, n, alts=0):
621 '''Return a series of powers M{[x**i for i=1..n]}.
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}).
628 @return: Tuple of powers of B{C{x}} (each C{type(B{x})}).
630 @raise TypeError: Invalid B{C{x}} or B{C{n}} not C{int}.
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)
639 p = x if isscalar(x) or _isFsum_2Tuple(x) else _2float(x=x)
640 ps = tuple(_powers(p, n))
642 if alts > 0: # x**2, x**4, ...
643 # ps[alts-1::2] chokes PyChecker
644 ps = ps[slice(alts-1, None, 2)]
646 return ps
649try:
650 from math import prod as fprod # Python 3.8
651except ImportError:
653 def fprod(xs, start=1):
654 '''Iterable product, like C{math.prod} or C{numpy.prod}.
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}).
661 @return: The product (C{float} or an L{Fsum}).
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)
669def frandoms(n, seeded=None):
670 '''Generate C{n} (long) lists of random C{floats}.
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}.
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
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)
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
702def frange(start, number, step=1):
703 '''Generate a range of C{float}s.
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}).
709 @return: A generator (C{float}s).
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)
720try:
721 from functools import reduce as freduce
722except ImportError:
723 try:
724 freduce = reduce # PYCHOK expected
725 except NameError: # Python 3+
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
741def fremainder(x, y):
742 '''Remainder in range C{[-B{y / 2}, B{y / 2}]}.
744 @arg x: Numerator (C{scalar}).
745 @arg y: Modulus, denominator (C{scalar}).
747 @return: Remainder (C{scalar}, preserving signed
748 0.0) or C{NAN} for any non-finite B{C{x}}.
750 @raise ValueError: Infinite or near-zero B{C{y}}.
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
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
786if _sys_version_info2 < (3, 8): # PYCHOK no cover
787 from math import hypot # OK in Python 3.7-
789 def hypot_(*xs):
790 '''Compute the norm M{sqrt(sum(x**2 for x in xs))}.
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.
797 @arg xs: X arguments (C{scalar}s), all positional.
799 @return: Norm (C{float}).
801 @raise OverflowError: Partial C{2sum} overflow.
803 @raise ValueError: Invalid or no B{C{xs}} values.
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))
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>}
819 def hypot(x, y):
820 '''Compute the norm M{sqrt(x**2 + y**2)}.
822 @arg x: X argument (C{scalar}).
823 @arg y: Y argument (C{scalar}).
825 @return: C{sqrt(B{x}**2 + B{y}**2)} (C{float}).
826 '''
827 return float(Fhypot(x, y, raiser=False))
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
835def hypot1(x):
836 '''Compute the norm M{sqrt(1 + x**2)}.
838 @arg x: Argument (C{scalar} or L{Fsum} or L{Fsum2Tuple}).
840 @return: Norm (C{float}).
841 '''
842 if _isFsum_2Tuple(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
849def hypot2(x, y):
850 '''Compute the I{squared} norm M{x**2 + y**2}.
852 @arg x: X (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
853 @arg y: Y (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
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
870def hypot2_(*xs):
871 '''Compute the I{squared} norm C{fsum(x**2 for x in B{xs})}.
873 @arg xs: Components (each C{scalar}, an L{Fsum} or
874 L{Fsum2Tuple}), all positional.
876 @return: Squared norm (C{float}).
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
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)
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))
904def norm2(x, y):
905 '''Normalize a 2-dimensional vector.
907 @arg x: X component (C{scalar}).
908 @arg y: Y component (C{scalar}).
910 @return: 2-Tuple C{(x, y)}, normalized.
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
928def norm_(*xs):
929 '''Normalize the components of an n-dimensional vector.
931 @arg xs: Components (each C{scalar}, an L{Fsum} or
932 L{Fsum2Tuple}), all positional.
934 @return: Yield each component, normalized.
936 @raise ValueError: Invalid or insufficent B{C{xs}}
937 or zero norm.
938 '''
939 try:
940 i = h = None
941 x = xs
942 h = hypot_(*xs)
943 _h = (_1_0 / h) if h else _0_0
944 for i, x in enumerate(xs):
945 yield x * _h
946 except Exception as X:
947 raise _xsError(X, xs, i, x, h=h)
950def _powers(x, n):
951 '''(INTERNAL) Yield C{x**i for i=1..n}.
952 '''
953 p = 1 # type(p) == type(x)
954 for _ in range(n):
955 p *= x
956 yield p
959def _root(x, p, where):
960 '''(INTERNAL) Raise C{x} to power C{0 < p < 1}.
961 '''
962 try:
963 if x > 0:
964 return Fsum(x).fpow(p).as_iscalar
965 elif x < 0:
966 raise ValueError(_negative_)
967 except Exception as X:
968 raise _xError(X, unstr(where, x))
969 return _0_0
972def sqrt0(x, Error=None):
973 '''Return the square root C{sqrt(B{x})} iff C{B{x} > }L{EPS02},
974 preserving C{type(B{x})}.
976 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
977 @kwarg Error: Error to raise for negative B{C{x}}.
979 @return: Square root (C{float} or L{Fsum}) or C{0.0}.
981 @raise TypeeError: Invalid B{C{x}}.
983 @note: Any C{B{x} < }L{EPS02} I{including} C{B{x} < 0}
984 returns C{0.0}.
985 '''
986 if Error and x < 0:
987 raise Error(unstr(sqrt0, x))
988 return _root(x, _0_5, sqrt0) if x > EPS02 else (
989 _0_0 if x < EPS02 else EPS0)
992def sqrt3(x):
993 '''Return the square root, I{cubed} M{sqrt(x)**3} or M{sqrt(x**3)},
994 preserving C{type(B{x})}.
996 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
998 @return: Square root I{cubed} (C{float} or L{Fsum}).
1000 @raise TypeeError: Invalid B{C{x}}.
1002 @raise ValueError: Negative B{C{x}}.
1004 @see: Functions L{cbrt} and L{cbrt2}.
1005 '''
1006 return _root(x, _1_5, sqrt3)
1009def sqrt_a(h, b):
1010 '''Compute C{I{a}} side of a right-angled triangle from
1011 C{sqrt(B{h}**2 - B{b}**2)}.
1013 @arg h: Hypotenuse or outer annulus radius (C{scalar}).
1014 @arg b: Triangle side or inner annulus radius (C{scalar}).
1016 @return: C{copysign(I{a}, B{h})} or C{unsigned 0.0} (C{float}).
1018 @raise TypeError: Non-scalar B{C{h}} or B{C{b}}.
1020 @raise ValueError: If C{abs(B{h}) < abs(B{b})}.
1022 @see: Inner tangent chord B{I{d}} of an U{annulus
1023 <https://WikiPedia.org/wiki/Annulus_(mathematics)>}
1024 and function U{annulus_area<https://People.SC.FSU.edu/
1025 ~jburkardt/py_src/geometry/geometry.py>}.
1026 '''
1027 try:
1028 if not (_isHeight(h) and _isRadius(b)):
1029 raise TypeError(_not_scalar_)
1030 c = fabs(h)
1031 if c > EPS0:
1032 s = _1_0 - (b / c)**2
1033 if s < 0:
1034 raise ValueError(_h_lt_b_)
1035 a = (sqrt(s) * c) if 0 < s < 1 else (c if s else _0_0)
1036 else: # PYCHOK no cover
1037 b = fabs(b)
1038 d = c - b
1039 if d < 0:
1040 raise ValueError(_h_lt_b_)
1041 d *= c + b
1042 a = sqrt(d) if d else _0_0
1043 except Exception as x:
1044 raise _xError(x, h=h, b=b)
1045 return copysign0(a, h)
1048def zcrt(x):
1049 '''Return the 6-th, I{zenzi-cubic} root, M{x**(1 / 6)},
1050 preserving C{type(B{x})}.
1052 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1054 @return: I{Zenzi-cubic} root (C{float} or L{Fsum}).
1056 @see: Functions L{bqrt} and L{zqrt}.
1058 @raise TypeeError: Invalid B{C{x}}.
1060 @raise ValueError: Negative B{C{x}}.
1061 '''
1062 return _root(x, _1_6th, zcrt)
1065def zqrt(x):
1066 '''Return the 8-th, I{zenzi-quartic} or I{squared-quartic} root,
1067 M{x**(1 / 8)}, preserving C{type(B{x})}.
1069 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1071 @return: I{Zenzi-quartic} root (C{float} or L{Fsum}).
1073 @see: Functions L{bqrt} and L{zcrt}.
1075 @raise TypeeError: Invalid B{C{x}}.
1077 @raise ValueError: Negative B{C{x}}.
1078 '''
1079 return _root(x, _0_125, zqrt)
1081# **) MIT License
1082#
1083# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved.
1084#
1085# Permission is hereby granted, free of charge, to any person obtaining a
1086# copy of this software and associated documentation files (the "Software"),
1087# to deal in the Software without restriction, including without limitation
1088# the rights to use, copy, modify, merge, publish, distribute, sublicense,
1089# and/or sell copies of the Software, and to permit persons to whom the
1090# Software is furnished to do so, subject to the following conditions:
1091#
1092# The above copyright notice and this permission notice shall be included
1093# in all copies or substantial portions of the Software.
1094#
1095# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1096# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1097# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1098# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1099# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1100# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1101# OTHER DEALINGS IN THE SOFTWARE.