Functions.php
10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
namespace Matrix;
class Functions
{
/**
* Validates an array of matrix, converting an array to a matrix if required.
*
* @param Matrix|array $matrix Matrix or an array to treat as a matrix.
* @return Matrix The new matrix
* @throws Exception If argument isn't a valid matrix or array.
*/
private static function validateMatrix($matrix)
{
if (is_array($matrix)) {
$matrix = new Matrix($matrix);
}
if (!$matrix instanceof Matrix) {
throw new Exception('Must be Matrix or array');
}
return $matrix;
}
/**
* Calculate the adjoint of the matrix
*
* @param Matrix $matrix The matrix whose adjoint we wish to calculate
* @return Matrix
*
* @throws Exception
*/
private static function getAdjoint(Matrix $matrix)
{
return self::transpose(
self::getCofactors($matrix)
);
}
/**
* 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.
*
* @param Matrix|array $matrix The matrix whose adjoint we wish to calculate
* @return Matrix
* @throws Exception
**/
public static function adjoint($matrix)
{
$matrix = self::validateMatrix($matrix);
if (!$matrix->isSquare()) {
throw new Exception('Adjoint can only be calculated for a square matrix');
}
return self::getAdjoint($matrix);
}
/**
* Calculate the cofactors of the matrix
*
* @param Matrix $matrix The matrix whose cofactors we wish to calculate
* @return Matrix
*
* @throws Exception
*/
private static function getCofactors(Matrix $matrix)
{
$cofactors = self::getMinors($matrix);
$dimensions = $matrix->rows;
$cof = 1;
for ($i = 0; $i < $dimensions; ++$i) {
$cofs = $cof;
for ($j = 0; $j < $dimensions; ++$j) {
$cofactors[$i][$j] *= $cofs;
$cofs = -$cofs;
}
$cof = -$cof;
}
return new Matrix($cofactors);
}
/**
* Return the cofactors of this matrix
*
* @param Matrix|array $matrix The matrix whose cofactors we wish to calculate
* @return Matrix
*
* @throws Exception
*/
public static function cofactors($matrix)
{
$matrix = self::validateMatrix($matrix);
if (!$matrix->isSquare()) {
throw new Exception('Cofactors can only be calculated for a square matrix');
}
return self::getCofactors($matrix);
}
/**
* @param Matrix $matrix
* @param int $row
* @param int $column
* @return float
* @throws Exception
*/
private static function getDeterminantSegment(Matrix $matrix, $row, $column)
{
$tmpMatrix = $matrix->toArray();
unset($tmpMatrix[$row]);
array_walk(
$tmpMatrix,
function (&$row) use ($column) {
unset($row[$column]);
}
);
return self::getDeterminant(new Matrix($tmpMatrix));
}
/**
* Calculate the determinant of the matrix
*
* @param Matrix $matrix The matrix whose determinant we wish to calculate
* @return float
*
* @throws Exception
*/
private static function getDeterminant(Matrix $matrix)
{
$dimensions = $matrix->rows;
$determinant = 0;
switch ($dimensions) {
case 1:
$determinant = $matrix->getValue(1, 1);
break;
case 2:
$determinant = $matrix->getValue(1, 1) * $matrix->getValue(2, 2) -
$matrix->getValue(1, 2) * $matrix->getValue(2, 1);
break;
default:
for ($i = 1; $i <= $dimensions; ++$i) {
$det = $matrix->getValue(1, $i) * self::getDeterminantSegment($matrix, 0, $i - 1);
if (($i % 2) == 0) {
$determinant -= $det;
} else {
$determinant += $det;
}
}
break;
}
return $determinant;
}
/**
* Return the determinant of this matrix
*
* @param Matrix|array $matrix The matrix whose determinant we wish to calculate
* @return float
* @throws Exception
**/
public static function determinant($matrix)
{
$matrix = self::validateMatrix($matrix);
if (!$matrix->isSquare()) {
throw new Exception('Determinant can only be calculated for a square matrix');
}
return self::getDeterminant($matrix);
}
/**
* Return the diagonal of this matrix
*
* @param Matrix|array $matrix The matrix whose diagonal we wish to calculate
* @return Matrix
* @throws Exception
**/
public static function diagonal($matrix)
{
$matrix = self::validateMatrix($matrix);
if (!$matrix->isSquare()) {
throw new Exception('Diagonal can only be extracted from a square matrix');
}
$dimensions = $matrix->rows;
$grid = Builder::createFilledMatrix(0, $dimensions, $dimensions)
->toArray();
for ($i = 0; $i < $dimensions; ++$i) {
$grid[$i][$i] = $matrix->getValue($i + 1, $i + 1);
}
return new Matrix($grid);
}
/**
* Return the antidiagonal of this matrix
*
* @param Matrix|array $matrix The matrix whose antidiagonal we wish to calculate
* @return Matrix
* @throws Exception
**/
public static function antidiagonal($matrix)
{
$matrix = self::validateMatrix($matrix);
if (!$matrix->isSquare()) {
throw new Exception('Anti-Diagonal can only be extracted from a square matrix');
}
$dimensions = $matrix->rows;
$grid = Builder::createFilledMatrix(0, $dimensions, $dimensions)
->toArray();
for ($i = 0; $i < $dimensions; ++$i) {
$grid[$i][$dimensions - $i - 1] = $matrix->getValue($i + 1, $dimensions - $i);
}
return new Matrix($grid);
}
/**
* 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
*
* @param Matrix|array $matrix The matrix whose identity we wish to calculate
* @return Matrix
* @throws Exception
**/
public static function identity($matrix)
{
$matrix = self::validateMatrix($matrix);
if (!$matrix->isSquare()) {
throw new Exception('Identity can only be created for a square matrix');
}
$dimensions = $matrix->rows;
return Builder::createIdentityMatrix($dimensions);
}
/**
* Return the inverse of this matrix
*
* @param Matrix|array $matrix The matrix whose inverse we wish to calculate
* @return Matrix
* @throws Exception
**/
public static function inverse($matrix, string $type = 'inverse')
{
$matrix = self::validateMatrix($matrix);
if (!$matrix->isSquare()) {
throw new Exception(ucfirst($type) . ' can only be calculated for a square matrix');
}
$determinant = self::getDeterminant($matrix);
if ($determinant == 0.0) {
throw new Div0Exception(ucfirst($type) . ' can only be calculated for a matrix with a non-zero determinant');
}
if ($matrix->rows == 1) {
return new Matrix([[1 / $matrix->getValue(1, 1)]]);
}
return self::getAdjoint($matrix)
->multiply(1 / $determinant);
}
/**
* Calculate the minors of the matrix
*
* @param Matrix $matrix The matrix whose minors we wish to calculate
* @return array[]
*
* @throws Exception
*/
protected static function getMinors(Matrix $matrix)
{
$minors = $matrix->toArray();
$dimensions = $matrix->rows;
if ($dimensions == 1) {
return $minors;
}
for ($i = 0; $i < $dimensions; ++$i) {
for ($j = 0; $j < $dimensions; ++$j) {
$minors[$i][$j] = self::getDeterminantSegment($matrix, $i, $j);
}
}
return $minors;
}
/**
* 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.
*
* @param Matrix|array $matrix The matrix whose minors we wish to calculate
* @return Matrix
* @throws Exception
**/
public static function minors($matrix)
{
$matrix = self::validateMatrix($matrix);
if (!$matrix->isSquare()) {
throw new Exception('Minors can only be calculated for a square matrix');
}
return new Matrix(self::getMinors($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
*
* @param Matrix|array $matrix The matrix whose trace we wish to calculate
* @return float
* @throws Exception
**/
public static function trace($matrix)
{
$matrix = self::validateMatrix($matrix);
if (!$matrix->isSquare()) {
throw new Exception('Trace can only be extracted from a square matrix');
}
$dimensions = $matrix->rows;
$result = 0;
for ($i = 1; $i <= $dimensions; ++$i) {
$result += $matrix->getValue($i, $i);
}
return $result;
}
/**
* Return the transpose of this matrix
*
* @param Matrix|\a $matrix The matrix whose transpose we wish to calculate
* @return Matrix
**/
public static function transpose($matrix)
{
$matrix = self::validateMatrix($matrix);
$array = array_values(array_merge([null], $matrix->toArray()));
$grid = call_user_func_array(
'array_map',
$array
);
return new Matrix($grid);
}
}