You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 3.9 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. PHPMatrix
  2. ==========
  3. ---
  4. PHP Class for handling Matrices
  5. Master: [![Build Status](https://travis-ci.org/MarkBaker/PHPMatrix.png?branch=master)](http://travis-ci.org/MarkBaker/PHPMatrix)
  6. Develop: [![Build Status](https://travis-ci.org/MarkBaker/PHPMatrix.png?branch=develop)](http://travis-ci.org/MarkBaker/PHPMatrix)
  7. [![Matrix Transform](https://imgs.xkcd.com/comics/matrix_transform.png)](https://xkcd.com/184/)
  8. Matrix Transform
  9. ---
  10. This library currently provides the following operations:
  11. - addition
  12. - direct sum
  13. - subtraction
  14. - multiplication
  15. - division (using [A].[B]<sup>-1</sup>)
  16. - division by
  17. - division into
  18. together with functions for
  19. - adjoint
  20. - antidiagonal
  21. - cofactors
  22. - determinant
  23. - diagonal
  24. - identity
  25. - inverse
  26. - minors
  27. - trace
  28. - transpose
  29. ## TO DO
  30. - power()
  31. - EigenValues
  32. - EigenVectors
  33. - Decomposition
  34. ---
  35. # Usage
  36. To create a new Matrix object, provide an array as the constructor argument
  37. ```
  38. $grid = [
  39. [16, 3, 2, 13],
  40. [ 5, 10, 11, 8],
  41. [ 9, 6, 7, 12],
  42. [ 4, 15, 14, 1],
  43. ];
  44. $matrix = new Matrix\Matrix($grid);
  45. ```
  46. The `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.
  47. ```
  48. $matrix = new Matrix\Builder::createFilledMatrix(1, 5, 3);
  49. ```
  50. Will create a matrix of 5 rows and 3 columns, filled with a `1` in every cell; while
  51. ```
  52. $matrix = new Matrix\Builder::createIdentityMatrix(3);
  53. ```
  54. will create a 3x3 identity matrix.
  55. Matrix 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).
  56. ## Performing Mathematical Operations
  57. To perform mathematical operations with Matrices, you can call the appropriate method against a matrix value, passing other values as arguments
  58. ```
  59. $matrix1 = new Matrix([
  60. [2, 7, 6],
  61. [9, 5, 1],
  62. [4, 3, 8],
  63. ]);
  64. $matrix2 = new Matrix([
  65. [1, 2, 3],
  66. [4, 5, 6],
  67. [7, 8, 9],
  68. ]);
  69. echo $matrix1->multiply($matrix2);
  70. ```
  71. or pass all values to the appropriate function
  72. ```
  73. $matrix1 = new Matrix([
  74. [2, 7, 6],
  75. [9, 5, 1],
  76. [4, 3, 8],
  77. ]);
  78. $matrix2 = new Matrix([
  79. [1, 2, 3],
  80. [4, 5, 6],
  81. [7, 8, 9],
  82. ]);
  83. echo Matrix\multiply($matrix1, $matrix2);
  84. ```
  85. You can pass in the arguments as Matrix objects, or as arrays.
  86. If 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.
  87. ## Using functions
  88. When calling any of the available functions for a matrix value, you can either call the relevant method for the Matrix object
  89. ```
  90. $grid = [
  91. [16, 3, 2, 13],
  92. [ 5, 10, 11, 8],
  93. [ 9, 6, 7, 12],
  94. [ 4, 15, 14, 1],
  95. ];
  96. $matrix = new Matrix\Matrix($grid);
  97. echo $matrix->trace();
  98. ```
  99. or you can call the function as you would in procedural code, passing the Matrix object as an argument
  100. ```
  101. $grid = [
  102. [16, 3, 2, 13],
  103. [ 5, 10, 11, 8],
  104. [ 9, 6, 7, 12],
  105. [ 4, 15, 14, 1],
  106. ];
  107. $matrix = new Matrix\Matrix($grid);
  108. echo Matrix\trace($matrix);
  109. ```
  110. When called procedurally using the function, you can pass in the argument as a Matrix object, or as an array.
  111. ```
  112. $grid = [
  113. [16, 3, 2, 13],
  114. [ 5, 10, 11, 8],
  115. [ 9, 6, 7, 12],
  116. [ 4, 15, 14, 1],
  117. ];
  118. echo Matrix\trace($grid);
  119. ```
  120. As an alternative, it is also possible to call the method directly from the `Functions` class.
  121. ```
  122. $grid = [
  123. [16, 3, 2, 13],
  124. [ 5, 10, 11, 8],
  125. [ 9, 6, 7, 12],
  126. [ 4, 15, 14, 1],
  127. ];
  128. $matrix = new Matrix\Matrix($grid);
  129. echo Matrix\Functions::trace($matrix);
  130. ```
  131. Used this way, methods must be called statically, and the argument must be the Matrix object, and cannot be an array.