1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Phpml\Math; |
6
|
|
|
|
7
|
|
|
use PHP_CodeSniffer\Tokenizers\PHP; |
8
|
|
|
use Phpml\Exception\InvalidArgumentException; |
9
|
|
|
use Phpml\Exception\MatrixException; |
10
|
|
|
use Phpml\Math\LinearAlgebra\LUDecomposition; |
11
|
|
|
use PHPSci\PHPSci; |
12
|
|
|
|
13
|
|
|
class Matrix |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private $matrix = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
private $rows; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
private $columns; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var float |
32
|
|
|
*/ |
33
|
|
|
private $determinant; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @throws InvalidArgumentException |
37
|
|
|
*/ |
38
|
|
|
public function __construct(array $matrix, bool $validate = true) |
39
|
|
|
{ |
40
|
|
|
// When a row vector is given |
41
|
|
|
if (!is_array($matrix[0])) { |
42
|
|
|
$this->rows = 1; |
43
|
|
|
$this->columns = count($matrix); |
44
|
|
|
$matrix = [$matrix]; |
45
|
|
|
} else { |
46
|
|
|
$this->rows = count($matrix); |
47
|
|
|
$this->columns = count($matrix[0]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($validate) { |
51
|
|
|
for ($i = 0; $i < $this->rows; ++$i) { |
52
|
|
|
if (count($matrix[$i]) !== $this->columns) { |
53
|
|
|
throw new InvalidArgumentException('Matrix dimensions did not match'); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->matrix = $matrix; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public static function fromFlatArray(array $array): self |
62
|
|
|
{ |
63
|
|
|
$matrix = []; |
64
|
|
|
foreach ($array as $value) { |
65
|
|
|
$matrix[] = [$value]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return new self($matrix); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function toArray(): array |
72
|
|
|
{ |
73
|
|
|
return $this->matrix; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function toScalar(): float |
77
|
|
|
{ |
78
|
|
|
return $this->matrix[0][0]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getRows(): int |
82
|
|
|
{ |
83
|
|
|
return $this->rows; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getColumns(): int |
87
|
|
|
{ |
88
|
|
|
return $this->columns; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @throws MatrixException |
93
|
|
|
*/ |
94
|
|
|
public function getColumnValues($column): array |
95
|
|
|
{ |
96
|
|
|
if ($column >= $this->columns) { |
97
|
|
|
throw new MatrixException('Column out of range'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return array_column($this->matrix, $column); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return float|int |
105
|
|
|
* |
106
|
|
|
* @throws MatrixException |
107
|
|
|
*/ |
108
|
|
|
public function getDeterminant() |
109
|
|
|
{ |
110
|
|
|
if ($this->determinant !== null) { |
111
|
|
|
return $this->determinant; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (!$this->isSquare()) { |
115
|
|
|
throw new MatrixException('Matrix is not square matrix'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$lu = new LUDecomposition($this); |
119
|
|
|
|
120
|
|
|
return $this->determinant = $lu->det(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function isSquare(): bool |
124
|
|
|
{ |
125
|
|
|
return $this->columns === $this->rows; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function transpose(): self |
129
|
|
|
{ |
130
|
|
|
if ($this->rows == 1) { |
131
|
|
|
$matrix = array_map(function ($el) { |
132
|
|
|
return [$el]; |
133
|
|
|
}, $this->matrix[0]); |
134
|
|
|
} else { |
135
|
|
|
$matrix = PHPSci::transpose(new PHPSci($this->matrix))->toArray(); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
return new self($matrix, false); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function multiply(self $matrix): self |
141
|
|
|
{ |
142
|
|
|
if ($this->columns != $matrix->getRows()) { |
143
|
|
|
throw new InvalidArgumentException('Inconsistent matrix supplied'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$array1 = $this->toArray(); |
147
|
|
|
$array2 = $matrix->toArray(); |
148
|
|
|
$colCount = $matrix->columns; |
|
|
|
|
149
|
|
|
|
150
|
|
|
/* |
151
|
|
|
- To speed-up multiplication, we need to avoid use of array index operator [ ] as much as possible( See #255 for details) |
152
|
|
|
- A combination of "foreach" and "array_column" works much faster then accessing the array via index operator |
153
|
|
|
*/ |
154
|
|
|
$product = PHPSci::matmul(new PHPSci($array1), new PHPSci($array2))->toArray(); |
155
|
|
|
|
156
|
|
|
return new self($product, false); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
View Code Duplication |
public function divideByScalar($value): self |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
$newMatrix = []; |
162
|
|
|
for ($i = 0; $i < $this->rows; ++$i) { |
163
|
|
|
for ($j = 0; $j < $this->columns; ++$j) { |
164
|
|
|
$newMatrix[$i][$j] = $this->matrix[$i][$j] / $value; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return new self($newMatrix, false); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
View Code Duplication |
public function multiplyByScalar($value): self |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
$newMatrix = []; |
174
|
|
|
for ($i = 0; $i < $this->rows; ++$i) { |
175
|
|
|
for ($j = 0; $j < $this->columns; ++$j) { |
176
|
|
|
$newMatrix[$i][$j] = $this->matrix[$i][$j] * $value; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return new self($newMatrix, false); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Element-wise addition of the matrix with another one |
185
|
|
|
*/ |
186
|
|
|
public function add(self $other): self |
187
|
|
|
{ |
188
|
|
|
return $this->_add($other); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Element-wise subtracting of another matrix from this one |
193
|
|
|
*/ |
194
|
|
|
public function subtract(self $other): self |
195
|
|
|
{ |
196
|
|
|
return $this->_add($other, -1); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function inverse(): self |
200
|
|
|
{ |
201
|
|
|
if (!$this->isSquare()) { |
202
|
|
|
throw new MatrixException('Matrix is not square matrix'); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$LU = new LUDecomposition($this); |
206
|
|
|
$identity = $this->getIdentity(); |
207
|
|
|
$inverse = $LU->solve($identity); |
208
|
|
|
|
209
|
|
|
return new self($inverse, false); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function crossOut(int $row, int $column): self |
213
|
|
|
{ |
214
|
|
|
$newMatrix = []; |
215
|
|
|
$r = 0; |
216
|
|
|
for ($i = 0; $i < $this->rows; ++$i) { |
217
|
|
|
$c = 0; |
218
|
|
|
if ($row != $i) { |
219
|
|
|
for ($j = 0; $j < $this->columns; ++$j) { |
220
|
|
|
if ($column != $j) { |
221
|
|
|
$newMatrix[$r][$c] = $this->matrix[$i][$j]; |
222
|
|
|
++$c; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
++$r; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return new self($newMatrix, false); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function isSingular(): bool |
234
|
|
|
{ |
235
|
|
|
return $this->getDeterminant() == 0; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Frobenius norm (Hilbert–Schmidt norm, Euclidean norm) (‖A‖F) |
240
|
|
|
* Square root of the sum of the square of all elements. |
241
|
|
|
* |
242
|
|
|
* https://en.wikipedia.org/wiki/Matrix_norm#Frobenius_norm |
243
|
|
|
* |
244
|
|
|
* _____________ |
245
|
|
|
* /ᵐ ⁿ |
246
|
|
|
* ‖A‖F = √ Σ Σ |aᵢⱼ|² |
247
|
|
|
* ᵢ₌₁ ᵢ₌₁ |
248
|
|
|
*/ |
249
|
|
|
public function frobeniusNorm(): float |
250
|
|
|
{ |
251
|
|
|
$squareSum = 0; |
252
|
|
|
for ($i = 0; $i < $this->rows; ++$i) { |
253
|
|
|
for ($j = 0; $j < $this->columns; ++$j) { |
254
|
|
|
$squareSum += ($this->matrix[$i][$j]) ** 2; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return sqrt($squareSum); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Returns the transpose of given array |
263
|
|
|
*/ |
264
|
|
|
public static function transposeArray(array $array): array |
265
|
|
|
{ |
266
|
|
|
return (new self($array, false))->transpose()->toArray(); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Returns the dot product of two arrays<br> |
271
|
|
|
* Matrix::dot(x, y) ==> x.y' |
272
|
|
|
*/ |
273
|
|
|
public static function dot(array $array1, array $array2): array |
274
|
|
|
{ |
275
|
|
|
$m1 = new self($array1, false); |
276
|
|
|
$m2 = new self($array2, false); |
277
|
|
|
|
278
|
|
|
return $m1->multiply($m2->transpose())->toArray()[0]; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Element-wise addition or substraction depending on the given sign parameter |
283
|
|
|
*/ |
284
|
|
|
private function _add(self $other, int $sign = 1): self |
285
|
|
|
{ |
286
|
|
|
$a1 = $this->toArray(); |
287
|
|
|
$a2 = $other->toArray(); |
288
|
|
|
|
289
|
|
|
$newMatrix = []; |
290
|
|
|
for ($i = 0; $i < $this->rows; ++$i) { |
291
|
|
|
for ($k = 0; $k < $this->columns; ++$k) { |
292
|
|
|
$newMatrix[$i][$k] = $a1[$i][$k] + $sign * $a2[$i][$k]; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
return new self($newMatrix, false); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Returns diagonal identity matrix of the same size of this matrix |
301
|
|
|
*/ |
302
|
|
|
private function getIdentity(): self |
303
|
|
|
{ |
304
|
|
|
return new self((PHPSci::identity($this->rows))->toArray(), false); |
|
|
|
|
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.