PHP Class MathPHP\LinearAlgebra\Matrix

Inheritance: implements ArrayAccess, implements JsonSerializable
Afficher le fichier Open project: markrogoyski/math-php Class Usage Examples

Protected Properties

Свойство 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

Méthodes publiques

Méthode 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

Méthodes protégées

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

Method Details

LUDecomposition() public méthode

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
Résultat array [ L: Lower triangular matrix U: Upper triangular matrix P: Permutation matrix A: Original square matrix ]

__construct() public méthode

Constructor
public __construct ( array $A )
$A array

__toString() public méthode

Ex: [1, 2, 3] [2, 3, 4] [3, 4, 5]
public __toString ( ) : string
Résultat string

add() public méthode

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
Résultat Matrix

augment() public méthode

[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
Résultat Matrix

augmentBelow() public méthode

[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
Résultat Matrix

augmentIdentity() public méthode

[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
Résultat Matrix

cofactor() public méthode

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
Résultat number

cofactorMatrix() public méthode

[A₀₀ A₀₁ A₀₂] A = [A₁₀ A₁₁ A₁₂] [A₂₀ A₂₁ A₂₂] [C₀₀ C₀₁ C₀₂] CM = [C₁₀ C₁₁ C₁₂] [C₂₀ C₂₁ C₂₂]
public cofactorMatrix ( ) : SquareMatrix
Résultat SquareMatrix

columnAdd() public méthode

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
Résultat Matrix

columnExclude() public méthode

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

columnInterchange() public méthode

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ᵢ
Résultat Matrix with columns nᵢ and nⱼ interchanged

columnMultiply() public méthode

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
Résultat Matrix

det() public méthode

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
Résultat number

diagonal() public méthode

All other off-diagonal elements are zeros.
public diagonal ( ) : Matrix
Résultat Matrix

directSum() public méthode

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
Résultat Matrix

frobeniusNorm() public méthode

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

get() public méthode

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

getColumn() public méthode

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

getDiagonalElements() public méthode

getDiagonalElements($A) = [1, 5, 9]
public getDiagonalElements ( ) : array
Résultat array

getM() public méthode

Get row count (m)
public getM ( ) : integer
Résultat integer number of rows

getMatrix() public méthode

Get matrix
public getMatrix ( ) : array
Résultat array of arrays

getN() public méthode

Get column count (n)
public getN ( ) : integer
Résultat integer number of columns

getRow() public méthode

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

hadamardProduct() public méthode

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
Résultat Matrix

infinityNorm() public méthode

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

inverse() public méthode

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
Résultat Matrix

isSquare() public méthode

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

isSymmetric() public méthode

Is the matrix symmetric? Does A = Aᵀ
public isSymmetric ( ) : boolean
Résultat boolean

jsonSerialize() public méthode

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

kroneckerProduct() public méthode

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
Résultat Matrix

map() public méthode

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

maxNorm() public méthode

Max norm (‖A‖max) Elementwise max
public maxNorm ( ) : number
Résultat number

minor() public méthode

[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
Résultat number

minorMatrix() public méthode

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

multiply() public méthode

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

offsetExists() public méthode

************************************************************************ ArrayAccess INTERFACE ************************************************************************
public offsetExists ( $i ) : boolean
Résultat boolean

offsetGet() public méthode

public offsetGet ( $i )

offsetSet() public méthode

public offsetSet ( $i, $value )

offsetUnset() public méthode

public offsetUnset ( $i )

oneNorm() public méthode

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

pivotize() protected méthode

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
Résultat Matrix

rowAdd() public méthode

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
Résultat Matrix

rowAddScalar() public méthode

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
Résultat Matrix

rowDivide() public méthode

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
Résultat Matrix

rowExclude() public méthode

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

rowInterchange() public méthode

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ᵢ
Résultat Matrix with rows mᵢ and mⱼ interchanged

rowMultiply() public méthode

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
Résultat Matrix

rowSubtract() public méthode

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
Résultat Matrix

rowSubtractScalar() public méthode

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
Résultat Matrix

rref() public méthode

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
Résultat Matrix in reduced row echelon form

scalarMultiply() public méthode

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

solve() public méthode

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
Résultat Vector x

subtract() public méthode

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
Résultat Matrix

trace() public méthode

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

transpose() public méthode

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
Résultat Matrix

Property Details

$A protected_oe property

Matrix
protected $A

$A⁻¹ protected_oe property

Inverse
protected $A⁻¹

$det protected_oe property

Determinant
protected number $det
Résultat number

$m protected_oe property

Number of rows
protected int $m
Résultat integer

$n protected_oe property

Number of columns
protected int $n
Résultat integer

$rref protected_oe property

Reduced row echelon form
protected Matrix,MathPHP\LinearAlgebra $rref
Résultat Matrix