[ index.php ] [ docs.php ] [ package.php ] [ test.php ] [ example.php ] [ download.php ]

Magic Square Example

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
Elapsed Time = 0.710 seconds

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.