-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
permutable point distance algorithm #12
- Loading branch information
1 parent
8695727
commit a4f3b16
Showing
10 changed files
with
180 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace KMeans\Algorithms; | ||
|
||
use KMeans\Interfaces\DistanceAlgorithmInterface; | ||
use KMeans\Interfaces\PointInterface; | ||
|
||
class CallbackDistance implements DistanceAlgorithmInterface | ||
{ | ||
private $fn; | ||
|
||
public function __construct(callable $fn) | ||
{ | ||
$this->fn = $fn; | ||
} | ||
|
||
public function getDistanceBetween(PointInterface $pointA, PointInterface $pointB): float | ||
{ | ||
return call_user_func($this->fn, $pointA, $pointB); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace KMeans\Algorithms; | ||
|
||
use KMeans\Interfaces\DistanceAlgorithmInterface; | ||
use KMeans\Interfaces\PointInterface; | ||
|
||
class EuclidianDistance implements DistanceAlgorithmInterface | ||
{ | ||
public function getDistanceBetween(PointInterface $pointA, PointInterface $pointB): float | ||
{ | ||
if ($pointA->getSpace()->getDimention() !== $pointB->getSpace()->getDimention()) { | ||
throw new \LogicException( | ||
"Cannot calculate euclidian distance between point of different dimentions" | ||
); | ||
} | ||
|
||
$distance = 0; | ||
$dimention = $pointA->getSpace()->getDimention(); | ||
$coordinatesA = $pointA->getCoordinates(); | ||
$coordinatesB = $pointB->getCoordinates(); | ||
|
||
for ($n = 0; $n < $dimention; $n++) { | ||
$difference = $coordinatesA[$n] - $coordinatesB[$n]; | ||
$distance += $difference ** 2; | ||
} | ||
|
||
return sqrt($distance); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace KMeans\Interfaces; | ||
|
||
use KMeans\Interfaces\PointInterface; | ||
|
||
interface DistanceAlgorithmInterface | ||
{ | ||
public function getDistanceBetween(PointInterface $pointA, PointInterface $pointB): float; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace KMeans\Interfaces; | ||
|
||
use KMeans\Interfaces\DistanceAlgorithmInterface; | ||
use KMeans\Interfaces\SpaceInterface; | ||
|
||
interface PointInterface | ||
{ | ||
public function getSpace(): SpaceInterface; | ||
|
||
public function getCoordinates(): array; | ||
|
||
public function setDistanceAlgorithm(DistanceAlgorithmInterface $algo): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace KMeans\Interfaces; | ||
|
||
use KMeans\Interfaces\DistanceAlgorithmInterface; | ||
|
||
interface SpaceInterface | ||
{ | ||
public function getDimention(): int; | ||
|
||
public function setDistanceAlgorithm($algo): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters