Skip to content

Commit 40f1ca0

Browse files
drupolakondas
authored andcommitted
Issue #351: Replace pow() and sqrt() with double stars notation. (#352)
1 parent 4b837fa commit 40f1ca0

File tree

12 files changed

+26
-25
lines changed

12 files changed

+26
-25
lines changed

src/Classification/DecisionTree.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function getGiniIndex($baseValue, array $colValues, array $targets): floa
137137
$sum = array_sum(array_column($countMatrix, $i));
138138
if ($sum > 0) {
139139
foreach ($this->labels as $label) {
140-
$part += pow($countMatrix[$label][$i] / (float) $sum, 2);
140+
$part += ($countMatrix[$label][$i] / (float) $sum) ** 2;
141141
}
142142
}
143143

src/Classification/Ensemble/RandomForest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ protected function initSingleClassifier(Classifier $classifier): Classifier
131131
if (is_float($this->featureSubsetRatio)) {
132132
$featureCount = (int) ($this->featureSubsetRatio * $this->featureCount);
133133
} elseif ($this->featureSubsetRatio === 'sqrt') {
134-
$featureCount = (int) sqrt($this->featureCount) + 1;
134+
$featureCount = (int) ($this->featureCount ** .5) + 1;
135135
} else {
136136
$featureCount = (int) log($this->featureCount, 2) + 1;
137137
}

src/Classification/NaiveBayes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private function sampleProbability(array $sample, int $feature, string $label):
162162
// scikit-learn did.
163163
// (See : https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/naive_bayes.py)
164164
$pdf = -0.5 * log(2.0 * M_PI * $std * $std);
165-
$pdf -= 0.5 * pow($value - $mean, 2) / ($std * $std);
165+
$pdf -= 0.5 * (($value - $mean) ** 2) / ($std * $std);
166166

167167
return $pdf;
168168
}

src/Clustering/KMeans/Point.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function getDistanceWith(self $point, bool $precise = true)
4949
$distance += $difference * $difference;
5050
}
5151

52-
return $precise ? sqrt((float) $distance) : $distance;
52+
return $precise ? $distance ** .5 : $distance;
5353
}
5454

