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