This line is simple HTML you should be able to read this, no matter if you have PHP or not on your server.

  1. 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 ) )
    123010

    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).

  2. Now, let's create the identity matrix :
  3. $id = $id->eye(3,3);
    $id->print_matrix();

    100
    010
    001

  4. Now, let's create a matrix of ones :

    $one = new matrix($id->numbers);
    $one = $one->ones(3,3);
    $one->print_matrix();

    111
    111
    111

  5. 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

  6. Now, we change the value of the north-west corner of the previous matrix :
    $one->set_value(1, 3, 10);
    $one->print_matrix();

    1110
    111
    111

  7. Below, we perform the addition $one + $id :

    $add = $one->plus($id);
    $add->print_matrix();

    2110
    121
    112

  8. Here, we transpose the matrix $add :

    $a = $add->prime();
    $a->print_matrix();

    211
    121
    1012

  9. 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();

    11
    12

  10. Below, we compute the determinant of the matrix $a :

    echo "The determinant is : ".$a->det($a);


    The determinant is : -5

  11. 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
  12. Now, we compute the inverse of $a :
    $a_inv = $a->inv();
    $a_inv->print_matrix();

    -0.60.20.2
    -1.61.20.2
    3.8-1.6-0.6


  13. 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.2204460492503E-160
    011.1102230246252E-16
    0-4.4408920985006E-161


  14. 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();

    100
    010
    001

    This is much nicer. :)

  15. To compute the mean of each column :
    $mean_a = $a->mean();
    $mean_a->print_matrix();

    4.33333333333331.33333333333331.3333333333333

  16. To compute the max of each column :
    $max_a = $a->mat_max();
    $max_a->print_matrix();

    1022

  17. To compute the min of each column :
    $min_a = $a->mat_min();
    $min_a->print_matrix();

    111

  18. To compute the element by element product :
    $ptp = $a->p_times($id);
    $ptp->print_matrix();

    200
    020
    002

  19. To generate some random matrix from a uniform distribution :
    $random_m = $a->random(3,3);
    $random_m->print_matrix();

    0.590.540.04
    0.70.880.25
    0.570.370.58

  20. How to generate a matrix of twos :
    $two_m = $a->ones(3,3);
    $two_m = $two_m->s_times(2);
    $two_m->print_matrix();

    222
    222
    222

  21. Perform element by element division :
    $ptpd = $a->p_div($two_m);
    $ptpd->print_matrix();

    10.50.5
    0.510.5
    50.51

  22. Delete rows 2 to 3 in the previous matrix :
    $ptpd->delete_rows(2,3);
    $ptpd->print_matrix();

    10.50.5

  23. Delete column 1 in the previous matrix :
    $ptpd->delete_columns(1,1);
    $ptpd->print_matrix();

    0.50.5

  24. Element by element greater than comparison :
    $comp = $a->gt($ptpd);
    $comp->print_matrix();

    Wrong dimensions in gt !!!

    0
    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();

    000
    000
    100

  25. Obtain the upper triangular matrix of $a :
    $utrig = $a->utrig();
    $utrig->print_matrix();

    011
    001
    000

  26. Obtain the lower triangular matrix of $a :
    $ltrig = $a->ltrig();
    $ltrig->print_matrix();

    000
    100
    1010

  27. Get the diagonal vector of some matrix (say, $a_inv) :
    $vec = $a_inv->diag();
    $vec->print_matrix();

    -0.6
    1.2
    -0.6

    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.333333333333300
    01.33333333333330
    001.3333333333333

  28. 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();
    100211
    0100.51.50.5
    0010.50.333333333333331.3333333333333

    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.

  29. 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();

    100
    010
    001

    100
    0.510
    0.50.333333333333331

    211
    01.50.5
    001.3333333333333