| 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 SymmetricMatrix extends Matrix { |
| 34 |
|
| 35 |
private static int lupCRLCriticalDimension = 36; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public SymmetricMatrix(double[][] a) { |
| 43 |
super(a); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public SymmetricMatrix(int n) throws NegativeArraySizeException { |
| 52 |
super(n, n); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public SymmetricMatrix(int n, int m) throws NegativeArraySizeException { |
| 64 |
super(n, m); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public SymmetricMatrix add(SymmetricMatrix a) |
| 74 |
throws IllegalDimension { |
| 75 |
return new SymmetricMatrix(addComponents(a)); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 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 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 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 |
|
| 129 |
|
| 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 |
|
| 142 |
|
| 143 |
|
| 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 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 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 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 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 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 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 |
|
| 207 |
|
| 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 |
|
| 222 |
|
| 223 |
|
| 224 |
public Matrix product(double a) { |
| 225 |
return new SymmetricMatrix(productComponents(a)); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public SymmetricMatrix product(SymmetricMatrix a) throws IllegalDimension { |
| 236 |
return new SymmetricMatrix(productComponents(a)); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 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 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
protected SymmetricMatrix secureAdd(SymmetricMatrix a) { |
| 265 |
return new SymmetricMatrix(addComponents(a)); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
protected SymmetricMatrix secureProduct(SymmetricMatrix a) { |
| 275 |
return new SymmetricMatrix(productComponents(a)); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
protected SymmetricMatrix secureSubtract(SymmetricMatrix a) { |
| 285 |
return new SymmetricMatrix(subtractComponents(a)); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 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 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
public SymmetricMatrix subtract(SymmetricMatrix a) |
| 323 |
throws IllegalDimension { |
| 324 |
return new SymmetricMatrix(subtractComponents(a)); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public Matrix transpose() { |
| 331 |
return this; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 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 |
} |