PHPackages                             mcordingley/linearalgebra - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mcordingley/linearalgebra

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mcordingley/linearalgebra
=========================

Matrix math for PHP.

3.0.0(3y ago)85146.3k—6.3%111MITPHPPHP ^8.0.0

Since Apr 3Pushed 3y ago5 watchersCompare

[ Source](https://github.com/mcordingley/LinearAlgebra)[ Packagist](https://packagist.org/packages/mcordingley/linearalgebra)[ RSS](/packages/mcordingley-linearalgebra/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (19)Used By (1)

Matrix
======

[](#matrix)

[![Build Status](https://camo.githubusercontent.com/0e042703fa3a543d9943a38890e94cc678a14d34786020f659ca571c256b5a00/68747470733a2f2f6170692e7472617669732d63692e636f6d2f7265706f7369746f726965732f6d636f7264696e676c65792f4c696e656172416c67656272612e737667)](https://travis-ci.com/github/mcordingley/LinearAlgebra)[![Code Climate](https://camo.githubusercontent.com/f0b9450543e86c0b8ee0ac5a365bb27d41d74318d553e8e1f06f4b571f5dc6f1/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6d636f7264696e676c65792f4c696e656172416c67656272612f6261646765732f6770612e737667)](https://codeclimate.com/github/mcordingley/LinearAlgebra)[![Code Coverage](https://camo.githubusercontent.com/391105ce2c15902aab8cd4ba2ef9fc581a0df6e68b45dcfb388bf1da78f5a014/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6d636f7264696e676c65792f4c696e656172416c67656272612f6261646765732f636f7665726167652e737667)](https://codeclimate.com/github/mcordingley/LinearAlgebra)

Stand-alone Linear Algebra Library for PHP

Installation
------------

[](#installation)

```
composer require mcordingley/LinearAlgebra

```

Alternately, include this in your composer.json and then update:

```
"mcordingley/linearalgebra": "^3.0.0"

```

If Composer isn't an option for you, clone this repository and run `build-phar.php` to generate a phar archive that you can include into your project. PHP will autoload classes from inside the archive as needed.

Usage
-----

[](#usage)

### Matrix

[](#matrix-1)

Start with a `use` statement for the class:

```
use MCordingley\LinearAlgebra\Matrix;

```

Then, instantiate a new instance of the matrix class like so:

```
$matrix = new Matrix([
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8]
]);

```

You can also generate an identity matrix with the `identity` factory function:

```
$threeByThreeIdentityMatrix = Matrix::identity(3);

```

With the matrix instance, you can retrieve individual elements with `get` using the zero-based indices of the row and column that you want:

```
$element = $matrix->get($row, $column);

```

It's also possible to find out how large the matrix is with `getRowCount()` and `getColumnCount()`:

```
$rows = $matrix->getRowCount();
$columns = $matrix->getColumnCount();

```

You can also add, subtract, and multiply the matrix with scalar values and other matrices. All operations return a new Matrix and do not modify the underlying matrix:

```
$addedScalar = $matrix->addScalar(3);
$addedMatrix = $matrix->addMatrix($anotherMatrix);
$subtractedScalar = $matrix->subtractScalar(2);
$subtractedMatrix = $matrix->subtractMatrix($anotherMatrix);
$multipliedByScalar = $matrix->multiplyScalar(4);
$multipliedByMatrix = $matrix->multiplyMatrix($anotherMatrix);

```

Matrices can be compared with `equals` to see if they're equal:

```
if ($matrix1->equals($matrix2)) {
    // Equality for all!
}

```

In addition to these basic operations, the Matrix class offers other common matrix operations:

```
$matrix->inverse()
$matrix->adjugate()
$matrix->determinant()
$matrix->trace()
$matrix->transpose()

```

You can get the upper and lower triangular matrices by calling `upper(bool)` and `lower(bool)`. The lone argument tells whether the main diagonal of the triangular matrix should be set to ones (`true`) or the value of the parent matrix (`false`).

It's also possible to run a map over the matrix:

```
$squaredElements = $matrix->map(function($element, $row, $column, $matrix) {
    return $element * $element
});

```

Submatrices may be extracted with `sliceColumns($offset, $length)` and `sliceRows($offset, $length)`. The semantics of the arguments are the same as PHP's `array_slice`.

Similarly, `spliceColumns($offset, $length, $replacement)` and `spliceRows($offset, $length, $replacement)` can be used to create new matrices with specific rows or columns removed or replaced. Unlike the native PHP `array_splice`, these operations do not modify the matrix in place and return the removed elements, but instead return a new matrix with the splice applied.

If you need to combine together matrices, you can do so by calling the concatenation methods:

```
$m1 = new Matrix([
  [1,2,3],
  [4,5,6],
]);

$m2 = new Matrix([
  [7],
  [8],
]);

$m3 = new Matrix([[3,2,1]]);

$m4 = $m1->concatenateRight($m2);
//  [
//      [1,2,3,7],
//      [4,5,6,8],
//  ]

$m5 = $m1->concatenateBottom($m3);
// [
//     [1,2,3],
//     [4,5,6],
//     [3,2,1],
// ]

```

LU and LUP decomposition methods are available as separate classes and both expose `lower()` and `upper()` for the L and U portions of the decompositions, respectively. The LUP decomposition additionally exposes `permutationMatrix` and `permutationArray` to fetch the P component of the decomposition as well as `parity` to return the total number of pivots performed.

### Vector

[](#vector)

As with `Matrix`, import the class into your current namespace:

```
use MCordingley\LinearAlgebra\Vector;

```

Since a `Vector` is a special case of a `Matrix`, `Vector` inherits from `Matrix`. As such, every method available on `Matrix` is also available on `Vector`. `Vector` also exposes additional methods specific to working with vectors.

Creating a `Vector` differs from creating a `Matrix` only in that the constructor takes an array of scalars, rather than an array of arrays:

```
$vector = new Vector([1, 2, 3, 4]);

```

Note that `Vector` instances are all row vectors. If you need a column vector, `transpose()` the vector to get a `Matrix` with a single column.

If you need to cast a `Matrix` into a `Vector`, call the factory method `fromMatrix()`:

```
$vector = Vector::fromMatrix($matrix);

```

`toArray()` is overridden to return an array of scalars to mirror how the constructor works. It is equivalent to calling `$matrix->toArray()[0]` on a `Matrix` instance.

`getSize()` is provided as an alias for `getColumnCount()`. `sum()` will return the sum of the `Vector` elements, while `dotProduct($otherVector)` will return the sum of the pair-wise products of `$vector` and `$otherVector`, and is also availabe aliased as `innerProduct($otherVector)`. `outerProduct($otherVector)` will return a new Matrix representing the outer product of the two vectors. `crossProduct($otherVector)` is also available. Vectors may be normalized with `normalize()`. They may also be projected onto other vectors with `project($otherVector)`. The Euclidean distance may also be calculated between two vectors with `euclideanDistance($otherVector)`.

For measures of vector magnitude, `l1Norm()`, `l2Norm()`, and `maxNorm()` are all available, with `length()` as an alias for `l2Norm()`.

Links to relevant Wikipedia articles are provided in the function documentation for additional detail.

Change-log
----------

[](#change-log)

- 3.0.0

    - Update to PHP 8 to start taking advantage of new features.
    - Adds some previously-missing type assertions enabled by PHP 8.
    - Adds `euclideanDistance` to `Vector`.
- 2.2.0

    - Implement the `ArrayAccess` interface on `Matrix` to return row vectors.
    - Implement the `ArrayAccess` interface on `Vector` to return scalars.
    - Add `addVector()` and `subtractVector()` to `Vector`
    - Add `magnitude()` as an alias to `length()` on `Vector`
- 2.1.1

    - Fix a bug involving inheritance with `map()` on `Vector`.
- 2.1.0

    - Add `Vector` as a subclass of `Matrix`. Thanks to battlecook for this contribution.
- 2.0.0

    - Drop support for PHP 5.x
    - Introduce strict scalar type hints
    - Drop deprecated functions and properties.
    - Tighten up interface with the `final` and `private` keywords.
    - `diagonal()` now returns a full matrix, not a vector.
    - Rename `adjoint()` to `adjugate()` for clarity.
    - Add `entrywise()` to compute the Hadamard product.
    - Add `upper()` and `lower()`
    - Add `sliceColumns()` and `sliceRows()`
    - Add `spliceColumns()` and `spliceRows()`
    - Add `LU` and `LUP` decompositions as classes.
- 1.3.2

    - Deprecate `__toString()` magic method.
    - Deprecate `isSymmetric()`.
- 1.3.1

    - Deprecate use of the `ArrayAccess` interface.
    - More internal code style fixes.
- 1.3.0

    - Fix typo in names of `concatenateRight()` and `concatenateBottom()`
    - Remove generated Phar file. Users who need it should use the `build-phar.php` script to generate one.
    - Refactor LUDecomposition to have a less awkward constructor.
    - Split `add()` into `addMatrix()` and `addScalar()`. Deprecate `add()`.
    - Split `subtract()` into `subtractMatrix()` and `subtractScalar()`. Deprecate `subtract()`.
    - Split `multiply()` into `multiplyMatrix()` and `multiplyScalar()`. Deprecate `multiply()`.
    - Add `getRowCount()` and `getColumnCount()` accessors.
    - Deprecate `rows` and `columns` properties.
- 1.2.0

    - Added `concatenateBottom($other)`
    - Added `concatencateRight($other)`
- 1.1.0

    - Added `diagonal()`.
- 1.0.0

    - Switch to PSR-4 from PSR-0.
    - Take `isSymmetric()` public.
    - Rearrange source in `Matrix.php` to be more readable and PSR-compliant.
- 0.9.1

    - Fix several bugs with the Cholesky decomposition and inverse.
- 0.9.0

    - Bump version up to represent that this is close to it's final form.
    - Merged PR for faster `inverse` calculations
    - KISS `Vector` class good-bye.
    - Renamed `eq` to `equals`.
    - Removed `set` function, so instantiated objects are immutable.
- 0.3.0

    - Added the `identity` factory function
    - Using Cholesky decomposition for faster matrix inversions for applicable matrices
    - Added `eq` function to test equality of matrices
    - Implemented the ArrayAccess interface
- 0.2.0

    - Created the Vector type
    - `\MCordingley` namespace is now `\mcordingley`
    - Matrix functions that return a new Matrix now return a new instance of the called class
- 0.1.0

    - Created the Matrix type
    - Scalar Addition
    - Scalar Subtraction
    - Scalar Multiplication
    - Matrix Addition
    - Matrix Subtraction
    - Matrix Multiplication
    - Inverse
    - Adjoint
    - Determinant
    - Trace
    - Transpose
    - Submatrix
    - Map

###  Health Score

45

—

FairBetter than 93% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity47

Moderate usage in the ecosystem

Community21

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 91.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~180 days

Recently: every ~466 days

Total

18

Last Release

1360d ago

Major Versions

0.9.1 → 1.0.02015-11-07

1.3.2 → 2.0.02016-11-27

2.3.0 → 3.0.02022-08-27

PHP version history (3 changes)0.1.0PHP &gt;=5.4.0

2.0.0PHP ^7.0.0

3.0.0PHP ^8.0.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/292870c77b057996c6d7ebb96381529d5aba3829e8a9a220328a408039e80d70?d=identicon)[mcordingley](/maintainers/mcordingley)

---

Top Contributors

[![mcordingley](https://avatars.githubusercontent.com/u/1084253?v=4)](https://github.com/mcordingley "mcordingley (285 commits)")[![thominj](https://avatars.githubusercontent.com/u/3819908?v=4)](https://github.com/thominj "thominj (19 commits)")[![battlecook](https://avatars.githubusercontent.com/u/9781396?v=4)](https://github.com/battlecook "battlecook (4 commits)")[![brad-jones](https://avatars.githubusercontent.com/u/2754772?v=4)](https://github.com/brad-jones "brad-jones (1 commits)")[![rreynier](https://avatars.githubusercontent.com/u/1953114?v=4)](https://github.com/rreynier "rreynier (1 commits)")

---

Tags

linear-algebra-librarylinearalgebramatrixphpmatrixlinear algebra

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mcordingley-linearalgebra/health.svg)

```
[![Health](https://phpackages.com/badges/mcordingley-linearalgebra/health.svg)](https://phpackages.com/packages/mcordingley-linearalgebra)
```

###  Alternatives

[markrogoyski/math-php

Math Library for PHP. Features descriptive statistics and regressions; Continuous and discrete probability distributions; Linear algebra with matrices and vectors, Numerical analysis; special mathematical functions; Algebra

2.4k7.1M40](/packages/markrogoyski-math-php)[markbaker/matrix

PHP Class for working with matrices

1.5k279.7M38](/packages/markbaker-matrix)[rubix/tensor

A library and extension that provides objects for scientific computing in PHP.

2751.4M5](/packages/rubix-tensor)[spicyweb/craft-neo

A Matrix-like field type with block hierarchy

395798.1k10](/packages/spicyweb-craft-neo)[sciphp/numphp

PHP library for scientific computing.

6618.9k](/packages/sciphp-numphp)[numphp/numphp

Mathematical PHP library for scientific computing

144106.2k1](/packages/numphp-numphp)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
