Created
January 25, 2018 01:30
-
-
Save ggorlen/e5d1c63a3fb3fc05716fa005a18c9ed7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (isset($_GET['source'])) { | |
highlight_file($_SERVER['SCRIPT_FILENAME']); | |
exit; | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang='en'> | |
<head> | |
<title>Matrix multiplier</title> | |
<style> | |
body { | |
font-family: monospace; | |
font-size: 16px; | |
} | |
input { | |
font-family: monospace; | |
} | |
table { | |
border-collapse: collapse; | |
} | |
td { | |
border: 1px solid #888; | |
text-align: right; | |
padding: 10px; | |
} | |
</style> | |
</head> | |
<body> | |
<div style="margin: 10px; width: 300px;"> | |
<h2>Matrix multiplier</h2> | |
<p> | |
Matrix contents delimited by spaces and line breaks. Ex: | |
<br> | |
<br>4 5 6 5 8 | |
<br>8 -1 3 3 5 | |
<br> 2 9 | |
</p> | |
<form id="input" method=POST action="matrixmult.php"> | |
<textarea autofocus rows=5 cols=10 name="matrix1"><?php | |
if (count($_POST) === 2 || | |
isset($_POST['matrix1'])) { | |
print trim($_POST['matrix1']); | |
} | |
?></textarea> | |
<textarea rows=5 cols=10 name="matrix2"><?php | |
if (count($_POST) === 2 || | |
isset($_POST['matrix2'])) { | |
print trim($_POST['matrix2']); | |
} | |
?></textarea> | |
<br> | |
<input type="submit"> | |
</form> | |
<br> | |
<?php | |
(function() { | |
/* matrix multiplier | |
* | |
* http://www.purplemath.com/modules/mtrxmult2.htm | |
*/ | |
// abort if no data submitted | |
if (!count($_POST) === 2 || | |
!isset($_POST['matrix1']) || | |
!isset($_POST['matrix2'])) return; | |
// grab input data and parse | |
$matrix1Raw = explode("\n", trim($_POST['matrix1'])); | |
$matrix2Raw = explode("\n", trim($_POST['matrix2'])); | |
$matrix1 = []; | |
$matrix2 = []; | |
$result = []; | |
for ($i = 0; $i < sizeof($matrix1Raw); $i++) { | |
array_push($matrix1, | |
array_map('floatval', explode(" ", trim($matrix1Raw[$i]))) | |
); | |
} | |
for ($i = 0; $i < sizeof($matrix2Raw); $i++) { | |
array_push($matrix2, | |
array_map('floatval', explode(" ", trim($matrix2Raw[$i]))) | |
); | |
} | |
// validation check | |
if (sizeof($matrix1[0]) !== sizeof($matrix2)) { | |
print "<em>Error: number of columns in matrix A | |
must equal the number of rows in matrix B.</em"; | |
return; | |
} | |
// calculate product of matrices | |
for ($row = 0; $row < sizeof($matrix1); $row++) { | |
for ($col = 0; $col < sizeof($matrix2[0]); $col++) { | |
$total = 0; | |
for ($i = 0; $i < sizeof($matrix2); $i++) { | |
if (!isset($matrix2[$i][$col]) || !isset($matrix1[$row][$i])) { | |
print "<em>Error: invalid input.</em>"; | |
return; | |
} | |
$total += $matrix2[$i][$col] * $matrix1[$row][$i]; | |
} | |
$result[$row][$col] = $total; | |
} | |
} | |
// print result matrix | |
print "<table>"; | |
for ($i = 0; $i < sizeof($result); $i++) { | |
print "<tr>"; | |
for ($j = 0; $j < sizeof($result[$i]); $j++) { | |
print "<td>" . $result[$i][$j] . "</td>"; | |
} | |
print "</tr>"; | |
} | |
print "</table>"; | |
})(); | |
?> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment