1PHPMatrix 2========== 3 4--- 5 6PHP Class for handling Matrices 7 8Master: [![Build Status](https://travis-ci.org/MarkBaker/PHPMatrix.png?branch=master)](http://travis-ci.org/MarkBaker/PHPMatrix) 9 10Develop: [![Build Status](https://travis-ci.org/MarkBaker/PHPMatrix.png?branch=develop)](http://travis-ci.org/MarkBaker/PHPMatrix) 11 12[![Matrix Transform](https://imgs.xkcd.com/comics/matrix_transform.png)](https://xkcd.com/184/) 13 14Matrix Transform 15 16--- 17 18This library currently provides the following operations: 19 20 - addition 21 - direct sum 22 - subtraction 23 - multiplication 24 - division (using [A].[B]<sup>-1</sup>) 25 - division by 26 - division into 27 28together with functions for 29 30 - adjoint 31 - antidiagonal 32 - cofactors 33 - determinant 34 - diagonal 35 - identity 36 - inverse 37 - minors 38 - trace 39 - transpose 40 41 42## TO DO 43 44 - power() 45 - EigenValues 46 - EigenVectors 47 - Decomposition 48 49--- 50 51# Usage 52 53To create a new Matrix object, provide an array as the constructor argument 54 55``` 56$grid = [ 57 [16, 3, 2, 13], 58 [ 5, 10, 11, 8], 59 [ 9, 6, 7, 12], 60 [ 4, 15, 14, 1], 61]; 62 63$matrix = new Matrix\Matrix($grid); 64``` 65The `Builder` class provides helper methods for creating specific matrices, specifically an identity matrix of a specified size; or a matrix of a specified dimensions, with every cell containing a set value. 66``` 67$matrix = new Matrix\Builder::createFilledMatrix(1, 5, 3); 68``` 69Will create a matrix of 5 rows and 3 columns, filled with a `1` in every cell; while 70``` 71$matrix = new Matrix\Builder::createIdentityMatrix(3); 72``` 73will create a 3x3 identity matrix. 74 75 76Matrix objects are immutable: whenever you call a method or pass a grid to a function that returns a matrix value, a new Matrix object will be returned, and the original will remain unchanged. This also allows you to chain multiple methods as you would for a fluent interface (as long as they are methods that will return a Matrix result). 77 78## Performing Mathematical Operations 79 80To perform mathematical operations with Matrices, you can call the appropriate method against a matrix value, passing other values as arguments 81 82``` 83$matrix1 = new Matrix([ 84 [2, 7, 6], 85 [9, 5, 1], 86 [4, 3, 8], 87]); 88$matrix2 = new Matrix([ 89 [1, 2, 3], 90 [4, 5, 6], 91 [7, 8, 9], 92]); 93 94echo $matrix1->multiply($matrix2); 95``` 96or pass all values to the appropriate function 97``` 98$matrix1 = new Matrix([ 99 [2, 7, 6], 100 [9, 5, 1], 101 [4, 3, 8], 102]); 103$matrix2 = new Matrix([ 104 [1, 2, 3], 105 [4, 5, 6], 106 [7, 8, 9], 107]); 108 109echo Matrix\multiply($matrix1, $matrix2); 110``` 111You can pass in the arguments as Matrix objects, or as arrays. 112 113If you want to perform the same operation against multiple values (e.g. to add three or more matrices), then you can pass multiple arguments to any of the operations. 114 115## Using functions 116 117When calling any of the available functions for a matrix value, you can either call the relevant method for the Matrix object 118``` 119$grid = [ 120 [16, 3, 2, 13], 121 [ 5, 10, 11, 8], 122 [ 9, 6, 7, 12], 123 [ 4, 15, 14, 1], 124]; 125 126$matrix = new Matrix\Matrix($grid); 127 128echo $matrix->trace(); 129``` 130or you can call the function as you would in procedural code, passing the Matrix object as an argument 131``` 132$grid = [ 133 [16, 3, 2, 13], 134 [ 5, 10, 11, 8], 135 [ 9, 6, 7, 12], 136 [ 4, 15, 14, 1], 137]; 138 139$matrix = new Matrix\Matrix($grid); 140echo Matrix\trace($matrix); 141``` 142When called procedurally using the function, you can pass in the argument as a Matrix object, or as an array. 143``` 144$grid = [ 145 [16, 3, 2, 13], 146 [ 5, 10, 11, 8], 147 [ 9, 6, 7, 12], 148 [ 4, 15, 14, 1], 149]; 150 151echo Matrix\trace($grid); 152``` 153As an alternative, it is also possible to call the method directly from the `Functions` class. 154``` 155$grid = [ 156 [16, 3, 2, 13], 157 [ 5, 10, 11, 8], 158 [ 9, 6, 7, 12], 159 [ 4, 15, 14, 1], 160]; 161 162$matrix = new Matrix\Matrix($grid); 163echo Matrix\Functions::trace($matrix); 164``` 165Used this way, methods must be called statically, and the argument must be the Matrix object, and cannot be an array. 166