PHP Classes
elePHPant
Icontem

File: matrix.example.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Jon Lawrence  >  LW_Matrix  >  matrix.example.php  >  Download  
File: matrix.example.php
Role: Example script
Content type: text/plain
Description: Example usage
Class: LW_Matrix
Perform math operations with matrices
Author: By
Last change:
Date: 3 years ago
Size: 2,992 bytes
 

Contents

Class file image Download
<?php
/**
 * Example usage of the LW_Matrix class
 *
 * Will show a variety of methods and how to handle errors
 * 2012 Jon Lawrence
 */

require_once "matrix.class.php"; //require the LW_Matrix class

/**
 * Creating a matrix via constructor
 * | 1 2 3 |
 * | 4 5 6 |
 * | 7 8 9 |
 */
$myMatrix = new LW_Matrix("[1,2,3;4,5,6;7,8,9]");

//For the sake of ease in demonstration, everything will
//be 'preformatted' in html.
echo "<pre>\n";

//return if it's a square matrix. In this case, true.
$isSquare = $myMatrix->isSquare();

//use a try block to so you can catch if an operation fails
try {
   
//Get the inverse matrix
   
$invMatrix = $myMatrix->inverse();
} catch (
Exception $e) {
   
$eCode = $e->getCode();
    if(
$eCode==LW_MATRIX_E_NO_INVERSE) {
       
//Show your own error based on constant
       
echo "No inverse matrix exists\n";
       
//Show the error string provided by the class
       
echo $e->getMessage() ."\n";
    }
}

//use the __toString() method by default for printing
echo "\$myMatrix : ". $myMatrix ."\n";

//Transpose the matrix, use (false, false) as parameters to return LW_Matrix object
$tranMatrix = $myMatrix->transpose(false, false);
echo
"\$tranMatrix : ". $tranMatrix ."\n";

//Creating an identity matrix that is 3 by 3
$identMatrix = new LW_Matrix();
$identMatrix->createIdentity(3);
echo
"\$identMatrix : ". $identMatrix ."\n";

/**
 * Solving a set of linear equations using the LW_Matrix Class
 *
 * Use in a try block to avoid a fatal error, and possibly
 * provide the user a reason why it failed
 * Equations to use:
 * 3x + 2y - z = 1
 * 2x - 2y + 4z = -2
 * -1x + .5y - z = 0
 *
 * Matrices will then be:
 * | 3 2 -1 |
 * A = | 2 -2 4 |
 * | -1 .5 -1 |
 * for the variable, the solution set will be:
 * | 1 |
 * C = | -2 |
 * | 0 |
 *
 * Solving the set is defined by A^(-1)C, with the solution set
 * being in the form of
 * | x |
 * solution = | y |
 * | z |
 */
try {
   
$A = new LW_Matrix("[3,2,-1;2,-2,4;-1,.5,-1]");
   
$C = new LW_Matrix("[1;-2;0]");
   
//get the inverse of A
   
$invA = $A->inverse();
   
   
//now multiply the inverse by C
   
$solution = $invA->mpMatrix($C);
   
   
/**
     * If we want to break down the solution to their own
     * variables, do the following:
     */
   
    //first, remove the brackets
   
$sText = str_replace("[", "", $solution->toString());
   
$sText = str_replace("]", "", $sText);
   
   
//Now explode the contents to their own variables:
   
list($x,$y,$z) = explode(";", $sText);
   
   
//output the variables
   
echo "x = $x\ny = $y\nz = $z";
} catch (
Exception $e) {
   
//show the default string of the first thing to go wrong
   
echo "\n". $e->getMessage() ."\n";
}

/**
 * Now that you are familiar with how the class is used,
 * and have plenty of documentation to reference, go have fun!
 */

?>