PHP Class MathPHP\LinearAlgebra\Matrix

Inheritance: implements ArrayAccess, implements JsonSerializable
Show file Open project: markrogoyski/math-php Class Usage Examples

Protected Properties

Property Type Description
$A Matrix
$A⁻¹ Inverse
$det number Determinant
$m integer Number of rows
$n integer Number of columns
$rref Matrix Reduced row echelon form

Public Methods

Method Description
LUDecomposition ( ) : array LU Decomposition (Doolittle decomposition) with pivoting via permutation matrix
__construct ( array $A ) Constructor
__toString ( ) : string Print the matrix as a string Format is as a matrix, not as the underlying array structure.
add ( Matrix $B ) : Matrix Add two matrices - Entrywise sum Adds each element of one matrix to the same element in the other matrix.
augment ( Matrix $B ) : Matrix Augment a matrix An augmented matrix is a matrix obtained by appending the columns of two given matrices
augmentBelow ( Matrix $B ) : Matrix Augment a matrix from below An augmented matrix is a matrix obtained by appending the rows of two given matrices
augmentIdentity ( ) : Matrix Augment a matrix with its identity matrix
cofactor ( integer $mᵢ, integer $nⱼ ) : number Cofactor Multiply the minor by (-1)ⁱ⁺ʲ.
cofactorMatrix ( ) : SquareMatrix Cofactor matrix A matrix where each element is a cofactor.
columnAdd ( integer $nᵢ, integer $nⱼ, integer $k ) : Matrix Add k times column nᵢ to column nⱼ
columnExclude ( integer $nᵢ ) : Matrix Exclude a column from the result matrix
columnInterchange ( integer $nᵢ, integer $nⱼ ) : Matrix Interchange two columns
columnMultiply ( integer $nᵢ, integer $k ) : Matrix Multiply a column by a factor k
det ( ) : number Determinant
diagonal ( ) : Matrix Diagonal matrix Retains the elements along the main diagonal.
directSum ( Matrix $B ) : Matrix Direct sum of two matrices: A ⊕ B The direct sum of any pair of matrices A of size m × n and B of size p × q is a matrix of size (m + p) × (n + q) https://en.wikipedia.org/wiki/Matrix_addition#Direct_sum
frobeniusNorm ( ) : number Frobenius norm (Hilbert–Schmidt norm, Euclidean norm) (‖A‖F) Square root of the sum of the square of all elements.
get ( integer $i, integer $j ) : number Get a specific value at row i, column j
getColumn ( integer $j ) : array Get single column from the matrix
getDiagonalElements ( ) : array Returns the elements on the diagonal of a square matrix as an array [1 2 3] A = [4 5 6] [7 8 9]
getM ( ) : integer Get row count (m)
getMatrix ( ) : array Get matrix
getN ( ) : integer Get column count (n)
getRow ( integer $i ) : array Get single row from the matrix
hadamardProduct ( Matrix $B ) : Matrix Hadamard product (A∘B) Also known as the Schur product, or the entrywise product
infinityNorm ( ) : number Infinity norm (‖A‖∞) Maximum absolute row sum of the matrix
inverse ( ) : Matrix Inverse
isSquare ( ) : boolean Is the matrix a square matrix? Do rows m = columns n?
isSymmetric ( ) : boolean Is the matrix symmetric? Does A = Aᵀ
jsonSerialize ( ) ************************************************************************ JsonSerializable INTERFACE ************************************************************************
kroneckerProduct ( Matrix $B ) : Matrix Kronecker product (A⊗B)
map ( callable $func ) : Matrix Map a function over all elements of the Matrix
maxNorm ( ) : number Max norm (‖A‖max) Elementwise max
minor ( integer $mᵢ, integer $nⱼ ) : number Minor (first minor) The determinant of some smaller square matrix, cut down from A by removing one of its rows and columns.
minorMatrix ( integer $mᵢ, integer $nⱼ ) : SquareMatrix Minor matrix Submatrix formed by deleting the iᵗʰ row and jᵗʰ column.
multiply ( Matrix/Vector $B ) : Matrix Matrix multiplication https://en.wikipedia.org/wiki/Matrix_multiplication#Matrix_product_.28two_matrices.29
offsetExists ( $i ) : boolean ************************************************************************ ArrayAccess INTERFACE ************************************************************************
offsetGet ( $i )
offsetSet ( $i, $value )
offsetUnset ( $i )
oneNorm ( ) : number 1-norm (‖A‖₁) Maximum absolute column sum of the matrix
rowAdd ( integer $mᵢ, integer $mⱼ, integer $k ) : Matrix Add k times row mᵢ to row mⱼ
rowAddScalar ( integer $mᵢ, integer $k ) : Matrix Add a scalar k to each item of a row
rowDivide ( integer $mᵢ, integer $k ) : Matrix Divide a row by a divisor k
rowExclude ( integer $mᵢ ) : Matrix Exclude a row from the result matrix
rowInterchange ( integer $mᵢ, integer $mⱼ ) : Matrix Interchange two rows
rowMultiply ( integer $mᵢ, integer $k ) : Matrix Multiply a row by a factor k
rowSubtract ( integer $mᵢ, integer $mⱼ, number $k ) : Matrix Subtract k times row mᵢ to row mⱼ
rowSubtractScalar ( integer $mᵢ, integer $k ) : Matrix Subtract a scalar k to each item of a row
rref ( ) : Matrix Ruduced row echelon form (row canonical form)
scalarMultiply ( number ) : Matrix Scalar matrix multiplication https://en.wikipedia.org/wiki/Matrix_multiplication#Scalar_multiplication
solve ( Vector/array $b ) : Vector Solve linear system of equations Ax = b where: A: Matrix x: unknown to solve for b: solution to linear system of equations (input to function)
subtract ( Matrix $B ) : Matrix Subtract two matrices - Entrywise subtraction Adds each element of one matrix to the same element in the other matrix.
trace ( ) : number Trace the trace of an n-by-n square matrix A is defined to be the sum of the elements on the main diagonal (the diagonal from the upper left to the lower right).
transpose ( ) : Matrix Transpose matrix

