Debian Codegraph @

src/dr/math/matrixAlgebra/CholeskyDecomposition.java

raw symbols SCIP
1 /*
2 * CholeskyDecomposition.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 * Created by IntelliJ IDEA.
30 * User: msuchard
31 * Date: Jan 12, 2007
32 * Time: 9:05:44 PM
33 * To change this template use File | Settings | File Templates.
34 */
35 public class CholeskyDecomposition {
36
37 /**
38 * Dimension of square matrix
39 */
40 private int n;
41
42 public boolean isSPD() {
43 return isspd;
44 }
45
46 /**
47 * Symmetric and positive definite flag.
48 */
49 private boolean isspd;
50
51 public double[][] getL() {
52 return L;
53 }
54
55 private double[][] L;
56
57 public CholeskyDecomposition(Matrix A) throws IllegalDimension{
58 this(A.components);
59 }
60
61 public CholeskyDecomposition(double[][] A) throws IllegalDimension {
62
63 n = A.length;
64 L = new double[n][n];
65 isspd = (A[0].length == n);
66 if (!isspd)
67 throw new IllegalDimension("Cholesky decomposition is only defined for square matrices");
68 // Main loop.
69 for (int j = 0; j < n; j++) {
70 double[] Lrowj = L[j];
71 double d = 0.0;
72 for (int k = 0; k < j; k++) {
73 double[] Lrowk = L[k];
74 double s = 0.0;
75 for (int i = 0; i < k; i++) {
76 s += Lrowk[i] * Lrowj[i];
77 }
78 Lrowj[k] = s = (A[j][k] - s) / L[k][k];
79 d = d + s * s;
80 isspd = isspd & (A[k][j] == A[j][k]);
81 }
82 d = A[j][j] - d;
83 isspd = isspd & (d > 0.0);
84 L[j][j] = Math.sqrt(Math.max(d, 0.0));
85 /*for (int k = j+1; k < n; k++) {
86 L[j][k] = 0.0;
87 }*/
88 }
89
90 }
91
92 public static double[][] execute(double[] A, final int offset, final int n) {
93
94 final double[][] L = new double[n][n];
95 // boolean isspd = (A[0].length == n);
96 // if (!isspd)
97 // throw new IllegalDimension("Cholesky decomposition is only defined for square matrices");
98 // Main loop.
99 for (int j = 0; j < n; j++) {
100 double[] Lrowj = L[j];
101 double d = 0.0;
102 for (int k = 0; k < j; k++) {
103 double[] Lrowk = L[k];
104 double s = 0.0;
105 for (int i = 0; i < k; i++) {
106 s += Lrowk[i] * Lrowj[i];
107 }
108 Lrowj[k] = s = (A[offset + j * n + k] - s) / L[k][k];
109 d = d + s * s;
110 // isspd = isspd & (A[k][j] == A[j][k]);
111 }
112 d = A[offset + j * n + j] - d;
113 // isspd = isspd & (d > 0.0);
114 L[j][j] = Math.sqrt(Math.max(d, 0.0));
115 }
116 return L;
117 }
118 }