| 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 |
import cern.colt.matrix.DoubleFactory1D; |
| 30 |
import cern.colt.matrix.DoubleFactory2D; |
| 31 |
import cern.colt.matrix.DoubleMatrix1D; |
| 32 |
import cern.colt.matrix.DoubleMatrix2D; |
| 33 |
import cern.colt.matrix.linalg.Property; |
| 34 |
import dr.math.MathUtils; |
| 35 |
|
| 36 |
public class RobustEigenDecomposition implements java.io.Serializable { |
| 37 |
static final long serialVersionUID = 1020; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
private int n; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private boolean issymmetric; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
private double[] d, e; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
private double[][] V; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
private double[][] H; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
private double[] ort; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
private transient double cdivr, cdivi; |
| 71 |
|
| 72 |
|
| 73 |
private int maxIterations; |
| 74 |
|
| 75 |
static private int maxIterationsDefault = 100000; |
| 76 |
static private String ERROR_STRING = "Eigendecomposition is not converged."; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public RobustEigenDecomposition(DoubleMatrix2D A) throws ArithmeticException { |
| 87 |
this(A,maxIterationsDefault); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
public RobustEigenDecomposition(DoubleMatrix2D A, int maxIterations) throws ArithmeticException { |
| 92 |
|
| 93 |
Property.DEFAULT.checkSquare(A); |
| 94 |
|
| 95 |
this.maxIterations = maxIterations; |
| 96 |
|
| 97 |
n = A.columns(); |
| 98 |
V = new double[n][n]; |
| 99 |
d = new double[n]; |
| 100 |
e = new double[n]; |
| 101 |
|
| 102 |
issymmetric = Property.DEFAULT.isSymmetric(A); |
| 103 |
|
| 104 |
if (issymmetric) { |
| 105 |
for (int i = 0; i < n; i++) { |
| 106 |
for (int j = 0; j < n; j++) { |
| 107 |
V[i][j] = A.getQuick(i,j); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
tred2(); |
| 113 |
|
| 114 |
|
| 115 |
tql2(); |
| 116 |
|
| 117 |
} |
| 118 |
else { |
| 119 |
H = new double[n][n]; |
| 120 |
ort = new double[n]; |
| 121 |
|
| 122 |
for (int j = 0; j < n; j++) { |
| 123 |
for (int i = 0; i < n; i++) { |
| 124 |
H[i][j] = A.getQuick(i,j); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
orthes(); |
| 130 |
|
| 131 |
|
| 132 |
hqr2(); |
| 133 |
} |
| 134 |
} |
| 135 |
private void cdiv(double xr, double xi, double yr, double yi) { |
| 136 |
double r,d; |
| 137 |
if (Math.abs(yr) > Math.abs(yi)) { |
| 138 |
r = yi/yr; |
| 139 |
d = yr + r*yi; |
| 140 |
cdivr = (xr + r*xi)/d; |
| 141 |
cdivi = (xi - r*xr)/d; |
| 142 |
} |
| 143 |
else { |
| 144 |
r = yr/yi; |
| 145 |
d = yi + r*yr; |
| 146 |
cdivr = (r*xr + xi)/d; |
| 147 |
cdivi = (r*xi - xr)/d; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public DoubleMatrix2D getD() { |
| 155 |
double[][] D = new double[n][n]; |
| 156 |
for (int i = 0; i < n; i++) { |
| 157 |
for (int j = 0; j < n; j++) { |
| 158 |
D[i][j] = 0.0; |
| 159 |
} |
| 160 |
D[i][i] = d[i]; |
| 161 |
if (e[i] > 0) { |
| 162 |
D[i][i+1] = e[i]; |
| 163 |
} |
| 164 |
else if (e[i] < 0) { |
| 165 |
D[i][i-1] = e[i]; |
| 166 |
} |
| 167 |
} |
| 168 |
return DoubleFactory2D.dense.make(D); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public DoubleMatrix1D getImagEigenvalues () { |
| 175 |
return DoubleFactory1D.dense.make(e); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public DoubleMatrix1D getRealEigenvalues () { |
| 182 |
return DoubleFactory1D.dense.make(d); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public DoubleMatrix2D getV () { |
| 189 |
return DoubleFactory2D.dense.make(V); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
private void hqr2() throws ArithmeticException { |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
int nn = this.n; |
| 203 |
int n = nn-1; |
| 204 |
int low = 0; |
| 205 |
int high = nn-1; |
| 206 |
double eps = Math.pow(2.0,-52.0); |
| 207 |
double exshift = 0.0; |
| 208 |
double p=0,q=0,r=0,s=0,z=0,t,w,x,y; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
double norm = 0.0; |
| 213 |
for (int i = 0; i < nn; i++) { |
| 214 |
if (i < low | i > high) { |
| 215 |
d[i] = H[i][i]; |
| 216 |
e[i] = 0.0; |
| 217 |
} |
| 218 |
for (int j = Math.max(i-1,0); j < nn; j++) { |
| 219 |
norm = norm + Math.abs(H[i][j]); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
int iter = 0; |
| 226 |
while (n >= low) { |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
int l = n; |
| 231 |
while (l > low) { |
| 232 |
s = Math.abs(H[l-1][l-1]) + Math.abs(H[l][l]); |
| 233 |
if (s == 0.0) { |
| 234 |
s = norm; |
| 235 |
} |
| 236 |
if (Math.abs(H[l][l-1]) < eps * s) { |
| 237 |
break; |
| 238 |
} |
| 239 |
l--; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
if (l == n) { |
| 246 |
H[n][n] = H[n][n] + exshift; |
| 247 |
d[n] = H[n][n]; |
| 248 |
e[n] = 0.0; |
| 249 |
n--; |
| 250 |
iter = 0; |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
} else if (l == n-1) { |
| 255 |
w = H[n][n-1] * H[n-1][n]; |
| 256 |
p = (H[n-1][n-1] - H[n][n]) / 2.0; |
| 257 |
q = p * p + w; |
| 258 |
z = Math.sqrt(Math.abs(q)); |
| 259 |
H[n][n] = H[n][n] + exshift; |
| 260 |
H[n-1][n-1] = H[n-1][n-1] + exshift; |
| 261 |
x = H[n][n]; |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
if (q >= 0) { |
| 266 |
if (p >= 0) { |
| 267 |
z = p + z; |
| 268 |
} else { |
| 269 |
z = p - z; |
| 270 |
} |
| 271 |
d[n-1] = x + z; |
| 272 |
d[n] = d[n-1]; |
| 273 |
if (z != 0.0) { |
| 274 |
d[n] = x - w / z; |
| 275 |
} |
| 276 |
e[n-1] = 0.0; |
| 277 |
e[n] = 0.0; |
| 278 |
x = H[n][n-1]; |
| 279 |
s = Math.abs(x) + Math.abs(z); |
| 280 |
p = x / s; |
| 281 |
q = z / s; |
| 282 |
r = Math.sqrt(p * p+q * q); |
| 283 |
p = p / r; |
| 284 |
q = q / r; |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
for (int j = n-1; j < nn; j++) { |
| 289 |
z = H[n-1][j]; |
| 290 |
H[n-1][j] = q * z + p * H[n][j]; |
| 291 |
H[n][j] = q * H[n][j] - p * z; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
for (int i = 0; i <= n; i++) { |
| 297 |
z = H[i][n-1]; |
| 298 |
H[i][n-1] = q * z + p * H[i][n]; |
| 299 |
H[i][n] = q * H[i][n] - p * z; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
for (int i = low; i <= high; i++) { |
| 305 |
z = V[i][n-1]; |
| 306 |
V[i][n-1] = q * z + p * V[i][n]; |
| 307 |
V[i][n] = q * V[i][n] - p * z; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
} else { |
| 313 |
d[n-1] = x + p; |
| 314 |
d[n] = x + p; |
| 315 |
e[n-1] = z; |
| 316 |
e[n] = -z; |
| 317 |
} |
| 318 |
n = n - 2; |
| 319 |
iter = 0; |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
} else { |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
x = H[n][n]; |
| 328 |
y = 0.0; |
| 329 |
w = 0.0; |
| 330 |
if (l < n) { |
| 331 |
y = H[n-1][n-1]; |
| 332 |
w = H[n][n-1] * H[n-1][n]; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
if (iter == 10) { |
| 338 |
exshift += x; |
| 339 |
for (int i = low; i <= n; i++) { |
| 340 |
H[i][i] -= x; |
| 341 |
} |
| 342 |
s = Math.abs(H[n][n-1]) + Math.abs(H[n-1][n-2]); |
| 343 |
x = y = 0.75 * s; |
| 344 |
w = -0.4375 * s * s; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
if (iter == 30) { |
| 350 |
s = (y - x) / 2.0; |
| 351 |
s = s * s + w; |
| 352 |
if (s > 0) { |
| 353 |
s = Math.sqrt(s); |
| 354 |
if (y < x) { |
| 355 |
s = -s; |
| 356 |
} |
| 357 |
s = x - w / ((y - x) / 2.0 + s); |
| 358 |
for (int i = low; i <= n; i++) { |
| 359 |
H[i][i] -= s; |
| 360 |
} |
| 361 |
exshift += s; |
| 362 |
x = y = w = 0.964; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
iter = iter + 1; |
| 367 |
if(iter > maxIterations) |
| 368 |
throw new ArithmeticException(ERROR_STRING); |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
int m = n-2; |
| 373 |
while (m >= l) { |
| 374 |
z = H[m][m]; |
| 375 |
r = x - z; |
| 376 |
s = y - z; |
| 377 |
p = (r * s - w) / H[m+1][m] + H[m][m+1]; |
| 378 |
q = H[m+1][m+1] - z - r - s; |
| 379 |
r = H[m+2][m+1]; |
| 380 |
s = Math.abs(p) + Math.abs(q) + Math.abs(r); |
| 381 |
p = p / s; |
| 382 |
q = q / s; |
| 383 |
r = r / s; |
| 384 |
if (m == l) { |
| 385 |
break; |
| 386 |
} |
| 387 |
if (Math.abs(H[m][m-1]) * (Math.abs(q) + Math.abs(r)) < |
| 388 |
eps * (Math.abs(p) * (Math.abs(H[m-1][m-1]) + Math.abs(z) + |
| 389 |
Math.abs(H[m+1][m+1])))) { |
| 390 |
break; |
| 391 |
} |
| 392 |
m--; |
| 393 |
} |
| 394 |
|
| 395 |
for (int i = m+2; i <= n; i++) { |
| 396 |
H[i][i-2] = 0.0; |
| 397 |
if (i > m+2) { |
| 398 |
H[i][i-3] = 0.0; |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
for (int k = m; k <= n-1; k++) { |
| 405 |
boolean notlast = (k != n-1); |
| 406 |
if (k != m) { |
| 407 |
p = H[k][k-1]; |
| 408 |
q = H[k+1][k-1]; |
| 409 |
r = (notlast ? H[k+2][k-1] : 0.0); |
| 410 |
x = Math.abs(p) + Math.abs(q) + Math.abs(r); |
| 411 |
if (x != 0.0) { |
| 412 |
p = p / x; |
| 413 |
q = q / x; |
| 414 |
r = r / x; |
| 415 |
} |
| 416 |
} |
| 417 |
if (x == 0.0) { |
| 418 |
break; |
| 419 |
} |
| 420 |
s = Math.sqrt(p * p + q * q + r * r); |
| 421 |
if (p < 0) { |
| 422 |
s = -s; |
| 423 |
} |
| 424 |
if (s != 0) { |
| 425 |
if (k != m) { |
| 426 |
H[k][k-1] = -s * x; |
| 427 |
} else if (l != m) { |
| 428 |
H[k][k-1] = -H[k][k-1]; |
| 429 |
} |
| 430 |
p = p + s; |
| 431 |
x = p / s; |
| 432 |
y = q / s; |
| 433 |
z = r / s; |
| 434 |
q = q / p; |
| 435 |
r = r / p; |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
for (int j = k; j < nn; j++) { |
| 440 |
p = H[k][j] + q * H[k+1][j]; |
| 441 |
if (notlast) { |
| 442 |
p = p + r * H[k+2][j]; |
| 443 |
H[k+2][j] = H[k+2][j] - p * z; |
| 444 |
} |
| 445 |
H[k][j] = H[k][j] - p * x; |
| 446 |
H[k+1][j] = H[k+1][j] - p * y; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
for (int i = 0; i <= Math.min(n,k+3); i++) { |
| 452 |
p = x * H[i][k] + y * H[i][k+1]; |
| 453 |
if (notlast) { |
| 454 |
p = p + z * H[i][k+2]; |
| 455 |
H[i][k+2] = H[i][k+2] - p * r; |
| 456 |
} |
| 457 |
H[i][k] = H[i][k] - p; |
| 458 |
H[i][k+1] = H[i][k+1] - p * q; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
for (int i = low; i <= high; i++) { |
| 464 |
p = x * V[i][k] + y * V[i][k+1]; |
| 465 |
if (notlast) { |
| 466 |
p = p + z * V[i][k+2]; |
| 467 |
V[i][k+2] = V[i][k+2] - p * r; |
| 468 |
} |
| 469 |
V[i][k] = V[i][k] - p; |
| 470 |
V[i][k+1] = V[i][k+1] - p * q; |
| 471 |
} |
| 472 |
} |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
if (norm == 0.0) { |
| 480 |
return; |
| 481 |
} |
| 482 |
|
| 483 |
for (n = nn-1; n >= 0; n--) { |
| 484 |
p = d[n]; |
| 485 |
q = e[n]; |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
if (q == 0) { |
| 490 |
int l = n; |
| 491 |
H[n][n] = 1.0; |
| 492 |
for (int i = n-1; i >= 0; i--) { |
| 493 |
w = H[i][i] - p; |
| 494 |
r = 0.0; |
| 495 |
for (int j = l; j <= n; j++) { |
| 496 |
r = r + H[i][j] * H[j][n]; |
| 497 |
} |
| 498 |
if (e[i] < 0.0) { |
| 499 |
z = w; |
| 500 |
s = r; |
| 501 |
} else { |
| 502 |
l = i; |
| 503 |
if (e[i] == 0.0) { |
| 504 |
if (w != 0.0) { |
| 505 |
H[i][n] = -r / w; |
| 506 |
} else { |
| 507 |
H[i][n] = -r / (eps * norm); |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
} else { |
| 513 |
x = H[i][i+1]; |
| 514 |
y = H[i+1][i]; |
| 515 |
q = (d[i] - p) * (d[i] - p) + e[i] * e[i]; |
| 516 |
t = (x * s - z * r) / q; |
| 517 |
H[i][n] = t; |
| 518 |
if (Math.abs(x) > Math.abs(z)) { |
| 519 |
H[i+1][n] = (-r - w * t) / x; |
| 520 |
} else { |
| 521 |
H[i+1][n] = (-s - y * t) / z; |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
t = Math.abs(H[i][n]); |
| 528 |
if ((eps * t) * t > 1) { |
| 529 |
for (int j = i; j <= n; j++) { |
| 530 |
H[j][n] = H[j][n] / t; |
| 531 |
} |
| 532 |
} |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
} else if (q < 0) { |
| 539 |
int l = n-1; |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
if (Math.abs(H[n][n-1]) > Math.abs(H[n-1][n])) { |
| 544 |
H[n-1][n-1] = q / H[n][n-1]; |
| 545 |
H[n-1][n] = -(H[n][n] - p) / H[n][n-1]; |
| 546 |
} else { |
| 547 |
cdiv(0.0,-H[n-1][n],H[n-1][n-1]-p,q); |
| 548 |
H[n-1][n-1] = cdivr; |
| 549 |
H[n-1][n] = cdivi; |
| 550 |
} |
| 551 |
H[n][n-1] = 0.0; |
| 552 |
H[n][n] = 1.0; |
| 553 |
for (int i = n-2; i >= 0; i--) { |
| 554 |
double ra,sa,vr,vi; |
| 555 |
ra = 0.0; |
| 556 |
sa = 0.0; |
| 557 |
for (int j = l; j <= n; j++) { |
| 558 |
ra = ra + H[i][j] * H[j][n-1]; |
| 559 |
sa = sa + H[i][j] * H[j][n]; |
| 560 |
} |
| 561 |
w = H[i][i] - p; |
| 562 |
|
| 563 |
if (e[i] < 0.0) { |
| 564 |
z = w; |
| 565 |
r = ra; |
| 566 |
s = sa; |
| 567 |
} else { |
| 568 |
l = i; |
| 569 |
if (e[i] == 0) { |
| 570 |
cdiv(-ra,-sa,w,q); |
| 571 |
H[i][n-1] = cdivr; |
| 572 |
H[i][n] = cdivi; |
| 573 |
} else { |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
x = H[i][i+1]; |
| 578 |
y = H[i+1][i]; |
| 579 |
vr = (d[i] - p) * (d[i] - p) + e[i] * e[i] - q * q; |
| 580 |
vi = (d[i] - p) * 2.0 * q; |
| 581 |
if (vr == 0.0 & vi == 0.0) { |
| 582 |
vr = eps * norm * (Math.abs(w) + Math.abs(q) + |
| 583 |
Math.abs(x) + Math.abs(y) + Math.abs(z)); |
| 584 |
} |
| 585 |
cdiv(x*r-z*ra+q*sa,x*s-z*sa-q*ra,vr,vi); |
| 586 |
H[i][n-1] = cdivr; |
| 587 |
H[i][n] = cdivi; |
| 588 |
if (Math.abs(x) > (Math.abs(z) + Math.abs(q))) { |
| 589 |
H[i+1][n-1] = (-ra - w * H[i][n-1] + q * H[i][n]) / x; |
| 590 |
H[i+1][n] = (-sa - w * H[i][n] - q * H[i][n-1]) / x; |
| 591 |
} else { |
| 592 |
cdiv(-r-y*H[i][n-1],-s-y*H[i][n],z,q); |
| 593 |
H[i+1][n-1] = cdivr; |
| 594 |
H[i+1][n] = cdivi; |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
t = Math.max(Math.abs(H[i][n-1]),Math.abs(H[i][n])); |
| 601 |
if ((eps * t) * t > 1) { |
| 602 |
for (int j = i; j <= n; j++) { |
| 603 |
H[j][n-1] = H[j][n-1] / t; |
| 604 |
H[j][n] = H[j][n] / t; |
| 605 |
} |
| 606 |
} |
| 607 |
} |
| 608 |
} |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
for (int i = 0; i < nn; i++) { |
| 615 |
if (i < low | i > high) { |
| 616 |
for (int j = i; j < nn; j++) { |
| 617 |
V[i][j] = H[i][j]; |
| 618 |
} |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
for (int j = nn-1; j >= low; j--) { |
| 625 |
for (int i = low; i <= high; i++) { |
| 626 |
z = 0.0; |
| 627 |
for (int k = low; k <= Math.min(j,high); k++) { |
| 628 |
z = z + V[i][k] * H[k][j]; |
| 629 |
} |
| 630 |
V[i][j] = z; |
| 631 |
} |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
private void orthes () { |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
int low = 0; |
| 644 |
int high = n-1; |
| 645 |
|
| 646 |
for (int m = low+1; m <= high-1; m++) { |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
double scale = 0.0; |
| 651 |
for (int i = m; i <= high; i++) { |
| 652 |
scale = scale + Math.abs(H[i][m-1]); |
| 653 |
} |
| 654 |
if (scale != 0.0) { |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
double h = 0.0; |
| 659 |
for (int i = high; i >= m; i--) { |
| 660 |
ort[i] = H[i][m-1]/scale; |
| 661 |
h += ort[i] * ort[i]; |
| 662 |
} |
| 663 |
double g = Math.sqrt(h); |
| 664 |
if (ort[m] > 0) { |
| 665 |
g = -g; |
| 666 |
} |
| 667 |
h = h - ort[m] * g; |
| 668 |
ort[m] = ort[m] - g; |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
for (int j = m; j < n; j++) { |
| 674 |
double f = 0.0; |
| 675 |
for (int i = high; i >= m; i--) { |
| 676 |
f += ort[i]*H[i][j]; |
| 677 |
} |
| 678 |
f = f/h; |
| 679 |
for (int i = m; i <= high; i++) { |
| 680 |
H[i][j] -= f*ort[i]; |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
for (int i = 0; i <= high; i++) { |
| 685 |
double f = 0.0; |
| 686 |
for (int j = high; j >= m; j--) { |
| 687 |
f += ort[j]*H[i][j]; |
| 688 |
} |
| 689 |
f = f/h; |
| 690 |
for (int j = m; j <= high; j++) { |
| 691 |
H[i][j] -= f*ort[j]; |
| 692 |
} |
| 693 |
} |
| 694 |
ort[m] = scale*ort[m]; |
| 695 |
H[m][m-1] = scale*g; |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
for (int i = 0; i < n; i++) { |
| 702 |
for (int j = 0; j < n; j++) { |
| 703 |
V[i][j] = (i == j ? 1.0 : 0.0); |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
for (int m = high-1; m >= low+1; m--) { |
| 708 |
if (H[m][m-1] != 0.0) { |
| 709 |
for (int i = m+1; i <= high; i++) { |
| 710 |
ort[i] = H[i][m-1]; |
| 711 |
} |
| 712 |
for (int j = m; j <= high; j++) { |
| 713 |
double g = 0.0; |
| 714 |
for (int i = m; i <= high; i++) { |
| 715 |
g += ort[i] * V[i][j]; |
| 716 |
} |
| 717 |
|
| 718 |
g = (g / ort[m]) / H[m][m-1]; |
| 719 |
for (int i = m; i <= high; i++) { |
| 720 |
V[i][j] += g * ort[i]; |
| 721 |
} |
| 722 |
} |
| 723 |
} |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
public String toString() { |
| 736 |
StringBuffer buf = new StringBuffer(); |
| 737 |
String unknown = "Illegal operation or error: "; |
| 738 |
|
| 739 |
buf.append("---------------------------------------------------------------------\n"); |
| 740 |
buf.append("EigenvalueDecomposition(A) --> D, V, realEigenvalues, imagEigenvalues\n"); |
| 741 |
buf.append("---------------------------------------------------------------------\n"); |
| 742 |
|
| 743 |
buf.append("realEigenvalues = "); |
| 744 |
try { buf.append(String.valueOf(this.getRealEigenvalues()));} |
| 745 |
catch (IllegalArgumentException exc) { buf.append(unknown+exc.getMessage()); } |
| 746 |
|
| 747 |
buf.append("\nimagEigenvalues = "); |
| 748 |
try { buf.append(String.valueOf(this.getImagEigenvalues()));} |
| 749 |
catch (IllegalArgumentException exc) { buf.append(unknown+exc.getMessage()); } |
| 750 |
|
| 751 |
buf.append("\n\nD = "); |
| 752 |
try { buf.append(String.valueOf(this.getD()));} |
| 753 |
catch (IllegalArgumentException exc) { buf.append(unknown+exc.getMessage()); } |
| 754 |
|
| 755 |
buf.append("\n\nV = "); |
| 756 |
try { buf.append(String.valueOf(this.getV()));} |
| 757 |
catch (IllegalArgumentException exc) { buf.append(unknown+exc.getMessage()); } |
| 758 |
|
| 759 |
return buf.toString(); |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
private void tql2 () throws ArithmeticException { |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
for (int i = 1; i < n; i++) { |
| 772 |
e[i-1] = e[i]; |
| 773 |
} |
| 774 |
e[n-1] = 0.0; |
| 775 |
|
| 776 |
double f = 0.0; |
| 777 |
double tst1 = 0.0; |
| 778 |
double eps = Math.pow(2.0,-52.0); |
| 779 |
for (int l = 0; l < n; l++) { |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
tst1 = Math.max(tst1,Math.abs(d[l]) + Math.abs(e[l])); |
| 784 |
int m = l; |
| 785 |
while (m < n) { |
| 786 |
if (Math.abs(e[m]) <= eps*tst1) { |
| 787 |
break; |
| 788 |
} |
| 789 |
m++; |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
if (m > l) { |
| 796 |
int iter = 0; |
| 797 |
do { |
| 798 |
iter = iter + 1; |
| 799 |
if( iter > maxIterations) |
| 800 |
throw new ArithmeticException(ERROR_STRING); |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
double g = d[l]; |
| 805 |
double p = (d[l+1] - g) / (2.0 * e[l]); |
| 806 |
double r = MathUtils.hypot(p,1.0); |
| 807 |
if (p < 0) { |
| 808 |
r = -r; |
| 809 |
} |
| 810 |
d[l] = e[l] / (p + r); |
| 811 |
d[l+1] = e[l] * (p + r); |
| 812 |
double dl1 = d[l+1]; |
| 813 |
double h = g - d[l]; |
| 814 |
for (int i = l+2; i < n; i++) { |
| 815 |
d[i] -= h; |
| 816 |
} |
| 817 |
f = f + h; |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
p = d[m]; |
| 822 |
double c = 1.0; |
| 823 |
double c2 = c; |
| 824 |
double c3 = c; |
| 825 |
double el1 = e[l+1]; |
| 826 |
double s = 0.0; |
| 827 |
double s2 = 0.0; |
| 828 |
for (int i = m-1; i >= l; i--) { |
| 829 |
c3 = c2; |
| 830 |
c2 = c; |
| 831 |
s2 = s; |
| 832 |
g = c * e[i]; |
| 833 |
h = c * p; |
| 834 |
r = MathUtils.hypot(p,e[i]); |
| 835 |
e[i+1] = s * r; |
| 836 |
s = e[i] / r; |
| 837 |
c = p / r; |
| 838 |
p = c * d[i] - s * g; |
| 839 |
d[i+1] = h + s * (c * g + s * d[i]); |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
for (int k = 0; k < n; k++) { |
| 844 |
h = V[k][i+1]; |
| 845 |
V[k][i+1] = s * V[k][i] + c * h; |
| 846 |
V[k][i] = c * V[k][i] - s * h; |
| 847 |
} |
| 848 |
} |
| 849 |
p = -s * s2 * c3 * el1 * e[l] / dl1; |
| 850 |
e[l] = s * p; |
| 851 |
d[l] = c * p; |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
} while (Math.abs(e[l]) > eps*tst1); |
| 856 |
} |
| 857 |
d[l] = d[l] + f; |
| 858 |
e[l] = 0.0; |
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
for (int i = 0; i < n-1; i++) { |
| 864 |
int k = i; |
| 865 |
double p = d[i]; |
| 866 |
for (int j = i+1; j < n; j++) { |
| 867 |
if (d[j] < p) { |
| 868 |
k = j; |
| 869 |
p = d[j]; |
| 870 |
} |
| 871 |
} |
| 872 |
if (k != i) { |
| 873 |
d[k] = d[i]; |
| 874 |
d[i] = p; |
| 875 |
for (int j = 0; j < n; j++) { |
| 876 |
p = V[j][i]; |
| 877 |
V[j][i] = V[j][k]; |
| 878 |
V[j][k] = p; |
| 879 |
} |
| 880 |
} |
| 881 |
} |
| 882 |
} |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
private void tred2 () { |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
for (int j = 0; j < n; j++) { |
| 894 |
d[j] = V[n-1][j]; |
| 895 |
} |
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
for (int i = n-1; i > 0; i--) { |
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
double scale = 0.0; |
| 905 |
double h = 0.0; |
| 906 |
for (int k = 0; k < i; k++) { |
| 907 |
scale = scale + Math.abs(d[k]); |
| 908 |
} |
| 909 |
if (scale == 0.0) { |
| 910 |
e[i] = d[i-1]; |
| 911 |
for (int j = 0; j < i; j++) { |
| 912 |
d[j] = V[i-1][j]; |
| 913 |
V[i][j] = 0.0; |
| 914 |
V[j][i] = 0.0; |
| 915 |
} |
| 916 |
} else { |
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
for (int k = 0; k < i; k++) { |
| 921 |
d[k] /= scale; |
| 922 |
h += d[k] * d[k]; |
| 923 |
} |
| 924 |
double f = d[i-1]; |
| 925 |
double g = Math.sqrt(h); |
| 926 |
if (f > 0) { |
| 927 |
g = -g; |
| 928 |
} |
| 929 |
e[i] = scale * g; |
| 930 |
h = h - f * g; |
| 931 |
d[i-1] = f - g; |
| 932 |
for (int j = 0; j < i; j++) { |
| 933 |
e[j] = 0.0; |
| 934 |
} |
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
for (int j = 0; j < i; j++) { |
| 939 |
f = d[j]; |
| 940 |
V[j][i] = f; |
| 941 |
g = e[j] + V[j][j] * f; |
| 942 |
for (int k = j+1; k <= i-1; k++) { |
| 943 |
g += V[k][j] * d[k]; |
| 944 |
e[k] += V[k][j] * f; |
| 945 |
} |
| 946 |
e[j] = g; |
| 947 |
} |
| 948 |
f = 0.0; |
| 949 |
for (int j = 0; j < i; j++) { |
| 950 |
e[j] /= h; |
| 951 |
f += e[j] * d[j]; |
| 952 |
} |
| 953 |
double hh = f / (h + h); |
| 954 |
for (int j = 0; j < i; j++) { |
| 955 |
e[j] -= hh * d[j]; |
| 956 |
} |
| 957 |
for (int j = 0; j < i; j++) { |
| 958 |
f = d[j]; |
| 959 |
g = e[j]; |
| 960 |
for (int k = j; k <= i-1; k++) { |
| 961 |
V[k][j] -= (f * e[k] + g * d[k]); |
| 962 |
} |
| 963 |
d[j] = V[i-1][j]; |
| 964 |
V[i][j] = 0.0; |
| 965 |
} |
| 966 |
} |
| 967 |
d[i] = h; |
| 968 |
} |
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
for (int i = 0; i < n-1; i++) { |
| 973 |
V[n-1][i] = V[i][i]; |
| 974 |
V[i][i] = 1.0; |
| 975 |
double h = d[i+1]; |
| 976 |
if (h != 0.0) { |
| 977 |
for (int k = 0; k <= i; k++) { |
| 978 |
d[k] = V[k][i+1] / h; |
| 979 |
} |
| 980 |
for (int j = 0; j <= i; j++) { |
| 981 |
double g = 0.0; |
| 982 |
for (int k = 0; k <= i; k++) { |
| 983 |
g += V[k][i+1] * V[k][j]; |
| 984 |
} |
| 985 |
for (int k = 0; k <= i; k++) { |
| 986 |
V[k][j] -= g * d[k]; |
| 987 |
} |
| 988 |
} |
| 989 |
} |
| 990 |
for (int k = 0; k <= i; k++) { |
| 991 |
V[k][i+1] = 0.0; |
| 992 |
} |
| 993 |
} |
| 994 |
for (int j = 0; j < n; j++) { |
| 995 |
d[j] = V[n-1][j]; |
| 996 |
V[n-1][j] = 0.0; |
| 997 |
} |
| 998 |
V[n-1][n-1] = 1.0; |
| 999 |
e[0] = 0.0; |
| 1000 |
} |
| 1001 |
|
| 1002 |
} |