Total Complexity | 57 |
Total Lines | 218 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like ops 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 ops, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | trait ops { |
||
16 | |||
17 | /** |
||
18 | * Return the element-wise maximum of two matrices| two vectors. |
||
19 | * @param matrix|vector $d |
||
20 | * @return matrix|vector |
||
21 | */ |
||
22 | public function max(matrix|vector $d): matrix|vector { |
||
23 | if ($this instanceof matrix && $d instanceof matrix && $this->checkShape($this, $d) && $this->checkDtype($this, $d)) { |
||
24 | $r = self::factory($this->row, $this->col, $this->dtype); |
||
25 | } elseif ($this instanceof vector && $d instanceof vector && $this->checkShape($this, $d) && $this->checkDtype($this, $d)) { |
||
26 | $r = self::factory($this->col, $this->dtype); |
||
27 | } |
||
28 | for ($i = 0; $i < $this->ndim; ++$i) { |
||
29 | $r->data[$i] = max($this->data[$i], $d->data[$i]); |
||
1 ignored issue
–
show
|
|||
30 | } |
||
31 | return $r; |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Return the element-wise minimum of two matrices|two vectors. |
||
36 | * @param matrix|vector $d |
||
37 | * @return matrix|vector |
||
38 | */ |
||
39 | public function min(matrix|vector $d): matrix|vector { |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Run a function over all of the elements in the matrix|vector. |
||
53 | * @param callable $func |
||
54 | * @return matrix|vector |
||
55 | */ |
||
56 | public function map(callable $func): matrix|vector { |
||
57 | if ($this instanceof matrix) { |
||
58 | $r = self::factory($this->row, $this->col, $this->dtype); |
||
59 | } else { |
||
60 | $r = self::factory($this->col, $this->dtype); |
||
61 | } |
||
62 | for ($i = 0; $i < $this->ndim; ++$i) { |
||
63 | $r->data[$i] = $func($this->data[$i]); |
||
64 | } |
||
65 | return $r; |
||
66 | } |
||
67 | |||
68 | public function abs(): matrix|vector { |
||
69 | return $this->map('abs'); |
||
70 | } |
||
71 | |||
72 | public function sqrt(): matrix|vector { |
||
73 | return $this->map('sqrt'); |
||
74 | } |
||
75 | |||
76 | public function exp(): matrix|vector { |
||
78 | } |
||
79 | |||
80 | public function exp1(): matrix|vector { |
||
81 | return $this->map('exp1'); |
||
82 | } |
||
83 | |||
84 | public function log(float $b = M_E): matrix|vector { |
||
85 | $ar = $this->copy(); |
||
86 | for ($i = 0; $i < $ar->ndim; ++$i) { |
||
87 | log($ar->data[$i], $b); |
||
88 | } |
||
89 | return $ar; |
||
90 | } |
||
91 | |||
92 | public function log1p(): matrix { |
||
93 | return $this->map('log1p'); |
||
94 | } |
||
95 | |||
96 | public function sin(): matrix { |
||
97 | return $this->map('sin'); |
||
98 | } |
||
99 | |||
100 | public function asin(): matrix { |
||
101 | return $this->map('asin'); |
||
102 | } |
||
103 | |||
104 | public function cos(): matrix { |
||
105 | return $this->map('cos'); |
||
106 | } |
||
107 | |||
108 | public function acos(): matrix { |
||
109 | return $this->map('acos'); |
||
110 | } |
||
111 | |||
112 | public function tan(): matrix { |
||
113 | return $this->map('tan'); |
||
114 | } |
||
115 | |||
116 | public function atan(): matrix { |
||
117 | return $this->map('atan'); |
||
118 | } |
||
119 | |||
120 | public function radToDeg(): matrix { |
||
121 | return $this->map('rad2deg'); |
||
122 | } |
||
123 | |||
124 | public function degToRad(): matrix { |
||
125 | return $this->map('deg2rad'); |
||
126 | } |
||
127 | |||
128 | public function floor(): matrix { |
||
129 | return $this->map('floor'); |
||
130 | } |
||
131 | |||
132 | public function ceil(): matrix { |
||
133 | return $this->map('ceil'); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * make a copy of matrix|vector; |
||
138 | * @return matrix|vector |
||
139 | */ |
||
140 | public function copy(): matrix|vector { |
||
141 | return clone $this; |
||
1 ignored issue
–
show
|
|||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Clip the elements in the matrix to be between given minimum and maximum |
||
146 | * and return a new matrix. |
||
147 | * |
||
148 | * @param float $min |
||
149 | * @param float $max |
||
150 | * @return matrix |
||
151 | */ |
||
152 | public function clip(float $min, float $max): matrix|vector { |
||
153 | if ($this instanceof matrix) { |
||
154 | $ar = self::factory($this->row, $this->col, $this->dtype); |
||
155 | } else { |
||
156 | $ar = self::factory($this->col, $this->dtype); |
||
157 | } |
||
158 | for ($i = 0; $i < $this->ndim; ++$i) { |
||
159 | if ($this->data[$i] > $max) { |
||
160 | $ar->data[$i] = $max; |
||
161 | continue; |
||
162 | } |
||
163 | if ($this->data[$i] < $min) { |
||
164 | $ar->data[$i] = $min; |
||
165 | continue; |
||
166 | } |
||
167 | $ar->data[$i] = $this->data[$i]; |
||
168 | } |
||
169 | return $ar; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Clip the matrix|vector to be lower bounded by a given minimum. |
||
174 | * @param float $min |
||
175 | * @return matrix |
||
176 | */ |
||
177 | public function clipLower(float $min): matrix { |
||
178 | if ($this instanceof matrix) { |
||
179 | $ar = self::factory($this->row, $this->col, $this->dtype); |
||
180 | } else { |
||
181 | $ar = self::factory($this->col, $this->dtype); |
||
182 | } |
||
183 | for ($i = 0; $i < $this->ndim; ++$i) { |
||
184 | if ($this->data[$i] < $min) { |
||
185 | $ar->data[$i] = $min; |
||
186 | continue; |
||
187 | } |
||
188 | $ar->data[$i] = $this->data[$i]; |
||
189 | } |
||
190 | return $ar; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Clip the matrix|vector to be upper bounded by a given maximum. |
||
195 | * |
||
196 | * @param float $max |
||
197 | * @return matrix |
||
198 | */ |
||
199 | public function clipUpper(float $max): matrix|vector { |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * return a reshaped data buffer as matrix |
||
217 | * @param int $row |
||
218 | * @param int $col |
||
219 | * @return matrix |
||
220 | */ |
||
221 | public function reshape(int $row, int $col): matrix { |
||
222 | if ($this->ndim != $row * $col) { |
||
223 | self::_dimensionaMisMatchErr('given dimenssion is not valid for current bufferData'); |
||
224 | } |
||
225 | if ($this instanceof vector) { |
||
226 | $ar = matrix::factory($row, $col, $this->dtype); |
||
227 | $ar->data = $this->data; |
||
228 | return $ar; |
||
229 | } |
||
230 | $this->row = $row; |
||
231 | $this->col = $col; |
||
232 | return $this; |
||
233 | } |
||
234 | |||
236 |