| Total Complexity | 150 |
| Total Lines | 795 |
| Duplicated Lines | 0 % |
| Changes | 12 | ||
| Bugs | 5 | Features | 0 |
Complex classes like vector 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 vector, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class vector extends nd { |
||
| 28 | use ops, linAlg; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Factory method to build a new vector. |
||
| 32 | * |
||
| 33 | * @param int $col |
||
| 34 | * @param int $dtype |
||
| 35 | * @return vector |
||
| 36 | */ |
||
| 37 | public static function factory(int $col, int $dtype = self::FLOAT): vector { |
||
| 38 | return new self($col, $dtype); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Build a new vector from a php array. |
||
| 43 | * |
||
| 44 | * @param array $data |
||
| 45 | * @param int $dtype |
||
| 46 | * @return vector |
||
| 47 | */ |
||
| 48 | public static function ar(array $data, int $dtype = self::FLOAT): vector { |
||
| 49 | if (is_array($data) && !is_array($data[0])) { |
||
| 50 | $ar = self::factory(count($data), $dtype); |
||
| 51 | $ar->setData($data); |
||
| 52 | return $ar; |
||
| 53 | } else { |
||
| 54 | self::_err('data must be of same dimensions'); |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Return vector with random values |
||
| 60 | * @param int $col |
||
| 61 | * @param int $dtype |
||
| 62 | * @return vector |
||
| 63 | */ |
||
| 64 | public static function randn(int $col, int $dtype = self::FLOAT): vector { |
||
| 65 | $ar = self::factory($col, $dtype); |
||
| 66 | $max = getrandmax(); |
||
| 67 | for ($i = 0; $i < $ar->col; ++$i) { |
||
| 68 | $ar->data[$i] = rand() / $max; |
||
| 69 | } |
||
| 70 | return $ar; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Return vector with uniform values |
||
| 75 | * @param int $col |
||
| 76 | * @param int $dtype |
||
| 77 | * @return vector |
||
| 78 | */ |
||
| 79 | public static function uniform(int $col, int $dtype = self::FLOAT): vector { |
||
| 80 | $ar = self::factory($col, $dtype); |
||
| 81 | $max = getrandmax(); |
||
| 82 | for ($i = 0; $i < $col; ++$i) { |
||
| 83 | $ar->data[$i] = rand(-$max, $max) / $max; |
||
| 84 | } |
||
| 85 | return $ar; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Build a vector of zeros with n elements. |
||
| 90 | * |
||
| 91 | * @param int $col |
||
| 92 | * @param int $dtype |
||
| 93 | * @return vector |
||
| 94 | */ |
||
| 95 | public static function zeros(int $col, int $dtype = self::FLOAT): vector { |
||
| 96 | $ar = self::factory($col, $dtype); |
||
| 97 | for ($i = 0; $i < $col; ++$i) { |
||
| 98 | $ar->data[$i] = 0; |
||
| 99 | } |
||
| 100 | return $ar; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * create one like vector |
||
| 105 | * |
||
| 106 | * @param int $col |
||
| 107 | * @return vector |
||
| 108 | */ |
||
| 109 | public static function ones(int $col, int $dtype = self::FLOAT): vector { |
||
| 110 | $ar = self::factory($col, $dtype); |
||
| 111 | for ($i = 0; $i < $col; ++$i) { |
||
| 112 | $ar->data[$i] = 1; |
||
| 113 | } |
||
| 114 | return $ar; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * create a null like vector |
||
| 119 | * @param int $col |
||
| 120 | * @return vector |
||
| 121 | */ |
||
| 122 | public static function null(int $col, int $dtype = self::FLOAT): vector { |
||
| 123 | $ar = self::factory($col, $dtype); |
||
| 124 | for ($i = 0; $i < $col; ++$i) { |
||
| 125 | $ar->data[$i] = null; |
||
| 126 | } |
||
| 127 | return $ar; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * create a vector with given scalar value |
||
| 132 | * @param int $col |
||
| 133 | * @param int|float|double $val |
||
| 134 | * @param int $dtype |
||
| 135 | * @return vector |
||
| 136 | */ |
||
| 137 | public static function full(int $col, int|float $val, int $dtype = self::FLOAT): vector { |
||
| 138 | $ar = self::factory($col, $dtype); |
||
| 139 | for ($i = 0; $i < $col; ++$i) { |
||
| 140 | $ar->data[$i] = $val; |
||
| 141 | } |
||
| 142 | return $ar; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Return evenly spaced values within a given interval. |
||
| 147 | * |
||
| 148 | * @param int|float $start |
||
| 149 | * @param int|float $end |
||
| 150 | * @param int|float $interval |
||
| 151 | * @param int $dtype |
||
| 152 | * @return vector |
||
| 153 | */ |
||
| 154 | public static function range(int|float $start, int|float $end, int|float $interval = 1, int $dtype = self::FLOAT): vector { |
||
| 155 | return self::ar(range($start, $end, $interval), $dtype); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Return a Gaussian random vector with mean 0 |
||
| 160 | * and unit variance. |
||
| 161 | * |
||
| 162 | * @param int $n |
||
| 163 | * @param int $dtype |
||
| 164 | * @return self |
||
| 165 | */ |
||
| 166 | public static function gaussian(int $n, int $dtype = self::FLOAT): vector { |
||
| 167 | $max = getrandmax(); |
||
| 168 | $a = []; |
||
| 169 | while (count($a) < $n) { |
||
| 170 | $r = sqrt(-2.0 * log(rand() / $max)); |
||
| 171 | $phi = rand() / $max * (2. * M_PI); |
||
| 172 | $a[] = $r * sin($phi); |
||
| 173 | $a[] = $r * cos($phi); |
||
| 174 | } |
||
| 175 | if (count($a) > $n) { |
||
| 176 | $a = array_slice($a, 0, $n); |
||
| 177 | } |
||
| 178 | return self::ar($a, $dtype); |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Generate a vector with n elements from a Poisson distribution. |
||
| 183 | * |
||
| 184 | * @param int $n |
||
| 185 | * @param float $lambda |
||
| 186 | * @param int $dtype |
||
| 187 | * @return vector |
||
| 188 | */ |
||
| 189 | public static function poisson(int $n, float $lambda = 1.0, int $dtype = self::FLOAT): vector { |
||
| 190 | $max = getrandmax(); |
||
| 191 | $l = exp(-$lambda); |
||
| 192 | $a = new self($n, $dtype); |
||
| 193 | for ($i = 0; $i < $n; ++$i) { |
||
| 194 | $k = 0; |
||
| 195 | $p = 1.0; |
||
| 196 | while ($p > $l) { |
||
| 197 | ++$k; |
||
| 198 | $p *= rand() / $max; |
||
| 199 | } |
||
| 200 | $a->data[$i] = $k - 1; |
||
| 201 | } |
||
| 202 | return $a; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Return a vector of n evenly spaced numbers between minimum and maximum. |
||
| 207 | * |
||
| 208 | * @param float $min |
||
| 209 | * @param float $max |
||
| 210 | * @param int $n |
||
| 211 | * @param int $dtype |
||
| 212 | * @throws invalidArgumentException |
||
| 213 | * @return vector |
||
| 214 | */ |
||
| 215 | public static function linspace(float $min, float $max, int $n, int $dtype = self::FLOAT): vector { |
||
| 216 | if ($min > $max) { |
||
| 217 | throw new invalidArgumentException('Minimum must be less than maximum.'); |
||
| 218 | } |
||
| 219 | if ($n < 2) { |
||
| 220 | throw new invalidArgumentException('Number of elements must be greater than 1.'); |
||
| 221 | } |
||
| 222 | $k = $n - 1; |
||
| 223 | $interval = abs($max - $min) / $k; |
||
| 224 | $a = [$min]; |
||
| 225 | while (count($a) < $k) { |
||
| 226 | $a[] = end($a) + $interval; |
||
| 227 | } |
||
| 228 | $a[] = $max; |
||
| 229 | return self::ar($a, $dtype); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Return the index of the minimum element in the vector. |
||
| 234 | * |
||
| 235 | * @return int |
||
| 236 | */ |
||
| 237 | public function argMin(): int { |
||
| 238 | return blas::min($this); |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Return the index of the maximum element in the vector. |
||
| 243 | * |
||
| 244 | * @return int |
||
| 245 | */ |
||
| 246 | public function argMax(): int { |
||
| 247 | return blas::max($this); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * The sum of the vector. |
||
| 252 | * @return float |
||
| 253 | */ |
||
| 254 | public function sum(): float { |
||
| 255 | return blas::asum($this); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Return the product of the vector. |
||
| 260 | * @return int|float |
||
| 261 | */ |
||
| 262 | public function product(): float { |
||
| 263 | $r = 1.0; |
||
| 264 | for ($i = 0; $i < $this->col; ++$i) { |
||
| 265 | $r *= $this->data[$i]; |
||
| 266 | } |
||
| 267 | return $r; |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Compute the vector-matrix dot product of this vector and matrix . |
||
| 272 | * @param \Np\matrix $m |
||
| 273 | * @return vector |
||
| 274 | */ |
||
| 275 | public function dotMatrix(\Np\matrix $m): vector { |
||
| 276 | if ($this->checkDtype($this, $m)) { |
||
| 277 | $mvr = self::factory($this->col, $this->dtype); |
||
| 278 | core\blas::gemv($m, $this, $mvr); |
||
| 279 | return $mvr; |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * |
||
| 285 | * @param int|float|matrix|vector $d |
||
| 286 | * @return matrix|vector |
||
| 287 | */ |
||
| 288 | public function divide(int|float|matrix|vector $d): matrix|vector { |
||
| 289 | if ($d instanceof matrix) { |
||
| 290 | return $this->divideMatrix($d); |
||
| 291 | } elseif ($d instanceof self) { |
||
| 292 | return $this->divideVector($d); |
||
| 293 | } else { |
||
| 294 | return $this->divideScalar($d); |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * |
||
| 300 | * @param \Np\matrix $m |
||
| 301 | * @return matrix |
||
| 302 | */ |
||
| 303 | protected function divideMatrix(\Np\matrix $m): matrix { |
||
| 304 | if ($this->checkShape($this, $m) && $this->checkDtype($this, $m)) { |
||
| 305 | $vr = matrix::factory($m->row, $m->col, $m->dtype); |
||
| 306 | for ($i = 0; $i < $m->row; ++$i) { |
||
| 307 | for ($j = 0; $j < $m->col; ++$j) { |
||
| 308 | $vr->data[$i * $m->col + $j] = $this->data[$j] / $m->data[$i * $m->col + $j]; |
||
| 309 | } |
||
| 310 | } |
||
| 311 | return $vr; |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * |
||
| 317 | * @param vector $v |
||
| 318 | * @return vector |
||
| 319 | */ |
||
| 320 | protected function divideVector(vector $v): vector { |
||
| 321 | if ($this->checkShape($this, $v) && $this->checkDtype($this, $v)) { |
||
| 322 | $vr = self::factory($this->col, $this->dtype); |
||
| 323 | for ($i = 0; $i < $this->col; ++$i) { |
||
| 324 | $vr->data[$i] = $this->data[$i] / $v->data[$i]; |
||
| 325 | } |
||
| 326 | return $vr; |
||
| 327 | } |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * |
||
| 332 | * @param int|float $s |
||
| 333 | * @return vector |
||
| 334 | */ |
||
| 335 | protected function divideScalar(int|float $s): vector { |
||
| 336 | $vr = self::factory($this->col, $this->dtype); |
||
| 337 | for ($i = 0; $i < $this->col; ++$i) { |
||
| 338 | $vr->data[$i] = $this->data[$i] / $s; |
||
| 339 | } |
||
| 340 | return $vr; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * |
||
| 345 | * @param int|float|matrix|vector $d |
||
| 346 | * @return matrix|vector |
||
| 347 | */ |
||
| 348 | public function multiply(int|float|matrix|vector $d): matrix|vector { |
||
| 349 | if ($d instanceof matrix) { |
||
| 350 | return $this->multiplyMatrix($d); |
||
| 351 | } elseif ($d instanceof self) { |
||
| 352 | return $this->multiplyVector($d); |
||
| 353 | } else { |
||
| 354 | return $this->multiplyScalar($d); |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * |
||
| 360 | * @param \Np\matrix $m |
||
| 361 | * @return matrix |
||
| 362 | */ |
||
| 363 | protected function multiplyMatrix(\Np\matrix $m): matrix { |
||
| 364 | if ($this->checkShape($this, $m) && $this->checkDtype($this, $m)) { |
||
| 365 | $vr = matrix::factory($m->row, $m->col, $m->dtype); |
||
| 366 | for ($i = 0; $i < $m->row; ++$i) { |
||
| 367 | for ($j = 0; $j < $m->col; ++$j) { |
||
| 368 | $vr->data[$i * $m->col + $j] = $this->data[$j] * $m->data[$i * $m->col + $j]; |
||
| 369 | } |
||
| 370 | } |
||
| 371 | return $vr; |
||
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * |
||
| 377 | * @param \Np\vector $vector |
||
| 378 | * @return vector |
||
| 379 | */ |
||
| 380 | protected function multiplyVector(\Np\vector $vector): vector { |
||
| 381 | if ($this->checkShape($this, $vector) && $this->checkDtype($this, $vector)) { |
||
| 382 | $vr = self::factory($this->col, $this->dtype); |
||
| 383 | for ($i = 0; $i < $this->col; ++$i) { |
||
| 384 | $vr->data[$i] = $this->data[$i] * $vector->data[$i]; |
||
| 385 | } |
||
| 386 | return $vr; |
||
| 387 | } |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * |
||
| 392 | * @param int|float $s |
||
| 393 | * @return vector |
||
| 394 | */ |
||
| 395 | protected function multiplyScalar(int|float $s): vector { |
||
| 396 | $vr = $this->copy(); |
||
| 397 | blas::scale($s, $vr); |
||
| 398 | return $vr; |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * |
||
| 403 | * @param int|float|matrix|vector $d |
||
| 404 | * @return matrix|vector |
||
| 405 | */ |
||
| 406 | public function add(int|float|matrix|vector $d): matrix|vector { |
||
| 407 | if ($d instanceof matrix) { |
||
| 408 | return $this->addMatrix($d); |
||
| 409 | } elseif ($d instanceof self) { |
||
| 410 | return $this->addVector($d); |
||
| 411 | } else { |
||
| 412 | return $this->addScalar($d); |
||
| 413 | } |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * |
||
| 418 | * @param \Np\matrix $m |
||
| 419 | * @return matrix |
||
| 420 | */ |
||
| 421 | protected function addMatrix(\Np\matrix $m): matrix { |
||
| 422 | if ($this->checkShape($this, $m) && $this->checkDtype($this, $m)) { |
||
| 423 | $vr = matrix::factory($m->row, $m->col, $m->dtype); |
||
| 424 | for ($i = 0; $i < $m->row; ++$i) { |
||
| 425 | for ($j = 0; $j < $m->col; ++$j) { |
||
| 426 | $vr->data[$i * $m->col + $j] = $this->data[$j] + $m->data[$i * $m->col + $j]; |
||
| 427 | } |
||
| 428 | } |
||
| 429 | return $vr; |
||
| 430 | } |
||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * |
||
| 435 | * @param \Np\vector $vector |
||
| 436 | * @return vector |
||
| 437 | */ |
||
| 438 | protected function addVector(\Np\vector $vector): vector { |
||
| 445 | } |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * |
||
| 450 | * @param int|float $s |
||
| 451 | * @return vector |
||
| 452 | */ |
||
| 453 | protected function addScalar(int|float $s): vector { |
||
| 454 | $vr = $this->copy(); |
||
| 455 | for ($i = 0; $i < $this->col; ++$i) { |
||
| 456 | $vr->data[$i] += $s; |
||
| 457 | } |
||
| 458 | return $vr; |
||
| 459 | } |
||
| 460 | |||
| 461 | /** |
||
| 462 | * |
||
| 463 | * @param int|float|\Np\matrix|\Np\vector $d |
||
| 464 | * @return matrix|vector |
||
| 465 | */ |
||
| 466 | public function pow(int|float|\Np\matrix|\Np\vector $d): matrix|vector { |
||
| 467 | if ($d instanceof matrix) { |
||
| 468 | return $this->powMatrix($d); |
||
| 469 | } elseif ($d instanceof vector) { |
||
| 470 | return $this->powVector($d); |
||
| 471 | } else { |
||
| 472 | return $this->powScalar($d); |
||
| 473 | } |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * |
||
| 478 | * @param \Np\matrix $m |
||
| 479 | * @return matrix |
||
| 480 | */ |
||
| 481 | protected function powMatrix(\Np\matrix $m): matrix { |
||
| 482 | if ($this->checkDimensions($this, $m) && $this->checkDtype($this, $m)) { |
||
| 483 | $ar = matrix::factory($m->row, $m->col, $this->dtype); |
||
| 484 | for ($i = 0; $i < $m->row; ++$i) { |
||
| 485 | for ($j = 0; $j < $m->col; ++$j) { |
||
| 486 | $ar->data[$i * $m->col + $j] = $m->data[$i * $m->col + $j] ** $this->data[$j]; |
||
| 487 | } |
||
| 488 | } |
||
| 489 | return $ar; |
||
| 490 | } |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * |
||
| 495 | * @param \Np\vector $vector |
||
| 496 | * @return vector |
||
| 497 | */ |
||
| 498 | protected function powVector(\Np\vector $vector): vector { |
||
| 505 | } |
||
| 506 | } |
||
| 507 | |||
| 508 | /** |
||
| 509 | * |
||
| 510 | * @param int|float $s |
||
| 511 | * @return vector |
||
| 512 | */ |
||
| 513 | protected function powScalar(int|float $s): vector { |
||
| 514 | $v = $this->copy(); |
||
| 515 | for ($i = 0; $i < $this->col; ++$i) { |
||
| 516 | $v->data[$i] = $v->data[$i] ** $s; |
||
| 517 | } |
||
| 518 | return $v; |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * |
||
| 523 | * @param int|float|\Np\matrix|\Np\vector $d |
||
| 524 | * @return matrix|vector |
||
| 525 | */ |
||
| 526 | public function mod(int|float|\Np\matrix|\Np\vector $d): matrix|vector { |
||
| 527 | if ($d instanceof matrix) { |
||
| 528 | return $this->powMatrix($d); |
||
| 529 | } elseif ($d instanceof vector) { |
||
| 530 | return $this->powVector($d); |
||
| 531 | } else { |
||
| 532 | return $this->powScalar($d); |
||
| 533 | } |
||
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * |
||
| 538 | * @param \Np\matrix $m |
||
| 539 | * @return matrix |
||
| 540 | */ |
||
| 541 | protected function modMatrix(\Np\matrix $m): matrix { |
||
| 542 | if ($this->checkDimensions($this, $m) && $this->checkDtype($this, $m)) { |
||
| 543 | $ar = matrix::factory($m->row, $m->col, $this->dtype); |
||
| 544 | for ($i = 0; $i < $m->row; ++$i) { |
||
| 545 | for ($j = 0; $j < $m->col; ++$j) { |
||
| 546 | $ar->data[$i * $m->col + $j] = $m->data[$i * $m->col + $j] % $this->data[$j]; |
||
| 547 | } |
||
| 548 | } |
||
| 549 | return $ar; |
||
| 550 | } |
||
| 551 | } |
||
| 552 | |||
| 553 | /** |
||
| 554 | * |
||
| 555 | * @param \Np\vector $vector |
||
| 556 | * @return vector |
||
| 557 | */ |
||
| 558 | protected function modVector(\Np\vector $vector): vector { |
||
| 559 | if ($this->checkShape($this, $vector) && $this->checkDtype($this, $vector)) { |
||
| 560 | $vr = self::factory($this->col, $this->dtype); |
||
| 561 | for ($i = 0; $i < $this->col; ++$i) { |
||
| 562 | $vr->data[$i] = $this->data[$i] % $vector->data[$i]; |
||
| 563 | } |
||
| 564 | return $vr; |
||
| 565 | } |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * |
||
| 570 | * @param int|float $s |
||
| 571 | */ |
||
| 572 | protected function modScalar(int|float $s) { |
||
| 573 | $v = $this->copy(); |
||
| 574 | for ($i = 0; $i < $this->col; ++$i) { |
||
| 575 | $v->data[$i] = $v->data[$i] % $s; |
||
| 576 | } |
||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * |
||
| 581 | * @param int|float|matrix|vector $d |
||
| 582 | * @return matrix|vector |
||
| 583 | */ |
||
| 584 | public function subtract(int|float|matrix|vector $d): matrix|vector { |
||
| 585 | if ($d instanceof matrix) { |
||
| 586 | return $this->subtractMatrix($d); |
||
| 587 | } elseif ($d instanceof self) { |
||
| 588 | return $this->subtractVector($d); |
||
| 589 | } else { |
||
| 590 | return $this->substractScalar($d); |
||
| 591 | } |
||
| 592 | } |
||
| 593 | |||
| 594 | /** |
||
| 595 | * |
||
| 596 | * @param \Np\matrix $m |
||
| 597 | * @return matrix |
||
| 598 | */ |
||
| 599 | protected function subtractMatrix(\Np\matrix $m): matrix { |
||
| 600 | if ($this->checkShape($this, $m) && $this->checkDtype($this, $m)) { |
||
| 601 | $vr = matrix::factory($m->row, $m->col, $m->dtype); |
||
| 602 | for ($i = 0; $i < $m->row; ++$i) { |
||
| 603 | for ($j = 0; $j < $m->col; ++$j) { |
||
| 604 | $vr->data[$i * $m->col + $j] = $this->data[$j] - $m->data[$i * $m->col + $j]; |
||
| 605 | } |
||
| 606 | } |
||
| 607 | return $vr; |
||
| 608 | } |
||
| 609 | } |
||
| 610 | |||
| 611 | /** |
||
| 612 | * |
||
| 613 | * @param \Np\vector $vector |
||
| 614 | * @return vector |
||
| 615 | */ |
||
| 616 | protected function subtractVector(\Np\vector $vector): vector { |
||
| 617 | if ($this->checkShape($this, $vector) && $this->checkDtype($this, $vector)) { |
||
| 618 | $vr = self::factory($this->col, $this->dtype); |
||
| 619 | for ($i = 0; $i < $this->col; ++$i) { |
||
| 620 | $vr->data[$i] = $this->data[$i] - $vector->data[$i]; |
||
| 621 | } |
||
| 622 | return $vr; |
||
| 623 | } |
||
| 624 | } |
||
| 625 | |||
| 626 | /** |
||
| 627 | * |
||
| 628 | * @param \Np\vector $scalar |
||
| 629 | * @return \Np\vector |
||
| 630 | */ |
||
| 631 | protected function substractScalar(int|float $scalar): vector { |
||
| 632 | $vr = self::factory($this->col, $this->dtype); |
||
| 633 | for ($i = 0; $i < $this->col; ++$i) { |
||
| 634 | $vr->data[$i] = $this->data[$i] - $scalar; |
||
| 635 | } |
||
| 636 | return $vr; |
||
| 637 | } |
||
| 638 | |||
| 639 | /** |
||
| 640 | * |
||
| 641 | * @param \Np\vector $v |
||
| 642 | * @param int $stride |
||
| 643 | * @return vector |
||
| 644 | */ |
||
| 645 | public function convolve(\Np\vector $v, int $stride = 1): vector { |
||
| 647 | } |
||
| 648 | |||
| 649 | public function max() { |
||
| 651 | } |
||
| 652 | |||
| 653 | public function min() { |
||
| 654 | $this->data[blas::min($this)]; |
||
| 655 | } |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Return the inner product of two vectors. |
||
| 659 | * |
||
| 660 | * @param \Np\vector $vector |
||
| 661 | * |
||
| 662 | */ |
||
| 663 | public function inner(\Np\vector $vector) { |
||
| 664 | return $this->dotVector($vector); |
||
| 665 | } |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Calculate the L1 norm of the vector. |
||
| 669 | * @return float |
||
| 670 | */ |
||
| 671 | public function normL1(): float { |
||
| 673 | } |
||
| 674 | |||
| 675 | public function normL2() { |
||
| 676 | return sqrt($this->square()->sum()); |
||
| 677 | } |
||
| 678 | |||
| 679 | public function normMax() { |
||
| 681 | } |
||
| 682 | |||
| 683 | public function normP(float $p = 2.5) { |
||
| 688 | } |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Return the reciprocal of the vector element-wise. |
||
| 692 | * |
||
| 693 | * @return self |
||
| 694 | */ |
||
| 695 | public function reciprocal(): vector { |
||
| 696 | return self::ones($this->col, $this->dtype) |
||
| 697 | ->divideVector($this); |
||
| 698 | } |
||
| 699 | |||
| 700 | /** |
||
| 701 | * |
||
| 702 | * @return int|float |
||
| 703 | */ |
||
| 704 | public function mean():int|float { |
||
| 705 | return $this->sum()/ $this->col; |
||
| 706 | } |
||
| 707 | |||
| 708 | /** |
||
| 709 | * |
||
| 710 | * @return int|float |
||
| 711 | */ |
||
| 712 | public function median():int|float { |
||
| 713 | $mid = intdiv($this->col, 2); |
||
| 714 | |||
| 715 | $a = $this->copy()->sort(); |
||
| 716 | if ($this->col % 2 === 1) { |
||
| 717 | $median = $a->data[$mid]; |
||
| 718 | } else { |
||
| 719 | $median = ($a->data[$mid - 1] + $a->data[$mid]) / 2.; |
||
| 720 | } |
||
| 721 | return $median; |
||
| 722 | } |
||
| 723 | |||
| 724 | public function variance($mean = null) |
||
| 735 | } |
||
| 736 | |||
| 737 | /** |
||
| 738 | * |
||
| 739 | * @return vector |
||
| 740 | */ |
||
| 741 | public function square(): vector { |
||
| 742 | return $this->multiplyVector($this); |
||
| 743 | } |
||
| 744 | |||
| 745 | public function pop(): mixed { |
||
| 746 | $ar = $this->asArray(); |
||
| 747 | \FFI::free($this->data); |
||
| 748 | $val = array_shift($ar); |
||
| 749 | $v = self::ar($ar); |
||
| 750 | $this->col = $v->col; |
||
| 751 | $this->ndim = $v->ndim; |
||
| 752 | $this->data = $v->data; |
||
| 753 | unset($v); |
||
| 754 | unset($ar); |
||
| 755 | return $val; |
||
| 756 | } |
||
| 757 | |||
| 758 | /** |
||
| 759 | * sort the vector |
||
| 760 | * @param string $type i or d |
||
| 761 | * |
||
| 762 | */ |
||
| 763 | public function sort($type = 'i') { |
||
| 764 | lapack::sort($this, $type); |
||
| 765 | return $this; |
||
| 766 | } |
||
| 767 | |||
| 768 | /** |
||
| 769 | * set data to vector |
||
| 770 | * @param int|float|array $data |
||
| 771 | */ |
||
| 772 | public function setData(int|float|array $data) { |
||
| 773 | if (is_array($data) && !is_array($data[0])) { |
||
| 774 | for ($i = 0; $i < $this->col; ++$i) { |
||
| 775 | $this->data[$i] = $data[$i]; |
||
| 776 | } |
||
| 777 | } elseif (is_numeric($data)) { |
||
| 778 | for ($i = 0; $i < $this->col; ++$i) { |
||
| 779 | $this->data[$i] = $data; |
||
| 780 | } |
||
| 781 | } |
||
| 782 | } |
||
| 783 | |||
| 784 | /** |
||
| 785 | * get the size of vector |
||
| 786 | * @return int |
||
| 787 | */ |
||
| 788 | public function getSize(): int { |
||
| 789 | return $this->col; |
||
| 790 | } |
||
| 791 | |||
| 792 | public function getDtype() { |
||
| 793 | return $this->dtype; |
||
| 794 | } |
||
| 795 | |||
| 796 | public function asArray() { |
||
| 802 | } |
||
| 803 | |||
| 804 | public function printVector() { |
||
| 810 | } |
||
| 811 | |||
| 812 | public function __toString() { |
||
| 814 | } |
||
| 815 | |||
| 816 | protected function __construct(public int $col, int $dtype = self::FLOAT) { |
||
| 822 | } |
||
| 823 | |||
| 824 | } |
||
| 825 |