ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Matrix\Matrix Class Reference
+ Collaboration diagram for Matrix\Matrix:

Public Member Functions

 __construct (array $grid)
 
 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. More...
 
 getColumns (int $column, int $columnCount=1)
 Return a new matrix as a subset of columns from this matrix, starting at column number $column, and $columnCount columns A $columnCount value of 0 will return all columns of the matrix from $column A negative $columnCount value will return columns until that many columns from the end of the matrix. More...
 
 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, and $rowCount rows A negative $rowCount value will drop rows until that many rows from the end of the matrix A $rowCount value of 0 will remove all rows of the matrix from $row. More...
 
 dropColumns (int $column, int $columnCount=1)
 Return a new matrix as a subset of columns from this matrix, dropping columns starting at column number $column, and $columnCount columns A negative $columnCount value will drop columns until that many columns from the end of the matrix A $columnCount value of 0 will remove all columns of the matrix from $column. More...
 
 getValue (int $row, int $column)
 Return a value from this matrix, from the "cell" identified by the row and column numbers Note that row and column numbers start from 1, not from 0. More...
 
 rows ()
 Returns a Generator that will yield each row of the matrix in turn as a vector matrix or the value of each cell if the matrix is a column vector. More...
 
 columns ()
 Returns a Generator that will yield each column of the matrix in turn as a vector matrix or the value of each cell if the matrix is a row vector. More...
 
 isSquare ()
 Identify if the row and column dimensions of this matrix are equal, i.e. More...
 
 isVector ()
 Identify if this matrix is a vector i.e. More...
 
 toArray ()
 Return the matrix as a 2-dimensional array. More...
 
 solve (Matrix $B)
 Solve A*X = B. More...
 
 __get (string $propertyName)
 Access specific properties as read-only (no setters) More...
 
 __call (string $functionName, $arguments)
 Returns the result of the function call or operation. More...
 

Static Public Member Functions

static validateRow (int $row)
 Validate that a row number is a positive integer. More...
 
static validateColumn (int $column)
 Validate that a column number is a positive integer. More...
 

Protected Member Functions

 buildFromArray (array $grid)
 
 validateRowInRange (int $row)
 Validate that a row number falls within the set of rows for this matrix. More...
 
 validateColumnInRange (int $column)
 Validate that a column number falls within the set of columns for this matrix. More...
 

Protected Attributes

 $rows
 
 $columns
 
 $grid = []
 

Static Protected Attributes

static $getters
 
static $functions
 
static $operations
 

Detailed Description

Definition at line 42 of file Matrix.php.

Constructor & Destructor Documentation

◆ __construct()

Matrix\Matrix::__construct ( array  $grid)
final

Definition at line 53 of file Matrix.php.

54  {
55  $this->buildFromArray(array_values($grid));
56  }
buildFromArray(array $grid)
Definition: Matrix.php:63

Member Function Documentation

◆ __call()

Matrix\Matrix::__call ( string  $functionName,
  $arguments 
)

Returns the result of the function call or operation.

Parameters
string$functionName
mixed[]$arguments
Returns
Matrix|float
Exceptions
Exception

Definition at line 410 of file Matrix.php.

411  {
412  $functionName = strtolower(str_replace('_', '', $functionName));
413 
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);
419  }
420  }
421  throw new Exception('Function or Operation does not exist');
422  }

◆ __get()

Matrix\Matrix::__get ( string  $propertyName)

Access specific properties as read-only (no setters)

Parameters
string$propertyName
Returns
mixed
Exceptions
Exception

Definition at line 368 of file Matrix.php.

369  {
370  $propertyName = strtolower($propertyName);
371 
372  // Test for function calls
373  if (in_array($propertyName, self::$getters)) {
374  return $this->$propertyName;
375  }
376 
377  throw new Exception('Property does not exist');
378  }

◆ buildFromArray()

Matrix\Matrix::buildFromArray ( array  $grid)
protected

Definition at line 63 of file Matrix.php.

References $columns, and $grid.

63  : void
64  {
65  $this->rows = count($grid);
66  $columns = array_reduce(
67  $grid,
68  function ($carry, $value) {
69  return max($carry, is_array($value) ? count($value) : 1);
70  }
71  );
72  $this->columns = $columns;
73 
74  array_walk(
75  $grid,
76  function (&$value) use ($columns) {
77  if (!is_array($value)) {
78  $value = [$value];
79  }
80  $value = array_pad(array_values($value), $columns, null);
81  }
82  );
83 
84  $this->grid = $grid;
85  }
rows()
Returns a Generator that will yield each row of the matrix in turn as a vector matrix or the value of...
Definition: Matrix.php:282
columns()
Returns a Generator that will yield each column of the matrix in turn as a vector matrix or the value...
Definition: Matrix.php:297