5555
/**

src/Math/LinearAlgebra/EigenvalueDecomposition.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,13 @@ public function getEigenvectors(): array
128128
$vectors = new Matrix($vectors);
129129
$vectors = array_map(function ($vect) {
130130
$sum = 0;
131-
for ($i = 0; $i < count($vect); ++$i) {
131+
$count = count($vect);
132+
for ($i = 0; $i < $count; ++$i) {
132133
$sum += $vect[$i] ** 2;
133134
}
134135

135-
$sum = sqrt($sum);
136-
for ($i = 0; $i < count($vect); ++$i) {
136+
$sum **= .5;
137+
for ($i = 0; $i < $count; ++$i) {
137138
$vect[$i] /= $sum;
138139
}
139140

@@ -208,11 +209,11 @@ private function tred2(): void
208209
// Generate Householder vector.
209210
for ($k = 0; $k < $i; ++$k) {
210211
$this->d[$k] /= $scale;
211-
$h += pow($this->d[$k], 2);
212+
$h += $this->d[$k] ** 2;
212213
}
213214

214215
$f = $this->d[$i_];
215-
$g = sqrt($h);
216+
$g = $h ** .5;
216217
if ($f > 0) {
217218
$g = -$g;
218219
}
@@ -320,7 +321,7 @@ private function tql2(): void
320321
$this->e[$this->n - 1] = 0.0;
321322
$f = 0.0;
322323
$tst1 = 0.0;
323-
$eps = pow(2.0, -52.0);
324+
$eps = 2.0 ** -52.0;
324325

325326
for ($l = 0; $l < $this->n; ++$l) {
326327
// Find small subdiagonal element
@@ -443,7 +444,7 @@ private function orthes(): void
443444
$h += $this->ort[$i] * $this->ort[$i];
444445
}
445446

446-
$g = sqrt($h);
447+
$g = $h ** .5;
447448
if ($this->ort[$m] > 0) {
448449
$g *= -1;
449450
}
@@ -548,7 +549,7 @@ private function hqr2(): void
548549
$n = $nn - 1;
549550
$low = 0;
550551
$high = $nn - 1;
551-
$eps = pow(2.0, -52.0);
552+
$eps = 2.0 ** -52.0;
552553
$exshift = 0.0;
553554
$p = $q = $r = $s = $z = 0;
554555
// Store roots isolated by balanc and compute matrix norm
@@ -596,7 +597,7 @@ private function hqr2(): void
596597
$w = $this->H[$n][$n - 1] * $this->H[$n - 1][$n];
597598
$p = ($this->H[$n - 1][$n - 1] - $this->H[$n][$n]) / 2.0;
598599
$q = $p * $p + $w;
599-
$z = sqrt(abs($q));
600+
$z = abs($q) ** .5;
600601
$this->H[$n][$n] += $exshift;
601602
$this->H[$n - 1][$n - 1] += $exshift;
602603
$x = $this->H[$n][$n];
@@ -620,7 +621,7 @@ private function hqr2(): void
620621
$s = abs($x) + abs($z);
621622
$p = $x / $s;
622623
$q = $z / $s;
623-
$r = sqrt($p * $p + $q * $q);
624+
$r = ($p * $p + $q * $q) ** .5;
624625
$p /= $r;
625626
$q /= $r;
626627
// Row modification
@@ -682,7 +683,7 @@ private function hqr2(): void
682683
$s = ($y - $x) / 2.0;
683684
$s *= $s + $w;
684685
if ($s > 0) {
685-
$s = sqrt($s);
686+
$s **= .5;
686687
if ($y < $x) {
687688
$s = -$s;
688689
}
@@ -750,7 +751,7 @@ private function hqr2(): void
750751
break;
751752
}
752753

753-
$s = sqrt($p * $p + $q * $q + $r * $r);
754+
$s = ($p * $p + $q * $q + $r * $r) ** .5;
754755
if ($p < 0) {
755756
$s = -$s;
756757
}

src/Math/Matrix.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public function frobeniusNorm(): float
271271
}
272272
}
273273

274-
return sqrt($squareSum);
274+
return $squareSum ** .5;
275275
}
276276

277277
/**

src/Math/Statistic/Correlation.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ public static function pearson(array $x, array $y): float
3232
$a = $x[$i] - $meanX;
3333
$b = $y[$i] - $meanY;
3434
$axb += ($a * $b);
35-
$a2 += pow($a, 2);
36-
$b2 += pow($b, 2);
35+
$a2 += $a ** 2;
36+
$b2 += $b ** 2;
3737
}
3838

39-
return $axb / sqrt((float) ($a2 * $b2));
39+
return $axb / ($a2 * $b2) ** .5;
4040
}
4141
}

src/Math/Statistic/Gaussian.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function pdf(float $value)
3434
$std2 = $this->std ** 2;
3535
$mean = $this->mean;
3636

37-
return exp(-(($value - $mean) ** 2) / (2 * $std2)) / sqrt(2 * $std2 * M_PI);
37+
return exp(-(($value - $mean) ** 2) / (2 * $std2)) / ((2 * $std2 * M_PI) ** .5);
3838
}
3939

4040
/**

src/Math/Statistic/StandardDeviation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function population(array $numbers, bool $sample = true): float
3232
--$n;
3333
}
3434

35-
return sqrt($carry / $n);
35+
return ($carry / $n) ** .5;
3636
}
3737

3838
/**

src/NeuralNetwork/ActivationFunction/Gaussian.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Gaussian implements ActivationFunction
1313
*/
1414
public function compute($value): float
1515
{
16-
return exp(-pow($value, 2));
16+
return exp(- $value ** 2);
1717
}
1818

1919
/**

0 commit comments

Comments
 (0)