These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Phpml\Math; |
||
6 | |||
7 | use Phpml\Exception\InvalidArgumentException; |
||
8 | use Phpml\Exception\MatrixException; |
||
9 | use Phpml\Math\LinearAlgebra\LUDecomposition; |
||
10 | |||
11 | class Matrix |
||
12 | { |
||
13 | /** |
||
14 | * @var array |
||
15 | */ |
||
16 | private $matrix = []; |
||
17 | |||
18 | /** |
||
19 | * @var int |
||
20 | */ |
||
21 | private $rows; |
||
22 | |||
23 | /** |
||
24 | * @var int |
||
25 | */ |
||
26 | private $columns; |
||
27 | |||
28 | /** |
||
29 | * @var float |
||
30 | */ |
||
31 | private $determinant; |
||
32 | |||
33 | /** |
||
34 | * @throws InvalidArgumentException |
||
35 | */ |
||
36 | public function __construct(array $matrix, bool $validate = true) |
||
37 | { |
||
38 | // When a row vector is given |
||
39 | if (!is_array($matrix[0])) { |
||
40 | $this->rows = 1; |
||
41 | $this->columns = count($matrix); |
||
42 | $matrix = [$matrix]; |
||
43 | } else { |
||
44 | $this->rows = count($matrix); |
||
45 | $this->columns = count($matrix[0]); |
||
46 | } |
||
47 | |||
48 | if ($validate) { |
||
49 | for ($i = 0; $i < $this->rows; ++$i) { |
||
50 | if (count($matrix[$i]) !== $this->columns) { |
||
51 | throw InvalidArgumentException::matrixDimensionsDidNotMatch(); |
||
52 | } |
||
53 | } |
||
54 | } |
||
55 | |||
56 | $this->matrix = $matrix; |
||
57 | } |
||
58 | |||
59 | public static function fromFlatArray(array $array): self |
||
60 | { |
||
61 | $matrix = []; |
||
62 | foreach ($array as $value) { |
||
63 | $matrix[] = [$value]; |
||
64 | } |
||
65 | |||
66 | return new self($matrix); |
||
67 | } |
||
68 | |||
69 | public function toArray(): array |
||
70 | { |
||
71 | return $this->matrix; |
||
72 | } |
||
73 | |||
74 | public function toScalar(): float |
||
75 | { |
||
76 | return $this->matrix[0][0]; |
||
77 | } |
||
78 | |||
79 | public function getRows(): int |
||
80 | { |
||
81 | return $this->rows; |
||
82 | } |
||
83 | |||
84 | public function getColumns(): int |
||
85 | { |
||
86 | return $this->columns; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @throws MatrixException |
||
91 | */ |
||
92 | public function getColumnValues($column): array |
||
93 | { |
||
94 | if ($column >= $this->columns) { |
||
95 | throw MatrixException::columnOutOfRange(); |
||
96 | } |
||
97 | |||
98 | return array_column($this->matrix, $column); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @return float|int |
||
103 | * |
||
104 | * @throws MatrixException |
||
105 | */ |
||
106 | public function getDeterminant() |
||
107 | { |
||
108 | if ($this->determinant) { |
||
109 | return $this->determinant; |
||
110 | } |
||
111 | |||
112 | if (!$this->isSquare()) { |
||
113 | throw MatrixException::notSquareMatrix(); |
||
114 | } |
||
115 | |||
116 | $lu = new LUDecomposition($this); |
||
117 | |||
118 | return $this->determinant = $lu->det(); |
||
0 ignored issues
–
show
|
|||
119 | } |
||
120 | |||
121 | public function isSquare(): bool |
||
122 | { |
||
123 | return $this->columns === $this->rows; |
||
124 | } |
||
125 | |||
126 | public function transpose(): self |
||
127 | { |
||
128 | if ($this->rows == 1) { |
||
129 | $matrix = array_map(function ($el) { |
||
130 | return [$el]; |
||
131 | }, $this->matrix[0]); |
||
132 | } else { |
||
133 | $matrix = array_map(null, ...$this->matrix); |
||
134 | } |
||
135 | |||
136 | return new self($matrix, false); |
||
137 | } |
||
138 | |||
139 | public function multiply(self $matrix): self |
||
140 | { |
||
141 | if ($this->columns != $matrix->getRows()) { |
||
142 | throw InvalidArgumentException::inconsistentMatrixSupplied(); |
||
143 | } |
||
144 | |||
145 | $product = []; |
||
146 | $multiplier = $matrix->toArray(); |
||
147 | for ($i = 0; $i < $this->rows; ++$i) { |
||
148 | $columns = $matrix->getColumns(); |
||
149 | for ($j = 0; $j < $columns; ++$j) { |
||
150 | $product[$i][$j] = 0; |
||
151 | for ($k = 0; $k < $this->columns; ++$k) { |
||
152 | $product[$i][$j] += $this->matrix[$i][$k] * $multiplier[$k][$j]; |
||
153 | } |
||
154 | } |
||
155 | } |
||
156 | |||
157 | return new self($product, false); |
||
158 | } |
||
159 | |||
160 | View Code Duplication | public function divideByScalar($value): self |
|
161 | { |
||
162 | $newMatrix = []; |
||
163 | for ($i = 0; $i < $this->rows; ++$i) { |
||
164 | for ($j = 0; $j < $this->columns; ++$j) { |
||
165 | $newMatrix[$i][$j] = $this->matrix[$i][$j] / $value; |
||
166 | } |
||
167 | } |
||
168 | |||
169 | return new self($newMatrix, false); |
||
170 | } |
||
171 | |||
172 | View Code Duplication | public function multiplyByScalar($value): self |
|
173 | { |
||
174 | $newMatrix = []; |
||
175 | for ($i = 0; $i < $this->rows; ++$i) { |
||
176 | for ($j = 0; $j < $this->columns; ++$j) { |
||
177 | $newMatrix[$i][$j] = $this->matrix[$i][$j] * $value; |
||
178 | } |
||
179 | } |
||
180 | |||
181 | return new self($newMatrix, false); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Element-wise addition of the matrix with another one |
||
186 | */ |
||
187 | public function add(self $other): self |
||
188 | { |
||
189 | return $this->_add($other); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Element-wise subtracting of another matrix from this one |
||
194 | */ |
||
195 | public function subtract(self $other): self |
||
196 | { |
||
197 | return $this->_add($other, -1); |
||
198 | } |
||
199 | |||
200 | public function inverse(): self |
||
201 | { |
||
202 | if (!$this->isSquare()) { |
||
203 | throw MatrixException::notSquareMatrix(); |
||
204 | } |
||
205 | |||
206 | $LU = new LUDecomposition($this); |
||
207 | $identity = $this->getIdentity(); |
||
208 | $inverse = $LU->solve($identity); |
||
209 | |||
210 | return new self($inverse, false); |
||
211 | } |
||
212 | |||
213 | public function crossOut(int $row, int $column): self |
||
214 | { |
||
215 | $newMatrix = []; |
||
216 | $r = 0; |
||
217 | for ($i = 0; $i < $this->rows; ++$i) { |
||
218 | $c = 0; |
||
219 | if ($row != $i) { |
||
220 | for ($j = 0; $j < $this->columns; ++$j) { |
||
221 | if ($column != $j) { |
||
222 | $newMatrix[$r][$c] = $this->matrix[$i][$j]; |
||
223 | ++$c; |
||
224 | } |
||
225 | } |
||
226 | |||
227 | ++$r; |
||
228 | } |
||
229 | } |
||
230 | |||
231 | return new self($newMatrix, false); |
||
232 | } |
||
233 | |||
234 | public function isSingular(): bool |
||
235 | { |
||
236 | return $this->getDeterminant() == 0; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Returns the transpose of given array |
||
241 | */ |
||
242 | public static function transposeArray(array $array): array |
||
243 | { |
||
244 | return (new self($array, false))->transpose()->toArray(); |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Returns the dot product of two arrays<br> |
||
249 | * Matrix::dot(x, y) ==> x.y' |
||
250 | */ |
||
251 | public static function dot(array $array1, array $array2): array |
||
252 | { |
||
253 | $m1 = new self($array1, false); |
||
254 | $m2 = new self($array2, false); |
||
255 | |||
256 | return $m1->multiply($m2->transpose())->toArray()[0]; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Element-wise addition or substraction depending on the given sign parameter |
||
261 | */ |
||
262 | protected function _add(self $other, int $sign = 1): self |
||
263 | { |
||
264 | $a1 = $this->toArray(); |
||
265 | $a2 = $other->toArray(); |
||
266 | |||
267 | $newMatrix = []; |
||
268 | for ($i = 0; $i < $this->rows; ++$i) { |
||
269 | for ($k = 0; $k < $this->columns; ++$k) { |
||
270 | $newMatrix[$i][$k] = $a1[$i][$k] + $sign * $a2[$i][$k]; |
||
271 | } |
||
272 | } |
||
273 | |||
274 | return new self($newMatrix, false); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Returns diagonal identity matrix of the same size of this matrix |
||
279 | */ |
||
280 | protected function getIdentity(): self |
||
281 | { |
||
282 | $array = array_fill(0, $this->rows, array_fill(0, $this->columns, 0)); |
||
283 | for ($i = 0; $i < $this->rows; ++$i) { |
||
284 | $array[$i][$i] = 1; |
||
285 | } |
||
286 | |||
287 | return new self($array, false); |
||
288 | } |
||
289 | } |
||
290 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.