Class SeriesCalculator
java.lang.Object
io.github.syst3ms.skriptparser.util.math.SeriesCalculator
- Direct Known Subclasses:
AsinCalculator, CosCalculator, ExpCalculator, SinCalculator
Utility class to calculate taylor series efficiently until the maximum error (as defined by the precision in the
MathContext is reached.
Stores the factors of the taylor series terms so that future calculations will be faster.
I do not claim ownership of this code, it is the intellectual property of @obermuhlner.-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructs aSeriesCalculatorthat calculates single terms.protectedSeriesCalculator(boolean calculateInPairs) Constructs aSeriesCalculatorwith control over whether the sum terms are calculated in pairs. -
Method Summary
Modifier and TypeMethodDescriptioncalculate(BigDecimal x, MathContext mathContext) Calculates the series for the specified value x and the precision defined in theMathContext.protected abstract voidCalculates the factor of the next term.protected abstract PowerIteratorcreatePowerIterator(BigDecimal x, MathContext mathContext) Creates thePowerIteratorused for this series.protected abstract BigRationalReturns the factor of the highest term already calculated.protected BigRationalgetFactor(int index) Returns the factor of the term with specified index.
-
Constructor Details
-
SeriesCalculator
protected SeriesCalculator()Constructs aSeriesCalculatorthat calculates single terms. -
SeriesCalculator
protected SeriesCalculator(boolean calculateInPairs) Constructs aSeriesCalculatorwith 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-trueto calculate the terms in pairs,falseto calculate single terms
-
-
Method Details
-
calculate
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
Creates thePowerIteratorused for this series.- Parameters:
x- the value xmathContext- theMathContext- Returns:
- the
PowerIterator
-
getFactor
Returns the factor of the term with specified index.- Parameters:
index- the index (starting with 0)- Returns:
- the factor of the specified term
-
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.
-