Overview | Package | Class | Tree | Deprecated | Index | Help | Weka's home
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## weka.core
Class MathematicalExpression

    java.lang.Object
      weka.core.MathematicalExpression

All Implemented Interfaces:
  RevisionHandler

    public class MathematicalExpression

extends java.lang.Object
implements RevisionHandler

Class for evaluating a string adhering the following grammar:

     expr_list ::= expr_list expr_part | expr_part ;
     expr_part ::= expr ;
     expr      ::=   NUMBER
                   | ( expr )
                   | opexpr
                   | varexpr
                   | funcexpr
                   ;

     opexpr    ::=   expr + expr
                   | expr - expr
                   | expr * expr
                   | expr / expr
                   ;

     varexpr  ::=  VARIABLE ;

     funcexpr ::=    abs ( expr )
                   | sqrt ( expr )
                   | log ( expr )
                   | exp ( expr )
                   | sin ( expr )
                   | cos ( expr )
                   | tan ( expr )
                   | rint ( expr )
                   | floor ( expr )
                   | pow ( expr , expr )
                   | ceil ( expr )
                   | ifelse ( boolexpr , expr (if true) , expr (if false) )
                   ;

     boolexpr ::=    BOOLEAN
                   | true
                   | false
                   | expr < expr
                   | expr <= expr
                   | expr > expr
                   | expr >= expr
                   | expr = expr
                   | ( boolexpr )
                   | ! boolexpr
                   | boolexpr & boolexpr
                   | boolexpr | boolexpr
                   ;

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 (uses the "ifelse" construct):

     String expr = "ifelse(I<0,pow(BASE,I*0.5),pow(BASE,I))";
     MathematicalExpression.TreeNode tree = MathematicalExpression.parse(expr);
     HashMap symbols = new HashMap();
     symbols.put("BASE", new Double(2));
     for (int i = -10; i <= 10; i++) {
       symbols.put("I", new Double(i));
       double result = MathematicalExpression.evaluate(expr, symbols);
       System.out.println(expr + " and " + symbols + " = " + result);
     }

Version:
  $Revision: 4942 $
Author:
  FracPete (fracpete at waikato dot ac dot nz)

Constructor Summary
`MathematicalExpression()`

Method Summary
`static double` | `evaluate(java.lang.String expr, java.util.HashMap symbols)` Parses and evaluates the given expression.
`java.lang.String` | `getRevision()` Returns the revision string.

Methods inherited from class java.lang.Object
`equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### MathematicalExpression

    public MathematicalExpression()

Method Detail

### evaluate

    public static double evaluate(java.lang.String expr,
                                  java.util.HashMap symbols)
                           throws java.lang.Exception

  Parses and evaluates the given expression. Returns the result of the 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

### getRevision

    public java.lang.String getRevision()

  Returns the revision string.

  Specified by:
    `getRevision` in interface `RevisionHandler`
  Returns:
    the revision

Overview | Package | Class | Tree | Deprecated | Index | Help | Weka's home
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD