Debian Codegraph @

src/dr/inference/model/DiagonalConstrainedMatrixView.java

raw symbols SCIP
1 /*
2 * CorrelationDiagonalMatrix.java
3 *
4 * Copyright (c) 2002-2017 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 A. Suchard
30 */
31
32 public class DiagonalConstrainedMatrixView extends CompoundParameter implements MatrixParameterInterface {
33
34 private final MatrixParameterInterface matrix;
35 private final Parameter mask;
36 private final double constraintValue;
37 private final double sqrtConstraintValue;
38
39
40 public DiagonalConstrainedMatrixView(String id,
41 MatrixParameterInterface matrix,
42 Parameter mask,
43 double constraintValue) {
44 super(id);
45 this.matrix = matrix;
46 this.mask = mask;
47 this.constraintValue = constraintValue;
48 this.sqrtConstraintValue = Math.sqrt(constraintValue);
49
50 matrix.addVariableListener(this);
51 }
52
53 private boolean constrained(int i) {
54 return mask.getParameterValue(i) == 1.0;
55 }
56
57 @Override
58 public double getParameterValue(int row, int col) {
59
60 if (row == col && constrained(row)) {
61 return constraintValue;
62 }
63
64 double entry = matrix.getParameterValue(row, col);
65
66 if (constrained(row)) {
67 entry /= Math.sqrt(matrix.getParameterValue(row, row));
68 entry *= sqrtConstraintValue;
69 }
70
71 if (constrained(col)) {
72 entry /= Math.sqrt(matrix.getParameterValue(col, col));
73 entry *= sqrtConstraintValue;
74 }
75
76 return entry;
77 }
78
79 @Override
80 public void setParameterValue(int row, int col, double value) {
81 throw new IllegalArgumentException("Not implemented");
82 }
83
84 @Override
85 public void setParameterValueQuietly(int row, int col, double value) {
86 throw new IllegalArgumentException("Not implemented");
87 }
88
89 @Override
90 public void setParameterValueNotifyChangedAll(int row, int col, double value) {
91 throw new IllegalArgumentException("Not implemented");
92 }
93
94 @Override
95 public double[] getColumnValues(int col) {
96 throw new IllegalArgumentException("Not implemented");
97 }
98
99 @Override
100 public double[][] getParameterAsMatrix() {
101 final int rowDim = getRowDimension();
102 final int colDim = getColumnDimension();
103
104 double[][] matrix = new double[rowDim][colDim];
105 for (int i = 0; i < rowDim; ++i) {
106 for (int j = 0; j < colDim; ++j) {
107 matrix[i][j] = getParameterValue(i, j);
108 }
109 }
110 return matrix;
111 }
112
113 @Override
114 public int getDimension() {
115 return matrix.getDimension();
116 }
117
118 public double getParameterValue(int dim) {
119 final int row = dim / getRowDimension();
120 final int col = dim % getRowDimension();
121 return getParameterValue(row, col);
122 }
123
124 @Override
125 public String getDimensionName(int i) {
126 return matrix.getDimensionName(i);
127 }
128
129 @Override
130 public int getColumnDimension() {
131 return matrix.getColumnDimension();
132 }
133
134 @Override
135 public int getRowDimension() {
136 return matrix.getRowDimension();
137 }
138
139 @Override
140 public int getUniqueParameterCount() {
141 throw new IllegalArgumentException("Not implemented");
142 }
143
144 @Override
145 public Parameter getUniqueParameter(int index) {
146 throw new IllegalArgumentException("Not implemented");
147 }
148
149 @Override
150 public void copyParameterValues(double[] destination, int offset) {
151 throw new IllegalArgumentException("Not implemented");
152 }
153
154 @Override
155 public void setAllParameterValuesQuietly(double[] values, int offset) {
156 throw new IllegalArgumentException("Not implemented");
157 }
158
159 @Override
160 public String toSymmetricString() {
161 return MatrixParameter.toSymmetricString(this);
162 }
163
164 @Override
165 public void variableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
166 if (variable == matrix) {
167 fireParameterChangedEvent(index, type);
168 } else {
169 throw new IllegalArgumentException("Unknown variable");
170 }
171 }
172 }