Skip to content

Commit

Permalink
added Vector
Browse files Browse the repository at this point in the history
  • Loading branch information
uestla committed Oct 22, 2023
1 parent 2dc70ab commit 267574c
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
63 changes: 63 additions & 0 deletions Simplex/Math/Vector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types = 1);

/**
* This file is part of the SimplexCalculator library
*
* Copyright (c) 2014 Petr Kessler (https://kesspess.cz)
*
* @license MIT
* @link https://github.com/uestla/Simplex-Calculator
*/

namespace Simplex\Math;

use Simplex\EmptyVectorException;


final class Vector implements \Countable
{

/** @var Fraction[] $values */
private readonly array $values;

private readonly int $size;


/** @param array<int, Fraction|string|int|float> $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<int, Fraction|string|int|float> $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;
}

}
11 changes: 11 additions & 0 deletions Simplex/exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{}
1 change: 1 addition & 0 deletions Simplex/simplex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
71 changes: 71 additions & 0 deletions tests/VectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types = 1);

namespace Simplex\Tests;

use Tester\Assert;
use Tester\TestCase;
use Simplex\Math\Vector;
use Simplex\Math\Fraction;
use Simplex\EmptyVectorException;


require_once __DIR__ . '/bootstrap.php';


final class VectorTest extends TestCase
{

/**
* @param array<int|float|string|Fraction> $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<int, array{array<int|float|string|Fraction>, 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();

0 comments on commit 267574c

Please sign in to comment.