◆ columns()

Matrix\Matrix::columns ( )

Returns a Generator that will yield each column of the matrix in turn as a vector matrix or the value of each cell if the matrix is a row vector.

Returns
Generator|Matrix[]|mixed[]

Definition at line 297 of file Matrix.php.

References $columns, and $i.

Referenced by Matrix\Decomposition\LU\__construct(), Matrix\Decomposition\QR\__construct(), Matrix\Operators\Operator\__construct(), Matrix\Operators\Multiplication\multiplyMatrix(), Matrix\Decomposition\LU\solve(), and Matrix\Operators\Operator\validateMatchingDimensions().

297  : Generator
298  {
299  for ($i = 0; $i < $this->columns; ++$i) {
300  yield $i + 1 => ($this->rows == 1)
301  ? $this->grid[0][$i]
302  : new static(array_column($this->grid, $i));
303  }
304  }
rows()
Returns a Generator that will yield each row of the matrix in turn as a vector matrix or the value of...
Definition: Matrix.php:282
$i
Definition: disco.tpl.php:19
+ Here is the caller graph for this function:

◆ dropColumns()

Matrix\Matrix::dropColumns ( int  $column,
int  $columnCount = 1 
)

Return a new matrix as a subset of columns from this matrix, dropping columns starting at column number $column, and $columnCount columns A negative $columnCount value will drop columns until that many columns from the end of the matrix A $columnCount value of 0 will remove all columns of the matrix from $column.

Note that column numbers start from 1, not from 0

Parameters
int$column
int$columnCount
Returns
static
Exceptions
Exception

Definition at line 241 of file Matrix.php.

References $columnCount, $grid, and $row.

241  : Matrix
242  {
243  $this->validateColumnInRange($column);
244  if ($columnCount < 1) {
245  $columnCount = $this->columns + $columnCount - $column + 1;
246  }
247 
248  $grid = $this->grid;
249  array_walk(
250  $grid,
251  function (&$row) use ($column, $columnCount) {
252  array_splice($row, $column - 1, (int)$columnCount);
253  }
254  );
255 
256  return new static($grid);
257  }
if(! $row) $columnCount
columns()
Returns a Generator that will yield each column of the matrix in turn as a vector matrix or the value...
Definition: Matrix.php:297
$row
validateColumnInRange(int $column)
Validate that a column number falls within the set of columns for this matrix.
Definition: Matrix.php:143
Class for the creating "special" Matrices.
Definition: Builder.php:11

◆ dropRows()

Matrix\Matrix::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, and $rowCount rows A negative $rowCount value will drop rows until that many rows from the end of the matrix A $rowCount value of 0 will remove all rows of the matrix from $row.

Note that row numbers start from 1, not from 0

Parameters
int$row
int$rowCount
Returns
static
Exceptions
Exception

Definition at line 215 of file Matrix.php.

References $grid.

215  : Matrix
216  {
217  $this->validateRowInRange($row);
218  if ($rowCount === 0) {
219  $rowCount = $this->rows - $row + 1;
220  }
221 
222  $grid = $this->grid;
223  array_splice($grid, $row - 1, (int)$rowCount);
224 
225  return new static($grid);
226  }
rows()
Returns a Generator that will yield each row of the matrix in turn as a vector matrix or the value of...
Definition: Matrix.php:282
$row
validateRowInRange(int $row)
Validate that a row number falls within the set of rows for this matrix.
Definition: Matrix.php:126
Class for the creating "special" Matrices.
Definition: Builder.php:11

◆ getColumns()

Matrix\Matrix::getColumns ( int  $column,
int  $columnCount = 1 
)

Return a new matrix as a subset of columns from this matrix, starting at column number $column, and $columnCount columns A $columnCount value of 0 will return all columns of the matrix from $column A negative $columnCount value will return columns until that many columns from the end of the matrix.

Note that column numbers start from 1, not from 0

Parameters
int$column
int$columnCount
Returns
Matrix
Exceptions
Exception

Definition at line 187 of file Matrix.php.

References $columnCount, $grid, and $i.

Referenced by Matrix\Operators\Multiplication\multiplyMatrix().

187  : Matrix
188  {
189  $column = $this->validateColumnInRange($column);
190  if ($columnCount < 1) {
191  $columnCount = $this->columns + $columnCount - $column + 1;
192  }
193 
194  $grid = [];
195  for ($i = $column - 1; $i < $column + $columnCount - 1; ++$i) {
196  $grid[] = array_column($this->grid, $i);
197  }
198 
199  return (new static($grid))->transpose();
200  }
if(! $row) $columnCount
columns()
Returns a Generator that will yield each column of the matrix in turn as a vector matrix or the value...
Definition: Matrix.php:297
validateColumnInRange(int $column)
Validate that a column number falls within the set of columns for this matrix.
Definition: Matrix.php:143
$i
Definition: disco.tpl.php:19
Class for the creating "special" Matrices.
Definition: Builder.php:11
+ Here is the caller graph for this function:

