Debian Codegraph @

src/dr/math/Procrustes.java

raw symbols SCIP
1 /*
2 * Procrustes.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.math;
27
28 import org.apache.commons.math.linear.*;
29
30 /**
31 * Procrustination function based on procrustes.r
32 *
33 * @author Andrew Rambaut
34 * @author Marc Suchard
35 * @version $Id$
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 // J <- diag(n) - 1/n * matrix(1, n, n)
53 // for n = 3, J = {{1, -2/3, -2/3}, {-2/3, 1, -2/3}, {-2/3, -2/3, 1}}
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 // J <- diag(n)
65
66 for (int i = 0; i < rowDimension; i++) {
67 J.setEntry(i, i, 1);
68 }
69
70 }
71
72
73 // C <- t(Xstar) %*% J %*% X
74
75 RealMatrix C = Xstar.transpose().multiply(J.multiply(X));
76
77 // svd.out <- svd(C)
78 // R <- svd.out$v %*% t(svd.out$u)
79 // NB: Apache math does a different SVD from R. TODO Should use Colt library
80 SingularValueDecomposition SVD = new SingularValueDecompositionImpl(C);
81 R = SVD.getV().multiply(SVD.getUT());
82
83 // s <- 1
84 double s = 1.0; // scale = 1 unless dilation is being used
85
86 if (allowDilation) {
87 // mat1 <- t(Xstar) %*% J %*% X %*% R
88 RealMatrix mat1 = Xstar.transpose().multiply(J.multiply(X.multiply(R)));
89
90 // mat2 <- t(X) %*% J %*% X
91 RealMatrix mat2 = X.transpose().multiply(J.multiply(X));
92
93 // s.numer <- 0
94 // s.denom <- 0
95 double numer = 0.0;
96 double denom = 0.0;
97
98 // for (i in 1:m) {
99 // s.numer <- s.numer + mat1[i, i]
100 // s.denom <- s.denom + mat2[i, i]
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 // s <- s.numer/s.denom
107 s = numer / denom;
108 }
109 this.s = s;
110
111 // tt <- matrix(0, m, 1)
112 RealMatrix tmpT = new Array2DRowRealMatrix(columnDimension, 1); // a translation vector of zero unless translation is being used
113
114 if (allowTranslation) {
115 // tt <- 1/n * t(Xstar - s * X %*% R) %*% matrix(1, n, 1)
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(); // NB Different from R
128 }
129
130 public final double getDilation() {
131 return s;
132 }
133
134 public final RealMatrix getR() {
135 return R.copy(); // NB Different from R
136 }
137
138 /**
139 * procrustinate the complete matrix of coordinates
140 * @param X the matrix containing coordinates (same dimensions as X in the constructor)
141 * @return the transformed matrix
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 // X.new <- s * X %*% R + matrix(tt, nrow(X), ncol(X), byrow = TRUE)
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 // rotate, scale and translate
159 return X.multiply(R).scalarMultiply(s).add(tt); // Was a bug here
160 }
161
162 /**
163 * procrustinate a single set of coordinates
164 * @param X
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 // rotate, scale and translate
174 RealMatrix Xnew = tmp.multiply(R).scalarMultiply(s).add(T);
175
176 return Xnew.getRow(0);
177 }
178
179 /**
180 * procrustinate the complete matrix of coordinates
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 }