2016-04-04 20:38:51 +00:00
|
|
|
<?php
|
2016-04-04 20:49:54 +00:00
|
|
|
|
|
|
|
declare (strict_types = 1);
|
2016-04-04 20:38:51 +00:00
|
|
|
|
|
|
|
namespace tests\Phpml\Metric;
|
|
|
|
|
|
|
|
use Phpml\Metric\Distance;
|
|
|
|
|
|
|
|
class DistanceTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @expectedException \Phpml\Exception\InvalidArgumentException
|
|
|
|
*/
|
2016-04-04 20:48:07 +00:00
|
|
|
public function testThrowExceptionOnInvalidArgumentsInEuclidean()
|
2016-04-04 20:38:51 +00:00
|
|
|
{
|
2016-04-04 20:48:07 +00:00
|
|
|
$a = [0, 1, 2];
|
|
|
|
$b = [0, 2];
|
|
|
|
|
|
|
|
Distance::euclidean($a, $b);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCalculateEuclideanDistanceForOneDimension()
|
|
|
|
{
|
|
|
|
$a = [4];
|
|
|
|
$b = [2];
|
|
|
|
|
|
|
|
$expectedDistance = 2;
|
|
|
|
$actualDistance = Distance::euclidean($a, $b);
|
|
|
|
|
2016-04-05 19:35:06 +00:00
|
|
|
$this->assertEquals($expectedDistance, $actualDistance);
|
2016-04-04 20:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCalculateEuclideanDistanceForTwoAndMoreDimension()
|
|
|
|
{
|
|
|
|
$a = [4, 6];
|
|
|
|
$b = [2, 5];
|
|
|
|
|
|
|
|
$expectedDistance = 2.2360679774998;
|
|
|
|
$actualDistance = Distance::euclidean($a, $b);
|
|
|
|
|
2016-04-05 19:35:06 +00:00
|
|
|
$this->assertEquals($expectedDistance, $actualDistance);
|
2016-04-04 20:48:07 +00:00
|
|
|
|
|
|
|
$a = [6, 10, 3];
|
|
|
|
$b = [2, 5, 5];
|
|
|
|
|
|
|
|
$expectedDistance = 6.7082039324993694;
|
|
|
|
$actualDistance = Distance::euclidean($a, $b);
|
|
|
|
|
2016-04-05 19:35:06 +00:00
|
|
|
$this->assertEquals($expectedDistance, $actualDistance);
|
2016-04-04 20:38:51 +00:00
|
|
|
}
|
|
|
|
}
|