java.lang.Object
org.bzdev.math.Adder
org.bzdev.math.Adder.Pairwise
- Enclosing class:
- Adder
Add a series of numbers using pairwise summation.
The values are recursively divided into two parts of about the
same size and summed. For efficiency, a small number of
values (64) are summed directly, eliminating approximately the
last 6 levels of recursive calls.
When the values to be summed are in an array, with no values to be added afterwards, this algorithm is only slightly less accurate than Kahan's algorithm, and is computationally less expensive.
The static method getSum(double[]) should be used if the sum of
the elements in an array should be immediately calculated, as this
method avoids allocating an object.
-
Nested Class Summary
Nested classes/interfaces inherited from class org.bzdev.math.Adder
Adder.Kahan, Adder.Pairwise -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(double value) Add a single number to the summation.voidadd(double[] array) Add the elements of an array to the summation.voidadd(double[] array, int start, int end) Add the elements of an array to the summation, specifying an offset into the array and the number of elements to process.voidAdd the values produced by an Iterable to the summation.doublegetSum()Get the sum.static doublegetSum(double[] array) Get the sum of the elements in an array.voidreset()Reset the class to sum a new set of values.
-
Constructor Details
-
Pairwise
public Pairwise()Constructor. This assumes a default capacity of 128.
-
-
Method Details
-
add
public void add(double[] array) Description copied from class:AdderAdd the elements of an array to the summation. -
add
Description copied from class:AdderAdd the elements of an array to the summation, specifying an offset into the array and the number of elements to process. The indices of the elements added are members of the interval [start,end).- Specified by:
addin classAdder- Parameters:
array- the array of elements to sumstart- the starting index (inclusive)end- the ending index (exclusive)- Throws:
IllegalArgumentException- the start and end values are not consistent with the array's size or are out of range or out of order
-
add
public void add(double value) Description copied from class:AdderAdd a single number to the summation. -
add
Description copied from class:AdderAdd the values produced by an Iterable to the summation. This method will add the elements of any object that can be used in a 'foreach' statement (the "for (TYPE value: object)" syntax) when TYPE is a subclass of Number (e.g., Double, Float, etc.) -
getSum
public double getSum()Description copied from class:AdderGet the sum. Additional values can be subsequently added. -
reset
public void reset()Description copied from class:AdderReset the class to sum a new set of values. -
getSum
public static double getSum(double[] array) Get the sum of the elements in an array. This is a shortcut method that avoids creating an object to handle the sum.- Parameters:
array- the array whose elements should be added together- Returns:
- the sum of the array's elements
-