source: src/main/java/agents/Jama/EigenvalueDecomposition.java

Last change on this file was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 26.2 KB
Line 
1package agents.Jama;
2import agents.Jama.util.*;
3
4/** Eigenvalues and eigenvectors of a real matrix.
5<P>
6 If A is symmetric, then A = V*D*V' where the eigenvalue matrix D is
7 diagonal and the eigenvector matrix V is orthogonal.
8 I.e. A = V.times(D.times(V.transpose())) and
9 V.times(V.transpose()) equals the identity matrix.
10<P>
11 If A is not symmetric, then the eigenvalue matrix D is block diagonal
12 with the real eigenvalues in 1-by-1 blocks and any complex eigenvalues,
13 lambda + i*mu, in 2-by-2 blocks, [lambda, mu; -mu, lambda]. The
14 columns of V represent the eigenvectors in the sense that A*V = V*D,
15 i.e. A.times(V) equals V.times(D). The matrix V may be badly
16 conditioned, or even singular, so the validity of the equation
17 A = V*D*inverse(V) depends upon V.cond().
18**/
19
20public class EigenvalueDecomposition implements java.io.Serializable {
21
22/* ------------------------
23 Class variables
24 * ------------------------ */
25
26 /** Row and column dimension (square matrix).
27 @serial matrix dimension.
28 */
29 private int n;
30
31 /** Symmetry flag.
32 @serial internal symmetry flag.
33 */
34 private boolean issymmetric;
35
36 /** Arrays for internal storage of eigenvalues.
37 @serial internal storage of eigenvalues.
38 */
39 private double[] d, e;
40
41 /** Array for internal storage of eigenvectors.
42 @serial internal storage of eigenvectors.
43 */
44 private double[][] V;
45
46 /** Array for internal storage of nonsymmetric Hessenberg form.
47 @serial internal storage of nonsymmetric Hessenberg form.
48 */
49 private double[][] H;
50
51 /** Working storage for nonsymmetric algorithm.
52 @serial working storage for nonsymmetric algorithm.
53 */
54 private double[] ort;
55
56/* ------------------------
57 Private Methods
58 * ------------------------ */
59
60 // Symmetric Householder reduction to tridiagonal form.
61
62 private void tred2 () {
63
64 // This is derived from the Algol procedures tred2 by
65 // Bowdler, Martin, Reinsch, and Wilkinson, Handbook for
66 // Auto. Comp., Vol.ii-Linear Algebra, and the corresponding
67 // Fortran subroutine in EISPACK.
68
69 for (int j = 0; j < n; j++) {
70 d[j] = V[n-1][j];
71 }
72
73 // Householder reduction to tridiagonal form.
74
75 for (int i = n-1; i > 0; i--) {
76
77 // Scale to avoid under/overflow.
78
79 double scale = 0.0;
80 double h = 0.0;
81 for (int k = 0; k < i; k++) {
82 scale = scale + Math.abs(d[k]);
83 }
84 if (scale == 0.0) {
85 e[i] = d[i-1];
86 for (int j = 0; j < i; j++) {
87 d[j] = V[i-1][j];
88 V[i][j] = 0.0;
89 V[j][i] = 0.0;
90 }
91 } else {
92
93 // Generate Householder vector.
94
95 for (int k = 0; k < i; k++) {
96 d[k] /= scale;
97 h += d[k] * d[k];
98 }
99 double f = d[i-1];
100 double g = Math.sqrt(h);
101 if (f > 0) {
102 g = -g;
103 }
104 e[i] = scale * g;
105 h = h - f * g;
106 d[i-1] = f - g;
107 for (int j = 0; j < i; j++) {
108 e[j] = 0.0;
109 }
110
111 // Apply similarity transformation to remaining columns.
112
113 for (int j = 0; j < i; j++) {
114 f = d[j];
115 V[j][i] = f;
116 g = e[j] + V[j][j] * f;
117 for (int k = j+1; k <= i-1; k++) {
118 g += V[k][j] * d[k];
119 e[k] += V[k][j] * f;
120 }
121 e[j] = g;
122 }
123 f = 0.0;
124 for (int j = 0; j < i; j++) {
125 e[j] /= h;
126 f += e[j] * d[j];
127 }
128 double hh = f / (h + h);
129 for (int j = 0; j < i; j++) {
130 e[j] -= hh * d[j];
131 }
132 for (int j = 0; j < i; j++) {
133 f = d[j];
134 g = e[j];
135 for (int k = j; k <= i-1; k++) {
136 V[k][j] -= (f * e[k] + g * d[k]);
137 }
138 d[j] = V[i-1][j];
139 V[i][j] = 0.0;
140 }
141 }
142 d[i] = h;
143 }
144
145 // Accumulate transformations.
146
147 for (int i = 0; i < n-1; i++) {
148 V[n-1][i] = V[i][i];
149 V[i][i] = 1.0;
150 double h = d[i+1];
151 if (h != 0.0) {
152 for (int k = 0; k <= i; k++) {
153 d[k] = V[k][i+1] / h;
154 }
155 for (int j = 0; j <= i; j++) {
156 double g = 0.0;
157 for (int k = 0; k <= i; k++) {
158 g += V[k][i+1] * V[k][j];
159 }
160 for (int k = 0; k <= i; k++) {
161 V[k][j] -= g * d[k];
162 }
163 }
164 }
165 for (int k = 0; k <= i; k++) {
166 V[k][i+1] = 0.0;
167 }
168 }
169 for (int j = 0; j < n; j++) {
170 d[j] = V[n-1][j];
171 V[n-1][j] = 0.0;
172 }
173 V[n-1][n-1] = 1.0;
174 e[0] = 0.0;
175 }
176
177 // Symmetric tridiagonal QL algorithm.
178
179 private void tql2 () {
180
181 // This is derived from the Algol procedures tql2, by
182 // Bowdler, Martin, Reinsch, and Wilkinson, Handbook for
183 // Auto. Comp., Vol.ii-Linear Algebra, and the corresponding
184 // Fortran subroutine in EISPACK.
185
186 for (int i = 1; i < n; i++) {
187 e[i-1] = e[i];
188 }
189 e[n-1] = 0.0;
190
191 double f = 0.0;
192 double tst1 = 0.0;
193 double eps = Math.pow(2.0,-52.0);
194 for (int l = 0; l < n; l++) {
195
196 // Find small subdiagonal element
197
198 tst1 = Math.max(tst1,Math.abs(d[l]) + Math.abs(e[l]));
199 int m = l;
200 while (m < n) {
201 if (Math.abs(e[m]) <= eps*tst1) {
202 break;
203 }
204 m++;
205 }
206
207 // If m == l, d[l] is an eigenvalue,
208 // otherwise, iterate.
209
210 if (m > l) {
211 int iter = 0;
212 do {
213 iter = iter + 1; // (Could check iteration count here.)
214
215 // Compute implicit shift
216
217 double g = d[l];
218 double p = (d[l+1] - g) / (2.0 * e[l]);
219 double r = Maths.hypot(p,1.0);
220 if (p < 0) {
221 r = -r;
222 }
223 d[l] = e[l] / (p + r);
224 d[l+1] = e[l] * (p + r);
225 double dl1 = d[l+1];
226 double h = g - d[l];
227 for (int i = l+2; i < n; i++) {
228 d[i] -= h;
229 }
230 f = f + h;
231
232 // Implicit QL transformation.
233
234 p = d[m];
235 double c = 1.0;
236 double c2 = c;
237 double c3 = c;
238 double el1 = e[l+1];
239 double s = 0.0;
240 double s2 = 0.0;
241 for (int i = m-1; i >= l; i--) {
242 c3 = c2;
243 c2 = c;
244 s2 = s;
245 g = c * e[i];
246 h = c * p;
247 r = Maths.hypot(p,e[i]);
248 e[i+1] = s * r;
249 s = e[i] / r;
250 c = p / r;
251 p = c * d[i] - s * g;
252 d[i+1] = h + s * (c * g + s * d[i]);
253
254 // Accumulate transformation.
255
256 for (int k = 0; k < n; k++) {
257 h = V[k][i+1];
258 V[k][i+1] = s * V[k][i] + c * h;
259 V[k][i] = c * V[k][i] - s * h;
260 }
261 }
262 p = -s * s2 * c3 * el1 * e[l] / dl1;
263 e[l] = s * p;
264 d[l] = c * p;
265
266 // Check for convergence.
267
268 } while (Math.abs(e[l]) > eps*tst1);
269 }
270 d[l] = d[l] + f;
271 e[l] = 0.0;
272 }
273
274 // Sort eigenvalues and corresponding vectors.
275
276 for (int i = 0; i < n-1; i++) {
277 int k = i;
278 double p = d[i];
279 for (int j = i+1; j < n; j++) {
280 if (d[j] < p) {
281 k = j;
282 p = d[j];
283 }
284 }
285 if (k != i) {
286 d[k] = d[i];
287 d[i] = p;
288 for (int j = 0; j < n; j++) {
289 p = V[j][i];
290 V[j][i] = V[j][k];
291 V[j][k] = p;
292 }
293 }
294 }
295 }
296
297 // Nonsymmetric reduction to Hessenberg form.
298
299 private void orthes () {
300
301 // This is derived from the Algol procedures orthes and ortran,
302 // by Martin and Wilkinson, Handbook for Auto. Comp.,
303 // Vol.ii-Linear Algebra, and the corresponding
304 // Fortran subroutines in EISPACK.
305
306 int low = 0;
307 int high = n-1;
308
309 for (int m = low+1; m <= high-1; m++) {
310
311 // Scale column.
312
313 double scale = 0.0;
314 for (int i = m; i <= high; i++) {
315 scale = scale + Math.abs(H[i][m-1]);
316 }
317 if (scale != 0.0) {
318
319 // Compute Householder transformation.
320
321 double h = 0.0;
322 for (int i = high; i >= m; i--) {
323 ort[i] = H[i][m-1]/scale;
324 h += ort[i] * ort[i];
325 }
326 double g = Math.sqrt(h);
327 if (ort[m] > 0) {
328 g = -g;
329 }
330 h = h - ort[m] * g;
331 ort[m] = ort[m] - g;
332
333 // Apply Householder similarity transformation
334 // H = (I-u*u'/h)*H*(I-u*u')/h)
335
336 for (int j = m; j < n; j++) {
337 double f = 0.0;
338 for (int i = high; i >= m; i--) {
339 f += ort[i]*H[i][j];
340 }
341 f = f/h;
342 for (int i = m; i <= high; i++) {
343 H[i][j] -= f*ort[i];
344 }
345 }
346
347 for (int i = 0; i <= high; i++) {
348 double f = 0.0;
349 for (int j = high; j >= m; j--) {
350 f += ort[j]*H[i][j];
351 }
352 f = f/h;
353 for (int j = m; j <= high; j++) {
354 H[i][j] -= f*ort[j];
355 }
356 }
357 ort[m] = scale*ort[m];
358 H[m][m-1] = scale*g;
359 }
360 }
361
362 // Accumulate transformations (Algol's ortran).
363
364 for (int i = 0; i < n; i++) {
365 for (int j = 0; j < n; j++) {
366 V[i][j] = (i == j ? 1.0 : 0.0);
367 }
368 }
369
370 for (int m = high-1; m >= low+1; m--) {
371 if (H[m][m-1] != 0.0) {
372 for (int i = m+1; i <= high; i++) {
373 ort[i] = H[i][m-1];
374 }
375 for (int j = m; j <= high; j++) {
376 double g = 0.0;
377 for (int i = m; i <= high; i++) {
378 g += ort[i] * V[i][j];
379 }
380 // Double division avoids possible underflow
381 g = (g / ort[m]) / H[m][m-1];
382 for (int i = m; i <= high; i++) {
383 V[i][j] += g * ort[i];
384 }
385 }
386 }
387 }
388 }
389
390
391 // Complex scalar division.
392
393 private transient double cdivr, cdivi;
394 private void cdiv(double xr, double xi, double yr, double yi) {
395 double r,d;
396 if (Math.abs(yr) > Math.abs(yi)) {
397 r = yi/yr;
398 d = yr + r*yi;
399 cdivr = (xr + r*xi)/d;
400 cdivi = (xi - r*xr)/d;
401 } else {
402 r = yr/yi;
403 d = yi + r*yr;
404 cdivr = (r*xr + xi)/d;
405 cdivi = (r*xi - xr)/d;
406 }
407 }
408
409
410 // Nonsymmetric reduction from Hessenberg to real Schur form.
411
412 private void hqr2 () {
413
414 // This is derived from the Algol procedure hqr2,
415 // by Martin and Wilkinson, Handbook for Auto. Comp.,
416 // Vol.ii-Linear Algebra, and the corresponding
417 // Fortran subroutine in EISPACK.
418
419 // Initialize
420
421 int nn = this.n;
422 int n = nn-1;
423 int low = 0;
424 int high = nn-1;
425 double eps = Math.pow(2.0,-52.0);
426 double exshift = 0.0;
427 double p=0,q=0,r=0,s=0,z=0,t,w,x,y;
428
429 // Store roots isolated by balanc and compute matrix norm
430
431 double norm = 0.0;
432 for (int i = 0; i < nn; i++) {
433 if (i < low | i > high) {
434 d[i] = H[i][i];
435 e[i] = 0.0;
436 }
437 for (int j = Math.max(i-1,0); j < nn; j++) {
438 norm = norm + Math.abs(H[i][j]);
439 }
440 }
441
442 // Outer loop over eigenvalue index
443
444 int iter = 0;
445 while (n >= low) {
446
447 // Look for single small sub-diagonal element
448
449 int l = n;
450 while (l > low) {
451 s = Math.abs(H[l-1][l-1]) + Math.abs(H[l][l]);
452 if (s == 0.0) {
453 s = norm;
454 }
455 if (Math.abs(H[l][l-1]) < eps * s) {
456 break;
457 }
458 l--;
459 }
460
461 // Check for convergence
462 // One root found
463
464 if (l == n) {
465 H[n][n] = H[n][n] + exshift;
466 d[n] = H[n][n];
467 e[n] = 0.0;
468 n--;
469 iter = 0;
470
471 // Two roots found
472
473 } else if (l == n-1) {
474 w = H[n][n-1] * H[n-1][n];
475 p = (H[n-1][n-1] - H[n][n]) / 2.0;
476 q = p * p + w;
477 z = Math.sqrt(Math.abs(q));
478 H[n][n] = H[n][n] + exshift;
479 H[n-1][n-1] = H[n-1][n-1] + exshift;
480 x = H[n][n];
481
482 // Real pair
483
484 if (q >= 0) {
485 if (p >= 0) {
486 z = p + z;
487 } else {
488 z = p - z;
489 }
490 d[n-1] = x + z;
491 d[n] = d[n-1];
492 if (z != 0.0) {
493 d[n] = x - w / z;
494 }
495 e[n-1] = 0.0;
496 e[n] = 0.0;
497 x = H[n][n-1];
498 s = Math.abs(x) + Math.abs(z);
499 p = x / s;
500 q = z / s;
501 r = Math.sqrt(p * p+q * q);
502 p = p / r;
503 q = q / r;
504
505 // Row modification
506
507 for (int j = n-1; j < nn; j++) {
508 z = H[n-1][j];
509 H[n-1][j] = q * z + p * H[n][j];
510 H[n][j] = q * H[n][j] - p * z;
511 }
512
513 // Column modification
514
515 for (int i = 0; i <= n; i++) {
516 z = H[i][n-1];
517 H[i][n-1] = q * z + p * H[i][n];
518 H[i][n] = q * H[i][n] - p * z;
519 }
520
521 // Accumulate transformations
522
523 for (int i = low; i <= high; i++) {
524 z = V[i][n-1];
525 V[i][n-1] = q * z + p * V[i][n];
526 V[i][n] = q * V[i][n] - p * z;
527 }
528
529 // Complex pair
530
531 } else {
532 d[n-1] = x + p;
533 d[n] = x + p;
534 e[n-1] = z;
535 e[n] = -z;
536 }
537 n = n - 2;
538 iter = 0;
539
540 // No convergence yet
541
542 } else {
543
544 // Form shift
545
546 x = H[n][n];
547 y = 0.0;
548 w = 0.0;
549 if (l < n) {
550 y = H[n-1][n-1];
551 w = H[n][n-1] * H[n-1][n];
552 }
553
554 // Wilkinson's original ad hoc shift
555
556 if (iter == 10) {
557 exshift += x;
558 for (int i = low; i <= n; i++) {
559 H[i][i] -= x;
560 }
561 s = Math.abs(H[n][n-1]) + Math.abs(H[n-1][n-2]);
562 x = y = 0.75 * s;
563 w = -0.4375 * s * s;
564 }
565
566 // MATLAB's new ad hoc shift
567
568 if (iter == 30) {
569 s = (y - x) / 2.0;
570 s = s * s + w;
571 if (s > 0) {
572 s = Math.sqrt(s);
573 if (y < x) {
574 s = -s;
575 }
576 s = x - w / ((y - x) / 2.0 + s);
577 for (int i = low; i <= n; i++) {
578 H[i][i] -= s;
579 }
580 exshift += s;
581 x = y = w = 0.964;
582 }
583 }
584
585 iter = iter + 1; // (Could check iteration count here.)
586
587 // Look for two consecutive small sub-diagonal elements
588
589 int m = n-2;
590 while (m >= l) {
591 z = H[m][m];
592 r = x - z;
593 s = y - z;
594 p = (r * s - w) / H[m+1][m] + H[m][m+1];
595 q = H[m+1][m+1] - z - r - s;
596 r = H[m+2][m+1];
597 s = Math.abs(p) + Math.abs(q) + Math.abs(r);
598 p = p / s;
599 q = q / s;
600 r = r / s;
601 if (m == l) {
602 break;
603 }
604 if (Math.abs(H[m][m-1]) * (Math.abs(q) + Math.abs(r)) <
605 eps * (Math.abs(p) * (Math.abs(H[m-1][m-1]) + Math.abs(z) +
606 Math.abs(H[m+1][m+1])))) {
607 break;
608 }
609 m--;
610 }
611
612 for (int i = m+2; i <= n; i++) {
613 H[i][i-2] = 0.0;
614 if (i > m+2) {
615 H[i][i-3] = 0.0;
616 }
617 }
618
619 // Double QR step involving rows l:n and columns m:n
620
621
622 for (int k = m; k <= n-1; k++) {
623 boolean notlast = (k != n-1);
624 if (k != m) {
625 p = H[k][k-1];
626 q = H[k+1][k-1];
627 r = (notlast ? H[k+2][k-1] : 0.0);
628 x = Math.abs(p) + Math.abs(q) + Math.abs(r);
629 if (x == 0.0) {
630 continue;
631 }
632 p = p / x;
633 q = q / x;
634 r = r / x;
635 }
636
637 s = Math.sqrt(p * p + q * q + r * r);
638 if (p < 0) {
639 s = -s;
640 }
641 if (s != 0) {
642 if (k != m) {
643 H[k][k-1] = -s * x;
644 } else if (l != m) {
645 H[k][k-1] = -H[k][k-1];
646 }
647 p = p + s;
648 x = p / s;
649 y = q / s;
650 z = r / s;
651 q = q / p;
652 r = r / p;
653
654 // Row modification
655
656 for (int j = k; j < nn; j++) {
657 p = H[k][j] + q * H[k+1][j];
658 if (notlast) {
659 p = p + r * H[k+2][j];
660 H[k+2][j] = H[k+2][j] - p * z;
661 }
662 H[k][j] = H[k][j] - p * x;
663 H[k+1][j] = H[k+1][j] - p * y;
664 }
665
666 // Column modification
667
668 for (int i = 0; i <= Math.min(n,k+3); i++) {
669 p = x * H[i][k] + y * H[i][k+1];
670 if (notlast) {
671 p = p + z * H[i][k+2];
672 H[i][k+2] = H[i][k+2] - p * r;
673 }
674 H[i][k] = H[i][k] - p;
675 H[i][k+1] = H[i][k+1] - p * q;
676 }
677
678 // Accumulate transformations
679
680 for (int i = low; i <= high; i++) {
681 p = x * V[i][k] + y * V[i][k+1];
682 if (notlast) {
683 p = p + z * V[i][k+2];
684 V[i][k+2] = V[i][k+2] - p * r;
685 }
686 V[i][k] = V[i][k] - p;
687 V[i][k+1] = V[i][k+1] - p * q;
688 }
689 } // (s != 0)
690 } // k loop
691 } // check convergence
692 } // while (n >= low)
693
694 // Backsubstitute to find vectors of upper triangular form
695
696 if (norm == 0.0) {
697 return;
698 }
699
700 for (n = nn-1; n >= 0; n--) {
701 p = d[n];
702 q = e[n];
703
704 // Real vector
705
706 if (q == 0) {
707 int l = n;
708 H[n][n] = 1.0;
709 for (int i = n-1; i >= 0; i--) {
710 w = H[i][i] - p;
711 r = 0.0;
712 for (int j = l; j <= n; j++) {
713 r = r + H[i][j] * H[j][n];
714 }
715 if (e[i] < 0.0) {
716 z = w;
717 s = r;
718 } else {
719 l = i;
720 if (e[i] == 0.0) {
721 if (w != 0.0) {
722 H[i][n] = -r / w;
723 } else {
724 H[i][n] = -r / (eps * norm);
725 }
726
727 // Solve real equations
728
729 } else {
730 x = H[i][i+1];
731 y = H[i+1][i];
732 q = (d[i] - p) * (d[i] - p) + e[i] * e[i];
733 t = (x * s - z * r) / q;
734 H[i][n] = t;
735 if (Math.abs(x) > Math.abs(z)) {
736 H[i+1][n] = (-r - w * t) / x;
737 } else {
738 H[i+1][n] = (-s - y * t) / z;
739 }
740 }
741
742 // Overflow control
743
744 t = Math.abs(H[i][n]);
745 if ((eps * t) * t > 1) {
746 for (int j = i; j <= n; j++) {
747 H[j][n] = H[j][n] / t;
748 }
749 }
750 }
751 }
752
753 // Complex vector
754
755 } else if (q < 0) {
756 int l = n-1;
757
758 // Last vector component imaginary so matrix is triangular
759
760 if (Math.abs(H[n][n-1]) > Math.abs(H[n-1][n])) {
761 H[n-1][n-1] = q / H[n][n-1];
762 H[n-1][n] = -(H[n][n] - p) / H[n][n-1];
763 } else {
764 cdiv(0.0,-H[n-1][n],H[n-1][n-1]-p,q);
765 H[n-1][n-1] = cdivr;
766 H[n-1][n] = cdivi;
767 }
768 H[n][n-1] = 0.0;
769 H[n][n] = 1.0;
770 for (int i = n-2; i >= 0; i--) {
771 double ra,sa,vr,vi;
772 ra = 0.0;
773 sa = 0.0;
774 for (int j = l; j <= n; j++) {
775 ra = ra + H[i][j] * H[j][n-1];
776 sa = sa + H[i][j] * H[j][n];
777 }
778 w = H[i][i] - p;
779
780 if (e[i] < 0.0) {
781 z = w;
782 r = ra;
783 s = sa;
784 } else {
785 l = i;
786 if (e[i] == 0) {
787 cdiv(-ra,-sa,w,q);
788 H[i][n-1] = cdivr;
789 H[i][n] = cdivi;
790 } else {
791
792 // Solve complex equations
793
794 x = H[i][i+1];
795 y = H[i+1][i];
796 vr = (d[i] - p) * (d[i] - p) + e[i] * e[i] - q * q;
797 vi = (d[i] - p) * 2.0 * q;
798 if (vr == 0.0 & vi == 0.0) {
799 vr = eps * norm * (Math.abs(w) + Math.abs(q) +
800 Math.abs(x) + Math.abs(y) + Math.abs(z));
801 }
802 cdiv(x*r-z*ra+q*sa,x*s-z*sa-q*ra,vr,vi);
803 H[i][n-1] = cdivr;
804 H[i][n] = cdivi;
805 if (Math.abs(x) > (Math.abs(z) + Math.abs(q))) {
806 H[i+1][n-1] = (-ra - w * H[i][n-1] + q * H[i][n]) / x;
807 H[i+1][n] = (-sa - w * H[i][n] - q * H[i][n-1]) / x;
808 } else {
809 cdiv(-r-y*H[i][n-1],-s-y*H[i][n],z,q);
810 H[i+1][n-1] = cdivr;
811 H[i+1][n] = cdivi;
812 }
813 }
814
815 // Overflow control
816
817 t = Math.max(Math.abs(H[i][n-1]),Math.abs(H[i][n]));
818 if ((eps * t) * t > 1) {
819 for (int j = i; j <= n; j++) {
820 H[j][n-1] = H[j][n-1] / t;
821 H[j][n] = H[j][n] / t;
822 }
823 }
824 }
825 }
826 }
827 }
828
829 // Vectors of isolated roots
830
831 for (int i = 0; i < nn; i++) {
832 if (i < low | i > high) {
833 for (int j = i; j < nn; j++) {
834 V[i][j] = H[i][j];
835 }
836 }
837 }
838
839 // Back transformation to get eigenvectors of original matrix
840
841 for (int j = nn-1; j >= low; j--) {
842 for (int i = low; i <= high; i++) {
843 z = 0.0;
844 for (int k = low; k <= Math.min(j,high); k++) {
845 z = z + V[i][k] * H[k][j];
846 }
847 V[i][j] = z;
848 }
849 }
850 }
851
852
853/* ------------------------
854 Constructor
855 * ------------------------ */
856
857 /** Check for symmetry, then construct the eigenvalue decomposition
858 Structure to access D and V.
859 @param Arg Square matrix
860 */
861
862 public EigenvalueDecomposition (Matrix Arg) {
863 double[][] A = Arg.getArray();
864 n = Arg.getColumnDimension();
865 V = new double[n][n];
866 d = new double[n];
867 e = new double[n];
868
869 issymmetric = true;
870 for (int j = 0; (j < n) & issymmetric; j++) {
871 for (int i = 0; (i < n) & issymmetric; i++) {
872 issymmetric = (A[i][j] == A[j][i]);
873 }
874 }
875
876 if (issymmetric) {
877 for (int i = 0; i < n; i++) {
878 for (int j = 0; j < n; j++) {
879 V[i][j] = A[i][j];
880 }
881 }
882
883 // Tridiagonalize.
884 tred2();
885
886 // Diagonalize.
887 tql2();
888
889 } else {
890 H = new double[n][n];
891 ort = new double[n];
892
893 for (int j = 0; j < n; j++) {
894 for (int i = 0; i < n; i++) {
895 H[i][j] = A[i][j];
896 }
897 }
898
899 // Reduce to Hessenberg form.
900 orthes();
901
902 // Reduce Hessenberg to real Schur form.
903 hqr2();
904 }
905 }
906
907/* ------------------------
908 Public Methods
909 * ------------------------ */
910
911 /** Return the eigenvector matrix
912 @return V
913 */
914
915 public Matrix getV () {
916 return new Matrix(V,n,n);
917 }
918
919 /** Return the real parts of the eigenvalues
920 @return real(diag(D))
921 */
922
923 public double[] getRealEigenvalues () {
924 return d;
925 }
926
927 /** Return the imaginary parts of the eigenvalues
928 @return imag(diag(D))
929 */
930
931 public double[] getImagEigenvalues () {
932 return e;
933 }
934
935 /** Return the block diagonal eigenvalue matrix
936 @return D
937 */
938
939 public Matrix getD () {
940 Matrix X = new Matrix(n,n);
941 double[][] D = X.getArray();
942 for (int i = 0; i < n; i++) {
943 for (int j = 0; j < n; j++) {
944 D[i][j] = 0.0;
945 }
946 D[i][i] = d[i];
947 if (e[i] > 0) {
948 D[i][i+1] = e[i];
949 } else if (e[i] < 0) {
950 D[i][i-1] = e[i];
951 }
952 }
953 return X;
954 }
955 private static final long serialVersionUID = 1;
956}
Note: See TracBrowser for help on using the repository browser.