Protected Methods

Method Description
pivotize ( ) : Matrix Pivotize creates the permutation matrix P for the LU decomposition.

Method Details

LUDecomposition() public method

A matrix has an LU-factorization if it can be expressed as the product of a lower-triangular matrix L and an upper-triangular matrix U. If A is a nonsingular matrix, then we can find a permutation matrix P so that PA will have an LU decomposition: PA = LU https://en.wikipedia.org/wiki/LU_decomposition https://en.wikipedia.org/wiki/LU_decomposition#Doolittle_algorithm L: Lower triangular matrix--all entries above the main diagonal are zero. The main diagonal will be all ones. U: Upper tirangular matrix--all entries below the main diagonal are zero. P: Permutation matrix--Identity matrix with possible rows interchanged. Example: [1 3 5] A = [2 4 7] [1 1 0] Create permutation matrix P: [0 1 0] P = [1 0 1] [0 0 1] Pivot A to be PA: [0 1 0][1 3 5] [2 4 7] PA = [1 0 1][2 4 7] = [1 3 5] [0 0 1][1 1 0] [1 1 0] Calculate L and U [1 0 0] [2 4 7] L = [0.5 1 0] U = [0 1 1.5] [0.5 -1 1] [0 0 -2]
public LUDecomposition ( ) : array
return array [ L: Lower triangular matrix U: Upper triangular matrix P: Permutation matrix A: Original square matrix ]

__construct() public method

Constructor
public __construct ( array $A )
$A array

__toString() public method

Ex: [1, 2, 3] [2, 3, 4] [3, 4, 5]
public __toString ( ) : string
return string

add() public method

Returns a new matrix. https://en.wikipedia.org/wiki/Matrix_addition#Entrywise_sum
public add ( Matrix $B ) : Matrix
$B Matrix Matrix to add to this matrix
return Matrix

