EigenvalueDecomposition.php 32 KB

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