/app/vendors/PHPExcel_deprecated/PHPExcel/Shared/JAMA/examples/benchmark.php

https://github.com/konscript/roskilde-el-old · PHP · 263 lines · 205 code · 49 blank · 9 comment · 10 complexity · 2b4c1606e0bd4461bbc715d957fafb21 MD5 · raw file

  1. <?php
  2. error_reporting(E_ALL);
  3. /**
  4. * @package JAMA
  5. */
  6. require_once '../Matrix.php';
  7. require_once 'Stats.php';
  8. /**
  9. * Example of use of Matrix Class, featuring magic squares.
  10. */
  11. class Benchmark {
  12. var $stat;
  13. /**
  14. * Simple function to replicate PHP 5 behaviour
  15. */
  16. function microtime_float() {
  17. list($usec, $sec) = explode(" ", microtime());
  18. return ((float)$usec + (float)$sec);
  19. } // function microtime_float()
  20. function displayStats($times = null) {
  21. $this->stat->setData($times);
  22. $stats = $this->stat->calcFull();
  23. echo '<table style="margin-left:32px;">';
  24. echo '<tr><td style="text-align:right;"><b>n:</b><td style="text-align:right;">' . $stats['count'] . ' </td></tr>';
  25. echo '<tr><td style="text-align:right;"><b>Mean:</b><td style="text-align:right;">' . $stats['mean'] . ' </td></tr>';
  26. echo '<tr><td style="text-align:right;"><b>Min.:</b><td style="text-align:right;">' . $stats['min'] . ' </td></tr>';
  27. echo '<tr><td style="text-align:right;"><b>Max.:</b><td style="text-align:right;">' . $stats['max'] . ' </td></tr>';
  28. echo '<tr><td style="text-align:right;"><b>&sigma;:</b><td style="text-align:right;">' . $stats['stdev'] . ' </td></tr>';
  29. echo '<tr><td style="text-align:right;"><b>Variance:</b><td style="text-align:right;">' . $stats['variance'] . ' </td></tr>';
  30. echo '<tr><td style="text-align:right;"><b>Range:</b><td style="text-align:right;">' . $stats['range'] . ' </td></tr>';
  31. echo '</table>';
  32. return $stats;
  33. } // function displayStats()
  34. function runEig($n = 4, $t = 100) {
  35. $times = array();
  36. for ($i = 0; $i < $t; ++$i) {
  37. $M = Matrix::random($n, $n);
  38. $start_time = $this->microtime_float();
  39. $E = new EigenvalueDecomposition($M);
  40. $stop_time = $this->microtime_float();
  41. $times[] = $stop_time - $start_time;
  42. }
  43. return $times;
  44. } // function runEig()
  45. function runLU($n = 4, $t = 100) {
  46. $times = array();
  47. for ($i = 0; $i < $t; ++$i) {
  48. $M = Matrix::random($n, $n);
  49. $start_time = $this->microtime_float();
  50. $E = new LUDecomposition($M);
  51. $stop_time = $this->microtime_float();
  52. $times[] = $stop_time - $start_time;
  53. }
  54. return $times;
  55. } // function runLU()
  56. function runQR($n = 4, $t = 100) {
  57. $times = array();
  58. for ($i = 0; $i < $t; ++$i) {
  59. $M = Matrix::random($n, $n);
  60. $start_time = $this->microtime_float();
  61. $E = new QRDecomposition($M);
  62. $stop_time = $this->microtime_float();
  63. $times[] = $stop_time - $start_time;
  64. }
  65. return $times;
  66. } // function runQR()
  67. function runCholesky($n = 4, $t = 100) {
  68. $times = array();
  69. for ($i = 0; $i < $t; ++$i) {
  70. $M = Matrix::random($n, $n);
  71. $start_time = $this->microtime_float();
  72. $E = new CholeskyDecomposition($M);
  73. $stop_time = $this->microtime_float();
  74. $times[] = $stop_time - $start_time;
  75. }
  76. return $times;
  77. } // function runCholesky()
  78. function runSVD($n = 4, $t = 100) {
  79. $times = array();
  80. for ($i = 0; $i < $t; ++$i) {
  81. $M = Matrix::random($n, $n);
  82. $start_time = $this->microtime_float();
  83. $E = new SingularValueDecomposition($M);
  84. $stop_time = $this->microtime_float();
  85. $times[] = $stop_time - $start_time;
  86. }
  87. return $times;
  88. } // function runSVD()
  89. function run() {
  90. $n = 8;
  91. $t = 16;
  92. $sum = 0;
  93. echo "<b>Cholesky decomposition: $t random {$n}x{$n} matrices</b><br />";
  94. $r = $this->displayStats($this->runCholesky($n, $t));
  95. $sum += $r['mean'] * $n;
  96. echo '<hr />';
  97. echo "<b>Eigenvalue decomposition: $t random {$n}x{$n} matrices</b><br />";
  98. $r = $this->displayStats($this->runEig($n, $t));
  99. $sum += $r['mean'] * $n;
  100. echo '<hr />';
  101. echo "<b>LU decomposition: $t random {$n}x{$n} matrices</b><br />";
  102. $r = $this->displayStats($this->runLU($n, $t));
  103. $sum += $r['mean'] * $n;
  104. echo '<hr />';
  105. echo "<b>QR decomposition: $t random {$n}x{$n} matrices</b><br />";
  106. $r = $this->displayStats($this->runQR($n, $t));
  107. $sum += $r['mean'] * $n;
  108. echo '<hr />';
  109. echo "<b>Singular Value decomposition: $t random {$n}x{$n} matrices</b><br />";
  110. $r = $this->displayStats($this->runSVD($n, $t));
  111. $sum += $r['mean'] * $n;
  112. return $sum;
  113. } // function run()
  114. public function __construct() {
  115. $this->stat = new Base();
  116. } // function Benchmark()
  117. } // class Benchmark (end MagicSquareExample)
  118. $benchmark = new Benchmark();
  119. switch($_REQUEST['decomposition']) {
  120. case 'cholesky':
  121. $m = array();
  122. for ($i = 2; $i <= 8; $i *= 2) {
  123. $t = 32 / $i;
  124. echo "<b>Cholesky decomposition: $t random {$i}x{$i} matrices</b><br />";
  125. $s = $benchmark->displayStats($benchmark->runCholesky($i, $t));
  126. $m[$i] = $s['mean'];
  127. echo "<br />";
  128. }
  129. echo '<pre>';
  130. foreach($m as $x => $y) {
  131. echo "$x\t" . 1000*$y . "\n";
  132. }
  133. echo '</pre>';
  134. break;
  135. case 'eigenvalue':
  136. $m = array();
  137. for ($i = 2; $i <= 8; $i *= 2) {
  138. $t = 32 / $i;
  139. echo "<b>Eigenvalue decomposition: $t random {$i}x{$i} matrices</b><br />";
  140. $s = $benchmark->displayStats($benchmark->runEig($i, $t));
  141. $m[$i] = $s['mean'];
  142. echo "<br />";
  143. }
  144. echo '<pre>';
  145. foreach($m as $x => $y) {
  146. echo "$x\t" . 1000*$y . "\n";
  147. }
  148. echo '</pre>';
  149. break;
  150. case 'lu':
  151. $m = array();
  152. for ($i = 2; $i <= 8; $i *= 2) {
  153. $t = 32 / $i;
  154. echo "<b>LU decomposition: $t random {$i}x{$i} matrices</b><br />";
  155. $s = $benchmark->displayStats($benchmark->runLU($i, $t));
  156. $m[$i] = $s['mean'];
  157. echo "<br />";
  158. }
  159. echo '<pre>';
  160. foreach($m as $x => $y) {
  161. echo "$x\t" . 1000*$y . "\n";
  162. }
  163. echo '</pre>';
  164. break;
  165. case 'qr':
  166. $m = array();
  167. for ($i = 2; $i <= 8; $i *= 2) {
  168. $t = 32 / $i;
  169. echo "<b>QR decomposition: $t random {$i}x{$i} matrices</b><br />";
  170. $s = $benchmark->displayStats($benchmark->runQR($i, $t));
  171. $m[$i] = $s['mean'];
  172. echo "<br />";
  173. }
  174. echo '<pre>';
  175. foreach($m as $x => $y) {
  176. echo "$x\t" . 1000*$y . "\n";
  177. }
  178. echo '</pre>';
  179. break;
  180. case 'svd':
  181. $m = array();
  182. for($i = 2; $i <= 8; $i *= 2) {
  183. $t = 32 / $i;
  184. echo "<b>Singular value decomposition: $t random {$i}x{$i} matrices</b><br />";
  185. $s = $benchmark->displayStats($benchmark->runSVD($i, $t));
  186. $m[$i] = $s['mean'];
  187. echo "<br />";
  188. }
  189. echo '<pre>';
  190. foreach($m as $x => $y) {
  191. echo "$x\t" . 1000*$y . "\n";
  192. }
  193. echo '</pre>';
  194. break;
  195. case 'all':
  196. $s = $benchmark->run();
  197. print("<br /><b>Total<b>: {$s}s<br />");
  198. break;
  199. default:
  200. ?>
  201. <ul>
  202. <li><a href="benchmark.php?decomposition=all">Complete Benchmark</a>
  203. <ul>
  204. <li><a href="benchmark.php?decomposition=cholesky">Cholesky</a></li>
  205. <li><a href="benchmark.php?decomposition=eigenvalue">Eigenvalue</a></li>
  206. <li><a href="benchmark.php?decomposition=lu">LU</a></li>
  207. <li><a href="benchmark.php?decomposition=qr">QR</a></li>
  208. <li><a href="benchmark.php?decomposition=svd">Singular Value</a></li>
  209. </ul>
  210. </li>
  211. </ul>
  212. <?php
  213. break;
  214. }