55 $this->buildFromArray(array_values($grid));
65 $this->rows = count($grid);
68 function ($carry, $value) {
69 return max($carry, is_array($value) ? count($value) : 1);
77 if (!is_array($value)) {
80 $value = array_pad(array_values($value),
$columns, null);
96 if ((!is_numeric($row)) || (intval($row) < 1)) {
112 if ((!is_numeric($column)) || (intval($column) < 1)) {
128 $row = static::validateRow($row);
129 if ($row > $this->rows) {
130 throw new Exception(
'Requested Row exceeds matrix size');
145 $column = static::validateColumn($column);
146 if ($column > $this->columns) {
147 throw new Exception(
'Requested Column exceeds matrix size');
167 $row = $this->validateRowInRange($row);
168 if ($rowCount === 0) {
169 $rowCount = $this->rows - $row + 1;
172 return new static(array_slice($this->grid, $row - 1, (
int)$rowCount));
189 $column = $this->validateColumnInRange($column);
196 $grid[] = array_column($this->grid,
$i);
199 return (
new static(
$grid))->transpose();
217 $this->validateRowInRange($row);
218 if ($rowCount === 0) {
219 $rowCount = $this->rows - $row + 1;
223 array_splice(
$grid, $row - 1, (
int)$rowCount);
225 return new static(
$grid);
243 $this->validateColumnInRange($column);
256 return new static(
$grid);
270 $row = $this->validateRowInRange($row);
271 $column = $this->validateColumnInRange($column);
273 return $this->grid[$row - 1][$column - 1];
284 foreach ($this->grid as
$i =>
$row) {
285 yield
$i + 1 => ($this->columns == 1)
287 :
new static([
$row]);
300 yield
$i + 1 => ($this->rows == 1)
302 :
new static(array_column($this->grid, $i));
325 return $this->rows === 1 || $this->columns === 1;
349 if ($this->columns === $this->rows) {
350 return (
new LU($this))->solve($B);
353 return (
new QR($this))->solve($B);
356 protected static $getters = [
368 public function __get(
string $propertyName)
370 $propertyName = strtolower($propertyName);
373 if (in_array($propertyName, self::$getters)) {
374 return $this->$propertyName;
377 throw new Exception(
'Property does not exist');
380 protected static $functions = [
393 protected static $operations = [
410 public function __call(
string $functionName, $arguments)
412 $functionName = strtolower(str_replace(
'_',
'', $functionName));
414 if (in_array($functionName, self::$functions,
true) || in_array($functionName, self::$operations,
true)) {
415 $functionName =
"\\" . __NAMESPACE__ .
"\\{$functionName}";
416 if (is_callable($functionName)) {
417 $arguments = array_values(array_merge([$this], $arguments));
418 return call_user_func_array($functionName, $arguments);
421 throw new Exception(
'Function or Operation does not exist');
isVector()
Identify if this matrix is a vector i.e.
rows()
Returns a Generator that will yield each row of the matrix in turn as a vector matrix or the value of...
getValue(int $row, int $column)
Return a value from this matrix, from the "cell" identified by the row and column numbers Note that r...
__get(string $propertyName)
Access specific properties as read-only (no setters)
static validateRow(int $row)
Validate that a row number is a positive integer.
buildFromArray(array $grid)
dropRows(int $row, int $rowCount=1)
Return a new matrix as a subset of rows from this matrix, dropping rows starting at row number $row...
columns()
Returns a Generator that will yield each column of the matrix in turn as a vector matrix or the value...
toArray()
Return the matrix as a 2-dimensional array.
__call(string $functionName, $arguments)
Returns the result of the function call or operation.
solve(Matrix $B)
Solve A*X = B.
dropColumns(int $column, int $columnCount=1)
Return a new matrix as a subset of columns from this matrix, dropping columns starting at column numb...
validateColumnInRange(int $column)
Validate that a column number falls within the set of columns for this matrix.
validateRowInRange(int $row)
Validate that a row number falls within the set of rows for this matrix.
Class for the creating "special" Matrices.
static validateColumn(int $column)
Validate that a column number is a positive integer.
getColumns(int $column, int $columnCount=1)
Return a new matrix as a subset of columns from this matrix, starting at column number $column...
isSquare()
Identify if the row and column dimensions of this matrix are equal, i.e.
getRows(int $row, int $rowCount=1)
Return a new matrix as a subset of rows from this matrix, starting at row number $row, and $rowCount rows A $rowCount value of 0 will return all rows of the matrix from $row A negative $rowCount value will return rows until that many rows from the end of the matrix.