Class Summation


  • public class Summation
    extends Object
    Summation is the operation of adding a sequence of numbers; the result is their sum or total. If numbers are added sequentially from left to right, any intermediate result is a partial sum, prefix sum, or running total of the summation. For a finite sequence of such elements, summation always produces a well-defined sum (possibly by virtue of the convention for empty sums). Given the Summation.Terms, xi, we have \[ S = \sum (x_i) \] If a threshold is specified, the summation is treated as a convergent series. The summing process stops after \(x_i < \texttt{threshold}\).

    Sample usages:

    
          Summation series = new Summation(new Summation.Term() {
    
             public double evaluate(double i) {
                  return i;
              }
          });
          double sum = series.sum(1, 100);
     
    
          Summation series = new Summation(new Summation.Term() {
    
              public double evaluate(double i) {
                  return 1d / i;
              }
          }, 0.0001);
          double sum = series.sumToInfinity(1);
     
    See Also:
    Wikipedia: Summation
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  Summation.Term
      Define the terms in a summation series.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      double sum​(double[] indices)
      Partial summation of the selected terms.
      double sum​(double from, double to, double inc)
      Sum up the terms from from to to with the increment inc.
      double sum​(int from, int to)
      Sum up the terms from from to to with the increment 1.
      double sum​(int from, int to, int inc)
      Sum up the terms from from to to with the increment inc.
      double sumToInfinity​(double from, double inc)
      Sum up the terms from from to infinity with increment inc until the series converges.
      double sumToInfinity​(int from)
      Sum up the terms from from to infinity with increment 1 until the series converges.
    • Constructor Detail

      • Summation

        public Summation​(Summation.Term term,
                         double threshold)
        Construct a summation series. It is assumed that the terms are smaller than a threshold for all terms after a certain index.
        Parameters:
        term - the terms to sum up
        threshold - the convergence threshold. When a term falls below it, the summing process stops. For a finite summation, it should be set to 0.
      • Summation

        public Summation​(Summation.Term term)
        Construct a finite summation series.
        Parameters:
        term - the terms to sum up
    • Method Detail

      • sum

        public double sum​(int from,
                          int to)
        Sum up the terms from from to to with the increment 1.
        Parameters:
        from - the starting index
        to - the ending index (inclusive)
        Returns:
        the sum
      • sum

        public double sum​(int from,
                          int to,
                          int inc)
        Sum up the terms from from to to with the increment inc.
        Parameters:
        from - the starting index
        to - the ending index (inclusive)
        inc - the increment
        Returns:
        the sum
      • sum

        public double sum​(double from,
                          double to,
                          double inc)
        Sum up the terms from from to to with the increment inc.
        Parameters:
        from - the starting index
        to - the ending index (inclusive)
        inc - the increment
        Returns:
        the sum
      • sum

        public double sum​(double[] indices)
        Partial summation of the selected terms.
        Parameters:
        indices - the indices to the selected terms
        Returns:
        the sum
      • sumToInfinity

        public double sumToInfinity​(int from)
        Sum up the terms from from to infinity with increment 1 until the series converges.
        Parameters:
        from - the starting index
        Returns:
        the sum
      • sumToInfinity

        public double sumToInfinity​(double from,
                                    double inc)
        Sum up the terms from from to infinity with increment inc until the series converges.
        Parameters:
        from - the starting index
        inc - the increment
        Returns:
        the sum