Conditions | 14 |
Paths | 147 |
Total Lines | 57 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
69 | public function __construct($A) |
||
70 | { |
||
71 | if ($A instanceof Matrix) { |
||
|
|||
72 | // Use a "left-looking", dot-product, Crout/Doolittle algorithm. |
||
73 | $this->LU = $A->getArray(); |
||
74 | $this->m = $A->getRowDimension(); |
||
75 | $this->n = $A->getColumnDimension(); |
||
76 | for ($i = 0; $i < $this->m; ++$i) { |
||
77 | $this->piv[$i] = $i; |
||
78 | } |
||
79 | $this->pivsign = 1; |
||
80 | $LUrowi = $LUcolj = []; |
||
81 | |||
82 | // Outer loop. |
||
83 | for ($j = 0; $j < $this->n; ++$j) { |
||
84 | // Make a copy of the j-th column to localize references. |
||
85 | for ($i = 0; $i < $this->m; ++$i) { |
||
86 | $LUcolj[$i] = &$this->LU[$i][$j]; |
||
87 | } |
||
88 | // Apply previous transformations. |
||
89 | for ($i = 0; $i < $this->m; ++$i) { |
||
90 | $LUrowi = $this->LU[$i]; |
||
91 | // Most of the time is spent in the following dot product. |
||
92 | $kmax = min($i, $j); |
||
93 | $s = 0.0; |
||
94 | for ($k = 0; $k < $kmax; ++$k) { |
||
95 | $s += $LUrowi[$k] * $LUcolj[$k]; |
||
96 | } |
||
97 | $LUrowi[$j] = $LUcolj[$i] -= $s; |
||
98 | } |
||
99 | // Find pivot and exchange if necessary. |
||
100 | $p = $j; |
||
101 | for ($i = $j + 1; $i < $this->m; ++$i) { |
||
102 | if (abs($LUcolj[$i]) > abs($LUcolj[$p])) { |
||
103 | $p = $i; |
||
104 | } |
||
105 | } |
||
106 | if ($p != $j) { |
||
107 | for ($k = 0; $k < $this->n; ++$k) { |
||
108 | $t = $this->LU[$p][$k]; |
||
109 | $this->LU[$p][$k] = $this->LU[$j][$k]; |
||
110 | $this->LU[$j][$k] = $t; |
||
111 | } |
||
112 | $k = $this->piv[$p]; |
||
113 | $this->piv[$p] = $this->piv[$j]; |
||
114 | $this->piv[$j] = $k; |
||
115 | $this->pivsign = $this->pivsign * -1; |
||
116 | } |
||
117 | // Compute multipliers. |
||
118 | if (($j < $this->m) && ($this->LU[$j][$j] != 0.0)) { |
||
119 | for ($i = $j + 1; $i < $this->m; ++$i) { |
||
120 | $this->LU[$i][$j] /= $this->LU[$j][$j]; |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | } else { |
||
125 | throw new CalculationException(Matrix::ARGUMENT_TYPE_EXCEPTION); |
||
126 | } |
||
286 |