diff --git a/Math/Statistic/Forecast/Error.php b/Math/Statistic/Forecast/Error.php index b0a335f4d..521ca6f69 100644 --- a/Math/Statistic/Forecast/Error.php +++ b/Math/Statistic/Forecast/Error.php @@ -14,9 +14,9 @@ declare(strict_types=1); namespace phpOMS\Math\Statistic\Forecast; -use phpOMS\Math\Functions\Functions; use phpOMS\Math\Statistic\Average; use phpOMS\Math\Statistic\MeasureOfDispersion; +use phpOMS\Utils\ArrayUtils; /** * Basic forecast functions. @@ -139,7 +139,7 @@ class Error */ public static function getRootMeanSquaredError(array $errors) : float { - return \sqrt(Average::arithmeticMean(Functions::powerInt($errors, 2))); + return \sqrt(Average::arithmeticMean(ArrayUtils::powerInt($errors, 2))); } /** @@ -273,7 +273,7 @@ class Error */ public static function getMeanAbsolutePercentageError(array $observed, array $forecasted) : float { - return Average::arithmeticMean(Functions::abs(self::getPercentageErrorArray(self::getForecastErrorArray($observed, $forecasted), $observed))); + return Average::arithmeticMean(ArrayUtils::abs(self::getPercentageErrorArray(self::getForecastErrorArray($observed, $forecasted), $observed))); } /** @@ -352,7 +352,7 @@ class Error */ public static function getMeanAbsoluteScaledError(array $scaledErrors) : float { - return Average::arithmeticMean(Functions::abs($scaledErrors)); + return Average::arithmeticMean(ArrayUtils::abs($scaledErrors)); } /** @@ -366,7 +366,7 @@ class Error */ public static function getMeanSquaredScaledError(array $scaledErrors) : float { - return Average::arithmeticMean(Functions::powerInt($scaledErrors, 2)); + return Average::arithmeticMean(ArrayUtils::powerInt($scaledErrors, 2)); } /** diff --git a/Math/Topology/Metrics2D.php b/Math/Topology/Metrics2D.php index 2bdafbc66..709ccefb3 100644 --- a/Math/Topology/Metrics2D.php +++ b/Math/Topology/Metrics2D.php @@ -15,6 +15,8 @@ declare(strict_types=1); namespace phpOMS\Math\Topology; +use phpOMS\Math\Matrix\Exception\InvalidDimensionException; + /** * Metrics. * @@ -204,7 +206,7 @@ final class Metrics2D public static function hamming(array $a, array $b) : int { if (($size = \count($a)) !== \count($b)) { - throw new \Exception(); + throw new InvalidDimensionException(\count($a) . 'x' . \count($b)); } $dist = 0; @@ -234,7 +236,7 @@ final class Metrics2D public static function ulam(array $a, array $b) : int { if (($size = \count($a)) !== \count($b)) { - throw new \Exception('Invalid dimensions'); + throw new InvalidDimensionException(\count($a) . 'x' . \count($b)); } $mp = []; diff --git a/tests/Math/Matrix/SingularValueDecompositionTest.php b/tests/Math/Matrix/SingularValueDecompositionTest.php deleted file mode 100644 index 750babb90..000000000 --- a/tests/Math/Matrix/SingularValueDecompositionTest.php +++ /dev/null @@ -1,109 +0,0 @@ -setMatrix([ - [1, 2, 3], - [-2, 3, 8], - [5, 1, -3], - ]); - - self::markTestIncomplete(); - return; - - $svd = new SingularValueDecomposition($A); - - self::assertEquals(3, $svd->rank()); - } - - /** - * @testdox Test the correct calculation of U, S and V - */ - public function testSUVCalculation() : void - { - $A = new Matrix(); - $A->setMatrix([ - [1, 2, 3], - [-2, 3, 8], - [5, 1, -3], - ]); - - self::markTestIncomplete(); - return; - - $svd = new SingularValueDecomposition($A); - - self::assertEquals([ - [0.2871, -0.4773, -0.8305], - [0.8640, -0.2453, 0.4397], - [-0.4136, -0.8438, 0.3420], - ], $svd->getU()->toArray(), '', 0.2); - - self::assertEquals([ - [10.0571, 0, 0], - [0, 4.9855, 0], - [0, 0, 0], - ], $svd->getS()->toArray(), '', 0.2); - - self::assertEquals([ - [-0.3489, -0.8436, -0.4082], - [0.2737, -0.5084, 0.8165], - [0.8963, -0.1731, -0.4082], - ], $svd->getV()->toArray(), '', 0.2); - } - - /** - * @testdox Test A = S * U * V' - */ - public function testComposition() : void - { - $A = new Matrix(); - $A->setMatrix([ - [1, 2, 3], - [-2, 3, 8], - [5, 1, -3], - ]); - - self::markTestIncomplete(); - return; - - $svd = new SingularValueDecomposition($A); - - self::assertEquals( - $A->toArray(), - $svd->getU() - ->mult($svd->getS()) - ->mult($svd->getV()->transpose()) - ->toArray(), - '', 0.2 - ); - } -} diff --git a/tests/Math/Number/ComplexTest.php b/tests/Math/Number/ComplexTest.php index 663553d94..a7b97db57 100644 --- a/tests/Math/Number/ComplexTest.php +++ b/tests/Math/Number/ComplexTest.php @@ -93,6 +93,7 @@ class ComplexTest extends \PHPUnit\Framework\TestCase self::assertEquals('7.00 + 24.00i', $cpl->square()->render()); self::assertEquals('7.00 + 24.00i', $cpl->pow(2)->render()); self::assertEquals('-44.00 + 117.00i', $cpl->pow(3)->render()); + self::assertEquals('1.00', $cpl->pow(0)->render()); } public function testAbs() : void diff --git a/tests/Math/Numerics/Interpolation/LinearInterpolationTest.php b/tests/Math/Numerics/Interpolation/LinearInterpolationTest.php index de5ee0ba9..2a15571d9 100644 --- a/tests/Math/Numerics/Interpolation/LinearInterpolationTest.php +++ b/tests/Math/Numerics/Interpolation/LinearInterpolationTest.php @@ -31,6 +31,8 @@ class LinearInterpolationTest extends \PHPUnit\Framework\TestCase ['x' => 50.0, 'y' => 7.0], ]); + self::assertEqualsWithDelta(0.45, $interpolation->interpolate(9.0), 0.1); self::assertEqualsWithDelta(4.4, $interpolation->interpolate(37.0), 0.1); + self::assertEqualsWithDelta(10, $interpolation->interpolate(55.0), 0.1); } } diff --git a/tests/Math/Statistic/AverageTest.php b/tests/Math/Statistic/AverageTest.php index 234317b12..d6e0636b9 100644 --- a/tests/Math/Statistic/AverageTest.php +++ b/tests/Math/Statistic/AverageTest.php @@ -84,6 +84,20 @@ class AverageTest extends \PHPUnit\Framework\TestCase Average::arithmeticMean([]); } + public function testInvalidMovingAverageZeroDevision() : void + { + self::expectException(\Exception::class); + + Average::movingAverage([], 4, 2); + } + + public function testInvalidHarmonicMeanZeroDevision() : void + { + self::expectException(\phpOMS\Math\Exception\ZeroDevisionException::class); + + Average::harmonicMean([]); + } + public function testInvalidGeometricMean() : void { self::expectException(\phpOMS\Math\Exception\ZeroDevisionException::class); diff --git a/tests/Math/Topology/Metrics2DTest.php b/tests/Math/Topology/Metrics2DTest.php index 9bd5aaf77..e13804c8f 100644 --- a/tests/Math/Topology/Metrics2DTest.php +++ b/tests/Math/Topology/Metrics2DTest.php @@ -97,4 +97,18 @@ class Metrics2DTest extends \PHPUnit\Framework\TestCase Metrics2D::ulam([3, 6, 4, 8], [4, 6, 8, 3]) ); } + + public function testInvalidHammingDimension() : void + { + self::expectException(\phpOMS\Math\Matrix\Exception\InvalidDimensionException::class); + + Metrics2D::hamming([1, 1, 1, 1], [0, 1, 0]); + } + + public function testInvalidUlamDimension() : void + { + self::expectException(\phpOMS\Math\Matrix\Exception\InvalidDimensionException::class); + + Metrics2D::ulam([3, 6, 4], [4, 6, 8, 3]); + } }