2016-04-04 20:38:51 +00:00
|
|
|
<?php
|
2016-04-04 20:49:54 +00:00
|
|
|
|
2016-11-20 21:53:17 +00:00
|
|
|
declare(strict_types=1);
|
2016-04-04 20:38:51 +00:00
|
|
|
|
2018-01-06 12:09:33 +00:00
|
|
|
namespace Phpml\Tests\Math\Distance;
|
2016-04-04 20:38:51 +00:00
|
|
|
|
2017-11-28 07:00:13 +00:00
|
|
|
use Phpml\Exception\InvalidArgumentException;
|
2016-04-20 21:56:33 +00:00
|
|
|
use Phpml\Math\Distance\Euclidean;
|
2017-02-03 11:58:25 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-04-04 20:38:51 +00:00
|
|
|
|
2017-02-03 11:58:25 +00:00
|
|
|
class EuclideanTest extends TestCase
|
2016-04-04 20:38:51 +00:00
|
|
|
{
|
2016-04-11 19:35:17 +00:00
|
|
|
/**
|
|
|
|
* @var Euclidean
|
|
|
|
*/
|
|
|
|
private $distanceMetric;
|
|
|
|
|
2018-06-15 05:57:45 +00:00
|
|
|
protected function setUp(): void
|
2016-04-11 19:35:17 +00:00
|
|
|
{
|
|
|
|
$this->distanceMetric = new Euclidean();
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testThrowExceptionOnInvalidArguments(): void
|
2016-04-04 20:38:51 +00:00
|
|
|
{
|
2017-11-28 07:00:13 +00:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2016-04-04 20:48:07 +00:00
|
|
|
$a = [0, 1, 2];
|
|
|
|
$b = [0, 2];
|
2016-04-11 19:35:17 +00:00
|
|
|
$this->distanceMetric->distance($a, $b);
|
2016-04-04 20:48:07 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testCalculateDistanceForOneDimension(): void
|
2016-04-04 20:48:07 +00:00
|
|
|
{
|
|
|
|
$a = [4];
|
|
|
|
$b = [2];
|
|
|
|
|
|
|
|
$expectedDistance = 2;
|
2016-04-11 19:35:17 +00:00
|
|
|
$actualDistance = $this->distanceMetric->distance($a, $b);
|
2016-04-04 20:48:07 +00:00
|
|
|
|
2016-04-05 19:35:06 +00:00
|
|
|
$this->assertEquals($expectedDistance, $actualDistance);
|
2016-04-04 20:48:07 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testCalculateDistanceForTwoDimensions(): void
|
2016-04-04 20:48:07 +00:00
|
|
|
{
|
|
|
|
$a = [4, 6];
|
|
|
|
$b = [2, 5];
|
|
|
|
|
|
|
|
$expectedDistance = 2.2360679774998;
|
2016-04-11 19:35:17 +00:00
|
|
|
$actualDistance = $this->distanceMetric->distance($a, $b);
|
2016-04-04 20:48:07 +00:00
|
|
|
|
2016-04-05 19:35:06 +00:00
|
|
|
$this->assertEquals($expectedDistance, $actualDistance);
|
2016-04-11 19:44:48 +00:00
|
|
|
}
|
2016-04-04 20:48:07 +00:00
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testCalculateDistanceForThreeDimensions(): void
|
2016-04-11 19:44:48 +00:00
|
|
|
{
|
2016-04-04 20:48:07 +00:00
|
|
|
$a = [6, 10, 3];
|
|
|
|
$b = [2, 5, 5];
|
|
|
|
|
|
|
|
$expectedDistance = 6.7082039324993694;
|
2016-04-11 19:35:17 +00:00
|
|
|
$actualDistance = $this->distanceMetric->distance($a, $b);
|
2016-04-04 20:48:07 +00:00
|
|
|
|
2016-04-05 19:35:06 +00:00
|
|
|
$this->assertEquals($expectedDistance, $actualDistance);
|
2016-04-04 20:38:51 +00:00
|
|
|
}
|
|
|
|
}
|