Debian Codegraph @

src/dr/math/matrixAlgebra/SymmetricMatrix.java

raw symbols SCIP
1 /*
2 * SymmetricMatrix.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.matrixAlgebra;
27
28 /**
29 * Symmetric matrix
30 *
31 * @author Didier H. Besset
32 */
33 public class SymmetricMatrix extends Matrix {
34
35 private static int lupCRLCriticalDimension = 36;
36
37 /**
38 * Creates a symmetric matrix with given components.
39 *
40 * @param a double[][]
41 */
42 public SymmetricMatrix(double[][] a) {
43 super(a);
44 }
45
46 /**
47 * @param n int
48 * @throws java.lang.NegativeArraySizeException
49 * if n <= 0
50 */
51 public SymmetricMatrix(int n) throws NegativeArraySizeException {
52 super(n, n);
53 }
54
55 /**
56 * Constructor method.
57 *
58 * @param n int
59 * @param m int
60 * @throws java.lang.NegativeArraySizeException
61 * if n,m <= 0
62 */
63 public SymmetricMatrix(int n, int m) throws NegativeArraySizeException {
64 super(n, m);
65 }
66
67 /**
68 * @param a Matrix
69 * @return SymmetricMatrix sum of the matrix with the supplied matrix.
70 * @throws IllegalDimension if the supplied matrix does not
71 * have the same dimensions.
72 */
73 public SymmetricMatrix add(SymmetricMatrix a)
74 throws IllegalDimension {
75 return new SymmetricMatrix(addComponents(a));
76 }
77
78 /**
79 * Answers the inverse of the receiver computed via the CRL algorithm.
80 *
81 * @return MatrixAlgebra.SymmetricMatrix
82 * @throws java.lang.ArithmeticException if the matrix is singular.
83 */
84 private SymmetricMatrix crlInverse() throws ArithmeticException {
85 if (rows() == 1)
86 return inverse1By1();
87 else if (rows() == 2)
88 return inverse2By2();
89 Matrix[] splitMatrices = split();
90 SymmetricMatrix b1 = (SymmetricMatrix) splitMatrices[0].inverse();
91 Matrix cb1 = splitMatrices[2].secureProduct(b1);
92 SymmetricMatrix cb1cT = new SymmetricMatrix(
93 cb1.productWithTransposedComponents(splitMatrices[2]));
94 splitMatrices[1] = ((SymmetricMatrix)
95 splitMatrices[1]).secureSubtract(cb1cT).inverse();
96 splitMatrices[2] = splitMatrices[1].secureProduct(cb1);
97 splitMatrices[0] = b1.secureAdd(new SymmetricMatrix(
98 cb1.transposedProductComponents(splitMatrices[2])));
99 return SymmetricMatrix.join(splitMatrices);
100 }
101
102 /**
103 * @return MatrixAlgebra.SymmetricMatrix
104 * @throws matrixAlgebra.IllegalDimension The supplied components are not those of a square matrix.
105 * @throws matrixAlgebra.NonSymmetricComponents
106 * The supplied components are not symmetric.
107 * @param comp double[][] components of the matrix
108 */
109 public static SymmetricMatrix fromComponents(double[][] comp)
110 throws IllegalDimension, NonSymmetricComponents {
111 if (comp.length != comp[0].length)
112 throw new IllegalDimension("Non symmetric components: a "
113 + comp.length + " by " + comp[0].length
114 + " matrix cannot be symmetric");
115 for (int i = 0; i < comp.length; i++) {
116 for (int j = 0; j < i; j++) {
117 if (comp[i][j] != comp[j][i])
118 throw new NonSymmetricComponents(
119 "Non symmetric components: a[" + i + "][" + j
120 + "]= " + comp[i][j] + ", a[" + j + "]["
121 + i + "]= " + comp[j][i]);
122 }
123 }
124 return new SymmetricMatrix(comp);
125 }
126
127 /**
128 * @param n int
129 * @return SymmetricMatrix an identity matrix of size n
130 */
131 public static SymmetricMatrix identityMatrix(int n) {
132 double[][] a = new double[n][n];
133 for (int i = 0; i < n; i++) {
134 for (int j = 0; j < n; j++) a[i][j] = 0;
135 a[i][i] = 1;
136 }
137 return new SymmetricMatrix(a);
138 }
139
140 /**
141 * @return Matrix inverse of the receiver.
142 * @throws java.lang.ArithmeticException if the receiver is
143 * a singular matrix.
144 */
145 public Matrix inverse() throws ArithmeticException {
146 return rows() < lupCRLCriticalDimension
147 ? new SymmetricMatrix(
148 (new LUPDecomposition(this)).inverseMatrixComponents())
149 : crlInverse();
150 }
151
152 /**
153 * Compute the inverse of the receiver in the case of a 1 by 1 matrix.
154 * Internal use only: no check is made.
155 *
156 * @return MatrixAlgebra.SymmetricMatrix
157 */
158 private SymmetricMatrix inverse1By1() {
159 double[][] newComponents = new double[1][1];
160 newComponents[0][0] = 1 / components[0][0];
161 return new SymmetricMatrix(newComponents);
162 }
163
164 /**
165 * Compute the inverse of the receiver in the case of a 2 by 2 matrix.
166 * Internal use only: no check is made.
167 *
168 * @return MatrixAlgebra.SymmetricMatrix
169 */
170 private SymmetricMatrix inverse2By2() {
171 double[][] newComponents = new double[2][2];
172 double inverseDeterminant = 1 / (components[0][0] * components[1][1]
173 - components[0][1] * components[1][0]);
174 newComponents[0][0] = inverseDeterminant * components[1][1];
175 newComponents[1][1] = inverseDeterminant * components[0][0];
176 newComponents[0][1] = newComponents[1][0] =
177 -inverseDeterminant * components[1][0];
178 return new SymmetricMatrix(newComponents);
179 }
180
181 /**
182 * Build a matrix from 3 parts (inverse of split).
183 *
184 * @param a MatrixAlgebra.Matrix[]
185 * @return MatrixAlgebra.SymmetricMatrix
186 */
187 private static SymmetricMatrix join(Matrix[] a) {
188 int p = a[0].rows();
189 int n = p + a[1].rows();
190 double[][] newComponents = new double[n][n];
191 for (int i = 0; i < p; i++) {
192 for (int j = 0; j < p; j++)
193 newComponents[i][j] = a[0].components[i][j];
194 for (int j = p; j < n; j++)
195 newComponents[i][j] = newComponents[j][i] =
196 -a[2].components[j - p][i];
197 }
198 for (int i = p; i < n; i++) {
199 for (int j = p; j < n; j++)
200 newComponents[i][j] = a[1].components[i - p][j - p];
201 }
202 return new SymmetricMatrix(newComponents);
203 }
204
205 /**
206 * @param n int
207 * @return int
208 */
209 private int largestPowerOf2SmallerThan(int n) {
210 int m = 2;
211 int m2;
212 while (true) {
213 m2 = 2 * m;
214 if (m2 >= n)
215 return m;
216 m = m2;
217 }
218 }
219
220 /**
221 * @param a double
222 * @return MatrixAlgebra.SymmetricMatrix
223 */
224 public Matrix product(double a) {
225 return new SymmetricMatrix(productComponents(a));
226 }
227
228 /**
229 * @param a Matrix
230 * @return Matrix product of the receiver with the supplied matrix
231 * @throws IllegalDimension If the number of columns of
232 * the receivers are not equal to the number of rows
233 * of the supplied matrix.
234 */
235 public SymmetricMatrix product(SymmetricMatrix a) throws IllegalDimension {
236 return new SymmetricMatrix(productComponents(a));
237 }
238
239 /**
240 * @param a MatrixAlgebra.Matrix
241 * @return MatrixAlgebra.Matrix product of the receiver with
242 * the transpose of the supplied matrix
243 * @throws MatrixAlgebra.IllegalDimension If the number of
244 * columns of the receiver are not equal to the number of
245 * columns of the supplied matrix.
246 */
247 public SymmetricMatrix productWithTransposed(SymmetricMatrix a)
248 throws IllegalDimension {
249 if (a.columns() != columns())
250 throw new IllegalDimension(
251 "Operation error: cannot multiply a "
252 + rows() + " by " + columns()
253 + " matrix with the transpose of a "
254 + a.rows() + " by " + a.columns() + " matrix");
255 return new SymmetricMatrix(productWithTransposedComponents(a));
256 }
257
258 /**
259 * Same as add ( SymmetricMatrix a), but without dimension checking.
260 *
261 * @param a MatrixAlgebra.SymmetricMatrix
262 * @return MatrixAlgebra.SymmetricMatrix
263 */
264 protected SymmetricMatrix secureAdd(SymmetricMatrix a) {
265 return new SymmetricMatrix(addComponents(a));
266 }
267
268 /**
269 * Same as product(SymmetricMatrix a), but without dimension checking.
270 *
271 * @param a MatrixAlgebra.SymmetricMatrix
272 * @return MatrixAlgebra.SymmetricMatrix
273 */
274 protected SymmetricMatrix secureProduct(SymmetricMatrix a) {
275 return new SymmetricMatrix(productComponents(a));
276 }
277
278 /**
279 * Same as subtract ( SymmetricMatrix a), but without dimension checking.
280 *
281 * @param a MatrixAlgebra.SymmetricMatrix
282 * @return MatrixAlgebra.SymmetricMatrix
283 */
284 protected SymmetricMatrix secureSubtract(SymmetricMatrix a) {
285 return new SymmetricMatrix(subtractComponents(a));
286 }
287
288 /**
289 * Divide the receiver into 3 matrices of approximately equal dimension.
290 *
291 * @return MatrixAlgebra.Matrix[] Array of splitted matrices
292 */
293 private Matrix[] split() {
294 int n = rows();
295 int p = largestPowerOf2SmallerThan(n);
296 int q = n - p;
297 double[][] a = new double[p][p];
298 double[][] b = new double[q][q];
299 double[][] c = new double[q][p];
300 for (int i = 0; i < p; i++) {
301 for (int j = 0; j < p; j++)
302 a[i][j] = components[i][j];
303 for (int j = p; j < n; j++)
304 c[j - p][i] = components[i][j];
305 }
306 for (int i = p; i < n; i++) {
307 for (int j = p; j < n; j++)
308 b[i - p][j - p] = components[i][j];
309 }
310 Matrix[] answer = new Matrix[3];
311 answer[0] = new SymmetricMatrix(a);
312 answer[1] = new SymmetricMatrix(b);
313 answer[2] = new Matrix(c);
314 return answer;
315 }
316
317 /**
318 * @param a matrixAlgebra.SymmetricMatrix
319 * @return matrixAlgebra.SymmetricMatrix
320 * @throws matrixAlgebra.IllegalDimension (from constructor).
321 */
322 public SymmetricMatrix subtract(SymmetricMatrix a)
323 throws IllegalDimension {
324 return new SymmetricMatrix(subtractComponents(a));
325 }
326
327 /**
328 * @return matrixAlgebra.Matrix the same matrix
329 */
330 public Matrix transpose() {
331 return this;
332 }
333
334 /**
335 * @param a MatrixAlgebra.SymmetricMatrix
336 * @return MatrixAlgebra.SymmetricMatrix product of the tranpose
337 * of the receiver with the supplied matrix
338 * @throws MatrixAlgebra.IllegalDimension If the number of
339 * rows of the receiver are not equal to
340 * the number of rows of the supplied matrix.
341 */
342 public SymmetricMatrix transposedProduct(SymmetricMatrix a)
343 throws IllegalDimension {
344 if (a.rows() != rows())
345 throw new IllegalDimension(
346 "Operation error: cannot multiply a tranposed "
347 + rows() + " by " + columns() + " matrix with a " +
348 a.rows() + " by " + a.columns() + " matrix");
349 return new SymmetricMatrix(transposedProductComponents(a));
350 }
351 }