Package net.imglib2.util
Class RealSum
java.lang.Object
net.imglib2.util.RealSum
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 -
Constructor Summary
Constructors -
Method Summary
-
Field Details
-
flags
protected boolean[] flags -
sums
protected double[] sums
-
-
Constructor Details
-
RealSum
public RealSum()Create a newRealSum
. 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 newRealSum
. 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
-