Class RealSum


  • public class RealSum
    extends java.lang.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[] flags  
      protected double[] sums  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(double a)
      Add an element to the sum.
      protected void expand​(double s)  
      double getSum()
      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 Detail

      • flags

        protected boolean[] flags
      • sums

        protected double[] sums
    • Constructor Detail

      • 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 Detail

      • 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