PHP Classes

File: examples.php

Recommend this page to a friend!
  Classes of Pier-André Bouchard St-Amant   Matrix new   examples.php   Download  
File: examples.php
Role: Example script
Content type: text/plain
Description: A few examples
Class: Matrix new
Perform operations to manipulate matrices
Author: By
Last change: Addition of new examples to illustrate new functionalities.
Date: 14 years ago
Size: 9,893 bytes
 

Contents

Class file image Download
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="fr" style="position: static; "><head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Examples with matrix class</title>
</head>
<body>
This line is simple HTML you should be able to read this, no matter if you have PHP or not on your server.<br><br>

<?php
include_once("matrix.php");
?>
<ol>
<li>Let's start by creating some matrix :<br>
<code>
$id = new matrix(array(0=>array(1,2,3, 0, 1, 0)));<br>
print_r($id->numbers);<br>
$id->print_matrix();<br><br>
</code>
<?php

$id
= new matrix(array(0=>array(1,2,3, 0, 1, 0)));
print_r($id->numbers);
$id->print_matrix();
//$id = $id->eye(3,3);

?>
<br>The first line creates an horizontal matrix [1,2,3, 0, 1, 0]. The second line prints the array of numbers created by the first line. One can notice that elements equal to zero are not stored. This is convention to save memory and increase efficiency of some routines (mutliplication, for instance) when the matrices are really big. Note that every get/set method handling the raw data in the class treats this transparently (e.g. : you will get a full matrix with zeros if you use get_data) The last line of code prints the matrix in a table, mostly for debugging purpose (and to illustrate the output of functions in this example).<br>

<br><li>Now, let's create the identity matrix : </li>
<code>
$id = $id->eye(3,3);<br>
$id->print_matrix();<br><br>
</code>
<?php
$id
= $id->eye(3,3);
$id->print_matrix();
?>


<br><li>Now, let's create a matrix of ones : <br><br>
<code>
$one = new matrix($id->numbers);<br>
$one = $one->ones(3,3);<br>
$one->print_matrix();<br><br>
</code>
<?php
//A 3x3 matrix of ones
$one = new matrix($id->numbers);
$one = $one->ones(3,3);
$one->print_matrix();
?>
<br><li>Now, how many rows are in this matrix of ones ? (In case you can't count, there are three of them)<br><br>

<code>
echo "The number of rows is : ".$one->get_num_rows();<br><br>
</code>
And the output is : <br>
<?php
//Print the number of rows
echo "<br>The number of rows is : ".$one->get_num_rows()."<br>";
?>
<br><li>Now, we change the value of the north-west corner of the previous matrix : <br>
<code>
$one->set_value(1, 3, 10);<br>
$one->print_matrix();<br><br>
</code>
<?php
//Set the value of 10 to the north-west corner
$one->set_value(1, 3, 10);
$one->print_matrix();
?>
<br>
<li>Below, we perform the addition <code>$one</code> + <code>$id</code> : <br><br>
<code>
$add = $one->plus($id);<br>
$add->print_matrix();<br><br>
</code>
<?php

//Addition of $one + $id
$add = $one->plus($id);
$add->print_matrix();
?>
<br><li>Here, we transpose the matrix <code>$add</code> : <br><br>
<code>
$a = $add->prime();<br>
$a->print_matrix();<br><br>
</code>
<?
//Compute the traspose of add (add')
$a = $add->prime();
$a->print_matrix();
?>
<br><li>Here, we find the minor matrix associated with column one and row two (that is, the resulting matrix after deletion of the first column and the second row) : <br><br>
<code>
$sub = $a->minor_matrix($a, 1, 2);<br>
$sub->print_matrix();<br><br>
</code>
<?php
//Find the minor_matrix associated to 1st column & 2nd row deleted :
$sub = $a->minor_matrix($a, 1, 2);
$sub->print_matrix();
?>
<br><li>Below, we compute the determinant of the matrix <code>$a</code> : <br><br>
<code>
echo "The determinant is : ".$a->det($a);<br><br>
</code>
<?php
//Compute the determinant of $a
echo "<br>The determinant is : ".$a->det($a)."<br>";
?>
<br><li>We check below if the matrix <code>$a</code> is square (yes, it is) : <br>
<code>
echo "This matrix is square (1 = true, 0 = false) : ".$a->is_square();<br><br>
</code>
<?php
//Check if $a is square
echo "This matrix is square (1 = true, 0 = false) : ".$a->is_square();
?>
<br><li>Now, we compute the inverse of <code>$a</code> : <br>
<code>
$a_inv = $a->inv();<br>
$a_inv->print_matrix();<br><br>
</code>
<?php
$a_inv
= $a->inv();
$a_inv->print_matrix();
?><br>
<br><li>We check if this is the inverse by multiplying <code>$a</code> by <code>$a_inv</code> :<br><br>
<code>
$should_be_id = $a->times($a_inv);<br>
$should_be_id->print_matrix();<br><br>
</code>
<?php
$should_be_id
= $a->times($a_inv);
$should_be_id->print_matrix();
?><br>
<br><li>Some components of the last matrix are not equal to zero due to some numerical errors. It would be nice if we could "smoothe" it them zero. That is the role of the so cleverly called function "smooth". Below, I smooth all values to zero if they do not differ from zero up to 10^(-10) : <br><br>
<code>
$should_be_id->smooth(pow(10, -10));<br>
$should_be_id->print_matrix();<br><br>
</code>
<?php
$should_be_id
->smoothe(pow(10, -10));
$should_be_id->print_matrix();
?><br>This is much nicer. :)<br>

