| 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 |
import dr.xml.*; |
| 29 |
import mpi.MPI; |
| 30 |
|
| 31 |
import java.util.ArrayList; |
| 32 |
import java.util.List; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class CompoundFastMatrixParameter extends CompoundParameter implements MatrixParameterInterface { |
| 38 |
|
| 39 |
private final int rowDimension; |
| 40 |
private int colDimension; |
| 41 |
|
| 42 |
private final List<MatrixParameterInterface> columns = new ArrayList<MatrixParameterInterface>(); |
| 43 |
private final List<Integer> offsets = new ArrayList<Integer>(); |
| 44 |
private final List<MatrixParameterInterface> matrices; |
| 45 |
|
| 46 |
public CompoundFastMatrixParameter(String name, List<MatrixParameterInterface> matrices) { |
| 47 |
super(name, compoundMatrices(matrices)); |
| 48 |
|
| 49 |
this.matrices = matrices; |
| 50 |
|
| 51 |
rowDimension = matrices.get(0).getRowDimension(); |
| 52 |
colDimension = 0; |
| 53 |
|
| 54 |
for (MatrixParameterInterface matrix : matrices) { |
| 55 |
if (matrix.getRowDimension() != rowDimension) { |
| 56 |
throw new IllegalArgumentException("Inconsistent row dimensions"); |
| 57 |
} |
| 58 |
|
| 59 |
for (int i = 0; i < matrix.getColumnDimension(); ++i) { |
| 60 |
columns.add(matrix); |
| 61 |
offsets.add(i); |
| 62 |
} |
| 63 |
|
| 64 |
colDimension += matrix.getColumnDimension(); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
private static Parameter[] compoundMatrices(List<MatrixParameterInterface> matrices) { |
| 69 |
int length = 0; |
| 70 |
for (MatrixParameterInterface matrix : matrices) { |
| 71 |
length += matrix.getUniqueParameterCount(); |
| 72 |
} |
| 73 |
|
| 74 |
Parameter[] parameters = new Parameter[length]; |
| 75 |
int index = 0; |
| 76 |
|
| 77 |
for (MatrixParameterInterface matrix : matrices) { |
| 78 |
for (int i = 0; i < matrix.getUniqueParameterCount(); ++i) { |
| 79 |
parameters[index] = matrix.getUniqueParameter(i); |
| 80 |
++index; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return parameters; |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public Parameter getParameter(int column) { |
| 89 |
return columns.get(column).getParameter(offsets.get(column)); |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public double getParameterValue(int row, int col) { |
| 94 |
return columns.get(col).getParameterValue(row, offsets.get(col)); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void setParameterValue(int row, int col, double value) { |
| 99 |
columns.get(col).setParameterValue(row, offsets.get(col), value); |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void setParameterValueQuietly(int row, int col, double value) { |
| 104 |
columns.get(col).setParameterValueQuietly(row, offsets.get(col), value); |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public void setParameterValueNotifyChangedAll(int row, int column, double value) { |
| 109 |
columns.get(column).setParameterValueNotifyChangedAll(row, offsets.get(column), value); |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public double[] getColumnValues(int col) { |
| 114 |
throw new RuntimeException("Not yet implemented"); |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public double[][] getParameterAsMatrix() { |
| 119 |
throw new RuntimeException("Not yet implemented"); |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
public int getColumnDimension() { |
| 124 |
return colDimension; |
| 125 |
} |
| 126 |
|
| 127 |
@Override |
| 128 |
public int getRowDimension() { |
| 129 |
return rowDimension; |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public int getUniqueParameterCount() { |
| 134 |
return getParameterCount(); |
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
public Parameter getUniqueParameter(int index) { |
| 139 |
return getParameter(index); |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public void copyParameterValues(double[] destination, int offset) { |
| 144 |
for (MatrixParameterInterface matrix : matrices) { |
| 145 |
matrix.copyParameterValues(destination, offset); |
| 146 |
offset += matrix.getRowDimension() * matrix.getColumnDimension(); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
@Override |
| 151 |
public void setAllParameterValuesQuietly(double[] values, int offset) { |
| 152 |
for (MatrixParameterInterface matrix : matrices) { |
| 153 |
matrix.setAllParameterValuesQuietly(values, offset); |
| 154 |
offset += matrix.getRowDimension() * matrix.getColumnDimension(); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
@Override |
| 159 |
public double[] getParameterValues() { |
| 160 |
int length = 0; |
| 161 |
for (MatrixParameterInterface matrix : matrices) { |
| 162 |
length += matrix.getRowDimension() * matrix.getColumnDimension(); |
| 163 |
} |
| 164 |
double[] rtn = new double[length]; |
| 165 |
copyParameterValues(rtn, 0); |
| 166 |
return rtn; |
| 167 |
} |
| 168 |
|
| 169 |
@Override |
| 170 |
public String toSymmetricString() { |
| 171 |
return MatrixParameter.toSymmetricString(this); |
| 172 |
} |
| 173 |
|
| 174 |
public final static String COMPOUND_FAST_MATRIX_PARAMETER = "compoundFastMatrixParameter"; |
| 175 |
|
| 176 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 177 |
|
| 178 |
public String getParserName() { |
| 179 |
return COMPOUND_FAST_MATRIX_PARAMETER; |
| 180 |
} |
| 181 |
|
| 182 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 183 |
|
| 184 |
List<MatrixParameterInterface> matrices = new ArrayList<MatrixParameterInterface>(); |
| 185 |
|
| 186 |
for (int i = 0; i < xo.getChildCount(); ++i) { |
| 187 |
matrices.add((MatrixParameterInterface) xo.getChild(i)); |
| 188 |
} |
| 189 |
|
| 190 |
final String name = xo.hasId() ? xo.getId() : null; |
| 191 |
|
| 192 |
return new CompoundFastMatrixParameter(name, matrices); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public String getParserDescription() { |
| 200 |
return "A compound matrix parameter constructed from its component parameters."; |
| 201 |
} |
| 202 |
|
| 203 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 204 |
return rules; |
| 205 |
} |
| 206 |
|
| 207 |
private final XMLSyntaxRule[] rules = { |
| 208 |
new ElementRule(MatrixParameterInterface.class, 1, Integer.MAX_VALUE), |
| 209 |
}; |
| 210 |
|
| 211 |
public Class getReturnType() { |
| 212 |
return CompoundFastMatrixParameter.class; |
| 213 |
} |
| 214 |
}; |
| 215 |
} |