augment() public method

[1, 2, 3] A = [2, 3, 4] [3, 4, 5] [4] B = [5] [6] [1, 2, 3 | 4] (A|B) = [2, 3, 4 | 5] [3, 4, 5 | 6]
public augment ( Matrix $B ) : Matrix
$B Matrix Matrix columns to add to matrix A
return Matrix

augmentBelow() public method

[1, 2, 3] A = [2, 3, 4] [3, 4, 5] B = [4, 5, 6] [1, 2, 3] (A_B) = [2, 3, 4] [3, 4, 5] [4, 5, 6]
public augmentBelow ( Matrix $B ) : Matrix
$B Matrix Matrix rows to add to matrix A
return Matrix

augmentIdentity() public method

[1, 2, 3] C = [2, 3, 4] [3, 4, 5] [1, 2, 3 | 1, 0, 0] (C|I) = [2, 3, 4 | 0, 1, 0] [3, 4, 5 | 0, 0, 1] C must be a square matrix
public augmentIdentity ( ) : Matrix
return Matrix

cofactor() public method

Cᵢⱼ = (-1)ⁱ⁺ʲMᵢⱼ Example: [1 4 7] If A = [3 0 5] [1 9 11] [1 4 -] [1 4] Then M₁₂ = det [- - -] = det [1 9] = 13 [1 9 -] Therefore C₁₂ = (-1)¹⁺²(13) = -13 https://en.wikipedia.org/wiki/Minor_(linear_algebra)
public cofactor ( integer $mᵢ, integer $nⱼ ) : number
$mᵢ integer Row to exclude
$nⱼ integer Column to exclude
return number

cofactorMatrix() public method

[A₀₀ A₀₁ A₀₂] A = [A₁₀ A₁₁ A₁₂] [A₂₀ A₂₁ A₂₂] [C₀₀ C₀₁ C₀₂] CM = [C₁₀ C₁₁ C₁₂] [C₂₀ C₂₁ C₂₂]
public cofactorMatrix ( ) : SquareMatrix
return SquareMatrix

columnAdd() public method

Add k times column nᵢ to column nⱼ
public columnAdd ( integer $nᵢ, integer $nⱼ, integer $k ) : Matrix
$nᵢ integer Column to multiply * k to be added to column nⱼ
$nⱼ integer Column that will have column nⱼ * k added to it
$k integer Multiplier
return Matrix

columnExclude() public method

Exclude a column from the result matrix
public columnExclude ( integer $nᵢ ) : Matrix
$nᵢ integer Column to exclude
return Matrix with column nᵢ excluded

columnInterchange() public method

Column nᵢ changes to position nⱼ Column nⱼ changes to position nᵢ
public columnInterchange ( integer $nᵢ, integer $nⱼ ) : Matrix
$nᵢ integer Column to swap into column position nⱼ
$nⱼ integer Column to swap into column position nᵢ
return Matrix with columns nᵢ and nⱼ interchanged

columnMultiply() public method

Each element of column nᵢ will be multiplied by k
public columnMultiply ( integer $nᵢ, integer $k ) : Matrix
$nᵢ integer Column to multiply
$k integer Multiplier
return Matrix

det() public method

For a 1x1 matrix: A = [a] |A| = a For a 2x2 matrix: [a b] A = [c d] │A│ = ad - bc For a 3x3 matrix: [a b c] A = [d e f] [g h i] │A│ = a(ei - fh) - b(di - fg) + c(dh - eg) For 4x4 and larger matrices: │A│ = (-1)ⁿ │rref(A)│ ∏1/k where: │rref(A)│ = determinant of the reduced row echelon form of A ⁿ = number of row swaps when computing RREF ∏1/k = product of 1/k where k is the scaling factor divisor
public det ( ) : number
return number

diagonal() public method

All other off-diagonal elements are zeros.
public diagonal ( ) : Matrix
return Matrix

directSum() public method

