leonickl/matrix

There is no license information available for the latest version (v2.3.1) of this package.

Installs: 9

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

Type:project

pkg:composer/leonickl/matrix

v2.3.1 2025-12-13 19:39 UTC

This package is auto-updated.

Last update: 2026-01-13 19:46:12 UTC


README

A PHP package incorporating basic matrix algebra. Examples can be found in playground.php.

Installation

composer require leonickl/matrix

Usage

Matrices are instances of Matrix and can be defined either via the class' constructor or the matrix() helper function.

// define a matrix
$A = matrix([
    [5, 8, 6, 3],
    [7, 9, 1, 3],
    [2, 6, 8, 7],
    [6, 0, 5, 7],
]);

// invert matrix
$A->invert();

// calculate determinant
$A->det();

// transpose matrix
$A->transpose(); // or ->t()

// multiply with another matrix B
$A->times($B);

// add two matrices
$A->plus($B); // or ->minus($B) for subtraction

// scalar multiplication
$A->scalar(2);

A Matrix can be converted to a Vector object if it has only one column or only one row. A vector is always a matrix of dimension $(n \times 1)$ and offers further methods, while a Vector is still treated as a Matrix. Vectors can be also created instantly by their constructor or the vector helper, of course.

// convert a matrix to a vector
$C->vector(); // works if C is of dimension (1⨉n) or (n⨉1)

// define a vector
$c = vector([1, 5, 7, 4]);

// multiply transposed c to A
$c->t()->times($A);

// inner (dot) product of c with itself (c'c)
$c->inner($c);

// outer product of c with itself (cc'), generating a (n⨉n) matrix
$c->outer($c);

// plus, minus, scalar, times work the same