Class RealSum

java.lang.Object
net.imglib2.util.RealSum

public class RealSum extends Object
RealSum implements a method to reduce numerical instabilities when summing up a very large number of double precision numbers. Numerical problems occur when a small number is added to an already very large sum. In such case, the reduced accuracy of the very large number may lead to the small number being entirely ignored. The method here stores and updates intermediate sums for all power of two elements such that the final sum can be generated from intermediate sums that result from equal number of summands.
Author:
Stephan Saalfeld
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected boolean[]
     
    protected double[]
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new RealSum.
    RealSum(int capacity)
    Create a new RealSum.
  • Method Summary

    Modifier and Type
    Method
    Description
    final void
    add(double a)
    Add an element to the sum.
    protected final void
    expand(double s)
     
    final double
    Get the current sum by summing up all intermediate sums.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • flags

      protected boolean[] flags
    • sums

      protected double[] sums
  • Constructor Details

    • RealSum

      public RealSum()
      Create a new RealSum. The fields for intermediate sums is initialized with a single element and expanded on demand as new elements are added.
    • RealSum

      public RealSum(int capacity)
      Create a new RealSum. The fields for intermediate sums is initialized with a given number of elements and will only be expanded on demand as new elements are added and the number of existing elements is not sufficient. This may be faster in cases where the required number of elements is known in prior.
      Parameters:
      capacity -
  • Method Details

    • getSum

      public final double getSum()
      Get the current sum by summing up all intermediate sums. Do not call this method repeatedly when the sum has not changed.
    • expand

      protected final void expand(double s)
    • add

      public final void add(double a)
      Add an element to the sum. All intermediate sums are updated and the capacity is increased on demand.
      Parameters:
      a - the summand to be added