Class MrpEval

  • All Implemented Interfaces:
    JepComponent, ParserVisitor, java.io.Serializable

    public final class MrpEval
    extends AbstractEval
    A fast evaluation algorithm for equations using Vectors and Matrix over the Doubles. This is based around reverse polish notation (hence the name M Rp Eval) and is optimised for speed at every opportunity.

    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:

    • 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.

    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.

    Limitations

    There is a limit to the number of function calls to non-inbuilt functions. A maximum of 65536 calls to external unary functions,

    See Also:
    Serialized Form
    • Constructor Detail

      • MrpEval

        public MrpEval​(Jep mjep)
        Constructor using the MatrixFactory from the jep instance
        Parameters:
        mjep - jep instance
      • MrpEval

        public MrpEval​(Jep mjep,
                       MatrixFactoryI mfac)
        Constructor using a supplied MatrixFactory
        Parameters:
        mjep - jep instance
        mfac - matrix factory instance
      • MrpEval

        public MrpEval​(Jep mjep,
                       MatrixFactoryI mfac,
                       int indexShift)
        Constructor using a supplied function table
        Parameters:
        mjep - jep instance
        mfac - matrix factory instance
        indexShift - base for array access, either 0 for zero based indexing, or 1 for 1 based indexing