Introduction
Matrices of real numbers for PHP. Every matrix is an immutable value object.
Requirements#
The library needs a current PHP version and phpmath/vector, which Composer installs for you. The vector classes are what you transform with a matrix.
- PHP 8.4 or newer
phpmath/vector2.0 or newer
Installation#
Install the library with Composer.
composer require phpmath/matrix
A first example#
A matrix is a grid of numbers arranged in rows and columns. Create one from a list of rows, then combine it with other matrices.
use PHPMath\Matrix\Matrix;
$a = Matrix::fromRows([[1, 2], [3, 4]]);
$b = Matrix::fromRows([[0, 1], [1, 0]]);
echo $a->add($b); // [[1, 3], [4, 4]]
echo $a->multiply($b); // [[2, 1], [4, 3]]
echo $a->determinant(); // -2
echo $a->inverse(); // [[-2, 1], [1.5, -0.5]]
Calling add() did not change $a. It returned a new Matrix. This is true for every operation in the library.
Where to go next#
Start with the core concepts, or jump straight to the topic you need.
Core conceptsImmutability, creating matrices and reading values.ArithmeticAdd, subtract, scale and multiply.Shape and transposeSize, transpose, trace and square matrices.Determinant and inverseSolve and invert square matrices.ComparisonCompare matrices with a tolerance.TransformationsMove, scale and rotate points with 4x4 matrices.API referenceEvery method with its parameters and errors.