Debian Codegraph @

src/dr/inference/model/DiagonalMatrix.java

raw symbols SCIP
1 /*
2 * DiagonalMatrix.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 /**
29 * @author Marc Suchard
30 */
31 public class DiagonalMatrix extends MatrixParameter
32 implements MatrixParameterInterface, VariableListener {
33
34 private Parameter diagonalParameter;
35
36 public DiagonalMatrix(Parameter param) {
37 super(MATRIX_PARAMETER);
38 addParameter(param);
39 diagonalParameter = param;
40 }
41
42 // public static DiagonalMatrix buildIdentityTimesElementMatrix(int dim, double value) {
43 // Parameter param = new Parameter.Default(dim, value);
44 // param.addBounds(new DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, dim));
45 // return new DiagonalMatrix(param);
46 // }
47
48 public void variableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
49 if (variable == diagonalParameter) {
50 fireParameterChangedEvent(-1, ChangeType.ALL_VALUES_CHANGED);
51 } else {
52 throw new IllegalArgumentException("Unknown variable");
53 }
54
55 }
56
57 public String getDimensionName(int dim) {
58 int row = dim / diagonalParameter.getDimension();
59 int col = dim % diagonalParameter.getDimension();
60 return getId() + "." + row + "." + col;
61 }
62
63 public Parameter getDiagonalParameter() { return diagonalParameter; }
64
65 public double getParameterValue(int row, int col) {
66 if (row != col)
67 return 0.0;
68 return diagonalParameter.getParameterValue(row);
69 }
70
71 public double[][] getParameterAsMatrix() {
72 final int I = getRowDimension();
73 double[][] parameterAsMatrix = new double[I][I];
74 for (int i = 0; i < I; i++) {
75 parameterAsMatrix[i][i] = diagonalParameter.getParameterValue(i);
76 }
77 return parameterAsMatrix;
78 }
79
80 public void inverse() {
81 for (int i = 0; i < diagonalParameter.getDimension(); i++) {
82 {
83 if (diagonalParameter.getValue(i) == 0) {
84 throw new RuntimeException("Diagonal matrix is not of full rank");
85 } else {
86 setParameterValue(i, 1 / diagonalParameter.getParameterValue(i));
87 }
88 }
89 }
90 }
91
92 public int getDimension() {
93 return diagonalParameter.getDimension() * diagonalParameter.getDimension();
94 }
95
96 public double getParameterValue(int i) {
97 return getParameterValue(i / diagonalParameter.getDimension(), i % diagonalParameter.getDimension());
98 }
99
100 // public MatrixParameter preMultiply(MatrixParameter right) {
101 // if (right.getRowDimension() != this.getColumnDimension()) {
102 // throw new RuntimeException("Incompatible Dimensions: " + right.getRowDimension() + " does not equal " + this.getColumnDimension() + ".\n");
103 // }
104 // MatrixParameter answer = new MatrixParameter(null);
105 // answer.setDimensions(right.getRowDimension(), right.getColumnDimension());
106 // for (int i = 0; i < right.getRowDimension(); i++) {
107 // for (int j = 0; j < right.getColumnDimension(); j++) {
108 // answer.setParameterValueQuietly(i, j, right.getParameterValue(i, j) * getParameterValue(i));
109 // }
110 //
111 // }
112 // return answer;
113 // }
114 //
115 // public MatrixParameter preMultiplyInPlace(MatrixParameter right, MatrixParameter answer) {
116 // if (right.getRowDimension() != this.getColumnDimension()) {
117 // throw new RuntimeException("Incompatible Dimensions: " + right.getRowDimension() + " does not equal " + this.getColumnDimension() + ".\n");
118 // }
119 //// MatrixParameter answer=new MatrixParameter(null);
120 //// answer.setDimensions(right.getRowDimension(), right.getColumnDimension());
121 // for (int i = 0; i < right.getRowDimension(); i++) {
122 // for (int j = 0; j < right.getColumnDimension(); j++) {
123 // answer.setParameterValueQuietly(i, j, right.getParameterValue(i, j) * getParameterValue(i));
124 // }
125 //
126 // }
127 // return answer;
128 // }
129 //
130 //
131 // public MatrixParameter postMultiply(MatrixParameter left) {
132 // if (left.getColumnDimension() != this.getRowDimension()) {
133 // throw new RuntimeException("Incompatible Dimensions: " + this.getColumnDimension() + " does not equal " + left.getRowDimension() + ".\n");
134 // }
135 // MatrixParameter answer = new MatrixParameter(null);
136 // answer.setDimensions(left.getRowDimension(), left.getColumnDimension());
137 // for (int i = 0; i < left.getRowDimension(); i++) {
138 // for (int j = 0; j < left.getColumnDimension(); j++) {
139 // answer.setParameterValueQuietly(i, j, left.getParameterValue(i, j) * getParameterValue(j));
140 // }
141 //
142 // }
143 // return answer;
144 // }
145 //
146 // public MatrixParameter postMultiplyInPlace(MatrixParameter left, MatrixParameter answer) {
147 // if (left.getColumnDimension() != this.getRowDimension()) {
148 // throw new RuntimeException("Incompatible Dimensions: " + this.getColumnDimension() + " does not equal " + left.getRowDimension() + ".\n");
149 // }
150 //// MatrixParameter answer=new MatrixParameter(null);
151 //// answer.setDimensions(left.getRowDimension(), left.getColumnDimension());
152 // for (int i = 0; i < left.getRowDimension(); i++) {
153 // for (int j = 0; j < left.getColumnDimension(); j++) {
154 // answer.setParameterValueQuietly(i, j, left.getParameterValue(i, j) * getParameterValue(j));
155 // }
156 //
157 // }
158 // return answer;
159 // }
160
161 public MatrixParameter add(MatrixParameter Right) {
162
163 MatrixParameter answer = new MatrixParameter(null);
164 answer.setDimensions(this.getRowDimension(), this.getColumnDimension());
165 return addInPlace(Right, answer);
166 }
167
168 public MatrixParameter addInPlace(MatrixParameter Right, MatrixParameter answer) {
169 if (this.getColumnDimension() != Right.getColumnDimension() || this.getRowDimension() != Right.getRowDimension()) {
170 throw new RuntimeException("You cannot add a " + getRowDimension() + " by " + getColumnDimension() + " matrix to a " + Right.getRowDimension() + " by " + Right.getColumnDimension() + " matrix.");
171 }
172
173 for (int i = 0; i < this.getRowDimension(); i++) {
174 for (int j = 0; j < this.getColumnDimension(); j++) {
175 if (i == j) {
176 answer.setParameterValueQuietly(i, j, this.getParameterValue(i, j) + Right.getParameterValue(i, j));
177 } else {
178 answer.setParameterValueQuietly(i, j, Right.getParameterValue(i, j));
179 }
180 }
181 }
182 return answer;
183 }
184
185 public void product(double a) {
186 for (int i = 0; i < this.getRowDimension(); i++) {
187 this.setParameterValueQuietly(i, i, a * this.getParameterValue(i, i));
188
189 }
190 this.fireParameterChangedEvent();
191 }
192
193 public MatrixParameter productInPlace(double a, MatrixParameter answer) {
194 for (int i = 0; i < this.getRowDimension(); i++) {
195 answer.setParameterValueQuietly(i, i, a * this.getParameterValue(i, i));
196
197 }
198 return answer;
199 }
200
201 public double getDeterminant() {
202 double product = 1;
203 for (int i = 0; i < diagonalParameter.getDimension(); i++) {
204 product *= diagonalParameter.getParameterValue(i);
205 }
206 return product;
207 }
208
209 public void setParameterValue(int i, double j) {
210 diagonalParameter.setParameterValue(i, j);
211 }
212
213 public void setParameterValueQuietly(int i, double j) {
214 diagonalParameter.setParameterValueQuietly(i, j);
215 }
216
217 public int getColumnDimension() {
218 return diagonalParameter.getDimension();
219 }
220
221 public int getRowDimension() {
222 return diagonalParameter.getDimension();
223 }
224
225 }