Debian Codegraph @

src/dr/inference/model/AbstractAlgebraStatistic.java

raw symbols SCIP
1 /*
2 * ProductStatistic.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.ArrayList;
29 import java.util.List;
30
31 /**
32 * @author Andrew Rambaut
33 */
34 public abstract class AbstractAlgebraStatistic extends Statistic.Abstract {
35
36 private int firstDataDimension = 0;
37
38 private int dimension = 0;
39 private boolean elementwise;
40 private final double[] constants;
41
42 public AbstractAlgebraStatistic(String name, boolean elementwise, double[] constants) {
43 super(name);
44 this.elementwise = elementwise;
45 this.constants = constants;
46 if (constants != null) {
47 firstDataDimension = constants.length;
48 }
49 }
50
51 public void addStatistic(Statistic statistic) {
52 if (dimension > 0) {
53 throw new IllegalArgumentException("Cannot add more data after the statistic has been evaluated.");
54 }
55
56 if (!elementwise) {
57 if (firstDataDimension == 0) {
58 firstDataDimension = statistic.getDimension();
59 } else if (firstDataDimension != statistic.getDimension()) {
60 throw new IllegalArgumentException("Data added doesn't match previous dimensions");
61 }
62 }
63
64 statistics.add(statistic);
65 }
66
67 public int getDimension() {
68 if (dimension == 0) {
69 if ((constants != null || statistics.size() > 1) && !elementwise) {
70 dimension = statistics.get(0).getDimension();
71 } else {
72 elementwise = true;
73 dimension = 1;
74 }
75 }
76
77 return dimension;
78 }
79
80 /**
81 * @return product of contained statistics
82 */
83 public double getStatisticValue(int dim) {
84
85 // Check we have set the dimension
86 getDimension();
87
88 double value = Double.NaN;
89
90 for (Statistic statistic : statistics) {
91 if (elementwise) {
92 // do the operation across all the elements
93
94 if (Double.isNaN(value)) {
95 value = statistic.getStatisticValue(0);
96 }
97 for (int j = 1; j < statistic.getDimension(); j++) {
98 value = doOperation(value, statistic.getStatisticValue(j));
99 }
100 } else {
101 if (Double.isNaN(value)) {
102 value = statistic.getStatisticValue(dim);
103 } else {
104 value = doOperation(value, statistic.getStatisticValue(dim));
105 }
106 }
107 }
108 if (constants != null && constants.length > 1) {
109 if (constants.length == 1) {
110 value = doOperation(value, constants[0]);
111 } else {
112 value = doOperation(value, constants[dim]);
113 }
114 }
115
116 return value;
117 }
118
119 protected abstract double doOperation(double a, double b);
120
121 // ****************************************************************
122 // Private and protected stuff
123 // ****************************************************************
124
125 private final List<Statistic> statistics = new ArrayList<Statistic>();
126 }