Package com.singularsys.jep.misc
Class MacroFunction
java.lang.Object
com.singularsys.jep.functions.PostfixMathCommand
com.singularsys.jep.misc.MacroFunction
- All Implemented Interfaces:
JepComponent,PostfixMathCommandI,Serializable
A function specified by a string.
For example
MacroFunction fact = new MacroFunction(
"fact",new String[]{"x"},
"if(x>1,x*fact(x-1),1)");
jep.addFunction("fact",fact);
fact.init(jep);
Node n = jep.parse("fact(5)");
- Author:
- R Morris.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Evaluatorprotected final Stringprotected Jepprotected NodeFactoryprotected final Nodeprotected SubstitutionVisitorprotected Nodeprotected final String[]protected Variable[]Fields inherited from class com.singularsys.jep.functions.PostfixMathCommand
curNumberOfParameters, description, name, NaN, numberOfParameters -
Constructor Summary
ConstructorsConstructorDescriptionMacroFunction(Jep jep, String funName, String[] parameters, String expression) Create a macro function with two or more arguments defined by an expression.MacroFunction(Jep jep, String funName, String parameter, String expression) Create a macro function with a single arguments defined by an expression.MacroFunction(String funName, String[] parameters, String expression) Create a macro function with two or more arguments defined by an expression.MacroFunction(String funName, String[] parameters, String expression, Node root) Create a MacroFunction from a node tree. -
Method Summary
Modifier and TypeMethodDescriptionReturn a description of the function from the properties file.Returns a new copy of the function, uses theMacroFunction(String, String[], String, Node)constructor.String[]The names specified in the constructor.The actual expression used for evaluation, after substitution of variable names.Variable[]getVars()Get the internal variables used, these have specific names like 'fun::x'voidThe init method must be called after construction.voidCalculates the value of the expression.voidChange the name of a MacroFunction.toString()Return a string representation of the function.Methods inherited from class com.singularsys.jep.functions.PostfixMathCommand
asArray, asBool, asDouble, asInt, asLong, asStrictInt, asString, asType, checkNumberOfParameters, getDescription, getDescriptionWithType, getName, getNumberOfParameters, setCurNumberOfParameters, setDescription, toString
-
Field Details
-
varNames
-
expression
-
preCompiled
-
topNode
-
ev
-
vars
-
sv
-
jep
-
nf
-
-
Constructor Details
-
MacroFunction
Create a macro function with two or more arguments defined by an expression.- Parameters:
jep- The jep instancefunName- name of the functionparameters- an array of the formal parameter names used by the functionexpression-- Since:
- 3.4.0
-
MacroFunction
Create a macro function with a single arguments defined by an expression.- Parameters:
jep- The jep instancefunName- name of the functionparameter- the name of the formal parameter.expression-- Since:
- 3.4.0
-
MacroFunction
Create a macro function with two or more arguments defined by an expression. Use this method for recursive functions like factorial. Theinit(Jep)method must be called after the function is added to the function table, which will recompile the function.- Parameters:
funName- name of the functionparameters- an array of the formal parameter names used by the functionexpression-
-
MacroFunction
Create a MacroFunction from a node tree. Theinit(Jep)method must be called to compile the expression.- Parameters:
funName- function nameparameters- list of parametersexpression- string representation of equationroot- root of expressions tree.- Since:
- Jep 4.0
-
-
Method Details
-
init
The init method must be called after construction. This complies the equation, either by parsing the expression, or by copying the node tree specified in theMacroFunction(String, String[], String, Node)constructor. Names of variables are changed to be of the formname::varname, or as specified by themisc.MacroFunction.variableNameFormatmessage.- Specified by:
initin interfaceJepComponent- Parameters:
j- Jep instance- Throws:
JepRuntimeException- if errors compiling the expression.
-
setName
Change the name of a MacroFunction. Names of all variables are also changed.- Specified by:
setNamein interfacePostfixMathCommandI- Overrides:
setNamein classPostfixMathCommand- Parameters:
newName- the function name
-
run
Calculates the value of the expression.- Specified by:
runin interfacePostfixMathCommandI- Overrides:
runin classPostfixMathCommand- Parameters:
stack- arguments for function- Throws:
EvaluationException- if the calculation cannot be performed
-
getTopNode
The actual expression used for evaluation, after substitution of variable names.- Returns:
- top node of expression
-
getParameterNames
The names specified in the constructor.- Returns:
- an array of names.
-
getVars
Get the internal variables used, these have specific names like 'fun::x'- Returns:
- list of variables
-
toString
Description copied from class:PostfixMathCommandReturn a string representation of the function. If the name is not null, return the name, otherwise return the class name.- Overrides:
toStringin classPostfixMathCommand
-
getLightWeightInstance
Returns a new copy of the function, uses theMacroFunction(String, String[], String, Node)constructor.- Specified by:
getLightWeightInstancein interfaceJepComponent- Returns:
- either a new instance, null or 'this'.
-
getDescription
Description copied from class:PostfixMathCommandReturn a description of the function from the properties file. The property name is the class name followed by ".description". The property is found using theJepMessagesclass, which can be configured to add additional properties files. IfPostfixMathCommand.setDescription(String)has been called, return the description set by that method instead.- Specified by:
getDescriptionin interfacePostfixMathCommandI- Overrides:
getDescriptionin classPostfixMathCommand- Returns:
- the description of the function
-