Coverage for pygeodesy/fmath.py: 90%
335 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-10-22 18:16 -0400
« prev ^ index » next coverage.py v7.6.1, created at 2024-10-22 18:16 -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 _1_5, _copysign_0_0, isfinite, remainder
14from pygeodesy.errors import _IsnotError, LenError, _TypeError, _ValueError, \
15 _xError, _xkwds_pop2, _xsError
16from pygeodesy.fsums import _2float, Fsum, fsum, _isFsum_2Tuple, _1primed, \
17 Fmt, unstr
18from pygeodesy.interns import MISSING, _negative_, _not_scalar_
19from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS
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.10.11'
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, **start_name_f2product_nonfinites_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
43 positional.
44 @kwarg start_name_f2product_nonfinites_RESIDUAL: Optional bias C{B{start}=0}
45 (C{scalar}, an L{Fsum} or L{Fsum2Tuple}), C{B{name}=NN} (C{str})
46 and other settings, see class L{Fsum<Fsum.__init__>}.
48 @raise LenError: Unequal C{len(B{a})} and C{len(B{b})}.
50 @raise OverflowError: Partial C{2sum} overflow.
52 @raise TypeError: Invalid B{C{x}}.
54 @raise ValueError: Non-finite B{C{x}}.
56 @see: Function L{fdot} and method L{Fsum.fadd}.
57 '''
58 s, kwds = _xkwds_pop2(start_name_f2product_nonfinites_RESIDUAL, start=_0_0)
59 Fsum.__init__(self, **kwds)
60 self(s)
62 n = len(b)
63 if len(a) != n: # PYCHOK no cover
64 raise LenError(Fdot, a=len(a), b=n)
65 self._faddot(n, a, b, **kwds)
67 def _faddot(self, n, xs, ys, **kwds):
68 if n > 0:
69 _f = Fsum(**kwds)
70 r = (_f(x).fmul(y) for x, y in zip(xs, ys)) # PYCHOK attr?
71 self.fadd(_1primed(r) if n < 4 else r) # PYCHOK attr?
72 return self
75class Fhorner(Fsum):
76 '''Precision polynomial evaluation using the Horner form.
77 '''
78 def __init__(self, x, *cs, **incx_name_f2product_nonfinites_RESIDUAL):
79 '''New L{Fhorner} form evaluation of polynomial M{sum(cs[i] * x**i for
80 i=0..n)} with in- or decreasing exponent M{sum(... i=n..0)}, where C{n
81 = len(cs) - 1}.
83 @arg x: Polynomial argument (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
84 @arg cs: Polynomial coeffients (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}),
85 all positional.
86 @kwarg incx_name_f2product_nonfinites_RESIDUAL: Optional C{B{name}=NN} (C{str}),
87 C{B{incx}=True} for in-/decreasing exponents (C{bool}) and other
88 settings, see class L{Fsum<Fsum.__init__>}.
90 @raise OverflowError: Partial C{2sum} overflow.
92 @raise TypeError: Invalid B{C{x}}.
94 @raise ValueError: Non-finite B{C{x}}.
96 @see: Function L{fhorner} and methods L{Fsum.fadd} and L{Fsum.fmul}.
97 '''
98 incx, kwds = _xkwds_pop2(incx_name_f2product_nonfinites_RESIDUAL, incx=True)
99 Fsum.__init__(self, **kwds)
100 self._fhorner(x, cs, Fhorner, incx=incx)
103class Fhypot(Fsum):
104 '''Precision summation and hypotenuse, default C{root=2}.
105 '''
106 def __init__(self, *xs, **root_name_f2product_nonfinites_RESIDUAL_raiser):
107 '''New L{Fhypot} hypotenuse of (the I{root} of) several components (raised
108 to the power I{root}).
110 @arg xs: Components (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all
111 positional.
112 @kwarg root_name_f2product_nonfinites_RESIDUAL_raiser: Optional, exponent
113 and C{B{root}=2} order (C{scalar}), C{B{name}=NN} (C{str}),
114 C{B{raiser}=True} (C{bool}) for raising L{ResidualError}s and
115 other settings, see class L{Fsum<Fsum.__init__>} and method
116 L{root<Fsum.root>}.
117 '''
118 r = None # _xkwds_pop2 error
119 try:
120 r, kwds = _xkwds_pop2(root_name_f2product_nonfinites_RESIDUAL_raiser, root=2)
121 r, kwds = _xkwds_pop2(kwds, power=r) # for backward compatibility
122 t, kwds = _xkwds_pop2(kwds, raiser=True)
123 Fsum.__init__(self, **kwds)
124 self(_0_0)
125 if xs:
126 self._facc_power(r, xs, Fhypot, raiser=t)
127 self._fset(self.root(r, raiser=t))
128 except Exception as X:
129 raise self._ErrorXs(X, xs, root=r)
132class Fpolynomial(Fdot):
133 '''Precision polynomial evaluation.
134 '''
135 def __init__(self, x, *cs, **name_f2product_nonfinites_RESIDUAL):
136 '''New L{Fpolynomial} evaluation of the polynomial M{sum(cs[i] * x**i for
137 i=0..len(cs)-1)}.
139 @arg x: Polynomial argument (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
140 @arg cs: Polynomial coeffients (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}),
141 all positional.
142 @kwarg name_f2product_nonfinites_RESIDUAL: Optional C{B{name}=NN} (C{str})
143 and other settings, see class L{Fsum<Fsum.__init__>}.
145 @raise OverflowError: Partial C{2sum} overflow.
147 @raise TypeError: Invalid B{C{x}}.
149 @raise ValueError: Non-finite B{C{x}}.
151 @see: Class L{Fhorner}, function L{fpolynomial} and method L{Fsum.fadd}.
152 '''
153 Fsum.__init__(self, *cs[:1], **name_f2product_nonfinites_RESIDUAL)
154 n = len(cs) - 1
155 if n > 0:
156 self._faddot(n, cs[1:], _powers(x, n), **name_f2product_nonfinites_RESIDUAL)
157 elif n < 0:
158 self(_0_0)
161class Fpowers(Fsum):
162 '''Precision summation of powers, optimized for C{power=2, 3 and 4}.
163 '''
164 def __init__(self, power, *xs, **name_f2product_nonfinites_RESIDUAL_raiser):
165 '''New L{Fpowers} sum of (the I{power} of) several bases.
167 @arg power: The exponent (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
168 @arg xs: One or more bases (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all
169 positional.
170 @kwarg name_f2product_nonfinites_RESIDUAL_raiser: Optional C{B{name}=NN}
171 (C{str}), C{B{raiser}=True} (C{bool}) for raising L{ResidualError}s
172 and other settings, see class L{Fsum<Fsum.__init__>} and method
173 L{fpow<Fsum.fpow>}.
174 '''
175 try:
176 t, kwds = _xkwds_pop2(name_f2product_nonfinites_RESIDUAL_raiser, raiser=True)
177 Fsum.__init__(self, **kwds)
178 self(_0_0)
179 if xs:
180 self._facc_power(power, xs, Fpowers, raiser=t) # x**0 == 1
181 except Exception as X:
182 raise self._ErrorXs(X, xs, power=power)
185class Froot(Fsum):
186 '''The root of a precision summation.
187 '''
188 def __init__(self, root, *xs, **name_f2product_nonfinites_RESIDUAL_raiser):
189 '''New L{Froot} root of a precision sum.
191 @arg root: The order (C{scalar}, an L{Fsum} or L{Fsum2Tuple}), non-zero.
192 @arg xs: Items to summate (each a C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all
193 positional.
194 @kwarg name_f2product_nonfinites_RESIDUAL_raiser: Optional C{B{name}=NN}
195 (C{str}), C{B{raiser}=True} (C{bool}) for raising L{ResidualError}s
196 and other settings, see class L{Fsum<Fsum.__init__>} and method
197 L{fpow<Fsum.fpow>}.
198 '''
199 try:
200 raiser, kwds = _xkwds_pop2(name_f2product_nonfinites_RESIDUAL_raiser, raiser=True)
201 Fsum.__init__(self, **kwds)
202 self(_0_0)
203 if xs:
204 self.fadd(xs)
205 self(self.root(root, raiser=raiser))
206 except Exception as X:
207 raise self._ErrorXs(X, xs, root=root)
210class Fcbrt(Froot):
211 '''Cubic root of a precision summation.
212 '''
213 def __init__(self, *xs, **name_f2product_nonfinites_RESIDUAL_raiser):
214 '''New L{Fcbrt} cubic root of a precision sum.
216 @see: Class L{Froot<Froot.__init__>} for further details.
217 '''
218 Froot.__init__(self, 3, *xs, **name_f2product_nonfinites_RESIDUAL_raiser)
221class Fsqrt(Froot):
222 '''Square root of a precision summation.
223 '''
224 def __init__(self, *xs, **name_f2product_nonfinites_RESIDUAL_raiser):
225 '''New L{Fsqrt} square root of a precision sum.
227 @see: Class L{Froot<Froot.__init__>} for further details.
228 '''
229 Froot.__init__(self, 2, *xs, **name_f2product_nonfinites_RESIDUAL_raiser)
232def bqrt(x):
233 '''Return the 4-th, I{bi-quadratic} or I{quartic} root, M{x**(1 / 4)},
234 preserving C{type(B{x})}.
236 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
238 @return: I{Quartic} root (C{float} or an L{Fsum}).
240 @raise TypeeError: Invalid B{C{x}}.
242 @raise ValueError: Negative B{C{x}}.
244 @see: Functions L{zcrt} and L{zqrt}.
245 '''
246 return _root(x, _0_25, bqrt)
249try:
250 from math import cbrt as _cbrt # Python 3.11+
252except ImportError: # Python 3.10-
254 def _cbrt(x):
255 '''(INTERNAL) Compute the I{signed}, cube root M{x**(1/3)}.
256 '''
257 # <https://archive.lib.MSU.edu/crcmath/math/math/r/r021.htm>
258 # simpler and more accurate than Ken Turkowski's CubeRoot, see
259 # <https://People.FreeBSD.org/~lstewart/references/apple_tr_kt32_cuberoot.pdf>
260 return _copysign(pow(fabs(x), _1_3rd), x) # to avoid complex
263def cbrt(x):
264 '''Compute the cube root M{x**(1/3)}, preserving C{type(B{x})}.
266 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
268 @return: Cubic root (C{float} or L{Fsum}).
270 @see: Functions L{cbrt2} and L{sqrt3}.
271 '''
272 if _isFsum_2Tuple(x):
273 r = abs(x).fpow(_1_3rd)
274 if x.signOf() < 0:
275 r = -r
276 else:
277 r = _cbrt(x)
278 return r # cbrt(-0.0) == -0.0
281def cbrt2(x): # PYCHOK attr
282 '''Compute the cube root I{squared} M{x**(2/3)}, preserving C{type(B{x})}.
284 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
286 @return: Cube root I{squared} (C{float} or L{Fsum}).
288 @see: Functions L{cbrt} and L{sqrt3}.
289 '''
290 return abs(x).fpow(_2_3rd) if _isFsum_2Tuple(x) else _cbrt(x**2)
293def euclid(x, y):
294 '''I{Appoximate} the norm M{sqrt(x**2 + y**2)} by M{max(abs(x),
295 abs(y)) + min(abs(x), abs(y)) * 0.4142...}.
297 @arg x: X component (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
298 @arg y: Y component (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
300 @return: Appoximate norm (C{float} or L{Fsum}).
302 @see: Function L{euclid_}.
303 '''
304 x, y = abs(x), abs(y) # NOT fabs!
305 if y > x:
306 x, y = y, x
307 return x + y * _0_4142 # * _0_5 before 20.10.02
310def euclid_(*xs):
311 '''I{Appoximate} the norm M{sqrt(sum(x**2 for x in xs))} by cascaded
312 L{euclid}.
314 @arg xs: X arguments (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}),
315 all positional.
317 @return: Appoximate norm (C{float} or L{Fsum}).
319 @see: Function L{euclid}.
320 '''
321 e = _0_0
322 for x in sorted(map(abs, xs)): # NOT fabs, reverse=True!
323 # e = euclid(x, e)
324 if e < x:
325 e, x = x, e
326 if x:
327 e += x * _0_4142
328 return e
331def facos1(x):
332 '''Fast approximation of L{pygeodesy.acos1}C{(B{x})}, scalar.
334 @see: U{ShaderFastLibs.h<https://GitHub.com/michaldrobot/
335 ShaderFastLibs/blob/master/ShaderFastMathLib.h>}.
336 '''
337 a = fabs(x)
338 if a < EPS0:
339 r = PI_2
340 elif a < EPS1:
341 r = _fast(-a, 1.5707288, 0.2121144, 0.0742610, 0.0187293)
342 r *= sqrt(_1_0 - a)
343 if x < 0:
344 r = PI - r
345 else:
346 r = PI if x < 0 else _0_0
347 return r
350def fasin1(x): # PYCHOK no cover
351 '''Fast approximation of L{pygeodesy.asin1}C{(B{x})}, scalar.
353 @see: L{facos1}.
354 '''
355 return PI_2 - facos1(x)
358def _fast(x, *cs):
359 '''(INTERNAL) Horner form for C{facos1} and C{fatan1}.
360 '''
361 h = 0
362 for c in reversed(cs):
363 h = _fma(x, h, c) if h else c
364 return h
367def fatan(x):
368 '''Fast approximation of C{atan(B{x})}, scalar.
369 '''
370 a = fabs(x)
371 if a < _1_0:
372 r = fatan1(a) if a else _0_0
373 elif a > _1_0:
374 r = PI_2 - fatan1(_1_0 / a) # == fatan2(a, _1_0)
375 else:
376 r = PI_4
377 if x < 0: # copysign0(r, x)
378 r = -r
379 return r
382def fatan1(x):
383 '''Fast approximation of C{atan(B{x})} for C{0 <= B{x} < 1}, I{unchecked}.
385 @see: U{ShaderFastLibs.h<https://GitHub.com/michaldrobot/ShaderFastLibs/
386 blob/master/ShaderFastMathLib.h>} and U{Efficient approximations
387 for the arctangent function<http://www-Labs.IRO.UMontreal.CA/
388 ~mignotte/IFT2425/Documents/EfficientApproximationArctgFunction.pdf>},
389 IEEE Signal Processing Magazine, 111, May 2006.
390 '''
391 # Eq (9): PI_4 * x - x * (abs(x) - 1) * (0.2447 + 0.0663 * abs(x)), for -1 < x < 1
392 # == PI_4 * x - (x**2 - x) * (0.2447 + 0.0663 * x), for 0 < x < 1
393 # == x * (1.0300981633974482 + x * (-0.1784 - x * 0.0663))
394 return _fast(x, _0_0, 1.0300981634, -0.1784, -0.0663)
397def fatan2(y, x):
398 '''Fast approximation of C{atan2(B{y}, B{x})}, scalar.
400 @see: U{fastApproximateAtan(x, y)<https://GitHub.com/CesiumGS/cesium/blob/
401 master/Source/Shaders/Builtin/Functions/fastApproximateAtan.glsl>}
402 and L{fatan1}.
403 '''
404 a, b = fabs(x), fabs(y)
405 if b > a:
406 r = (PI_2 - fatan1(a / b)) if a else PI_2
407 elif a > b:
408 r = fatan1(b / a) if b else _0_0
409 elif a: # a == b != 0
410 r = PI_4
411 else: # a == b == 0
412 return _0_0
413 if x < 0:
414 r = PI - r
415 if y < 0: # copysign0(r, y)
416 r = -r
417 return r
420def favg(a, b, f=_0_5, nonfinites=True):
421 '''Return the precise average of two values.
423 @arg a: One (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
424 @arg b: Other (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
425 @kwarg f: Optional fraction (C{float}).
426 @kwarg nonfinites: Optional setting, see function L{fma}.
428 @return: M{a + f * (b - a)} (C{float}).
429 '''
430 F = fma(f, (b - a), a, nonfinites=nonfinites)
431 return float(F)
434def fdot(a, *b, **start):
435 '''Return the precision dot product M{sum(a[i] * b[i] for ni=0..len(a))}.
437 @arg a: Iterable of values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
438 @arg b: Other values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all
439 positional.
440 @kwarg start: Optional bias C{B{start}=0} (C{scalar}, an L{Fsum} or
441 L{Fsum2Tuple}).
443 @return: Dot product (C{float}).
445 @raise LenError: Unequal C{len(B{a})} and C{len(B{b})}.
447 @see: Class L{Fdot}, U{Algorithm 5.10 B{DotK}
448 <https://www.TUHH.De/ti3/paper/rump/OgRuOi05.pdf>} and function
449 C{math.sumprod} in Python 3.12 and later.
450 '''
451 D = Fdot(a, nonfinites=True, *b, **start)
452 return float(D)
455def fdot3(xs, ys, zs, start=0):
456 '''Return the precision dot product M{start + sum(a[i] * b[i] * c[i]
457 for i=0..len(a)-1)}.
459 @arg xs: Iterable (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
460 @arg ys: Iterable (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
461 @arg zs: Iterable (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
462 @kwarg start: Optional bias (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
464 @return: Dot product (C{float}).
466 @raise LenError: Unequal C{len(B{xs})}, C{len(B{ys})} and/or C{len(B{zs})}.
467 '''
468 n = len(xs)
469 if not n == len(ys) == len(zs):
470 raise LenError(fdot3, xs=n, ys=len(ys), zs=len(zs))
472 D = Fdot((), nonfinites=True, start=start)
473 _f = Fsum(nonfinites=True) # f2product=True
474 r = (_f(x).f2mul_(y, z) for x, y, z in zip(xs, ys, zs))
475 D = D.fadd(_1primed(r) if n < 4 else r)
476 return float(D)
479def fhorner(x, *cs, **incx):
480 '''Horner form evaluation of polynomial M{sum(cs[i] * x**i for
481 i=0..n)} with in- or decreasing exponent M{sum(... i=n..0)},
482 where C{n = len(cs) - 1}.
484 @return: Horner sum (C{float}).
486 @see: Class L{Fhorner<Fhorner.__init__>} for further details.
487 '''
488 H = Fhorner(x, *cs, **incx)
489 return float(H)
492def fidw(xs, ds, beta=2):
493 '''Interpolate using U{Inverse Distance Weighting
494 <https://WikiPedia.org/wiki/Inverse_distance_weighting>} (IDW).
496 @arg xs: Known values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
497 @arg ds: Non-negative distances (each C{scalar}, an L{Fsum} or
498 L{Fsum2Tuple}).
499 @kwarg beta: Inverse distance power (C{int}, 0, 1, 2, or 3).
501 @return: Interpolated value C{x} (C{float}).
503 @raise LenError: Unequal or zero C{len(B{ds})} and C{len(B{xs})}.
505 @raise TypeError: An invalid B{C{ds}} or B{C{xs}}.
507 @raise ValueError: Invalid B{C{beta}}, negative B{C{ds}} or
508 weighted B{C{ds}} below L{EPS}.
510 @note: Using C{B{beta}=0} returns the mean of B{C{xs}}.
511 '''
512 n, xs = len2(xs)
513 if n > 1:
514 b = -Int_(beta=beta, low=0, high=3)
515 if b < 0:
516 try: # weighted
517 _d, W, X = (Fsum() for _ in range(3))
518 for i, d in enumerate(_xiterable(ds)):
519 x = xs[i]
520 D = _d(d)
521 if D < EPS0:
522 if D < 0:
523 raise ValueError(_negative_)
524 x = float(x)
525 i = n
526 break
527 if D.fpow(b):
528 W += D
529 X += D.fmul(x)
530 else:
531 x = X.fover(W, raiser=False)
532 i += 1 # len(xs) >= len(ds)
533 except IndexError:
534 i += 1 # len(xs) < i < len(ds)
535 except Exception as X:
536 _I = Fmt.INDEX
537 raise _xError(X, _I(xs=i), x,
538 _I(ds=i), d)
539 else: # b == 0
540 x = fsum(xs) / n # fmean(xs)
541 i = n
542 elif n:
543 x = float(xs[0])
544 i = n
545 else:
546 x = _0_0
547 i, _ = len2(ds)
548 if i != n:
549 raise LenError(fidw, xs=n, ds=i)
550 return x
553try:
554 from math import fma as _fma
555except ImportError: # PYCHOK DSPACE!
557 def _fma(x, y, z): # no need for accuracy
558 return x * y + z
561def fma(x, y, z, **nonfinites): # **raiser
562 '''Fused-multiply-add, using C{math.fma(x, y, z)} in Python 3.13+
563 or an equivalent implementation.
565 @arg x: Multiplicand (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
566 @arg y: Multiplier (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
567 @arg z: Addend (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
568 @kwarg nonfinites: Use C{B{nonfinites}=True} or C{=False},
569 to override default L{nonfiniterrors}
570 (C{bool}), see method L{Fsum.fma}.
572 @return: C{(x * y) + z} (C{float} or L{Fsum}).
573 '''
574 F, raiser = _Fm2(x, **nonfinites)
575 return F.fma(y, z, **raiser).as_iscalar
578def _Fm2(x, nonfinites=None, **raiser):
579 '''(INTERNAL) Handle C{fma} and C{f2mul} DEPRECATED C{raiser=False}.
580 '''
581 return Fsum(x, nonfinites=nonfinites), raiser
584def fmean(xs):
585 '''Compute the accurate mean M{sum(xs) / len(xs)}.
587 @arg xs: Values (each C{scalar}, or L{Fsum} or L{Fsum2Tuple}).
589 @return: Mean value (C{float}).
591 @raise LenError: No B{C{xs}} values.
593 @raise OverflowError: Partial C{2sum} overflow.
594 '''
595 n, xs = len2(xs)
596 if n < 1:
597 raise LenError(fmean, xs=xs)
598 M = Fsum(*xs, nonfinites=True)
599 return M.fover(n) if n > 1 else float(M)
602def fmean_(*xs, **nonfinites):
603 '''Compute the accurate mean M{sum(xs) / len(xs)}.
605 @see: Function L{fmean} for further details.
606 '''
607 return fmean(xs, **nonfinites)
610def f2mul_(x, *ys, **nonfinites): # **raiser
611 '''Cascaded, accurate multiplication C{B{x} * B{y} * B{y} ...} for all B{C{ys}}.
613 @arg x: Multiplicand (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
614 @arg ys: Multipliers (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all
615 positional.
616 @kwarg nonfinites: Use C{B{nonfinites}=True} or C{=False}, to override default
617 L{nonfiniterrors} (C{bool}), see method L{Fsum.f2mul_}.
619 @return: The cascaded I{TwoProduct} (C{float}, C{int} or L{Fsum}).
621 @see: U{Equations 2.3<https://www.TUHH.De/ti3/paper/rump/OzOgRuOi06.pdf>}
622 '''
623 F, raiser = _Fm2(x, **nonfinites)
624 return F.f2mul_(*ys, **raiser).as_iscalar
627def fpolynomial(x, *cs, **over_f2product_nonfinites):
628 '''Evaluate the polynomial M{sum(cs[i] * x**i for i=0..len(cs)) [/ over]}.
630 @kwarg over_f2product_nonfinites: Optional final divisor C{B{over}=None}
631 (I{non-zero} C{scalar}) and other settings, see class
632 L{Fpolynomial<Fpolynomial.__init__>}.
634 @return: Polynomial value (C{float} or L{Fpolynomial}).
635 '''
636 d, kwds = _xkwds_pop2(over_f2product_nonfinites, over=0)
637 P = Fpolynomial(x, *cs, **kwds)
638 return P.fover(d) if d else float(P)
641def fpowers(x, n, alts=0):
642 '''Return a series of powers M{[x**i for i=1..n]}, note I{1..!}
644 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
645 @arg n: Highest exponent (C{int}).
646 @kwarg alts: Only alternating powers, starting with this
647 exponent (C{int}).
649 @return: Tuple of powers of B{C{x}} (each C{type(B{x})}).
651 @raise TypeError: Invalid B{C{x}} or B{C{n}} not C{int}.
653 @raise ValueError: Non-finite B{C{x}} or invalid B{C{n}}.
654 '''
655 if not isint(n):
656 raise _IsnotError(int.__name__, n=n)
657 elif n < 1:
658 raise _ValueError(n=n)
660 p = x if isscalar(x) or _isFsum_2Tuple(x) else _2float(x=x)
661 ps = tuple(_powers(p, n))
663 if alts > 0: # x**2, x**4, ...
664 # ps[alts-1::2] chokes PyChecker
665 ps = ps[slice(alts-1, None, 2)]
667 return ps
670try:
671 from math import prod as fprod # Python 3.8
672except ImportError:
674 def fprod(xs, start=1):
675 '''Iterable product, like C{math.prod} or C{numpy.prod}.
677 @arg xs: Iterable of values to be multiplied (each
678 C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
679 @kwarg start: Initial value, also the value returned
680 for an empty B{C{xs}} (C{scalar}).
682 @return: The product (C{float} or L{Fsum}).
684 @see: U{NumPy.prod<https://docs.SciPy.org/doc/
685 numpy/reference/generated/numpy.prod.html>}.
686 '''
687 return freduce(_operator.mul, xs, start)
690def frandoms(n, seeded=None):
691 '''Generate C{n} (long) lists of random C{floats}.
693 @arg n: Number of lists to generate (C{int}, non-negative).
694 @kwarg seeded: If C{scalar}, use C{random.seed(B{seeded})} or
695 if C{True}, seed using today's C{year-day}.
697 @see: U{Hettinger<https://GitHub.com/ActiveState/code/tree/master/recipes/
698 Python/393090_Binary_floating_point_summatiaccurate_full/recipe-393090.py>}.
699 '''
700 from random import gauss, random, seed, shuffle
702 if seeded is None:
703 pass
704 elif seeded and isbool(seeded):
705 from time import localtime
706 seed(localtime().tm_yday)
707 elif isscalar(seeded):
708 seed(seeded)
710 c = (7, 1e100, -7, -1e100, -9e-20, 8e-20) * 7
711 for _ in range(n):
712 s = 0
713 t = list(c)
714 _a = t.append
715 for _ in range(n * 8):
716 v = gauss(0, random())**7 - s
717 _a(v)
718 s += v
719 shuffle(t)
720 yield t
723def frange(start, number, step=1):
724 '''Generate a range of C{float}s.
726 @arg start: First value (C{float}).
727 @arg number: The number of C{float}s to generate (C{int}).
728 @kwarg step: Increment value (C{float}).
730 @return: A generator (C{float}s).
732 @see: U{NumPy.prod<https://docs.SciPy.org/doc/
733 numpy/reference/generated/numpy.arange.html>}.
734 '''
735 if not isint(number):
736 raise _IsnotError(int.__name__, number=number)
737 for i in range(number):
738 yield start + (step * i)
741try:
742 from functools import reduce as freduce
743except ImportError:
744 try:
745 freduce = reduce # PYCHOK expected
746 except NameError: # Python 3+
748 def freduce(f, xs, *start):
749 '''For missing C{functools.reduce}.
750 '''
751 if start:
752 r = v = start[0]
753 else:
754 r, v = 0, MISSING
755 for v in xs:
756 r = f(r, v)
757 if v is MISSING:
758 raise _TypeError(xs=(), start=MISSING)
759 return r
762def fremainder(x, y):
763 '''Remainder in range C{[-B{y / 2}, B{y / 2}]}.
765 @arg x: Numerator (C{scalar}).
766 @arg y: Modulus, denominator (C{scalar}).
768 @return: Remainder (C{scalar}, preserving signed
769 0.0) or C{NAN} for any non-finite B{C{x}}.
771 @raise ValueError: Infinite or near-zero B{C{y}}.
773 @see: I{Karney}'s U{Math.remainder<https://PyPI.org/
774 project/geographiclib/>} and Python 3.7+
775 U{math.remainder<https://docs.Python.org/3/
776 library/math.html#math.remainder>}.
777 '''
778 # with Python 2.7.16 and 3.7.3 on macOS 10.13.6 and
779 # with Python 3.10.2 on macOS 12.2.1 M1 arm64 native
780 # fmod( 0, 360) == 0.0
781 # fmod( 360, 360) == 0.0
782 # fmod(-0, 360) == 0.0
783 # fmod(-0.0, 360) == -0.0
784 # fmod(-360, 360) == -0.0
785 # however, using the % operator ...
786 # 0 % 360 == 0
787 # 360 % 360 == 0
788 # 360.0 % 360 == 0.0
789 # -0 % 360 == 0
790 # -360 % 360 == 0 == (-360) % 360
791 # -0.0 % 360 == 0.0 == (-0.0) % 360
792 # -360.0 % 360 == 0.0 == (-360.0) % 360
794 # On Windows 32-bit with python 2.7, math.fmod(-0.0, 360)
795 # == +0.0. This fixes this bug. See also Math::AngNormalize
796 # in the C++ library, Math.sincosd has a similar fix.
797 if isfinite(x):
798 try:
799 r = remainder(x, y) if x else x
800 except Exception as e:
801 raise _xError(e, unstr(fremainder, x, y))
802 else: # handle x INF and NINF as NAN
803 r = NAN
804 return r
807if _MODS.sys_version_info2 < (3, 8): # PYCHOK no cover
808 from math import hypot # OK in Python 3.7-
810 def hypot_(*xs):
811 '''Compute the norm M{sqrt(sum(x**2 for x in xs))}.
813 Similar to Python 3.8+ n-dimension U{math.hypot
814 <https://docs.Python.org/3.8/library/math.html#math.hypot>},
815 but exceptions, C{nan} and C{infinite} values are
816 handled differently.
818 @arg xs: X arguments (C{scalar}s), all positional.
820 @return: Norm (C{float}).
822 @raise OverflowError: Partial C{2sum} overflow.
824 @raise ValueError: Invalid or no B{C{xs}} values.
826 @note: The Python 3.8+ Euclidian distance U{math.dist
827 <https://docs.Python.org/3.8/library/math.html#math.dist>}
828 between 2 I{n}-dimensional points I{p1} and I{p2} can be
829 computed as M{hypot_(*((c1 - c2) for c1, c2 in zip(p1, p2)))},
830 provided I{p1} and I{p2} have the same, non-zero length I{n}.
831 '''
832 return float(_Hypot(*xs))
834elif _MODS.sys_version_info2 < (3, 10):
835 # In Python 3.8 and 3.9 C{math.hypot} is inaccurate, see
836 # U{agdhruv<https://GitHub.com/geopy/geopy/issues/466>},
837 # U{cffk<https://Bugs.Python.org/issue43088>} and module
838 # U{geomath.py<https://PyPI.org/project/geographiclib/1.52>}
840 def hypot(x, y):
841 '''Compute the norm M{sqrt(x**2 + y**2)}.
843 @arg x: X argument (C{scalar}).
844 @arg y: Y argument (C{scalar}).
846 @return: C{sqrt(B{x}**2 + B{y}**2)} (C{float}).
847 '''
848 return float(_Hypot(x, y))
850 from math import hypot as hypot_ # PYCHOK in Python 3.8 and 3.9
851else:
852 from math import hypot # PYCHOK in Python 3.10+
853 hypot_ = hypot
856def _Hypot(*xs):
857 '''(INTERNAL) Substitute for inaccurate C{math.hypot}.
858 '''
859 return Fhypot(*xs, nonfinites=True, raiser=False) # f2product=True
862def hypot1(x):
863 '''Compute the norm M{sqrt(1 + x**2)}.
865 @arg x: Argument (C{scalar} or L{Fsum} or L{Fsum2Tuple}).
867 @return: Norm (C{float} or L{Fhypot}).
868 '''
869 h = _1_0
870 if x:
871 if _isFsum_2Tuple(x):
872 h = _Hypot(h, x)
873 h = float(h)
874 else:
875 h = hypot(h, x)
876 return h
879def hypot2(x, y):
880 '''Compute the I{squared} norm M{x**2 + y**2}.
882 @arg x: X (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
883 @arg y: Y (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
885 @return: C{B{x}**2 + B{y}**2} (C{float}).
886 '''
887 x, y = map1(abs, x, y) # NOT fabs!
888 if y > x:
889 x, y = y, x
890 h2 = x**2
891 if h2 and y:
892 h2 *= (y / x)**2 + _1_0
893 return float(h2)
896def hypot2_(*xs):
897 '''Compute the I{squared} norm C{fsum(x**2 for x in B{xs})}.
899 @arg xs: Components (each C{scalar}, an L{Fsum} or
900 L{Fsum2Tuple}), all positional.
902 @return: Squared norm (C{float}).
904 @see: Class L{Fpowers} for further details.
905 '''
906 h2 = float(max(map(abs, xs))) if xs else _0_0
907 if h2: # and isfinite(h2)
908 _h = _1_0 / h2
909 xs = ((x * _h) for x in xs)
910 H2 = Fpowers(2, *xs, nonfinites=True) # f2product=True
911 h2 = H2.fover(_h**2)
912 return h2
915def norm2(x, y):
916 '''Normalize a 2-dimensional vector.
918 @arg x: X component (C{scalar}).
919 @arg y: Y component (C{scalar}).
921 @return: 2-Tuple C{(x, y)}, normalized.
923 @raise ValueError: Invalid B{C{x}} or B{C{y}}
924 or zero norm.
925 '''
926 try:
927 h = None
928 h = hypot(x, y)
929 if h:
930 x, y = (x / h), (y / h)
931 else:
932 x = _copysign_0_0(x) # pass?
933 y = _copysign_0_0(y)
934 except Exception as e:
935 raise _xError(e, x=x, y=y, h=h)
936 return x, y
939def norm_(*xs):
940 '''Normalize the components of an n-dimensional vector.
942 @arg xs: Components (each C{scalar}, an L{Fsum} or
943 L{Fsum2Tuple}), all positional.
945 @return: Yield each component, normalized.
947 @raise ValueError: Invalid or insufficent B{C{xs}}
948 or zero norm.
949 '''
950 try:
951 i = h = None
952 x = xs
953 h = hypot_(*xs)
954 _h = (_1_0 / h) if h else _0_0
955 for i, x in enumerate(xs):
956 yield x * _h
957 except Exception as X:
958 raise _xsError(X, xs, i, x, h=h)
961def _powers(x, n):
962 '''(INTERNAL) Yield C{x**i for i=1..n}.
963 '''
964 p = 1 # type(p) == type(x)
965 for _ in range(n):
966 p *= x
967 yield p
970def _root(x, p, where):
971 '''(INTERNAL) Raise C{x} to power C{0 < p < 1}.
972 '''
973 try:
974 if x > 0:
975 r = Fsum(f2product=True, nonfinites=True)(x)
976 return r.fpow(p).as_iscalar
977 elif x < 0:
978 raise ValueError(_negative_)
979 except Exception as X:
980 raise _xError(X, unstr(where, x))
981 return _0_0
984def sqrt0(x, Error=None):
985 '''Return the square root C{sqrt(B{x})} iff C{B{x} > }L{EPS02},
986 preserving C{type(B{x})}.
988 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
989 @kwarg Error: Error to raise for negative B{C{x}}.
991 @return: Square root (C{float} or L{Fsum}) or C{0.0}.
993 @raise TypeeError: Invalid B{C{x}}.
995 @note: Any C{B{x} < }L{EPS02} I{including} C{B{x} < 0}
996 returns C{0.0}.
997 '''
998 if Error and x < 0:
999 raise Error(unstr(sqrt0, x))
1000 return _root(x, _0_5, sqrt0) if x > EPS02 else (
1001 _0_0 if x < EPS02 else EPS0)
1004def sqrt3(x):
1005 '''Return the square root, I{cubed} M{sqrt(x)**3} or M{sqrt(x**3)},
1006 preserving C{type(B{x})}.
1008 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1010 @return: Square root I{cubed} (C{float} or L{Fsum}).
1012 @raise TypeeError: Invalid B{C{x}}.
1014 @raise ValueError: Negative B{C{x}}.
1016 @see: Functions L{cbrt} and L{cbrt2}.
1017 '''
1018 return _root(x, _1_5, sqrt3)
1021def sqrt_a(h, b):
1022 '''Compute C{I{a}} side of a right-angled triangle from
1023 C{sqrt(B{h}**2 - B{b}**2)}.
1025 @arg h: Hypotenuse or outer annulus radius (C{scalar}).
1026 @arg b: Triangle side or inner annulus radius (C{scalar}).
1028 @return: C{copysign(I{a}, B{h})} or C{unsigned 0.0} (C{float}).
1030 @raise TypeError: Non-scalar B{C{h}} or B{C{b}}.
1032 @raise ValueError: If C{abs(B{h}) < abs(B{b})}.
1034 @see: Inner tangent chord B{I{d}} of an U{annulus
1035 <https://WikiPedia.org/wiki/Annulus_(mathematics)>}
1036 and function U{annulus_area<https://People.SC.FSU.edu/
1037 ~jburkardt/py_src/geometry/geometry.py>}.
1038 '''
1039 try:
1040 if not (_isHeight(h) and _isRadius(b)):
1041 raise TypeError(_not_scalar_)
1042 c = fabs(h)
1043 if c > EPS0:
1044 s = _1_0 - (b / c)**2
1045 if s < 0:
1046 raise ValueError(_h_lt_b_)
1047 a = (sqrt(s) * c) if 0 < s < 1 else (c if s else _0_0)
1048 else: # PYCHOK no cover
1049 b = fabs(b)
1050 d = c - b
1051 if d < 0:
1052 raise ValueError(_h_lt_b_)
1053 d *= c + b
1054 a = sqrt(d) if d else _0_0
1055 except Exception as x:
1056 raise _xError(x, h=h, b=b)
1057 return copysign0(a, h)
1060def zcrt(x):
1061 '''Return the 6-th, I{zenzi-cubic} root, M{x**(1 / 6)},
1062 preserving C{type(B{x})}.
1064 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1066 @return: I{Zenzi-cubic} root (C{float} or L{Fsum}).
1068 @see: Functions L{bqrt} and L{zqrt}.
1070 @raise TypeeError: Invalid B{C{x}}.
1072 @raise ValueError: Negative B{C{x}}.
1073 '''
1074 return _root(x, _1_6th, zcrt)
1077def zqrt(x):
1078 '''Return the 8-th, I{zenzi-quartic} or I{squared-quartic} root,
1079 M{x**(1 / 8)}, preserving C{type(B{x})}.
1081 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1083 @return: I{Zenzi-quartic} root (C{float} or L{Fsum}).
1085 @see: Functions L{bqrt} and L{zcrt}.
1087 @raise TypeeError: Invalid B{C{x}}.
1089 @raise ValueError: Negative B{C{x}}.
1090 '''
1091 return _root(x, _0_125, zqrt)
1093# **) MIT License
1094#
1095# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved.
1096#
1097# Permission is hereby granted, free of charge, to any person obtaining a
1098# copy of this software and associated documentation files (the "Software"),
1099# to deal in the Software without restriction, including without limitation
1100# the rights to use, copy, modify, merge, publish, distribute, sublicense,
1101# and/or sell copies of the Software, and to permit persons to whom the
1102# Software is furnished to do so, subject to the following conditions:
1103#
1104# The above copyright notice and this permission notice shall be included
1105# in all copies or substantial portions of the Software.
1106#
1107# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1108# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1109# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1110# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1111# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1112# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1113# OTHER DEALINGS IN THE SOFTWARE.