Debian Codegraph @

src/dr/inference/model/RPNexpressionCalculator.java

raw symbols SCIP
1 /*
2 * RPNexpressionCalculator.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.inference.model;
27
28 import java.util.Stack;
29
30 /**
31 * Simple RPN expression evaluator.
32 *
33 * Limitations:
34 * - variables are statistics of 1 dimension.
35 * - Four basic operations (easy to extend, though)
36 *
37 * @author Joseph Heled
38 * Date: 10/05/2008
39 */
40 public class RPNexpressionCalculator {
41 /**
42 * Interfave for variable access by name
43 */
44 public interface GetVariable {
45 /**
46 *
47 * @param name
48 * @return variable value
49 */
50 double get(String name);
51 }
52
53 private enum OP { OP_ADD, OP_SUB, OP_MULT, OP_DIV, OP_LOG, OP_EXP, OP_CHS, OP_CONST, OP_REF }
54
55 private class Eelement {
56 OP op;
57 String name;
58 private double value;
59
60 Eelement(OP op) {
61 this.op = op;
62 name = null;
63 }
64
65 Eelement(String name) {
66 this.op = OP.OP_REF;
67 this.name = name;
68 }
69
70 Eelement(double val) {
71 this.op = OP.OP_CONST;
72 this.value = val;
73 }
74 }
75
76 Eelement[] expression;
77
78 public RPNexpressionCalculator(String expressionString) {
79 String[] tokens = expressionString.trim().split("\\s+");
80
81 expression = new Eelement[tokens.length];
82
83 for(int k = 0; k < tokens.length; ++k) {
84 String tok = tokens[k];
85 Eelement element;
86 if( tok.equals("+") ) {
87 element = new Eelement(OP.OP_ADD);
88 } else if( tok.equals("-") ) {
89 element = new Eelement(OP.OP_SUB);
90 } else if( tok.equals("*") ) {
91 element = new Eelement(OP.OP_MULT);
92 } else if( tok.equals("/") ) {
93 element = new Eelement(OP.OP_DIV);
94 } else if( tok.equals("log") ) {
95 element = new Eelement(OP.OP_LOG);
96 } else if( tok.equals("exp") ) {
97 element = new Eelement(OP.OP_EXP);
98 } else if( tok.equals("chs") ) {
99 element = new Eelement(OP.OP_CHS);
100 } else {
101 try {
102 double val = Double.parseDouble(tok);
103 element = new Eelement(val);
104 } catch(java.lang.NumberFormatException ex) {
105 element = new Eelement(tok);
106 }
107 }
108 expression[k] = element;
109 }
110 }
111
112 /**
113 *
114 * @param variables
115 * @return evaluate expression given context (i.e. variables)
116 */
117 public double evaluate(GetVariable variables) {
118 Stack<Double> stack = new Stack<Double>();
119
120 for( Eelement elem : expression ) {
121 switch( elem.op ) {
122 case OP_ADD: {
123 final Double y = stack.pop();
124 final Double x = stack.pop();
125 stack.push(x+y);
126 break;
127 }
128 case OP_SUB: {
129 final Double y = stack.pop();
130 final Double x = stack.pop();
131 stack.push(x-y);
132 break;
133 }
134 case OP_MULT : {
135 final Double y = stack.pop();
136 final Double x = stack.pop();
137 stack.push(x*y);
138 break;
139 }
140 case OP_DIV : {
141 final Double y = stack.pop();
142 final Double x = stack.pop();
143 stack.push(x/y);
144 break;
145 }
146 case OP_CHS: {
147 final Double x = stack.pop();
148 stack.push(-x);
149 break;
150 }
151 case OP_LOG: {
152 final Double x = stack.pop();
153 if( x <= 0.0 ) {
154 return Double.NaN;
155 }
156 stack.push(Math.log(x));
157 break;
158 }
159 case OP_EXP: {
160 final Double x = stack.pop();
161 stack.push(Math.exp(x));
162 break;
163 }
164 case OP_CONST: {
165 stack.push(elem.value);
166 break;
167 }
168 case OP_REF: {
169 stack.push(variables.get(elem.name) );
170 break;
171 }
172 }
173 }
174
175 return stack.pop();
176 }
177
178 /**
179 * @return null if all ok, error message otherwise
180 **/
181 public String validate() {
182 int stackSize = 0;
183
184 for(Eelement elem : expression) {
185 switch( elem.op ) {
186 case OP_ADD:
187 case OP_SUB:
188 case OP_MULT:
189 case OP_DIV: {
190 if( stackSize < 2 ) {
191 return "Binary operator underflow";
192 }
193 stackSize -= 1;
194 break;
195 }
196 case OP_CHS:
197 case OP_LOG:
198 case OP_EXP: {
199 if( stackSize == 0 ) {
200 return "Unary operator underflow";
201 }
202 break;
203 }
204 case OP_CONST: {
205 stackSize += 1;
206 break;
207 }
208 case OP_REF: {
209 stackSize += 1;
210 break;
211 }
212 }
213 }
214
215 if( stackSize != 1 ) {
216 return "Stack size " + stackSize + " ( != 1 ) at end of expression evaluation";
217 }
218
219 return null;
220 }
221 }