Skip to content

Commit

Permalink
Issue #351: Replace pow() and sqrt() with double stars notation. (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol authored and akondas committed Feb 8, 2019
1 parent 4b837fa commit 40f1ca0
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Classification/DecisionTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function getGiniIndex($baseValue, array $colValues, array $targets): floa
$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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Classification/Ensemble/RandomForest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ protected function initSingleClassifier(Classifier $classifier): Classifier
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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Classification/NaiveBayes.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private function sampleProbability(array $sample, int $feature, string $label):
// 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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Clustering/KMeans/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getDistanceWith(self $point, bool $precise = true)
$distance += $difference * $difference;
}

return $precise ? sqrt((float) $distance) : $distance;
return $precise ? $distance ** .5 : $distance;
}

/**
Expand Down
25 changes: 13 additions & 12 deletions src/Math/LinearAlgebra/EigenvalueDecomposition.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,13 @@ public function getEigenvectors(): array
$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;
}

Expand Down Expand Up @@ -208,11 +209,11 @@ private function tred2(): void
// 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;
}
Expand Down Expand Up @@ -320,7 +321,7 @@ private function tql2(): void
$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
Expand Down Expand Up @@ -443,7 +444,7 @@ private function orthes(): void
$h += $this->ort[$i] * $this->ort[$i];
}

$g = sqrt($h);
$g = $h ** .5;
if ($this->ort[$m] > 0) {
$g *= -1;
}
Expand Down Expand Up @@ -548,7 +549,7 @@ private function hqr2(): void
$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
Expand Down Expand Up @@ -596,7 +597,7 @@ private function hqr2(): void
$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];
Expand All @@ -620,7 +621,7 @@ private function hqr2(): void
$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
Expand Down Expand Up @@ -682,7 +683,7 @@ private function hqr2(): void
$s = ($y - $x) / 2.0;
$s *= $s + $w;
if ($s > 0) {
$s = sqrt($s);
$s **= .5;
if ($y < $x) {
$s = -$s;
}
Expand Down Expand Up @@ -750,7 +751,7 @@ private function hqr2(): void
break;
}

$s = sqrt($p * $p + $q * $q + $r * $r);
$s = ($p * $p + $q * $q + $r * $r) ** .5;
if ($p < 0) {
$s = -$s;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Math/Matrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public function frobeniusNorm(): float
}
}

return sqrt($squareSum);
return $squareSum ** .5;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Math/Statistic/Correlation.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public static function pearson(array $x, array $y): float
$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;
}
}
2 changes: 1 addition & 1 deletion src/Math/Statistic/Gaussian.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function pdf(float $value)
$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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Math/Statistic/StandardDeviation.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function population(array $numbers, bool $sample = true): float
--$n;
}

return sqrt($carry / $n);
return ($carry / $n) ** .5;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/NeuralNetwork/ActivationFunction/Gaussian.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Gaussian implements ActivationFunction
*/
public function compute($value): float
{
return exp(-pow($value, 2));
return exp(- $value ** 2);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/NeuralNetwork/ActivationFunction/HyperbolicTangent.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public function compute($value): float
*/
public function differentiate($value, $computedvalue): float
{
return 1 - pow($computedvalue, 2);
return 1 - $computedvalue ** 2;
}
}
2 changes: 1 addition & 1 deletion src/Preprocessing/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private function normalizeL2(array &$sample): void
$norm2 += $feature * $feature;
}

$norm2 = sqrt((float) $norm2);
$norm2 **= .5;

if ($norm2 == 0) {
$sample = array_fill(0, count($sample), 1);
Expand Down

0 comments on commit 40f1ca0

Please sign in to comment.