</li>
<br><li>To compute the mean of each column : <br>
<code>
$mean_a = $a->mean();<br>
$mean_a->print_matrix();<br><br>
</code>
<?php
//Compute the mean of each column.
$mean_a = $a->mean();
$mean_a->print_matrix();
?>
<br><li>To compute the max of each column : <br>
<code>
$max_a = $a->mat_max();<br>
$max_a->print_matrix();<br><br>
</code>
<?php
//Find the max of each column.
$max_a = $a->mat_max();
$max_a->print_matrix();
?>
<br><li>To compute the min of each column : <br>
<code>
$min_a = $a->mat_min();<br>
$min_a->print_matrix();<br><br>
</code>
<?php
//Find the min of each column.
$min_a = $a->mat_min();
$min_a->print_matrix();
?>
<br><li>To compute the element by element product : <br>
<code>
$ptp = $a->p_times($id);<br>
$ptp->print_matrix();<br><br>
</code>
<?php
//Perform point to point multiplication :
$ptp = $a->p_times($id);
$ptp->print_matrix();
?>
<br><li>To generate some random matrix from a uniform distribution : <br>
<code>
$random_m = $a->random(3,3);<br>
$random_m->print_matrix();<br><br>
</code>
<?php
//Generate a random matrix :
$random_m = $a->random(3,3);
$random_m->print_matrix();
?>
<br><li>How to generate a matrix of twos : <br>
<code>
$two_m = $a->ones(3,3);<br>
$two_m = $two_m->s_times(2);<br>
$two_m->print_matrix();<br><br>
</code>
<?php
//Generate a matrix of twos :
$two_m = $a->ones(3,3);
$two_m = $two_m->s_times(2);
$two_m->print_matrix();
?>
<br><li>Perform element by element division : <br>
<code>
$ptpd = $a->p_div($two_m);<br>
$ptpd->print_matrix();<br><br>
</code>
<?php
//Perform point to point division
$ptpd = $a->p_div($two_m);
$ptpd->print_matrix();
?>
<br><li>Delete rows 2 to 3 in the previous matrix : <br>
<code>
$ptpd->delete_rows(2,3);<br>
$ptpd->print_matrix();<br><br>
</code>
<?php
//Delete rows from 2 to 3 of matrix $ptpd :
$ptpd->delete_rows(2,3);
$ptpd->print_matrix();
?>
<br><li>Delete column 1 in the previous matrix :<br>
<code>
$ptpd->delete_columns(1,1);<br>
$ptpd->print_matrix();<br><br>
</code>
<?php
//Delete column 1 of matrix $ptpd :
$ptpd->delete_columns(1,1);
$ptpd->print_matrix();
?>
<br><li>Element by element greater than comparison : <br>
<code>
$comp = $a->gt($ptpd);<br>
$comp->print_matrix();<br><br>
</code>
<?php
//Are the elements of $a greater than those of $id ?
$comp = $a->gt($ptpd);
$comp->print_matrix();
?>
Ooops ! The matrices do not have the same dimensions... perhaps I sould pick another matrix to compare <code>$a</code> with...<br><br>
<code>
$comp = $a->gt($two_m);<br>
$comp->print_matrix();<br><br>
</code>
<?php
//Are the elements of $a greater than those of $id ?
$comp = $a->gt($two_m);
$comp->print_matrix();
?>
<br><li>Obtain the upper triangular matrix of <code>$a</code> : <br>
<code>
$utrig = $a->utrig();<br>
$utrig->print_matrix();<br><br>
</code>
<?php
//Are the elements of $a greater than those of $id ?
$utrig = $a->utrig();
$utrig->print_matrix();
?>
<br><li>Obtain the lower triangular matrix of <code>$a</code> : <br>
<code>
$ltrig = $a->ltrig();<br>
$ltrig->print_matrix();<br><br>
</code>
<?php
//Are the elements of $a greater than those of $id ?
$utrig = $a->ltrig();
$utrig->print_matrix();
?>
<br><li>Get the diagonal vector of some matrix (say, <code>$a_inv</code>) : <br>
<code>
$vec = $a_inv->diag();<br>
$vec->print_matrix();<br><br>
</code>
<?php
//Are the elements of $a greater than those of $id ?
$vec = $a_inv->diag();
$vec->print_matrix();
?>
<br>For any vector, you can also use the same function to generate a diagonal matrix : <br>
<code>
$diag_m = $a_inv->diag($mean_a);<br>
$diag_m->print_matrix();<br><br>
</code>
<?php
$diag_m
= $a_inv->diag($mean_a);
$diag_m->print_matrix();
?>