◆ getRows()

Matrix\Matrix::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.

Note that row numbers start from 1, not from 0

Parameters
int$row
int$rowCount
Returns
static
Exceptions
Exception

Definition at line 165 of file Matrix.php.

Referenced by Matrix\Decomposition\LU\pivotB().

165  : Matrix
166  {
167  $row = $this->validateRowInRange($row);
168  if ($rowCount === 0) {
169  $rowCount = $this->rows - $row + 1;
170  }
171 
172  return new static(array_slice($this->grid, $row - 1, (int)$rowCount));
173  }
rows()
Returns a Generator that will yield each row of the matrix in turn as a vector matrix or the value of...
Definition: Matrix.php:282
$row
validateRowInRange(int $row)
Validate that a row number falls within the set of rows for this matrix.
Definition: Matrix.php:126
Class for the creating "special" Matrices.
Definition: Builder.php:11
+ Here is the caller graph for this function:

◆ getValue()

Matrix\Matrix::getValue ( int  $row,
int  $column 
)

Return a value from this matrix, from the "cell" identified by the row and column numbers Note that row and column numbers start from 1, not from 0.

Parameters
int$row
int$column
Returns
mixed
Exceptions
Exception

Definition at line 268 of file Matrix.php.

Referenced by Matrix\Operators\Addition\addMatrix(), Matrix\Functions\antidiagonal(), Matrix\Functions\diagonal(), Matrix\Functions\getDeterminant(), Matrix\Functions\inverse(), Matrix\Operators\Subtraction\subtractMatrix(), and Matrix\Functions\trace().

269  {
270  $row = $this->validateRowInRange($row);
271  $column = $this->validateColumnInRange($column);
272 
273  return $this->grid[$row - 1][$column - 1];
274  }
$row
validateColumnInRange(int $column)
Validate that a column number falls within the set of columns for this matrix.
Definition: Matrix.php:143
validateRowInRange(int $row)
Validate that a row number falls within the set of rows for this matrix.
Definition: Matrix.php:126
+ Here is the caller graph for this function:

◆ isSquare()

Matrix\Matrix::isSquare ( )

Identify if the row and column dimensions of this matrix are equal, i.e.

if it is a "square" matrix

Returns
bool

Definition at line 312 of file Matrix.php.

References $columns.

Referenced by Matrix\Functions\adjoint(), Matrix\Functions\antidiagonal(), Matrix\Functions\cofactors(), Matrix\Functions\determinant(), Matrix\Functions\diagonal(), Matrix\Functions\identity(), Matrix\Functions\inverse(), Matrix\Functions\minors(), and Matrix\Functions\trace().

312  : bool
313  {
314  return $this->rows === $this->columns;
315  }
rows()
Returns a Generator that will yield each row of the matrix in turn as a vector matrix or the value of...
Definition: Matrix.php:282
+ Here is the caller graph for this function:

◆ isVector()

Matrix\Matrix::isVector ( )

Identify if this matrix is a vector i.e.

if it comprises only a single row or a single column

Returns
bool

Definition at line 323 of file Matrix.php.

323  : bool
324  {
325  return $this->rows === 1 || $this->columns === 1;
326  }
rows()
Returns a Generator that will yield each row of the matrix in turn as a vector matrix or the value of...
Definition: Matrix.php:282
columns()
Returns a Generator that will yield each column of the matrix in turn as a vector matrix or the value...
Definition: Matrix.php:297

◆ rows()

Matrix\Matrix::rows ( )

Returns a Generator that will yield each row of the matrix in turn as a vector matrix or the value of each cell if the matrix is a column vector.

Returns
Generator|Matrix[]|mixed[]

Definition at line 282 of file Matrix.php.

References $i, and $row.

Referenced by Matrix\Decomposition\LU\__construct(), Matrix\Decomposition\QR\__construct(), Matrix\Operators\Operator\__construct(), Matrix\Functions\antidiagonal(), Matrix\Functions\diagonal(), Matrix\Functions\getCofactors(), Matrix\Functions\getDeterminant(), Matrix\Functions\getMinors(), Matrix\Functions\identity(), Matrix\Functions\inverse(), Matrix\Decomposition\QR\solve(), Matrix\Decomposition\LU\solve(), Matrix\Functions\trace(), Matrix\Operators\Operator\validateMatchingDimensions(), and Matrix\Operators\Operator\validateReflectingDimensions().

