2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 2 of the License, or
5
* (at your option) any later version.
7
* This program is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
* GNU General Public License for more details.
12
* You should have received a copy of the GNU General Public License
13
* along with this program; if not, write to the Free Software
14
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
* MathematicalExpression.java
19
* Copyright (C) 2008 University of Waikato, Hamilton, New Zealand
24
import weka.core.mathematicalexpression.Parser;
25
import weka.core.mathematicalexpression.Scanner;
26
import weka.core.parser.java_cup.runtime.DefaultSymbolFactory;
27
import weka.core.parser.java_cup.runtime.SymbolFactory;
29
import java.io.ByteArrayInputStream;
30
import java.util.HashMap;
33
* Class for evaluating a string adhering the following grammar:<br/>
36
* expr_list ::= expr_list expr_part | expr_part ;
37
* expr_part ::= expr ;
45
* opexpr ::= expr + expr
51
* varexpr ::= VARIABLE ;
53
* funcexpr ::= abs ( expr )
62
* | pow ( expr , expr )
64
* | ifelse ( boolexpr , expr (if true) , expr (if false) )
67
* boolexpr ::= BOOLEAN
77
* | boolexpr & boolexpr
78
* | boolexpr | boolexpr
84
* String expr = "pow(BASE,EXPONENT)*MULT";
85
* HashMap symbols = new HashMap();
86
* symbols.put("BASE", new Double(2));
87
* symbols.put("EXPONENT", new Double(9));
88
* symbols.put("MULT", new Double(0.1));
89
* double result = MathematicalExpression.evaluate(expr, symbols);
90
* System.out.println(expr + " and " + symbols + " = " + result);
93
* Code Example 2 (uses the "ifelse" construct):
95
* String expr = "ifelse(I<0,pow(BASE,I*0.5),pow(BASE,I))";
96
* MathematicalExpression.TreeNode tree = MathematicalExpression.parse(expr);
97
* HashMap symbols = new HashMap();
98
* symbols.put("BASE", new Double(2));
99
* for (int i = -10; i <= 10; i++) {
100
* symbols.put("I", new Double(i));
101
* double result = MathematicalExpression.evaluate(expr, symbols);
102
* System.out.println(expr + " and " + symbols + " = " + result);
106
* @author FracPete (fracpete at waikato dot ac dot nz)
107
* @version $Revision: 1.8 $
109
public class MathematicalExpression
110
implements RevisionHandler {
113
* Parses and evaluates the given expression.
114
* Returns the result of the mathematical expression, based on the given
115
* values of the symbols.
117
* @param expr the expression to evaluate
118
* @param symbols the symbol/value mapping
119
* @return the evaluated result
120
* @throws Exception if something goes wrong
122
public static double evaluate(String expr, HashMap symbols) throws Exception {
124
ByteArrayInputStream parserInput;
127
sf = new DefaultSymbolFactory();
128
parserInput = new ByteArrayInputStream(expr.getBytes());
129
parser = new Parser(new Scanner(parserInput, sf), sf);
130
parser.setSymbols(symbols);
133
return parser.getResult();
137
* Returns the revision string.
139
* @return the revision
141
public String getRevision() {
142
return RevisionUtils.extract("$Revision: 1.8 $");