weka.core
Class MathematicalExpression

java.lang.Object
  extended by weka.core.MathematicalExpression

public class MathematicalExpression
extends java.lang.Object

Class to build (and evaluate) the tree of the grammar:

 Exp  -> Term '+' Exp   
         | Term '-' Exp 
         | Term         
 Term -> Atom '*' Term  
         | Atom '/' Term 
         | Atom 
 Atom -> <number> 
         | '('Exp')' 
         | function '(' Params ')' 
         | '-' Atom | VARIABLE 
         | Disjunction 
 Params -> E 
           | E,Params 
 Disjunction -> Conjunction 
                | Conjunction '|' Disjunction 
 Conjunction -> NumTest 
                | NumTest '&' Conjunction 
 NumTest -> Exp '<' Exp | Exp '>' Exp | Exp '=' Exp | '[' Disjunction ']' | '!' '[' Disjunction ']'
 
Code example 1:
 String expr = "pow(BASE,EXPONENT)*MULT";
 HashMap symbols = new HashMap();
 symbols.put("BASE", new Double(2));
 symbols.put("EXPONENT", new Double(9));
 symbols.put("MULT", new Double(0.1));
 double result = MathematicalExpression.evaluate(expr, symbols);
 System.out.println(expr + " and " + symbols + " = " + result);
 
Code Example 2 (utilizing a parse tree several times):
 String expr = "pow(BASE,EXPONENT)";
 MathematicalExpression.TreeNode tree = MathematicalExpression.parse(expr);
 for (int i = -10; i <= 10; i++) {
   HashMap symbols = new HashMap();
   symbols.put("BASE", new Double(2));
   symbols.put("EXPONENT", new Double(i));
   double result = MathematicalExpression.evaluate(tree, symbols);
   System.out.println(expr + " and " + symbols + " = " + result);
 }
 

Version:
$Revision: 1.3 $
Author:
Eibe Frank (eibe@cs.waikato.ac.nz), Prados Julien (julien.prados@cui.unige.ch)

Nested Class Summary
static class MathematicalExpression.Tokenizer
          A tokenizer for MathematicalExpression
static class MathematicalExpression.TreeNode
          Tree Node of MathematicalExpression
 
Constructor Summary
MathematicalExpression()
           
 
Method Summary
static double evaluate(MathematicalExpression.TreeNode parseTree, java.util.Map symbols)
          evalutes and returns the result of a mathematical expression, based on the given expression tree and values of the symbols.
static double evaluate(java.lang.String expr, java.util.Map symbols)
          generates a new parse tree from the given expression and evalutes and returns the result of a mathematical expression, based on the given values of the symbols.
static MathematicalExpression.TreeNode parse(java.lang.String exp)
          Construct the Expression tree for a given String
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MathematicalExpression

public MathematicalExpression()
Method Detail

parse

public static MathematicalExpression.TreeNode parse(java.lang.String exp)
                                             throws java.lang.Exception
Construct the Expression tree for a given String

Parameters:
exp - the expression used to build the tree
Returns:
the generated node
Throws:
java.lang.Exception - if EOF is not reached

evaluate

public static double evaluate(java.lang.String expr,
                              java.util.Map symbols)
                       throws java.lang.Exception
generates a new parse tree from the given expression and evalutes and returns the result of a mathematical expression, based on the given values of the symbols.

Parameters:
expr - the expression to evaluate
symbols - the symbol/value mapping
Returns:
the evaluated result
Throws:
java.lang.Exception - if something goes wrong

evaluate

public static double evaluate(MathematicalExpression.TreeNode parseTree,
                              java.util.Map symbols)
                       throws java.lang.Exception
evalutes and returns the result of a mathematical expression, based on the given expression tree and values of the symbols.

Parameters:
parseTree - fully generated expression tree
symbols - the symbol/value mapping
Returns:
the evaluated result
Throws:
java.lang.Exception - if something goes wrong