酒店预订平台
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 4.8 KiB

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