<br><li>To obtain the LU decomposition of a matrix <code>$sim</code> (if you want this, I assume you know what you are doing) : <br>
<code>
$sim = $a->ones(3,3);<br>
$sim = $sim->plus($a->eye(3,3));<br>
$lu = $sim->lu();<br>
$lu->print_matrix();<br>
</code>
<?php
//Are the elements of $a greater than those of $id ?
$sim = $a->ones(3,3);
$sim = $sim->plus($a->eye(3,3));
$lu = $sim->lu();
$lu->print_matrix();
?>
<br>The first 3x3 matrix is the permutation matrix (the identity, in this case). The second 3x3 matrix is the matrices L and U stacked together, where the ones on the diagonal of the Lower matrix are omitted.<br>


<br><li>To obtain each matrix of the LU decomposition separately, you need to use previously defined functions : <br>
<code>
$P = $lu->get_columns(1,3);<br>
$lu = $lu->get_columns(4, 6);<br>
$L = $lu->ltrig();<br>
$L = $L->plus($L->eye(3,3));<br>
$U = $lu->utrig();<br>
$d = $lu->diag();<br>
$U = $U->plus($d->diag($d->prime()));<br>
$P->print_matrix();<br>
$L->print_matrix();<br>
$U->print_matrix();<br><br>
</code>
<?php
$P
= $lu->get_columns(1,3);
$lu = $lu->get_columns(4, 6);
$L = $lu->ltrig();
$L = $L->plus($L->eye(3,3));
$U = $lu->utrig();
$d = $lu->diag();
$U = $U->plus($d->diag($d->prime()));
$P->print_matrix();
echo
"<br>";
$L->print_matrix();
echo
"<br>";
$U->print_matrix();
echo
"<br><br>";
?>
<br><br></li>
</ol>

</body></html>