{"id":20254805,"url":"https://github.com/chippyash/math-matrix","last_synced_at":"2025-04-11T00:05:03.147Z","repository":{"id":19026193,"uuid":"22250069","full_name":"chippyash/Math-Matrix","owner":"chippyash","description":"Extends chippyash/Matrix to provide numerical matrix support","archived":false,"fork":false,"pushed_at":"2018-07-04T22:52:44.000Z","size":685,"stargazers_count":2,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-11T00:04:56.919Z","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":"dropbox/zxcvbn","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-25T08:24:34.000Z","updated_at":"2018-07-04T22:52:46.000Z","dependencies_parsed_at":"2022-07-07T22:49:55.481Z","dependency_job_id":null,"html_url":"https://github.com/chippyash/Math-Matrix","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chippyash%2FMath-Matrix","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chippyash%2FMath-Matrix/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chippyash%2FMath-Matrix/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chippyash%2FMath-Matrix/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chippyash","download_url":"https://codeload.github.com/chippyash/Math-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:58.564Z","updated_at":"2025-04-11T00:05:03.118Z","avatar_url":"https://github.com/chippyash.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# chippyash/Math-Matrix\n\n## Quality\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/Math-Matrix.svg?branch=master)](https://travis-ci.org/chippyash/Math-Matrix)\n[![Test Coverage](https://codeclimate.com/github/chippyash/Math-Matrix/badges/coverage.svg)](https://codeclimate.com/github/chippyash/Math-Matrix/coverage)\n[![Code Climate](https://codeclimate.com/github/chippyash/Math-Matrix/badges/gpa.svg)](https://codeclimate.com/github/chippyash/Math-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 \nSee the [Test Contract](https://github.com/chippyash/Math-Matrix/blob/master/docs/Test-Contract.md) (526 tests, 820 assertions)\n\nSee the [API Documentation](http://chippyash.github.io/Math-Matrix/) for further information\n\nPlease note that developer support for PHP5.4 \u0026 5.5 was withdrawn at version 2.0.0 of this library.\nIf you need support for PHP 5.4 or 5.5, please use a version `\u003e=1,\u003c2`\n\n## What?\n\nSince the [JAMA library](http://www.phpmath.com/build02/JAMA/downloads/), there\nhas not been been a library to my knowledge that allows PHP devs to simply\nincorporate arithmetic Matrix functionality within an application.\n\nIf you are using reasonably small matrices then the complexity of having to compile in external\n[Fortran or C based](http://en.wikipedia.org/wiki/LAPACK) libraries\nis something you can do without. And even when you do, it transpires the PHP\nbindings are limited.\n\nYou need speed - PHP is never going to do it for you on big Matrices, start compiling.\nFor everything else, give this a go.\n\nThis library aims to provide arithmetic matrix functionality in the most efficient way\npossible using PHP given that:\n\n*  Everything has a test case\n*  It's PHP 5.5+\n\nThe library is released under the [GNU GPL V3 or later license](http://www.gnu.org/copyleft/gpl.html)\n\n## Why?\n\nThis adds maths to the Chippyash/Matrix library giving you the ability to create\nand manipulate matrices containing numeric (float, int, rational and complex) values.\n\n## When\n\nThe current library covers basic matrix maths.  It is a work in progress and has some\nlimitations.  Addition, subtraction and multiplication  are straight forward and\nshould work for any size matrix.  Division relies on inversion which currently relies on\nthe ability to determine the determinant of a matrix.  The library supports two strategies\nfor finding a determinant - Laplace expansion and LU.  This places a realistic limit on the\nsize of matrices that can be operated on.  See the examples/example-laplace-bounds.php\nand examples/example-lu-determinant-bounds.php scripts to understand why.\nThe limit is a 20x20 matrix when using the Determinant derivative\nin auto mode.  The limit is arbitrary and based on what can computed on my\nlaptop in about a second.  Other brands of machinery may vary.\n\nThis may change in the future as I refactor for performance or incorporate more\nperformant strategies for computing inverses and determinants.  If you are good\nat maths computation, this is an area where you can really help out\n\nIf you want more, either suggest it, or better still, fork it and provide a pull request.\n\nCheck out [chippyash/Matrix](https://github.com/chippyash/Matrix) for underlying matrix operations\n\nCheck out [chippyash/Logical-Matrix](https://github.com/chippyash/Logical-matrix) for logical matrix operations\n\nCheck out [ZF4 Packages](http://zf4.biz/packages?utm_source=github\u0026utm_medium=web\u0026utm_campaign=blinks\u0026utm_content=mathmatrix) for more packages\n\n## How\n\nThe current version of this library utilises the PHP Native numeric Strong Types as the\ncalculator that it uses can only deal with them at present.  GMP support is on the\nroadmap once the calculator provides it.  You can ensure that this is enforced by\nforcing its setting with a call to \n\n\u003cpre\u003e\nuse Chippyash\\Type\\RequiredType;\nRequiredType::getInstance()-\u003eset(RequiredType::TYPE_NATIVE);\n\u003c/pre\u003e\n\nbefore any operation with the matrices.\n\n### Coding Basics\n\nIn PHP terms a matrix is an array of arrays, 2 dimensional i.e\n\n-  [[]]\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 tempered amongst us the salient\npoints are:\n\nThe library extends the Chippyash/Matrix library, so anything you can do on a basic\nmatrix, you can do with a numeric matrix.  The library utilises the Chippyash/Strong-Type\nstrong types.\n\nThree basic Matrix types are supplied\n\n* NumericMatrix: containing int, float, IntType, WholeIntType, NaturalIntType,\nFloatType, RationalType and ComplexType data items\n* RationalMatrix: containing only RationalType data items\n* ComplexMatrix: containing only ComplexType data items\n\nThe NumericMatrix is fine for general purpose work, but not if you want prove that\nthe inverse(M) * M = Identity.  For that you'll need the RationalMatrix which is\nfar more arithmetically stable.  This is also the matrix that will benefit from\nforthcoming support of PHP's various maths extension libraries in the Chippyash/Strong-Type\nlibrary.\n\nThe ComplexMatrix fully supports the Chippyash\\Type\\Numeric\\Complex\\ComplexType.\n\nBoth the RationalMatrix and ComplexMatrix extend the NumericMatrix.\n\nCreating a numeric type matrix is straightforward:\n\n\u003cpre\u003e\n    use Chippyash\\Math\\Matrix\\NumericMatrix;\n    use Chippyash\\Math\\Matrix\\RationalMatrix;\n    use Chippyash\\Math\\Matrix\\ComplexMatrix;\n    //create empty matrices\n    $mA = new NumericMatrix([]);\n    $mB = new RationalMatrix([]);\n    $mC = new ComplexMatrix([]);\n\u003c/pre\u003e\n Supplying data to matrix construction will create data items inside the matrix\naccording to the following rules:\n\n*  NumericMatrix: items are created in the lowest NumericTypeInterface possible.  The\nlow to high order is IntType, FloatType, RationalType, ComplexType\n*  RationalMatrix: items are created as RationalType\n*  ComplexMatrix items are created as ComplexType.  If you supply non-complex data\nitems, then 'real' complex items are created, i.e. the imaginary part == zero\n\nBe aware that operations on a NumericMatrix will probably produce the result\nhaving different data type entries.  This is inevitable given the constraints\nplaced on computer based arithmetic. If any operation requires the item to be\ncast upwards then it is unlikely to be cast downwards again.\n\nSome helpers are provided:\n\n#### MatrixFactory\n\nprovides static methods:\n\n*  MatrixFactory::create(string $type, array $data).  $type is one of numeric, rational or complex\n*  MatrixFactory::createComplex(array $data)\n*  MatrixFactory::createRational(array $data)\n*  MatrixFactory::createNumeric(array $data)\n*  MatrixFactory::createFromFunction(callable $fn, IntType $rows, IntType $cols, $type = 'numeric')\n*  MatrixFactory::createFromComplex(ComplexType $c)\n\n#### FunctionMatrix\n\n\\[DEPRECATED - Use SpecialMatrix::create('functional', int:rows, int:cols, \\Closure:f(int:rows, int:cols))\\] instead\n\nCreate a numeric matrix as a result of applying a function.\n\n\u003cpre\u003e\n    $f = function($row, $col) {return TypeFactory::createInt($row * $col);};\n    $mA = new FunctionMatrix($f, TypeFactory::createInt(3), TypeFactory::createInt(4));\n\u003c/pre\u003e\n\n#### IdentityMatrix\n\n\\[DEPRECATED - Use SpecialMatrix::create('identity',int:size) instead\\]\n\nCreate a NumericMatrix Identity matrix\n\n\u003cpre\u003e\n    //create 4x4 identity matrix with integer entries\n    $mA = new IdentityMatrix(TypeFactory::createInt(4));\n    //or more usually, use the factory method\n    $mA - IdentityMatrix::numericIdentity(TypeFactory::createInt(4));\n\u003c/pre\u003e\n\nCreate rational and complex identity matrices using factory methods:\n\n\u003cpre\u003e\n    $mR = IdentityMatrix::rationalIdentity(TypeFactory::createInt(4));\n    $mC = IdentityMatrix::complexIdentity(TypeFactory::createInt(4));\n\u003c/pre\u003e\n\n#### ZeroMatrix\n\n\\[DEPRECATED - Use SpecialMatrix::create('zeros', int:rows\\[, int:cols\\])\\]\n\nCreate a NumericMatrix with all entries set to zero\n\n\u003cpre\u003e\n    //create 2 x 4 zero matrix\n    $mZ = new ZeroMatrix(TypeFactory::createInt(2), TypeFactory::createInt(4));\n\u003c/pre\u003e\n\n#### ShiftMatrix\n\nCreate a [Shift Matrix](https://en.wikipedia.org/wiki/Shift_matrix)\n\n\u003cpre\u003e\n\t$mA - IdentityMatrix::numericIdentity(TypeFactory::createInt(5));\n\t\n\t//create 5 x 5 shift matrices\n\t$mSupper = new ShiftMatrix(new IntType(5), new StringType(ShiftMatrix::SM_TYPE_UPPER);\n\t$mSlower = new ShiftMatrix(new IntType(5), new StringType(ShiftMatrix::SM_TYPE_LOWER);\n\t//optionally specify the matrix content type\n\t$mSupper = new ShiftMatrix(new IntType(5), new StringType(ShiftMatrix::SM_TYPE_UPPER, new IntType(IdentityMatrix::IDM_TYPE_COMPLEX));\n\t\n\t$mC = $mA('Mul\\Matrix', $mSupper);\n\t$mD = $mLower('Mul\\Matrix', $mA);\n\u003c/pre\u003e\n\n#### SpecialMatrix\n\nProvides numerous special matrices:\n\nAdopting an idea from [Octave Gallery Matrices](https://www.gnu.org/software/octave/doc/v4.0.1/Famous-Matrices.html)\n\n\u003cpre\u003e\n//inline creation if your version of PHP allows it\nuse Chippyash\\Math\\Matrix\\SpecialMatrix;\n$mS = (new SpecialMatrix())-\u003ecreate(new StringType('NameOfMatrix')[, $arg1, $arg2]);\n\n//or as an invokable class\n$factory = new SpecialMatrix();\n$mS = $factory(new StringType('nameOfMatrix')[, $arg1, $arg2]);\n//or\n$mS = $factory('NameOfMatrix'[, $arg1, $arg2]);\n\u003c/pre\u003e\n\nMatrices provided:\n\n*  Ones Matrix/Vector: create('ones', int:rows) or create ('ones', int:rows, int:cols)\n*  Cauchy Matrix: create('cauchy', int:x) or create('cauchy', vector:x, vector:y)\n*  Identity Matrix: create('identity', int:size)\n*  Functional Matrix: create('functional', int:rows, int:cols, \\\\Closure: f(int:rows, int:cols))\n*  Zeros Matrix/Vector: create('zeros', int:rows) or create ('zeros', int:rows, int:cols)\n\nAll returned matrices are NumericMatrices.\n\nReally important: We don't do this shit by ourselves. So read the tests and read the source files\nwhere you'll find attribution to some far cleverer people than me. I just try\nto translate into the PHP world.\n \n####  Numeric matrices have additional attributes\n\n*  IsComplex: boolean - Is the matrix instanceof ComplexMatrix?\n*  IsIdentity: boolean - Is the matrix an identity matrix?\n*  IsNonsingular: boolean - Is the matrix non singular\n*  IsNumeric: boolean - Is the matrix instanceof NumericMatrix?\n*  IsRational: boolean -  Is the matrix instanceof RationalMatrix?\n*  IsMarkov: boolean - Does matrix conform to requirements for a Markov Chain Matrix\n\nRemember, you can use the is() method to test for an attribute on a matrix.\n\n####  Matrices can be computed\n\n*  Computations always return a matrix.\n*  The original matrix is untouched\n*  You can use the magic __invoke functionality\n*  Computations implement the Chippyash\\Math\\Matrix\\Interfaces\\ComputationInterface\n*  Computations work with scalar values or other matrices.  Non scalar values will throw an exception\n\nOn the whole, computations, will work with any scalar but:\n\n**All matrix computations follow the natural laws**\n\n*  you can't divide by zero\n*  dividing a matrix by a non invertible matrix is like 1/0 - oops!\n*  using float, int and rational data items will work together\n*  using complex number data items: all items have to be complex\n\nThe following computations are provided (using the magic invoke interface method):\n\n*  'Add\\Scalar'    : add scalar value to the matrix\n*  'Add\\Matrix'    : add a matrix to the matrix\n*  'Sub\\Scalar'    : subtract scalar value from the matrix\n*  'Sub\\Matrix'    : subtract a matrix from the matrix\n*  'Mul\\Scalar'    : multiply matrix by scalar value\n*  'Mul\\Matrix'    : multiply matrix by another matrix using common [Matrix Product Method](https://en.wikipedia.org/wiki/Matrix_multiplication#General_definition_of_the_matrix_product)\n*  'Mul\\Entrywise' : multiply matrix by another matrix using [Hadamard or Schur Product Method](https://en.wikipedia.org/wiki/Matrix_multiplication#Hadamard_product)\n*  'Div\\Scalar'    : divide matrix by scalar value\n*  'Div\\Matrix'    : divide matrix by another matrix - see notes at head of this readme\n*  'Div\\Entrywise' : divide matrix by another matrix using an Entrywise method. Where a particular vertex is division\n by zero, a zero will be resulted.  This is a defensive strategy and there is no right answer.\n Best defence is to ensure that the matrix you are dividing by does not contain zeros \n\n\u003cpre\u003e\n    $mC = $mA('Mul\\Matrix', $mB);\n    //same as\n    $fMul = new Chippyash\\Math\\Matrix\\Computation\\Mul\\Matrix();\n    $mC = $mA-\u003ecompute($fMul, $mB)\n    //same as\n    $mC = $fMul-\u003ecompute($mA, $mB);\n\u003c/pre\u003e\n\n#### You can derive other information from a matrix\n\n*  Derivatives always return a numeric result\n*  The original matrix is untouched\n*  You can use the magic __invoke functionality\n*  Derivatives implement the Chippyash\\Math\\Matrix\\Interfaces\\DerivativeInterface\n\nFour derivatives are currently supplied.\n\n*  Determinant\n\n\u003cpre\u003e\n    $det = $mA(\"Determinant\");\n    //same as\n    $fDet = new Chippyash\\Math\\Matrix\\Derivative\\Determinant();\n    $det = $mA-\u003ederive($fDet);\n    //same as\n    $det = $fDet($mA);\n\u003c/pre\u003e\n\nAs noted above, the Determinant derivative currently only supports up to a 20x20 matrix\nin auto mode. It will bork if you supply a matrix bigger than that. This will\nbe ok for most purposes.  If you are happy to wait a while to compute determinants\nfor bigger matrices, create the determinant by construction (second way above)\nand specify Determinant::METHOD_LU as a construction parameter.\nDeterminant::METHOD_LAPLACE is left for historical reasons, as there is very\nlittle likelihood you will want to use it!\n\n*  Trace. Returns the trace of a square matrix\n\n\u003cpre\u003e\n    $tr = $mA('Trace');\n    //or other variations as with Determinant\n\u003c/pre\u003e\n\n*  Sum. Simply sums all the vertices in the matrix and returns the result\n\n\u003cpre\u003e\n    $sum = $mA('Sum');\n    //or other variations as with Determinant\n\u003c/pre\u003e\n\n* MarkovWeightedRandom. Return the next step using Random Weighted method on a \nMarkov Chain Matrix\n\n\u003cpre\u003e\n    $mA = new NumericMatrix(\n        [\n            [0,2,0,8]  //row 1\n            [1,0,0,0]  //row 2\n            [0,0,5,5]  //row 3\n            [0,1,6,3]  //row 4\n        ]\n    )\n    \n    $next = $mA('MarkovWeightedRandom', TypeFactory::createInt(3));\n    //will return IntType(3) or IntType(4) with 50% chance of either being returned\n    \n\u003c/pre\u003e\n\nA NotMarkovException will be thrown if the supplied Matrix does not conform to the\nIsMarkov Attribute.  Please note, that whilst you can supply a matrix with floats, the\nnature of mt_rand() function used to generate the next number means that, for the time\nbeing, you should provide integer values.  As long as you supply a square matrix where\neach row row sums to the same number, you have a Markov Chain expressed as a Matrix.\n\n#### Additional transformations are supported by numeric matrices\n\n*  Invert - Returns the inverted matrix or throws an exception if not possible.\nThe current inversion method relies on determinants and as noted, this is only\ncurrently feasible for matrices up to 20x20\n\n\u003cpre\u003e\n    try {\n        $mI = $mA('Invert');\n        //same as\n        $fInvert = new Chippyash\\Math\\Matrix\\Transformation\\Invert();\n        $mI = $mA-\u003etransform($fInvert);\n        //same as\n        $mI = $fInvert($mA);\n    } catch (Chippyash\\Math\\Matrix\\Exceptions\\ComputationException $e) {\n        //cannot invert\n    }\n\u003c/pre\u003e\n\nIf you want to break the 20x20 limit, you can do the following:\n\n\u003cpre\u003e\n    $det = new Chippyash\\Math\\Matrix\\Derivative\\Determinant();\n    $det-\u003etune('luLimit', 40); //or whatever you are prepared to put up with\n    $mI = $mA('Invert');\n\u003c/pre\u003e\n\n*  MarkovRandomWalk - Given a Markov Chain represented as a Numeric Matrix, randomly \nwalk from a start row to a target row, returning a Row Vector Numeric Matrix of IntTypes:\n\n\u003cpre\u003e\n    $det = new Chippyash\\Math\\Matrix\\Derivative\\MarkovRandomWalk();\n    $res = $det-\u003etransform(\n        $mA, \n        array(\n            'start'=\u003eTypeFactory::createInt(2), \n            'target'=\u003eTypeFactory::createInt(4)\n        )\n    );\n\u003c/pre\u003e\n\nYou can supply an optional third parameter, `limit` to limit the steps that can be taken.\nThis is set to 100 by default.\n\n\u003cpre\u003e\n    $res = $mA(\n        'MarkovRandomWalk', \n        array(\n            'start'=\u003eTypeFactory::createInt(2), \n            'target'=\u003eTypeFactory::createInt(4),\n            'limit'=\u003eTypeFactory::createInt(10)\n        )\n    );\n\u003c/pre\u003e\n\n\n#### Matrices can be decomposed\n\n*  The original matrix is untouched\n*  You can use the magic __invoke functionality\n*  Decompositions return a decomposition object, from which you can access the various parts of the decomposition.\n*  Decompositions implement the Chippyash\\Math\\Matrix\\Interfaces\\DecompositionInterface\n\nThe library currently supports:\n\n*  LU Decomposition\n*  Gauss Jordan Elimination\n\n**LU**\n\u003cpre\u003e\n    $lu = $mA('Lu');\n    //same as\n    $fLu = new Chippyash\\Math\\Matrix\\Decomposition\\Lu()\n    $lu = $mA-\u003edecompose($fLu);\n    //same as\n    $lu = $fLu($mA);\n\u003c/pre\u003e\n\nThe LU products are:\n\n*  LU : NumericMatrix - The LU complete decomposition matrix\n*  L : NumericMatrix - The lower triangle\n*  U : NumericMatrix - The upper triangle\n*  PivotVector : NumericMatrix - Pivot vector of the decomposition\n*  PermutationMatrix : NumericMatrix - Permutation matrix\n*  Det : NumericTypeInterface|Null - Determinant or null if matrix is not square\n\nAccessing the products is either via the product() method or more simply as an\nattribute of the decomposition:\n\n\u003cpre\u003e\n    $pv = $lu-\u003eproduct('PivotVector');\n    //same as\n    $pv = $lu-\u003ePivotVector;\n\u003c/pre\u003e\n\nN.B.  Product names for any decomposition are case sensitive\n\n**Gauss Jordan Elimination**\n\n\u003cpre\u003e\n    $mA = new NumericMatrix(\n                [[1, 1, 1],\n                 [2, 3, 5],\n                 [4, 0, 5]]\n                );\n    $mB = new NumericMatrix(\n            [[5],\n             [8],\n             [2]]\n            );\n    $elim = $mA('GaussJordonElimination', $mB);\n    //same as\n    $fElim = new Chippyash\\Math\\Matrix\\Decomposition\\GaussJordonElimination()\n    $elim = $mA-\u003edecompose($fElim, $mB);\n    //same as\n    $elim = $fElim($mA, $mB);\n\u003c/pre\u003e\n\nThe products are:\n\n*  left  The left matrix after elimination\n*  right The right matrix after elimination\n\nUsing the above example then $elim-\u003eleft should == an identity matrix and\n$elim-\u003eright == [[3],[4],[-2]] as we've just solved\n\n\u003cpre\u003e\n    x + y + z = 5\n    2x + 3y + 5z = 8\n    4x + 5z = 2\nwhere\n    x = 3, y = 4, z = -2\n\u003c/pre\u003e\n\n#### Formatting for numeric display\n\nAn additional display formatter is supported by the library:\n\n\\Chippyash\\Math\\Matrix\\Formatter\\AsciiNumeric\n\nIt extends the \\Chippyash\\Matrix\\Formatter\\Ascii.  An additional option 'outputType'\nis provided that should take one of the following values:\n\n\u003cpre\u003e\n    AsciiNumeric::TP_NONE      //behave as base formatter - default behaviour\n    AsciiNumeric::TP_INT       //convert all entries to int.  This will floor() if possible\n    AsciiNumeric::TP_FLOAT     //convert all entries to float if possible\n    AsciiNumeric::TP_RATIONAL  //convert all entries to rational if possible\n    AsciiNumeric::TP_COMPLEX   //convert all entries to complex (always possible)\n\u003c/pre\u003e\n\nPlease note that although you instruct to convert to a particular numeric type the\nactual display may result in a simpler form if possible.\n\nExample:\n\n\u003cpre\u003e\n    echo $mA-\u003esetFormatter(new AsciiNumeric())\n        -\u003edisplay(['outputType' =\u003e AsciiNumeric::TP_FLOAT]);\n\u003c/pre\u003e\n\n#### Formatting for a directed graph\n\nTo use this functionality you need to include\n\n\u003cpre\u003e\n    \"clue/graph\": \"^0.9\",\n    \"graphp/graphviz\": \"0.2.*\"\n\u003c/pre\u003e\n\nto your composer requires section and run a composer update.\n\nThis adds the functionality to use the GraphViz suite of programs to create image files\nfrom a NumericMatrix that describes a graph.\n  \nThe simplest use of the renderer is to simply supply it a NumericMatrix:\n\n\u003cpre\u003e\n$mA = new NumericMatrix([[0,0,1],[0,1,0],[1,0,0]]);\n$dot = $mA-\u003esetFormatter(new DirectedGraph())-\u003edisplay();\n\u003c/pre\u003e\n\nwill produce a GraphViz .dot file content string such as:\n \n\u003cpre\u003e\ndigraph G {\n  0 -\u003e 2 [label=1]\n  1 -\u003e 1 [label=1 dir=\"none\"]\n  2 -\u003e 0 [label=1]\n}\n\u003c/pre\u003e\n\nMot often however, you'll want to give some meaning to your vertices.  To do this, you \ncan pass in a Monad\\Collection of VertexDescription objects.  For example\n\n\u003cpre\u003e\nuse Monad\\Collection;\nuse Chippyash\\Math\\Matrix\\Formatter\\DirectedGraph\\VertexDescription;\n\n$attribs = new Collection(\n    [\n        new VertexDescription(new StringType('A')),\n        new VertexDescription(new StringType('B')),\n        new VertexDescription(new StringType('C')),\n    ]\n);\n$dot = $mA-\u003esetFormatter(new DirectedGraph())-\u003edisplay(['attribs' =\u003e $attribs]);\n\u003c/pre\u003e\n\ngives\n\n\u003cpre\u003e\n'digraph G {\n  \"A\" -\u003e \"C\" [label=1]\n  \"B\" -\u003e \"B\" [label=1 dir=\"none\"]\n  \"C\" -\u003e \"A\" [label=1]\n}\n\u003c/pre\u003e\n\nYou can also set things like colours and shapes via the VertexDescription.  Take a\nlook at the tests.\n\nIf you want to do some additional processing prior to generating something through GraphViz,\nyou can pass in an optional parameter:\n\n\u003cpre\u003e\n$graph = $mA-\u003esetFormatter(new DirectedGraph())-\u003edisplay(['output' =\u003e 'object']);\n\u003c/pre\u003e\n\nwill return a Fhaculty\\Graph\\Graph object that you can process further before sending\ninto Graphp\\GraphViz\\GraphViz.  You may also want to get a Graph so that you can do some\ngraph processing via the Graphp\\Algorithms library.\n\nFinally, with the DirectedGraph renderer, you can provide an optional callable function\nthat will be applied to the values of the edges.  This is often useful to reformat the value\nin some way.  For instance:\n\n\u003cpre\u003e\n$mA = new NumericMatrix([[0,50,50],[33,33,33],[100,0,0]]);\n$func = function($origValue) {return \\round($origValue / 100, 2);};\n$dot = $mA-\u003esetFormatter(new DirectedGraph())-\u003edisplay(['edgeFunc' =\u003e $func]);\n\u003c/pre\u003e\n\nwill produce\n\n\u003cpre\u003e\ndigraph G {\n  0 -\u003e 1 [label=0.5]\n  0 -\u003e 2 [label=0.5]\n  1 -\u003e 0 [label=0.33]\n  1 -\u003e 1 [label=0.33 dir=\"none\"]\n  1 -\u003e 2 [label=0.33]\n  2 -\u003e 0 [label=1]\n}\n\u003c/pre\u003e\n\n#### Exception handling\n\nAs matrix maths can throw up problems, particularly when inverting or decomposing,\nit is always a good idea to wrap whatever you are doing in a try - catch block.\nThe following exceptions are supported by the library.  They all extend from the\nChippyash\\Matrix\\Exceptions\\MatrixException.  The base namespace is Chippyash\\Math\\Matrix\\Exceptions\n\n\u003cpre\u003e\n  MathMatrixException\n  ComputationException\n  NoInverseException\n  SingularMatrixException\n  UndefinedComputationException\n  NotMarkovException\n\u003c/pre\u003e\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/Math-Matrix).\nIt is available at [Packagist.org](https://packagist.org/packages/chippyash/math-matrix) as a\n[Composable](https://getcomposer.org/) module\n\n### Installation\n\nInstall [Composer] (https://getcomposer.org/)\n\n#### For production\n\nadd\n\n\u003cpre\u003e\n    \"chippyash/math-matrix\": \"\u003e=2,\u003c3\"\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/Math-Matrix.git Math-Matrix\n    cd Math-Matrix\n    composer install\n\u003c/pre\u003e\n\nTo run the tests:\n\n\u003cpre\u003e\n    cd Math-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 Initial Release - after 2 years of development - yippee!\n\nV1.1.0 Update dependencies\n\nV1.2.0 Add Entrywise calculations\n\nV1.2.1 Eradicate calls to plain numeric strong types - use the factories\n\nV1.2.2 Fix AsciiNumeric formatter - don't format strings\n\nV1.2.3 Add link to packages\n\nV1.3.0 Add Directed Graph from Matrix rendering\n\nV1.4.0 Add ShiftMatrix\n\nV1.5.0 Add Special Matrices\n```\nDeprecation notice: IdentityMatrix, ZeroMatrix, FunctionMatrix are\ndeprecated, Use the SpecialMatrix instead to create these.  I found a\nproblem in some of the auto conversion algorithms used in the Matrix calculator\nthat depended on class names.  The new convention ensures that these matrix types\nare all returned as NumericMatrix objects which is more conformant with their\nintended use.  I will shortly deprecate ShiftMatrix in favour of a SpecialMatrix\ntype instead, but as it's brand new, it probably won't effect too many.\n\nSpecialMatrix allows for arbitrary inclusion of all sorts of matrices and the idea\ncomes from the Octave/Matlab world. I probably won't get round to including all those\nthat are provided by Octave/Matlab, but it is certainly an opportunity for others\nto implement the other types provided by those libraries as required.\n\nIn due course, the version number for this library will be bumped at which\npoint the old classes will disappear.\n```\nV1.5.1 dependency update\n\nV1.5.2 dependency update\n\nV1.5.3 build script update\n\nV1.5.4 update composer - forced by packagist composer.json format change\n\nV2.0.0 BC Break. Withdraw support for old PHP versions\n\nV2.1.0 Change of license from GPL V3 to BSD 3 Clause","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchippyash%2Fmath-matrix","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchippyash%2Fmath-matrix","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchippyash%2Fmath-matrix/lists"}