{"id":20254744,"url":"https://github.com/chippyash/matrix","last_synced_at":"2025-04-11T00:04:55.015Z","repository":{"id":18490002,"uuid":"21685732","full_name":"chippyash/Matrix","owner":"chippyash","description":"PHP Matrix data structure","archived":false,"fork":false,"pushed_at":"2018-07-04T22:59:03.000Z","size":150,"stargazers_count":14,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-11T00:04:34.521Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chippyash.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-07-10T08:08:20.000Z","updated_at":"2024-12-11T19:53:10.000Z","dependencies_parsed_at":"2022-09-12T15:24:10.014Z","dependency_job_id":null,"html_url":"https://github.com/chippyash/Matrix","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chippyash%2FMatrix","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chippyash%2FMatrix/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chippyash%2FMatrix/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chippyash%2FMatrix/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chippyash","download_url":"https://codeload.github.com/chippyash/Matrix/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248317708,"owners_count":21083528,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-11-14T10:34:42.316Z","updated_at":"2025-04-11T00:04:54.994Z","avatar_url":"https://github.com/chippyash.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# chippyash/Matrix\n\n## Quality Assurance\n\n![PHP 5.6](https://img.shields.io/badge/PHP-5.6-blue.svg)\n![PHP 7](https://img.shields.io/badge/PHP-7-blue.svg)\n[![Build Status](https://travis-ci.org/chippyash/Matrix.svg?branch=master)](https://travis-ci.org/chippyash/Matrix)\n[![Test Coverage](https://codeclimate.com/github/chippyash/Matrix/badges/coverage.svg)](https://codeclimate.com/github/chippyash/Matrix/coverage)\n[![Code Climate](https://codeclimate.com/github/chippyash/Matrix/badges/gpa.svg)](https://codeclimate.com/github/chippyash/Matrix)\n\nThe above badges represent the current development branch.  As a rule, I don't push\n to GitHub unless tests, coverage and usability are acceptable.  This may not be\n true for short periods of time; on holiday, need code for some other downstream\n project etc.  If you need stable code, use a tagged version. Read 'Further Documentation'\n and 'Installation'.\n \n[Test Contract](https://github.com/chippyash/matrix/blob/master/docs/Test-Contract.md) in the docs directory.\n\nPlease note that developer support for PHP5.4 \u0026 5.5 was withdrawn at version 3.0.0 of this library.\nIf you need support for PHP 5.4 or 5.5, please use a version `\u003e=2,\u003c3`\n \n## What?\n\nThis library aims to provide matrix transformation functionality given that:\n\n*  Everything has a test case\n*  It's PHP 5.4+\n\nThe matrix  supplied in this library is a generic one. It treats a matrix as a\ndata structure giving the ability to create matrices and carry out transformations.\n\nThe library is released under the [GNU GPL V3 or later license](http://www.gnu.org/copyleft/gpl.html)\n\n## Why?\n\nI finally got round, after too may years, to investigate\n[TDD](http://en.wikipedia.org/wiki/Test-driven_development) as a serious methodology.\nAlways interested in something a bit maths related, I thought that having a go at\nmatrices would brush up on old forgotten things and provide a bit of entertainment.\nSo this library is as much about using TDD as it is about matrices.  The bonus for\ntaking the TDD approach is that as I've (re)learnt something about matrices,\nI've been able to refactor in safety.\n\n## When\n\nThe current library covers basic matrix manipulation.  The library will cover most\nwell known generic matrix transformations and derivatives.\n\nIf you want more, either suggest it, or better still, fork it and provide a pull request.\n\nCheck out [chippyash/Logical-Matrix](https://github.com/chippyash/Logical-matrix) for logical matrix operations\n\nCheck out [chippyash/Math-Matrix](https://github.com/chippyash/Math-Matrix) for mathematical matrix operations\n\nCheck out [chippyash/Strong-Type](https://github.com/chippyashl/Strong-Type) for strong type including numeric,\nrational and complex type support\n\nCheck out [chippyash/Math-Type-Calculator](https://github.com/chippyash/Math-Type-Calculator) for arithmetic operations on aforementioned strong types\n\nCheck out [ZF4 Packages](http://zf4.biz/packages?utm_source=github\u0026utm_medium=web\u0026utm_campaign=blinks\u0026utm_content=matrix) for more packages\n\n## How\n\n### Coding Basics\n\nIn PHP terms a matrix is an array of arrays, 2 dimensional i.e\n\n-  [[]]\n\n\nA shortcut for a single item matrix is to supply a single array\n\n\u003cpre\u003e\n    use Chippyash/Matrix/Matrix;\n\n    $mA = new Matrix([]);  //empty matrix\n    $mA = new Matrix([[]]);  //empty matrix\n    $mA = new Matrix([1]);  //single item matrix\n\u003c/pre\u003e\n\nAs with any TDD application, the tests tell you everything you need to know about\nthe SUT.  Read them!  However for the short of temper amongst us, the salient\npoints are:\n\nA basic Matrix type is supplied\n\n*  Matrix(array $source, bool $complete = false, bool $normalize= false, bool $normalizeDefault = null)\n\nMatrices are 1 based, not 0 (zero) based\n\nThe following methods on a matrix are supported:\n\n*  toArray() : array - return matrix data as an array\n*  rows(): int - number of rows (n) in the matrix\n*  columns(): int - number of columns (m) in the matrix\n*  vertices(): int - number of entries in matrix (== n * m)\n*  get(int $row \u003e= 0, int $col \u003e= 0): mixed - if **either row or col** are zero, then return\na row vector if col == 0, column vector if row == 0, else return single vertex from matrix\nindictated by row, col. \n*  is(string $attribute): boolean - see attributes below\n*  test(string $attribute): boolean - Raw form of is() method. You can use this to test for attributes\n   not supplied with the library by passing in $attribute conforming to AttributeInterface\n*  transform(TransformationInterface $transformation, $extra = null): Matrix - see transformations below\n*  setFormatter(FormatterInterface $formatter): fluent - set a display formatter\n*  display(): mixed -  Return the matrix in some displayable format\n*  equality(Matrix $mB, boolean $strict = true): boolean - Is the matrix equal to matrix mB?\n     * mA-\u003erows() == mB-\u003erows()\n     * mA-\u003ecolumns() == mB-\u003ecolumns()\n     * mA-\u003eget(i,j) ==\\[=\\] mB-\u003eget(i,j)\n     If strict then type and value checked else only equivalence of value\n\n#### Matrices are immutable\n\nNo operation on a matrix will change the internal structure of the matrix.  Any\ntransformation or similar will return another matrix, leaving the original alone.\nThis allows for arithmetic stability.\n\nHowever, a Mutability trait is provided for you to create a mutable matrix for special\npurposes.\n\n\u003cpre\u003e\nuse Chippyash\\Matrix\\Traits\\Mutability;\n\nclass MutableMatrix extends Matrix\n{\n    use Mutability;\n}\n\u003c/pre\u003e\n\nThis provides a set() method:\n\u003cpre\u003e\n    /**\n     * Set a matrix vertex, row or column vector\n     * If row == 0 \u0026\u0026 col \u003e 0, then set the column vector indicated by col\n     * If col == 0 \u0026\u0026 row \u003e 0, then set the row vector indicated by row\n     * if row \u003e 0 \u0026\u0026 col \u003e 0, set the vertex\n     * row == col == 0 is an error\n     *\n     * @param int $row\n     * @param int $col\n     * @param mixed|Matrix $data If setting a vector, supply the correct vector matrix\n     *\n     * @return Matrix\n     \n     * @throws VerticeOutOfBoundsException\n     * @throws MatrixException\n     */\n    public function set($row, $col, $data);\n\u003c/pre\u003e\n\n#### Matrices have attributes\n\n*  Attributes always return a boolean.\n*  Attributes implement the Chippyash\\Matrix\\Interfaces\\AttributeInterface\n*  You can use the is() method of a Matrix to test for an attribute\n\u003cpre\u003e\n    if ($mA-\u003eis('square')) {}\n    //is the same as\n    $attr = new Matrix/Attribute/IsSquare();\n    if ($attr($mA) {}\n\u003c/pre\u003e\n\nAttributes supported:\n\n*  IsColumnvector() - Is the matrix a column vector?\n*  IsComplete() - Does the matrix have all its entries present?\n*  IsDiagonal() - Are only entries on the main diagonal non-zero?\n*  IsEmpty() - Is the matrix an empty one?\n*  IsRectangle() - Is matrix non-empty and non-square?\n*  IsRowVector() - Is the matrix a row vector?\n*  IsSquare() - Is the matrix square? i.e. n == m \u0026 n \u003e= 0\n*  IsVector() - Is the matrix a row vector or a column vector?\n\n\n#### Matrices can be transformed\n\n*  Transformation always returns a Matrix\n*  The original matrix is untouched\n*  You can use the magic __invoke functionality\n*  Transformations implement the Chippyash\\Matrix\\Interfaces\\TransformationInterface\n\n\u003cpre\u003e\n    $mB = $mA(\"Transpose\");\n    //same as :\n    $comp = new Matrix\\Transformation\\Transpose;\n    $mB = $comp($mA);\n    //or\n    $mB = $mA-\u003etransform($comp);\n\u003c/pre\u003e\n\nTransformations supported:\n\n*  Circshift - shift columns in matrix left or right (default) around the Y axis\n*  Cofactor - return the cofactor matrix of a vertice\n*  Colreduce - return matrix reduced by 1+ columns\n*  Colslice - return a slice (1+) of the matrix columns\n*  Concatenate - return matrix with extra matrix appended to the right\n*  Diagonal - reduce a matrix to its diagonal elements substituting non diagonal entries with zero\n*  MFunction - apply a function to every entry in the matrix\n*  Reflect - reflect matrix on X plane, Y plane, y=x plane and through origin\n*  Resize - resize a matrix by adding or subtracting rows and columns from bottom and right side respectively\n*  Rotate - rotate matrix through 90, 180 or 270 degrees\n*  Rowreduce - return matrix reduced by 1+ rows\n*  Rowslice - return a slice (1+) of matrix rows\n*  Shift - shift matrix rows columns left or right across the matrix replacing new columns with a value\n*  Stack - return matrix stacked on top of extra matrix\n*  Transpose - return matrix with rows and columns swapped around the diagonal\n\n\n#### The magic invoke methods allow you to write in a functional way\n\nFor example (taken from Transformation\\Cofactor):\n\n\u003cpre\u003e\n        $fC = new Colreduce();\n        $fR = new Rowreduce();\n        //R(C(mA))\n        return $fR($fC($mA,[$col]),[$row]);\n\u003c/pre\u003e\n\nor this (from Transformation\\Colreduce):\n\n\u003cpre\u003e\n\n        $fT = new Transpose();\n        $fR = new Rowreduce();\n\n        return $fT($fR($fT($mA), [$col, $numCols]));\n\u003c/pre\u003e\n\n#### Matrices can be output for display\n\nYou can supply a formatter to create output via the display() method. The library\ncurrently has an Ascii formatter.  To use it\n\n\u003cpre\u003e\n    use Chippyash\\Matrix\\Formatter\\Ascii;\n    use Chippyash\\Matrix\\Matrix;\n\n    $mA = new Matrix([[1,2,3],['a','b','c'],[true, false, 'foo']]);\n    echo $mA-\u003esetFormatter(new Asciii())-\u003edisplay();\n\u003c/pre\u003e\n\n*  Formatters implement the Chippyash\\Matrix\\Interfaces\\FormatterInterface\n*  The Matrix::display() method accepts an optional array of options that are passed to the formatter\n*  Formatters are not limited to string output. You could for instance, write a formatter to output an SVG file\n\n#### You can debug a Matrix\n\nIf you find something weird happening, utilise the Debug trait to dump out the\nmatrix to screen using the Ascii formatter.\n\n### Changing the library\n\n1.  fork it\n2.  write the test\n3.  amend it\n4.  do a pull request\n\nFound a bug you can't figure out?\n\n1.  fork it\n2.  write the test\n3.  do a pull request\n\nNB. Make sure you rebase to HEAD before your pull request\n\n## Where?\n\nThe library is hosted at [Github](https://github.com/chippyash/Matrix). It is\navailable at [Packagist.org](https://packagist.org/packages/chippyash/matrix)\n\n### Installation\n\nInstall [Composer](https://getcomposer.org/)\n\n#### For production\n\nadd\n\n\u003cpre\u003e\n    \"chippyash/matrix\": \"\u003e=3,\u003c4\"\n\u003c/pre\u003e\n\nto your composer.json \"requires\" section\n\n#### For development\n\nClone this repo, and then run Composer in local repo root to pull in dependencies\n\n\u003cpre\u003e\n    git clone git@github.com:chippyash/Matrix.git Matrix\n    cd Matrix\n    composer update\n\u003c/pre\u003e\n\nTo run the tests:\n\n\u003cpre\u003e\n    cd Matrix\n    vendor/bin/phpunit -c test/phpunit.xml test/\n\u003c/pre\u003e\n\n## License\n\nThis software library is released under the [BSD 3 Clause license](https://opensource.org/licenses/BSD-3-Clause)\n\nThis software library is Copyright (c) 2014-2018, Ashley Kitson, UK\n\n## History\n\nV1.0.0 Original release\n\nV1.0.1 Minor bug fixes\n\nV1.1.0 New transformations\n\n*  Concatenate\n*  Stack\n*  Rotate\n\nV1.1.1 Code analysis conformance\n\nV1.1.2 Update readme\n\nV1.2.0 New transformation\n\n* Reflect\n\nV1.2.1 Code analysis conformance\n\nV1.2.2 Fix Ascii formatter to work with descendent matrices\n\nV1.2.3 Fix transformations to work with descendent matrices\n\nV1.2.4 Amend IsSquare attribute test to accept empty matrix as square\n\n    Update documentation\n\nV1.2.5 Add equality() method\n\nV1.2.6 Add ability to set the formatter on Debug trait\n\nV1.2.7 Add ability to set the formatter options for debug\n\nV1.2.8 update phpunit to ~V4.3.0\n\nV2.0.0 BC Break: change chippyash\\Matrix namespace to Chippyash\\Matrix\n\nV2.0.1 remove duplicated tests\n\nV2.0.2 Add link to packages\n\nV2.0.3 code cleanup\n\nV2.1.0 Add Circshift transformation\n\nV2.2.0 Add Shift transformation\n\nV2.3.0 Add IsVector attribute\n\nV2.3.1 Add ability to get() method to return vectors\n\nV2.4.0 Add Mutable Set Trait\n\nV2.4.1 code cleanup\n\nV2.4.2 remove user config files\n\nV2.5.0 add Resize transformation\n\nV2.5.1 update build script\n\nV3.0.0 BC Break. Withdraw support for old PHP versions\n\nV3.1.0 Change of license from GPL V3 to BSD 3 Clause\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchippyash%2Fmatrix","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchippyash%2Fmatrix","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchippyash%2Fmatrix/lists"}