| 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.math; |
| 27 |
|
| 28 |
import org.apache.commons.math.linear.*; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class Procrustes { |
| 38 |
public Procrustes(RealMatrix X, RealMatrix Xstar, boolean allowTranslation, boolean allowDilation) { |
| 39 |
rowDimension = X.getRowDimension(); |
| 40 |
columnDimension = X.getColumnDimension(); |
| 41 |
|
| 42 |
if (Xstar.getRowDimension() != rowDimension) { |
| 43 |
throw new IllegalArgumentException("X and Xstar do not have the same number of rows"); |
| 44 |
} |
| 45 |
if (Xstar.getColumnDimension() != columnDimension) { |
| 46 |
throw new IllegalArgumentException("X and Xstar do not have the same number of columns"); |
| 47 |
} |
| 48 |
|
| 49 |
RealMatrix J = new Array2DRowRealMatrix(rowDimension, rowDimension); |
| 50 |
|
| 51 |
if (allowTranslation) { |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
for (int i = 0; i < rowDimension; i++) { |
| 56 |
J.setEntry(i, i, 1.0 - (1.0 / rowDimension)); |
| 57 |
|
| 58 |
for (int j = i + 1; j < rowDimension; j++) { |
| 59 |
J.setEntry(i, j, -1.0 / rowDimension); |
| 60 |
J.setEntry(j, i, -1.0 / rowDimension); |
| 61 |
} |
| 62 |
} |
| 63 |
} else { |
| 64 |
|
| 65 |
|
| 66 |
for (int i = 0; i < rowDimension; i++) { |
| 67 |
J.setEntry(i, i, 1); |
| 68 |
} |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
RealMatrix C = Xstar.transpose().multiply(J.multiply(X)); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
SingularValueDecomposition SVD = new SingularValueDecompositionImpl(C); |
| 81 |
R = SVD.getV().multiply(SVD.getUT()); |
| 82 |
|
| 83 |
|
| 84 |
double s = 1.0; |
| 85 |
|
| 86 |
if (allowDilation) { |
| 87 |
|
| 88 |
RealMatrix mat1 = Xstar.transpose().multiply(J.multiply(X.multiply(R))); |
| 89 |
|
| 90 |
|
| 91 |
RealMatrix mat2 = X.transpose().multiply(J.multiply(X)); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
double numer = 0.0; |
| 96 |
double denom = 0.0; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
for (int i = 0; i < columnDimension; i++) { |
| 103 |
numer = numer + mat1.getEntry(i, i); |
| 104 |
denom = denom + mat2.getEntry(i, i); |
| 105 |
} |
| 106 |
|
| 107 |
s = numer / denom; |
| 108 |
} |
| 109 |
this.s = s; |
| 110 |
|
| 111 |
|
| 112 |
RealMatrix tmpT = new Array2DRowRealMatrix(columnDimension, 1); |
| 113 |
|
| 114 |
if (allowTranslation) { |
| 115 |
|
| 116 |
RealMatrix tmp = new Array2DRowRealMatrix(rowDimension, 1); |
| 117 |
for (int i = 0; i < rowDimension; i++) { |
| 118 |
tmp.setEntry(i, 0, 1); |
| 119 |
} |
| 120 |
tmpT = Xstar.subtract(X.multiply(R).scalarMultiply(s)).transpose().scalarMultiply(1.0 / rowDimension).multiply(tmp); |
| 121 |
} |
| 122 |
|
| 123 |
T = tmpT; |
| 124 |
} |
| 125 |
|
| 126 |
public final RealMatrix getTranslation() { |
| 127 |
return T.copy(); |
| 128 |
} |
| 129 |
|
| 130 |
public final double getDilation() { |
| 131 |
return s; |
| 132 |
} |
| 133 |
|
| 134 |
public final RealMatrix getR() { |
| 135 |
return R.copy(); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public final RealMatrix procrustinate(RealMatrix X) { |
| 144 |
if (X.getRowDimension() != rowDimension) { |
| 145 |
throw new IllegalArgumentException("X does not have the expected number of rows"); |
| 146 |
} |
| 147 |
if (X.getColumnDimension() != columnDimension) { |
| 148 |
throw new IllegalArgumentException("X does not have the expected number of columns"); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
RealMatrix tt = new Array2DRowRealMatrix(rowDimension, columnDimension); |
| 153 |
|
| 154 |
for (int i = 0; i < rowDimension; i++) { |
| 155 |
tt.setRowMatrix(i, T.transpose()); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
return X.multiply(R).scalarMultiply(s).add(tt); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public double[] procrustinate(double[] X) { |
| 167 |
if (X.length != columnDimension) { |
| 168 |
throw new IllegalArgumentException("X does not have the expected number of elements"); |
| 169 |
} |
| 170 |
|
| 171 |
RealMatrix tmp = new Array2DRowRealMatrix(X); |
| 172 |
|
| 173 |
|
| 174 |
RealMatrix Xnew = tmp.multiply(R).scalarMultiply(s).add(T); |
| 175 |
|
| 176 |
return Xnew.getRow(0); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public final static RealMatrix procrustinate(RealMatrix X, RealMatrix Xstar, boolean allowTranslation, boolean allowDilation) { |
| 183 |
return new Procrustes(X, Xstar, allowTranslation, allowDilation).procrustinate(X); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
private final RealMatrix R; |
| 188 |
private final RealMatrix T; |
| 189 |
private final double s; |
| 190 |
private final int rowDimension; |
| 191 |
private final int columnDimension; |
| 192 |
} |