2 include_once
"includes/header.php";
3 include_once
"includes/navbar.php";
5 <h2>
Magic Square Example</h2>
7 The Jama distribution comes with a magic square example that is used to
8 test and benchmark the LU, QR, SVD and symmetric Eig decompositions.
9 The example outputs a multi-column table with these column headings:
12 <table border=
'1' cellpadding=
'5' cellspacing=
'0' align=
'center'>
15 <td>Order of magic square.</td>
19 <td>Diagonal sum, should be the magic sum, (
n^3 +
n)/2.</td>
22 <td><b>max_eig</b></td>
23 <td>Maximum eigenvalue of (A + A
')/2, should equal trace.</td>
27 <td>Linear algebraic rank, should equal n if n is odd, be less than n if n is even.</td>
31 <td>L_2 condition number, ratio of singular values.</td>
34 <td><b>lu_res</b></td>
35 <td>test of LU factorization, norm1(L*U-A(p,:))/(n*eps).</td>
38 <td><b>qr_res</b></td>
39 <td>test of QR factorization, norm1(Q*R-A)/(n*eps).</td>
43 Running the Java-based version of the matix square example produces these results:
46 <table border='1
' align='center
'>
57 <td>3</td><td>15</td><td>15.000</td><td>3</td><td>4.330</td><td>0.000</td><td>11.333</td>
60 <td>4</td><td>34</td><td>34.000</td><td>3</td><td> Inf</td><td>0.000</td><td>13.500</td>
62 <td>5</td><td>65</td><td>65.000</td><td>5</td><td>5.462</td><td>0.000</td><td>14.400</td>
65 <td>6</td><td>111</td><td>111.000</td><td>5</td><td> Inf</td><td>5.333</td><td>16.000</td>
68 <td>7</td><td>175</td><td>175.000</td><td>7</td><td>7.111</td><td>2.286</td><td>37.714</td>
71 <td>8</td><td>260</td><td>260.000</td><td>3</td><td> Inf</td><td>0.000</td><td>59.000</td>
74 <td>9</td><td>369</td><td>369.000</td><td>9</td><td>9.102</td><td>7.111</td><td>53.333</td>
77 <td>10</td><td>505</td><td>505.000</td><td>7</td><td> Inf</td><td>3.200</td><td>159.200</td>
80 <td>11</td><td>671</td><td>671.000</td><td>11</td><td>11.102</td><td>2.909</td><td>215.273</td>
83 <td>12</td><td>870</td><td>870.000</td><td>3</td><td> Inf</td><td>0.000</td><td>185.333</td>
86 <td>13</td><td>1105</td><td>1105.000</td><td>13</td><td>13.060</td><td>4.923</td><td>313.846</td>
89 <td>14</td><td>1379</td><td>1379.000</td><td>9</td><td> Inf</td><td>4.571</td><td>540.571</td>
92 <td>15</td><td>1695</td><td>1695.000</td><td>15</td><td>15.062</td><td>4.267</td><td>242.133</td>
95 <td>16</td><td>2056</td><td>2056.000</td><td>3</td><td> Inf</td><td>0.000</td><td>488.500</td>
98 <td>17</td><td>2465</td><td>2465.000</td><td>17</td><td>17.042</td><td>7.529</td><td>267.294</td>
101 <td>18</td><td>2925</td><td>2925.000</td><td>11</td><td> Inf</td><td>7.111</td><td>520.889</td>
104 <td>19</td><td>3439</td><td>3439.000</td><td>19</td><td>19.048</td><td>16.842</td><td>387.368</td>
107 <td>20</td><td>4010</td><td>4010.000</td><td>3</td><td> Inf</td><td>14.400</td><td>584.800</td>
110 <td>21</td><td>4641</td><td>4641.000</td><td>21</td><td>21.035</td><td>6.095</td><td>1158.095</td>
113 <td>22</td><td>5335</td><td>5335.000</td><td>13</td><td> Inf</td><td>6.545</td><td>1132.364</td>
116 <td>23</td><td>6095</td><td>6095.000</td><td>23</td><td>23.037</td><td>11.130</td><td>1268.870</td>
119 <td>24</td><td>6924</td><td>6924.000</td><td>3</td><td> Inf</td><td>10.667</td><td>827.500</td>
122 <td>25</td><td>7825</td><td>7825.000</td><td>25</td><td>25.029</td><td>35.840</td><td>1190.400</td>
125 <td>26</td><td>8801</td><td>8801.000</td><td>15</td><td> Inf</td><td>4.923</td><td>1859.077</td>
128 <td>27</td><td>9855</td><td>9855.000</td><td>27</td><td>27.032</td><td>37.926</td><td>1365.333</td>
131 <td>28</td><td>10990</td><td>10990.000</td><td>3</td><td> Inf</td><td>34.286</td><td>1365.714</td>
134 <td>29</td><td>12209</td><td>12209.000</td><td>29</td><td>29.025</td><td>30.897</td><td>1647.448</td>
137 <td>30</td><td>13515</td><td>13515.000</td><td>17</td><td> Inf</td><td>8.533</td><td>2571.733</td>
140 <td>31</td><td>14911</td><td>14911.000</td><td>31</td><td>31.027</td><td>33.032</td><td>1426.581</td>
143 <td>32</td><td>16400</td><td>16400.000</td><td>3</td><td> Inf</td><td>0.000</td><td>1600.125</td>
146 <center>Elapsed Time = 0.710 seconds</center>
149 The magic square example does not fare well when <a href='../examples/
MagicSquareExample.php
'>run as a PHP script</a>. For a 32x32 matrix array
150 it takes around a second to complete just the last row of computations in the above table.
151 Hopefully this result will spur PHP developers to find optimizations and better attuned algorithms
152 to speed things up. Matrix algebra is a great testing ground for ideas about time and memory
153 performance optimation. Keep in perspective that PHP JAMA scripts are still plenty fast for use as
154 a tool for learning about matrix algebra and quickly extending your knowledge with new scripts
159 To learn more about the subject of magic squares you can visit the <a href='http:
160 You can also learn more by carefully examining the <code>
MagicSquareExample.php</code> source code below.
164 highlight_file(
"../examples/MagicSquareExample.php");
165 include_once
"includes/footer.php";