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

Static Public Member Functions

static adjoint (Matrix $matrix)
 Return the adjoint of this matrix The adjugate, classical adjoint, or adjunct of a square matrix is the transpose of its cofactor matrix. More...
 
static cofactors (Matrix $matrix)
 Return the cofactors of this matrix. More...
 
static determinant (Matrix $matrix)
 Return the determinant of this matrix. More...
 
static diagonal (Matrix $matrix)
 Return the diagonal of this matrix. More...
 
static antidiagonal (Matrix $matrix)
 Return the antidiagonal of this matrix. More...
 
static identity (Matrix $matrix)
 Return the identity matrix The identity matrix, or sometimes ambiguously called a unit matrix, of size n is the n × n square matrix with ones on the main diagonal and zeros elsewhere. More...
 
static inverse (Matrix $matrix, string $type='inverse')
 Return the inverse of this matrix. More...
 
static minors (Matrix $matrix)
 Return the minors of the matrix The minor of a matrix A is the determinant of some smaller square matrix, cut down from A by removing one or more of its rows or columns. More...
 
static trace (Matrix $matrix)
 Return the trace of this matrix The trace is defined as the sum of the elements on the main diagonal (the diagonal from the upper left to the lower right) of the matrix. More...
 
static transpose (Matrix $matrix)
 Return the transpose of this matrix. More...
 

Static Protected Member Functions

static getMinors (Matrix $matrix)
 Calculate the minors of the matrix. More...
 

Static Private Member Functions

static getAdjoint (Matrix $matrix)
 Calculate the adjoint of the matrix. More...
 
static getCofactors (Matrix $matrix)
 Calculate the cofactors of the matrix. More...
 
static getDeterminantSegment (Matrix $matrix, $row, $column)
 
static getDeterminant (Matrix $matrix)
 Calculate the determinant of the matrix. More...
 

Detailed Description

Definition at line 5 of file Functions.php.

Member Function Documentation

◆ adjoint()

static Matrix\Functions::adjoint ( Matrix  $matrix)
static

Return the adjoint of this matrix The adjugate, classical adjoint, or adjunct of a square matrix is the transpose of its cofactor matrix.

The adjugate has sometimes been called the "adjoint", but today the "adjoint" of a matrix normally refers to its corresponding adjoint operator, which is its conjugate transpose.

Parameters
Matrix$matrixThe matrix whose adjoint we wish to calculate
Returns
Matrix
Exceptions
Exception

Definition at line 32 of file Functions.php.

References Matrix\Matrix\isSquare().

33  {
34  if (!$matrix->isSquare()) {
35  throw new Exception('Adjoint can only be calculated for a square matrix');
36  }
37 
38  return self::getAdjoint($matrix);
39  }
$matrix
Definition: test.php:18
+ Here is the call graph for this function:

◆ antidiagonal()

static Matrix\Functions::antidiagonal ( Matrix  $matrix)
static

Return the antidiagonal of this matrix.

Parameters
Matrix$matrixThe matrix whose antidiagonal we wish to calculate
Returns
Matrix
Exceptions
Exception

Definition at line 188 of file Functions.php.

References $grid, $i, Matrix\Matrix\getValue(), Matrix\Matrix\isSquare(), and Matrix\Matrix\rows().

189  {
190  if (!$matrix->isSquare()) {
191  throw new Exception('Anti-Diagonal can only be extracted from a square matrix');
192  }
193 
194  $dimensions = $matrix->rows;
195  $grid = Builder::createFilledMatrix(0, $dimensions, $dimensions)
196  ->toArray();
197 
198  for ($i = 0; $i < $dimensions; ++$i) {
199  $grid[$i][$dimensions - $i - 1] = $matrix->getValue($i + 1, $dimensions - $i);
200  }
201 
202  return new Matrix($grid);
203  }
$grid
Definition: test.php:8
static createFilledMatrix($fillValue, $rows, $columns=null)
Create a new matrix of specified dimensions, and filled with a specified value If the column argument...
Definition: Builder.php:30
$matrix
Definition: test.php:18
$i
Definition: disco.tpl.php:19
Class for the creating "special" Matrices.
Definition: Builder.php:11
+ Here is the call graph for this function:

