Search by

phpmath / matrix

waltertamboer

A PHP library to work with immutable matrices.

Package info

github.com/phpmath/matrix

pkg:composer/phpmath/matrix

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-11-23 11:30 UTC

This package is auto-updated.

Last update: 2026-11-24 12:59:40 UTC


README

Latest Version on Packagist Software License CI Total Downloads

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() and zeros().
  • Reading with rows(), columns(), get(), toArray() and isSquare().
  • add(), subtract(), scale(), multiply() (matrix product) and transpose().
  • trace(), determinant() (LU decomposition with partial pivoting) and inverse().
  • Transforming vectors with transform(), and building 4x4 transformation matrices with translation(), scaling(), rotationX(), rotationY(), rotationZ() and rotation().
  • 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.