{"id":43308408,"url":"https://github.com/mcordingley/LinearAlgebra","last_synced_at":"2026-02-12T21:00:37.301Z","repository":{"id":15601464,"uuid":"18337618","full_name":"mcordingley/LinearAlgebra","owner":"mcordingley","description":"Stand-alone Linear Algebra Library for PHP","archived":false,"fork":false,"pushed_at":"2022-08-27T19:28:29.000Z","size":1067,"stargazers_count":83,"open_issues_count":0,"forks_count":11,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-10-19T20:32:16.068Z","etag":null,"topics":["linear-algebra-library","linearalgebra","matrix","php"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mcordingley.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-04-01T17:12:28.000Z","updated_at":"2024-11-13T02:29:51.000Z","dependencies_parsed_at":"2022-09-06T23:21:59.223Z","dependency_job_id":null,"html_url":"https://github.com/mcordingley/LinearAlgebra","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"purl":"pkg:github/mcordingley/LinearAlgebra","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcordingley%2FLinearAlgebra","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcordingley%2FLinearAlgebra/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcordingley%2FLinearAlgebra/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcordingley%2FLinearAlgebra/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mcordingley","download_url":"https://codeload.github.com/mcordingley/LinearAlgebra/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcordingley%2FLinearAlgebra/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29381022,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-12T20:34:40.886Z","status":"ssl_error","status_checked_at":"2026-02-12T20:23:00.490Z","response_time":55,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["linear-algebra-library","linearalgebra","matrix","php"],"created_at":"2026-02-01T21:00:22.048Z","updated_at":"2026-02-12T21:00:37.293Z","avatar_url":"https://github.com/mcordingley.png","language":"PHP","funding_links":[],"categories":["Math, Statistics \u0026 Linear Algebra"],"sub_categories":["Recommended core stack"],"readme":"# Matrix\n\n[![Build Status](https://api.travis-ci.com/repositories/mcordingley/LinearAlgebra.svg)](https://travis-ci.com/github/mcordingley/LinearAlgebra)\n[![Code Climate](https://codeclimate.com/github/mcordingley/LinearAlgebra/badges/gpa.svg)](https://codeclimate.com/github/mcordingley/LinearAlgebra)\n[![Code Coverage](https://codeclimate.com/github/mcordingley/LinearAlgebra/badges/coverage.svg)](https://codeclimate.com/github/mcordingley/LinearAlgebra)\n\nStand-alone Linear Algebra Library for PHP\n\n## Installation\n\n    composer require mcordingley/LinearAlgebra\n\nAlternately, include this in your composer.json and then update:\n\n    \"mcordingley/linearalgebra\": \"^3.0.0\"\n\nIf Composer isn't an option for you, clone this repository and run `build-phar.php` to generate a phar\narchive that you can include into your project. PHP will autoload classes from inside the archive as needed.\n\n## Usage\n\n### Matrix\n\nStart with a `use` statement for the class:\n\n    use MCordingley\\LinearAlgebra\\Matrix;\n\nThen, instantiate a new instance of the matrix class like so:\n\n    $matrix = new Matrix([\n        [0, 1, 2],\n        [3, 4, 5],\n        [6, 7, 8]\n    ]);\n\nYou can also generate an identity matrix with the `identity` factory function:\n\n    $threeByThreeIdentityMatrix = Matrix::identity(3);\n\nWith the matrix instance, you can retrieve individual elements with `get` using\nthe zero-based indices of the row and column that you want:\n\n    $element = $matrix-\u003eget($row, $column);\n\nIt's also possible to find out how large the matrix is with `getRowCount()` and `getColumnCount()`:\n\n    $rows = $matrix-\u003egetRowCount();\n    $columns = $matrix-\u003egetColumnCount();\n\nYou can also add, subtract, and multiply the matrix with scalar values and other\nmatrices. All operations return a new Matrix and do not modify the underlying matrix:\n\n    $addedScalar = $matrix-\u003eaddScalar(3);\n    $addedMatrix = $matrix-\u003eaddMatrix($anotherMatrix);\n    $subtractedScalar = $matrix-\u003esubtractScalar(2);\n    $subtractedMatrix = $matrix-\u003esubtractMatrix($anotherMatrix);\n    $multipliedByScalar = $matrix-\u003emultiplyScalar(4);\n    $multipliedByMatrix = $matrix-\u003emultiplyMatrix($anotherMatrix);\n\nMatrices can be compared with `equals` to see if they're equal:\n\n    if ($matrix1-\u003eequals($matrix2)) {\n        // Equality for all!\n    }\n\nIn addition to these basic operations, the Matrix class offers other common\nmatrix operations:\n\n    $matrix-\u003einverse()\n    $matrix-\u003eadjugate()\n    $matrix-\u003edeterminant()\n    $matrix-\u003etrace()\n    $matrix-\u003etranspose()\n\nYou can get the upper and lower triangular matrices by calling `upper(bool)` and `lower(bool)`. The lone argument tells\nwhether the main diagonal of the triangular matrix should be set to ones (`true`) or the value of the parent matrix\n(`false`).\n\nIt's also possible to run a map over the matrix:\n\n    $squaredElements = $matrix-\u003emap(function($element, $row, $column, $matrix) {\n        return $element * $element\n    });\n\nSubmatrices may be extracted with `sliceColumns($offset, $length)` and `sliceRows($offset, $length)`. The semantics of\nthe arguments are the same as PHP's `array_slice`.\n\nSimilarly, `spliceColumns($offset, $length, $replacement)` and `spliceRows($offset, $length, $replacement)` can be used\nto create new matrices with specific rows or columns removed or replaced. Unlike the native PHP `array_splice`, these\noperations do not modify the matrix in place and return the removed elements, but instead return a new matrix with the\nsplice applied.\n\nIf you need to combine together matrices, you can do so by calling the concatenation methods:\n\n    $m1 = new Matrix([\n      [1,2,3],\n      [4,5,6],\n    ]);\n\n    $m2 = new Matrix([\n      [7],\n      [8],\n    ]);\n\n    $m3 = new Matrix([[3,2,1]]);\n\n    $m4 = $m1-\u003econcatenateRight($m2);\n    //  [\n    //      [1,2,3,7],\n    //      [4,5,6,8],\n    //  ]\n\n    $m5 = $m1-\u003econcatenateBottom($m3);\n    // [\n    //     [1,2,3],\n    //     [4,5,6],\n    //     [3,2,1],\n    // ]\n\nLU and LUP decomposition methods are available as separate classes and both expose `lower()` and `upper()` for the L\nand U portions of the decompositions, respectively. The LUP decomposition additionally exposes `permutationMatrix` and\n`permutationArray` to fetch the P component of the decomposition as well as `parity` to return the total number of\npivots performed.\n\n### Vector\n\nAs with `Matrix`, import the class into your current namespace:\n\n    use MCordingley\\LinearAlgebra\\Vector;\n\nSince a `Vector` is a special case of a `Matrix`, `Vector` inherits from `Matrix`. As such, every method available on\n`Matrix` is also available on `Vector`. `Vector` also exposes additional methods specific to working with vectors.\n\nCreating a `Vector` differs from creating a `Matrix` only in that the constructor takes an array of scalars, rather\nthan an array of arrays:\n\n    $vector = new Vector([1, 2, 3, 4]);\n\nNote that `Vector` instances are all row vectors. If you need a column vector, `transpose()` the vector to get a\n`Matrix` with a single column.\n\nIf you need to cast a `Matrix` into a `Vector`, call the factory method `fromMatrix()`:\n\n    $vector = Vector::fromMatrix($matrix);\n\n`toArray()` is overridden to return an array of scalars to mirror how the constructor works. It is equivalent to\ncalling `$matrix-\u003etoArray()[0]` on a `Matrix` instance.\n\n`getSize()` is provided as an alias for `getColumnCount()`. `sum()` will return the sum of the `Vector` elements,\nwhile `dotProduct($otherVector)` will return the sum of the pair-wise products of `$vector` and `$otherVector`,\nand is also availabe aliased as `innerProduct($otherVector)`. `outerProduct($otherVector)` will return a new Matrix\nrepresenting the outer product of the two vectors. `crossProduct($otherVector)` is also available. Vectors may be\nnormalized with `normalize()`. They may also be projected onto other vectors with `project($otherVector)`. The\nEuclidean distance may also be calculated between two vectors with `euclideanDistance($otherVector)`.\n\nFor measures of vector magnitude, `l1Norm()`, `l2Norm()`, and `maxNorm()` are all available, with `length()` as\nan alias for `l2Norm()`.\n\nLinks to relevant Wikipedia articles are provided in the function documentation for additional detail.\n\n\n## Change-log\n\n- 3.0.0\n    - Update to PHP 8 to start taking advantage of new features.\n    - Adds some previously-missing type assertions enabled by PHP 8.\n    - Adds `euclideanDistance` to `Vector`.\n\n- 2.2.0\n    - Implement the `ArrayAccess` interface on `Matrix` to return row vectors.\n    - Implement the `ArrayAccess` interface on `Vector` to return scalars.\n    - Add `addVector()` and `subtractVector()` to `Vector`\n    - Add `magnitude()` as an alias to `length()` on `Vector`\n\n- 2.1.1\n    - Fix a bug involving inheritance with `map()` on `Vector`.\n\n- 2.1.0\n    - Add `Vector` as a subclass of `Matrix`. Thanks to battlecook for this contribution.\n\n- 2.0.0\n    - Drop support for PHP 5.x\n    - Introduce strict scalar type hints\n    - Drop deprecated functions and properties.\n    - Tighten up interface with the `final` and `private` keywords.\n    - `diagonal()` now returns a full matrix, not a vector.\n    - Rename `adjoint()` to `adjugate()` for clarity.\n    - Add `entrywise()` to compute the Hadamard product.\n    - Add `upper()` and `lower()`\n    - Add `sliceColumns()` and `sliceRows()`\n    - Add `spliceColumns()` and `spliceRows()`\n    - Add `LU` and `LUP` decompositions as classes.\n\n- 1.3.2\n    - Deprecate `__toString()` magic method.\n    - Deprecate `isSymmetric()`.\n\n- 1.3.1\n    - Deprecate use of the `ArrayAccess` interface.\n    - More internal code style fixes.\n\n- 1.3.0\n    - Fix typo in names of `concatenateRight()` and `concatenateBottom()`\n    - Remove generated Phar file. Users who need it should use the `build-phar.php` script to generate one.\n    - Refactor LUDecomposition to have a less awkward constructor.\n    - Split `add()` into `addMatrix()` and `addScalar()`. Deprecate `add()`.\n    - Split `subtract()` into `subtractMatrix()` and `subtractScalar()`. Deprecate `subtract()`.\n    - Split `multiply()` into `multiplyMatrix()` and `multiplyScalar()`. Deprecate `multiply()`.\n    - Add `getRowCount()` and `getColumnCount()` accessors.\n    - Deprecate `rows` and `columns` properties.\n\n- 1.2.0\n    - Added `concatenateBottom($other)`\n    - Added `concatencateRight($other)`\n\n- 1.1.0\n    - Added `diagonal()`.\n\n- 1.0.0\n    - Switch to PSR-4 from PSR-0.\n    - Take `isSymmetric()` public.\n    - Rearrange source in `Matrix.php` to be more readable and PSR-compliant.\n\n- 0.9.1\n    - Fix several bugs with the Cholesky decomposition and inverse.\n\n- 0.9.0\n    - Bump version up to represent that this is close to it's final form.\n    - Merged PR for faster `inverse` calculations\n    - KISS `Vector` class good-bye.\n    - Renamed `eq` to `equals`.\n    - Removed `set` function, so instantiated objects are immutable.\n\n- 0.3.0\n    - Added the `identity` factory function\n    - Using Cholesky decomposition for faster matrix inversions for applicable matrices\n    - Added `eq` function to test equality of matrices\n    - Implemented the ArrayAccess interface\n\n- 0.2.0\n    - Created the Vector type\n    - `\\MCordingley` namespace is now `\\mcordingley`\n    - Matrix functions that return a new Matrix now return a new instance of the called class\n\n- 0.1.0\n    - Created the Matrix type\n    - Scalar Addition\n    - Scalar Subtraction\n    - Scalar Multiplication\n    - Matrix Addition\n    - Matrix Subtraction\n    - Matrix Multiplication\n    - Inverse\n    - Adjoint\n    - Determinant\n    - Trace\n    - Transpose\n    - Submatrix\n    - Map\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcordingley%2FLinearAlgebra","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmcordingley%2FLinearAlgebra","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcordingley%2FLinearAlgebra/lists"}