Coverage for pygeodesy/fmath.py: 94%
277 statements
« prev ^ index » next coverage.py v7.2.2, created at 2024-04-19 10:22 -0400
« prev ^ index » next coverage.py v7.2.2, created at 2024-04-19 10:22 -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, isint, len2
10from pygeodesy.constants import EPS0, EPS02, EPS1, NAN, PI, PI_2, PI_4, \
11 _0_0, _0_125, _0_25, _0_5, _1_0, _1_3rd, \
12 _1_5, _1_6th, _2_0, _2_3rd, _3_0, \
13 _copysign_0_0, _isfinite, _over, remainder
14from pygeodesy.errors import _IsnotError, LenError, _TypeError, _ValueError, \
15 _xError, _xkwds_get, _xkwds_pop2
16from pygeodesy.fsums import _2float, Fsum, _fsum, fsum, fsum1_, _1primed, \
17 Fmt, unstr
18from pygeodesy.interns import MISSING, _few_, _h_, _invokation_, _negative_, \
19 _not_scalar_, _SPACE_, _too_
20from pygeodesy.lazily import _ALL_LAZY, _sys_version_info2
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__ = '24.04.17'
30# sqrt(2) <https://WikiPedia.org/wiki/Square_root_of_2>
31_0_4142 = 0.41421356237309504880 # ... sqrt(2) - 1
32_h_lt_b_ = 'abs(h) < abs(b)'
35def _Fsum__init__(inst, raiser=MISSING, **name_RESIDUAL):
36 '''(INTERNAL) Init an C{Fsum} instance.
37 '''
38 Fsum.__init__(inst, **name_RESIDUAL) # PYCHOK self
39 inst._fset_ps(_0_0)
40 return {} if raiser is MISSING else dict(raiser=raiser)
43class Fdot(Fsum):
44 '''Precision dot product.
45 '''
46 def __init__(self, a, *b, **name_RESIDUAL):
47 '''New L{Fdot} precision dot product M{sum(a[i] * b[i]
48 for i=0..len(a)-1)}.
50 @arg a: Iterable, list, tuple, etc. (C{scalar}s).
51 @arg b: Other values (C{scalar}s), all positional.
52 @kwarg name_RESIDUAL: Optional C{B{name}=NN} and C{B{RESIDUAL}=None},
53 see L{Fsum<Fsum.__init__>}.
55 @raise OverflowError: Partial C{2sum} overflow.
57 @raise LenError: Unequal C{len(B{a})} and C{len(B{b})}.
59 @see: Function L{fdot} and method L{Fsum.fadd}.
60 '''
61 Fsum.__init__(self, **name_RESIDUAL)
62 self.fadd(_map_mul(a, b, Fdot))
65class Fhorner(Fsum):
66 '''Precision polynomial evaluation using the Horner form.
67 '''
68 def __init__(self, x, *cs, **name_RESIDUAL):
69 '''New L{Fhorner} evaluation of polynomial M{sum(cs[i] * x**i
70 for i=0..len(cs)-1)}.
72 @arg x: Polynomial argument (C{scalar} or C{Fsum} instance).
73 @arg cs: Polynomial coeffients (C{scalar} or C{Fsum}
74 instances), all positional.
75 @kwarg name_RESIDUAL: Optional C{B{name}=NN} and C{B{RESIDUAL}=None},
76 see L{Fsum<Fsum.__init__>}.
78 @raise OverflowError: Partial C{2sum} overflow.
80 @raise TypeError: Non-scalar B{C{x}}.
82 @raise ValueError: Non-finite B{C{x}}.
84 @see: Function L{fhorner} and methods L{Fsum.fadd} and
85 L{Fsum.fmul}.
86 '''
87 Fsum.__init__(self, **name_RESIDUAL)
88 if cs:
89 if isinstance(x, Fsum):
90 _mul = self._mul_Fsum
91 else:
92 _mul = self._mul_scalar
93 x = _2float(x=x)
94 op = Fhorner.__name__
95 if len(cs) > 1 and x:
96 for c in reversed(cs):
97 self._fset_ps(_mul(x, op))
98 self._fadd(c, op, up=False)
99 self._update()
100 else: # x == 0
101 self._fadd(cs[0], op)
102 else:
103 self._fset_ps(_0_0)
106class Fhypot(Fsum):
107 '''Precision summation and hypotenuse, default C{power=2}.
108 '''
109 def __init__(self, *xs, **root_name_RESIDUAL_raiser):
110 '''New L{Fhypot} hypotenuse of (the I{root} of) several components.
112 @arg xs: One or more components (each a C{scalar} or an C{Fsum} instance).
113 @kwarg root_name_RESIDUAL_raiser: Optional, exponent and C{B{root}=2} order,
114 C{B{name}=NN}, C{B{RESIDUAL}=None} and C{B{raiser}=True}, see
115 class L{Fsum<Fsum.__init__>} and method L{root<Fsum.root>}.
116 '''
117 r = None # _xkwds_pop2 error
118 try:
119 r, kwds = _xkwds_pop2(root_name_RESIDUAL_raiser, root=2)
120 r, kwds = _xkwds_pop2(kwds, power=r) # for backward compatibility
121 raiser = _Fsum__init__(self, **kwds)
122 if xs:
123 self._facc_power(r, xs, Fhypot, **raiser)
124 self._fset(self.root(r, **raiser))
125 except Exception as X:
126 raise self._ErrorXs(X, xs, root=r)
129class Fpolynomial(Fsum):
130 '''Precision polynomial evaluation.
131 '''
132 def __init__(self, x, *cs, **name_RESIDUAL):
133 '''New L{Fpolynomial} evaluation of the polynomial
134 M{sum(cs[i] * x**i for i=0..len(cs)-1)}.
136 @arg x: Polynomial argument (C{scalar} or L{Fsum}).
137 @arg cs: Polynomial coeffients (each a C{scalar} or an L{Fsum} instance),
138 all positional.
139 @kwarg name_RESIDUAL: Optional C{B{name}=NN} and C{B{RESIDUAL}=None},
140 see L{Fsum<Fsum.__init__>}.
142 @raise OverflowError: Partial C{2sum} overflow.
144 @raise TypeError: Non-scalar B{C{x}}.
146 @raise ValueError: Non-finite B{C{x}}.
148 @see: Class L{Fhorner}, function L{fpolynomial} and
149 method L{Fsum.fadd}.
150 '''
151 Fsum.__init__(self, *cs[:1], **name_RESIDUAL)
152 n = len(cs) - 1
153 if n > 0:
154 self.fadd(_1map_mul(cs[1:], _powers(x, n)))
155 elif n < 0:
156 self._fset_ps(_0_0)
159class Fpowers(Fsum):
160 '''Precision summation of powers, optimized for C{power=2, 3 and 4}.
161 '''
162 def __init__(self, power, *xs, **name_RESIDUAL_raiser):
163 '''New L{Fpowers} sum of (the I{power} of) several values.
165 @arg power: The exponent (C{scalar} or L{Fsum}).
166 @arg xs: One or more values (each a C{scalar} or an C{Fsum} instance).
167 @kwarg name_RESIDUAL_raiser: Optional C{B{name}=NN}, C{B{RESIDUAL}=None} and
168 C{B{raiser}=True}, see L{Fsum<Fsum.__init__>} and L{fpow<Fsum.fpow>}.
169 '''
170 try:
171 raiser = _Fsum__init__(self, **name_RESIDUAL_raiser)
172 if xs:
173 self._facc_power(power, xs, Fpowers, **raiser) # x**0 == 1
174 except Exception as X:
175 raise self._ErrorXs(X, xs, power=power)
178class Froot(Fsum):
179 '''The root of a precision summation.
180 '''
181 def __init__(self, root, *xs, **name_RESIDUAL_raiser):
182 '''New L{Froot} root of a precision sum.
184 @arg root: The order (C{scalar} or C{Fsum}), non-zero.
185 @arg xs: Values to summate (each a C{scalar} or an C{Fsum} instance).
186 @kwarg name_RESIDUAL_raiser: Optional C{B{name}=NN}, C{B{RESIDUAL}=None} and
187 C{B{raiser}=True}, see L{Fsum<Fsum.__init__>} and L{fpow<Fsum.fpow>}.
188 '''
189 try:
190 raiser = _Fsum__init__(self, **name_RESIDUAL_raiser)
191 if xs:
192 self. fadd(xs)
193 self._fset(self.root(root, **raiser))
194 except Exception as X:
195 raise self._ErrorXs(X, xs, root=root)
198class Fcbrt(Froot):
199 '''Cubic root of a precision summation.
200 '''
201 def __init__(self, *xs, **name_RESIDUAL_raiser):
202 '''New L{Fcbrt} cubic root of a precision sum.
204 @see: Class L{Froot} for further details.
205 '''
206 Froot.__init__(self, _3_0, *xs, **name_RESIDUAL_raiser)
209class Fsqrt(Froot):
210 '''Square root of a precision summation.
211 '''
212 def __init__(self, *xs, **name_RESIDUAL_raiser):
213 '''New L{Fsqrt} square root of a precision sum.
215 @see: Class L{Froot} for further details.
216 '''
217 Froot.__init__(self, _2_0, *xs, **name_RESIDUAL_raiser)
220def bqrt(x):
221 '''Return the 4-th, I{bi-quadratic} or I{quartic} root, M{x**(1 / 4)}.
223 @arg x: Value (C{scalar}).
225 @return: I{Quartic} root (C{float}).
227 @raise ValueError: Negative B{C{x}}.
229 @see: Functions L{zcrt} and L{zqrt}.
230 '''
231 return _root(x, _0_25, bqrt)
234try:
235 from math import cbrt # Python 3.11+
237 def cbrt2(x):
238 '''Compute the cube root I{squared} M{x**(2/3)}.
239 '''
240 return cbrt(x)**2 # cbrt(-0.0*2) == -0.0
242except ImportError: # Python 3.10-
244 def cbrt(x):
245 '''Compute the cube root M{x**(1/3)}.
247 @arg x: Value (C{scalar}).
249 @return: Cubic root (C{float}).
251 @see: Functions L{cbrt2} and L{sqrt3}.
252 '''
253 # <https://archive.lib.MSU.edu/crcmath/math/math/r/r021.htm>
254 # simpler and more accurate than Ken Turkowski's CubeRoot, see
255 # <https://People.FreeBSD.org/~lstewart/references/apple_tr_kt32_cuberoot.pdf>
256 return _copysign(pow(fabs(x), _1_3rd), x) # cbrt(-0.0) == -0.0
258 def cbrt2(x): # PYCHOK attr
259 '''Compute the cube root I{squared} M{x**(2/3)}.
261 @arg x: Value (C{scalar}).
263 @return: Cube root I{squared} (C{float}).
265 @see: Functions L{cbrt} and L{sqrt3}.
266 '''
267 return pow(fabs(x), _2_3rd) # XXX pow(fabs(x), _1_3rd)**2
270def euclid(x, y):
271 '''I{Appoximate} the norm M{sqrt(x**2 + y**2)} by
272 M{max(abs(x), abs(y)) + min(abs(x), abs(y)) * 0.4142...}.
274 @arg x: X component (C{scalar}).
275 @arg y: Y component (C{scalar}).
277 @return: Appoximate norm (C{float}).
279 @see: Function L{euclid_}.
280 '''
281 x, y = fabs(x), fabs(y)
282 if x < y:
283 x, y = y, x
284 return x + y * _0_4142 # XXX * _0_5 before 20.10.02
287def euclid_(*xs):
288 '''I{Appoximate} the norm M{sqrt(sum(x**2 for x in xs))}
289 by cascaded L{euclid}.
291 @arg xs: X arguments, positional (C{scalar}s).
293 @return: Appoximate norm (C{float}).
295 @see: Function L{euclid}.
296 '''
297 e = _0_0
298 for x in sorted(map(fabs, xs)): # XXX not reverse=True
299 # e = euclid(x, e)
300 if e < x:
301 e, x = x, e
302 if x:
303 e += x * _0_4142
304 return e
307def facos1(x):
308 '''Fast approximation of L{pygeodesy.acos1}C{(B{x})}.
310 @see: U{ShaderFastLibs.h<https://GitHub.com/michaldrobot/
311 ShaderFastLibs/blob/master/ShaderFastMathLib.h>}.
312 '''
313 a = fabs(x)
314 if a < EPS0:
315 r = PI_2
316 elif a < EPS1:
317 H = Fhorner(-a, 1.5707288, 0.2121144, 0.0742610, 0.0187293)
318 r = float(H * sqrt(_1_0 - a))
319 if x < 0:
320 r = PI - r
321 else:
322 r = PI if x < 0 else _0_0
323 return r
326def fasin1(x): # PYCHOK no cover
327 '''Fast approximation of L{pygeodesy.asin1}C{(B{x})}.
329 @see: L{facos1}.
330 '''
331 return PI_2 - facos1(x)
334def fatan(x):
335 '''Fast approximation of C{atan(B{x})}.
336 '''
337 a = fabs(x)
338 if a < _1_0:
339 r = fatan1(a) if a else _0_0
340 elif a > _1_0:
341 r = PI_2 - fatan1(_1_0 / a) # == fatan2(a, _1_0)
342 else:
343 r = PI_4
344 if x < 0: # copysign0(r, x)
345 r = -r
346 return r
349def fatan1(x):
350 '''Fast approximation of C{atan(B{x})} for C{0 <= B{x} <= 1}, I{unchecked}.
352 @see: U{ShaderFastLibs.h<https://GitHub.com/michaldrobot/ShaderFastLibs/
353 blob/master/ShaderFastMathLib.h>} and U{Efficient approximations
354 for the arctangent function<http://www-Labs.IRO.UMontreal.CA/
355 ~mignotte/IFT2425/Documents/EfficientApproximationArctgFunction.pdf>},
356 IEEE Signal Processing Magazine, 111, May 2006.
357 '''
358 # Eq (9): PI_4 * x - x * (abs(x) - 1) * (0.2447 + 0.0663 * abs(x)), for -1 < x < 1
359 # PI_4 * x - (x**2 - x) * (0.2447 + 0.0663 * x), for 0 < x - 1
360 # x * (1.0300981633974482 + x * (-0.1784 - x * 0.0663))
361 H = Fhorner(x, _0_0, 1.0300981634, -0.1784, -0.0663)
362 return float(H)
365def fatan2(y, x):
366 '''Fast approximation of C{atan2(B{y}, B{x})}.
368 @see: U{fastApproximateAtan(x, y)<https://GitHub.com/CesiumGS/cesium/blob/
369 master/Source/Shaders/Builtin/Functions/fastApproximateAtan.glsl>}
370 and L{fatan1}.
371 '''
372 a, b = fabs(x), fabs(y)
373 if b > a:
374 r = (PI_2 - fatan1(a / b)) if a else PI_2
375 elif a > b:
376 r = fatan1(b / a) if b else _0_0
377 elif a: # a == b != 0
378 r = PI_4
379 else: # a == b == 0
380 return _0_0
381 if x < 0:
382 r = PI - r
383 if y < 0: # copysign0(r, y)
384 r = -r
385 return r
388def favg(v1, v2, f=_0_5):
389 '''Return the average of two values.
391 @arg v1: One value (C{scalar}).
392 @arg v2: Other value (C{scalar}).
393 @kwarg f: Optional fraction (C{float}).
395 @return: M{v1 + f * (v2 - v1)} (C{float}).
396 '''
397# @raise ValueError: Fraction out of range.
398# '''
399# if not 0 <= f <= 1: # XXX restrict fraction?
400# raise _ValueError(fraction=f)
401 # v1 + f * (v2 - v1) == v1 * (1 - f) + v2 * f
402 return fsum1_(v1, -f * v1, f * v2)
405def fdot(a, *b):
406 '''Return the precision dot product M{sum(a[i] * b[i] for
407 i=0..len(a))}.
409 @arg a: Iterable, list, tuple, etc. (C{scalar}s).
410 @arg b: All positional arguments (C{scalar}s).
412 @return: Dot product (C{float}).
414 @raise LenError: Unequal C{len(B{a})} and C{len(B{b})}.
416 @see: Class L{Fdot} and U{Algorithm 5.10 B{DotK}
417 <https://www.TUHH.De/ti3/paper/rump/OgRuOi05.pdf>}.
418 '''
419 return fsum(_map_mul(a, b, fdot))
422def fdot3(a, b, c, start=0):
423 '''Return the precision dot product M{start +
424 sum(a[i] * b[i] * c[i] for i=0..len(a)-1)}.
426 @arg a: Iterable, list, tuple, etc. (C{scalar}s).
427 @arg b: Iterable, list, tuple, etc. (C{scalar}s).
428 @arg c: Iterable, list, tuple, etc. (C{scalar}s).
429 @kwarg start: Optional bias (C{scalar}).
431 @return: Dot product (C{float}).
433 @raise LenError: Unequal C{len(B{a})}, C{len(B{b})}
434 and/or C{len(B{c})}.
436 @raise OverflowError: Partial C{2sum} overflow.
437 '''
438 def _mul3(a, b, c): # map function
439 return a * b * c
441 def _mul3_(a, b, c, start):
442 yield start
443 for abc in map(_mul3, a, b, c):
444 yield abc
446 if not len(a) == len(b) == len(c):
447 raise LenError(fdot3, a=len(a), b=len(b), c=len(c))
449 return fsum(_mul3_(a, b, c, start) if start else map(_mul3, a, b, c))
452def fhorner(x, *cs):
453 '''Evaluate the polynomial M{sum(cs[i] * x**i for
454 i=0..len(cs)-1)} using the Horner form.
456 @arg x: Polynomial argument (C{scalar}).
457 @arg cs: Polynomial coeffients (C{scalar}s).
459 @return: Horner value (C{float}).
461 @raise OverflowError: Partial C{2sum} overflow.
463 @raise TypeError: Non-scalar B{C{x}}.
465 @raise ValueError: No B{C{cs}} coefficients or B{C{x}} is not finite.
467 @see: Function L{fpolynomial} and class L{Fhorner}.
468 '''
469 H = Fhorner(x, *cs)
470 return float(H)
473def fidw(xs, ds, beta=2):
474 '''Interpolate using U{Inverse Distance Weighting
475 <https://WikiPedia.org/wiki/Inverse_distance_weighting>} (IDW).
477 @arg xs: Known values (C{scalar}s).
478 @arg ds: Non-negative distances (C{scalar}s).
479 @kwarg beta: Inverse distance power (C{int}, 0, 1, 2, or 3).
481 @return: Interpolated value C{x} (C{float}).
483 @raise LenError: Unequal or zero C{len(B{ds})} and C{len(B{xs})}.
485 @raise ValueError: Invalid B{C{beta}}, negative B{C{ds}} value,
486 weighted B{C{ds}} below L{EPS}.
488 @note: Using C{B{beta}=0} returns the mean of B{C{xs}}.
489 '''
490 n, xs = len2(xs)
491 d, ds = len2(ds)
492 if n != d or n < 1:
493 raise LenError(fidw, xs=n, ds=d)
495 d, x = min(zip(ds, xs))
496 if d > EPS0 and n > 1:
497 b = -Int_(beta=beta, low=0, high=3)
498 if b < 0:
499 ws = tuple(float(d)**b for d in ds)
500 t = fsum(_1map_mul(xs, ws)) # Fdot(xs, *ws)
501 x = _over(t, fsum(ws, floats=True))
502 else: # b == 0
503 x = fsum(xs) / n # fmean(xs)
504 elif d < 0: # PYCHOK no cover
505 n = Fmt.SQUARE(distance=ds.index(d))
506 raise _ValueError(n, d, txt=_negative_)
507 return x
510def fmean(xs):
511 '''Compute the accurate mean M{sum(xs) / len(xs)}.
513 @arg xs: Values (C{scalar} or L{Fsum} instances).
515 @return: Mean value (C{float}).
517 @raise LenError: No B{C{xs}} values.
519 @raise OverflowError: Partial C{2sum} overflow.
520 '''
521 n, xs = len2(xs)
522 if n < 1:
523 raise LenError(fmean, xs=xs)
524 return Fsum(*xs).fover(n) if n > 1 else _2float(index=0, xs=xs[0])
527def fmean_(*xs):
528 '''Compute the accurate mean M{sum(xs) / len(xs)}.
530 @see: Function L{fmean} for further details.
531 '''
532 return fmean(xs)
535def fpolynomial(x, *cs, **over):
536 '''Evaluate the polynomial M{sum(cs[i] * x**i for
537 i=0..len(cs)) [/ over]}.
539 @arg x: Polynomial argument (C{scalar}).
540 @arg cs: Polynomial coeffients (C{scalar}s), all
541 positional.
542 @kwarg over: Optional final, I{non-zero} divisor
543 (C{scalar}).
545 @return: Polynomial value (C{float}).
547 @raise OverflowError: Partial C{2sum} overflow.
549 @raise TypeError: Non-scalar B{C{x}}.
551 @raise ValueError: No B{C{cs}} coefficients or B{C{x}} is not finite.
553 @see: Function L{fhorner} and class L{Fpolynomial}.
554 '''
555 P = Fpolynomial(x, *cs)
556 d = _xkwds_get(over, over=0) if over else 0
557 return P.fover(d) if d else float(P)
560def fpowers(x, n, alts=0):
561 '''Return a series of powers M{[x**i for i=1..n]}.
563 @arg x: Value (C{scalar} or L{Fsum}).
564 @arg n: Highest exponent (C{int}).
565 @kwarg alts: Only alternating powers, starting with this
566 exponent (C{int}).
568 @return: Tuple of powers of B{C{x}} (C{type(B{x})}).
570 @raise TypeError: Invalid B{C{x}} or B{C{n}} not C{int}.
572 @raise ValueError: Non-finite B{C{x}} or invalid B{C{n}}.
573 '''
574 if not isint(n):
575 raise _IsnotError(int.__name__, n=n)
576 elif n < 1:
577 raise _ValueError(n=n)
579 p = x if isint(x) or isinstance(x, Fsum) else _2float(x=x)
580 ps = tuple(_powers(p, n))
582 if alts > 0: # x**2, x**4, ...
583 # ps[alts-1::2] chokes PyChecker
584 ps = ps[slice(alts-1, None, 2)]
586 return ps
589try:
590 from math import prod as fprod # Python 3.8
591except ImportError:
593 def fprod(xs, start=1):
594 '''Iterable product, like C{math.prod} or C{numpy.prod}.
596 @arg xs: Terms to be multiplied, an iterable, list,
597 tuple, etc. (C{scalar}s).
598 @kwarg start: Initial term, also the value returned
599 for an empty B{C{xs}} (C{scalar}).
601 @return: The product (C{float}).
603 @see: U{NumPy.prod<https://docs.SciPy.org/doc/
604 numpy/reference/generated/numpy.prod.html>}.
605 '''
606 return freduce(_operator.mul, xs, start)
609def frange(start, number, step=1):
610 '''Generate a range of C{float}s.
612 @arg start: First value (C{float}).
613 @arg number: The number of C{float}s to generate (C{int}).
614 @kwarg step: Increment value (C{float}).
616 @return: A generator (C{float}s).
618 @see: U{NumPy.prod<https://docs.SciPy.org/doc/
619 numpy/reference/generated/numpy.arange.html>}.
620 '''
621 if not isint(number):
622 raise _IsnotError(int.__name__, number=number)
623 for i in range(number):
624 yield start + (step * i)
627try:
628 from functools import reduce as freduce
629except ImportError:
630 try:
631 freduce = reduce # PYCHOK expected
632 except NameError: # Python 3+
634 def freduce(f, xs, *start):
635 '''For missing C{functools.reduce}.
636 '''
637 if start:
638 r = v = start[0]
639 else:
640 r, v = 0, MISSING
641 for v in xs:
642 r = f(r, v)
643 if v is MISSING:
644 raise _TypeError(xs=(), start=MISSING)
645 return r
648def fremainder(x, y):
649 '''Remainder in range C{[-B{y / 2}, B{y / 2}]}.
651 @arg x: Numerator (C{scalar}).
652 @arg y: Modulus, denominator (C{scalar}).
654 @return: Remainder (C{scalar}, preserving signed
655 0.0) or C{NAN} for any non-finite B{C{x}}.
657 @raise ValueError: Infinite or near-zero B{C{y}}.
659 @see: I{Karney}'s U{Math.remainder<https://PyPI.org/
660 project/geographiclib/>} and Python 3.7+
661 U{math.remainder<https://docs.Python.org/3/
662 library/math.html#math.remainder>}.
663 '''
664 # with Python 2.7.16 and 3.7.3 on macOS 10.13.6 and
665 # with Python 3.10.2 on macOS 12.2.1 M1 arm64 native
666 # fmod( 0, 360) == 0.0
667 # fmod( 360, 360) == 0.0
668 # fmod(-0, 360) == 0.0
669 # fmod(-0.0, 360) == -0.0
670 # fmod(-360, 360) == -0.0
671 # however, using the % operator ...
672 # 0 % 360 == 0
673 # 360 % 360 == 0
674 # 360.0 % 360 == 0.0
675 # -0 % 360 == 0
676 # -360 % 360 == 0 == (-360) % 360
677 # -0.0 % 360 == 0.0 == (-0.0) % 360
678 # -360.0 % 360 == 0.0 == (-360.0) % 360
680 # On Windows 32-bit with python 2.7, math.fmod(-0.0, 360)
681 # == +0.0. This fixes this bug. See also Math::AngNormalize
682 # in the C++ library, Math.sincosd has a similar fix.
683 if _isfinite(x):
684 try:
685 r = remainder(x, y) if x else x
686 except Exception as e:
687 raise _xError(e, unstr(fremainder, x, y))
688 else: # handle x INF and NINF as NAN
689 r = NAN
690 return r
693if _sys_version_info2 < (3, 8): # PYCHOK no cover
694 from math import hypot # OK in Python 3.7-
696 def hypot_(*xs):
697 '''Compute the norm M{sqrt(sum(x**2 for x in xs))}.
699 Similar to Python 3.8+ n-dimension U{math.hypot
700 <https://docs.Python.org/3.8/library/math.html#math.hypot>},
701 but exceptions, C{nan} and C{infinite} values are
702 handled differently.
704 @arg xs: X arguments (C{scalar}s), all positional.
706 @return: Norm (C{float}).
708 @raise OverflowError: Partial C{2sum} overflow.
710 @raise ValueError: Invalid or no B{C{xs}} values.
712 @note: The Python 3.8+ Euclidian distance U{math.dist
713 <https://docs.Python.org/3.8/library/math.html#math.dist>}
714 between 2 I{n}-dimensional points I{p1} and I{p2} can be
715 computed as M{hypot_(*((c1 - c2) for c1, c2 in zip(p1, p2)))},
716 provided I{p1} and I{p2} have the same, non-zero length I{n}.
717 '''
718 h, x2 = _h_x2(xs, hypot_)
719 return (h * sqrt(x2)) if x2 else _0_0
721elif _sys_version_info2 < (3, 10):
722 # In Python 3.8 and 3.9 C{math.hypot} is inaccurate, see
723 # U{agdhruv<https://GitHub.com/geopy/geopy/issues/466>},
724 # U{cffk<https://Bugs.Python.org/issue43088>} and module
725 # U{geomath.py<https://PyPI.org/project/geographiclib/1.52>}
727 def hypot(x, y):
728 '''Compute the norm M{sqrt(x**2 + y**2)}.
730 @arg x: X argument (C{scalar}).
731 @arg y: Y argument (C{scalar}).
733 @return: C{sqrt(B{x}**2 + B{y}**2)} (C{float}).
734 '''
735 if x:
736 h = sqrt(x**2 + y**2) if y else fabs(x)
737 elif y:
738 h = fabs(y)
739 else:
740 h = _0_0
741 return h
743 from math import hypot as hypot_ # PYCHOK in Python 3.8 and 3.9
744else:
745 from math import hypot # PYCHOK in Python 3.10+
746 hypot_ = hypot
749def _h_x2(xs, which):
750 '''(INTERNAL) Helper for L{hypot_} and L{hypot2_}.
751 '''
752 n, xs = len2(xs)
753 if n > 0:
754 h = float(max(map(fabs, xs)))
755 if h < EPS0:
756 x2 = _0_0
757 elif n > 1:
758 _h = (_1_0 / h) if h != _1_0 else _1_0
759 x2 = _fsum(_1primed((x * _h)**2 for x in xs))
760 else:
761 x2 = _1_0
762 return h, x2
764 t = Fmt.PAREN(which.__name__, xs)
765 raise _ValueError(t, txt=_too_(_few_))
768def hypot1(x):
769 '''Compute the norm M{sqrt(1 + x**2)}.
771 @arg x: Argument (C{scalar}).
773 @return: Norm (C{float}).
774 '''
775 return hypot(_1_0, x) if x else _1_0
778def hypot2(x, y):
779 '''Compute the I{squared} norm M{x**2 + y**2}.
781 @arg x: X argument (C{scalar}).
782 @arg y: Y argument (C{scalar}).
784 @return: C{B{x}**2 + B{y}**2} (C{float}).
785 '''
786 if fabs(x) < fabs(y):
787 x, y = y, x
788 if x:
789 h2 = x**2
790 if y:
791 h2 *= (y / x)**2 + _1_0
792 else:
793 h2 = _0_0
794 return h2
797def hypot2_(*xs):
798 '''Compute the I{squared} norm C{sum(x**2 for x in B{xs})}.
800 @arg xs: X arguments (C{scalar}s), all positional.
802 @return: Squared norm (C{float}).
804 @raise OverflowError: Partial C{2sum} overflow.
806 @raise ValueError: Invalid or no B{C{xs}} value.
808 @see: Function L{hypot_}.
809 '''
810 h, x2 = _h_x2(xs, hypot2_)
811 return (h**2 * x2) if x2 else _0_0
814def _map_mul(a, b, where):
815 '''(INTERNAL) Yield each B{C{a * b}}.
816 '''
817 n = len(b)
818 if len(a) != n: # PYCHOK no cover
819 raise LenError(where, a=len(a), b=n)
820 return _1map_mul(a, b) if n < 4 else map(_operator.mul, a, b)
823def _1map_mul(a, b):
824 '''(INTERNAL) Yield each B{C{a * b}}, 1-primed.
825 '''
826 return _1primed(map(_operator.mul, a, b))
829def norm2(x, y):
830 '''Normalize a 2-dimensional vector.
832 @arg x: X component (C{scalar}).
833 @arg y: Y component (C{scalar}).
835 @return: 2-Tuple C{(x, y)}, normalized.
837 @raise ValueError: Invalid B{C{x}} or B{C{y}}
838 or zero norm.
839 '''
840 try:
841 h = hypot(x, y)
842 if h:
843 x, y = (x / h), (y / h)
844 else:
845 x = _copysign_0_0(x) # pass?
846 y = _copysign_0_0(y)
847 except Exception as e:
848 raise _xError(e, x=x, y=y, h=h)
849 return x, y
852def norm_(*xs):
853 '''Normalize all n-dimensional vector components.
855 @arg xs: Components (C{scalar}s), all positional.
857 @return: Yield each component, normalized.
859 @raise ValueError: Invalid or insufficent B{C{xs}}
860 or zero norm.
861 '''
862 try:
863 h = hypot_(*xs)
864 _h = (_1_0 / h) if h else _0_0
865 for i, x in enumerate(xs):
866 yield x * _h
867 except Exception as e:
868 raise _xError(e, Fmt.SQUARE(xs=i), x, _h_, h)
871def _powers(x, n):
872 '''(INTERNAL) Yield C{x**i for i=1..n}.
873 '''
874 p = 1 # type(p) == type(x)
875 for _ in range(n):
876 p *= x
877 yield p
880def _root(x, p, where):
881 '''(INTERNAL) Raise C{x} to power C{0 < p < 1}.
882 '''
883 if x < 0:
884 t = _SPACE_(_invokation_, where.__name__)
885 raise _ValueError(unstr(t, x), txt=_negative_)
886 return pow(x, p) if x else _0_0
889def sqrt0(x, Error=None):
890 '''Return the square root iff C{B{x} >} L{EPS02}.
892 @arg x: Value (C{scalar}).
893 @kwarg Error: Error to raise for negative B{C{x}}.
895 @return: Square root (C{float}) or C{0.0}.
897 @note: Any C{B{x} < }L{EPS02} I{including} C{B{x} < 0}
898 returns C{0.0}.
899 '''
900 if Error and x < 0:
901 raise Error(Fmt.PAREN(sqrt=x))
902 return sqrt(x) if x > EPS02 else (_0_0 if x < EPS02 else EPS0)
905def sqrt3(x):
906 '''Return the square root, I{cubed} M{sqrt(x)**3} or M{sqrt(x**3)}.
908 @arg x: Value (C{scalar}).
910 @return: Square root I{cubed} (C{float}).
912 @raise ValueError: Negative B{C{x}}.
914 @see: Functions L{cbrt} and L{cbrt2}.
915 '''
916 return _root(x, _1_5, sqrt3)
919def sqrt_a(h, b):
920 '''Compute C{I{a}} side of a right-angled triangle from
921 C{sqrt(B{h}**2 - B{b}**2)}.
923 @arg h: Hypotenuse or outer annulus radius (C{scalar}).
924 @arg b: Triangle side or inner annulus radius (C{scalar}).
926 @return: C{copysign(I{a}, B{h})} or C{unsigned 0.0} (C{float}).
928 @raise TypeError: Non-scalar B{C{h}} or B{C{b}}.
930 @raise ValueError: If C{abs(B{h}) < abs(B{b})}.
932 @see: Inner tangent chord B{I{d}} of an U{annulus
933 <https://WikiPedia.org/wiki/Annulus_(mathematics)>}
934 and function U{annulus_area<https://People.SC.FSU.edu/
935 ~jburkardt/py_src/geometry/geometry.py>}.
936 '''
937 try:
938 if not (_isHeight(h) and _isRadius(b)):
939 raise TypeError(_not_scalar_)
940 c = fabs(h)
941 if c > EPS0:
942 s = _1_0 - (b / c)**2
943 if s < 0:
944 raise ValueError(_h_lt_b_)
945 a = (sqrt(s) * c) if 0 < s < 1 else (c if s else _0_0)
946 else: # PYCHOK no cover
947 b = fabs(b)
948 d = c - b
949 if d < 0:
950 raise ValueError(_h_lt_b_)
951 d *= c + b
952 a = sqrt(d) if d else _0_0
953 except Exception as x:
954 raise _xError(x, h=h, b=b)
955 return copysign0(a, h)
958def zcrt(x):
959 '''Return the 6-th, I{zenzi-cubic} root, M{x**(1 / 6)}.
961 @arg x: Value (C{scalar}).
963 @return: I{Zenzi-cubic} root (C{float}).
965 @see: Functions L{bqrt} and L{zqrt}.
967 @raise ValueError: Negative B{C{x}}.
968 '''
969 return _root(x, _1_6th, zcrt)
972def zqrt(x):
973 '''Return the 8-th, I{zenzi-quartic} or I{squared-quartic} root, M{x**(1 / 8)}.
975 @arg x: Value (C{scalar}).
977 @return: I{Zenzi-quartic} root (C{float}).
979 @see: Functions L{bqrt} and L{zcrt}.
981 @raise ValueError: Negative B{C{x}}.
982 '''
983 return _root(x, _0_125, zqrt)
985# **) MIT License
986#
987# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved.
988#
989# Permission is hereby granted, free of charge, to any person obtaining a
990# copy of this software and associated documentation files (the "Software"),
991# to deal in the Software without restriction, including without limitation
992# the rights to use, copy, modify, merge, publish, distribute, sublicense,
993# and/or sell copies of the Software, and to permit persons to whom the
994# Software is furnished to do so, subject to the following conditions:
995#
996# The above copyright notice and this permission notice shall be included
997# in all copies or substantial portions of the Software.
998#
999# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1000# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1001# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1002# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1003# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1004# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1005# OTHER DEALINGS IN THE SOFTWARE.