| @@ -6,15 +6,10 @@ | ||
| 6 | 6 | * |
| 7 | 7 | * @copyright Copyright (c) 2018 Mark Baker (https://github.com/MarkBaker/PHPMatrix) |
| 8 | 8 | * @license https://opensource.org/licenses/MIT MIT |
| 9 | 9 | */ |
| 10 | - | |
| 11 | 10 | namespace Matrix; |
| 12 | 11 | |
| 13 | -use Generator; | |
| 14 | -use Matrix\Decomposition\LU; | |
| 15 | -use Matrix\Decomposition\QR; | |
| 16 | - | |
| 17 | 12 | /** |
| 18 | 13 | * Matrix object. |
| 19 | 14 | * |
| 20 | 15 | * @package Matrix |
| @@ -36,9 +31,8 @@ | ||
| 36 | 31 | * @method Matrix subtract(...$matrices) |
| 37 | 32 | * @method Matrix multiply(...$matrices) |
| 38 | 33 | * @method Matrix divideby(...$matrices) |
| 39 | 34 | * @method Matrix divideinto(...$matrices) |
| 40 | - * @method Matrix directsum(...$matrices) | |
| 41 | 35 | */ |
| 42 | 36 | class Matrix |
| 43 | 37 | { |
| 44 | 38 | protected $rows; |
| @@ -49,9 +43,9 @@ | ||
| 49 | 43 | * Create a new Matrix object from an array of values |
| 50 | 44 | * |
| 51 | 45 | * @param array $grid |
| 52 | 46 | */ |
| 53 | - final public function __construct(array $grid) | |
| 47 | + public function __construct(array $grid) | |
| 54 | 48 | { |
| 55 | 49 | $this->buildFromArray(array_values($grid)); |
| 56 | 50 | } |
| 57 | 51 | |
| @@ -66,9 +60,10 @@ | ||
| 66 | 60 | $columns = array_reduce( |
| 67 | 61 | $grid, |
| 68 | 62 | function ($carry, $value) { |
| 69 | 63 | return max($carry, is_array($value) ? count($value) : 1); |
| 70 | - } | |
| 64 | + }, | |
| 65 | + 0 | |
| 71 | 66 | ); |
| 72 | 67 | $this->columns = $columns; |
| 73 | 68 | |
| 74 | 69 | array_walk( |
| @@ -86,9 +81,9 @@ | ||
| 86 | 81 | |
| 87 | 82 | /** |
| 88 | 83 | * Validate that a row number is a positive integer |
| 89 | 84 | * |
| 90 | - * @param int $row | |
| 85 | + * @param $row | |
| 91 | 86 | * @return int |
| 92 | 87 | * @throws Exception |
| 93 | 88 | */ |
| 94 | 89 | public static function validateRow($row) |
| @@ -96,15 +91,15 @@ | ||
| 96 | 91 | if ((!is_numeric($row)) || (intval($row) < 1)) { |
| 97 | 92 | throw new Exception('Invalid Row'); |
| 98 | 93 | } |
| 99 | 94 | |
| 100 | - return (int)$row; | |
| 95 | + return (int) $row; | |
| 101 | 96 | } |
| 102 | 97 | |
| 103 | 98 | /** |
| 104 | 99 | * Validate that a column number is a positive integer |
| 105 | 100 | * |
| 106 | - * @param int $column | |
| 101 | + * @param $column | |
| 107 | 102 | * @return int |
| 108 | 103 | * @throws Exception |
| 109 | 104 | */ |
| 110 | 105 | public static function validateColumn($column) |
| @@ -112,15 +107,15 @@ | ||
| 112 | 107 | if ((!is_numeric($column)) || (intval($column) < 1)) { |
| 113 | 108 | throw new Exception('Invalid Column'); |
| 114 | 109 | } |
| 115 | 110 | |
| 116 | - return (int)$column; | |
| 111 | + return (int) $column; | |
| 117 | 112 | } |
| 118 | 113 | |
| 119 | 114 | /** |
| 120 | 115 | * Validate that a row number falls within the set of rows for this matrix |
| 121 | 116 | * |
| 122 | - * @param int $row | |
| 117 | + * @param $row | |
| 123 | 118 | * @return int |
| 124 | 119 | * @throws Exception |
| 125 | 120 | */ |
| 126 | 121 | protected function validateRowInRange($row) |
| @@ -135,9 +130,9 @@ | ||
| 135 | 130 | |
| 136 | 131 | /** |
| 137 | 132 | * Validate that a column number falls within the set of columns for this matrix |
| 138 | 133 | * |
| 139 | - * @param int $column | |
| 134 | + * @param $column | |
| 140 | 135 | * @return int |
| 141 | 136 | * @throws Exception |
| 142 | 137 | */ |
| 143 | 138 | protected function validateColumnInRange($column) |
| @@ -156,9 +151,9 @@ | ||
| 156 | 151 | * A negative $rowCount value will return rows until that many rows from the end of the matrix |
| 157 | 152 | * |
| 158 | 153 | * Note that row numbers start from 1, not from 0 |
| 159 | 154 | * |
| 160 | - * @param int $row | |
| 155 | + * @param $row | |
| 161 | 156 | * @param int $rowCount |
| 162 | 157 | * @return static |
| 163 | 158 | * @throws Exception |
| 164 | 159 | */ |
| @@ -164,13 +159,13 @@ | ||
| 164 | 159 | */ |
| 165 | 160 | public function getRows($row, $rowCount = 1) |
| 166 | 161 | { |
| 167 | 162 | $row = $this->validateRowInRange($row); |
| 168 | - if ($rowCount === 0) { | |
| 163 | + if ($rowCount == 0) { | |
| 169 | 164 | $rowCount = $this->rows - $row + 1; |
| 170 | 165 | } |
| 171 | 166 | |
| 172 | - return new static(array_slice($this->grid, $row - 1, (int)$rowCount)); | |
| 167 | + return new static(array_slice($this->grid, $row - 1, $rowCount)); | |
| 173 | 168 | } |
| 174 | 169 | |
| 175 | 170 | /** |
| 176 | 171 | * Return a new matrix as a subset of columns from this matrix, starting at column number $column, and $columnCount columns |
| @@ -178,11 +173,11 @@ | ||
| 178 | 173 | * A negative $columnCount value will return columns until that many columns from the end of the matrix |
| 179 | 174 | * |
| 180 | 175 | * Note that column numbers start from 1, not from 0 |
| 181 | 176 | * |
| 182 | - * @param int $column | |
| 177 | + * @param $column | |
| 183 | 178 | * @param int $columnCount |
| 184 | - * @return Matrix | |
| 179 | + * @return static | |
| 185 | 180 | * @throws Exception |
| 186 | 181 | */ |
| 187 | 182 | public function getColumns($column, $columnCount = 1) |
| 188 | 183 | { |
| @@ -206,9 +201,9 @@ | ||
| 206 | 201 | * A $rowCount value of 0 will remove all rows of the matrix from $row |
| 207 | 202 | * |
| 208 | 203 | * Note that row numbers start from 1, not from 0 |
| 209 | 204 | * |
| 210 | - * @param int $row | |
| 205 | + * @param $row | |
| 211 | 206 | * @param int $rowCount |
| 212 | 207 | * @return static |
| 213 | 208 | * @throws Exception |
| 214 | 209 | */ |
| @@ -214,14 +209,14 @@ | ||
| 214 | 209 | */ |
| 215 | 210 | public function dropRows($row, $rowCount = 1) |
| 216 | 211 | { |
| 217 | 212 | $this->validateRowInRange($row); |
| 218 | - if ($rowCount === 0) { | |
| 213 | + if ($rowCount == 0) { | |
| 219 | 214 | $rowCount = $this->rows - $row + 1; |
| 220 | 215 | } |
| 221 | 216 | |
| 222 | 217 | $grid = $this->grid; |
| 223 | - array_splice($grid, $row - 1, (int)$rowCount); | |
| 218 | + array_splice($grid, $row - 1, $rowCount); | |
| 224 | 219 | |
| 225 | 220 | return new static($grid); |
| 226 | 221 | } |
| 227 | 222 | |
| @@ -232,9 +227,9 @@ | ||
| 232 | 227 | * A $columnCount value of 0 will remove all columns of the matrix from $column |
| 233 | 228 | * |
| 234 | 229 | * Note that column numbers start from 1, not from 0 |
| 235 | 230 | * |
| 236 | - * @param int $column | |
| 231 | + * @param $column | |
| 237 | 232 | * @param int $columnCount |
| 238 | 233 | * @return static |
| 239 | 234 | * @throws Exception |
| 240 | 235 | */ |
| @@ -243,14 +238,14 @@ | ||
| 243 | 238 | $this->validateColumnInRange($column); |
| 244 | 239 | if ($columnCount < 1) { |
| 245 | 240 | $columnCount = $this->columns + $columnCount - $column + 1; |
| 246 | 241 | } |
| 247 | - | |
| 242 | + | |
| 248 | 243 | $grid = $this->grid; |
| 249 | 244 | array_walk( |
| 250 | 245 | $grid, |
| 251 | 246 | function (&$row) use ($column, $columnCount) { |
| 252 | - array_splice($row, $column - 1, (int)$columnCount); | |
| 247 | + array_splice($row, $column - 1, $columnCount); | |
| 253 | 248 | } |
| 254 | 249 | ); |
| 255 | 250 | |
| 256 | 251 | return new static($grid); |
| @@ -259,11 +254,11 @@ | ||
| 259 | 254 | /** |
| 260 | 255 | * Return a value from this matrix, from the "cell" identified by the row and column numbers |
| 261 | 256 | * Note that row and column numbers start from 1, not from 0 |
| 262 | 257 | * |
| 263 | - * @param int $row | |
| 264 | - * @param int $column | |
| 265 | - * @return mixed | |
| 258 | + * @param $row | |
| 259 | + * @param $column | |
| 260 | + * @return static | |
| 266 | 261 | * @throws Exception |
| 267 | 262 | */ |
| 268 | 263 | public function getValue($row, $column) |
| 269 | 264 | { |
| @@ -274,11 +269,11 @@ | ||
| 274 | 269 | } |
| 275 | 270 | |
| 276 | 271 | /** |
| 277 | 272 | * Returns a Generator that will yield each row of the matrix in turn as a vector matrix |
| 278 | - * or the value of each cell if the matrix is a column vector | |
| 273 | + * or the value of each cell if the matrix is a vector | |
| 279 | 274 | * |
| 280 | - * @return Generator|Matrix[]|mixed[] | |
| 275 | + * @return \Generator|Matrix[]|mixed[] | |
| 281 | 276 | */ |
| 282 | 277 | public function rows() |
| 283 | 278 | { |
| 284 | 279 | foreach ($this->grid as $i => $row) { |
| @@ -289,11 +284,11 @@ | ||
| 289 | 284 | } |
| 290 | 285 | |
| 291 | 286 | /** |
| 292 | 287 | * Returns a Generator that will yield each column of the matrix in turn as a vector matrix |
| 293 | - * or the value of each cell if the matrix is a row vector | |
| 288 | + * or the value of each cell if the matrix is a vector | |
| 294 | 289 | * |
| 295 | - * @return Generator|Matrix[]|mixed[] | |
| 290 | + * @return \Generator|Matrix[]|mixed[] | |
| 296 | 291 | */ |
| 297 | 292 | public function columns() |
| 298 | 293 | { |
| 299 | 294 | for ($i = 0; $i < $this->columns; ++$i) { |
| @@ -334,26 +329,8 @@ | ||
| 334 | 329 | { |
| 335 | 330 | return $this->grid; |
| 336 | 331 | } |
| 337 | 332 | |
| 338 | - /** | |
| 339 | - * Solve A*X = B. | |
| 340 | - * | |
| 341 | - * @param Matrix $B Right hand side | |
| 342 | - * | |
| 343 | - * @throws Exception | |
| 344 | - * | |
| 345 | - * @return Matrix ... Solution if A is square, least squares solution otherwise | |
| 346 | - */ | |
| 347 | - public function solve(Matrix $B) | |
| 348 | - { | |
| 349 | - if ($this->columns === $this->rows) { | |
| 350 | - return (new LU($this))->solve($B); | |
| 351 | - } | |
| 352 | - | |
| 353 | - return (new QR($this))->solve($B); | |
| 354 | - } | |
| 355 | - | |
| 356 | 333 | protected static $getters = [ |
| 357 | 334 | 'rows', |
| 358 | 335 | 'columns', |
| 359 | 336 | ]; |
| @@ -360,11 +337,11 @@ | ||
| 360 | 337 | |
| 361 | 338 | /** |
| 362 | 339 | * Access specific properties as read-only (no setters) |
| 363 | 340 | * |
| 364 | - * @param string $propertyName | |
| 365 | - * @return mixed | |
| 366 | - * @throws Exception | |
| 341 | + * @param $propertyName | |
| 342 | + * @return mixed | |
| 343 | + * @throws Exception | |
| 367 | 344 | */ |
| 368 | 345 | public function __get($propertyName) |
| 369 | 346 | { |
| 370 | 347 | $propertyName = strtolower($propertyName); |
| @@ -401,23 +378,26 @@ | ||
| 401 | 378 | |
| 402 | 379 | /** |
| 403 | 380 | * Returns the result of the function call or operation |
| 404 | 381 | * |
| 405 | - * @param string $functionName | |
| 406 | - * @param mixed[] $arguments | |
| 407 | - * @return Matrix|float | |
| 408 | - * @throws Exception | |
| 382 | + * @param string $functionName | |
| 383 | + * @param mixed[] $arguments | |
| 384 | + * @return Matrix|float | |
| 385 | + * @throws Exception|\InvalidArgumentException | |
| 409 | 386 | */ |
| 410 | 387 | public function __call($functionName, $arguments) |
| 411 | 388 | { |
| 412 | 389 | $functionName = strtolower(str_replace('_', '', $functionName)); |
| 413 | 390 | |
| 414 | - if (in_array($functionName, self::$functions, true) || in_array($functionName, self::$operations, true)) { | |
| 391 | + // Test for function calls | |
| 392 | + if (in_array($functionName, self::$functions)) { | |
| 415 | 393 | $functionName = "\\" . __NAMESPACE__ . "\\{$functionName}"; |
| 416 | - if (is_callable($functionName)) { | |
| 417 | - $arguments = array_values(array_merge([$this], $arguments)); | |
| 418 | - return call_user_func_array($functionName, $arguments); | |
| 419 | - } | |
| 394 | + return $functionName($this, ...$arguments); | |
| 395 | + } | |
| 396 | + // Test for operation calls | |
| 397 | + if (in_array($functionName, self::$operations)) { | |
| 398 | + $functionName = "\\" . __NAMESPACE__ . "\\{$functionName}"; | |
| 399 | + return $functionName($this, ...$arguments); | |
| 420 | 400 | } |
| 421 | 401 | throw new Exception('Function or Operation does not exist'); |
| 422 | 402 | } |
| 423 | 403 | } |