A class for summation of floating point numbers.
Modulemath-functions-0.3.4.4Haskell2010
Numeric.Sum
Functions for summing floating point numbers more accurately than
the naive sum function and its counterparts in the
vector package and elsewhere.
When used with floating point numbers, in the worst case, the sum function accumulates numeric error at a rate proportional to the number of values being summed. The algorithms in this module implement different methods of /compensated summation/, which reduce the accumulation of numeric error so that it either grows much more slowly than the number of inputs (e.g. logarithmically), or remains constant.
- 3 types
- 1 class
- 5 values
- Packagemath-functions-0.3.4.4
- Exports9
- LanguageHaskell2010
- LicenceBSD-2-Clause
- SourceSum.hs
Summation type class
2 declarationsO(n) Sum a vector of values.
Usage
Most of these summation algorithms are intended to be used via the Summation typeclass interface. Explicit type annotations should not be necessary, as the use of a function such as kbn or kb2 to extract the final sum out of a Summation instance gives the compiler enough information to determine the precise type of summation algorithm to use.
As an example, here is a (somewhat silly) function that manually computes the sum of elements in a list.
sillySumList :: [Double] -> Double
sillySumList = loop zero
where loop s [] = kbn s
loop s (x:xs) = seq s' loop s' xs
where s' = add s x
In most instances, you can simply use the much more general sum function instead of writing a summation function by hand.
-- Avoid ambiguity around which sum function we are using.
import Prelude hiding (sum)
--
betterSumList :: [Double] -> Double
betterSumList xs = sum kbn xs
Kahan-Babuška-Neumaier summation
2 declarationsKahan-Babuška-Neumaier summation. This is a little more computationally costly than plain Kahan summation, but is always at least as accurate.
Instances12Eq, Data, Show, Semigroup, Monoid, NFData, …
Eq KBNSumDefined in math-functions-0.3.4.4 · Numeric.SumData KBNSumDefined in math-functions-0.3.4.4 · Numeric.SumShow KBNSumDefined in math-functions-0.3.4.4 · Numeric.SumSemigroup KBNSumDefined in math-functions-0.3.4.4 · Numeric.SumMonoid KBNSumDefined in math-functions-0.3.4.4 · Numeric.SumNFData KBNSumDefined in math-functions-0.3.4.4 · Numeric.SumUnbox KBNSumDefined in math-functions-0.3.4.4 · Numeric.SumSummation KBNSumDefined in math-functions-0.3.4.4 · Numeric.SumVector Vector KBNSumDefined in math-functions-0.3.4.4 · Numeric.SumMVector MVector KBNSumDefined in math-functions-0.3.4.4 · Numeric.Sumdata MVector s KBNSumDefined in math-functions-0.3.4.4 · Numeric.Sumdata Vector KBNSumDefined in math-functions-0.3.4.4 · Numeric.Sum
Return the result of a Kahan-Babuška-Neumaier sum.
Order-2 Kahan-Babuška summation
2 declarationsSecond-order Kahan-Babuška summation. This is more computationally costly than Kahan-Babuška-Neumaier summation, running at about a third the speed. Its advantage is that it can lose less precision (in admittedly obscure cases).
This method compensates for error in both the sum and the first-order compensation term, hence the use of "second order" in the name.
Instances12Eq, Data, Show, Semigroup, Monoid, NFData, …
Eq KB2SumDefined in math-functions-0.3.4.4 · Numeric.SumData KB2SumDefined in math-functions-0.3.4.4 · Numeric.SumShow KB2SumDefined in math-functions-0.3.4.4 · Numeric.SumSemigroup KB2SumDefined in math-functions-0.3.4.4 · Numeric.SumMonoid KB2SumDefined in math-functions-0.3.4.4 · Numeric.SumNFData KB2SumDefined in math-functions-0.3.4.4 · Numeric.SumUnbox KB2SumDefined in math-functions-0.3.4.4 · Numeric.SumSummation KB2SumDefined in math-functions-0.3.4.4 · Numeric.SumVector Vector KB2SumDefined in math-functions-0.3.4.4 · Numeric.SumMVector MVector KB2SumDefined in math-functions-0.3.4.4 · Numeric.Sumdata MVector s KB2SumDefined in math-functions-0.3.4.4 · Numeric.Sumdata Vector KB2SumDefined in math-functions-0.3.4.4 · Numeric.Sum
Return the result of an order-2 Kahan-Babuška sum.
Less desirable approaches
0 declarationsKahan summation
Kahan summation. This is the least accurate of the compensated summation methods. In practice, it only beats naive summation for inputs with large magnitude. Kahan summation can be less accurate than naive summation for small-magnitude inputs.
This summation method is included for completeness. Its use is not recommended. In practice, KBNSum is both 30% faster and more accurate.
Instances12Eq, Data, Show, Semigroup, Monoid, NFData, …
Eq KahanSumDefined in math-functions-0.3.4.4 · Numeric.SumData KahanSumDefined in math-functions-0.3.4.4 · Numeric.SumShow KahanSumDefined in math-functions-0.3.4.4 · Numeric.SumSemigroup KahanSumDefined in math-functions-0.3.4.4 · Numeric.SumMonoid KahanSumDefined in math-functions-0.3.4.4 · Numeric.SumNFData KahanSumDefined in math-functions-0.3.4.4 · Numeric.SumUnbox KahanSumDefined in math-functions-0.3.4.4 · Numeric.SumSummation KahanSumDefined in math-functions-0.3.4.4 · Numeric.SumVector Vector KahanSumDefined in math-functions-0.3.4.4 · Numeric.SumMVector MVector KahanSumDefined in math-functions-0.3.4.4 · Numeric.Sumdata MVector s KahanSumDefined in math-functions-0.3.4.4 · Numeric.Sumdata Vector KahanSumDefined in math-functions-0.3.4.4 · Numeric.Sum
Return the result of a Kahan sum.
Pairwise summation
O(n) Sum a vector of values using pairwise summation.
This approach is perhaps 10% faster than KBNSum, but has poorer bounds on its error growth. Instead of having roughly constant error regardless of the size of the input vector, in the worst case its accumulated error grows with O(log n).
References
0 declarationsKahan, W. (1965), Further remarks on reducing truncation errors. Communications of the ACM 8(1):40.
Neumaier, A. (1974), Rundungsfehleranalyse einiger Verfahren zur Summation endlicher Summen. Zeitschrift für Angewandte Mathematik und Mechanik 54:39–51.
Klein, A. (2006), A Generalized Kahan-Babuška-Summation-Algorithm. Computing 76(3):279-293.
Higham, N.J. (1993), The accuracy of floating point summation. SIAM Journal on Scientific Computing 14(4):783–799.