Debian Codegraph @

src/dr/inference/model/MatrixParameter.java

raw symbols SCIP
1 /*
2 * MatrixParameter.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.StringTokenizer;
31
32 /**
33 * @author Marc Suchard
34 * @author Max Tolkoff
35 */
36 public class MatrixParameter extends CompoundParameter implements MatrixParameterInterface {
37
38 public final static String MATRIX_PARAMETER = "matrixParameter";
39
40 public MatrixParameter(String name) {
41 super(name);
42 }
43
44 public MatrixParameter(String name, Parameter[] parameters) {
45 super(name, parameters);
46 rowDimension=parameters[0].getDimension();
47 columnDimension=parameters.length;
48 dimensionsEstablished = true;
49 }
50
51 public MatrixParameter(String name, int row, int column){
52 super(name);
53 setDimensions(row, column);
54 }
55
56 public MatrixParameter(String name, int row, int column, double a){
57 super(name);
58 setDimensions(row, column, a);
59 }
60
61 public void setParameterValue(int row, int column, double a) {
62 getParameter(column).setParameterValue(row, a);
63 }
64
65 public void setParameterValueQuietly(int row, int column, double a){
66 getParameter(column).setParameterValueQuietly(row, a);
67 }
68
69 public void setParameterValueNotifyChangedAll(int row, int column, double val){
70 getParameter(column).setParameterValueNotifyChangedAll(row, val);
71 }
72
73 public static MatrixParameter recast(String name, CompoundParameter compoundParameter) {
74 final int count = compoundParameter.getParameterCount();
75 Parameter[] parameters = new Parameter[count];
76 for (int i = 0; i < count; ++i) {
77 parameters[i] = compoundParameter.getParameter(i);
78 }
79 return new MatrixParameter(name, parameters);
80 }
81
82 public double getParameterValue(int row, int col) {
83 return getParameter(col).getParameterValue(row);
84 }
85
86 public double[] getRowValues(int row){
87 int colDim=getColumnDimension();
88 double[] rowValues=new double[colDim];
89 for (int i = 0; i <colDim ; i++) {
90 rowValues[i]=getParameterValue(row, i);
91 }
92 return rowValues;
93 }
94
95 public double[] getColumnValues(int col) {
96 return this.getParameter(col).getParameterValues();
97 }
98
99 public double[][] getParameterAsMatrix() {
100 final int I = getRowDimension();
101 final int J = getColumnDimension();
102 double[][] parameterAsMatrix = new double[I][J];
103 for (int i = 0; i < I; i++) {
104 for (int j = 0; j < J; j++)
105 parameterAsMatrix[i][j] = getParameterValue(i, j);
106 }
107 return parameterAsMatrix;
108 }
109
110 public void setColumnDimension(int columnDimension) {
111 if (dimensionsEstablished) {
112 throw new IllegalArgumentException("Attempt to change dimensions after initialization");
113 }
114 this.columnDimension = columnDimension;
115 setupParameters();
116 }
117
118 public void setRowDimension(int rowDimension) {
119 if (dimensionsEstablished) {
120 throw new IllegalArgumentException("Attempt to change dimensions after initialization");
121 }
122 this.rowDimension = rowDimension;
123 setupParameters();
124 }
125
126 private void setupParameters() {
127 if (columnDimension > 0 && rowDimension > 0) {
128 dimensionsEstablished = true;
129
130 for (int i = 0; i < rowDimension; i++) {
131 Parameter row = new Parameter.Default(columnDimension, 0.0);
132 row.addBounds(new DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, columnDimension));
133 addParameter(row);
134 }
135 }
136 }
137
138
139 //TODO rewrite so that it doesn't destroy existing parameters
140 public void setDimensions(int rowDim, int colDim){
141 setDimensions(rowDim, colDim, 0.0);
142 }
143
144 public void setDimensions(int rowDim, int colDim, double a){
145 rowDimension=rowDim;
146 columnDimension=colDim;
147 for (int i = 0; i < colDim; i++) {
148 Parameter column = new Parameter.Default(rowDim, a);
149 column.addBounds(new DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, rowDim));
150 addParameter(column);
151 }
152 }
153
154 public int getColumnDimension() {
155 return getParameterCount();
156 }
157
158 public int getRowDimension() {
159 return getParameter(0).getDimension();
160 }
161
162 @Override
163 public int getUniqueParameterCount() {
164 return getParameterCount();
165 }
166
167 @Override
168 public Parameter getUniqueParameter(int index) {
169 return super.getParameter(index);
170 }
171
172 @Override
173 public void copyParameterValues(double[] destination, int offset) {
174 final int length = getDimension();
175 for (int i = 0; i < length; ++i) {
176 destination[offset + i] = getParameterValue(i);
177 }
178 }
179
180 @Override
181 public void setAllParameterValuesQuietly(double[] values, int offset) {
182 for (int i = 0; i < getDimension(); ++i) {
183 setParameterValueQuietly(i, values[offset + i]);
184 }
185 }
186
187 public static String toSymmetricString(MatrixParameterInterface mat) {
188 StringBuilder sb = new StringBuilder("{");
189 int dim = mat.getRowDimension();
190 int total = dim * (dim + 1) / 2;
191 for (int i = 0; i < dim; i++) {
192 for (int j = i; j < dim; j++) {
193 sb.append(String.format("%5.4e", mat.getParameterValue(i, j)));
194 total--;
195 if (total > 0)
196 sb.append(",");
197 }
198 }
199 sb.append("}");
200 return sb.toString();
201 }
202
203 public String toSymmetricString() {
204 return toSymmetricString(this);
205 }
206
207 public static MatrixParameter parseFromSymmetricString(String string) {
208 String clip = string.replace("{", "").replace("}", "").trim();
209 StringTokenizer st = new StringTokenizer(clip, ",");
210 int count = st.countTokens();
211 int dim = (-1 + (int) Math.sqrt(1 + 8 * count)) / 2;
212 Parameter[] parameter = new Parameter[dim];
213 for (int i = 0; i < dim; i++)
214 parameter[i] = new Parameter.Default(dim);
215 for (int i = 0; i < dim; i++) {
216 for (int j = i; j < dim; j++) {
217 double datum = new Double(st.nextToken());
218 parameter[i].setParameterValue(j, datum);
219 parameter[j].setParameterValue(i, datum);
220 }
221 }
222 return new MatrixParameter(null, parameter);
223 }
224
225 public static MatrixParameter parseFromSymmetricDoubleArray(Object[] data) {
226
227 int dim = (-1 + (int) Math.sqrt(1 + 8 * data.length)) / 2;
228 Parameter[] parameter = new Parameter[dim];
229 for (int i = 0; i < dim; i++)
230 parameter[i] = new Parameter.Default(dim);
231 int index = 0;
232 for (int i = 0; i < dim; i++) {
233 for (int j = i; j < dim; j++) {
234 double datum = (Double) data[index++];
235 parameter[i].setParameterValue(j, datum);
236 parameter[j].setParameterValue(i, datum);
237 }
238 }
239 return new MatrixParameter(null, parameter);
240 }
241
242 public void rowMultiply(double a, int row){
243 rowMultiplyQuietly(a, row);
244 fireParameterChangedEvent();
245 }
246
247 public void columnMultiply(double a, int col){
248 columnMultiplyQuietly(a,col);
249 fireParameterChangedEvent();
250 }
251
252
253
254
255
256 public TransposedMatrixParameter transpose(){
257 return TransposedMatrixParameter.recast(null, this);
258 }
259
260 // **************************************************************
261 // Matrix Operations
262 // **************************************************************
263
264 public MatrixParameter add(MatrixParameter Right) {
265 if (Right.getRowDimension() != getRowDimension() || getColumnDimension() != Right.getColumnDimension()){
266 throw new RuntimeException("You cannot add a " + getRowDimension() +" by " + getColumnDimension() + " matrix to a " + Right.getRowDimension() + " by " + Right.getColumnDimension() + " matrix.");
267 }
268 MatrixParameter answer=new MatrixParameter(null);
269 answer.setDimensions(getRowDimension(), getColumnDimension());
270 for (int i = 0; i <getRowDimension() ; i++) {
271 for (int j = 0; j <getColumnDimension() ; j++) {
272 answer.setParameterValueQuietly(i, j, getParameterValue(i, j) + Right.getParameterValue(i, j));
273 }
274
275 }
276 return answer;
277 }
278
279 public MatrixParameter addInPlace(MatrixParameter Right, MatrixParameter answer) {
280 if (Right.getRowDimension() != getRowDimension() || getColumnDimension() != Right.getColumnDimension()){
281 throw new RuntimeException("You cannot add a " + getRowDimension() +" by " + getColumnDimension() + " matrix to a " + Right.getRowDimension() + " by " + Right.getColumnDimension() + " matrix.");
282 }
283 // MatrixParameter answer=new MatrixParameter(null);
284 // answer.setDimensions(getRowDimension(), getColumnDimension());
285 for (int i = 0; i <getRowDimension() ; i++) {
286 for (int j = 0; j <getColumnDimension() ; j++) {
287 answer.setParameterValueQuietly(i, j, getParameterValue(i, j) + Right.getParameterValue(i, j));
288 }
289
290 }
291 return answer;
292 }
293
294 public MatrixParameter subtract(MatrixParameter Right) {
295 if (Right.getRowDimension() != getRowDimension() || getColumnDimension() != Right.getColumnDimension()){
296 throw new RuntimeException("You cannot subtract a " + getRowDimension() +" by " + getColumnDimension() + " matrix to a " + Right.getRowDimension() + " by " + Right.getColumnDimension() + " matrix.");
297 }
298 MatrixParameter answer=new MatrixParameter(null);
299 answer.setDimensions(getRowDimension(), getColumnDimension());
300 for (int i = 0; i <getRowDimension() ; i++) {
301 for (int j = 0; j <getColumnDimension() ; j++) {
302 answer.setParameterValueQuietly(i, j, getParameterValue(i, j) - Right.getParameterValue(i, j));
303 }
304
305 }
306 return answer;
307 }
308
309 public MatrixParameter subtractInPlace(MatrixParameter Right, MatrixParameter answer) {
310 if (Right.getRowDimension() != getRowDimension() || getColumnDimension() != Right.getColumnDimension()){
311 throw new RuntimeException("You cannot subtract a " + getRowDimension() +" by " + getColumnDimension() + " matrix from a " + Right.getRowDimension() + " by " + Right.getColumnDimension() + " matrix.");
312 }
313 // MatrixParameter answer=new MatrixParameter(null);
314 // answer.setDimensions(getRowDimension(), getColumnDimension());
315 for (int i = 0; i <getRowDimension() ; i++) {
316 for (int j = 0; j <getColumnDimension() ; j++) {
317 answer.setParameterValueQuietly(i,j, getParameterValue(i,j)-Right.getParameterValue(i,j));
318 }
319
320 }
321 return answer;
322 }
323
324 public MatrixParameter transposeThenProduct(MatrixParameter Right){
325 if(this.getRowDimension()!=Right.getRowDimension()){
326 throw new RuntimeException("Incompatible Dimensions: " + Right.getRowDimension() + " does not equal " + this.getRowDimension() +".\n");
327 }
328 MatrixParameter answer=new MatrixParameter(null);
329 answer.setDimensions(this.getColumnDimension(), Right.getColumnDimension());
330
331 int p = this.getRowDimension();
332 int n = this.getColumnDimension();
333 int m = Right.getColumnDimension();
334 for (int i = 0; i < n; i++) {
335 for (int j = 0; j < m; j++) {
336 double sum = 0;
337 for (int k = 0; k < p; k++)
338 sum += getParameterValue(k,i) * Right.getParameterValue(k,j);
339 answer.setParameterValueQuietly(i, j, sum);
340 }
341 }
342
343 return answer;
344 }
345
346 public MatrixParameter transposeThenProductInPlace(MatrixParameter Right, MatrixParameter answer){
347 if(this.getRowDimension()!=Right.getRowDimension()){
348 throw new RuntimeException("Incompatible Dimensions: " + Right.getRowDimension() + " does not equal " + this.getRowDimension() +".\n");
349 }
350 // MatrixParameter answer=new MatrixParameter(null);
351 // answer.setDimensions(this.getColumnDimension(), Right.getColumnDimension());
352
353 int p = this.getRowDimension();
354 int n = this.getColumnDimension();
355 int m = Right.getColumnDimension();
356 for (int i = 0; i < n; i++) {
357 for (int j = 0; j < m; j++) {
358 double sum = 0;
359 for (int k = 0; k < p; k++)
360 sum += getParameterValue(k,i) * Right.getParameterValue(k,j);
361 answer.setParameterValueQuietly(i,j, sum);
362 }
363 }
364
365 return answer;
366 }
367
368
369 public MatrixParameter product(MatrixParameter Right){
370 if(this.getColumnDimension()!=Right.getRowDimension()){
371 throw new RuntimeException("Incompatible Dimensions: " + Right.getRowDimension() + " does not equal " + this.getColumnDimension() +".\n");
372 }
373 MatrixParameter answer=new MatrixParameter(null);
374 answer.setDimensions(this.getRowDimension(), Right.getColumnDimension());
375
376 int p = this.getColumnDimension();
377 int n = this.getRowDimension();
378 int m = Right.getColumnDimension();
379 for (int i = 0; i < n; i++) {
380 for (int j = 0; j < m; j++) {
381 double sum = 0;
382 for (int k = 0; k < p; k++)
383 sum += getParameterValue(i,k) * Right.getParameterValue(k,j);
384 answer.setParameterValueQuietly(i, j, sum);
385 }
386 }
387
388 return answer;
389 }
390
391 public MatrixParameter productInPlace(MatrixParameter Right, MatrixParameter answer){
392 if(this.getColumnDimension()!=Right.getRowDimension()){
393 throw new RuntimeException("Incompatible Dimensions: " + Right.getRowDimension() + " does not equal " + this.getColumnDimension() +".\n");
394 }
395 // MatrixParameter answer=new MatrixParameter(null);
396 // answer.setDimensions(this.getRowDimension(), Right.getColumnDimension());
397
398 int p = this.getColumnDimension();
399 int n = this.getRowDimension();
400 int m = Right.getColumnDimension();
401 for (int i = 0; i < n; i++) {
402 for (int j = 0; j < m; j++) {
403 double sum = 0;
404 for (int k = 0; k < p; k++)
405 sum += getParameterValue(i,k) * Right.getParameterValue(k,j);
406 answer.setParameterValueQuietly(i,j, sum);
407 }
408 }
409
410 return answer;
411 }
412
413 public MatrixParameter productWithTransposed(MatrixParameter Right){
414 if(this.getColumnDimension()!=Right.getColumnDimension()){
415 throw new RuntimeException("Incompatible Dimensions: " + Right.getColumnDimension() + " does not equal " + this.getColumnDimension() +".\n");
416 }
417 MatrixParameter answer=new MatrixParameter(null);
418 answer.setDimensions(this.getRowDimension(), Right.getRowDimension());
419
420 int p = this.getColumnDimension();
421 int n = this.getRowDimension();
422 int m = Right.getRowDimension();
423 for (int i = 0; i < n; i++) {
424 for (int j = 0; j < m; j++) {
425 double sum = 0;
426 for (int k = 0; k < p; k++)
427 sum += getParameterValue(i,k) * Right.getParameterValue(j,k);
428 answer.setParameterValueQuietly(i,j, sum);
429 }
430 }
431
432 return answer;
433 }
434
435 public MatrixParameter productWithTransposedInPlace(MatrixParameter Right, MatrixParameter answer){
436 if(this.getColumnDimension()!=Right.getColumnDimension()){
437 throw new RuntimeException("Incompatible Dimensions: " + Right.getColumnDimension() + " does not equal " + this.getColumnDimension() +".\n");
438 }
439 // MatrixParameter answer=new MatrixParameter(null);
440 // answer.setDimensions(this.getRowDimension(), Right.getRowDimension());
441
442 int p = this.getColumnDimension();
443 int n = this.getRowDimension();
444 int m = Right.getRowDimension();
445 for (int i = 0; i < n; i++) {
446 for (int j = 0; j < m; j++) {
447 double sum = 0;
448 for (int k = 0; k < p; k++)
449 sum += getParameterValue(i,k) * Right.getParameterValue(j,k);
450 answer.setParameterValueQuietly(i,j, sum);
451 }
452 }
453
454 return answer;
455 }
456
457 public void product(double a){
458 for (int i = 0; i <this.getRowDimension() ; i++) {
459 for (int j = 0; j < this.getColumnDimension(); j++) {
460 this.setParameterValueQuietly(i, j, a * this.getParameterValue(i, j));
461 }
462 }
463 this.fireParameterChangedEvent();
464 }
465
466 public MatrixParameter productInPlace(double a, MatrixParameter answer){
467 // MatrixParameter answer=new MatrixParameter(null);
468 // answer.setDimensions(this.getRowDimension(), this.getColumnDimension());
469 for (int i = 0; i <this.getRowDimension() ; i++) {
470 for (int j = 0; j <this.getColumnDimension() ; j++) {
471 answer.setParameterValueQuietly(i,j, a*this.getParameterValue(i,j));
472 }
473
474 }
475 return answer;
476 }
477
478 public void rowMultiplyQuietly(double a, int row){
479 for (int i = 0; i <getColumnDimension() ; i++) {
480 if(getParameter(i).getDimension()<row){
481 getParameter(i).setParameterValueQuietly(row, a*getParameterValue(row, i));
482 }
483 }
484 }
485
486 public void columnMultiplyQuietly(double a, int col){
487 Parameter i=getParameter(col);
488 for (int j = 0; j < i.getDimension() ; j++) {
489 i.setParameterValueQuietly(j, a*i.getParameterValue(j));
490 }
491 }
492
493
494 private boolean dimensionsEstablished = false;
495 private int columnDimension = 0;
496 private int rowDimension = 0;
497
498 // **************************************************************
499 // XMLElement IMPLEMENTATION
500 // **************************************************************
501
502 // public Element createElement(Document d) {
503 // throw new RuntimeException("Not implemented yet!");
504 // }
505
506 public static final String ROW_DIMENSION = "rows";
507 public static final String COLUMN_DIMENSION = "columns";
508 public static final String TRANSPOSE = "transpose";
509 public static final String AS_COMPOUND = "asCompoundParameter";
510 public static final String BEHAVIOR = "test";
511
512 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
513
514 public String getParserName() {
515 return MATRIX_PARAMETER;
516 }
517
518 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
519
520 final String name = xo.hasId() ? xo.getId() : null;
521 boolean transposed = xo.getAttribute(TRANSPOSE, false);
522 boolean compound = xo.getAttribute(AS_COMPOUND, false);
523
524 MatrixParameter matrixParameter;
525
526 if (compound) {
527 CompoundParameter parameter = (CompoundParameter) xo.getChild(0);
528 if (transposed) {
529 matrixParameter = TransposedMatrixParameter.recast(name, parameter);
530 } else {
531 matrixParameter = MatrixParameter.recast(name, parameter);
532 }
533 return matrixParameter;
534 }
535
536 if (!transposed) {
537 matrixParameter = new MatrixParameter(name);
538 } else {
539 matrixParameter = new TransposedMatrixParameter(name);
540 }
541
542 if (xo.getAttribute(BEHAVIOR, false) && xo.hasAttribute(ROW_DIMENSION) && xo.hasAttribute(COLUMN_DIMENSION)) {
543 int rowDim = xo.getIntegerAttribute(ROW_DIMENSION);
544 int colDim = xo.getIntegerAttribute(COLUMN_DIMENSION);
545 matrixParameter.setDimensions(rowDim, colDim);
546 } else {
547 if (xo.hasAttribute(ROW_DIMENSION)) {
548 int rowDimension = xo.getIntegerAttribute(ROW_DIMENSION);
549 matrixParameter.setRowDimension(rowDimension);
550 }
551
552 if (xo.hasAttribute(COLUMN_DIMENSION)) {
553 int columnDimension = xo.getIntegerAttribute(COLUMN_DIMENSION);
554 matrixParameter.setColumnDimension(columnDimension);
555 }
556 }
557
558 int dim = 0;
559 for (int i = 0; i < xo.getChildCount(); i++) {
560 Parameter parameter = (Parameter) xo.getChild(i);
561 matrixParameter.addParameter(parameter);
562 if (i == 0)
563 dim = parameter.getDimension();
564 else if (dim != parameter.getDimension())
565 throw new XMLParseException("All parameters must have the same dimension to construct a rectangular matrix");
566 }
567
568 return matrixParameter;
569 }
570
571 //************************************************************************
572 // AbstractXMLObjectParser implementation
573 //************************************************************************
574
575 public String getParserDescription() {
576 return "A matrix parameter constructed from its component parameters.";
577 }
578
579 public XMLSyntaxRule[] getSyntaxRules() {
580 return rules;
581 }
582
583 private final XMLSyntaxRule[] rules = {
584 new ElementRule(Parameter.class, 0, Integer.MAX_VALUE),
585 AttributeRule.newIntegerRule(ROW_DIMENSION, true),
586 AttributeRule.newIntegerRule(COLUMN_DIMENSION, true),
587 AttributeRule.newBooleanRule(TRANSPOSE, true),
588 AttributeRule.newBooleanRule(AS_COMPOUND, true),
589 AttributeRule.newBooleanRule(BEHAVIOR, true),
590 };
591
592 public Class getReturnType() {
593 return MatrixParameter.class;
594 }
595 };
596 }