public final class MrpEval extends AbstractEval
To use do
Jep jep = new Jep(); MRpEval rpe = new MRpEval(j); DimensionVisitor dimV = new DimensionVisitor(jep); Node node = jep.parse(...); dimV.visit(node); MRpCommandList list = rpe.compile(node); MRpRes rpRes = rpe.evaluate(list); System.out.println(rpRes.toString()); MatrixI mat = rpRes.toVecMat(); rpe.cleanUp();
The real use of this class is when an equation (or set of equations)
need to be repeatedly evaluated with different values for the variables.
MRpEval use an internal store for variable values different from those
used in the main Jep classes. Changes in the Jep variable values,
say by calling Jep.setVarValue
,
are reflected
in changes in MRpEval variables, (the converse does not hold).
A more efficient way is to use int ref=getVarRef(var)
to return an index number of the variable and then calling
setVarVal(ref,value)
to set its value.
For example
MRpCommandList list = rpe.compile(node); int ref = rpe.getVarRef(j.getVar("x")); for(double x=-1.;x<1.001;x+=0.1) { rpe.setVarVal(ref,x); rpe.evaluate(list); }
Combining mrpe with Differentiation requires special techniques to cope with that fact that internal equations are used
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 (actually a set of stacks) and returns the last object on the stack.
A few cautionary notes: the values returned by evaluate are references to internal variables, their values will change at the next call to compile or evaluate. Its very unlikely to be thread safe. It only works over doubles; expressions with complex numbers or strings will cause problems. It is tuned to work best for expressions involving scalers and 2, 3 and 4 dimensional vectors and matricies, larger vectors and matrices will be noticeably slower. The cleanUp function should be used when you no longer need the evaluator, this stops the evaluator listening to Variable through the java.util.Observer interface.
Implementation notes A lot of things have been done to make it as fast as possible:
Scalers, Vectors and matrices used during evaluation are kept in three store classes. Each Store class contains a stack, a heap and a array of variable values. During evaluation objects are pushed and popped from the stack when a new object is needed it is taken from the heap.
customFunctionCommands, functionHash, jep, ls, opSet
Constructor and Description |
---|
MrpEval(Jep mjep,
MatrixFactoryI mfac)
Constructor using a supplied function table
|
Modifier and Type | Method and Description |
---|---|
MrpCommandList |
compile(Node node)
Compile the expressions to produce a set of commands in reverse Polish notation.
|
MrpCommandList |
compile(Variable var,
Node node)
compile an expression of the type var = node.
|
java.lang.Object |
convertResult(MrpRes res)
Converts the results from the evaluate method into a more useful Double, VectorI or MatrixI type.
|
MatrixI |
convertToMatrix(MrpRes res)
Converts the results from the evaluate method into a MatrixI type.
|
VectorI |
convertToVector(MrpRes res)
Converts the results from the evaluate method into a VectorI type.
|
MrpRes |
evaluate(MrpCommandList comList)
Evaluate the expression.
|
protected java.lang.Object |
getConstantValue(short ref) |
Variable |
getVariable(MrpVarRef ref)
Gets the Jep Variable associated with a particular reference
|
MrpVarRef |
getVarRef(java.lang.String name)
Finds the reference used for this variable.
|
MrpVarRef |
getVarRef(Variable uVar)
Finds the reference used for this variable.
|
MrpRes |
getVarValue(MrpVarRef ref)
Return the value of a variable.
|
void |
reset()
Clear all internal data.
|
void |
setVarValue(MrpVarRef varRef,
double... vals)
Sets the value of a scaler or vector variable
|
void |
setVarValue(MrpVarRef ref,
double d)
Sets the value of a variable
|
void |
setVarValue(MrpVarRef varRef,
MatrixI val)
Sets the value of a matrix variable
|
void |
setVarValue(MrpVarRef varRef,
VectorI val)
Sets the value of a vector variable
|
java.lang.String |
toString() |
java.lang.String |
toString(MrpCommandList.MrpCommand com) |
void |
updateFromJepVariables()
If the variables used by Jep have changed the values
|
void |
updateToJepVariables() |
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) |
java.lang.Object |
visitAssign(ASTOpNode node) |
java.lang.Object |
visitSetEle(ASTOpNode node)
Givens a expression like ele(var,index) = value
Produce sequence of commands
...
|
getFunction, getLightWeightInstance, getUserFunction, init, staticGetFunction
public MrpEval(Jep mjep, MatrixFactoryI mfac)
mjep
- mfac
- public final MrpCommandList compile(Variable var, Node node) throws ParseException
ParseException
public final MrpCommandList compile(Node node) throws ParseException
ParseException
public final java.lang.Object visit(ASTConstant node, java.lang.Object data) throws ParseException
ParseException
public final java.lang.Object visit(ASTVarNode node, java.lang.Object data) throws ParseException
ParseException
public java.lang.Object visitAssign(ASTOpNode node) throws JepException, ParseException
JepException
ParseException
public java.lang.Object visitSetEle(ASTOpNode node) throws JepException, ParseException
... value = pop() index = pop() addCommand(VAR,dimType,vRef); // pops the var value addCommand(SETELE,dimType,index) // sets top of stacks the index's element addCommand(ASSIGN,dt,vRef);
node
- JepException
ParseException
public final java.lang.Object visit(ASTOpNode node, java.lang.Object data) throws JepException
JepException
public final java.lang.Object visit(ASTFunNode node, java.lang.Object data) throws JepException
JepException
public final MrpRes evaluate(MrpCommandList comList)
protected java.lang.Object getConstantValue(short ref)
getConstantValue
in class AbstractEval
public MrpVarRef getVarRef(Variable uVar) throws ParseException
DimensionVisitor
by setting it to a explicit value or by setting
the dimensions explicitly using
var.setHook(DimensionVisitor.DIM_KEY,Dimensions.ONE)
.uVar
- the variableParseException
- if the dimensions for the variable cannot be found.public MrpVarRef getVarRef(java.lang.String name) throws ParseException
name
- variable nameParseException
- if the dimensions for the variable cannot be found.public void setVarValue(MrpVarRef ref, double d) throws EvaluationException
ref
- reference to the variabled
- valueEvaluationException
- if the reference is not a scaler variablepublic void setVarValue(MrpVarRef varRef, double... vals) throws EvaluationException
varRef
- reference to the variablevals
- either an array of doubles, or a set of valuesEvaluationException
public void setVarValue(MrpVarRef varRef, VectorI val) throws EvaluationException
varRef
- reference to the variableval
- vector valueEvaluationException
- if the dimensions do not matchpublic void setVarValue(MrpVarRef varRef, MatrixI val) throws EvaluationException
varRef
- reference to the variableval
- a matrix vlaueEvaluationException
- if the dimensions do not matchpublic MrpRes getVarValue(MrpVarRef ref)
MrpRes
which can be converted using convertResult(MrpRes)
.ref
- a reference to the variablepublic Variable getVariable(MrpVarRef ref) throws ParseException
ref
- reference to variableParseException
public void updateFromJepVariables() throws EvaluationException
EvaluationException
public void updateToJepVariables() throws EvaluationException
EvaluationException
public java.lang.Object convertResult(MrpRes res) throws EvaluationException
res
- the value returned by evaluate(MrpCommandList)
EvaluationException
public MatrixI convertToMatrix(MrpRes res) throws EvaluationException
res
- the value returned by evaluate(MrpCommandList)
EvaluationException
- if the res is a scalar or vectorpublic VectorI convertToVector(MrpRes res) throws EvaluationException
res
- the value returned by evaluate(MrpCommandList)
EvaluationException
- if the res is a scalar or matrixpublic void reset()
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toString(MrpCommandList.MrpCommand com)
Copyright © 2018 Singular Systems http://www.singularsys.com/jep