Une classe PHP pour l'algèbre linéaire.


Class matrix


Files


Source code here
.
Examples here.


Description

  • A class for operations on matrices
  • See the file "changes" for an history of the changes on the class.
  • Comments :
    1. All mathematical functions are written to ressemble paper algebra :
    2. AxB --> $A->times($B);
    3. A-B --> $A->minus($B);
    4. A^(-1) --> $A->inv();
    5. A' --> $A->prime();
    6. det(A) --> $A->det($A);
    7. etc.
    8. This restricts nested calls, but augment readability.
  • The algorithm for matrix inversion is based on PLU decomposition. The algorithm performs relatively well numerically, but it requires an order of 2*n^2 x sizeof(doubles) of memory space.
  • Error messages are echoed to the standard output (likely to be your webpage)

    Class Variable Summary



    Method Summary


    Method Detail

      matrix::matrix

      matrix(
      array
      $numbers );

      Parameters

      $numbers

      [, $numRows

      , $numColumns]

      Remarks

      $numbers[$i][$j] is a two-dimensionnal array of numbers where $i is the depth and $j is the width. If $numRows and $numColumns are specified, any column/row that is not defined by $numbers will be filled with zeros.

      [ Top ]


      matrix::eye

      eye(
      int $n, int $m );

      Parameters

      $n

      $m

      Remarks

      Builds an $n by $m matrix with an identity matrix of size min($n, $m) in the north-east corner and puts zeros elsewhere


      matrix::ones

      ones(
      int $n, int $m );

      Parameters

      $n

      $m

      Remarks

      Builds an $n by $m matrix of ones.


      matrix::zeros

      zeros(
      int $n, int $m );

      Parameters

      $n

      $m

      Remarks

      Builds an $n by $m matrix of zeros.


      matrix::random

      random(
      int $n, int $m );

      Parameters

      $n

      $m

      Remarks

      Builds an $n by $m matrix of uniformly distributed random numbers (0,1).


      matrix::diag

      diag(
      matrix $vector);

      Parameters

      $vector

      Remarks

      Builds a diagonal matrix with $vector on the diagonal. $vector must have one of its dimensions equal to one.


      matrix::get_num_rows

      get_num_rows();

      Remarks

      Returns the number of rows of this matrix.


      matrix::get_num_columns

      get_num_columns();

      Remarks

      Returns the number of columns of this matrix.


      matrix::get_data

      get_data();

      Remarks

      Returns the array of numbers of the data.


      matrix::get_value

      get_value(int $i, int $j);

      Remarks

      Returns the value at position $i, $j.


      matrix::get_rows

      get_rows(int $start, int $stop);

      Remarks

      Returns the rows starting at position $start and finishing at position $stop. $start and $stop must be within the number of rows of the matrix.


      matrix::get_columns

      get_columns(int $start, int $stop);

      Remarks

      Returns the columns starting at position $start and finishing at position $stop. $start and $stop must be within the number of rows of the matrix.


      matrix::set_data

      set_data(array $numbers);

      Parameters

      $numbers

      Remarks

      Sets the numbers of the matrix. $numbers[$i][$j] is a two-dimensionnal array of numbers where $i is the depth and $j is the width.


      matrix::set_value

      set_value(int $i, int $jfloat $value);

      Parameters

      $i

      $j

      $value

      Remarks

      Sets the value of the entry $i, $j of the matrix.


      matrix::smoothe

      smoothe(floar $tolerance);

      Parameters

      $tolerance

      Remarks

      Sets the all values of $this within $tolerance to zero to zero.


      matrix::set_columns

      set_columns(int $start, int $stopmatrix $block_matrix);

      Parameters

      $start

      $stop

      $bloc_matrix

      Remarks

      Sets the columns starting at position $start and finishing at position $stop in the matrix. $start and $stop must be within the number of columns of the matrix.


      matrix::set_rows

      set_rows(int $start, int $stopmatrix $block_matrix);

      Parameters

      $start

      $stop

      $block_matrix

      Remarks

      Sets the rows starting at position $start and finishing at position $stop in the matrix. $start and $stop must be within the number of rows of the matrix.


      matrix::delete_columns

      delete_columns(int $start, int $stop);

      Parameters

      $start

      $stop

      Remarks

      Deletes the columns starting at position $start and finishing at position $stop in the matrix. $start and $stop must be within the number of columns of the matrix.


      matrix::delete_rows

      delete_rows(int $start, int $stop);

      Parameters

      $start

      $stop

      Remarks

      Deletes the rows starting at position $start and finishing at position $stop in the matrix. $start and $stop must be within the number of rows of the matrix.


      matrix::utrig

      utrig();

      Parameters

      Remarks

      Returns the upper triangular matrix of $this (without the diagonal).


      matrix::ltrig

      ltrig();

      Parameters

      Remarks

      Returns the lower triangular matrix of $this (without the diagonal).


      matrix::diag

      diag();

      Parameters

      Remarks

      Returns the diagonal vector of $this.


      matrix::is_square

      is_square();

      Remarks

      Checks if depth == width.


      matrix::size_equal

      size_equal(matrix $someMatrix);

      Parameters

      $someMatrix

      Remarks

      Checks if this matrix has the same size as $someMatrix


      matrix::plus

      plus(matrix $someMatrix);

      Parameters

      $someMatrix

      Remarks

      Adds this matrix with $someMatrix. Both matrices must have the same size.


      matrix::minus

      minus(matrix $someMatrix);

      Parameters

      $someMatrix

      Remarks

      Substracts $someMatrix from this matrix. Both matrices must have the same size.


      matrix::times

      times(matrix $someMatrix);

      Parameters

      $someMatrix

      Remarks

      Right-multiply this matrix by $someMatrix. The length of this matrix must equal the depth of $someMatrix.


      matrix::s_times

      s_times(float $someScalar);

      Parameters

      $someScalar

      Remarks

      Scalar multiplication of this matrix.


      matrix::det

      det(matrix $someMatrix);

      Parameters

      $someMatrix

      Remarks

      Computes the determinant of $someMatrix (which must be square).


      matrix::minor_matrix

      minor_matrix(matrix $someMatrix, int $row, int $column);

      Parameters

      $someMatrix

      $row

      $column

      Remarks

      Finds the matrix used for computing the minor of a factor (e.g. : the original matrix where $column and $row are deleted).


      matrix::prime

      prime();

      Remarks

      Transposes this matrix.


      matrix::lu

      lu();

      Remarks

      Returns a 2n x n matrix containing the PLU decomposition of this matrix. The matrix must be square. The first n x n matrix contains the permutation matrix and the second n x n matrix contains the "stacked" L and U matrices. Since elements on the diagonal of the L matrix, these are deleted by convention (and thus, the diagonal of the second n x n matrix is the diagonal of the U matrix). See the file example.php for an example.


      matrix::inv

      inv();

      Remarks

      Finds the inverse of the matrix by PLU decomposition. The matrix must be square.


      matrix::p_times

      p_times(matrix $someMatrix);

      Parameters

      $someMatrix

      Remarks

      Performs element by element multiplication. Both matrices must have the same size.


      matrix::p_div

      p_div(matrix $someMatrix);

      Parameters

      $someMatrix

      Remarks

      Performs element by element division. Both matrices must have the same size and no element of $someMatrix can be zero.


      matrix::mat_max

      mat_max();

      Remarks

      Finds the column maximum.


      matrix::mat_min

      mat_min();

      Remarks

      Finds the column minimum.


      matrix::sum

      sum();

      Remarks

      Computes the column total.


      matrix::mean

      sum();

      Remarks

      Computes the column average.


      matrix::gt

      gt(matrix $someMatrix);

      Parameters

      $someMatrix

      Remarks

      Compare if this matrix is greater than $someMatrix element by element . Both matrices must have the same size.


      matrix::gteq

      gteq(matrix $someMatrix);

      Parameters

      $someMatrix

      Remarks

      Compare if this matrix is greater than or equal to $someMatrix element by element . Both matrices must have the same size.


      matrix::lt

      lt(matrix $someMatrix);

      Parameters

      $someMatrix

      Remarks

      Compare if this matrix is lesser than $someMatrix element by element . Both matrices must have the same size.


      matrix::lteq

      lteq(matrix $someMatrix);

      Parameters

      $someMatrix

      Remarks

      Compare if this matrix is lesser than or equal to $someMatrix element by element . Both matrices must have the same size.