Package org.mariuszgromada.math.mxparser
Class RecursiveArgument
- java.lang.Object
-
- org.mariuszgromada.math.mxparser.PrimitiveElement
-
- org.mariuszgromada.math.mxparser.Argument
-
- org.mariuszgromada.math.mxparser.RecursiveArgument
-
- All Implemented Interfaces:
java.io.Serializable
public class RecursiveArgument extends Argument implements java.io.Serializable
RecursiveArgument class enables to declare the argument (variable) which is defined in a recursive way. Such an argument can be used in further processing in expressions, functions and dependent or recursive arguments.
For example:- 'fib(n) = fin(n-1)+fib(n-2), fib(0) = 0, fib(1) = 1'
- 'factorial(n) = n*factorial(n-1), factorial(0) = 1'
When creating an argument you should avoid:
- names reserved as parser keywords, in general words known in mathematical language as function names, operators (for example: sin, cos, +, -, etc...). Please be informed that after associating the argument with the expression, function or dependent/recursive argument its name will be recognized by the parser as reserved key word. It means that it could not be the same as any other key word known by the parser for this particular expression.
- defining statements with increasing index: 'a(n) = a(n+1) + ... ', otherwise you will get Double.NaN
- if recursion is not properly defined you will get Double.NaN in the result. This is due to the recursion counter inside of the recursive argument. Calculating n-th element requires no more than n recursion steps (usually less than n).
- For negative 'n' you will get Double.NaN.
- Version:
- 6.1.0
- See Also:
Argument,Expression,Function,Constant, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<java.lang.Double>baseValuesBase valuesprivate intrecursiveCounterTo avoid never ending loopsprivate static intserialClassIDprivate static longserialVersionUIDprivate intstartingIndexstatic java.lang.StringTYPE_DESC_RECURSIVEstatic intTYPE_ID_RECURSIVEType identifier for recursive arguments.-
Fields inherited from class org.mariuszgromada.math.mxparser.Argument
ARGUMENT_INITIAL_VALUE, argumentExpression, argumentType, argumentValue, BODY_EXTENDED, BODY_RUNTIME, computingTime, DEPENDENT_ARGUMENT, FREE_ARGUMENT, MAX_RECURSION_CALLS, n, NO_SYNTAX_ERRORS, NOT_FOUND, RECURSIVE_ARGUMENT, SYNTAX_ERROR, SYNTAX_ERROR_OR_STATUS_UNKNOWN, syntaxStatusDefinition, TYPE_DESC, TYPE_ID
-
-
Constructor Summary
Constructors Modifier Constructor Description RecursiveArgument(java.lang.String argumentName, java.lang.String recursiveExpressionString, java.lang.String indexName)Constructor - creates recursive argument.RecursiveArgument(java.lang.String argumentName, java.lang.String recursiveExpressionString, Argument n, PrimitiveElement... elements)Constructor - creates recursive argument.RecursiveArgument(java.lang.String argumentDefinitionString, PrimitiveElement... elements)Constructor - creates argument based on the argument definition string.privateRecursiveArgument(RecursiveArgument recursiveArgumentToClone, CloneCache cloneCache)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddBaseCase(int index, double value)Adds base caseRecursiveArgumentcloneForThreadSafe()Creates a completely independent 1-1 clone that can be safely used by a separate thread.(package private) RecursiveArgumentcloneForThreadSafeInternal(CloneCache cloneCache)(package private) RecursiveArgumentcloneForThreadSafeInternal(Expression relatedExpressionThatInitiatedClone, CloneCache cloneCache)doublegetArgumentValue(double index)Gets recursive argument valueprivate voidinitRecursiveArgument()private voidinitRecursiveArgument(PrimitiveElement... elements)voidresetAllCases()Clears all based cases and stored calculated values-
Methods inherited from class org.mariuszgromada.math.mxparser.Argument
addArguments, addConstants, addConstants, addDefinitions, addFunctions, addRelatedExpression, checkSyntax, clone, defineArgument, defineArguments, defineConstant, defineFunction, getArgument, getArgument, getArgumentBodyType, getArgumentExpressionString, getArgumentIndex, getArgumentName, getArgumentsNumber, getArgumentType, getArgumentValue, getArgumentValue, getComputingTime, getConstant, getConstant, getConstantIndex, getConstantsNumber, getDescription, getErrorMessage, getFunction, getFunction, getFunctionIndex, getFunctionsNumber, getRecursiveMode, getVerboseMode, refreshMaxAllowedRecursionDepth, removeAllArguments, removeAllConstants, removeAllFunctions, removeArguments, removeArguments, removeConstants, removeConstants, removeDefinitions, removeFunctions, removeFunctions, removeRelatedExpression, setArgumentExpressionString, setArgumentName, setArgumentValue, setDescription, setExpressionModifiedFlags, setSilentMode, setVerboseMode
-
Methods inherited from class org.mariuszgromada.math.mxparser.PrimitiveElement
getMyTypeId
-
-
-
-
Field Detail
-
serialClassID
private static final int serialClassID
- See Also:
- Constant Field Values
-
serialVersionUID
private static final long serialVersionUID
-
TYPE_ID_RECURSIVE
public static final int TYPE_ID_RECURSIVE
Type identifier for recursive arguments.- See Also:
- Constant Field Values
-
TYPE_DESC_RECURSIVE
public static java.lang.String TYPE_DESC_RECURSIVE
-
baseValues
private java.util.List<java.lang.Double> baseValues
Base values
-
recursiveCounter
private int recursiveCounter
To avoid never ending loops
-
startingIndex
private int startingIndex
-
-
Constructor Detail
-
RecursiveArgument
public RecursiveArgument(java.lang.String argumentName, java.lang.String recursiveExpressionString, java.lang.String indexName)Constructor - creates recursive argument.- Parameters:
argumentName- the argument namerecursiveExpressionString- the recursive expression stringindexName- index argument name
-
RecursiveArgument
public RecursiveArgument(java.lang.String argumentName, java.lang.String recursiveExpressionString, Argument n, PrimitiveElement... elements)Constructor - creates recursive argument.- Parameters:
argumentName- the argument namerecursiveExpressionString- the recursive expression stringn- the index argumentelements- Optional elements list (variadic - comma separated) of types: Argument, Constant, Function- See Also:
PrimitiveElement,Argument
-
RecursiveArgument
public RecursiveArgument(java.lang.String argumentDefinitionString, PrimitiveElement... elements)Constructor - creates argument based on the argument definition string.- Parameters:
argumentDefinitionString- Argument definition string, i.e.:- 'x' - only argument name
- 'x=5' - argument name and argument value
- 'x=2*5' - argument name and argument value given as simple expression
- 'x=2*y' - argument name and argument expression (dependent argument 'x' on argument 'y')
- 'x(n)=x(n-1)+x(n-2)' - for recursive arguments)
elements- Optional elements list (variadic - comma separated) of types: Argument, Constant, Function- See Also:
PrimitiveElement,Argument
-
RecursiveArgument
private RecursiveArgument(RecursiveArgument recursiveArgumentToClone, CloneCache cloneCache)
-
-
Method Detail
-
initRecursiveArgument
private void initRecursiveArgument()
-
initRecursiveArgument
private void initRecursiveArgument(PrimitiveElement... elements)
-
addBaseCase
public void addBaseCase(int index, double value)Adds base case- Parameters:
index- the base case indexvalue- the base case value
-
resetAllCases
public void resetAllCases()
Clears all based cases and stored calculated values
-
getArgumentValue
public double getArgumentValue(double index)
Gets recursive argument value- Parameters:
index- the index- Returns:
- value as double
-
cloneForThreadSafeInternal
RecursiveArgument cloneForThreadSafeInternal(CloneCache cloneCache)
- Overrides:
cloneForThreadSafeInternalin classArgument
-
cloneForThreadSafeInternal
RecursiveArgument cloneForThreadSafeInternal(Expression relatedExpressionThatInitiatedClone, CloneCache cloneCache)
- Overrides:
cloneForThreadSafeInternalin classArgument
-
cloneForThreadSafe
public RecursiveArgument cloneForThreadSafe()
Creates a completely independent 1-1 clone that can be safely used by a separate thread. If the cloned element contains references to other elements (e.g. arguments, functions, constants), then they will also be cloned and the newly created element will contain references to the corresponding clones. Important - the API allows you to extract all these clones.- Overrides:
cloneForThreadSafein classArgument- Returns:
- Cloned object.
-
-