Class MrpEval
- All Implemented Interfaces:
GenericVisitor<Void,,Void, ParseException> JepComponent,Serializable
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 scalars (multiplies last two entries on stack) Add scalars (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. It is 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 scalars and 2, 3 and 4 dimensional vectors and matrices, larger vectors and matrices will be noticeably slower. The cleanUp function should be used when you no longer need the evaluator.
Implementation notes A lot of things have been done to make it as fast as possible:
- Everything is final which maximises the possibility for in-lining.
- All object creation happens during compile.
- All calculations done using double values.
- Vectors and Matrices are instances of VnObj and MatObj optimised for speed.
- The values of variables are kept on local arrays for fast access.
Scalars, 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.
LimitationsThere is a limit to the number of function calls to non-inbuilt functions. A maximum of 65536 calls to external unary functions.
Since Jep 4.1, extension 2.2, the parent class now implements GenericVisitor
and some method signatures have changed to be more specific.
- See Also:
-
Field Summary
Fields inherited from class com.singularsys.extensions.fastreal.AbstractEval
customFunctionCommands, functionHash, jep, ls, opSet -
Constructor Summary
ConstructorsConstructorDescriptionConstructor using the MatrixFactory from the jep instanceMrpEval(Jep mjep, MatrixFactoryI mfac) Constructor using a supplied MatrixFactoryMrpEval(Jep mjep, MatrixFactoryI mfac, int indexShift) Constructor using a supplied function table -
Method Summary
Modifier and TypeMethodDescriptionCompile the expressions to produce a set of commands in reverse Polish notation.compile an expression of the type var = node.convertResult(MrpRes res) Converts the results from the evaluate method into a more useful Double, VectorI or MatrixI type.convertToMatrix(MrpRes res) Converts the results from the evaluate method into a MatrixI type.convertToVector(MrpRes res) Converts the results from the evaluate method into a VectorI type.evaluate(MrpCommandList comList) Evaluate the expression.protected ObjectgetConstantValue(short ref) Returns a new instance which can be safely used in a separate thread.getLightWeightInstance(Jep newjep) Create a new instance using the supplied Jep context.getVariable(MrpVarRef ref) Gets the Jep Variable associated with a particular referenceFinds the reference used for this variable.Finds the reference used for this variable.getVarValue(MrpVarRef ref) Return the value of a variable.voidCallsJepComponent.init(Jep)for any custom function which implementsJepComponent.voidreset()Clear all internal data.voidsetVarValue(MrpVarRef ref, double d) Sets the value of a variablevoidsetVarValue(MrpVarRef varRef, double... vals) Sets the value of a scalar or vector variable.voidsetVarValue(MrpVarRef varRef, double[][] vals) Sets the value of a matrix variablevoidsetVarValue(MrpVarRef varRef, MatrixI val) Sets the value of a matrix variablevoidsetVarValue(MrpVarRef varRef, VectorI val) Sets the value of a vector variabletoString()voidIf the variables used by Jep has a valid value set the corresponding mrpe valuevoidvisit(ASTConstant node, Void data) Visit a constant nodevisit(ASTFunNode node, Void data) Visit a function nodeVisit an operator nodevisit(ASTVarNode node, Void data) Visit a variable nodevisitAssign(ASTOpNode node) visitSetEle(ASTOpNode node) Givens a expression like ele(var,index) = value Produce sequence of commandsMethods inherited from class com.singularsys.extensions.fastreal.AbstractEval
chargeFunctionName, duplicateCustomFunctions, getFunction, getUserFunction, makeException, staticGetFunctionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface com.singularsys.jep.GenericVisitor
makeException, visit, visitChildren
-
Constructor Details
-
MrpEval
Constructor using the MatrixFactory from the jep instance- Parameters:
mjep- jep instance
-
MrpEval
Constructor using a supplied MatrixFactory- Parameters:
mjep- jep instancemfac- matrix factory instance
-
MrpEval
Constructor using a supplied function table- Parameters:
mjep- jep instancemfac- matrix factory instanceindexShift- base for array access, either 0 for zero based indexing, or 1 for 1 based indexing
-
-
Method Details
-
compile
compile an expression of the type var = node.- Throws:
ParseException
-
compile
Compile the expressions to produce a set of commands in reverse Polish notation.- Throws:
ParseException
-
visit
Description copied from interface:GenericVisitorVisit a constant node- Parameters:
node- current nodedata- context data- Returns:
- the result of visiting the node
- Throws:
ParseException
-
visit
Description copied from interface:GenericVisitorVisit a variable node- Parameters:
node- current nodedata- context data- Returns:
- the result of visiting the node
- Throws:
ParseException
-
visitAssign
- Throws:
ParseException
-
visitSetEle
Givens a expression like ele(var,index) = value Produce sequence of commands... 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);
- Parameters:
node- the expression- Returns:
- null
- Throws:
ParseException- on error
-
visit
Description copied from interface:GenericVisitorVisit an operator node- Parameters:
node- current nodedata- context data- Returns:
- the result of visiting the node
- Throws:
ParseException
-
visit
Description copied from interface:GenericVisitorVisit a function node- Parameters:
node- current nodedata- context data- Returns:
- the result of visiting the node
- Throws:
ParseException
-
evaluate
Evaluate the expression.- Parameters:
comList- list of commands to evaluate- Returns:
- the value after evaluation
- Throws:
EvaluationException- if an exception occurs in a user function
-
getConstantValue
- Specified by:
getConstantValuein classAbstractEval
-
getVarRef
Finds the reference used for this variable. The variable should have a known dimensions. This can be set either by using theDimensionVisitorby setting it to an explicit value or by setting the dimensions explicitly usingvar.setHook(DimensionVisitor.DIM_KEY,Dimensions.SCALAR).- Parameters:
uVar- the variable- Returns:
- an index used to refer to the variable
- Throws:
ParseException- if the dimensions for the variable cannot be found.
-
getVarRef
Finds the reference used for this variable. This method requires an associate Jep reference so cannot be used with instances created usinggetLightWeightInstance()- Parameters:
name- variable name- Returns:
- the reference
- Throws:
ParseException- if the dimensions for the variable cannot be found, or there is no jep instance
-
setVarValue
Sets the value of a variable- Parameters:
ref- reference to the variabled- value- Throws:
EvaluationException- if the reference is not a scalar variable
-
setVarValue
Sets the value of a scalar or vector variable. Scalar variables can be set to arrays of length 1.- Parameters:
varRef- reference to the variablevals- either an array of doubles, or a set of values- Throws:
EvaluationException
-
setVarValue
Sets the value of a matrix variable- Parameters:
varRef- reference to the variablevals- two-dimensional array of values- Throws:
EvaluationException- if the variable is not a matrix variable or has the wrong dimensions- Since:
- Jep 4.0/Extensions 2.1
-
setVarValue
Sets the value of a vector variable- Parameters:
varRef- reference to the variableval- vector value- Throws:
EvaluationException- if the dimensions do not match
-
setVarValue
Sets the value of a matrix variable- Parameters:
varRef- reference to the variableval- a matrix value- Throws:
EvaluationException- if the dimensions do not match
-
getVarValue
Return the value of a variable. It returns a subtype ofMrpReswhich can be converted usingconvertResult(MrpRes).- Parameters:
ref- a reference to the variable- Returns:
- the value of that variable or null if the reference is no longer current (happens when the dimensions of the variable changes)
-
getVariable
Gets the Jep Variable associated with a particular reference- Parameters:
ref- reference to variable- Returns:
- the Jep Variable
-
updateFromJepVariables
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
-
updateToJepVariables
- Throws:
EvaluationException
-
convertResult
Converts the results from the evaluate method into a more useful Double, VectorI or MatrixI type. Conversion to vectors or matrices will not work if aMatrixFactoryIis not defined, i.e. if this instance is created usinggetLightWeightInstance().- Parameters:
res- the value returned byevaluate(MrpCommandList)- Returns:
- a Double, VectorI or MatrixI type.
- Throws:
EvaluationException- if noMatrixFactoryIis defined
-
convertToMatrix
Converts the results from the evaluate method into a MatrixI type. Will fail if aMatrixFactoryIis not defined, i.e. if this instance is created usinggetLightWeightInstance().- Parameters:
res- the value returned byevaluate(MrpCommandList)- Returns:
- a MatrixI type, uses the MatrixFactory supplied to MatrixFunctionTable
- Throws:
EvaluationException- if the res is a scalar or vector or noMatrixFactoryIis defined
-
convertToVector
Converts the results from the evaluate method into a VectorI type. Will fail if aMatrixFactoryIis not defined,- Parameters:
res- the value returned byevaluate(MrpCommandList)- Returns:
- a VectorI type,
- Throws:
EvaluationException- if the res is a scalar or matrix or noMatrixFactoryIis defined
-
reset
public void reset()Clear all internal data. This removes all variables, so variable references previously found fromgetVarRef(String),getVarRef(Variable)will no longer be valid. -
toString
-
toString
-
getLightWeightInstance
Returns a new instance which can be safely used in a separate thread. SeeAbstractEval.duplicateCustomFunctions(AbstractEval, Jep)for handling of custom functions.- Returns:
- a new instance
- Since:
- Jep 4.0/Extensions 2.1
-
getLightWeightInstance
Create a new instance using the supplied Jep context. Will duplicate all variables and other data so the evaluator can be safely used in a new thread. SeeAbstractEval.duplicateCustomFunctions(AbstractEval, Jep)for handling of custom functions.- Parameters:
newjep- new jep instance for context- Returns:
- a new instance
- Since:
- Jep 4.0/Extensions 2.1
-
init
Description copied from class:AbstractEvalCallsJepComponent.init(Jep)for any custom function which implementsJepComponent.- Specified by:
initin interfaceJepComponent- Overrides:
initin classAbstractEval- Parameters:
jep- the current Jep instance
-