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 -*-
''' # make sure int/int division yields float quotient, see .basics
_TypeError, _ValueError, _xkwds_get _SPACE_, _supported_, _0_0, _1_0, _N_1_0 # from pygeodesy.props import property_RO # from .units
'''(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) Return B{C{x}} as L{Fsum} instance. ''' Fsum(x, name=name)
'''(INTERNAL) Precision C{2sum} of M{a + b} as 2-tuple (sum, residual). ''' raise _OverflowError(unstr(_2sum.__name__, a, b), txt=str(s))
'''Precision summation similar to standard Python function C{math.fsum}.
Unlike C{math.fsum}, this class accumulates the values and provides intermediate, precision running sums. Accumulation may continue after intermediate summations.
@note: Handling of exceptions, C{inf}, C{INF}, C{nan} and C{NAN} values is different from C{math.fsum}.
@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>}. '''
'''Initialize a new accumulator with one or more start values.
@arg starts: No, one or more start values (C{scalar}s). @kwarg name: Optional name (C{str}).
@raise OverflowError: Partial C{2sum} overflow.
@raise TypeError: Non-scalar B{C{starts}} value.
@raise ValueError: Invalid or non-finite B{C{starts}} value. ''' # self._n = 0
'''Return absolute value of this instance. '''
'''Sum of this and a scalar or an other instance.
@arg other: L{Fsum} instance or C{scalar}.
@return: The sum (L{Fsum}).
@see: Method L{Fsum.__iadd__}. '''
'''Is this instance non-zero?. '''
'''Return the C{ceil} of this instance as C{float}.
@see: Methods L{__floor__} and L{__int__}. '''
'''Return C{divmod(this_instance, B{other})} as 2-tuple C{(quotient, remainder)} both C{float}.
@see: Method L{__itruediv__}. '''
'''Compare this and an other instance or scalar. '''
'''Convert this instance to C{float} as C{float(self.fsum())}. '''
'''Return the C{floor} of this instance as C{float}.
@see: Methods L{__ceil__} and L{__int__}. '''
def __floordiv__(self, other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, other)
def __format__(self, *other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, *other)
'''Compare this and an other instance or scalar. '''
'''Compare this and an other instance or scalar. '''
def __hash__(self): # PYCHOK no cover '''Return the C{hash} of this instance. ''' return hash(self._ps)
'''Add a scalar or an other instance to this instance.
@arg other: L{Fsum} instance or C{scalar}.
@return: This instance, updated (L{Fsum}).
@raise TypeError: Invalid B{C{other}} type.
@see: Method L{Fsum.fadd}. ''' else: else: raise self._Error(_iadd_, other)
def __imatmul__(self, other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, other)
'''Multiply this instance by a scalar or an other instance.
@arg other: L{Fsum} instance or C{scalar}.
@return: This instance, updated (L{Fsum}).
@raise TypeError: Invalid B{C{other}} type.
@see: Method L{Fsum.fmul}. ''' else: # PYCHOK no cover self._iset(_0_0) else: raise self._Error(_imul_, other)
'''Convert this instance to C{int} as C{int(self.fsum() + partials)}.
@see: Methods L{__ceil__} and L{__floor__}. ''' (s < 0 and p > s) else s)
'''Raise this instance to power B{C{other}}.
@arg other: The exponent (C{scalar}). @arg mod: Not implemented (C{scalar}).
@return: This instance, updated (L{Fsum}).
@raise NotImplementedError: Argument B{C{mod}} used.
@raise TypeError: Non-scalar B{C{other}}.
@raise ValueError: Fractional B{C{other}} and this instance is negative or has a non-zero C{residual} or negative B{C{other}} and this instance C{0}.
@see: CPython function U{float_pow<https://GitHub.com/ python/cpython/blob/main/Objects/floatobject.c>}. ''' raise self._Error(_ipow_, other)
s, r = s.fsum2() if r: raise self._Erres(_ipow_, other) # use **= -1 for the CPython float_pow # error if s is zero, and not s = 1 / s x = -1 else: raise self._Erres(_ipow_, other) else: # x == 0 or x == 1 raise self._Erres(_ipow_, other) raise self._Error(_ipow_, other, Error=_ValueError, txt=_not_(_supported_)) except Exception as X: raise self._Error(_ipow_, other, Error=_ValueError, txt=str(X))
'''Subtract a scalar or an other instance from this instance.
@arg other: L{Fsum} instance or C{scalar}.
@return: This instance, updated (L{Fsum}).
@raise TypeError: Invalid B{C{other}} type.
@see: Method L{Fsum.fadd}. ''' else: else: raise self._Error(_isub_, other)
'''Devide this instance by a I{scalar} divisor only.
@arg other: The denominator (C{scalar}).
@raise TypeError: Non-scalar B{C{other}}.
@return: This instance, updated (L{Fsum}).
@raise ValueError: Zero, invalid or non-finite B{C{other}} or an L{Fsum} B{C{other}} with non-zero C{partials}.
@raise ZerDivisionError: Zero B{C{other}}. ''' else: raise _ValueError(_SPACE_(self, _idiv_, f.toRepr()), txt=_SPACE_(_non_zero_, _residual_)) # d == 0 throws ZeroDivisionError else: raise self._Error(_idiv_, other)
'''Compare this and an other instance or scalar. '''
'''Return the I{total} number of accumulated values (C{int}). '''
'''Compare this and an other instance or scalar. '''
def __matmul__(self, other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, other)
'''Return C{this_instance % B{other}}.
@see: Method L{__divmod__}. '''
'''Product of this and an other instance or a scalar.
@arg other: L{Fsum} instance or C{scalar}.
@return: The product (L{Fsum}).
@see: Method L{Fsum.__imul__}. '''
'''Compare this and an other instance or scalar. '''
'''Return a copy of this instance, negated. '''
def __pos__(self): # PYCHOK no cover '''Return this instance, I{as-is}. ''' return self
'''Return C{self ** other} as L{Fsum}, see L{Fsum.__ipow__}.'''
def __rdivmod__ (self, other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, other)
def __rfloordiv__(self, other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, other)
def __rmatmul__(self, other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, other)
def __rmod__(self, other): # PYCHOK no cover '''Return C{other % self} as L{Fsum}.''' f = _2Fsum(other, name=self.__rmod__.__name__) _, m = divmod(f, self) # %= chokes PyChecker return m
def __round__(self, ndigits=None): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, ndigits=ndigits)
def __rpow__(self, other, *mod): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, other, *mod)
'''Return C{other - this} as L{Fsum}.'''
def __rtruediv__(self, other): # PYCHOK no cover '''Return C{other / self} as L{Fsum}.''' f = _2Fsum(other, name=self.__rtruediv__.__name__) f.__itruediv__(self) # /= chokes PyChecker return f
def __sizeof__(self): # PYCHOK no cover '''Size of this instance in C{bytes}. ''' return sum(p.__sizeof__() for p in (self._ps + [self._ps]))
'''Default C{str(self)}. '''
'''Difference of this and an other instance or a scalar.
@arg other: L{Fsum} instance or C{scalar}.
@return: The difference (L{Fsum}).
@see: Method L{Fsum.__isub__}. '''
'''Quotient of this instance and a I{scalar}.
@arg other: The denominator (C{scalar}).
@raise TypeError: Non-scalar B{C{other}}.
@raise ValueError: Zero, invalid or non-finite B{C{other}} or an L{Fsum} B{C{other}} with too many C{partials}.
@return: The quotient (L{Fsum}). '''
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__
'''(INTERNAL) Diff this and another instance or C{0}. ''' fsum(p for p in f if p < 0)) if f else _0_0
'''(INTERNAL) Return an residual/partials C{ValueError}. ''' txt=_SPACE_(_non_zero_, _residual_))
'''(INTERNAL) Return an operation B{C{Error}}. '''
'''(INTERNAL) Override this instance with an other. ''' elif isinstance(other, Fsum): # PYCHOK no cover if other is not self: self._fsum2_ = other._fsum2_ self._n = other._n self._ps[:] = other._ps else: raise self._Error(_isub_, other)
'''Accumulate more scalar values from an iterable.
@arg xs: Iterable, list, tuple, etc. (C{scalar}s).
@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. ''' xs = (xs,) # PYCHOK no cover
# assert self._ps is ps
'''Accumulate more I{scalar} values from positional arguments.
@arg xs: Values to add (C{scalar}s), 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. '''
'''Copy this instance, C{shallow} or B{C{deep}}.
@return: The copy (L{Fsum}). ''' # f._fsum2_ = self._fsum2_
'''Devide this instance by a I{scalar}.
@arg divisor: The denominator (C{scalar}).
@raise TypeError: Non-scalar B{C{divisor}}.
@raise ValueError: Zero, invalid or non-finite B{C{divisor}}.
@return: This instance (L{Fsum}). ''' except (TypeError, ValueError, ZeroDivisionError) as x: raise self._Error(_idiv_, divisor, Error=_ValueError, txt=str(x))
'''Multiple this instance by a I{scalar}.
@arg factor: The multiplier (C{scalar}).
@raise TypeError: Non-scalar B{C{factor}}.
@raise ValueError: Invalid or non-finite B{C{factor}}.
@return: This instance (L{Fsum}).
@see: Method L{Fsum.fadd}. ''' # multiply and adjust partial sums # assert self._ps is ps
'''Subtract several I{scalar} values.
@arg xs: Iterable, list, tuple. etc. (C{scalar}s).
@return: This instance (L{Fsum}).
@see: Method L{Fsum.fadd}. '''
'''Subtract all I{scalar} positional values.
@arg xs: Values to subtract (C{scalar}s), all positional.
@return: This instance (L{Fsum}).
@see: Method L{Fsum.fadd}. '''
'''Accumulate more I{scalar} values and sum all.
@kwarg xs: Iterable, list, tuple, etc. (C{scalar}s).
@return: Accurate, 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. '''
else: # assert self._ps is ps
'''Accumulate all I{scalar} positional values and sum all.
@arg xs: Values to add (C{scalar}s), all positional.
@return: Accurate, running sum (C{float}).
@see: Method L{Fsum.fsum}.
@note: Accumulation can continue after summation. '''
'''Accumulate more I{scalar} values and return the sum and residual.
@kwarg xs: Iterable, list, tuple, etc. (C{scalar}s).
@return: L{Fsum2Tuple}C{(fsum, residual)} with the accurate, running C{fsum} and C{residual} the precision sum of the remaining partials.
@see: Methods L{Fsum.fsum} and L{Fsum.fsum2_} '''
'''Accumulate all I{scalar} positional values and provide the sum and delta.
@arg xs: Values to add (C{scalar}s), all positional.
@return: 2-Tuple C{(sum, delta)} with the accurate, running C{sum} and the C{delta} with the previous running C{sum} (C{float}s).
@see: Method L{Fsum.fsum_}.
@note: Accumulation can continue after summation. '''
'''Return the imaginary part of this instance. '''
'''Return C{True} if this instance is an integer. '''
'''Return the real part of this instance. '''
'''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}). '''
'''2-Tuple C{(fsum, residual)} with the accurate, running C{fsum} and C{residual} the precision sum of the remaining partials, both C{float}. '''
# 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): '''Precision summation similar to standard Python function C{math.fsum}.
Exception and I{non-finite} handling differs from C{math.fsum}.
@arg xs: Iterable, list, tuple, etc. of values (C{scalar}s).
@return: Accurate 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 method L{Fsum.fsum}. ''' return Fsum(name=fsum.__name__).fsum(xs) if xs else _0_0
'''Precision summation of all positional arguments.
@arg xs: Values to be added (C{scalar}s).
@return: Accurate L{fsum} (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. '''
'''Precision summation, primed with C{1.0}.
@arg xs: Iterable, list, tuple, etc. of values (C{scalar}s).
@return: Accurate L{fsum} (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. '''
'''Precision summation of a few arguments, primed with C{1.0}.
@arg xs: Values to be added (C{scalar}s), all positional.
@return: Accurate L{fsum} (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. '''
# **) 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. |