PHP Classes

File: linear.matrix.php

Recommend this page to a friend!
  Classes of Jon Lawrence   LW_Matrix   linear.matrix.php   Download  
File: linear.matrix.php
Role: Example script
Content type: text/plain
Description: Solve Linear Equations
Class: LW_Matrix
Perform math operations with matrices
Author: By
Last change:
Date: 12 years ago
Size: 4,589 bytes
 

Contents

Class file image Download
<?php

/**
 * This is fully functional page to solve linear equations
 * with the LW_Matrix class. It will provide the html
 * as well as do the required math and display it it a
 * semi-neat format.
 */

//The real work of this starts on line 127

//Make sure the Matrix class is included:
require_once "matrix.class.php";

//Create a function to turn a Matrix into a styled table
function matrixTable(LW_Matrix $matrix, $cap)
{
   
$mArray = $matrix->getArray();
   
$out = "<div class=\"LW_Matrix_float\">\n <table class=\"LW_Matrix_table\">\n";
   
$out .= " <caption class=\"LW_left\">$cap</caption>\n";
    foreach(
$mArray as $row) {
       
$out .= " <tr class=\"LW_Matrix_row\">\n";
        foreach(
$row as $col) {
            if(
floor($col)!= $col) {
               
$col1 = sprintf("%.2f", $col);
                if(
stristr($col1, ".00")) {
                   
//two decimal places didn't change the value, so don't have them
                   
$col1 = sprintf("%.0f", $col);
                }
            } else {
               
$col1 = $col;
            }
           
$out .= " <td class=\"LW_Matrix_col\">$col1</td>\n";
        }
       
$out .= " </tr>\n";
    }
   
$out .= " </table>\n</div>\n";
    return
$out;
}

//next we'll add the boring html/css header stuff
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Linear Algebra</title>
<style type="text/css">
.LW_Matrix_table {
    border: 1px black solid;
    border-collapse: collapse;
    padding: 5px;
}
.LW_Matrix_row {
    padding: 5px;
}
.LW_Matrix_col {
    border: 1px black dashed;
    padding: 5px;
    text-align:center;
}
.LW_Matrix_float {
    float: left;
    margin: 10px;
    vertical-align: middle;
}
.LW_Id_float {
    text-align:center;
    vertical-align: middle;
    float: left;
}
caption.LW_left {
    caption-side: left;
    vertical-align: middle;
}
.clearFloat {
    clear: both;
}
div.LW_output {
    border: 1px black solid;
    overflow: auto;
    width:600px;
}
</style></head>

<body>
<div> When entering your matrices, the format is as follows:
</div>

<?php
//Create sample Matrices to display format
$sample = new LW_Matrix("[1,2,3;4,5,6;7,8,9]");
$sol = new LW_Matrix("[1;2;3]");
$out = matrixTable($sample, "[1,2,3;4,5,6;7,8,9]=");
echo
$out;
$out = matrixTable($sol,"[1;2;3]=");
echo
$out;

//Get Post variables to put in form if they exist, otherwise set to ""
$coeff = $_POST['coeff'];
$consts = $_POST['consts'];

//Further explanation in HTML + HTML Form
?>
<div class="clearFloat">&nbsp;</div>
<pre>To solve for:
x - 3y + 3z = -4
2x + 3y - z = 15
4x - 3y - z = 19

Coefficient matrix = [1,-3,3;2,3,-1;4,-3,-1]
Constants matrix = [-4;15;19]
</pre>
<form name="theForm" method="post" action="">
  <label for="coeff">Coefficients in Matrix Form:</label>
  <input type="text" name="coeff" id="coeff" value="<?php echo $coeff; ?>" size="50">
  <br>
  <label for="consts">Constants in Matrix Form:</label>
  <input name="consts" type="text" id="consts" value="<?php echo $consts; ?>" size="15">
  <br>
  Get Variable solutions!
  <input type="submit" name="submit" id="submit" value="Submit">
</form>
<p>&nbsp;</p>
<div class="LW_output">
<?php

//Ok, form and everything displayed, now to do the math and display further output based on that.
//check to see if anything was submitted before trying to do the math:
if($coeff!="" && $consts!="")
{
   
//try/catch to we don't get a fatal error
   
try {
       
$A = new LW_Matrix($coeff);
       
$C = new LW_Matrix($consts);
       
       
//Get the inverse of A
       
$invA = $A->inverse();
       
       
//Multiply the inverse of A by C to get the solution set
       
$sols = $invA->mpMatrix($C);
       
//Now we'll display all 3 matrices so user can check input
       
echo "<p>Solution:</p>";
        echo
matrixTable($A, "A=");
        echo
matrixTable($C, "C=");
        echo
matrixTable($invA, "A<sup>-1</sup>=");
       
//clear the floats
       
echo "<div class=\"clearFloat\">&nbsp;</div>\n";
       
//now display the Solution with Caption for the Multiplication preformed
       
echo matrixTable($sols, "A<sup>-1</sup>C=");
    } catch (
Exception $e) {
        echo
"<p>Error:<br />". $e->getMessage() ."</p>";
       
//do nothing for now
   
}
}

//Finish the HTML to complete the page now...
?>
</div>
</body>
</html>