Debian Codegraph @

src/dr/inference/model/FastMatrixParameter.java

raw symbols SCIP
1 /*
2 * FastMatrixParameter.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 dr.xml.*;
29
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
33
34 /**
35 * @author Marc A. Suchard
36 */
37 public class FastMatrixParameter extends CompoundParameter implements MatrixParameterInterface {
38
39 public static final String FAST_MATRIX_PARAMETER = "fastMatrixParameter";
40 public static final String ROW_DIMENSION = MatrixParameter.ROW_DIMENSION;
41 public static final String COLUMN_DIMENSION = MatrixParameter.COLUMN_DIMENSION;
42 public static final String STARTING_VALUE = "startingValue";
43
44 public FastMatrixParameter(String id, int rowDimension, int colDimension, double startingValue) {
45 super(id);
46 singleParameter = new Parameter.Default(rowDimension * colDimension);
47 addParameter(singleParameter);
48 for (int i = 0; i < singleParameter.getDimension(); i++) {
49 singleParameter.setParameterValue(i, startingValue);
50 }
51 this.rowDimension = rowDimension;
52 this.colDimension = colDimension;
53 DefaultBounds bounds = new DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, singleParameter.getDimension());
54 addBounds(bounds);
55 }
56
57 public Parameter getParameter(int index) {
58 if (proxyList == null) {
59 proxyList = new ArrayList<ParameterProxy>(colDimension);
60 for (int i = 0; i < colDimension; ++i) {
61 proxyList.add(new ParameterProxy(this, i));
62 }
63 }
64 return proxyList.get(index);
65 }
66
67 class ParameterProxy extends Parameter.Abstract {
68
69 private final int column;
70 private final FastMatrixParameter matrix;
71
72 ParameterProxy(FastMatrixParameter matrix, int column) {
73 this.matrix = matrix;
74 // this.addParameterListener(this.matrix);
75 this.column = column;
76 }
77
78 @Override
79 protected void storeValues() {
80 // Do nothing; storeValues() on whole matrix should have been called.
81 }
82
83 @Override
84 protected void restoreValues() {
85 // Do nothing; restoreValues() on whole matrix should have been called.
86 }
87
88 @Override
89 protected void acceptValues() {
90 // Do nothing; acceptValues() on whole matrix should have been called.
91 }
92
93 @Override
94 protected void adoptValues(Parameter source) {
95 throw new RuntimeException("Do not call");
96 }
97
98 @Override
99 public double getParameterValue(int dim) {
100 return matrix.getParameterValue(dim, column);
101 }
102
103 @Override
104 public void setParameterValue(int dim, double value) {
105 matrix.setParameterValue(dim, column, value);
106 super.fireParameterChangedEvent(dim, Parameter.ChangeType.VALUE_CHANGED);
107 }
108
109 @Override
110 public void setParameterValueQuietly(int dim, double value) {
111 matrix.setParameterValueQuietly(dim, column, value);
112 }
113
114 @Override
115 public void setParameterValueNotifyChangedAll(int dim, double value) {
116 throw new RuntimeException("Do not call");
117 }
118
119 @Override
120 public String getParameterName() {
121 return getId();
122 }
123
124 @Override
125 public void addBounds(Bounds<Double> boundary) {
126 bounds = boundary;
127 }
128
129 @Override
130 public Bounds<Double> getBounds() {
131 return bounds;
132 }
133
134 @Override
135 public void fireParameterChangedEvent(int index, ChangeType type){
136 matrix.fireParameterChangedEvent(index + column * getDimension(), type);
137 super.fireParameterChangedEvent(index, ChangeType.VALUE_CHANGED);
138 }
139
140 @Override
141 public void addDimension(int index, double value) {
142 throw new RuntimeException("Do not call");
143 }
144
145 @Override
146 public double removeDimension(int index) {
147 throw new RuntimeException("Do not call");
148 }
149
150 @Override
151 public int getDimension() {
152 return matrix.getRowDimension();
153 }
154
155 private Bounds<Double> bounds = null;
156
157 }
158
159 private final int index(int row, int col) {
160 // column-major
161 if(col > getColumnDimension()){
162 throw new RuntimeException("Column " + col + " out of bounds: Compared to " + getColumnDimension() + "maximum size.");
163 }
164 if(row > getRowDimension()){
165 throw new RuntimeException("Row " + row + " out of bounds: Compared to " + getRowDimension() + "maximum size.");
166 }
167 return col * rowDimension + row;
168 }
169
170 @Override
171 public double getParameterValue(int row, int col) {
172 return singleParameter.getParameterValue(index(row, col));
173 }
174
175 @Override
176 public double[] getParameterValues() {
177 double[] destination = new double[getDimension()];
178 copyParameterValues(destination, 0);
179 return destination;
180 }
181
182 @Override
183 public void copyParameterValues(double[] destination, int offset) {
184 final double[] source = ((Parameter.Default) singleParameter).inspectParameterValues();
185 System.arraycopy(source, 0, destination, offset, source.length);
186 }
187
188 @Override
189 public void setAllParameterValuesQuietly(double[] values, int offset) {
190 final double[] destination = ((Parameter.Default) singleParameter).inspectParameterValues();
191 System.arraycopy(values, offset, destination, 0, destination.length);
192 }
193
194 @Override
195 public String toSymmetricString() {
196 return MatrixParameter.toSymmetricString(this);
197 }
198
199 @Override
200 public void setParameterValue(int row, int col, double value) {
201 singleParameter.setParameterValue(index(row, col), value);
202 }
203
204 @Override
205 public void setParameterValueQuietly(int row, int col, double value) {
206 singleParameter.setParameterValueQuietly(index(row, col), value);
207 }
208
209 @Override
210 public void setParameterValueNotifyChangedAll(int row, int col, double value) {
211 singleParameter.setParameterValueNotifyChangedAll(index(row, col), value);
212 }
213
214 @Override
215 public double[] getColumnValues(int col) {
216 double[] rtn = new double[getRowDimension()];
217 for (int i = 0; i < getRowDimension(); ++i) {
218 rtn[i] = getParameterValue(i, col);
219 }
220 return rtn;
221 }
222
223 @Override
224 public double[][] getParameterAsMatrix() {
225 double[][] rtn = new double[getRowDimension()][getColumnDimension()];
226 for (int j = 0; j < getColumnDimension(); ++j) {
227 for (int i = 0; i < getRowDimension(); ++i) {
228 rtn[i][j] = getParameterValue(i, j);
229 }
230 }
231 return rtn;
232 }
233
234 @Override
235 public int getColumnDimension() {
236 return colDimension;
237 }
238
239 @Override
240 public int getRowDimension() {
241 return rowDimension;
242 }
243
244 @Override
245 public int getParameterCount() {
246 return getColumnDimension();
247 }
248
249 @Override
250 public int getUniqueParameterCount() {
251 return 1;
252 }
253
254 @Override
255 public Parameter getUniqueParameter(int index) {
256 return super.getParameter(0);
257 }
258
259 public void addBounds(Bounds<Double> boundary){
260 singleParameter.addBounds(boundary);
261 }
262
263 public Bounds<Double> getBounds(){
264 return singleParameter.getBounds();
265 }
266
267
268 private final int rowDimension;
269 private final int colDimension;
270 private final Parameter singleParameter;
271
272 private List<ParameterProxy> proxyList = null;
273
274 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
275
276 public String getParserName() {
277 return FAST_MATRIX_PARAMETER;
278 }
279
280 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
281
282 final String name = xo.hasId() ? xo.getId() : null;
283 final double startingValue = xo.hasAttribute(STARTING_VALUE) ? xo.getDoubleAttribute(STARTING_VALUE) : 1;
284 final int rowDimension = xo.getIntegerAttribute(ROW_DIMENSION);
285 final int colDimension = xo.getIntegerAttribute(COLUMN_DIMENSION);
286
287 FastMatrixParameter matrixParameter = new FastMatrixParameter(name, rowDimension, colDimension, startingValue);
288
289 return matrixParameter;
290 }
291
292 //************************************************************************
293 // AbstractXMLObjectParser implementation
294 //************************************************************************
295
296 public String getParserDescription() {
297 return "A fast matrix parameter constructed from a single parameter.";
298 }
299
300 public XMLSyntaxRule[] getSyntaxRules() {
301 return rules;
302 }
303
304 private final XMLSyntaxRule[] rules = {
305 new ElementRule(Parameter.class, 0, Integer.MAX_VALUE),
306 AttributeRule.newIntegerRule(ROW_DIMENSION, false),
307 AttributeRule.newIntegerRule(COLUMN_DIMENSION, false),
308 AttributeRule.newDoubleRule(STARTING_VALUE, true),
309 };
310
311 public Class getReturnType() {
312 return FastMatrixParameter.class;
313 }
314 };
315 }