Debian Codegraph @

src/dr/inference/model/MatrixMatrixProduct.java

raw symbols SCIP
1 /*
2 * MatrixMatrixProduct.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 Max Tolkoff
30 */
31
32 //Designed to return a data matrix post computation if asked. Designed for latent liabilities
33 public class MatrixMatrixProduct extends MatrixParameter implements VariableListener {
34 MatrixParameter left;
35 MatrixParameter right;
36 MatrixParameter inPlace;
37 private final int leftDim;
38 private final int rightDim;
39 private final int midDim;
40 Parameter columnMask;
41
42 boolean[][] oldStoredValues;
43
44 double[][] storedValues;
45 boolean[][] areValuesStored;
46 private Bounds bounds=null;
47
48 public MatrixMatrixProduct(MatrixParameter[] params, Parameter columnMask) {
49 super(null, params);
50
51
52
53 this.columnMask=columnMask;
54
55 this.left=params[0];
56 this.right=params[1];
57 if(params.length==3){
58 inPlace=params[2];
59 inPlace.addVariableListener(this);
60 }
61 storedValues=new double[left.getRowDimension()][right.getColumnDimension()];
62 areValuesStored=new boolean[left.getRowDimension()][right.getColumnDimension()];
63 oldStoredValues=new boolean[left.getRowDimension()][right.getColumnDimension()];
64
65 for (int i = 0; i <left.getRowDimension() ; i++) {
66 for (int j = 0; j <right.getColumnDimension() ; j++) {
67 areValuesStored[i][j]=false;
68 }
69
70 }
71 leftDim=left.getRowDimension();
72 midDim=left.getColumnDimension();
73 rightDim=right.getColumnDimension();
74
75 left.addVariableListener(this);
76 right.addVariableListener(this);
77 inPlace.addVariableListener(this);
78
79 }
80
81
82 public void variableChangedEvent(Variable variable, int index, ChangeType type) {
83 if(variable==right) {
84 // System.out.println("RightChanged");
85 // System.out.println(index/getRowDimension());
86 // System.out.println(index);
87 for (int i = 0; i <getRowDimension() ; i++) {
88 areValuesStored[i][index/right.getRowDimension()]=false;
89 } }
90 if(variable==left) {
91 // System.out.println("LeftChanged");
92 // System.out.println(index%left.getRowDimension());
93 // System.out.println(index);
94 for (int i = 0; i <getColumnDimension(); i++) {
95 areValuesStored[index%left.getRowDimension()][i]=false;
96 } }
97 fireParameterChangedEvent(index, type);
98 }
99
100 @Override
101 public int getDimension(){
102 return leftDim*rightDim;
103
104 }
105
106 public void addBounds(Bounds bounds) {
107 this.bounds = bounds;
108 }
109
110 protected void storeValues() {
111 System.arraycopy(areValuesStored, 0, oldStoredValues, 0, areValuesStored.length);
112 left.storeParameterValues();
113 right.storeParameterValues();
114 inPlace.storeParameterValues();
115 }
116
117 protected void restoreValues() {
118 left.restoreParameterValues();
119 right.restoreVariableValues();
120 inPlace.restoreParameterValues();
121 areValuesStored=oldStoredValues;
122 }
123
124 // protected void acceptValues() {
125 // left.acceptParameterValues();
126 // right.acceptParameterValues();
127 // }
128
129
130 public double getParameterValue(int i, int j) {
131 double sum = 0;
132 if (columnMask.getParameterValue(j)!=0 && !areValuesStored[i][j]) {
133 for (int k = 0; k < midDim; k++) {
134 {
135 sum += left.getParameterValue(i, k) * right.getParameterValue(k, j);
136 }
137
138 }
139 inPlace.setParameterValue(i,j, sum);
140 areValuesStored[i][j]=true;
141 }
142 else{
143 sum=inPlace.getParameterValue(i,j);
144 }
145 return sum;
146 }
147
148 @Override
149 public double[][] getParameterAsMatrix() {
150 return super.getParameterAsMatrix();
151 }
152
153 public Parameter getParameter(int PID) {
154 for (int i = 0; i <leftDim ; i++) {
155 getParameterValue(i,PID);
156 }
157 return inPlace.getParameter(PID) ;
158 }
159
160 public int getRowDimension(){
161 return leftDim;
162 }
163
164 public int getColumnDimension(){
165 return rightDim;
166 }
167
168 @Override
169 public double getParameterValue(int dim) {
170 return getParameterValue(dim/getRowDimension(),dim%rightDim);
171 }
172
173 private void throwError(String functionName) throws RuntimeException {
174 throw new RuntimeException("Object " + getId() + " is a deterministic function. Calling "
175 + functionName + " is not allowed");
176 }
177
178 public void setParameterValue(int dim, double value) {
179 throwError("setParameterValue()");
180 }
181
182 public void setParameterValueQuietly(int dim, double value) {
183 throwError("setParameterValueQuietly()");
184 }
185
186 @Override
187
188 public void setParameterValueNotifyChangedAll(int dim, double value) {
189 throwError("setParameterValueNotifyChangedAll()");
190 }
191
192
193 // @Override
194 // public String getParameterName() {
195 // if (getId() == null) {
196 // StringBuilder sb = new StringBuilder("product");
197 // sb.append(".").append(left.getId());
198 // sb.append(".").append(right.getId());
199 // setId(sb.toString());
200 // }
201 // return getId();
202 // }
203
204 @Override
205 public Bounds<Double> getBounds() {
206 return bounds;
207 }
208
209 public void addDimension(int index, double value) {
210 throw new RuntimeException("Not yet implemented.");
211 }
212
213 public double removeDimension(int index) {
214 throw new RuntimeException("Not yet implemented.");
215 }
216 };
217