Coverage for pygeodesy / fmath.py: 89%
354 statements
« prev ^ index » next coverage.py v7.14.0, created at 2026-06-12 20:42 -0400
« prev ^ index » next coverage.py v7.14.0, created at 2026-06-12 20:42 -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 _ # noqa: E702 ;
10from pygeodesy.basics import _copysign, copysign0, isbool, isint, isodd, \
11 isscalar, len2, _xiterable, _zip, typename
12from pygeodesy.constants import EPS0, EPS02, EPS1, NAN, PI, PI_2, PI_4, \
13 _0_0, _0_125, _0_25, _1_3rd, _0_5, _2_3rd, \
14 _1_0, _1_5, _copysign_0_0, isfinite, remainder
15from pygeodesy.errors import _IsnotError, LenError, _TypeError, _ValueError, \
16 _xError, _xkwds, _xkwds_get, _xkwds_pop2, _xsError
17from pygeodesy.fsums import _2float, Fsum, fsum, _isFsum_2Tuple
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
21from pygeodesy.streprs import Fmt, unstr
22from pygeodesy.units import Int_, _isHeight, _isRadius
23# from utily import atan2b, atan2p # _MODS, circular import!
25from math import fabs, sqrt # pow
26import operator as _operator # in .datums, .elliptic, .trf, .utm
28__all__ = _ALL_LAZY.fmath
29__version__ = '26.03.31'
31# sqrt(2) - 1 <https://WikiPedia.org/wiki/Square_root_of_2>
32_0_4142 = 0.41421356237309504880 # ~ 3_730_904_090_310_553 / 9_007_199_254_740_992
33_1_6th = 1 / 6
34_h_lt_b_ = 'abs(h) < abs(b)'
37class Fdot(Fsum):
38 '''Precision dot product.
39 '''
40 def __init__(self, a, *b, **start_name_f2product_nonfinites_RESIDUAL):
41 '''New L{Fdot} precision dot product M{start + sum(a[i] * b[i] for i=0..len(a)-1)}.
43 @arg a: Iterable of values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
44 @arg b: Other values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all
45 positional.
46 @kwarg start_name_f2product_nonfinites_RESIDUAL: Optional bias C{B{start}=0}
47 (C{scalar}, an L{Fsum} or L{Fsum2Tuple}), C{B{name}=NN} (C{str})
48 and other settings, see class L{Fsum<Fsum.__init__>}.
50 @raise LenError: Unequal C{len(B{a})} and C{len(B{b})}.
52 @raise OverflowError: Partial C{2sum} overflow.
54 @raise TypeError: Invalid B{C{x}}.
56 @raise ValueError: Non-finite B{C{x}}.
58 @see: Function L{fdot} and method L{Fsum.fadd}.
59 '''
60 s, kwds = _xkwds_pop2(start_name_f2product_nonfinites_RESIDUAL, start=_0_0)
61 Fsum.__init__(self, **kwds)
62 self(s)
64 n = len(b)
65 if len(a) != n: # PYCHOK no cover
66 raise LenError(Fdot, a=len(a), b=n)
67 self._facc_dot(n, a, b, **kwds)
70class Fdot_(Fdot): # in .elliptic
71 '''Precision dot product.
72 '''
73 def __init__(self, *xys, **start_name_f2product_nonfinites_RESIDUAL):
74 '''New L{Fdot_} precision dot product M{start + sum(xys[i] * xys[i+1] for i in
75 range(0, len(xys), B{2}))}.
77 @arg xys: Pairwise values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}),
78 all positional.
80 @see: Class L{Fdot<Fdot.__init__>} for further details.
81 '''
82 if isodd(len(xys)):
83 raise LenError(Fdot_, xys=len(xys))
84 Fdot.__init__(self, xys[0::2], *xys[1::2], **start_name_f2product_nonfinites_RESIDUAL)
87class Fhorner(Fsum):
88 '''Precision polynomial evaluation using the Horner form.
89 '''
90 def __init__(self, x, *cs, **incx_name_f2product_nonfinites_RESIDUAL):
91 '''New L{Fhorner} form evaluation of polynomial M{sum(cs[i] * x**i for i=0..n)}
92 with in- or decreasing exponent M{sum(... i=n..0)}, where C{n = len(cs) - 1}.
94 @arg x: Polynomial argument (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
95 @arg cs: Polynomial coeffients (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}),
96 all positional.
97 @kwarg incx_name_f2product_nonfinites_RESIDUAL: Optional C{B{name}=NN} (C{str}),
98 C{B{incx}=True} for in-/decreasing exponents (C{bool}) and other
99 settings, see class L{Fsum<Fsum.__init__>}.
101 @raise OverflowError: Partial C{2sum} overflow.
103 @raise TypeError: Invalid B{C{x}}.
105 @raise ValueError: Non-finite B{C{x}}.
107 @see: Function L{fhorner} and methods L{Fsum.fadd} and L{Fsum.fmul}.
108 '''
109 incx, kwds = _xkwds_pop2(incx_name_f2product_nonfinites_RESIDUAL, incx=True)
110 Fsum.__init__(self, **kwds)
111 self._fhorner(x, cs, Fhorner, incx=incx)
114class Fhypot(Fsum):
115 '''Precision summation and hypotenuse, default C{root=2}.
116 '''
117 def __init__(self, *xs, **root_name_f2product_nonfinites_RESIDUAL_raiser):
118 '''New L{Fhypot} hypotenuse of (the I{root} of) several components (raised
119 to the power I{root}).
121 @arg xs: Components (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all
122 positional.
123 @kwarg root_name_f2product_nonfinites_RESIDUAL_raiser: Optional, exponent
124 and C{B{root}=2} order (C{scalar}), C{B{name}=NN} (C{str}),
125 C{B{raiser}=True} (C{bool}) for raising L{ResidualError}s and
126 other settings, see class L{Fsum<Fsum.__init__>} and method
127 L{root<Fsum.root>}.
128 '''
129 def _r_X_kwds(power=None, raiser=True, root=2, **kwds):
130 # DEPRECATED keyword argument C{power=2}, use C{root=2}
131 return (root if power is None else power), raiser, kwds
133 r = None # _xkwds_pop2 error
134 try:
135 r, X, kwds = _r_X_kwds(**root_name_f2product_nonfinites_RESIDUAL_raiser)
136 Fsum.__init__(self, **kwds)
137 self(_0_0)
138 if xs:
139 self._facc_power(r, xs, Fhypot, raiser=X)
140 self._fset(self.root(r, raiser=X))
141 except Exception as X:
142 raise self._ErrorXs(X, xs, root=r)
145class Fpolynomial(Fsum):
146 '''Precision polynomial evaluation.
147 '''
148 def __init__(self, x, *cs, **name_f2product_nonfinites_RESIDUAL):
149 '''New L{Fpolynomial} evaluation of the polynomial M{sum(cs[i] * x**i for
150 i=0..len(cs)-1)}.
152 @arg x: Polynomial argument (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
153 @arg cs: Polynomial coeffients (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}),
154 all positional.
155 @kwarg name_f2product_nonfinites_RESIDUAL: Optional C{B{name}=NN} (C{str})
156 and other settings, see class L{Fsum<Fsum.__init__>}.
158 @raise OverflowError: Partial C{2sum} overflow.
160 @raise TypeError: Invalid B{C{x}}.
162 @raise ValueError: Non-finite B{C{x}}.
164 @see: Class L{Fhorner}, function L{fpolynomial} and method L{Fsum.fadd}.
165 '''
166 Fsum.__init__(self, **name_f2product_nonfinites_RESIDUAL)
167 n = len(cs) - 1
168 self(_0_0 if n < 0 else cs[0])
169 self._facc_dot(n, cs[1:], _powers(x, n), **name_f2product_nonfinites_RESIDUAL)
172class Fpowers(Fsum):
173 '''Precision summation of powers, optimized for C{power=2, 3 and 4}.
174 '''
175 def __init__(self, power, *xs, **name_f2product_nonfinites_RESIDUAL_raiser):
176 '''New L{Fpowers} sum of (the I{power} of) several bases.
178 @arg power: The exponent (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
179 @arg xs: One or more bases (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all
180 positional.
181 @kwarg name_f2product_nonfinites_RESIDUAL_raiser: Optional C{B{name}=NN}
182 (C{str}), C{B{raiser}=True} (C{bool}) for raising L{ResidualError}s
183 and other settings, see class L{Fsum<Fsum.__init__>} and method
184 L{fpow<Fsum.fpow>}.
185 '''
186 try:
187 X, kwds = _xkwds_pop2(name_f2product_nonfinites_RESIDUAL_raiser, raiser=True)
188 Fsum.__init__(self, **kwds)
189 self(_0_0)
190 if xs:
191 self._facc_power(power, xs, Fpowers, raiser=X) # x**0 == 1
192 except Exception as X:
193 raise self._ErrorXs(X, xs, power=power)
196class Froot(Fsum):
197 '''The root of a precision summation.
198 '''
199 def __init__(self, root, *xs, **name_f2product_nonfinites_RESIDUAL_raiser):
200 '''New L{Froot} root of a precision sum.
202 @arg root: The order (C{scalar}, an L{Fsum} or L{Fsum2Tuple}), non-zero.
203 @arg xs: Items to summate (each a C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all
204 positional.
205 @kwarg name_f2product_nonfinites_RESIDUAL_raiser: Optional C{B{name}=NN}
206 (C{str}), C{B{raiser}=True} (C{bool}) for raising L{ResidualError}s
207 and other settings, see class L{Fsum<Fsum.__init__>} and method
208 L{fpow<Fsum.fpow>}.
209 '''
210 try:
211 X, kwds = _xkwds_pop2(name_f2product_nonfinites_RESIDUAL_raiser, raiser=True)
212 Fsum.__init__(self, **kwds)
213 self(_0_0)
214 if xs:
215 self.fadd(xs)
216 self(self.root(root, raiser=X))
217 except Exception as X:
218 raise self._ErrorXs(X, xs, root=root)
221class Fcbrt(Froot):
222 '''Cubic root of a precision summation.
223 '''
224 def __init__(self, *xs, **name_f2product_nonfinites_RESIDUAL_raiser):
225 '''New L{Fcbrt} cubic root of a precision sum.
227 @see: Class L{Froot<Froot.__init__>} for further details.
228 '''
229 Froot.__init__(self, 3, *xs, **name_f2product_nonfinites_RESIDUAL_raiser)
232class Fsqrt(Froot):
233 '''Square root of a precision summation.
234 '''
235 def __init__(self, *xs, **name_f2product_nonfinites_RESIDUAL_raiser):
236 '''New L{Fsqrt} square root of a precision sum.
238 @see: Class L{Froot<Froot.__init__>} for further details.
239 '''
240 Froot.__init__(self, 2, *xs, **name_f2product_nonfinites_RESIDUAL_raiser)
243def bqrt(x):
244 '''Return the 4-th, I{bi-quadratic} or I{quartic} root, M{x**(1 / 4)},
245 preserving C{type(B{x})}.
247 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
249 @return: I{Quartic} root (C{float} or an L{Fsum}).
251 @raise TypeeError: Invalid B{C{x}}.
253 @raise ValueError: Negative B{C{x}}.
255 @see: Functions L{zcrt} and L{zqrt}.
256 '''
257 return _root(x, _0_25, bqrt)
260try:
261 from math import cbrt as _cbrt # Python 3.11+
262except ImportError: # Python 3.10-
264 def _cbrt(x):
265 '''(INTERNAL) Compute the I{signed}, cube root M{x**(1/3)}.
266 '''
267 # <https://archive.lib.MSU.edu/crcmath/math/math/r/r021.htm>
268 # simpler and more accurate than Ken Turkowski's CubeRoot, see
269 # <https://People.FreeBSD.org/~lstewart/references/apple_tr_kt32_cuberoot.pdf>
270 return _copysign(pow(fabs(x), _1_3rd), x) # to avoid complex
273def cbrt(x):
274 '''Compute the cube root M{x**(1/3)}, preserving C{type(B{x})}.
276 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
278 @return: Cubic root (C{float} or L{Fsum}).
280 @see: Functions L{cbrt2} and L{sqrt3}.
281 '''
282 if _isFsum_2Tuple(x):
283 r = abs(x).fpow(_1_3rd)
284 if x.signOf() < 0:
285 r = -r
286 else:
287 r = _cbrt(x)
288 return r # cbrt(-0.0) == -0.0
291def cbrt2(x): # PYCHOK attr
292 '''Compute the cube root I{squared} M{x**(2/3)}, preserving C{type(B{x})}.
294 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
296 @return: Cube root I{squared} (C{float} or L{Fsum}).
298 @see: Functions L{cbrt} and L{sqrt3}.
299 '''
300 return abs(x).fpow(_2_3rd) if _isFsum_2Tuple(x) else _cbrt(x**2)
303def euclid(x, y, *xy0):
304 '''I{Appoximate} the norm M{hypot(B{x}, B{y})} by M{max(abs(B{x}),
305 abs(B{y})) + min(abs(B{x}), abs(B{y})) * 0.4142...}.
307 @arg x: X component (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
308 @arg y: Y component (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
309 @arg xy0: Optional reference C{(x0, y0)} (each C{scalar}, an
310 L{Fsum} or L{Fsum2Tuple}).
312 @return: Appoximate norm (C{float} or L{Fsum}).
313 '''
314 x, y = _map0(abs, x, y, *xy0) # NOT fabs!
315 if x < y:
316 x, y = y, x
317 return x + y * _0_4142 # _0_5 before 20.10.02
320def euclid_(*xs):
321 '''I{Appoximate} the norm M{sqrt(sum(x**2 for x in xs))} by cascaded
322 L{euclid}.
324 @arg xs: X values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}),
325 all positional.
327 @return: Appoximate norm (C{float} or L{Fsum}).
329 @see: Function L{euclid}.
330 '''
331 e = _0_0
332 for x in sorted(map(abs, xs)): # NOT fabs, reverse=True!
333 # e = euclid(x, e)
334 if x:
335 if e < x:
336 e, x = x, e
337 x *= _0_4142
338# s = e + x
339# if e < x: # like .fsums._2sum
340# x -= s # e = (x - s) + e
341# else:
342# e -= s # e = (e - s) + x
343 e += x
344# e += s
345 return e
348def facos1(x):
349 '''Fast approximation of L{pygeodesy.acos1}C{(B{x})}, scalar.
351 @see: U{ShaderFastLibs.h<https://GitHub.com/michaldrobot/
352 ShaderFastLibs/blob/master/ShaderFastMathLib.h>}.
353 '''
354 a = fabs(x)
355 if a < EPS0:
356 r = PI_2
357 elif a < EPS1:
358 r = _fast(-a, 1.5707288, 0.2121144, 0.0742610, 0.0187293)
359 r *= sqrt(_1_0 - a)
360 if x < 0:
361 r = PI - r
362 else:
363 r = PI if x < 0 else _0_0
364 return r
367def fasin1(x): # PYCHOK no cover
368 '''Fast approximation of L{pygeodesy.asin1}C{(B{x})}, scalar.
370 @see: L{facos1}.
371 '''
372 return PI_2 - facos1(x)
375def _fast(x, *cs):
376 '''(INTERNAL) Horner form for C{facos1} and C{fatan1}.
377 '''
378 h = 0
379 for c in reversed(cs):
380 h = _fma(x, h, c) if h else c
381 return h
384def fatan(x):
385 '''Fast approximation of C{atan(B{x})}, scalar.
386 '''
387 a = fabs(x)
388 if a < _1_0:
389 r = fatan1(a) if a else _0_0
390 elif a > _1_0:
391 r = PI_2 - fatan1(_1_0 / a) # == fatan2(a, _1_0)
392 else:
393 r = PI_4
394 if x < 0: # copysign0(r, x)
395 r = -r
396 return r
399def fatan1(x):
400 '''Fast approximation of C{atan(B{x})} for C{0 <= B{x} < 1}, I{unchecked}.
402 @see: U{ShaderFastLibs.h<https://GitHub.com/michaldrobot/ShaderFastLibs/
403 blob/master/ShaderFastMathLib.h>} and U{Efficient approximations
404 for the arctangent function<http://www-Labs.IRO.UMontreal.CA/
405 ~mignotte/IFT2425/Documents/EfficientApproximationArctgFunction.pdf>},
406 IEEE Signal Processing Magazine, 111, May 2006.
407 '''
408 # Eq (9): PI_4 * x - x * (abs(x) - 1) * (0.2447 + 0.0663 * abs(x)), for -1 < x < 1
409 # == PI_4 * x - (x**2 - x) * (0.2447 + 0.0663 * x), for 0 < x < 1
410 # == x * (1.0300981633974482 + x * (-0.1784 - x * 0.0663))
411 return _fast(x, _0_0, 1.0300981634, -0.1784, -0.0663)
414def fatan2(y, x):
415 '''Fast approximation of C{atan2(B{y}, B{x})}, scalar.
417 @see: U{fastApproximateAtan(x, y)<https://GitHub.com/CesiumGS/cesium/blob/
418 master/Source/Shaders/Builtin/Functions/fastApproximateAtan.glsl>}
419 and L{fatan1}.
420 '''
421 a, b = fabs(x), fabs(y)
422 if b > a:
423 r = (PI_2 - fatan1(a / b)) if a else PI_2
424 elif a > b:
425 r = fatan1(b / a) if b else _0_0
426 elif a: # a == b != 0
427 r = PI_4
428 else: # a == b == 0
429 return _0_0
430 if x < 0:
431 r = PI - r
432 if y < 0: # copysign0(r, y)
433 r = -r
434 return r
437def favg(a, b, f=_0_5, nonfinites=True):
438 '''Return the precise average of two values.
440 @arg a: One (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
441 @arg b: Other (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
442 @kwarg f: Optional fraction (C{float}).
443 @kwarg nonfinites: Optional setting, see function L{fma}.
445 @return: M{a + f * (b - a)} (C{float}).
446 '''
447 F = fma(f, (b - a), a, nonfinites=nonfinites)
448 return float(F)
451def fdot(xs, *ys, **start_f2product_nonfinites):
452 '''Return the precision dot product M{start + sum(xs[i] * ys[i] for i in range(len(xs)))}.
454 @arg xs: Iterable of values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
455 @arg ys: Other values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all positional.
456 @kwarg start_f2product_nonfinites: Optional bias C{B{start}=0} (C{scalar}, an
457 L{Fsum} or L{Fsum2Tuple}) and settings C{B{f2product}=None} (C{bool})
458 and C{B{nonfinites=True}} (C{bool}), see class L{Fsum<Fsum.__init__>}.
460 @return: Dot product (C{float}).
462 @raise LenError: Unequal C{len(B{xs})} and C{len(B{ys})}.
464 @see: Class L{Fdot}, U{Algorithm 5.10 B{DotK}
465 <https://www.TUHH.De/ti3/paper/rump/OgRuOi05.pdf>} and function
466 C{math.sumprod} in Python 3.12 and later.
467 '''
468 D = Fdot(xs, *ys, **_xkwds(start_f2product_nonfinites, nonfinites=True))
469 return float(D)
472def fdot_(*xys, **start_f2product_nonfinites):
473 '''Return the (precision) dot product M{start + sum(xys[i] * xys[i+1] for i in range(0, len(xys), B{2}))}.
475 @arg xys: Pairwise values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all positional.
477 @see: Function L{fdot} for further details.
479 @return: Dot product (C{float}).
480 '''
481 D = Fdot_(*xys, **_xkwds(start_f2product_nonfinites, nonfinites=True))
482 return float(D)
485def fdot3(xs, ys, zs, **start_f2product_nonfinites):
486 '''Return the (precision) dot product M{start + sum(xs[i] * ys[i] * zs[i] for i in range(len(xs)))}.
488 @arg xs: X values iterable (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
489 @arg ys: Y values iterable (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
490 @arg zs: Z values iterable (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
492 @see: Function L{fdot} for further details.
494 @return: Dot product (C{float}).
496 @raise LenError: Unequal C{len(B{xs})}, C{len(B{ys})} and/or C{len(B{zs})}.
497 '''
498 n = len(xs)
499 if not n == len(ys) == len(zs):
500 raise LenError(fdot3, xs=n, ys=len(ys), zs=len(zs))
502 D = Fdot((), **_xkwds(start_f2product_nonfinites, nonfinites=True))
503 kwds = dict(f2product=D.f2product(), nonfinites=D.nonfinites())
504 _f = Fsum(**kwds)
505 D = D._facc(_f(x).f2mul_(y, z, **kwds) for x, y, z in zip(xs, ys, zs))
506 return float(D)
509if _zip is zip: # Python 3.9-
510 _fdotf = fdot
511else:
512 from pygeodesy.fsums import _fsum # math.fsum
514 def _fdotf(xs, *ys): # in .datums, .ecef
515 '''(INTERNAL) Dot product for C{float} tuples of matching C{len}gths.
516 '''
517 return _fsum(x * y for x, y in _zip(xs, ys)) # strict=True
520def fhorner(x, *cs, **incx):
521 '''Horner form evaluation of polynomial M{sum(cs[i] * x**i for i=0..n)} as
522 in- or decreasing exponents M{sum(... i=n..0)}, where C{n = len(cs) - 1}.
524 @return: Horner sum (C{float}).
526 @see: Class L{Fhorner<Fhorner.__init__>} for further details.
527 '''
528 H = Fhorner(x, *cs, **incx)
529 return float(H)
532def fidw(xs, ds, beta=2):
533 '''Interpolate using U{Inverse Distance Weighting
534 <https://WikiPedia.org/wiki/Inverse_distance_weighting>} (IDW).
536 @arg xs: Known values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
537 @arg ds: Non-negative distances (each C{scalar}, an L{Fsum} or
538 L{Fsum2Tuple}).
539 @kwarg beta: Inverse distance power (C{int}, 0, 1, 2, or 3).
541 @return: Interpolated value C{x} (C{float}).
543 @raise LenError: Unequal or zero C{len(B{ds})} and C{len(B{xs})}.
545 @raise TypeError: An invalid B{C{ds}} or B{C{xs}}.
547 @raise ValueError: Invalid B{C{beta}}, negative B{C{ds}} or
548 weighted B{C{ds}} below L{EPS}.
550 @note: Using C{B{beta}=0} returns the mean of B{C{xs}}.
551 '''
552 n, xs = len2(xs)
553 if n > 1:
554 b = -Int_(beta=beta, low=0, high=3)
555 if b < 0:
556 try: # weighted
557 _d, W, X = (Fsum() for _ in range(3))
558 for i, d in enumerate(_xiterable(ds)):
559 x = xs[i]
560 D = _d(d)
561 if D < EPS0:
562 if D < 0:
563 raise ValueError(_negative_)
564 x = float(x)
565 i = n
566 break
567 if D.fpow(b):
568 W += D
569 X += D.fmul(x)
570 else:
571 x = X.fover(W, raiser=False)
572 i += 1 # len(xs) >= len(ds)
573 except IndexError:
574 i += 1 # len(xs) < i < len(ds)
575 except Exception as X:
576 _I = Fmt.INDEX
577 raise _xError(X, _I(xs=i), x,
578 _I(ds=i), d)
579 else: # b == 0
580 x = fsum(xs) / n # fmean(xs)
581 i = n
582 elif n:
583 x = float(xs[0])
584 i = n
585 else:
586 x = _0_0
587 i, _ = len2(ds)
588 if i != n:
589 raise LenError(fidw, xs=n, ds=i)
590 return x
593try:
594 from math import fma as _fma # in .resections
596 def _fhornerf(x, *cs, **incx):
597 '''(INTERNAL) Horner form for C{float} coefficients C{cs} and C{x}
598 and C{B{incx}=True} in- or decreasing with exponents=.
599 '''
600 h = _0_0
601 if cs:
602 if _xkwds_get(incx, incx=True):
603 cs = reversed(cs)
604 for c in cs:
605 h = _fma(x, h, c) if h else c
606 return h
608except ImportError: # PYCHOK DSPACE!
609 _fhornerf = fhorner # PYCHOK redef
611 def _fma(x, y, z): # no need for accuracy
612 return x * y + z
615def fma(x, y, z, **nonfinites): # **raiser
616 '''Fused-multiply-add, using C{math.fma(x, y, z)} in Python 3.13+
617 or an equivalent implementation.
619 @arg x: Multiplicand (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
620 @arg y: Multiplier (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
621 @arg z: Addend (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
622 @kwarg nonfinites: Use C{B{nonfinites}=True} or C{=False},
623 to override default L{nonfiniterrors}
624 (C{bool}), see method L{Fsum.fma}.
626 @return: C{(x * y) + z} (C{float} or L{Fsum}).
627 '''
628 F, raiser = _Fm2(x, **nonfinites)
629 return F.fma(y, z, **raiser).as_iscalar
632def _Fm2(x, nonfinites=None, **raiser):
633 '''(INTERNAL) Handle C{fma} and C{f2mul} DEPRECATED C{raiser=False}.
634 '''
635 return Fsum(x, nonfinites=nonfinites), raiser
638def fmean(xs, nonfinites=True):
639 '''Compute the accurate mean M{sum(xs) / len(xs)}.
641 @arg xs: Values (each C{scalar}, or L{Fsum} or L{Fsum2Tuple}).
643 @return: Mean value (C{float}).
645 @raise LenError: No B{C{xs}} values.
647 @raise OverflowError: Partial C{2sum} overflow.
648 '''
649 n, xs = len2(xs)
650 if n < 1:
651 raise LenError(fmean, xs=xs)
652 M = Fsum(*xs, nonfinites=nonfinites)
653 return M.fover(n) if n > 1 else float(M)
656def fmean_(*xs, **nonfinites):
657 '''Compute the accurate mean M{sum(xs) / len(xs)}.
659 @see: Function L{fmean} for further details.
660 '''
661 return fmean(xs, **nonfinites)
664def f2mul_(x, *ys, **nonfinites): # **raiser
665 '''Cascaded, accurate multiplication C{B{x} * B{y} * B{y} ...} for all B{C{ys}}.
667 @arg x: Multiplicand (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
668 @arg ys: Multipliers (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all
669 positional.
670 @kwarg nonfinites: Use C{B{nonfinites}=True} or C{=False}, to override default
671 L{nonfiniterrors} (C{bool}), see method L{Fsum.f2mul_}.
673 @return: The cascaded I{TwoProduct} (C{float}, C{int} or L{Fsum}).
675 @see: U{Equations 2.3<https://www.TUHH.De/ti3/paper/rump/OzOgRuOi06.pdf>}
676 '''
677 F, raiser = _Fm2(x, **nonfinites)
678 return F.f2mul_(*ys, **raiser).as_iscalar
681def fpolynomial(x, *cs, **over_f2product_nonfinites):
682 '''Evaluate the polynomial M{sum(cs[i] * x**i for i=0..len(cs)) [/ over]}.
684 @kwarg over_f2product_nonfinites: Optional final divisor C{B{over}=None}
685 (I{non-zero} C{scalar}) and other settings, see class
686 L{Fpolynomial<Fpolynomial.__init__>}.
688 @return: Polynomial value (C{float} or L{Fpolynomial}).
689 '''
690 d, kwds = _xkwds_pop2(over_f2product_nonfinites, over=0)
691 P = Fpolynomial(x, *cs, **kwds)
692 return P.fover(d) if d else float(P)
695def fpowers(x, n, alts=0):
696 '''Return a series of powers M{[x**i for i=1..n]}, note I{1..!}
698 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
699 @arg n: Highest exponent (C{int}).
700 @kwarg alts: Only alternating powers, starting with this
701 exponent (C{int}).
703 @return: Tuple of powers of B{C{x}} (each C{type(B{x})}).
705 @raise TypeError: Invalid B{C{x}} or B{C{n}} not C{int}.
707 @raise ValueError: Non-finite B{C{x}} or invalid B{C{n}}.
708 '''
709 if not isint(n):
710 raise _IsnotError(typename(int), n=n)
711 elif n < 1:
712 raise _ValueError(n=n)
714 p = x if isscalar(x) or _isFsum_2Tuple(x) else _2float(x=x)
715 ps = tuple(_powers(p, n))
717 if alts > 0: # x**2, x**4, ...
718 # ps[alts-1::2] chokes PyChecker
719 ps = ps[slice(alts-1, None, 2)]
721 return ps
724try:
725 from math import prod as fprod # Python 3.8
726except ImportError:
728 def fprod(xs, start=1):
729 '''Iterable product, like C{math.prod} or C{numpy.prod}.
731 @arg xs: Iterable of values to be multiplied (each
732 C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
733 @kwarg start: Initial value, also the value returned
734 for an empty B{C{xs}} (C{scalar}).
736 @return: The product (C{float} or L{Fsum}).
738 @see: U{NumPy.prod<https://docs.SciPy.org/doc/
739 numpy/reference/generated/numpy.prod.html>}.
740 '''
741 return freduce(_operator.mul, xs, start)
744def frandoms(n, seeded=None):
745 '''Generate C{n} (long) lists of random C{floats}.
747 @arg n: Number of lists to generate (C{int}, non-negative).
748 @kwarg seeded: If C{scalar}, use C{random.seed(B{seeded})} or
749 if C{True}, seed using today's C{year-day}.
751 @see: U{Hettinger<https://GitHub.com/ActiveState/code/tree/master/recipes/
752 Python/393090_Binary_floating_point_summatiaccurate_full/recipe-393090.py>}.
753 '''
754 from random import gauss, random, seed, shuffle
756 if seeded is None:
757 pass
758 elif seeded and isbool(seeded):
759 from time import localtime
760 seed(localtime().tm_yday)
761 elif isscalar(seeded):
762 seed(seeded)
764 c = (7, 1e100, -7, -1e100, -9e-20, 8e-20) * 7
765 for _ in range(n):
766 s = 0
767 t = list(c)
768 _a = t.append
769 for _ in range(n * 8):
770 v = gauss(0, random())**7 - s
771 _a(v)
772 s += v
773 shuffle(t)
774 yield t
777def frange(start, number, step=1):
778 '''Generate a range of C{float}s.
780 @arg start: First value (C{float}).
781 @arg number: The number of C{float}s to generate (C{int}).
782 @kwarg step: Increment value (C{float}).
784 @return: A generator (C{float}s).
786 @see: U{NumPy.prod<https://docs.SciPy.org/doc/
787 numpy/reference/generated/numpy.arange.html>}.
788 '''
789 if not isint(number):
790 raise _IsnotError(typename(int), number=number)
791 for i in range(number):
792 yield start + (step * i)
795try:
796 from functools import reduce as freduce
797except ImportError:
798 try:
799 freduce = reduce # PYCHOK expected
800 except NameError: # Python 3+
802 def freduce(f, xs, *start):
803 '''For missing C{functools.reduce}.
804 '''
805 if start:
806 r = v = start[0]
807 else:
808 r, v = 0, MISSING
809 for v in xs:
810 r = f(r, v)
811 if v is MISSING:
812 raise _TypeError(xs=(), start=MISSING)
813 return r
816def fremainder(x, y):
817 '''Remainder in range C{[-B{y / 2}, B{y / 2}]}.
819 @arg x: Numerator (C{scalar}).
820 @arg y: Modulus, denominator (C{scalar}).
822 @return: Remainder (C{scalar}, preserving signed
823 0.0) or C{NAN} for any non-finite B{C{x}}.
825 @raise ValueError: Infinite or near-zero B{C{y}}.
827 @see: I{Karney}'s U{Math.remainder<https://PyPI.org/
828 project/geographiclib/>} and Python 3.7+
829 U{math.remainder<https://docs.Python.org/3/
830 library/math.html#math.remainder>}.
831 '''
832 # with Python 2.7.16 and 3.7.3 on macOS 10.13.6 and
833 # with Python 3.10.2 on macOS 12.2.1 M1 arm64 native
834 # fmod( 0, 360) == 0.0
835 # fmod( 360, 360) == 0.0
836 # fmod(-0, 360) == 0.0
837 # fmod(-0.0, 360) == -0.0
838 # fmod(-360, 360) == -0.0
839 # however, using the % operator ...
840 # 0 % 360 == 0
841 # 360 % 360 == 0
842 # 360.0 % 360 == 0.0
843 # -0 % 360 == 0
844 # -360 % 360 == 0 == (-360) % 360
845 # -0.0 % 360 == 0.0 == (-0.0) % 360
846 # -360.0 % 360 == 0.0 == (-360.0) % 360
848 # On Windows 32-bit with python 2.7, math.fmod(-0.0, 360)
849 # == +0.0. This fixes this bug. See also Math::AngNormalize
850 # in the C++ library, Math.sincosd has a similar fix.
851 if isfinite(x):
852 try:
853 r = remainder(x, y) if x else x
854 except Exception as e:
855 raise _xError(e, unstr(fremainder, x, y))
856 else: # handle x INF and NINF as NAN
857 r = NAN
858 return r
861if _MODS.sys_version_info2 < (3, 8): # PYCHOK no cover
862 from math import hypot # OK in Python 3.7-
864 def hypot_(*xs):
865 '''Compute the norm M{sqrt(sum(x**2 for x in xs))}.
867 Similar to Python 3.8+ n-dimension U{math.hypot
868 <https://docs.Python.org/3.8/library/math.html#math.hypot>},
869 but exceptions, C{nan} and C{infinite} values are
870 handled differently.
872 @arg xs: X arguments (C{scalar}s), all positional.
874 @return: Norm (C{float}).
876 @raise OverflowError: Partial C{2sum} overflow.
878 @raise ValueError: Invalid or no B{C{xs}} values.
880 @note: The Python 3.8+ Euclidian distance U{math.dist
881 <https://docs.Python.org/3.8/library/math.html#math.dist>}
882 between 2 I{n}-dimensional points I{p1} and I{p2} can be
883 computed as M{hypot_(*((c1 - c2) for c1, c2 in zip(p1, p2)))},
884 provided I{p1} and I{p2} have the same, non-zero length I{n}.
885 '''
886 return float(_Hypot(*xs))
888elif _MODS.sys_version_info2 < (3, 10): # PYCHOK no cover
889 # In Python 3.8 and 3.9 C{math.hypot} is inaccurate, see
890 # U{agdhruv<https://GitHub.com/geopy/geopy/issues/466>},
891 # U{cffk<https://Bugs.Python.org/issue43088>} and module
892 # U{geomath.py<https://PyPI.org/project/geographiclib/1.52>}
894 def hypot(x, y):
895 '''Compute the norm M{sqrt(x**2 + y**2)}.
897 @arg x: X argument (C{scalar}).
898 @arg y: Y argument (C{scalar}).
900 @return: C{sqrt(B{x}**2 + B{y}**2)} (C{float}).
901 '''
902 return float(_Hypot(x, y))
904 from math import hypot as hypot_ # PYCHOK in Python 3.8 and 3.9
905else:
906 from math import hypot # PYCHOK in Python 3.10+
907 hypot_ = hypot
910def _Hypot(*xs):
911 '''(INTERNAL) Substitute for inaccurate C{math.hypot}.
912 '''
913 return Fhypot(*xs, nonfinites=True, raiser=False) # f2product=True
916def hypot1(x):
917 '''Compute the norm M{sqrt(1 + x**2)}.
919 @arg x: Argument (C{scalar} or L{Fsum} or L{Fsum2Tuple}).
921 @return: Norm (C{float} or L{Fhypot}).
922 '''
923 h = _1_0
924 if x:
925 if _isFsum_2Tuple(x):
926 h = _Hypot(h, x)
927 h = float(h)
928 else:
929 h = hypot(h, x)
930 return h
933def hypot2(x, y, *xy0):
934 '''Compute the I{squared} norm M{x**2 + y**2}.
936 @arg x: X (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
937 @arg y: Y (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
938 @arg xy0: Optional reference C{(x0, y0)} (each C{scalar},
939 an L{Fsum} or L{Fsum2Tuple}).
941 @return: C{B{x}**2 + B{y}**2} (C{float}).
942 '''
943 x, y = _map0(abs, x, y, *xy0) # NOT fabs!
944 if x < y:
945 x, y = y, x
946 h2 = x**2
947 if h2 and y:
948 h2 *= (y / x)**2 + _1_0
949 return float(h2)
952def hypot2_(*xs):
953 '''Compute the I{squared} norm C{fsum(x**2 for x in B{xs})}.
955 @arg xs: Components (each C{scalar}, an L{Fsum} or
956 L{Fsum2Tuple}), all positional.
958 @return: Squared norm (C{float}).
960 @see: Class L{Fpowers} for further details.
961 '''
962 h2 = float(max(map(abs, xs))) if xs else _0_0
963 if h2: # and isfinite(h2)
964 _h = _1_0 / h2
965 xs = ((x * _h) for x in xs)
966 H2 = Fpowers(2, *xs, nonfinites=True) # f2product=True
967 h2 = H2.fover(_h**2)
968 return h2
971def _map0(_f, x, y, x0=_0_0, y0=_0_0, *unused):
972 return _f(x - x0), _f(y - y0)
975def norm2(x, y, *xy0):
976 '''Normalize a 2-dimensional vector.
978 @arg x: X component (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
979 @arg y: Y component (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
980 @arg xy0: Optional reference C{(x0, y0)} (each C{scalar}, an
981 L{Fsum} or L{Fsum2Tuple}).
983 @return: 2-Tuple C{(x, y)}, normalized.
985 @raise ValueError: Invalid B{C{x}} or B{C{y}}.
986 '''
987 try:
988 h = None
989 x, y = _map0(float, x, y, *xy0)
990 h = hypot(x, y)
991 if h:
992 t = (x / h), (y / h)
993 else:
994 t = (_copysign_0_0(x), # pass?
995 _copysign_0_0(y))
996 except Exception as X:
997 raise _xError(X, x=x, y=y, h=h)
998 return t
1001def norm_(*xs):
1002 '''Normalize the components of an n-dimensional vector.
1004 @arg xs: Components (each C{scalar}, an L{Fsum} or
1005 L{Fsum2Tuple}), all positional.
1007 @return: Yield each component, normalized.
1009 @raise ValueError: Invalid or insufficent B{C{xs}}
1010 or zero norm.
1011 '''
1012 try:
1013 i = h = None
1014 x = xs
1015 h = hypot_(*xs)
1016 _h = (_1_0 / h) if h else _0_0
1017 for i, x in enumerate(xs):
1018 yield x * _h
1019 except Exception as X:
1020 raise _xsError(X, xs, i, x, h=h)
1023def polar2(x, y, *xy0):
1024 '''Return 2-tuple C{(length, angle)} with C{angle} in radians M{[0..+PI2)}.
1026 @arg x: X (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1027 @arg y: Y (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1028 @arg xy0: Optional reference C{(x0, y0)} (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1030 @note: Use C{polar(B{y}, B{x}, *B{yx0})} to get the angle as C{bearing} from North.
1031 '''
1032 x, y = _map0(float, x, y, *xy0)
1033 return hypot(x, y), _MODS.utily.atan2p(y, x)
1036def polar2d(x, y, *xy0):
1037 '''Return 2-tuple C{(length, angle)} with C{angle} in degrees M{[0..+360)}.
1039 @arg x: X (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1040 @arg y: Y (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1041 @arg xy0: Optional reference C{(x0, y0)} (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1043 @note: Use C{polar2d(B{y}, B{x}, *B{yx0})} to get the angle as C{bearing} from North.
1044 '''
1045 x, y = _map0(float, x, y, *xy0)
1046 return hypot(x, y), _MODS.utily.atan2b(y, x)
1049def _powers(x, n):
1050 '''(INTERNAL) Yield C{x**i for i=1..n}.
1051 '''
1052 p = 1 # type(p) == type(x)
1053 for _ in range(n):
1054 p *= x
1055 yield p
1058def _root(x, p, where):
1059 '''(INTERNAL) Raise C{x} to power C{0 <= p < 1}.
1060 '''
1061 try:
1062 if x > 0:
1063 r = Fsum(f2product=True, nonfinites=True)(x)
1064 return r.fpow(p).as_iscalar
1065 elif x < 0:
1066 raise ValueError(_negative_)
1067 except Exception as X:
1068 raise _xError(X, unstr(where, x))
1069 return _0_0 if p else _1_0 # x == 0
1072def sqrt0(x, Error=None):
1073 '''Return the square root C{sqrt(B{x})} iff C{B{x} > }L{EPS02},
1074 preserving C{type(B{x})}.
1076 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1077 @kwarg Error: Error to raise for negative B{C{x}}.
1079 @return: Square root (C{float} or L{Fsum}) or C{0.0}.
1081 @raise TypeeError: Invalid B{C{x}}.
1083 @note: Any C{B{x} < }L{EPS02} I{including} C{B{x} < 0}
1084 returns C{0.0}.
1085 '''
1086 if Error and x < 0:
1087 raise Error(unstr(sqrt0, x))
1088 return _root(x, _0_5, sqrt0) if x > EPS02 else (
1089 _0_0 if x < EPS02 else EPS0)
1092def sqrt3(x):
1093 '''Return the square root, I{cubed} M{sqrt(x)**3} or M{sqrt(x**3)},
1094 preserving C{type(B{x})}.
1096 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1098 @return: Square root I{cubed} (C{float} or L{Fsum}).
1100 @raise TypeeError: Invalid B{C{x}}.
1102 @raise ValueError: Negative B{C{x}}.
1104 @see: Functions L{cbrt} and L{cbrt2}.
1105 '''
1106 return _root(x, _1_5, sqrt3)
1109def sqrt_a(h, b):
1110 '''Compute the C{I{a}} side of a right-angled triangle from
1111 C{sqrt(B{h}**2 - B{b}**2)}.
1113 @arg h: Hypotenuse or outer annulus radius (C{scalar}).
1114 @arg b: Triangle side or inner annulus radius (C{scalar}).
1116 @return: C{copysign(I{a}, B{h})} or C{unsigned 0.0} (C{float}).
1118 @raise TypeError: Non-scalar B{C{h}} or B{C{b}}.
1120 @raise ValueError: If C{abs(B{h}) < abs(B{b})}.
1122 @see: Inner tangent chord B{I{d}} of an U{annulus
1123 <https://WikiPedia.org/wiki/Annulus_(mathematics)>}
1124 and function U{annulus_area<https://People.SC.FSU.edu/
1125 ~jburkardt/py_src/geometry/geometry.py>}.
1126 '''
1127 try:
1128 if not (_isHeight(h) and _isRadius(b)):
1129 raise TypeError(_not_scalar_)
1130 c = fabs(h)
1131 if c > EPS0:
1132 s = _1_0 - (b / c)**2
1133 if s < 0:
1134 raise ValueError(_h_lt_b_)
1135 a = (sqrt(s) * c) if 0 < s < 1 else (c if s else _0_0)
1136 else: # PYCHOK no cover
1137 b = fabs(b)
1138 d = c - b
1139 if d < 0:
1140 raise ValueError(_h_lt_b_)
1141 d *= c + b
1142 a = sqrt(d) if d else _0_0
1143 except Exception as x:
1144 raise _xError(x, h=h, b=b)
1145 return copysign0(a, h)
1148def zcrt(x):
1149 '''Return the 6-th, I{zenzi-cubic} root, M{x**(1 / 6)},
1150 preserving C{type(B{x})}.
1152 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1154 @return: I{Zenzi-cubic} root (C{float} or L{Fsum}).
1156 @see: Functions L{bqrt} and L{zqrt}.
1158 @raise TypeeError: Invalid B{C{x}}.
1160 @raise ValueError: Negative B{C{x}}.
1161 '''
1162 return _root(x, _1_6th, zcrt)
1165def zqrt(x):
1166 '''Return the 8-th, I{zenzi-quartic} or I{squared-quartic} root,
1167 M{x**(1 / 8)}, preserving C{type(B{x})}.
1169 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1171 @return: I{Zenzi-quartic} root (C{float} or L{Fsum}).
1173 @see: Functions L{bqrt} and L{zcrt}.
1175 @raise TypeeError: Invalid B{C{x}}.
1177 @raise ValueError: Negative B{C{x}}.
1178 '''
1179 return _root(x, _0_125, zqrt)
1181# **) MIT License
1182#
1183# Copyright (C) 2016-2026 -- mrJean1 at Gmail -- All Rights Reserved.
1184#
1185# Permission is hereby granted, free of charge, to any person obtaining a
1186# copy of this software and associated documentation files (the "Software"),
1187# to deal in the Software without restriction, including without limitation
1188# the rights to use, copy, modify, merge, publish, distribute, sublicense,
1189# and/or sell copies of the Software, and to permit persons to whom the
1190# Software is furnished to do so, subject to the following conditions:
1191#
1192# The above copyright notice and this permission notice shall be included
1193# in all copies or substantial portions of the Software.
1194#
1195# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1196# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1197# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1198# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1199# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1200# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1201# OTHER DEALINGS IN THE SOFTWARE.