Developer-Guide/math/matrix.md
2022-02-19 13:57:38 +01:00

2.5 KiB
Executable File

Matrix

The Matrix class implements matrices of the form m x n.

$matrix = new Matrix();
$matrix->setMatrix([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
);

The implementation implements ArrayAccess for direct access and an Iterator for iteration.

$matrix[0][2]; // = 3
foreach($matrix as $rowId => $row) {
    foreach($row as $columnId => $value) {
        ...
    }
}

Addition

The matrix addition supports matrix and scalar addition. The scalar addition adds a scalar to every element in the matrix.

$matrix->add(4);

The matrix addition requires both matrices to have the same dimensions and adds every index to the same index in the base matrix.

$matrix->add($matrixB);

Subtraction

The matrix subtraction supports matrix and scalar subtraction. The scalar subtraction subtracts a scalar to every element in the matrix.

$matrix->sub(4);

The matrix subtraction requires both matrices to have the same dimensions and subtracts every index to the same index in the base matrix.

$matrix->sub($matrixB);

Multiplication

The matrix multiplication supports matrix/vector and scalar multiplication. The scalar multiplication subtracts a scalar to every element in the matrix.

$matrix->mult(4);

The matrix multiplication performs a normal matrix multiplication.

$matrix->mult($matrixB);

Solve

In order to solve A x = b the matrix implements solve() which automatically selects an appropriate algorithm (LU or QR decomposition) to solve the equation.

$matrix->solve($matrixB);

Inverse

The inverse calculates the inverse if possible.

$matrix->inverse();

Transpose

The transpose function transposes the matrix.

$matrix->transpose();

Determinant

The determinant of a matrix is calculated via the det() function.

$matrix->det();

Diagonalize

The diagonalize function diagonalizes the matrix if possible.

$matrix->diagonalize();

Triangulize

Upper Triangular

The upper triangular of a matrix can be created via the upperTriangular() function.

$matrix->upperTriangular();

Lower Triangular

The lower triangular of a matrix can be created via the lowerTriangular() function.

$matrix->lowerTriangular();

Vector

The Vector class is a extension of the Matrix class m x 1 and implements vectors.

Identity Matrix

The IdentityMatrix class is a extension of the Matrix class and implements the identity matrix m x n.