Coverage for pygeodesy/fsums.py : 98%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: utf-8 -*-
Set env variable C{PYGEODESY_FSUM_RESIDUAL} to nay non-empty string to throw a L{ResidualError} for division or exponention by an L{Fsum} instance with a non-zero C{residual}, see method L{Fsum.fsum2}. ''' # make sure int/int division yields float quotient, see .basics
neg, signOf _TypeError, _ValueError, _xkwds_get, \ _ZeroDivisionError _iadd_, _not_, _SPACE_, _supported_, \ _0_0, _1_0, _N_1_0 # from pygeodesy.props import Property_RO, property_RO
'''(INTERNAL) Half-even rounding. ''' (r < 0 and p < 0): # signs match
'''(INTERNAL) Raise C{TypeError} or C{ValueError} if not scalar or infinite. ''' X, t = _ValueError, _not_(_finite_) except TypeError as x: X, t = _TypeError, str(x) except ValueError as x: X, t = _ValueError, str(x) except Exception as x: X, t = _NotImplementedError, repr(x) if index is not None: n = Fmt.SQUARE(n, index) raise X(n, v, txt=t)
'''(INTERNAL) Yield all items as C{float}s. ''' ps = map(neg, ps) else:
'''(INTERNAL) Return B{C{other}} as an L{Fsum}. '''
'''(INTERNAL) Precision C{2sum} of M{a + b} as 2-tuple (sum, residual). ''' raise _OverflowError(unstr(_2sum.__name__, a, b), txt=str(s))
'''Error raised for an operation involving an L{Fsum} with a non-zero residual. '''
'''Precision I{running} floating point summation similar to standard Python's C{math.fsum}.
Unlike C{math.fsum}, this class accumulates values and provides intermediate, I{running} precision floating point summation. Accumulation may continue after intermediate, I{running} summations.
@note: Handling of exceptions, C{inf}, C{INF}, C{nan} and C{NAN} values differs from standard Python's C{math.fsum}.
@note: Values to be accumulated are C{scalar} or L{fsum} instances with C{scalar} meaning type C{float}, C{int} or any type convertible to C{float}.
@see: U{Hettinger<https://GitHub.com/ActiveState/code/blob/master/recipes/Python/ 393090_Binary_floating_point_summatiaccurate_full/recipe-393090.py>}, U{Kahan<https://WikiPedia.org/wiki/Kahan_summation_algorithm>}, U{Klein<https://Link.Springer.com/article/10.1007/s00607-005-0139-x>}, Python 2.6+ file I{Modules/mathmodule.c} and the issue log U{Full precision summation<https://Bugs.Python.org/issue2819>}. '''
'''New L{Fsum} for I{running} precision floating point summation.
@arg xs: No, one or more initial values (C{scalar} or L{Fsum} instances). @kwarg name_NN: Optional name (C{str}).
@see: Method L{Fsum.fadd}. ''' # self._n = 0
'''Return this instance' absolute value as an L{Fsum}. '''
'''Return the sum C{B{self} + B{other}} as an L{Fsum}.
@arg other: An L{Fsum} or C{scalar}.
@return: The sum (L{Fsum}).
@see: Method L{Fsum.__iadd__}. '''
'''Return C{True} if this instance is non-zero. '''
'''Return this instance' C{math.ceil} as C{int} or C{float}Fsum(.
@return: An C{int}, except C{float} in Python 2.
@see: Methods L{Fsum.__floor__} and L{Fsum.__int__}. '''
'''Return C{divmod(B{self}, B{other})} as 2-tuple C{(quotient, remainder)}, an C{int} or C{float} and an L{Fsum}.
@arg other: An L{Fsum} or C{scalar} divisor.
@return: The quotient is C{int}, except C{float} in Python 2.
@see: Method L{Fsum.__itruediv__}. '''
'''Compare this with an other instance or scalar. '''
'''Return this instance as C{float}, without residual.
@see: Method L{Fsum.fsum2}. '''
'''Return this instance' C{math.floor} as C{int} or C{float}.
@return: An C{int}, except C{float} in Python 2.
@see: Methods L{Fsum.__ceil__} and L{Fsum.__int__}. '''
'''Return C{B{self} // B{other}} as an L{Fsum}.
@arg other: An L{Fsum} or C{scalar} divisor.
@return: The C{floor} quotient (L{Fsum}).
@see: Methods L{Fsum.__ifloordiv__}. '''
def __format__(self, *other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, *other)
'''Compare this with an other instance or scalar. '''
'''Compare this with an other instance or scalar. '''
def __hash__(self): # PYCHOK no cover '''Return this instance' C{hash}. ''' return hash(self._ps) # XXX id(self)?
'''Apply C{B{self} += B{other}} to this instance.
@arg other: An L{Fsum} or C{scalar}.
@return: This instance, updated (L{Fsum}).
@raise TypeError: Invalid B{C{other}} type.
@see: Method L{Fsum.fadd}. ''' raise self._TypeError(_iadd_, other)
'''Apply C{B{self} //= B{other}} to this instance.
@arg other: An L{Fsum} or C{scalar} divisor.
@return: This instance, updated (L{Fsum}).
@raise ResidualError: Non-zero residual in B{C{other}}.
@raise TypeError: Invalid B{C{other}} type.
@raise ValueError: Zero, invalid or non-finite B{C{other}}.
@see: Methods L{Fsum.__itruediv__}. '''
def __imatmul__(self, other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, other)
'''Apply C{B{self} %= B{other}} to this instance.
@arg other: An L{Fsum} or C{scalar} divisor.
@return: This instance, updated (L{Fsum}).
@raise TypeError: Invalid B{C{other}} type.
@see: Method L{Fsum.__divmod__}. '''
'''Apply C{B{self} *= B{other}} to this instance.
@arg other: An L{Fsum} or C{scalar} factor.
@return: This instance, updated (L{Fsum}).
@raise TypeError: Invalid B{C{other}} type.
@see: Method L{Fsum.fmul}. ''' else: else: raise self._TypeError(_imul_, other)
'''Return this instance as an C{int}.
@see: Methods L{Fsum.__ceil__} and L{Fsum.__floor__}. '''
'''Apply C{B{self} **= B{other}} to this instance.
@arg other: An L{Fsum} or C{scalar} exponent. @arg mod: Not implemented (C{scalar}).
@return: This instance, updated (L{Fsum}).
@raise NotImplementedError: Argument B{C{mod}} used.
@raise ResidualError: This residual non-zero and negative or fractional B{C{other}} or non-zero residual in B{C{other}}.
@raise TypeError: Invalid B{C{other}} type.
@raise ValueError: If B{C{other}} is a negative C{scalar} and this instance is C{0} or B{C{other}} is a fractional C{scalar} and this instance is negative or has a non-zero residual or B{C{other}} is an L{Fsum} with a non-zero residual.
@see: CPython function U{float_pow<https://GitHub.com/ python/cpython/blob/main/Objects/floatobject.c>}. '''
raise self._ResidualError(_ipow_, other, r) s *= p
'''Apply C{B{self} -= B{other}} to this instance.
@arg other: An L{Fsum} or C{scalar}.
@return: This instance, updated (L{Fsum}).
@raise TypeError: Invalid B{C{other}} type.
@see: Method L{Fsum.fadd}. ''' raise self._TypeError(_isub_, other)
'''Return an C{iter}ator over a C{partials} duplicate. '''
'''Apply C{B{self} /= B{other}} to this instance.
@arg other: An L{Fsum} or C{scalar} divisor.
@return: This instance, updated (L{Fsum}).
@raise TypeError: Invalid B{C{other}} type.
@raise ValueError: Invalid or non-finite B{C{other}} or an B{C{other}} is an L{Fsum} with a non-zero residual.
@raise ZeroDivisionError: Zero B{C{other}}.
@see: Method L{Fsum.__ifloordiv__}. '''
'''Compare this with an other instance or scalar. '''
'''Return the I{total} number of value accumulated (C{int}). '''
'''Compare this with an other instance or scalar. '''
def __matmul__(self, other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, other)
'''Return C{B{self} % B{other}} as an L{Fsum}.
@see: Method L{Fsum.__imod__}. '''
'''Return C{B{self} * B{other}} as an L{Fsum}.
@see: Method L{Fsum.__imul__}. '''
'''Compare this with an other instance or scalar. '''
'''Return I{a copy of} this instance, negated. '''
def __pos__(self): # PYCHOK no cover '''Return this instance, I{as-is}. ''' return self.fcopy(name=self.__pos__.__name__)
'''Return C{B{self} ** B{other}} as an L{Fsum}.
@see: Method L{Fsum.__ipow__}. '''
'''Return C{B{other} + B{self}} as an L{Fsum}.
@see: Method L{Fsum.__iadd__}. '''
'''Return C{divmod(B{other}, B{self})} as 2-tuple C{(quotient, remainder)}, an C{int} or C{float} and an L{Fsum}.
@return: The quotient is C{int}, except C{float} in Python 2.
@see: Method L{Fsum.__divmod__}. '''
# def __repr__(self): # '''Return the default C{repr(this)}. # ''' # return self.toRepr()
'''Return C{B{other} // B{self}} as an L{Fsum}.
@see: Method L{Fsum.__ifloordiv__}. '''
def __rmatmul__(self, other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, other)
'''Return C{B{other} % B{self}} as an L{Fsum}.
@see: Method L{Fsum.__imod__}. '''
'''Return C{B{other} * B{self}} as an L{Fsum}.
@see: Method L{Fsum.__imul__}. '''
def __round__(self, ndigits=None): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, ndigits=ndigits)
'''Return C{B{other} ** B{self}} as an L{Fsum}.
@see: Method L{Fsum.__ipow__}. '''
'''Return C{B{other} - B{self}} as L{Fsum}.
@see: Method L{Fsum.__isub__}. '''
'''Return C{B{other} / B{self}} as an L{Fsum}.
@see: Method L{Fsum.__itruediv__}. '''
'''Return the size of this instance in C{bytes}. ''' self._n, self._ps, *self._ps))
'''Return the default C{str(this)}. '''
'''Return C{B{self} - B{other}} as an L{Fsum}.
@arg other: An L{Fsum} or C{scalar}.
@return: The difference (L{Fsum}).
@see: Method L{Fsum.__isub__}. '''
'''Return C{B{self} / B{other}} as an L{Fsum}.
@arg other: An L{Fsum} or C{scalar} divisor.
@return: The quotient (L{Fsum}).
@see: Method L{Fsum.__itruediv__}. '''
if _sys_version_info2 < (3, 0): # PYCHOK no cover # <https://docs.Python.org/2/library/operator.html#mapping-operators-to-functions> __div__ = __truediv__ __idiv__ = __itruediv__ __long__ = __int__ __nonzero__ = __bool__ __rdiv__ = __rtruediv__
'''Get this instance' C{ceil} value (C{int}, except C{float} in Python 2).
@see: Read-only properties L{Fsum.floor}, L{Fsum.imag} and L{Fsum.real}. '''
'''(INTERNAL) Subtract an B{C{other}} instance or scalar and return an L{Fsum2Tuple}C{(fsum, residual)} for comparison operator B{C{cop}}. ''' else: raise self._TypeError(cop, other) else:
'''(INTERNAL) Fast, un-named copy. '''
'''Return C{divmod(B{self}, B{other})} as 2-tuple C{(quotient, remainder)}, an C{int} or C{float} and an L{Fsum}.
@arg other: An L{Fsum} or C{scalar} divisor.
@return: The quotient is C{int}, except C{float} in Python 2.
@see: Method L{Fsum.__itruediv__}. '''
'''(INTERNAL) C{divmod(B{self}, B{other})} as 2-tuple (C{int}, L{Fsum}). ''' # mostly like like CPython function U{float_divmod # <https://GitHub.com/python/cpython/blob/main/Objects/floatobject.c>}, # but at least _divmod(-3, 2) equals Cpython's result (-2, 1).
f += other i -= 1
# t = f.signOf() # if t and t != s: # from pygeodesy.errors import _AssertionError # raise f._Error(_dimo_, other, _AssertionError, txt=signOf.__name__)
'''(INTERNAL) Format an B{C{Error}} for C{{self} B{op} B{other}}. '''
'''Accumulate more scalar values from an iterable.
@arg xs: Iterable, list, tuple, etc. (C{scalar} or L{Fsum} instances).
@return: This instance (L{Fsum}).
@raise OverflowError: Partial C{2sum} overflow.
@raise TypeError: Non-scalar B{C{xs}} value.
@raise ValueError: Invalid or non-finite B{C{xs}} value. ''' else: # ... backward compatibility self._fadd_(_2float(xs=xs)) # PYCHOK no cover
'''Accumulate more I{scalar} values from positional arguments.
@arg xs: Values to add (C{scalar} or L{Fsum} instances), all positional.
@return: This instance (L{Fsum}).
@raise OverflowError: Partial C{2sum} overflow.
@raise TypeError: Non-scalar B{C{xs}} value.
@raise ValueError: Invalid or non-finite B{C{xs}} value. '''
'''(INTERNAL) Accumulate more I{known} C{scalar}s. ''' # assert self._ps is ps
'''(INTERNAL) Add all positional, I{known} C{scalar}s. '''
'''(INTERNAL) Adjust the C{partials}, by removing and re-adding the final C{partial}. ''' else:
'''Copy this instance, C{shallow} or B{C{deep}}.
@return: The copy (L{Fsum}). ''' # f._update(other=self)
'''(INTERNAL) Divide this instance by a C{scalar} only. ''' except ZeroDivisionError as x: raise self._ZeroDivisionError(op, d, txt=str(x)) except (TypeError, ValueError) as x: raise self._ValueError(op, d, txt=str(x))
'''Get this instance' C{floor} (C{int}, except C{float} in Python 2).
@see: Read-only properties L{Fsum.ceil}, L{Fsum.imag} and L{Fsum.real}. '''
'''(INTERNAL) Multiply this instance by a C{scalar} only. ''' # multiply and adjust partials else: # assert self._ps is ps
'''Subtract several values.
@arg xs: Iterable, list, tuple. etc. (C{scalar} or L{Fsum} instances).
@return: This instance, updated (L{Fsum}).
@see: Method L{Fsum.fadd}. '''
'''Subtract any positional value.
@arg xs: Values to subtract (C{scalar} or L{Fsum} instances), all positional.
@return: This instance, updated (L{Fsum}).
@see: Method L{Fsum.fadd}. '''
'''Add several values or C{None} and sum all.
@kwarg xs: Iterable, list, tuple, etc. (C{scalar} or L{Fsum} instances).
@return: Precision running sum (C{float}).
@raise OverflowError: Partial C{2sum} overflow.
@raise TypeError: Non-scalar B{C{xs}} value.
@raise ValueError: Invalid or non-finite B{C{xs}} value.
@note: Accumulation can continue after summation. '''
'''(INTERNAL) Compute and cache the current L{Fsum.fsum}. ''' else: else: # PYCHOK no cover ps[i] = s = p # swap s and p and continue # assert self._ps is ps
'''Add any positional value and sum all.
@arg xs: Values to add (C{scalar} or L{Fsum} instances), all positional.
@return: Precision running sum (C{float}).
@see: Method L{Fsum.fsum}. '''
'''Add several values or C{None} and return the running sum and the residual.
@kwarg xs: Iterable, list, tuple, etc. (C{scalar} or L{Fsum} instances).
@return: L{Fsum2Tuple}C{(fsum, residual)} with the running C{fsum} and the C{residual}, the sum of the remaining C{partials}.
@see: Methods L{Fsum.fsum} and L{Fsum.fsum2_} ''' self._fadd(_2floats(xs))
'''(INTERNAL) Cache the L{Fsum.fsum2} result. '''
'''Add any positional value and return the precision running sum and the difference.
@arg xs: Values to add (C{scalar} or L{Fsum} instances), all positional.
@return: 2-Tuple C{(sum, delta)} with the precision running C{sum} and the difference C{delta} with the prior running C{sum} (C{float}s).
@see: Method L{Fsum.fsum_}. '''
'''(INTERNAL) Apply C{B{self} /= B{other}}. ''' else: raise self._ResidualError(op, other, r) # self / (d + r) == self * n / d # n = d / (d + r) = 1 / (1 + r / d) # d' = d / n = d * (1 + r / d), but # is pointless if (1 + r / d) == 1 else: # PYCHOK no cover d = r else: raise self._TypeError(op, other)
'''Return the C{imaginary} part of this instance (C{0.0}, always).
@see: Read-only properties L{Fsum.ceil}, L{Fsum.floor} and L{Fsum.real}. '''
'''(INTERNAL) Negate this instance. '''
'''(INTERNAL) Overwrite this instance with an other or a C{scalar}. ''' else: # PYCHOK no cover raise self._TypeError(_iset_, other)
'''Return C{True} if this instance is an integer, C{False} otherwise. '''
'''Get this instance' partial sums (C{tuple} of C{float}s). '''
'''Return C{B{self} ** B{x}} as L{Fsum}.
@arg x: The exponent (C{scalar} or L{Fsum} instance).
@return: The C{pow(self, B{x})} (L{Fsum}).
@raise ResidualError: This residual non-zero and negative or fractional B{C{x}}.
@raise TypeError: Non-scalar B{C{x}}.
@raise ValueError: Invalid or non-finite B{C{factor}}.
@see: Method L{Fsum.__ipow__}. ''' while True: else: else: # scalar B{C{x}} only
'''(INTERNAL) Return C{self ** B{x}} for C{scalar B{x}} only. ''' s, r = s._fsum2 if r: raise self._ResidualError(_ipow_, other, r) # use **= -1 for the CPython float_pow # error if s is zero, and not s = 1 / s x = -1 else: raise self._ResidualError(_ipow_, other, r) else: # x == 1 or x == 0 raise self._TypeError(_ipow_, other) raise self._ResidualError(_ipow_, other, r) raise self._ValueError(_ipow_, other, txt=_not_(_supported_)) except Exception as e: raise self._ValueError(_ipow_, other, txt=str(e))
'''(INTERNAL) Yield partials, pseudo-sorted, 1-primed minus C{less} if non-zero. '''
'''(INTERNAL) Yield partials, multiplied by B{C{factor}}. '''
'''Return the C{real} part of this instance (C{float}).
@see: Method L{Fsum.__float__} and read-only properties L{Fsum.ceil}, L{Fsum.floor} and L{Fsum.imag}. '''
'''(INTERNAL) Non-zero residual C{ValueError}. '''
'''Determine the sign of this instance.
@kwarg res: If C{True} include the residual, otherwise ignore (C{bool}).
@return: The sign (C{int}, -1, 0 or +1). '''
'''Return this C{Fsum} instance as representation.
@kwarg prec: The C{float} precision, number of decimal digits (0..9). Trailing zero decimals are stripped for B{C{prec}} values of 1 and above, but kept for negative B{C{prec}} values. @kwarg sep: Optional separator to join (C{str}). @kwarg fmt: Optional, C{float} format (C{str}).
@return: This instance (C{str}). '''
'''Return this C{Fsum} instance as string.
@kwarg prec: The C{float} precision, number of decimal digits (0..9). Trailing zero decimals are stripped for B{C{prec}} values of 1 and above, but kept for negative B{C{prec}} values. @kwarg sep: Optional separator to join (C{str}). @kwarg fmt: Optional, C{float} format (C{str}).
@return: This instance (C{repr}). '''
'''(INTERNAL) Operand C{TypeError}. ''' return self._Error(op, other, _TypeError, **txt)
'''(INTERNAL) Copy, set or zap all cached C{Property_RO} values. ''' else: # copy if present, otherwise zap # Property_RO ._fsum and ._fsum2 can not be a Property since # Property's _fset zaps the value just set by the @setter plus # the Property and the value attribute are named the same
def _ValueError(self, op, other, **txt): # PYCHOK no cover '''(INTERNAL) Return a C{ValueError}. ''' return self._Error(op, other, _ValueError, **txt)
def _ZeroDivisionError(self, op, other, **txt): # PYCHOK no cover '''(INTERNAL) Return a C{ZeroDivisionError}. ''' return self._Error(op, other, _ZeroDivisionError, **txt)
'''(INTERNAL) Unit of L{Fsum2Tuple} items. '''
'''2-Tuple C{(fsum, residual)} with the precision running C{fsum} and the C{residual}, the sum of the remaining partials if any. Each item is either C{float} or an C{int}. '''
# make sure _fsum works as expected (XXX check # float.__getformat__('float')[:4] == 'IEEE'?) if _fsum((1, 1e101, 1, -1e101)) != 2: # PYCHOK no cover del _fsum # nope, remove _fsum ... raise ImportError # ... use _fsum below
except ImportError:
def _fsum(xs): '''(INTERNAL) Precision summation, Python 2.6-. ''' return Fsum(name=_fsum.__name__)._fadd(xs)._fsum
'''Precision floating point summation similar to Python's C{math.fsum}.
Exception and I{non-finite} handling differ from C{math.fsum}.
@arg xs: Iterable, list, tuple, etc. of values (C{scalar} or L{Fsum} instances).
@return: Precision C{sum} (C{float}).
@raise OverflowError: Partial C{2sum} overflow.
@raise TypeError: Non-scalar B{C{xs}} value.
@raise ValueError: Invalid or non-finite B{C{xs}} value.
@see: Class L{Fsum} and methods L{Fsum.fsum} and L{Fsum.fadd}. '''
'''Precision floating point summation of all positional arguments.
@arg xs: Values to add (C{scalar} or L{Fsum}instances), all positional.
@return: Precision C{sum} (C{float}).
@see: Function C{fsum}. '''
'''Precision floating point summation of a few values, 1-primed.
@arg xs: Iterable, list, tuple, etc. of values (C{scalar} or L{Fsum} instances).
@return: Precision C{sum} (C{float}).
@see: Function C{fsum}. '''
'''Precision floating point summation of a few arguments, 1-primed.
@arg xs: Values to add (C{scalar} or L{Fsum} instances), all positional.
@return: Precision C{sum} (C{float}).
@see: Function C{fsum} '''
# **) MIT License # # Copyright (C) 2016-2022 -- mrJean1 at Gmail -- All Rights Reserved. # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), # to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. |