Debian Codegraph @

src/dr/inference/model/BlockUpperTriangularMatrixParameter.java

raw symbols SCIP
1 /*
2 * BlockUpperTriangularMatrixParameter.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 Max Tolkoff
30 */
31
32 public class BlockUpperTriangularMatrixParameter extends MatrixParameter {
33 private int rowDim;
34 private Bounds bounds = null;
35
36 public TransposedBlockUpperTriangularMatrixParameter transposeBlock() {
37 return TransposedBlockUpperTriangularMatrixParameter.recast(getVariableName(), this);
38 }
39
40 public BlockUpperTriangularMatrixParameter(String name, Parameter[] params, boolean diagonalRestriction) {
41 super(name);
42
43 int rowDim = params[params.length - 1].getSize();
44 int colDim = params.length;
45
46 for (int i = 0; i < colDim; i++) {
47 this.addParameter(params[i]);
48 }
49
50 this.rowDim = rowDim;
51
52 if (diagonalRestriction) {
53 for (int i = 0; i < getColumnDimension(); i++) {
54 if (i < getRowDimension()) {
55 double[] uppers = new double[i + 1];
56 double[] lowers = new double[i + 1];
57
58 for (int j = 0; j < uppers.length; j++) {
59 uppers[j] = Double.POSITIVE_INFINITY;
60 lowers[j] = Double.NEGATIVE_INFINITY;
61
62 }
63 lowers[i] = 0;
64 getParameter(i).addBounds(new DefaultBounds(uppers, lowers));
65 }
66 }
67 }
68
69 }
70
71 @Override
72 public int getRowDimension() {
73 return rowDim;
74 }
75
76 public void setRowDimension(int rowDim) {
77 this.rowDim = rowDim;
78 }
79
80 public double getParameterValue(int row, int col) {
81 if (!matrixCondition(row, col)) {
82 return 0.0;
83 } else {
84 return getParameter(col).getParameterValue(getInnerDimension(row, col));
85 }
86 }
87
88 protected int getRow(int PID) {
89 return PID % getRowDimension();
90 }
91
92 protected int getColumn(int PID) {
93 return PID / getRowDimension();
94 }
95
96 public void setParameterValueQuietly(int row, int col, double value) {
97 if (matrixCondition(row, col)) {
98 getParameter(col).setParameterValueQuietly(getInnerDimension(row, col), value);
99 }
100 }
101
102 public void setParameterValue(int row, int col, double value) {
103 setParameterValueQuietly(row, col, value);
104 fireParameterChangedEvent();
105 }
106
107 public void setParameterValue(int PID, double value) {
108
109 int row = getRow(PID);
110 int col = getColumn(PID);
111
112 if (matrixCondition(row, col)) {
113 setParameterValue(row, col, value);
114 }
115 }
116
117
118 //test if violates matrix condition
119 boolean matrixCondition(int row, int col) {
120 return row>=(col);
121 }
122
123 public double getParameterValue(int id) {
124 int row = getRow(id);
125 int col = getColumn(id);
126
127 if (matrixCondition(row, col)) {
128 return getParameterValue(row, col);
129 } else {
130 return 0;
131 }
132 }
133
134 public void addBounds(Bounds<Double> boundary) {
135
136 if (bounds == null) {
137 bounds = new BUTMPBounds();
138 }
139
140 IntersectionBounds newBounds = new IntersectionBounds(getDimension());
141 newBounds.addBounds(bounds);
142
143 ((IntersectionBounds) bounds).addBounds(boundary);
144 }
145
146 public Bounds<Double> getBounds() {
147
148 if (bounds == null) {
149 bounds = new BUTMPBounds();
150 }
151 return bounds;
152 }
153
154 protected int getInnerDimension(int row, int col) {
155 return row;
156 }
157
158 private class BUTMPBounds implements Bounds<Double> {
159 //TODO test!
160
161 public Double getUpperLimit(int dim) {
162 int row = getRow(dim);
163 int col = getColumn(dim);
164
165 if (matrixCondition(row, col)) {
166 return getParameter(col).getBounds().getUpperLimit(getInnerDimension(row, col));
167 } else {
168 return 0.0;
169 }
170 }
171
172 public Double getLowerLimit(int dim) {
173 int row = getRow(dim);
174 int col = getColumn(dim);
175
176 if (matrixCondition(row, col)) {
177 return getParameter(col).getBounds().getLowerLimit(getInnerDimension(row, col));
178 } else
179 return 0.0;
180 }
181
182 public int getBoundsDimension() {
183 return getDimension();
184 }
185 }
186
187 public String getDimensionName(int dim) {
188 return getId() + "." + getRow(dim) + "." + getColumn(dim);
189 }
190
191 public int getDimension() {
192 return getRowDimension() * getColumnDimension();
193 }
194
195
196 }