Matrix.php
11.2 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<?php
/**
*
* Class for the management of Matrices
*
* @copyright Copyright (c) 2018 Mark Baker (https://github.com/MarkBaker/PHPMatrix)
* @license https://opensource.org/licenses/MIT MIT
*/
namespace Matrix;
use Generator;
use Matrix\Decomposition\LU;
use Matrix\Decomposition\QR;
/**
* Matrix object.
*
* @package Matrix
*
* @property-read int $rows The number of rows in the matrix
* @property-read int $columns The number of columns in the matrix
* @method Matrix antidiagonal()
* @method Matrix adjoint()
* @method Matrix cofactors()
* @method float determinant()
* @method Matrix diagonal()
* @method Matrix identity()
* @method Matrix inverse()
* @method Matrix pseudoInverse()
* @method Matrix minors()
* @method float trace()
* @method Matrix transpose()
* @method Matrix add(...$matrices)
* @method Matrix subtract(...$matrices)
* @method Matrix multiply(...$matrices)
* @method Matrix divideby(...$matrices)
* @method Matrix divideinto(...$matrices)
* @method Matrix directsum(...$matrices)
*/
class Matrix
{
protected $rows;
protected $columns;
protected $grid = [];
/*
* Create a new Matrix object from an array of values
*
* @param array $grid
*/
final public function __construct(array $grid)
{
$this->buildFromArray(array_values($grid));
}
/*
* Create a new Matrix object from an array of values
*
* @param array $grid
*/
protected function buildFromArray(array $grid)
{
$this->rows = count($grid);
$columns = array_reduce(
$grid,
function ($carry, $value) {
return max($carry, is_array($value) ? count($value) : 1);
}
);
$this->columns = $columns;
array_walk(
$grid,
function (&$value) use ($columns) {
if (!is_array($value)) {
$value = [$value];
}
$value = array_pad(array_values($value), $columns, null);
}
);
$this->grid = $grid;
}
/**
* Validate that a row number is a positive integer
*
* @param int $row
* @return int
* @throws Exception
*/
public static function validateRow($row)
{
if ((!is_numeric($row)) || (intval($row) < 1)) {
throw new Exception('Invalid Row');
}
return (int)$row;
}
/**
* Validate that a column number is a positive integer
*
* @param int $column
* @return int
* @throws Exception
*/
public static function validateColumn($column)
{
if ((!is_numeric($column)) || (intval($column) < 1)) {
throw new Exception('Invalid Column');
}
return (int)$column;
}
/**
* Validate that a row number falls within the set of rows for this matrix
*
* @param int $row
* @return int
* @throws Exception
*/
protected function validateRowInRange($row)
{
$row = static::validateRow($row);
if ($row > $this->rows) {
throw new Exception('Requested Row exceeds matrix size');
}
return $row;
}
/**
* Validate that a column number falls within the set of columns for this matrix
*
* @param int $column
* @return int
* @throws Exception
*/
protected function validateColumnInRange($column)
{
$column = static::validateColumn($column);
if ($column > $this->columns) {
throw new Exception('Requested Column exceeds matrix size');
}
return $column;
}
/**
* 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
*
* @param int $row
* @param int $rowCount
* @return static
* @throws Exception
*/
public function getRows($row, $rowCount = 1)
{
$row = $this->validateRowInRange($row);
if ($rowCount === 0) {
$rowCount = $this->rows - $row + 1;
}
return new static(array_slice($this->grid, $row - 1, (int)$rowCount));
}
/**
* 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
*
* @param int $column
* @param int $columnCount
* @return Matrix
* @throws Exception
*/
public function getColumns($column, $columnCount = 1)
{
$column = $this->validateColumnInRange($column);
if ($columnCount < 1) {
$columnCount = $this->columns + $columnCount - $column + 1;
}
$grid = [];
for ($i = $column - 1; $i < $column + $columnCount - 1; ++$i) {
$grid[] = array_column($this->grid, $i);
}
return (new static($grid))->transpose();
}
/**
* 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
*
* @param int $row
* @param int $rowCount
* @return static
* @throws Exception
*/
public function dropRows($row, $rowCount = 1)
{
$this->validateRowInRange($row);
if ($rowCount === 0) {
$rowCount = $this->rows - $row + 1;
}
$grid = $this->grid;
array_splice($grid, $row - 1, (int)$rowCount);
return new static($grid);
}
/**
* 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
*
* @param int $column
* @param int $columnCount
* @return static
* @throws Exception
*/
public function dropColumns($column, $columnCount = 1)
{
$this->validateColumnInRange($column);
if ($columnCount < 1) {
$columnCount = $this->columns + $columnCount - $column + 1;
}
$grid = $this->grid;
array_walk(
$grid,
function (&$row) use ($column, $columnCount) {
array_splice($row, $column - 1, (int)$columnCount);
}
);
return new static($grid);
}
/**
* 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
*
* @param int $row
* @param int $column
* @return mixed
* @throws Exception
*/
public function getValue($row, $column)
{
$row = $this->validateRowInRange($row);
$column = $this->validateColumnInRange($column);
return $this->grid[$row - 1][$column - 1];
}
/**
* 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
*
* @return Generator|Matrix[]|mixed[]
*/
public function rows()
{
foreach ($this->grid as $i => $row) {
yield $i + 1 => ($this->columns == 1)
? $row[0]
: new static([$row]);
}
}
/**
* 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
*
* @return Generator|Matrix[]|mixed[]
*/
public function columns()
{
for ($i = 0; $i < $this->columns; ++$i) {
yield $i + 1 => ($this->rows == 1)
? $this->grid[0][$i]
: new static(array_column($this->grid, $i));
}
}
/**
* Identify if the row and column dimensions of this matrix are equal,
* i.e. if it is a "square" matrix
*
* @return bool
*/
public function isSquare()
{
return $this->rows == $this->columns;
}
/**
* Identify if this matrix is a vector
* i.e. if it comprises only a single row or a single column
*
* @return bool
*/
public function isVector()
{
return $this->rows == 1 || $this->columns == 1;
}
/**
* Return the matrix as a 2-dimensional array
*
* @return array
*/
public function toArray()
{
return $this->grid;
}
/**
* Solve A*X = B.
*
* @param Matrix $B Right hand side
*
* @throws Exception
*
* @return Matrix ... Solution if A is square, least squares solution otherwise
*/
public function solve(Matrix $B)
{
if ($this->columns === $this->rows) {
return (new LU($this))->solve($B);
}
return (new QR($this))->solve($B);
}
protected static $getters = [
'rows',
'columns',
];
/**
* Access specific properties as read-only (no setters)
*
* @param string $propertyName
* @return mixed
* @throws Exception
*/
public function __get($propertyName)
{
$propertyName = strtolower($propertyName);
// Test for function calls
if (in_array($propertyName, self::$getters)) {
return $this->$propertyName;
}
throw new Exception('Property does not exist');
}
protected static $functions = [
'antidiagonal',
'adjoint',
'cofactors',
'determinant',
'diagonal',
'identity',
'inverse',
'minors',
'trace',
'transpose',
];
protected static $operations = [
'add',
'subtract',
'multiply',
'divideby',
'divideinto',
'directsum',
];
/**
* Returns the result of the function call or operation
*
* @param string $functionName
* @param mixed[] $arguments
* @return Matrix|float
* @throws Exception
*/
public function __call($functionName, $arguments)
{
$functionName = strtolower(str_replace('_', '', $functionName));
if (in_array($functionName, self::$functions, true) || in_array($functionName, self::$operations, true)) {
$functionName = "\\" . __NAMESPACE__ . "\\{$functionName}";
if (is_callable($functionName)) {
$arguments = array_values(array_merge([$this], $arguments));
return call_user_func_array($functionName, $arguments);
}
}
throw new Exception('Function or Operation does not exist');
}
}