Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like LUDecomposition often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LUDecomposition, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
31 | class LUDecomposition |
||
32 | { |
||
33 | /** |
||
34 | * Decomposition storage |
||
35 | * @var array |
||
36 | */ |
||
37 | private $LU = []; |
||
38 | |||
39 | /** |
||
40 | * Row dimension. |
||
41 | * @var int |
||
42 | */ |
||
43 | private $m; |
||
44 | |||
45 | /** |
||
46 | * Column dimension. |
||
47 | * @var int |
||
48 | */ |
||
49 | private $n; |
||
50 | |||
51 | /** |
||
52 | * Pivot sign. |
||
53 | * @var int |
||
54 | */ |
||
55 | private $pivsign; |
||
56 | |||
57 | /** |
||
58 | * Internal storage of pivot vector. |
||
59 | * @var array |
||
60 | */ |
||
61 | private $piv = []; |
||
62 | |||
63 | |||
64 | /** |
||
65 | * LU Decomposition constructor. |
||
66 | * |
||
67 | * @param $A Rectangular matrix |
||
68 | * @return Structure to access L, U and piv. |
||
|
|||
69 | */ |
||
70 | public function __construct(Matrix $A) |
||
129 | |||
130 | |||
131 | /** |
||
132 | * Get lower triangular factor. |
||
133 | * |
||
134 | * @return array Lower triangular factor |
||
135 | */ |
||
136 | public function getL() |
||
152 | |||
153 | |||
154 | /** |
||
155 | * Get upper triangular factor. |
||
156 | * |
||
157 | * @return array Upper triangular factor |
||
158 | */ |
||
159 | public function getU() |
||
173 | |||
174 | |||
175 | /** |
||
176 | * Return pivot permutation vector. |
||
177 | * |
||
178 | * @return array Pivot vector |
||
179 | */ |
||
180 | public function getPivot() |
||
184 | |||
185 | |||
186 | /** |
||
187 | * Alias for getPivot |
||
188 | * |
||
189 | * @see getPivot |
||
190 | */ |
||
191 | public function getDoublePivot() |
||
195 | |||
196 | |||
197 | /** |
||
198 | * Is the matrix nonsingular? |
||
199 | * |
||
200 | * @return true if U, and hence A, is nonsingular. |
||
201 | */ |
||
202 | public function isNonsingular() |
||
211 | |||
212 | |||
213 | /** |
||
214 | * Count determinants |
||
215 | * |
||
216 | * @return array d matrix deterninat |
||
217 | */ |
||
218 | public function det() |
||
230 | |||
231 | |||
232 | /** |
||
233 | * Solve A*X = B |
||
234 | * |
||
235 | * @param Matrix $B A Matrix with as many rows as A and any number of columns. |
||
236 | * |
||
237 | * @return array X so that L*U*X = B(piv,:) |
||
238 | * |
||
239 | * @throws MatrixException |
||
240 | */ |
||
241 | public function solve(Matrix $B) |
||
275 | |||
276 | /** |
||
277 | * @param Matrix $matrix |
||
278 | * @param int $j0 |
||
279 | * @param int $jF |
||
280 | * |
||
281 | * @return array |
||
282 | */ |
||
283 | protected function getSubMatrix(array $matrix, array $RL, int $j0, int $jF) |
||
297 | } // class LUDecomposition |
||
298 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.