1 | /* |
---|
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. |
---|
6 | * |
---|
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. |
---|
11 | * |
---|
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. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * MathematicalExpression.java |
---|
19 | * Copyright (C) 2008 University of Waikato, Hamilton, New Zealand |
---|
20 | */ |
---|
21 | |
---|
22 | package weka.core; |
---|
23 | |
---|
24 | import weka.core.mathematicalexpression.Parser; |
---|
25 | import weka.core.mathematicalexpression.Scanner; |
---|
26 | import java_cup.runtime.DefaultSymbolFactory; |
---|
27 | import java_cup.runtime.SymbolFactory; |
---|
28 | |
---|
29 | import java.io.ByteArrayInputStream; |
---|
30 | import java.util.HashMap; |
---|
31 | |
---|
32 | /** |
---|
33 | * Class for evaluating a string adhering the following grammar:<br/> |
---|
34 | * |
---|
35 | * <pre> |
---|
36 | * expr_list ::= expr_list expr_part | expr_part ; |
---|
37 | * expr_part ::= expr ; |
---|
38 | * expr ::= NUMBER |
---|
39 | * | ( expr ) |
---|
40 | * | opexpr |
---|
41 | * | varexpr |
---|
42 | * | funcexpr |
---|
43 | * ; |
---|
44 | * |
---|
45 | * opexpr ::= expr + expr |
---|
46 | * | expr - expr |
---|
47 | * | expr * expr |
---|
48 | * | expr / expr |
---|
49 | * ; |
---|
50 | * |
---|
51 | * varexpr ::= VARIABLE ; |
---|
52 | * |
---|
53 | * funcexpr ::= abs ( expr ) |
---|
54 | * | sqrt ( expr ) |
---|
55 | * | log ( expr ) |
---|
56 | * | exp ( expr ) |
---|
57 | * | sin ( expr ) |
---|
58 | * | cos ( expr ) |
---|
59 | * | tan ( expr ) |
---|
60 | * | rint ( expr ) |
---|
61 | * | floor ( expr ) |
---|
62 | * | pow ( expr , expr ) |
---|
63 | * | ceil ( expr ) |
---|
64 | * | ifelse ( boolexpr , expr (if true) , expr (if false) ) |
---|
65 | * ; |
---|
66 | * |
---|
67 | * boolexpr ::= BOOLEAN |
---|
68 | * | true |
---|
69 | * | false |
---|
70 | * | expr < expr |
---|
71 | * | expr <= expr |
---|
72 | * | expr > expr |
---|
73 | * | expr >= expr |
---|
74 | * | expr = expr |
---|
75 | * | ( boolexpr ) |
---|
76 | * | ! boolexpr |
---|
77 | * | boolexpr & boolexpr |
---|
78 | * | boolexpr | boolexpr |
---|
79 | * ; |
---|
80 | * </pre> |
---|
81 | * |
---|
82 | * Code example 1: |
---|
83 | * <pre> |
---|
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); |
---|
91 | * </pre> |
---|
92 | * |
---|
93 | * Code Example 2 (uses the "ifelse" construct): |
---|
94 | * <pre> |
---|
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); |
---|
103 | * } |
---|
104 | * </pre> |
---|
105 | * |
---|
106 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
107 | * @version $Revision: 4939 $ |
---|
108 | */ |
---|
109 | public class MathematicalExpression |
---|
110 | implements RevisionHandler { |
---|
111 | |
---|
112 | /** |
---|
113 | * Parses and evaluates the given expression. |
---|
114 | * Returns the result of the mathematical expression, based on the given |
---|
115 | * values of the symbols. |
---|
116 | * |
---|
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 |
---|
121 | */ |
---|
122 | public static double evaluate(String expr, HashMap symbols) throws Exception { |
---|
123 | SymbolFactory sf; |
---|
124 | ByteArrayInputStream parserInput; |
---|
125 | Parser parser; |
---|
126 | |
---|
127 | sf = new DefaultSymbolFactory(); |
---|
128 | parserInput = new ByteArrayInputStream(expr.getBytes()); |
---|
129 | parser = new Parser(new Scanner(parserInput, sf), sf); |
---|
130 | parser.setSymbols(symbols); |
---|
131 | parser.parse(); |
---|
132 | |
---|
133 | return parser.getResult(); |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * Returns the revision string. |
---|
138 | * |
---|
139 | * @return the revision |
---|
140 | */ |
---|
141 | public String getRevision() { |
---|
142 | return RevisionUtils.extract("$Revision: 4939 $"); |
---|
143 | } |
---|
144 | } |
---|