From 40f1ca06aa40ffd409c16ff4aacb7f8e2ffb483a Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Fri, 8 Feb 2019 22:24:02 +0100 Subject: [PATCH] Issue #351: Replace pow() and sqrt() with double stars notation. (#352) --- src/Classification/DecisionTree.php | 2 +- src/Classification/Ensemble/RandomForest.php | 2 +- src/Classification/NaiveBayes.php | 2 +- src/Clustering/KMeans/Point.php | 2 +- .../LinearAlgebra/EigenvalueDecomposition.php | 25 ++++++++++--------- src/Math/Matrix.php | 2 +- src/Math/Statistic/Correlation.php | 6 ++--- src/Math/Statistic/Gaussian.php | 2 +- src/Math/Statistic/StandardDeviation.php | 2 +- .../ActivationFunction/Gaussian.php | 2 +- .../ActivationFunction/HyperbolicTangent.php | 2 +- src/Preprocessing/Normalizer.php | 2 +- 12 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/Classification/DecisionTree.php b/src/Classification/DecisionTree.php index d8010f0..04cde56 100644 --- a/src/Classification/DecisionTree.php +++ b/src/Classification/DecisionTree.php @@ -137,7 +137,7 @@ class DecisionTree implements Classifier $sum = array_sum(array_column($countMatrix, $i)); if ($sum > 0) { foreach ($this->labels as $label) { - $part += pow($countMatrix[$label][$i] / (float) $sum, 2); + $part += ($countMatrix[$label][$i] / (float) $sum) ** 2; } } diff --git a/src/Classification/Ensemble/RandomForest.php b/src/Classification/Ensemble/RandomForest.php index ac1304c..b75d7ae 100644 --- a/src/Classification/Ensemble/RandomForest.php +++ b/src/Classification/Ensemble/RandomForest.php @@ -131,7 +131,7 @@ class RandomForest extends Bagging if (is_float($this->featureSubsetRatio)) { $featureCount = (int) ($this->featureSubsetRatio * $this->featureCount); } elseif ($this->featureSubsetRatio === 'sqrt') { - $featureCount = (int) sqrt($this->featureCount) + 1; + $featureCount = (int) ($this->featureCount ** .5) + 1; } else { $featureCount = (int) log($this->featureCount, 2) + 1; } diff --git a/src/Classification/NaiveBayes.php b/src/Classification/NaiveBayes.php index 45075a4..079b6f7 100644 --- a/src/Classification/NaiveBayes.php +++ b/src/Classification/NaiveBayes.php @@ -162,7 +162,7 @@ class NaiveBayes implements Classifier // scikit-learn did. // (See : https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/naive_bayes.py) $pdf = -0.5 * log(2.0 * M_PI * $std * $std); - $pdf -= 0.5 * pow($value - $mean, 2) / ($std * $std); + $pdf -= 0.5 * (($value - $mean) ** 2) / ($std * $std); return $pdf; } diff --git a/src/Clustering/KMeans/Point.php b/src/Clustering/KMeans/Point.php index f6ad3f5..a3f195d 100644 --- a/src/Clustering/KMeans/Point.php +++ b/src/Clustering/KMeans/Point.php @@ -49,7 +49,7 @@ class Point implements ArrayAccess, \Countable $distance += $difference * $difference; } - return $precise ? sqrt((float) $distance) : $distance; + return $precise ? $distance ** .5 : $distance; } /** diff --git a/src/Math/LinearAlgebra/EigenvalueDecomposition.php b/src/Math/LinearAlgebra/EigenvalueDecomposition.php index e0f1639..cc68864 100644 --- a/src/Math/LinearAlgebra/EigenvalueDecomposition.php +++ b/src/Math/LinearAlgebra/EigenvalueDecomposition.php @@ -128,12 +128,13 @@ class EigenvalueDecomposition $vectors = new Matrix($vectors); $vectors = array_map(function ($vect) { $sum = 0; - for ($i = 0; $i < count($vect); ++$i) { + $count = count($vect); + for ($i = 0; $i < $count; ++$i) { $sum += $vect[$i] ** 2; } - $sum = sqrt($sum); - for ($i = 0; $i < count($vect); ++$i) { + $sum **= .5; + for ($i = 0; $i < $count; ++$i) { $vect[$i] /= $sum; } @@ -208,11 +209,11 @@ class EigenvalueDecomposition // Generate Householder vector. for ($k = 0; $k < $i; ++$k) { $this->d[$k] /= $scale; - $h += pow($this->d[$k], 2); + $h += $this->d[$k] ** 2; } $f = $this->d[$i_]; - $g = sqrt($h); + $g = $h ** .5; if ($f > 0) { $g = -$g; } @@ -320,7 +321,7 @@ class EigenvalueDecomposition $this->e[$this->n - 1] = 0.0; $f = 0.0; $tst1 = 0.0; - $eps = pow(2.0, -52.0); + $eps = 2.0 ** -52.0; for ($l = 0; $l < $this->n; ++$l) { // Find small subdiagonal element @@ -443,7 +444,7 @@ class EigenvalueDecomposition $h += $this->ort[$i] * $this->ort[$i]; } - $g = sqrt($h); + $g = $h ** .5; if ($this->ort[$m] > 0) { $g *= -1; } @@ -548,7 +549,7 @@ class EigenvalueDecomposition $n = $nn - 1; $low = 0; $high = $nn - 1; - $eps = pow(2.0, -52.0); + $eps = 2.0 ** -52.0; $exshift = 0.0; $p = $q = $r = $s = $z = 0; // Store roots isolated by balanc and compute matrix norm @@ -596,7 +597,7 @@ class EigenvalueDecomposition $w = $this->H[$n][$n - 1] * $this->H[$n - 1][$n]; $p = ($this->H[$n - 1][$n - 1] - $this->H[$n][$n]) / 2.0; $q = $p * $p + $w; - $z = sqrt(abs($q)); + $z = abs($q) ** .5; $this->H[$n][$n] += $exshift; $this->H[$n - 1][$n - 1] += $exshift; $x = $this->H[$n][$n]; @@ -620,7 +621,7 @@ class EigenvalueDecomposition $s = abs($x) + abs($z); $p = $x / $s; $q = $z / $s; - $r = sqrt($p * $p + $q * $q); + $r = ($p * $p + $q * $q) ** .5; $p /= $r; $q /= $r; // Row modification @@ -682,7 +683,7 @@ class EigenvalueDecomposition $s = ($y - $x) / 2.0; $s *= $s + $w; if ($s > 0) { - $s = sqrt($s); + $s **= .5; if ($y < $x) { $s = -$s; } @@ -750,7 +751,7 @@ class EigenvalueDecomposition break; } - $s = sqrt($p * $p + $q * $q + $r * $r); + $s = ($p * $p + $q * $q + $r * $r) ** .5; if ($p < 0) { $s = -$s; } diff --git a/src/Math/Matrix.php b/src/Math/Matrix.php index a511f55..db6be42 100644 --- a/src/Math/Matrix.php +++ b/src/Math/Matrix.php @@ -271,7 +271,7 @@ class Matrix } } - return sqrt($squareSum); + return $squareSum ** .5; } /** diff --git a/src/Math/Statistic/Correlation.php b/src/Math/Statistic/Correlation.php index 6878388..c730c47 100644 --- a/src/Math/Statistic/Correlation.php +++ b/src/Math/Statistic/Correlation.php @@ -32,10 +32,10 @@ class Correlation $a = $x[$i] - $meanX; $b = $y[$i] - $meanY; $axb += ($a * $b); - $a2 += pow($a, 2); - $b2 += pow($b, 2); + $a2 += $a ** 2; + $b2 += $b ** 2; } - return $axb / sqrt((float) ($a2 * $b2)); + return $axb / ($a2 * $b2) ** .5; } } diff --git a/src/Math/Statistic/Gaussian.php b/src/Math/Statistic/Gaussian.php index ff8470c..649063d 100644 --- a/src/Math/Statistic/Gaussian.php +++ b/src/Math/Statistic/Gaussian.php @@ -34,7 +34,7 @@ class Gaussian $std2 = $this->std ** 2; $mean = $this->mean; - return exp(-(($value - $mean) ** 2) / (2 * $std2)) / sqrt(2 * $std2 * M_PI); + return exp(-(($value - $mean) ** 2) / (2 * $std2)) / ((2 * $std2 * M_PI) ** .5); } /** diff --git a/src/Math/Statistic/StandardDeviation.php b/src/Math/Statistic/StandardDeviation.php index a9724d1..50effab 100644 --- a/src/Math/Statistic/StandardDeviation.php +++ b/src/Math/Statistic/StandardDeviation.php @@ -32,7 +32,7 @@ class StandardDeviation --$n; } - return sqrt($carry / $n); + return ($carry / $n) ** .5; } /** diff --git a/src/NeuralNetwork/ActivationFunction/Gaussian.php b/src/NeuralNetwork/ActivationFunction/Gaussian.php index 8871b58..29cfef2 100644 --- a/src/NeuralNetwork/ActivationFunction/Gaussian.php +++ b/src/NeuralNetwork/ActivationFunction/Gaussian.php @@ -13,7 +13,7 @@ class Gaussian implements ActivationFunction */ public function compute($value): float { - return exp(-pow($value, 2)); + return exp(- $value ** 2); } /** diff --git a/src/NeuralNetwork/ActivationFunction/HyperbolicTangent.php b/src/NeuralNetwork/ActivationFunction/HyperbolicTangent.php index 7aa9614..c230aff 100644 --- a/src/NeuralNetwork/ActivationFunction/HyperbolicTangent.php +++ b/src/NeuralNetwork/ActivationFunction/HyperbolicTangent.php @@ -32,6 +32,6 @@ class HyperbolicTangent implements ActivationFunction */ public function differentiate($value, $computedvalue): float { - return 1 - pow($computedvalue, 2); + return 1 - $computedvalue ** 2; } } diff --git a/src/Preprocessing/Normalizer.php b/src/Preprocessing/Normalizer.php index fb14ada..9888e0e 100644 --- a/src/Preprocessing/Normalizer.php +++ b/src/Preprocessing/Normalizer.php @@ -106,7 +106,7 @@ class Normalizer implements Preprocessor $norm2 += $feature * $feature; } - $norm2 = sqrt((float) $norm2); + $norm2 **= .5; if ($norm2 == 0) { $sample = array_fill(0, count($sample), 1);