Total Complexity | 40 |
Total Lines | 223 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like QRDecomposition 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.
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 QRDecomposition, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class QRDecomposition |
||
23 | { |
||
24 | const MATRIX_RANK_EXCEPTION = 'Can only perform operation on full-rank matrix.'; |
||
25 | |||
26 | /** |
||
27 | * Array for internal storage of decomposition. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | private $QR = []; |
||
32 | |||
33 | /** |
||
34 | * Row dimension. |
||
35 | * |
||
36 | * @var int |
||
37 | */ |
||
38 | private $m; |
||
39 | |||
40 | /** |
||
41 | * Column dimension. |
||
42 | * |
||
43 | * @var int |
||
44 | */ |
||
45 | private $n; |
||
46 | |||
47 | /** |
||
48 | * Array for internal storage of diagonal of R. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | private $Rdiag = []; |
||
53 | |||
54 | /** |
||
55 | * QR Decomposition computed by Householder reflections. |
||
56 | * |
||
57 | * @param matrix $A Rectangular matrix |
||
58 | */ |
||
59 | public function __construct($A) |
||
98 | } |
||
99 | } |
||
100 | |||
101 | // function __construct() |
||
102 | |||
103 | /** |
||
104 | * Is the matrix full rank? |
||
105 | * |
||
106 | * @return bool true if R, and hence A, has full rank, else false |
||
107 | */ |
||
108 | public function isFullRank() |
||
109 | { |
||
110 | for ($j = 0; $j < $this->n; ++$j) { |
||
111 | if ($this->Rdiag[$j] == 0) { |
||
112 | return false; |
||
113 | } |
||
114 | } |
||
115 | |||
116 | return true; |
||
117 | } |
||
118 | |||
119 | // function isFullRank() |
||
120 | |||
121 | /** |
||
122 | * Return the Householder vectors. |
||
123 | * |
||
124 | * @return Matrix Lower trapezoidal matrix whose columns define the reflections |
||
125 | */ |
||
126 | public function getH() |
||
127 | { |
||
128 | for ($i = 0; $i < $this->m; ++$i) { |
||
129 | for ($j = 0; $j < $this->n; ++$j) { |
||
130 | if ($i >= $j) { |
||
131 | $H[$i][$j] = $this->QR[$i][$j]; |
||
132 | } else { |
||
133 | $H[$i][$j] = 0.0; |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | |||
138 | return new Matrix($H); |
||
139 | } |
||
140 | |||
141 | // function getH() |
||
142 | |||
143 | /** |
||
144 | * Return the upper triangular factor. |
||
145 | * |
||
146 | * @return Matrix upper triangular factor |
||
147 | */ |
||
148 | public function getR() |
||
149 | { |
||
150 | for ($i = 0; $i < $this->n; ++$i) { |
||
151 | for ($j = 0; $j < $this->n; ++$j) { |
||
152 | if ($i < $j) { |
||
153 | $R[$i][$j] = $this->QR[$i][$j]; |
||
154 | } elseif ($i == $j) { |
||
155 | $R[$i][$j] = $this->Rdiag[$i]; |
||
156 | } else { |
||
157 | $R[$i][$j] = 0.0; |
||
158 | } |
||
159 | } |
||
160 | } |
||
161 | |||
162 | return new Matrix($R); |
||
163 | } |
||
164 | |||
165 | // function getR() |
||
166 | |||
167 | /** |
||
168 | * Generate and return the (economy-sized) orthogonal factor. |
||
169 | * |
||
170 | * @return Matrix orthogonal factor |
||
171 | */ |
||
172 | public function getQ() |
||
194 | } |
||
195 | |||
196 | // function getQ() |
||
197 | |||
198 | /** |
||
199 | * Least squares solution of A*X = B. |
||
200 | * |
||
201 | * @param Matrix $B a Matrix with as many rows as A and any number of columns |
||
202 | * |
||
203 | * @return Matrix matrix that minimizes the two norm of Q*R*X-B |
||
204 | */ |
||
205 | public function solve($B) |
||
247 |