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
31515.00034.3300.00011.333
43434.0003 Inf0.00013.500
56565.00055.4620.00014.400
6111111.0005 Inf5.33316.000
7175175.00077.1112.28637.714
8260260.0003 Inf0.00059.000
9369369.00099.1027.11153.333
10505505.0007 Inf3.200159.200
11671671.0001111.1022.909215.273
12870870.0003 Inf0.000185.333
1311051105.0001313.0604.923313.846
1413791379.0009 Inf4.571540.571
1516951695.0001515.0624.267242.133
1620562056.0003 Inf0.000488.500
1724652465.0001717.0427.529267.294
1829252925.00011 Inf7.111520.889
1934393439.0001919.04816.842387.368
2040104010.0003 Inf14.400584.800
2146414641.0002121.0356.0951158.095
2253355335.00013 Inf6.5451132.364
2360956095.0002323.03711.1301268.870
2469246924.0003 Inf10.667827.500
2578257825.0002525.02935.8401190.400
2688018801.00015 Inf4.9231859.077
2798559855.0002727.03237.9261365.333
281099010990.0003 Inf34.2861365.714
291220912209.0002929.02530.8971647.448
301351513515.00017 Inf8.5332571.733
311491114911.0003131.02733.0321426.581
321640016400.0003 Inf0.0001600.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.