GestureRecognitionToolkit  Version: 0.2.0
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
EigenvalueDecomposition.cpp
1 /*
2  This code is based on EigenvalueDecomposition.java from http://math.nist.gov/javanumerics/jama/
3  The license below is from that file.
4  */
5 
6 /*
7  * This software is a cooperative product of The MathWorks and the National
8  * Institute of Standards and Technology (NIST) which has been released to the
9  * public domain. Neither The MathWorks nor NIST assumes any responsibility
10  * whatsoever for its use by other parties, and makes no guarantees, expressed
11  * or implied, about its quality, reliability, or any other characteristic.
12 
13  * EigenvalueDecomposition.java
14  * Copyright (C) 1999 The Mathworks and NIST
15  *
16  */
17 
18 #define GRT_DLL_EXPORTS
19 #include "EigenvalueDecomposition.h"
20 
21 GRT_BEGIN_NAMESPACE
22 
23 EigenvalueDecomposition::EigenvalueDecomposition(){
24  warningLog.setProceedingText("[WARNING EigenvalueDecomposition]");
25 }
26 
27 EigenvalueDecomposition::~EigenvalueDecomposition(){
28 
29 }
30 
31 bool EigenvalueDecomposition::decompose(const MatrixFloat &a){
32 
33  n = a.getNumCols();
34  eigenvectors.resize(n,n);
35  realEigenvalues.resize(n);
36  complexEigenvalues.resize(n);
37 
38  issymmetric = true;
39  for(int j = 0; (j < n) & issymmetric; j++) {
40  for(int i = 0; (i < n) & issymmetric; i++) {
41  issymmetric = (a[i][j] == a[j][i]);
42  }
43  }
44 
45  if (issymmetric) {
46  for(int i = 0; i < n; i++) {
47  for(int j = 0; j < n; j++) {
48  eigenvectors[i][j] = a[i][j];
49  }
50  }
51 
52  // Tridiagonalize.
53  tred2();
54 
55  // Diagonalize.
56  tql2();
57 
58  } else {
59  h.resize(n,n);
60  ort.resize(n);
61 
62  for(int j = 0; j < n; j++) {
63  for(int i = 0; i < n; i++) {
64  h[i][j] = a[i][j];
65  }
66  }
67 
68  // Reduce to Hessenberg form.
69  orthes();
70 
71  // Reduce Hessenberg to real Schur form.
72  hqr2();
73  }
74 
75  return true;
76 }
77 
79 
80  for(int j = 0; j < n; j++) {
81  realEigenvalues[j] = eigenvectors[n-1][j];
82  }
83 
84  // Householder reduction to tridiagonal form.
85  for(int i = n-1; i > 0; i--) {
86 
87  // Scale to avoid under/overflow.
88  Float scale = 0.0;
89  Float h = 0.0;
90  for (int k = 0; k < i; k++) {
91  scale = scale + fabs(realEigenvalues[k]);
92  }
93  if (scale == 0.0) {
94  complexEigenvalues[i] = realEigenvalues[i-1];
95  for (int j = 0; j < i; j++) {
96  realEigenvalues[j] = eigenvectors[i-1][j];
97  eigenvectors[i][j] = 0.0;
98  eigenvectors[j][i] = 0.0;
99  }
100  } else {
101 
102  // Generate Householder vector.
103  for (int k = 0; k < i; k++) {
104  realEigenvalues[k] /= scale;
105  h += realEigenvalues[k] * realEigenvalues[k];
106  }
107  Float f = realEigenvalues[i-1];
108  Float g = sqrt(h);
109  if (f > 0) {
110  g = -g;
111  }
112  complexEigenvalues[i] = scale * g;
113  h = h - f * g;
114  realEigenvalues[i-1] = f - g;
115  for (int j = 0; j < i; j++) {
116  complexEigenvalues[j] = 0.0;
117  }
118 
119  // Apply similarity transformation to remaining columns.
120  for (int j = 0; j < i; j++) {
121  f = realEigenvalues[j];
122  eigenvectors[j][i] = f;
123  g = complexEigenvalues[j] + eigenvectors[j][j] * f;
124  for (int k = j+1; k <= i-1; k++) {
125  g += eigenvectors[k][j] * realEigenvalues[k];
126  complexEigenvalues[k] += eigenvectors[k][j] * f;
127  }
128  complexEigenvalues[j] = g;
129  }
130  f = 0.0;
131  for (int j = 0; j < i; j++) {
132  complexEigenvalues[j] /= h;
133  f += complexEigenvalues[j] * realEigenvalues[j];
134  }
135  Float hh = f / (h + h);
136  for (int j = 0; j < i; j++) {
137  complexEigenvalues[j] -= hh * realEigenvalues[j];
138  }
139  for (int j = 0; j < i; j++) {
140  f = realEigenvalues[j];
141  g = complexEigenvalues[j];
142  for (int k = j; k <= i-1; k++) {
143  eigenvectors[k][j] -= (f * complexEigenvalues[k] + g * realEigenvalues[k]);
144  }
145  realEigenvalues[j] = eigenvectors[i-1][j];
146  eigenvectors[i][j] = 0.0;
147  }
148  }
149  realEigenvalues[i] = h;
150  }
151 
152  // Accumulate transformations.
153  for(int i = 0; i < n-1; i++) {
154  eigenvectors[n-1][i] = eigenvectors[i][i];
155  eigenvectors[i][i] = 1.0;
156  Float h = realEigenvalues[i+1];
157  if (h != 0.0) {
158  for (int k = 0; k <= i; k++) {
159  realEigenvalues[k] = eigenvectors[k][i+1] / h;
160  }
161  for (int j = 0; j <= i; j++) {
162  Float g = 0.0;
163  for (int k = 0; k <= i; k++) {
164  g += eigenvectors[k][i+1] * eigenvectors[k][j];
165  }
166  for (int k = 0; k <= i; k++) {
167  eigenvectors[k][j] -= g * realEigenvalues[k];
168  }
169  }
170  }
171  for(int k = 0; k <= i; k++) {
172  eigenvectors[k][i+1] = 0.0;
173  }
174  }
175  for(int j = 0; j < n; j++) {
176  realEigenvalues[j] = eigenvectors[n-1][j];
177  eigenvectors[n-1][j] = 0.0;
178  }
179  eigenvectors[n-1][n-1] = 1.0;
180  complexEigenvalues[0] = 0.0;
181 
182  return;
183 }
184 
186 
187  for(int i = 1; i < n; i++) {
188  complexEigenvalues[i-1] = complexEigenvalues[i];
189  }
190  complexEigenvalues[n-1] = 0.0;
191 
192  Float f = 0.0;
193  Float tst1 = 0.0;
194  Float eps = pow(2.0,-52.0);
195  for (int l = 0; l < n; l++) {
196 
197  // Find small subdiagonal element
198  tst1 = findMax(tst1,fabs(realEigenvalues[l]) + fabs(complexEigenvalues[l]));
199  int m = l;
200  while (m < n) {
201  if(fabs(complexEigenvalues[m]) <= eps*tst1) {
202  break;
203  }
204  m++;
205  }
206 
207  // If m == l, d[l] is an eigenvalue, otherwise, iterate.
208  if (m > l) {
209  int iter = 0;
210  do {
211  iter = iter + 1; // (Could check iteration count here.)
212 
213  // Compute implicit shift
214  Float g = realEigenvalues[l];
215  Float p = (realEigenvalues[l+1] - g) / (2.0 * complexEigenvalues[l]);
216  Float r = hypot(p,1.0);
217  if (p < 0) {
218  r = -r;
219  }
220  realEigenvalues[l] = complexEigenvalues[l] / (p + r);
221  realEigenvalues[l+1] = complexEigenvalues[l] * (p + r);
222  Float dl1 = realEigenvalues[l+1];
223  Float h = g - realEigenvalues[l];
224  for (int i = l+2; i < n; i++) {
225  realEigenvalues[i] -= h;
226  }
227  f = f + h;
228 
229  // Implicit QL transformation.
230  p = realEigenvalues[m];
231  Float c = 1.0;
232  Float c2 = c;
233  Float c3 = c;
234  Float el1 = complexEigenvalues[l+1];
235  Float s = 0.0;
236  Float s2 = 0.0;
237  for (int i = m-1; i >= l; i--) {
238  c3 = c2;
239  c2 = c;
240  s2 = s;
241  g = c * complexEigenvalues[i];
242  h = c * p;
243  r = hypot(p,complexEigenvalues[i]);
244  complexEigenvalues[i+1] = s * r;
245  s = complexEigenvalues[i] / r;
246  c = p / r;
247  p = c * realEigenvalues[i] - s * g;
248  realEigenvalues[i+1] = h + s * (c * g + s * realEigenvalues[i]);
249 
250  // Accumulate transformation.
251  for(int k = 0; k < n; k++) {
252  h = eigenvectors[k][i+1];
253  eigenvectors[k][i+1] = s * eigenvectors[k][i] + c * h;
254  eigenvectors[k][i] = c * eigenvectors[k][i] - s * h;
255  }
256  }
257  p = -s * s2 * c3 * el1 * complexEigenvalues[l] / dl1;
258  complexEigenvalues[l] = s * p;
259  realEigenvalues[l] = c * p;
260 
261  // Check for convergence.
262  } while (fabs(complexEigenvalues[l]) > eps*tst1);
263  }
264  realEigenvalues[l] = realEigenvalues[l] + f;
265  complexEigenvalues[l] = 0.0;
266  }
267 
268  // Sort eigenvalues and corresponding vectors.
269  for(int i = 0; i < n-1; i++) {
270  int k = i;
271  Float p = realEigenvalues[i];
272  for (int j = i+1; j < n; j++) {
273  if(realEigenvalues[j] < p) {
274  k = j;
275  p = realEigenvalues[j];
276  }
277  }
278  if (k != i) {
279  realEigenvalues[k] = realEigenvalues[i];
280  realEigenvalues[i] = p;
281  for (int j = 0; j < n; j++) {
282  p = eigenvectors[j][i];
283  eigenvectors[j][i] = eigenvectors[j][k];
284  eigenvectors[j][k] = p;
285  }
286  }
287  }
288  return;
289 }
290 
292 
293  int low = 0;
294  int high = n-1;
295 
296  for(int m = low+1; m <= high-1; m++) {
297 
298  // Scale column.
299  Float scale = 0.0;
300  for (int i = m; i <= high; i++) {
301  scale = scale + fabs(h[i][m-1]);
302  }
303  if (scale != 0.0) {
304 
305  // Compute Householder transformation.
306  Float ht = 0.0;
307  for(int i = high; i >= m; i--) {
308  ort[i] = h[i][m-1]/scale;
309  ht += ort[i] * ort[i];
310  }
311  Float g = sqrt( ht );
312  if (ort[m] > 0) {
313  g = -g;
314  }
315  ht = ht - ort[m] * g;
316  ort[m] = ort[m] - g;
317 
318  // Apply Householder similarity transformation
319  // H = (I-u*u'/h)*H*(I-u*u')/h)
320  for (int j = m; j < n; j++) {
321  Float f = 0.0;
322  for (int i = high; i >= m; i--) {
323  f += ort[i]*h[i][j];
324  }
325  f = f/ht;
326  for (int i = m; i <= high; i++) {
327  h[i][j] -= f*ort[i];
328  }
329  }
330 
331  for(int i = 0; i <= high; i++) {
332  Float f = 0.0;
333  for(int j = high; j >= m; j--) {
334  f += ort[j]*h[i][j];
335  }
336  f = f/ht;
337  for (int j = m; j <= high; j++) {
338  h[i][j] -= f*ort[j];
339  }
340  }
341  ort[m] = scale*ort[m];
342  h[m][m-1] = scale*g;
343  }
344  }
345 
346  // Accumulate transformations (Algol's ortran).
347  for (int i = 0; i < n; i++) {
348  for (int j = 0; j < n; j++) {
349  eigenvectors[i][j] = (i == j ? 1.0 : 0.0);
350  }
351  }
352 
353  for (int m = high-1; m >= low+1; m--) {
354  if (h[m][m-1] != 0.0) {
355  for (int i = m+1; i <= high; i++) {
356  ort[i] = h[i][m-1];
357  }
358  for (int j = m; j <= high; j++) {
359  Float g = 0.0;
360  for (int i = m; i <= high; i++) {
361  g += ort[i] * eigenvectors[i][j];
362  }
363  // Double division avoids possible underflow
364  g = (g / ort[m]) / h[m][m-1];
365  for (int i = m; i <= high; i++) {
366  eigenvectors[i][j] += g * ort[i];
367  }
368  }
369  }
370  }
371  return;
372 }
373 
375 
376  // Initialize
377  int nn = this->n;
378  int n = nn-1;
379  int low = 0;
380  int high = nn-1;
381  Float eps = pow(2.0,-52.0);
382  Float exshift = 0.0;
383  Float p=0,q=0,r=0,s=0,z=0,t,w,x,y;
384 
385  // Store roots isolated by balanc and compute matrix norm
386  Float norm = 0.0;
387  for(int i = 0; i < nn; i++) {
388  if( (i < low) | (i > high) ){
389  realEigenvalues[i] = h[i][i];
390  complexEigenvalues[i] = 0.0;
391  }
392  for (int j = findMax(i-1,0); j < nn; j++) {
393  norm = norm + fabs(h[i][j]);
394  }
395  }
396 
397  // Outer loop over eigenvalue index
398  int iter = 0;
399  while (n >= low) {
400 
401  // Look for single small sub-diagonal element
402  int l = n;
403  while (l > low) {
404  s = fabs(h[l-1][l-1]) + fabs(h[l][l]);
405  if (s == 0.0) {
406  s = norm;
407  }
408  if(fabs(h[l][l-1]) < eps * s) {
409  break;
410  }
411  l--;
412  }
413 
414  // Check for convergence
415  // One root found
416  if(l == n) {
417  h[n][n] = h[n][n] + exshift;
418  realEigenvalues[n] = h[n][n];
419  complexEigenvalues[n] = 0.0;
420  n--;
421  iter = 0;
422 
423  // Two roots found
424  } else if (l == n-1) {
425  w = h[n][n-1] * h[n-1][n];
426  p = (h[n-1][n-1] - h[n][n]) / 2.0;
427  q = p * p + w;
428  z = sqrt(fabs(q));
429  h[n][n] = h[n][n] + exshift;
430  h[n-1][n-1] = h[n-1][n-1] + exshift;
431  x = h[n][n];
432 
433  // Real pair
434  if (q >= 0) {
435  if (p >= 0) {
436  z = p + z;
437  } else {
438  z = p - z;
439  }
440  realEigenvalues[n-1] = x + z;
441  realEigenvalues[n] = realEigenvalues[n-1];
442  if (z != 0.0) {
443  realEigenvalues[n] = x - w / z;
444  }
445  complexEigenvalues[n-1] = 0.0;
446  complexEigenvalues[n] = 0.0;
447  x = h[n][n-1];
448  s = fabs(x) + fabs(z);
449  p = x / s;
450  q = z / s;
451  r = sqrt(p * p+q * q);
452  p = p / r;
453  q = q / r;
454 
455  // Row modification
456  for(int j = n-1; j < nn; j++) {
457  z = h[n-1][j];
458  h[n-1][j] = q * z + p * h[n][j];
459  h[n][j] = q * h[n][j] - p * z;
460  }
461 
462  // Column modification
463  for(int i = 0; i <= n; i++) {
464  z = h[i][n-1];
465  h[i][n-1] = q * z + p * h[i][n];
466  h[i][n] = q * h[i][n] - p * z;
467  }
468 
469  // Accumulate transformations
470  for(int i = low; i <= high; i++) {
471  z = eigenvectors[i][n-1];
472  eigenvectors[i][n-1] = q * z + p * eigenvectors[i][n];
473  eigenvectors[i][n] = q * eigenvectors[i][n] - p * z;
474  }
475 
476  // Complex pair
477  } else {
478  realEigenvalues[n-1] = x + p;
479  realEigenvalues[n] = x + p;
480  complexEigenvalues[n-1] = z;
481  complexEigenvalues[n] = -z;
482  }
483  n = n - 2;
484  iter = 0;
485 
486  // No convergence yet
487  } else {
488 
489  // Form shift
490  x = h[n][n];
491  y = 0.0;
492  w = 0.0;
493  if (l < n) {
494  y = h[n-1][n-1];
495  w = h[n][n-1] * h[n-1][n];
496  }
497 
498  // Wilkinson's original ad hoc shift
499  if (iter == 10) {
500  exshift += x;
501  for (int i = low; i <= n; i++) {
502  h[i][i] -= x;
503  }
504  s = fabs(h[n][n-1]) + fabs(h[n-1][n-2]);
505  x = y = 0.75 * s;
506  w = -0.4375 * s * s;
507  }
508 
509  // MATLAB's new ad hoc shift
510  if (iter == 30) {
511  s = (y - x) / 2.0;
512  s = s * s + w;
513  if (s > 0) {
514  s = sqrt(s);
515  if (y < x) {
516  s = -s;
517  }
518  s = x - w / ((y - x) / 2.0 + s);
519  for(int i = low; i <= n; i++) {
520  h[i][i] -= s;
521  }
522  exshift += s;
523  x = y = w = 0.964;
524  }
525  }
526 
527  iter = iter + 1; // (Could check iteration count here.)
528 
529  // Look for two consecutive small sub-diagonal elements
530  int m = n-2;
531  while (m >= l) {
532  z = h[m][m];
533  r = x - z;
534  s = y - z;
535  p = (r * s - w) / h[m+1][m] + h[m][m+1];
536  q = h[m+1][m+1] - z - r - s;
537  r = h[m+2][m+1];
538  s = fabs(p) + fabs(q) + fabs(r);
539  p = p / s;
540  q = q / s;
541  r = r / s;
542  if(m == l){
543  break;
544  }
545  if(fabs(h[m][m-1]) * (fabs(q) +fabs(r)) <
546  eps * (fabs(p) * (fabs(h[m-1][m-1]) + fabs(z) +
547  fabs(h[m+1][m+1])))) {
548  break;
549  }
550  m--;
551  }
552 
553  for(int i = m+2; i <= n; i++) {
554  h[i][i-2] = 0.0;
555  if (i > m+2) {
556  h[i][i-3] = 0.0;
557  }
558  }
559 
560  // Double QR step involving rows l:n and columns m:n
561  for(int k = m; k <= n-1; k++) {
562  bool notlast = (k != n-1);
563  if(k != m) {
564  p = h[k][k-1];
565  q = h[k+1][k-1];
566  r = (notlast ? h[k+2][k-1] : 0.0);
567  x = fabs(p) + fabs(q) + fabs(r);
568  if (x != 0.0) {
569  p = p / x;
570  q = q / x;
571  r = r / x;
572  }
573  }
574  if(x == 0.0){
575  break;
576  }
577  s = sqrt(p * p + q * q + r * r);
578  if(p < 0){
579  s = -s;
580  }
581  if(s != 0){
582  if(k != m){
583  h[k][k-1] = -s * x;
584  }else if(l != m){
585  h[k][k-1] = -h[k][k-1];
586  }
587  p = p + s;
588  x = p / s;
589  y = q / s;
590  z = r / s;
591  q = q / p;
592  r = r / p;
593 
594  // Row modification
595  for(int j = k; j < nn; j++) {
596  p = h[k][j] + q * h[k+1][j];
597  if(notlast){
598  p = p + r * h[k+2][j];
599  h[k+2][j] = h[k+2][j] - p * z;
600  }
601  h[k][j] = h[k][j] - p * x;
602  h[k+1][j] = h[k+1][j] - p * y;
603  }
604 
605  // Column modification
606  for (int i = 0; i <= findMin(n,k+3); i++) {
607  p = x * h[i][k] + y * h[i][k+1];
608  if(notlast){
609  p = p + z * h[i][k+2];
610  h[i][k+2] = h[i][k+2] - p * r;
611  }
612  h[i][k] = h[i][k] - p;
613  h[i][k+1] = h[i][k+1] - p * q;
614  }
615 
616  // Accumulate transformations
617  for(int i = low; i <= high; i++) {
618  p = x * eigenvectors[i][k] + y * eigenvectors[i][k+1];
619  if(notlast){
620  p = p + z * eigenvectors[i][k+2];
621  eigenvectors[i][k+2] = eigenvectors[i][k+2] - p * r;
622  }
623  eigenvectors[i][k] = eigenvectors[i][k] - p;
624  eigenvectors[i][k+1] = eigenvectors[i][k+1] - p * q;
625  }
626  } // (s != 0)
627  } // k loop
628  } // check convergence
629  } // while (n >= low)
630 
631  // Backsubstitute to find vectors of upper triangular form
632  if(norm == 0.0){
633  return;
634  }
635 
636  for(n = nn-1; n >= 0; n--) {
637  p = realEigenvalues[n];
638  q = complexEigenvalues[n];
639 
640  // Real vector
641  if (q == 0) {
642  int l = n;
643  h[n][n] = 1.0;
644  for(int i = n-1; i >= 0; i--) {
645  w = h[i][i] - p;
646  r = 0.0;
647  for(int j = l; j <= n; j++) {
648  r = r + h[i][j] * h[j][n];
649  }
650  if(complexEigenvalues[i] < 0.0) {
651  z = w;
652  s = r;
653  } else {
654  l = i;
655  if (complexEigenvalues[i] == 0.0) {
656  if (w != 0.0) {
657  h[i][n] = -r / w;
658  } else {
659  h[i][n] = -r / (eps * norm);
660  }
661 
662  // Solve real equations
663  } else {
664  x = h[i][i+1];
665  y = h[i+1][i];
666  q = (realEigenvalues[i] - p) * (realEigenvalues[i] - p) + complexEigenvalues[i] * complexEigenvalues[i];
667  t = (x * s - z * r) / q;
668  h[i][n] = t;
669  if(fabs(x) > fabs(z)) {
670  h[i+1][n] = (-r - w * t) / x;
671  } else {
672  h[i+1][n] = (-s - y * t) / z;
673  }
674  }
675 
676  // Overflow control
677  t = fabs(h[i][n]);
678  if ((eps * t) * t > 1) {
679  for(int j = i; j <= n; j++) {
680  h[j][n] = h[j][n] / t;
681  }
682  }
683  }
684  }
685 
686  // Complex vector
687  } else if (q < 0) {
688  int l = n-1;
689 
690  // Last vector component imaginary so matrix is triangular
691  if (fabs(h[n][n-1]) > fabs(h[n-1][n])) {
692  h[n-1][n-1] = q / h[n][n-1];
693  h[n-1][n] = -(h[n][n] - p) / h[n][n-1];
694  } else {
695  cdiv(0.0,-h[n-1][n],h[n-1][n-1]-p,q);
696  h[n-1][n-1] = cdivr;
697  h[n-1][n] = cdivi;
698  }
699  h[n][n-1] = 0.0;
700  h[n][n] = 1.0;
701  for(int i = n-2; i >= 0; i--) {
702  Float ra,sa,vr,vi;
703  ra = 0.0;
704  sa = 0.0;
705  for (int j = l; j <= n; j++) {
706  ra = ra + h[i][j] * h[j][n-1];
707  sa = sa + h[i][j] * h[j][n];
708  }
709  w = h[i][i] - p;
710 
711  if(complexEigenvalues[i] < 0.0) {
712  z = w;
713  r = ra;
714  s = sa;
715  } else {
716  l = i;
717  if(complexEigenvalues[i] == 0) {
718  cdiv(-ra,-sa,w,q);
719  h[i][n-1] = cdivr;
720  h[i][n] = cdivi;
721  } else {
722 
723  // Solve complex equations
724  x = h[i][i+1];
725  y = h[i+1][i];
726  vr = (realEigenvalues[i] - p) * (realEigenvalues[i] - p) + complexEigenvalues[i] * complexEigenvalues[i] - q * q;
727  vi = (realEigenvalues[i] - p) * 2.0 * q;
728  if((vr == 0.0) & (vi == 0.0)){
729  vr = eps * norm * (fabs(w) + fabs(q) + fabs(x) + fabs(y) + fabs(z));
730  }
731  cdiv(x*r-z*ra+q*sa,x*s-z*sa-q*ra,vr,vi);
732  h[i][n-1] = cdivr;
733  h[i][n] = cdivi;
734  if (fabs(x) > (fabs(z) + fabs(q))) {
735  h[i+1][n-1] = (-ra - w * h[i][n-1] + q * h[i][n]) / x;
736  h[i+1][n] = (-sa - w * h[i][n] - q * h[i][n-1]) / x;
737  } else {
738  cdiv(-r-y*h[i][n-1],-s-y*h[i][n],z,q);
739  h[i+1][n-1] = cdivr;
740  h[i+1][n] = cdivi;
741  }
742  }
743 
744  // Overflow control
745  t = findMax(fabs(h[i][n-1]),fabs(h[i][n]));
746  if ((eps * t) * t > 1) {
747  for(int j = i; j <= n; j++) {
748  h[j][n-1] = h[j][n-1] / t;
749  h[j][n] = h[j][n] / t;
750  }
751  }
752  }
753  }
754  }
755  }
756 
757  // Vectors of isolated roots
758  for (int i = 0; i < nn; i++) {
759  if((i < low) | (i > high)){
760  for (int j = i; j < nn; j++) {
761  eigenvectors[i][j] = h[i][j];
762  }
763  }
764  }
765 
766  // Back transformation to get eigenvectors of original matrix
767  for (int j = nn-1; j >= low; j--) {
768  for (int i = low; i <= high; i++) {
769  z = 0.0;
770  for (int k = low; k <= findMin(j,high); k++) {
771  z = z + eigenvectors[i][k] * h[k][j];
772  }
773  eigenvectors[i][j] = z;
774  }
775  }
776 
777  return;
778 }
779 
780 void EigenvalueDecomposition::cdiv(Float xr, Float xi, Float yr, Float yi){
781  Float r,d;
782  if(fabs(yr) > fabs(yi)){
783  r = yi/yr;
784  d = yr + r*yi;
785  cdivr = (xr + r*xi)/d;
786  cdivi = (xi - r*xr)/d;
787  } else {
788  r = yr/yi;
789  d = yi + r*yr;
790  cdivr = (r*xr + xi)/d;
791  cdivi = (r*xi - xr)/d;
792  }
793  return;
794 }
795 
797 
798  MatrixFloat x(n,n);
799  for (int i = 0; i < n; i++) {
800  for (int j = 0; j < n; j++) {
801  x[i][j] = 0.0;
802  }
803  x[i][i] = realEigenvalues[i];
804  if(complexEigenvalues[i] > 0) {
805  x[i][i+1] = complexEigenvalues[i];
806  } else if(complexEigenvalues[i] < 0) {
807  x[i][i-1] = complexEigenvalues[i];
808  }
809  }
810  return x;
811 }
812 
814  return realEigenvalues;
815 }
816 
818  return complexEigenvalues;
819 }
820 
821 GRT_END_NAMESPACE
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
void cdiv(Float xr, Float xi, Float yr, Float yi)
unsigned int getNumCols() const
Definition: Matrix.h:549
virtual bool resize(const unsigned int r, const unsigned int c)
Definition: Matrix.h:232