◆ cofactors()

static Matrix\Functions::cofactors ( Matrix  $matrix)
static

Return the cofactors of this matrix.

Parameters
Matrix$matrixThe matrix whose cofactors we wish to calculate
Returns
Matrix
Exceptions
Exception

Definition at line 75 of file Functions.php.

References Matrix\Matrix\isSquare().

76  {
77  if (!$matrix->isSquare()) {
78  throw new Exception('Cofactors can only be calculated for a square matrix');
79  }
80 
81  return self::getCofactors($matrix);
82  }
$matrix
Definition: test.php:18
+ Here is the call graph for this function:

◆ determinant()

static Matrix\Functions::determinant ( Matrix  $matrix)
static

Return the determinant of this matrix.

Parameters
Matrix$matrixThe matrix whose determinant we wish to calculate
Returns
float
Exceptions
Exception

Definition at line 148 of file Functions.php.

References Matrix\Matrix\isSquare().

149  {
150  if (!$matrix->isSquare()) {
151  throw new Exception('Determinant can only be calculated for a square matrix');
152  }
153 
154  return self::getDeterminant($matrix);
155  }
$matrix
Definition: test.php:18
+ Here is the call graph for this function:

◆ diagonal()

static Matrix\Functions::diagonal ( Matrix  $matrix)
static

Return the diagonal of this matrix.

Parameters
Matrix$matrixThe matrix whose diagonal we wish to calculate
Returns
Matrix
Exceptions
Exception

Definition at line 164 of file Functions.php.

References $grid, $i, Matrix\Matrix\getValue(), Matrix\Matrix\isSquare(), and Matrix\Matrix\rows().

165  {
166  if (!$matrix->isSquare()) {
167  throw new Exception('Diagonal can only be extracted from a square matrix');
168  }
169 
170  $dimensions = $matrix->rows;
171  $grid = Builder::createFilledMatrix(0, $dimensions, $dimensions)
172  ->toArray();
173 
174  for ($i = 0; $i < $dimensions; ++$i) {
175  $grid[$i][$i] = $matrix->getValue($i + 1, $i + 1);
176  }
177 
178  return new Matrix($grid);
179  }
$grid
Definition: test.php:8
static createFilledMatrix($fillValue, $rows, $columns=null)
Create a new matrix of specified dimensions, and filled with a specified value If the column argument...
Definition: Builder.php:30
$matrix
Definition: test.php:18
$i
Definition: disco.tpl.php:19
Class for the creating "special" Matrices.
Definition: Builder.php:11
+ Here is the call graph for this function:

◆ getAdjoint()

static Matrix\Functions::getAdjoint ( Matrix  $matrix)
staticprivate

Calculate the adjoint of the matrix.

Parameters
Matrix$matrixThe matrix whose adjoint we wish to calculate
Returns
Matrix
Exceptions
Exception

Definition at line 15 of file Functions.php.

16  {
17  return self::transpose(
18  self::getCofactors($matrix)
19  );
20  }
$matrix
Definition: test.php:18

◆ getCofactors()

static Matrix\Functions::getCofactors ( Matrix  $matrix)
staticprivate

Calculate the cofactors of the matrix.

Parameters
Matrix$matrixThe matrix whose cofactors we wish to calculate
Returns
Matrix
Exceptions
Exception

Definition at line 49 of file Functions.php.

References $i, and Matrix\Matrix\rows().

