Package ch.obermuhlner.math.big.internal
Class SeriesCalculator
- java.lang.Object
-
- ch.obermuhlner.math.big.internal.SeriesCalculator
-
- Direct Known Subclasses:
AsinCalculator
,AtanhCalculator
,CosCalculator
,CoshCalculator
,ExpCalculator
,SinCalculator
,SinhCalculator
public abstract class SeriesCalculator extends java.lang.Object
Utility class to calculate taylor series efficiently until the maximum error (as defined by the precision in theMathContext
is reached.Stores the factors of the taylor series terms so that future calculations will be faster.
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
calculateInPairs
private java.util.List<BigRational>
factors
-
Constructor Summary
Constructors Modifier Constructor Description protected
SeriesCalculator()
Constructs aSeriesCalculator
that calculates single terms.protected
SeriesCalculator(boolean calculateInPairs)
Constructs aSeriesCalculator
with control over whether the sum terms are calculated in pairs.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description private void
addFactor(BigRational factor)
java.math.BigDecimal
calculate(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the series for the specified value x and the precision defined in theMathContext
.protected abstract void
calculateNextFactor()
Calculates the factor of the next term.protected abstract PowerIterator
createPowerIterator(java.math.BigDecimal x, java.math.MathContext mathContext)
Creates thePowerIterator
used for this series.protected abstract BigRational
getCurrentFactor()
Returns the factor of the highest term already calculated.protected BigRational
getFactor(int index)
Returns the factor of the term with specified index.
-
-
-
Field Detail
-
calculateInPairs
private final boolean calculateInPairs
-
factors
private final java.util.List<BigRational> factors
-
-
Constructor Detail
-
SeriesCalculator
protected SeriesCalculator()
Constructs aSeriesCalculator
that calculates single terms.
-
SeriesCalculator
protected SeriesCalculator(boolean calculateInPairs)
Constructs aSeriesCalculator
with control over whether the sum terms are calculated in pairs.Calculation of pairs is useful for taylor series where the terms alternate the sign. In these cases it is more efficient to calculate two terms at once check then whether the acceptable error has been reached.
- Parameters:
calculateInPairs
-true
to calculate the terms in pairs,false
to calculate single terms
-
-
Method Detail
-
calculate
public java.math.BigDecimal calculate(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the series for the specified value x and the precision defined in theMathContext
.- Parameters:
x
- the value xmathContext
- theMathContext
- Returns:
- the calculated result
-
createPowerIterator
protected abstract PowerIterator createPowerIterator(java.math.BigDecimal x, java.math.MathContext mathContext)
Creates thePowerIterator
used for this series.- Parameters:
x
- the value xmathContext
- theMathContext
- Returns:
- the
PowerIterator
-
getFactor
protected BigRational getFactor(int index)
Returns the factor of the term with specified index. All mutable state of this class (and all its subclasses) must be modified in this method. This method is synchronized to allow thread-safe usage of this class.- Parameters:
index
- the index (starting with 0)- Returns:
- the factor of the specified term
-
addFactor
private void addFactor(BigRational factor)
-
getCurrentFactor
protected abstract BigRational getCurrentFactor()
Returns the factor of the highest term already calculated.When called for the first time will return the factor of the first term (index 0).
After this call the method
calculateNextFactor()
will be called to prepare for the next term.- Returns:
- the factor of the highest term
-
calculateNextFactor
protected abstract void calculateNextFactor()
Calculates the factor of the next term.
-
-