| 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.matrixAlgebra; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class LUPDecomposition { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
private double[][] rows; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
private int[] permutation = null; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
private int parity = 1; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 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 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 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 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public LUPDecomposition(SymmetricMatrix m) { |
| 84 |
initialize(m.components); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 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 |
|
| 121 |
|
| 122 |
private boolean decomposed() { |
| 123 |
if (parity == 1 && permutation == null) |
| 124 |
decompose(); |
| 125 |
return parity != 0; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 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() { |
| 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 |
|
| 172 |
|
| 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 |
|
| 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 |
|
| 205 |
|
| 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 |
|
| 226 |
|
| 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 |
|
| 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 |
|
| 258 |
|
| 259 |
|
| 260 |
public double[] solve(double[] c) { |
| 261 |
return decomposed() |
| 262 |
? backwardSubstitution(forwardSubstitution(c)) |
| 263 |
: null; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 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 |
|
| 279 |
|
| 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 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 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 |
|
| 315 |
|
| 316 |
|
| 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 |
} |