Class Adder.Kahan

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

public static final class Adder.Kahan extends Adder
Add a series of values using the Kahan summation algorithm. A description of the algorithm can be found in a Wikipedia article. This class is intended for cases were additional numerical accuracy better than summing the elements of a vector in the order in which they appear is needed. Accuracy is higher than when the Pairwise class is used, at the cost of some additional computation per element being summed.

The documentation for the class Adder.Kahan.State describes how to use the Kahan summation algorithm directly (e.g., to avoid the overhead of some method calls).

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Encapsulate the state of an instance of Adder.Kahan.

    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.
    Get the state of this adder.
    double
    Get the sum.
    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

    • Kahan

      public Kahan()
      Constructor.
  • Method Details

    • getState

      public Adder.Kahan.State getState()
      Get the state of this adder.
      Returns:
      this adder's state
      See Also:
    • 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)
      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)
    • 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