diff --git a/src/KMeans/Cluster.php b/src/KMeans/Cluster.php index 68e700f..bfd2d71 100644 --- a/src/KMeans/Cluster.php +++ b/src/KMeans/Cluster.php @@ -1,4 +1,5 @@ points = new \SplObjectStorage; + $this->points = new \SplObjectStorage(); } public function toArray(): array @@ -84,12 +85,12 @@ public function updateCentroid(): void $centroid = $this->space->newPoint(array_fill(0, $this->dimention, 0)); foreach ($this->points as $point) { - for ($n=0; $n<$this->dimention; $n++) { + for ($n = 0; $n < $this->dimention; $n++) { $centroid->coordinates[$n] += $point->coordinates[$n]; } } - for ($n=0; $n<$this->dimention; $n++) { + for ($n = 0; $n < $this->dimention; $n++) { $this->coordinates[$n] = $centroid->coordinates[$n] / $count; } } diff --git a/src/KMeans/Point.php b/src/KMeans/Point.php index 8621fee..6d6e682 100644 --- a/src/KMeans/Point.php +++ b/src/KMeans/Point.php @@ -1,4 +1,5 @@ dimention; $n++) { + for ($n = 0; $n < $this->dimention; $n++) { $difference = $this->coordinates[$n] - $point->coordinates[$n]; $distance += $difference * $difference; } diff --git a/src/KMeans/Space.php b/src/KMeans/Space.php index 73e001e..b7ac42e 100644 --- a/src/KMeans/Space.php +++ b/src/KMeans/Space.php @@ -1,4 +1,5 @@ newPoint(array_fill(0, $this->dimention, null)); foreach ($this as $point) { - for ($n=0; $n < $this->dimention; $n++) { + for ($n = 0; $n < $this->dimention; $n++) { if ($min[$n] === null || $min[$n] > $point[$n]) { $min[$n] = $point[$n]; } @@ -114,7 +115,7 @@ public function getRandomPoint(Point $min, Point $max): Point $point = $this->newPoint(array_fill(0, $this->dimention, null)); $rng = static::$rng; - for ($n=0; $n < $this->dimention; $n++) { + for ($n = 0; $n < $this->dimention; $n++) { $point[$n] = $rng($min[$n], $max[$n]); } @@ -154,7 +155,7 @@ protected function initializeClusters(int $nbClusters): array list($min, $max) = $this->getBoundaries(); // initialize N clusters with a random point within space boundaries - for ($n=0; $n<$nbClusters; $n++) { + for ($n = 0; $n < $nbClusters; $n++) { $clusters[] = new Cluster($this, $this->getRandomPoint($min, $max)->getCoordinates()); } @@ -169,8 +170,8 @@ protected function iterate(array $clusters): bool $continue = false; // migration storages - $attach = new \SplObjectStorage; - $detach = new \SplObjectStorage; + $attach = new \SplObjectStorage(); + $detach = new \SplObjectStorage(); // calculate proximity amongst points and clusters foreach ($clusters as $cluster) { @@ -181,11 +182,11 @@ protected function iterate(array $clusters): bool // move the point from its old cluster to its closest if ($closest !== $cluster) { if (! isset($attach[$closest])) { - $attach[$closest] = new \SplObjectStorage; + $attach[$closest] = new \SplObjectStorage(); } if (! isset($detach[$cluster])) { - $detach[$cluster] = new \SplObjectStorage; + $detach[$cluster] = new \SplObjectStorage(); } $attach[$closest]->attach($point); diff --git a/tests/Kmeans/ClusterTest.php b/tests/Kmeans/ClusterTest.php index 022e4ec..ba69dcb 100644 --- a/tests/Kmeans/ClusterTest.php +++ b/tests/Kmeans/ClusterTest.php @@ -106,7 +106,7 @@ public function testAttachAll() new Point($space, [2,2]), ]; - $storage = new \SplObjectStorage; + $storage = new \SplObjectStorage(); foreach ($points as $point) { $storage->attach($point); } @@ -129,7 +129,7 @@ public function testDetachAll() $cluster->attach($point); } - $storage = new \SplObjectStorage; + $storage = new \SplObjectStorage(); foreach ($points as $point) { $storage->attach($point); }