Une classe PHP pour l'algèbre linéaire.
Class matrix
Files
Source code here
.
Examples here.
Description
- All mathematical functions are written to ressemble paper algebra :
- AxB --> $A->times($B);
- A-B --> $A->minus($B);
- A^(-1) --> $A->inv();
- A' --> $A->prime();
- det(A) --> $A->det($A);
- etc.
- This restricts nested calls, but augment readability.
Class Variable Summary
$numbers=array()$numColumns=0$numRows=0
Method Summary
matrix matrix($numbers [, $numRows, $numColumns])
Matrix constructor.matrix eye($n, $m)
Creates an$nby$m"identity" matrix.matrix ones($n, $m)
Creates an$nby$mmatrix of ones.matrix zeros($n, $m)
Creates an$nby$mmatrix of zeros.matrix diag($vector)
Creates a diagonal matrix with$vectoron the diagonal.matrix random($n, $m)
Creates an$nby$mmatrix of uniformly distributed numbers.unknown get_num_rows()
Obtain the number of rows in the matrix.unknown get_num_columns()
Obtain the number of columns in the matrix.array get_data()
Obtain the two dimensionnal array of numbers.unknown get_value($i, $j)
Obtain value at the entry$i,$jof the matrix.matrix get_columns($start, $stop)
Obtain the matrix formed by the columns$startto (and including)$stop.matrix get_rows($start, $stop)
Obtain the matrix formed by the rows$startto (and including)$stop.void set_data($numbers)
Set the data of the matrix.void set_value($i, $j, $value)
Set the value of the entry$i,$jof the matrix.void smoothe($tolerance)
All values of$thiswithin$toleranceof zero are set to zero.void set_columns($start, $stop, $block_matrix)
Set the columns from$startto (and including)$stopto$block_matrix.void set_rows($start, $stop, $block_matrix)
Set the rows from$startto (and including)$stopto$block_matrix.void delete_columns($start, $stop)
Delete the columns from$startto (and including)$stopif$this.void delete_rows($start, $stop)
Delete the rows from$startto (and including)$stopof$this.matrix utrig()
Obtain the upper triangular matrix (without the diagonal) of$this.matrix ltrig()
Obtain the lower triangular matrix (without the diagonal) of$this.matrix diag()
Obtain the diagonal vector of$this.bool size_eq($someMatrix)
Test if this matrix has the same dimensions as$someMatrix.bool is_square()
Test if this matrix is square.matrix plus($someMatrix)
Add this matrix to$someMatrix.$C = $A->plus($B);matrix minus($someMatrix)
Substract$someMatrixfrom this matrix.$C = $A->minus($B);matrix times($someMatrix)
Multiply this matrix by$someMatrix.$C = $A->times($B);matrix s_times($someScalar)
Multiply this matrix by$someScalar.$C = $A->s_times($s);matrix det($someMatrix)
Computes the determinant of$someMatrix.$det = $A->det($A);matrix minor_matrix($someMatrix, $column, $row)
Returns$someMatrixmatrix without the$columnth column and$rowth row.$C = $A->minor_matrix($A, $i, $j);matrix prime()
Returns the transpose of the matrix.$A_p = $A->prime();matrix lu()
Returns a2n x nmatrix containing the PLU decomposition of this matrix.$PLU = $A->LU();matrix inv()
Returns the inverse of the matrix.$A_I = $A->inv();matrix p_times($someMatrix)
Performs element by element multiplication of this matrix with$someMatrix.$C = $A->p_times($B);matrix p_div($someMatrix)
Performs element by element division of this matrix by$someMatrix.$C = $A->p_div($B);matrix mat_max()
Finds the maximum of each column of the matrix.$max = $A->mat_max();matrix mat_min()
Finds the minimum of each column of the matrix.$max = $A->mat_min();matrix sum()
Finds the sum of each column of the matrix.$sum = $A->sum();matrix mean()
Finds the mean of each column of the matrix.$mean = $A->mean();matrix gt($someMatrix)
Compares element by element if this matrix greater than$someMatrix.$max = $A->gt($B);matrix gteq($someMatrix)
Compares element by element if this matrix greater or equal than$someMatrix.$max = $A->gteq($B);matrix lt($someMatrix)
Compares element by element if this matrix less than$someMatrix.$max = $A->lt($B);matrix lteq($someMatrix)
Compares element by element if this matrix less or equal than$someMatrix.$max = $A->lteq($B);
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.