@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | * @email [email protected] |
| 23 | 23 | * @copyright (c) 2020-2021, Shubham Chaudhary |
| 24 | 24 | */ |
| 25 | -class matrix extends nd{ |
|
| 25 | +class matrix extends nd { |
|
| 26 | 26 | |
| 27 | 27 | public $row, $col; |
| 28 | 28 | /** |
@@ -342,7 +342,7 @@ discard block |
||
| 342 | 342 | * @param \Np\matrix|\Np\vector $d |
| 343 | 343 | * @return matrix|vector |
| 344 | 344 | */ |
| 345 | - public function dot(matrix|vector $d): matrix|vector { |
|
| 345 | + public function dot(matrix | vector $d): matrix | vector { |
|
| 346 | 346 | if ($d instanceof self) { |
| 347 | 347 | return $this->dotMatrix($d); |
| 348 | 348 | } else { |
@@ -388,7 +388,7 @@ discard block |
||
| 388 | 388 | * @param int|float|matrix|vector $m |
| 389 | 389 | * @return matrix|vector |
| 390 | 390 | */ |
| 391 | - public function multiply(int|float|matrix|vector $m): matrix|vector { |
|
| 391 | + public function multiply(int | float | matrix | vector $m): matrix | vector { |
|
| 392 | 392 | if ($m instanceof self) { |
| 393 | 393 | return $this->multiplyMatrix($m); |
| 394 | 394 | } else if ($m instanceof vector) { |
@@ -437,7 +437,7 @@ discard block |
||
| 437 | 437 | * @param int|float $scalar |
| 438 | 438 | * @return matrix |
| 439 | 439 | */ |
| 440 | - public function scale(int|float $scalar): matrix { |
|
| 440 | + public function scale(int | float $scalar): matrix { |
|
| 441 | 441 | if ($scalar == 0) { |
| 442 | 442 | return self::zeros($this->row, $this->col, $this->dtype); |
| 443 | 443 | } |
@@ -472,7 +472,7 @@ discard block |
||
| 472 | 472 | * @param int|float|matrix|vector $m |
| 473 | 473 | * @return matrix |
| 474 | 474 | */ |
| 475 | - public function sum(int|float|matrix|vector $m): matrix { |
|
| 475 | + public function sum(int | float | matrix | vector $m): matrix { |
|
| 476 | 476 | if ($m instanceof self) { |
| 477 | 477 | return $this->sumMatrix($m); |
| 478 | 478 | } elseif ($m instanceof vector) { |
@@ -482,7 +482,7 @@ discard block |
||
| 482 | 482 | } |
| 483 | 483 | } |
| 484 | 484 | |
| 485 | - protected function sumScalar(int|float $s): matrix { |
|
| 485 | + protected function sumScalar(int | float $s): matrix { |
|
| 486 | 486 | $ar = self::factory($this->row, $this->col, $this->dtype); |
| 487 | 487 | for ($i = 0; $i < $this->ndim; ++$i) { |
| 488 | 488 | $ar->data[$i] = $this->data[$i] + $s; |
@@ -517,7 +517,7 @@ discard block |
||
| 517 | 517 | * @param int|float|matrix|vector $d matrix|$scalar to subtract this matrix |
| 518 | 518 | * @return \Np\matrix |
| 519 | 519 | */ |
| 520 | - public function subtract(int|float|matrix|vector $d): matrix { |
|
| 520 | + public function subtract(int | float | matrix | vector $d): matrix { |
|
| 521 | 521 | if ($d instanceof self) { |
| 522 | 522 | return $this->subtractMatrix($d); |
| 523 | 523 | } elseif ($d instanceof vector) { |
@@ -527,7 +527,7 @@ discard block |
||
| 527 | 527 | } |
| 528 | 528 | } |
| 529 | 529 | |
| 530 | - protected function subtractScalar(int|float $s): matrix { |
|
| 530 | + protected function subtractScalar(int | float $s): matrix { |
|
| 531 | 531 | $ar = self::factory($this->row, $this->col, $this->dtype); |
| 532 | 532 | for ($i = 0; $i < $this->ndim; ++$i) { |
| 533 | 533 | $ar->data[$i] = $this->data[$i] - $s; |
@@ -589,7 +589,7 @@ discard block |
||
| 589 | 589 | * @param int|float|matrix $d |
| 590 | 590 | * @return matrix |
| 591 | 591 | */ |
| 592 | - public function divide(int|float|matrix|vector $d): matrix { |
|
| 592 | + public function divide(int | float | matrix | vector $d): matrix { |
|
| 593 | 593 | if ($d instanceof self) { |
| 594 | 594 | return $this->divideMatrix($d); |
| 595 | 595 | } elseif ($d instanceof vector) { |
@@ -625,7 +625,7 @@ discard block |
||
| 625 | 625 | } |
| 626 | 626 | } |
| 627 | 627 | |
| 628 | - protected function divideScalar(int|float $s): matrix { |
|
| 628 | + protected function divideScalar(int | float $s): matrix { |
|
| 629 | 629 | $ar = self::factory($this->row, $this->col, $this->dtype); |
| 630 | 630 | for ($i = 0; $i < $this->ndim; ++$i) { |
| 631 | 631 | $ar->data[$i] = $this->data[$i] / $s; |
@@ -640,7 +640,7 @@ discard block |
||
| 640 | 640 | * @param int|float|matrix $m |
| 641 | 641 | * @return matrix |
| 642 | 642 | */ |
| 643 | - public function pow(int|float|matrix|vector $d): matrix { |
|
| 643 | + public function pow(int | float | matrix | vector $d): matrix { |
|
| 644 | 644 | if ($d instanceof self) { |
| 645 | 645 | return $this->powMatrix($d); |
| 646 | 646 | } else if ($d instanceof vector) { |
@@ -672,7 +672,7 @@ discard block |
||
| 672 | 672 | } |
| 673 | 673 | } |
| 674 | 674 | |
| 675 | - protected function powScalar(int|float $s): matrix { |
|
| 675 | + protected function powScalar(int | float $s): matrix { |
|
| 676 | 676 | $ar = $this->copyMatrix(); |
| 677 | 677 | for ($i = 0; $i < $this->ndim; ++$i) { |
| 678 | 678 | $ar->data[$i] **= $s; |
@@ -685,7 +685,7 @@ discard block |
||
| 685 | 685 | * @param int|float|matrix|vector $d |
| 686 | 686 | * @return matrix |
| 687 | 687 | */ |
| 688 | - public function mod(int|float|matrix|vector $d): matrix { |
|
| 688 | + public function mod(int | float | matrix | vector $d): matrix { |
|
| 689 | 689 | if ($d instanceof self) { |
| 690 | 690 | $this->modMatrix($d); |
| 691 | 691 | } else if ($d instanceof vector) { |
@@ -719,7 +719,7 @@ discard block |
||
| 719 | 719 | } |
| 720 | 720 | } |
| 721 | 721 | |
| 722 | - protected function modScalar(int|float $s): matrix { |
|
| 722 | + protected function modScalar(int | float $s): matrix { |
|
| 723 | 723 | $ar = $this->copyMatrix(); |
| 724 | 724 | for ($i = 0; $i < $this->ndim; ++$i) { |
| 725 | 725 | $ar->data[$i] %= $s; |
@@ -944,7 +944,7 @@ discard block |
||
| 944 | 944 | * |
| 945 | 945 | * @return matrix|null |
| 946 | 946 | */ |
| 947 | - public function ref(): matrix|null { |
|
| 947 | + public function ref(): matrix | null { |
|
| 948 | 948 | return ref::factory($this); |
| 949 | 949 | } |
| 950 | 950 | |
@@ -953,7 +953,7 @@ discard block |
||
| 953 | 953 | * |
| 954 | 954 | * @return matrix|null |
| 955 | 955 | */ |
| 956 | - public function cholesky(): matrix|null { |
|
| 956 | + public function cholesky(): matrix | null { |
|
| 957 | 957 | return cholesky::factory($this); |
| 958 | 958 | } |
| 959 | 959 | |
@@ -1036,7 +1036,7 @@ discard block |
||
| 1036 | 1036 | * @param bool $dignoal |
| 1037 | 1037 | * @return void |
| 1038 | 1038 | */ |
| 1039 | - public function setData(int|float|array $data, bool $dignoal = false): void { |
|
| 1039 | + public function setData(int | float | array $data, bool $dignoal = false): void { |
|
| 1040 | 1040 | if ($dignoal == false) { |
| 1041 | 1041 | if (is_array($data) && is_array($data[0])) { |
| 1042 | 1042 | $f = $this->flattenArray($data); |
@@ -1060,7 +1060,7 @@ discard block |
||
| 1060 | 1060 | * @return object |
| 1061 | 1061 | */ |
| 1062 | 1062 | public function getShape(): object { |
| 1063 | - return (object) ['m' => $this->row, 'n' => $this->col]; |
|
| 1063 | + return (object)['m' => $this->row, 'n' => $this->col]; |
|
| 1064 | 1064 | } |
| 1065 | 1065 | |
| 1066 | 1066 | /** |
@@ -1192,7 +1192,7 @@ discard block |
||
| 1192 | 1192 | * Compute the (Moore-Penrose) pseudo inverse of the general matrix. |
| 1193 | 1193 | * @return matrix|null |
| 1194 | 1194 | */ |
| 1195 | - public function pseudoInverse(): matrix|null { |
|
| 1195 | + public function pseudoInverse(): matrix | null { |
|
| 1196 | 1196 | $k = min($this->row, $this->col); |
| 1197 | 1197 | $s = vector::factory($k, $this->dtype); |
| 1198 | 1198 | $u = self::factory($this->row, $this->row, $this->dtype); |
@@ -1376,7 +1376,7 @@ discard block |
||
| 1376 | 1376 | * @param vector|null $mean |
| 1377 | 1377 | * @return vector |
| 1378 | 1378 | */ |
| 1379 | - public function variance(vector|null $mean = null): vector { |
|
| 1379 | + public function variance(vector | null $mean = null): vector { |
|
| 1380 | 1380 | if (isset($mean)) { |
| 1381 | 1381 | if (!$mean instanceof vector) { |
| 1382 | 1382 | self::_invalidArgument('mean must be a vector!'); |
@@ -1418,7 +1418,7 @@ discard block |
||
| 1418 | 1418 | * @param vector|null $mean |
| 1419 | 1419 | * @return matrix |
| 1420 | 1420 | */ |
| 1421 | - public function covariance(vector|null $mean = null): matrix { |
|
| 1421 | + public function covariance(vector | null $mean = null): matrix { |
|
| 1422 | 1422 | if (isset($mean)) { |
| 1423 | 1423 | if ($mean->col !== $this->row) { |
| 1424 | 1424 | self::_err('Err:: given mean vector dimensionality mismatched!'); |
@@ -1505,7 +1505,7 @@ discard block |
||
| 1505 | 1505 | * @param int|float|matrix|vector $d |
| 1506 | 1506 | * @return matrix |
| 1507 | 1507 | */ |
| 1508 | - public function equal(int|float|matrix|vector $d): matrix { |
|
| 1508 | + public function equal(int | float | matrix | vector $d): matrix { |
|
| 1509 | 1509 | if ($d instanceof self) { |
| 1510 | 1510 | return $this->equalMatrix($d); |
| 1511 | 1511 | } elseif ($d instanceof vector) { |
@@ -1537,7 +1537,7 @@ discard block |
||
| 1537 | 1537 | } |
| 1538 | 1538 | } |
| 1539 | 1539 | |
| 1540 | - protected function equalScalar(int|float $s): matrix { |
|
| 1540 | + protected function equalScalar(int | float $s): matrix { |
|
| 1541 | 1541 | $ar = self::factory($this->row, $this->col, $this->dtype); |
| 1542 | 1542 | for ($i = 0; $i < $this->ndim; ++$i) { |
| 1543 | 1543 | $ar->data[$i] = $this->data[$i] == $s ? 1 : 0; |
@@ -1550,7 +1550,7 @@ discard block |
||
| 1550 | 1550 | * @param int|float|matrix|vector $d |
| 1551 | 1551 | * @return matrix |
| 1552 | 1552 | */ |
| 1553 | - public function greater(int|float|matrix|vector $d): matrix { |
|
| 1553 | + public function greater(int | float | matrix | vector $d): matrix { |
|
| 1554 | 1554 | if ($d instanceof self) { |
| 1555 | 1555 | return $this->greaterMatrix($d); |
| 1556 | 1556 | } elseif ($d instanceof vector) { |
@@ -1582,7 +1582,7 @@ discard block |
||
| 1582 | 1582 | } |
| 1583 | 1583 | } |
| 1584 | 1584 | |
| 1585 | - protected function greaterScalar(int|float $s): matrix { |
|
| 1585 | + protected function greaterScalar(int | float $s): matrix { |
|
| 1586 | 1586 | $ar = self::factory($this->row, $this->col, $this->dtype); |
| 1587 | 1587 | for ($i = 0; $i < $this->ndim; ++$i) { |
| 1588 | 1588 | $ar->data[$i] = $this->data[$i] > $s ? 1 : 0; |
@@ -1595,7 +1595,7 @@ discard block |
||
| 1595 | 1595 | * @param int|float|matrix $m |
| 1596 | 1596 | * @return matrix |
| 1597 | 1597 | */ |
| 1598 | - public function less(int|float|matrix $m): matrix { |
|
| 1598 | + public function less(int | float | matrix $m): matrix { |
|
| 1599 | 1599 | $ar = self::factory($this->row, $this->col, $this->dtype); |
| 1600 | 1600 | if ($m instanceof self) { |
| 1601 | 1601 | if ($this->checkShape($m)) { |
@@ -1646,7 +1646,7 @@ discard block |
||
| 1646 | 1646 | } |
| 1647 | 1647 | |
| 1648 | 1648 | public function __toString() { |
| 1649 | - return (string) $this->printMatrix(); |
|
| 1649 | + return (string)$this->printMatrix(); |
|
| 1650 | 1650 | } |
| 1651 | 1651 | |
| 1652 | 1652 | protected function flattenArray(array $ar) { |
@@ -1686,7 +1686,7 @@ discard block |
||
| 1686 | 1686 | if ($row < 1 || $col < 1) { |
| 1687 | 1687 | throw new invalidArgumentException('* To create Numphp/Matrix row & col must be greater than 0!, Op Failed! * '); |
| 1688 | 1688 | } |
| 1689 | - parent::__construct($row*$col, $dtype); |
|
| 1689 | + parent::__construct($row * $col, $dtype); |
|
| 1690 | 1690 | $this->row = $row; |
| 1691 | 1691 | $this->col = $col; |
| 1692 | 1692 | return $this; |
@@ -135,7 +135,7 @@ discard block |
||
| 135 | 135 | * @param int $dtype |
| 136 | 136 | * @return vector |
| 137 | 137 | */ |
| 138 | - public static function full(int $col, int|float $val, int $dtype = self::FLOAT): vector { |
|
| 138 | + public static function full(int $col, int | float $val, int $dtype = self::FLOAT): vector { |
|
| 139 | 139 | $ar = self::factory($col, $dtype); |
| 140 | 140 | for ($i = 0; $i < $col; ++$i) { |
| 141 | 141 | $ar->data[$i] = $val; |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | * @param int $dtype |
| 153 | 153 | * @return vector |
| 154 | 154 | */ |
| 155 | - public static function range(int|float $start, int|float $end, int|float $interval = 1, int $dtype = self::FLOAT): vector { |
|
| 155 | + public static function range(int | float $start, int | float $end, int | float $interval = 1, int $dtype = self::FLOAT): vector { |
|
| 156 | 156 | return self::ar(range($start, $end, $interval)); |
| 157 | 157 | } |
| 158 | 158 | |
@@ -247,8 +247,8 @@ discard block |
||
| 247 | 247 | public function maximum(\Np\vector $vector): vector { |
| 248 | 248 | if ($this->checkDimensions($vector) && $this->checkDtype($vector)) { |
| 249 | 249 | $v = new self($this->ndim, $this->dtype); |
| 250 | - for($i = 0; $i<$v->ndim; ++$i) { |
|
| 251 | - $v->data[$i] = max($this->data[$i],$vector->data[$i]); |
|
| 250 | + for ($i = 0; $i < $v->ndim; ++$i) { |
|
| 251 | + $v->data[$i] = max($this->data[$i], $vector->data[$i]); |
|
| 252 | 252 | } |
| 253 | 253 | return $v; |
| 254 | 254 | } |
@@ -263,8 +263,8 @@ discard block |
||
| 263 | 263 | public function minium(\Np\vector $vector): vector { |
| 264 | 264 | if ($this->checkDimensions($vector) && $this->checkDtype($vector)) { |
| 265 | 265 | $v = new self($this->ndim, $this->dtype); |
| 266 | - for($i = 0; $i<$v->ndim; ++$i) { |
|
| 267 | - $v->data[$i] = min($this->data[$i],$vector->data[$i]); |
|
| 266 | + for ($i = 0; $i < $v->ndim; ++$i) { |
|
| 267 | + $v->data[$i] = min($this->data[$i], $vector->data[$i]); |
|
| 268 | 268 | } |
| 269 | 269 | return $v; |
| 270 | 270 | } |
@@ -340,7 +340,7 @@ discard block |
||
| 340 | 340 | * @param int|float|matrix|vector $d |
| 341 | 341 | * @return matrix|vector |
| 342 | 342 | */ |
| 343 | - public function divide(int|float|matrix|vector $d): matrix|vector { |
|
| 343 | + public function divide(int | float | matrix | vector $d): matrix | vector { |
|
| 344 | 344 | if ($d instanceof matrix) { |
| 345 | 345 | return $this->divideMatrix($d); |
| 346 | 346 | } elseif ($d instanceof self) { |
@@ -387,7 +387,7 @@ discard block |
||
| 387 | 387 | * @param int|float $s |
| 388 | 388 | * @return vector |
| 389 | 389 | */ |
| 390 | - protected function divideScalar(int|float $s): vector { |
|
| 390 | + protected function divideScalar(int | float $s): vector { |
|
| 391 | 391 | $vr = self::factory($this->col, $this->dtype); |
| 392 | 392 | for ($i = 0; $i < $this->col; ++$i) { |
| 393 | 393 | $vr->data[$i] = $this->data[$i] / $s; |
@@ -400,7 +400,7 @@ discard block |
||
| 400 | 400 | * @param int|float|matrix|vector $d |
| 401 | 401 | * @return matrix|vector |
| 402 | 402 | */ |
| 403 | - public function multiply(int|float|matrix|vector $d): matrix|vector { |
|
| 403 | + public function multiply(int | float | matrix | vector $d): matrix | vector { |
|
| 404 | 404 | if ($d instanceof matrix) { |
| 405 | 405 | return $this->multiplyMatrix($d); |
| 406 | 406 | } elseif ($d instanceof self) { |
@@ -447,7 +447,7 @@ discard block |
||
| 447 | 447 | * @param int|float $s |
| 448 | 448 | * @return vector |
| 449 | 449 | */ |
| 450 | - protected function multiplyScalar(int|float $s): vector { |
|
| 450 | + protected function multiplyScalar(int | float $s): vector { |
|
| 451 | 451 | $vr = $this->copyVector(); |
| 452 | 452 | blas::scale($s, $vr); |
| 453 | 453 | return $vr; |
@@ -458,7 +458,7 @@ discard block |
||
| 458 | 458 | * @param int|float|matrix|vector $d |
| 459 | 459 | * @return matrix|vector |
| 460 | 460 | */ |
| 461 | - public function add(int|float|matrix|vector $d): matrix|vector { |
|
| 461 | + public function add(int | float | matrix | vector $d): matrix | vector { |
|
| 462 | 462 | if ($d instanceof matrix) { |
| 463 | 463 | return $this->addMatrix($d); |
| 464 | 464 | } elseif ($d instanceof self) { |
@@ -506,7 +506,7 @@ discard block |
||
| 506 | 506 | * @param int|float $s |
| 507 | 507 | * @return vector |
| 508 | 508 | */ |
| 509 | - protected function addScalar(int|float $s): vector { |
|
| 509 | + protected function addScalar(int | float $s): vector { |
|
| 510 | 510 | $vr = $this->copyVector(); |
| 511 | 511 | for ($i = 0; $i < $this->col; ++$i) { |
| 512 | 512 | $vr->data[$i] += $s; |
@@ -549,7 +549,7 @@ discard block |
||
| 549 | 549 | * @param int|float|matrix|vector $d |
| 550 | 550 | * @return matrix|vector |
| 551 | 551 | */ |
| 552 | - public function subtract(int|float|matrix|vector $d): matrix|vector { |
|
| 552 | + public function subtract(int | float | matrix | vector $d): matrix | vector { |
|
| 553 | 553 | if ($d instanceof matrix) { |
| 554 | 554 | return $this->subtractMatrix($d); |
| 555 | 555 | } elseif ($d instanceof self) { |
@@ -597,7 +597,7 @@ discard block |
||
| 597 | 597 | * @param \Np\vector $scalar |
| 598 | 598 | * @return \Np\vector |
| 599 | 599 | */ |
| 600 | - protected function substractScalar(int|float $scalar): vector { |
|
| 600 | + protected function substractScalar(int | float $scalar): vector { |
|
| 601 | 601 | $vr = self::factory($this->col, $this->dtype); |
| 602 | 602 | for ($i = 0; $i < $this->col; ++$i) { |
| 603 | 603 | $vr->data[$i] = $this->data[$i] - $scalar; |
@@ -647,7 +647,7 @@ discard block |
||
| 647 | 647 | * set data to vector |
| 648 | 648 | * @param int|float|array $data |
| 649 | 649 | */ |
| 650 | - public function setData(int|float|array $data) { |
|
| 650 | + public function setData(int | float | array $data) { |
|
| 651 | 651 | if (is_array($data) && !is_array($data[0])) { |
| 652 | 652 | for ($i = 0; $i < $this->col; ++$i) { |
| 653 | 653 | $this->data[$i] = $data[$i]; |
@@ -660,7 +660,7 @@ discard block |
||
| 660 | 660 | } |
| 661 | 661 | |
| 662 | 662 | public function asMatrix(): matrix { |
| 663 | - $size = (int) sqrt($this->col); |
|
| 663 | + $size = (int)sqrt($this->col); |
|
| 664 | 664 | $ar = matrix::factory($size, $size, $this->dtype); |
| 665 | 665 | for ($i = 0; $i < $ar->ndim; ++$i) { |
| 666 | 666 | $ar->data[$i] = $this->data[$i]; |
@@ -696,7 +696,7 @@ discard block |
||
| 696 | 696 | } |
| 697 | 697 | |
| 698 | 698 | public function __toString() { |
| 699 | - return (string) $this->printVector(); |
|
| 699 | + return (string)$this->printVector(); |
|
| 700 | 700 | } |
| 701 | 701 | |
| 702 | 702 | protected function checkDimensions(vector $vector) { |
@@ -8,6 +8,6 @@ |
||
| 8 | 8 | * |
| 9 | 9 | * @author ghost |
| 10 | 10 | */ |
| 11 | -class dtypeException extends invalidArgumentException{ |
|
| 11 | +class dtypeException extends invalidArgumentException { |
|
| 12 | 12 | //put your code here |
| 13 | 13 | } |
@@ -5,7 +5,7 @@ |
||
| 5 | 5 | use Np\vector; |
| 6 | 6 | vector::time(); |
| 7 | 7 | vector::getMemory(); |
| 8 | -$v = vector::ar(range(random_int(1,2), random_int(99999,9999999))); |
|
| 8 | +$v = vector::ar(range(random_int(1, 2), random_int(99999, 9999999))); |
|
| 9 | 9 | |
| 10 | 10 | echo $v->sum() . PHP_EOL; |
| 11 | 11 | vector::getMemory(); |