282  : Generator
283  {
284  foreach ($this->grid as $i => $row) {
285  yield $i + 1 => ($this->columns == 1)
286  ? $row[0]
287  : new static([$row]);
288  }
289  }
columns()
Returns a Generator that will yield each column of the matrix in turn as a vector matrix or the value...
Definition: Matrix.php:297
$row
$i
Definition: disco.tpl.php:19
+ Here is the caller graph for this function:

◆ solve()

Matrix\Matrix::solve ( Matrix  $B)

Solve A*X = B.

Parameters
Matrix$BRight hand side
Exceptions
Exception
Returns
Matrix ... Solution if A is square, least squares solution otherwise

Definition at line 347 of file Matrix.php.

347  : Matrix
348  {
349  if ($this->columns === $this->rows) {
350  return (new LU($this))->solve($B);
351  }
352 
353  return (new QR($this))->solve($B);
354  }
rows()
Returns a Generator that will yield each row of the matrix in turn as a vector matrix or the value of...
Definition: Matrix.php:282
columns()
Returns a Generator that will yield each column of the matrix in turn as a vector matrix or the value...
Definition: Matrix.php:297
Class for the creating "special" Matrices.
Definition: Builder.php:11

◆ toArray()

Matrix\Matrix::toArray ( )

Return the matrix as a 2-dimensional array.

Returns
array

Definition at line 333 of file Matrix.php.

References $grid.

Referenced by Matrix\Decomposition\LU\__construct(), Matrix\Decomposition\QR\__construct(), Matrix\Operators\Operator\__construct(), Matrix\Functions\getDeterminantSegment(), Matrix\Functions\getMinors(), and Matrix\Functions\transpose().

333  : array
334  {
335  return $this->grid;
336  }
+ Here is the caller graph for this function:

◆ validateColumn()

static Matrix\Matrix::validateColumn ( int  $column)
static

Validate that a column number is a positive integer.

Parameters
int$column
Returns
int
Exceptions
Exception

Definition at line 110 of file Matrix.php.

110  : int
111  {
112  if ((!is_numeric($column)) || (intval($column) < 1)) {
113  throw new Exception('Invalid Column');
114  }
115 
116  return (int)$column;
117  }

◆ validateColumnInRange()

Matrix\Matrix::validateColumnInRange ( int  $column)
protected

Validate that a column number falls within the set of columns for this matrix.

Parameters
int$column
Returns
int
Exceptions
Exception

Definition at line 143 of file Matrix.php.

143  : int
144  {
145  $column = static::validateColumn($column);
146  if ($column > $this->columns) {
147  throw new Exception('Requested Column exceeds matrix size');
148  }
149 
150  return $column;
151  }
columns()
Returns a Generator that will yield each column of the matrix in turn as a vector matrix or the value...
Definition: Matrix.php:297

◆ validateRow()

static Matrix\Matrix::validateRow ( int  $row)
static

Validate that a row number is a positive integer.

Parameters
int$row
Returns
int
Exceptions
Exception

Definition at line 94 of file Matrix.php.

References $row.

94  : int
95  {
96  if ((!is_numeric($row)) || (intval($row) < 1)) {
97  throw new Exception('Invalid Row');
98  }
99 
100  return (int)$row;
101  }
$row

◆ validateRowInRange()

Matrix\Matrix::validateRowInRange ( int  $row)
protected

Validate that a row number falls within the set of rows for this matrix.

Parameters
int$row
Returns
int
Exceptions
Exception

Definition at line 126 of file Matrix.php.

References $row.

126  : int
127  {
128  $row = static::validateRow($row);
129  if ($row > $this->rows) {
130  throw new Exception('Requested Row exceeds matrix size');
131  }
132 
133  return $row;
134  }
rows()
Returns a Generator that will yield each row of the matrix in turn as a vector matrix or the value of...
Definition: Matrix.php:282
$row

Field Documentation

◆ $columns

Matrix\Matrix::$columns
protected

Definition at line 45 of file Matrix.php.

◆ $functions

Matrix\Matrix::$functions
staticprotected
Initial value:
= [
'adjoint',
'antidiagonal',
'cofactors',
'determinant',
'diagonal',
'identity',
'inverse',
'minors',
'trace',
'transpose',
]

Definition at line 380 of file Matrix.php.

◆ $getters

Matrix\Matrix::$getters
staticprotected
Initial value:
= [
'rows',
'columns',
]

Definition at line 356 of file Matrix.php.

◆ $grid

Matrix\Matrix::$grid = []
protected

Definition at line 46 of file Matrix.php.

◆ $operations

Matrix\Matrix::$operations
staticprotected
Initial value:
= [
'add',
'subtract',
'multiply',
'divideby',
'divideinto',
'directsum',
]

Definition at line 393 of file Matrix.php.

◆ $rows

Matrix\Matrix::$rows
protected

Definition at line 44 of file Matrix.php.


The documentation for this class was generated from the following file: