From 3f438238e7cc8ce2e635f3b13533c5af244fa05a Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 23 Feb 2018 10:34:01 +0100 Subject: [PATCH] Added power function on arrays --- Math/Functions/Functions.php | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/Math/Functions/Functions.php b/Math/Functions/Functions.php index 82e96cf0f..e2aa19840 100644 --- a/Math/Functions/Functions.php +++ b/Math/Functions/Functions.php @@ -241,6 +241,50 @@ 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 + * todo: move to utils?! implement sqrt for array as well... could be usefull for others (e.g. matrix) + */ + 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 + * todo: move to utils?! implement sqrt for array as well... could be usefull for others (e.g. matrix) + */ + public static function powerInt(array $values, int $exp = 2) : array + { + $squared = []; + + foreach ($values as $value) { + $squared[] = $value * $exp; + } + + return $squared; + } /** * Gets the relative position on a circular construct.