markbaker/matrix
PHPMatrix is a PHP matrix algebra library supporting addition, subtraction, multiplication, division, determinants, inverses, transpose, trace, solve (A·X=B), and LU/QR decompositions. Install via Composer as markbaker/matrix and use Matrix objects from arrays.
Start by installing the package via Composer: composer require markbaker/matrix:^3.0. Create a Matrix instance from a 2D PHP array—e.g., $matrix = new Matrix\Matrix([[1,2],[3,4]]);. The core day-to-day use case is performing linear algebra operations common in scientific or financial computation: addition, multiplication, inversion, and solving systems of equations (e.g., $A->solve($B) for AX = B). Use Matrix\Builder for convenience: Builder::createIdentityMatrix(4) or Builder::createFilledMatrix(0, 5, 3) for fast initialization. First stop: read the README’s “Usage” section—it’s concise, includes working examples, and clarifies the immutability model.
$result = $A->transpose()->multiply($B->inverse())->add($C);$A->determinant()) and static helpers (Operations::multiply($A, $B)) when passing arrays or chaining in mixed-type contexts.$qr = new Matrix\Decomposition\QR($designMatrix);$coefficients = $qr->solve($responseVector);$lu = new Matrix\Decomposition\LU($A);$x1 = $lu->solve($b1); $x2 = $lu->solve($b2);LinearRegressionService), convert via ->toArray(), and hydrate DTOs or collections (collect($matrix->toArray())->flatten()).$A = $A->multiply($B);. Forgetting this causes subtle bugs.getRows()/getColumns() before calling multiply() or add(). Wrap operations in try/catch (Matrix\Exceptions\MatrixException) since dimension mismatches throw immediately.matrix_multiply($A, $B), migrate to markbaker/matrix-functions. This package does not provide them.assertEquals($expected, $actual->toArray(), '', 1e-9) in tests to ignore precision noise—default tolerances are not set for you.trace() or determinant() assume a non-empty square matrix. Use isEmpty() or isValid() guards first.How can I help you explore Laravel packages today?