Debian Codegraph @

src/dr/inference/model/DecomposedMatrix.java

raw symbols SCIP
1 /*
2 * DecomposedMatrix.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 /**
31 * @author Marc A. Suchard
32 */
33 public class DecomposedMatrix extends MatrixParameter {
34
35 public static final String DIM = "dim";
36 public static final String MATRIX_PARAMETER = "decomposedMatrix";
37
38 public DecomposedMatrix(String name) {
39 super(name);
40 }
41
42 public DecomposedMatrix(String name, int dim, Parameter parameter) {
43 super(name);
44 this.dim = dim;
45 this.decomposition = parameter;
46 matrix = new double[dim][dim];
47 savedMatrix = new double[dim][dim];
48 composeMatrix();
49 addParameter(parameter);
50 }
51
52 public void parameterChangedEvent(Parameter parameter, int index) {
53 compositionKnown = false;
54 // System.err.println("called");
55 fireParameterChangedEvent();
56 }
57
58
59 void composeMatrix() {
60
61 // L_{ij} = index[i]+j
62 // M_{ij} = \sum_k L_{ik} L_{jk}
63
64 double[] values = decomposition.getParameterValues();
65
66 for (int i = 0; i < dim; i++) {
67 for (int j = i; j < dim; j++) {
68 matrix[i][j] = 0.0;
69 for (int k = 0; k <= i; k++)
70 matrix[i][j] += values[index[i] + k] * values[index[j] + k];
71 matrix[j][i] = matrix[i][j];
72 }
73 }
74
75 compositionKnown = true;
76 }
77
78 public int getDimension() {
79 return dim * dim;
80 }
81
82 protected void storeValues() {
83 super.storeValues();
84 for (int i = 0; i < dim; i++) {
85 System.arraycopy(matrix[i], 0, savedMatrix[i], 0, dim);
86 }
87
88 }
89
90 public double getParameterValue(int index) {
91 int x = index / dim;
92 int y = index - x * dim;
93 return matrix[x][y];
94 }
95
96 protected void restoreValues() {
97 super.restoreValues();
98 for (int i = 0; i < dim; i++) {
99 System.arraycopy(savedMatrix[i], 0, matrix[i], 0, dim);
100 }
101 }
102
103 public double getParameterValue(int row, int col) {
104 // System.err.println("row-col");
105 if (!compositionKnown)
106 composeMatrix();
107 return matrix[row][col];
108
109 }
110
111 public double[][] getParameterAsMatrix() {
112 // System.err.println("as-matrix");
113 if (!compositionKnown)
114 composeMatrix();
115
116 return matrix;
117 }
118
119
120 public int getColumnDimension() {
121 return dim;
122 }
123
124 public int getRowDimension() {
125 return dim;
126 }
127
128 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
129
130 public String getParserName() {
131 return MATRIX_PARAMETER;
132 }
133
134 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
135
136 Parameter parameter = (Parameter) xo.getChild(Parameter.class);
137 int dim = xo.getIntegerAttribute(DIM);
138
139 if (dim * (dim + 1) / 2 != parameter.getDimension())
140 throw new XMLParseException("Dim attribute and parameter dimension do not match");
141
142 return new DecomposedMatrix(MATRIX_PARAMETER, dim, parameter);
143 }
144
145 //************************************************************************
146 // AbstractXMLObjectParser implementation
147 //************************************************************************
148
149 public String getParserDescription() {
150 return "A diagonal matrix parameter constructed from its diagonals.";
151 }
152
153 public XMLSyntaxRule[] getSyntaxRules() {
154 return rules;
155 }
156
157 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
158 new ElementRule(Parameter.class),
159 AttributeRule.newIntegerArrayRule(DIM, false)
160 };
161
162 public Class getReturnType() {
163 return DecomposedMatrix.class;
164 }
165 };
166
167 private boolean compositionKnown = false;
168 private Parameter decomposition;
169 private int dim;
170 private double[][] matrix;
171 private double[][] savedMatrix;
172
173 private static int[] index = {0, 1, 3, 6, 10, 15, 21, 28, 36};
174 }