Coverage for pygeodesy/fmath.py: 91%
327 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-04 12:01 -0400
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-04 12:01 -0400
2# -*- coding: utf-8 -*-
4u'''Utilities for precision floating point summation, multiplication,
5C{fused-multiply-add}, polynomials, roots, etc.
6'''
7# make sure int/int division yields float quotient, see .basics
8from __future__ import division as _; del _ # PYCHOK semicolon
10from pygeodesy.basics import _copysign, copysign0, isbool, isint, isscalar, \
11 len2, map1, _xiterable, typename
12from pygeodesy.constants import EPS0, EPS02, EPS1, NAN, PI, PI_2, PI_4, \
13 _0_0, _0_125, _1_6th, _0_25, _1_3rd, _0_5, _1_0, \
14 _1_5, _copysign_0_0, isfinite, remainder
15from pygeodesy.errors import _IsnotError, LenError, _TypeError, _ValueError, \
16 _xError, _xkwds, _xkwds_pop2, _xsError
17from pygeodesy.fsums import _2float, Fsum, fsum, _isFsum_2Tuple, Fmt, unstr
18# from pygeodesy.internals import typename # from .basics
19from pygeodesy.interns import MISSING, _negative_, _not_scalar_
20from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS
21# from pygeodesy.streprs import Fmt, unstr # from .fsums
22from pygeodesy.units import Int_, _isHeight, _isRadius, Float_ # PYCHOK for .heights
24from math import fabs, sqrt # pow
25import operator as _operator # in .datums, .trf, .utm
27__all__ = _ALL_LAZY.fmath
28__version__ = '25.04.30'
30# sqrt(2) - 1 <https://WikiPedia.org/wiki/Square_root_of_2>
31_0_4142 = 0.41421356237309504880 # ~ 3_730_904_090_310_553 / 9_007_199_254_740_992
32_2_3rd = _1_3rd * 2
33_h_lt_b_ = 'abs(h) < abs(b)'
36class Fdot(Fsum):
37 '''Precision dot product.
38 '''
39 def __init__(self, a, *b, **start_name_f2product_nonfinites_RESIDUAL):
40 '''New L{Fdot} precision dot product M{sum(a[i] * b[i] for i=0..len(a)-1)}.
42 @arg a: Iterable of values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
43 @arg b: Other values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all
44 positional.
45 @kwarg start_name_f2product_nonfinites_RESIDUAL: Optional bias C{B{start}=0}
46 (C{scalar}, an L{Fsum} or L{Fsum2Tuple}), C{B{name}=NN} (C{str})
47 and other settings, see class L{Fsum<Fsum.__init__>}.
49 @raise LenError: Unequal C{len(B{a})} and C{len(B{b})}.
51 @raise OverflowError: Partial C{2sum} overflow.
53 @raise TypeError: Invalid B{C{x}}.
55 @raise ValueError: Non-finite B{C{x}}.
57 @see: Function L{fdot} and method L{Fsum.fadd}.
58 '''
59 s, kwds = _xkwds_pop2(start_name_f2product_nonfinites_RESIDUAL, start=_0_0)
60 Fsum.__init__(self, **kwds)
61 self(s)
63 n = len(b)
64 if len(a) != n: # PYCHOK no cover
65 raise LenError(Fdot, a=len(a), b=n)
66 self._facc_dot(n, a, b, **kwds)
69class Fdot_(Fdot):
70 '''Precision dot product.
71 '''
72 def __init__(self, *xys, **start_name_f2product_nonfinites_RESIDUAL):
73 '''New L{Fdot_} precision dot product M{sum(xys[i] * xys[i+1] for i in
74 range(0, len(xys), B{2}))}.
76 @arg xys: Pairwise values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}),
77 all positional.
79 @see: Class L{Fdot<Fdot.__init__>} for further details.
80 '''
81 Fdot.__init__(self, xys[0::2], *xys[1::2], **start_name_f2product_nonfinites_RESIDUAL)
84class Fhorner(Fsum):
85 '''Precision polynomial evaluation using the Horner form.
86 '''
87 def __init__(self, x, *cs, **incx_name_f2product_nonfinites_RESIDUAL):
88 '''New L{Fhorner} form evaluation of polynomial M{sum(cs[i] * x**i for i=0..n)}
89 with in- or decreasing exponent M{sum(... i=n..0)}, where C{n = len(cs) - 1}.
91 @arg x: Polynomial argument (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
92 @arg cs: Polynomial coeffients (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}),
93 all positional.
94 @kwarg incx_name_f2product_nonfinites_RESIDUAL: Optional C{B{name}=NN} (C{str}),
95 C{B{incx}=True} for in-/decreasing exponents (C{bool}) and other
96 settings, see class L{Fsum<Fsum.__init__>}.
98 @raise OverflowError: Partial C{2sum} overflow.
100 @raise TypeError: Invalid B{C{x}}.
102 @raise ValueError: Non-finite B{C{x}}.
104 @see: Function L{fhorner} and methods L{Fsum.fadd} and L{Fsum.fmul}.
105 '''
106 incx, kwds = _xkwds_pop2(incx_name_f2product_nonfinites_RESIDUAL, incx=True)
107 Fsum.__init__(self, **kwds)
108 self._fhorner(x, cs, Fhorner, incx=incx)
111class Fhypot(Fsum):
112 '''Precision summation and hypotenuse, default C{root=2}.
113 '''
114 def __init__(self, *xs, **root_name_f2product_nonfinites_RESIDUAL_raiser):
115 '''New L{Fhypot} hypotenuse of (the I{root} of) several components (raised
116 to the power I{root}).
118 @arg xs: Components (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all
119 positional.
120 @kwarg root_name_f2product_nonfinites_RESIDUAL_raiser: Optional, exponent
121 and C{B{root}=2} order (C{scalar}), C{B{name}=NN} (C{str}),
122 C{B{raiser}=True} (C{bool}) for raising L{ResidualError}s and
123 other settings, see class L{Fsum<Fsum.__init__>} and method
124 L{root<Fsum.root>}.
125 '''
126 def _r_X_kwds(power=None, raiser=True, root=2, **kwds):
127 # DEPRECATED keyword argument C{power=2}, use C{root=2}
128 return (root if power is None else power), raiser, kwds
130 r = None # _xkwds_pop2 error
131 try:
132 r, X, kwds = _r_X_kwds(**root_name_f2product_nonfinites_RESIDUAL_raiser)
133 Fsum.__init__(self, **kwds)
134 self(_0_0)
135 if xs:
136 self._facc_power(r, xs, Fhypot, raiser=X)
137 self._fset(self.root(r, raiser=X))
138 except Exception as X:
139 raise self._ErrorXs(X, xs, root=r)
142class Fpolynomial(Fsum):
143 '''Precision polynomial evaluation.
144 '''
145 def __init__(self, x, *cs, **name_f2product_nonfinites_RESIDUAL):
146 '''New L{Fpolynomial} evaluation of the polynomial M{sum(cs[i] * x**i for
147 i=0..len(cs)-1)}.
149 @arg x: Polynomial argument (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
150 @arg cs: Polynomial coeffients (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}),
151 all positional.
152 @kwarg name_f2product_nonfinites_RESIDUAL: Optional C{B{name}=NN} (C{str})
153 and other settings, see class L{Fsum<Fsum.__init__>}.
155 @raise OverflowError: Partial C{2sum} overflow.
157 @raise TypeError: Invalid B{C{x}}.
159 @raise ValueError: Non-finite B{C{x}}.
161 @see: Class L{Fhorner}, function L{fpolynomial} and method L{Fsum.fadd}.
162 '''
163 Fsum.__init__(self, **name_f2product_nonfinites_RESIDUAL)
164 n = len(cs) - 1
165 self(_0_0 if n < 0 else cs[0])
166 self._facc_dot(n, cs[1:], _powers(x, n), **name_f2product_nonfinites_RESIDUAL)
169class Fpowers(Fsum):
170 '''Precision summation of powers, optimized for C{power=2, 3 and 4}.
171 '''
172 def __init__(self, power, *xs, **name_f2product_nonfinites_RESIDUAL_raiser):
173 '''New L{Fpowers} sum of (the I{power} of) several bases.
175 @arg power: The exponent (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
176 @arg xs: One or more bases (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all
177 positional.
178 @kwarg name_f2product_nonfinites_RESIDUAL_raiser: Optional C{B{name}=NN}
179 (C{str}), C{B{raiser}=True} (C{bool}) for raising L{ResidualError}s
180 and other settings, see class L{Fsum<Fsum.__init__>} and method
181 L{fpow<Fsum.fpow>}.
182 '''
183 try:
184 X, kwds = _xkwds_pop2(name_f2product_nonfinites_RESIDUAL_raiser, raiser=True)
185 Fsum.__init__(self, **kwds)
186 self(_0_0)
187 if xs:
188 self._facc_power(power, xs, Fpowers, raiser=X) # x**0 == 1
189 except Exception as X:
190 raise self._ErrorXs(X, xs, power=power)
193class Froot(Fsum):
194 '''The root of a precision summation.
195 '''
196 def __init__(self, root, *xs, **name_f2product_nonfinites_RESIDUAL_raiser):
197 '''New L{Froot} root of a precision sum.
199 @arg root: The order (C{scalar}, an L{Fsum} or L{Fsum2Tuple}), non-zero.
200 @arg xs: Items to summate (each a C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all
201 positional.
202 @kwarg name_f2product_nonfinites_RESIDUAL_raiser: Optional C{B{name}=NN}
203 (C{str}), C{B{raiser}=True} (C{bool}) for raising L{ResidualError}s
204 and other settings, see class L{Fsum<Fsum.__init__>} and method
205 L{fpow<Fsum.fpow>}.
206 '''
207 try:
208 X, kwds = _xkwds_pop2(name_f2product_nonfinites_RESIDUAL_raiser, raiser=True)
209 Fsum.__init__(self, **kwds)
210 self(_0_0)
211 if xs:
212 self.fadd(xs)
213 self(self.root(root, raiser=X))
214 except Exception as X:
215 raise self._ErrorXs(X, xs, root=root)
218class Fcbrt(Froot):
219 '''Cubic root of a precision summation.
220 '''
221 def __init__(self, *xs, **name_f2product_nonfinites_RESIDUAL_raiser):
222 '''New L{Fcbrt} cubic root of a precision sum.
224 @see: Class L{Froot<Froot.__init__>} for further details.
225 '''
226 Froot.__init__(self, 3, *xs, **name_f2product_nonfinites_RESIDUAL_raiser)
229class Fsqrt(Froot):
230 '''Square root of a precision summation.
231 '''
232 def __init__(self, *xs, **name_f2product_nonfinites_RESIDUAL_raiser):
233 '''New L{Fsqrt} square root of a precision sum.
235 @see: Class L{Froot<Froot.__init__>} for further details.
236 '''
237 Froot.__init__(self, 2, *xs, **name_f2product_nonfinites_RESIDUAL_raiser)
240def bqrt(x):
241 '''Return the 4-th, I{bi-quadratic} or I{quartic} root, M{x**(1 / 4)},
242 preserving C{type(B{x})}.
244 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
246 @return: I{Quartic} root (C{float} or an L{Fsum}).
248 @raise TypeeError: Invalid B{C{x}}.
250 @raise ValueError: Negative B{C{x}}.
252 @see: Functions L{zcrt} and L{zqrt}.
253 '''
254 return _root(x, _0_25, bqrt)
257try:
258 from math import cbrt as _cbrt # Python 3.11+
260except ImportError: # Python 3.10-
262 def _cbrt(x):
263 '''(INTERNAL) Compute the I{signed}, cube root M{x**(1/3)}.
264 '''
265 # <https://archive.lib.MSU.edu/crcmath/math/math/r/r021.htm>
266 # simpler and more accurate than Ken Turkowski's CubeRoot, see
267 # <https://People.FreeBSD.org/~lstewart/references/apple_tr_kt32_cuberoot.pdf>
268 return _copysign(pow(fabs(x), _1_3rd), x) # to avoid complex
271def cbrt(x):
272 '''Compute the cube root M{x**(1/3)}, preserving C{type(B{x})}.
274 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
276 @return: Cubic root (C{float} or L{Fsum}).
278 @see: Functions L{cbrt2} and L{sqrt3}.
279 '''
280 if _isFsum_2Tuple(x):
281 r = abs(x).fpow(_1_3rd)
282 if x.signOf() < 0:
283 r = -r
284 else:
285 r = _cbrt(x)
286 return r # cbrt(-0.0) == -0.0
289def cbrt2(x): # PYCHOK attr
290 '''Compute the cube root I{squared} M{x**(2/3)}, preserving C{type(B{x})}.
292 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
294 @return: Cube root I{squared} (C{float} or L{Fsum}).
296 @see: Functions L{cbrt} and L{sqrt3}.
297 '''
298 return abs(x).fpow(_2_3rd) if _isFsum_2Tuple(x) else _cbrt(x**2)
301def euclid(x, y):
302 '''I{Appoximate} the norm M{sqrt(x**2 + y**2)} by M{max(abs(x),
303 abs(y)) + min(abs(x), abs(y)) * 0.4142...}.
305 @arg x: X component (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
306 @arg y: Y component (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
308 @return: Appoximate norm (C{float} or L{Fsum}).
310 @see: Function L{euclid_}.
311 '''
312 x, y = abs(x), abs(y) # NOT fabs!
313 return (x + y * _0_4142) if x > y else \
314 (y + x * _0_4142) # * _0_5 before 20.10.02
317def euclid_(*xs):
318 '''I{Appoximate} the norm M{sqrt(sum(x**2 for x in xs))} by cascaded
319 L{euclid}.
321 @arg xs: X arguments (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}),
322 all positional.
324 @return: Appoximate norm (C{float} or L{Fsum}).
326 @see: Function L{euclid}.
327 '''
328 e = _0_0
329 for x in sorted(map(abs, xs)): # NOT fabs, reverse=True!
330 # e = euclid(x, e)
331 if e < x:
332 e, x = x, e
333 if x:
334 e += x * _0_4142
335 return e
338def facos1(x):
339 '''Fast approximation of L{pygeodesy.acos1}C{(B{x})}, scalar.
341 @see: U{ShaderFastLibs.h<https://GitHub.com/michaldrobot/
342 ShaderFastLibs/blob/master/ShaderFastMathLib.h>}.
343 '''
344 a = fabs(x)
345 if a < EPS0:
346 r = PI_2
347 elif a < EPS1:
348 r = _fast(-a, 1.5707288, 0.2121144, 0.0742610, 0.0187293)
349 r *= sqrt(_1_0 - a)
350 if x < 0:
351 r = PI - r
352 else:
353 r = PI if x < 0 else _0_0
354 return r
357def fasin1(x): # PYCHOK no cover
358 '''Fast approximation of L{pygeodesy.asin1}C{(B{x})}, scalar.
360 @see: L{facos1}.
361 '''
362 return PI_2 - facos1(x)
365def _fast(x, *cs):
366 '''(INTERNAL) Horner form for C{facos1} and C{fatan1}.
367 '''
368 h = 0
369 for c in reversed(cs):
370 h = _fma(x, h, c) if h else c
371 return h
374def fatan(x):
375 '''Fast approximation of C{atan(B{x})}, scalar.
376 '''
377 a = fabs(x)
378 if a < _1_0:
379 r = fatan1(a) if a else _0_0
380 elif a > _1_0:
381 r = PI_2 - fatan1(_1_0 / a) # == fatan2(a, _1_0)
382 else:
383 r = PI_4
384 if x < 0: # copysign0(r, x)
385 r = -r
386 return r
389def fatan1(x):
390 '''Fast approximation of C{atan(B{x})} for C{0 <= B{x} < 1}, I{unchecked}.
392 @see: U{ShaderFastLibs.h<https://GitHub.com/michaldrobot/ShaderFastLibs/
393 blob/master/ShaderFastMathLib.h>} and U{Efficient approximations
394 for the arctangent function<http://www-Labs.IRO.UMontreal.CA/
395 ~mignotte/IFT2425/Documents/EfficientApproximationArctgFunction.pdf>},
396 IEEE Signal Processing Magazine, 111, May 2006.
397 '''
398 # Eq (9): PI_4 * x - x * (abs(x) - 1) * (0.2447 + 0.0663 * abs(x)), for -1 < x < 1
399 # == PI_4 * x - (x**2 - x) * (0.2447 + 0.0663 * x), for 0 < x < 1
400 # == x * (1.0300981633974482 + x * (-0.1784 - x * 0.0663))
401 return _fast(x, _0_0, 1.0300981634, -0.1784, -0.0663)
404def fatan2(y, x):
405 '''Fast approximation of C{atan2(B{y}, B{x})}, scalar.
407 @see: U{fastApproximateAtan(x, y)<https://GitHub.com/CesiumGS/cesium/blob/
408 master/Source/Shaders/Builtin/Functions/fastApproximateAtan.glsl>}
409 and L{fatan1}.
410 '''
411 a, b = fabs(x), fabs(y)
412 if b > a:
413 r = (PI_2 - fatan1(a / b)) if a else PI_2
414 elif a > b:
415 r = fatan1(b / a) if b else _0_0
416 elif a: # a == b != 0
417 r = PI_4
418 else: # a == b == 0
419 return _0_0
420 if x < 0:
421 r = PI - r
422 if y < 0: # copysign0(r, y)
423 r = -r
424 return r
427def favg(a, b, f=_0_5, nonfinites=True):
428 '''Return the precise average of two values.
430 @arg a: One (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
431 @arg b: Other (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
432 @kwarg f: Optional fraction (C{float}).
433 @kwarg nonfinites: Optional setting, see function L{fma}.
435 @return: M{a + f * (b - a)} (C{float}).
436 '''
437 F = fma(f, (b - a), a, nonfinites=nonfinites)
438 return float(F)
441def fdot(xs, *ys, **start_f2product_nonfinites):
442 '''Return the precision dot product M{sum(xs[i] * ys[i] for i in range(len(xs)))}.
444 @arg xs: Iterable of values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
445 @arg ys: Other values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all positional.
446 @kwarg start_f2product_nonfinites: Optional bias C{B{start}=0} (C{scalar}, an
447 L{Fsum} or L{Fsum2Tuple}) and settings C{B{f2product}=None} (C{bool})
448 and C{B{nonfinites=True}} (C{bool}), see class L{Fsum<Fsum.__init__>}.
450 @return: Dot product (C{float}).
452 @raise LenError: Unequal C{len(B{xs})} and C{len(B{ys})}.
454 @see: Class L{Fdot}, U{Algorithm 5.10 B{DotK}
455 <https://www.TUHH.De/ti3/paper/rump/OgRuOi05.pdf>} and function
456 C{math.sumprod} in Python 3.12 and later.
457 '''
458 D = Fdot(xs, *ys, **_xkwds(start_f2product_nonfinites, nonfinites=True))
459 return float(D)
462def fdot_(*xys, **start_f2product_nonfinites):
463 '''Return the (precision) dot product M{sum(xys[i] * xys[i+1] for i in range(0, len(xys), B{2}))}.
465 @arg xys: Pairwise values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all positional.
467 @see: Function L{fdot} for further details.
469 @return: Dot product (C{float}).
470 '''
471 D = Fdot_(*xys, **_xkwds(start_f2product_nonfinites, nonfinites=True))
472 return float(D)
475def fdot3(xs, ys, zs, **start_f2product_nonfinites):
476 '''Return the (precision) dot product M{start + sum(xs[i] * ys[i] * zs[i] for i in range(len(xs)))}.
478 @arg xs: X values iterable (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
479 @arg ys: Y values iterable (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
480 @arg zs: Z values iterable (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
482 @see: Function L{fdot} for further details.
484 @return: Dot product (C{float}).
486 @raise LenError: Unequal C{len(B{xs})}, C{len(B{ys})} and/or C{len(B{zs})}.
487 '''
488 n = len(xs)
489 if not n == len(ys) == len(zs):
490 raise LenError(fdot3, xs=n, ys=len(ys), zs=len(zs))
492 D = Fdot((), **_xkwds(start_f2product_nonfinites, nonfinites=True))
493 kwds = dict(f2product=D.f2product(), nonfinites=D.nonfinites())
494 _f = Fsum(**kwds)
495 D = D._facc(_f(x).f2mul_(y, z, **kwds) for x, y, z in zip(xs, ys, zs))
496 return float(D)
499def fhorner(x, *cs, **incx):
500 '''Horner form evaluation of polynomial M{sum(cs[i] * x**i for i=0..n)} as
501 in- or decreasing exponent M{sum(... i=n..0)}, where C{n = len(cs) - 1}.
503 @return: Horner sum (C{float}).
505 @see: Class L{Fhorner<Fhorner.__init__>} for further details.
506 '''
507 H = Fhorner(x, *cs, **incx)
508 return float(H)
511def fidw(xs, ds, beta=2):
512 '''Interpolate using U{Inverse Distance Weighting
513 <https://WikiPedia.org/wiki/Inverse_distance_weighting>} (IDW).
515 @arg xs: Known values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
516 @arg ds: Non-negative distances (each C{scalar}, an L{Fsum} or
517 L{Fsum2Tuple}).
518 @kwarg beta: Inverse distance power (C{int}, 0, 1, 2, or 3).
520 @return: Interpolated value C{x} (C{float}).
522 @raise LenError: Unequal or zero C{len(B{ds})} and C{len(B{xs})}.
524 @raise TypeError: An invalid B{C{ds}} or B{C{xs}}.
526 @raise ValueError: Invalid B{C{beta}}, negative B{C{ds}} or
527 weighted B{C{ds}} below L{EPS}.
529 @note: Using C{B{beta}=0} returns the mean of B{C{xs}}.
530 '''
531 n, xs = len2(xs)
532 if n > 1:
533 b = -Int_(beta=beta, low=0, high=3)
534 if b < 0:
535 try: # weighted
536 _d, W, X = (Fsum() for _ in range(3))
537 for i, d in enumerate(_xiterable(ds)):
538 x = xs[i]
539 D = _d(d)
540 if D < EPS0:
541 if D < 0:
542 raise ValueError(_negative_)
543 x = float(x)
544 i = n
545 break
546 if D.fpow(b):
547 W += D
548 X += D.fmul(x)
549 else:
550 x = X.fover(W, raiser=False)
551 i += 1 # len(xs) >= len(ds)
552 except IndexError:
553 i += 1 # len(xs) < i < len(ds)
554 except Exception as X:
555 _I = Fmt.INDEX
556 raise _xError(X, _I(xs=i), x,
557 _I(ds=i), d)
558 else: # b == 0
559 x = fsum(xs) / n # fmean(xs)
560 i = n
561 elif n:
562 x = float(xs[0])
563 i = n
564 else:
565 x = _0_0
566 i, _ = len2(ds)
567 if i != n:
568 raise LenError(fidw, xs=n, ds=i)
569 return x
572try:
573 from math import fma as _fma # in .resections
574except ImportError: # PYCHOK DSPACE!
576 def _fma(x, y, z): # no need for accuracy
577 return x * y + z
580def fma(x, y, z, **nonfinites): # **raiser
581 '''Fused-multiply-add, using C{math.fma(x, y, z)} in Python 3.13+
582 or an equivalent implementation.
584 @arg x: Multiplicand (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
585 @arg y: Multiplier (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
586 @arg z: Addend (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
587 @kwarg nonfinites: Use C{B{nonfinites}=True} or C{=False},
588 to override default L{nonfiniterrors}
589 (C{bool}), see method L{Fsum.fma}.
591 @return: C{(x * y) + z} (C{float} or L{Fsum}).
592 '''
593 F, raiser = _Fm2(x, **nonfinites)
594 return F.fma(y, z, **raiser).as_iscalar
597def _Fm2(x, nonfinites=None, **raiser):
598 '''(INTERNAL) Handle C{fma} and C{f2mul} DEPRECATED C{raiser=False}.
599 '''
600 return Fsum(x, nonfinites=nonfinites), raiser
603def fmean(xs):
604 '''Compute the accurate mean M{sum(xs) / len(xs)}.
606 @arg xs: Values (each C{scalar}, or L{Fsum} or L{Fsum2Tuple}).
608 @return: Mean value (C{float}).
610 @raise LenError: No B{C{xs}} values.
612 @raise OverflowError: Partial C{2sum} overflow.
613 '''
614 n, xs = len2(xs)
615 if n < 1:
616 raise LenError(fmean, xs=xs)
617 M = Fsum(*xs, nonfinites=True)
618 return M.fover(n) if n > 1 else float(M)
621def fmean_(*xs, **nonfinites):
622 '''Compute the accurate mean M{sum(xs) / len(xs)}.
624 @see: Function L{fmean} for further details.
625 '''
626 return fmean(xs, **nonfinites)
629def f2mul_(x, *ys, **nonfinites): # **raiser
630 '''Cascaded, accurate multiplication C{B{x} * B{y} * B{y} ...} for all B{C{ys}}.
632 @arg x: Multiplicand (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
633 @arg ys: Multipliers (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all
634 positional.
635 @kwarg nonfinites: Use C{B{nonfinites}=True} or C{=False}, to override default
636 L{nonfiniterrors} (C{bool}), see method L{Fsum.f2mul_}.
638 @return: The cascaded I{TwoProduct} (C{float}, C{int} or L{Fsum}).
640 @see: U{Equations 2.3<https://www.TUHH.De/ti3/paper/rump/OzOgRuOi06.pdf>}
641 '''
642 F, raiser = _Fm2(x, **nonfinites)
643 return F.f2mul_(*ys, **raiser).as_iscalar
646def fpolynomial(x, *cs, **over_f2product_nonfinites):
647 '''Evaluate the polynomial M{sum(cs[i] * x**i for i=0..len(cs)) [/ over]}.
649 @kwarg over_f2product_nonfinites: Optional final divisor C{B{over}=None}
650 (I{non-zero} C{scalar}) and other settings, see class
651 L{Fpolynomial<Fpolynomial.__init__>}.
653 @return: Polynomial value (C{float} or L{Fpolynomial}).
654 '''
655 d, kwds = _xkwds_pop2(over_f2product_nonfinites, over=0)
656 P = Fpolynomial(x, *cs, **kwds)
657 return P.fover(d) if d else float(P)
660def fpowers(x, n, alts=0):
661 '''Return a series of powers M{[x**i for i=1..n]}, note I{1..!}
663 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
664 @arg n: Highest exponent (C{int}).
665 @kwarg alts: Only alternating powers, starting with this
666 exponent (C{int}).
668 @return: Tuple of powers of B{C{x}} (each C{type(B{x})}).
670 @raise TypeError: Invalid B{C{x}} or B{C{n}} not C{int}.
672 @raise ValueError: Non-finite B{C{x}} or invalid B{C{n}}.
673 '''
674 if not isint(n):
675 raise _IsnotError(typename(int), n=n)
676 elif n < 1:
677 raise _ValueError(n=n)
679 p = x if isscalar(x) or _isFsum_2Tuple(x) else _2float(x=x)
680 ps = tuple(_powers(p, n))
682 if alts > 0: # x**2, x**4, ...
683 # ps[alts-1::2] chokes PyChecker
684 ps = ps[slice(alts-1, None, 2)]
686 return ps
689try:
690 from math import prod as fprod # Python 3.8
691except ImportError:
693 def fprod(xs, start=1):
694 '''Iterable product, like C{math.prod} or C{numpy.prod}.
696 @arg xs: Iterable of values to be multiplied (each
697 C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
698 @kwarg start: Initial value, also the value returned
699 for an empty B{C{xs}} (C{scalar}).
701 @return: The product (C{float} or L{Fsum}).
703 @see: U{NumPy.prod<https://docs.SciPy.org/doc/
704 numpy/reference/generated/numpy.prod.html>}.
705 '''
706 return freduce(_operator.mul, xs, start)
709def frandoms(n, seeded=None):
710 '''Generate C{n} (long) lists of random C{floats}.
712 @arg n: Number of lists to generate (C{int}, non-negative).
713 @kwarg seeded: If C{scalar}, use C{random.seed(B{seeded})} or
714 if C{True}, seed using today's C{year-day}.
716 @see: U{Hettinger<https://GitHub.com/ActiveState/code/tree/master/recipes/
717 Python/393090_Binary_floating_point_summatiaccurate_full/recipe-393090.py>}.
718 '''
719 from random import gauss, random, seed, shuffle
721 if seeded is None:
722 pass
723 elif seeded and isbool(seeded):
724 from time import localtime
725 seed(localtime().tm_yday)
726 elif isscalar(seeded):
727 seed(seeded)
729 c = (7, 1e100, -7, -1e100, -9e-20, 8e-20) * 7
730 for _ in range(n):
731 s = 0
732 t = list(c)
733 _a = t.append
734 for _ in range(n * 8):
735 v = gauss(0, random())**7 - s
736 _a(v)
737 s += v
738 shuffle(t)
739 yield t
742def frange(start, number, step=1):
743 '''Generate a range of C{float}s.
745 @arg start: First value (C{float}).
746 @arg number: The number of C{float}s to generate (C{int}).
747 @kwarg step: Increment value (C{float}).
749 @return: A generator (C{float}s).
751 @see: U{NumPy.prod<https://docs.SciPy.org/doc/
752 numpy/reference/generated/numpy.arange.html>}.
753 '''
754 if not isint(number):
755 raise _IsnotError(typename(int), number=number)
756 for i in range(number):
757 yield start + (step * i)
760try:
761 from functools import reduce as freduce
762except ImportError:
763 try:
764 freduce = reduce # PYCHOK expected
765 except NameError: # Python 3+
767 def freduce(f, xs, *start):
768 '''For missing C{functools.reduce}.
769 '''
770 if start:
771 r = v = start[0]
772 else:
773 r, v = 0, MISSING
774 for v in xs:
775 r = f(r, v)
776 if v is MISSING:
777 raise _TypeError(xs=(), start=MISSING)
778 return r
781def fremainder(x, y):
782 '''Remainder in range C{[-B{y / 2}, B{y / 2}]}.
784 @arg x: Numerator (C{scalar}).
785 @arg y: Modulus, denominator (C{scalar}).
787 @return: Remainder (C{scalar}, preserving signed
788 0.0) or C{NAN} for any non-finite B{C{x}}.
790 @raise ValueError: Infinite or near-zero B{C{y}}.
792 @see: I{Karney}'s U{Math.remainder<https://PyPI.org/
793 project/geographiclib/>} and Python 3.7+
794 U{math.remainder<https://docs.Python.org/3/
795 library/math.html#math.remainder>}.
796 '''
797 # with Python 2.7.16 and 3.7.3 on macOS 10.13.6 and
798 # with Python 3.10.2 on macOS 12.2.1 M1 arm64 native
799 # fmod( 0, 360) == 0.0
800 # fmod( 360, 360) == 0.0
801 # fmod(-0, 360) == 0.0
802 # fmod(-0.0, 360) == -0.0
803 # fmod(-360, 360) == -0.0
804 # however, using the % operator ...
805 # 0 % 360 == 0
806 # 360 % 360 == 0
807 # 360.0 % 360 == 0.0
808 # -0 % 360 == 0
809 # -360 % 360 == 0 == (-360) % 360
810 # -0.0 % 360 == 0.0 == (-0.0) % 360
811 # -360.0 % 360 == 0.0 == (-360.0) % 360
813 # On Windows 32-bit with python 2.7, math.fmod(-0.0, 360)
814 # == +0.0. This fixes this bug. See also Math::AngNormalize
815 # in the C++ library, Math.sincosd has a similar fix.
816 if isfinite(x):
817 try:
818 r = remainder(x, y) if x else x
819 except Exception as e:
820 raise _xError(e, unstr(fremainder, x, y))
821 else: # handle x INF and NINF as NAN
822 r = NAN
823 return r
826if _MODS.sys_version_info2 < (3, 8): # PYCHOK no cover
827 from math import hypot # OK in Python 3.7-
829 def hypot_(*xs):
830 '''Compute the norm M{sqrt(sum(x**2 for x in xs))}.
832 Similar to Python 3.8+ n-dimension U{math.hypot
833 <https://docs.Python.org/3.8/library/math.html#math.hypot>},
834 but exceptions, C{nan} and C{infinite} values are
835 handled differently.
837 @arg xs: X arguments (C{scalar}s), all positional.
839 @return: Norm (C{float}).
841 @raise OverflowError: Partial C{2sum} overflow.
843 @raise ValueError: Invalid or no B{C{xs}} values.
845 @note: The Python 3.8+ Euclidian distance U{math.dist
846 <https://docs.Python.org/3.8/library/math.html#math.dist>}
847 between 2 I{n}-dimensional points I{p1} and I{p2} can be
848 computed as M{hypot_(*((c1 - c2) for c1, c2 in zip(p1, p2)))},
849 provided I{p1} and I{p2} have the same, non-zero length I{n}.
850 '''
851 return float(_Hypot(*xs))
853elif _MODS.sys_version_info2 < (3, 10): # PYCHOK no cover
854 # In Python 3.8 and 3.9 C{math.hypot} is inaccurate, see
855 # U{agdhruv<https://GitHub.com/geopy/geopy/issues/466>},
856 # U{cffk<https://Bugs.Python.org/issue43088>} and module
857 # U{geomath.py<https://PyPI.org/project/geographiclib/1.52>}
859 def hypot(x, y):
860 '''Compute the norm M{sqrt(x**2 + y**2)}.
862 @arg x: X argument (C{scalar}).
863 @arg y: Y argument (C{scalar}).
865 @return: C{sqrt(B{x}**2 + B{y}**2)} (C{float}).
866 '''
867 return float(_Hypot(x, y))
869 from math import hypot as hypot_ # PYCHOK in Python 3.8 and 3.9
870else:
871 from math import hypot # PYCHOK in Python 3.10+
872 hypot_ = hypot
875def _Hypot(*xs):
876 '''(INTERNAL) Substitute for inaccurate C{math.hypot}.
877 '''
878 return Fhypot(*xs, nonfinites=True, raiser=False) # f2product=True
881def hypot1(x):
882 '''Compute the norm M{sqrt(1 + x**2)}.
884 @arg x: Argument (C{scalar} or L{Fsum} or L{Fsum2Tuple}).
886 @return: Norm (C{float} or L{Fhypot}).
887 '''
888 h = _1_0
889 if x:
890 if _isFsum_2Tuple(x):
891 h = _Hypot(h, x)
892 h = float(h)
893 else:
894 h = hypot(h, x)
895 return h
898def hypot2(x, y):
899 '''Compute the I{squared} norm M{x**2 + y**2}.
901 @arg x: X (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
902 @arg y: Y (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
904 @return: C{B{x}**2 + B{y}**2} (C{float}).
905 '''
906 x, y = map1(abs, x, y) # NOT fabs!
907 if y > x:
908 x, y = y, x
909 h2 = x**2
910 if h2 and y:
911 h2 *= (y / x)**2 + _1_0
912 return float(h2)
915def hypot2_(*xs):
916 '''Compute the I{squared} norm C{fsum(x**2 for x in B{xs})}.
918 @arg xs: Components (each C{scalar}, an L{Fsum} or
919 L{Fsum2Tuple}), all positional.
921 @return: Squared norm (C{float}).
923 @see: Class L{Fpowers} for further details.
924 '''
925 h2 = float(max(map(abs, xs))) if xs else _0_0
926 if h2: # and isfinite(h2)
927 _h = _1_0 / h2
928 xs = ((x * _h) for x in xs)
929 H2 = Fpowers(2, *xs, nonfinites=True) # f2product=True
930 h2 = H2.fover(_h**2)
931 return h2
934def norm2(x, y):
935 '''Normalize a 2-dimensional vector.
937 @arg x: X component (C{scalar}).
938 @arg y: Y component (C{scalar}).
940 @return: 2-Tuple C{(x, y)}, normalized.
942 @raise ValueError: Invalid B{C{x}} or B{C{y}}
943 or zero norm.
944 '''
945 try:
946 h = None
947 h = hypot(x, y)
948 if h:
949 x, y = (x / h), (y / h)
950 else:
951 x = _copysign_0_0(x) # pass?
952 y = _copysign_0_0(y)
953 except Exception as e:
954 raise _xError(e, x=x, y=y, h=h)
955 return x, y
958def norm_(*xs):
959 '''Normalize the components of an n-dimensional vector.
961 @arg xs: Components (each C{scalar}, an L{Fsum} or
962 L{Fsum2Tuple}), all positional.
964 @return: Yield each component, normalized.
966 @raise ValueError: Invalid or insufficent B{C{xs}}
967 or zero norm.
968 '''
969 try:
970 i = h = None
971 x = xs
972 h = hypot_(*xs)
973 _h = (_1_0 / h) if h else _0_0
974 for i, x in enumerate(xs):
975 yield x * _h
976 except Exception as X:
977 raise _xsError(X, xs, i, x, h=h)
980def _powers(x, n):
981 '''(INTERNAL) Yield C{x**i for i=1..n}.
982 '''
983 p = 1 # type(p) == type(x)
984 for _ in range(n):
985 p *= x
986 yield p
989def _root(x, p, where):
990 '''(INTERNAL) Raise C{x} to power C{0 < p < 1}.
991 '''
992 try:
993 if x > 0:
994 r = Fsum(f2product=True, nonfinites=True)(x)
995 return r.fpow(p).as_iscalar
996 elif x < 0:
997 raise ValueError(_negative_)
998 except Exception as X:
999 raise _xError(X, unstr(where, x))
1000 return _0_0 if p else _1_0
1003def sqrt0(x, Error=None):
1004 '''Return the square root C{sqrt(B{x})} iff C{B{x} > }L{EPS02},
1005 preserving C{type(B{x})}.
1007 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1008 @kwarg Error: Error to raise for negative B{C{x}}.
1010 @return: Square root (C{float} or L{Fsum}) or C{0.0}.
1012 @raise TypeeError: Invalid B{C{x}}.
1014 @note: Any C{B{x} < }L{EPS02} I{including} C{B{x} < 0}
1015 returns C{0.0}.
1016 '''
1017 if Error and x < 0:
1018 raise Error(unstr(sqrt0, x))
1019 return _root(x, _0_5, sqrt0) if x > EPS02 else (
1020 _0_0 if x < EPS02 else EPS0)
1023def sqrt3(x):
1024 '''Return the square root, I{cubed} M{sqrt(x)**3} or M{sqrt(x**3)},
1025 preserving C{type(B{x})}.
1027 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1029 @return: Square root I{cubed} (C{float} or L{Fsum}).
1031 @raise TypeeError: Invalid B{C{x}}.
1033 @raise ValueError: Negative B{C{x}}.
1035 @see: Functions L{cbrt} and L{cbrt2}.
1036 '''
1037 return _root(x, _1_5, sqrt3)
1040def sqrt_a(h, b):
1041 '''Compute C{I{a}} side of a right-angled triangle from
1042 C{sqrt(B{h}**2 - B{b}**2)}.
1044 @arg h: Hypotenuse or outer annulus radius (C{scalar}).
1045 @arg b: Triangle side or inner annulus radius (C{scalar}).
1047 @return: C{copysign(I{a}, B{h})} or C{unsigned 0.0} (C{float}).
1049 @raise TypeError: Non-scalar B{C{h}} or B{C{b}}.
1051 @raise ValueError: If C{abs(B{h}) < abs(B{b})}.
1053 @see: Inner tangent chord B{I{d}} of an U{annulus
1054 <https://WikiPedia.org/wiki/Annulus_(mathematics)>}
1055 and function U{annulus_area<https://People.SC.FSU.edu/
1056 ~jburkardt/py_src/geometry/geometry.py>}.
1057 '''
1058 try:
1059 if not (_isHeight(h) and _isRadius(b)):
1060 raise TypeError(_not_scalar_)
1061 c = fabs(h)
1062 if c > EPS0:
1063 s = _1_0 - (b / c)**2
1064 if s < 0:
1065 raise ValueError(_h_lt_b_)
1066 a = (sqrt(s) * c) if 0 < s < 1 else (c if s else _0_0)
1067 else: # PYCHOK no cover
1068 b = fabs(b)
1069 d = c - b
1070 if d < 0:
1071 raise ValueError(_h_lt_b_)
1072 d *= c + b
1073 a = sqrt(d) if d else _0_0
1074 except Exception as x:
1075 raise _xError(x, h=h, b=b)
1076 return copysign0(a, h)
1079def zcrt(x):
1080 '''Return the 6-th, I{zenzi-cubic} root, M{x**(1 / 6)},
1081 preserving C{type(B{x})}.
1083 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1085 @return: I{Zenzi-cubic} root (C{float} or L{Fsum}).
1087 @see: Functions L{bqrt} and L{zqrt}.
1089 @raise TypeeError: Invalid B{C{x}}.
1091 @raise ValueError: Negative B{C{x}}.
1092 '''
1093 return _root(x, _1_6th, zcrt)
1096def zqrt(x):
1097 '''Return the 8-th, I{zenzi-quartic} or I{squared-quartic} root,
1098 M{x**(1 / 8)}, preserving C{type(B{x})}.
1100 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1102 @return: I{Zenzi-quartic} root (C{float} or L{Fsum}).
1104 @see: Functions L{bqrt} and L{zcrt}.
1106 @raise TypeeError: Invalid B{C{x}}.
1108 @raise ValueError: Negative B{C{x}}.
1109 '''
1110 return _root(x, _0_125, zqrt)
1112# **) MIT License
1113#
1114# Copyright (C) 2016-2025 -- mrJean1 at Gmail -- All Rights Reserved.
1115#
1116# Permission is hereby granted, free of charge, to any person obtaining a
1117# copy of this software and associated documentation files (the "Software"),
1118# to deal in the Software without restriction, including without limitation
1119# the rights to use, copy, modify, merge, publish, distribute, sublicense,
1120# and/or sell copies of the Software, and to permit persons to whom the
1121# Software is furnished to do so, subject to the following conditions:
1122#
1123# The above copyright notice and this permission notice shall be included
1124# in all copies or substantial portions of the Software.
1125#
1126# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1127# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1128# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1129# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1130# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1131# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1132# OTHER DEALINGS IN THE SOFTWARE.