| 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 java.util.ArrayList; |
| 29 |
import java.util.List; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class TransposedMatrixParameter extends MatrixParameter { |
| 36 |
|
| 37 |
public TransposedMatrixParameter(String name) { |
| 38 |
super(name + ".transpose"); |
| 39 |
} |
| 40 |
|
| 41 |
public TransposedMatrixParameter(String name, Parameter[] parameters) { |
| 42 |
super(name + ".transpose", parameters); |
| 43 |
} |
| 44 |
|
| 45 |
public static TransposedMatrixParameter recast(String name, CompoundParameter compoundParameter) { |
| 46 |
final int count = compoundParameter.getParameterCount(); |
| 47 |
Parameter[] parameters = new Parameter[count]; |
| 48 |
for (int i = 0; i < count; ++i) { |
| 49 |
parameters[i] = compoundParameter.getParameter(i); |
| 50 |
} |
| 51 |
return new TransposedMatrixParameter(name, parameters); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public int getColumnDimension() { |
| 60 |
|
| 61 |
return super.getParameter(0).getDimension(); |
| 62 |
} |
| 63 |
|
| 64 |
public int getRowDimension() { |
| 65 |
|
| 66 |
return super.getParameterCount(); |
| 67 |
} |
| 68 |
|
| 69 |
public int getParameterCount() { |
| 70 |
|
| 71 |
|
| 72 |
return getColumnDimension(); |
| 73 |
} |
| 74 |
|
| 75 |
public double[][] getParameterAsMatrix() { |
| 76 |
final int I = getColumnDimension(); |
| 77 |
final int J = getRowDimension(); |
| 78 |
double[][] parameterAsMatrix = new double[J][I]; |
| 79 |
for (int i = 0; i < I; i++) { |
| 80 |
for (int j = 0; j < J; j++) |
| 81 |
parameterAsMatrix[j][i] = getParameterValue(i, j); |
| 82 |
|
| 83 |
} |
| 84 |
return parameterAsMatrix; |
| 85 |
} |
| 86 |
|
| 87 |
public double getParameterValue(int dim) { |
| 88 |
|
| 89 |
int transposedDim = dim; |
| 90 |
return super.getParameterValue(transposedDim); |
| 91 |
} |
| 92 |
|
| 93 |
public Parameter getParameter(int index) { |
| 94 |
if (slices == null) { |
| 95 |
|
| 96 |
slices = new ArrayList<Parameter>(); |
| 97 |
for (int i = 0; i < getColumnDimension(); ++i) { |
| 98 |
VectorSliceParameter thisSlice = new VectorSliceParameter(getParameterName() + "." + i, i); |
| 99 |
for (int j = 0; j < getRowDimension(); ++j) { |
| 100 |
thisSlice.addParameter(super.getParameter(j)); |
| 101 |
} |
| 102 |
slices.add(thisSlice); |
| 103 |
} |
| 104 |
} |
| 105 |
return slices.get(index); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
MatrixParameter transposeBack(){ |
| 114 |
return MatrixParameter.recast(null, this); |
| 115 |
} |
| 116 |
|
| 117 |
private List<Parameter> slices = null; |
| 118 |
} |