EigenvalueDecomposition.php 32 KB

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