diff --git a/Simplex/Math/Vector.php b/Simplex/Math/Vector.php new file mode 100644 index 0000000..beef509 --- /dev/null +++ b/Simplex/Math/Vector.php @@ -0,0 +1,63 @@ + $values */ + public function __construct(array $values) + { + if ($values === []) { + throw new EmptyVectorException; + } + + $this->values = array_map(static function ($value): Fraction { + return Fraction::create($value); + + }, array_values($values)); + + $this->size = count($this->values); + } + + + /** @param Vector|array $values */ + public static function create(self|array $values): self + { + return $values instanceof self ? $values : new self($values); + } + + + /** @return Fraction[] */ + public function toArray(): array + { + return $this->values; + } + + + public function count(): int + { + return $this->size; + } + +} diff --git a/Simplex/exceptions.php b/Simplex/exceptions.php index 5754356..6561ca0 100644 --- a/Simplex/exceptions.php +++ b/Simplex/exceptions.php @@ -58,5 +58,16 @@ public function __construct() } +final class EmptyVectorException extends SimplexException +{ + + public function __construct() + { + parent::__construct('Vector must have at least one value.'); + } + +} + + abstract class SimplexException extends \Exception {} diff --git a/Simplex/simplex.php b/Simplex/simplex.php index cd3931f..d22439b 100644 --- a/Simplex/simplex.php +++ b/Simplex/simplex.php @@ -17,3 +17,4 @@ require_once __DIR__ . '/exceptions.php'; require_once __DIR__ . '/Math/math.php'; require_once __DIR__ . '/Math/Fraction.php'; +require_once __DIR__ . '/Math/Vector.php'; diff --git a/tests/VectorTest.php b/tests/VectorTest.php new file mode 100644 index 0000000..16a2476 --- /dev/null +++ b/tests/VectorTest.php @@ -0,0 +1,71 @@ + $values + * @param Fraction[] $expectedValues + * @dataProvider provideCreationData + */ + public function testCreation(array $values, array $expectedValues, int $expectedCount): void + { + $vectorConstructor = new Vector($values); + Assert::equal($expectedValues, $vectorConstructor->toArray()); + Assert::count($expectedCount, $vectorConstructor); + + $vectorFactory = Vector::create($values); + Assert::equal($expectedValues, $vectorFactory->toArray()); + Assert::count($expectedCount, $vectorFactory); + } + + + /** @return array, Fraction[], int}> */ + public function provideCreationData(): array + { + return [ + [ + [1, '2', 3.0, new Fraction('4')], + [new Fraction('1'), new Fraction('2'), new Fraction('3'), new Fraction('4')], + 4, + ], + [ + ['a' => 1, 'b' => '2', 3.0, 'xyz' => new Fraction('4')], + [new Fraction('1'), new Fraction('2'), new Fraction('3'), new Fraction('4')], + 4, + ], + ]; + } + + + public function testEmptyVector(): void + { + Assert::exception(static function (): void { + new Vector([]); + + }, EmptyVectorException::class, 'Vector must have at least one value.'); + + Assert::exception(static function (): void { + Vector::create([]); + + }, EmptyVectorException::class, 'Vector must have at least one value.'); + } + +} + + +(new VectorTest)->run();