@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare (strict_types=1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Np\linAlgb\decompositions; |
| 6 | 6 | |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare (strict_types=1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Np\linAlgb\decompositions; |
| 6 | 6 | |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | * @return matrix|null |
| 28 | 28 | * |
| 29 | 29 | */ |
| 30 | - public static function factory(matrix $m): matrix|null { |
|
| 30 | + public static function factory(matrix $m): matrix | null { |
|
| 31 | 31 | if ($m->isSquare()) { |
| 32 | 32 | $ar = $m->copy(); |
| 33 | 33 | $lp = lapack::potrf($ar); |
@@ -2,7 +2,7 @@ discard block |
||
| 2 | 2 | declare(strict_types=1); |
| 3 | 3 | namespace Np\linAlgb; |
| 4 | 4 | |
| 5 | -use Np\core\{blas,lapack}; |
|
| 5 | +use Np\core\{blas, lapack}; |
|
| 6 | 6 | use Np\matrix, Np\vector; |
| 7 | 7 | /** |
| 8 | 8 | * Linear Algebra |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * @param \Np\matrix|\Np\vector $d |
| 25 | 25 | * @return matrix|vector |
| 26 | 26 | */ |
| 27 | - public function dot(matrix|vector $d): matrix|vector { |
|
| 27 | + public function dot(matrix | vector $d): matrix | vector { |
|
| 28 | 28 | if ($this instanceof matrix) { |
| 29 | 29 | if ($d instanceof matrix) { |
| 30 | 30 | return $this->dotMatrix($d); |
@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | * @return \Np\matrix |
| 41 | 41 | */ |
| 42 | 42 | protected function dotMatrix(matrix $matrix): matrix { |
| 43 | - if ($this->checkDimensions($this,$matrix)) { |
|
| 43 | + if ($this->checkDimensions($this, $matrix)) { |
|
| 44 | 44 | $ar = self::factory($this->row, $matrix->col); |
| 45 | 45 | blas::gemm($this, $matrix, $ar); |
| 46 | 46 | return $ar; |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | * Compute the multiplicative inverse of the matrix. |
| 66 | 66 | * @return matrix|null |
| 67 | 67 | */ |
| 68 | - public function inverse(): matrix|null { |
|
| 68 | + public function inverse(): matrix | null { |
|
| 69 | 69 | if ($this->isSquare()) { |
| 70 | 70 | $imat = $this->copy(); |
| 71 | 71 | $ipiv = vector::factory($this->row, vector::INT); |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | * Compute the (Moore-Penrose) pseudo inverse of the general matrix. |
| 90 | 90 | * @return matrix|null |
| 91 | 91 | */ |
| 92 | - public function pseudoInverse(): matrix|null { |
|
| 92 | + public function pseudoInverse(): matrix | null { |
|
| 93 | 93 | $k = min($this->row, $this->col); |
| 94 | 94 | $s = vector::factory($k); |
| 95 | 95 | $u = self::factory($this->row, $this->row); |
@@ -31,21 +31,21 @@ discard block |
||
| 31 | 31 | public $data; |
| 32 | 32 | protected $_time = null, $_mem = null; |
| 33 | 33 | |
| 34 | - public function checkDimensions(\Np\matrix|\Np\vector $Obj1, \Np\matrix $Obj2) { |
|
| 35 | - if($Obj1->col == $Obj2->row){ |
|
| 34 | + public function checkDimensions(\Np\matrix | \Np\vector $Obj1, \Np\matrix $Obj2) { |
|
| 35 | + if ($Obj1->col == $Obj2->row) { |
|
| 36 | 36 | return true; |
| 37 | 37 | } |
| 38 | 38 | self::_dimensionaMisMatchErr('Mismatch Dimensions of given Objects! Obj-A col & Obj-B row amount need to be the same!'); |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | - public function checkDtype(\Np\matrix|\Np\vector $Obj1, \Np\matrix|\Np\vector $Obj2){ |
|
| 42 | - if($Obj1->dtype == $Obj2->dtype) { |
|
| 41 | + public function checkDtype(\Np\matrix | \Np\vector $Obj1, \Np\matrix | \Np\vector $Obj2) { |
|
| 42 | + if ($Obj1->dtype == $Obj2->dtype) { |
|
| 43 | 43 | return true; |
| 44 | 44 | } |
| 45 | 45 | self::_dtypeErr('mismatch data type of given Np\Objects!'); |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | - public function checkShape(\Np\matrix|\Np\vector $Obj1, \Np\matrix|\Np\vector $Obj2) { |
|
| 48 | + public function checkShape(\Np\matrix | \Np\vector $Obj1, \Np\matrix | \Np\vector $Obj2) { |
|
| 49 | 49 | if ($Obj1 instanceof \Np\vector && $Obj2 instanceof \Np\vector) { |
| 50 | 50 | if ($Obj1->col == $Obj2->col) { |
| 51 | 51 | return true; |
@@ -64,8 +64,8 @@ discard block |
||
| 64 | 64 | } |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | - public function asType(int $dtype){ |
|
| 68 | - switch ($dtype){ |
|
| 67 | + public function asType(int $dtype) { |
|
| 68 | + switch ($dtype) { |
|
| 69 | 69 | case self::FLOAT: |
| 70 | 70 | \FFI::cast('float *', $this->data); |
| 71 | 71 | break; |
@@ -318,7 +318,7 @@ |
||
| 318 | 318 | * @param \Np\vector|\Np\matrix $v |
| 319 | 319 | * @return \FFI\CData |
| 320 | 320 | */ |
| 321 | - public static function scale(float $alpha, \Np\vector|\Np\matrix $v) { |
|
| 321 | + public static function scale(float $alpha, \Np\vector | \Np\matrix $v) { |
|
| 322 | 322 | self::init(); |
| 323 | 323 | return self::$ffi_blas->cblas_dscal($v->ndim, $alpha, $v->data, 1); |
| 324 | 324 | } |
@@ -138,7 +138,7 @@ |
||
| 138 | 138 | * @param string $trans |
| 139 | 139 | * @return type |
| 140 | 140 | */ |
| 141 | - public static function gels(\Np\matrix $m, \Np\matrix|\Np\vector $b, int $matLayout = self::ROW_MAJOR, string $trans = 'N') { |
|
| 141 | + public static function gels(\Np\matrix $m, \Np\matrix | \Np\vector $b, int $matLayout = self::ROW_MAJOR, string $trans = 'N') { |
|
| 142 | 142 | self::init(); |
| 143 | 143 | return self::$ffi_lapack->LAPACKE_dgels($matLayout, $trans, $m->row, $m->col, $b->col, $m->data, |
| 144 | 144 | $m->col, $b->data, $b->col); |
@@ -4,9 +4,9 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | namespace Np; |
| 6 | 6 | |
| 7 | -use Np\core\{nd,blas,lapack}; |
|
| 8 | -use Np\linAlgb\reductions\{ref,rref}; |
|
| 9 | -use Np\linAlgb\decompositions\{lu,svd,eigen,cholesky}; |
|
| 7 | +use Np\core\{nd, blas, lapack}; |
|
| 8 | +use Np\linAlgb\reductions\{ref, rref}; |
|
| 9 | +use Np\linAlgb\decompositions\{lu, svd, eigen, cholesky}; |
|
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | 12 | * Matrix |
@@ -19,7 +19,7 @@ discard block |
||
| 19 | 19 | */ |
| 20 | 20 | class matrix extends nd { |
| 21 | 21 | |
| 22 | - use ops,linAlgb\linAlg; |
|
| 22 | + use ops, linAlgb\linAlg; |
|
| 23 | 23 | |
| 24 | 24 | /** |
| 25 | 25 | * create empty 2d matrix for given data type |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | * @param int|float $val |
| 127 | 127 | * @return \Np\matrix |
| 128 | 128 | */ |
| 129 | - public static function full(int $row, int $col, int|float $val): matrix { |
|
| 129 | + public static function full(int $row, int $col, int | float $val): matrix { |
|
| 130 | 130 | $ar = self::factory($row, $col); |
| 131 | 131 | for ($i = 0; $i < $ar->ndim; ++$i) { |
| 132 | 132 | $ar->data[$i] = $val; |
@@ -350,7 +350,7 @@ discard block |
||
| 350 | 350 | * @param int|float|matrix|vector $m |
| 351 | 351 | * @return matrix|vector |
| 352 | 352 | */ |
| 353 | - public function multiply(int|float|matrix|vector $m): matrix|vector { |
|
| 353 | + public function multiply(int | float | matrix | vector $m): matrix | vector { |
|
| 354 | 354 | if ($m instanceof self) { |
| 355 | 355 | return $this->multiplyMatrix($m); |
| 356 | 356 | } else if ($m instanceof vector) { |
@@ -400,7 +400,7 @@ discard block |
||
| 400 | 400 | * @param int|float|matrix|vector $m |
| 401 | 401 | * @return matrix |
| 402 | 402 | */ |
| 403 | - public function sum(int|float|matrix|vector $m): matrix { |
|
| 403 | + public function sum(int | float | matrix | vector $m): matrix { |
|
| 404 | 404 | if ($m instanceof self) { |
| 405 | 405 | return $this->sumMatrix($m); |
| 406 | 406 | } elseif ($m instanceof vector) { |
@@ -410,7 +410,7 @@ discard block |
||
| 410 | 410 | } |
| 411 | 411 | } |
| 412 | 412 | |
| 413 | - protected function sumScalar(int|float $s): matrix { |
|
| 413 | + protected function sumScalar(int | float $s): matrix { |
|
| 414 | 414 | $ar = self::factory($this->row, $this->col); |
| 415 | 415 | for ($i = 0; $i < $this->ndim; ++$i) { |
| 416 | 416 | $ar->data[$i] = $this->data[$i] + $s; |
@@ -461,7 +461,7 @@ discard block |
||
| 461 | 461 | * @param int|float|matrix|vector $d matrix|$scalar to subtract this matrix |
| 462 | 462 | * @return \Np\matrix |
| 463 | 463 | */ |
| 464 | - public function subtract(int|float|matrix|vector $d): matrix { |
|
| 464 | + public function subtract(int | float | matrix | vector $d): matrix { |
|
| 465 | 465 | if ($d instanceof self) { |
| 466 | 466 | return $this->subtractMatrix($d); |
| 467 | 467 | } elseif ($d instanceof vector) { |
@@ -471,7 +471,7 @@ discard block |
||
| 471 | 471 | } |
| 472 | 472 | } |
| 473 | 473 | |
| 474 | - protected function subtractScalar(int|float $s): matrix { |
|
| 474 | + protected function subtractScalar(int | float $s): matrix { |
|
| 475 | 475 | $ar = self::factory($this->row, $this->col); |
| 476 | 476 | for ($i = 0; $i < $this->ndim; ++$i) { |
| 477 | 477 | $ar->data[$i] = $this->data[$i] - $s; |
@@ -533,7 +533,7 @@ discard block |
||
| 533 | 533 | * @param int|float|matrix $d |
| 534 | 534 | * @return matrix |
| 535 | 535 | */ |
| 536 | - public function divide(int|float|matrix|vector $d): matrix { |
|
| 536 | + public function divide(int | float | matrix | vector $d): matrix { |
|
| 537 | 537 | if ($d instanceof self) { |
| 538 | 538 | return $this->divideMatrix($d); |
| 539 | 539 | } elseif ($d instanceof vector) { |
@@ -565,7 +565,7 @@ discard block |
||
| 565 | 565 | } |
| 566 | 566 | } |
| 567 | 567 | |
| 568 | - protected function divideScalar(int|float $s): matrix { |
|
| 568 | + protected function divideScalar(int | float $s): matrix { |
|
| 569 | 569 | $ar = self::factory($this->row, $this->col); |
| 570 | 570 | for ($i = 0; $i < $this->ndim; ++$i) { |
| 571 | 571 | $ar->data[$i] = $this->data[$i] / $s; |
@@ -580,7 +580,7 @@ discard block |
||
| 580 | 580 | * @param int|float|matrix $m |
| 581 | 581 | * @return matrix |
| 582 | 582 | */ |
| 583 | - public function pow(int|float|matrix|vector $d): matrix { |
|
| 583 | + public function pow(int | float | matrix | vector $d): matrix { |
|
| 584 | 584 | if ($d instanceof self) { |
| 585 | 585 | return $this->powMatrix($d); |
| 586 | 586 | } else if ($d instanceof vector) { |
@@ -612,7 +612,7 @@ discard block |
||
| 612 | 612 | } |
| 613 | 613 | } |
| 614 | 614 | |
| 615 | - protected function powScalar(int|float $s): matrix { |
|
| 615 | + protected function powScalar(int | float $s): matrix { |
|
| 616 | 616 | $ar = $this->copy(); |
| 617 | 617 | for ($i = 0; $i < $this->ndim; ++$i) { |
| 618 | 618 | $ar->data[$i] **= $s; |
@@ -625,7 +625,7 @@ discard block |
||
| 625 | 625 | * @param int|float|matrix|vector $d |
| 626 | 626 | * @return matrix |
| 627 | 627 | */ |
| 628 | - public function mod(int|float|matrix|vector $d): matrix { |
|
| 628 | + public function mod(int | float | matrix | vector $d): matrix { |
|
| 629 | 629 | if ($d instanceof self) { |
| 630 | 630 | $this->modMatrix($d); |
| 631 | 631 | } else if ($d instanceof vector) { |
@@ -657,7 +657,7 @@ discard block |
||
| 657 | 657 | } |
| 658 | 658 | } |
| 659 | 659 | |
| 660 | - protected function modScalar(int|float $s): matrix { |
|
| 660 | + protected function modScalar(int | float $s): matrix { |
|
| 661 | 661 | $ar = $this->copy(); |
| 662 | 662 | for ($i = 0; $i < $this->ndim; ++$i) { |
| 663 | 663 | $ar->data[$i] %= $s; |
@@ -730,7 +730,7 @@ discard block |
||
| 730 | 730 | * @param int|float $scalar |
| 731 | 731 | * @return matrix |
| 732 | 732 | */ |
| 733 | - public function scale(int|float $scalar): matrix { |
|
| 733 | + public function scale(int | float $scalar): matrix { |
|
| 734 | 734 | if ($scalar == 0) { |
| 735 | 735 | return self::zeros($this->row, $this->col); |
| 736 | 736 | } |
@@ -748,7 +748,7 @@ discard block |
||
| 748 | 748 | * @param int $row |
| 749 | 749 | * @param int|float $c |
| 750 | 750 | */ |
| 751 | - public function scaleRow(int $row, int|float $c) { |
|
| 751 | + public function scaleRow(int $row, int | float $c) { |
|
| 752 | 752 | for ($i = 0; $i < $this->col; ++$i) { |
| 753 | 753 | $this->data[$row * $this->col + $i] *= $c; |
| 754 | 754 | } |
@@ -759,7 +759,7 @@ discard block |
||
| 759 | 759 | * @param int $col |
| 760 | 760 | * @param int|float $c |
| 761 | 761 | */ |
| 762 | - public function scaleCol(int $col, int|float $c) { |
|
| 762 | + public function scaleCol(int $col, int | float $c) { |
|
| 763 | 763 | for ($i = 0; $i < $this->row; ++$i) { |
| 764 | 764 | $this->data[$i * $this->col + $col] *= $c; |
| 765 | 765 | } |
@@ -770,7 +770,7 @@ discard block |
||
| 770 | 770 | * @param int|float $c |
| 771 | 771 | * @param bool $lDig |
| 772 | 772 | */ |
| 773 | - public function scaleDigonalCol(int|float $c, bool $lDig = true) { |
|
| 773 | + public function scaleDigonalCol(int | float $c, bool $lDig = true) { |
|
| 774 | 774 | if ($lDig) { |
| 775 | 775 | for ($i = 0; $i < $this->row; ++$i) { |
| 776 | 776 | $this->data[$i * $this->col + $i] *= $c; |
@@ -944,7 +944,7 @@ discard block |
||
| 944 | 944 | * @param bool $dignoal |
| 945 | 945 | * @return void |
| 946 | 946 | */ |
| 947 | - public function setData(int|float|array $data): void { |
|
| 947 | + public function setData(int | float | array $data): void { |
|
| 948 | 948 | |
| 949 | 949 | if (is_array($data) && is_array($data[0])) { |
| 950 | 950 | $f = $this->flattenArray($data); |
@@ -975,7 +975,7 @@ discard block |
||
| 975 | 975 | * @return object |
| 976 | 976 | */ |
| 977 | 977 | public function getShape(): object { |
| 978 | - return (object) ['m' => $this->row, 'n' => $this->col]; |
|
| 978 | + return (object)['m' => $this->row, 'n' => $this->col]; |
|
| 979 | 979 | } |
| 980 | 980 | |
| 981 | 981 | /** |
@@ -1087,7 +1087,7 @@ discard block |
||
| 1087 | 1087 | * |
| 1088 | 1088 | * @return matrix|null |
| 1089 | 1089 | */ |
| 1090 | - public function ref(): matrix|null { |
|
| 1090 | + public function ref(): matrix | null { |
|
| 1091 | 1091 | return ref::factory($this); |
| 1092 | 1092 | } |
| 1093 | 1093 | |
@@ -1096,7 +1096,7 @@ discard block |
||
| 1096 | 1096 | * |
| 1097 | 1097 | * @return matrix|null |
| 1098 | 1098 | */ |
| 1099 | - public function cholesky(): matrix|null { |
|
| 1099 | + public function cholesky(): matrix | null { |
|
| 1100 | 1100 | return cholesky::factory($this); |
| 1101 | 1101 | } |
| 1102 | 1102 | |
@@ -1189,7 +1189,7 @@ discard block |
||
| 1189 | 1189 | * @param vector|null $mean |
| 1190 | 1190 | * @return vector |
| 1191 | 1191 | */ |
| 1192 | - public function variance(vector|null $mean = null): vector { |
|
| 1192 | + public function variance(vector | null $mean = null): vector { |
|
| 1193 | 1193 | if (isset($mean)) { |
| 1194 | 1194 | if (!$mean instanceof vector) { |
| 1195 | 1195 | self::_invalidArgument('mean must be a vector!'); |
@@ -1231,7 +1231,7 @@ discard block |
||
| 1231 | 1231 | * @param vector|null $mean |
| 1232 | 1232 | * @return matrix |
| 1233 | 1233 | */ |
| 1234 | - public function covariance(vector|null $mean = null): matrix { |
|
| 1234 | + public function covariance(vector | null $mean = null): matrix { |
|
| 1235 | 1235 | if (isset($mean)) { |
| 1236 | 1236 | if ($mean->col !== $this->row) { |
| 1237 | 1237 | self::_err('Err:: given mean vector dimensionality mismatched!'); |
@@ -1259,7 +1259,7 @@ discard block |
||
| 1259 | 1259 | * @param int|float|matrix|vector $d |
| 1260 | 1260 | * @return matrix |
| 1261 | 1261 | */ |
| 1262 | - public function equal(int|float|matrix|vector $d): matrix { |
|
| 1262 | + public function equal(int | float | matrix | vector $d): matrix { |
|
| 1263 | 1263 | if ($d instanceof self) { |
| 1264 | 1264 | return $this->equalMatrix($d); |
| 1265 | 1265 | } |
@@ -1291,7 +1291,7 @@ discard block |
||
| 1291 | 1291 | } |
| 1292 | 1292 | } |
| 1293 | 1293 | |
| 1294 | - protected function equalScalar(int|float $s): matrix { |
|
| 1294 | + protected function equalScalar(int | float $s): matrix { |
|
| 1295 | 1295 | $ar = self::factory($this->row, $this->col); |
| 1296 | 1296 | for ($i = 0; $i < $this->ndim; ++$i) { |
| 1297 | 1297 | $ar->data[$i] = $this->data[$i] == $s ? 1 : 0; |
@@ -1304,7 +1304,7 @@ discard block |
||
| 1304 | 1304 | * @param int|float|matrix|vector $d |
| 1305 | 1305 | * @return matrix |
| 1306 | 1306 | */ |
| 1307 | - public function greater(int|float|matrix|vector $d): matrix { |
|
| 1307 | + public function greater(int | float | matrix | vector $d): matrix { |
|
| 1308 | 1308 | if ($d instanceof self) { |
| 1309 | 1309 | return $this->greaterMatrix($d); |
| 1310 | 1310 | } |
@@ -1336,7 +1336,7 @@ discard block |
||
| 1336 | 1336 | } |
| 1337 | 1337 | } |
| 1338 | 1338 | |
| 1339 | - protected function greaterScalar(int|float $s): matrix { |
|
| 1339 | + protected function greaterScalar(int | float $s): matrix { |
|
| 1340 | 1340 | $ar = self::factory($this->row, $this->col); |
| 1341 | 1341 | for ($i = 0; $i < $this->ndim; ++$i) { |
| 1342 | 1342 | $ar->data[$i] = $this->data[$i] > $s ? 1 : 0; |
@@ -1349,7 +1349,7 @@ discard block |
||
| 1349 | 1349 | * @param int|float|matrix $m |
| 1350 | 1350 | * @return matrix |
| 1351 | 1351 | */ |
| 1352 | - public function less(int|float|matrix $m): matrix { |
|
| 1352 | + public function less(int | float | matrix $m): matrix { |
|
| 1353 | 1353 | $ar = self::factory($this->row, $this->col); |
| 1354 | 1354 | if ($m instanceof self) { |
| 1355 | 1355 | if ($this->checkShape($this, $m)) { |
@@ -1380,7 +1380,7 @@ discard block |
||
| 1380 | 1380 | } |
| 1381 | 1381 | |
| 1382 | 1382 | public function __toString() { |
| 1383 | - return (string) $this->printMatrix(); |
|
| 1383 | + return (string)$this->printMatrix(); |
|
| 1384 | 1384 | } |
| 1385 | 1385 | |
| 1386 | 1386 | private function flattenArray(array $ar) { |