PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/v1.6.5/Classes/PHPExcel/Shared/JAMA/SingularValueDecomposition.php

#
PHP | 502 lines | 332 code | 29 blank | 141 comment | 108 complexity | 034ddafe941f529b35ed10ed6afcbf7d MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * @package JAMA
  4. *
  5. * For an m-by-n matrix A with m >= n, the singular value decomposition is
  6. * an m-by-n orthogonal matrix U, an n-by-n diagonal matrix S, and
  7. * an n-by-n orthogonal matrix V so that A = U*S*V'.
  8. *
  9. * The singular values, sigma[$k] = S[$k][$k], are ordered so that
  10. * sigma[0] >= sigma[1] >= ... >= sigma[n-1].
  11. *
  12. * The singular value decompostion always exists, so the constructor will
  13. * never fail. The matrix condition number and the effective numerical
  14. * rank can be computed from this decomposition.
  15. *
  16. * @author Paul Meagher
  17. * @license PHP v3.0
  18. * @version 1.1
  19. */
  20. class SingularValueDecomposition {
  21. /**
  22. * Internal storage of U.
  23. * @var array
  24. */
  25. var $U = array();
  26. /**
  27. * Internal storage of V.
  28. * @var array
  29. */
  30. var $V = array();
  31. /**
  32. * Internal storage of singular values.
  33. * @var array
  34. */
  35. var $s = array();
  36. /**
  37. * Row dimension.
  38. * @var int
  39. */
  40. var $m;
  41. /**
  42. * Column dimension.
  43. * @var int
  44. */
  45. var $n;
  46. /**
  47. * Construct the singular value decomposition
  48. *
  49. * Derived from LINPACK code.
  50. *
  51. * @param $A Rectangular matrix
  52. * @return Structure to access U, S and V.
  53. */
  54. function SingularValueDecomposition ($Arg) {
  55. // Initialize.
  56. $A = $Arg->getArrayCopy();
  57. $this->m = $Arg->getRowDimension();
  58. $this->n = $Arg->getColumnDimension();
  59. $nu = min($this->m, $this->n);
  60. $e = array();
  61. $work = array();
  62. $wantu = true;
  63. $wantv = true;
  64. $nct = min($this->m - 1, $this->n);
  65. $nrt = max(0, min($this->n - 2, $this->m));
  66. // Reduce A to bidiagonal form, storing the diagonal elements
  67. // in s and the super-diagonal elements in e.
  68. for ($k = 0; $k < max($nct,$nrt); $k++) {
  69. if ($k < $nct) {
  70. // Compute the transformation for the k-th column and
  71. // place the k-th diagonal in s[$k].
  72. // Compute 2-norm of k-th column without under/overflow.
  73. $this->s[$k] = 0;
  74. for ($i = $k; $i < $this->m; $i++)
  75. $this->s[$k] = hypo($this->s[$k], $A[$i][$k]);
  76. if ($this->s[$k] != 0.0) {
  77. if ($A[$k][$k] < 0.0)
  78. $this->s[$k] = -$this->s[$k];
  79. for ($i = $k; $i < $this->m; $i++)
  80. $A[$i][$k] /= $this->s[$k];
  81. $A[$k][$k] += 1.0;
  82. }
  83. $this->s[$k] = -$this->s[$k];
  84. }
  85. for ($j = $k + 1; $j < $this->n; $j++) {
  86. if (($k < $nct) & ($this->s[$k] != 0.0)) {
  87. // Apply the transformation.
  88. $t = 0;
  89. for ($i = $k; $i < $this->m; $i++)
  90. $t += $A[$i][$k] * $A[$i][$j];
  91. $t = -$t / $A[$k][$k];
  92. for ($i = $k; $i < $this->m; $i++)
  93. $A[$i][$j] += $t * $A[$i][$k];
  94. // Place the k-th row of A into e for the
  95. // subsequent calculation of the row transformation.
  96. $e[$j] = $A[$k][$j];
  97. }
  98. }
  99. if ($wantu AND ($k < $nct)) {
  100. // Place the transformation in U for subsequent back
  101. // multiplication.
  102. for ($i = $k; $i < $this->m; $i++)
  103. $this->U[$i][$k] = $A[$i][$k];
  104. }
  105. if ($k < $nrt) {
  106. // Compute the k-th row transformation and place the
  107. // k-th super-diagonal in e[$k].
  108. // Compute 2-norm without under/overflow.
  109. $e[$k] = 0;
  110. for ($i = $k + 1; $i < $this->n; $i++)
  111. $e[$k] = hypo($e[$k], $e[$i]);
  112. if ($e[$k] != 0.0) {
  113. if ($e[$k+1] < 0.0)
  114. $e[$k] = -$e[$k];
  115. for ($i = $k + 1; $i < $this->n; $i++)
  116. $e[$i] /= $e[$k];
  117. $e[$k+1] += 1.0;
  118. }
  119. $e[$k] = -$e[$k];
  120. if (($k+1 < $this->m) AND ($e[$k] != 0.0)) {
  121. // Apply the transformation.
  122. for ($i = $k+1; $i < $this->m; $i++)
  123. $work[$i] = 0.0;
  124. for ($j = $k+1; $j < $this->n; $j++)
  125. for ($i = $k+1; $i < $this->m; $i++)
  126. $work[$i] += $e[$j] * $A[$i][$j];
  127. for ($j = $k + 1; $j < $this->n; $j++) {
  128. $t = -$e[$j] / $e[$k+1];
  129. for ($i = $k + 1; $i < $this->m; $i++)
  130. $A[$i][$j] += $t * $work[$i];
  131. }
  132. }
  133. if ($wantv) {
  134. // Place the transformation in V for subsequent
  135. // back multiplication.
  136. for ($i = $k + 1; $i < $this->n; $i++)
  137. $this->V[$i][$k] = $e[$i];
  138. }
  139. }
  140. }
  141. // Set up the final bidiagonal matrix or order p.
  142. $p = min($this->n, $this->m + 1);
  143. if ($nct < $this->n)
  144. $this->s[$nct] = $A[$nct][$nct];
  145. if ($this->m < $p)
  146. $this->s[$p-1] = 0.0;
  147. if ($nrt + 1 < $p)
  148. $e[$nrt] = $A[$nrt][$p-1];
  149. $e[$p-1] = 0.0;
  150. // If required, generate U.
  151. if ($wantu) {
  152. for ($j = $nct; $j < $nu; $j++) {
  153. for ($i = 0; $i < $this->m; $i++)
  154. $this->U[$i][$j] = 0.0;
  155. $this->U[$j][$j] = 1.0;
  156. }
  157. for ($k = $nct - 1; $k >= 0; $k--) {
  158. if ($this->s[$k] != 0.0) {
  159. for ($j = $k + 1; $j < $nu; $j++) {
  160. $t = 0;
  161. for ($i = $k; $i < $this->m; $i++)
  162. $t += $this->U[$i][$k] * $this->U[$i][$j];
  163. $t = -$t / $this->U[$k][$k];
  164. for ($i = $k; $i < $this->m; $i++)
  165. $this->U[$i][$j] += $t * $this->U[$i][$k];
  166. }
  167. for ($i = $k; $i < $this->m; $i++ )
  168. $this->U[$i][$k] = -$this->U[$i][$k];
  169. $this->U[$k][$k] = 1.0 + $this->U[$k][$k];
  170. for ($i = 0; $i < $k - 1; $i++)
  171. $this->U[$i][$k] = 0.0;
  172. } else {
  173. for ($i = 0; $i < $this->m; $i++)
  174. $this->U[$i][$k] = 0.0;
  175. $this->U[$k][$k] = 1.0;
  176. }
  177. }
  178. }
  179. // If required, generate V.
  180. if ($wantv) {
  181. for ($k = $this->n - 1; $k >= 0; $k--) {
  182. if (($k < $nrt) AND ($e[$k] != 0.0)) {
  183. for ($j = $k + 1; $j < $nu; $j++) {
  184. $t = 0;
  185. for ($i = $k + 1; $i < $this->n; $i++)
  186. $t += $this->V[$i][$k]* $this->V[$i][$j];
  187. $t = -$t / $this->V[$k+1][$k];
  188. for ($i = $k + 1; $i < $this->n; $i++)
  189. $this->V[$i][$j] += $t * $this->V[$i][$k];
  190. }
  191. }
  192. for ($i = 0; $i < $this->n; $i++)
  193. $this->V[$i][$k] = 0.0;
  194. $this->V[$k][$k] = 1.0;
  195. }
  196. }
  197. // Main iteration loop for the singular values.
  198. $pp = $p - 1;
  199. $iter = 0;
  200. $eps = pow(2.0, -52.0);
  201. while ($p > 0) {
  202. // Here is where a test for too many iterations would go.
  203. // This section of the program inspects for negligible
  204. // elements in the s and e arrays. On completion the
  205. // variables kase and k are set as follows:
  206. // kase = 1 if s(p) and e[k-1] are negligible and k<p
  207. // kase = 2 if s(k) is negligible and k<p
  208. // kase = 3 if e[k-1] is negligible, k<p, and
  209. // s(k), ..., s(p) are not negligible (qr step).
  210. // kase = 4 if e(p-1) is negligible (convergence).
  211. for ($k = $p - 2; $k >= -1; $k--) {
  212. if ($k == -1)
  213. break;
  214. if (abs($e[$k]) <= $eps * (abs($this->s[$k]) + abs($this->s[$k+1]))) {
  215. $e[$k] = 0.0;
  216. break;
  217. }
  218. }
  219. if ($k == $p - 2)
  220. $kase = 4;
  221. else {
  222. for ($ks = $p - 1; $ks >= $k; $ks--) {
  223. if ($ks == $k)
  224. break;
  225. $t = ($ks != $p ? abs($e[$ks]) : 0.) + ($ks != $k + 1 ? abs($e[$ks-1]) : 0.);
  226. if (abs($this->s[$ks]) <= $eps * $t) {
  227. $this->s[$ks] = 0.0;
  228. break;
  229. }
  230. }
  231. if ($ks == $k)
  232. $kase = 3;
  233. else if ($ks == $p-1)
  234. $kase = 1;
  235. else {
  236. $kase = 2;
  237. $k = $ks;
  238. }
  239. }
  240. $k++;
  241. // Perform the task indicated by kase.
  242. switch ($kase) {
  243. // Deflate negligible s(p).
  244. case 1:
  245. $f = $e[$p-2];
  246. $e[$p-2] = 0.0;
  247. for ($j = $p - 2; $j >= $k; $j--) {
  248. $t = hypo($this->s[$j],$f);
  249. $cs = $this->s[$j] / $t;
  250. $sn = $f / $t;
  251. $this->s[$j] = $t;
  252. if ($j != $k) {
  253. $f = -$sn * $e[$j-1];
  254. $e[$j-1] = $cs * $e[$j-1];
  255. }
  256. if ($wantv) {
  257. for ($i = 0; $i < $this->n; $i++) {
  258. $t = $cs * $this->V[$i][$j] + $sn * $this->V[$i][$p-1];
  259. $this->V[$i][$p-1] = -$sn * $this->V[$i][$j] + $cs * $this->V[$i][$p-1];
  260. $this->V[$i][$j] = $t;
  261. }
  262. }
  263. }
  264. break;
  265. // Split at negligible s(k).
  266. case 2:
  267. $f = $e[$k-1];
  268. $e[$k-1] = 0.0;
  269. for ($j = $k; $j < $p; $j++) {
  270. $t = hypo($this->s[$j], $f);
  271. $cs = $this->s[$j] / $t;
  272. $sn = $f / $t;
  273. $this->s[$j] = $t;
  274. $f = -$sn * $e[$j];
  275. $e[$j] = $cs * $e[$j];
  276. if ($wantu) {
  277. for ($i = 0; $i < $this->m; $i++) {
  278. $t = $cs * $this->U[$i][$j] + $sn * $this->U[$i][$k-1];
  279. $this->U[$i][$k-1] = -$sn * $this->U[$i][$j] + $cs * $this->U[$i][$k-1];
  280. $this->U[$i][$j] = $t;
  281. }
  282. }
  283. }
  284. break;
  285. // Perform one qr step.
  286. case 3:
  287. // Calculate the shift.
  288. $scale = max(max(max(max(
  289. abs($this->s[$p-1]),abs($this->s[$p-2])),abs($e[$p-2])),
  290. abs($this->s[$k])), abs($e[$k]));
  291. $sp = $this->s[$p-1] / $scale;
  292. $spm1 = $this->s[$p-2] / $scale;
  293. $epm1 = $e[$p-2] / $scale;
  294. $sk = $this->s[$k] / $scale;
  295. $ek = $e[$k] / $scale;
  296. $b = (($spm1 + $sp) * ($spm1 - $sp) + $epm1 * $epm1) / 2.0;
  297. $c = ($sp * $epm1) * ($sp * $epm1);
  298. $shift = 0.0;
  299. if (($b != 0.0) || ($c != 0.0)) {
  300. $shift = sqrt($b * $b + $c);
  301. if ($b < 0.0)
  302. $shift = -$shift;
  303. $shift = $c / ($b + $shift);
  304. }
  305. $f = ($sk + $sp) * ($sk - $sp) + $shift;
  306. $g = $sk * $ek;
  307. // Chase zeros.
  308. for ($j = $k; $j < $p-1; $j++) {
  309. $t = hypo($f,$g);
  310. $cs = $f/$t;
  311. $sn = $g/$t;
  312. if ($j != $k)
  313. $e[$j-1] = $t;
  314. $f = $cs * $this->s[$j] + $sn * $e[$j];
  315. $e[$j] = $cs * $e[$j] - $sn * $this->s[$j];
  316. $g = $sn * $this->s[$j+1];
  317. $this->s[$j+1] = $cs * $this->s[$j+1];
  318. if ($wantv) {
  319. for ($i = 0; $i < $this->n; $i++) {
  320. $t = $cs * $this->V[$i][$j] + $sn * $this->V[$i][$j+1];
  321. $this->V[$i][$j+1] = -$sn * $this->V[$i][$j] + $cs * $this->V[$i][$j+1];
  322. $this->V[$i][$j] = $t;
  323. }
  324. }
  325. $t = hypo($f,$g);
  326. $cs = $f/$t;
  327. $sn = $g/$t;
  328. $this->s[$j] = $t;
  329. $f = $cs * $e[$j] + $sn * $this->s[$j+1];
  330. $this->s[$j+1] = -$sn * $e[$j] + $cs * $this->s[$j+1];
  331. $g = $sn * $e[$j+1];
  332. $e[$j+1] = $cs * $e[$j+1];
  333. if ($wantu && ($j < $this->m - 1)) {
  334. for ($i = 0; $i < $this->m; $i++) {
  335. $t = $cs * $this->U[$i][$j] + $sn * $this->U[$i][$j+1];
  336. $this->U[$i][$j+1] = -$sn * $this->U[$i][$j] + $cs * $this->U[$i][$j+1];
  337. $this->U[$i][$j] = $t;
  338. }
  339. }
  340. }
  341. $e[$p-2] = $f;
  342. $iter = $iter + 1;
  343. break;
  344. // Convergence.
  345. case 4:
  346. // Make the singular values positive.
  347. if ($this->s[$k] <= 0.0) {
  348. $this->s[$k] = ($this->s[$k] < 0.0 ? -$this->s[$k] : 0.0);
  349. if ($wantv) {
  350. for ($i = 0; $i <= $pp; $i++)
  351. $this->V[$i][$k] = -$this->V[$i][$k];
  352. }
  353. }
  354. // Order the singular values.
  355. while ($k < $pp) {
  356. if ($this->s[$k] >= $this->s[$k+1])
  357. break;
  358. $t = $this->s[$k];
  359. $this->s[$k] = $this->s[$k+1];
  360. $this->s[$k+1] = $t;
  361. if ($wantv AND ($k < $this->n - 1)) {
  362. for ($i = 0; $i < $this->n; $i++) {
  363. $t = $this->V[$i][$k+1];
  364. $this->V[$i][$k+1] = $this->V[$i][$k];
  365. $this->V[$i][$k] = $t;
  366. }
  367. }
  368. if ($wantu AND ($k < $this->m-1)) {
  369. for ($i = 0; $i < $this->m; $i++) {
  370. $t = $this->U[$i][$k+1];
  371. $this->U[$i][$k+1] = $this->U[$i][$k];
  372. $this->U[$i][$k] = $t;
  373. }
  374. }
  375. $k++;
  376. }
  377. $iter = 0;
  378. $p--;
  379. break;
  380. } // end switch
  381. } // end while
  382. /*
  383. echo "<p>Output A</p>";
  384. $A = new Matrix($A);
  385. $A->toHTML();
  386. echo "<p>Matrix U</p>";
  387. echo "<pre>";
  388. print_r($this->U);
  389. echo "</pre>";
  390. echo "<p>Matrix V</p>";
  391. echo "<pre>";
  392. print_r($this->V);
  393. echo "</pre>";
  394. echo "<p>Vector S</p>";
  395. echo "<pre>";
  396. print_r($this->s);
  397. echo "</pre>";
  398. exit;
  399. */
  400. } // end constructor
  401. /**
  402. * Return the left singular vectors
  403. * @access public
  404. * @return U
  405. */
  406. function getU() {
  407. return new Matrix($this->U, $this->m, min($this->m + 1, $this->n));
  408. }
  409. /**
  410. * Return the right singular vectors
  411. * @access public
  412. * @return V
  413. */
  414. function getV() {
  415. return new Matrix($this->V);
  416. }
  417. /**
  418. * Return the one-dimensional array of singular values
  419. * @access public
  420. * @return diagonal of S.
  421. */
  422. function getSingularValues() {
  423. return $this->s;
  424. }
  425. /**
  426. * Return the diagonal matrix of singular values
  427. * @access public
  428. * @return S
  429. */
  430. function getS() {
  431. for ($i = 0; $i < $this->n; $i++) {
  432. for ($j = 0; $j < $this->n; $j++)
  433. $S[$i][$j] = 0.0;
  434. $S[$i][$i] = $this->s[$i];
  435. }
  436. return new Matrix($S);
  437. }
  438. /**
  439. * Two norm
  440. * @access public
  441. * @return max(S)
  442. */
  443. function norm2() {
  444. return $this->s[0];
  445. }
  446. /**
  447. * Two norm condition number
  448. * @access public
  449. * @return max(S)/min(S)
  450. */
  451. function cond() {
  452. return $this->s[0] / $this->s[min($this->m, $this->n) - 1];
  453. }
  454. /**
  455. * Effective numerical matrix rank
  456. * @access public
  457. * @return Number of nonnegligible singular values.
  458. */
  459. function rank() {
  460. $eps = pow(2.0, -52.0);
  461. $tol = max($this->m, $this->n) * $this->s[0] * $eps;
  462. $r = 0;
  463. for ($i = 0; $i < count($this->s); $i++) {
  464. if ($this->s[$i] > $tol)
  465. $r++;
  466. }
  467. return $r;
  468. }
  469. }
  470. ?>