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
,java.io.Serializable
- Direct Known Subclasses:
LambdaFunction
public class MacroFunction extends PostfixMathCommand implements JepComponent
A function specified by a string. For exampleMacroFunction 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:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected Evaluator
ev
protected java.lang.String
expression
protected int
nParam
protected Node
preCompiled
protected Node
topNode
protected java.lang.String[]
varNames
protected Variable[]
vars
-
Fields inherited from class com.singularsys.jep.functions.PostfixMathCommand
curNumberOfParameters, name, NaN, numberOfParameters
-
-
Constructor Summary
Constructors Constructor Description MacroFunction(Jep jep, java.lang.String funName, java.lang.String[] parameters, java.lang.String expression)
Create a macro function with two or more arguments defined by an expression.MacroFunction(Jep jep, java.lang.String funName, java.lang.String parameter, java.lang.String expression)
Create a macro function with a single arguments defined by an expression.MacroFunction(java.lang.String funName, java.lang.String[] parameters, java.lang.String expression)
Create a macro function with two or more arguments defined by an expression.MacroFunction(java.lang.String funName, java.lang.String[] parameters, java.lang.String expression, Node root)
Create a MacroFunction with a node tree.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JepComponent
getLightWeightInstance()
Returns a new copy of the function, uses theMacroFunction(String, String[], String, Node)
constructor.java.lang.String[]
getParameterNames()
Node
getTopNode()
Variable[]
getVars()
Get the internal variables used, these have specific names like 'fun::x'void
init(Jep j)
The init method must be called after construction.void
run(java.util.Stack<java.lang.Object> stack)
Calculates the value of the expression.java.lang.String
toString()
-
Methods inherited from class com.singularsys.jep.functions.PostfixMathCommand
asArray, asBool, asDouble, asInt, asLong, asStrictInt, asString, checkNumberOfParameters, getName, getNumberOfParameters, setCurNumberOfParameters, setName, toString
-
-
-
-
Field Detail
-
nParam
protected final int nParam
-
varNames
protected final java.lang.String[] varNames
-
expression
protected final java.lang.String expression
-
preCompiled
protected final Node preCompiled
-
topNode
protected transient Node topNode
-
ev
protected transient Evaluator ev
-
vars
protected transient Variable[] vars
-
-
Constructor Detail
-
MacroFunction
public MacroFunction(Jep jep, java.lang.String funName, java.lang.String[] parameters, java.lang.String expression)
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
public MacroFunction(Jep jep, java.lang.String funName, java.lang.String parameter, java.lang.String expression)
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
public MacroFunction(java.lang.String funName, java.lang.String[] parameters, java.lang.String expression)
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
public MacroFunction(java.lang.String funName, java.lang.String[] parameters, java.lang.String expression, Node root)
Create a MacroFunction with a node tree. The ImportationVisitor is used to convert the tree into context for this object.- Parameters:
funName
- function nameparameters
- list of parametersexpression
- string representation of equationroot
- root of expressions tree.- Since:
- 4.0
-
-
Method Detail
-
init
public void init(Jep j)
The init method must be called after construction.- Specified by:
init
in interfaceJepComponent
- Parameters:
j
- jep instance- Throws:
java.lang.RuntimeException
- if errors compiling the expression.
-
run
public void run(java.util.Stack<java.lang.Object> stack) throws EvaluationException
Calculates the value of the expression.- Specified by:
run
in interfacePostfixMathCommandI
- Overrides:
run
in classPostfixMathCommand
- Parameters:
stack
- arguments for function- Throws:
EvaluationException
- if the calculation cannot be performed
-
getTopNode
public Node getTopNode()
-
getParameterNames
public java.lang.String[] getParameterNames()
-
getVars
public Variable[] getVars()
Get the internal variables used, these have specific names like 'fun::x'- Returns:
- list of variables
-
toString
public java.lang.String toString()
- Overrides:
toString
in classPostfixMathCommand
-
getLightWeightInstance
public JepComponent getLightWeightInstance()
Returns a new copy of the function, uses theMacroFunction(String, String[], String, Node)
constructor.- Specified by:
getLightWeightInstance
in interfaceJepComponent
- Returns:
- either an new instance, null or 'this'.
-
-