50  {
51  $cofactors = self::getMinors($matrix);
52  $dimensions = $matrix->rows;
53 
54  $cof = 1;
55  for ($i = 0; $i < $dimensions; ++$i) {
56  $cofs = $cof;
57  for ($j = 0; $j < $dimensions; ++$j) {
58  $cofactors[$i][$j] *= $cofs;
59  $cofs = -$cofs;
60  }
61  $cof = -$cof;
62  }
63 
64  return new Matrix($cofactors);
65  }
$matrix
Definition: test.php:18
$i
Definition: disco.tpl.php:19
Class for the creating "special" Matrices.
Definition: Builder.php:11
+ Here is the call graph for this function:

◆ getDeterminant()

static Matrix\Functions::getDeterminant ( Matrix  $matrix)
staticprivate

Calculate the determinant of the matrix.

Parameters
Matrix$matrixThe matrix whose determinant we wish to calculate
Returns
float
Exceptions
Exception

Definition at line 113 of file Functions.php.

References $i, Matrix\Matrix\getValue(), and Matrix\Matrix\rows().

114  {
115  $dimensions = $matrix->rows;
116  $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;
136  }
137 
138  return $determinant;
139  }
$matrix
Definition: test.php:18
$i
Definition: disco.tpl.php:19
+ Here is the call graph for this function:

◆ getDeterminantSegment()

static Matrix\Functions::getDeterminantSegment ( Matrix  $matrix,
  $row,
  $column 
)
staticprivate
Parameters
Matrix$matrix
int$row
int$column
Returns
float
Exceptions
Exception

Definition at line 91 of file Functions.php.

References $row, and Matrix\Matrix\toArray().

92  {
93  $tmpMatrix = $matrix->toArray();
94  unset($tmpMatrix[$row]);
95  array_walk(
96  $tmpMatrix,
97  function (&$row) use ($column) {
98  unset($row[$column]);
99  }
100  );
101 
102  return self::getDeterminant(new Matrix($tmpMatrix));
103  }
$matrix
Definition: test.php:18
$row
Class for the creating "special" Matrices.
Definition: Builder.php:11
+ Here is the call graph for this function:

◆ getMinors()

static Matrix\Functions::getMinors ( Matrix  $matrix)
staticprotected

Calculate the minors of the matrix.

Parameters
Matrix$matrixThe matrix whose minors we wish to calculate
Returns
array[]
Exceptions
Exception

Definition at line 259 of file Functions.php.

References $i, Matrix\Matrix\rows(), and Matrix\Matrix\toArray().

260  {
261  $minors = $matrix->toArray();
262  $dimensions = $matrix->rows;
263  if ($dimensions == 1) {
264  return $minors;
265  }
266 
267  for ($i = 0; $i < $dimensions; ++$i) {
268  for ($j = 0; $j < $dimensions; ++$j) {
269  $minors[$i][$j] = self::getDeterminantSegment($matrix, $i, $j);
270  }
271  }
272 
273  return $minors;
274  }
$matrix
Definition: test.php:18
$i
Definition: disco.tpl.php:19
+ Here is the call graph for this function:

◆ identity()

static Matrix\Functions::identity ( Matrix  $matrix)
static

Return the identity matrix The identity matrix, or sometimes ambiguously called a unit matrix, of size n is the n × n square matrix with ones on the main diagonal and zeros elsewhere.

Parameters
Matrix$matrixThe matrix whose identity we wish to calculate
Returns
Matrix
Exceptions
Exception

Definition at line 214 of file Functions.php.

References Matrix\Matrix\isSquare(), and Matrix\Matrix\rows().

215  {
216  if (!$matrix->isSquare()) {
217  throw new Exception('Identity can only be created for a square matrix');
218  }
219 
220  $dimensions = $matrix->rows;
221 
222  return Builder::createIdentityMatrix($dimensions);
223  }
static createIdentityMatrix($dimensions, $fillValue=null)
Create a new identity matrix of specified dimensions This will always be a square matrix...
Definition: Builder.php:60
$matrix
Definition: test.php:18
+ Here is the call graph for this function:

