Package org.mariuszgromada.math.mxparser
Class Function
java.lang.Object
org.mariuszgromada.math.mxparser.PrimitiveElement
org.mariuszgromada.math.mxparser.Function
Function class provides possibility to define user functions.
Functions can be used in further processing by any expression,
dependent or recursive argument, function, etc... For example:
- 'f(x) = sin(x)'
- 'g(x,y) = sin(x)+cos(y)'
- 'h(x,y = f(x)+g(y,x)'
- in general 'f(x1,x2,...,xn)' where x1,...,xn are arguments
When creating a function you should avoid names reserved as parser keywords, in general words known in mathematical language as function names, operators (for example: sin, cos, +, -, pi, e, etc...). Please be informed that after associating the constant 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.
- Version:
- 4.3.0
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Function with body based on the extended code.static final int
Function with body based on the expression string.static final boolean
No syntax errors in the function.static final int
When function was not foundstatic final boolean
Syntax error in the function or syntax status unknown.static final String
static final int
Function type id identifier -
Constructor Summary
ConstructorsConstructorDescriptionConstructor - creates function from function name, function expression string and argument names.Function
(String functionName, String functionExpressionString, PrimitiveElement... elements) Constructor - creates function from function name and function expression string.Function
(String functionName, FunctionExtension functionExtension) Constructor for function definition based on your own source code - this is via implementation of FunctionExtension interface.Function
(String functionName, FunctionExtensionVariadic functionExtensionVariadic) Constructor for function definition based on your own source code - this is via implementation of FunctionExtensionVariadic interface.Function
(String functionDefinitionString, PrimitiveElement... elements) Constructor for function definition in natural math language, for instance providing on string "f(x,y) = sin(x) + cos(x)" is enough to define function "f" with parameters "x and y" and function body "sin(x) + cos(x)". -
Method Summary
Modifier and TypeMethodDescriptionvoid
addArguments
(Argument... arguments) Adds arguments (variadic) to the function expression definition.void
addConstants
(List<Constant> constantsList) Adds constants to the function expression definition.void
addConstants
(Constant... constants) Adds constants (variadic parameters) to the function expression definition.void
addDefinitions
(PrimitiveElement... elements) Adds user defined elements (such as: Arguments, Constants, Functions) to the function expressions.void
addFunctions
(Function... functions) Adds functions (variadic parameters) to the function expression definition.double
Calculates function valuedouble
calculate
(double... parameters) Calculates function valuedouble
Calculates function valueboolean
Checks function syntaxprotected Function
clone()
clone methodvoid
defineArgument
(String argumentName, double argumentValue) Enables to define the argument (associated with the function expression) based on the argument name and the argument value.void
defineArguments
(String... argumentsNames) Enables to define the arguments (associated with the function expression) based on the given arguments names.void
defineConstant
(String constantName, double constantValue) Enables to define the constant (associated with the function expression) based on the constant name and constant value.void
defineFunction
(String functionName, String functionExpressionString, String... argumentsNames) Enables to define the function (associated with the function expression) based on the function name, function expression string and arguments names (variadic parameters).getArgument
(int argumentIndex) Gets argument from the function expression.getArgument
(String argumentName) Gets argument from the function expression.int
getArgumentIndex
(String argumentName) Gets argument index from the function expression.int
Gets number of arguments associated with the function expression.double
Gets computing timegetConstant
(int constantIndex) Gets constant associated with the function expression.getConstant
(String constantName) Gets constant associated with the function expression.int
getConstantIndex
(String constantName) Gets constant index associated with the function expression.int
Gets number of constants associated with the function expression.Gets function descriptionReturns error message after checking the syntax.getFunction
(int functionIndex) Gets function associated with the function expression.getFunction
(String functionName) Gets function associated with the function expression.int
Returns function body type:BODY_RUNTIME
BODY_EXTENDED
Gets function expression stringint
getFunctionIndex
(String functionName) Gets index of function associated with the function expression.Gets function name.int
Gets number of functions associated with the function expression.getParameterName
(int parameterIndex) Gets user defined function parameter nameint
Gets number of parameters associated with the function expression.boolean
Gets recursive mode statusboolean
Returns verbose mode statusvoid
Removes all arguments associated with the function expression.void
Removes all constants associated with the function expressionvoid
Removes all functions associated with the function expression.void
removeArguments
(String... argumentsNames) Removes first occurrences of the arguments associated with the function expression.void
removeArguments
(Argument... arguments) Removes first occurrences of the arguments associated with the function expression.void
removeConstants
(String... constantsNames) Removes first occurrences of the constants associated with the function expression.void
removeConstants
(Constant... constants) Removes first occurrences of the constants associated with the function expressionvoid
removeDefinitions
(PrimitiveElement... elements) Removes user defined elements (such as: Arguments, Constants, Functions) from the function expressions.void
removeFunctions
(String... functionsNames) Removes first occurrences of the functions associated with the function expression.void
removeFunctions
(Function... functions) Removes first occurrences of the functions associated with the function expression.void
setArgumentValue
(int argumentIndex, double argumentValue) Sets value of function argument (function parameter).void
setDescription
(String description) Sets function description.void
setFunction
(String functionDefinitionString, PrimitiveElement... elements) Constructor for function definition in natural math language, for instance providing on string "f(x,y) = sin(x) + cos(x)" is enough to define function "f" with parameters "x and y" and function body "sin(x) + cos(x)".void
setFunctionName
(String functionName) Sets function name.void
setParametersNumber
(int parametersNumber) Set parameters number.void
Disables function verbose mode (sets default silent mode)void
Enables verbose function modeMethods inherited from class org.mariuszgromada.math.mxparser.PrimitiveElement
getMyTypeId
-
Field Details
-
NO_SYNTAX_ERRORS
public static final boolean NO_SYNTAX_ERRORSNo syntax errors in the function.- See Also:
-
SYNTAX_ERROR_OR_STATUS_UNKNOWN
public static final boolean SYNTAX_ERROR_OR_STATUS_UNKNOWNSyntax error in the function or syntax status unknown.- See Also:
-
NOT_FOUND
public static final int NOT_FOUNDWhen function was not found- See Also:
-
TYPE_ID
public static final int TYPE_IDFunction type id identifier- See Also:
-
TYPE_DESC
- See Also:
-
BODY_RUNTIME
public static final int BODY_RUNTIMEFunction with body based on the expression string.- See Also:
-
BODY_EXTENDED
public static final int BODY_EXTENDEDFunction with body based on the extended code.
-
-
Constructor Details
-
Function
Constructor - creates function from function name and function expression string.- Parameters:
functionName
- the function namefunctionExpressionString
- the function expression stringelements
- Optional elements list (variadic - comma separated) of types: Argument, Constant, Function- See Also:
-
Function
Constructor - creates function from function name, function expression string and argument names.- Parameters:
functionName
- the function namefunctionExpressionString
- the function expression stringargumentsNames
- the arguments names (variadic parameters) comma separated list- See Also:
-
Function
Constructor for function definition in natural math language, for instance providing on string "f(x,y) = sin(x) + cos(x)" is enough to define function "f" with parameters "x and y" and function body "sin(x) + cos(x)".- Parameters:
functionDefinitionString
- Function definition in the form of one String, ie "f(x,y) = sin(x) + cos(x)"elements
- Optional elements list (variadic - comma separated) of types: Argument, Constant, Function- See Also:
-
Function
Constructor for function definition based on your own source code - this is via implementation of FunctionExtension interface.- Parameters:
functionName
- Function namefunctionExtension
- Your own source code
-
Function
Constructor for function definition based on your own source code - this is via implementation of FunctionExtensionVariadic interface.- Parameters:
functionName
- Function namefunctionExtensionVariadic
- Your own source code
-
-
Method Details
-
setFunction
Constructor for function definition in natural math language, for instance providing on string "f(x,y) = sin(x) + cos(x)" is enough to define function "f" with parameters "x and y" and function body "sin(x) + cos(x)".- Parameters:
functionDefinitionString
- Function definition in the form of one String, ie "f(x,y) = sin(x) + cos(x)"elements
- Optional elements list (variadic - comma separated) of types: Argument, Constant, Function- See Also:
-
setDescription
Sets function description.- Parameters:
description
- the function description
-
getDescription
Gets function description- Returns:
- Function description as string
-
getFunctionName
Gets function name.- Returns:
- Function name as string.
-
getFunctionExpressionString
Gets function expression string- Returns:
- Function expression as string.
-
setFunctionName
Sets function name.- Parameters:
functionName
- the function name
-
setArgumentValue
public void setArgumentValue(int argumentIndex, double argumentValue) Sets value of function argument (function parameter).- Parameters:
argumentIndex
- the argument index (in accordance to arguments declaration sequence)argumentValue
- the argument value
-
getFunctionBodyType
public int getFunctionBodyType()Returns function body type:BODY_RUNTIME
BODY_EXTENDED
- Returns:
- Returns function body type:
BODY_RUNTIME
BODY_EXTENDED
-
checkSyntax
public boolean checkSyntax()Checks function syntax- Returns:
- syntax status: Function.NO_SYNTAX_ERRORS, Function.SYNTAX_ERROR_OR_STATUS_UNKNOWN
-
getErrorMessage
Returns error message after checking the syntax.- Returns:
- Error message as string.
-
clone
clone method -
calculate
public double calculate()Calculates function value- Returns:
- Function value as double.
-
calculate
public double calculate(double... parameters) Calculates function value- Parameters:
parameters
- the function parameters values (as doubles)- Returns:
- function value as double.
-
calculate
Calculates function value- Parameters:
arguments
- function parameters (as Arguments)- Returns:
- function value as double
-
addDefinitions
Adds user defined elements (such as: Arguments, Constants, Functions) to the function expressions.- Parameters:
elements
- Elements list (variadic), where Argument, Constant, Function extend the same class PrimitiveElement- See Also:
-
removeDefinitions
Removes user defined elements (such as: Arguments, Constants, Functions) from the function expressions.- Parameters:
elements
- Elements list (variadic), where Argument, Constant, Function extend the same class PrimitiveElement- See Also:
-
addArguments
Adds arguments (variadic) to the function expression definition.- Parameters:
arguments
- the arguments list (comma separated list)- See Also:
-
defineArguments
Enables to define the arguments (associated with the function expression) based on the given arguments names.- Parameters:
argumentsNames
- the arguments names (variadic) comma separated list- See Also:
-
defineArgument
Enables to define the argument (associated with the function expression) based on the argument name and the argument value.- Parameters:
argumentName
- the argument nameargumentValue
- the the argument value- See Also:
-
getArgumentIndex
Gets argument index from the function expression.- Parameters:
argumentName
- the argument name- Returns:
- The argument index if the argument name was found, otherwise returns Argument.NOT_FOUND
- See Also:
-
getArgument
Gets argument from the function expression.- Parameters:
argumentName
- the argument name- Returns:
- The argument if the argument name was found, otherwise returns null.
- See Also:
-
getArgument
Gets argument from the function expression.- Parameters:
argumentIndex
- the argument index- Returns:
- Argument if the argument index is between 0 and the last available argument index (getArgumentsNumber()-1), otherwise returns null.
- See Also:
-
getParametersNumber
public int getParametersNumber()Gets number of parameters associated with the function expression.- Returns:
- The number of function parameters (int >= 0)
- See Also:
-
setParametersNumber
public void setParametersNumber(int parametersNumber) Set parameters number.- Parameters:
parametersNumber
- the number of function parameters (default = number of arguments (less number might be specified).
-
getParameterName
Gets user defined function parameter name- Parameters:
parameterIndex
- Parameter index between 0 and n-1- Returns:
- If parameter exists returns parameters name, otherwise empty string is returned.
-
getArgumentsNumber
public int getArgumentsNumber()Gets number of arguments associated with the function expression.- Returns:
- The number of arguments (int >= 0)
- See Also:
-
removeArguments
Removes first occurrences of the arguments associated with the function expression.- Parameters:
argumentsNames
- the arguments names (variadic parameters) comma separated list- See Also:
-
removeArguments
Removes first occurrences of the arguments associated with the function expression.- Parameters:
arguments
- the arguments (variadic parameters) comma separated list- See Also:
-
removeAllArguments
public void removeAllArguments()Removes all arguments associated with the function expression.- See Also:
-
addConstants
Adds constants (variadic parameters) to the function expression definition.- Parameters:
constants
- the constants (comma separated list)- See Also:
-
addConstants
Adds constants to the function expression definition.- Parameters:
constantsList
- the list of constants- See Also:
-
defineConstant
Enables to define the constant (associated with the function expression) based on the constant name and constant value.- Parameters:
constantName
- the constant nameconstantValue
- the constant value- See Also:
-
getConstantIndex
Gets constant index associated with the function expression.- Parameters:
constantName
- the constant name- Returns:
- Constant index if constant name was found, otherwise return Constant.NOT_FOUND.
- See Also:
-
getConstant
Gets constant associated with the function expression.- Parameters:
constantName
- the constant name- Returns:
- Constant if constant name was found, otherwise return null.
- See Also:
-
getConstant
Gets constant associated with the function expression.- Parameters:
constantIndex
- the constant index- Returns:
- Constant if the constantIndex is between 0 and the last available constant index (getConstantsNumber() - 1), otherwise it returns null.
- See Also:
-
getConstantsNumber
public int getConstantsNumber()Gets number of constants associated with the function expression.- Returns:
- number of constants (int >= 0)
- See Also:
-
removeConstants
Removes first occurrences of the constants associated with the function expression.- Parameters:
constantsNames
- the constants names (variadic parameters) comma separated list- See Also:
-
removeConstants
Removes first occurrences of the constants associated with the function expression- Parameters:
constants
- the constants (variadic parameters) comma separated list- See Also:
-
removeAllConstants
public void removeAllConstants()Removes all constants associated with the function expression- See Also:
-
addFunctions
Adds functions (variadic parameters) to the function expression definition.- Parameters:
functions
- the functions (variadic parameters) comma separated list- See Also:
-
defineFunction
public void defineFunction(String functionName, String functionExpressionString, String... argumentsNames) Enables to define the function (associated with the function expression) based on the function name, function expression string and arguments names (variadic parameters).- Parameters:
functionName
- the function namefunctionExpressionString
- the expression stringargumentsNames
- the function arguments names (variadic parameters) comma separated list- See Also:
-
getFunctionIndex
Gets index of function associated with the function expression.- Parameters:
functionName
- the function name- Returns:
- Function index if function name was found, otherwise returns Function.NOT_FOUND
- See Also:
-
getFunction
Gets function associated with the function expression.- Parameters:
functionName
- the function name- Returns:
- Function if function name was found, otherwise returns null.
- See Also:
-
getFunction
Gets function associated with the function expression.- Parameters:
functionIndex
- the function index- Returns:
- Function if function index is between 0 and the last available function index (getFunctionsNumber()-1), otherwise returns null.
- See Also:
-
getFunctionsNumber
public int getFunctionsNumber()Gets number of functions associated with the function expression.- Returns:
- number of functions (int >= 0)
- See Also:
-
removeFunctions
Removes first occurrences of the functions associated with the function expression.- Parameters:
functionsNames
- the functions names (variadic parameters) comma separated list- See Also:
-
removeFunctions
Removes first occurrences of the functions associated with the function expression.- Parameters:
functions
- the functions (variadic parameters) comma separated list.- See Also:
-
removeAllFunctions
public void removeAllFunctions()Removes all functions associated with the function expression.- See Also:
-
setVerboseMode
public void setVerboseMode()Enables verbose function mode -
setSilentMode
public void setSilentMode()Disables function verbose mode (sets default silent mode) -
getVerboseMode
public boolean getVerboseMode()Returns verbose mode status- Returns:
- true if verbose mode is on, otherwise returns false
-
getRecursiveMode
public boolean getRecursiveMode()Gets recursive mode status- Returns:
- true if recursive mode is enabled, otherwise returns false
-
getComputingTime
public double getComputingTime()Gets computing time- Returns:
- computing time in seconds.
-