|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.lsmp.djep.rpe.RpEval
public final class RpEval
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:
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 |
---|
public static final short CONST
public static final short VAR
public static final short ADD
public static final short SUB
public static final short MUL
public static final short DIV
public static final short MOD
public static final short POW
public static final short AND
public static final short OR
public static final short NOT
public static final short LT
public static final short LE
public static final short GT
public static final short GE
public static final short NE
public static final short EQ
public static final short LIST
public static final short DOT
public static final short CROSS
public static final short ASSIGN
public static final short VLIST
public static final short MLIST
public static final short FUN
public static final short UMINUS
Constructor Detail |
---|
public RpEval(JEP jep)
Method Detail |
---|
public final RpCommandList compile(Node node) throws ParseException
ParseException
public final java.lang.Object visit(ASTStart node, java.lang.Object data) throws ParseException
visit
in interface ParserVisitor
ParseException
public final java.lang.Object visit(SimpleNode node, java.lang.Object data) throws ParseException
visit
in interface ParserVisitor
ParseException
public final java.lang.Object visit(ASTConstant node, java.lang.Object data) throws ParseException
visit
in interface ParserVisitor
ParseException
public final java.lang.Object visit(ASTVarNode node, java.lang.Object data) throws ParseException
visit
in interface ParserVisitor
ParseException
public final java.lang.Object visit(ASTFunNode node, java.lang.Object data) throws ParseException
visit
in interface ParserVisitor
ParseException
public final double evaluate(RpCommandList comList)
public void cleanUp()
public Variable getVariable(int ref)
public java.lang.String getFunction(short ref)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |