Debian Codegraph @

src/dr/inference/model/RPNcalculatorStatistic.java

raw symbols SCIP
1 /*
2 * RPNcalculatorStatistic.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.Map;
29
30 /**
31 * A statistic based on evaluating simple expressions.
32 *
33 * The expressions are in RPN, so no parsing issues. whitspace separated. Variables (other statistics),
34 * constants and operations. Currently just the basic four, but easy to extend.
35 *
36 * @author Joseph Heled
37 */
38 public class RPNcalculatorStatistic extends Statistic.Abstract {
39
40 private final RPNexpressionCalculator[] expressions;
41 private final String[] names;
42 private final Map<String, Statistic> variables;
43
44 RPNexpressionCalculator.GetVariable vars = new RPNexpressionCalculator.GetVariable() {
45 public double get(String name) {
46 return variables.get(name).getStatisticValue(0);
47 }
48 };
49
50 public RPNcalculatorStatistic(String name, String[] expressions, String[] names,
51 Map<String, Statistic> variables) {
52 super(name);
53
54 this.expressions = new RPNexpressionCalculator[expressions.length];
55 for(int i = 0; i < expressions.length; ++ i) {
56 this.expressions[i] = new RPNexpressionCalculator(expressions[i]);
57
58 String err = this.expressions[i].validate();
59 if( err != null ) {
60 throw new RuntimeException("Error in expression " + i + ": " + err);
61 }
62 }
63
64 this.names = names;
65 this.variables = variables;
66 }
67
68 public int getDimension() {
69 return expressions.length;
70 }
71
72 public String getDimensionName(int dim) {
73 return names[dim];
74 }
75
76 /** @return the value of the expression */
77 public double getStatisticValue(int dim) {
78 return expressions[dim].evaluate(vars);
79 }
80
81 }