Class Adder.Pairwise

java.lang.Object
org.bzdev.math.Adder
org.bzdev.math.Adder.Pairwise
Enclosing class:
Adder

public static final class Adder.Pairwise extends 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
    Constructor
    Description
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(double value)
    Add a single number to the summation.
    void
    add(double[] array)
    Add the elements of an array to the summation.
    void
    add(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.
    void
    add(Iterable<? extends Number> iterable)
    Add the values produced by an Iterable to the summation.
    double
    Get the sum.
    static double
    getSum(double[] array)
    Get the sum of the elements in an array.
    void
    Reset the class to sum a new set of values.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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: Adder
      Add the elements of an array to the summation.
      Specified by:
      add in class Adder
      Parameters:
      array - elements to add to the sum
    • add

      public void add(double[] array, int start, int end) throws IllegalArgumentException
      Description copied from class: Adder
      Add 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:
      add in class Adder
      Parameters:
      array - the array of elements to sum
      start - 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: Adder
      Add a single number to the summation.
      Specified by:
      add in class Adder
      Parameters:
      value - the value to add
    • add

      public void add(Iterable<? extends Number> iterable)
      Description copied from class: Adder
      Add 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.)
      Specified by:
      add in class Adder
      Parameters:
      iterable - the Iterable whose values will be summed
    • getSum

      public double getSum()
      Description copied from class: Adder
      Get the sum. Additional values can be subsequently added.
      Specified by:
      getSum in class Adder
      Returns:
      the sum
    • reset

      public void reset()
      Description copied from class: Adder
      Reset the class to sum a new set of values.
      Specified by:
      reset in class Adder
    • 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