org.lsmp.djep.rpe
Class RpEval

java.lang.Object
  extended byorg.lsmp.djep.rpe.RpEval
All Implemented Interfaces:
ParserVisitor

public final class RpEval
extends java.lang.Object
implements ParserVisitor

A fast evaluation algorithm for equations over Doubles, does not work with vectors or matricies. This is based around reverse polish notation and is optimised for speed at every oportunity.

To use do

 JEP j = ...;
 Node node = ...; 
 RpEval rpe = new RpEval(j);
 RpCommandList list = rpe.compile(node);
 double val rpRes = rpe.evaluate(list);
 System.out.println(val);
 rpe.cleanUp();
 
The compile methods converts the expresion represented by node into a string of commands. For example the expresion "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 thoe last object on the stack.

A few cautionary notes: Its very unlikely to be thread safe. It only works over doubles expresions with complex numbers or strings will cause problems. It only works for expressions involving scalers.

Implementation notes A lot of things have been done to make it as fast as posible:

Author:
Rich Morris Created on 14-Apr-2004

Constructor Summary
RpEval(JEP jep)
           
 
Method Summary
 void cleanUp()
          Removes observers and other cleanup needed when evaluator no longer used.
 RpCommandList compile(Node node)
          Compile the expresions to produce a set of commands in reverse Polish notation.
 double evaluate(RpCommandList comList)
          Evaluate the expression.
 java.lang.Object visit(ASTConstant node, java.lang.Object data)
           
 java.lang.Object visit(ASTFunNode node, java.lang.Object data)
           
 java.lang.Object visit(ASTStart node, java.lang.Object data)
           
 java.lang.Object visit(ASTVarNode node, java.lang.Object data)
           
 java.lang.Object visit(SimpleNode node, java.lang.Object data)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RpEval

public RpEval(JEP jep)
Method Detail

compile

public final RpCommandList compile(Node node)
                            throws ParseException
Compile the expresions to produce a set of commands in reverse Polish notation.

Throws:
ParseException

visit

public final java.lang.Object visit(ASTStart node,
                                    java.lang.Object data)
                             throws ParseException
Specified by:
visit in interface ParserVisitor
Throws:
ParseException

visit

public final java.lang.Object visit(SimpleNode node,
                                    java.lang.Object data)
                             throws ParseException
Specified by:
visit in interface ParserVisitor
Throws:
ParseException

visit

public final java.lang.Object visit(ASTConstant node,
                                    java.lang.Object data)
                             throws ParseException
Specified by:
visit in interface ParserVisitor
Throws:
ParseException

visit

public final java.lang.Object visit(ASTVarNode node,
                                    java.lang.Object data)
                             throws ParseException
Specified by:
visit in interface ParserVisitor
Throws:
ParseException

visit

public final java.lang.Object visit(ASTFunNode node,
                                    java.lang.Object data)
                             throws ParseException
Specified by:
visit in interface ParserVisitor
Throws:
ParseException

evaluate

public final double evaluate(RpCommandList comList)
Evaluate the expression.

Returns:
the double value of the equation

cleanUp

public void cleanUp()
Removes observers and other cleanup needed when evaluator no longer used.



http://www.singularsys.com/jep Copyright © 2004 Singular Systems