2016-04-12 22:02:14 +02:00
|
|
|
<?php
|
|
|
|
|
2016-11-20 22:53:17 +01:00
|
|
|
declare(strict_types=1);
|
2016-04-12 22:02:14 +02:00
|
|
|
|
2018-01-06 13:09:33 +01:00
|
|
|
namespace Phpml\Tests\Math\Distance;
|
2016-04-12 22:02:14 +02:00
|
|
|
|
2017-11-28 08:00:13 +01:00
|
|
|
use Phpml\Exception\InvalidArgumentException;
|
2016-04-20 23:56:33 +02:00
|
|
|
use Phpml\Math\Distance\Minkowski;
|
2017-02-03 12:58:25 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-04-12 22:02:14 +02:00
|
|
|
|
2017-02-03 12:58:25 +01:00
|
|
|
class MinkowskiTest extends TestCase
|
2016-04-12 22:02:14 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Minkowski
|
|
|
|
*/
|
|
|
|
private $distanceMetric;
|
|
|
|
|
2017-11-14 21:21:23 +01:00
|
|
|
public function setUp(): void
|
2016-04-12 22:02:14 +02:00
|
|
|
{
|
|
|
|
$this->distanceMetric = new Minkowski();
|
|
|
|
}
|
|
|
|
|
2017-11-14 21:21:23 +01:00
|
|
|
public function testThrowExceptionOnInvalidArguments(): void
|
2016-04-12 22:02:14 +02:00
|
|
|
{
|
2017-11-28 08:00:13 +01:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2016-04-12 22:02:14 +02:00
|
|
|
$a = [0, 1, 2];
|
|
|
|
$b = [0, 2];
|
|
|
|
$this->distanceMetric->distance($a, $b);
|
|
|
|
}
|
|
|
|
|
2017-11-14 21:21:23 +01:00
|
|
|
public function testCalculateDistanceForOneDimension(): void
|
2016-04-12 22:02:14 +02:00
|
|
|
{
|
|
|
|
$a = [4];
|
|
|
|
$b = [2];
|
|
|
|
|
|
|
|
$expectedDistance = 2;
|
|
|
|
$actualDistance = $this->distanceMetric->distance($a, $b);
|
|
|
|
|
|
|
|
$this->assertEquals($expectedDistance, $actualDistance);
|
|
|
|
}
|
|
|
|
|
2017-11-14 21:21:23 +01:00
|
|
|
public function testCalculateDistanceForTwoDimensions(): void
|
2016-04-12 22:02:14 +02:00
|
|
|
{
|
|
|
|
$a = [4, 6];
|
|
|
|
$b = [2, 5];
|
|
|
|
|
|
|
|
$expectedDistance = 2.080;
|
|
|
|
$actualDistance = $this->distanceMetric->distance($a, $b);
|
|
|
|
|
2016-04-12 23:10:33 +02:00
|
|
|
$this->assertEquals($expectedDistance, $actualDistance, '', $delta = 0.001);
|
2016-04-12 22:02:14 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 21:21:23 +01:00
|
|
|
public function testCalculateDistanceForThreeDimensions(): void
|
2016-04-12 22:02:14 +02:00
|
|
|
{
|
|
|
|
$a = [6, 10, 3];
|
|
|
|
$b = [2, 5, 5];
|
|
|
|
|
|
|
|
$expectedDistance = 5.819;
|
|
|
|
$actualDistance = $this->distanceMetric->distance($a, $b);
|
|
|
|
|
2016-04-12 23:10:33 +02:00
|
|
|
$this->assertEquals($expectedDistance, $actualDistance, '', $delta = 0.001);
|
2016-04-12 22:02:14 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 21:21:23 +01:00
|
|
|
public function testCalculateDistanceForThreeDimensionsWithDifferentLambda(): void
|
2016-04-12 22:02:14 +02:00
|
|
|
{
|
|
|
|
$distanceMetric = new Minkowski($lambda = 5);
|
|
|
|
|
|
|
|
$a = [6, 10, 3];
|
|
|
|
$b = [2, 5, 5];
|
|
|
|
|
|
|
|
$expectedDistance = 5.300;
|
|
|
|
$actualDistance = $distanceMetric->distance($a, $b);
|
|
|
|
|
2016-04-12 23:10:33 +02:00
|
|
|
$this->assertEquals($expectedDistance, $actualDistance, '', $delta = 0.001);
|
2016-04-12 22:02:14 +02:00
|
|
|
}
|
|
|
|
}
|