SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.analysis.sequence
Class Summation

java.lang.Object
  extended by com.numericalmethod.suanshu.analysis.sequence.Summation

public class Summation
extends java.lang.Object

Summation sums up the Summation.Terms.

 Summation = ∑(xi)
 
where xi are the terms.

If a threshold is specified, the summation is treated as a convergent series. The summing process stops after xi < 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
static interface Summation.Term
          Define the terms in a summation series.
 
Field Summary
 Summation.Term term
          the terms to sum up in a series It takes an index, and computes a value;
 double threshold
          the convergence threshold for a convergent series When a term falls below this threshold, the summing process stops.
 
Constructor Summary
Summation(Summation.Term term)
          Constructor a Summation instance with a term structure.
Summation(Summation.Term term, double threshold)
          Construct a Summation instance with a term structure and a threshold.
 
Method Summary
 double sum(double[] indices)
          Finite summation of the 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

term

public final Summation.Term term
the terms to sum up in a series

It takes an index, and computes a value;


threshold

public final double threshold
the convergence threshold for a convergent series

When a term falls below this threshold, the summing process stops. For a finite summation, threshold should be set to 0.

Constructor Detail

Summation

public Summation(Summation.Term term,
                 double threshold)
Construct a Summation instance with a term structure and a threshold. This series is assumed to converge with terms falling below the threshold for all terms after a certain index.

Parameters:
term - the terms to sum up
threshold - the convergence threshold

Summation

public Summation(Summation.Term term)
Constructor a Summation instance with a term structure. This series is assumed to be a finite summation.

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
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
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
inc - the increment
Returns:
the sum

sum

public double sum(double[] indices)
Finite summation of the terms.

Parameters:
indices - the indices to evaluate
Returns:
the summation

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 summation

SuanShu, a Java numerical and statistical library

Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.