org.lsmp.djep.rpe
Class RpEval

java.lang.Object
  extended by org.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 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();
 
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: Its very unlikely to be thread safe. It only works over doubles expressions 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 possible:

Author:
Rich Morris Created on 14-Apr-2004

Field Summary
static short ADD
           
static short AND
           
static short ASSIGN
           
static short CONST
          Index for each command
static short CROSS
           
static short DIV
           
static short DOT
           
static short EQ
           
static short FUN
           
static short GE
           
static short GT
           
static short LE
           
static short LIST
           
static short LT
           
static short MLIST
           
static short MOD
           
static short MUL
           
static short NE
           
static short NOT
           
static short OR
           
static short POW
           
static short SUB
           
static short UMINUS
           
static short VAR
           
static short VLIST
           
 
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 expressions to produce a set of commands in reverse Polish notation.
 double evaluate(RpCommandList comList)
          Evaluate the expression.
 java.lang.String getFunction(short ref)
           
 Variable getVariable(int ref)
           
 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
 

Field Detail

CONST

public static final short CONST
Index for each command

See Also:
Constant Field Values

VAR

public static final short VAR
See Also:
Constant Field Values

ADD

public static final short ADD
See Also:
Constant Field Values

SUB

public static final short SUB
See Also:
Constant Field Values

MUL

public static final short MUL
See Also:
Constant Field Values

DIV

public static final short DIV
See Also:
Constant Field Values

MOD

public static final short MOD
See Also:
Constant Field Values

POW

public static final short POW
See Also:
Constant Field Values

AND

public static final short AND
See Also:
Constant Field Values

OR

public static final short OR
See Also:
Constant Field Values

NOT

public static final short NOT
See Also:
Constant Field Values

LT

public static final short LT
See Also:
Constant Field Values

LE

public static final short LE
See Also:
Constant Field Values

GT

public static final short GT
See Also:
Constant Field Values

GE

public static final short GE
See Also:
Constant Field Values

NE

public static final short NE
See Also:
Constant Field Values

EQ

public static final short EQ
See Also:
Constant Field Values

LIST

public static final short LIST
See Also:
Constant Field Values

DOT

public static final short DOT
See Also:
Constant Field Values

CROSS

public static final short CROSS
See Also:
Constant Field Values

ASSIGN

public static final short ASSIGN
See Also:
Constant Field Values

VLIST

public static final short VLIST
See Also:
Constant Field Values

MLIST

public static final short MLIST
See Also:
Constant Field Values

FUN

public static final short FUN
See Also:
Constant Field Values

UMINUS

public static final short UMINUS
See Also:
Constant Field Values
Constructor Detail

RpEval

public RpEval(JEP jep)
Method Detail

compile

public final RpCommandList compile(Node node)
                            throws ParseException
Compile the expressions 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.


getVariable

public Variable getVariable(int ref)

getFunction

public java.lang.String getFunction(short ref)


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