Functions.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace Matrix;
  3. class Functions
  4. {
  5. /**
  6. * Calculate the adjoint of the matrix
  7. *
  8. * @param Matrix $matrix The matrix whose adjoint we wish to calculate
  9. * @return Matrix
  10. **/
  11. private static function getAdjoint(Matrix $matrix)
  12. {
  13. return self::transpose(
  14. self::getCofactors($matrix)
  15. );
  16. }
  17. /**
  18. * Return the adjoint of this matrix
  19. * The adjugate, classical adjoint, or adjunct of a square matrix is the transpose of its cofactor matrix.
  20. * The adjugate has sometimes been called the "adjoint", but today the "adjoint" of a matrix normally refers
  21. * to its corresponding adjoint operator, which is its conjugate transpose.
  22. *
  23. * @param Matrix $matrix The matrix whose adjoint we wish to calculate
  24. * @return Matrix
  25. * @throws Exception
  26. **/
  27. public static function adjoint(Matrix $matrix)
  28. {
  29. if (!$matrix->isSquare()) {
  30. throw new Exception('Adjoint can only be calculated for a square matrix');
  31. }
  32. return self::getAdjoint($matrix);
  33. }
  34. /**
  35. * Calculate the cofactors of the matrix
  36. *
  37. * @param Matrix $matrix The matrix whose cofactors we wish to calculate
  38. * @return Matrix
  39. **/
  40. private static function getCofactors(Matrix $matrix)
  41. {
  42. $cofactors = self::getMinors($matrix);
  43. $dimensions = $matrix->rows;
  44. $cof = 1;
  45. for ($i = 0; $i < $dimensions; ++$i) {
  46. $cofs = $cof;
  47. for ($j = 0; $j < $dimensions; ++$j) {
  48. $cofactors[$i][$j] *= $cofs;
  49. $cofs = -$cofs;
  50. }
  51. $cof = -$cof;
  52. }
  53. return new Matrix($cofactors);
  54. }
  55. /**
  56. * Return the cofactors of this matrix
  57. *
  58. * @param Matrix $matrix The matrix whose cofactors we wish to calculate
  59. * @return Matrix
  60. * @throws Exception
  61. **/
  62. public static function cofactors(Matrix $matrix)
  63. {
  64. if (!$matrix->isSquare()) {
  65. throw new Exception('Cofactors can only be calculated for a square matrix');
  66. }
  67. return self::getCofactors($matrix);
  68. }
  69. private static function getDeterminantSegment(Matrix $matrix, $row, $column)
  70. {
  71. $tmpMatrix = $matrix->toArray();
  72. unset($tmpMatrix[$row]);
  73. array_walk(
  74. $tmpMatrix,
  75. function (&$row) use ($column) {
  76. unset($row[$column]);
  77. }
  78. );
  79. return self::getDeterminant(new Matrix($tmpMatrix));
  80. }
  81. /**
  82. * Calculate the determinant of the matrix
  83. *
  84. * @param Matrix $matrix The matrix whose determinant we wish to calculate
  85. * @return float
  86. **/
  87. private static function getDeterminant(Matrix $matrix)
  88. {
  89. $dimensions = $matrix->rows;
  90. if ($dimensions == 1) {
  91. return $matrix->getValue(1, 1);
  92. } elseif ($dimensions == 2) {
  93. return $matrix->getValue(1, 1) * $matrix->getValue(2, 2) - $matrix->getValue(1, 2) * $matrix->getValue(2, 1);
  94. }
  95. $determinant = 0;
  96. for ($i = 1; $i <= $dimensions; ++$i) {
  97. $det = $matrix->getValue(1, $i) * self::getDeterminantSegment($matrix, 0, $i-1);
  98. if (($i % 2) == 0) {
  99. $determinant -= $det;
  100. } else {
  101. $determinant += $det;
  102. }
  103. }
  104. return $determinant;
  105. }
  106. /**
  107. * Return the determinant of this matrix
  108. *
  109. * @param Matrix $matrix The matrix whose determinant we wish to calculate
  110. * @return float
  111. * @throws Exception
  112. **/
  113. public static function determinant(Matrix $matrix)
  114. {
  115. if (!$matrix->isSquare()) {
  116. throw new Exception('Determinant can only be calculated for a square matrix');
  117. }
  118. return self::getDeterminant($matrix);
  119. }
  120. /**
  121. * Return the diagonal of this matrix
  122. *
  123. * @param Matrix $matrix The matrix whose diagonal we wish to calculate
  124. * @return Matrix
  125. * @throws Exception
  126. **/
  127. public static function diagonal(Matrix $matrix)
  128. {
  129. if (!$matrix->isSquare()) {
  130. throw new Exception('Diagonal can only be extracted from a square matrix');
  131. }
  132. $dimensions = $matrix->rows;
  133. $grid = Builder::createFilledMatrix(0, $dimensions, $dimensions)
  134. ->toArray();
  135. for ($i = 0; $i < $dimensions; ++$i) {
  136. $grid[$i][$i] = $matrix->getValue($i + 1, $i + 1);
  137. }
  138. return new Matrix($grid);
  139. }
  140. /**
  141. * Return the antidiagonal of this matrix
  142. *
  143. * @param Matrix $matrix The matrix whose antidiagonal we wish to calculate
  144. * @return Matrix
  145. * @throws Exception
  146. **/
  147. public static function antidiagonal(Matrix $matrix)
  148. {
  149. if (!$matrix->isSquare()) {
  150. throw new Exception('Anti-Diagonal can only be extracted from a square matrix');
  151. }
  152. $dimensions = $matrix->rows;
  153. $grid = Builder::createFilledMatrix(0, $dimensions, $dimensions)
  154. ->toArray();
  155. for ($i = 0; $i < $dimensions; ++$i) {
  156. $grid[$i][$dimensions - $i - 1] = $matrix->getValue($i + 1, $dimensions - $i);
  157. }
  158. return new Matrix($grid);
  159. }
  160. /**
  161. * Return the identity matrix
  162. * The identity matrix, or sometimes ambiguously called a unit matrix, of size n is the n × n square matrix
  163. * with ones on the main diagonal and zeros elsewhere
  164. *
  165. * @param Matrix $matrix The matrix whose identity we wish to calculate
  166. * @return Matrix
  167. * @throws Exception
  168. **/
  169. public static function identity(Matrix $matrix)
  170. {
  171. if (!$matrix->isSquare()) {
  172. throw new Exception('Identity can only be created for a square matrix');
  173. }
  174. $dimensions = $matrix->rows;
  175. return Builder::createIdentityMatrix($dimensions);
  176. }
  177. /**
  178. * Return the inverse of this matrix
  179. *
  180. * @param Matrix $matrix The matrix whose inverse we wish to calculate
  181. * @return Matrix
  182. * @throws Exception
  183. **/
  184. public static function inverse(Matrix $matrix)
  185. {
  186. if (!$matrix->isSquare()) {
  187. throw new Exception('Inverse can only be calculated for a square matrix');
  188. }
  189. $determinant = self::getDeterminant($matrix);
  190. if ($determinant == 0.0) {
  191. throw new Exception('Inverse can only be calculated for a matrix with a non-zero determinant');
  192. }
  193. if ($matrix->rows == 1) {
  194. return new Matrix([[1 / $matrix->getValue(1, 1)]]);
  195. }
  196. return self::getAdjoint($matrix)
  197. ->multiply(1 / $determinant);
  198. }
  199. /**
  200. * Calculate the minors of the matrix
  201. *
  202. * @param Matrix $matrix The matrix whose minors we wish to calculate
  203. * @return array[]
  204. **/
  205. protected static function getMinors(Matrix $matrix)
  206. {
  207. $minors = $matrix->toArray();
  208. $dimensions = $matrix->rows;
  209. if ($dimensions == 1) {
  210. return $minors;
  211. }
  212. for ($i = 0; $i < $dimensions; ++$i) {
  213. for ($j = 0; $j < $dimensions; ++$j) {
  214. $minors[$i][$j] = self::getDeterminantSegment($matrix, $i, $j);
  215. }
  216. }
  217. return $minors;
  218. }
  219. /**
  220. * Return the minors of the matrix
  221. * The minor of a matrix A is the determinant of some smaller square matrix, cut down from A by removing one or
  222. * more of its rows or columns.
  223. * Minors obtained by removing just one row and one column from square matrices (first minors) are required for
  224. * calculating matrix cofactors, which in turn are useful for computing both the determinant and inverse of
  225. * square matrices.
  226. *
  227. * @param Matrix $matrix The matrix whose minors we wish to calculate
  228. * @return Matrix
  229. * @throws Exception
  230. **/
  231. public static function minors(Matrix $matrix)
  232. {
  233. if (!$matrix->isSquare()) {
  234. throw new Exception('Minors can only be calculated for a square matrix');
  235. }
  236. return new Matrix(self::getMinors($matrix));
  237. }
  238. /**
  239. * Return the trace of this matrix
  240. * The trace is defined as the sum of the elements on the main diagonal (the diagonal from the upper left to the lower right)
  241. * of the matrix
  242. *
  243. * @param Matrix $matrix The matrix whose trace we wish to calculate
  244. * @return float
  245. * @throws Exception
  246. **/
  247. public static function trace(Matrix $matrix)
  248. {
  249. if (!$matrix->isSquare()) {
  250. throw new Exception('Trace can only be extracted from a square matrix');
  251. }
  252. $dimensions = $matrix->rows;
  253. $result = 0;
  254. for ($i = 1; $i <= $dimensions; ++$i) {
  255. $result += $matrix->getValue($i, $i);
  256. }
  257. return $result;
  258. }
  259. /**
  260. * Return the transpose of this matrix
  261. *
  262. * @param Matrix $matrix The matrix whose transpose we wish to calculate
  263. * @return Matrix
  264. * @throws Exception
  265. **/
  266. public static function transpose(Matrix $matrix)
  267. {
  268. $grid = call_user_func_array(
  269. 'array_map',
  270. array_merge(
  271. [null],
  272. $matrix->toArray()
  273. )
  274. );
  275. return new Matrix($grid);
  276. }
  277. }