From e8ecfce4fd44369ea1cd90f1625687df46846788 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 19 Oct 2019 13:11:27 +0200 Subject: [PATCH] move some functions to array utils --- Math/Functions/Functions.php | 82 -------------------------- Utils/ArrayUtils.php | 82 ++++++++++++++++++++++++++ tests/Math/Functions/FunctionsTest.php | 14 ----- tests/Utils/ArrayUtilsTest.php | 19 ++++++ 4 files changed, 101 insertions(+), 96 deletions(-) diff --git a/Math/Functions/Functions.php b/Math/Functions/Functions.php index cee52066f..2aaa93e58 100644 --- a/Math/Functions/Functions.php +++ b/Math/Functions/Functions.php @@ -124,26 +124,6 @@ final class Functions return self::ackermann($m - 1, self::ackermann($m, $n - 1)); } - /** - * Applying abs to every array value - * - * @param array $values Numeric values - * - * @return array - * - * @since 1.0.0 - */ - public static function abs(array $values) : array - { - $abs = []; - - foreach ($values as $value) { - $abs[] = \abs($value); - } - - return $abs; - } - /** * Calculate inverse modular. * @@ -237,68 +217,6 @@ final class Functions return !((bool) ($a & 1)); } - /** - * Power all values in array. - * - * @param array $values Values to square - * @param float $exp Exponent - * - * @return array - * - * @since 1.0.0 - */ - public static function powerFloat(array $values, float $exp = 2.0) : array - { - $squared = []; - - foreach ($values as $value) { - $squared[] = $value ** $exp; - } - - return $squared; - } - - /** - * Power all values in array. - * - * @param array $values Values to square - * @param int $exp Exponent - * - * @return array - * - * @since 1.0.0 - */ - public static function powerInt(array $values, int $exp = 2) : array - { - $squared = []; - - foreach ($values as $value) { - $squared[] = $value ** $exp; - } - - return $squared; - } - - /** - * Sqrt all values in array. - * - * @param array $values Values to sqrt - * - * @return array - * - * @since 1.0.0 - */ - public static function sqrt(array $values) : array - { - $squared = []; - - foreach ($values as $value) { - $squared[] = \sqrt($value); - } - - return $squared; - } - /** * Gets the relative position on a circular construct. * diff --git a/Utils/ArrayUtils.php b/Utils/ArrayUtils.php index 2728acbf3..5877bfbd6 100644 --- a/Utils/ArrayUtils.php +++ b/Utils/ArrayUtils.php @@ -404,4 +404,86 @@ final class ArrayUtils { return \array_sum(self::arrayFlatten($array)); } + + /** + * Applying abs to every array value + * + * @param array $values Numeric values + * + * @return array + * + * @since 1.0.0 + */ + public static function abs(array $values) : array + { + $abs = []; + + foreach ($values as $value) { + $abs[] = \abs($value); + } + + return $abs; + } + + /** + * Power all values in array. + * + * @param array $values Values to square + * @param float $exp Exponent + * + * @return array + * + * @since 1.0.0 + */ + public static function powerFloat(array $values, float $exp = 2.0) : array + { + $squared = []; + + foreach ($values as $value) { + $squared[] = $value ** $exp; + } + + return $squared; + } + + /** + * Power all values in array. + * + * @param array $values Values to square + * @param int $exp Exponent + * + * @return array + * + * @since 1.0.0 + */ + public static function powerInt(array $values, int $exp = 2) : array + { + $squared = []; + + foreach ($values as $value) { + $squared[] = $value ** $exp; + } + + return $squared; + } + + /** + * Sqrt all values in array. + * + * @param array $values Values to sqrt + * + * @return array + * + * @since 1.0.0 + */ + public static function sqrt(array $values) : array + { + $squared = []; + + foreach ($values as $value) { + $squared[] = \sqrt($value); + } + + return $squared; + } } diff --git a/tests/Math/Functions/FunctionsTest.php b/tests/Math/Functions/FunctionsTest.php index 7f4dac6e4..68cd298d4 100644 --- a/tests/Math/Functions/FunctionsTest.php +++ b/tests/Math/Functions/FunctionsTest.php @@ -46,11 +46,6 @@ class FunctionsTest extends \PHPUnit\Framework\TestCase self::assertEquals(5, Functions::invMod(-10, 17)); } - public function testAbs() : void - { - self::assertEquals([1, 3, 4], Functions::abs([-1, 3, -4])); - } - public function testProperties() : void { self::assertTrue(Functions::isOdd(3)); @@ -70,13 +65,4 @@ class FunctionsTest extends \PHPUnit\Framework\TestCase self::assertEquals(5, Functions::getRelativeDegree(12, 12, 7)); self::assertEquals(11, Functions::getRelativeDegree(6, 12, 7)); } - - public function testPower() : void - { - self::assertEquals([4, 9, 16], Functions::powerInt([2, 3, 4], 2)); - self::assertEquals([8, 27, 64], Functions::powerInt([2, 3, 4], 3)); - - self::assertEqualsWithDelta([2.0, 3.0, 4.0], Functions::powerFloat([4, 9, 16], 1 / 2), 0.0); - self::assertEqualsWithDelta([2.0, 3.0, 4.0], Functions::powerFloat([8, 27, 64], 1 / 3), 0.0); - } } diff --git a/tests/Utils/ArrayUtilsTest.php b/tests/Utils/ArrayUtilsTest.php index ad2360319..6e37f71d3 100644 --- a/tests/Utils/ArrayUtilsTest.php +++ b/tests/Utils/ArrayUtilsTest.php @@ -144,4 +144,23 @@ class ArrayUtilsTest extends \PHPUnit\Framework\TestCase ArrayUtils::stringify([new class() {}]); } + + public function testPower() : void + { + self::assertEquals([4, 9, 16], ArrayUtils::powerInt([2, 3, 4], 2)); + self::assertEquals([8, 27, 64], ArrayUtils::powerInt([2, 3, 4], 3)); + + self::assertEqualsWithDelta([2.0, 3.0, 4.0], ArrayUtils::powerFloat([4, 9, 16], 1 / 2), 0.0); + self::assertEqualsWithDelta([2.0, 3.0, 4.0], ArrayUtils::powerFloat([8, 27, 64], 1 / 3), 0.0); + } + + public function testSqrt() : void + { + self::assertEqualsWithDelta([2, 3, 4], ArrayUtils::sqrt([4, 9, 16]), 0.01); + } + + public function testAbs() : void + { + self::assertEquals([1, 3, 4], ArrayUtils::abs([-1, 3, -4])); + } }