Issue #351: Replace pow() and sqrt() with double stars notation. (#352)

This commit is contained in:
Pol Dellaiera 2019-02-08 22:24:02 +01:00 committed by Arkadiusz Kondas
parent 4b837fae8e
commit 40f1ca06aa
12 changed files with 26 additions and 25 deletions

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -49,7 +49,7 @@ class Point implements ArrayAccess, \Countable
$distance += $difference * $difference;
}
return $precise ? sqrt((float) $distance) : $distance;
return $precise ? $distance ** .5 : $distance;
}
/**

View File

@ -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;
}

View File

@ -271,7 +271,7 @@ class Matrix
}
}
return sqrt($squareSum);
return $squareSum ** .5;
}
/**

View File

@ -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;
}
}

View File

@ -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);
}
/**

View File

@ -32,7 +32,7 @@ class StandardDeviation
--$n;
}
return sqrt($carry / $n);
return ($carry / $n) ** .5;
}
/**

View File

@ -13,7 +13,7 @@ class Gaussian implements ActivationFunction
*/
public function compute($value): float
{
return exp(-pow($value, 2));
return exp(- $value ** 2);
}
/**

View File

@ -32,6 +32,6 @@ class HyperbolicTangent implements ActivationFunction
*/
public function differentiate($value, $computedvalue): float
{
return 1 - pow($computedvalue, 2);
return 1 - $computedvalue ** 2;
}
}

View File

@ -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);