| Total Complexity | 59 |
| Total Lines | 231 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | 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 { |
||
| 94 | } |
||
| 95 | |||
| 96 | public function sin(): matrix { |
||
| 97 | return $this->map('sin'); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function asin(): matrix { |
||
| 102 | } |
||
| 103 | |||
| 104 | public function cos(): matrix { |
||
| 106 | } |
||
| 107 | |||
| 108 | public function acos(): matrix { |
||
| 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 { |
||
| 134 | } |
||
| 135 | |||
| 136 | public function free():void { |
||
| 137 | if($this instanceof matrix) { |
||
| 138 | unset($this->row); |
||
| 139 | unset($this->col); |
||
| 140 | unset($this->ndim); |
||
| 141 | unset($this->data); |
||
| 142 | return; |
||
| 143 | } |
||
| 144 | unset($this->col); |
||
| 145 | unset($this->ndim); |
||
| 146 | unset($this->data); |
||
| 147 | return; |
||
| 148 | } |
||
| 149 | /** |
||
| 150 | * make a copy of matrix|vector; |
||
| 151 | * @return matrix|vector |
||
| 152 | */ |
||
| 153 | public function copy(): matrix|vector { |
||
| 154 | return clone $this; |
||
|
1 ignored issue
–
show
|
|||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Clip the elements in the matrix to be between given minimum and maximum |
||
| 159 | * and return a new matrix. |
||
| 160 | * |
||
| 161 | * @param float $min |
||
| 162 | * @param float $max |
||
| 163 | * @return matrix |
||
| 164 | */ |
||
| 165 | public function clip(float $min, float $max): matrix|vector { |
||
| 166 | if ($this instanceof matrix) { |
||
| 167 | $ar = self::factory($this->row, $this->col, $this->dtype); |
||
| 168 | } else { |
||
| 169 | $ar = self::factory($this->col, $this->dtype); |
||
| 170 | } |
||
| 171 | for ($i = 0; $i < $this->ndim; ++$i) { |
||
| 172 | if ($this->data[$i] > $max) { |
||
| 173 | $ar->data[$i] = $max; |
||
| 174 | continue; |
||
| 175 | } |
||
| 176 | if ($this->data[$i] < $min) { |
||
| 177 | $ar->data[$i] = $min; |
||
| 178 | continue; |
||
| 179 | } |
||
| 180 | $ar->data[$i] = $this->data[$i]; |
||
| 181 | } |
||
| 182 | return $ar; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Clip the matrix|vector to be lower bounded by a given minimum. |
||
| 187 | * @param float $min |
||
| 188 | * @return matrix |
||
| 189 | */ |
||
| 190 | public function clipLower(float $min): matrix { |
||
| 191 | if ($this instanceof matrix) { |
||
| 192 | $ar = self::factory($this->row, $this->col, $this->dtype); |
||
| 193 | } else { |
||
| 194 | $ar = self::factory($this->col, $this->dtype); |
||
| 195 | } |
||
| 196 | for ($i = 0; $i < $this->ndim; ++$i) { |
||
| 197 | if ($this->data[$i] < $min) { |
||
| 198 | $ar->data[$i] = $min; |
||
| 199 | continue; |
||
| 200 | } |
||
| 201 | $ar->data[$i] = $this->data[$i]; |
||
| 202 | } |
||
| 203 | return $ar; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Clip the matrix|vector to be upper bounded by a given maximum. |
||
| 208 | * |
||
| 209 | * @param float $max |
||
| 210 | * @return matrix |
||
| 211 | */ |
||
| 212 | public function clipUpper(float $max): matrix|vector { |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * return a reshaped data buffer as matrix |
||
| 230 | * @param int $row |
||
| 231 | * @param int $col |
||
| 232 | * @return matrix |
||
| 233 | */ |
||
| 234 | public function reshape(int $row, int $col): matrix { |
||
| 246 | } |
||
| 247 | |||
| 249 |