Debian Codegraph @

src/dr/math/matrixAlgebra/LUPDecomposition.java

raw symbols SCIP
1 /*
2 * LUPDecomposition.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 * Lower Upper Permutation (LUP) decomposition
30 *
31 * @author Didier H. Besset
32 */
33 public class LUPDecomposition {
34 /**
35 * Rows of the system
36 */
37 private double[][] rows;
38 /**
39 * Permutation
40 */
41 private int[] permutation = null;
42 /**
43 * Permutation's parity
44 */
45 private int parity = 1;
46
47 /**
48 * Constructor method
49 *
50 * @param components double[][]
51 * @throws DhbMatrixAlgebra.DhbIllegalDimension
52 * the supplied matrix is not square
53 */
54 public LUPDecomposition(double[][] components)
55 throws IllegalDimension {
56 int n = components.length;
57 if (components[0].length != n)
58 throw new IllegalDimension("Illegal system: a" + n + " by "
59 + components[0].length + " matrix is not a square matrix");
60 rows = components;
61 initialize();
62 }
63
64 /**
65 * Constructor method.
66 *
67 * @param m DhbMatrixAlgebra.Matrix
68 * @throws DhbMatrixAlgebra.DhbIllegalDimension
69 * the supplied matrix is not square
70 */
71 public LUPDecomposition(Matrix m) throws IllegalDimension {
72 if (!m.isSquare())
73 throw new IllegalDimension(
74 "Supplied matrix is not a square matrix");
75 initialize(m.components);
76 }
77
78 /**
79 * Constructor method.
80 *
81 * @param m DhbMatrixAlgebra.DhbSymmetricMatrix
82 */
83 public LUPDecomposition(SymmetricMatrix m) {
84 initialize(m.components);
85 }
86
87 /**
88 * @param xTilde double[]
89 * @return double[]
90 */
91 private double[] backwardSubstitution(double[] xTilde) {
92 int n = rows.length;
93 double[] answer = new double[n];
94 for (int i = n - 1; i >= 0; i--) {
95 answer[i] = xTilde[i];
96 for (int j = i + 1; j < n; j++)
97 answer[i] -= rows[i][j] * answer[j];
98 answer[i] /= rows[i][i];
99 }
100 return answer;
101 }
102
103 private void decompose() {
104 int n = rows.length;
105 permutation = new int[n];
106 for (int i = 0; i < n; i++)
107 permutation[i] = i;
108 parity = 1;
109 try {
110 for (int i = 0; i < n; i++) {
111 swapRows(i, largestPivot(i));
112 pivot(i);
113 }
114 } catch (ArithmeticException e) {
115 parity = 0;
116 }
117 }
118
119 /**
120 * @return boolean true if decomposition was done already
121 */
122 private boolean decomposed() {
123 if (parity == 1 && permutation == null)
124 decompose();
125 return parity != 0;
126 }
127
128 /**
129 * @param c double[]
130 * @return double[]
131 */
132 public double determinant() {
133 if (!decomposed())
134 return Double.NaN;
135 double determinant = parity;
136 for (int i = 0; i < rows.length; i++)
137 determinant *= rows[i][i];
138 return determinant;
139 }
140
141 public double logDeterminant() {
142 if (!decomposed()) {
143 return Double.NaN;
144 }
145 int sign = parity;
146 double logDeterminant = 0.0;
147 for (int i = 0; i < rows.length; i++) {
148 if (rows[i][i] == 0.0) {
149 return Double.NaN;
150 }
151 logDeterminant += Math.log(Math.abs(rows[i][i]));
152 sign *= (rows[i][i] > 0 ? 1 : -1);
153 }
154 if (sign < 0) {
155 return Double.NaN;
156 } else {
157 return logDeterminant;
158 }
159 }
160
161 public boolean isPD() { // TODO Fix; check sign and for 0 in rows[i][i]
162 for (int i = 0; i < rows.length; i++) {
163 if (rows[i][i] <= 0)
164 return false;
165 }
166 return true;
167 }
168
169
170 /**
171 * @param c double[]
172 * @return double[]
173 */
174 private double[] forwardSubstitution(double[] c) {
175 int n = rows.length;
176 double[] answer = new double[n];
177 for (int i = 0; i < n; i++) {
178 answer[i] = c[permutation[i]];
179 for (int j = 0; j <= i - 1; j++)
180 answer[i] -= rows[i][j] * answer[j];
181 }
182 return answer;
183 }
184
185 private void initialize() {
186 permutation = null;
187 parity = 1;
188 }
189
190 /**
191 * @param components double[][] components obtained from constructor methods.
192 */
193 private void initialize(double[][] components) {
194 int n = components.length;
195 rows = new double[n][n];
196 for (int i = 0; i < n; i++) {
197 for (int j = 0; j < n; j++)
198 rows[i][j] = components[i][j];
199 }
200 initialize();
201 }
202
203 /**
204 * @param c double[]
205 * @return double[]
206 */
207 public double[][] inverseMatrixComponents() {
208 if (!decomposed())
209 return null;
210 int n = rows.length;
211 double[][] inverseRows = new double[n][n];
212 double[] column = new double[n];
213 for (int i = 0; i < n; i++) {
214 for (int j = 0; j < n; j++)
215 column[j] = 0;
216 column[i] = 1;
217 column = solve(column);
218 for (int j = 0; j < n; j++)
219 inverseRows[i][j] = column[j];
220 }
221 return inverseRows;
222 }
223
224 /**
225 * @param k int
226 * @return int
227 */
228 private int largestPivot(int k) {
229 double maximum = Math.abs(rows[k][k]);
230 double abs;
231 int index = k;
232 for (int i = k + 1; i < rows.length; i++) {
233 abs = Math.abs(rows[i][k]);
234 if (abs > maximum) {
235 maximum = abs;
236 index = i;
237 }
238 }
239 return index;
240 }
241
242 /**
243 * @param k int
244 */
245 private void pivot(int k) {
246 double inversePivot = 1 / rows[k][k];
247 int k1 = k + 1;
248 int n = rows.length;
249 for (int i = k1; i < n; i++) {
250 rows[i][k] *= inversePivot;
251 for (int j = k1; j < n; j++)
252 rows[i][j] -= rows[i][k] * rows[k][j];
253 }
254 }
255
256 /**
257 * @param c double[]
258 * @return double[]
259 */
260 public double[] solve(double[] c) {
261 return decomposed()
262 ? backwardSubstitution(forwardSubstitution(c))
263 : null;
264 }
265
266 /**
267 * @param c double[]
268 * @return double[]
269 */
270 public Vector solve(Vector c) {
271 double[] components = solve(c.components);
272 if (components == null)
273 return null;
274 return components == null ? null : new Vector(components);
275 }
276
277 /**
278 * @param i int
279 * @param k int
280 */
281 private void swapRows(int i, int k) {
282 if (i != k) {
283 double temp;
284 for (int j = 0; j < rows.length; j++) {
285 temp = rows[i][j];
286 rows[i][j] = rows[k][j];
287 rows[k][j] = temp;
288 }
289 int nTemp;
290 nTemp = permutation[i];
291 permutation[i] = permutation[k];
292 permutation[k] = nTemp;
293 parity = -parity;
294 }
295 }
296
297 /**
298 * Make sure the supplied matrix components are those of
299 * a symmetric matrix
300 *
301 * @param components double
302 */
303 public static void symmetrizeComponents(double[][] components) {
304 for (int i = 0; i < components.length; i++) {
305 for (int j = i + 1; j < components.length; j++) {
306 components[i][j] += components[j][i];
307 components[i][j] *= 0.5;
308 components[j][i] = components[i][j];
309 }
310 }
311 }
312
313 /**
314 * Returns a String that represents the value of this object.
315 *
316 * @return a string representation of the receiver
317 */
318 public String toString() {
319 StringBuffer sb = new StringBuffer();
320 char[] separator = {'[', ' '};
321 int n = rows.length;
322 for (int i = 0; i < n; i++) {
323 separator[0] = '{';
324 for (int j = 0; j < n; j++) {
325 sb.append(separator);
326 sb.append(rows[i][j]);
327 separator[0] = ' ';
328 }
329 sb.append('}');
330 sb.append('\n');
331 }
332 if (permutation != null) {
333 sb.append(parity == 1 ? '+' : '-');
334 sb.append("( " + permutation[0]);
335 for (int i = 1; i < n; i++)
336 sb.append(", " + permutation[i]);
337 sb.append(')');
338 sb.append('\n');
339 }
340 return sb.toString();
341 }
342 }