| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.inference.model; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 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 |
|
| 85 |
|
| 86 |
|
| 87 |
for (int i = 0; i <getRowDimension() ; i++) { |
| 88 |
areValuesStored[i][index/right.getRowDimension()]=false; |
| 89 |
} } |
| 90 |
if(variable==left) { |
| 91 |
|
| 92 |
|
| 93 |
|
| 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 |
|
| 125 |
|
| 126 |
|
| 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 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 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 |
|