The Jama distribution comes with a magic square example that is used to test and benchmark the LU, QR, SVD and symmetric Eig decompositions. The example outputs a multi-column table with these column headings:
n | Order of magic square. |
trace | Diagonal sum, should be the magic sum, (n^3 + n)/2. |
max_eig | Maximum eigenvalue of (A + A')/2, should equal trace. |
rank | Linear algebraic rank, should equal n if n is odd, be less than n if n is even. |
cond | L_2 condition number, ratio of singular values. |
lu_res | test of LU factorization, norm1(L*U-A(p,:))/(n*eps). |
qr_res | test of QR factorization, norm1(Q*R-A)/(n*eps). |
Running the Java-based version of the matix square example produces these results:
n | trace | max_eig | rank | cond | lu_res | qr_res |
---|---|---|---|---|---|---|
3 | 15 | 15.000 | 3 | 4.330 | 0.000 | 11.333 |
4 | 34 | 34.000 | 3 | Inf | 0.000 | 13.500 |
5 | 65 | 65.000 | 5 | 5.462 | 0.000 | 14.400 |
6 | 111 | 111.000 | 5 | Inf | 5.333 | 16.000 |
7 | 175 | 175.000 | 7 | 7.111 | 2.286 | 37.714 |
8 | 260 | 260.000 | 3 | Inf | 0.000 | 59.000 |
9 | 369 | 369.000 | 9 | 9.102 | 7.111 | 53.333 |
10 | 505 | 505.000 | 7 | Inf | 3.200 | 159.200 |
11 | 671 | 671.000 | 11 | 11.102 | 2.909 | 215.273 |
12 | 870 | 870.000 | 3 | Inf | 0.000 | 185.333 |
13 | 1105 | 1105.000 | 13 | 13.060 | 4.923 | 313.846 |
14 | 1379 | 1379.000 | 9 | Inf | 4.571 | 540.571 |
15 | 1695 | 1695.000 | 15 | 15.062 | 4.267 | 242.133 |
16 | 2056 | 2056.000 | 3 | Inf | 0.000 | 488.500 |
17 | 2465 | 2465.000 | 17 | 17.042 | 7.529 | 267.294 |
18 | 2925 | 2925.000 | 11 | Inf | 7.111 | 520.889 |
19 | 3439 | 3439.000 | 19 | 19.048 | 16.842 | 387.368 |
20 | 4010 | 4010.000 | 3 | Inf | 14.400 | 584.800 |
21 | 4641 | 4641.000 | 21 | 21.035 | 6.095 | 1158.095 |
22 | 5335 | 5335.000 | 13 | Inf | 6.545 | 1132.364 |
23 | 6095 | 6095.000 | 23 | 23.037 | 11.130 | 1268.870 |
24 | 6924 | 6924.000 | 3 | Inf | 10.667 | 827.500 |
25 | 7825 | 7825.000 | 25 | 25.029 | 35.840 | 1190.400 |
26 | 8801 | 8801.000 | 15 | Inf | 4.923 | 1859.077 |
27 | 9855 | 9855.000 | 27 | 27.032 | 37.926 | 1365.333 |
28 | 10990 | 10990.000 | 3 | Inf | 34.286 | 1365.714 |
29 | 12209 | 12209.000 | 29 | 29.025 | 30.897 | 1647.448 |
30 | 13515 | 13515.000 | 17 | Inf | 8.533 | 2571.733 |
31 | 14911 | 14911.000 | 31 | 31.027 | 33.032 | 1426.581 |
32 | 16400 | 16400.000 | 3 | Inf | 0.000 | 1600.125 |
The magic square example does not fare well when run as a PHP script. For a 32x32 matrix array it takes around a second to complete just the last row of computations in the above table. Hopefully this result will spur PHP developers to find optimizations and better attuned algorithms to speed things up. Matrix algebra is a great testing ground for ideas about time and memory performance optimation. Keep in perspective that PHP JAMA scripts are still plenty fast for use as a tool for learning about matrix algebra and quickly extending your knowledge with new scripts to apply knowledge.
To learn more about the subject of magic squares you can visit the Drexel Math Forum on Magic Squares.
You can also learn more by carefully examining the MagicSquareExample.php
source code below.
<?php
/**
* @package JAMA
*/
require_once "../Matrix.php";
/**
* Example of use of Matrix Class, featuring magic squares.
*/
class MagicSquareExample {
/**
* Generate magic square test matrix.
* @param int n dimension of matrix
*/
function magic($n) {
// Odd order
if (($n % 2) == 1) {
$a = ($n+1)/2;
$b = ($n+1);
for ($j = 0; $j < $n; ++$j)
for ($i = 0; $i < $n; ++$i)
$M[$i][$j] = $n*(($i+$j+$a) % $n) + (($i+2*$j+$b) % $n) + 1;
// Doubly Even Order
} else if (($n % 4) == 0) {
for ($j = 0; $j < $n; ++$j) {
for ($i = 0; $i < $n; ++$i) {
if ((($i+1)/2)%2 == (($j+1)/2)%2)
$M[$i][$j] = $n*$n-$n*$i-$j;
else
$M[$i][$j] = $n*$i+$j+1;
}
}
// Singly Even Order
} else {
$p = $n/2;
$k = ($n-2)/4;
$A = $this->magic($p);
$M = array();
for ($j = 0; $j < $p; ++$j) {
for ($i = 0; $i < $p; ++$i) {
$aij = $A->get($i,$j);
$M[$i][$j] = $aij;
$M[$i][$j+$p] = $aij + 2*$p*$p;
$M[$i+$p][$j] = $aij + 3*$p*$p;
$M[$i+$p][$j+$p] = $aij + $p*$p;
}
}
for ($i = 0; $i < $p; ++$i) {
for ($j = 0; $j < $k; ++$j) {
$t = $M[$i][$j];
$M[$i][$j] = $M[$i+$p][$j];
$M[$i+$p][$j] = $t;
}
for ($j = $n-$k+1; $j < $n; ++$j) {
$t = $M[$i][$j];
$M[$i][$j] = $M[$i+$p][$j];
$M[$i+$p][$j] = $t;
}
}
$t = $M[$k][0]; $M[$k][0] = $M[$k+$p][0]; $M[$k+$p][0] = $t;
$t = $M[$k][$k]; $M[$k][$k] = $M[$k+$p][$k]; $M[$k+$p][$k] = $t;
}
return new Matrix($M);
}
/**
* Simple function to replicate PHP 5 behaviour
*/
function microtime_float() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
/**
* Tests LU, QR, SVD and symmetric Eig decompositions.
*
* n = order of magic square.
* trace = diagonal sum, should be the magic sum, (n^3 + n)/2.
* max_eig = maximum eigenvalue of (A + A')/2, should equal trace.
* rank = linear algebraic rank, should equal n if n is odd,
* be less than n if n is even.
* cond = L_2 condition number, ratio of singular values.
* lu_res = test of LU factorization, norm1(L*U-A(p,:))/(n*eps).
* qr_res = test of QR factorization, norm1(Q*R-A)/(n*eps).
*/
function main() {
?>
<p>Test of Matrix Class, using magic squares.</p>
<p>See MagicSquareExample.main() for an explanation.</p>
<table border='1' cellspacing='0' cellpadding='4'>
<tr>
<th>n</th>
<th>trace</th>
<th>max_eig</th>
<th>rank</th>
<th>cond</th>
<th>lu_res</th>
<th>qr_res</th>
</tr>
<?php
$start_time = $this->microtime_float();
$eps = pow(2.0,-52.0);
for ($n = 3; $n <= 6; ++$n) {
echo "<tr>";
echo "<td align='right'>$n</td>";
$M = $this->magic($n);
$t = (int) $M->trace();
echo "<td align='right'>$t</td>";
$O = $M->plus($M->transpose());
$E = new EigenvalueDecomposition($O->times(0.5));
$d = $E->getRealEigenvalues();
echo "<td align='right'>".$d[$n-1]."</td>";
$r = $M->rank();
echo "<td align='right'>".$r."</td>";
$c = $M->cond();
if ($c < 1/$eps)
echo "<td align='right'>".sprintf("%.3f",$c)."</td>";
else
echo "<td align='right'>Inf</td>";
$LU = new LUDecomposition($M);
$L = $LU->getL();
$U = $LU->getU();
$p = $LU->getPivot();
// Java version: R = L.times(U).minus(M.getMatrix(p,0,n-1));
$S = $L->times($U);
$R = $S->minus($M->getMatrix($p,0,$n-1));
$res = $R->norm1()/($n*$eps);
echo "<td align='right'>".sprintf("%.3f",$res)."</td>";
$QR = new QRDecomposition($M);
$Q = $QR->getQ();
$R = $QR->getR();
$S = $Q->times($R);
$R = $S->minus($M);
$res = $R->norm1()/($n*$eps);
echo "<td align='right'>".sprintf("%.3f",$res)."</td>";
echo "</tr>";
}
echo "<table>";
echo "<br />";
$stop_time = $this->microtime_float();
$etime = $stop_time - $start_time;
echo "<p>Elapsed time is ". sprintf("%.4f",$etime) ." seconds.</p>";
}
}
$magic = new MagicSquareExample();
$magic->main();
?>