Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

jmat.function.expressionParser
Class Evaluator  view Evaluator download Evaluator.java

java.lang.Object
  extended byjmat.function.expressionParser.Evaluator

public class Evaluator
extends java.lang.Object

Mathematic expression evaluator. Supports the following functions: +, -, *, /, ^, %, cos, sin, tan, acos, asin, atan, sqrt, sqr, log, min, max, ceil, floor, abs, neg, rndr.
When the getValue() is called, a Double object is returned. If it returns null, an error occured.

 Sample:
 MathEvaluator m = new MathEvaluator("-5-6/(-2) + sqr(15+x)");
 m.addVariable("x", 15.1d);
 System.out.println( m.getValue() );
 

Version:
1.1

Nested Class Summary
protected  class Evaluator.Node
           
protected  class Evaluator.Operator
           
 
Field Summary
private  java.lang.String expression
           
private  Evaluator.Node node
           
protected static Evaluator.Operator[] operators
           
private  java.util.HashMap variables
           
 
Constructor Summary
Evaluator()
          creates an empty MathEvaluator.
Evaluator(java.lang.String s)
          creates a MathEvaluator and assign the math expression string.
 
Method Summary
protected static void _D(java.lang.String s)
           
 void addVariable(java.lang.String v, double val)
          adds a variable and its value in the MathEvaluator
 void addVariable(java.lang.String v, java.lang.Double val)
          adds a variable and its value in the MathEvaluator
 void addVariable(java.lang.String v, jmat.data.Matrix val)
          adds a variable and its value in the MathEvaluator
private static java.lang.Object evaluate(Evaluator.Node n)
           
private static java.lang.Object evaluateExpression1(Evaluator.Operator o, java.lang.Object o1)
           
private static java.lang.Object evaluateExpression2(Evaluator.Operator o, java.lang.Object o1, java.lang.Object o2)
           
private  java.lang.Object getObject(java.lang.String s)
           
protected  Evaluator.Operator[] getOperators()
           
 java.lang.Object getValue()
          evaluates and returns the value of the expression
 java.lang.Object getVariable(java.lang.String s)
          gets the variable's value that was assigned previously
private  void init()
           
private  void initializeOperators()
           
 void reset()
          resets the evaluator
 void setExpression(java.lang.String s)
          sets the expression
 void trace()
          trace the binary tree for debug
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

operators

protected static Evaluator.Operator[] operators

variables

private java.util.HashMap variables

node

private Evaluator.Node node

expression

private java.lang.String expression
Constructor Detail

Evaluator

public Evaluator()
creates an empty MathEvaluator. You need to use setExpression(String s) to assign a math expression string to it.


Evaluator

public Evaluator(java.lang.String s)
creates a MathEvaluator and assign the math expression string.

Method Detail

setExpression

public void setExpression(java.lang.String s)
sets the expression


getValue

public java.lang.Object getValue()
evaluates and returns the value of the expression


getVariable

public java.lang.Object getVariable(java.lang.String s)
gets the variable's value that was assigned previously


addVariable

public void addVariable(java.lang.String v,
                        jmat.data.Matrix val)
adds a variable and its value in the MathEvaluator


addVariable

public void addVariable(java.lang.String v,
                        double val)
adds a variable and its value in the MathEvaluator


addVariable

public void addVariable(java.lang.String v,
                        java.lang.Double val)
adds a variable and its value in the MathEvaluator


reset

public void reset()
resets the evaluator


trace

public void trace()
trace the binary tree for debug


getOperators

protected Evaluator.Operator[] getOperators()

_D

protected static void _D(java.lang.String s)

evaluate

private static java.lang.Object evaluate(Evaluator.Node n)

evaluateExpression1

private static java.lang.Object evaluateExpression1(Evaluator.Operator o,
                                                    java.lang.Object o1)

evaluateExpression2

private static java.lang.Object evaluateExpression2(Evaluator.Operator o,
                                                    java.lang.Object o1,
                                                    java.lang.Object o2)

getObject

private java.lang.Object getObject(java.lang.String s)

init

private void init()

initializeOperators

private void initializeOperators()