Direct sum of two matrices: A ⊕ B The direct sum of any pair of matrices A of size m × n and B of size p × q is a matrix of size (m + p) × (n + q) https://en.wikipedia.org/wiki/Matrix_addition#Direct_sum
public directSum ( Matrix $B ) : Matrix
$B Matrix Matrix to add to this matrix
return Matrix

frobeniusNorm() public method

https://en.wikipedia.org/wiki/Matrix_norm#Frobenius_norm _____________ ᵐ ⁿ ‖A‖F = √ Σ Σ |aᵢⱼ|² ᵢ₌₁ ᵢ₌₁
public frobeniusNorm ( ) : number
return number

get() public method

Get a specific value at row i, column j
public get ( integer $i, integer $j ) : number
$i integer row index
$j integer column index
return number

getColumn() public method

Get single column from the matrix
public getColumn ( integer $j ) : array
$j integer column index (from 0 to n - 1)
return array

getDiagonalElements() public method

getDiagonalElements($A) = [1, 5, 9]
public getDiagonalElements ( ) : array
return array

getM() public method

Get row count (m)
public getM ( ) : integer
return integer number of rows

getMatrix() public method

Get matrix
public getMatrix ( ) : array
return array of arrays

getN() public method

Get column count (n)
public getN ( ) : integer
return integer number of columns

getRow() public method

Get single row from the matrix
public getRow ( integer $i ) : array
$i integer row index (from 0 to m - 1)
return array

hadamardProduct() public method

A binary operation that takes two matrices of the same dimensions, and produces another matrix where each element ij is the product of elements ij of the original two matrices. https://en.wikipedia.org/wiki/Hadamard_product_(matrices) (A∘B)ᵢⱼ = (A)ᵢⱼ(B)ᵢⱼ
public hadamardProduct ( Matrix $B ) : Matrix
$B Matrix
return Matrix

infinityNorm() public method

Infinity norm (‖A‖∞) Maximum absolute row sum of the matrix
public infinityNorm ( ) : number
return number

inverse() public method

For a 2x2 matrix: [a b] A = [c d] 1 A⁻¹ = --- [d -b] │A│ [-c a] For a 3x3 matrix or larger: Augment with identity matrix and calculate reduced row echelon form.
public inverse ( ) : Matrix
return Matrix

isSquare() public method

Is the matrix a square matrix? Do rows m = columns n?
public isSquare ( ) : boolean
return boolean

isSymmetric() public method

Is the matrix symmetric? Does A = Aᵀ
public isSymmetric ( ) : boolean
return boolean

jsonSerialize() public method

************************************************************************ JsonSerializable INTERFACE ************************************************************************
public jsonSerialize ( )

kroneckerProduct() public method

If A is an m × n matrix and B is a p × q matrix, then the Kronecker product A ⊗ B is the mp × nq block matrix: [a₁₁b₁₁ a₁₁b₁₂ ⋯ a₁₁b₁q ⋯ ⋯ a₁nb₁₁ a₁nb₁₂ ⋯ a₁nb₁q] [a₁₁b₂₁ a₁₁b₂₂ ⋯ a₁₁b₂q ⋯ ⋯ a₁nb₂₁ a₁nb₂₂ ⋯ a₁nb₂q] [ ⋮ ⋮ ⋱ ⋮ ⋮ ⋮ ⋱ ⋮ ] [a₁₁bp₁ a₁₁bp₂ ⋯ a₁₁bpq ⋯ ⋯ a₁nbp₁ a₁nbp₂ ⋯ a₁nbpq] A⊗B = [ ⋮ ⋮ ⋮ ⋱ ⋮ ⋮ ⋮ ] [ ⋮ ⋮ ⋮ ⋱ ⋮ ⋮ ⋮ ] [am₁b₁₁ am₁b₁₂ ⋯ am₁b₁q ⋯ ⋯ amnb₁₁ amnb₁₂ ⋯ amnb₁q] [am₁b₂₁ am₁b₂₂ ⋯ am₁b₂q ⋯ ⋯ amnb₂₁ amnb₂₂ ⋯ amnb₂q] [ ⋮ ⋮ ⋱ ⋮ ⋮ ⋮ ⋱ ⋮ ] [am₁bp₁ am₁bp₂ ⋯ am₁bpq ⋯ ⋯ amnbp₁ amnbp₂ ⋯ amnbpq] https://en.wikipedia.org/wiki/Kronecker_product
public kroneckerProduct ( Matrix $B ) : Matrix
$B Matrix
return Matrix

map() public method

Map a function over all elements of the Matrix
public map ( callable $func ) : Matrix
$func callable takes a matrix item as input
return Matrix

maxNorm() public method

Max norm (‖A‖max) Elementwise max
public maxNorm ( ) : number
return number

minor() public method

[1 4 7] If A = [3 0 5] [1 9 11] [1 4 -] [1 4] Then M₁₂ = det [- - -] = det [1 9] = 13 [1 9 -] https://en.wikipedia.org/wiki/Minor_(linear_algebra)
public minor ( integer $mᵢ, integer $nⱼ ) : number
$mᵢ integer Row to exclude
$nⱼ integer Column to exclude
return number

minorMatrix() public method

Used in computing the minor Mᵢⱼ.
public minorMatrix ( integer $mᵢ, integer $nⱼ ) : SquareMatrix
$mᵢ integer Row to exclude
$nⱼ integer Column to exclude
return SquareMatrix with row mᵢ and column nⱼ removed

multiply() public method

Matrix multiplication https://en.wikipedia.org/wiki/Matrix_multiplication#Matrix_product_.28two_matrices.29
public multiply ( Matrix/Vector $B ) : Matrix
$B Matrix/Vector
return Matrix

offsetExists() public method

************************************************************************ ArrayAccess INTERFACE ************************************************************************
public offsetExists ( $i ) : boolean
return boolean

offsetGet() public method

public offsetGet ( $i )

offsetSet() public method

public offsetSet ( $i, $value )

offsetUnset() public method

public offsetUnset ( $i )

oneNorm() public method

1-norm (‖A‖₁) Maximum absolute column sum of the matrix
public oneNorm ( ) : number
return number

pivotize() protected method

The permutation matrix is an identity matrix with rows possibly interchanged. The product PA results in a new matrix whose rows consist of the rows of A but no rearranged in the order specified by the permutation matrix P. Example: [α₁₁ α₁₂ α₁₃] A = [α₂₁ α₂₂ α₂₃] [α₃₁ α₃₂ α₃₃] [0 1 0] P = [1 0 0] [0 0 1] [α₂₁ α₂₂ α₂₃] \ rows PA = [α₁₁ α₁₂ α₁₃] / interchanged [α₃₁ α₃₂ α₃₃]
protected pivotize ( ) : Matrix
return Matrix

rowAdd() public method

Add k times row mᵢ to row mⱼ
public rowAdd ( integer $mᵢ, integer $mⱼ, integer $k ) : Matrix
$mᵢ integer Row to multiply * k to be added to row mⱼ
$mⱼ integer Row that will have row mⱼ * k added to it
$k integer Multiplier
return Matrix

rowAddScalar() public method

Each element of Row mᵢ will have k added to it
public rowAddScalar ( integer $mᵢ, integer $k ) : Matrix
$mᵢ integer Row to add k to
$k integer scalar
return Matrix

rowDivide() public method

Each element of Row mᵢ will be divided by k
public rowDivide ( integer $mᵢ, integer $k ) : Matrix
$mᵢ integer Row to multiply
$k integer divisor
return Matrix

rowExclude() public method

Exclude a row from the result matrix
public rowExclude ( integer $mᵢ ) : Matrix
$mᵢ integer Row to exclude
return Matrix with row mᵢ excluded

rowInterchange() public method

Row mᵢ changes to position mⱼ Row mⱼ changes to position mᵢ
public rowInterchange ( integer $mᵢ, integer $mⱼ ) : Matrix
$mᵢ integer Row to swap into row position mⱼ
$mⱼ integer Row to swap into row position mᵢ
return Matrix with rows mᵢ and mⱼ interchanged

