Coverage for pygeodesy / fmath.py: 89%
358 statements
« prev ^ index » next coverage.py v7.14.0, created at 2026-10-21 16:43 -0400
« prev ^ index » next coverage.py v7.14.0, created at 2026-10-21 16:43 -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.10.04'
31# 1.4142135623730950_48801688724209 <https://WikiPedia.org/wiki/Square_root_of_2>
32# math.sqrt(2) - 1 == 0.4142135623730950_3 == 3730904090310553 / 9007199254740992
33_0_4142 = 0.4142135623730950488
34_1_6th = 1 / 6
35_h_lt_b_ = 'abs(h) < abs(b)'
38class Fdot(Fsum):
39 '''Precision dot product.
40 '''
41 def __init__(self, a, *b, **start_name_f2product_nonfinites_RESIDUAL):
42 '''New L{Fdot} precision dot product M{start + sum(a[i] * b[i] for i=0..len(a)-1)}.
44 @arg a: Iterable of values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
45 @arg b: Other values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all
46 positional.
47 @kwarg start_name_f2product_nonfinites_RESIDUAL: Optional bias C{B{start}=0}
48 (C{scalar}, an L{Fsum} or L{Fsum2Tuple}), C{B{name}=NN} (C{str})
49 and other settings, see class L{Fsum<Fsum.__init__>}.
51 @raise LenError: Unequal C{len(B{a})} and C{len(B{b})}.
53 @raise OverflowError: Partial C{2sum} overflow.
55 @raise TypeError: Invalid B{C{x}}.
57 @raise ValueError: Non-finite B{C{x}}.
59 @see: Function L{fdot} and method L{Fsum.fadd}.
60 '''
61 s, kwds = _xkwds_pop2(start_name_f2product_nonfinites_RESIDUAL, start=_0_0)
62 Fsum.__init__(self, **kwds)
63 self(s)
65 n = len(b)
66 if len(a) != n: # PYCHOK no cover
67 raise LenError(Fdot, a=len(a), b=n)
68 self._facc_dot(n, a, b, **kwds)
71class Fdot_(Fdot): # in .elliptic
72 '''Precision dot product.
73 '''
74 def __init__(self, *xys, **start_name_f2product_nonfinites_RESIDUAL):
75 '''New L{Fdot_} precision dot product M{start + sum(xys[i] * xys[i+1] for i in
76 range(0, len(xys), B{2}))}.
78 @arg xys: Pairwise values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}),
79 all positional.
81 @see: Class L{Fdot<Fdot.__init__>} for further details.
82 '''
83 if isodd(len(xys)):
84 raise LenError(Fdot_, xys=len(xys))
85 Fdot.__init__(self, xys[0::2], *xys[1::2], **start_name_f2product_nonfinites_RESIDUAL)
88class Fhorner(Fsum):
89 '''Precision polynomial evaluation using the Horner form.
90 '''
91 def __init__(self, x, *cs, **incx_name_f2product_nonfinites_RESIDUAL):
92 '''New L{Fhorner} form evaluation of polynomial M{sum(cs[i] * x**i for i=0..n)}
93 with in- or decreasing exponent M{sum(... i=n..0)}, where C{n = len(cs) - 1}.
95 @arg x: Polynomial argument (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
96 @arg cs: Polynomial coeffients (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}),
97 all positional.
98 @kwarg incx_name_f2product_nonfinites_RESIDUAL: Optional C{B{name}=NN} (C{str}),
99 C{B{incx}=True} for in-/decreasing exponents (C{bool}) and other
100 settings, see class L{Fsum<Fsum.__init__>}.
102 @raise OverflowError: Partial C{2sum} overflow.
104 @raise TypeError: Invalid B{C{x}}.
106 @raise ValueError: Non-finite B{C{x}}.
108 @see: Function L{fhorner} and methods L{Fsum.fadd} and L{Fsum.fmul}.
109 '''
110 incx, kwds = _xkwds_pop2(incx_name_f2product_nonfinites_RESIDUAL, incx=True)
111 Fsum.__init__(self, **kwds)
112 self._fhorner(x, cs, Fhorner, incx=incx)
115class Fhypot(Fsum):
116 '''Precision summation and hypotenuse, default C{root=2}.
117 '''
118 def __init__(self, *xs, **root_name_f2product_nonfinites_RESIDUAL_raiser):
119 '''New L{Fhypot} hypotenuse of (the I{root} of) several components (raised
120 to the power I{root}).
122 @arg xs: Components (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all
123 positional.
124 @kwarg root_name_f2product_nonfinites_RESIDUAL_raiser: Optional, exponent
125 and C{B{root}=2} order (C{scalar}), C{B{name}=NN} (C{str}),
126 C{B{raiser}=True} (C{bool}) for raising L{ResidualError}s and
127 other settings, see class L{Fsum<Fsum.__init__>} and method
128 L{root<Fsum.root>}.
129 '''
130 def _r_X_kwds(power=None, raiser=True, root=2, **kwds):
131 # DEPRECATED keyword argument C{power=2}, use C{root=2}
132 return (root if power is None else power), raiser, kwds
134 r = None # _xkwds_pop2 error
135 try:
136 r, X, kwds = _r_X_kwds(**root_name_f2product_nonfinites_RESIDUAL_raiser)
137 Fsum.__init__(self, **kwds)
138 self(_0_0)
139 if xs:
140 self._facc_power(r, xs, Fhypot, raiser=X)
141 self._fset(self.root(r, raiser=X))
142 except Exception as X:
143 raise self._ErrorXs(X, xs, root=r)
146class Fpolynomial(Fsum):
147 '''Precision polynomial evaluation.
148 '''
149 def __init__(self, x, *cs, **name_f2product_nonfinites_RESIDUAL):
150 '''New L{Fpolynomial} evaluation of the polynomial M{sum(cs[i] * x**i for
151 i=0..len(cs)-1)}.
153 @arg x: Polynomial argument (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
154 @arg cs: Polynomial coeffients (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}),
155 all positional.
156 @kwarg name_f2product_nonfinites_RESIDUAL: Optional C{B{name}=NN} (C{str})
157 and other settings, see class L{Fsum<Fsum.__init__>}.
159 @raise OverflowError: Partial C{2sum} overflow.
161 @raise TypeError: Invalid B{C{x}}.
163 @raise ValueError: Non-finite B{C{x}}.
165 @see: Class L{Fhorner}, function L{fpolynomial} and method L{Fsum.fadd}.
166 '''
167 Fsum.__init__(self, **name_f2product_nonfinites_RESIDUAL)
168 n = len(cs) - 1
169 self(_0_0 if n < 0 else cs[0])
170 self._facc_dot(n, cs[1:], _powers(x, n), **name_f2product_nonfinites_RESIDUAL)
173class Fpowers(Fsum):
174 '''Precision summation of powers, optimized for C{power=2, 3 and 4}.
175 '''
176 def __init__(self, power, *xs, **name_f2product_nonfinites_RESIDUAL_raiser):
177 '''New L{Fpowers} sum of (the I{power} of) several bases.
179 @arg power: The exponent (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
180 @arg xs: One or more bases (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all
181 positional.
182 @kwarg name_f2product_nonfinites_RESIDUAL_raiser: Optional C{B{name}=NN}
183 (C{str}), C{B{raiser}=True} (C{bool}) for raising L{ResidualError}s
184 and other settings, see class L{Fsum<Fsum.__init__>} and method
185 L{fpow<Fsum.fpow>}.
186 '''
187 try:
188 X, kwds = _xkwds_pop2(name_f2product_nonfinites_RESIDUAL_raiser, raiser=True)
189 Fsum.__init__(self, **kwds)
190 self(_0_0)
191 if xs:
192 self._facc_power(power, xs, Fpowers, raiser=X) # x**0 == 1
193 except Exception as X:
194 raise self._ErrorXs(X, xs, power=power)
197class Froot(Fsum):
198 '''The root of a precision summation.
199 '''
200 def __init__(self, root, *xs, **name_f2product_nonfinites_RESIDUAL_raiser):
201 '''New L{Froot} root of a precision sum.
203 @arg root: The order (C{scalar}, an L{Fsum} or L{Fsum2Tuple}), non-zero.
204 @arg xs: Items to summate (each a C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all
205 positional.
206 @kwarg name_f2product_nonfinites_RESIDUAL_raiser: Optional C{B{name}=NN}
207 (C{str}), C{B{raiser}=True} (C{bool}) for raising L{ResidualError}s
208 and other settings, see class L{Fsum<Fsum.__init__>} and method
209 L{fpow<Fsum.fpow>}.
210 '''
211 try:
212 X, kwds = _xkwds_pop2(name_f2product_nonfinites_RESIDUAL_raiser, raiser=True)
213 Fsum.__init__(self, **kwds)
214 self(_0_0)
215 if xs:
216 self.fadd(xs)
217 self(self.root(root, raiser=X))
218 except Exception as X:
219 raise self._ErrorXs(X, xs, root=root)
222class Fcbrt(Froot):
223 '''Cubic root of a precision summation.
224 '''
225 def __init__(self, *xs, **name_f2product_nonfinites_RESIDUAL_raiser):
226 '''New L{Fcbrt} cubic root of a precision sum.
228 @see: Class L{Froot<Froot.__init__>} for further details.
229 '''
230 Froot.__init__(self, 3, *xs, **name_f2product_nonfinites_RESIDUAL_raiser)
233class Fsqrt(Froot):
234 '''Square root of a precision summation.
235 '''
236 def __init__(self, *xs, **name_f2product_nonfinites_RESIDUAL_raiser):
237 '''New L{Fsqrt} square root of a precision sum.
239 @see: Class L{Froot<Froot.__init__>} for further details.
240 '''
241 Froot.__init__(self, 2, *xs, **name_f2product_nonfinites_RESIDUAL_raiser)
244def bqrt(x):
245 '''Return the 4-th, I{bi-quadratic} or I{quartic} root, M{x**(1 / 4)},
246 preserving C{type(B{x})}.
248 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
250 @return: I{Quartic} root (C{float} or an L{Fsum}).
252 @raise TypeeError: Invalid B{C{x}}.
254 @raise ValueError: Negative B{C{x}}.
256 @see: Functions L{zcrt} and L{zqrt}.
257 '''
258 return _root(x, _0_25, bqrt)
261try:
262 from math import cbrt as _cbrt # Python 3.11+
263except ImportError: # Python 3.10-
265 def _cbrt(x):
266 '''(INTERNAL) Compute the I{signed}, cube root M{x**(1/3)}.
267 '''
268 # <https://archive.lib.MSU.edu/crcmath/math/math/r/r021.htm>
269 # simpler and more accurate than Ken Turkowski's CubeRoot, see
270 # <https://People.FreeBSD.org/~lstewart/references/apple_tr_kt32_cuberoot.pdf>
271 return _copysign(pow(fabs(x), _1_3rd), x) # to avoid complex
274def cbrt(x):
275 '''Compute the cube root M{x**(1/3)}, preserving C{type(B{x})}.
277 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
279 @return: Cubic root (C{float} or L{Fsum}).
281 @see: Functions L{cbrt2} and L{sqrt3}.
282 '''
283 if _isFsum_2Tuple(x):
284 r = abs(x).fpow(_1_3rd)
285 if x.signOf() < 0:
286 r = -r
287 else:
288 r = _cbrt(x)
289 return r # cbrt(-0.0) == -0.0
292def cbrt2(x): # PYCHOK attr
293 '''Compute the cube root I{squared} M{x**(2/3)}, preserving C{type(B{x})}.
295 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
297 @return: Cube root I{squared} (C{float} or L{Fsum}).
299 @see: Functions L{cbrt} and L{sqrt3}.
300 '''
301 return abs(x).fpow(_2_3rd) if _isFsum_2Tuple(x) else _cbrt(x**2)
304def euclid(x, y, *x0_y0, **half):
305 '''I{Appoximate} the norm M{hypot(B{x}, B{y})} by M{max(abs(B{x}),
306 abs(B{y})) + min(abs(B{x}), abs(B{y})) * 0.4142...}.
308 @arg x: X component (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
309 @arg y: Y component (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
310 @arg x0_y0: Optional reference C{(x0, y0)} (each C{scalar}, an
311 L{Fsum} or L{Fsum2Tuple}).
312 @kwarg half: Use C{B{half}=True} to apply C{0.5} instead of
313 C{sqrt(2) - 1} (C{bool}).
315 @return: Appoximate norm (C{float} or L{Fsum}).
316 '''
317 x, y = _map0(abs, x, y, *x0_y0) # NOT fabs!
318 if x < y:
319 x, y = y, x
320 y *= _half(**half) if half else _0_4142 # _0_5 before 20.10.02
321 return x + y
324def euclid_(*xs, **half):
325 '''I{Appoximate} the norm M{sqrt(sum(x**2 for x in xs))} by cascaded
326 L{euclid}.
328 @arg xs: X values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}),
329 all positional.
330 @kwarg half: Use C{B{half}=True} to apply C{0.5} instead of
331 C{sqrt(2) - 1} (C{bool}).
333 @return: Appoximate norm (C{float} or L{Fsum}).
335 @see: Function L{euclid}.
336 '''
337 e = _0_0
338 f = _half(**half)
339 for x in sorted(map(abs, xs)): # NOT fabs, reverse=True!
340 # e = euclid(x, e)
341 if x:
342 if e < x:
343 e, x = x, e
344 x *= f
345# s = e + x
346# if e < x: # like .fsums._2sum
347# x -= s # e = (x - s) + e
348# else:
349# e -= s # e = (e - s) + x
350 e += x
351# e += s
352 return e
355def facos1(x):
356 '''Fast approximation of L{pygeodesy.acos1}C{(B{x})}, scalar.
358 @see: U{ShaderFastLibs.h<https://GitHub.com/michaldrobot/
359 ShaderFastLibs/blob/master/ShaderFastMathLib.h>}.
360 '''
361 a = fabs(x)
362 if a < EPS0:
363 r = PI_2
364 elif a < EPS1:
365 r = _fast(-a, 1.5707288, 0.2121144, 0.0742610, 0.0187293)
366 r *= sqrt(_1_0 - a)
367 if x < 0:
368 r = PI - r
369 else:
370 r = PI if x < 0 else _0_0
371 return r
374def fasin1(x): # PYCHOK no cover
375 '''Fast approximation of L{pygeodesy.asin1}C{(B{x})}, scalar.
377 @see: L{facos1}.
378 '''
379 return PI_2 - facos1(x)
382def _fast(x, *cs):
383 '''(INTERNAL) Horner form for C{facos1} and C{fatan1}.
384 '''
385 h = 0
386 for c in reversed(cs):
387 h = _fma(x, h, c) if h else c
388 return h
391def fatan(x):
392 '''Fast approximation of C{atan(B{x})}, scalar.
393 '''
394 a = fabs(x)
395 if a < _1_0:
396 r = fatan1(a) if a else _0_0
397 elif a > _1_0:
398 r = PI_2 - fatan1(_1_0 / a) # == fatan2(a, _1_0)
399 else:
400 r = PI_4
401 if x < 0: # copysign0(r, x)
402 r = -r
403 return r
406def fatan1(x):
407 '''Fast approximation of C{atan(B{x})} for C{0 <= B{x} < 1}, I{unchecked}.
409 @see: U{ShaderFastLibs.h<https://GitHub.com/michaldrobot/ShaderFastLibs/
410 blob/master/ShaderFastMathLib.h>} and U{Efficient approximations
411 for the arctangent function<http://www-Labs.IRO.UMontreal.CA/
412 ~mignotte/IFT2425/Documents/EfficientApproximationArctgFunction.pdf>},
413 IEEE Signal Processing Magazine, 111, May 2006.
414 '''
415 # Eq (9): PI_4 * x - x * (abs(x) - 1) * (0.2447 + 0.0663 * abs(x)), for -1 < x < 1
416 # == PI_4 * x - (x**2 - x) * (0.2447 + 0.0663 * x), for 0 < x < 1
417 # == x * (1.0300981633974482 + x * (-0.1784 - x * 0.0663))
418 return _fast(x, _0_0, 1.0300981634, -0.1784, -0.0663)
421def fatan2(y, x):
422 '''Fast approximation of C{atan2(B{y}, B{x})}, scalar.
424 @see: U{fastApproximateAtan(x, y)<https://GitHub.com/CesiumGS/cesium/blob/
425 master/Source/Shaders/Builtin/Functions/fastApproximateAtan.glsl>}
426 and L{fatan1}.
427 '''
428 a, b = fabs(x), fabs(y)
429 if b > a:
430 r = (PI_2 - fatan1(a / b)) if a else PI_2
431 elif a > b:
432 r = fatan1(b / a) if b else _0_0
433 elif a: # a == b != 0
434 r = PI_4
435 else: # a == b == 0
436 return _0_0
437 if x < 0:
438 r = PI - r
439 if y < 0: # copysign0(r, y)
440 r = -r
441 return r
444def favg(a, b, f=_0_5, nonfinites=True):
445 '''Return the precise average of two values.
447 @arg a: One (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
448 @arg b: Other (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
449 @kwarg f: Optional fraction (C{float}).
450 @kwarg nonfinites: Optional setting, see function L{fma}.
452 @return: M{a + f * (b - a)} (C{float}).
453 '''
454 F = fma(f, (b - a), a, nonfinites=nonfinites)
455 return float(F)
458def fdot(xs, *ys, **start_f2product_nonfinites):
459 '''Return the precision dot product M{start + sum(xs[i] * ys[i] for i in range(len(xs)))}.
461 @arg xs: Iterable of values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
462 @arg ys: Other values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all positional.
463 @kwarg start_f2product_nonfinites: Optional bias C{B{start}=0} (C{scalar}, an
464 L{Fsum} or L{Fsum2Tuple}) and settings C{B{f2product}=None} (C{bool})
465 and C{B{nonfinites=True}} (C{bool}), see class L{Fsum<Fsum.__init__>}.
467 @return: Dot product (C{float}).
469 @raise LenError: Unequal C{len(B{xs})} and C{len(B{ys})}.
471 @see: Class L{Fdot}, U{Algorithm 5.10 B{DotK}
472 <https://www.TUHH.De/ti3/paper/rump/OgRuOi05.pdf>} and function
473 C{math.sumprod} in Python 3.12 and later.
474 '''
475 D = Fdot(xs, *ys, **_xkwds(start_f2product_nonfinites, nonfinites=True))
476 return float(D)
479def fdot_(*xys, **start_f2product_nonfinites):
480 '''Return the (precision) dot product M{start + sum(xys[i] * xys[i+1] for i in range(0, len(xys), B{2}))}.
482 @arg xys: Pairwise values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all positional.
484 @see: Function L{fdot} for further details.
486 @return: Dot product (C{float}).
487 '''
488 D = Fdot_(*xys, **_xkwds(start_f2product_nonfinites, nonfinites=True))
489 return float(D)
492def fdot3(xs, ys, zs, **start_f2product_nonfinites):
493 '''Return the (precision) dot product M{start + sum(xs[i] * ys[i] * zs[i] for i in range(len(xs)))}.
495 @arg xs: X values iterable (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
496 @arg ys: Y values iterable (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
497 @arg zs: Z values iterable (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
499 @see: Function L{fdot} for further details.
501 @return: Dot product (C{float}).
503 @raise LenError: Unequal C{len(B{xs})}, C{len(B{ys})} and/or C{len(B{zs})}.
504 '''
505 n = len(xs)
506 if not n == len(ys) == len(zs):
507 raise LenError(fdot3, xs=n, ys=len(ys), zs=len(zs))
509 D = Fdot((), **_xkwds(start_f2product_nonfinites, nonfinites=True))
510 kwds = dict(f2product=D.f2product(), nonfinites=D.nonfinites())
511 _f = Fsum(**kwds)
512 D = D._facc(_f(x).f2mul_(y, z, **kwds) for x, y, z in zip(xs, ys, zs))
513 return float(D)
516if _zip is zip: # Python 3.9-
517 _fdotf = fdot
518else:
519 from pygeodesy.fsums import _fsum # math.fsum
521 def _fdotf(xs, *ys): # in .datums, .ecef
522 '''(INTERNAL) Dot product for C{float} tuples of matching C{len}gths.
523 '''
524 return _fsum(x * y for x, y in _zip(xs, ys)) # strict=True
527def fhorner(x, *cs, **incx):
528 '''Horner form evaluation of polynomial M{sum(cs[i] * x**i for i=0..n)} as
529 in- or decreasing exponents M{sum(... i=n..0)}, where C{n = len(cs) - 1}.
531 @return: Horner sum (C{float}).
533 @see: Class L{Fhorner<Fhorner.__init__>} for further details.
534 '''
535 H = Fhorner(x, *cs, **incx)
536 return float(H)
539def fidw(xs, ds, beta=2):
540 '''Interpolate using U{Inverse Distance Weighting
541 <https://WikiPedia.org/wiki/Inverse_distance_weighting>} (IDW).
543 @arg xs: Known values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
544 @arg ds: Non-negative distances (each C{scalar}, an L{Fsum} or
545 L{Fsum2Tuple}).
546 @kwarg beta: Inverse distance power (C{int}, 0, 1, 2, or 3).
548 @return: Interpolated value C{x} (C{float}).
550 @raise LenError: Unequal or zero C{len(B{ds})} and C{len(B{xs})}.
552 @raise TypeError: An invalid B{C{ds}} or B{C{xs}}.
554 @raise ValueError: Invalid B{C{beta}}, negative B{C{ds}} or
555 weighted B{C{ds}} below L{EPS}.
557 @note: Using C{B{beta}=0} returns the mean of B{C{xs}}.
558 '''
559 n, xs = len2(xs)
560 if n > 1:
561 b = -Int_(beta=beta, low=0, high=3)
562 if b < 0:
563 try: # weighted
564 _d, W, X = (Fsum() for _ in range(3))
565 for i, d in enumerate(_xiterable(ds)):
566 x = xs[i]
567 D = _d(d)
568 if D < EPS0:
569 if D < 0:
570 raise ValueError(_negative_)
571 x = float(x)
572 i = n
573 break
574 if D.fpow(b):
575 W += D
576 X += D.fmul(x)
577 else:
578 x = X.fover(W, raiser=False)
579 i += 1 # len(xs) >= len(ds)
580 except IndexError:
581 i += 1 # len(xs) < i < len(ds)
582 except Exception as X:
583 _I = Fmt.INDEX
584 raise _xError(X, _I(xs=i), x, # PYLINT E1120
585 _I(ds=i), d) # PYLINT E1120
586 else: # b == 0
587 x = fsum(xs) / n # fmean(xs)
588 i = n
589 elif n:
590 x = float(xs[0])
591 i = n
592 else:
593 x = _0_0
594 i, _ = len2(ds)
595 if i != n:
596 raise LenError(fidw, xs=n, ds=i)
597 return x
600try:
601 from math import fma as _fma # in .resections
603 def _fhornerf(x, *cs, **incx):
604 '''(INTERNAL) Horner form for C{float} coefficients C{cs} and C{x}
605 and C{B{incx}=True} in- or decreasing with exponents=.
606 '''
607 h = _0_0
608 if cs:
609 if _xkwds_get(incx, incx=True):
610 cs = reversed(cs)
611 for c in cs:
612 h = _fma(x, h, c) if h else c
613 return h
615except ImportError: # PYCHOK DSPACE!
616 _fhornerf = fhorner # PYCHOK redef
618 def _fma(x, y, z): # no need for accuracy
619 return x * y + z
622def fma(x, y, z, **nonfinites): # **raiser
623 '''Fused-multiply-add, using C{math.fma(x, y, z)} in Python 3.13+
624 or an equivalent implementation.
626 @arg x: Multiplicand (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
627 @arg y: Multiplier (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
628 @arg z: Addend (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
629 @kwarg nonfinites: Use C{B{nonfinites}=True} or C{=False},
630 to override default L{nonfiniterrors}
631 (C{bool}), see method L{Fsum.fma}.
633 @return: C{(x * y) + z} (C{float} or L{Fsum}).
634 '''
635 F, raiser = _Fm2(x, **nonfinites)
636 return F.fma(y, z, **raiser).as_iscalar
639def _Fm2(x, nonfinites=None, **raiser):
640 '''(INTERNAL) Handle C{fma} and C{f2mul} DEPRECATED C{raiser=False}.
641 '''
642 return Fsum(x, nonfinites=nonfinites), raiser
645def fmean(xs, nonfinites=True):
646 '''Compute the accurate mean M{sum(xs) / len(xs)}.
648 @arg xs: Values (each C{scalar}, or L{Fsum} or L{Fsum2Tuple}).
650 @return: Mean value (C{float}).
652 @raise LenError: No B{C{xs}} values.
654 @raise OverflowError: Partial C{2sum} overflow.
655 '''
656 n, xs = len2(xs)
657 if n < 1:
658 raise LenError(fmean, xs=xs)
659 M = Fsum(*xs, nonfinites=nonfinites)
660 return M.fover(n) if n > 1 else float(M)
663def fmean_(*xs, **nonfinites):
664 '''Compute the accurate mean M{sum(xs) / len(xs)}.
666 @see: Function L{fmean} for further details.
667 '''
668 return fmean(xs, **nonfinites)
671def f2mul_(x, *ys, **nonfinites): # **raiser
672 '''Cascaded, accurate multiplication C{B{x} * B{y} * B{y} ...} for all B{C{ys}}.
674 @arg x: Multiplicand (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
675 @arg ys: Multipliers (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all
676 positional.
677 @kwarg nonfinites: Use C{B{nonfinites}=True} or C{=False}, to override default
678 L{nonfiniterrors} (C{bool}), see method L{Fsum.f2mul_}.
680 @return: The cascaded I{TwoProduct} (C{float}, C{int} or L{Fsum}).
682 @see: U{Equations 2.3<https://www.TUHH.De/ti3/paper/rump/OzOgRuOi06.pdf>}
683 '''
684 F, raiser = _Fm2(x, **nonfinites)
685 return F.f2mul_(*ys, **raiser).as_iscalar
688def fpolynomial(x, *cs, **over_f2product_nonfinites):
689 '''Evaluate the polynomial M{sum(cs[i] * x**i for i=0..len(cs)) [/ over]}.
691 @kwarg over_f2product_nonfinites: Optional final divisor C{B{over}=None}
692 (I{non-zero} C{scalar}) and other settings, see class
693 L{Fpolynomial<Fpolynomial.__init__>}.
695 @return: Polynomial value (C{float} or L{Fpolynomial}).
696 '''
697 d, kwds = _xkwds_pop2(over_f2product_nonfinites, over=0)
698 P = Fpolynomial(x, *cs, **kwds)
699 return P.fover(d) if d else float(P)
702def fpowers(x, n, alts=0):
703 '''Return a series of powers M{[x**i for i=1..n]}, note I{1..!}
705 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
706 @arg n: Highest exponent (C{int}).
707 @kwarg alts: Only alternating powers, starting with this
708 exponent (C{int}).
710 @return: Tuple of powers of B{C{x}} (each C{type(B{x})}).
712 @raise TypeError: Invalid B{C{x}} or B{C{n}} not C{int}.
714 @raise ValueError: Non-finite B{C{x}} or invalid B{C{n}}.
715 '''
716 if not isint(n):
717 raise _IsnotError(typename(int), n=n)
718 elif n < 1:
719 raise _ValueError(n=n)
721 p = x if isscalar(x) or _isFsum_2Tuple(x) else _2float(x=x)
722 ps = tuple(_powers(p, n))
724 if alts > 0: # x**2, x**4, ...
725 # ps[alts-1::2] chokes PyChecker
726 ps = ps[slice(alts-1, None, 2)]
728 return ps
731try:
732 from math import prod as fprod # PYLINT W0611 Python 3.8
733except ImportError:
735 def fprod(xs, start=1):
736 '''Iterable product, like C{math.prod} or C{numpy.prod}.
738 @arg xs: Iterable of values to be multiplied (each
739 C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
740 @kwarg start: Initial value, also the value returned
741 for an empty B{C{xs}} (C{scalar}).
743 @return: The product (C{float} or L{Fsum}).
745 @see: U{NumPy.prod<https://docs.SciPy.org/doc/
746 numpy/reference/generated/numpy.prod.html>}.
747 '''
748 return freduce(_operator.mul, xs, start)
751def frandoms(n, seeded=None):
752 '''Generate C{n} (long) lists of random C{floats}.
754 @arg n: Number of lists to generate (C{int}, non-negative).
755 @kwarg seeded: If C{scalar}, use C{random.seed(B{seeded})} or
756 if C{True}, seed using today's C{year-day}.
758 @see: U{Hettinger<https://GitHub.com/ActiveState/code/tree/master/recipes/
759 Python/393090_Binary_floating_point_summatiaccurate_full/recipe-393090.py>}.
760 '''
761 from random import gauss, random, seed, shuffle
763 if seeded is None:
764 pass
765 elif seeded and isbool(seeded):
766 from time import localtime
767 seed(localtime().tm_yday)
768 elif isscalar(seeded):
769 seed(seeded)
771 c = (7, 1e100, -7, -1e100, -9e-20, 8e-20) * 7
772 for _ in range(n):
773 s = 0
774 t = list(c)
775 _a = t.append
776 for _ in range(n * 8):
777 v = gauss(0, random())**7 - s
778 _a(v)
779 s += v
780 shuffle(t)
781 yield t
784def frange(start, number, step=1):
785 '''Generate a range of C{float}s.
787 @arg start: First value (C{float}).
788 @arg number: The number of C{float}s to generate (C{int}).
789 @kwarg step: Increment value (C{float}).
791 @return: A generator (C{float}s).
793 @see: U{NumPy.prod<https://docs.SciPy.org/doc/
794 numpy/reference/generated/numpy.arange.html>}.
795 '''
796 if not isint(number):
797 raise _IsnotError(typename(int), number=number)
798 for i in range(number):
799 yield start + (step * i)
802try:
803 from functools import reduce as freduce
804except ImportError:
805 try:
806 freduce = reduce # PYCHOK expected
807 except NameError: # Python 3+
809 def freduce(f, xs, *start):
810 '''For missing C{functools.reduce}.
811 '''
812 if start:
813 r = v = start[0]
814 else:
815 r, v = 0, MISSING
816 for v in xs:
817 r = f(r, v)
818 if v is MISSING:
819 raise _TypeError(xs=(), start=MISSING)
820 return r
823def fremainder(x, y):
824 '''Remainder in range C{[-B{y / 2}, B{y / 2}]}.
826 @arg x: Numerator (C{scalar}).
827 @arg y: Modulus, denominator (C{scalar}).
829 @return: Remainder (C{scalar}, preserving signed
830 0.0) or C{NAN} for any non-finite B{C{x}}.
832 @raise ValueError: Infinite or near-zero B{C{y}}.
834 @see: I{Karney}'s U{Math.remainder<https://PyPI.org/
835 project/geographiclib/>} and Python 3.7+
836 U{math.remainder<https://docs.Python.org/3/
837 library/math.html#math.remainder>}.
838 '''
839 # with Python 2.7.16 and 3.7.3 on macOS 10.13.6 and
840 # with Python 3.10.2 on macOS 12.2.1 M1 arm64 native
841 # fmod( 0, 360) == 0.0
842 # fmod( 360, 360) == 0.0
843 # fmod(-0, 360) == 0.0
844 # fmod(-0.0, 360) == -0.0
845 # fmod(-360, 360) == -0.0
846 # however, using the % operator ...
847 # 0 % 360 == 0
848 # 360 % 360 == 0
849 # 360.0 % 360 == 0.0
850 # -0 % 360 == 0
851 # -360 % 360 == 0 == (-360) % 360
852 # -0.0 % 360 == 0.0 == (-0.0) % 360
853 # -360.0 % 360 == 0.0 == (-360.0) % 360
855 # On Windows 32-bit with python 2.7, math.fmod(-0.0, 360)
856 # == +0.0. This fixes this bug. See also Math::AngNormalize
857 # in the C++ library, Math.sincosd has a similar fix.
858 if isfinite(x):
859 try:
860 r = remainder(x, y) if x else x
861 except Exception as e:
862 raise _xError(e, unstr(fremainder, x, y))
863 else: # handle x INF and NINF as NAN
864 r = NAN
865 return r
868def _half(half=False):
869 return _0_5 if half else _0_4142
872if _MODS.sys_version_info2 < (3, 8): # PYCHOK no cover
873 from math import hypot # OK in Python 3.7-
875 def hypot_(*xs):
876 '''Compute the norm M{sqrt(sum(x**2 for x in xs))}.
878 Similar to Python 3.8+ n-dimension U{math.hypot
879 <https://docs.Python.org/3.8/library/math.html#math.hypot>},
880 but exceptions, C{nan} and C{infinite} values are
881 handled differently.
883 @arg xs: X arguments (C{scalar}s), all positional.
885 @return: Norm (C{float}).
887 @raise OverflowError: Partial C{2sum} overflow.
889 @raise ValueError: Invalid or no B{C{xs}} values.
891 @note: The Python 3.8+ Euclidian distance U{math.dist
892 <https://docs.Python.org/3.8/library/math.html#math.dist>}
893 between 2 I{n}-dimensional points I{p1} and I{p2} can be
894 computed as M{hypot_(*((c1 - c2) for c1, c2 in zip(p1, p2)))},
895 provided I{p1} and I{p2} have the same, non-zero length I{n}.
896 '''
897 return float(_Hypot(*xs))
899elif _MODS.sys_version_info2 < (3, 10): # PYCHOK no cover
900 # In Python 3.8 and 3.9 C{math.hypot} is inaccurate, see
901 # U{agdhruv<https://GitHub.com/geopy/geopy/issues/466>},
902 # U{cffk<https://Bugs.Python.org/issue43088>} and module
903 # U{geomath.py<https://PyPI.org/project/geographiclib/1.52>}
905 def hypot(x, y):
906 '''Compute the norm M{sqrt(x**2 + y**2)}.
908 @arg x: X argument (C{scalar}).
909 @arg y: Y argument (C{scalar}).
911 @return: C{sqrt(B{x}**2 + B{y}**2)} (C{float}).
912 '''
913 return float(_Hypot(x, y))
915 from math import hypot as hypot_ # PYCHOK in Python 3.8 and 3.9
916else:
917 from math import hypot # PYCHOK in Python 3.10+
918 hypot_ = hypot
921def _Hypot(*xs):
922 '''(INTERNAL) Substitute for inaccurate C{math.hypot}.
923 '''
924 return Fhypot(*xs, nonfinites=True, raiser=False) # f2product=True
927def hypot1(x):
928 '''Compute the norm M{sqrt(1 + x**2)}.
930 @arg x: Argument (C{scalar} or L{Fsum} or L{Fsum2Tuple}).
932 @return: Norm (C{float} or L{Fhypot}).
933 '''
934 h = _1_0
935 if x:
936 if _isFsum_2Tuple(x):
937 h = _Hypot(h, x)
938 h = float(h)
939 else:
940 h = hypot(h, x)
941 return h
944def hypot2(x, y, *x0_y0):
945 '''Compute the I{squared} norm M{x**2 + y**2}.
947 @arg x: X (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
948 @arg y: Y (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
949 @arg x0_y0: Optional reference C{(x0, y0)} (each C{scalar},
950 an L{Fsum} or L{Fsum2Tuple}).
952 @return: C{B{x}**2 + B{y}**2} (C{float}).
953 '''
954 x, y = _map0(abs, x, y, *x0_y0) # NOT fabs!
955 if x < y:
956 x, y = y, x
957 h2 = x**2
958 if h2 and y:
959 h2 *= (y / x)**2 + _1_0
960 return float(h2)
963def hypot2_(*xs):
964 '''Compute the I{squared} norm C{fsum(x**2 for x in B{xs})}.
966 @arg xs: Components (each C{scalar}, an L{Fsum} or
967 L{Fsum2Tuple}), all positional.
969 @return: Squared norm (C{float}).
971 @see: Class L{Fpowers} for further details.
972 '''
973 h2 = float(max(map(abs, xs))) if xs else _0_0
974 if h2: # and isfinite(h2)
975 _h = _1_0 / h2
976 xs = ((x * _h) for x in xs)
977 H2 = Fpowers(2, *xs, nonfinites=True) # f2product=True
978 h2 = H2.fover(_h**2)
979 return h2
982def _map0(_f, x, y, x0=_0_0, y0=_0_0, *unused): # PYLINT W1113
983 return _f(x - x0), _f(y - y0)
986def norm2(x, y, *x0_y0):
987 '''Normalize a 2-dimensional vector.
989 @arg x: X component (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
990 @arg y: Y component (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
991 @arg x0_y0: Optional reference C{(x0, y0)} (each C{scalar},
992 an L{Fsum} or L{Fsum2Tuple}).
994 @return: 2-Tuple C{(x, y)}, normalized.
996 @raise ValueError: Invalid B{C{x}} or B{C{y}}.
997 '''
998 try:
999 h = None
1000 x, y = _map0(float, x, y, *x0_y0)
1001 h = hypot(x, y)
1002 if h:
1003 t = (x / h), (y / h)
1004 else:
1005 t = (_copysign_0_0(x), # pass?
1006 _copysign_0_0(y))
1007 except Exception as X:
1008 raise _xError(X, x=x, y=y, h=h)
1009 return t
1012def norm_(*xs):
1013 '''Normalize the components of an n-dimensional vector.
1015 @arg xs: Components (each C{scalar}, an L{Fsum} or
1016 L{Fsum2Tuple}), all positional.
1018 @return: Yield each component, normalized.
1020 @raise ValueError: Invalid or insufficent B{C{xs}}
1021 or zero norm.
1022 '''
1023 try:
1024 i = h = None
1025 x = xs
1026 h = hypot_(*xs)
1027 _h = (_1_0 / h) if h else _0_0
1028 for i, x in enumerate(xs):
1029 yield x * _h
1030 except Exception as X:
1031 raise _xsError(X, xs, i, x, h=h) # PYLINT E0601
1034def polar2(x, y, *x0_y0):
1035 '''Return 2-tuple C{(length, angle)} with C{angle} in radians M{[0..+PI2)}.
1037 @arg x: X (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1038 @arg y: Y (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1039 @arg x0_y0: Optional reference C{(x0, y0)} (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1041 @note: Use C{polar(B{y}, B{x}, *B{yx0})} to get the angle as C{bearing} from North.
1042 '''
1043 x, y = _map0(float, x, y, *x0_y0)
1044 return hypot(x, y), _MODS.utily.atan2p(y, x)
1047def polar2d(x, y, *x0_y0):
1048 '''Return 2-tuple C{(length, angle)} with C{angle} in degrees M{[0..+360)}.
1050 @arg x: X (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1051 @arg y: Y (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1052 @arg x0_y0: Optional reference C{(x0, y0)} (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1054 @note: Use C{polar2d(B{y}, B{x}, *B{yx0})} to get the angle as C{bearing} from North.
1055 '''
1056 x, y = _map0(float, x, y, *x0_y0)
1057 return hypot(x, y), _MODS.utily.atan2b(y, x)
1060def _powers(x, n):
1061 '''(INTERNAL) Yield C{x**i for i=1..n}.
1062 '''
1063 p = 1 # type(p) == type(x)
1064 for _ in range(n):
1065 p *= x
1066 yield p
1069def _root(x, p, where):
1070 '''(INTERNAL) Raise C{x} to power C{0 <= p < 1}.
1071 '''
1072 try:
1073 if x > 0:
1074 r = Fsum(f2product=True, nonfinites=True)(x)
1075 return r.fpow(p).as_iscalar
1076 elif x < 0:
1077 raise ValueError(_negative_)
1078 except Exception as X:
1079 raise _xError(X, unstr(where, x))
1080 return _0_0 if p else _1_0 # x == 0
1083def sqrt0(x, Error=None):
1084 '''Return the square root C{sqrt(B{x})} iff C{B{x} > }L{EPS02},
1085 preserving C{type(B{x})}.
1087 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1088 @kwarg Error: Error to raise for negative B{C{x}}.
1090 @return: Square root (C{float} or L{Fsum}) or C{0.0}.
1092 @raise TypeeError: Invalid B{C{x}}.
1094 @note: Any C{B{x} < }L{EPS02} I{including} C{B{x} < 0}
1095 returns C{0.0}.
1096 '''
1097 if Error and x < 0:
1098 raise Error(unstr(sqrt0, x))
1099 return _root(x, _0_5, sqrt0) if x > EPS02 else (
1100 _0_0 if x < EPS02 else EPS0)
1103def sqrt3(x):
1104 '''Return the square root, I{cubed} M{sqrt(x)**3} or M{sqrt(x**3)},
1105 preserving C{type(B{x})}.
1107 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1109 @return: Square root I{cubed} (C{float} or L{Fsum}).
1111 @raise TypeeError: Invalid B{C{x}}.
1113 @raise ValueError: Negative B{C{x}}.
1115 @see: Functions L{cbrt} and L{cbrt2}.
1116 '''
1117 return _root(x, _1_5, sqrt3)
1120def sqrt_a(h, b):
1121 '''Compute the C{I{a}} side of a right-angled triangle from
1122 C{sqrt(B{h}**2 - B{b}**2)}.
1124 @arg h: Hypotenuse or outer annulus radius (C{scalar}).
1125 @arg b: Triangle side or inner annulus radius (C{scalar}).
1127 @return: C{copysign(I{a}, B{h})} or C{unsigned 0.0} (C{float}).
1129 @raise TypeError: Non-scalar B{C{h}} or B{C{b}}.
1131 @raise ValueError: If C{abs(B{h}) < abs(B{b})}.
1133 @see: Inner tangent chord B{I{d}} of an U{annulus
1134 <https://WikiPedia.org/wiki/Annulus_(mathematics)>}
1135 and function U{annulus_area<https://People.SC.FSU.edu/
1136 ~jburkardt/py_src/geometry/geometry.py>}.
1137 '''
1138 try:
1139 if not (_isHeight(h) and _isRadius(b)):
1140 raise TypeError(_not_scalar_)
1141 c = fabs(h)
1142 if c > EPS0:
1143 s = _1_0 - (b / c)**2
1144 if s < 0:
1145 raise ValueError(_h_lt_b_)
1146 a = (sqrt(s) * c) if 0 < s < 1 else (c if s else _0_0)
1147 else: # PYCHOK no cover
1148 b = fabs(b)
1149 d = c - b
1150 if d < 0:
1151 raise ValueError(_h_lt_b_)
1152 d *= c + b
1153 a = sqrt(d) if d else _0_0
1154 except Exception as x:
1155 raise _xError(x, h=h, b=b)
1156 return copysign0(a, h)
1159def zcrt(x):
1160 '''Return the 6-th, I{zenzi-cubic} root, M{x**(1 / 6)},
1161 preserving C{type(B{x})}.
1163 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1165 @return: I{Zenzi-cubic} root (C{float} or L{Fsum}).
1167 @see: Functions L{bqrt} and L{zqrt}.
1169 @raise TypeeError: Invalid B{C{x}}.
1171 @raise ValueError: Negative B{C{x}}.
1172 '''
1173 return _root(x, _1_6th, zcrt)
1176def zqrt(x):
1177 '''Return the 8-th, I{zenzi-quartic} or I{squared-quartic} root,
1178 M{x**(1 / 8)}, preserving C{type(B{x})}.
1180 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).
1182 @return: I{Zenzi-quartic} root (C{float} or L{Fsum}).
1184 @see: Functions L{bqrt} and L{zcrt}.
1186 @raise TypeeError: Invalid B{C{x}}.
1188 @raise ValueError: Negative B{C{x}}.
1189 '''
1190 return _root(x, _0_125, zqrt)
1192# **) MIT License
1193#
1194# Copyright (C) 2016-2026 -- mrJean1 at Gmail -- All Rights Reserved.
1195#
1196# Permission is hereby granted, free of charge, to any person obtaining a
1197# copy of this software and associated documentation files (the "Software"),
1198# to deal in the Software without restriction, including without limitation
1199# the rights to use, copy, modify, merge, publish, distribute, sublicense,
1200# and/or sell copies of the Software, and to permit persons to whom the
1201# Software is furnished to do so, subject to the following conditions:
1202#
1203# The above copyright notice and this permission notice shall be included
1204# in all copies or substantial portions of the Software.
1205#
1206# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1207# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1208# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1209# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1210# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1211# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1212# OTHER DEALINGS IN THE SOFTWARE.