PHP Класс MathPHP\LinearAlgebra\Matrix

Наследование: implements ArrayAccess, implements JsonSerializable
Показать файл Открыть проект Примеры использования класса

Защищенные свойства (Protected)

Свойство Тип Описание
$A Matrix
$A⁻¹ Inverse
$det number Determinant
$m integer Number of rows
$n integer Number of columns
$rref Matrix Reduced row echelon form

Открытые методы

Метод Описание
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

Защищенные методы

Метод Описание
pivotize ( ) : Matrix Pivotize creates the permutation matrix P for the LU decomposition.

Описание методов

LUDecomposition() публичный Метод

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
Результат array [ L: Lower triangular matrix U: Upper triangular matrix P: Permutation matrix A: Original square matrix ]

__construct() публичный Метод

Constructor
public __construct ( array $A )
$A array

__toString() публичный Метод

Ex: [1, 2, 3] [2, 3, 4] [3, 4, 5]
public __toString ( ) : string
Результат string

add() публичный Метод

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
Результат Matrix

augment() публичный Метод

[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
Результат Matrix

augmentBelow() публичный Метод

[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
Результат Matrix

augmentIdentity() публичный Метод

[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
Результат Matrix

cofactor() публичный Метод

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
Результат number

cofactorMatrix() публичный Метод

[A₀₀ A₀₁ A₀₂] A = [A₁₀ A₁₁ A₁₂] [A₂₀ A₂₁ A₂₂] [C₀₀ C₀₁ C₀₂] CM = [C₁₀ C₁₁ C₁₂] [C₂₀ C₂₁ C₂₂]
public cofactorMatrix ( ) : SquareMatrix
Результат SquareMatrix

columnAdd() публичный Метод

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
Результат Matrix

columnExclude() публичный Метод

Exclude a column from the result matrix
public columnExclude ( integer $nᵢ ) : Matrix
$nᵢ integer Column to exclude
Результат Matrix with column nᵢ excluded

columnInterchange() публичный Метод

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ᵢ
Результат Matrix with columns nᵢ and nⱼ interchanged

columnMultiply() публичный Метод

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
Результат Matrix

det() публичный Метод

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
Результат number

diagonal() публичный Метод

All other off-diagonal elements are zeros.
public diagonal ( ) : Matrix
Результат Matrix

directSum() публичный Метод

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
Результат Matrix

frobeniusNorm() публичный Метод

https://en.wikipedia.org/wiki/Matrix_norm#Frobenius_norm _____________ ᵐ ⁿ ‖A‖F = √ Σ Σ |aᵢⱼ|² ᵢ₌₁ ᵢ₌₁
public frobeniusNorm ( ) : number
Результат number

get() публичный Метод

Get a specific value at row i, column j
public get ( integer $i, integer $j ) : number
$i integer row index
$j integer column index
Результат number

getColumn() публичный Метод

Get single column from the matrix
public getColumn ( integer $j ) : array
$j integer column index (from 0 to n - 1)
Результат array

getDiagonalElements() публичный Метод

getDiagonalElements($A) = [1, 5, 9]
public getDiagonalElements ( ) : array
Результат array

getM() публичный Метод

Get row count (m)
public getM ( ) : integer
Результат integer number of rows

getMatrix() публичный Метод

Get matrix
public getMatrix ( ) : array
Результат array of arrays

getN() публичный Метод

Get column count (n)
public getN ( ) : integer
Результат integer number of columns

getRow() публичный Метод

Get single row from the matrix
public getRow ( integer $i ) : array
$i integer row index (from 0 to m - 1)
Результат array

hadamardProduct() публичный Метод

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
Результат Matrix

infinityNorm() публичный Метод

Infinity norm (‖A‖∞) Maximum absolute row sum of the matrix
public infinityNorm ( ) : number
Результат number

inverse() публичный Метод

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
Результат Matrix

isSquare() публичный Метод

Is the matrix a square matrix? Do rows m = columns n?
public isSquare ( ) : boolean
Результат boolean

isSymmetric() публичный Метод

Is the matrix symmetric? Does A = Aᵀ
public isSymmetric ( ) : boolean
Результат boolean

jsonSerialize() публичный Метод

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

kroneckerProduct() публичный Метод

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
Результат Matrix

map() публичный Метод

Map a function over all elements of the Matrix
public map ( callable $func ) : Matrix
$func callable takes a matrix item as input
Результат Matrix

maxNorm() публичный Метод

Max norm (‖A‖max) Elementwise max
public maxNorm ( ) : number
Результат number

minor() публичный Метод

[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
Результат number

minorMatrix() публичный Метод

Used in computing the minor Mᵢⱼ.
public minorMatrix ( integer $mᵢ, integer $nⱼ ) : SquareMatrix
$mᵢ integer Row to exclude
$nⱼ integer Column to exclude
Результат SquareMatrix with row mᵢ and column nⱼ removed

multiply() публичный Метод

Matrix multiplication https://en.wikipedia.org/wiki/Matrix_multiplication#Matrix_product_.28two_matrices.29
public multiply ( Matrix/Vector $B ) : Matrix
$B Matrix/Vector
Результат Matrix

offsetExists() публичный Метод

************************************************************************ ArrayAccess INTERFACE ************************************************************************
public offsetExists ( $i ) : boolean
Результат boolean

offsetGet() публичный Метод

public offsetGet ( $i )

offsetSet() публичный Метод

public offsetSet ( $i, $value )

offsetUnset() публичный Метод

public offsetUnset ( $i )

oneNorm() публичный Метод

1-norm (‖A‖₁) Maximum absolute column sum of the matrix
public oneNorm ( ) : number
Результат number

pivotize() защищенный Метод

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
Результат Matrix

rowAdd() публичный Метод

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
Результат Matrix

rowAddScalar() публичный Метод

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
Результат Matrix

rowDivide() публичный Метод

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
Результат Matrix

rowExclude() публичный Метод

Exclude a row from the result matrix
public rowExclude ( integer $mᵢ ) : Matrix
$mᵢ integer Row to exclude
Результат Matrix with row mᵢ excluded

rowInterchange() публичный Метод

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ᵢ
Результат Matrix with rows mᵢ and mⱼ interchanged

rowMultiply() публичный Метод

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
Результат Matrix

rowSubtract() публичный Метод

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
Результат Matrix

rowSubtractScalar() публичный Метод

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
Результат Matrix

rref() публичный Метод

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
Результат Matrix in reduced row echelon form

scalarMultiply() публичный Метод

Scalar matrix multiplication https://en.wikipedia.org/wiki/Matrix_multiplication#Scalar_multiplication
public scalarMultiply ( number ) : Matrix
number
Результат Matrix

solve() публичный Метод

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
Результат Vector x

subtract() публичный Метод

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
Результат Matrix

trace() публичный Метод

https://en.wikipedia.org/wiki/Trace_(linear_algebra) tr(A) = a₁₁ + a₂₂ + ... ann
public trace ( ) : number
Результат number

transpose() публичный Метод

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
Результат Matrix

Описание свойств

$A защищенное свойство

Matrix
protected $A

$A⁻¹ защищенное свойство

Inverse
protected $A⁻¹

$det защищенное свойство

Determinant
protected number $det
Результат number

$m защищенное свойство

Number of rows
protected int $m
Результат integer

$n защищенное свойство

Number of columns
protected int $n
Результат integer

$rref защищенное свойство

Reduced row echelon form
protected Matrix,MathPHP\LinearAlgebra $rref
Результат Matrix