rowMultiply() public method

Each element of Row mᵢ will be multiplied by k
public rowMultiply ( integer $mᵢ, integer $k ) : Matrix
$mᵢ integer Row to multiply
$k integer Multiplier
return Matrix

rowSubtract() public method

Subtract k times row mᵢ to row mⱼ
public rowSubtract ( integer $mᵢ, integer $mⱼ, number $k ) : Matrix
$mᵢ integer Row to multiply * k to be subtracted to row mⱼ
$mⱼ integer Row that will have row mⱼ * k subtracted to it
$k number Multiplier
return Matrix

rowSubtractScalar() public method

Each element of Row mᵢ will have k subtracted from it
public rowSubtractScalar ( integer $mᵢ, integer $k ) : Matrix
$mᵢ integer Row to add k to
$k integer scalar
return Matrix

rref() public method

Adapted from reference algorithm: https://rosettacode.org/wiki/Reduced_row_echelon_form Also computes number of swaps and product of scaling factor. These are used for computing the determinant.
public rref ( ) : Matrix
return Matrix in reduced row echelon form

scalarMultiply() public method

Scalar matrix multiplication https://en.wikipedia.org/wiki/Matrix_multiplication#Scalar_multiplication
public scalarMultiply ( number ) : Matrix
number
return Matrix

solve() public method

If A is nxn invertible matrix, and the inverse is already computed: x = A⁻¹b If 2x2, just take the inverse and solve: x = A⁻¹b If 3x3 or higher, check if the RREF is already computed, and if so, then just take the inverse and solve: x = A⁻¹b Otherwise, it is more efficient to decompose and then solve. Use LU Decomposition and solve Ax = b. LU Decomposition: - Equation to solve: Ax = b - LU Decomposition produces: PA = LU - Substitute: LUx = Pb, or Pb = LUx - Can rewrite as Pb = L(Ux) - Can say y = Ux - Then can rewrite as Pb = Ly - Solve for y (we know Pb and L) - Solve for x in y = Ux once we know y Solving triangular systems Ly = Pb and Ux = y - Solve for Ly = Pb using forward substitution 1 / ᵢ₋₁ \ yᵢ = --- | bᵢ - ∑ Lᵢⱼyⱼ | Lᵢᵢ \ ʲ⁼¹ / - Solve for Ux = y using back substitution 1 / m \ xᵢ = --- | yᵢ - ∑ Uᵢⱼxⱼ | Uᵢᵢ \ ʲ⁼ⁱ⁺¹ /
public solve ( Vector/array $b ) : Vector
$b Vector/array
return Vector x

subtract() public method

Returns a new matrix. https://en.wikipedia.org/wiki/Matrix_addition#Entrywise_sum
public subtract ( Matrix $B ) : Matrix
$B Matrix Matrix to subtract from this matrix
return Matrix

trace() public method

https://en.wikipedia.org/wiki/Trace_(linear_algebra) tr(A) = a₁₁ + a₂₂ + ... ann
public trace ( ) : number
return number

transpose() public method

The transpose of a matrix A is another matrix Aᵀ: - reflect A over its main diagonal (which runs from top-left to bottom-right) to obtain AT - write the rows of A as the columns of AT - write the columns of A as the rows of AT Formally, the i th row, j th column element of Aᵀ is the j th row, i th column element of A. If A is an m × n matrix then Aᵀ is an n × m matrix. https://en.wikipedia.org/wiki/Transpose
public transpose ( ) : Matrix
return Matrix

Property Details

$A protected property

Matrix
protected $A

$A⁻¹ protected property

Inverse
protected $A⁻¹

$det protected property

Determinant
protected number $det
return number

$m protected property

Number of rows
protected int $m
return integer

$n protected property

Number of columns
protected int $n
return integer

$rref protected property

Reduced row echelon form
protected Matrix,MathPHP\LinearAlgebra $rref
return Matrix