Class RpEval
- java.lang.Object
-
- com.singularsys.extensions.fastreal.AbstractEval
-
- com.singularsys.extensions.fastreal.RpEval
-
- All Implemented Interfaces:
JepComponent
,ParserVisitor
,java.io.Serializable
public final class RpEval extends AbstractEval
A fast evaluation algorithm for equations over Doubles, does not work with vectors or matrices. This is based around reverse polish notation and is optimised for speed at every opportunity.To use do
Jep j = ...; Node node = ...; RpEval rpe = new RpEval(j); RpCommandList list = rpe.compile(node); double val = rpe.evaluate(list); System.out.println(val); rpe.cleanUp();
Variable values in the evaluator are stored in a array. The array index of a variable can be found using
Variable v = j.getVar("x"); int ref = rpe.getVarRef(v);
and the value of the variable set usingrpe.setVarValue(ref,0.1234);
Variable values can also be set using the standardVariable.setValue()
or (slower)Jep.setVarVal(name,vlaue)
methods. Setting the value of a jep variable will automatically update the corresponding rpe value but there will be a performance hit. Setting the value of the rpe variable does not change the corresponding jep value.The compile methods converts the expression represented by node into a string of commands. For example the expression "1+2*3" will be converted into the sequence of commands
Constant no 1 (pushes constant onto stack) Constant no 2 Constant no 3 Multiply scalers (multiplies last two entries on stack) Add scalers (adds last two entries on stack)
The evaluate method executes these methods sequentially using a stack and returns the last object on the stack.A few cautionary notes:
- It only works over doubles expressions with complex numbers or strings will cause problems.
- It only works for expressions involving scalers.
- It is safe to use individual RpEval instances in separate threads,
using the same instance in separate threads is like to cause exceptions.
The RpCommandList objects created by compile are immutable and are safe to use across threads.
The
duplicate()
method creates a copy of the RpEval object which can evaluate the same commandList in separate threads.
Implementation notes A lot of things have been done to make it as fast as possible:
- Everything is final which maximizes the possibility for in-lining.
- All object creation happens during compile.
- All calculations done using double values.
- Each operator/function is hand coded. To extend functionality you will have to modify the source.
This class and the
RpCommandList
can be serialised, allowing a expression to be stored or transmitted. The serialized form stores the constants, variables and custom functions used. It does not save the associated Jep instance. This allows a deserialized instance to be used with a deserializedRpCommandList
. If a Jep instance is serialized in the same stream as theRpEval
instance then theAbstractEval.init(Jep)
should be called on deserialization.- Author:
- Rich Morris Created on 14-Apr-2004
- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from class com.singularsys.extensions.fastreal.AbstractEval
customFunctionCommands, functionHash, jep, ls, opSet
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
cleanUp()
Removes observers and other cleanup needed when evaluator no longer used.RpCommandList
compile(Node node)
Compile the expressions to produce a set of commands in reverse Polish notation.RpEval
duplicate()
Returns a copy of the RpEval object which is safe to use for evaluation in a new thread.double
evaluate(RpCommandList comList)
Evaluate the expression.protected java.lang.Object
getConstantValue(short ref)
JepComponent
getLightWeightInstance()
Creates a new instance which can be used in a separate thread without a jep instance.JepComponent
getLightWeightInstance(Jep jep)
Create a new instance which can be used in a separate thread.Variable
getVariable(int ref)
Gets the Jep Variable for a give reference numberint
getVarRef(Variable var)
Gets the reference number for a given variableint
getVarRef(java.lang.String varName)
Gets the reference number for a given variabledouble
getVarValue(int ref)
Gets the value of a variablevoid
setVarValue(int ref, double val)
Sets the value of a variablejava.lang.String
toString()
java.lang.String
toString(RpCommand com)
Enhanced RpCommand to String conversion.java.lang.String
toString(RpCommandList coms1)
void
updateFromJepVariables()
If the variables used by Jep has a valid value set the corresponding mrpe valuevoid
updateJepVariables()
Copies all the values from the Rpe variable to the corresponding Jep variables.java.lang.Object
visit(ASTConstant node, java.lang.Object data)
java.lang.Object
visit(ASTFunNode node, java.lang.Object data)
java.lang.Object
visit(ASTOpNode node, java.lang.Object data)
java.lang.Object
visit(ASTVarNode node, java.lang.Object data)
-
Methods inherited from class com.singularsys.extensions.fastreal.AbstractEval
chargeFunctionName, duplicateCustomFunctions, getFunction, getUserFunction, init, staticGetFunction
-
-
-
-
Constructor Detail
-
RpEval
public RpEval(Jep jep)
-
-
Method Detail
-
compile
public final RpCommandList compile(Node node) throws JepException
Compile the expressions to produce a set of commands in reverse Polish notation.- Throws:
JepException
-
visit
public final java.lang.Object visit(ASTConstant node, java.lang.Object data) throws JepException
- Throws:
JepException
-
visit
public final java.lang.Object visit(ASTVarNode node, java.lang.Object data) throws JepException
- Throws:
JepException
-
visit
public final java.lang.Object visit(ASTOpNode node, java.lang.Object data) throws JepException
- Throws:
JepException
-
visit
public final java.lang.Object visit(ASTFunNode node, java.lang.Object data) throws JepException
- Throws:
JepException
-
evaluate
public final double evaluate(RpCommandList comList) throws EvaluationException
Evaluate the expression.- Parameters:
comList
- list of commands- Returns:
- the double value of the equation
- Throws:
EvaluationException
-
cleanUp
public void cleanUp()
Removes observers and other cleanup needed when evaluator no longer used.
-
getVariable
public Variable getVariable(int ref)
Gets the Jep Variable for a give reference number- Parameters:
ref
- reference number for the variable- Returns:
- corresponding Jep variable, or null if no jep instance
-
getVarRef
public int getVarRef(Variable var) throws JepException
Gets the reference number for a given variable- Parameters:
var
- Jep Variable- Returns:
- reference number for the variable
- Throws:
JepException
- if there is a miss-match in sizes of the internal representation. This can be caused if the method is called using a lightweight instance with no
-
getVarRef
public int getVarRef(java.lang.String varName) throws JepException
Gets the reference number for a given variable- Parameters:
varName
- name of variable- Returns:
- reference number for the variable
- Throws:
JepException
- if no jep instance, can happen if using a lightweight instance
-
setVarValue
public void setVarValue(int ref, double val)
Sets the value of a variable- Parameters:
ref
- reference number for the variableval
- the value to set the variable
-
getVarValue
public double getVarValue(int ref)
Gets the value of a variable- Parameters:
ref
- reference number for the variable
-
updateJepVariables
public void updateJepVariables()
Copies all the values from the Rpe variable to the corresponding Jep variables.
-
updateFromJepVariables
public void updateFromJepVariables() throws EvaluationException
If the variables used by Jep has a valid value set the corresponding mrpe value- Throws:
EvaluationException
- if the jep variable has different dimensions to the mrpe variable- Since:
- Jep 4.0/Extensions 2.1
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
getConstantValue
protected java.lang.Object getConstantValue(short ref)
- Specified by:
getConstantValue
in classAbstractEval
-
toString
public java.lang.String toString(RpCommand com)
Enhanced RpCommand to String conversion. Used when rpe instance is available, prints the values of the constants, variables and functions.- Parameters:
com
- an RpCommand to convert- Returns:
- string representation
-
duplicate
public RpEval duplicate()
Returns a copy of the RpEval object which is safe to use for evaluation in a new thread.RpEval rpe1 = new RpEval(jep); RpCommandList com = rpe1.compile(node); int varRef = rpe1.getVarRef(jep.getVar("x")); RpEval rpe2 = rpe1.duplicate(); rpe2.setVarValue(ref,0.5); double result = rpe2.evaluate(com);
Since Jep 4.0, extension 2.1 just callsgetLightWeightInstance()
with a cast SeeAbstractEval.duplicateCustomFunctions(AbstractEval, Jep)
for handling of custom functions.- Returns:
- a new instance
-
getLightWeightInstance
public JepComponent getLightWeightInstance()
Creates a new instance which can be used in a separate thread without a jep instance. Variables can only refered to using thesetVarValue(int, double)
andgetVarValue(int)
.RpEval rpe1 = new RpEval(jep); RpCommandList com = rpe1.compile(node); int varRef = rpe1.getVarRef(jep.getVar("x")); RpEval rpe2 = (RpEval) rpe1.getLightWeightInstance(); rpe2.setVarValue(ref,0.5); double result = rpe2.evaluate(com);
SeeAbstractEval.duplicateCustomFunctions(AbstractEval, Jep)
for handling of custom functions.- Returns:
- a new instance
-
getLightWeightInstance
public JepComponent getLightWeightInstance(Jep jep)
Create a new instance which can be used in a separate thread. This uses a Jep instance and the variable references can be found usinggetVarRef(Variable)
andgetVarRef(String)
and the variable for the given reference found usinggetVariable(int)
. SeeAbstractEval.duplicateCustomFunctions(AbstractEval, Jep)
for handling of custom functions.- Parameters:
jep
-- Returns:
- a new instance
-
toString
public java.lang.String toString(RpCommandList coms1)
-
-