From 48ca4bd6bb9a3e6c07dbc4d34e1babb082e2cf06 Mon Sep 17 00:00:00 2001 From: Mark Rogoyski Date: Tue, 2 May 2023 06:57:20 -0700 Subject: [PATCH 01/11] Minor static analysis comment. --- src/Statistics/Multivariate/PLS.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Statistics/Multivariate/PLS.php b/src/Statistics/Multivariate/PLS.php index 546c76aa3..a447c61e2 100644 --- a/src/Statistics/Multivariate/PLS.php +++ b/src/Statistics/Multivariate/PLS.php @@ -144,6 +144,7 @@ public function __construct(NumericMatrix $X, NumericMatrix $Y, int $ncomp, bool // Calculate R (or W*) @phpstan-ignore-next-line $R = $this->W->multiply($this->P->transpose()->multiply($this->W)->inverse()); + // @phpstan-ignore-next-line $this->B = $R->multiply($this->C->transpose()); } From 460377d00ea58f6e4660e70d07829aba542e1e80 Mon Sep 17 00:00:00 2001 From: Mark Rogoyski Date: Sun, 7 May 2023 10:23:07 -0700 Subject: [PATCH 02/11] Update CHANGELOG for v2.8.0. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c0e89e65..65a3334d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### Improvements * Better error handling and exception message in `Sequence\NonIntenger::hyperharmonic` +* Internal code improvements to conform to static analysis checks ### Improvements From fc5e16aee7bff63aa65a6db53cf535d410dbf0ba Mon Sep 17 00:00:00 2001 From: Mark Rogoyski Date: Sun, 7 May 2023 10:28:40 -0700 Subject: [PATCH 03/11] Rename methods to better indicate assertion and no return value. --- CHANGELOG.md | 5 ++++- .../NumericalDifferentiation/FivePointFormula.php | 4 ++-- .../NumericalDifferentiation/NumericalDifferentiation.php | 8 ++------ .../SecondDerivativeMidpointFormula.php | 2 +- .../NumericalDifferentiation/ThreePointFormula.php | 4 ++-- .../NumericalDifferentiationTest.php | 4 ++-- 6 files changed, 13 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65a3334d8..2bab9420d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,10 @@ * Better error handling and exception message in `Sequence\NonIntenger::hyperharmonic` * Internal code improvements to conform to static analysis checks -### Improvements +### Backwards Incompatible Changes +* Helper method names changed (public abstract methods but not part of published interface) + * `NumericalDifferentiation::isTargetInPoints` changed to `assertTargetInPoints` + * `NumericalDifferentiation::isSpacingConstant` changed to `assertSpacingConstant` ## v2.7.0 - 2022-12-31 diff --git a/src/NumericalAnalysis/NumericalDifferentiation/FivePointFormula.php b/src/NumericalAnalysis/NumericalDifferentiation/FivePointFormula.php index 9cc214af0..3b99c027a 100644 --- a/src/NumericalAnalysis/NumericalDifferentiation/FivePointFormula.php +++ b/src/NumericalAnalysis/NumericalDifferentiation/FivePointFormula.php @@ -80,8 +80,8 @@ public static function differentiate(float $target, $source, ...$args): float // sure our target is contained in an interval supplied by our $source self::validate($points, $degree = 5); $sorted = self::sort($points); - self::isSpacingConstant($sorted); - self::isTargetInPoints($target, $sorted); + self::assertSpacingConstant($sorted); + self::assertTargetInPoints($target, $sorted); // Descriptive constants $x = self::X; diff --git a/src/NumericalAnalysis/NumericalDifferentiation/NumericalDifferentiation.php b/src/NumericalAnalysis/NumericalDifferentiation/NumericalDifferentiation.php index 172157bef..46d45dd89 100644 --- a/src/NumericalAnalysis/NumericalDifferentiation/NumericalDifferentiation.php +++ b/src/NumericalAnalysis/NumericalDifferentiation/NumericalDifferentiation.php @@ -157,10 +157,8 @@ protected static function sort(array $points): array * * @throws Exception\BadDataException if the spacing between any two points is not equal * to the average spacing between every point - * - * FIXME: maybe rename to checkIsSpacingConstant? */ - public static function isSpacingConstant(array $sorted): void + public static function assertSpacingConstant(array $sorted): void { $x = self::X; $length = \count($sorted); @@ -180,10 +178,8 @@ public static function isSpacingConstant(array $sorted): void * @param array $sorted Points sorted by (increasing) x-component * * @throws Exception\BadDataException if $target is not contained in the array of our x-components - * - * FIXME: maybe rename to checkIsTargetInPoints? */ - public static function isTargetInPoints($target, array $sorted): void + public static function assertTargetInPoints($target, array $sorted): void { $xComponents = \array_map( function (array $point) { diff --git a/src/NumericalAnalysis/NumericalDifferentiation/SecondDerivativeMidpointFormula.php b/src/NumericalAnalysis/NumericalDifferentiation/SecondDerivativeMidpointFormula.php index af150142d..8e31c2c8f 100644 --- a/src/NumericalAnalysis/NumericalDifferentiation/SecondDerivativeMidpointFormula.php +++ b/src/NumericalAnalysis/NumericalDifferentiation/SecondDerivativeMidpointFormula.php @@ -69,7 +69,7 @@ public static function differentiate(float $target, $source, ...$args) // sure our target is contained in an interval supplied by our $source self::validate($points, $degree = 3); $sorted = self::sort($points); - self::isSpacingConstant($sorted); + self::assertSpacingConstant($sorted); // Descriptive constants $x = self::X; diff --git a/src/NumericalAnalysis/NumericalDifferentiation/ThreePointFormula.php b/src/NumericalAnalysis/NumericalDifferentiation/ThreePointFormula.php index e36213358..f0a53a03a 100644 --- a/src/NumericalAnalysis/NumericalDifferentiation/ThreePointFormula.php +++ b/src/NumericalAnalysis/NumericalDifferentiation/ThreePointFormula.php @@ -80,8 +80,8 @@ public static function differentiate(float $target, $source, ...$args): float // sure our target is contained in an interval supplied by our $source self::validate($points, $degree = 3); $sorted = self::sort($points); - self::isSpacingConstant($sorted); - self::isTargetInPoints($target, $sorted); + self::assertSpacingConstant($sorted); + self::assertTargetInPoints($target, $sorted); // Descriptive constants $x = self::X; diff --git a/tests/NumericalAnalysis/NumericalDifferentiation/NumericalDifferentiationTest.php b/tests/NumericalAnalysis/NumericalDifferentiation/NumericalDifferentiationTest.php index e54fabcc6..27851fd3d 100644 --- a/tests/NumericalAnalysis/NumericalDifferentiation/NumericalDifferentiationTest.php +++ b/tests/NumericalAnalysis/NumericalDifferentiation/NumericalDifferentiationTest.php @@ -88,7 +88,7 @@ public function testSpacingNonConstant() $this->expectException(Exception\BadDataException::class); // When - NumericalDifferentiation::isSpacingConstant($sortedPoints); + NumericalDifferentiation::assertSpacingConstant($sortedPoints); } /** @@ -105,6 +105,6 @@ public function testTargetNotInPoints() $this->expectException(Exception\BadDataException::class); // When - NumericalDifferentiation::isTargetInPoints($target, $sortedPoints); + NumericalDifferentiation::assertTargetInPoints($target, $sortedPoints); } } From b3872ef1330361a52425cffcca1546dff83a9aed Mon Sep 17 00:00:00 2001 From: Mark Rogoyski Date: Sun, 7 May 2023 10:29:59 -0700 Subject: [PATCH 04/11] Minor comment change. --- src/SetTheory/Set.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/SetTheory/Set.php b/src/SetTheory/Set.php index 5ad97011b..735f3ae45 100644 --- a/src/SetTheory/Set.php +++ b/src/SetTheory/Set.php @@ -150,10 +150,7 @@ public function isEmpty(): bool */ public function isMember($x): bool { - /** - * FIXME: $this->getKey() may return null, int|string required. - * @phpstan-ignore-next-line - */ + // @phpstan-ignore-next-line ($this->getKey() may return null, int|string required) return \array_key_exists($this->getKey($x), $this->A); } @@ -167,10 +164,7 @@ public function isMember($x): bool */ public function isNotMember($x): bool { - /** - * FIXME: $this->getKey() may return null, int|string required. - * @phpstan-ignore-next-line - */ + // @phpstan-ignore-next-line ($this->getKey() may return null, int|string required) return !\array_key_exists($this->getKey($x), $this->A); } From 64bc11a5375f31d90cbb36a4b22207ac4b03fdfc Mon Sep 17 00:00:00 2001 From: Mark Rogoyski Date: Sun, 7 May 2023 10:30:54 -0700 Subject: [PATCH 05/11] Minor comment change. --- src/Algebra.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Algebra.php b/src/Algebra.php index 6da542a37..b89777b8d 100644 --- a/src/Algebra.php +++ b/src/Algebra.php @@ -531,10 +531,7 @@ public static function quartic(float $a₄, float $a₃, float $a₂, float $a // The roots for this polynomial are the roots of the depressed polynomial minus a₃/4. if (!$return_complex) { - /** - * FIXME: are the roots real? Single::subtract() works with real numbers only. - * @phpstan-ignore-next-line - */ + // @phpstan-ignore-next-line (Single::subtract() works with real numbers only, must be real roots) return Single::subtract($depressed_quartic_roots, $a₃ / 4); } From 3c13e5a51fd982796e51ffc5c3519ecc1e19c42b Mon Sep 17 00:00:00 2001 From: Mark Rogoyski Date: Sun, 7 May 2023 10:34:47 -0700 Subject: [PATCH 06/11] Minor comment change. --- src/LinearAlgebra/Matrix.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/LinearAlgebra/Matrix.php b/src/LinearAlgebra/Matrix.php index 085d6dadb..140694a28 100644 --- a/src/LinearAlgebra/Matrix.php +++ b/src/LinearAlgebra/Matrix.php @@ -221,10 +221,7 @@ public function asVectors(): array $vectors = []; for ($j = 0; $j < $n; $j++) { - /** - * FIXME: maybe define vector as a generic class of T type? - * @phpstan-ignore-next-line - */ + // @phpstan-ignore-next-line (Vector expects numbers, Matrix may be generic T) $vectors[] = new Vector(\array_column($this->A, $j)); } @@ -249,10 +246,7 @@ public function asRowVectors(): array { return \array_map( function (array $row) { - /** - * FIXME: maybe define vector as a generic class of T type? - * @phpstan-ignore-next-line - */ + // @phpstan-ignore-next-line (Vector expects numbers, Matrix may be generic T) return new Vector($row); }, $this->A From 6fb5024851f71d0a32c5941688c41d4cd56f9597 Mon Sep 17 00:00:00 2001 From: Mark Rogoyski Date: Sun, 7 May 2023 10:35:16 -0700 Subject: [PATCH 07/11] Update CHANGELOG for v2.8.0. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bab9420d..ebabe823e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # MathPHP Change Log -## v2.8.0 - TBD +## v2.8.0 - 2023-05-07 ### New Features * Matrix `rowAddVector` From de15ba5c7ce8a1cf2a78505991520aefa04253f3 Mon Sep 17 00:00:00 2001 From: Mark Rogoyski Date: Sun, 7 May 2023 10:43:48 -0700 Subject: [PATCH 08/11] Github actions improvements for static analysis. --- .github/workflows/test_develop_and_master.yml | 4 +++- .github/workflows/test_other_branches.yml | 4 +++- .github/workflows/test_pull_request.yml | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test_develop_and_master.yml b/.github/workflows/test_develop_and_master.yml index f9a5a0451..07940bc4b 100644 --- a/.github/workflows/test_develop_and_master.yml +++ b/.github/workflows/test_develop_and_master.yml @@ -57,7 +57,9 @@ jobs: run: ./vendor/bin/phpcs --ignore=vendor --standard=tests/coding_standard.xml -s . - name: Static analysis - run: ./vendor/bin/phpstan analyze --level max src/ + run: | + ./vendor/bin/phpstan --version + ./vendor/bin/phpstan analyze --level max src/ code-coverage: name: Code coverage diff --git a/.github/workflows/test_other_branches.yml b/.github/workflows/test_other_branches.yml index c198d4f28..855495f06 100644 --- a/.github/workflows/test_other_branches.yml +++ b/.github/workflows/test_other_branches.yml @@ -56,4 +56,6 @@ jobs: run: ./vendor/bin/phpcs --ignore=vendor --standard=tests/coding_standard.xml -s . - name: Static analysis - run: ./vendor/bin/phpstan analyze --level max src/ + run: | + ./vendor/bin/phpstan --version + ./vendor/bin/phpstan analyze --level max src/ diff --git a/.github/workflows/test_pull_request.yml b/.github/workflows/test_pull_request.yml index bff052c4b..3e0c3eabb 100644 --- a/.github/workflows/test_pull_request.yml +++ b/.github/workflows/test_pull_request.yml @@ -53,7 +53,9 @@ jobs: run: ./vendor/bin/phpcs --ignore=vendor --standard=tests/coding_standard.xml -s . - name: Static analysis - run: ./vendor/bin/phpstan analyze --level max src/ + run: | + ./vendor/bin/phpstan --version + ./vendor/bin/phpstan analyze --level max src/ code-coverage: name: Code coverage From 70d0111a0930338a60eed9cad23cf587a4806380 Mon Sep 17 00:00:00 2001 From: Mark Rogoyski Date: Sun, 7 May 2023 10:45:19 -0700 Subject: [PATCH 09/11] Github actions changes for static analysis. --- .github/workflows/test_develop_and_master.yml | 4 +++- .github/workflows/test_other_branches.yml | 4 +++- .github/workflows/test_pull_request.yml | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test_develop_and_master.yml b/.github/workflows/test_develop_and_master.yml index 07940bc4b..f55d04032 100644 --- a/.github/workflows/test_develop_and_master.yml +++ b/.github/workflows/test_develop_and_master.yml @@ -54,7 +54,9 @@ jobs: ./vendor/bin/phpunit --configuration tests/phpunit.xml --testsuite=LinearAlgebra - name: PHP Code Sniffer - run: ./vendor/bin/phpcs --ignore=vendor --standard=tests/coding_standard.xml -s . + run: | + ./vendor/bin/phpcs --version + ./vendor/bin/phpcs --ignore=vendor --standard=tests/coding_standard.xml -s . - name: Static analysis run: | diff --git a/.github/workflows/test_other_branches.yml b/.github/workflows/test_other_branches.yml index 855495f06..347cd67ce 100644 --- a/.github/workflows/test_other_branches.yml +++ b/.github/workflows/test_other_branches.yml @@ -53,7 +53,9 @@ jobs: ./vendor/bin/phpunit --configuration tests/phpunit.xml --testsuite=LinearAlgebra - name: PHP Code Sniffer - run: ./vendor/bin/phpcs --ignore=vendor --standard=tests/coding_standard.xml -s . + run: | + ./vendor/bin/phpcs --version + ./vendor/bin/phpcs --ignore=vendor --standard=tests/coding_standard.xml -s . - name: Static analysis run: | diff --git a/.github/workflows/test_pull_request.yml b/.github/workflows/test_pull_request.yml index 3e0c3eabb..56dc25250 100644 --- a/.github/workflows/test_pull_request.yml +++ b/.github/workflows/test_pull_request.yml @@ -50,7 +50,9 @@ jobs: ./vendor/bin/phpunit --configuration tests/phpunit.xml --testsuite=LinearAlgebra - name: PHP Code Sniffer - run: ./vendor/bin/phpcs --ignore=vendor --standard=tests/coding_standard.xml -s . + run: | + ./vendor/bin/phpcs --version + ./vendor/bin/phpcs --ignore=vendor --standard=tests/coding_standard.xml -s . - name: Static analysis run: | From 341013a253e67862cbea89db973c62bdd957f87c Mon Sep 17 00:00:00 2001 From: Mark Rogoyski Date: Thu, 18 May 2023 21:38:31 -0700 Subject: [PATCH 10/11] Changes for static anlaysis improvements, and various other internal changes. --- .github/workflows/test_develop_and_master.yml | 40 ++++++++++++++++--- .github/workflows/test_other_branches.yml | 38 +++++++++++++++--- .github/workflows/test_pull_request.yml | 40 ++++++++++++++++--- Makefile | 2 +- README.md | 2 +- composer.json | 2 +- src/Algebra.php | 10 +++-- src/Expression/Piecewise.php | 1 - src/Expression/Polynomial.php | 8 ++-- src/Functions/Map/Multi.php | 22 +++++----- src/Functions/Map/Single.php | 28 ++++++------- src/Functions/Support.php | 2 +- src/InformationTheory/Entropy.php | 16 ++++---- src/LinearAlgebra/Decomposition/LU.php | 4 +- src/LinearAlgebra/Decomposition/QR.php | 4 +- src/LinearAlgebra/Decomposition/SVD.php | 14 +++---- src/LinearAlgebra/Eigenvalue.php | 4 +- src/LinearAlgebra/MatrixFactory.php | 33 +++++++++------ src/LinearAlgebra/NumericDiagonalMatrix.php | 2 +- src/LinearAlgebra/NumericMatrix.php | 31 +++++++------- src/LinearAlgebra/NumericSquareMatrix.php | 2 +- src/LinearAlgebra/ObjectMatrix.php | 3 +- .../Reduction/ReducedRowEchelonForm.php | 2 +- .../Reduction/RowEchelonForm.php | 8 ++-- src/LinearAlgebra/Vector.php | 28 ++++++------- src/Number/Complex.php | 17 ++++---- src/Number/Quaternion.php | 35 ++++++++-------- .../Interpolation/ClampedCubicSpline.php | 20 +++++----- .../Interpolation/Interpolation.php | 4 +- .../Interpolation/LagrangePolynomial.php | 2 +- .../Interpolation/NaturalCubicSpline.php | 2 +- .../Interpolation/NevillesMethod.php | 2 +- .../Interpolation/NewtonPolynomialForward.php | 2 +- .../Interpolation/RegularGridInterpolator.php | 16 ++++---- .../FivePointFormula.php | 2 +- .../NumericalDifferentiation.php | 8 ++-- .../SecondDerivativeMidpointFormula.php | 2 +- .../ThreePointFormula.php | 2 +- .../NumericalIntegration/BoolesRule.php | 2 +- .../NumericalIntegration/MidpointRule.php | 2 +- .../NumericalIntegration.php | 14 +++---- .../NumericalIntegration/RectangleMethod.php | 2 +- .../NumericalIntegration/SimpsonsRule.php | 2 +- .../SimpsonsThreeEighthsRule.php | 2 +- .../NumericalIntegration/TrapezoidalRule.php | 2 +- .../NumericalIntegration/Validation.php | 2 +- .../RootFinding/BisectionMethod.php | 2 +- .../RootFinding/FixedPointIteration.php | 2 +- .../RootFinding/NewtonsMethod.php | 2 +- .../Distribution/Continuous/Beta.php | 4 +- .../Distribution/Continuous/Cauchy.php | 4 +- .../Distribution/Continuous/Continuous.php | 6 +-- .../Continuous/ContinuousDistribution.php | 4 +- .../Distribution/Continuous/Normal.php | 2 + .../Distribution/Discrete/Categorical.php | 6 +-- .../Distribution/Discrete/Zipf.php | 10 ++--- .../Multivariate/Hypergeometric.php | 6 +-- .../Distribution/Multivariate/Multinomial.php | 4 +- .../Distribution/Multivariate/Normal.php | 6 +-- .../Distribution/Table/ChiSquared.php | 1 - .../Distribution/Table/StandardNormal.php | 2 +- .../Distribution/Table/TDistribution.php | 10 ++--- src/SampleData/Cereal.php | 14 +++---- src/SampleData/Iris.php | 12 +++--- src/SampleData/MtCars.php | 34 ++++++++-------- src/SampleData/PlantGrowth.php | 6 +-- src/SampleData/ToothGrowth.php | 4 +- src/SampleData/UsArrests.php | 12 +++--- src/SetTheory/ImmutableSet.php | 4 +- src/SetTheory/Set.php | 2 +- src/Statistics/Average.php | 10 ++--- src/Statistics/Correlation.php | 6 +-- src/Statistics/Descriptive.php | 2 +- src/Statistics/Distance.php | 12 +++--- src/Statistics/Distribution.php | 8 ++-- src/Statistics/Divergence.php | 8 ++-- src/Statistics/Regression/LOESS.php | 2 +- .../Regression/Methods/LeastSquares.php | 2 +- src/Statistics/Significance.php | 1 - .../Matrix/MatrixFactoryTest.php | 16 ++++++++ tests/phpstan.neon | 5 +++ 81 files changed, 412 insertions(+), 307 deletions(-) create mode 100644 tests/phpstan.neon diff --git a/.github/workflows/test_develop_and_master.yml b/.github/workflows/test_develop_and_master.yml index f55d04032..3fdc2ffdf 100644 --- a/.github/workflows/test_develop_and_master.yml +++ b/.github/workflows/test_develop_and_master.yml @@ -8,7 +8,7 @@ on: jobs: test-and-static-analysis: - name: Test and Static Analysis + name: Test and Lint runs-on: ubuntu-latest strategy: matrix: @@ -23,12 +23,12 @@ jobs: tools: composer:v2 - name: Set up Node - uses: actions/setup-node@v1 + uses: actions/setup-node@v3 with: node-version: '14.x' - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 with: fetch-depth: 0 @@ -58,10 +58,38 @@ jobs: ./vendor/bin/phpcs --version ./vendor/bin/phpcs --ignore=vendor --standard=tests/coding_standard.xml -s . + static-analysis: + name: Static Analysis + runs-on: ubuntu-latest + strategy: + matrix: + php: ['7.2'] + + steps: + - name: Set up PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: xdebug + tools: composer:v2 + + - name: Set up Node + uses: actions/setup-node@v3 + with: + node-version: '14.x' + + - name: Checkout code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Run Composer + run: composer install --no-interaction + - name: Static analysis run: | ./vendor/bin/phpstan --version - ./vendor/bin/phpstan analyze --level max src/ + ./vendor/bin/phpstan analyze -c tests/phpstan.neon code-coverage: name: Code coverage @@ -79,12 +107,12 @@ jobs: tools: composer:v2 - name: Set up Node - uses: actions/setup-node@v1 + uses: actions/setup-node@v3 with: node-version: '14.x' - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 with: fetch-depth: 0 diff --git a/.github/workflows/test_other_branches.yml b/.github/workflows/test_other_branches.yml index 347cd67ce..a4cef9afd 100644 --- a/.github/workflows/test_other_branches.yml +++ b/.github/workflows/test_other_branches.yml @@ -7,8 +7,8 @@ on: - master jobs: - test-and-static-analysis: - name: Test and Static Analysis + test-and-lint: + name: Test and Lint runs-on: ubuntu-latest strategy: matrix: @@ -22,12 +22,12 @@ jobs: tools: composer:v2 - name: Set up Node - uses: actions/setup-node@v1 + uses: actions/setup-node@v3 with: node-version: '14.x' - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 with: fetch-depth: 0 @@ -57,7 +57,35 @@ jobs: ./vendor/bin/phpcs --version ./vendor/bin/phpcs --ignore=vendor --standard=tests/coding_standard.xml -s . + static-analysis: + name: Static Analysis + runs-on: ubuntu-latest + strategy: + matrix: + php: ['7.2'] + + steps: + - name: Set up PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: xdebug + tools: composer:v2 + + - name: Set up Node + uses: actions/setup-node@v3 + with: + node-version: '14.x' + + - name: Checkout code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Run Composer + run: composer install --no-interaction + - name: Static analysis run: | ./vendor/bin/phpstan --version - ./vendor/bin/phpstan analyze --level max src/ + ./vendor/bin/phpstan analyze -c tests/phpstan.neon \ No newline at end of file diff --git a/.github/workflows/test_pull_request.yml b/.github/workflows/test_pull_request.yml index 56dc25250..957cad081 100644 --- a/.github/workflows/test_pull_request.yml +++ b/.github/workflows/test_pull_request.yml @@ -4,7 +4,7 @@ on: pull_request jobs: test-and-static-analysis: - name: Test and Static Analysis + name: Test and Lint runs-on: ubuntu-latest strategy: matrix: @@ -19,12 +19,12 @@ jobs: tools: composer:v2 - name: Set up Node - uses: actions/setup-node@v1 + uses: actions/setup-node@v3 with: node-version: '14.x' - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 with: fetch-depth: 0 @@ -54,10 +54,38 @@ jobs: ./vendor/bin/phpcs --version ./vendor/bin/phpcs --ignore=vendor --standard=tests/coding_standard.xml -s . + static-analysis: + name: Static Analysis + runs-on: ubuntu-latest + strategy: + matrix: + php: ['7.2'] + + steps: + - name: Set up PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: xdebug + tools: composer:v2 + + - name: Set up Node + uses: actions/setup-node@v3 + with: + node-version: '14.x' + + - name: Checkout code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Run Composer + run: composer install --no-interaction + - name: Static analysis run: | ./vendor/bin/phpstan --version - ./vendor/bin/phpstan analyze --level max src/ + ./vendor/bin/phpstan analyze -c tests/phpstan.neon code-coverage: name: Code coverage @@ -75,12 +103,12 @@ jobs: tools: composer:v2 - name: Set up Node - uses: actions/setup-node@v1 + uses: actions/setup-node@v3 with: node-version: '14.x' - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 with: fetch-depth: 0 diff --git a/Makefile b/Makefile index 3270e55f0..9eb7ee0b8 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ style : vendor/bin/phpcs --standard=tests/coding_standard.xml --ignore=vendor -s . phpstan : - vendor/bin/phpstan analyze --level max src/ + vendor/bin/phpstan analyze -c tests/phpstan.neon phpmd : vendor/bin/phpmd src/ ansi cleancode,codesize,design,unusedcode,naming diff --git a/README.md b/README.md index 6f0c29298..71a125b8e 100644 --- a/README.md +++ b/README.md @@ -977,7 +977,7 @@ $f’⟮x⟯ = function ($x) { }; [$start, $end, $n] = [0, 3, 4]; -$p = Interpolation\ClampedCubicSpline::interpolate($points); // input as a set of points +$p = Interpolation\ClampedCubicSpline::interpolate($points); // input as a set of points $p = Interpolation\ClampedCubicSpline::interpolate($f⟮x⟯, $f’⟮x⟯, $start, $end, $n); // input as a callback function $p(0); // 1 diff --git a/composer.json b/composer.json index 031208891..760839c27 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "phpunit/phpunit": "^8.5", "php-coveralls/php-coveralls": "^2.0", "squizlabs/php_codesniffer": "3.*", - "phpstan/phpstan": "*", + "phpstan/phpstan": "^1.10", "phpmd/phpmd": "^2.6", "phploc/phploc": "*", "php-parallel-lint/php-parallel-lint": "^1.2" diff --git a/src/Algebra.php b/src/Algebra.php index b89777b8d..3a1e2cf93 100644 --- a/src/Algebra.php +++ b/src/Algebra.php @@ -95,6 +95,7 @@ public static function extendedGcd(int $a, int $b): array * @param int $b * * @return int + * @psalm-suppress InvalidReturnType (Change to intdiv for PHP 8.0) */ public static function lcm(int $a, int $b): int { @@ -236,7 +237,7 @@ public static function linear(float $a, float $b): ?float * @param float $c constant coefficient * @param bool $return_complex Whether to return complex numbers or NANs if imaginary roots * - * @return array{0: float|Complex, 1?: float|Complex} + * @return float[]|Complex[] * [x₁, x₂] roots of the equation, or * [NAN, NAN] if discriminant is negative, or * [Complex, Complex] if discriminant is negative and complex option is on or @@ -367,7 +368,7 @@ public static function discriminant(float $a, float $b, float $c): float * @param float $a₀ constant coefficient * @param bool $return_complex whether to return complex numbers * - * @return array{0: float|Complex, 1?: float|Complex, 2?: float|Complex} + * @return float[]|Complex[] * array of roots (three real roots, or one real root and two NANs because complex numbers not yet supported) * (If $a₃ = 0, then only two roots of quadratic equation) * @@ -531,7 +532,10 @@ public static function quartic(float $a₄, float $a₃, float $a₂, float $a // The roots for this polynomial are the roots of the depressed polynomial minus a₃/4. if (!$return_complex) { - // @phpstan-ignore-next-line (Single::subtract() works with real numbers only, must be real roots) + /** + * @phpstan-ignore-next-line (Single::subtract() works with real numbers only, must be real roots) + * @psalm-suppress InvalidArgument + */ return Single::subtract($depressed_quartic_roots, $a₃ / 4); } diff --git a/src/Expression/Piecewise.php b/src/Expression/Piecewise.php index 22dd13015..6417d79b8 100644 --- a/src/Expression/Piecewise.php +++ b/src/Expression/Piecewise.php @@ -71,7 +71,6 @@ public function __construct(array $intervals, array $functions) $lastB = $b ?? -\INF; $lastBOpen = $bOpen ?? false; - // @phpstan-ignore-next-line (Strict comparison using !== between 2 and 2 will always evaluate to false.) if (\count(\array_filter($interval, '\is_numeric')) !== 2) { throw new Exception\BadDataException('Each interval must contain two numbers.'); } diff --git a/src/Expression/Polynomial.php b/src/Expression/Polynomial.php index e81612523..e44085f68 100644 --- a/src/Expression/Polynomial.php +++ b/src/Expression/Polynomial.php @@ -56,7 +56,7 @@ class Polynomial implements ObjectArithmetic /** @var int */ private $degree; - /** @var array */ + /** @var array */ private $coefficients; /** @var string */ @@ -71,7 +71,7 @@ class Polynomial implements ObjectArithmetic * When a polynomial is instantiated, set the coefficients and degree of * that polynomial as its object parameters. * - * @param array $coefficients An array of coefficients in decreasing powers + * @param array $coefficients An array of coefficients in decreasing powers * Example: new Polynomial([1, 2, 3]) will create * a polynomial that looks like x² + 2x + 3. * @param string $variable @@ -215,7 +215,7 @@ private function checkNumericOrPolynomial($input): Polynomial if ($input instanceof Polynomial) { return $input; } elseif (\is_numeric($input)) { - /** @var number $input */ + /** @var int|float $input */ return new Polynomial([$input]); } else { throw new Exception\IncorrectTypeException('Input must be a Polynomial or a number'); @@ -235,7 +235,7 @@ public function getDegree(): int /** * Getter method for the coefficients of a polynomial * - * @return array The coefficients array of a polynomial object + * @return array The coefficients array of a polynomial object */ public function getCoefficients(): array { diff --git a/src/Functions/Map/Multi.php b/src/Functions/Map/Multi.php index 023c5d380..f60ba257b 100644 --- a/src/Functions/Map/Multi.php +++ b/src/Functions/Map/Multi.php @@ -14,9 +14,9 @@ class Multi * * [x₁ + y₁, x₂ + y₂, ... ] * - * @param array ...$arrays Two or more arrays of numbers + * @param array ...$arrays Two or more arrays of numbers * - * @return array + * @return array * * @throws Exception\BadDataException */ @@ -42,9 +42,9 @@ public static function add(array ...$arrays): array * * [x₁ - y₁, x₂ - y₂, ... ] * - * @param array ...$arrays Two or more arrays of numbers + * @param array ...$arrays Two or more arrays of numbers * - * @return array + * @return array * * @throws Exception\BadDataException */ @@ -75,9 +75,9 @@ function ($x) { * * [x₁ * y₁, x₂ * y₂, ... ] * - * @param array ...$arrays Two or more arrays of numbers + * @param array ...$arrays Two or more arrays of numbers * - * @return array + * @return array * * @throws Exception\BadDataException */ @@ -103,7 +103,7 @@ public static function multiply(array ...$arrays): array * * [x₁ / y₁, x₂ / y₂, ... ] * - * @param array ...$arrays Two or more arrays of numbers + * @param array ...$arrays Two or more arrays of numbers * * @return array * @@ -136,9 +136,9 @@ function ($x) { * * [max(x₁, y₁), max(x₂, y₂), ... ] * - * @param array ...$arrays Two or more arrays of numbers + * @param array ...$arrays Two or more arrays of numbers * - * @return array + * @return array * * @throws Exception\BadDataException */ @@ -169,9 +169,9 @@ function ($x) { * * [max(x₁, y₁), max(x₂, y₂), ... ] * - * @param array ...$arrays Two or more arrays of numbers + * @param array ...$arrays Two or more arrays of numbers * - * @return array + * @return array * * @throws Exception\BadDataException */ diff --git a/src/Functions/Map/Single.php b/src/Functions/Map/Single.php index f9a20feb6..a989b215c 100644 --- a/src/Functions/Map/Single.php +++ b/src/Functions/Map/Single.php @@ -12,7 +12,7 @@ class Single /** * Map addition * - * @param array $xs + * @param array $xs * @param int|float $k Number to add to each element * * @return array @@ -30,7 +30,7 @@ function ($x) use ($k) { /** * Map subtract * - * @param array $xs + * @param array $xs * @param int|float $k Number to subtract from each element * * @return array @@ -48,7 +48,7 @@ function ($x) use ($k) { /** * Map multiply * - * @param array $xs + * @param array $xs * @param int|float $k Number to multiply to each element * * @return array @@ -66,7 +66,7 @@ function ($x) use ($k) { /** * Map Divide * - * @param array $xs + * @param array $xs * @param int|float $k Number to divide each element by * * @return array @@ -84,7 +84,7 @@ function ($x) use ($k) { /** * Map square * - * @param array $xs + * @param array $xs * * @return array */ @@ -101,7 +101,7 @@ function ($x) { /** * Map cube * - * @param array $xs + * @param array $xs * * @return array */ @@ -119,7 +119,7 @@ function ($x) { * Map reciprocal * x := 1/x * - * @param array $xs + * @param array $xs * * @return array * @@ -148,7 +148,7 @@ function ($x) { /** * Map raise to a power * - * @param array $xs + * @param array $xs * @param int|float $n * * @return array @@ -166,7 +166,7 @@ function ($x) use ($n) { /** * Map square root * - * @param array $xs + * @param array $xs * * @return array */ @@ -183,7 +183,7 @@ function ($x) { /** * Map absolute value * - * @param array $xs + * @param array $xs * * @return array */ @@ -202,10 +202,10 @@ function ($x) { * Each element in array is compared against the value, * and the min of each is returned. * - * @param array $xs + * @param array $xs * @param int|float $value * - * @return array + * @return array */ public static function min(array $xs, $value): array { @@ -222,10 +222,10 @@ function ($x) use ($value) { * Each element in the array is compared against the value, * and the max of each is returned. * - * @param array $xs + * @param array $xs * @param int|float $value * - * @return array + * @return array */ public static function max(array $xs, $value): array { diff --git a/src/Functions/Support.php b/src/Functions/Support.php index 6694367ad..c4248df71 100644 --- a/src/Functions/Support.php +++ b/src/Functions/Support.php @@ -21,7 +21,7 @@ class Support * * @param array $limits Boundary limit definitions for each parameter * ['var1' => limit, 'var2' => limit, ...] - * @param array $params Parameters and their value to check against the defined limits + * @param array $params Parameters and their value to check against the defined limits * ['var1' => value, 'var2' => value, ...] * * @return bool True if all parameters are within defined limits diff --git a/src/InformationTheory/Entropy.php b/src/InformationTheory/Entropy.php index 8a707b115..35ad6b6bb 100644 --- a/src/InformationTheory/Entropy.php +++ b/src/InformationTheory/Entropy.php @@ -31,7 +31,7 @@ class Entropy * * H is in shannons, or bits. * - * @param array $p probability distribution + * @param array $p probability distribution * * @return float average minimum number of bits * @@ -74,7 +74,7 @@ function ($pᵢ) { * 1 nat = 1/ln(2) shannons or bits. * https://en.wikipedia.org/wiki/Nat_(unit) * - * @param array $p probability distribution + * @param array $p probability distribution * * @return float average minimum number of nats * @@ -117,7 +117,7 @@ function ($pᵢ) { * 1 hartley = log₂(10) bit = ln(10) nat, or approximately 3.322 Sh, or 2.303 nat. * https://en.wikipedia.org/wiki/Hartley_(unit) * - * @param array $p probability distribution + * @param array $p probability distribution * * @return float average minimum number of hartleys * @@ -158,8 +158,8 @@ function ($pᵢ) { * * H(p,q) = -∑ p(x) log₂ q(x) * - * @param array $p distribution p - * @param array $q distribution q + * @param array $p distribution p + * @param array $q distribution q * * @return float entropy between distributions * @@ -213,7 +213,7 @@ function ($pᵢ, $qᵢ) { * Joint entropy is basically just shannonEntropy but the probability distribution input * represents the probability of two variables happening at the same time. * - * @param array $P⟮x、y⟯ probability distribution of x and y occuring together + * @param array $P⟮x、y⟯ probability distribution of x and y occuring together * * @return float uncertainty * @@ -236,7 +236,7 @@ public static function jointEntropy(array $P⟮x、y⟯) * * H is in shannons, or bits. * - * @param array $p probability distribution + * @param array $p probability distribution * @param int|float $α order α * * @return float @@ -274,7 +274,7 @@ public static function renyiEntropy(array $p, $α) * * Perplexity is in shannons, or bits. * - * @param array $p probability distribution + * @param array $p probability distribution * * @return float perplexity * diff --git a/src/LinearAlgebra/Decomposition/LU.php b/src/LinearAlgebra/Decomposition/LU.php index a88c63812..107931d97 100644 --- a/src/LinearAlgebra/Decomposition/LU.php +++ b/src/LinearAlgebra/Decomposition/LU.php @@ -224,7 +224,7 @@ protected static function pivotize(NumericMatrix $A): NumericMatrix * xᵢ = --- | yᵢ - ∑ Uᵢⱼxⱼ | * Uᵢᵢ \ ʲ⁼ⁱ⁺¹ / * - * @param Vector|array $b solution to Ax = b + * @param Vector|array $b solution to Ax = b * * @return Vector x * @@ -236,7 +236,7 @@ protected static function pivotize(NumericMatrix $A): NumericMatrix */ public function solve($b): Vector { - // Input must be a Vector or array. @phpstan-ignore-next-line + // Input must be a Vector or array. if (!($b instanceof Vector || \is_array($b))) { throw new Exception\IncorrectTypeException('b in Ax = b must be a Vector or array'); } diff --git a/src/LinearAlgebra/Decomposition/QR.php b/src/LinearAlgebra/Decomposition/QR.php index f45546024..a944c330d 100644 --- a/src/LinearAlgebra/Decomposition/QR.php +++ b/src/LinearAlgebra/Decomposition/QR.php @@ -120,7 +120,7 @@ public static function decompose(NumericMatrix $A): QR * - R⁻¹R = I, so we get x = R⁻¹Qᵀb * Solve x = R⁻¹Qᵀb * - * @param Vector|array $b solution to Ax = b + * @param Vector|array $b solution to Ax = b * * @return Vector x * @@ -128,7 +128,7 @@ public static function decompose(NumericMatrix $A): QR */ public function solve($b): Vector { - // Input must be a Vector or array. @phpstan-ignore-next-line + // Input must be a Vector or array. if (!($b instanceof Vector || \is_array($b))) { throw new Exception\IncorrectTypeException('b in Ax = b must be a Vector or array'); } diff --git a/src/LinearAlgebra/Decomposition/SVD.php b/src/LinearAlgebra/Decomposition/SVD.php index 28ea10708..3bb807833 100644 --- a/src/LinearAlgebra/Decomposition/SVD.php +++ b/src/LinearAlgebra/Decomposition/SVD.php @@ -13,10 +13,10 @@ * The generalization of the eigendecomposition of a square matrix to an m x n matrix * https://en.wikipedia.org/wiki/Singular_value_decomposition * - * @property-read NumericMatrix $S m x n diagonal matrix - * @property-read NumericMatrix $V n x n orthogonal matrix - * @property-read NumericMatrix $U m x m orthogonal matrix - * @property-read Vector $D diagonal elements from S + * @property-read NumericMatrix $S m x n diagonal matrix + * @property-read NumericMatrix $V n x n orthogonal matrix + * @property-read NumericMatrix $U m x m orthogonal matrix + * @property-read Vector $D diagonal elements from S */ class SVD extends Decomposition { @@ -29,7 +29,7 @@ class SVD extends Decomposition /** @var NumericMatrix m x n diagonal matrix containing the singular values */ private $S; - /** @var Vector diagonal elements from S that are the singular values */ + /** @var Vector diagonal elements from S that are the singular values */ private $D; /** @@ -78,7 +78,7 @@ public function getV(): NumericMatrix /** * Get D * - * @return Vector + * @return Vector */ public function getD(): Vector { @@ -128,7 +128,7 @@ public static function decompose(NumericMatrix $M): SVD * * @param string $name * - * @return NumericMatrix|Vector + * @return NumericMatrix|Vector */ public function __get(string $name) { diff --git a/src/LinearAlgebra/Eigenvalue.php b/src/LinearAlgebra/Eigenvalue.php index 46e71bf0b..c4db0ef4c 100644 --- a/src/LinearAlgebra/Eigenvalue.php +++ b/src/LinearAlgebra/Eigenvalue.php @@ -108,10 +108,10 @@ public static function closedFormPolynomialRootMethod(NumericMatrix $A): array $det = $⟮B − λ⟯->det(); // Calculate the roots of the determinant. - /** @var array $eigenvalues */ + /** @var array $eigenvalues */ $eigenvalues = $det->roots(); \usort($eigenvalues, function ($a, $b) { - /** @var number $a */ /** @var number $b */ + /** @var int|float $a */ /** @var int|float $b */ return \abs($b) <=> \abs($a); }); diff --git a/src/LinearAlgebra/MatrixFactory.php b/src/LinearAlgebra/MatrixFactory.php index 273b75761..8a16fc99a 100644 --- a/src/LinearAlgebra/MatrixFactory.php +++ b/src/LinearAlgebra/MatrixFactory.php @@ -10,13 +10,15 @@ * Matrix factory to create matrices of all types. * Use factory instead of instantiating individual Matrix classes. * - * @template T = int[][]|float[][]|Complex[][]|object[][] + * template T = int[][]|float[][]|Complex[][]|object[][] */ class MatrixFactory { /** * Factory method * + * @template T = int|float|Complex|object + * * @param T[][] $A 2-dimensional array of Matrix data * @param float|null $ε Optional error tolerance * @@ -36,7 +38,7 @@ public static function create(array $A, ?float $ε = null): Matrix switch ($matrix_type) { case 'numeric': case 'numeric_square': - /** @var array> $A */ + /** @var array> $A */ return self::createNumeric($A, $ε); case 'complex': /** @var array> $A */ @@ -128,6 +130,8 @@ public static function createFromVectors(array $A, ?float $ε = null): NumericMa * [⋮ ] * [xm] * + * @template T = int|float|Complex|object + * * @param T[] $A m × 1 vector representing the matrix * * @return Matrix|NumericMatrix|ComplexMatrix|ObjectMatrix|ObjectSquareMatrix @@ -154,6 +158,8 @@ public static function createFromColumnVector(array $A): Matrix * * x = [x₁ x₂ ⋯ xn] * + * @template T = int|float|Complex|object + * * @param T[] $A 1 × n vector representing the matrix * * @return Matrix|NumericMatrix|ComplexMatrix|ObjectMatrix|ObjectSquareMatrix @@ -301,7 +307,7 @@ public static function downshiftPermutation(int $n): NumericSquareMatrix $bottom_row = \array_pop($I); \array_unshift($I, $bottom_row); - /** @var array> $I */ + /** @var array> $I */ return new NumericSquareMatrix($I); } @@ -451,7 +457,7 @@ public static function eye(int $m, int $n, int $k, float $x = null): NumericMatr * A = [0 2 0] * [0 0 3] * - * @param array $D elements of the diagonal + * @param array $D elements of the diagonal * * @return NumericDiagonalMatrix * @@ -518,7 +524,7 @@ public static function hilbert(int $n): NumericMatrix /** * Create the Vandermonde Matrix from a simple array. * - * @param array $M (α₁, α₂, α₃ ⋯ αm) + * @param array $M (α₁, α₂, α₃ ⋯ αm) * @param int $n * * @return NumericMatrix @@ -610,7 +616,9 @@ public static function random(int $m, int $n, int $min = 0, int $max = 20): Nume /** * Check input parameters * - * @param array $A + * @template T = int|float|Complex|object + * + * @param array> $A * * @throws Exception\BadDataException if array data not provided for matrix creation * @throws Exception\MatrixException if any row has a different column count @@ -620,13 +628,14 @@ private static function checkParams(array $A): void if (empty($A)) { throw new Exception\BadDataException('Array data not provided for Matrix creation'); } + if (!isset($A[0]) || !\is_array($A[0])) { + throw new Exception\BadDataException('Array of array data not provided for Matrix creation'); + } - if (isset($A[0]) && \is_array($A[0])) { - $column_count = \count($A[0]); - foreach ($A as $i => $row) { - if (\count($row) !== $column_count) { - throw new Exception\MatrixException("Row $i has a different column count: " . \count($row) . "; was expecting $column_count."); - } + $column_count = \count($A[0]); + foreach ($A as $i => $row) { + if (\count($row) !== $column_count) { + throw new Exception\MatrixException("Row $i has a different column count: " . \count($row) . "; was expecting $column_count."); } } } diff --git a/src/LinearAlgebra/NumericDiagonalMatrix.php b/src/LinearAlgebra/NumericDiagonalMatrix.php index 8748fad40..dc1e424f7 100644 --- a/src/LinearAlgebra/NumericDiagonalMatrix.php +++ b/src/LinearAlgebra/NumericDiagonalMatrix.php @@ -15,7 +15,7 @@ class NumericDiagonalMatrix extends NumericSquareMatrix /** * Constructor * - * @param array> $A + * @param array> $A */ public function __construct(array $A) { diff --git a/src/LinearAlgebra/NumericMatrix.php b/src/LinearAlgebra/NumericMatrix.php index cf0637453..52970dced 100644 --- a/src/LinearAlgebra/NumericMatrix.php +++ b/src/LinearAlgebra/NumericMatrix.php @@ -10,7 +10,7 @@ /** * m x n Matrix * - * @extends Matrix + * @extends Matrix */ class NumericMatrix extends Matrix { @@ -34,7 +34,7 @@ class NumericMatrix extends Matrix /** * Constructor * - * @param array> $A of arrays $A m x n matrix + * @param array> $A of arrays $A m x n matrix * * @throws Exception\BadDataException if any rows have a different column count */ @@ -1266,7 +1266,6 @@ public function subtract($B): NumericMatrix */ public function multiply($B): NumericMatrix { - // @phpstan-ignore-next-line if ((!$B instanceof NumericMatrix) && (!$B instanceof Vector)) { throw new Exception\IncorrectTypeException('Can only do matrix multiplication with a Matrix or Vector'); } @@ -1281,15 +1280,16 @@ public function multiply($B): NumericMatrix $Bᵀ = $B->transpose()->getMatrix(); foreach ($this->A as $i => $Aʳᵒʷ⟦i⟧) { - /** @var array $R */ $R[$i] = \array_fill(0, $B->n, 0); foreach ($Bᵀ as $j => $Bᶜᵒˡ⟦j⟧) { foreach ($Aʳᵒʷ⟦i⟧ as $k => $A⟦i⟧⟦k⟧) { + // @phpstan-ignore-next-line (Remove in PHP 8.0, no longer returns false) $R[$i][$j] += $A⟦i⟧⟦k⟧ * $Bᶜᵒˡ⟦j⟧[$k]; } } } + // @phpstan-ignore-next-line (Due to above false from array_fill) return MatrixFactory::createNumeric($R, $this->ε); } @@ -2053,7 +2053,7 @@ public function columnMeans(): Vector * * tr(A) = a₁₁ + a₂₂ + ... ann * - * @return number + * @return int|float * * @throws Exception\MatrixException if the matrix is not a square matrix */ @@ -2077,7 +2077,7 @@ public function trace() * 1-norm (‖A‖₁) * Maximum absolute column sum of the matrix * - * @return number + * @return int|float */ public function oneNorm() { @@ -2102,7 +2102,7 @@ public function oneNorm() * ‖A‖F = √ Σ Σ |aᵢⱼ|² * ᵢ₌₁ ᵢ₌₁ * - * @return number + * @return int|float */ public function frobeniusNorm() { @@ -2123,7 +2123,7 @@ public function frobeniusNorm() * Infinity norm (‖A‖∞) * Maximum absolute row sum of the matrix * - * @return number + * @return int|float */ public function infinityNorm() { @@ -2141,7 +2141,7 @@ public function infinityNorm() * Max norm (‖A‖max) * Elementwise max * - * @return number + * @return int|float */ public function maxNorm() { @@ -2187,7 +2187,7 @@ public function maxNorm() * │ref(A)│ = determinant of the row echelon form of A * ⁿ = number of row swaps when computing REF * - * @return number + * @return int|float * * @throws Exception\MatrixException if matrix is not square * @throws Exception\IncorrectTypeException @@ -2196,7 +2196,7 @@ public function maxNorm() public function det() { if ($this->catalog->hasDeterminant()) { - /** @var number */ + /** @var int|float */ return $this->catalog->getDeterminant(); } @@ -2316,7 +2316,7 @@ public function det() * @param int $mᵢ Row to exclude * @param int $nⱼ Column to exclude * - * @return number + * @return int|float * * @throws Exception\MatrixException if matrix is not square * @throws Exception\MatrixException if row to exclude for cofactor does not exist @@ -2893,8 +2893,8 @@ public function svd(): Decomposition\SVD * Otherwise, it is more efficient to decompose and then solve. * Use LU Decomposition and solve Ax = b. * - * @param Vector|array $b solution to Ax = b - * @param string $method (optional) Force a specific solve method - defaults to DEFAULT where various methods are tried + * @param Vector|array $b solution to Ax = b + * @param string $method (optional) Force a specific solve method - defaults to DEFAULT where various methods are tried * * @return Vector x * @@ -2907,7 +2907,6 @@ public function svd(): Decomposition\SVD public function solve($b, string $method = self::DEFAULT): Vector { // Input must be a Vector or array. - // @phpstan-ignore-next-line if (!($b instanceof Vector || \is_array($b))) { throw new Exception\IncorrectTypeException('b in Ax = b must be a Vector or array'); } @@ -3025,7 +3024,7 @@ private function solveRref(Vector $b): Vector * * @param string $method Algorithm used to compute the eigenvalues * - * @return array of eigenvalues + * @return array of eigenvalues * * @throws Exception\MatrixException if method is not a valid eigenvalue method * @throws Exception\MathException diff --git a/src/LinearAlgebra/NumericSquareMatrix.php b/src/LinearAlgebra/NumericSquareMatrix.php index fcf716c11..b263de142 100644 --- a/src/LinearAlgebra/NumericSquareMatrix.php +++ b/src/LinearAlgebra/NumericSquareMatrix.php @@ -14,7 +14,7 @@ class NumericSquareMatrix extends NumericMatrix /** * Constructor * - * @param array> $A + * @param array> $A * * @throws Exception\MathException */ diff --git a/src/LinearAlgebra/ObjectMatrix.php b/src/LinearAlgebra/ObjectMatrix.php index 749b844af..beec727dc 100644 --- a/src/LinearAlgebra/ObjectMatrix.php +++ b/src/LinearAlgebra/ObjectMatrix.php @@ -56,7 +56,6 @@ protected function validateMatrixData(): void { if ($this->A[0][0] instanceof ObjectArithmetic) { $this->object_type = \get_class($this->A[0][0]); - // @phpstan-ignore-next-line } else { throw new Exception\IncorrectTypeException("The object must implement the interface."); } @@ -271,7 +270,7 @@ public function scalarMultiply($λ): Matrix /** * {@inheritDoc} * - * @return number + * @return int|float * * @throws Exception\MatrixException if the matrix is not a square matrix */ diff --git a/src/LinearAlgebra/Reduction/ReducedRowEchelonForm.php b/src/LinearAlgebra/Reduction/ReducedRowEchelonForm.php index ccc8fee03..cc27bdc25 100644 --- a/src/LinearAlgebra/Reduction/ReducedRowEchelonForm.php +++ b/src/LinearAlgebra/Reduction/ReducedRowEchelonForm.php @@ -29,7 +29,7 @@ class ReducedRowEchelonForm extends NumericMatrix /** * ReducedRowEchelonForm constructor * - * @param array> $A + * @param array> $A * * @throws Exception\BadDataException */ diff --git a/src/LinearAlgebra/Reduction/RowEchelonForm.php b/src/LinearAlgebra/Reduction/RowEchelonForm.php index ee49b0c32..510c6d7d7 100644 --- a/src/LinearAlgebra/Reduction/RowEchelonForm.php +++ b/src/LinearAlgebra/Reduction/RowEchelonForm.php @@ -24,8 +24,8 @@ class RowEchelonForm extends NumericMatrix /** * RowEchelonForm constructor - * @param array> $A - * @param int $swaps Number of row swaps when computing REF + * @param array> $A + * @param int $swaps Number of row swaps when computing REF * * @throws Exception\BadDataException */ @@ -114,7 +114,7 @@ public static function reduce(NumericMatrix $A): RowEchelonForm * * @param NumericMatrix $A * - * @return array{array>, int} - matrix in row echelon form and number of row swaps + * @return array{array>, int} - matrix in row echelon form and number of row swaps * * @throws Exception\SingularMatrixException if the matrix is singular */ @@ -179,7 +179,7 @@ public static function gaussianElimination(NumericMatrix $A): array * * @param NumericMatrix $A * - * @return array{array>, int} - matrix in row echelon form and number of row swaps + * @return array{array>, int} - matrix in row echelon form and number of row swaps * * @throws Exception\IncorrectTypeException * @throws Exception\MatrixException diff --git a/src/LinearAlgebra/Vector.php b/src/LinearAlgebra/Vector.php index 9d62e89a6..75d56d82e 100644 --- a/src/LinearAlgebra/Vector.php +++ b/src/LinearAlgebra/Vector.php @@ -9,15 +9,15 @@ /** * 1 x n Vector * - * @implements \Iterator - * @implements \ArrayAccess + * @implements \Iterator + * @implements \ArrayAccess */ class Vector implements \Countable, \Iterator, \ArrayAccess, \JsonSerializable { /** @var int Number of elements */ private $n; - /** @var array of numbers */ + /** @var array of numbers */ private $A; /** @var int Iterator position */ @@ -26,7 +26,7 @@ class Vector implements \Countable, \Iterator, \ArrayAccess, \JsonSerializable /** * Constructor * - * @param array $A 1 x n vector + * @param array $A 1 x n vector * * @throws Exception\BadDataException if the Vector is empty */ @@ -53,7 +53,7 @@ public function __construct(array $A) /** * Get matrix * - * @return array + * @return array */ public function getVector(): array { @@ -75,7 +75,7 @@ public function getN(): int * * @param int $i index * - * @return number + * @return int|float * * @throws Exception\VectorException */ @@ -160,7 +160,7 @@ public function sum() * Vector length (magnitude) * Same as l2-norm * - * @return number + * @return int|float */ public function length() { @@ -170,7 +170,7 @@ public function length() /** * Max of all the elements * - * @return number|false + * @return int|float|false * * Note: Remove false from return value after PHP 8.0 */ @@ -182,7 +182,7 @@ public function max() /** * Min of all the elements * - * @return number|false + * @return int|float|false * * Note: Remove false from return value after PHP 8.0 */ @@ -714,7 +714,7 @@ public function pNorm($p) * * |x|∞ = max |x| * - * @return number|false + * @return int|float|false * * Note: Remove false from return value after PHP 8.0 */ @@ -767,7 +767,7 @@ public function offsetExists($i): bool /** * @param mixed $i - * @return number + * @return int|float */ #[\ReturnTypeWillChange] public function offsetGet($i) @@ -777,7 +777,7 @@ public function offsetGet($i) /** * @param int $i - * @param number $value + * @param int|float $value * @throws Exception\VectorException */ public function offsetSet($i, $value): void @@ -799,7 +799,7 @@ public function offsetUnset($i): void **************************************************************************/ /** - * @return array + * @return array */ public function jsonSerialize(): array { @@ -816,7 +816,7 @@ public function rewind(): void } /** - * @return number + * @return int|float */ #[\ReturnTypeWillChange] public function current() diff --git a/src/Number/Complex.php b/src/Number/Complex.php index e7135f617..d58b03543 100644 --- a/src/Number/Complex.php +++ b/src/Number/Complex.php @@ -14,20 +14,20 @@ * part of the complex number. * https://en.wikipedia.org/wiki/Complex_number * - * @property-read number $r - * @property-read number $i + * @property-read int|float $r + * @property-read int|float $i */ class Complex implements ObjectArithmetic { /** * Real part of the complex number - * @var number + * @var int|float */ protected $r; /** * Imaginary part fo the complex number - * @var number + * @var int|float */ protected $i; @@ -84,7 +84,7 @@ public function __toString(): string * * @param string $part * - * @return number + * @return int|float * * @throws Exception\BadParameterException if something other than r or i is attempted */ @@ -124,7 +124,7 @@ public function complexConjugate(): Complex * _______ * |z| = √a² + b² * - * @return number + * @return int|float */ public function abs() { @@ -139,7 +139,7 @@ public function abs() * If z = a + bi * arg(z) = atan(b, a) * - * @return number + * @return int|float */ public function arg() { @@ -244,7 +244,7 @@ public function negate(): Complex * r = |z| * θ = arg(z) (in radians) * - * @return number[] + * @return int[]|float[] */ public function polarForm(): array { @@ -406,7 +406,6 @@ public function pow($c): Complex return new Complex($new_r, $new_i); } - // @phpstan-ignore-next-line throw new Exception\IncorrectTypeException('Argument must be real or complex number'); } diff --git a/src/Number/Quaternion.php b/src/Number/Quaternion.php index fb9f54c5a..90d7bde22 100644 --- a/src/Number/Quaternion.php +++ b/src/Number/Quaternion.php @@ -14,16 +14,16 @@ */ class Quaternion implements ObjectArithmetic { - /** @var number Real part of the quaternionic number */ + /** @var int|float Real part of the quaternionic number */ protected $r; - /** @var number First Imaginary part of the quaternionic number */ + /** @var int|float First Imaginary part of the quaternionic number */ protected $i; - /** @var number Second Imaginary part of the quaternionic number */ + /** @var int|float Second Imaginary part of the quaternionic number */ protected $j; - /** @var number Third Imaginary part of the quaternionic number */ + /** @var int|float Third Imaginary part of the quaternionic number */ protected $k; /** Floating-point range near zero to consider insignificant */ @@ -37,7 +37,6 @@ class Quaternion implements ObjectArithmetic */ public function __construct($r, $i, $j, $k) { - // @phpstan-ignore-next-line if (!\is_numeric($r) || !\is_numeric($i) || !\is_numeric($j) || !\is_numeric($k)) { throw new Exception\BadDataException('Values must be real numbers.'); } @@ -79,7 +78,7 @@ public function __toString(): string * * @param string $part * - * @return number + * @return int|float * * @throws Exception\BadParameterException if something other than r or i is attempted */ @@ -122,7 +121,7 @@ public function complexConjugate(): Quaternion * _________________ * |z| = √a² + b² + c² + d² * - * @return number + * @return int|float */ public function abs() { @@ -172,7 +171,7 @@ public function negate(): Quaternion * * (a + bi + cj + dk) - (e + fi + gj + hk) = (a + e) + (b + f)i + (c + g)j + (d + h)k * - * @param mixed $q + * @param int|float|Quaternion $q * * @return Quaternion * @@ -180,10 +179,10 @@ public function negate(): Quaternion */ public function add($q): Quaternion { - if (!is_numeric($q) && ! $q instanceof Quaternion) { - throw new Exception\IncorrectTypeException('Argument must be real or quaternion' . print_r($q, true)); + if (!\is_numeric($q) && ! $q instanceof Quaternion) { + throw new Exception\IncorrectTypeException('Argument must be real or quaternion' . \print_r($q, true)); } - if (is_numeric($q)) { + if (\is_numeric($q)) { $r = $this->r + $q; return new Quaternion($r, $this->i, $this->j, $this->k); } @@ -202,7 +201,7 @@ public function add($q): Quaternion * * (a + bi + cj + dk) - (e + fi + gj + hk) = (a - e) + (b - f)i + (c - g)j + (d - h)k * - * @param mixed $q + * @param int|float|Quaternion $q * * @return Quaternion * @@ -213,7 +212,7 @@ public function subtract($q): Quaternion if (!is_numeric($q) && ! $q instanceof Quaternion) { throw new Exception\IncorrectTypeException('Argument must be real or quaternion' . print_r($q, true)); } - if (is_numeric($q)) { + if (\is_numeric($q)) { $r = $this->r - $q; return new Quaternion($r, $this->i, $this->j, $this->k); } @@ -238,7 +237,7 @@ public function subtract($q): Quaternion * * Note: Quaternion multiplication is not commutative. * - * @param mixed $q + * @param int|float|Quaternion $q * * @return Quaternion * @@ -249,7 +248,7 @@ public function multiply($q): Quaternion if (!is_numeric($q) && ! $q instanceof Quaternion) { throw new Exception\IncorrectTypeException('Argument must be real or quaternion' . print_r($q, true)); } - if (is_numeric($q)) { + if (\is_numeric($q)) { return new Quaternion($this->r * $q, $this->i * $q, $this->j * $q, $this->k * $q); } @@ -269,7 +268,7 @@ public function multiply($q): Quaternion * Dividing two quaternions is accomplished by multiplying the first by the inverse of the second * This is not commutative! * - * @param mixed $q + * @param int|float|Quaternion $q * * @return Quaternion * @@ -281,7 +280,7 @@ public function divide($q): Quaternion throw new Exception\IncorrectTypeException('Argument must be real or quaternion' . print_r($q, true)); } - if (is_numeric($q)) { + if (\is_numeric($q)) { $r = $this->r / $q; $i = $this->i / $q; $j = $this->j / $q; @@ -318,7 +317,7 @@ public function equals(Quaternion $q): bool /** * Stringify an additional part of the quaternion * - * @param number $q + * @param int|float $q * @param string $unit * @param string $string * @return string diff --git a/src/NumericalAnalysis/Interpolation/ClampedCubicSpline.php b/src/NumericalAnalysis/Interpolation/ClampedCubicSpline.php index ca4ba5b2d..d1f388632 100644 --- a/src/NumericalAnalysis/Interpolation/ClampedCubicSpline.php +++ b/src/NumericalAnalysis/Interpolation/ClampedCubicSpline.php @@ -37,13 +37,13 @@ class ClampedCubicSpline extends Interpolation /** * Interpolate * - * @param callable|array $source + * @param callable|array $source * The source of our approximation. Should be either * a callback function or a set of arrays. Each array * (point) contains precisely three numbers: x, y, and y' * Example array: [[1,2,1], [2,3,0], [3,4,2]]. * Example callback: function($x) {return $x**2;} - * @param mixed ...$args + * @param array{callable, int|float, int|float, int|float} ...$args * (Optional) An additional callback: our first derivative, * and arguments of our callback functions: start, * end, and n. @@ -59,7 +59,7 @@ class ClampedCubicSpline extends Interpolation */ public static function interpolate($source, ...$args): Piecewise { - // Get an array of points from our $source argument + // Get an array of points from our $source argument @phpstan-ignore-next-line $points = self::getSplinePoints($source, $args); // Validate input and sort points @@ -156,22 +156,21 @@ public static function interpolate($source, ...$args): Piecewise * @todo Add method to verify input arguments are valid. * Verify $start and $end are numbers, $end > $start, and $points is an integer > 1 * - * @param callable|array> $source + * @param callable|array> $source * The source of our approximation. Should be either * a callback function or a set of arrays. - * @param array{callable, number, number, number}|array $args + * @param array{callable, int|float, int|float, int|float} $args * The arguments of our callback function: derivative, * start, end, and n. Example: [$derivative, 0, 8, 5]. * If $source is a set of arrays, $args will default to []. * - * @return array + * @return array * * @throws Exception\BadDataException if $source is not callable or a set of arrays */ - public static function getSplinePoints($source, array $args = []): array + public static function getSplinePoints($source, array $args): array { // Guard clause - source must be callable or array of points - // @phpstan-ignore-next-line if (!(\is_callable($source) || \is_array($source))) { throw new Exception\BadDataException('Input source is incorrect. You need to input either a callback function or a set of arrays'); } @@ -202,7 +201,7 @@ public static function getSplinePoints($source, array $args = []): array * @param float $end the end of the interval * @param int $n the number of function evaluations * - * @return array + * @return array */ protected static function functionToSplinePoints(callable $function, callable $derivative, float $start, float $end, int $n): array { @@ -224,7 +223,7 @@ protected static function functionToSplinePoints(callable $function, callable $d * has precisely three numbers, and that no two points share the same first number * (x-component) * - * @param array $points Array of arrays (points) + * @param array $points Array of arrays (points) * @param int $degree The minimum number of input arrays * * @throws Exception\BadDataException if there are less than two points @@ -239,7 +238,6 @@ public static function validateSpline(array $points, int $degree = 2): void $x_coordinates = []; foreach ($points as $point) { - // @phpstan-ignore-next-line if (\count($point) !== 3) { throw new Exception\BadDataException('Each array needs to have have precisely three numbers, representing x, y, and y-prime'); } diff --git a/src/NumericalAnalysis/Interpolation/Interpolation.php b/src/NumericalAnalysis/Interpolation/Interpolation.php index b4c3d2b03..16ec00194 100644 --- a/src/NumericalAnalysis/Interpolation/Interpolation.php +++ b/src/NumericalAnalysis/Interpolation/Interpolation.php @@ -26,7 +26,7 @@ abstract class Interpolation * @todo Add method to verify input arguments are valid. * Verify $start and $end are numbers, $end > $start, and $points is an integer > 1 * - * @param callable|array $source + * @param callable|array $source * The source of our approximation. Should be either a callback function or a set of arrays. * @param array $args * The arguments of our callback function: start, end, and n. @@ -39,7 +39,6 @@ abstract class Interpolation public static function getPoints($source, array $args = []): array { // Guard clause - source must be callable or array of points - // @phpstan-ignore-next-line if (!(\is_callable($source) || \is_array($source))) { throw new Exception\BadDataException('Input source is incorrect. You need to input either a callback function or a set of arrays'); } @@ -102,7 +101,6 @@ public static function validate(array $points, int $degree = 2): void $x_coordinates = []; foreach ($points as $point) { - // @phpstan-ignore-next-line if (\count($point) !== 2) { throw new Exception\BadDataException('Each array needs to have have precisely two numbers, an x- and y-component'); } diff --git a/src/NumericalAnalysis/Interpolation/LagrangePolynomial.php b/src/NumericalAnalysis/Interpolation/LagrangePolynomial.php index e461e1486..e7de91573 100644 --- a/src/NumericalAnalysis/Interpolation/LagrangePolynomial.php +++ b/src/NumericalAnalysis/Interpolation/LagrangePolynomial.php @@ -31,7 +31,7 @@ class LagrangePolynomial extends Interpolation /** * Interpolate * - * @param callable|array $source + * @param callable|array $source * The source of our approximation. Should be either * a callback function or a set of arrays. Each array * (point) contains precisely two numbers, an x and y. diff --git a/src/NumericalAnalysis/Interpolation/NaturalCubicSpline.php b/src/NumericalAnalysis/Interpolation/NaturalCubicSpline.php index 7326d2ee9..e691ee469 100644 --- a/src/NumericalAnalysis/Interpolation/NaturalCubicSpline.php +++ b/src/NumericalAnalysis/Interpolation/NaturalCubicSpline.php @@ -34,7 +34,7 @@ class NaturalCubicSpline extends Interpolation /** * Interpolate * - * @param callable|array $source + * @param callable|array $source * The source of our approximation. Should be either * a callback function or a set of arrays. Each array * (point) contains precisely two numbers, an x and y. diff --git a/src/NumericalAnalysis/Interpolation/NevillesMethod.php b/src/NumericalAnalysis/Interpolation/NevillesMethod.php index b32318571..f8dfd460b 100644 --- a/src/NumericalAnalysis/Interpolation/NevillesMethod.php +++ b/src/NumericalAnalysis/Interpolation/NevillesMethod.php @@ -29,7 +29,7 @@ class NevillesMethod extends Interpolation * * @param float $target * The point at which we are interpolation - * @param callable|array $source + * @param callable|array $source * The source of our approximation. Should be either * a callback function or a set of arrays. Each array * (point) contains precisely two numbers, an x and y. diff --git a/src/NumericalAnalysis/Interpolation/NewtonPolynomialForward.php b/src/NumericalAnalysis/Interpolation/NewtonPolynomialForward.php index 8f8550f7c..67155dc4c 100644 --- a/src/NumericalAnalysis/Interpolation/NewtonPolynomialForward.php +++ b/src/NumericalAnalysis/Interpolation/NewtonPolynomialForward.php @@ -26,7 +26,7 @@ class NewtonPolynomialForward extends Interpolation /** * Interpolate * - * @param callable|array $source + * @param callable|array $source * The source of our approximation. Should be either * a callback function or a set of arrays. Each array * (point) contains precisely two numbers, an x and y. diff --git a/src/NumericalAnalysis/Interpolation/RegularGridInterpolator.php b/src/NumericalAnalysis/Interpolation/RegularGridInterpolator.php index 41fe2103f..4dbf58217 100644 --- a/src/NumericalAnalysis/Interpolation/RegularGridInterpolator.php +++ b/src/NumericalAnalysis/Interpolation/RegularGridInterpolator.php @@ -53,16 +53,16 @@ class RegularGridInterpolator /** @var string Interpolation method (linear or nearest) */ private $method; - /** @var array> Points defining the regular grid in n dimensions */ + /** @var array> Points defining the regular grid in n dimensions */ private $grid; /** @var array Data on the regular grid in n dimensions */ private $values; /** - * @param array> $points Points defining the regular grid in n dimensions - * @param array $values Data on the regular grid in n dimensions - * @param string $method (optional - default: linear) Interpolation method (linear or nearest) + * @param array> $points Points defining the regular grid in n dimensions + * @param array $values Data on the regular grid in n dimensions + * @param string $method (optional - default: linear) Interpolation method (linear or nearest) * * @throws Exception\BadDataException the points and value dimensions do not align, or if an unknown method is used */ @@ -126,8 +126,8 @@ public function __invoke(array $xi): float } /** - * @param array $indices - * @param array $normDistances + * @param array $indices + * @param array $normDistances * * @return float|int */ @@ -155,8 +155,8 @@ private function evaluateLinear(array $indices, array $normDistances) } /** - * @param array $indices - * @param array $normDistances + * @param array $indices + * @param array $normDistances * * @return float|int */ diff --git a/src/NumericalAnalysis/NumericalDifferentiation/FivePointFormula.php b/src/NumericalAnalysis/NumericalDifferentiation/FivePointFormula.php index 3b99c027a..8c3446868 100644 --- a/src/NumericalAnalysis/NumericalDifferentiation/FivePointFormula.php +++ b/src/NumericalAnalysis/NumericalDifferentiation/FivePointFormula.php @@ -53,7 +53,7 @@ class FivePointFormula extends NumericalDifferentiation * * @param float $target * The value at which we are approximating the derivative - * @param callable|array $source + * @param callable|array $source * The source of our approximation. Should be either * a callback function or a set of arrays. Each array * (point) contains precisely two numbers, an x and y. diff --git a/src/NumericalAnalysis/NumericalDifferentiation/NumericalDifferentiation.php b/src/NumericalAnalysis/NumericalDifferentiation/NumericalDifferentiation.php index 46d45dd89..247ad6ac4 100644 --- a/src/NumericalAnalysis/NumericalDifferentiation/NumericalDifferentiation.php +++ b/src/NumericalAnalysis/NumericalDifferentiation/NumericalDifferentiation.php @@ -24,8 +24,8 @@ abstract class NumericalDifferentiation /** * @param float $target - * @param callable|array $source - * @param number ...$args + * @param callable|array $source + * @param int|float ...$args * @return mixed */ abstract public static function differentiate(float $target, $source, ...$args); @@ -41,7 +41,7 @@ abstract public static function differentiate(float $target, $source, ...$args); * @todo Add method to verify input arguments are valid. * Verify $start and $end are numbers, $end > $start, and $points is an integer > 1 * - * @param callable|array $source + * @param callable|array $source * The source of our approximation. Should be either * a callback function or a set of arrays. * @param array $args @@ -55,7 +55,6 @@ abstract public static function differentiate(float $target, $source, ...$args); public static function getPoints($source, array $args = []): array { // Guard clause - source must be callable or array of points - // @phpstan-ignore-next-line if (!(\is_callable($source) || \is_array($source))) { throw new Exception\BadDataException('Input source is incorrect. You need to input either a callback function or a set of arrays'); } @@ -119,7 +118,6 @@ public static function validate(array $points, int $degree): void $x_coordinates = []; foreach ($points as $point) { - // @phpstan-ignore-next-line if (\count($point) !== 2) { throw new Exception\BadDataException('Each array needs to have have precisely two numbers, an x- and y-component'); } diff --git a/src/NumericalAnalysis/NumericalDifferentiation/SecondDerivativeMidpointFormula.php b/src/NumericalAnalysis/NumericalDifferentiation/SecondDerivativeMidpointFormula.php index 8e31c2c8f..f165e479a 100644 --- a/src/NumericalAnalysis/NumericalDifferentiation/SecondDerivativeMidpointFormula.php +++ b/src/NumericalAnalysis/NumericalDifferentiation/SecondDerivativeMidpointFormula.php @@ -42,7 +42,7 @@ class SecondDerivativeMidpointFormula extends NumericalDifferentiation * * @param float $target * The value at which we are approximating the derivative - * @param callable|array $source + * @param callable|array $source * The source of our approximation. Should be either * a callback function or a set of arrays. Each array * (point) contains precisely two numbers, an x and y. diff --git a/src/NumericalAnalysis/NumericalDifferentiation/ThreePointFormula.php b/src/NumericalAnalysis/NumericalDifferentiation/ThreePointFormula.php index f0a53a03a..1fbca659e 100644 --- a/src/NumericalAnalysis/NumericalDifferentiation/ThreePointFormula.php +++ b/src/NumericalAnalysis/NumericalDifferentiation/ThreePointFormula.php @@ -53,7 +53,7 @@ class ThreePointFormula extends NumericalDifferentiation * * @param float $target * The value at which we are approximating the derivative - * @param callable|array $source + * @param callable|array $source * The source of our approximation. Should be either * a callback function or a set of arrays. Each array * (point) contains precisely two numbers, an x and y. diff --git a/src/NumericalAnalysis/NumericalIntegration/BoolesRule.php b/src/NumericalAnalysis/NumericalIntegration/BoolesRule.php index 617acd69c..2e301221e 100644 --- a/src/NumericalAnalysis/NumericalIntegration/BoolesRule.php +++ b/src/NumericalAnalysis/NumericalIntegration/BoolesRule.php @@ -63,7 +63,7 @@ class BoolesRule extends NumericalIntegration * ⁱ⁼¹ 45 * where h = (xn - x₁) / (n - 1) * - * @param callable|array $source + * @param callable|array $source * The source of our approximation. Should be either * a callback function or a set of arrays. Each array * (point) contains precisely two numbers, an x and y. diff --git a/src/NumericalAnalysis/NumericalIntegration/MidpointRule.php b/src/NumericalAnalysis/NumericalIntegration/MidpointRule.php index bf0e4917b..f5d67161a 100644 --- a/src/NumericalAnalysis/NumericalIntegration/MidpointRule.php +++ b/src/NumericalAnalysis/NumericalIntegration/MidpointRule.php @@ -55,7 +55,7 @@ class MidpointRule extends NumericalIntegration * * where h = xᵢ₊₁ - xᵢ * note: this implementation does not compute the error term. - * @param callable|array $source + * @param callable|array $source * The source of our approximation. Should be either * a callback function or a set of arrays. Each array * (point) contains precisely two numbers, an x and y. diff --git a/src/NumericalAnalysis/NumericalIntegration/NumericalIntegration.php b/src/NumericalAnalysis/NumericalIntegration/NumericalIntegration.php index 9cc6d40a0..8613da1c6 100644 --- a/src/NumericalAnalysis/NumericalIntegration/NumericalIntegration.php +++ b/src/NumericalAnalysis/NumericalIntegration/NumericalIntegration.php @@ -23,9 +23,9 @@ abstract class NumericalIntegration protected const Y = 1; /** - * @param callable|array $source - * @param number ...$args - * @return number + * @param callable|array $source + * @param int|float ...$args + * @return int|float */ abstract public static function approximate($source, ...$args); @@ -40,7 +40,7 @@ abstract public static function approximate($source, ...$args); * @todo Add method to verify input arguments are valid. * Verify $start and $end are numbers, $end > $start, and $points is an integer > 1 * - * @param callable|array $source + * @param callable|array $source * The source of our approximation. Should be either * a callback function or a set of arrays. * @param array $args @@ -55,7 +55,6 @@ abstract public static function approximate($source, ...$args); public static function getPoints($source, array $args = []): array { // Guard clause - source must be callable or array of points - // @phpstan-ignore-next-line if (!(\is_callable($source) || \is_array($source))) { throw new Exception\BadDataException('Input source is incorrect. You need to input either a callback function or a set of arrays'); } @@ -102,8 +101,8 @@ protected static function functionToPoints(callable $function, float $start, flo * has precisely two numbers, and that no two points share the same first number * (x-component) * - * @param array $points Array of arrays (points) - * @param int $degree The minimum number of input arrays + * @param array $points Array of arrays (points) + * @param int $degree The minimum number of input arrays * * @throws Exception\BadDataException if there are less than two points * @throws Exception\BadDataException if any point does not contain two numbers @@ -117,7 +116,6 @@ public static function validate(array $points, int $degree = 2): void $x_coordinates = []; foreach ($points as $point) { - // @phpstan-ignore-next-line if (\count($point) !== 2) { throw new Exception\BadDataException('Each array needs to have have precisely two numbers, an x- and y-component'); } diff --git a/src/NumericalAnalysis/NumericalIntegration/RectangleMethod.php b/src/NumericalAnalysis/NumericalIntegration/RectangleMethod.php index 430ea3bd2..587e0c6ff 100644 --- a/src/NumericalAnalysis/NumericalIntegration/RectangleMethod.php +++ b/src/NumericalAnalysis/NumericalIntegration/RectangleMethod.php @@ -58,7 +58,7 @@ class RectangleMethod extends NumericalIntegration * * where h = xᵢ₊₁ - xᵢ * note: this implementation does not compute the error term. - * @param callable|array $source + * @param callable|array $source * The source of our approximation. Should be either * a callback function or a set of arrays. Each array * (point) contains precisely two numbers, an x and y. diff --git a/src/NumericalAnalysis/NumericalIntegration/SimpsonsRule.php b/src/NumericalAnalysis/NumericalIntegration/SimpsonsRule.php index aba2f456c..1861523da 100644 --- a/src/NumericalAnalysis/NumericalIntegration/SimpsonsRule.php +++ b/src/NumericalAnalysis/NumericalIntegration/SimpsonsRule.php @@ -62,7 +62,7 @@ class SimpsonsRule extends NumericalIntegration * ⁱ⁼¹ 3 * where h = (xn - x₁) / (n - 1) * - * @param callable|array $source + * @param callable|array $source * The source of our approximation. Should be either * a callback function or a set of arrays. Each array * (point) contains precisely two numbers, an x and y. diff --git a/src/NumericalAnalysis/NumericalIntegration/SimpsonsThreeEighthsRule.php b/src/NumericalAnalysis/NumericalIntegration/SimpsonsThreeEighthsRule.php index a19d656c2..30d37510e 100644 --- a/src/NumericalAnalysis/NumericalIntegration/SimpsonsThreeEighthsRule.php +++ b/src/NumericalAnalysis/NumericalIntegration/SimpsonsThreeEighthsRule.php @@ -63,7 +63,7 @@ class SimpsonsThreeEighthsRule extends NumericalIntegration * ⁱ⁼¹ 8 * where h = (xn - x₁) / (n - 1) * - * @param callable|array $source + * @param callable|array $source * The source of our approximation. Should be either * a callback function or a set of arrays. Each array * (point) contains precisely two numbers, an x and y. diff --git a/src/NumericalAnalysis/NumericalIntegration/TrapezoidalRule.php b/src/NumericalAnalysis/NumericalIntegration/TrapezoidalRule.php index e0f597dbc..66dda5fc0 100644 --- a/src/NumericalAnalysis/NumericalIntegration/TrapezoidalRule.php +++ b/src/NumericalAnalysis/NumericalIntegration/TrapezoidalRule.php @@ -59,7 +59,7 @@ class TrapezoidalRule extends NumericalIntegration * * where h = xᵢ₊₁ - xᵢ * note: this implementation does not compute the error term. - * @param callable|array $source + * @param callable|array $source * The source of our approximation. Should be either * a callback function or a set of arrays. Each array * (point) contains precisely two numbers, an x and y. diff --git a/src/NumericalAnalysis/NumericalIntegration/Validation.php b/src/NumericalAnalysis/NumericalIntegration/Validation.php index fb290a917..eb7a14542 100644 --- a/src/NumericalAnalysis/NumericalIntegration/Validation.php +++ b/src/NumericalAnalysis/NumericalIntegration/Validation.php @@ -14,7 +14,7 @@ class Validation * Ensures that the length of each subinterval is equal, or equivalently, * that the spacing between each point is equal * - * @param array> $sorted Points sorted by (increasing) x-component + * @param array> $sorted Points sorted by (increasing) x-component * * @throws Exception\BadDataException if the spacing between any two points is not equal to the average spacing between every point */ diff --git a/src/NumericalAnalysis/RootFinding/BisectionMethod.php b/src/NumericalAnalysis/RootFinding/BisectionMethod.php index 6360d533f..90e76e511 100644 --- a/src/NumericalAnalysis/RootFinding/BisectionMethod.php +++ b/src/NumericalAnalysis/RootFinding/BisectionMethod.php @@ -31,7 +31,7 @@ class BisectionMethod * @param int|float $b The end of the interval which contains a root * @param int|float $tol Tolerance; How close to the actual solution we would like. - * @return number + * @return int|float * * @throws Exception\OutOfBoundsException * @throws Exception\BadDataException diff --git a/src/NumericalAnalysis/RootFinding/FixedPointIteration.php b/src/NumericalAnalysis/RootFinding/FixedPointIteration.php index 678cd09df..b77e8ed6a 100644 --- a/src/NumericalAnalysis/RootFinding/FixedPointIteration.php +++ b/src/NumericalAnalysis/RootFinding/FixedPointIteration.php @@ -33,7 +33,7 @@ class FixedPointIteration * @param int|float $p The initial guess of our root, in [$a, $b] * @param int|float $tol Tolerance; How close to the actual solution we would like. - * @return number + * @return int|float * * @throws Exception\OutOfBoundsException * @throws Exception\BadDataException diff --git a/src/NumericalAnalysis/RootFinding/NewtonsMethod.php b/src/NumericalAnalysis/RootFinding/NewtonsMethod.php index 1a94effe5..77c03ac6e 100644 --- a/src/NumericalAnalysis/RootFinding/NewtonsMethod.php +++ b/src/NumericalAnalysis/RootFinding/NewtonsMethod.php @@ -25,7 +25,7 @@ class NewtonsMethod * @param int $position Which element in the $args array will be changed; also serves as initial guess * @param int $iterations * - * @return number + * @return int|float * * @throws Exception\OutOfBoundsException if the tolerance is not valid */ diff --git a/src/Probability/Distribution/Continuous/Beta.php b/src/Probability/Distribution/Continuous/Beta.php index eb3883fc5..c7a966d68 100644 --- a/src/Probability/Distribution/Continuous/Beta.php +++ b/src/Probability/Distribution/Continuous/Beta.php @@ -32,10 +32,10 @@ class Beta extends Continuous 'x' => '[0,1]', ]; - /** @var number Shape Parameter */ + /** @var int|float Shape Parameter */ protected $α; - /** @var number Shape Parameter */ + /** @var int|float Shape Parameter */ protected $β; /** diff --git a/src/Probability/Distribution/Continuous/Cauchy.php b/src/Probability/Distribution/Continuous/Cauchy.php index 941b73b29..109f94b2f 100644 --- a/src/Probability/Distribution/Continuous/Cauchy.php +++ b/src/Probability/Distribution/Continuous/Cauchy.php @@ -30,10 +30,10 @@ class Cauchy extends Continuous 'x' => '(-∞,∞)', ]; - /** @var number Location Parameter */ + /** @var int|float Location Parameter */ protected $x₀; - /** @var number Scale Parameter */ + /** @var int|float Scale Parameter */ protected $γ; /** diff --git a/src/Probability/Distribution/Continuous/Continuous.php b/src/Probability/Distribution/Continuous/Continuous.php index 60f1a19c5..9b9e1ab09 100644 --- a/src/Probability/Distribution/Continuous/Continuous.php +++ b/src/Probability/Distribution/Continuous/Continuous.php @@ -16,7 +16,7 @@ abstract class Continuous extends \MathPHP\Probability\Distribution\Distribution * @param float $target The area for which we are trying to find the $x * * @todo check the parameter ranges. - * @return number + * @return int|float */ public function inverse(float $target) { @@ -109,7 +109,7 @@ public function above(float $x): float /** * Produce a random number with a particular distribution * - * @return number + * @return float * * @throws \Exception */ @@ -119,7 +119,7 @@ public function rand() } /** - * @return number + * @return int|float */ abstract public function median(); } diff --git a/src/Probability/Distribution/Continuous/ContinuousDistribution.php b/src/Probability/Distribution/Continuous/ContinuousDistribution.php index c4426a82d..20c0aea61 100644 --- a/src/Probability/Distribution/Continuous/ContinuousDistribution.php +++ b/src/Probability/Distribution/Continuous/ContinuousDistribution.php @@ -21,14 +21,14 @@ public function pdf(float $x); * * @param float $x * - * @return mixed + * @return int|float */ public function cdf(float $x); /** * Mean average * - * @return number + * @return int|float */ public function mean(); } diff --git a/src/Probability/Distribution/Continuous/Normal.php b/src/Probability/Distribution/Continuous/Normal.php index 6bb536987..43462c345 100644 --- a/src/Probability/Distribution/Continuous/Normal.php +++ b/src/Probability/Distribution/Continuous/Normal.php @@ -169,6 +169,8 @@ public function variance(): float * Random number - Box–Muller transform * * https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform + * + * @return float */ public function rand() { diff --git a/src/Probability/Distribution/Discrete/Categorical.php b/src/Probability/Distribution/Discrete/Categorical.php index ae08fc3af..4c2c2cf2d 100644 --- a/src/Probability/Distribution/Discrete/Categorical.php +++ b/src/Probability/Distribution/Discrete/Categorical.php @@ -20,7 +20,7 @@ class Categorical extends Discrete private $k; /** - * @var array + * @var array * Probability of each category * If associative array, category names are keys. * Otherwise, category names are array indexes. @@ -31,7 +31,7 @@ class Categorical extends Discrete * Distribution constructor * * @param int $k number of categories - * @param array $probabilities of each category - If associative array, category names are keys. + * @param array $probabilities of each category - If associative array, category names are keys. * Otherwise, category names are array indexes. * * @throws Exception\BadParameterException if k does not indicate at least one category @@ -66,7 +66,7 @@ public function __construct(int $k, array $probabilities) * * pmf = p(x = i) = pᵢ * - * @param string|number $x category name/number + * @param int|float $x category name/number * * @return float * diff --git a/src/Probability/Distribution/Discrete/Zipf.php b/src/Probability/Distribution/Discrete/Zipf.php index 78ecc794e..ca47c2979 100644 --- a/src/Probability/Distribution/Discrete/Zipf.php +++ b/src/Probability/Distribution/Discrete/Zipf.php @@ -33,7 +33,7 @@ class Zipf extends Discrete 'k' => '[1,∞)', ]; - /** @var number Characterizing exponent */ + /** @var int|float Characterizing exponent */ protected $s; /** @var int Number of elements */ @@ -59,7 +59,7 @@ public function __construct($s, int $N) * * @param int $k * - * @return number + * @return int|float * * @throws Exception\OutOfBoundsException if k is > N */ @@ -86,7 +86,7 @@ public function pmf(int $k) * * @param int $k * - * @return number + * @return int|float * * @throws Exception\OutOfBoundsException if k is > N */ @@ -112,7 +112,7 @@ public function cdf(int $k) * μ = --------- * Hₙ,ₛ * - * @return number + * @return int|float */ public function mean() { @@ -131,7 +131,7 @@ public function mean() * * μ = 1 * - * @return number + * @return int|float */ public function mode() { diff --git a/src/Probability/Distribution/Multivariate/Hypergeometric.php b/src/Probability/Distribution/Multivariate/Hypergeometric.php index 9b79d3f17..90d00f693 100644 --- a/src/Probability/Distribution/Multivariate/Hypergeometric.php +++ b/src/Probability/Distribution/Multivariate/Hypergeometric.php @@ -29,13 +29,13 @@ class Hypergeometric */ protected $supportLimits = []; - /** @var array */ + /** @var array */ protected $quantities; /** * Multivariate Hypergeometric constructor * - * @param array $quantities + * @param array $quantities * * @throws Exception\BadDataException if the quantities are not positive integers. */ @@ -57,7 +57,7 @@ public function __construct(array $quantities) /** * Probability mass function * - * @param array $picks + * @param array $picks * * @return float * diff --git a/src/Probability/Distribution/Multivariate/Multinomial.php b/src/Probability/Distribution/Multivariate/Multinomial.php index 9c0ab6696..33f13f944 100644 --- a/src/Probability/Distribution/Multivariate/Multinomial.php +++ b/src/Probability/Distribution/Multivariate/Multinomial.php @@ -12,13 +12,13 @@ */ class Multinomial { - /** @var array */ + /** @var array */ protected $probabilities; /** * Multinomial constructor * - * @param array $probabilities + * @param array $probabilities * * @throws Exception\BadDataException if the probabilities do not add up to 1 */ diff --git a/src/Probability/Distribution/Multivariate/Normal.php b/src/Probability/Distribution/Multivariate/Normal.php index 91ce93516..8a5f122a6 100644 --- a/src/Probability/Distribution/Multivariate/Normal.php +++ b/src/Probability/Distribution/Multivariate/Normal.php @@ -14,7 +14,7 @@ */ class Normal { - /** @var array location */ + /** @var array location */ protected $μ; /** @var NumericMatrix covariance matrix */ @@ -23,7 +23,7 @@ class Normal /** * Constructor * - * @param array $μ ∈ Rᵏ location + * @param array $μ ∈ Rᵏ location * @param NumericMatrix $∑ ∈ Rᵏˣᵏ covariance matrix * * @throws Exception\BadDataException if the covariance matrix does not have the same number of rows and columns as number of elements in μ @@ -56,7 +56,7 @@ public function __construct(array $μ, NumericMatrix $∑) * μ is a real k-dimensinoal column vector of means * │∑│ ≡ det(∑) * - * @param array $X ∈ Rᵏ k-dimensional random vector + * @param array $X ∈ Rᵏ k-dimensional random vector * * @return float density * diff --git a/src/Probability/Distribution/Table/ChiSquared.php b/src/Probability/Distribution/Table/ChiSquared.php index 9a1927a69..2e39315d1 100644 --- a/src/Probability/Distribution/Table/ChiSquared.php +++ b/src/Probability/Distribution/Table/ChiSquared.php @@ -312,7 +312,6 @@ class ChiSquared */ public static function getChiSquareValue(int $df, float $p): float { - // @phpstan-ignore-next-line (Offset numeric-string on array in isset() does not exist.) if (isset(self::CHI_SQUARED_SCORES[$df][\sprintf('%1.3f', $p)])) { return self::CHI_SQUARED_SCORES[$df][\sprintf('%1.3f', $p)]; } diff --git a/src/Probability/Distribution/Table/StandardNormal.php b/src/Probability/Distribution/Table/StandardNormal.php index 5ef642291..0a29a3851 100644 --- a/src/Probability/Distribution/Table/StandardNormal.php +++ b/src/Probability/Distribution/Table/StandardNormal.php @@ -120,7 +120,7 @@ public static function getZScoreProbability(float $Z): float } /** * @var string $z - * @var numeric $+0.0x + * @var int|float $+0.0x */ [$z, $+0.0x] = [ $matches[1], $matches[2] ]; return self::Z_SCORES[$z][$+0.0x]; diff --git a/src/Probability/Distribution/Table/TDistribution.php b/src/Probability/Distribution/Table/TDistribution.php index 394f6a78e..a7c483590 100644 --- a/src/Probability/Distribution/Table/TDistribution.php +++ b/src/Probability/Distribution/Table/TDistribution.php @@ -9,7 +9,7 @@ * * Tables for one sided and two sided, * Initial index is degrees of freedom (ν). - * Second index is confidence level precentage, or alpha value (α). + * Second index is confidence level percentage, or alpha value (α). * * https://en.wikipedia.org/wiki/Student%27s_t-distribution#Table_of_selected_values * @@ -21,8 +21,8 @@ class TDistribution { /** * One-sided t distribution table - * Confidence level percentaces - * @var array> + * Confidence level percentages + * @var array> */ private const ONE_SIDED_CONFIDENCE_LEVEL = [ 1 => [ 0 => 0, 75 => 1.000, 80 => 1.376, 85 => 1.963, 90 => 3.078, 95 => 6.314, '97.5' => 12.71, 99 => 31.82, '99.5' => 63.66, '99.75' => 127.3, '99.9' => 318.3, '99.95' => 636.6 ], @@ -112,7 +112,7 @@ class TDistribution /** * Two-sided t distribution table * Confidence level percentaces - * @var array> + * @var array> */ private const TWO_SIDED_CONFIDENCE_LEVEL = [ 1 => [ 0 => 0, 50 => 1.000, 60 => 1.376, 70 => 1.963, 80 => 3.078, 90 => 6.314, 95 => 12.71, 98 => 31.82, 99 => 63.66, '99.5' => 127.3, '99.8' => 318.3, '99.9' => 636.6 ], @@ -157,7 +157,7 @@ class TDistribution /** * Two-sided t distribution table * Alphas - * @var array> + * @var array> */ private const TWO_SIDED_ALPHA = [ 1 => [ '1.00' => 0, '0.50' => 1.000, '0.40' => 1.376, '0.30' => 1.963, '0.20' => 3.078, '0.10' => 6.314, '0.05' => 12.71, '0.02' => 31.82, '0.01' => 63.66, '0.005' => 127.3, '0.002' => 318.3, '0.001' => 636.6 ], diff --git a/src/SampleData/Cereal.php b/src/SampleData/Cereal.php index ab4f506c5..ea8568603 100644 --- a/src/SampleData/Cereal.php +++ b/src/SampleData/Cereal.php @@ -113,7 +113,7 @@ public function getCereals(): array * Raw data without labels * [[0.002682755, 0.003370673, 0.004085942, ... ], [0.002781597, 0.003474863, 0.004191472, ... ], ... ] * - * @return number[][] + * @return float[][] */ public function getXData(): array { @@ -124,11 +124,11 @@ public function getXData(): array * Raw data with each observation labeled * ['B1' => ['X1126.0' => 0.002682755, 'X1134.0' => 0.003370673, 'X1142.0' => 0.004085942, ... ]] * - * @return array> + * @return array> */ public function getLabeledXData(): array { - /** @var array> */ + /** @var array> */ return \array_map( function (array $data) { return \array_combine(self::X_LABELS, $data); @@ -141,7 +141,7 @@ function (array $data) { * Raw data without labels * [[18373, 41.61500, 6.565000, ... ], [18536, 41.40500, 6.545000, ... ], ... ] * - * @return number[][] + * @return float[][] */ public function getYData(): array { @@ -152,11 +152,11 @@ public function getYData(): array * Raw data with each observation labeled * ['B1' => ['Heating value' => 18373, 'C' => 41.61500, 'H' => 6.565000, ... ]] * - * @return array> + * @return array> */ public function getLabeledYData(): array { - /** @var array> */ + /** @var array> */ return \array_map( function (array $data) { return \array_combine(self::Y_LABELS, $data); @@ -169,7 +169,7 @@ function (array $data) { * Raw data without labels * [[-0.1005049, 0.6265746, -1.1716630, ... ], [0.9233889, 0.1882929, -1.3185289, ... ], ... ] * - * @return number[][] + * @return float[][] */ public function getYscData(): array { diff --git a/src/SampleData/Iris.php b/src/SampleData/Iris.php index 8eb2a7741..81faeeb4a 100644 --- a/src/SampleData/Iris.php +++ b/src/SampleData/Iris.php @@ -187,11 +187,11 @@ public function getData(): array * Raw data with each observation labeled * [['sepalLength' => 5.11, 'sepalWidth' => 3.5, 'petalLength' => 1.4, 'petalWidth' => 0.2, 'species' => 'setosa'], ... ] * - * @return array> + * @return array> */ public function getLabeledData(): array { - /** @var array> */ + /** @var array> */ return \array_map( function (array $data) { return \array_combine(self::LABELS, $data); @@ -203,7 +203,7 @@ function (array $data) { /** * Sepal length observations * - * @return number[] + * @return float[] */ public function getSepalLength(): array { @@ -213,7 +213,7 @@ public function getSepalLength(): array /** * Sepal width observations * - * @return number[] + * @return float[] */ public function getSepalWidth(): array { @@ -223,7 +223,7 @@ public function getSepalWidth(): array /** * Petal length observations * - * @return number[] + * @return float[] */ public function getPetalLength(): array { @@ -233,7 +233,7 @@ public function getPetalLength(): array /** * Petal width observations * - * @return number[] + * @return float[] */ public function getPetalWidth(): array { diff --git a/src/SampleData/MtCars.php b/src/SampleData/MtCars.php index 6c267e9aa..718224208 100644 --- a/src/SampleData/MtCars.php +++ b/src/SampleData/MtCars.php @@ -56,7 +56,7 @@ class MtCars * Raw data without labels * [[21, 6, 160, ... ], [30.4, 4, 71.1, ... ], ... ] * - * @return number[][] + * @return int[][]|float[][] */ public function getData(): array { @@ -67,11 +67,11 @@ public function getData(): array * Raw data with each observation labeled * ['Car Model' => ['mpg' => 21, 'cyl' => 6, 'disp' => 160, ... ]] * - * @return array> + * @return array> */ public function getLabeledData(): array { - /** @var array> */ + /** @var array> */ return \array_map( function (array $data) { return \array_combine(self::LABELS, $data); @@ -96,11 +96,11 @@ public function getModels(): array * * @param string $model * - * @return array + * @return array */ public function getModelData(string $model): array { - /** @var array */ + /** @var array */ return \array_combine(self::LABELS, self::DATA[$model]); } @@ -108,11 +108,11 @@ public function getModelData(string $model): array * Miles per gallon observations for all models * ['Mazda RX4' => 21, 'Honda civic' => 30.4, ... ] * - * @return array + * @return array */ public function getMpg(): array { - /** @var array */ + /** @var array */ return \array_combine($this->getModels(), \array_column(self::DATA, 0)); } @@ -120,7 +120,7 @@ public function getMpg(): array * Number of cylinders observations for all models * ['Mazda RX4' => 6, 'Honda civic' => 4, ... ] * - * @return number[] + * @return int[] */ public function getCyl(): array { @@ -131,7 +131,7 @@ public function getCyl(): array * Displacement (cubic inches) observations for all models * ['Mazda RX4' => 160, 'Honda civic' => 75.7, ... ] * - * @return number[] + * @return int[]|float[] */ public function getDisp(): array { @@ -142,7 +142,7 @@ public function getDisp(): array * Gross horsepower observations for all models * ['Mazda RX4' => 110, 'Honda civic' => 52, ... ] * - * @return number[] + * @return int[] */ public function getHp(): array { @@ -153,7 +153,7 @@ public function getHp(): array * Rear axle ratio observations for all models * ['Mazda RX4' => 3.9, 'Honda civic' => 4.93, ... ] * - * @return number[] + * @return float[] */ public function getDrat(): array { @@ -164,7 +164,7 @@ public function getDrat(): array * Weight (1,000 pounds) observations for all models * ['Mazda RX4' => 2.62, 'Honda civic' => 1.615, ... ] * - * @return number[] + * @return float[] */ public function getWt(): array { @@ -175,7 +175,7 @@ public function getWt(): array * Quarter-mile time observations for all models * ['Mazda RX4' => 16.46, 'Honda civic' => 18.52, ... ] * - * @return number[] + * @return float[] */ public function getQsec(): array { @@ -186,7 +186,7 @@ public function getQsec(): array * V/S observations for all models * ['Mazda RX4' => 0, 'Honda civic' => 1, ... ] * - * @return number[] + * @return int[] */ public function getVs(): array { @@ -197,7 +197,7 @@ public function getVs(): array * Transmission (automatic: 0, manual: 1) observations for all models * ['Mazda RX4' => 1, 'Honda civic' => 1, ... ] * - * @return number[] + * @return int[] */ public function getAm(): array { @@ -208,7 +208,7 @@ public function getAm(): array * Number of forward gears observations for all models * ['Mazda RX4' => 4, 'Honda civic' => 4, ... ] * - * @return number[] + * @return int[] */ public function getGear(): array { @@ -219,7 +219,7 @@ public function getGear(): array * Number of carburetors observations for all models * ['Mazda RX4' => 4, 'Honda civic' => 2, ... ] * - * @return number[] + * @return int[] */ public function getCarb(): array { diff --git a/src/SampleData/PlantGrowth.php b/src/SampleData/PlantGrowth.php index c449e10eb..19458a056 100644 --- a/src/SampleData/PlantGrowth.php +++ b/src/SampleData/PlantGrowth.php @@ -65,11 +65,11 @@ public function getData(): array * Raw data with each observation labeled * [['weight' => 4.17, 'group' => 'ctrl'], ['weight' => 5.58, 'group' => 'ctrl'], ... ] * - * @return array> + * @return array> */ public function getLabeledData(): array { - /** @var array> */ + /** @var array> */ return \array_map( function (array $data) { return \array_combine(self::LABELS, $data); @@ -81,7 +81,7 @@ function (array $data) { /** * Weight observations * - * @return number[] + * @return float[] */ public function getWeight(): array { diff --git a/src/SampleData/ToothGrowth.php b/src/SampleData/ToothGrowth.php index d786630b5..2cbbdda1d 100644 --- a/src/SampleData/ToothGrowth.php +++ b/src/SampleData/ToothGrowth.php @@ -111,7 +111,7 @@ function (array $data) { /** * Tooth length observations * - * @return number[] + * @return float[] */ public function getLen(): array { @@ -131,7 +131,7 @@ public function getSupp(): array /** * Dose in milligrams/day observations * - * @return number[] + * @return float[] */ public function getDose(): array { diff --git a/src/SampleData/UsArrests.php b/src/SampleData/UsArrests.php index 1cae07a97..80e538cbc 100644 --- a/src/SampleData/UsArrests.php +++ b/src/SampleData/UsArrests.php @@ -74,7 +74,7 @@ class UsArrests * Raw data without labels * [[13.2, 236, 58, 21.2], [10.0, 263, 48, 44.5], ... ] * - * @return number[][] + * @return int[][]|float[][] */ public function getData(): array { @@ -85,13 +85,13 @@ public function getData(): array * Raw data with each observation labeled * ['Alabama' => ['murder' => 13.2, 'assault' => 236, 'urbanPop' => 58, 'rape' => 21.2], ... ] * - * @return array> + * @return array> */ public function getLabeledData(): array { return \array_map( function (array $data) { - /** @var array */ + /** @var array */ return \array_combine(self::LABELS, $data); }, self::DATA @@ -99,7 +99,7 @@ function (array $data) { } /** - * State names names + * State names * * @return string[] */ @@ -114,11 +114,11 @@ public function getStates(): array * * @param string $state * - * @return number[] + * @return int[]|float[] */ public function getStateData(string $state): array { - /** @var array */ + /** @var array */ return \array_combine(self::LABELS, self::DATA[$state]); } diff --git a/src/SetTheory/ImmutableSet.php b/src/SetTheory/ImmutableSet.php index d44fe0f1f..5513e91a5 100644 --- a/src/SetTheory/ImmutableSet.php +++ b/src/SetTheory/ImmutableSet.php @@ -37,11 +37,11 @@ public function add($x): Set /** * Cannot add members to an immutable set * - * @param array $x + * @param array $members * * @return Set (this set unchanged) */ - public function addMulti(array $x): Set + public function addMulti(array $members): Set { return $this; } diff --git a/src/SetTheory/Set.php b/src/SetTheory/Set.php index 735f3ae45..c9e4f68ab 100644 --- a/src/SetTheory/Set.php +++ b/src/SetTheory/Set.php @@ -265,7 +265,7 @@ protected function getKey($x): ?string } elseif (\is_array($x)) { return 'Array(' . serialize($x) . ')'; } elseif (\is_resource($x)) { - return 'Resource(' . \strval($x) . ')'; + return 'Resource(' . $x . ')'; } return null; diff --git a/src/Statistics/Average.php b/src/Statistics/Average.php index a8f377d92..64ee57a01 100644 --- a/src/Statistics/Average.php +++ b/src/Statistics/Average.php @@ -721,9 +721,9 @@ public static function cumulativeMovingAverage(array $numbers): array * * Each weighted average = ∑(weighted values) / ∑(weights) * - * @param array $numbers - * @param int $n n-point moving average - * @param array $weights Weights for each n points + * @param array $numbers + * @param int $n n-point moving average + * @param array $weights Weights for each n points * * @return array of averages * @@ -757,8 +757,8 @@ public static function weightedMovingAverage(array $numbers, int $n, array $weig * where * α: coefficient that represents the degree of weighting decrease, a constant smoothing factor between 0 and 1. * - * @param array $numbers - * @param int $n Length of the EPA + * @param array $numbers + * @param int $n Length of the EPA * * @return array of exponential moving averages */ diff --git a/src/Statistics/Correlation.php b/src/Statistics/Correlation.php index 196a20bab..e3f563567 100644 --- a/src/Statistics/Correlation.php +++ b/src/Statistics/Correlation.php @@ -471,8 +471,8 @@ function ($x, $y) { * cov(rgᵪ, rgᵧ): covariance of the rank variables * σᵣᵪ and σᵣᵧ: standard deviations of the rank variables * - * @param array $X values for random variable X - * @param array $Y values for random variable Y + * @param array $X values for random variable X + * @param array $Y values for random variable Y * * @return float * @@ -538,7 +538,7 @@ public static function describe(array $X, array $Y, bool $population = false): a * will be one larger because the first point and last will be repeated * to ease display. * - * @return array> paired x and y points on an ellipse aligned with the data provided + * @return array> paired x and y points on an ellipse aligned with the data provided * * @throws Exception\BadDataException * @throws Exception\BadParameterException diff --git a/src/Statistics/Descriptive.php b/src/Statistics/Descriptive.php index 9e7a35378..e7bd102ae 100644 --- a/src/Statistics/Descriptive.php +++ b/src/Statistics/Descriptive.php @@ -739,7 +739,7 @@ public static function describe(array $numbers, bool $population = false): array * * https://en.wikipedia.org/wiki/Five-number_summary * - * @param array $numbers + * @param array $numbers * * @return array{ * min: float|int|false, diff --git a/src/Statistics/Distance.php b/src/Statistics/Distance.php index 709ee55c8..8b569a6d7 100644 --- a/src/Statistics/Distance.php +++ b/src/Statistics/Distance.php @@ -49,8 +49,8 @@ class Distance * BC(p,q) = ∑ √(p(x) q(x)) * x∈X * - * @param array $p distribution p - * @param array $q distribution q + * @param array $p distribution p + * @param array $q distribution q * * @return float distance between distributions * @@ -84,8 +84,8 @@ public static function bhattacharyya(array $p, array $q): float * H(P,Q) = -- √ ∑ (√pᵢ - √qᵢ)² * √2 * - * @param array $p distribution p - * @param array $q distribution q + * @param array $p distribution p + * @param array $q distribution q * * @return float difference between distributions * @@ -146,8 +146,8 @@ function ($pᵢ, $qᵢ) { * * D(P‖Q) = Kullback-Leibler divergence * - * @param array $p distribution p - * @param array $q distribution q + * @param array $p distribution p + * @param array $q distribution q * * @return float * diff --git a/src/Statistics/Distribution.php b/src/Statistics/Distribution.php index cca1c09bd..8c7d74fe2 100644 --- a/src/Statistics/Distribution.php +++ b/src/Statistics/Distribution.php @@ -109,7 +109,7 @@ function ($frequency) use ($sample_size) { * * Similar to R: rank(values, ties.method='average') * - * @param array $values to be ranked + * @param array $values to be ranked * * @return array Rankings of the data in the same order the values were input */ @@ -148,7 +148,7 @@ function ($value) use ($rg⟮X⟯) { * * Similar to R: rank(values, ties.method='min') * - * @param array $values to be ranked + * @param array $values to be ranked * * @return array Rankings of the data in the same order the values were input */ @@ -184,7 +184,7 @@ function ($value) use ($ranking⟮X⟯) { * * Similar to R: rank(values, ties.method='max') * - * @param array $values to be ranked + * @param array $values to be ranked * * @return array Rankings of the data in the same order the values were input */ @@ -221,7 +221,7 @@ function ($value) use ($ranking⟮X⟯) { * * Similar to R: rank(values, ties.method='first') * - * @param array $values to be ranked + * @param array $values to be ranked * * @return array Rankings of the data in the same order the values were input */ diff --git a/src/Statistics/Divergence.php b/src/Statistics/Divergence.php index 7162c768a..e387f51ae 100644 --- a/src/Statistics/Divergence.php +++ b/src/Statistics/Divergence.php @@ -35,8 +35,8 @@ class Divergence * * * - * @param array $p distribution p - * @param array $q distribution q + * @param array $p distribution p + * @param array $q distribution q * * @return float difference between distributions * @@ -99,8 +99,8 @@ function ($P, $Q) { * * D(P‖Q) = Kullback-Leibler divergence * - * @param array $p distribution p - * @param array $q distribution q + * @param array $p distribution p + * @param array $q distribution q * * @return float difference between distributions * diff --git a/src/Statistics/Regression/LOESS.php b/src/Statistics/Regression/LOESS.php index 6fa1b7d0b..9f963d4aa 100644 --- a/src/Statistics/Regression/LOESS.php +++ b/src/Statistics/Regression/LOESS.php @@ -19,7 +19,7 @@ class LOESS extends NonParametricRegression /** * Smoothness parameter - * @var number + * @var int|float */ protected $α; diff --git a/src/Statistics/Regression/Methods/LeastSquares.php b/src/Statistics/Regression/Methods/LeastSquares.php index 74677052d..dfd922b55 100644 --- a/src/Statistics/Regression/Methods/LeastSquares.php +++ b/src/Statistics/Regression/Methods/LeastSquares.php @@ -188,7 +188,7 @@ public function getProjectionMatrix(): NumericMatrix * which is the i-th diagonal element of the project matrix H, * where H = X⟮XᵀX⟯⁻¹Xᵀ where X is the design matrix. * - * @return array + * @return array */ public function leverages(): array { diff --git a/src/Statistics/Significance.php b/src/Statistics/Significance.php index f91899df6..e3cded5ca 100644 --- a/src/Statistics/Significance.php +++ b/src/Statistics/Significance.php @@ -219,7 +219,6 @@ public static function tTest(array $a, $b): array return self::tTestTwoSample($a, $b); } - // @phpstan-ignore-next-line (Unreachable statement - code above always terminates.) throw new Exception\BadParameterException('Second parameter must be numeric for one-sample t-test, or an array for two-sample t-test'); } diff --git a/tests/LinearAlgebra/Matrix/MatrixFactoryTest.php b/tests/LinearAlgebra/Matrix/MatrixFactoryTest.php index bb998bb08..8b2a41a85 100644 --- a/tests/LinearAlgebra/Matrix/MatrixFactoryTest.php +++ b/tests/LinearAlgebra/Matrix/MatrixFactoryTest.php @@ -339,6 +339,22 @@ public function testCheckParamsExceptionEmptyArray() $M = MatrixFactory::create($A); } + /** + * @test check params exception for single dimensional array + * @throws \Exception + */ + public function testCheckParamsExceptionSingleDimensionalArray() + { + // Given + $A = [1, 2, 3]; + + // Then + $this->expectException(Exception\BadDataException::class); + + // When + $M = MatrixFactory::create($A); + } + /** * @test matrix unknown type exception * @throws \Exception diff --git a/tests/phpstan.neon b/tests/phpstan.neon new file mode 100644 index 000000000..6344674e3 --- /dev/null +++ b/tests/phpstan.neon @@ -0,0 +1,5 @@ +parameters: + level: max + paths: + - ../src + treatPhpDocTypesAsCertain: false From ea4f212732c333c62123c6f733edfb735a4e3abd Mon Sep 17 00:00:00 2001 From: Mark Rogoyski Date: Thu, 18 May 2023 21:40:46 -0700 Subject: [PATCH 11/11] Update CHANGELOG for v2.8.1. --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ebabe823e..215cde3b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # MathPHP Change Log +## v2.8.1 - 2023-05-18 + +### Improvements +* Internal improvements to improve conformance with static analysis tools + ## v2.8.0 - 2023-05-07 ### New Features