| 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 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class DecomposedMatrix extends MatrixParameter { |
| 34 |
|
| 35 |
public static final String DIM = "dim"; |
| 36 |
public static final String MATRIX_PARAMETER = "decomposedMatrix"; |
| 37 |
|
| 38 |
public DecomposedMatrix(String name) { |
| 39 |
super(name); |
| 40 |
} |
| 41 |
|
| 42 |
public DecomposedMatrix(String name, int dim, Parameter parameter) { |
| 43 |
super(name); |
| 44 |
this.dim = dim; |
| 45 |
this.decomposition = parameter; |
| 46 |
matrix = new double[dim][dim]; |
| 47 |
savedMatrix = new double[dim][dim]; |
| 48 |
composeMatrix(); |
| 49 |
addParameter(parameter); |
| 50 |
} |
| 51 |
|
| 52 |
public void parameterChangedEvent(Parameter parameter, int index) { |
| 53 |
compositionKnown = false; |
| 54 |
|
| 55 |
fireParameterChangedEvent(); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
void composeMatrix() { |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
double[] values = decomposition.getParameterValues(); |
| 65 |
|
| 66 |
for (int i = 0; i < dim; i++) { |
| 67 |
for (int j = i; j < dim; j++) { |
| 68 |
matrix[i][j] = 0.0; |
| 69 |
for (int k = 0; k <= i; k++) |
| 70 |
matrix[i][j] += values[index[i] + k] * values[index[j] + k]; |
| 71 |
matrix[j][i] = matrix[i][j]; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
compositionKnown = true; |
| 76 |
} |
| 77 |
|
| 78 |
public int getDimension() { |
| 79 |
return dim * dim; |
| 80 |
} |
| 81 |
|
| 82 |
protected void storeValues() { |
| 83 |
super.storeValues(); |
| 84 |
for (int i = 0; i < dim; i++) { |
| 85 |
System.arraycopy(matrix[i], 0, savedMatrix[i], 0, dim); |
| 86 |
} |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
public double getParameterValue(int index) { |
| 91 |
int x = index / dim; |
| 92 |
int y = index - x * dim; |
| 93 |
return matrix[x][y]; |
| 94 |
} |
| 95 |
|
| 96 |
protected void restoreValues() { |
| 97 |
super.restoreValues(); |
| 98 |
for (int i = 0; i < dim; i++) { |
| 99 |
System.arraycopy(savedMatrix[i], 0, matrix[i], 0, dim); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
public double getParameterValue(int row, int col) { |
| 104 |
|
| 105 |
if (!compositionKnown) |
| 106 |
composeMatrix(); |
| 107 |
return matrix[row][col]; |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
public double[][] getParameterAsMatrix() { |
| 112 |
|
| 113 |
if (!compositionKnown) |
| 114 |
composeMatrix(); |
| 115 |
|
| 116 |
return matrix; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
public int getColumnDimension() { |
| 121 |
return dim; |
| 122 |
} |
| 123 |
|
| 124 |
public int getRowDimension() { |
| 125 |
return dim; |
| 126 |
} |
| 127 |
|
| 128 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 129 |
|
| 130 |
public String getParserName() { |
| 131 |
return MATRIX_PARAMETER; |
| 132 |
} |
| 133 |
|
| 134 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 135 |
|
| 136 |
Parameter parameter = (Parameter) xo.getChild(Parameter.class); |
| 137 |
int dim = xo.getIntegerAttribute(DIM); |
| 138 |
|
| 139 |
if (dim * (dim + 1) / 2 != parameter.getDimension()) |
| 140 |
throw new XMLParseException("Dim attribute and parameter dimension do not match"); |
| 141 |
|
| 142 |
return new DecomposedMatrix(MATRIX_PARAMETER, dim, parameter); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public String getParserDescription() { |
| 150 |
return "A diagonal matrix parameter constructed from its diagonals."; |
| 151 |
} |
| 152 |
|
| 153 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 154 |
return rules; |
| 155 |
} |
| 156 |
|
| 157 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 158 |
new ElementRule(Parameter.class), |
| 159 |
AttributeRule.newIntegerArrayRule(DIM, false) |
| 160 |
}; |
| 161 |
|
| 162 |
public Class getReturnType() { |
| 163 |
return DecomposedMatrix.class; |
| 164 |
} |
| 165 |
}; |
| 166 |
|
| 167 |
private boolean compositionKnown = false; |
| 168 |
private Parameter decomposition; |
| 169 |
private int dim; |
| 170 |
private double[][] matrix; |
| 171 |
private double[][] savedMatrix; |
| 172 |
|
| 173 |
private static int[] index = {0, 1, 3, 6, 10, 15, 21, 28, 36}; |
| 174 |
} |