EigenvalueDecomposition.cs 33 KB

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