phpmath / matrix
A PHP library to work with immutable matrices.
Requires
- php: ^8.4
- phpmath/vector: ^2.0
Requires (Dev)
- phpmath/coding-standard: ^2.0
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-24 12:59:40 UTC
README
A PHP library to work with immutable mathematical matrices of real numbers.
Documentation
The full documentation is at https://phpmath.github.io/matrix/. Every change is listed in the changelog and every release on the releases page.
Install
composer require phpmath/matrix
Requirements
- PHP 8.4 or newer
phpmath/vector, which Composer installs for you
Usage
Every matrix is an immutable value object. Operations never change the instance they are called on, they return a new instance.
use PHPMath\Matrix\Matrix; $a = Matrix::fromRows([[1, 2], [3, 4]]); $b = Matrix::identity(2); $sum = $a->add($b); // [[2, 2], [3, 5]] $product = $a->multiply($b); // [[1, 2], [3, 4]] $det = $a->determinant(); // -2 $inverse = $a->inverse(); // [[-2, 1], [1.5, -0.5]] echo $a->transpose(); // [[1, 3], [2, 4]]
Features
- Creating matrices with
fromRows(),identity()andzeros(). - Reading with
rows(),columns(),get(),toArray()andisSquare(). add(),subtract(),scale(),multiply()(matrix product) andtranspose().trace(),determinant()(LU decomposition with partial pivoting) andinverse().- Transforming vectors with
transform(), and building 4x4 transformation matrices withtranslation(),scaling(),rotationX(),rotationY(),rotationZ()androtation(). - Comparing with
equals(), which accepts a tolerance. - Specific exceptions, all implementing
MatrixException.
Development
composer test
composer test-coverage
composer analyse
composer check-style
composer fix-style
Contributing
All contributions are welcome. Open an issue or a pull request on GitHub. Please follow the coding standard and add tests.
Security
If you discover a security issue, please create an issue in the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.