◆ inverse()

static Matrix\Functions::inverse ( Matrix  $matrix,
string  $type = 'inverse' 
)
static

Return the inverse of this matrix.

Parameters
Matrix$matrixThe matrix whose inverse we wish to calculate
Returns
Matrix
Exceptions
Exception

Definition at line 232 of file Functions.php.

References $type, Matrix\Matrix\getValue(), Matrix\Matrix\isSquare(), and Matrix\Matrix\rows().

Referenced by Matrix\Operators\Division\execute().

233  {
234  if (!$matrix->isSquare()) {
235  throw new Exception(ucfirst($type) . ' can only be calculated for a square matrix');
236  }
237 
238  $determinant = self::getDeterminant($matrix);
239  if ($determinant == 0.0) {
240  throw new Div0Exception(ucfirst($type) . ' can only be calculated for a matrix with a non-zero determinant');
241  }
242 
243  if ($matrix->rows == 1) {
244  return new Matrix([[1 / $matrix->getValue(1, 1)]]);
245  }
246 
247  return self::getAdjoint($matrix)
248  ->multiply(1 / $determinant);
249  }
$type
$matrix
Definition: test.php:18
Class for the creating "special" Matrices.
Definition: Builder.php:11
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ minors()

static Matrix\Functions::minors ( Matrix  $matrix)
static

Return the minors of the matrix The minor of a matrix A is the determinant of some smaller square matrix, cut down from A by removing one or more of its rows or columns.

Minors obtained by removing just one row and one column from square matrices (first minors) are required for calculating matrix cofactors, which in turn are useful for computing both the determinant and inverse of square matrices.

Parameters
Matrix$matrixThe matrix whose minors we wish to calculate
Returns
Matrix
Exceptions
Exception

Definition at line 288 of file Functions.php.

References Matrix\Matrix\isSquare().

289  {
290  if (!$matrix->isSquare()) {
291  throw new Exception('Minors can only be calculated for a square matrix');
292  }
293 
294  return new Matrix(self::getMinors($matrix));
295  }
$matrix
Definition: test.php:18
Class for the creating "special" Matrices.
Definition: Builder.php:11
+ Here is the call graph for this function:

◆ trace()

static Matrix\Functions::trace ( Matrix  $matrix)
static

Return the trace of this matrix The trace is defined as the sum of the elements on the main diagonal (the diagonal from the upper left to the lower right) of the matrix.

Parameters
Matrix$matrixThe matrix whose trace we wish to calculate
Returns
float
Exceptions
Exception

Definition at line 306 of file Functions.php.

References $i, $result, Matrix\Matrix\getValue(), Matrix\Matrix\isSquare(), and Matrix\Matrix\rows().

307  {
308  if (!$matrix->isSquare()) {
309  throw new Exception('Trace can only be extracted from a square matrix');
310  }
311 
312  $dimensions = $matrix->rows;
313  $result = 0;
314  for ($i = 1; $i <= $dimensions; ++$i) {
315  $result += $matrix->getValue($i, $i);
316  }
317 
318  return $result;
319  }
$result
$matrix
Definition: test.php:18
$i
Definition: disco.tpl.php:19
+ Here is the call graph for this function:

◆ transpose()

static Matrix\Functions::transpose ( Matrix  $matrix)
static

Return the transpose of this matrix.

Parameters
Matrix$matrixThe matrix whose transpose we wish to calculate
Returns
Matrix

Definition at line 327 of file Functions.php.

References $grid, and Matrix\Matrix\toArray().

328  {
329  $array = array_values(array_merge([null], $matrix->toArray()));
330  $grid = call_user_func_array(
331  'array_map',
332  $array
333  );
334 
335  return new Matrix($grid);
336  }
$grid
Definition: test.php:8
$matrix
Definition: test.php:18
Class for the creating "special" Matrices.
Definition: Builder.php:11
+ Here is the call graph for this function:

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