← All changes
|
vendor/markbaker/matrix/classes/src/Functions.php
+42
-23
3.1.3
→
3.7.12
View file →
| @@ -8,9 +8,11 @@ | ||
| 8 | 8 | * Calculate the adjoint of the matrix |
| 9 | 9 | * |
| 10 | 10 | * @param Matrix $matrix The matrix whose adjoint we wish to calculate |
| 11 | 11 | * @return Matrix |
| 12 | - **/ | |
| 12 | + * | |
| 13 | + * @throws Exception | |
| 14 | + */ | |
| 13 | 15 | private static function getAdjoint(Matrix $matrix) |
| 14 | 16 | { |
| 15 | 17 | return self::transpose( |
| 16 | 18 | self::getCofactors($matrix) |
| @@ -40,9 +42,11 @@ | ||
| 40 | 42 | * Calculate the cofactors of the matrix |
| 41 | 43 | * |
| 42 | 44 | * @param Matrix $matrix The matrix whose cofactors we wish to calculate |
| 43 | 45 | * @return Matrix |
| 44 | - **/ | |
| 46 | + * | |
| 47 | + * @throws Exception | |
| 48 | + */ | |
| 45 | 49 | private static function getCofactors(Matrix $matrix) |
| 46 | 50 | { |
| 47 | 51 | $cofactors = self::getMinors($matrix); |
| 48 | 52 | $dimensions = $matrix->rows; |
| @@ -64,10 +68,11 @@ | ||
| 64 | 68 | * Return the cofactors of this matrix |
| 65 | 69 | * |
| 66 | 70 | * @param Matrix $matrix The matrix whose cofactors we wish to calculate |
| 67 | 71 | * @return Matrix |
| 72 | + * | |
| 68 | 73 | * @throws Exception |
| 69 | - **/ | |
| 74 | + */ | |
| 70 | 75 | public static function cofactors(Matrix $matrix) |
| 71 | 76 | { |
| 72 | 77 | if (!$matrix->isSquare()) { |
| 73 | 78 | throw new Exception('Cofactors can only be calculated for a square matrix'); |
| @@ -75,8 +80,15 @@ | ||
| 75 | 80 | |
| 76 | 81 | return self::getCofactors($matrix); |
| 77 | 82 | } |
| 78 | 83 | |
| 84 | + /** | |
| 85 | + * @param Matrix $matrix | |
| 86 | + * @param int $row | |
| 87 | + * @param int $column | |
| 88 | + * @return float | |
| 89 | + * @throws Exception | |
| 90 | + */ | |
| 79 | 91 | private static function getDeterminantSegment(Matrix $matrix, $row, $column) |
| 80 | 92 | { |
| 81 | 93 | $tmpMatrix = $matrix->toArray(); |
| 82 | 94 | unset($tmpMatrix[$row]); |
| @@ -94,26 +106,34 @@ | ||
| 94 | 106 | * Calculate the determinant of the matrix |
| 95 | 107 | * |
| 96 | 108 | * @param Matrix $matrix The matrix whose determinant we wish to calculate |
| 97 | 109 | * @return float |
| 98 | - **/ | |
| 110 | + * | |
| 111 | + * @throws Exception | |
| 112 | + */ | |
| 99 | 113 | private static function getDeterminant(Matrix $matrix) |
| 100 | 114 | { |
| 101 | 115 | $dimensions = $matrix->rows; |
| 102 | - if ($dimensions == 1) { | |
| 103 | - return $matrix->getValue(1, 1); | |
| 104 | - } elseif ($dimensions == 2) { | |
| 105 | - return $matrix->getValue(1, 1) * $matrix->getValue(2, 2) - $matrix->getValue(1, 2) * $matrix->getValue(2, 1); | |
| 106 | - } | |
| 107 | - | |
| 108 | 116 | $determinant = 0; |
| 109 | - for ($i = 1; $i <= $dimensions; ++$i) { | |
| 110 | - $det = $matrix->getValue(1, $i) * self::getDeterminantSegment($matrix, 0, $i-1); | |
| 111 | - if (($i % 2) == 0) { | |
| 112 | - $determinant -= $det; | |
| 113 | - } else { | |
| 114 | - $determinant += $det; | |
| 115 | - } | |
| 117 | + | |
| 118 | + switch ($dimensions) { | |
| 119 | + case 1: | |
| 120 | + $determinant = $matrix->getValue(1, 1); | |
| 121 | + break; | |
| 122 | + case 2: | |
| 123 | + $determinant = $matrix->getValue(1, 1) * $matrix->getValue(2, 2) - | |
| 124 | + $matrix->getValue(1, 2) * $matrix->getValue(2, 1); | |
| 125 | + break; | |
| 126 | + default: | |
| 127 | + for ($i = 1; $i <= $dimensions; ++$i) { | |
| 128 | + $det = $matrix->getValue(1, $i) * self::getDeterminantSegment($matrix, 0, $i - 1); | |
| 129 | + if (($i % 2) == 0) { | |
| 130 | + $determinant -= $det; | |
| 131 | + } else { | |
| 132 | + $determinant += $det; | |
| 133 | + } | |
| 134 | + } | |
| 135 | + break; | |
| 116 | 136 | } |
| 117 | 137 | |
| 118 | 138 | return $determinant; |
| 119 | 139 | } |
| @@ -232,9 +252,11 @@ | ||
| 232 | 252 | * Calculate the minors of the matrix |
| 233 | 253 | * |
| 234 | 254 | * @param Matrix $matrix The matrix whose minors we wish to calculate |
| 235 | 255 | * @return array[] |
| 236 | - **/ | |
| 256 | + * | |
| 257 | + * @throws Exception | |
| 258 | + */ | |
| 237 | 259 | protected static function getMinors(Matrix $matrix) |
| 238 | 260 | { |
| 239 | 261 | $minors = $matrix->toArray(); |
| 240 | 262 | $dimensions = $matrix->rows; |
| @@ -300,18 +322,15 @@ | ||
| 300 | 322 | * Return the transpose of this matrix |
| 301 | 323 | * |
| 302 | 324 | * @param Matrix $matrix The matrix whose transpose we wish to calculate |
| 303 | 325 | * @return Matrix |
| 304 | - * @throws Exception | |
| 305 | 326 | **/ |
| 306 | 327 | public static function transpose(Matrix $matrix) |
| 307 | 328 | { |
| 329 | + $array = array_values(array_merge([null], $matrix->toArray())); | |
| 308 | 330 | $grid = call_user_func_array( |
| 309 | 331 | 'array_map', |
| 310 | - array_merge( | |
| 311 | - [null], | |
| 312 | - $matrix->toArray() | |
| 313 | - ) | |
| 332 | + $array | |
| 314 | 333 | ); |
| 315 | 334 | |
| 316 | 335 | return new Matrix($grid); |
| 317 | 336 | } |