ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Functions.php
Go to the documentation of this file.
1<?php
2
3namespace Matrix;
4
6{
15 private static function getAdjoint(Matrix $matrix)
16 {
17 return self::transpose(
18 self::getCofactors($matrix)
19 );
20 }
21
32 public static function adjoint(Matrix $matrix)
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 }
40
49 private static function getCofactors(Matrix $matrix)
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 }
66
75 public static function cofactors(Matrix $matrix)
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 }
83
91 private static function getDeterminantSegment(Matrix $matrix, $row, $column)
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 }
104
113 private static function getDeterminant(Matrix $matrix)
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 }
140
148 public static function determinant(Matrix $matrix)
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 }
156
164 public static function diagonal(Matrix $matrix)
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 }
180
188 public static function antidiagonal(Matrix $matrix)
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 }
204
214 public static function identity(Matrix $matrix)
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 }
224
232 public static function inverse(Matrix $matrix, string $type = 'inverse')
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 }
250
259 protected static function getMinors(Matrix $matrix)
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 }
275
288 public static function minors(Matrix $matrix)
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 }
296
306 public static function trace(Matrix $matrix)
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 }
320
327 public static function transpose(Matrix $matrix)
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 }
337}
$result
An exception for terminatinating execution or to throw for unit testing.
static inverse(Matrix $matrix, string $type='inverse')
Return the inverse of this matrix.
Definition: Functions.php:232
static getMinors(Matrix $matrix)
Calculate the minors of the matrix.
Definition: Functions.php:259
static getDeterminantSegment(Matrix $matrix, $row, $column)
Definition: Functions.php:91
static adjoint(Matrix $matrix)
Return the adjoint of this matrix The adjugate, classical adjoint, or adjunct of a square matrix is t...
Definition: Functions.php:32
static diagonal(Matrix $matrix)
Return the diagonal of this matrix.
Definition: Functions.php:164
static cofactors(Matrix $matrix)
Return the cofactors of this matrix.
Definition: Functions.php:75
static transpose(Matrix $matrix)
Return the transpose of this matrix.
Definition: Functions.php:327
static minors(Matrix $matrix)
Return the minors of the matrix The minor of a matrix A is the determinant of some smaller square mat...
Definition: Functions.php:288
static getAdjoint(Matrix $matrix)
Calculate the adjoint of the matrix.
Definition: Functions.php:15
static antidiagonal(Matrix $matrix)
Return the antidiagonal of this matrix.
Definition: Functions.php:188
static identity(Matrix $matrix)
Return the identity matrix The identity matrix, or sometimes ambiguously called a unit matrix,...
Definition: Functions.php:214
static trace(Matrix $matrix)
Return the trace of this matrix The trace is defined as the sum of the elements on the main diagonal ...
Definition: Functions.php:306
static determinant(Matrix $matrix)
Return the determinant of this matrix.
Definition: Functions.php:148
static getCofactors(Matrix $matrix)
Calculate the cofactors of the matrix.
Definition: Functions.php:49
static getDeterminant(Matrix $matrix)
Calculate the determinant of the matrix.
Definition: Functions.php:113
$i
Definition: disco.tpl.php:19
$grid
Definition: test.php:8
$matrix
Definition: test.php:18
$row
Class for the creating "special" Matrices.
Definition: Builder.php:11
$type