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)); $sum = array_sum(array_column($countMatrix, $i));
if ($sum > 0) { if ($sum > 0) {
foreach ($this->labels as $label) { 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)) { if (is_float($this->featureSubsetRatio)) {
$featureCount = (int) ($this->featureSubsetRatio * $this->featureCount); $featureCount = (int) ($this->featureSubsetRatio * $this->featureCount);
} elseif ($this->featureSubsetRatio === 'sqrt') { } elseif ($this->featureSubsetRatio === 'sqrt') {
$featureCount = (int) sqrt($this->featureCount) + 1; $featureCount = (int) ($this->featureCount ** .5) + 1;
} else { } else {
$featureCount = (int) log($this->featureCount, 2) + 1; $featureCount = (int) log($this->featureCount, 2) + 1;
} }

View File

@ -162,7 +162,7 @@ class NaiveBayes implements Classifier
// scikit-learn did. // scikit-learn did.
// (See : https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/naive_bayes.py) // (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 * 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; return $pdf;
} }

View File

@ -49,7 +49,7 @@ class Point implements ArrayAccess, \Countable
$distance += $difference * $difference; $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 = new Matrix($vectors);
$vectors = array_map(function ($vect) { $vectors = array_map(function ($vect) {
$sum = 0; $sum = 0;
for ($i = 0; $i < count($vect); ++$i) { $count = count($vect);
for ($i = 0; $i < $count; ++$i) {
$sum += $vect[$i] ** 2; $sum += $vect[$i] ** 2;
} }
$sum = sqrt($sum); $sum **= .5;
for ($i = 0; $i < count($vect); ++$i) { for ($i = 0; $i < $count; ++$i) {
$vect[$i] /= $sum; $vect[$i] /= $sum;
} }
@ -208,11 +209,11 @@ class EigenvalueDecomposition
// Generate Householder vector. // Generate Householder vector.
for ($k = 0; $k < $i; ++$k) { for ($k = 0; $k < $i; ++$k) {
$this->d[$k] /= $scale; $this->d[$k] /= $scale;
$h += pow($this->d[$k], 2); $h += $this->d[$k] ** 2;
} }
$f = $this->d[$i_]; $f = $this->d[$i_];
$g = sqrt($h); $g = $h ** .5;
if ($f > 0) { if ($f > 0) {
$g = -$g; $g = -$g;
} }
@ -320,7 +321,7 @@ class EigenvalueDecomposition
$this->e[$this->n - 1] = 0.0; $this->e[$this->n - 1] = 0.0;
$f = 0.0; $f = 0.0;
$tst1 = 0.0; $tst1 = 0.0;
$eps = pow(2.0, -52.0); $eps = 2.0 ** -52.0;
for ($l = 0; $l < $this->n; ++$l) { for ($l = 0; $l < $this->n; ++$l) {
// Find small subdiagonal element // Find small subdiagonal element
@ -443,7 +444,7 @@ class EigenvalueDecomposition
$h += $this->ort[$i] * $this->ort[$i]; $h += $this->ort[$i] * $this->ort[$i];
} }
$g = sqrt($h); $g = $h ** .5;
if ($this->ort[$m] > 0) { if ($this->ort[$m] > 0) {
$g *= -1; $g *= -1;
} }
@ -548,7 +549,7 @@ class EigenvalueDecomposition
$n = $nn - 1; $n = $nn - 1;
$low = 0; $low = 0;
$high = $nn - 1; $high = $nn - 1;
$eps = pow(2.0, -52.0); $eps = 2.0 ** -52.0;
$exshift = 0.0; $exshift = 0.0;
$p = $q = $r = $s = $z = 0; $p = $q = $r = $s = $z = 0;
// Store roots isolated by balanc and compute matrix norm // 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]; $w = $this->H[$n][$n - 1] * $this->H[$n - 1][$n];
$p = ($this->H[$n - 1][$n - 1] - $this->H[$n][$n]) / 2.0; $p = ($this->H[$n - 1][$n - 1] - $this->H[$n][$n]) / 2.0;
$q = $p * $p + $w; $q = $p * $p + $w;
$z = sqrt(abs($q)); $z = abs($q) ** .5;
$this->H[$n][$n] += $exshift; $this->H[$n][$n] += $exshift;
$this->H[$n - 1][$n - 1] += $exshift; $this->H[$n - 1][$n - 1] += $exshift;
$x = $this->H[$n][$n]; $x = $this->H[$n][$n];
@ -620,7 +621,7 @@ class EigenvalueDecomposition
$s = abs($x) + abs($z); $s = abs($x) + abs($z);
$p = $x / $s; $p = $x / $s;
$q = $z / $s; $q = $z / $s;
$r = sqrt($p * $p + $q * $q); $r = ($p * $p + $q * $q) ** .5;
$p /= $r; $p /= $r;
$q /= $r; $q /= $r;
// Row modification // Row modification
@ -682,7 +683,7 @@ class EigenvalueDecomposition
$s = ($y - $x) / 2.0; $s = ($y - $x) / 2.0;
$s *= $s + $w; $s *= $s + $w;
if ($s > 0) { if ($s > 0) {
$s = sqrt($s); $s **= .5;
if ($y < $x) { if ($y < $x) {
$s = -$s; $s = -$s;
} }
@ -750,7 +751,7 @@ class EigenvalueDecomposition
break; break;
} }
$s = sqrt($p * $p + $q * $q + $r * $r); $s = ($p * $p + $q * $q + $r * $r) ** .5;
if ($p < 0) { if ($p < 0) {
$s = -$s; $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; $a = $x[$i] - $meanX;
$b = $y[$i] - $meanY; $b = $y[$i] - $meanY;
$axb += ($a * $b); $axb += ($a * $b);
$a2 += pow($a, 2); $a2 += $a ** 2;
$b2 += pow($b, 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; $std2 = $this->std ** 2;
$mean = $this->mean; $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; --$n;
} }
return sqrt($carry / $n); return ($carry / $n) ** .5;
} }
/** /**

View File

@ -13,7 +13,7 @@ class Gaussian implements ActivationFunction
*/ */
public function compute($value): float 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 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 += $feature * $feature;
} }
$norm2 = sqrt((float) $norm2); $norm2 **= .5;
if ($norm2 == 0) { if ($norm2 == 0) {
$sample = array_fill(0, count($sample), 1); $sample = array_fill(0, count($sample), 1);