This line is simple HTML you should be able to read this, no matter if you have PHP or not on your server.
- Let's start by creating some matrix :
$id = new matrix(array(0=>array(1,2,3, 0, 1, 0)));
print_r($id->numbers);
$id->print_matrix();
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[4] => 1
)
)
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).
- Now, let's create the identity matrix :
$id = $id->eye(3,3);
$id->print_matrix();
- Now, let's create a matrix of ones :
$one = new matrix($id->numbers);
$one = $one->ones(3,3);
$one->print_matrix();
- Now, how many rows are in this matrix of ones ? (In case you can't count, there are three of them)
echo "The number of rows is : ".$one->get_num_rows();
And the output is :
The number of rows is : 3
- Now, we change the value of the north-west corner of the previous matrix :
$one->set_value(1, 3, 10);
$one->print_matrix();
- Below, we perform the addition
$one + $id :
$add = $one->plus($id);
$add->print_matrix();
- Here, we transpose the matrix
$add :
$a = $add->prime();
$a->print_matrix();
- 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) :
$sub = $a->minor_matrix($a, 1, 2);
$sub->print_matrix();
- Below, we compute the determinant of the matrix
$a :
echo "The determinant is : ".$a->det($a);
The determinant is : -5
- We check below if the matrix
$a is square (yes, it is) :
echo "This matrix is square (1 = true, 0 = false) : ".$a->is_square();
This matrix is square (1 = true, 0 = false) : 1
- Now, we compute the inverse of
$a :
$a_inv = $a->inv();
$a_inv->print_matrix();
| -0.6 | 0.2 | 0.2 |
| -1.6 | 1.2 | 0.2 |
| 3.8 | -1.6 | -0.6 |
- We check if this is the inverse by multiplying
$a by $a_inv :
$should_be_id = $a->times($a_inv);
$should_be_id->print_matrix();
| 1 | -2.22044604925E-16 | 0 |
| 0 | 1 | 1.11022302463E-16 |
| 0 | -4.4408920985E-16 | 1 |
- 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) :
$should_be_id->smooth(pow(10, -10));
$should_be_id->print_matrix();
This is much nicer. :)
- To compute the mean of each column :
$mean_a = $a->mean();
$mean_a->print_matrix();
| 4.33333333333 | 1.33333333333 | 1.33333333333 |
- To compute the max of each column :
$max_a = $a->mat_max();
$max_a->print_matrix();
- To compute the min of each column :
$min_a = $a->mat_min();
$min_a->print_matrix();
- To compute the element by element product :
$ptp = $a->p_times($id);
$ptp->print_matrix();
- To generate some random matrix from a uniform distribution :
$random_m = $a->random(3,3);
$random_m->print_matrix();
| 0.18 | 0.7 | 0.91 |
| 0.74 | 0.9 | 0.85 |
| 0.11 | 0.31 | 0.65 |
- How to generate a matrix of twos :
$two_m = $a->ones(3,3);
$two_m = $two_m->s_times(2);
$two_m->print_matrix();
- Perform element by element division :
$ptpd = $a->p_div($two_m);
$ptpd->print_matrix();
- Delete rows 2 to 3 in the previous matrix :
$ptpd->delete_rows(2,3);
$ptpd->print_matrix();
- Delete column 1 in the previous matrix :
$ptpd->delete_columns(1,1);
$ptpd->print_matrix();
- Element by element greater than comparison :
$comp = $a->gt($ptpd);
$comp->print_matrix();
Wrong dimensions in gt !!!
Ooops ! The matrices do not have the same dimensions... perhaps I sould pick another matrix to compare $a with...
$comp = $a->gt($two_m);
$comp->print_matrix();
- Obtain the upper triangular matrix of
$a :
$utrig = $a->utrig();
$utrig->print_matrix();
- Obtain the lower triangular matrix of
$a :
$ltrig = $a->ltrig();
$ltrig->print_matrix();
- Get the diagonal vector of some matrix (say,
$a_inv) :
$vec = $a_inv->diag();
$vec->print_matrix();
For any vector, you can also use the same function to generate a diagonal matrix :
$diag_m = $a_inv->diag($mean_a);
$diag_m->print_matrix();
| 4.33333333333 | 0 | 0 |
| 0 | 1.33333333333 | 0 |
| 0 | 0 | 1.33333333333 |
- To obtain the LU decomposition of a matrix
$sim (if you want this, I assume you know what you are doing) :
$sim = $a->ones(3,3);
$sim = $sim->plus($a->eye(3,3));
$lu = $sim->lu();
$lu->print_matrix();
| 1 | 0 | 0 | 2 | 1 | 1 |
| 0 | 1 | 0 | 0.5 | 1.5 | 0.5 |
| 0 | 0 | 1 | 0.5 | 0.333333333333 | 1.33333333333 |
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.
- To obtain each matrix of the LU decomposition separately, you need to use previously defined functions :
$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();
$L->print_matrix();
$U->print_matrix();
| 1 | 0 | 0 |
| 0.5 | 1 | 0 |
| 0.5 | 0.333333333333 | 1 |
| 2 | 1 | 1 |
| 0 | 1.5 | 0.5 |
| 0 | 0 | 1.33333333333 |