diff --git a/.gitattributes b/.gitattributes index 4160131..05f17e3 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,4 +1,3 @@ /.gitattributes export-ignore /.gitignore export-ignore -/phpunit.xml.dist export-ignore /tests export-ignore diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index feba0f6..2f3eecb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,68 +3,12 @@ name: CI on: [push, pull_request] jobs: - phpunit: - runs-on: ${{ matrix.os }} - strategy: - matrix: - os: [ubuntu-latest, macOS-latest] - php-version: ['8.2', '8.3'] - dependencies: ['lowest', 'highest'] - name: 'PHPUnit' - steps: - - name: Checkout - uses: actions/checkout@v2 - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-version }} - extensions: mbstring, intl - coverage: xdebug - - name: Composer - uses: "ramsey/composer-install@v2" - with: - dependency-versions: ${{ matrix.dependencies }} - - name: PHPUnit - run: vendor/bin/phpunit --coverage-clover=coverage.clover - - uses: codecov/codecov-action@v1 - with: - token: ${{ secrets.CODECOV_TOKEN }} + blackbox: + uses: innmind/github-workflows/.github/workflows/black-box-matrix.yml@main + coverage: + uses: innmind/github-workflows/.github/workflows/coverage-matrix.yml@main + secrets: inherit psalm: - runs-on: ubuntu-latest - strategy: - matrix: - php-version: ['8.2', '8.3'] - dependencies: ['lowest', 'highest'] - name: 'Psalm' - steps: - - name: Checkout - uses: actions/checkout@v2 - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-version }} - extensions: mbstring, intl - - name: Composer - uses: "ramsey/composer-install@v2" - with: - dependency-versions: ${{ matrix.dependencies }} - - name: Psalm - run: vendor/bin/psalm --shepherd + uses: innmind/github-workflows/.github/workflows/psalm-matrix.yml@main cs: - runs-on: ubuntu-latest - strategy: - matrix: - php-version: ['8.2'] - name: 'CS' - steps: - - name: Checkout - uses: actions/checkout@v2 - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-version }} - extensions: mbstring, intl - - name: Composer - uses: "ramsey/composer-install@v2" - - name: CS - run: vendor/bin/php-cs-fixer fix --diff --dry-run + uses: innmind/github-workflows/.github/workflows/cs.yml@main diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..b25ad8a --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,11 @@ +name: Create release + +on: + push: + tags: + - '*' + +jobs: + release: + uses: innmind/github-workflows/.github/workflows/release.yml@main + secrets: inherit diff --git a/.gitignore b/.gitignore index e96516b..987e2a2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ composer.lock vendor -.phpunit.result.cache diff --git a/CHANGELOG.md b/CHANGELOG.md index 26acd42..fdb7728 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,54 @@ ## [Unreleased] +### Added + +- `Innmind\Math\Algebra\Number::apply()` +- `Innmind\Math\Algebra\Number::as()` +- `Innmind\Math\Algebra\Number::memoize()` +- `Innmind\Math\Algebra\Number::optimize()` +- `Innmind\Math\Algebra\Func` +- `Innmind\Math\Algebra\Logarithm` +- `Innmind\Math\Geometry\Trigonometry` +- `Innmind\Math\Geometry\Angle\Degree::cosine()` +- `Innmind\Math\Geometry\Angle\Degree::sine()` +- `Innmind\Math\Geometry\Angle\Degree::tangent()` +- `Innmind\Math\Geometry\Angle\Radian::cosine()` +- `Innmind\Math\Geometry\Angle\Radian::sine()` +- `Innmind\Math\Geometry\Angle\Radian::tangent()` + +### Changed + +- Requires PHP `8.4` +- `Innmind\Math\DefinitionSet\Set` is now a final class, all previous implementations are now flagged as internal +- `Innmind\Math\Monoid\*` are now enums + - `Addition` is now accessible via `Algebra::addition` + - `Multiplication` is now accessible via `Algebra::multiplication` +- `Innmind\Math\Probabilities\BinomialDistribution::__invoke()` arguments now expects `int`s +- `Innmind\Math\Algebra\Number` is now a final class, all previous implementations are now flagged as internal +- Requires `innmind/immutable:~6.0` +- `Innmind\Math\Geometry\*` methods are no longer prefixed with `is` +- `Innmind\Math\DefinitionSet\Set::accept()` now returns an `Innmind\Immutable\Attempt` + ### Fixed - PHP `8.4` deprecations +### Removed + +- `Innmind\Math\Algebra\Operation` +- The possibility to specify multiple values to `Innmind\Math\Algebra\Number::add()`, `::multiplyBy()` and `::subtract()` +- `Innmind\Math\Geometry\Trigonometry\ArcCosine` +- `Innmind\Math\Geometry\Trigonometry\ArcSine` +- `Innmind\Math\Geometry\Trigonometry\ArcTangent` +- `Innmind\Math\Geometry\Trigonometry\Cosine` +- `Innmind\Math\Geometry\Trigonometry\Sine` +- `Innmind\Math\Geometry\Trigonometry\Tangent` +- `Innmind\Math\Algebra\Number::binaryLogarithm()`, use `->apply(Logarithm::binary)` instead +- `Innmind\Math\Algebra\Number::commonLogarithm()`, use `->apply(Logarithm::common)` instead +- `Innmind\Math\Algebra\Number::naturalLogarithm()`, use `->apply(Logarithm::natural)` instead +- `Innmind\Math\Algebra\Number::collapse()`, use `->optimize()->memoize()` instead + ## 6.1.0 - 2023-09-23 ### Added diff --git a/blackbox.php b/blackbox.php new file mode 100644 index 0000000..dcc7e0b --- /dev/null +++ b/blackbox.php @@ -0,0 +1,26 @@ +when( + \getenv('ENABLE_COVERAGE') !== false, + static fn(Application $app) => $app + ->codeCoverage( + CodeCoverage::of( + __DIR__.'/src/', + __DIR__.'/tests/', + ) + ->dumpTo('coverage.clover') + ->enableWhen(true), + ), + ) + ->tryToProve(Load::directory(__DIR__.'/tests/')) + ->exit(); diff --git a/composer.json b/composer.json index c67a0b0..8e80fe3 100644 --- a/composer.json +++ b/composer.json @@ -15,8 +15,8 @@ "issues": "http://github.com/Innmind/Math/issues" }, "require": { - "php": "~8.2", - "innmind/immutable": "~4.15|~5.0" + "php": "~8.4", + "innmind/immutable": "~6.0" }, "autoload": { "psr-4": { @@ -35,8 +35,8 @@ ] }, "require-dev": { - "phpunit/phpunit": "~9.0", - "innmind/static-analysis": "^1.2.1", + "innmind/black-box": "~6.5", + "innmind/static-analysis": "~1.3", "innmind/coding-standard": "~2.0" } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist deleted file mode 100644 index 3968e2d..0000000 --- a/phpunit.xml.dist +++ /dev/null @@ -1,17 +0,0 @@ - - - - - . - - - ./tests - ./vendor - - - - - ./tests - - - diff --git a/src/Algebra/Absolute.php b/src/Algebra/Absolute.php index 31fba1d..0275ae5 100644 --- a/src/Algebra/Absolute.php +++ b/src/Algebra/Absolute.php @@ -5,20 +5,18 @@ /** * @psalm-immutable + * @internal */ -final class Absolute implements Operation, Number +final class Absolute implements Implementation { - private Number $number; - - private function __construct(Number $number) + private function __construct(private Implementation $number) { - $this->number = $number; } /** * @psalm-pure */ - public static function of(Number $number): self + public static function of(Implementation $number): self { return new self($number); } @@ -30,152 +28,37 @@ public function value(): int|float } #[\Override] - public function result(): Number - { - return $this->compute($this->number); - } - - #[\Override] - public function equals(Number $number): bool - { - return $this->result()->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->result()->higherThan($number); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return Addition::of($this, $number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return Subtraction::of($this, $number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return Division::of($this, $number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return Multiplication::of($this, $number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return Round::up($this, $precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return Round::down($this, $precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return Round::even($this, $precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number + public function equals(Implementation $number): bool { - return Round::odd($this, $precision); + return $this->value() == $number->value(); } #[\Override] - public function floor(): Number + public function optimize(): Implementation { - return Floor::of($this); + return new self($this->number->optimize()); } #[\Override] - public function ceil(): Number - { - return Ceil::of($this); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return Modulo::of($this, $modulus); - } - - #[\Override] - public function absolute(): self - { - return new self($this); - } - - #[\Override] - public function power(Number $power): Number - { - return Power::of($this, $power); - } - - #[\Override] - public function squareRoot(): Number - { - return SquareRoot::of($this); - } - - #[\Override] - public function exponential(): Number - { - return Exponential::of($this); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return BinaryLogarithm::of($this); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return NaturalLogarithm::of($this); - } - - #[\Override] - public function commonLogarithm(): Number - { - return CommonLogarithm::of($this); - } - - #[\Override] - public function signum(): Number + public function toString(): string { - return Signum::of($this); + return '|'.$this->number->toString().'|'; } #[\Override] - public function collapse(): Number + public function format(): string { - return $this->compute($this->number->collapse()); + return '('.$this->toString().')'; } - #[\Override] - public function toString(): string + private function result(): Implementation { - return '|'.$this->number->toString().'|'; + return $this->compute($this->number); } - private function compute(Number $number): Number + private function compute(Implementation $number): Implementation { - return Real::of( + return Native::of( \abs($number->value()), ); } diff --git a/src/Algebra/Addition.php b/src/Algebra/Addition.php index 90e0d02..755e7d0 100644 --- a/src/Algebra/Addition.php +++ b/src/Algebra/Addition.php @@ -10,204 +10,78 @@ /** * @psalm-immutable + * @internal */ -final class Addition implements Operation, Number +final class Addition implements Implementation { - /** @var Sequence */ - private Sequence $values; - private function __construct( - Number $first, - Number ...$values, + private Implementation $a, + private Implementation $b, ) { - $this->values = Sequence::of($first, ...$values); } /** * @psalm-pure */ - public static function of(Number $first, Number ...$values): self + public static function of(Implementation $a, Implementation $b): self { - return new self($first, ...$values); + return new self($a, $b); } #[\Override] public function value(): int|float { - return $this->result()->value(); - } - - #[\Override] - public function equals(Number $number): bool - { - return $this->result()->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->result()->higherThan($number); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): self - { - return new self($this, $number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return Subtraction::of($this, $number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return Division::of($this, $number); + return $this->sum()->value(); } #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number + public function equals(Implementation $number): bool { - return Multiplication::of($this, $number, ...$numbers); + return $this->value() == $number->value(); } - #[\Override] - public function roundUp(int $precision = 0): Number + public function sum(): Implementation { - return Round::up($this, $precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return Round::down($this, $precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return Round::even($this, $precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return Round::odd($this, $precision); - } - - #[\Override] - public function floor(): Number - { - return Floor::of($this); - } - - #[\Override] - public function ceil(): Number - { - return Ceil::of($this); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return Modulo::of($this, $modulus); - } - - #[\Override] - public function absolute(): Number - { - return Absolute::of($this); - } - - #[\Override] - public function power(Number $power): Number - { - return Power::of($this, $power); - } - - #[\Override] - public function squareRoot(): Number - { - return SquareRoot::of($this); - } - - #[\Override] - public function exponential(): Number - { - return Exponential::of($this); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return BinaryLogarithm::of($this); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return NaturalLogarithm::of($this); - } - - #[\Override] - public function commonLogarithm(): Number - { - return CommonLogarithm::of($this); + return Native::of( + $this->a->value() + $this->b->value(), + ); } #[\Override] - public function signum(): Number - { - return Signum::of($this); - } - - public function sum(): Number + public function optimize(): Implementation { - return $this->result(); + return new self( + $this->a->optimize(), + $this->b->optimize(), + ); } #[\Override] - public function result(): Number + public function toString(): string { - return $this->compute($this->values); - } + $values = $this->collect()->map( + static fn($number) => $number->format(), + ); - #[\Override] - public function collapse(): Number - { - return $this->compute($this->values->map( - static fn($value) => $value->collapse(), - )); + return Str::of(' + ')->join($values)->toString(); } #[\Override] - public function toString(): string + public function format(): string { - $values = $this->values->map( - static function(Number $number): string { - if ($number instanceof Operation) { - return '('.$number->toString().')'; - } - - return $number->toString(); - }, - ); - - return Str::of(' + ')->join($values)->toString(); + return '('.$this->toString().')'; } /** - * @param Sequence $values + * @return Sequence */ - private function compute(Sequence $values): Number + private function collect(): Sequence { - $value = $values->reduce( - 0, - static fn(int|float $carry, $number): int|float => $carry + $number->value(), + return Sequence::of($this->a, $this->b)->flatMap( + static fn($number) => match (true) { + $number instanceof self => $number->collect(), + default => Sequence::of($number), + }, ); - - return Real::of($value); } } diff --git a/src/Algebra/AppliedFunc.php b/src/Algebra/AppliedFunc.php new file mode 100644 index 0000000..6fcf204 --- /dev/null +++ b/src/Algebra/AppliedFunc.php @@ -0,0 +1,62 @@ +func)($this->x)->value(); + } + + #[\Override] + public function equals(Implementation $number): bool + { + return $this->value() == $number->value(); + } + + #[\Override] + public function optimize(): Implementation + { + return new self( + $this->func, + $this->x->optimize(), + ); + } + + #[\Override] + public function toString(): string + { + return \sprintf( + '%s(%s)', + $this->func->name(), + $this->x->toString(), + ); + } + + #[\Override] + public function format(): string + { + return $this->toString(); + } +} diff --git a/src/Algebra/BinaryLogarithm.php b/src/Algebra/BinaryLogarithm.php deleted file mode 100644 index d0bded4..0000000 --- a/src/Algebra/BinaryLogarithm.php +++ /dev/null @@ -1,201 +0,0 @@ -accept($number); - - $this->number = $number; - } - - /** - * @psalm-pure - */ - public static function of(Number $number): self - { - return new self($number); - } - - #[\Override] - public function value(): int|float - { - return $this->result()->value(); - } - - #[\Override] - public function equals(Number $number): bool - { - return $this->result()->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->result()->higherThan($number); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return Addition::of($this, $number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return Subtraction::of($this, $number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return Division::of($this, $number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return Multiplication::of($this, $number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return Round::up($this, $precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return Round::down($this, $precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return Round::even($this, $precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return Round::odd($this, $precision); - } - - #[\Override] - public function floor(): Number - { - return Floor::of($this); - } - - #[\Override] - public function ceil(): Number - { - return Ceil::of($this); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return Modulo::of($this, $modulus); - } - - #[\Override] - public function absolute(): Number - { - return Absolute::of($this); - } - - #[\Override] - public function power(Number $power): Number - { - return Power::of($this, $power); - } - - #[\Override] - public function squareRoot(): Number - { - return SquareRoot::of($this); - } - - #[\Override] - public function exponential(): Number - { - return Exponential::of($this); - } - - #[\Override] - public function binaryLogarithm(): self - { - return new self($this); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return NaturalLogarithm::of($this); - } - - #[\Override] - public function commonLogarithm(): Number - { - return CommonLogarithm::of($this); - } - - #[\Override] - public function signum(): Number - { - return Signum::of($this); - } - - #[\Override] - public function result(): Number - { - return $this->compute($this->number); - } - - /** - * @psalm-pure - */ - public static function definitionSet(): Set - { - return Range::exclusive( - Value::zero, - Value::infinite, - ); - } - - #[\Override] - public function collapse(): Number - { - return $this->compute($this->number->collapse()); - } - - #[\Override] - public function toString(): string - { - return \sprintf('lb(%s)', $this->number->toString()); - } - - private function compute(Number $number): Number - { - return Real::of( - \log($number->value(), 2), - ); - } -} diff --git a/src/Algebra/Ceil.php b/src/Algebra/Ceil.php index 9af4269..e619f86 100644 --- a/src/Algebra/Ceil.php +++ b/src/Algebra/Ceil.php @@ -5,20 +5,18 @@ /** * @psalm-immutable + * @internal */ -final class Ceil implements Number +final class Ceil implements Implementation { - private Number $number; - - private function __construct(Number $number) + private function __construct(private Implementation $number) { - $this->number = $number; } /** * @psalm-pure */ - public static function of(Number $number): self + public static function of(Implementation $number): self { return new self($number); } @@ -30,144 +28,30 @@ public function value(): int|float } #[\Override] - public function equals(Number $number): bool + public function equals(Implementation $number): bool { return $this->value() == $number->value(); } #[\Override] - public function higherThan(Number $number): bool - { - return $this->value() > $number->value(); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return Addition::of($this, $number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return Subtraction::of($this, $number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return Division::of($this, $number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return Multiplication::of($this, $number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return Round::up($this, $precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return Round::down($this, $precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return Round::even($this, $precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return Round::odd($this, $precision); - } - - #[\Override] - public function floor(): Number - { - return Floor::of($this); - } - - #[\Override] - public function ceil(): self + public function optimize(): Implementation { - return new self($this); + return new self($this->number->optimize()); } #[\Override] - public function modulo(Number $modulus): Number - { - return Modulo::of($this, $modulus); - } - - #[\Override] - public function absolute(): Number - { - return Absolute::of($this); - } - - #[\Override] - public function power(Number $power): Number - { - return Power::of($this, $power); - } - - #[\Override] - public function squareRoot(): Number - { - return SquareRoot::of($this); - } - - #[\Override] - public function exponential(): Number - { - return Exponential::of($this); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return BinaryLogarithm::of($this); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return NaturalLogarithm::of($this); - } - - #[\Override] - public function commonLogarithm(): Number - { - return CommonLogarithm::of($this); - } - - #[\Override] - public function signum(): Number - { - return Signum::of($this); - } - - #[\Override] - public function collapse(): Number + public function toString(): string { - return Real::of($this->compute($this->number->collapse())); + return \var_export($this->value(), true); } #[\Override] - public function toString(): string + public function format(): string { - return \var_export($this->value(), true); + return $this->toString(); } - private function compute(Number $number): int|float + private function compute(Implementation $number): int|float { return \ceil($number->value()); } diff --git a/src/Algebra/CommonLogarithm.php b/src/Algebra/CommonLogarithm.php deleted file mode 100644 index ceecb2e..0000000 --- a/src/Algebra/CommonLogarithm.php +++ /dev/null @@ -1,201 +0,0 @@ -accept($number); - - $this->number = $number; - } - - /** - * @psalm-pure - */ - public static function of(Number $number): self - { - return new self($number); - } - - #[\Override] - public function value(): int|float - { - return $this->result()->value(); - } - - #[\Override] - public function equals(Number $number): bool - { - return $this->result()->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->result()->higherThan($number); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return Addition::of($this, $number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return Subtraction::of($this, $number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return Division::of($this, $number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return Multiplication::of($this, $number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return Round::up($this, $precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return Round::down($this, $precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return Round::even($this, $precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return Round::odd($this, $precision); - } - - #[\Override] - public function floor(): Number - { - return Floor::of($this); - } - - #[\Override] - public function ceil(): Number - { - return Ceil::of($this); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return Modulo::of($this, $modulus); - } - - #[\Override] - public function absolute(): Number - { - return Absolute::of($this); - } - - #[\Override] - public function power(Number $power): Number - { - return Power::of($this, $power); - } - - #[\Override] - public function squareRoot(): Number - { - return SquareRoot::of($this); - } - - #[\Override] - public function exponential(): Number - { - return Exponential::of($this); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return BinaryLogarithm::of($this); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return NaturalLogarithm::of($this); - } - - #[\Override] - public function commonLogarithm(): self - { - return new self($this); - } - - #[\Override] - public function signum(): Number - { - return Signum::of($this); - } - - #[\Override] - public function result(): Number - { - return $this->compute($this->number); - } - - /** - * @psalm-pure - */ - public static function definitionSet(): Set - { - return Range::exclusive( - Value::zero, - Value::infinite, - ); - } - - #[\Override] - public function collapse(): Number - { - return $this->compute($this->number->collapse()); - } - - #[\Override] - public function toString(): string - { - return \sprintf('lg(%s)', $this->number->toString()); - } - - private function compute(Number $number): Number - { - return Real::of( - \log10($number->value()), - ); - } -} diff --git a/src/Algebra/ComplexNumber.php b/src/Algebra/ComplexNumber.php index 336a227..411fc2a 100644 --- a/src/Algebra/ComplexNumber.php +++ b/src/Algebra/ComplexNumber.php @@ -8,13 +8,10 @@ */ final class ComplexNumber { - private Number $real; - private Number $imaginary; - - private function __construct(Number $real, Number $imaginary) - { - $this->real = $real; - $this->imaginary = $imaginary; + private function __construct( + private Number $real, + private Number $imaginary, + ) { } /** @@ -60,7 +57,7 @@ public function multiplyBy(self $number): self $this ->imaginary ->multiplyBy($number->imaginary()) - ->multiplyBy(Integer::of(-1)), // because i^2 == -1 + ->multiplyBy(Number::of(-1)), // because i^2 == -1 ); $imaginary = $this ->real @@ -75,10 +72,10 @@ public function divideBy(self $number): self $dividend = $this->multiplyBy($number->conjugate()); $divisor = $number ->real() - ->power(Value::two) + ->power(Number::two()) ->subtract( - Integer::of(-1)->multiplyBy( - $number->imaginary()->power(Value::two), + Number::of(-1)->multiplyBy( + $number->imaginary()->power(Number::two()), ), ); $real = $dividend->real()->divideBy($divisor); @@ -91,7 +88,7 @@ public function conjugate(): self { return new self( $this->real, - Integer::of(-1)->multiplyBy($this->imaginary()), + Number::of(-1)->multiplyBy($this->imaginary()), ); } @@ -99,8 +96,8 @@ public function absolute(): Number { return $this ->real - ->power(Value::two) - ->add($this->imaginary->power(Value::two)) + ->power(Number::two()) + ->add($this->imaginary->power(Number::two())) ->squareRoot(); } @@ -108,12 +105,12 @@ public function reciprocal(): self { $divisor = $this ->real - ->power(Value::two) - ->add($this->imaginary->power(Value::two)); + ->power(Number::two()) + ->add($this->imaginary->power(Number::two())); return new self( $this->real->divideBy($divisor), - Integer::of(-1)->multiplyBy( + Number::of(-1)->multiplyBy( $this->imaginary->divideBy($divisor), ), ); @@ -122,8 +119,8 @@ public function reciprocal(): self public function negation(): self { return new self( - Integer::of(-1)->multiplyBy($this->real), - Integer::of(-1)->multiplyBy($this->imaginary), + Number::of(-1)->multiplyBy($this->real), + Number::of(-1)->multiplyBy($this->imaginary), ); } @@ -132,13 +129,13 @@ public function squareRoot(): self $real = $this ->real ->add($this->absolute()) - ->divideBy(Value::two) + ->divideBy(Number::two()) ->squareRoot(); $imaginary = $this->imaginary->signum()->multiplyBy( - Integer::of(-1) + Number::of(-1) ->multiplyBy($this->real) ->add($this->absolute()) - ->divideBy(Value::two) + ->divideBy(Number::two()) ->squareRoot(), ); @@ -153,10 +150,8 @@ public function equals(self $number): bool public function toString(): string { - $real = $this->real instanceof Operation ? - '('.$this->real->toString().')' : $this->real->toString(); - $imaginary = $this->imaginary instanceof Operation ? - '('.$this->imaginary->toString().')' : $this->imaginary->toString(); + $real = $this->real->format(); + $imaginary = $this->imaginary->format(); return \sprintf('(%s + %si)', $real, $imaginary); } diff --git a/src/Algebra/DisplayAs.php b/src/Algebra/DisplayAs.php new file mode 100644 index 0000000..e82cc01 --- /dev/null +++ b/src/Algebra/DisplayAs.php @@ -0,0 +1,58 @@ +number->value(); + } + + #[\Override] + public function equals(Implementation $number): bool + { + return $this->number->equals($number); + } + + #[\Override] + public function optimize(): Implementation + { + return new self( + $this->number->optimize(), + $this->string, + ); + } + + #[\Override] + public function toString(): string + { + return $this->string; + } + + #[\Override] + public function format(): string + { + return '('.$this->toString().')'; + } +} diff --git a/src/Algebra/Division.php b/src/Algebra/Division.php index 1e0bee6..f4b19fb 100644 --- a/src/Algebra/Division.php +++ b/src/Algebra/Division.php @@ -7,15 +7,16 @@ /** * @psalm-immutable + * @internal */ -final class Division implements Operation, Number +final class Division implements Implementation { - private Number $dividend; - private Number $divisor; + private Implementation $dividend; + private Implementation $divisor; - private function __construct(Number $dividend, Number $divisor) + private function __construct(Implementation $dividend, Implementation $divisor) { - if ($divisor->collapse()->equals(Value::zero)) { + if ($divisor->optimize()->value() == 0) { throw new DivisionByZero; } @@ -26,17 +27,17 @@ private function __construct(Number $dividend, Number $divisor) /** * @psalm-pure */ - public static function of(Number $dividend, Number $divisor): self + public static function of(Implementation $dividend, Implementation $divisor): self { return new self($dividend, $divisor); } - public function dividend(): Number + public function dividend(): Implementation { return $this->dividend; } - public function divisor(): Number + public function divisor(): Implementation { return $this->divisor; } @@ -44,142 +45,16 @@ public function divisor(): Number #[\Override] public function value(): int|float { - return $this->result()->value(); + return $this->quotient()->value(); } #[\Override] - public function equals(Number $number): bool + public function equals(Implementation $number): bool { - return $this->result()->equals($number); + return $this->value() == $number->value(); } - #[\Override] - public function higherThan(Number $number): bool - { - return $this->result()->higherThan($number); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return Addition::of($this, $number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return Subtraction::of($this, $number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): self - { - return new self($this, $number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return Multiplication::of($this, $number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return Round::up($this, $precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return Round::down($this, $precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return Round::even($this, $precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return Round::odd($this, $precision); - } - - #[\Override] - public function floor(): Number - { - return Floor::of($this); - } - - #[\Override] - public function ceil(): Number - { - return Ceil::of($this); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return Modulo::of($this, $modulus); - } - - #[\Override] - public function absolute(): Number - { - return Absolute::of($this); - } - - #[\Override] - public function power(Number $power): Number - { - return Power::of($this, $power); - } - - #[\Override] - public function squareRoot(): Number - { - return SquareRoot::of($this); - } - - #[\Override] - public function exponential(): Number - { - return Exponential::of($this); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return BinaryLogarithm::of($this); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return NaturalLogarithm::of($this); - } - - #[\Override] - public function commonLogarithm(): Number - { - return CommonLogarithm::of($this); - } - - #[\Override] - public function signum(): Number - { - return Signum::of($this); - } - - public function quotient(): Number - { - return $this->result(); - } - - #[\Override] - public function result(): Number + public function quotient(): Implementation { return $this->compute( $this->dividend, @@ -188,21 +63,19 @@ public function result(): Number } #[\Override] - public function collapse(): Number + public function optimize(): Implementation { - return $this->compute( - $this->dividend->collapse(), - $this->divisor->collapse(), + return new self( + $this->dividend->optimize(), + $this->divisor->optimize(), ); } #[\Override] public function toString(): string { - $dividend = $this->dividend instanceof Operation ? - '('.$this->dividend->toString().')' : $this->dividend->toString(); - $divisor = $this->divisor instanceof Operation ? - '('.$this->divisor->toString().')' : $this->divisor->toString(); + $dividend = $this->dividend->format(); + $divisor = $this->divisor->format(); return \sprintf( '%s ÷ %s', @@ -211,9 +84,17 @@ public function toString(): string ); } - private function compute(Number $dividend, Number $divisor): Number + #[\Override] + public function format(): string { - return Real::of( + return '('.$this->toString().')'; + } + + private function compute( + Implementation $dividend, + Implementation $divisor, + ): Implementation { + return Native::of( $dividend->value() / $divisor->value(), ); } diff --git a/src/Algebra/Exponential.php b/src/Algebra/Exponential.php index 315f295..6939d8c 100644 --- a/src/Algebra/Exponential.php +++ b/src/Algebra/Exponential.php @@ -5,30 +5,22 @@ /** * @psalm-immutable + * @internal */ -final class Exponential implements Operation, Number +final class Exponential implements Implementation { - private Number $power; - - private function __construct(Number $power) + private function __construct(private Implementation $power) { - $this->power = $power; } /** * @psalm-pure */ - public static function of(Number $power): self + public static function of(Implementation $power): self { return new self($power); } - #[\Override] - public function result(): Number - { - return $this->compute($this->power); - } - #[\Override] public function value(): int|float { @@ -36,149 +28,39 @@ public function value(): int|float } #[\Override] - public function equals(Number $number): bool - { - return $this->result()->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->result()->higherThan($number); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return Addition::of($this, $number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return Subtraction::of($this, $number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return Division::of($this, $number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return Multiplication::of($this, $number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return Round::up($this, $precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return Round::down($this, $precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return Round::even($this, $precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number + public function equals(Implementation $number): bool { - return Round::odd($this, $precision); + return $this->value() == $number->value(); } #[\Override] - public function floor(): Number + public function optimize(): Implementation { - return Floor::of($this); + return new self($this->power->optimize()); } #[\Override] - public function ceil(): Number - { - return Ceil::of($this); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return Modulo::of($this, $modulus); - } - - #[\Override] - public function absolute(): Number - { - return Absolute::of($this); - } - - #[\Override] - public function power(Number $power): Number - { - return Power::of($this, $power); - } - - #[\Override] - public function squareRoot(): Number - { - return SquareRoot::of($this); - } - - #[\Override] - public function exponential(): self - { - return new self($this); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return BinaryLogarithm::of($this); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return NaturalLogarithm::of($this); - } - - #[\Override] - public function commonLogarithm(): Number + public function toString(): string { - return CommonLogarithm::of($this); - } + $power = $this->power->format(); - #[\Override] - public function signum(): Number - { - return Signum::of($this); + return 'e^'.$power; } #[\Override] - public function collapse(): Number + public function format(): string { - return $this->compute($this->power->collapse()); + return '('.$this->toString().')'; } - #[\Override] - public function toString(): string + private function result(): Implementation { - $power = $this->power instanceof Operation ? - '('.$this->power->toString().')' : $this->power->toString(); - - return 'e^'.$power; + return $this->compute($this->power); } - private function compute(Number $power): Number + private function compute(Implementation $power): Implementation { - return Real::of( + return Native::of( \exp($power->value()), ); } diff --git a/src/Algebra/Factorial.php b/src/Algebra/Factorial.php index 3e63859..06ce703 100644 --- a/src/Algebra/Factorial.php +++ b/src/Algebra/Factorial.php @@ -7,8 +7,9 @@ /** * @psalm-immutable + * @internal */ -final class Factorial implements Operation, Number +final class Factorial { private int $value; @@ -29,161 +30,24 @@ public static function of(int $value): self return new self($value); } - #[\Override] - public function value(): int|float - { - return $this->result()->value(); - } - - #[\Override] - public function equals(Number $number): bool - { - return $this->result()->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->result()->higherThan($number); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return $this->result()->add($number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return $this->result()->subtract($number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return $this->result()->divideBy($number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return $this->result()->multiplyBy($number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return Round::up($this, $precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return Round::down($this, $precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return Round::even($this, $precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return Round::odd($this, $precision); - } - - #[\Override] - public function floor(): Number - { - return $this->result()->floor(); - } - - #[\Override] - public function ceil(): Number - { - return $this->result()->ceil(); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return $this->result()->modulo($modulus); - } - - #[\Override] - public function absolute(): Number - { - return $this->result()->absolute(); - } - - #[\Override] - public function power(Number $power): Number - { - return $this->result()->power($power); - } - - #[\Override] - public function squareRoot(): Number - { - return $this->result()->squareRoot(); - } - - #[\Override] - public function exponential(): Number - { - return Exponential::of($this); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return BinaryLogarithm::of($this); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return NaturalLogarithm::of($this); - } - - #[\Override] - public function commonLogarithm(): Number - { - return CommonLogarithm::of($this); - } - - #[\Override] - public function signum(): Number - { - return Signum::of($this); - } - - #[\Override] - public function result(): Number + public function number(): Number { if ($this->value < 2) { - return Value::one; + return Number::one(); } - $factorial = $i = $this->value; + $i = $this->value; + $factorial = Number::of($i); do { - $factorial *= --$i; + $factorial = $factorial + ->multiplyBy(Number::of(--$i)) + ->memoize(); } while ($i > 1); - return Real::of($factorial); - } - - #[\Override] - public function collapse(): Number - { - return $this->result(); + return $factorial; } - #[\Override] public function toString(): string { return $this->value.'!'; diff --git a/src/Algebra/Floor.php b/src/Algebra/Floor.php index 5f94958..73234c7 100644 --- a/src/Algebra/Floor.php +++ b/src/Algebra/Floor.php @@ -5,20 +5,18 @@ /** * @psalm-immutable + * @internal */ -final class Floor implements Number +final class Floor implements Implementation { - private Number $number; - - private function __construct(Number $number) + private function __construct(private Implementation $number) { - $this->number = $number; } /** * @psalm-pure */ - public static function of(Number $number): self + public static function of(Implementation $number): self { return new self($number); } @@ -30,144 +28,30 @@ public function value(): int|float } #[\Override] - public function equals(Number $number): bool + public function equals(Implementation $number): bool { return $this->value() == $number->value(); } #[\Override] - public function higherThan(Number $number): bool - { - return $this->value() > $number->value(); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return Addition::of($this, $number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return Subtraction::of($this, $number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return Division::of($this, $number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return Multiplication::of($this, $number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return Round::up($this, $precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return Round::down($this, $precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return Round::even($this, $precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return Round::odd($this, $precision); - } - - #[\Override] - public function floor(): self - { - return new self($this); - } - - #[\Override] - public function ceil(): Number + public function optimize(): Implementation { - return Ceil::of($this); + return new self($this->number->optimize()); } #[\Override] - public function modulo(Number $modulus): Number - { - return Modulo::of($this, $modulus); - } - - #[\Override] - public function absolute(): Number - { - return Absolute::of($this); - } - - #[\Override] - public function power(Number $power): Number - { - return Power::of($this, $power); - } - - #[\Override] - public function squareRoot(): Number - { - return SquareRoot::of($this); - } - - #[\Override] - public function exponential(): Number - { - return Exponential::of($this); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return BinaryLogarithm::of($this); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return NaturalLogarithm::of($this); - } - - #[\Override] - public function commonLogarithm(): Number - { - return CommonLogarithm::of($this); - } - - #[\Override] - public function signum(): Number - { - return Signum::of($this); - } - - #[\Override] - public function collapse(): Number + public function toString(): string { - return Real::of($this->compute($this->number->collapse())); + return \var_export($this->value(), true); } #[\Override] - public function toString(): string + public function format(): string { - return \var_export($this->value(), true); + return $this->toString(); } - private function compute(Number $number): int|float + private function compute(Implementation $number): int|float { return \floor($number->value()); } diff --git a/src/Algebra/Func.php b/src/Algebra/Func.php new file mode 100644 index 0000000..ff48411 --- /dev/null +++ b/src/Algebra/Func.php @@ -0,0 +1,17 @@ +value = $value; - } - - /** - * @psalm-pure - */ - public static function of(int $value): self - { - if ($value > 0) { - return self::positive($value); - } - - if ($value < 0) { - return new Integer\Negative($value); - } - - return new self($value); - } - - /** - * @psalm-pure - * - * @param positive-int $value - */ - public static function positive(int $value): Integer\Positive - { - return new Integer\Positive($value); - } - - #[\Override] - public function value(): int - { - return $this->value; - } - - #[\Override] - public function equals(Number $number): bool - { - return $this->value() == $number->collapse()->value(); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->value() > $number->collapse()->value(); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return Addition::of($this, $number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return Subtraction::of($this, $number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return Division::of($this, $number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return Multiplication::of($this, $number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return Round::up($this, $precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return Round::down($this, $precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return Round::even($this, $precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return Round::odd($this, $precision); - } - - #[\Override] - public function floor(): Number - { - return Floor::of($this); - } - - #[\Override] - public function ceil(): Number - { - return Ceil::of($this); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return Modulo::of($this, $modulus); - } - - #[\Override] - public function absolute(): Number - { - return Absolute::of($this); - } - - #[\Override] - public function power(Number $power): Number - { - return Power::of($this, $power); - } - - #[\Override] - public function squareRoot(): Number - { - return SquareRoot::of($this); - } - - public function factorial(): Factorial - { - return Factorial::of($this->value); - } - - #[\Override] - public function exponential(): Number - { - return Exponential::of($this); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return BinaryLogarithm::of($this); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return NaturalLogarithm::of($this); - } - - #[\Override] - public function commonLogarithm(): Number - { - return CommonLogarithm::of($this); - } - - #[\Override] - public function signum(): Number - { - return Signum::of($this); - } - - public function increment(): self - { - return self::of($this->value + 1); - } - - public function decrement(): self - { - return self::of($this->value - 1); - } - - #[\Override] - public function collapse(): Number - { - return $this; - } - - #[\Override] - public function toString(): string - { - return \var_export($this->value, true); - } -} diff --git a/src/Algebra/Integer/Negative.php b/src/Algebra/Integer/Negative.php deleted file mode 100644 index 3f03cf7..0000000 --- a/src/Algebra/Integer/Negative.php +++ /dev/null @@ -1,19 +0,0 @@ -value() - 1); - } -} diff --git a/src/Algebra/Integer/Positive.php b/src/Algebra/Integer/Positive.php deleted file mode 100644 index 3718122..0000000 --- a/src/Algebra/Integer/Positive.php +++ /dev/null @@ -1,28 +0,0 @@ -value() + 1); - } -} diff --git a/src/Algebra/Logarithm.php b/src/Algebra/Logarithm.php new file mode 100644 index 0000000..31f96cb --- /dev/null +++ b/src/Algebra/Logarithm.php @@ -0,0 +1,46 @@ +accept($x) + ->unwrap(); + + return Number::of(match ($this) { + self::binary, self::base2 => \log($x->value(), 2), + self::common, self::base10 => \log10($x->value()), + self::natural, self::baseE => \log($x->value()), + }); + } + + #[\Override] + public function name(): string + { + return match ($this) { + self::binary, self::base2 => 'lb', + self::common, self::base10 => 'lg', + self::natural, self::baseE => 'ln', + }; + } +} diff --git a/src/Algebra/Modulo.php b/src/Algebra/Modulo.php index dbd497b..e1909a6 100644 --- a/src/Algebra/Modulo.php +++ b/src/Algebra/Modulo.php @@ -5,32 +5,24 @@ /** * @psalm-immutable + * @internal */ -final class Modulo implements Operation, Number +final class Modulo implements Implementation { - private Number $number; - private Number $modulus; - - private function __construct(Number $number, Number $modulus) - { - $this->number = $number; - $this->modulus = $modulus; + private function __construct( + private Implementation $number, + private Implementation $modulus, + ) { } /** * @psalm-pure */ - public static function of(Number $number, Number $modulus): self + public static function of(Implementation $number, Implementation $modulus): self { return new self($number, $modulus); } - #[\Override] - public function result(): Number - { - return $this->compute($this->number, $this->modulus); - } - #[\Override] public function value(): int|float { @@ -38,154 +30,45 @@ public function value(): int|float } #[\Override] - public function equals(Number $number): bool - { - return $this->result()->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->result()->higherThan($number); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return Addition::of($this, $number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return Subtraction::of($this, $number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return Division::of($this, $number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return Multiplication::of($this, $number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return Round::up($this, $precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return Round::down($this, $precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return Round::even($this, $precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return Round::odd($this, $precision); - } - - #[\Override] - public function floor(): Number - { - return Floor::of($this); - } - - #[\Override] - public function ceil(): Number - { - return Ceil::of($this); - } - - #[\Override] - public function modulo(Number $modulus): self - { - return new self($this, $modulus); - } - - #[\Override] - public function absolute(): Number - { - return Absolute::of($this); - } - - #[\Override] - public function power(Number $power): Number - { - return Power::of($this, $power); - } - - #[\Override] - public function squareRoot(): Number - { - return SquareRoot::of($this); - } - - #[\Override] - public function exponential(): Number + public function equals(Implementation $number): bool { - return Exponential::of($this); + return $this->value() == $number->value(); } #[\Override] - public function binaryLogarithm(): Number + public function optimize(): Implementation { - return BinaryLogarithm::of($this); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return NaturalLogarithm::of($this); + return new self( + $this->number->optimize(), + $this->modulus->optimize(), + ); } #[\Override] - public function commonLogarithm(): Number + public function toString(): string { - return CommonLogarithm::of($this); - } + $number = $this->number->format(); + $modulus = $this->modulus->format(); - #[\Override] - public function signum(): Number - { - return Signum::of($this); + return $number.' % '.$modulus; } #[\Override] - public function collapse(): Number + public function format(): string { - return $this->compute( - $this->number->collapse(), - $this->modulus->collapse(), - ); + return '('.$this->toString().')'; } - #[\Override] - public function toString(): string + private function result(): Implementation { - $number = $this->number instanceof Operation ? - '('.$this->number->toString().')' : $this->number->toString(); - $modulus = $this->modulus instanceof Operation ? - '('.$this->modulus->toString().')' : $this->modulus->toString(); - - return $number.' % '.$modulus; + return $this->compute($this->number, $this->modulus); } - private function compute(Number $number, Number $modulus): Number - { - return Real::of( + private function compute( + Implementation $number, + Implementation $modulus, + ): Implementation { + return Native::of( \fmod($number->value(), $modulus->value()), ); } diff --git a/src/Algebra/Multiplication.php b/src/Algebra/Multiplication.php index 63d2a96..c2eb0d3 100644 --- a/src/Algebra/Multiplication.php +++ b/src/Algebra/Multiplication.php @@ -10,230 +10,92 @@ /** * @psalm-immutable + * @internal */ -final class Multiplication implements Operation, Number +final class Multiplication implements Implementation { - private Number $first; - private Number $second; - /** @var Sequence */ - private Sequence $values; - private function __construct( - Number $first, - Number $second, - Number ...$values, + private Implementation $a, + private Implementation $b, ) { - $this->first = $first; - $this->second = $second; - $this->values = Sequence::of($first, $second, ...$values); } /** * @psalm-pure */ - public static function of( - Number $first, - Number $second, - Number ...$values, - ): self { - return new self($first, $second, ...$values); - } - - #[\Override] - public function value(): int|float - { - return $this->result()->value(); - } - - #[\Override] - public function equals(Number $number): bool - { - return $this->result()->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->result()->higherThan($number); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return Addition::of($this, $number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number + public static function of(Implementation $a, Implementation $b): self { - return Subtraction::of($this, $number, ...$numbers); + return new self($a, $b); } #[\Override] - public function divideBy(Number $number): Number - { - return Division::of($this, $number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): self - { - return new self($this, $number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return Round::up($this, $precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return Round::down($this, $precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return Round::even($this, $precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return Round::odd($this, $precision); - } - - #[\Override] - public function floor(): Number - { - return Floor::of($this); - } - - #[\Override] - public function ceil(): Number - { - return Ceil::of($this); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return Modulo::of($this, $modulus); - } - - #[\Override] - public function absolute(): Number + public function value(): int|float { - return Absolute::of($this); + return $this->product()->value(); } #[\Override] - public function power(Number $power): Number + public function equals(Implementation $number): bool { - return Power::of($this, $power); + return $this->value() == $number->value(); } - #[\Override] - public function squareRoot(): Number + public function product(): Implementation { - return SquareRoot::of($this); + return Native::of($this->a->value() * $this->b->value()); } #[\Override] - public function exponential(): Number + public function optimize(): Implementation { - return Exponential::of($this); - } + $a = $this->a->optimize(); + $b = $this->b->optimize(); - #[\Override] - public function binaryLogarithm(): Number - { - return BinaryLogarithm::of($this); - } + if ($a instanceof Division) { + $divisor = $a->divisor(); - #[\Override] - public function naturalLogarithm(): Number - { - return NaturalLogarithm::of($this); - } + if ($b->equals($divisor)) { + return $a->dividend(); + } + } - #[\Override] - public function commonLogarithm(): Number - { - return CommonLogarithm::of($this); - } + if ($b instanceof Division) { + $divisor = $b->divisor(); - #[\Override] - public function signum(): Number - { - return Signum::of($this); - } + if ($a->equals($divisor)) { + return $b->dividend(); + } + } - public function product(): Number - { - return $this->result(); + return $this; } #[\Override] - public function result(): Number + public function toString(): string { - $value = $this->values->reduce( - 1, - static fn(int|float $carry, $number): int|float => $carry * $number->value(), + $values = $this->collect()->map( + static fn($number) => $number->format(), ); - return Real::of($value); + return Str::of(' x ')->join($values)->toString(); } #[\Override] - public function collapse(): Number + public function format(): string { - return $this - ->values - ->drop(2) - ->reduce( - $this->doCollapse($this->first, $this->second), - $this->doCollapse(...), - ) - ->collapse(); + return '('.$this->toString().')'; } - #[\Override] - public function toString(): string + /** + * @return Sequence + */ + private function collect(): Sequence { - $values = $this->values->map( - static function(Number $number) { - if ($number instanceof Operation) { - return '('.$number->toString().')'; - } - - return $number->toString(); + return Sequence::of($this->a, $this->b)->flatMap( + static fn($number) => match (true) { + $number instanceof self => $number->collect(), + default => Sequence::of($number), }, ); - - return Str::of(' x ')->join($values)->toString(); - } - - private function doCollapse(Number $a, Number $b): Number - { - if ($a instanceof Division) { - $divisor = $a->divisor()->collapse(); - - if ($b->equals($divisor)) { - return $a->dividend(); - } - } - - if ($b instanceof Division) { - $divisor = $b->divisor()->collapse(); - - if ($a->equals($divisor)) { - return $b->dividend(); - } - } - - return (new self($a->collapse(), $b->collapse()))->result(); } } diff --git a/src/Algebra/Native.php b/src/Algebra/Native.php new file mode 100644 index 0000000..b6a2ddd --- /dev/null +++ b/src/Algebra/Native.php @@ -0,0 +1,72 @@ + 0 ? Value::infinite : Value::negativeInfinite; + } + + if (\is_nan($value)) { + throw new NotANumber; + } + + return match ($value) { + 0 => Value::zero, + 1 => Value::one, + 2 => Value::two, + 10 => Value::ten, + 100 => Value::hundred, + \M_E => Value::e, + \M_PI => Value::pi, + default => new self($value), + }; + } + + #[\Override] + public function value(): int|float + { + return $this->value; + } + + #[\Override] + public function equals(Implementation $number): bool + { + return $this->value == $number->value(); + } + + #[\Override] + public function optimize(): Implementation + { + return $this; + } + + #[\Override] + public function toString(): string + { + return \var_export($this->value, true); + } + + #[\Override] + public function format(): string + { + return $this->toString(); + } +} diff --git a/src/Algebra/NaturalLogarithm.php b/src/Algebra/NaturalLogarithm.php deleted file mode 100644 index 8141f3a..0000000 --- a/src/Algebra/NaturalLogarithm.php +++ /dev/null @@ -1,201 +0,0 @@ -accept($number); - - $this->number = $number; - } - - /** - * @psalm-pure - */ - public static function of(Number $number): self - { - return new self($number); - } - - #[\Override] - public function value(): int|float - { - return $this->result()->value(); - } - - #[\Override] - public function equals(Number $number): bool - { - return $this->result()->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->result()->higherThan($number); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return Addition::of($this, $number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return Subtraction::of($this, $number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return Division::of($this, $number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return Multiplication::of($this, $number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return Round::up($this, $precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return Round::down($this, $precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return Round::even($this, $precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return Round::odd($this, $precision); - } - - #[\Override] - public function floor(): Number - { - return Floor::of($this); - } - - #[\Override] - public function ceil(): Number - { - return Ceil::of($this); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return Modulo::of($this, $modulus); - } - - #[\Override] - public function absolute(): Number - { - return Absolute::of($this); - } - - #[\Override] - public function power(Number $power): Number - { - return Power::of($this, $power); - } - - #[\Override] - public function squareRoot(): Number - { - return SquareRoot::of($this); - } - - #[\Override] - public function exponential(): Number - { - return Exponential::of($this); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return BinaryLogarithm::of($this); - } - - #[\Override] - public function naturalLogarithm(): self - { - return new self($this); - } - - #[\Override] - public function commonLogarithm(): Number - { - return CommonLogarithm::of($this); - } - - #[\Override] - public function signum(): Number - { - return Signum::of($this); - } - - #[\Override] - public function result(): Number - { - return $this->compute($this->number); - } - - /** - * @psalm-pure - */ - public static function definitionSet(): Set - { - return Range::exclusive( - Value::zero, - Value::infinite, - ); - } - - #[\Override] - public function collapse(): Number - { - return $this->compute($this->number->collapse()); - } - - #[\Override] - public function toString(): string - { - return \sprintf('ln(%s)', $this->number->toString()); - } - - private function compute(Number $number): Number - { - return Real::of( - \log($number->value()), - ); - } -} diff --git a/src/Algebra/Number.php b/src/Algebra/Number.php index 2665bb8..5fc251f 100644 --- a/src/Algebra/Number.php +++ b/src/Algebra/Number.php @@ -6,38 +6,286 @@ /** * @psalm-immutable */ -interface Number +final class Number { - public function value(): int|float; - public function equals(self $number): bool; - public function higherThan(self $number): bool; - public function add(self $number, self ...$numbers): self; - public function subtract(self $number, self ...$numbers): self; - public function divideBy(self $number): self; - public function multiplyBy(self $number, self ...$numbers): self; - public function roundUp(int $precision = 0): self; - public function roundDown(int $precision = 0): self; - public function roundEven(int $precision = 0): self; - public function roundOdd(int $precision = 0): self; - public function floor(): self; - public function ceil(): self; - public function modulo(self $modulus): self; - public function absolute(): self; - public function power(self $power): self; - public function squareRoot(): self; - public function exponential(): self; - public function binaryLogarithm(): self; - public function naturalLogarithm(): self; - public function commonLogarithm(): self; - public function signum(): self; - public function toString(): string; - - /** - * Compute the underlying number like the value() method but it will try to - * skip some operations to provide the most accurate number + private function __construct( + private Implementation $implementation, + ) { + } + + /** + * @psalm-pure + */ + public static function of(int|float $value): self + { + return new self(Native::of($value)); + } + + /** + * @psalm-pure + */ + public static function zero(): self + { + return new self(Value::zero); + } + + /** + * @psalm-pure + */ + public static function one(): self + { + return new self(Value::one); + } + + /** + * @psalm-pure + */ + public static function two(): self + { + return new self(Value::two); + } + + /** + * @psalm-pure + */ + public static function ten(): self + { + return new self(Value::ten); + } + + /** + * @psalm-pure + */ + public static function hundred(): self + { + return new self(Value::hundred); + } + + /** + * @psalm-pure + */ + public static function e(): self + { + return new self(Value::e); + } + + /** + * @psalm-pure + */ + public static function pi(): self + { + return new self(Value::pi); + } + + /** + * @psalm-pure + */ + public static function infinite(): self + { + return new self(Value::infinite); + } + + /** + * @psalm-pure + */ + public static function negativeInfinite(): self + { + return new self(Value::negativeInfinite); + } + + public function apply(Func $func): self + { + return new self(AppliedFunc::of($func, $this)); + } + + public function value(): int|float + { + return $this->implementation->value(); + } + + public function equals(self $number): bool + { + return $this->implementation->equals( + $number->implementation, + ); + } + + public function higherThan(self $number): bool + { + return $this->value() > $number->value(); + } + + public function add(self $number): self + { + return new self(Addition::of( + $this->implementation, + $number->implementation, + )); + } + + public function subtract(self $number): self + { + return new self(Subtraction::of( + $this->implementation, + $number->implementation, + )); + } + + public function divideBy(self $number): self + { + return new self(Division::of( + $this->implementation, + $number->implementation, + )); + } + + public function multiplyBy(self $number): self + { + return new self(Multiplication::of( + $this->implementation, + $number->implementation, + )); + } + + /** + * @param int<0, max> $precision + */ + public function roundUp(int $precision = 0): self + { + return new self(Round::up( + $this->implementation, + $precision, + )); + } + + /** + * @param int<0, max> $precision + */ + public function roundDown(int $precision = 0): self + { + return new self(Round::down( + $this->implementation, + $precision, + )); + } + + /** + * @param int<0, max> $precision + */ + public function roundEven(int $precision = 0): self + { + return new self(Round::even( + $this->implementation, + $precision, + )); + } + + /** + * @param int<0, max> $precision + */ + public function roundOdd(int $precision = 0): self + { + return new self(Round::odd( + $this->implementation, + $precision, + )); + } + + public function floor(): self + { + return new self(Floor::of($this->implementation)); + } + + public function ceil(): self + { + return new self(Ceil::of($this->implementation)); + } + + public function modulo(self $modulus): self + { + return new self(Modulo::of( + $this->implementation, + $modulus->implementation, + )); + } + + public function absolute(): self + { + return new self(Absolute::of($this->implementation)); + } + + public function power(self $power): self + { + return new self(Power::of( + $this->implementation, + $power->implementation, + )); + } + + public function squareRoot(): self + { + return new self(SquareRoot::of( + $this->implementation, + )); + } + + public function exponential(): self + { + return new self(Exponential::of( + $this->implementation, + )); + } + + public function signum(): self + { + return new self(Signum::of($this->implementation)); + } + + /** + * This allows to change the way the number is represented as a string. + * + * You can use this to display a number as a name instead of the real value. + */ + public function as(string $string): self + { + return new self(DisplayAs::of( + $this->implementation, + $string, + )); + } + + public function toString(): string + { + return $this->implementation->toString(); + } + + public function format(): string + { + return $this->implementation->format(); + } + + /** + * This method will remove unnecessary operations in order to have the most + * precise number in the end. * * For example instead of computing each operation of `sqrt(square(x))` it * will directly return `x` */ - public function collapse(): self; + public function optimize(): self + { + return new self($this->implementation->optimize()); + } + + /** + * This prevents recomputing the underlying operations each time the result + * is accessed. + */ + public function memoize(): self + { + if ($this->implementation instanceof Native) { + return $this; + } + + return self::of($this->value()); + } } diff --git a/src/Algebra/Operation.php b/src/Algebra/Operation.php deleted file mode 100644 index ecb35a8..0000000 --- a/src/Algebra/Operation.php +++ /dev/null @@ -1,13 +0,0 @@ -number = $number; - $this->power = $power; + private function __construct( + private Implementation $number, + private Implementation $power, + ) { } /** * @psalm-pure */ - public static function of(Number $number, Number $power): self + public static function of(Implementation $number, Implementation $power): self { return new self($number, $power); } - #[\Override] - public function result(): Number - { - return Real::of( - $this->number->value() ** $this->power->value(), - ); - } - #[\Override] public function value(): int|float { @@ -40,142 +30,22 @@ public function value(): int|float } #[\Override] - public function equals(Number $number): bool - { - return $this->result()->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool + public function equals(Implementation $number): bool { - return $this->result()->higherThan($number); + return $this->value() == $number->value(); } #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return Addition::of($this, $number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return Subtraction::of($this, $number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return Division::of($this, $number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return Multiplication::of($this, $number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return Round::up($this, $precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return Round::down($this, $precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return Round::even($this, $precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return Round::odd($this, $precision); - } - - #[\Override] - public function floor(): Number - { - return Floor::of($this); - } - - #[\Override] - public function ceil(): Number - { - return Ceil::of($this); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return Modulo::of($this, $modulus); - } - - #[\Override] - public function absolute(): Number - { - return Absolute::of($this); - } - - #[\Override] - public function power(Number $power): self - { - return new self($this, $power); - } - - #[\Override] - public function squareRoot(): Number - { - return SquareRoot::of($this); - } - - #[\Override] - public function exponential(): Number - { - return Exponential::of($this); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return BinaryLogarithm::of($this); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return NaturalLogarithm::of($this); - } - - #[\Override] - public function commonLogarithm(): Number - { - return CommonLogarithm::of($this); - } - - #[\Override] - public function signum(): Number - { - return Signum::of($this); - } - - #[\Override] - public function collapse(): Number + public function optimize(): Implementation { if ($this->number instanceof SquareRoot && $this->square()) { - return $this->number->number()->collapse(); + return $this->number->number()->optimize(); } - return $this->result(); + return $this; } - public function number(): Number + public function number(): Implementation { return $this->number; } @@ -188,11 +58,22 @@ public function square(): bool #[\Override] public function toString(): string { - $number = $this->number instanceof Operation ? - '('.$this->number->toString().')' : $this->number->toString(); - $power = $this->power instanceof Operation ? - '('.$this->power->toString().')' : $this->power->toString(); + $number = $this->number->format(); + $power = $this->power->format(); return $number.'^'.$power; } + + #[\Override] + public function format(): string + { + return '('.$this->toString().')'; + } + + private function result(): Implementation + { + return Native::of( + $this->number->value() ** $this->power->value(), + ); + } } diff --git a/src/Algebra/Real.php b/src/Algebra/Real.php deleted file mode 100644 index 955a750..0000000 --- a/src/Algebra/Real.php +++ /dev/null @@ -1,189 +0,0 @@ -value = $value; - } - - /** - * @psalm-pure - */ - public static function of(int|float $value): Number - { - if (\is_infinite($value)) { - return $value > 0 ? Value::infinite : Value::negativeInfinite; - } - - if (\is_int($value)) { - return Integer::of($value); - } - - return new self($value); - } - - #[\Override] - public function value(): float - { - return $this->value; - } - - #[\Override] - public function equals(Number $number): bool - { - return $this->value() == $number->collapse()->value(); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->value() > $number->collapse()->value(); - } - - #[\Override] - public function add( - Number $number, - Number ...$numbers, - ): Number { - return Addition::of($this, $number, ...$numbers); - } - - #[\Override] - public function subtract( - Number $number, - Number ...$numbers, - ): Number { - return Subtraction::of($this, $number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return Division::of($this, $number); - } - - #[\Override] - public function multiplyBy( - Number $number, - Number ...$numbers, - ): Number { - return Multiplication::of($this, $number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return Round::up($this, $precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return Round::down($this, $precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return Round::even($this, $precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return Round::odd($this, $precision); - } - - #[\Override] - public function floor(): Number - { - return Floor::of($this); - } - - #[\Override] - public function ceil(): Number - { - return Ceil::of($this); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return Modulo::of($this, $modulus); - } - - #[\Override] - public function absolute(): Number - { - return Absolute::of($this); - } - - #[\Override] - public function power(Number $power): Number - { - return Power::of($this, $power); - } - - #[\Override] - public function squareRoot(): Number - { - return SquareRoot::of($this); - } - - #[\Override] - public function exponential(): Number - { - return Exponential::of($this); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return BinaryLogarithm::of($this); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return NaturalLogarithm::of($this); - } - - #[\Override] - public function commonLogarithm(): Number - { - return CommonLogarithm::of($this); - } - - #[\Override] - public function signum(): Number - { - return Signum::of($this); - } - - #[\Override] - public function collapse(): Number - { - return $this; - } - - #[\Override] - public function toString(): string - { - return \var_export($this->value, true); - } -} diff --git a/src/Algebra/Round.php b/src/Algebra/Round.php index b6bb285..a9a726e 100644 --- a/src/Algebra/Round.php +++ b/src/Algebra/Round.php @@ -3,60 +3,59 @@ namespace Innmind\Math\Algebra; -use Innmind\Math\Exception\PrecisionMustBePositive; - /** * @psalm-immutable + * @internal */ -final class Round implements Number +final class Round implements Implementation { - private Number $number; - private int $precision; - /** @var 0|positive-int */ - private int $mode; - /** + * @param int<0, max> $precision * @param 0|positive-int $mode */ - private function __construct(Number $number, int $precision, int $mode) - { - if ($precision < 0) { - throw new PrecisionMustBePositive((string) $precision); - } - - $this->number = $number; - $this->precision = $precision; - $this->mode = $mode; + private function __construct( + private Implementation $number, + private int $precision, + private int $mode, + ) { } /** * @psalm-pure + * + * @param int<0, max> $precision */ - public static function up(Number $number, int $precision = 0): self + public static function up(Implementation $number, int $precision = 0): self { return new self($number, $precision, \PHP_ROUND_HALF_UP); } /** * @psalm-pure + * + * @param int<0, max> $precision */ - public static function down(Number $number, int $precision = 0): self + public static function down(Implementation $number, int $precision = 0): self { return new self($number, $precision, \PHP_ROUND_HALF_DOWN); } /** * @psalm-pure + * + * @param int<0, max> $precision */ - public static function even(Number $number, int $precision = 0): self + public static function even(Implementation $number, int $precision = 0): self { return new self($number, $precision, \PHP_ROUND_HALF_EVEN); } /** * @psalm-pure + * + * @param int<0, max> $precision */ - public static function odd(Number $number, int $precision = 0): self + public static function odd(Implementation $number, int $precision = 0): self { return new self($number, $precision, \PHP_ROUND_HALF_ODD); } @@ -68,144 +67,34 @@ public function value(): int|float } #[\Override] - public function equals(Number $number): bool + public function equals(Implementation $number): bool { return $this->value() == $number->value(); } #[\Override] - public function higherThan(Number $number): bool - { - return $this->value() > $number->value(); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return Addition::of($this, $number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return Subtraction::of($this, $number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return Division::of($this, $number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return Multiplication::of($this, $number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return self::up($this, $precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return self::down($this, $precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return self::even($this, $precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return self::odd($this, $precision); - } - - #[\Override] - public function floor(): Number - { - return Floor::of($this); - } - - #[\Override] - public function ceil(): Number + public function optimize(): Implementation { - return Ceil::of($this); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return Modulo::of($this, $modulus); - } - - #[\Override] - public function absolute(): Number - { - return Absolute::of($this); - } - - #[\Override] - public function power(Number $power): Number - { - return Power::of($this, $power); - } - - #[\Override] - public function squareRoot(): Number - { - return SquareRoot::of($this); - } - - #[\Override] - public function exponential(): Number - { - return Exponential::of($this); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return BinaryLogarithm::of($this); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return NaturalLogarithm::of($this); - } - - #[\Override] - public function commonLogarithm(): Number - { - return CommonLogarithm::of($this); - } - - #[\Override] - public function signum(): Number - { - return Signum::of($this); + return new self( + $this->number->optimize(), + $this->precision, + $this->mode, + ); } #[\Override] - public function collapse(): Number + public function toString(): string { - return Real::of($this->compute($this->number->collapse())); + return \var_export($this->value(), true); } #[\Override] - public function toString(): string + public function format(): string { - return \var_export($this->value(), true); + return $this->toString(); } - private function compute(Number $number): int|float + private function compute(Implementation $number): int|float { return \round( $number->value(), diff --git a/src/Algebra/Signum.php b/src/Algebra/Signum.php index afef297..2da5cc1 100644 --- a/src/Algebra/Signum.php +++ b/src/Algebra/Signum.php @@ -5,30 +5,22 @@ /** * @psalm-immutable + * @internal */ -final class Signum implements Operation, Number +final class Signum implements Implementation { - private Number $number; - - private function __construct(Number $number) + private function __construct(private Implementation $number) { - $this->number = $number; } /** * @psalm-pure */ - public static function of(Number $number): self + public static function of(Implementation $number): self { return new self($number); } - #[\Override] - public function result(): Number - { - return $this->compute($this->number); - } - #[\Override] public function value(): int|float { @@ -36,146 +28,37 @@ public function value(): int|float } #[\Override] - public function equals(Number $number): bool - { - return $this->result()->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->result()->higherThan($number); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return Addition::of($this, $number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return Subtraction::of($this, $number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return Division::of($this, $number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return Multiplication::of($this, $number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return Round::up($this, $precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return Round::down($this, $precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return Round::even($this, $precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number + public function equals(Implementation $number): bool { - return Round::odd($this, $precision); + return $this->value() == $number->value(); } #[\Override] - public function floor(): Number + public function optimize(): Implementation { - return Floor::of($this); + return new self($this->number->optimize()); } #[\Override] - public function ceil(): Number - { - return Ceil::of($this); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return Modulo::of($this, $modulus); - } - - #[\Override] - public function absolute(): Number - { - return Absolute::of($this); - } - - #[\Override] - public function power(Number $power): Number - { - return Power::of($this, $power); - } - - #[\Override] - public function squareRoot(): Number - { - return SquareRoot::of($this); - } - - #[\Override] - public function exponential(): Number - { - return Exponential::of($this); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return BinaryLogarithm::of($this); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return NaturalLogarithm::of($this); - } - - #[\Override] - public function commonLogarithm(): Number - { - return CommonLogarithm::of($this); - } - - #[\Override] - public function signum(): self + public function toString(): string { - return new self($this); + return \sprintf('sgn(%s)', $this->number->toString()); } #[\Override] - public function collapse(): Number + public function format(): string { - return $this->compute($this->number->collapse()); + return $this->toString(); } - #[\Override] - public function toString(): string + private function result(): Implementation { - return \sprintf('sgn(%s)', $this->number->toString()); + return $this->compute($this->number); } - private function compute(Number $number): Integer + private function compute(Implementation $number): Implementation { - return Integer::of( + return Native::of( $number->value() <=> 0, ); } diff --git a/src/Algebra/SquareRoot.php b/src/Algebra/SquareRoot.php index e4cbe6d..51b9559 100644 --- a/src/Algebra/SquareRoot.php +++ b/src/Algebra/SquareRoot.php @@ -5,20 +5,18 @@ /** * @psalm-immutable + * @internal */ -final class SquareRoot implements Operation, Number +final class SquareRoot implements Implementation { - private Number $number; - - private function __construct(Number $number) + private function __construct(private Implementation $number) { - $this->number = $number; } /** * @psalm-pure */ - public static function of(Number $number): self + public static function of(Implementation $number): self { return new self($number); } @@ -30,160 +28,44 @@ public function value(): int|float } #[\Override] - public function result(): Number - { - return Real::of( - \sqrt($this->number->value()), - ); - } - - #[\Override] - public function equals(Number $number): bool - { - return $this->result()->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->result()->higherThan($number); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return Addition::of($this, $number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return Subtraction::of($this, $number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return Division::of($this, $number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return Multiplication::of($this, $number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return Round::up($this, $precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return Round::down($this, $precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return Round::even($this, $precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return Round::odd($this, $precision); - } - - #[\Override] - public function floor(): Number - { - return Floor::of($this); - } - - #[\Override] - public function ceil(): Number - { - return Ceil::of($this); - } - - #[\Override] - public function modulo(Number $modulus): Number + public function equals(Implementation $number): bool { - return Modulo::of($this, $modulus); + return $this->value() == $number->value(); } #[\Override] - public function absolute(): Number + public function optimize(): Implementation { - return Absolute::of($this); - } - - #[\Override] - public function power(Number $power): Number - { - return Power::of($this, $power); - } - - #[\Override] - public function squareRoot(): self - { - return new self($this); - } - - #[\Override] - public function exponential(): Number - { - return Exponential::of($this); - } + if ($this->number instanceof Power && $this->number->square()) { + return $this->number->number()->optimize(); + } - #[\Override] - public function binaryLogarithm(): Number - { - return BinaryLogarithm::of($this); + return $this; } - #[\Override] - public function naturalLogarithm(): Number + public function number(): Implementation { - return NaturalLogarithm::of($this); + return $this->number; } #[\Override] - public function commonLogarithm(): Number + public function toString(): string { - return CommonLogarithm::of($this); - } + $number = $this->number->format(); - #[\Override] - public function signum(): Number - { - return Signum::of($this); + return '√'.$number; } #[\Override] - public function collapse(): Number + public function format(): string { - if ($this->number instanceof Power && $this->number->square()) { - return $this->number->number()->collapse(); - } - - return $this->result(); + return '('.$this->toString().')'; } - public function number(): Number + private function result(): Implementation { - return $this->number; - } - - #[\Override] - public function toString(): string - { - $number = $this->number instanceof Operation ? - '('.$this->number->toString().')' : $this->number->toString(); - - return '√'.$number; + return Native::of( + \sqrt($this->number->value()), + ); } } diff --git a/src/Algebra/Subtraction.php b/src/Algebra/Subtraction.php index 2e4057b..fd1323b 100644 --- a/src/Algebra/Subtraction.php +++ b/src/Algebra/Subtraction.php @@ -10,213 +10,76 @@ /** * @psalm-immutable + * @internal */ -final class Subtraction implements Operation, Number +final class Subtraction implements Implementation { - private Number $first; - /** @var Sequence */ - private Sequence $values; - private function __construct( - Number $first, - Number $second, - Number ...$values, + private Implementation $a, + private Implementation $b, ) { - $this->first = $first; - $this->values = Sequence::of($first, $second, ...$values); } /** * @psalm-pure */ - public static function of( - Number $first, - Number $second, - Number ...$values, - ): self { - return new self($first, $second, ...$values); - } - - #[\Override] - public function value(): int|float - { - return $this->result()->value(); - } - - #[\Override] - public function equals(Number $number): bool - { - return $this->result()->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->result()->higherThan($number); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return Addition::of($this, $number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): self - { - return new self($this, $number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return Division::of($this, $number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return Multiplication::of($this, $number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return Round::up($this, $precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return Round::down($this, $precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return Round::even($this, $precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number + public static function of(Implementation $a, Implementation $b): self { - return Round::odd($this, $precision); + return new self($a, $b); } #[\Override] - public function floor(): Number - { - return Floor::of($this); - } - - #[\Override] - public function ceil(): Number - { - return Ceil::of($this); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return Modulo::of($this, $modulus); - } - - #[\Override] - public function absolute(): Number - { - return Absolute::of($this); - } - - #[\Override] - public function power(Number $power): Number - { - return Power::of($this, $power); - } - - #[\Override] - public function squareRoot(): Number - { - return SquareRoot::of($this); - } - - #[\Override] - public function exponential(): Number - { - return Exponential::of($this); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return BinaryLogarithm::of($this); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return NaturalLogarithm::of($this); - } - - #[\Override] - public function commonLogarithm(): Number + public function value(): int|float { - return CommonLogarithm::of($this); + return $this->difference()->value(); } #[\Override] - public function signum(): Number + public function equals(Implementation $number): bool { - return Signum::of($this); + return $this->value() == $number->value(); } - public function difference(): Number + public function difference(): Implementation { - return $this->result(); + return Native::of($this->a->value() - $this->b->value()); } #[\Override] - public function result(): Number + public function optimize(): Implementation { - return $this->compute($this->first, $this->values); - } - - #[\Override] - public function collapse(): Number - { - return $this->compute( - $this->first->collapse(), - $this->values->map(static fn($value) => $value->collapse()), + return new self( + $this->a->optimize(), + $this->b->optimize(), ); } #[\Override] public function toString(): string { - $values = $this->values->map( - static function(Number $number) { - if ($number instanceof Operation) { - return '('.$number->toString().')'; - } - - return $number->toString(); - }, + $values = $this->collect()->map( + static fn($number) => $number->format(), ); return Str::of(' - ')->join($values)->toString(); } + #[\Override] + public function format(): string + { + return '('.$this->toString().')'; + } + /** - * @param Sequence $values + * @return Sequence */ - private function compute(Number $first, Sequence $values): Number + private function collect(): Sequence { - $value = $values - ->drop(1) - ->reduce( - $first->value(), - static fn(int|float $carry, $number): int|float => $carry - $number->value(), - ); - - return Real::of($value); + return Sequence::of($this->a, $this->b)->flatMap( + static fn($number) => match (true) { + $number instanceof self => $number->collect(), + default => Sequence::of($number), + }, + ); } } diff --git a/src/Algebra/Value.php b/src/Algebra/Value.php index 43660e5..76b9e41 100644 --- a/src/Algebra/Value.php +++ b/src/Algebra/Value.php @@ -5,8 +5,9 @@ /** * @psalm-immutable + * @internal */ -enum Value implements Number +enum Value implements Implementation { case zero; case one; @@ -36,139 +37,13 @@ public function value(): int|float } #[\Override] - public function equals(Number $number): bool + public function equals(Implementation $number): bool { - return $this->value() == $number->collapse()->value(); + return $this->value() == $number->value(); } #[\Override] - public function higherThan(Number $number): bool - { - return $this->value() > $number->collapse()->value(); - } - - #[\Override] - public function add( - Number $number, - Number ...$numbers, - ): Number { - return Addition::of($this, $number, ...$numbers); - } - - #[\Override] - public function subtract( - Number $number, - Number ...$numbers, - ): Number { - return Subtraction::of($this, $number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return Division::of($this, $number); - } - - #[\Override] - public function multiplyBy( - Number $number, - Number ...$numbers, - ): Number { - return Multiplication::of($this, $number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return Round::up($this, $precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return Round::down($this, $precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return Round::even($this, $precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return Round::odd($this, $precision); - } - - #[\Override] - public function floor(): Number - { - return Floor::of($this); - } - - #[\Override] - public function ceil(): Number - { - return Ceil::of($this); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return Modulo::of($this, $modulus); - } - - #[\Override] - public function absolute(): Number - { - return Absolute::of($this); - } - - #[\Override] - public function power(Number $power): Number - { - return Power::of($this, $power); - } - - #[\Override] - public function squareRoot(): Number - { - return SquareRoot::of($this); - } - - #[\Override] - public function exponential(): Number - { - return Exponential::of($this); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return BinaryLogarithm::of($this); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return NaturalLogarithm::of($this); - } - - #[\Override] - public function commonLogarithm(): Number - { - return CommonLogarithm::of($this); - } - - #[\Override] - public function signum(): Number - { - return Signum::of($this); - } - - #[\Override] - public function collapse(): Number + public function optimize(): Implementation { return $this; } @@ -188,4 +63,10 @@ public function toString(): string self::negativeInfinite => '-∞', }; } + + #[\Override] + public function format(): string + { + return $this->toString(); + } } diff --git a/src/DefinitionSet/Implementation.php b/src/DefinitionSet/Implementation.php new file mode 100644 index 0000000..9750d85 --- /dev/null +++ b/src/DefinitionSet/Implementation.php @@ -0,0 +1,19 @@ +modulo(Value::one) - ->equals(Value::zero); - } - - #[\Override] - public function accept(Number $number): void - { - if (!$this->contains($number)) { - throw new OutOfDefinitionSet($this, $number); - } - } - - #[\Override] - public function union(Set $set): Set - { - return Union::of($this, $set); - } - - #[\Override] - public function intersect(Set $set): Set - { - return Intersection::of($this, $set); + ->modulo(Number::one()) + ->equals(Number::zero()); } #[\Override] diff --git a/src/DefinitionSet/IntegersExceptZero.php b/src/DefinitionSet/IntegersExceptZero.php index a1b262f..c865a54 100644 --- a/src/DefinitionSet/IntegersExceptZero.php +++ b/src/DefinitionSet/IntegersExceptZero.php @@ -3,52 +3,24 @@ namespace Innmind\Math\DefinitionSet; -use Innmind\Math\{ - Algebra\Number, - Algebra\Integer, - Algebra\Value, - Exception\OutOfDefinitionSet, -}; +use Innmind\Math\Algebra\Number; /** * @psalm-immutable + * @internal */ -final class IntegersExceptZero implements Set +final class IntegersExceptZero implements Implementation { #[\Override] public function contains(Number $number): bool { - if ($number->equals(Value::zero)) { + if ($number->equals(Number::zero())) { return false; } - if ($number instanceof Integer) { - return true; - } - return $number - ->modulo(Value::one) - ->equals(Value::zero); - } - - #[\Override] - public function accept(Number $number): void - { - if (!$this->contains($number)) { - throw new OutOfDefinitionSet($this, $number); - } - } - - #[\Override] - public function union(Set $set): Set - { - return Union::of($this, $set); - } - - #[\Override] - public function intersect(Set $set): Set - { - return Intersection::of($this, $set); + ->modulo(Number::one()) + ->equals(Number::zero()); } #[\Override] diff --git a/src/DefinitionSet/Intersection.php b/src/DefinitionSet/Intersection.php index 5f00fac..ea1bd91 100644 --- a/src/DefinitionSet/Intersection.php +++ b/src/DefinitionSet/Intersection.php @@ -3,29 +3,24 @@ namespace Innmind\Math\DefinitionSet; -use Innmind\Math\{ - Algebra\Number, - Exception\OutOfDefinitionSet, -}; +use Innmind\Math\Algebra\Number; /** * @psalm-immutable + * @internal */ -final class Intersection implements Set +final class Intersection implements Implementation { - private Set $left; - private Set $right; - - private function __construct(Set $left, Set $right) - { - $this->left = $left; - $this->right = $right; + private function __construct( + private Implementation $left, + private Implementation $right, + ) { } /** * @psalm-pure */ - public static function of(Set $left, Set $right): self + public static function of(Implementation $left, Implementation $right): self { return new self($left, $right); } @@ -37,26 +32,6 @@ public function contains(Number $number): bool $this->right->contains($number); } - #[\Override] - public function accept(Number $number): void - { - if (!$this->contains($number)) { - throw new OutOfDefinitionSet($this, $number); - } - } - - #[\Override] - public function union(Set $set): Set - { - return Union::of($this, $set); - } - - #[\Override] - public function intersect(Set $set): Set - { - return new self($this, $set); - } - #[\Override] public function toString(): string { diff --git a/src/DefinitionSet/NaturalNumbers.php b/src/DefinitionSet/NaturalNumbers.php index 9e91f40..e997715 100644 --- a/src/DefinitionSet/NaturalNumbers.php +++ b/src/DefinitionSet/NaturalNumbers.php @@ -3,52 +3,24 @@ namespace Innmind\Math\DefinitionSet; -use Innmind\Math\{ - Algebra\Number, - Algebra\Integer, - Algebra\Value, - Exception\OutOfDefinitionSet, -}; +use Innmind\Math\Algebra\Number; /** * @psalm-immutable + * @internal */ -final class NaturalNumbers implements Set +final class NaturalNumbers implements Implementation { #[\Override] public function contains(Number $number): bool { - if (Value::zero->higherThan($number)) { + if (Number::zero()->higherThan($number)) { return false; } - if ($number instanceof Integer) { - return true; - } - return $number - ->modulo(Value::one) - ->equals(Value::zero); - } - - #[\Override] - public function accept(Number $number): void - { - if (!$this->contains($number)) { - throw new OutOfDefinitionSet($this, $number); - } - } - - #[\Override] - public function union(Set $set): Set - { - return Union::of($this, $set); - } - - #[\Override] - public function intersect(Set $set): Set - { - return Intersection::of($this, $set); + ->modulo(Number::one()) + ->equals(Number::zero()); } #[\Override] diff --git a/src/DefinitionSet/NaturalNumbersExceptZero.php b/src/DefinitionSet/NaturalNumbersExceptZero.php index 74a8d79..d57c552 100644 --- a/src/DefinitionSet/NaturalNumbersExceptZero.php +++ b/src/DefinitionSet/NaturalNumbersExceptZero.php @@ -3,52 +3,24 @@ namespace Innmind\Math\DefinitionSet; -use Innmind\Math\{ - Algebra\Number, - Algebra\Integer, - Algebra\Value, - Exception\OutOfDefinitionSet, -}; +use Innmind\Math\Algebra\Number; /** * @psalm-immutable + * @internal */ -final class NaturalNumbersExceptZero implements Set +final class NaturalNumbersExceptZero implements Implementation { #[\Override] public function contains(Number $number): bool { - if (Value::one->higherThan($number)) { + if (Number::one()->higherThan($number)) { return false; } - if ($number instanceof Integer) { - return true; - } - return $number - ->modulo(Value::one) - ->equals(Value::zero); - } - - #[\Override] - public function accept(Number $number): void - { - if (!$this->contains($number)) { - throw new OutOfDefinitionSet($this, $number); - } - } - - #[\Override] - public function union(Set $set): Set - { - return Union::of($this, $set); - } - - #[\Override] - public function intersect(Set $set): Set - { - return Intersection::of($this, $set); + ->modulo(Number::one()) + ->equals(Number::zero()); } #[\Override] diff --git a/src/DefinitionSet/Range.php b/src/DefinitionSet/Range.php index d934498..2acf742 100644 --- a/src/DefinitionSet/Range.php +++ b/src/DefinitionSet/Range.php @@ -3,31 +3,20 @@ namespace Innmind\Math\DefinitionSet; -use Innmind\Math\{ - Algebra\Number, - Exception\OutOfDefinitionSet, -}; +use Innmind\Math\Algebra\Number; /** * @psalm-immutable + * @internal */ -final class Range implements Set +final class Range implements Implementation { - private bool $lowerInclusivity; - private bool $upperInclusivity; - private Number $lower; - private Number $upper; - private function __construct( - bool $lowerInclusivity, - Number $lower, - Number $upper, - bool $upperInclusivity, + private bool $lowerInclusivity, + private Number $lower, + private Number $upper, + private bool $upperInclusivity, ) { - $this->lowerInclusivity = $lowerInclusivity; - $this->lower = $lower; - $this->upper = $upper; - $this->upperInclusivity = $upperInclusivity; } /** @@ -46,16 +35,6 @@ public static function exclusive(Number $lower, Number $upper): self return new self(false, $lower, $upper, false); } - public function excludeLowerBound(): self - { - return new self(false, $this->lower, $this->upper, $this->upperInclusivity); - } - - public function excludeUpperBound(): self - { - return new self($this->lowerInclusivity, $this->lower, $this->upper, false); - } - #[\Override] public function contains(Number $number): bool { @@ -84,26 +63,6 @@ public function contains(Number $number): bool return true; } - #[\Override] - public function accept(Number $number): void - { - if (!$this->contains($number)) { - throw new OutOfDefinitionSet($this, $number); - } - } - - #[\Override] - public function union(Set $set): Set - { - return Union::of($this, $set); - } - - #[\Override] - public function intersect(Set $set): Set - { - return Intersection::of($this, $set); - } - #[\Override] public function toString(): string { diff --git a/src/DefinitionSet/RealNumbers.php b/src/DefinitionSet/RealNumbers.php index b43e0cd..7cdaea3 100644 --- a/src/DefinitionSet/RealNumbers.php +++ b/src/DefinitionSet/RealNumbers.php @@ -7,8 +7,9 @@ /** * @psalm-immutable + * @internal */ -final class RealNumbers implements Set +final class RealNumbers implements Implementation { #[\Override] public function contains(Number $number): bool @@ -16,24 +17,6 @@ public function contains(Number $number): bool return true; } - #[\Override] - public function accept(Number $number): void - { - // it accepts everything - } - - #[\Override] - public function union(Set $set): Set - { - return Union::of($this, $set); - } - - #[\Override] - public function intersect(Set $set): Set - { - return Intersection::of($this, $set); - } - #[\Override] public function toString(): string { diff --git a/src/DefinitionSet/RealNumbersExceptZero.php b/src/DefinitionSet/RealNumbersExceptZero.php index d1bf8e6..62abd03 100644 --- a/src/DefinitionSet/RealNumbersExceptZero.php +++ b/src/DefinitionSet/RealNumbersExceptZero.php @@ -3,41 +3,18 @@ namespace Innmind\Math\DefinitionSet; -use Innmind\Math\{ - Algebra\Number, - Algebra\Value, - Exception\OutOfDefinitionSet, -}; +use Innmind\Math\Algebra\Number; /** * @psalm-immutable + * @internal */ -final class RealNumbersExceptZero implements Set +final class RealNumbersExceptZero implements Implementation { #[\Override] public function contains(Number $number): bool { - return !$number->equals(Value::zero); - } - - #[\Override] - public function accept(Number $number): void - { - if (!$this->contains($number)) { - throw new OutOfDefinitionSet($this, $number); - } - } - - #[\Override] - public function union(Set $set): Set - { - return Union::of($this, $set); - } - - #[\Override] - public function intersect(Set $set): Set - { - return Intersection::of($this, $set); + return !$number->equals(Number::zero()); } #[\Override] diff --git a/src/DefinitionSet/Set.php b/src/DefinitionSet/Set.php index 390c9f2..aa45206 100644 --- a/src/DefinitionSet/Set.php +++ b/src/DefinitionSet/Set.php @@ -7,22 +7,132 @@ Algebra\Number, Exception\OutOfDefinitionSet, }; +use Innmind\Immutable\{ + Attempt, + SideEffect, +}; /** * @psalm-immutable */ -interface Set +final class Set { + private function __construct( + private Implementation $implementation, + ) { + } + + /** + * @psalm-pure + * @no-named-arguments + */ + public static function of(Number ...$values): self + { + return new self(Values::of(...$values)); + } + + /** + * @psalm-pure + */ + public static function integers(): self + { + return new self(new Integers); + } + + /** + * @psalm-pure + */ + public static function integersExceptZero(): self + { + return new self(new IntegersExceptZero); + } + + /** + * @psalm-pure + */ + public static function naturalNumbers(): self + { + return new self(new NaturalNumbers); + } + + /** + * @psalm-pure + */ + public static function naturalNumbersExceptZero(): self + { + return new self(new NaturalNumbersExceptZero); + } + + /** + * @psalm-pure + */ + public static function realNumbers(): self + { + return new self(new RealNumbers); + } + + /** + * @psalm-pure + */ + public static function realNumbersExceptZero(): self + { + return new self(new RealNumbersExceptZero); + } + + /** + * @psalm-pure + */ + public static function inclusiveRange(Number $lower, Number $upper): self + { + return new self(Range::inclusive($lower, $upper)); + } + + /** + * @psalm-pure + */ + public static function exclusiveRange(Number $lower, Number $upper): self + { + return new self(Range::exclusive($lower, $upper)); + } + /** * ∈ or ∉ */ - public function contains(Number $number): bool; + public function contains(Number $number): bool + { + return $this->implementation->contains($number); + } /** - * @throws OutOfDefinitionSet + * @return Attempt */ - public function accept(Number $number): void; - public function union(self $set): self; - public function intersect(self $set): self; - public function toString(): string; + public function accept(Number $number): Attempt + { + if (!$this->contains($number)) { + return Attempt::error(new OutOfDefinitionSet($this->implementation, $number)); + } + + return Attempt::result(SideEffect::identity); + } + + public function union(self $set): self + { + return new self(Union::of( + $this->implementation, + $set->implementation, + )); + } + + public function intersect(self $set): self + { + return new self(Intersection::of( + $this->implementation, + $set->implementation, + )); + } + + public function toString(): string + { + return $this->implementation->toString(); + } } diff --git a/src/DefinitionSet/Set/Set.php b/src/DefinitionSet/Set/Set.php deleted file mode 100644 index 5345c17..0000000 --- a/src/DefinitionSet/Set/Set.php +++ /dev/null @@ -1,89 +0,0 @@ - */ - private Sequence $values; - - /** - * @no-named-arguments - */ - private function __construct(Number ...$values) - { - $this->values = Sequence::of(...$values)->map( - static fn(Number $v) => $v->value(), - ); - } - - /** - * @psalm-pure - * @no-named-arguments - */ - public static function of(Number ...$values): self - { - return new self(...$values); - } - - #[\Override] - public function contains(Number $number): bool - { - return $this->values->contains($number->value()); - } - - #[\Override] - public function accept(Number $number): void - { - if (!$this->contains($number)) { - throw new OutOfDefinitionSet($this, $number); - } - } - - #[\Override] - public function union(SetInterface $set): SetInterface - { - return Union::of($this, $set); - } - - #[\Override] - public function intersect(SetInterface $set): SetInterface - { - return Intersection::of($this, $set); - } - - #[\Override] - public function toString(): string - { - if ($this->values->size() === 0) { - return '∅'; - } - - /** @var Sequence */ - $values = $this->values->map( - static fn($number): string => (string) $number, - ); - - return Str::of(';') - ->join($values) - ->prepend('{') - ->append('}') - ->toString(); - } -} diff --git a/src/DefinitionSet/Union.php b/src/DefinitionSet/Union.php index c95ab72..c1fa9b1 100644 --- a/src/DefinitionSet/Union.php +++ b/src/DefinitionSet/Union.php @@ -3,29 +3,24 @@ namespace Innmind\Math\DefinitionSet; -use Innmind\Math\{ - Algebra\Number, - Exception\OutOfDefinitionSet, -}; +use Innmind\Math\Algebra\Number; /** * @psalm-immutable + * @internal */ -final class Union implements Set +final class Union implements Implementation { - private Set $left; - private Set $right; - - private function __construct(Set $left, Set $right) - { - $this->left = $left; - $this->right = $right; + private function __construct( + private Implementation $left, + private Implementation $right, + ) { } /** * @psalm-pure */ - public static function of(Set $left, Set $right): self + public static function of(Implementation $left, Implementation $right): self { return new self($left, $right); } @@ -37,26 +32,6 @@ public function contains(Number $number): bool $this->right->contains($number); } - #[\Override] - public function accept(Number $number): void - { - if (!$this->contains($number)) { - throw new OutOfDefinitionSet($this, $number); - } - } - - #[\Override] - public function union(Set $set): Set - { - return new self($this, $set); - } - - #[\Override] - public function intersect(Set $set): Set - { - return Intersection::of($this, $set); - } - #[\Override] public function toString(): string { diff --git a/src/DefinitionSet/Values.php b/src/DefinitionSet/Values.php new file mode 100644 index 0000000..4dd486e --- /dev/null +++ b/src/DefinitionSet/Values.php @@ -0,0 +1,60 @@ + $values + */ + private function __construct(private Sequence $values) + { + } + + /** + * @psalm-pure + * @no-named-arguments + */ + public static function of(Number ...$values): self + { + return new self(Sequence::of(...$values)->map( + static fn(Number $v) => $v->value(), + )); + } + + #[\Override] + public function contains(Number $number): bool + { + return $this->values->contains($number->value()); + } + + #[\Override] + public function toString(): string + { + if ($this->values->size() === 0) { + return '∅'; + } + + /** @var Sequence */ + $values = $this->values->map( + static fn($number): string => (string) $number, + ); + + return Str::of(';') + ->join($values) + ->prepend('{') + ->append('}') + ->toString(); + } +} diff --git a/src/Exception/OutOfDefinitionSet.php b/src/Exception/OutOfDefinitionSet.php index 5525510..02ef3b4 100644 --- a/src/Exception/OutOfDefinitionSet.php +++ b/src/Exception/OutOfDefinitionSet.php @@ -4,13 +4,13 @@ namespace Innmind\Math\Exception; use Innmind\Math\{ - DefinitionSet\Set, + DefinitionSet\Implementation, Algebra\Number, }; final class OutOfDefinitionSet extends LogicException { - public function __construct(Set $set, Number $number) + public function __construct(Implementation $set, Number $number) { parent::__construct($number->toString().' ∉ '.$set->toString()); } diff --git a/src/Exception/PrecisionMustBePositive.php b/src/Exception/PrecisionMustBePositive.php deleted file mode 100644 index d9207ca..0000000 --- a/src/Exception/PrecisionMustBePositive.php +++ /dev/null @@ -1,8 +0,0 @@ -firstSegment = $firstSegment; - $this->secondSegment = $secondSegment; - $this->degree = $degree; } /** @@ -74,6 +66,6 @@ public function scalarProduct(): Number ->firstSegment ->length() ->multiplyBy($this->secondSegment->length()) - ->multiplyBy(Cosine::of($this->degree)); + ->multiplyBy($this->degree->cosine()); } } diff --git a/src/Geometry/Angle/Degree.php b/src/Geometry/Angle/Degree.php index 73eee14..5c8f10c 100644 --- a/src/Geometry/Angle/Degree.php +++ b/src/Geometry/Angle/Degree.php @@ -3,10 +3,9 @@ namespace Innmind\Math\Geometry\Angle; -use Innmind\Math\Algebra\{ - Number, - Integer, - Real, +use Innmind\Math\{ + Algebra\Number, + Geometry\Trigonometry, }; /** @@ -14,15 +13,8 @@ */ final class Degree { - private Number $number; - - private function __construct(Number $number) + private function __construct(private Number $number) { - $modulus = Integer::of(360); - $this->number = $number - ->modulo($modulus) - ->add($modulus) - ->modulo($modulus); } /** @@ -30,41 +22,75 @@ private function __construct(Number $number) */ public static function of(Number $number): self { - return new self($number); + $modulus = Number::of(360); + + return new self( + $number + ->modulo($modulus) + ->add($modulus) + ->modulo($modulus), + ); } public function toRadian(): Radian { return Radian::of( - Real::of( + Number::of( \deg2rad($this->number->value()), ), ); } - public function isRight(): bool + public function right(): bool { - return $this->number->equals(Integer::of(90)); + return $this->number->equals(Number::of(90)); } - public function isObtuse(): bool + public function obtuse(): bool { - return $this->number->higherThan(Integer::of(90)); + return $this->number->higherThan(Number::of(90)); } - public function isAcuse(): bool + public function acuse(): bool { - return Integer::of(90)->higherThan($this->number); + return Number::of(90)->higherThan($this->number); } - public function isFlat(): bool + public function flat(): bool { - return $this->number->equals(Integer::of(180)); + return $this->number->equals(Number::of(180)); } public function opposite(): self { - return new self($this->number->add(Integer::of(180))); + return self::of($this->number->add(Number::of(180))); + } + + public function cosine(): Number + { + return $this + ->toRadian() + ->number() + ->as($this->toString()) + ->apply(Trigonometry::cosine); + } + + public function sine(): Number + { + return $this + ->toRadian() + ->number() + ->as($this->toString()) + ->apply(Trigonometry::sine); + } + + public function tangent(): Number + { + return $this + ->toRadian() + ->number() + ->as($this->toString()) + ->apply(Trigonometry::tangent); } public function number(): Number diff --git a/src/Geometry/Angle/Radian.php b/src/Geometry/Angle/Radian.php index a25a2da..466d836 100644 --- a/src/Geometry/Angle/Radian.php +++ b/src/Geometry/Angle/Radian.php @@ -3,10 +3,9 @@ namespace Innmind\Math\Geometry\Angle; -use Innmind\Math\Algebra\{ - Number, - Value, - Real, +use Innmind\Math\{ + Algebra\Number, + Geometry\Trigonometry, }; /** @@ -14,13 +13,8 @@ */ final class Radian { - private Number $number; - - private function __construct(Number $number) + private function __construct(private Number $number) { - $this->number = $number->modulo( - Value::pi->multiplyBy(Value::two), - ); } /** @@ -28,36 +22,38 @@ private function __construct(Number $number) */ public static function of(Number $number): self { - return new self($number); + return new self($number->modulo( + Number::pi()->multiplyBy(Number::two()), + )); } public function toDegree(): Degree { return Degree::of( - Real::of( + Number::of( \rad2deg($this->number->value()), ), ); } - public function isRight(): bool + public function right(): bool { - return $this->toDegree()->isRight(); + return $this->toDegree()->right(); } - public function isObtuse(): bool + public function obtuse(): bool { - return $this->toDegree()->isObtuse(); + return $this->toDegree()->obtuse(); } - public function isAcuse(): bool + public function acuse(): bool { - return $this->toDegree()->isAcuse(); + return $this->toDegree()->acuse(); } - public function isFlat(): bool + public function flat(): bool { - return $this->toDegree()->isFlat(); + return $this->toDegree()->flat(); } public function opposite(): self @@ -65,6 +61,30 @@ public function opposite(): self return $this->toDegree()->opposite()->toRadian(); } + public function cosine(): Number + { + return $this + ->number + ->as($this->toString()) + ->apply(Trigonometry::cosine); + } + + public function sine(): Number + { + return $this + ->number + ->as($this->toString()) + ->apply(Trigonometry::sine); + } + + public function tangent(): Number + { + return $this + ->number + ->as($this->toString()) + ->apply(Trigonometry::tangent); + } + public function number(): Number { return $this->number; diff --git a/src/Geometry/Figure/Circle.php b/src/Geometry/Figure/Circle.php index 7c78fc4..ecf76bd 100644 --- a/src/Geometry/Figure/Circle.php +++ b/src/Geometry/Figure/Circle.php @@ -7,7 +7,6 @@ Geometry\Figure, Geometry\Segment, Algebra\Number, - Algebra\Value, }; /** @@ -22,7 +21,7 @@ private function __construct(Segment $radius) { $this->radius = $radius; $this->diameter = Segment::of( - $radius->length()->multiplyBy(Value::two), + $radius->length()->multiplyBy(Number::two()), ); } @@ -37,15 +36,15 @@ public static function of(Segment $radius): self #[\Override] public function perimeter(): Number { - return Value::pi - ->multiplyBy(Value::two) + return Number::pi() + ->multiplyBy(Number::two()) ->multiplyBy($this->radius->length()); } #[\Override] public function area(): Number { - return Value::pi + return Number::pi() ->multiplyBy($this->radius->length()) ->multiplyBy($this->radius->length()); } diff --git a/src/Geometry/Figure/Rectangle.php b/src/Geometry/Figure/Rectangle.php index 7158d11..8ea482b 100644 --- a/src/Geometry/Figure/Rectangle.php +++ b/src/Geometry/Figure/Rectangle.php @@ -7,7 +7,6 @@ Geometry\Figure, Geometry\Segment, Algebra\Number, - Algebra\Value, }; /** @@ -15,13 +14,10 @@ */ final class Rectangle implements Figure { - private Segment $length; - private Segment $width; - - private function __construct(Segment $length, Segment $width) - { - $this->length = $length; - $this->width = $width; + private function __construct( + private Segment $length, + private Segment $width, + ) { } /** @@ -38,8 +34,8 @@ public function perimeter(): Number return $this ->length ->length() - ->multiplyBy(Value::two) - ->add($this->width->length()->multiplyBy(Value::two)); + ->multiplyBy(Number::two()) + ->add($this->width->length()->multiplyBy(Number::two())); } #[\Override] diff --git a/src/Geometry/Figure/Square.php b/src/Geometry/Figure/Square.php index 1295af1..cbcea1f 100644 --- a/src/Geometry/Figure/Square.php +++ b/src/Geometry/Figure/Square.php @@ -7,7 +7,6 @@ Geometry\Figure, Geometry\Segment, Algebra\Number, - Algebra\Integer, }; /** @@ -15,11 +14,8 @@ */ final class Square implements Figure { - private Segment $side; - - private function __construct(Segment $side) + private function __construct(private Segment $side) { - $this->side = $side; } /** @@ -33,7 +29,7 @@ public static function of(Segment $side): self #[\Override] public function perimeter(): Number { - return $this->side->length()->multiplyBy(Integer::of(4)); + return $this->side->length()->multiplyBy(Number::of(4)); } #[\Override] diff --git a/src/Geometry/Figure/Triangle.php b/src/Geometry/Figure/Triangle.php index 87d6c93..aeedcfd 100644 --- a/src/Geometry/Figure/Triangle.php +++ b/src/Geometry/Figure/Triangle.php @@ -8,8 +8,6 @@ Geometry\Figure, Geometry\Segment, Algebra\Number, - Algebra\Value, - Algebra\Addition, }; /** @@ -42,7 +40,7 @@ private function __construct( $this ->area() ->divideBy($base) - ->multiplyBy(Value::two), + ->multiplyBy(Number::two()), ); } @@ -60,14 +58,17 @@ public static function of( #[\Override] public function perimeter(): Number { - return Addition::of($this->a, $this->b, $this->c); + return $this + ->a + ->add($this->b) + ->add($this->c); } #[\Override] public function area(): Number { // Heron's formula - $p = $this->perimeter()->divideBy(Value::two); + $p = $this->perimeter()->divideBy(Number::two()); return $p ->multiplyBy($p->subtract($this->a)) @@ -86,14 +87,14 @@ public function height(): Segment return $this->height; } - public function isIsosceles(): bool + public function isosceles(): bool { return $this->a->equals($this->b) || $this->a->equals($this->c) || $this->b->equals($this->c); } - public function isEquilateral(): bool + public function equilateral(): bool { return $this->a->equals($this->b) && $this->b->equals($this->c); diff --git a/src/Geometry/Segment.php b/src/Geometry/Segment.php index 1ad85b4..61c0408 100644 --- a/src/Geometry/Segment.php +++ b/src/Geometry/Segment.php @@ -6,7 +6,6 @@ use Innmind\Math\{ Geometry\Angle\Degree, Algebra\Number, - Algebra\Value, Exception\LengthMustBePositive, }; @@ -19,7 +18,7 @@ final class Segment private function __construct(Number $length) { - if (!$length->higherThan(Value::zero)) { + if (!$length->higherThan(Number::zero())) { throw new LengthMustBePositive($length->toString()); } diff --git a/src/Geometry/Theorem/AlKashi.php b/src/Geometry/Theorem/AlKashi.php index 3acd1d1..69514e2 100644 --- a/src/Geometry/Theorem/AlKashi.php +++ b/src/Geometry/Theorem/AlKashi.php @@ -5,11 +5,11 @@ use function Innmind\Math\max as maximum; use Innmind\Math\{ - Algebra\Value, + Algebra\Number, Geometry\Angle\Degree, + Geometry\Angle\Radian, Geometry\Segment, - Geometry\Trigonometry\Cosine, - Geometry\Trigonometry\ArcCosine, + Geometry\Trigonometry, Exception\SegmentsCannotBeJoined, }; @@ -34,15 +34,15 @@ public static function side( $b = $b->length(); $side = $a - ->power(Value::two) + ->power(Number::two()) ->add( - $b->power(Value::two), + $b->power(Number::two()), ) ->subtract( - Value::two + Number::two() ->multiplyBy($a) ->multiplyBy($b) - ->multiplyBy(Cosine::of($degree)), + ->multiplyBy($degree->cosine()), ) ->squareRoot(); @@ -73,16 +73,16 @@ public static function angle( } $cosAB = $a - ->power(Value::two) - ->add($b->power(Value::two)) - ->subtract($c->power(Value::two)) + ->power(Number::two()) + ->add($b->power(Number::two())) + ->subtract($c->power(Number::two())) ->divideBy( - Value::two + Number::two() ->multiplyBy($a) ->multiplyBy($b), ); - return ArcCosine::of($cosAB)->toDegree(); + return Radian::of($cosAB->apply(Trigonometry::arcCosine))->toDegree(); } public function toString(): string diff --git a/src/Geometry/Theorem/Pythagora.php b/src/Geometry/Theorem/Pythagora.php index ec8413a..0f3cb6a 100644 --- a/src/Geometry/Theorem/Pythagora.php +++ b/src/Geometry/Theorem/Pythagora.php @@ -4,7 +4,7 @@ namespace Innmind\Math\Geometry\Theorem; use Innmind\Math\{ - Algebra\Value, + Algebra\Number, Geometry\Segment, }; @@ -24,8 +24,8 @@ public static function hypotenuse( ): Segment { $hypotenuse = $a ->length() - ->power(Value::two) - ->add($b->length()->power(Value::two)) + ->power(Number::two()) + ->add($b->length()->power(Number::two())) ->squareRoot(); return Segment::of($hypotenuse); @@ -41,8 +41,8 @@ public static function adjacentSide( ): Segment { $side = $hypotenuse ->length() - ->power(Value::two) - ->subtract($adjacentSide->length()->power(Value::two)) + ->power(Number::two()) + ->subtract($adjacentSide->length()->power(Number::two())) ->squareRoot(); return Segment::of($side); diff --git a/src/Geometry/Trigonometry.php b/src/Geometry/Trigonometry.php new file mode 100644 index 0000000..cb6be25 --- /dev/null +++ b/src/Geometry/Trigonometry.php @@ -0,0 +1,48 @@ + \acos($x->value()), + self::arcSine => \asin($x->value()), + self::arcTangent => \atan($x->value()), + self::cosine => \cos($x->value()), + self::sine => \sin($x->value()), + self::tangent => \tan($x->value()), + }); + } + + #[\Override] + public function name(): string + { + return match ($this) { + self::arcCosine => 'cos⁻¹', + self::arcSine => 'sin⁻¹', + self::arcTangent => 'tan⁻¹', + self::cosine => 'cos', + self::sine => 'sin', + self::tangent => 'tan', + }; + } +} diff --git a/src/Geometry/Trigonometry/ArcCosine.php b/src/Geometry/Trigonometry/ArcCosine.php deleted file mode 100644 index e6bfb17..0000000 --- a/src/Geometry/Trigonometry/ArcCosine.php +++ /dev/null @@ -1,193 +0,0 @@ -number = $number; - } - - /** - * @psalm-pure - */ - public static function of(Number $number): self - { - return new self($number); - } - - public function toDegree(): Degree - { - return $this->arcCosine(); - } - - #[\Override] - public function value(): int|float - { - return $this->arcCosine()->number()->value(); - } - - #[\Override] - public function equals(Number $number): bool - { - return $this->arcCosine()->number()->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->arcCosine()->number()->higherThan($number); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return $this->arcCosine()->number()->add($number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return $this->arcCosine()->number()->subtract($number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return $this->arcCosine()->number()->divideBy($number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return $this->arcCosine()->number()->multiplyBy($number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return $this->arcCosine()->number()->roundUp($precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return $this->arcCosine()->number()->roundDown($precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return $this->arcCosine()->number()->roundEven($precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return $this->arcCosine()->number()->roundOdd($precision); - } - - #[\Override] - public function floor(): Number - { - return $this->arcCosine()->number()->floor(); - } - - #[\Override] - public function ceil(): Number - { - return $this->arcCosine()->number()->ceil(); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return $this->arcCosine()->number()->modulo($modulus); - } - - #[\Override] - public function absolute(): Number - { - return $this->arcCosine()->number()->absolute(); - } - - #[\Override] - public function power(Number $power): Number - { - return $this->arcCosine()->number()->power($power); - } - - #[\Override] - public function squareRoot(): Number - { - return $this->arcCosine()->number()->squareRoot(); - } - - #[\Override] - public function exponential(): Number - { - return $this->arcCosine()->number()->exponential(); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return $this->arcCosine()->number()->binaryLogarithm(); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return $this->arcCosine()->number()->naturalLogarithm(); - } - - #[\Override] - public function commonLogarithm(): Number - { - return $this->arcCosine()->number()->commonLogarithm(); - } - - #[\Override] - public function signum(): Number - { - return $this->arcCosine()->number()->signum(); - } - - #[\Override] - public function collapse(): Number - { - return new self($this->number->collapse()); - } - - #[\Override] - public function toString(): string - { - return \sprintf('cos⁻¹(%s)', $this->number->toString()); - } - - private function arcCosine(): Degree - { - $radians = Real::of( - \acos( - $this->number->value(), - ), - ); - - return Radian::of($radians)->toDegree(); - } -} diff --git a/src/Geometry/Trigonometry/ArcSine.php b/src/Geometry/Trigonometry/ArcSine.php deleted file mode 100644 index 6b946be..0000000 --- a/src/Geometry/Trigonometry/ArcSine.php +++ /dev/null @@ -1,193 +0,0 @@ -number = $number; - } - - /** - * @psalm-pure - */ - public static function of(Number $number): self - { - return new self($number); - } - - public function toDegree(): Degree - { - return $this->arcSine(); - } - - #[\Override] - public function value(): int|float - { - return $this->arcSine()->number()->value(); - } - - #[\Override] - public function equals(Number $number): bool - { - return $this->arcSine()->number()->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->arcSine()->number()->higherThan($number); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return $this->arcSine()->number()->add($number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return $this->arcSine()->number()->subtract($number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return $this->arcSine()->number()->divideBy($number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return $this->arcSine()->number()->multiplyBy($number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return $this->arcSine()->number()->roundUp($precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return $this->arcSine()->number()->roundDown($precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return $this->arcSine()->number()->roundEven($precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return $this->arcSine()->number()->roundOdd($precision); - } - - #[\Override] - public function floor(): Number - { - return $this->arcSine()->number()->floor(); - } - - #[\Override] - public function ceil(): Number - { - return $this->arcSine()->number()->ceil(); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return $this->arcSine()->number()->modulo($modulus); - } - - #[\Override] - public function absolute(): Number - { - return $this->arcSine()->number()->absolute(); - } - - #[\Override] - public function power(Number $power): Number - { - return $this->arcSine()->number()->power($power); - } - - #[\Override] - public function squareRoot(): Number - { - return $this->arcSine()->number()->squareRoot(); - } - - #[\Override] - public function exponential(): Number - { - return $this->arcSine()->number()->exponential(); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return $this->arcSine()->number()->binaryLogarithm(); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return $this->arcSine()->number()->naturalLogarithm(); - } - - #[\Override] - public function commonLogarithm(): Number - { - return $this->arcSine()->number()->commonLogarithm(); - } - - #[\Override] - public function signum(): Number - { - return $this->arcSine()->number()->signum(); - } - - #[\Override] - public function collapse(): Number - { - return new self($this->number->collapse()); - } - - #[\Override] - public function toString(): string - { - return \sprintf('sin⁻¹(%s)', $this->number->toString()); - } - - private function arcSine(): Degree - { - $radians = Real::of( - \asin( - $this->number->value(), - ), - ); - - return Radian::of($radians)->toDegree(); - } -} diff --git a/src/Geometry/Trigonometry/ArcTangent.php b/src/Geometry/Trigonometry/ArcTangent.php deleted file mode 100644 index da9fe72..0000000 --- a/src/Geometry/Trigonometry/ArcTangent.php +++ /dev/null @@ -1,199 +0,0 @@ -number = $number; - } - - /** - * @psalm-pure - */ - public static function of(Number $number): self - { - return new self($number); - } - - public function toDegree(): Degree - { - return $this->arcTangent(); - } - - #[\Override] - public function value(): int|float - { - return $this->arcTangent()->number()->value(); - } - - #[\Override] - public function equals(Number $number): bool - { - return $this->arcTangent()->number()->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->arcTangent()->number()->higherThan($number); - } - - #[\Override] - public function add( - Number $number, - Number ...$numbers, - ): Number { - return $this->arcTangent()->number()->add($number, ...$numbers); - } - - #[\Override] - public function subtract( - Number $number, - Number ...$numbers, - ): Number { - return $this->arcTangent()->number()->subtract($number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return $this->arcTangent()->number()->divideBy($number); - } - - #[\Override] - public function multiplyBy( - Number $number, - Number ...$numbers, - ): Number { - return $this->arcTangent()->number()->multiplyBy($number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return $this->arcTangent()->number()->roundUp($precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return $this->arcTangent()->number()->roundDown($precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return $this->arcTangent()->number()->roundEven($precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return $this->arcTangent()->number()->roundOdd($precision); - } - - #[\Override] - public function floor(): Number - { - return $this->arcTangent()->number()->floor(); - } - - #[\Override] - public function ceil(): Number - { - return $this->arcTangent()->number()->ceil(); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return $this->arcTangent()->number()->modulo($modulus); - } - - #[\Override] - public function absolute(): Number - { - return $this->arcTangent()->number()->absolute(); - } - - #[\Override] - public function power(Number $power): Number - { - return $this->arcTangent()->number()->power($power); - } - - #[\Override] - public function squareRoot(): Number - { - return $this->arcTangent()->number()->squareRoot(); - } - - #[\Override] - public function exponential(): Number - { - return $this->arcTangent()->number()->exponential(); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return $this->arcTangent()->number()->binaryLogarithm(); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return $this->arcTangent()->number()->naturalLogarithm(); - } - - #[\Override] - public function commonLogarithm(): Number - { - return $this->arcTangent()->number()->commonLogarithm(); - } - - #[\Override] - public function signum(): Number - { - return $this->arcTangent()->number()->signum(); - } - - #[\Override] - public function collapse(): Number - { - return new self($this->number->collapse()); - } - - #[\Override] - public function toString(): string - { - return \sprintf('tan⁻¹(%s)', $this->number->toString()); - } - - private function arcTangent(): Degree - { - $radians = Real::of( - \atan( - $this->number->value(), - ), - ); - - return Radian::of($radians)->toDegree(); - } -} diff --git a/src/Geometry/Trigonometry/Cosine.php b/src/Geometry/Trigonometry/Cosine.php deleted file mode 100644 index 565fd15..0000000 --- a/src/Geometry/Trigonometry/Cosine.php +++ /dev/null @@ -1,189 +0,0 @@ -degree = $degree; - } - - /** - * @psalm-pure - */ - public static function of(Degree $degree): self - { - return new self($degree); - } - - #[\Override] - public function value(): int|float - { - return $this->cosine()->value(); - } - - #[\Override] - public function equals(Number $number): bool - { - return $this->cosine()->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->cosine()->higherThan($number); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return $this->cosine()->add($number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return $this->cosine()->subtract($number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return $this->cosine()->divideBy($number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return $this->cosine()->multiplyBy($number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return $this->cosine()->roundUp($precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return $this->cosine()->roundDown($precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return $this->cosine()->roundEven($precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return $this->cosine()->roundOdd($precision); - } - - #[\Override] - public function floor(): Number - { - return $this->cosine()->floor(); - } - - #[\Override] - public function ceil(): Number - { - return $this->cosine()->ceil(); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return $this->cosine()->modulo($modulus); - } - - #[\Override] - public function absolute(): Number - { - return $this->cosine()->absolute(); - } - - #[\Override] - public function power(Number $power): Number - { - return $this->cosine()->power($power); - } - - #[\Override] - public function squareRoot(): Number - { - return $this->cosine()->squareRoot(); - } - - #[\Override] - public function exponential(): Number - { - return $this->cosine()->exponential(); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return $this->cosine()->binaryLogarithm(); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return $this->cosine()->naturalLogarithm(); - } - - #[\Override] - public function commonLogarithm(): Number - { - return $this->cosine()->commonLogarithm(); - } - - #[\Override] - public function signum(): Number - { - return $this->cosine()->signum(); - } - - #[\Override] - public function collapse(): Number - { - return $this; - } - - #[\Override] - public function toString(): string - { - return \sprintf('cos(%s)', $this->degree->toString()); - } - - private function cosine(): Number - { - return Real::of( - \cos( - $this->degree->toRadian()->number()->value(), - ), - ); - } -} diff --git a/src/Geometry/Trigonometry/Sine.php b/src/Geometry/Trigonometry/Sine.php deleted file mode 100644 index 88cb3df..0000000 --- a/src/Geometry/Trigonometry/Sine.php +++ /dev/null @@ -1,189 +0,0 @@ -degree = $degree; - } - - /** - * @psalm-pure - */ - public static function of(Degree $degree): self - { - return new self($degree); - } - - #[\Override] - public function value(): int|float - { - return $this->sine()->value(); - } - - #[\Override] - public function equals(Number $number): bool - { - return $this->sine()->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->sine()->higherThan($number); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return $this->sine()->add($number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return $this->sine()->subtract($number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return $this->sine()->divideBy($number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return $this->sine()->multiplyBy($number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return $this->sine()->roundUp($precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return $this->sine()->roundDown($precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return $this->sine()->roundEven($precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return $this->sine()->roundOdd($precision); - } - - #[\Override] - public function floor(): Number - { - return $this->sine()->floor(); - } - - #[\Override] - public function ceil(): Number - { - return $this->sine()->ceil(); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return $this->sine()->modulo($modulus); - } - - #[\Override] - public function absolute(): Number - { - return $this->sine()->absolute(); - } - - #[\Override] - public function power(Number $power): Number - { - return $this->sine()->power($power); - } - - #[\Override] - public function squareRoot(): Number - { - return $this->sine()->squareRoot(); - } - - #[\Override] - public function exponential(): Number - { - return $this->sine()->exponential(); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return $this->sine()->binaryLogarithm(); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return $this->sine()->naturalLogarithm(); - } - - #[\Override] - public function commonLogarithm(): Number - { - return $this->sine()->commonLogarithm(); - } - - #[\Override] - public function signum(): Number - { - return $this->sine()->signum(); - } - - #[\Override] - public function collapse(): Number - { - return $this; - } - - #[\Override] - public function toString(): string - { - return \sprintf('sin(%s)', $this->degree->toString()); - } - - private function sine(): Number - { - return Real::of( - \sin( - $this->degree->toRadian()->number()->value(), - ), - ); - } -} diff --git a/src/Geometry/Trigonometry/Tangent.php b/src/Geometry/Trigonometry/Tangent.php deleted file mode 100644 index 607ac08..0000000 --- a/src/Geometry/Trigonometry/Tangent.php +++ /dev/null @@ -1,189 +0,0 @@ -degree = $degree; - } - - /** - * @psalm-pure - */ - public static function of(Degree $degree): self - { - return new self($degree); - } - - #[\Override] - public function value(): int|float - { - return $this->tangent()->value(); - } - - #[\Override] - public function equals(Number $number): bool - { - return $this->tangent()->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->tangent()->higherThan($number); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return $this->tangent()->add($number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return $this->tangent()->subtract($number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return $this->tangent()->divideBy($number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return $this->tangent()->multiplyBy($number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return $this->tangent()->roundUp($precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return $this->tangent()->roundDown($precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return $this->tangent()->roundEven($precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return $this->tangent()->roundOdd($precision); - } - - #[\Override] - public function floor(): Number - { - return $this->tangent()->floor(); - } - - #[\Override] - public function ceil(): Number - { - return $this->tangent()->ceil(); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return $this->tangent()->modulo($modulus); - } - - #[\Override] - public function absolute(): Number - { - return $this->tangent()->absolute(); - } - - #[\Override] - public function power(Number $power): Number - { - return $this->tangent()->power($power); - } - - #[\Override] - public function squareRoot(): Number - { - return $this->tangent()->squareRoot(); - } - - #[\Override] - public function exponential(): Number - { - return $this->tangent()->exponential(); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return $this->tangent()->binaryLogarithm(); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return $this->tangent()->naturalLogarithm(); - } - - #[\Override] - public function commonLogarithm(): Number - { - return $this->tangent()->commonLogarithm(); - } - - #[\Override] - public function signum(): Number - { - return $this->tangent()->signum(); - } - - #[\Override] - public function collapse(): Number - { - return $this; - } - - #[\Override] - public function toString(): string - { - return \sprintf('tan(%s)', $this->degree->toString()); - } - - private function tangent(): Number - { - return Real::of( - \tan( - $this->degree->toRadian()->number()->value(), - ), - ); - } -} diff --git a/src/Matrix.php b/src/Matrix.php index 6daafea..19d0fa2 100644 --- a/src/Matrix.php +++ b/src/Matrix.php @@ -15,9 +15,6 @@ Exception\LogicException, Matrix\Dimension, Algebra\Number, - Algebra\Integer, - Algebra\Value, - Algebra\Real, }; use Innmind\Immutable\Sequence; @@ -42,7 +39,7 @@ private function __construct(Sequence $rows) static fn() => throw new LogicException('Empty matrix'), ); $_ = $rest->foreach( - static fn($row) => match ($row->dimension()->equals($first->dimension())) { + static fn($row) => match ($row->dimension() === $first->dimension()) { true => null, // as expected false => throw new VectorsMustMeOfTheSameDimension, }, @@ -52,7 +49,7 @@ private function __construct(Sequence $rows) /** @psalm-suppress InvalidArgument There is always at least one row */ $this->dimension = Dimension::of( - Integer::positive($this->rows->size()), + $this->rows->size(), $first->dimension(), ); $this->columns = $this->buildColumns(); @@ -67,7 +64,7 @@ public static function of(array $values): self { $numerize = static fn(int|float|Number $number): Number => match (true) { $number instanceof Number => $number, - default => Real::of($number), + default => Number::of($number), }; return new self( @@ -111,7 +108,7 @@ public static function fromColumns(Sequence $columns): self public static function initialize(Dimension $dimension, Number $value): self { return new self( - Range::of(Integer::of(1), $dimension->rows())->map( + Range::of(1, $dimension->rows())->map( static fn() => RowVector::initialize($dimension->columns(), $value), ), ); @@ -194,7 +191,7 @@ public function dot(self $matrix): self public function isSquare(): bool { - return $this->dimension->rows()->equals($this->dimension->columns()); + return $this->dimension->rows() === $this->dimension->columns(); } public function diagonal(): self @@ -209,9 +206,9 @@ public function diagonal(): self static function(Sequence $rows, RowVector $row): Sequence { return ($rows)(RowVector::ofSequence( Range::until($row->dimension())->map( - static fn($i) => match ($i->value()) { - $rows->size() => $row->get($i->value()), - default => Value::zero, + static fn($i) => match ($i) { + $rows->size() => $row->get($i), + default => Number::zero(), }, ), )); @@ -234,9 +231,9 @@ static function(Sequence $rows, RowVector $row): Sequence { /** @psalm-suppress InvalidArgument Value is a subtype of Number */ return ($rows)(RowVector::ofSequence( Range::until($row->dimension())->map( - static fn($i) => match ($i->value()) { - $rows->size() => Value::one, - default => Value::zero, + static fn($i) => match ($i) { + $rows->size() => Number::one(), + default => Number::zero(), }, ), )); @@ -266,29 +263,17 @@ public function isSymmetric(): bool public function isAntisymmetric(): bool { return $this - ->multiplyBy(Integer::of(-1)) + ->multiplyBy(Number::of(-1)) ->equals($this->transpose()); } public function isInRowEchelonForm(): bool { $leadingZeros = $this->rows->map( - static function(RowVector $row): int { - $isLeadingZero = true; - - return $row - ->toSequence() - ->map(static function($number) use (&$isLeadingZero): bool { - if ($isLeadingZero && !$number->equals(Value::zero)) { - $isLeadingZero = false; - } - - /** @var bool */ - return $isLeadingZero; - }) - ->filter(static fn($isLeadingZero) => $isLeadingZero) - ->size(); - }, + static fn(RowVector $row) => $row + ->toSequence() + ->takeWhile(static fn($number) => $number->equals(Number::zero())) + ->size(), ); return $leadingZeros->equals($leadingZeros->sort(static fn($a, $b) => $a <=> $b)) && @@ -327,7 +312,7 @@ public function inverse(): self $matrix ->columns() ->takeEnd( - $this->dimension->columns()->value(), + $this->dimension->columns(), ), ); } @@ -340,7 +325,7 @@ private function buildColumns(): Sequence /** @psalm-suppress ArgumentTypeCoercion */ return Range::until($this->dimension->columns()) ->map(fn($column) => $this->rows->map( - static fn($row) => $row->get($column->value()), + static fn($row) => $row->get($column), )) ->map(ColumnVector::ofSequence(...)); } @@ -385,7 +370,7 @@ static function(Sequence $rows, int $index): Sequence { ->map(static fn($row) => $row->multiplyBy( RowVector::initialize( $row->dimension(), - Value::one->divideBy($row->lead()), + Number::one()->divideBy($row->lead()), ), )); }, diff --git a/src/Matrix/ColumnVector.php b/src/Matrix/ColumnVector.php index 204a6b7..2d5827a 100644 --- a/src/Matrix/ColumnVector.php +++ b/src/Matrix/ColumnVector.php @@ -6,7 +6,6 @@ use Innmind\Math\{ Matrix, Algebra\Number, - Algebra\Integer, Exception\LogicException, }; use Innmind\Immutable\{ @@ -19,11 +18,8 @@ */ final class ColumnVector { - private Vector $vector; - - private function __construct(Vector $vector) + private function __construct(private Vector $vector) { - $this->vector = $vector; } /** @@ -36,8 +32,10 @@ public static function of(Number $number, Number ...$numbers): self /** * @psalm-pure + * + * @param int<1, max> $dimension */ - public static function initialize(Integer\Positive $dimension, Number $value): self + public static function initialize(int $dimension, Number $value): self { return new self(Vector::initialize($dimension, $value)); } @@ -54,7 +52,10 @@ public static function ofSequence(Sequence $numbers): self return new self(Vector::ofSequence($numbers)); } - public function dimension(): Integer\Positive + /** + * @return int<1, max> + */ + public function dimension(): int { return $this->vector->dimension(); } @@ -77,7 +78,10 @@ public function matrix(RowVector $row): Matrix ->vector ->toSequence() ->map(static fn($number) => $row->toSequence()->map( - static fn($rowNumber) => $number->multiplyBy($rowNumber)->collapse(), + static fn($rowNumber) => $number + ->multiplyBy($rowNumber) + ->optimize() + ->memoize(), )) ->map(RowVector::ofSequence(...)), ); diff --git a/src/Matrix/Dimension.php b/src/Matrix/Dimension.php index f2670bb..97c5fb5 100644 --- a/src/Matrix/Dimension.php +++ b/src/Matrix/Dimension.php @@ -3,48 +3,56 @@ namespace Innmind\Math\Matrix; -use Innmind\Math\Algebra\Integer; - /** * @psalm-immutable */ final class Dimension { - private Integer\Positive $rows; - private Integer\Positive $columns; - - private function __construct(Integer\Positive $rows, Integer\Positive $columns) - { - $this->rows = $rows; - $this->columns = $columns; + /** + * @param int<1, max> $rows + * @param int<1, max> $columns + */ + private function __construct( + private int $rows, + private int $columns, + ) { } /** * @psalm-pure + * + * @param int<1, max> $rows + * @param int<1, max> $columns */ - public static function of(Integer\Positive $rows, Integer\Positive $columns): self + public static function of(int $rows, int $columns): self { return new self($rows, $columns); } - public function rows(): Integer\Positive + /** + * @return int<1, max> + */ + public function rows(): int { return $this->rows; } - public function columns(): Integer\Positive + /** + * @return int<1, max> + */ + public function columns(): int { return $this->columns; } public function equals(self $dimension): bool { - return $this->rows->equals($dimension->rows()) && - $this->columns->equals($dimension->columns()); + return $this->rows === $dimension->rows() && + $this->columns === $dimension->columns(); } public function toString(): string { - return \sprintf('%s x %s', $this->rows->toString(), $this->columns->toString()); + return \sprintf('%s x %s', $this->rows, $this->columns); } } diff --git a/src/Matrix/RowVector.php b/src/Matrix/RowVector.php index fc41faf..fce6b16 100644 --- a/src/Matrix/RowVector.php +++ b/src/Matrix/RowVector.php @@ -6,7 +6,6 @@ use Innmind\Math\{ Matrix, Algebra\Number, - Algebra\Integer, Exception\LogicException, }; use Innmind\Immutable\{ @@ -19,11 +18,8 @@ */ final class RowVector { - private Vector $vector; - - private function __construct(Vector $vector) + private function __construct(private Vector $vector) { - $this->vector = $vector; } /** @@ -36,8 +32,10 @@ public static function of(Number $number, Number ...$numbers): self /** * @psalm-pure + * + * @param int<1, max> $dimension */ - public static function initialize(Integer\Positive $dimension, Number $value): self + public static function initialize(int $dimension, Number $value): self { return new self(Vector::initialize($dimension, $value)); } @@ -54,7 +52,10 @@ public static function ofSequence(Sequence $numbers): self return new self(Vector::ofSequence($numbers)); } - public function dimension(): Integer\Positive + /** + * @return int<1, max> + */ + public function dimension(): int { return $this->vector->dimension(); } @@ -73,7 +74,10 @@ public function matrix(ColumnVector $column): Matrix $column ->toSequence() ->map(fn($number) => $this->vector->toSequence()->map( - static fn($rowNumber) => $rowNumber->multiplyBy($number)->collapse(), + static fn($rowNumber) => $rowNumber + ->multiplyBy($number) + ->optimize() + ->memoize(), )) ->map(self::ofSequence(...)), ); diff --git a/src/Matrix/Vector.php b/src/Matrix/Vector.php index 14d0954..9d29d5f 100644 --- a/src/Matrix/Vector.php +++ b/src/Matrix/Vector.php @@ -7,10 +7,7 @@ Exception\VectorsMustMeOfTheSameDimension, Exception\LogicException, Algebra\Number, - Algebra\Integer, - Algebra\Value, - Monoid\Addition, - Range, + Monoid\Algebra, }; use Innmind\Immutable\{ Sequence, @@ -22,18 +19,11 @@ */ final class Vector { - /** @var Sequence */ - private Sequence $numbers; - private Integer\Positive $dimension; - /** * @param Sequence $numbers */ - private function __construct(Sequence $numbers) + private function __construct(private Sequence $numbers) { - $this->numbers = $numbers; - /** @psalm-suppress InvalidArgument There is always at least one number in the sequence */ - $this->dimension = Integer::positive($this->numbers->size()); } /** @@ -46,11 +36,13 @@ public static function of(Number $number, Number ...$numbers): self /** * @psalm-pure + * + * @param int<1, max> $dimension */ - public static function initialize(Integer\Positive $dimension, Number $value): self + public static function initialize(int $dimension, Number $value): self { return new self( - Range::until($dimension)->map(static fn() => $value), + Sequence::of($value)->pad($dimension, $value), ); } @@ -70,9 +62,13 @@ public static function ofSequence(Sequence $numbers): self return new self($numbers); } - public function dimension(): Integer\Positive + /** + * @return int<1, max> + */ + public function dimension(): int { - return $this->dimension; + /** @var int<1, max> There's always at least one element */ + return $this->numbers->size(); } /** @@ -80,20 +76,25 @@ public function dimension(): Integer\Positive */ public function dot(self $vector): Number { - if (!$this->dimension()->equals($vector->dimension())) { + if ($this->dimension() !== $vector->dimension()) { throw new VectorsMustMeOfTheSameDimension; } return $this ->numbers ->zip($vector->numbers) - ->map(static fn($pair) => $pair[0]->multiplyBy($pair[1])->collapse()) - ->fold(new Addition); + ->map( + static fn($pair) => $pair[0] + ->multiplyBy($pair[1]) + ->optimize() + ->memoize(), + ) + ->fold(Algebra::addition); } public function multiplyBy(self $vector): self { - if (!$this->dimension()->equals($vector->dimension())) { + if ($this->dimension() !== $vector->dimension()) { throw new VectorsMustMeOfTheSameDimension; } @@ -101,13 +102,18 @@ public function multiplyBy(self $vector): self $this ->numbers ->zip($vector->numbers) - ->map(static fn($pair) => $pair[0]->multiplyBy($pair[1])->collapse()), + ->map( + static fn($pair) => $pair[0] + ->multiplyBy($pair[1]) + ->optimize() + ->memoize(), + ), ); } public function divideBy(self $vector): self { - if (!$this->dimension()->equals($vector->dimension())) { + if ($this->dimension() !== $vector->dimension()) { throw new VectorsMustMeOfTheSameDimension; } @@ -121,7 +127,7 @@ public function divideBy(self $vector): self public function subtract(self $vector): self { - if (!$this->dimension()->equals($vector->dimension())) { + if ($this->dimension() !== $vector->dimension()) { throw new VectorsMustMeOfTheSameDimension; } @@ -135,7 +141,7 @@ public function subtract(self $vector): self public function add(self $vector): self { - if (!$this->dimension()->equals($vector->dimension())) { + if ($this->dimension() !== $vector->dimension()) { throw new VectorsMustMeOfTheSameDimension; } @@ -156,7 +162,7 @@ public function power(Number $power): self public function sum(): Number { - return $this->numbers->fold(new Addition); + return $this->numbers->fold(Algebra::addition); } /** @@ -191,7 +197,7 @@ public function reduce($carry, callable $reducer) } /** - * @param 0|positive-int $position + * @param int<0, max> $position */ public function get(int $position): Number { @@ -203,7 +209,7 @@ public function get(int $position): Number public function equals(self $vector): bool { - if (!$this->dimension()->equals($vector->dimension())) { + if ($this->dimension() !== $vector->dimension()) { return false; } @@ -220,10 +226,10 @@ public function lead(): Number { return $this ->numbers - ->find(static fn($number) => !$number->equals(Value::zero)) + ->find(static fn($number) => !$number->equals(Number::zero())) ->match( static fn($lead) => $lead, - static fn() => Value::zero, + static fn() => Number::zero(), ); } diff --git a/src/Monoid/Addition.php b/src/Monoid/Addition.php deleted file mode 100644 index a9e1c4d..0000000 --- a/src/Monoid/Addition.php +++ /dev/null @@ -1,29 +0,0 @@ - - */ -final class Addition implements Monoid -{ - #[\Override] - public function identity(): Number - { - return Value::zero; - } - - #[\Override] - public function combine(mixed $a, mixed $b): Number - { - return $a->add($b); - } -} diff --git a/src/Monoid/Algebra.php b/src/Monoid/Algebra.php new file mode 100644 index 0000000..adf7e4d --- /dev/null +++ b/src/Monoid/Algebra.php @@ -0,0 +1,35 @@ + + */ +enum Algebra implements Monoid +{ + case addition; + case multiplication; + + #[\Override] + public function identity(): Number + { + return match ($this) { + self::addition => Number::zero(), + self::multiplication => Number::one(), + }; + } + + #[\Override] + public function combine(mixed $a, mixed $b): Number + { + return match ($this) { + self::addition => $a->add($b), + self::multiplication => $a->multiplyBy($b), + }; + } +} diff --git a/src/Monoid/Multiplication.php b/src/Monoid/Multiplication.php deleted file mode 100644 index 87a67c0..0000000 --- a/src/Monoid/Multiplication.php +++ /dev/null @@ -1,29 +0,0 @@ - - */ -final class Multiplication implements Monoid -{ - #[\Override] - public function identity(): Number - { - return Value::one; - } - - #[\Override] - public function combine(mixed $a, mixed $b): Number - { - return $a->multiplyBy($b); - } -} diff --git a/src/Polynom/Degree.php b/src/Polynom/Degree.php index 225f21b..46abb0a 100644 --- a/src/Polynom/Degree.php +++ b/src/Polynom/Degree.php @@ -3,25 +3,20 @@ namespace Innmind\Math\Polynom; -use Innmind\Math\{ - Algebra\Number, - Algebra\Integer, - Algebra\Operation, - Algebra\Value, -}; +use Innmind\Math\Algebra\Number; /** * @psalm-immutable */ final class Degree { - private Integer\Positive $degree; - private Number $coeff; - - private function __construct(Integer\Positive $degree, Number $coeff) - { - $this->degree = $degree; - $this->coeff = $coeff; + /** + * @param int<1, max> $degree + */ + private function __construct( + private int $degree, + private Number $coeff, + ) { } /** @@ -29,18 +24,23 @@ private function __construct(Integer\Positive $degree, Number $coeff) */ public function __invoke(Number $x): Number { - return $this->coeff->multiplyBy($x->power($this->degree)); + return $this->coeff->multiplyBy($x->power(Number::of($this->degree))); } /** * @psalm-pure + * + * @param int<1, max> $degree */ - public static function of(Integer\Positive $degree, Number $coeff): self + public static function of(int $degree, Number $coeff): self { return new self($degree, $coeff); } - public function degree(): Integer\Positive + /** + * @return int<1, max> + */ + public function degree(): int { return $this->degree; } @@ -53,33 +53,35 @@ public function coeff(): Number public function primitive(): self { return new self( - $this->degree->increment(), - $this->coeff->divideBy($this->degree->add(Value::one)), + $this->degree + 1, + $this->coeff->divideBy(Number::of($this->degree)->add(Number::one())), ); } public function derivative(): self { - /** @psalm-suppress ArgumentTypeCoercion It must throw if we decrement below 1 as it means there is a bug somewhere */ + if ($this->degree === 1) { + throw new \LogicException('Cannot derivate a degree of 1'); + } + return new self( - $this->degree->decrement(), - $this->coeff->multiplyBy($this->degree), + $this->degree - 1, + $this->coeff->multiplyBy(Number::of($this->degree)), ); } public function toString(): string { - $coeff = $this->coeff instanceof Operation ? - '('.$this->coeff->toString().')' : $this->coeff->toString(); + $coeff = $this->coeff->format(); - if ($this->degree->equals(Value::one)) { + if ($this->degree === 1) { return $coeff.'x'; } return \sprintf( '%sx^%s', $coeff, - $this->degree->toString(), + $this->degree, ); } } diff --git a/src/Polynom/Integral.php b/src/Polynom/Integral.php index a4b0b27..a6536e8 100644 --- a/src/Polynom/Integral.php +++ b/src/Polynom/Integral.php @@ -10,11 +10,8 @@ */ final class Integral { - private Polynom $polynom; - - private function __construct(Polynom $polynom) + private function __construct(private Polynom $polynom) { - $this->polynom = $polynom; } public function __invoke(Number $a, Number $b): Number diff --git a/src/Polynom/Polynom.php b/src/Polynom/Polynom.php index 302842f..74c90a1 100644 --- a/src/Polynom/Polynom.php +++ b/src/Polynom/Polynom.php @@ -3,13 +3,9 @@ namespace Innmind\Math\Polynom; -use function Innmind\Math\desc; -use Innmind\Math\Algebra\{ - Number, - Integer, - Operation, - Value, - Addition, +use Innmind\Math\{ + Algebra\Number, + Monoid\Algebra, }; use Innmind\Immutable\{ Sequence, @@ -22,17 +18,13 @@ */ final class Polynom { - private Number $intercept; - /** @var Sequence */ - private Sequence $degrees; - /** * @param Sequence $degrees */ - private function __construct(Number $intercept, Sequence $degrees) - { - $this->intercept = $intercept; - $this->degrees = $degrees; + private function __construct( + private Number $intercept, + private Sequence $degrees, + ) { } /** @@ -40,12 +32,11 @@ private function __construct(Number $intercept, Sequence $degrees) */ public function __invoke(Number $x): Number { - $values = $this + return $this ->degrees ->map(static fn($degree) => $degree($x)) - ->toList(); - - return Addition::of($this->intercept, ...$values); + ->prepend(Sequence::of($this->intercept)) + ->fold(Algebra::addition); } /** @@ -56,7 +47,7 @@ public static function zero(): self /** @var Sequence */ $degrees = Sequence::of(); - return new self(Value::zero, $degrees); + return new self(Number::zero(), $degrees); } /** @@ -72,14 +63,16 @@ public static function interceptAt(Number $intercept): self /** * Create a new polynom with this added degree + * + * @param int<1, max> $degree */ - public function withDegree(Integer\Positive $degree, Number $coeff): self + public function withDegree(int $degree, Number $coeff): self { return new self( $this->intercept, $this ->degrees - ->filter(static fn($known) => !$known->degree()->equals($degree)) + ->exclude(static fn($known) => $known->degree() === $degree) ->add(Degree::of($degree, $coeff)), ); } @@ -95,14 +88,14 @@ public function intercept(): Number /** * Return the given degree * - * @param positive-int $degree + * @param int<1, max> $degree * * @return Maybe */ public function degree(int $degree): Maybe { return $this->degrees->find( - static fn($known) => $known->degree()->equals(Integer::of($degree)), + static fn($known) => $known->degree() === $degree, ); } @@ -131,15 +124,15 @@ public function tangent(Number $x, ?Number $limit = null): Tangent public function primitive(): self { $primitive = new self( - Value::zero, + Number::zero(), $this ->degrees ->map(static fn($degree) => $degree->primitive()), ); - if (!$this->intercept->equals(Value::zero)) { + if (!$this->intercept->equals(Number::zero())) { $primitive = $primitive->withDegree( - Integer::positive(1), + 1, $this->intercept, ); } @@ -151,15 +144,15 @@ public function derivative(): self { [$intercept, $degrees] = $this ->degrees - ->find(static fn($degree) => $degree->degree()->equals(Value::one)) + ->find(static fn($degree) => $degree->degree() === 1) ->match( fn($degree) => [ $degree->coeff(), - $this->degrees->filter( - static fn($degree) => !$degree->degree()->equals(Value::one), + $this->degrees->exclude( + static fn($degree) => $degree->degree() === 1, ), ], - fn() => [Value::zero, $this->degrees], + fn() => [Number::zero(), $this->degrees], ); return new self( @@ -177,13 +170,12 @@ public function toString(): string { $degrees = $this ->degrees - ->sort(static fn($a, $b) => desc($a->degree(), $b->degree())) + ->sort(static fn($a, $b) => ($a->degree() <=> $b->degree()) * -1) ->map(static fn($degree) => $degree->toString()); $polynom = Str::of(' + ')->join($degrees); - if (!$this->intercept->equals(Value::zero)) { - $intercept = $this->intercept instanceof Operation ? - '('.$this->intercept->toString().')' : $this->intercept->toString(); + if (!$this->intercept->equals(Number::zero())) { + $intercept = $this->intercept->format(); $polynom = $polynom ->append(' + ') diff --git a/src/Polynom/Tangent.php b/src/Polynom/Tangent.php index 4c6a07e..e6f072a 100644 --- a/src/Polynom/Tangent.php +++ b/src/Polynom/Tangent.php @@ -3,10 +3,7 @@ namespace Innmind\Math\Polynom; -use Innmind\Math\Algebra\{ - Number, - Real, -}; +use Innmind\Math\Algebra\Number; /** * @psalm-immutable @@ -63,6 +60,6 @@ public function abscissa(): Number */ public static function limit(): Number { - return Real::of(0.000000000001); + return Number::of(0.000000000001); } } diff --git a/src/Probabilities/BinomialDistribution.php b/src/Probabilities/BinomialDistribution.php index 618ea59..aabdbb1 100644 --- a/src/Probabilities/BinomialDistribution.php +++ b/src/Probabilities/BinomialDistribution.php @@ -5,8 +5,7 @@ use Innmind\Math\Algebra\{ Number, - Integer, - Value, + Factorial, }; /** @@ -14,28 +13,28 @@ */ final class BinomialDistribution { - private Number $probability; - - private function __construct(Number $probability) + private function __construct(private Number $probability) { - $this->probability = $probability; } - public function __invoke(Integer $trials, Integer $success): Number + public function __invoke(int $trials, int $success): Number { - /** @var Integer */ - $errors = $trials->subtract($success)->collapse(); - $coefficient = $trials - ->factorial() + $errors = $trials - $success; + $coefficient = Factorial::of($trials) + ->number() ->divideBy( - $success->factorial()->multiplyBy( - $errors->factorial(), + Factorial::of($success)->number()->multiplyBy( + Factorial::of($errors)->number(), ), ); return $coefficient - ->multiplyBy($this->probability->power($success)) - ->multiplyBy(Value::one->subtract($this->probability)->power($errors)); + ->multiplyBy($this->probability->power(Number::of($success))) + ->multiplyBy( + Number::one() + ->subtract($this->probability) + ->power(Number::of($errors)), + ); } /** diff --git a/src/Probabilities/Variance.php b/src/Probabilities/Variance.php index c1e49c2..7fef678 100644 --- a/src/Probabilities/Variance.php +++ b/src/Probabilities/Variance.php @@ -7,7 +7,6 @@ Regression\Dataset, Matrix\ColumnVector, Algebra\Number, - Algebra\Value, }; /** @@ -28,7 +27,7 @@ private function __construct(Dataset $dataset) $expectation, ), ) - ->power(Value::two) + ->power(Number::two()) ->multiplyBy($dataset->ordinates()) ->sum(); } diff --git a/src/Quantile/Quantile.php b/src/Quantile/Quantile.php index 47d94ec..2f1d1ce 100644 --- a/src/Quantile/Quantile.php +++ b/src/Quantile/Quantile.php @@ -6,10 +6,6 @@ use function Innmind\Math\asc; use Innmind\Math\{ Algebra\Number, - Algebra\Real, - Algebra\Value, - Algebra\Integer, - Algebra\Addition, Statistics\Mean, Statistics\Median, Exception\LogicException, @@ -64,7 +60,7 @@ public function max(): Quartile public function mean(): Number { /** @psalm-suppress InvalidArgument At least one value present */ - return Mean::of(...$this->values->toList()); + return Mean::of(...$this->values->toList())->result(); } /** @@ -73,17 +69,17 @@ public function mean(): Number public function median(): Quartile { /** @psalm-suppress InvalidArgument At least one value present */ - return Quartile::of(Median::of(...$this->values->toList())); + return Quartile::of(Median::of(...$this->values->toList())->result()); } public function firstQuartile(): Quartile { - return Quartile::of($this->buildQuartile(Real::of(0.25))); + return Quartile::of($this->buildQuartile(Number::of(0.25))); } public function thirdQuartile(): Quartile { - return Quartile::of($this->buildQuartile(Real::of(0.75))); + return Quartile::of($this->buildQuartile(Number::of(0.75))); } /** @@ -92,22 +88,22 @@ public function thirdQuartile(): Quartile private function buildQuartile(Number $percentage): Number { /** @var positive-int */ - $index = (int) Integer::of($this->values->size()) + $index = (int) Number::of($this->values->size()) ->multiplyBy($percentage) ->roundUp() - ->collapse() + ->optimize() ->value(); return $this->values->match( fn($first, $rest) => $rest->match( fn($second, $rest) => match ($rest->empty()) { - true => $first->add($second)->divideBy(Value::two), + true => $first->add($second)->divideBy(Number::two()), false => Maybe::all( $this->values->get($index), $this->values->get($index - 1), ) - ->map(Addition::of(...)) - ->map(static fn($sum) => $sum->divideBy(Value::two)) + ->map(static fn(Number $a, Number $b) => $a->add($b)) + ->map(static fn($sum) => $sum->divideBy(Number::two())) ->match( static fn($quartile) => $quartile, fn() => throw new LogicException("Operation not working for size {$this->values->size()}"), diff --git a/src/Quantile/Quartile.php b/src/Quantile/Quartile.php index 57250ea..94e6815 100644 --- a/src/Quantile/Quartile.php +++ b/src/Quantile/Quartile.php @@ -11,11 +11,8 @@ */ final class Quartile { - private Number $value; - - private function __construct(Number $value) + private function __construct(private Number $value) { - $this->value = $value; } /** diff --git a/src/Range.php b/src/Range.php index f8e7029..d77eb26 100644 --- a/src/Range.php +++ b/src/Range.php @@ -3,7 +3,6 @@ namespace Innmind\Math; -use Innmind\Math\Algebra\Integer; use Innmind\Immutable\Sequence; final class Range @@ -11,33 +10,36 @@ final class Range /** * @psalm-pure * - * @return Sequence + * @param int<1, max> $max + * + * @return Sequence */ - public static function until(Integer\Positive $max): Sequence + public static function until(int $max): Sequence { - return self::of(Integer::of(0), $max->decrement()); + return self::of(0, $max - 1); } /** * @psalm-pure * - * @return Sequence + * @return Sequence */ - public static function of(Integer $min, Integer $max): Sequence + public static function of(int $min, int $max): Sequence { - return Sequence::of(...\range($min->value(), $max->value())) - ->map(Integer::of(...)); + return Sequence::of(...\range($min, $max)); } /** * @psalm-pure * - * @return Sequence + * @param int<1, max> $min + * @param int<1, max> $max + * + * @return Sequence> */ - public static function ofPositive(Integer\Positive $min, Integer\Positive $max): Sequence + public static function ofPositive(int $min, int $max): Sequence { - /** @psalm-suppress ArgumentTypeCoercion */ - return Sequence::of(...\range($min->value(), $max->value())) - ->map(Integer::positive(...)); + /** @var Sequence> */ + return Sequence::of(...\range($min, $max)); } } diff --git a/src/Regression/Dataset.php b/src/Regression/Dataset.php index 7841266..0cbf406 100644 --- a/src/Regression/Dataset.php +++ b/src/Regression/Dataset.php @@ -8,8 +8,6 @@ Matrix\Dimension, Matrix\ColumnVector, Algebra\Number, - Algebra\Real, - Algebra\Integer, }; use Innmind\Immutable\Sequence; @@ -18,15 +16,11 @@ */ final class Dataset { - /** @var Sequence */ - private Sequence $points; - /** * @param Sequence $points */ - private function __construct(Sequence $points) + private function __construct(private Sequence $points) { - $this->points = $points; } /** @@ -38,7 +32,7 @@ public static function of(array $values): self { $numerize = static fn(int|float|Number $number): Number => match (true) { $number instanceof Number => $number, - default => Real::of($number), + default => Number::of($number), }; return new self(Sequence::of(...\array_map( @@ -76,8 +70,8 @@ public function dimension(): Dimension { /** @psalm-suppress InvalidArgument There is always at least one point */ return Dimension::of( - Integer::positive($this->points->size()), - Integer::positive(2), + $this->points->size(), + 2, ); } } diff --git a/src/Regression/Dataset/Point.php b/src/Regression/Dataset/Point.php index 201cde4..ffb7505 100644 --- a/src/Regression/Dataset/Point.php +++ b/src/Regression/Dataset/Point.php @@ -10,13 +10,10 @@ */ final class Point { - private Number $abscissa; - private Number $ordinate; - - private function __construct(Number $abscissa, Number $ordinate) - { - $this->abscissa = $abscissa; - $this->ordinate = $ordinate; + private function __construct( + private Number $abscissa, + private Number $ordinate, + ) { } /** diff --git a/src/Regression/LinearRegression.php b/src/Regression/LinearRegression.php index 6e09068..38e604f 100644 --- a/src/Regression/LinearRegression.php +++ b/src/Regression/LinearRegression.php @@ -6,11 +6,7 @@ use Innmind\Math\{ Polynom\Polynom, Algebra\Number, - Algebra\Integer, - Algebra\Value, - Algebra\Division, - Algebra\Subtraction, - Monoid, + Monoid\Algebra, }; /** @@ -26,7 +22,7 @@ private function __construct(Dataset $data) { [$slope, $intercept] = $this->compute($data); $this->polynom = Polynom::interceptAt($intercept)->withDegree( - Integer::positive(1), + 1, $slope, ); $this->slope = $slope; @@ -79,7 +75,7 @@ public function rootMeanSquareDeviation(): Number */ private function compute(Dataset $data): array { - $dimension = $data->dimension()->rows(); + $dimension = Number::of($data->dimension()->rows()); $xSum = $data->abscissas()->sum(); $ySum = $data->ordinates()->sum(); @@ -87,29 +83,23 @@ private function compute(Dataset $data): array ->abscissas() ->toSequence() ->map(static fn($x) => $x->multiplyBy($x)) - ->fold(new Monoid\Addition); + ->fold(Algebra::addition); $xySum = $data ->points() ->map(static fn($point) => $point->abscissa()->multiplyBy($point->ordinate())) - ->fold(new Monoid\Addition); + ->fold(Algebra::addition); - $slope = Division::of( - Subtraction::of( - $dimension->multiplyBy($xySum), - $xSum->multiplyBy($ySum), - ), - Subtraction::of( - $dimension->multiplyBy($xxSum), - $xSum->power(Value::two), - ), - ); - $intercept = Division::of( - Subtraction::of( - $ySum, - $slope->multiplyBy($xSum), - ), - $dimension, - ); + $slope = $dimension + ->multiplyBy($xySum) + ->subtract($xSum->multiplyBy($ySum)) + ->divideBy( + $dimension + ->multiplyBy($xxSum) + ->subtract($xSum->power(Number::two())), + ); + $intercept = $ySum + ->subtract($slope->multiplyBy($xSum)) + ->divideBy($dimension); return [$slope, $intercept]; } @@ -123,9 +113,9 @@ private function buildRmsd(Dataset $dataset, Polynom $interpolate): Number return $values ->subtract($estimated) - ->power(Value::two) + ->power(Number::two()) ->sum() - ->divideBy($values->dimension()) + ->divideBy(Number::of($values->dimension())) ->squareRoot(); } } diff --git a/src/Regression/PolynomialRegression.php b/src/Regression/PolynomialRegression.php index fa49872..16ba34e 100644 --- a/src/Regression/PolynomialRegression.php +++ b/src/Regression/PolynomialRegression.php @@ -5,8 +5,6 @@ use Innmind\Math\{ Algebra\Number, - Algebra\Integer, - Algebra\Value, Polynom\Polynom, Matrix, Matrix\RowVector, @@ -23,7 +21,10 @@ final class PolynomialRegression private Polynom $polynom; private Number $deviation; - private function __construct(Dataset $dataset, Integer\Positive $degree) + /** + * @param int<1, max> $degree + */ + private function __construct(Dataset $dataset, int $degree) { $matrix = $this->buildMatrix($dataset, $degree); $vector = $this->buildVector($dataset); @@ -40,9 +41,9 @@ private function __construct(Dataset $dataset, Integer\Positive $degree) static fn() => throw new LogicException('Empty matrix'), ); - $this->polynom = Range::ofPositive(Integer::positive(1), $degree) + $this->polynom = Range::ofPositive(1, $degree) ->zip($coefficients->toSequence()->drop(1)) - ->filter(static fn($pair) => !$pair[1]->equals(Value::zero)) + ->filter(static fn($pair) => !$pair[1]->equals(Number::zero())) ->reduce( Polynom::interceptAt($coefficients->get(0)), static fn(Polynom $polynom, $pair) => $polynom->withDegree( @@ -61,8 +62,10 @@ public function __invoke(Number $x): Number /** * @psalm-pure + * + * @param int<1, max> $degree */ - public static function of(Dataset $data, Integer\Positive $degree): self + public static function of(Dataset $data, int $degree): self { return new self($data, $degree); } @@ -77,10 +80,12 @@ public function rootMeanSquareDeviation(): Number return $this->deviation; } - private function buildMatrix(Dataset $dataset, Integer\Positive $degree): Matrix + /** + * @param int<1, max> $degree + */ + private function buildMatrix(Dataset $dataset, int $degree): Matrix { - /** @psalm-suppress InvalidArgument */ - $powers = RowVector::ofSequence(Range::of(Integer::of(0), $degree)); + $powers = RowVector::ofSequence(Range::of(0, $degree)->map(Number::of(...))); return Matrix::fromRows( $dataset @@ -106,9 +111,9 @@ private function buildRmsd(Dataset $dataset, Polynom $interpolate): Number return $values ->subtract($estimated) - ->power(Value::two) + ->power(Number::two()) ->sum() - ->divideBy($values->dimension()) + ->divideBy(Number::of($values->dimension())) ->squareRoot(); } } diff --git a/src/Statistics/Frequence.php b/src/Statistics/Frequence.php index 3a07c99..05912ae 100644 --- a/src/Statistics/Frequence.php +++ b/src/Statistics/Frequence.php @@ -3,11 +3,7 @@ namespace Innmind\Math\Statistics; -use Innmind\Math\Algebra\{ - Number, - Division, - Integer, -}; +use Innmind\Math\Algebra\Number; use Innmind\Immutable\Sequence; /** @@ -15,17 +11,11 @@ */ final class Frequence { - /** @var Sequence */ - private Sequence $values; - private Integer $size; - /** - * @no-named-arguments + * @param Sequence $values */ - private function __construct(Number ...$values) + private function __construct(private Sequence $values) { - $this->values = Sequence::of(...$values); - $this->size = Integer::of($this->values->size()); } public function __invoke(Number $number): Number @@ -35,7 +25,7 @@ public function __invoke(Number $number): Number ->filter(static fn($value) => $value->equals($number)) ->size(); - return Division::of(Integer::of($frequence), $this->size); + return Number::of($frequence)->divideBy(Number::of($this->size())); } /** @@ -44,11 +34,14 @@ public function __invoke(Number $number): Number */ public static function of(Number ...$values): self { - return new self(...$values); + return new self(Sequence::of(...$values)); } - public function size(): Integer + /** + * @return int<0, max> + */ + public function size(): int { - return $this->size; + return $this->values->size(); } } diff --git a/src/Statistics/Mean.php b/src/Statistics/Mean.php index 31372ec..bbe5705 100644 --- a/src/Statistics/Mean.php +++ b/src/Statistics/Mean.php @@ -3,25 +3,25 @@ namespace Innmind\Math\Statistics; -use Innmind\Math\Algebra\{ - Number, - Integer, - Addition, -}; -use Innmind\Immutable\Sequence; +use Innmind\Math\Algebra\Number; /** * @psalm-immutable */ -final class Mean implements Number +final class Mean { private Number $result; private function __construct(Number $first, Number ...$values) { - $sequence = Sequence::of($first, ...$values); - $sum = Addition::of($first, ...$values); - $this->result = $sum->divideBy(Integer::of($sequence->size())); + $sum = \array_reduce( + $values, + static fn(Number $a, Number $b) => $a->add($b), + $first, + ); + $this->result = $sum->divideBy(Number::of( + \count($values) + 1, + )); } /** @@ -36,148 +36,4 @@ public function result(): Number { return $this->result; } - - #[\Override] - public function value(): int|float - { - return $this->result->value(); - } - - #[\Override] - public function equals(Number $number): bool - { - return $this->result->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->result->higherThan($number); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return $this->result->add($number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return $this->result->subtract($number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return $this->result->divideBy($number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return $this->result->multiplyBy($number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return $this->result->roundUp($precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return $this->result->roundDown($precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return $this->result->roundEven($precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return $this->result->roundOdd($precision); - } - - #[\Override] - public function floor(): Number - { - return $this->result->floor(); - } - - #[\Override] - public function ceil(): Number - { - return $this->result->ceil(); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return $this->result->modulo($modulus); - } - - #[\Override] - public function absolute(): Number - { - return $this->result->absolute(); - } - - #[\Override] - public function power(Number $power): Number - { - return $this->result->power($power); - } - - #[\Override] - public function squareRoot(): Number - { - return $this->result->squareRoot(); - } - - #[\Override] - public function exponential(): Number - { - return $this->result->exponential(); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return $this->result->binaryLogarithm(); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return $this->result->naturalLogarithm(); - } - - #[\Override] - public function commonLogarithm(): Number - { - return $this->result->commonLogarithm(); - } - - #[\Override] - public function signum(): Number - { - return $this->result->signum(); - } - - #[\Override] - public function collapse(): Number - { - return $this->result->collapse(); - } - - #[\Override] - public function toString(): string - { - return $this->result->toString(); - } } diff --git a/src/Statistics/Median.php b/src/Statistics/Median.php index 5d7cf48..8f39399 100644 --- a/src/Statistics/Median.php +++ b/src/Statistics/Median.php @@ -13,7 +13,7 @@ /** * @psalm-immutable */ -final class Median implements Number +final class Median { private Number $result; @@ -40,150 +40,6 @@ public function result(): Number return $this->result; } - #[\Override] - public function value(): int|float - { - return $this->result->value(); - } - - #[\Override] - public function equals(Number $number): bool - { - return $this->result->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->result->higherThan($number); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return $this->result->add($number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return $this->result->subtract($number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return $this->result->divideBy($number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return $this->result->multiplyBy($number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return $this->result->roundUp($precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return $this->result->roundDown($precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return $this->result->roundEven($precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return $this->result->roundOdd($precision); - } - - #[\Override] - public function floor(): Number - { - return $this->result->floor(); - } - - #[\Override] - public function ceil(): Number - { - return $this->result->ceil(); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return $this->result->modulo($modulus); - } - - #[\Override] - public function absolute(): Number - { - return $this->result->absolute(); - } - - #[\Override] - public function power(Number $power): Number - { - return $this->result->power($power); - } - - #[\Override] - public function squareRoot(): Number - { - return $this->result->squareRoot(); - } - - #[\Override] - public function exponential(): Number - { - return $this->result->exponential(); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return $this->result->binaryLogarithm(); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return $this->result->naturalLogarithm(); - } - - #[\Override] - public function commonLogarithm(): Number - { - return $this->result->commonLogarithm(); - } - - #[\Override] - public function signum(): Number - { - return $this->result->signum(); - } - - #[\Override] - public function collapse(): Number - { - return $this; - } - - #[\Override] - public function toString(): string - { - return $this->result->toString(); - } - /** * @param Sequence $values */ @@ -221,6 +77,6 @@ private function even(Sequence $values): Number static fn($number) => $number, static fn() => throw new LogicException, ), - ); + )->result(); } } diff --git a/src/Statistics/Scope.php b/src/Statistics/Scope.php index 8e9a686..739d5fd 100644 --- a/src/Statistics/Scope.php +++ b/src/Statistics/Scope.php @@ -16,7 +16,7 @@ /** * @psalm-immutable */ -final class Scope implements Number +final class Scope { private Number $result; @@ -29,10 +29,8 @@ private function __construct( $this->result = Maybe::all($sequence->last(), $sequence->first()) ->map(static fn(Number $last, Number $first) => $last->subtract($first)) - ->match( - static fn($result) => $result, - static fn() => throw new LogicException('Unreachable'), - ); + ->attempt(static fn() => new LogicException('Unreachable')) + ->unwrap(); } /** @@ -50,148 +48,4 @@ public function result(): Number { return $this->result; } - - #[\Override] - public function value(): int|float - { - return $this->result->value(); - } - - #[\Override] - public function equals(Number $number): bool - { - return $this->result->equals($number); - } - - #[\Override] - public function higherThan(Number $number): bool - { - return $this->result->higherThan($number); - } - - #[\Override] - public function add(Number $number, Number ...$numbers): Number - { - return $this->result->add($number, ...$numbers); - } - - #[\Override] - public function subtract(Number $number, Number ...$numbers): Number - { - return $this->result->subtract($number, ...$numbers); - } - - #[\Override] - public function divideBy(Number $number): Number - { - return $this->result->divideBy($number); - } - - #[\Override] - public function multiplyBy(Number $number, Number ...$numbers): Number - { - return $this->result->multiplyBy($number, ...$numbers); - } - - #[\Override] - public function roundUp(int $precision = 0): Number - { - return $this->result->roundUp($precision); - } - - #[\Override] - public function roundDown(int $precision = 0): Number - { - return $this->result->roundDown($precision); - } - - #[\Override] - public function roundEven(int $precision = 0): Number - { - return $this->result->roundEven($precision); - } - - #[\Override] - public function roundOdd(int $precision = 0): Number - { - return $this->result->roundOdd($precision); - } - - #[\Override] - public function floor(): Number - { - return $this->result->floor(); - } - - #[\Override] - public function ceil(): Number - { - return $this->result->ceil(); - } - - #[\Override] - public function modulo(Number $modulus): Number - { - return $this->result->modulo($modulus); - } - - #[\Override] - public function absolute(): Number - { - return $this->result->absolute(); - } - - #[\Override] - public function power(Number $power): Number - { - return $this->result->power($power); - } - - #[\Override] - public function squareRoot(): Number - { - return $this->result->squareRoot(); - } - - #[\Override] - public function exponential(): Number - { - return $this->result->exponential(); - } - - #[\Override] - public function binaryLogarithm(): Number - { - return $this->result->binaryLogarithm(); - } - - #[\Override] - public function naturalLogarithm(): Number - { - return $this->result->naturalLogarithm(); - } - - #[\Override] - public function commonLogarithm(): Number - { - return $this->result->commonLogarithm(); - } - - #[\Override] - public function signum(): Number - { - return $this->result->signum(); - } - - #[\Override] - public function collapse(): Number - { - return $this->result->collapse(); - } - - #[\Override] - public function toString(): string - { - return $this->result->toString(); - } } diff --git a/src/functions.php b/src/functions.php index 57fea20..2761391 100644 --- a/src/functions.php +++ b/src/functions.php @@ -46,10 +46,8 @@ function max(Number $first, Number ...$numbers): Number return Sequence::of($first, ...$numbers) ->sort(desc(...)) ->first() - ->match( - static fn($max) => $max, - static fn() => throw new LogicException('Unreachable'), - ); + ->attempt(static fn() => new LogicException('Unreachable')) + ->unwrap(); } /** @@ -61,8 +59,6 @@ function min(Number $first, Number ...$numbers): Number return Sequence::of($first, ...$numbers) ->sort(asc(...)) ->first() - ->match( - static fn($min) => $min, - static fn() => throw new LogicException('Unreachable'), - ); + ->attempt(static fn() => new LogicException('Unreachable')) + ->unwrap(); } diff --git a/tests/Algebra/AbsoluteTest.php b/tests/Algebra/AbsoluteTest.php index 8f0fcc3..f76ea4f 100644 --- a/tests/Algebra/AbsoluteTest.php +++ b/tests/Algebra/AbsoluteTest.php @@ -3,45 +3,23 @@ namespace Tests\Innmind\Math\Algebra; -use Innmind\Math\Algebra\{ - Absolute, - Ceil, - Floor, - Operation, - Number, - Addition, - Subtraction, - Multiplication, - Division, - Round, - Modulo, - Power, - SquareRoot, - Exponential, - BinaryLogarithm, - NaturalLogarithm, - CommonLogarithm, - Signum, - Real, -}; -use PHPUnit\Framework\TestCase; +use Innmind\Math\Algebra\Number; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; class AbsoluteTest extends TestCase { public function testInterface() { - $absolute = Absolute::of(Real::of(42.42)); + $absolute = Number::of(42.42)->absolute(); $this->assertInstanceOf(Number::class, $absolute); - $this->assertInstanceOf(Operation::class, $absolute); } - /** - * @dataProvider values - */ + #[DataProvider('values')] public function testValue($number, $expected) { - $absolute = Absolute::of(Real::of($number)); + $absolute = Number::of($number)->absolute(); $this->assertSame($expected, $absolute->value()); } @@ -50,171 +28,134 @@ public function testStringCast() { $this->assertSame( '|42.45|', - Absolute::of(Real::of(42.45))->toString(), + Number::of(42.45)->absolute()->toString(), ); } public function testEquals() { - $absolute = Absolute::of(Real::of(-42.45)); + $absolute = Number::of(-42.45)->absolute(); - $this->assertTrue($absolute->equals(Real::of(42.45))); - $this->assertFalse($absolute->equals(Real::of(-42.45))); + $this->assertTrue($absolute->equals(Number::of(42.45))); + $this->assertFalse($absolute->equals(Number::of(-42.45))); } public function testHigherThan() { - $absolute = Absolute::of(Real::of(-42.45)); + $absolute = Number::of(-42.45)->absolute(); - $this->assertTrue($absolute->higherThan(Real::of(0))); - $this->assertFalse($absolute->higherThan(Real::of(43))); + $this->assertTrue($absolute->higherThan(Number::of(0))); + $this->assertFalse($absolute->higherThan(Number::of(43))); } public function testAdd() { - $absolute = Absolute::of(Real::of(-43)); - $number = $absolute->add(Real::of(7)); + $absolute = Number::of(-43)->absolute(); + $number = $absolute->add(Number::of(7)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(50, $number->value()); } public function testSubtract() { - $absolute = Absolute::of(Real::of(-43)); - $number = $absolute->subtract(Real::of(7)); + $absolute = Number::of(-43)->absolute(); + $number = $absolute->subtract(Number::of(7)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(36, $number->value()); } public function testMultiplication() { - $absolute = Absolute::of(Real::of(-43)); - $number = $absolute->multiplyBy(Real::of(2)); + $absolute = Number::of(-43)->absolute(); + $number = $absolute->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(86, $number->value()); } public function testDivision() { - $absolute = Absolute::of(Real::of(-43)); - $number = $absolute->divideBy(Real::of(2)); + $absolute = Number::of(-43)->absolute(); + $number = $absolute->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(21.5, $number->value()); } - public function testRound() - { - $number = Absolute::of(Real::of(-42.45)); - - $this->assertEquals(Round::up($number, 2), $number->roundUp(2)); - $this->assertEquals(Round::down($number, 2), $number->roundDown(2)); - $this->assertEquals(Round::even($number, 2), $number->roundEven(2)); - $this->assertEquals(Round::odd($number, 2), $number->roundOdd(2)); - } - public function testFloor() { - $absolute = Absolute::of(Real::of(-42.45)); + $absolute = Number::of(-42.45)->absolute(); $number = $absolute->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(42.0, $number->value()); } public function testCeil() { - $absolute = Absolute::of(Real::of(-42.45)); + $absolute = Number::of(-42.45)->absolute(); $number = $absolute->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(43.0, $number->value()); } public function testAbsolute() { - $absolute = Absolute::of(Real::of(-42.45)); + $absolute = Number::of(-42.45)->absolute(); $number = $absolute->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(42.45, $number->value()); } public function testModulo() { - $absolute = Absolute::of(Real::of(-42.45)); - $number = $absolute->modulo(Real::of(2.1)); + $absolute = Number::of(-42.45)->absolute(); + $number = $absolute->modulo(Number::of(2.1)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertEqualsWithDelta(0.45, $number->value(), 0.001); } public function testPower() { - $absolute = Absolute::of(Real::of(-4)); - $number = $absolute->power(Real::of(2)); + $absolute = Number::of(-4)->absolute(); + $number = $absolute->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(16, $number->value()); } public function testSquareRoot() { - $absolute = Absolute::of(Real::of(-4)); + $absolute = Number::of(-4)->absolute(); $number = $absolute->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(2.0, $number->value()); } public function testExponential() { - $number = Absolute::of(Real::of(-4))->exponential(); + $number = Number::of(-4)->absolute()->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\exp(4), $number->value()); } - public function testBinaryLogarithm() - { - $number = Absolute::of(Real::of(-4))->binaryLogarithm(); - - $this->assertInstanceOf(BinaryLogarithm::class, $number); - $this->assertSame(\log(4, 2), $number->value()); - } - - public function testNaturalLogarithm() - { - $number = Absolute::of(Real::of(-4))->naturalLogarithm(); - - $this->assertInstanceOf(NaturalLogarithm::class, $number); - $this->assertSame(\log(4), $number->value()); - } - - public function testCommonLogarithm() - { - $number = Absolute::of(Real::of(-4))->commonLogarithm(); - - $this->assertInstanceOf(CommonLogarithm::class, $number); - $this->assertSame(\log10(4), $number->value()); - } - public function testSignum() { - $number = Absolute::of(Real::of(-4))->signum(); + $number = Number::of(-4)->absolute()->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } - public function values(): array + public static function values(): array { return [ [-1, 1], [1, 1], ]; } + + private function assertEqualsWithDelta( + int|float $expected, + int|float $value, + int|float $delta, + ): void { + $this->assertGreaterThanOrEqual($expected-$delta, $value); + $this->assertLessThanOrEqual($expected+$delta, $value); + } } diff --git a/tests/Algebra/AdditionTest.php b/tests/Algebra/AdditionTest.php index 1e84e9d..585cac4 100644 --- a/tests/Algebra/AdditionTest.php +++ b/tests/Algebra/AdditionTest.php @@ -3,291 +3,189 @@ namespace Tests\Innmind\Math\Algebra; -use Innmind\Math\Algebra\{ - Addition, - Operation, - Number, - Subtraction, - Division, - Multiplication, - Round, - Floor, - Ceil, - Modulo, - Absolute, - Power, - SquareRoot, - Exponential, - BinaryLogarithm, - NaturalLogarithm, - CommonLogarithm, - Signum, - Real, -}; -use PHPUnit\Framework\TestCase; +use Innmind\Math\Algebra\Number; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class AdditionTest extends TestCase { public function testInterface() { - $addition = Addition::of( - Real::of(24), - Real::of(42), - Real::of(66), - ); + $addition = Number::of(24) + ->add(Number::of(42)) + ->add(Number::of(66)); - $this->assertInstanceOf(Operation::class, $addition); $this->assertInstanceOf(Number::class, $addition); $this->assertSame('24 + 42 + 66', $addition->toString()); } public function testResult() { - $addition = Addition::of( - Real::of(24), - Real::of(42), - Real::of(66), - ); - $result = $addition->result(); + $addition = Number::of(24) + ->add(Number::of(42)) + ->add(Number::of(66)); - $this->assertInstanceOf(Number::class, $result); - $this->assertSame(132, $result->value()); - $this->assertTrue($result->equals($addition->sum())); + $this->assertInstanceOf(Number::class, $addition); + $this->assertSame(132, $addition->value()); } public function testValue() { - $addition = Addition::of( - Real::of(24), - Real::of(42), - Real::of(66), - ); + $addition = Number::of(24) + ->add(Number::of(42)) + ->add(Number::of(66)); $this->assertSame(132, $addition->value()); } public function testEquals() { - $addition = Addition::of( - Real::of(24), - Real::of(42), - Real::of(66), - ); + $addition = Number::of(24) + ->add(Number::of(42)) + ->add(Number::of(66)); - $this->assertTrue($addition->equals(Real::of(132))); - $this->assertFalse($addition->equals(Real::of(131))); + $this->assertTrue($addition->equals(Number::of(132))); + $this->assertFalse($addition->equals(Number::of(131))); } public function testHigherThan() { - $addition = Addition::of( - Real::of(24), - Real::of(42), - Real::of(66), - ); + $addition = Number::of(24) + ->add(Number::of(42)) + ->add(Number::of(66)); - $this->assertFalse($addition->higherThan(Real::of(132))); - $this->assertTrue($addition->higherThan(Real::of(131))); + $this->assertFalse($addition->higherThan(Number::of(132))); + $this->assertTrue($addition->higherThan(Number::of(131))); } public function testAdd() { - $addition = Addition::of( - Real::of(24), - Real::of(42), - ); - $number = $addition->add(Real::of(66)); + $addition = Number::of(24) + ->add(Number::of(42)); + $number = $addition->add(Number::of(66)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(132, $number->value()); } public function testSubtract() { - $addition = Addition::of( - Real::of(24), - Real::of(42), - ); - $number = $addition->subtract(Real::of(66)); + $addition = Number::of(24) + ->add(Number::of(42)); + $number = $addition->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(0, $number->value()); } public function testDivideBy() { - $addition = Addition::of( - Real::of(24), - Real::of(42), - ); - $number = $addition->divideBy(Real::of(3)); + $addition = Number::of(24) + ->add(Number::of(42)); + $number = $addition->divideBy(Number::of(3)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(22, $number->value()); } public function testMulitplyBy() { - $addition = Addition::of( - Real::of(24), - Real::of(42), - ); - $number = $addition->multiplyBy(Real::of(2)); + $addition = Number::of(24) + ->add(Number::of(42)); + $number = $addition->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(132, $number->value()); } - public function testRound() - { - $number = Addition::of( - Real::of(2.1), - Real::of(4.24), - ); - - $this->assertEquals(Round::up($number, 2), $number->roundUp(2)); - $this->assertEquals(Round::down($number, 2), $number->roundDown(2)); - $this->assertEquals(Round::even($number, 2), $number->roundEven(2)); - $this->assertEquals(Round::odd($number, 2), $number->roundOdd(2)); - } - public function testFloor() { - $addition = Addition::of( - Real::of(2.1), - Real::of(4.24), + $addition = Number::of(2.1)->add( + Number::of(4.24), ); $number = $addition->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(6.0, $number->value()); } public function testCeil() { - $addition = Addition::of( - Real::of(2.1), - Real::of(4.24), + $addition = Number::of(2.1)->add( + Number::of(4.24), ); $number = $addition->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(7.0, $number->value()); } public function testStringCast() { - $addition = Addition::of( - Real::of(24), - Addition::of( - Real::of(42), - Real::of(66), - ), + $addition = Number::of(24)->add( + Number::of(42)->add(Number::of(66)), ); - $this->assertSame('24 + (42 + 66)', $addition->toString()); + $this->assertSame('24 + 42 + 66', $addition->toString()); } public function testModulo() { - $addition = Addition::of( - Real::of(2.1), - Real::of(4.24), + $addition = Number::of(2.1)->add( + Number::of(4.24), ); - $number = $addition->modulo(Real::of(0.1)); + $number = $addition->modulo(Number::of(0.1)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertEqualsWithDelta(0.04, $number->value(), 0.0001); } public function testAbsolute() { - $addition = Addition::of( - Real::of(2.1), - Real::of(4.24), + $addition = Number::of(2.1)->add( + Number::of(4.24), ); $number = $addition->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(6.34, $number->value()); } public function testPower() { - $addition = Addition::of( - Real::of(2), - Real::of(4), + $addition = Number::of(2)->add( + Number::of(4), ); - $number = $addition->power(Real::of(2)); + $number = $addition->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(36, $number->value()); } public function testSquareRoot() { - $addition = Addition::of( - Real::of(2), - Real::of(2), + $addition = Number::of(2)->add( + Number::of(2), ); $number = $addition->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(2.0, $number->value()); } public function testExponential() { - $number = Addition::of( - Real::of(2), - Real::of(2), - )->exponential(); + $number = Number::of(2) + ->add(Number::of(2)) + ->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\exp(4), $number->value()); } - public function testBinaryLogarithm() - { - $number = Addition::of( - Real::of(2), - Real::of(2), - )->binaryLogarithm(); - - $this->assertInstanceOf(BinaryLogarithm::class, $number); - $this->assertSame(\log(4, 2), $number->value()); - } - - public function testNaturalLogarithm() - { - $number = Addition::of( - Real::of(2), - Real::of(2), - )->naturalLogarithm(); - - $this->assertInstanceOf(NaturalLogarithm::class, $number); - $this->assertSame(\log(4), $number->value()); - } - - public function testCommonLogarithm() - { - $number = Addition::of( - Real::of(2), - Real::of(2), - )->commonLogarithm(); - - $this->assertInstanceOf(CommonLogarithm::class, $number); - $this->assertSame(\log10(4), $number->value()); - } - public function testSignum() { - $number = Addition::of( - Real::of(2), - Real::of(2), - )->signum(); + $number = Number::of(2) + ->add(Number::of(2)) + ->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } + + private function assertEqualsWithDelta( + int|float $expected, + int|float $value, + int|float $delta, + ): void { + $this->assertGreaterThanOrEqual($expected-$delta, $value); + $this->assertLessThanOrEqual($expected+$delta, $value); + } } diff --git a/tests/Algebra/BinaryLogarithmTest.php b/tests/Algebra/BinaryLogarithmTest.php index 1cddc6c..fdd0aa7 100644 --- a/tests/Algebra/BinaryLogarithmTest.php +++ b/tests/Algebra/BinaryLogarithmTest.php @@ -4,59 +4,38 @@ namespace Tests\Innmind\Math\Algebra; use Innmind\Math\{ - Algebra\BinaryLogarithm, - Algebra\SquareRoot, - Algebra\Ceil, - Algebra\Floor, - Algebra\Operation, + Algebra\Logarithm, Algebra\Number, - Algebra\Addition, - Algebra\Subtraction, - Algebra\Multiplication, - Algebra\Division, - Algebra\Round, - Algebra\Modulo, - Algebra\Absolute, - Algebra\Power, - Algebra\Exponential, - Algebra\NaturalLogarithm, - Algebra\CommonLogarithm, - Algebra\Signum, - Algebra\Real, - DefinitionSet\Set, Exception\OutOfDefinitionSet }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class BinaryLogarithmTest extends TestCase { public function testInterface() { - $lb = BinaryLogarithm::of(Real::of(42.42)); + $lb = Number::of(42.42)->apply(Logarithm::binary); $this->assertInstanceOf(Number::class, $lb); - $this->assertInstanceOf(Operation::class, $lb); } public function testThrowWhenNotAllowedValue() { $this->expectException(OutOfDefinitionSet::class); - BinaryLogarithm::of(Real::of(0)); + Number::of(0)->apply(Logarithm::binary)->memoize(); } public function testResult() { - $lb = BinaryLogarithm::of(Real::of(1)); - $result = $lb->result(); + $lb = Number::of(1)->apply(Logarithm::binary); - $this->assertInstanceOf(Number::class, $result); - $this->assertSame(0.0, $result->value()); + $this->assertSame(0.0, $lb->value()); } public function testValue() { - $lb = BinaryLogarithm::of(Real::of(1)); + $lb = Number::of(1)->apply(Logarithm::binary); $this->assertSame(0.0, $lb->value()); } @@ -65,187 +44,153 @@ public function testStringCast() { $this->assertSame( 'lb(4)', - BinaryLogarithm::of(Real::of(4))->toString(), + Number::of(4)->apply(Logarithm::binary)->toString(), ); } public function testEquals() { - $lb = BinaryLogarithm::of(Real::of(1)); + $lb = Number::of(1)->apply(Logarithm::binary); - $this->assertTrue($lb->equals(Real::of(0))); - $this->assertFalse($lb->equals(Real::of(0.1))); + $this->assertTrue($lb->equals(Number::of(0))); + $this->assertFalse($lb->equals(Number::of(0.1))); } public function testHigherThan() { - $lb = BinaryLogarithm::of(Real::of(1)); + $lb = Number::of(1)->apply(Logarithm::binary); - $this->assertTrue($lb->higherThan(Real::of(-0.1))); - $this->assertFalse($lb->higherThan(Real::of(0))); + $this->assertTrue($lb->higherThan(Number::of(-0.1))); + $this->assertFalse($lb->higherThan(Number::of(0))); } public function testAdd() { - $lb = BinaryLogarithm::of(Real::of(1)); - $number = $lb->add(Real::of(7)); + $lb = Number::of(1)->apply(Logarithm::binary); + $number = $lb->add(Number::of(7)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(7.0, $number->value()); } public function testSubtract() { - $lb = BinaryLogarithm::of(Real::of(1)); - $number = $lb->subtract(Real::of(7)); + $lb = Number::of(1)->apply(Logarithm::binary); + $number = $lb->subtract(Number::of(7)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-7.0, $number->value()); } public function testMultiplication() { - $lb = BinaryLogarithm::of(Real::of(1)); - $number = $lb->multiplyBy(Real::of(2)); + $lb = Number::of(1)->apply(Logarithm::binary); + $number = $lb->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(0.0, $number->value()); } public function testDivision() { - $lb = BinaryLogarithm::of(Real::of(1)); - $number = $lb->divideBy(Real::of(2)); + $lb = Number::of(1)->apply(Logarithm::binary); + $number = $lb->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(0.0, $number->value()); } - public function testRound() - { - $number = BinaryLogarithm::of(Real::of(1)); - - $this->assertEquals(Round::up($number, 2), $number->roundUp(2)); - $this->assertEquals(Round::down($number, 2), $number->roundDown(2)); - $this->assertEquals(Round::even($number, 2), $number->roundEven(2)); - $this->assertEquals(Round::odd($number, 2), $number->roundOdd(2)); - } - public function testFloor() { - $lb = BinaryLogarithm::of(Real::of(1)); + $lb = Number::of(1)->apply(Logarithm::binary); $number = $lb->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(0.0, $number->value()); } public function testCeil() { - $lb = BinaryLogarithm::of(Real::of(1)); + $lb = Number::of(1)->apply(Logarithm::binary); $number = $lb->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(0.0, $number->value()); } public function testAbsolute() { - $lb = BinaryLogarithm::of(Real::of(0.5)); + $lb = Number::of(0.5)->apply(Logarithm::binary); $number = $lb->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(1.0, $number->value()); } public function testModulo() { - $lb = BinaryLogarithm::of(Real::of(1)); - $number = $lb->modulo(Real::of(2)); + $lb = Number::of(1)->apply(Logarithm::binary); + $number = $lb->modulo(Number::of(2)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(0.0, $number->value()); } public function testPower() { - $lb = BinaryLogarithm::of(Real::of(1)); - $number = $lb->power(Real::of(2)); + $lb = Number::of(1)->apply(Logarithm::binary); + $number = $lb->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(0.0, $number->value()); } public function testSquareRoot() { - $lb = BinaryLogarithm::of(Real::of(1)); + $lb = Number::of(1)->apply(Logarithm::binary); $number = $lb->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(0.0, $number->value()); } public function testExponential() { - $number = BinaryLogarithm::of(Real::of(1))->exponential(); + $number = Number::of(1)->apply(Logarithm::binary)->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(1.0, $number->value()); } public function testBinaryLogarithm() { - $number = BinaryLogarithm::of(Real::of(2))->binaryLogarithm(); + $number = Number::of(2)->apply(Logarithm::binary)->apply(Logarithm::base2); - $this->assertInstanceOf(BinaryLogarithm::class, $number); $this->assertSame(\log(\log(2, 2), 2), $number->value()); } public function testNaturalLogarithm() { - $number = BinaryLogarithm::of(Real::of(2))->naturalLogarithm(); + $number = Number::of(2)->apply(Logarithm::binary)->apply(Logarithm::baseE); - $this->assertInstanceOf(NaturalLogarithm::class, $number); $this->assertSame(\log(\log(2, 2)), $number->value()); } public function testCommonLogarithm() { - $number = BinaryLogarithm::of(Real::of(2))->commonLogarithm(); + $number = Number::of(2)->apply(Logarithm::binary)->apply(Logarithm::base10); - $this->assertInstanceOf(CommonLogarithm::class, $number); $this->assertSame(\log10(\log(2, 2)), $number->value()); } public function testSignum() { - $number = BinaryLogarithm::of(Real::of(2))->signum(); + $number = Number::of(2)->apply(Logarithm::binary)->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } - public function testDefinitionSet() - { - $set = BinaryLogarithm::definitionSet(); - - $this->assertInstanceOf(Set::class, $set); - $this->assertSame(']0;+∞[', $set->toString()); - } - public function testLogarithmMultiplication() { //lb(axb) === lb(a) + lb(b) $this->assertTrue( - BinaryLogarithm::of( - ($a = Real::of(2))->multiplyBy( - $b = Real::of(4), + ($a = Number::of(2)) + ->multiplyBy($b = Number::of(4)) + ->apply(Logarithm::binary) + ->equals( + $a->apply(Logarithm::binary)->add( + $b->apply(Logarithm::binary), + ), ), - )->equals( - BinaryLogarithm::of($a)->add( - BinaryLogarithm::of($b), - ), - ), ); } @@ -253,15 +198,14 @@ public function testLogarithmDivision() { //lb(a/b) === lb(a) - lb(b) $this->assertTrue( - BinaryLogarithm::of( - ($a = Real::of(2))->divideBy( - $b = Real::of(4), - ), - )->equals( - BinaryLogarithm::of($a)->subtract( - BinaryLogarithm::of($b), + ($a = Number::of(2)) + ->divideBy($b = Number::of(4)) + ->apply(Logarithm::binary) + ->equals( + $a->apply(Logarithm::binary)->subtract( + $b->apply(Logarithm::binary), + ), ), - ), ); } } diff --git a/tests/Algebra/CeilTest.php b/tests/Algebra/CeilTest.php index 84e95fc..b0015c1 100644 --- a/tests/Algebra/CeilTest.php +++ b/tests/Algebra/CeilTest.php @@ -3,43 +3,23 @@ namespace Tests\Innmind\Math\Algebra; -use Innmind\Math\Algebra\{ - Ceil, - Floor, - Number, - Addition, - Subtraction, - Multiplication, - Division, - Round, - Modulo, - Absolute, - Power, - SquareRoot, - Exponential, - BinaryLogarithm, - NaturalLogarithm, - CommonLogarithm, - Signum, - Real, -}; -use PHPUnit\Framework\TestCase; +use Innmind\Math\Algebra\Number; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; class CeilTest extends TestCase { public function testInterface() { - $ceil = Ceil::of(Real::of(42.42)); + $ceil = Number::of(42.42)->ceil(); $this->assertInstanceOf(Number::class, $ceil); } - /** - * @dataProvider values - */ + #[DataProvider('values')] public function testValue($number, $expected) { - $ceil = Ceil::of(Real::of($number)); + $ceil = Number::of($number)->ceil(); $this->assertSame($expected, $ceil->value()); } @@ -48,168 +28,122 @@ public function testStringCast() { $this->assertSame( '43.0', - (Ceil::of(Real::of(42.45)))->toString(), + Number::of(42.45)->ceil()->toString(), ); } public function testEquals() { - $ceil = Ceil::of(Real::of(42.45)); + $ceil = Number::of(42.45)->ceil(); - $this->assertTrue($ceil->equals(Real::of(43))); - $this->assertTrue($ceil->equals(Real::of(43.0))); - $this->assertFalse($ceil->equals(Real::of(42))); + $this->assertTrue($ceil->equals(Number::of(43))); + $this->assertTrue($ceil->equals(Number::of(43.0))); + $this->assertFalse($ceil->equals(Number::of(42))); } public function testHigherThan() { - $ceil = Ceil::of(Real::of(42.45)); + $ceil = Number::of(42.45)->ceil(); - $this->assertTrue($ceil->higherThan(Real::of(41.9))); - $this->assertFalse($ceil->higherThan(Real::of(43.5))); + $this->assertTrue($ceil->higherThan(Number::of(41.9))); + $this->assertFalse($ceil->higherThan(Number::of(43.5))); } public function testAdd() { - $ceil = Ceil::of(Real::of(42.5)); - $number = $ceil->add(Real::of(7)); + $ceil = Number::of(42.5)->ceil(); + $number = $ceil->add(Number::of(7)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(50.0, $number->value()); } public function testSubtract() { - $ceil = Ceil::of(Real::of(42.5)); - $number = $ceil->subtract(Real::of(7)); + $ceil = Number::of(42.5)->ceil(); + $number = $ceil->subtract(Number::of(7)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(36.0, $number->value()); } public function testMultiplication() { - $ceil = Ceil::of(Real::of(42.5)); - $number = $ceil->multiplyBy(Real::of(2)); + $ceil = Number::of(42.5)->ceil(); + $number = $ceil->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(86.0, $number->value()); } public function testDivision() { - $ceil = Ceil::of(Real::of(42.5)); - $number = $ceil->divideBy(Real::of(2)); + $ceil = Number::of(42.5)->ceil(); + $number = $ceil->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(21.5, $number->value()); } - public function testRound() - { - $number = Ceil::of(Real::of(42.45)); - - $this->assertEquals(Round::up($number, 2), $number->roundUp(2)); - $this->assertEquals(Round::down($number, 2), $number->roundDown(2)); - $this->assertEquals(Round::even($number, 2), $number->roundEven(2)); - $this->assertEquals(Round::odd($number, 2), $number->roundOdd(2)); - } - public function testFloor() { - $ceil = Ceil::of(Real::of(42.45)); + $ceil = Number::of(42.45)->ceil(); $number = $ceil->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(43.0, $number->value()); } public function testCeil() { - $ceil = Ceil::of(Real::of(42.45)); + $ceil = Number::of(42.45)->ceil(); $number = $ceil->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(43.0, $number->value()); } public function testModulo() { - $ceil = Ceil::of(Real::of(42.45)); - $number = $ceil->modulo(Real::of(2.1)); + $ceil = Number::of(42.45)->ceil(); + $number = $ceil->modulo(Number::of(2.1)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertEqualsWithDelta(1.0, $number->value(), 0.0001); } public function testAbsolute() { - $ceil = Ceil::of(Real::of(-42.45)); + $ceil = Number::of(-42.45)->ceil(); $number = $ceil->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(42.0, $number->value()); } public function testPower() { - $ceil = Ceil::of(Real::of(2.5)); - $number = $ceil->power(Real::of(2)); + $ceil = Number::of(2.5)->ceil(); + $number = $ceil->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(9.0, $number->value()); } public function testSquareRoot() { - $ceil = Ceil::of(Real::of(3.5)); + $ceil = Number::of(3.5)->ceil(); $number = $ceil->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(2.0, $number->value()); } public function testExponential() { - $number = Ceil::of(Real::of(3.5))->exponential(); + $number = Number::of(3.5)->ceil()->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\exp(4), $number->value()); } - public function testBinaryLogarithm() - { - $number = Ceil::of(Real::of(3.5))->binaryLogarithm(); - - $this->assertInstanceOf(BinaryLogarithm::class, $number); - $this->assertSame(\log(4, 2), $number->value()); - } - - public function testNaturalLogarithm() - { - $number = Ceil::of(Real::of(3.5))->naturalLogarithm(); - - $this->assertInstanceOf(NaturalLogarithm::class, $number); - $this->assertSame(\log(4), $number->value()); - } - - public function testCommonLogarithm() - { - $number = Ceil::of(Real::of(3.5))->commonLogarithm(); - - $this->assertInstanceOf(CommonLogarithm::class, $number); - $this->assertSame(\log10(4), $number->value()); - } - public function testSignum() { - $number = Ceil::of(Real::of(2))->signum(); + $number = Number::of(2)->ceil()->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } - public function values(): array + public static function values(): array { return [ [42.4, 43.0], @@ -218,4 +152,13 @@ public function values(): array [42.51, 43.0], ]; } + + private function assertEqualsWithDelta( + int|float $expected, + int|float $value, + int|float $delta, + ): void { + $this->assertGreaterThanOrEqual($expected-$delta, $value); + $this->assertLessThanOrEqual($expected+$delta, $value); + } } diff --git a/tests/Algebra/CommonLogarithmTest.php b/tests/Algebra/CommonLogarithmTest.php index f9afa93..7b91862 100644 --- a/tests/Algebra/CommonLogarithmTest.php +++ b/tests/Algebra/CommonLogarithmTest.php @@ -4,59 +4,38 @@ namespace Tests\Innmind\Math\Algebra; use Innmind\Math\{ - Algebra\CommonLogarithm, - Algebra\SquareRoot, - Algebra\Ceil, - Algebra\Floor, - Algebra\Operation, + Algebra\Logarithm, Algebra\Number, - Algebra\Addition, - Algebra\Subtraction, - Algebra\Multiplication, - Algebra\Division, - Algebra\Round, - Algebra\Modulo, - Algebra\Absolute, - Algebra\Power, - Algebra\Exponential, - Algebra\BinaryLogarithm, - Algebra\NaturalLogarithm, - Algebra\Signum, - Algebra\Real, - DefinitionSet\Set, Exception\OutOfDefinitionSet }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class CommonLogarithmTest extends TestCase { public function testInterface() { - $lg = CommonLogarithm::of(Real::of(42.42)); + $lg = Number::of(42.42)->apply(Logarithm::common); $this->assertInstanceOf(Number::class, $lg); - $this->assertInstanceOf(Operation::class, $lg); } public function testThrowWhenNotAllowedValue() { $this->expectException(OutOfDefinitionSet::class); - CommonLogarithm::of(Real::of(0)); + Number::of(0)->apply(Logarithm::common)->memoize(); } public function testResult() { - $lg = CommonLogarithm::of(Real::of(1)); - $result = $lg->result(); + $lg = Number::of(1)->apply(Logarithm::common); - $this->assertInstanceOf(Number::class, $result); - $this->assertSame(0.0, $result->value()); + $this->assertSame(0.0, $lg->value()); } public function testValue() { - $lg = CommonLogarithm::of(Real::of(1)); + $lg = Number::of(1)->apply(Logarithm::common); $this->assertSame(0.0, $lg->value()); } @@ -65,187 +44,153 @@ public function testStringCast() { $this->assertSame( 'lg(4)', - CommonLogarithm::of(Real::of(4))->toString(), + Number::of(4)->apply(Logarithm::common)->toString(), ); } public function testEquals() { - $lg = CommonLogarithm::of(Real::of(1)); + $lg = Number::of(1)->apply(Logarithm::common); - $this->assertTrue($lg->equals(Real::of(0))); - $this->assertFalse($lg->equals(Real::of(0.1))); + $this->assertTrue($lg->equals(Number::of(0))); + $this->assertFalse($lg->equals(Number::of(0.1))); } public function testHigherThan() { - $lg = CommonLogarithm::of(Real::of(1)); + $lg = Number::of(1)->apply(Logarithm::common); - $this->assertTrue($lg->higherThan(Real::of(-0.1))); - $this->assertFalse($lg->higherThan(Real::of(0))); + $this->assertTrue($lg->higherThan(Number::of(-0.1))); + $this->assertFalse($lg->higherThan(Number::of(0))); } public function testAdd() { - $lg = CommonLogarithm::of(Real::of(1)); - $number = $lg->add(Real::of(7)); + $lg = Number::of(1)->apply(Logarithm::common); + $number = $lg->add(Number::of(7)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(7.0, $number->value()); } public function testSubtract() { - $lg = CommonLogarithm::of(Real::of(1)); - $number = $lg->subtract(Real::of(7)); + $lg = Number::of(1)->apply(Logarithm::common); + $number = $lg->subtract(Number::of(7)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-7.0, $number->value()); } public function testMultiplication() { - $lg = CommonLogarithm::of(Real::of(1)); - $number = $lg->multiplyBy(Real::of(2)); + $lg = Number::of(1)->apply(Logarithm::common); + $number = $lg->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(0.0, $number->value()); } public function testDivision() { - $lg = CommonLogarithm::of(Real::of(1)); - $number = $lg->divideBy(Real::of(2)); + $lg = Number::of(1)->apply(Logarithm::common); + $number = $lg->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(0.0, $number->value()); } - public function testRound() - { - $number = CommonLogarithm::of(Real::of(1)); - - $this->assertEquals(Round::up($number, 2), $number->roundUp(2)); - $this->assertEquals(Round::down($number, 2), $number->roundDown(2)); - $this->assertEquals(Round::even($number, 2), $number->roundEven(2)); - $this->assertEquals(Round::odd($number, 2), $number->roundOdd(2)); - } - public function testFloor() { - $lg = CommonLogarithm::of(Real::of(1)); + $lg = Number::of(1)->apply(Logarithm::common); $number = $lg->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(0.0, $number->value()); } public function testCeil() { - $lg = CommonLogarithm::of(Real::of(1)); + $lg = Number::of(1)->apply(Logarithm::common); $number = $lg->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(0.0, $number->value()); } public function testAbsolute() { - $lg = CommonLogarithm::of(Real::of(0.5)); + $lg = Number::of(0.5)->apply(Logarithm::common); $number = $lg->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(0.3010299956639812, $number->value()); } public function testModulo() { - $lg = CommonLogarithm::of(Real::of(1)); - $number = $lg->modulo(Real::of(2)); + $lg = Number::of(1)->apply(Logarithm::common); + $number = $lg->modulo(Number::of(2)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(0.0, $number->value()); } public function testPower() { - $lg = CommonLogarithm::of(Real::of(1)); - $number = $lg->power(Real::of(2)); + $lg = Number::of(1)->apply(Logarithm::common); + $number = $lg->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(0.0, $number->value()); } public function testSquareRoot() { - $lg = CommonLogarithm::of(Real::of(1)); + $lg = Number::of(1)->apply(Logarithm::common); $number = $lg->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(0.0, $number->value()); } public function testExponential() { - $number = CommonLogarithm::of(Real::of(1))->exponential(); + $number = Number::of(1)->apply(Logarithm::common)->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(1.0, $number->value()); } public function testBinaryLogarithm() { - $number = CommonLogarithm::of(Real::of(2))->binaryLogarithm(); + $number = Number::of(2)->apply(Logarithm::common)->apply(Logarithm::base2); - $this->assertInstanceOf(BinaryLogarithm::class, $number); $this->assertSame(\log(\log10(2), 2), $number->value()); } public function testNaturalLogarithm() { - $number = CommonLogarithm::of(Real::of(2))->naturalLogarithm(); + $number = Number::of(2)->apply(Logarithm::common)->apply(Logarithm::baseE); - $this->assertInstanceOf(NaturalLogarithm::class, $number); $this->assertSame(\log(\log10(2)), $number->value()); } public function testCommonLogarithm() { - $number = CommonLogarithm::of(Real::of(2))->commonLogarithm(); + $number = Number::of(2)->apply(Logarithm::common)->apply(Logarithm::base10); - $this->assertInstanceOf(CommonLogarithm::class, $number); $this->assertSame(\log10(\log10(2)), $number->value()); } public function testSignum() { - $number = CommonLogarithm::of(Real::of(2))->signum(); + $number = Number::of(2)->apply(Logarithm::common)->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } - public function testDefinitionSet() - { - $set = BinaryLogarithm::definitionSet(); - - $this->assertInstanceOf(Set::class, $set); - $this->assertSame(']0;+∞[', $set->toString()); - } - public function testLogarithmMultiplication() { //lg(axb) === lg(a) + lg(b) $this->assertTrue( - CommonLogarithm::of( - ($a = Real::of(2))->multiplyBy( - $b = Real::of(4), + ($a = Number::of(2)) + ->multiplyBy($b = Number::of(4)) + ->apply(Logarithm::common) + ->equals( + $a->apply(Logarithm::common)->add( + $b->apply(Logarithm::common), + ), ), - )->equals( - CommonLogarithm::of($a)->add( - CommonLogarithm::of($b), - ), - ), ); } @@ -253,15 +198,14 @@ public function testLogarithmDivision() { //lg(a/b) === lg(a) - lg(b) $this->assertTrue( - CommonLogarithm::of( - ($a = Real::of(2))->divideBy( - $b = Real::of(4), - ), - )->equals( - CommonLogarithm::of($a)->subtract( - CommonLogarithm::of($b), + ($a = Number::of(2)) + ->divideBy($b = Number::of(4)) + ->apply(Logarithm::common) + ->equals( + $a->apply(Logarithm::common)->subtract( + $b->apply(Logarithm::common), + ), ), - ), ); } } diff --git a/tests/Algebra/ComplexNumberTest.php b/tests/Algebra/ComplexNumberTest.php index 09baf56..1dc69c6 100644 --- a/tests/Algebra/ComplexNumberTest.php +++ b/tests/Algebra/ComplexNumberTest.php @@ -5,19 +5,17 @@ use Innmind\Math\Algebra\{ ComplexNumber, - Integer, Number, - Value, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class ComplexNumberTest extends TestCase { public function testInterface() { $number = ComplexNumber::of( - $real = Integer::of(1), - $imaginary = Integer::of(2), + $real = Number::of(1), + $imaginary = Number::of(2), ); $this->assertSame($real, $number->real()); @@ -27,15 +25,15 @@ public function testInterface() public function testStringCast() { $number = ComplexNumber::of( - Integer::of(1), - Integer::of(2), + Number::of(1), + Number::of(2), ); $this->assertSame('(1 + 2i)', $number->toString()); $number = ComplexNumber::of( - Value::one->add(Value::two), - Value::two->add(Integer::of(3)), + Number::one()->add(Number::two()), + Number::two()->add(Number::of(3)), ); $this->assertSame('((1 + 2) + (2 + 3)i)', $number->toString()); @@ -44,11 +42,11 @@ public function testStringCast() public function testAdd() { $number = (ComplexNumber::of( - Integer::of(2), - Integer::of(3), + Number::of(2), + Number::of(3), ))->add(ComplexNumber::of( - Integer::of(4), - Integer::of(5), + Number::of(4), + Number::of(5), )); $this->assertInstanceOf(ComplexNumber::class, $number); @@ -59,11 +57,11 @@ public function testAdd() public function testSubtract() { $number = (ComplexNumber::of( - Integer::of(2), - Integer::of(3), + Number::of(2), + Number::of(3), ))->subtract(ComplexNumber::of( - Integer::of(4), - Integer::of(5), + Number::of(4), + Number::of(5), )); $this->assertInstanceOf(ComplexNumber::class, $number); @@ -74,11 +72,11 @@ public function testSubtract() public function testMultiplyBy() { $number = (ComplexNumber::of( - Integer::of(2), - Integer::of(3), + Number::of(2), + Number::of(3), ))->multiplyBy(ComplexNumber::of( - Integer::of(4), - Integer::of(5), + Number::of(4), + Number::of(5), )); $this->assertInstanceOf(ComplexNumber::class, $number); @@ -89,11 +87,11 @@ public function testMultiplyBy() public function testDivideBy() { $number = (ComplexNumber::of( - Integer::of(2), - Integer::of(3), + Number::of(2), + Number::of(3), ))->divideBy(ComplexNumber::of( - Integer::of(4), - Integer::of(5), + Number::of(4), + Number::of(5), )); $this->assertInstanceOf(ComplexNumber::class, $number); @@ -104,8 +102,8 @@ public function testDivideBy() public function testConjugate() { $number = ComplexNumber::of( - Integer::of(2), - Integer::of(3), + Number::of(2), + Number::of(3), ); $conjugate = $number->conjugate(); @@ -118,8 +116,8 @@ public function testConjugate() public function testAbsolute() { $number = ComplexNumber::of( - Integer::of(2), - Integer::of(3), + Number::of(2), + Number::of(3), ); $absolute = $number->absolute(); @@ -130,8 +128,8 @@ public function testAbsolute() public function testReciprocal() { $number = ComplexNumber::of( - Integer::of(2), - Integer::of(3), + Number::of(2), + Number::of(3), ); $reciprocal = $number->reciprocal(); @@ -147,8 +145,8 @@ public function testReciprocal() public function testNegate() { $number = ComplexNumber::of( - Integer::of(2), - Integer::of(3), + Number::of(2), + Number::of(3), ); $negation = $number->negation(); @@ -161,8 +159,8 @@ public function testNegate() public function testSquareRoot() { $number = ComplexNumber::of( - Integer::of(3), - Integer::of(4), + Number::of(3), + Number::of(4), ); $squareRoot = $number->squareRoot(); @@ -172,7 +170,7 @@ public function testSquareRoot() $this->assertSame(2.0, $squareRoot->real()->value()); $this->assertSame(1.0, $squareRoot->imaginary()->value()); $this->assertSame( - '((√((3 + (√((3^2) + (4^2)))) ÷ 2)) + ((sgn(4)) x (√(((-1 x 3) + (√((3^2) + (4^2)))) ÷ 2)))i)', + '((√((3 + (√((3^2) + (4^2)))) ÷ 2)) + (sgn(4) x (√(((-1 x 3) + (√((3^2) + (4^2)))) ÷ 2)))i)', $squareRoot->toString(), ); } @@ -180,21 +178,21 @@ public function testSquareRoot() public function testEquals() { $number = ComplexNumber::of( - Integer::of(2), - Integer::of(3), + Number::of(2), + Number::of(3), ); $this->assertTrue($number->equals($number)); $this->assertTrue($number->equals( ComplexNumber::of( - Integer::of(2), - Integer::of(3), + Number::of(2), + Number::of(3), ), )); $this->assertFalse($number->equals( ComplexNumber::of( - Integer::of(3), - Integer::of(3), + Number::of(3), + Number::of(3), ), )); } diff --git a/tests/Algebra/DivisionTest.php b/tests/Algebra/DivisionTest.php index cc270ef..c220f5a 100644 --- a/tests/Algebra/DivisionTest.php +++ b/tests/Algebra/DivisionTest.php @@ -4,214 +4,144 @@ namespace Tests\Innmind\Math\Algebra; use Innmind\Math\{ - Algebra\Division, Algebra\Number, - Algebra\Operation, - Algebra\Addition, - Algebra\Subtraction, - Algebra\Multiplication, - Algebra\Round, - Algebra\Floor, - Algebra\Ceil, - Algebra\Modulo, - Algebra\Absolute, - Algebra\Power, - Algebra\SquareRoot, - Algebra\Exponential, - Algebra\BinaryLogarithm, - Algebra\NaturalLogarithm, - Algebra\CommonLogarithm, - Algebra\Signum, - Algebra\Real, Exception\DivisionByZero, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class DivisionTest extends TestCase { public function testInterface() { - $division = Division::of( - $dividend = Real::of(4), - $divisor = Real::of(2), + $division = Number::of(4)->divideBy( + Number::of(2), ); - $this->assertInstanceOf(Operation::class, $division); $this->assertInstanceOf(Number::class, $division); - $this->assertSame($dividend, $division->dividend()); - $this->assertSame($divisor, $division->divisor()); } public function testResult() { - $division = Division::of(Real::of(4), Real::of(2)); - $result = $division->result(); + $division = Number::of(4)->divideBy(Number::of(2)); - $this->assertInstanceOf(Number::class, $result); - $this->assertSame(2, $result->value()); - $this->assertTrue($result->equals($division->quotient())); + $this->assertInstanceOf(Number::class, $division); + $this->assertSame(2, $division->value()); } public function testValue() { - $division = Division::of(Real::of(4), Real::of(2)); + $division = Number::of(4)->divideBy(Number::of(2)); $this->assertSame(2, $division->value()); } public function testEquals() { - $division = Division::of(Real::of(4), Real::of(2)); + $division = Number::of(4)->divideBy(Number::of(2)); - $this->assertTrue($division->equals(Real::of(2))); - $this->assertFalse($division->equals(Real::of(2.1))); + $this->assertTrue($division->equals(Number::of(2))); + $this->assertFalse($division->equals(Number::of(2.1))); } public function testHigherThan() { - $division = Division::of(Real::of(4), Real::of(2)); + $division = Number::of(4)->divideBy(Number::of(2)); - $this->assertFalse($division->higherThan(Real::of(2))); - $this->assertTrue($division->higherThan(Real::of(1.9))); + $this->assertFalse($division->higherThan(Number::of(2))); + $this->assertTrue($division->higherThan(Number::of(1.9))); } public function testAdd() { - $division = Division::of(Real::of(4), Real::of(2)); - $number = $division->add(Real::of(66)); + $division = Number::of(4)->divideBy(Number::of(2)); + $number = $division->add(Number::of(66)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(68, $number->value()); } public function testSubtract() { - $division = Division::of(Real::of(4), Real::of(2)); - $number = $division->subtract(Real::of(66)); + $division = Number::of(4)->divideBy(Number::of(2)); + $number = $division->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-64, $number->value()); } public function testDivideBy() { - $division = Division::of(Real::of(9), Real::of(3)); - $number = $division->divideBy(Real::of(3)); + $division = Number::of(9)->divideBy(Number::of(3)); + $number = $division->divideBy(Number::of(3)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(1, $number->value()); } public function testMulitplyBy() { - $division = Division::of(Real::of(4), Real::of(2)); - $number = $division->multiplyBy(Real::of(2)); + $division = Number::of(4)->divideBy(Number::of(2)); + $number = $division->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(4, $number->value()); } - public function testRound() - { - $number = Division::of(Real::of(6.66), Real::of(3)); - - $this->assertEquals(Round::up($number, 2), $number->roundUp(2)); - $this->assertEquals(Round::down($number, 2), $number->roundDown(2)); - $this->assertEquals(Round::even($number, 2), $number->roundEven(2)); - $this->assertEquals(Round::odd($number, 2), $number->roundOdd(2)); - } - public function testFloor() { - $division = Division::of(Real::of(6.66), Real::of(3)); + $division = Number::of(6.66)->divideBy(Number::of(3)); $number = $division->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(2.0, $number->value()); } public function testCeil() { - $division = Division::of(Real::of(6.66), Real::of(3)); + $division = Number::of(6.66)->divideBy(Number::of(3)); $number = $division->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(3.0, $number->value()); } public function testModulo() { - $division = Division::of(Real::of(9), Real::of(3)); - $number = $division->modulo(Real::of(2)); + $division = Number::of(9)->divideBy(Number::of(3)); + $number = $division->modulo(Number::of(2)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(1.0, $number->value()); } public function testAbsolute() { - $division = Division::of(Real::of(9), Real::of(-3)); + $division = Number::of(9)->divideBy(Number::of(-3)); $number = $division->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(3, $number->value()); } public function testPower() { - $division = Division::of(Real::of(9), Real::of(3)); - $number = $division->power(Real::of(2)); + $division = Number::of(9)->divideBy(Number::of(3)); + $number = $division->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(9, $number->value()); } public function testSquareRoot() { - $division = Division::of(Real::of(8), Real::of(2)); + $division = Number::of(8)->divideBy(Number::of(2)); $number = $division->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(2.0, $number->value()); } public function testExponential() { - $number = Division::of(Real::of(8), Real::of(2))->exponential(); + $number = Number::of(8)->divideBy(Number::of(2))->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\exp(4), $number->value()); } - public function testBinaryLogarithm() - { - $number = Division::of(Real::of(8), Real::of(2))->binaryLogarithm(); - - $this->assertInstanceOf(BinaryLogarithm::class, $number); - $this->assertSame(\log(4, 2), $number->value()); - } - - public function testNaturalLogarithm() - { - $number = Division::of(Real::of(8), Real::of(2))->naturalLogarithm(); - - $this->assertInstanceOf(NaturalLogarithm::class, $number); - $this->assertSame(\log(4), $number->value()); - } - - public function testCommonLogarithm() - { - $number = Division::of(Real::of(8), Real::of(2))->commonLogarithm(); - - $this->assertInstanceOf(CommonLogarithm::class, $number); - $this->assertSame(\log10(4), $number->value()); - } - public function testSignum() { - $number = Division::of(Real::of(8), Real::of(2))->signum(); + $number = Number::of(8)->divideBy(Number::of(2))->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } @@ -219,13 +149,10 @@ public function testStringCast() { $this->assertSame( '(2 + 2) ÷ 2', - Division::of( - Addition::of( - Real::of(2), - Real::of(2), - ), - Real::of(2), - )->toString(), + Number::of(2) + ->add(Number::of(2)) + ->divideBy(Number::of(2)) + ->toString(), ); } @@ -233,6 +160,6 @@ public function testThrowWhenTryingToDivideByZero() { $this->expectException(DivisionByZero::class); - Division::of(Real::of(4), Real::of(-0.0)); + Number::of(4)->divideBy(Number::of(-0.0)); } } diff --git a/tests/Algebra/ExponentialTest.php b/tests/Algebra/ExponentialTest.php index d3f7aa7..c4e3cb8 100644 --- a/tests/Algebra/ExponentialTest.php +++ b/tests/Algebra/ExponentialTest.php @@ -3,252 +3,148 @@ namespace Tests\Innmind\Math\Algebra; -use Innmind\Math\Algebra\{ - Exponential, - Power, - Operation, - Number, - Addition, - Subtraction, - Multiplication, - Division, - Round, - Floor, - Ceil, - Modulo, - Absolute, - SquareRoot, - BinaryLogarithm, - NaturalLogarithm, - CommonLogarithm, - Signum, - Real, -}; -use PHPUnit\Framework\TestCase; +use Innmind\Math\Algebra\Number; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class ExponentialTest extends TestCase { public function testInterface() { - $power = Exponential::of( - $this->createMock(Number::class), - ); + $power = Number::zero()->exponential(); $this->assertInstanceOf(Number::class, $power); - $this->assertInstanceOf(Operation::class, $power); } public function testResult() { - $power = Exponential::of( - Real::of(2.1), - ); - $result = $power->result(); + $power = Number::of(2.1)->exponential(); - $this->assertInstanceOf(Number::class, $result); - $this->assertSame(8.166169912567652, $result->value()); + $this->assertSame(8.166169912567652, $power->value()); } public function testStringCast() { - $power = Exponential::of( - Real::of(2.1), - ); + $power = Number::of(2.1)->exponential(); $this->assertSame('e^2.1', $power->toString()); } public function testStringCastOperations() { - $power = Exponential::of( - Addition::of( - Real::of(2), - Real::of(2), - ), - ); + $power = Number::of(2) + ->add(Number::of(2)) + ->exponential(); $this->assertSame('e^(2 + 2)', $power->toString()); } public function testEquals() { - $power = Exponential::of( - Real::of(2.1), - ); + $power = Number::of(2.1)->exponential(); - $this->assertTrue($power->equals(Real::of(8.166169912567652))); - $this->assertFalse($power->equals(Real::of(8.16))); + $this->assertTrue($power->equals(Number::of(8.166169912567652))); + $this->assertFalse($power->equals(Number::of(8.16))); } public function testHigherThan() { - $power = Exponential::of( - Real::of(2.1), - ); + $power = Number::of(2.1)->exponential(); - $this->assertTrue($power->higherThan(Real::of(8.16))); - $this->assertFalse($power->higherThan(Real::of(8.166169912567652))); + $this->assertTrue($power->higherThan(Number::of(8.16))); + $this->assertFalse($power->higherThan(Number::of(8.166169912567652))); } public function testAdd() { - $power = Exponential::of( - Real::of(2.1), - ); - $number = $power->add(Real::of(66)); + $power = Number::of(2.1)->exponential(); + $number = $power->add(Number::of(66)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(74.16616991256765, $number->value()); } public function testSubtract() { - $power = Exponential::of( - Real::of(2.1), - ); - $number = $power->subtract(Real::of(66)); + $power = Number::of(2.1)->exponential(); + $number = $power->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-57.83383008743235, $number->value()); } public function testDivideBy() { - $power = Exponential::of( - Real::of(2.1), - ); - $number = $power->divideBy(Real::of(2)); + $power = Number::of(2.1)->exponential(); + $number = $power->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(4.083084956283826, $number->value()); } public function testMulitplyBy() { - $power = Exponential::of( - Real::of(2.1), - ); - $number = $power->multiplyBy(Real::of(2)); + $power = Number::of(2.1)->exponential(); + $number = $power->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(16.332339825135303, $number->value()); } - public function testRound() - { - $number = Exponential::of( - Real::of(2.1), - ); - - $this->assertEquals(Round::up($number, 2), $number->roundUp(2)); - $this->assertEquals(Round::down($number, 2), $number->roundDown(2)); - $this->assertEquals(Round::even($number, 2), $number->roundEven(2)); - $this->assertEquals(Round::odd($number, 2), $number->roundOdd(2)); - } - public function testFloor() { - $power = Exponential::of( - Real::of(2.1), - ); + $power = Number::of(2.1)->exponential(); $number = $power->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(8.0, $number->value()); } public function testCeil() { - $power = Exponential::of( - Real::of(2.1), - ); + $power = Number::of(2.1)->exponential(); $number = $power->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(9.0, $number->value()); } public function testModulo() { - $power = Exponential::of( - Real::of(2.1), - ); - $number = $power->modulo(Real::of(8)); + $power = Number::of(2.1)->exponential(); + $number = $power->modulo(Number::of(8)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(0.16616991256765168, $number->value()); } public function testAbsolute() { - $power = Exponential::of( - Real::of(-2.1), - ); + $power = Number::of(-2.1)->exponential(); $number = $power->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(0.1224564282529819, $number->value()); } public function testPower() { - $power = Exponential::of( - Real::of(2.1), - ); - $number = $power->power(Real::of(2)); + $power = Number::of(2.1)->exponential(); + $number = $power->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(66.68633104092517, $number->value()); } public function testSquareRoot() { - $power = Exponential::of( - Real::of(2.1), - ); + $power = Number::of(2.1)->exponential(); $number = $power->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(2.857651118063164, $number->value()); } public function testExponential() { - $number = Exponential::of(Real::of(0))->exponential(); + $number = Number::of(0)->exponential()->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\exp(1), $number->value()); } - public function testBinaryLogarithm() - { - $number = Exponential::of(Real::of(1))->binaryLogarithm(); - - $this->assertInstanceOf(BinaryLogarithm::class, $number); - $this->assertSame(\log(\exp(1), 2), $number->value()); - } - - public function testNaturalLogarithm() - { - $number = Exponential::of(Real::of(1))->naturalLogarithm(); - - $this->assertInstanceOf(NaturalLogarithm::class, $number); - $this->assertSame(\log(\exp(1)), $number->value()); - } - - public function testCommonLogarithm() - { - $number = Exponential::of(Real::of(1))->commonLogarithm(); - - $this->assertInstanceOf(CommonLogarithm::class, $number); - $this->assertSame(\log10(\exp(1)), $number->value()); - } - public function testSignum() { - $number = Exponential::of(Real::of(1))->signum(); + $number = Number::of(1)->exponential()->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } } diff --git a/tests/Algebra/FactorialTest.php b/tests/Algebra/FactorialTest.php index 6fb2682..4f1021b 100644 --- a/tests/Algebra/FactorialTest.php +++ b/tests/Algebra/FactorialTest.php @@ -5,39 +5,14 @@ use Innmind\Math\{ Algebra\Factorial, - Algebra\Integer, Algebra\Number, - Algebra\Addition, - Algebra\Subtraction, - Algebra\Multiplication, - Algebra\Division, - Algebra\Round, - Algebra\Floor, - Algebra\Ceil, - Algebra\Modulo, - Algebra\Absolute, - Algebra\Power, - Algebra\SquareRoot, - Algebra\Exponential, - Algebra\BinaryLogarithm, - Algebra\NaturalLogarithm, - Algebra\CommonLogarithm, - Algebra\Signum, - Algebra\Real, Exception\FactorialMustBePositive }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; class FactorialTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - Number::class, - Factorial::of(42), - ); - } - public function testThrowWhenNegativeFactorial() { $this->expectException(FactorialMustBePositive::class); @@ -49,175 +24,126 @@ public function testStringCast() { $number = Factorial::of(3); - $this->assertSame(6, $number->value()); + $this->assertSame(6, $number->number()->value()); $this->assertSame('3!', $number->toString()); } public function testEquals() { - $this->assertTrue(Factorial::of(3)->equals(Factorial::of(3))); - $this->assertTrue(Factorial::of(3)->equals(Real::of(6.0))); - $this->assertFalse(Factorial::of(3)->equals(Real::of(42.24))); + $this->assertTrue(Factorial::of(3)->number()->equals(Factorial::of(3)->number())); + $this->assertTrue(Factorial::of(3)->number()->equals(Number::of(6.0))); + $this->assertFalse(Factorial::of(3)->number()->equals(Number::of(42.24))); } public function testHigherThan() { - $this->assertFalse(Factorial::of(3)->higherThan(Factorial::of(3))); - $this->assertTrue(Factorial::of(3)->higherThan(Real::of(1.24))); + $this->assertFalse(Factorial::of(3)->number()->higherThan(Factorial::of(3)->number())); + $this->assertTrue(Factorial::of(3)->number()->higherThan(Number::of(1.24))); } public function testAdd() { - $number = Factorial::of(3); - $number = $number->add(Integer::of(66)); + $number = Factorial::of(3)->number(); + $number = $number->add(Number::of(66)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(72, $number->value()); } public function testSubtract() { - $number = Factorial::of(3); - $number = $number->subtract(Integer::of(66)); + $number = Factorial::of(3)->number(); + $number = $number->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-60, $number->value()); } public function testDivideBy() { - $number = Factorial::of(3); - $number = $number->divideBy(Integer::of(2)); + $number = Factorial::of(3)->number(); + $number = $number->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(3, $number->value()); } public function testMulitplyBy() { - $number = Factorial::of(3); - $number = $number->multiplyBy(Integer::of(2)); + $number = Factorial::of(3)->number(); + $number = $number->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(12, $number->value()); } - public function testRound() - { - $number = Factorial::of(3); - - $this->assertEquals(Round::up($number, 2), $number->roundUp(2)); - $this->assertEquals(Round::down($number, 2), $number->roundDown(2)); - $this->assertEquals(Round::even($number, 2), $number->roundEven(2)); - $this->assertEquals(Round::odd($number, 2), $number->roundOdd(2)); - } - public function testFloor() { - $number = Factorial::of(3); + $number = Factorial::of(3)->number(); $number = $number->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(6.0, $number->value()); } public function testCeil() { - $number = Factorial::of(3); + $number = Factorial::of(3)->number(); $number = $number->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(6.0, $number->value()); } public function testModulo() { - $number = Factorial::of(3); - $number = $number->modulo(Integer::of(4)); + $number = Factorial::of(3)->number(); + $number = $number->modulo(Number::of(4)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(2.0, $number->value()); } public function testAbsolute() { - $number = Factorial::of(3); + $number = Factorial::of(3)->number(); $number = $number->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(6, $number->value()); } public function testPower() { - $number = Factorial::of(3); - $number = $number->power(Integer::of(2)); + $number = Factorial::of(3)->number(); + $number = $number->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(36, $number->value()); } public function testSquareRoot() { - $number = Factorial::of(3); + $number = Factorial::of(3)->number(); $number = $number->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(2.449489742783178, $number->value()); } public function testExponential() { - $number = Factorial::of(4)->exponential(); + $number = Factorial::of(4)->number()->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\exp(24), $number->value()); } - public function testBinaryLogarithm() - { - $number = Factorial::of(4)->binaryLogarithm(); - - $this->assertInstanceOf(BinaryLogarithm::class, $number); - $this->assertSame(\log(24, 2), $number->value()); - } - - public function testNaturalLogarithm() - { - $number = Factorial::of(4)->naturalLogarithm(); - - $this->assertInstanceOf(NaturalLogarithm::class, $number); - $this->assertSame(\log(24), $number->value()); - } - - public function testCommonLogarithm() - { - $number = Factorial::of(4)->commonLogarithm(); - - $this->assertInstanceOf(CommonLogarithm::class, $number); - $this->assertSame(\log10(24), $number->value()); - } - public function testSignum() { - $number = Factorial::of(4)->signum(); + $number = Factorial::of(4)->number()->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } - /** - * @dataProvider factorials - */ + #[DataProvider('factorials')] public function testResult($integer, $expected) { - $number = Factorial::of($integer); + $number = Factorial::of($integer)->number(); - $this->assertInstanceOf(Number::class, $number->result()); - $this->assertSame($expected, $number->result()->value()); + $this->assertSame($expected, $number->value()); } - public function factorials(): array + public static function factorials(): array { return [ [0, 1], diff --git a/tests/Algebra/FloorTest.php b/tests/Algebra/FloorTest.php index d458358..8f264ea 100644 --- a/tests/Algebra/FloorTest.php +++ b/tests/Algebra/FloorTest.php @@ -3,43 +3,23 @@ namespace Tests\Innmind\Math\Algebra; -use Innmind\Math\Algebra\{ - Floor, - Number, - Addition, - Subtraction, - Multiplication, - Division, - Round, - Ceil, - Modulo, - Absolute, - Power, - SquareRoot, - Exponential, - BinaryLogarithm, - NaturalLogarithm, - CommonLogarithm, - Signum, - Real, -}; -use PHPUnit\Framework\TestCase; +use Innmind\Math\Algebra\Number; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; class FloorTest extends TestCase { public function testInterface() { - $floor = Floor::of(Real::of(42.42)); + $floor = Number::of(42.42)->floor(); $this->assertInstanceOf(Number::class, $floor); } - /** - * @dataProvider values - */ + #[DataProvider('values')] public function testValue($number, $expected) { - $floor = Floor::of(Real::of($number)); + $floor = Number::of($number)->floor(); $this->assertSame($expected, $floor->value()); } @@ -48,168 +28,122 @@ public function testStringCast() { $this->assertSame( '42.0', - Floor::of(Real::of(42.45))->toString(), + Number::of(42.45)->floor()->toString(), ); } public function testEquals() { - $floor = Floor::of(Real::of(42.45)); + $floor = Number::of(42.45)->floor(); - $this->assertTrue($floor->equals(Real::of(42))); - $this->assertTrue($floor->equals(Real::of(42.0))); - $this->assertFalse($floor->equals(Real::of(43))); + $this->assertTrue($floor->equals(Number::of(42))); + $this->assertTrue($floor->equals(Number::of(42.0))); + $this->assertFalse($floor->equals(Number::of(43))); } public function testHigherThan() { - $floor = Floor::of(Real::of(42.45)); + $floor = Number::of(42.45)->floor(); - $this->assertTrue($floor->higherThan(Real::of(41.9))); - $this->assertFalse($floor->higherThan(Real::of(42.5))); + $this->assertTrue($floor->higherThan(Number::of(41.9))); + $this->assertFalse($floor->higherThan(Number::of(42.5))); } public function testAdd() { - $floor = Floor::of(Real::of(42.5)); - $number = $floor->add(Real::of(7)); + $floor = Number::of(42.5)->floor(); + $number = $floor->add(Number::of(7)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(49.0, $number->value()); } public function testSubtract() { - $floor = Floor::of(Real::of(42.5)); - $number = $floor->subtract(Real::of(7)); + $floor = Number::of(42.5)->floor(); + $number = $floor->subtract(Number::of(7)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(35.0, $number->value()); } public function testMultiplication() { - $floor = Floor::of(Real::of(42.5)); - $number = $floor->multiplyBy(Real::of(2)); + $floor = Number::of(42.5)->floor(); + $number = $floor->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(84.0, $number->value()); } public function testDivision() { - $floor = Floor::of(Real::of(42.5)); - $number = $floor->divideBy(Real::of(2)); + $floor = Number::of(42.5)->floor(); + $number = $floor->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(21.0, $number->value()); } - public function testRound() - { - $number = Floor::of(Real::of(42.45)); - - $this->assertEquals(Round::up($number, 2), $number->roundUp(2)); - $this->assertEquals(Round::down($number, 2), $number->roundDown(2)); - $this->assertEquals(Round::even($number, 2), $number->roundEven(2)); - $this->assertEquals(Round::odd($number, 2), $number->roundOdd(2)); - } - public function testFloor() { - $floor = Floor::of(Real::of(42.45)); + $floor = Number::of(42.45)->floor(); $number = $floor->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(42.0, $number->value()); } public function testCeil() { - $floor = Floor::of(Real::of(42.45)); + $floor = Number::of(42.45)->floor(); $number = $floor->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(42.0, $number->value()); } public function testModulo() { - $floor = Floor::of(Real::of(42.45)); - $number = $floor->modulo(Real::of(20)); + $floor = Number::of(42.45)->floor(); + $number = $floor->modulo(Number::of(20)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(2.0, $number->value()); } public function testAbsolute() { - $floor = Floor::of(Real::of(-42.45)); + $floor = Number::of(-42.45)->floor(); $number = $floor->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(43.0, $number->value()); } public function testPower() { - $floor = Floor::of(Real::of(2.5)); - $number = $floor->power(Real::of(2)); + $floor = Number::of(2.5)->floor(); + $number = $floor->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(4.0, $number->value()); } public function testSquareRoot() { - $floor = Floor::of(Real::of(4.5)); + $floor = Number::of(4.5)->floor(); $number = $floor->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(2.0, $number->value()); } public function testExponential() { - $number = Floor::of(Real::of(4.5))->exponential(); + $number = Number::of(4.5)->floor()->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\exp(4), $number->value()); } - public function testBinaryLogarithm() - { - $number = Floor::of(Real::of(3.5))->binaryLogarithm(); - - $this->assertInstanceOf(BinaryLogarithm::class, $number); - $this->assertSame(\log(3, 2), $number->value()); - } - - public function testNaturalLogarithm() - { - $number = Floor::of(Real::of(3.5))->naturalLogarithm(); - - $this->assertInstanceOf(NaturalLogarithm::class, $number); - $this->assertSame(\log(3), $number->value()); - } - - public function testCommonLogarithm() - { - $number = Floor::of(Real::of(3.5))->commonLogarithm(); - - $this->assertInstanceOf(CommonLogarithm::class, $number); - $this->assertSame(\log10(3), $number->value()); - } - public function testSignum() { - $number = Floor::of(Real::of(2))->signum(); + $number = Number::of(2)->floor()->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } - public function values(): array + public static function values(): array { return [ [42.4, 42.0], diff --git a/tests/Algebra/IntegerTest.php b/tests/Algebra/IntegerTest.php index 2f42efa..bf27e79 100644 --- a/tests/Algebra/IntegerTest.php +++ b/tests/Algebra/IntegerTest.php @@ -3,43 +3,14 @@ namespace Tests\Innmind\Math\Algebra; -use Innmind\Math\{ - Algebra\Integer, - Algebra\Number, - Algebra\Addition, - Algebra\Subtraction, - Algebra\Multiplication, - Algebra\Division, - Algebra\Floor, - Algebra\Ceil, - Algebra\Modulo, - Algebra\Absolute, - Algebra\Power, - Algebra\SquareRoot, - Algebra\Factorial, - Algebra\Exponential, - Algebra\BinaryLogarithm, - Algebra\NaturalLogarithm, - Algebra\CommonLogarithm, - Algebra\Signum, - Algebra\Real, - Exception\FactorialMustBePositive -}; -use PHPUnit\Framework\TestCase; +use Innmind\Math\Algebra\Number; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class IntegerTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - Number::class, - Integer::of(42), - ); - } - public function testInt() { - $number = Integer::of(42); + $number = Number::of(42); $this->assertSame(42, $number->value()); $this->assertSame('42', $number->toString()); @@ -47,56 +18,52 @@ public function testInt() public function testEquals() { - $this->assertTrue(Integer::of(42)->equals(Integer::of(42))); - $this->assertTrue(Integer::of(42)->equals(Real::of(42.0))); - $this->assertFalse(Integer::of(42)->equals(Real::of(42.24))); + $this->assertTrue(Number::of(42)->equals(Number::of(42))); + $this->assertTrue(Number::of(42)->equals(Number::of(42.0))); + $this->assertFalse(Number::of(42)->equals(Number::of(42.24))); } public function testHigherThan() { - $this->assertFalse(Integer::of(42)->higherThan(Integer::of(42))); - $this->assertTrue(Integer::of(42)->higherThan(Real::of(41.24))); + $this->assertFalse(Number::of(42)->higherThan(Number::of(42))); + $this->assertTrue(Number::of(42)->higherThan(Number::of(41.24))); } public function testAdd() { - $number = Integer::of(42); - $number = $number->add(Integer::of(66)); + $number = Number::of(42); + $number = $number->add(Number::of(66)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(108, $number->value()); } public function testSubtract() { - $number = Integer::of(42); - $number = $number->subtract(Integer::of(66)); + $number = Number::of(42); + $number = $number->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-24, $number->value()); } public function testDivideBy() { - $number = Integer::of(42); - $number = $number->divideBy(Integer::of(2)); + $number = Number::of(42); + $number = $number->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(21, $number->value()); } public function testMulitplyBy() { - $number = Integer::of(42); - $number = $number->multiplyBy(Integer::of(2)); + $number = Number::of(42); + $number = $number->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(84, $number->value()); } public function testRound() { - $number = Integer::of(42); + $number = Number::of(42); $this->assertEquals(42.0, $number->roundUp(1)->value()); $this->assertEquals(42.0, $number->roundDown(1)->value()); @@ -106,132 +73,63 @@ public function testRound() public function testFloor() { - $number = Integer::of(42); + $number = Number::of(42); $number = $number->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(42.0, $number->value()); } public function testCeil() { - $number = Integer::of(42); + $number = Number::of(42); $number = $number->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(42.0, $number->value()); } public function testModulo() { - $number = Integer::of(3); - $number = $number->modulo(Integer::of(2)); + $number = Number::of(3); + $number = $number->modulo(Number::of(2)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(1.0, $number->value()); } public function testAbsolute() { - $number = Integer::of(-9); + $number = Number::of(-9); $number = $number->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(9, $number->value()); } public function testPower() { - $number = Integer::of(-9); - $number = $number->power(Integer::of(2)); + $number = Number::of(-9); + $number = $number->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(81, $number->value()); } public function testSquareRoot() { - $number = Integer::of(4); + $number = Number::of(4); $number = $number->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(2.0, $number->value()); } - public function testFactorial() - { - $number = Integer::of(3); - - $this->assertInstanceOf(Factorial::class, $number->factorial()); - $this->assertSame('3!', $number->factorial()->toString()); - } - public function testExponential() { - $number = (Integer::of(4))->exponential(); + $number = Number::of(4)->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\exp(4), $number->value()); } - public function testBinaryLogarithm() - { - $number = Integer::of(4)->binaryLogarithm(); - - $this->assertInstanceOf(BinaryLogarithm::class, $number); - $this->assertSame(\log(4, 2), $number->value()); - } - - public function testNaturalLogarithm() - { - $number = Integer::of(4)->naturalLogarithm(); - - $this->assertInstanceOf(NaturalLogarithm::class, $number); - $this->assertSame(\log(4), $number->value()); - } - - public function testCommonLogarithm() - { - $number = Integer::of(4)->commonLogarithm(); - - $this->assertInstanceOf(CommonLogarithm::class, $number); - $this->assertSame(\log10(4), $number->value()); - } - public function testSignum() { - $number = Integer::of(4)->signum(); + $number = Number::of(4)->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } - - public function testThrowWhenNegativeFactorial() - { - $this->expectException(FactorialMustBePositive::class); - - Integer::of(-1)->factorial(); - } - - public function testIncrement() - { - $number = Integer::of(0); - $increment = $number->increment(); - - $this->assertInstanceOf(Integer::class, $increment); - $this->assertNotSame($number, $increment); - $this->assertSame(0, $number->value()); - $this->assertSame(1, $increment->value()); - } - - public function testDecrement() - { - $number = Integer::of(0); - $decrement = $number->decrement(); - - $this->assertInstanceOf(Integer::class, $decrement); - $this->assertNotSame($number, $decrement); - $this->assertSame(0, $number->value()); - $this->assertSame(-1, $decrement->value()); - } } diff --git a/tests/Algebra/ModuloTest.php b/tests/Algebra/ModuloTest.php index 26a3432..a72ec3c 100644 --- a/tests/Algebra/ModuloTest.php +++ b/tests/Algebra/ModuloTest.php @@ -3,59 +3,31 @@ namespace Tests\Innmind\Math\Algebra; -use Innmind\Math\Algebra\{ - Modulo, - Operation, - Number, - Addition, - Subtraction, - Multiplication, - Division, - Round, - Floor, - Ceil, - Absolute, - Power, - SquareRoot, - Exponential, - BinaryLogarithm, - NaturalLogarithm, - CommonLogarithm, - Signum, - Real, -}; -use PHPUnit\Framework\TestCase; +use Innmind\Math\Algebra\Number; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class ModuloTest extends TestCase { public function testInterface() { - $modulo = Modulo::of( - $this->createMock(Number::class), - $this->createMock(Number::class), - ); + $modulo = Number::one()->modulo(Number::one()); $this->assertInstanceOf(Number::class, $modulo); - $this->assertInstanceOf(Operation::class, $modulo); } public function testResult() { - $modulo = Modulo::of( - Real::of(42.24), - Real::of(2.1), + $modulo = Number::of(42.24)->modulo( + Number::of(2.1), ); - $result = $modulo->result(); - $this->assertInstanceOf(Number::class, $result); - $this->assertEqualsWithDelta(0.24, $result->value(), 0.0001); + $this->assertEqualsWithDelta(0.24, $modulo->value(), 0.0001); } public function testStringCast() { - $modulo = Modulo::of( - Real::of(42.24), - Real::of(2.1), + $modulo = Number::of(42.24)->modulo( + Number::of(2.1), ); $this->assertSame('42.24 % 2.1', $modulo->toString()); @@ -63,75 +35,62 @@ public function testStringCast() public function testStringCastOperations() { - $modulo = Modulo::of( - Addition::of( - Real::of(1), - Real::of(1), - ), - Addition::of( - Real::of(2), - Real::of(2), - ), - ); + $modulo = Number::of(1) + ->add(Number::of(1)) + ->modulo( + Number::of(2)->add(Number::of(2)), + ); $this->assertSame('(1 + 1) % (2 + 2)', $modulo->toString()); } public function testEquals() { - $modulo = Modulo::of( - Real::of(42.24), - Real::of(2.1), + $modulo = Number::of(42.24)->modulo( + Number::of(2.1), ); - $this->assertTrue($modulo->equals(Real::of(0.2400000000000002))); - $this->assertFalse($modulo->equals(Real::of(1.24))); + $this->assertTrue($modulo->equals(Number::of(0.2400000000000002))); + $this->assertFalse($modulo->equals(Number::of(1.24))); } public function testHigherThan() { - $modulo = Modulo::of( - Real::of(42.24), - Real::of(2.1), + $modulo = Number::of(42.24)->modulo( + Number::of(2.1), ); - $this->assertTrue($modulo->higherThan(Real::of(0.23))); - $this->assertFalse($modulo->higherThan(Real::of(0.2400000000000002))); + $this->assertTrue($modulo->higherThan(Number::of(0.23))); + $this->assertFalse($modulo->higherThan(Number::of(0.2400000000000002))); } public function testAdd() { - $modulo = Modulo::of( - Real::of(42.24), - Real::of(2.1), + $modulo = Number::of(42.24)->modulo( + Number::of(2.1), ); - $number = $modulo->add(Real::of(66)); + $number = $modulo->add(Number::of(66)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(66.24, $number->value()); } public function testSubtract() { - $modulo = Modulo::of( - Real::of(42.24), - Real::of(2.1), + $modulo = Number::of(42.24)->modulo( + Number::of(2.1), ); - $number = $modulo->subtract(Real::of(66)); + $number = $modulo->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-65.76, $number->value()); } public function testDivideBy() { - $modulo = Modulo::of( - Real::of(42.24), - Real::of(2.1), + $modulo = Number::of(42.24)->modulo( + Number::of(2.1), ); - $number = $modulo->divideBy(Real::of(2)); + $number = $modulo->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertEqualsWithDelta( 0.12, $number->value(), @@ -141,62 +100,41 @@ public function testDivideBy() public function testMulitplyBy() { - $modulo = Modulo::of( - Real::of(42.24), - Real::of(2.1), + $modulo = Number::of(42.24)->modulo( + Number::of(2.1), ); - $number = $modulo->multiplyBy(Real::of(2)); + $number = $modulo->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertEqualsWithDelta(0.48, $number->value(), 0.0001); } - public function testRound() - { - $number = Modulo::of( - Real::of(42.24), - Real::of(2.1), - ); - - $this->assertEquals(Round::up($number, 2), $number->roundUp(2)); - $this->assertEquals(Round::down($number, 2), $number->roundDown(2)); - $this->assertEquals(Round::even($number, 2), $number->roundEven(2)); - $this->assertEquals(Round::odd($number, 2), $number->roundOdd(2)); - } - public function testFloor() { - $modulo = Modulo::of( - Real::of(42.24), - Real::of(2.1), + $modulo = Number::of(42.24)->modulo( + Number::of(2.1), ); $number = $modulo->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(0.0, $number->value()); } public function testCeil() { - $modulo = Modulo::of( - Real::of(42.24), - Real::of(2.1), + $modulo = Number::of(42.24)->modulo( + Number::of(2.1), ); $number = $modulo->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(1.0, $number->value()); } public function testModulo() { - $modulo = Modulo::of( - Real::of(42.24), - Real::of(2.1), + $modulo = Number::of(42.24)->modulo( + Number::of(2.1), ); - $number = $modulo->modulo(Real::of(0.1)); + $number = $modulo->modulo(Number::of(0.1)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertEqualsWithDelta( 0.04, $number->value(), @@ -206,92 +144,58 @@ public function testModulo() public function testAbsolute() { - $modulo = Modulo::of( - Real::of(-42.24), - Real::of(2.1), + $modulo = Number::of(-42.24)->modulo( + Number::of(2.1), ); $number = $modulo->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertEqualsWithDelta(0.24, $number->value(), 0.00001); } public function testPower() { - $modulo = Modulo::of( - Real::of(9), - Real::of(2), + $modulo = Number::of(9)->modulo( + Number::of(2), ); - $number = $modulo->power(Real::of(2)); + $number = $modulo->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(1.0, $number->value()); } public function testSquareRoot() { - $modulo = Modulo::of( - Real::of(12), - Real::of(8), + $modulo = Number::of(12)->modulo( + Number::of(8), ); $number = $modulo->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(2.0, $number->value()); } public function testExponential() { - $number = Modulo::of( - Real::of(12), - Real::of(8), - )->exponential(); + $number = Number::of(12) + ->modulo(Number::of(8)) + ->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\exp(4), $number->value()); } - public function testBinaryLogarithm() - { - $number = Modulo::of( - Real::of(12), - Real::of(8), - )->binaryLogarithm(); - - $this->assertInstanceOf(BinaryLogarithm::class, $number); - $this->assertSame(\log(4, 2), $number->value()); - } - - public function testNaturalLogarithm() - { - $number = Modulo::of( - Real::of(12), - Real::of(8), - )->naturalLogarithm(); - - $this->assertInstanceOf(NaturalLogarithm::class, $number); - $this->assertSame(\log(4), $number->value()); - } - - public function testCommonLogarithm() - { - $number = Modulo::of( - Real::of(12), - Real::of(8), - )->commonLogarithm(); - - $this->assertInstanceOf(CommonLogarithm::class, $number); - $this->assertSame(\log10(4), $number->value()); - } - public function testSignum() { - $number = Modulo::of( - Real::of(12), - Real::of(8), - )->signum(); + $number = Number::of(12) + ->modulo(Number::of(8)) + ->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } + + private function assertEqualsWithDelta( + int|float $expected, + int|float $value, + int|float $delta, + ): void { + $this->assertGreaterThanOrEqual($expected-$delta, $value); + $this->assertLessThanOrEqual($expected+$delta, $value); + } } diff --git a/tests/Algebra/MultiplicationTest.php b/tests/Algebra/MultiplicationTest.php index 3d85f65..0943051 100644 --- a/tests/Algebra/MultiplicationTest.php +++ b/tests/Algebra/MultiplicationTest.php @@ -3,61 +3,35 @@ namespace Tests\Innmind\Math\Algebra; -use Innmind\Math\Algebra\{ - Multiplication, - Number, - Operation, - Addition, - Subtraction, - Division, - Round, - Floor, - Ceil, - Modulo, - Absolute, - Power, - SquareRoot, - Exponential, - BinaryLogarithm, - NaturalLogarithm, - CommonLogarithm, - Signum, - Real, -}; -use PHPUnit\Framework\TestCase; +use Innmind\Math\Algebra\Number; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class MultiplicationTest extends TestCase { public function testInterface() { - $multiplication = Multiplication::of( - Real::of(4), - Real::of(42), + $multiplication = Number::of(4)->multiplyBy( + Number::of(42), ); - $this->assertInstanceOf(Operation::class, $multiplication); $this->assertInstanceOf(Number::class, $multiplication); $this->assertSame('4 x 42', $multiplication->toString()); } public function testResult() { - $multiplication = Multiplication::of( - Real::of(42), - Real::of(24), + $multiplication = Number::of(42)->multiplyBy( + Number::of(24), ); - $result = $multiplication->result(); - $this->assertInstanceOf(Number::class, $result); - $this->assertSame(1008, $result->value()); - $this->assertTrue($result->equals($multiplication->product())); + $this->assertInstanceOf(Number::class, $multiplication); + $this->assertSame(1008, $multiplication->value()); } public function testValue() { - $multiplication = Multiplication::of( - Real::of(4), - Real::of(2), + $multiplication = Number::of(4)->multiplyBy( + Number::of(2), ); $this->assertSame(8, $multiplication->value()); @@ -65,224 +39,148 @@ public function testValue() public function testEquals() { - $multiplication = Multiplication::of( - Real::of(4), - Real::of(2), + $multiplication = Number::of(4)->multiplyBy( + Number::of(2), ); - $this->assertTrue($multiplication->equals(Real::of(8))); - $this->assertFalse($multiplication->equals(Real::of(8.1))); + $this->assertTrue($multiplication->equals(Number::of(8))); + $this->assertFalse($multiplication->equals(Number::of(8.1))); } public function testHigherThan() { - $multiplication = Multiplication::of( - Real::of(4), - Real::of(2), + $multiplication = Number::of(4)->multiplyBy( + Number::of(2), ); - $this->assertFalse($multiplication->higherThan(Real::of(8))); - $this->assertTrue($multiplication->higherThan(Real::of(7.9))); + $this->assertFalse($multiplication->higherThan(Number::of(8))); + $this->assertTrue($multiplication->higherThan(Number::of(7.9))); } public function testAdd() { - $multiplication = Multiplication::of( - Real::of(4), - Real::of(2), + $multiplication = Number::of(4)->multiplyBy( + Number::of(2), ); - $number = $multiplication->add(Real::of(66)); + $number = $multiplication->add(Number::of(66)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(74, $number->value()); } public function testSubtract() { - $multiplication = Multiplication::of( - Real::of(4), - Real::of(2), + $multiplication = Number::of(4)->multiplyBy( + Number::of(2), ); - $number = $multiplication->subtract(Real::of(66)); + $number = $multiplication->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-58, $number->value()); } public function testDivideBy() { - $multiplication = Multiplication::of( - Real::of(4.5), - Real::of(2), + $multiplication = Number::of(4.5)->multiplyBy( + Number::of(2), ); - $number = $multiplication->divideBy(Real::of(3)); + $number = $multiplication->divideBy(Number::of(3)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(3.0, $number->value()); } public function testMulitplyBy() { - $multiplication = Addition::of( - Real::of(24), - Real::of(42), + $multiplication = Number::of(24)->add( + Number::of(42), ); - $number = $multiplication->multiplyBy(Real::of(2)); + $number = $multiplication->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(132, $number->value()); } - public function testRound() - { - $number = Multiplication::of( - Real::of(2.22), - Real::of(3), - ); - - $this->assertEquals(Round::up($number, 2), $number->roundUp(2)); - $this->assertEquals(Round::down($number, 2), $number->roundDown(2)); - $this->assertEquals(Round::even($number, 2), $number->roundEven(2)); - $this->assertEquals(Round::odd($number, 2), $number->roundOdd(2)); - } - public function testFloor() { - $multiplication = Multiplication::of( - Real::of(2.22), - Real::of(3), + $multiplication = Number::of(2.22)->multiplyBy( + Number::of(3), ); $number = $multiplication->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(6.0, $number->value()); } public function testCeil() { - $multiplication = Multiplication::of( - Real::of(2.22), - Real::of(3), + $multiplication = Number::of(2.22)->multiplyBy( + Number::of(3), ); $number = $multiplication->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(7.0, $number->value()); } public function testModulo() { - $multiplication = Multiplication::of( - Real::of(3), - Real::of(3), + $multiplication = Number::of(3)->multiplyBy( + Number::of(3), ); - $number = $multiplication->modulo(Real::of(2)); + $number = $multiplication->modulo(Number::of(2)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(1.0, $number->value()); } public function testAbsolute() { - $multiplication = Multiplication::of( - Real::of(-3), - Real::of(3), + $multiplication = Number::of(-3)->multiplyBy( + Number::of(3), ); $number = $multiplication->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(9, $number->value()); } public function testPower() { - $multiplication = Multiplication::of( - Real::of(-3), - Real::of(3), + $multiplication = Number::of(-3)->multiplyBy( + Number::of(3), ); - $number = $multiplication->power(Real::of(2)); + $number = $multiplication->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(81, $number->value()); } public function testSquareRoot() { - $multiplication = Multiplication::of( - Real::of(2), - Real::of(2), + $multiplication = Number::of(2)->multiplyBy( + Number::of(2), ); $number = $multiplication->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(2.0, $number->value()); } public function testExponential() { - $number = Multiplication::of( - Real::of(2), - Real::of(2), - )->exponential(); + $number = Number::of(2) + ->multiplyBy(Number::of(2)) + ->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\exp(4), $number->value()); } - public function testBinaryLogarithm() - { - $number = Multiplication::of( - Real::of(2), - Real::of(2), - )->binaryLogarithm(); - - $this->assertInstanceOf(BinaryLogarithm::class, $number); - $this->assertSame(\log(4, 2), $number->value()); - } - - public function testNaturalLogarithm() - { - $number = Multiplication::of( - Real::of(2), - Real::of(2), - )->naturalLogarithm(); - - $this->assertInstanceOf(NaturalLogarithm::class, $number); - $this->assertSame(\log(4), $number->value()); - } - - public function testCommonLogarithm() - { - $number = Multiplication::of( - Real::of(2), - Real::of(2), - )->commonLogarithm(); - - $this->assertInstanceOf(CommonLogarithm::class, $number); - $this->assertSame(\log10(4), $number->value()); - } - public function testSignum() { - $number = Multiplication::of( - Real::of(2), - Real::of(2), - )->signum(); + $number = Number::of(2) + ->multiplyBy(Number::of(2)) + ->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } public function testStringCast() { - $multiplication = Multiplication::of( - Addition::of( - Real::of(12), - Real::of(12), - ), - Real::of(42), - Real::of(66), - ); + $multiplication = Number::of(12) + ->add(Number::of(12)) + ->multiplyBy(Number::of(42)) + ->multiplyBy(Number::of(66)); $this->assertSame('(12 + 12) x 42 x 66', $multiplication->toString()); } @@ -291,9 +189,9 @@ public function testDistributivity() { //a(b+c) === ab + ac $this->assertTrue( - ($a = Real::of(2)) + ($a = Number::of(2)) ->multiplyBy( - ($b = Real::of(3))->add($c = Real::of(4)), + ($b = Number::of(3))->add($c = Number::of(4)), ) ->equals( $a @@ -307,10 +205,10 @@ public function testDoubleDistributivity() { //(a+b)(c+d) === ac + ad + bc + bd $this->assertTrue( - ($a = Real::of(2)) - ->add($b = Real::of(3)) + ($a = Number::of(2)) + ->add($b = Number::of(3)) ->multiplyBy( - ($c = Real::of(4))->add($d = Real::of(5)), + ($c = Number::of(4))->add($d = Number::of(5)), ) ->equals( $a @@ -326,47 +224,47 @@ public function testRemarkableIdentity() { //(a+b)^2 = a^2 + 2ab + b^2 $this->assertTrue( - ($a = Real::of(2)) - ->add($b = Real::of(3)) - ->power(Real::of(2)) + ($a = Number::of(2)) + ->add($b = Number::of(3)) + ->power(Number::of(2)) ->equals( $a - ->power(Real::of(2)) + ->power(Number::of(2)) ->add( - (Real::of(2)) + (Number::of(2)) ->multiplyBy($a) ->multiplyBy($b), ) - ->add($b->power(Real::of(2))), + ->add($b->power(Number::of(2))), ), ); //(a-b)^2 = a^2 - 2ab + b^2 $this->assertTrue( - ($a = Real::of(2)) - ->subtract($b = Real::of(3)) - ->power(Real::of(2)) + ($a = Number::of(2)) + ->subtract($b = Number::of(3)) + ->power(Number::of(2)) ->equals( $a - ->power(Real::of(2)) + ->power(Number::of(2)) ->subtract( - (Real::of(2)) + (Number::of(2)) ->multiplyBy($a) ->multiplyBy($b), ) - ->add($b->power(Real::of(2))), + ->add($b->power(Number::of(2))), ), ); //(a+b)(a-b) = a^2 - b^2 $this->assertTrue( - ($a = Real::of(2)) - ->add($b = Real::of(3)) + ($a = Number::of(2)) + ->add($b = Number::of(3)) ->multiplyBy($a->subtract($b)) ->equals( $a - ->power(Real::of(2)) - ->subtract($b->power(Real::of(2))), + ->power(Number::of(2)) + ->subtract($b->power(Number::of(2))), ), ); } @@ -375,19 +273,19 @@ public function testCollapse() { $this->assertSame( 3, - Real::of(3) - ->divideBy(Real::of(2)) - ->multiplyBy(Real::of(2)) - ->collapse() + Number::of(3) + ->divideBy(Number::of(2)) + ->multiplyBy(Number::of(2)) + ->optimize() ->value(), ); $this->assertSame( 3, - Real::of(2) + Number::of(2) ->multiplyBy( - Real::of(3)->divideBy(Real::of(2)), + Number::of(3)->divideBy(Number::of(2)), ) - ->collapse() + ->optimize() ->value(), ); } diff --git a/tests/Algebra/NaturalLogarithmTest.php b/tests/Algebra/NaturalLogarithmTest.php index 872e415..0320a99 100644 --- a/tests/Algebra/NaturalLogarithmTest.php +++ b/tests/Algebra/NaturalLogarithmTest.php @@ -4,59 +4,38 @@ namespace Tests\Innmind\Math\Algebra; use Innmind\Math\{ - Algebra\NaturalLogarithm, - Algebra\SquareRoot, - Algebra\Ceil, - Algebra\Floor, - Algebra\Operation, + Algebra\Logarithm, Algebra\Number, - Algebra\Addition, - Algebra\Subtraction, - Algebra\Multiplication, - Algebra\Division, - Algebra\Round, - Algebra\Modulo, - Algebra\Absolute, - Algebra\Power, - Algebra\Exponential, - Algebra\BinaryLogarithm, - Algebra\CommonLogarithm, - Algebra\Signum, - Algebra\Real, - DefinitionSet\Set, Exception\OutOfDefinitionSet }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class NaturalLogarithmTest extends TestCase { public function testInterface() { - $ln = NaturalLogarithm::of(Real::of(42.42)); + $ln = Number::of(42.42)->apply(Logarithm::natural); $this->assertInstanceOf(Number::class, $ln); - $this->assertInstanceOf(Operation::class, $ln); } public function testThrowWhenNotAllowedValue() { $this->expectException(OutOfDefinitionSet::class); - NaturalLogarithm::of(Real::of(0)); + Number::of(0)->apply(Logarithm::natural)->memoize(); } public function testResult() { - $ln = NaturalLogarithm::of(Real::of(1)); - $result = $ln->result(); + $ln = Number::of(1)->apply(Logarithm::natural); - $this->assertInstanceOf(Number::class, $result); - $this->assertSame(0.0, $result->value()); + $this->assertSame(0.0, $ln->value()); } public function testValue() { - $ln = NaturalLogarithm::of(Real::of(1)); + $ln = Number::of(1)->apply(Logarithm::natural); $this->assertSame(0.0, $ln->value()); } @@ -65,187 +44,153 @@ public function testStringCast() { $this->assertSame( 'ln(4)', - NaturalLogarithm::of(Real::of(4))->toString(), + Number::of(4)->apply(Logarithm::natural)->toString(), ); } public function testEquals() { - $ln = NaturalLogarithm::of(Real::of(1)); + $ln = Number::of(1)->apply(Logarithm::natural); - $this->assertTrue($ln->equals(Real::of(0))); - $this->assertFalse($ln->equals(Real::of(0.1))); + $this->assertTrue($ln->equals(Number::of(0))); + $this->assertFalse($ln->equals(Number::of(0.1))); } public function testHigherThan() { - $ln = NaturalLogarithm::of(Real::of(1)); + $ln = Number::of(1)->apply(Logarithm::natural); - $this->assertTrue($ln->higherThan(Real::of(-0.1))); - $this->assertFalse($ln->higherThan(Real::of(0))); + $this->assertTrue($ln->higherThan(Number::of(-0.1))); + $this->assertFalse($ln->higherThan(Number::of(0))); } public function testAdd() { - $ln = NaturalLogarithm::of(Real::of(1)); - $number = $ln->add(Real::of(7)); + $ln = Number::of(1)->apply(Logarithm::natural); + $number = $ln->add(Number::of(7)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(7.0, $number->value()); } public function testSubtract() { - $ln = NaturalLogarithm::of(Real::of(1)); - $number = $ln->subtract(Real::of(7)); + $ln = Number::of(1)->apply(Logarithm::natural); + $number = $ln->subtract(Number::of(7)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-7.0, $number->value()); } public function testMultiplication() { - $ln = NaturalLogarithm::of(Real::of(1)); - $number = $ln->multiplyBy(Real::of(2)); + $ln = Number::of(1)->apply(Logarithm::natural); + $number = $ln->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(0.0, $number->value()); } public function testDivision() { - $ln = NaturalLogarithm::of(Real::of(1)); - $number = $ln->divideBy(Real::of(2)); + $ln = Number::of(1)->apply(Logarithm::natural); + $number = $ln->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(0.0, $number->value()); } - public function testRound() - { - $number = NaturalLogarithm::of(Real::of(1)); - - $this->assertEquals(Round::up($number, 2), $number->roundUp(2)); - $this->assertEquals(Round::down($number, 2), $number->roundDown(2)); - $this->assertEquals(Round::even($number, 2), $number->roundEven(2)); - $this->assertEquals(Round::odd($number, 2), $number->roundOdd(2)); - } - public function testFloor() { - $ln = NaturalLogarithm::of(Real::of(1)); + $ln = Number::of(1)->apply(Logarithm::natural); $number = $ln->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(0.0, $number->value()); } public function testCeil() { - $ln = NaturalLogarithm::of(Real::of(1)); + $ln = Number::of(1)->apply(Logarithm::natural); $number = $ln->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(0.0, $number->value()); } public function testAbsolute() { - $ln = NaturalLogarithm::of(Real::of(0.5)); + $ln = Number::of(0.5)->apply(Logarithm::natural); $number = $ln->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(0.6931471805599453, $number->value()); } public function testModulo() { - $ln = NaturalLogarithm::of(Real::of(1)); - $number = $ln->modulo(Real::of(2)); + $ln = Number::of(1)->apply(Logarithm::natural); + $number = $ln->modulo(Number::of(2)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(0.0, $number->value()); } public function testPower() { - $ln = NaturalLogarithm::of(Real::of(1)); - $number = $ln->power(Real::of(2)); + $ln = Number::of(1)->apply(Logarithm::natural); + $number = $ln->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(0.0, $number->value()); } public function testSquareRoot() { - $ln = NaturalLogarithm::of(Real::of(1)); + $ln = Number::of(1)->apply(Logarithm::natural); $number = $ln->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(0.0, $number->value()); } public function testExponential() { - $number = NaturalLogarithm::of(Real::of(1))->exponential(); + $number = Number::of(1)->apply(Logarithm::natural)->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(1.0, $number->value()); } public function testBinaryLogarithm() { - $number = NaturalLogarithm::of(Real::of(2))->binaryLogarithm(); + $number = Number::of(2)->apply(Logarithm::natural)->apply(Logarithm::base2); - $this->assertInstanceOf(BinaryLogarithm::class, $number); $this->assertSame(\log(\log(2), 2), $number->value()); } public function testNaturalLogarithm() { - $number = NaturalLogarithm::of(Real::of(2))->naturalLogarithm(); + $number = Number::of(2)->apply(Logarithm::natural)->apply(Logarithm::baseE); - $this->assertInstanceOf(NaturalLogarithm::class, $number); $this->assertSame(\log(\log(2)), $number->value()); } public function testCommonLogarithm() { - $number = NaturalLogarithm::of(Real::of(2))->commonLogarithm(); + $number = Number::of(2)->apply(Logarithm::natural)->apply(Logarithm::base10); - $this->assertInstanceOf(CommonLogarithm::class, $number); $this->assertSame(\log10(\log(2)), $number->value()); } public function testSignum() { - $number = NaturalLogarithm::of(Real::of(2))->signum(); + $number = Number::of(2)->apply(Logarithm::natural)->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } - public function testDefinitionSet() - { - $set = BinaryLogarithm::definitionSet(); - - $this->assertInstanceOf(Set::class, $set); - $this->assertSame(']0;+∞[', $set->toString()); - } - public function testLogarithmMultiplication() { //ln(axb) === ln(a) + ln(b) $this->assertTrue( - NaturalLogarithm::of( - ($a = Real::of(2))->multiplyBy( - $b = Real::of(4), + ($a = Number::of(2)) + ->multiplyBy($b = Number::of(4)) + ->apply(Logarithm::natural) + ->equals( + $a->apply(Logarithm::natural)->add( + $b->apply(Logarithm::natural), + ), ), - )->equals( - NaturalLogarithm::of($a)->add( - NaturalLogarithm::of($b), - ), - ), ); } @@ -253,15 +198,14 @@ public function testLogarithmDivision() { //ln(a/b) === ln(a) - ln(b) $this->assertTrue( - NaturalLogarithm::of( - ($a = Real::of(2))->divideBy( - $b = Real::of(4), - ), - )->equals( - NaturalLogarithm::of($a)->subtract( - NaturalLogarithm::of($b), + ($a = Number::of(2)) + ->divideBy($b = Number::of(4)) + ->apply(Logarithm::natural) + ->equals( + $a->apply(Logarithm::natural)->subtract( + $b->apply(Logarithm::natural), + ), ), - ), ); } } diff --git a/tests/Algebra/Number/ETest.php b/tests/Algebra/Number/ETest.php index fd5cc47..4e2280b 100644 --- a/tests/Algebra/Number/ETest.php +++ b/tests/Algebra/Number/ETest.php @@ -3,191 +3,134 @@ namespace Tests\Innmind\Math\Algebra\Number; -use Innmind\Math\Algebra\{ - Value, - Number, - Addition, - Subtraction, - Multiplication, - Division, - Round, - Floor, - Ceil, - Modulo, - Absolute, - Power, - SquareRoot, - Exponential, - BinaryLogarithm, - NaturalLogarithm, - CommonLogarithm, - Signum, - Real, -}; -use PHPUnit\Framework\TestCase; +use Innmind\Math\Algebra\Number; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class ETest extends TestCase { public function testInterface() { - $this->assertInstanceOf(Number::class, Value::e); + $this->assertInstanceOf(Number::class, Number::e()); } public function testStringCast() { - $this->assertSame('e', Value::e->toString()); + $this->assertSame('e', Number::e()->toString()); } public function testEquals() { - $this->assertTrue(Value::e->equals(Real::of(\M_E))); - $this->assertFalse(Value::e->equals(Real::of(2.718))); + $this->assertTrue(Number::e()->equals(Number::of(\M_E))); + $this->assertFalse(Number::e()->equals(Number::of(2.718))); } public function testHigherThan() { - $this->assertTrue(Value::e->higherThan(Real::of(2.718))); - $this->assertFalse(Value::e->higherThan(Real::of(\M_E))); + $this->assertTrue(Number::e()->higherThan(Number::of(2.718))); + $this->assertFalse(Number::e()->higherThan(Number::of(\M_E))); } public function testAdd() { - $number = Value::e; - $number = $number->add(Real::of(66)); + $number = Number::e(); + $number = $number->add(Number::of(66)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(68.71828182845904, $number->value()); } public function testSubtract() { - $number = Value::e; - $number = $number->subtract(Real::of(2)); + $number = Number::e(); + $number = $number->subtract(Number::of(2)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(0.7182818284590451, $number->value()); } public function testDivideBy() { - $number = Value::e; - $number = $number->divideBy(Real::of(2)); + $number = Number::e(); + $number = $number->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(\M_E / 2, $number->value()); } public function testMulitplyBy() { - $number = Value::e; - $number = $number->multiplyBy(Real::of(2)); + $number = Number::e(); + $number = $number->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(\M_E * 2, $number->value()); } - public function testRound() - { - $number = Value::e; - - $this->assertEquals(Round::up($number, 2), $number->roundUp(2)); - $this->assertEquals(Round::down($number, 2), $number->roundDown(2)); - $this->assertEquals(Round::even($number, 2), $number->roundEven(2)); - $this->assertEquals(Round::odd($number, 2), $number->roundOdd(2)); - } - public function testFloor() { - $number = Value::e; + $number = Number::e(); $number = $number->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(2.0, $number->value()); } public function testCeil() { - $number = Value::e; + $number = Number::e(); $number = $number->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(3.0, $number->value()); } public function testModulo() { - $number = Value::e; - $number = $number->modulo(Real::of(2)); + $number = Number::e(); + $number = $number->modulo(Number::of(2)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(0.7182818284590451, $number->value()); } public function testAbsolute() { - $number = Value::e; + $number = Number::e(); $number = $number->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(\M_E, $number->value()); } public function testPower() { - $number = Value::e; - $number = $number->power(Real::of(2)); + $number = Number::e(); + $number = $number->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertEqualsWithDelta(\exp(2), $number->value(), 0.00000000000001); $this->assertSame(7.3890560989306495, $number->value()); } public function testSquareRoot() { - $number = Value::e; + $number = Number::e(); $number = $number->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(\sqrt(\M_E), $number->value()); } public function testExponential() { - $number = Value::e->exponential(); + $number = Number::e()->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\exp(\M_E), $number->value()); } - public function testBinaryLogarithm() - { - $number = Value::e->binaryLogarithm(); - - $this->assertInstanceOf(BinaryLogarithm::class, $number); - $this->assertSame(\log(\M_E, 2), $number->value()); - } - - public function testNaturalLogarithm() - { - $number = Value::e->naturalLogarithm(); - - $this->assertInstanceOf(NaturalLogarithm::class, $number); - $this->assertSame(\log(\M_E), $number->value()); - } - - public function testCommonLogarithm() - { - $number = Value::e->commonLogarithm(); - - $this->assertInstanceOf(CommonLogarithm::class, $number); - $this->assertSame(\log10(\M_E), $number->value()); - } - public function testSignum() { - $number = Value::e->signum(); + $number = Number::e()->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } + + private function assertEqualsWithDelta( + int|float $expected, + int|float $value, + int|float $delta, + ): void { + $this->assertGreaterThanOrEqual($expected-$delta, $value); + $this->assertLessThanOrEqual($expected+$delta, $value); + } } diff --git a/tests/Algebra/Number/InfiniteTest.php b/tests/Algebra/Number/InfiniteTest.php index ecc6028..c165a93 100644 --- a/tests/Algebra/Number/InfiniteTest.php +++ b/tests/Algebra/Number/InfiniteTest.php @@ -4,156 +4,122 @@ namespace Tests\Innmind\Math\Algebra\Number; use Innmind\Math\{ - Algebra\Value, Algebra\Number, - Algebra\Addition, - Algebra\Subtraction, - Algebra\Multiplication, - Algebra\Division, - Algebra\Round, - Algebra\Floor, - Algebra\Ceil, - Algebra\Absolute, - Algebra\Power, - Algebra\SquareRoot, - Algebra\Exponential, - Algebra\Signum, - Algebra\Real, + Algebra\Logarithm, Exception\NotANumber, Exception\OutOfDefinitionSet }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class InfiniteTest extends TestCase { public function testInterface() { - $this->assertInstanceOf(Number::class, Value::infinite); + $this->assertInstanceOf(Number::class, Number::infinite()); } public function testStringCast() { - $this->assertSame('+∞', Value::infinite->toString()); - $this->assertSame('-∞', Value::negativeInfinite->toString()); + $this->assertSame('+∞', Number::infinite()->toString()); + $this->assertSame('-∞', Number::negativeInfinite()->toString()); } public function testEquals() { - $this->assertTrue((Value::infinite)->equals(Value::infinite)); - $this->assertFalse((Value::infinite)->equals(Real::of(3.14))); + $this->assertTrue(Number::infinite()->equals(Number::infinite())); + $this->assertFalse(Number::infinite()->equals(Number::of(3.14))); } public function testHigherThan() { - $this->assertTrue((Value::infinite)->higherThan(Real::of(3.14))); - $this->assertFalse((Value::infinite)->higherThan(Value::infinite)); + $this->assertTrue((Number::infinite())->higherThan(Number::of(3.14))); + $this->assertFalse((Number::infinite())->higherThan(Number::infinite())); } public function testAdd() { - $number = Value::infinite; - $number = $number->add(Real::of(66)); + $number = Number::infinite(); + $number = $number->add(Number::of(66)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(\INF, $number->value()); } public function testSubtract() { - $number = Value::infinite; - $number = $number->subtract(Real::of(66)); + $number = Number::infinite(); + $number = $number->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(\INF, $number->value()); } public function testDivideBy() { - $number = Value::infinite; - $number = $number->divideBy(Real::of(2)); + $number = Number::infinite(); + $number = $number->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(\INF, $number->value()); } public function testMulitplyBy() { - $number = Value::infinite; - $number = $number->multiplyBy(Real::of(2)); + $number = Number::infinite(); + $number = $number->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(\INF, $number->value()); } - public function testRound() - { - $number = Value::infinite; - - $this->assertEquals(Round::up($number, 2), $number->roundUp(2)); - $this->assertEquals(Round::down($number, 2), $number->roundDown(2)); - $this->assertEquals(Round::even($number, 2), $number->roundEven(2)); - $this->assertEquals(Round::odd($number, 2), $number->roundOdd(2)); - } - public function testFloor() { - $number = Value::infinite; + $number = Number::infinite(); $number = $number->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(\INF, $number->value()); } public function testCeil() { - $number = Value::infinite; - $number = $number->ceil(); + $number = Number::infinite(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(\INF, $number->value()); } public function testModulo() { - $number = Value::infinite; + $number = Number::infinite(); $this->expectException(NotANumber::class); - $number->modulo(Real::of(1))->value(); + $number->modulo(Number::of(1))->value(); } public function testAbsolute() { - $number = Value::negativeInfinite; + $number = Number::negativeInfinite(); $number = $number->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(\INF, $number->value()); } public function testPower() { - $number = Value::infinite; - $number = $number->power(Real::of(2)); + $number = Number::infinite(); + $number = $number->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(\INF, $number->value()); } public function testSquareRoot() { - $number = Value::infinite; + $number = Number::infinite(); $number = $number->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(\INF, $number->value()); } public function testExponential() { - $number = Value::infinite->exponential(); + $number = Number::infinite()->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\INF, $number->value()); } @@ -161,28 +127,27 @@ public function testBinaryLogarithm() { $this->expectException(OutOfDefinitionSet::class); - Value::infinite->binaryLogarithm(); + Number::infinite()->apply(Logarithm::base2)->memoize(); } public function testNaturalLogarithm() { $this->expectException(OutOfDefinitionSet::class); - Value::infinite->naturalLogarithm(); + Number::infinite()->apply(Logarithm::baseE)->memoize(); } public function testCommonLogarithm() { $this->expectException(OutOfDefinitionSet::class); - Value::infinite->commonLogarithm(); + Number::infinite()->apply(Logarithm::base10)->memoize(); } public function testSignum() { - $number = Value::infinite->signum(); + $number = Number::infinite()->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } } diff --git a/tests/Algebra/Number/PiTest.php b/tests/Algebra/Number/PiTest.php index e148991..e7fd422 100644 --- a/tests/Algebra/Number/PiTest.php +++ b/tests/Algebra/Number/PiTest.php @@ -3,194 +3,137 @@ namespace Tests\Innmind\Math\Algebra\Number; -use Innmind\Math\Algebra\{ - Value, - Number, - Addition, - Subtraction, - Multiplication, - Division, - Round, - Floor, - Ceil, - Modulo, - Absolute, - Power, - SquareRoot, - Exponential, - BinaryLogarithm, - NaturalLogarithm, - CommonLogarithm, - Signum, - Real, -}; -use PHPUnit\Framework\TestCase; +use Innmind\Math\Algebra\Number; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class PiTest extends TestCase { public function testInterface() { - $this->assertInstanceOf(Number::class, Value::pi); + $this->assertInstanceOf(Number::class, Number::pi()); } public function testStringCast() { - $this->assertSame('π', Value::pi->toString()); + $this->assertSame('π', Number::pi()->toString()); } public function testEquals() { - $this->assertTrue(Value::pi->equals(Real::of(3.141592653589793))); - $this->assertFalse(Value::pi->equals(Real::of(3.14))); + $this->assertTrue(Number::pi()->equals(Number::of(3.141592653589793))); + $this->assertFalse(Number::pi()->equals(Number::of(3.14))); } public function testHigherThan() { - $this->assertTrue(Value::pi->higherThan(Real::of(3.14))); - $this->assertFalse(Value::pi->higherThan(Real::of(3.15))); + $this->assertTrue(Number::pi()->higherThan(Number::of(3.14))); + $this->assertFalse(Number::pi()->higherThan(Number::of(3.15))); } public function testAdd() { - $number = Value::pi; - $number = $number->add(Real::of(66)); + $number = Number::pi(); + $number = $number->add(Number::of(66)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(69.1415926535898, $number->value()); } public function testSubtract() { - $number = Value::pi; - $number = $number->subtract(Real::of(66)); + $number = Number::pi(); + $number = $number->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-62.8584073464102, $number->value()); } public function testDivideBy() { - $number = Value::pi; - $number = $number->divideBy(Real::of(2)); + $number = Number::pi(); + $number = $number->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(\M_PI_2, $number->value()); } public function testMulitplyBy() { - $number = Value::pi; - $number = $number->multiplyBy(Real::of(2)); + $number = Number::pi(); + $number = $number->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(\pi() * 2, $number->value()); } - public function testRound() - { - $number = Value::pi; - - $this->assertEquals(Round::up($number, 2), $number->roundUp(2)); - $this->assertEquals(Round::down($number, 2), $number->roundDown(2)); - $this->assertEquals(Round::even($number, 2), $number->roundEven(2)); - $this->assertEquals(Round::odd($number, 2), $number->roundOdd(2)); - } - public function testFloor() { - $number = Value::pi; + $number = Number::pi(); $number = $number->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(3.0, $number->value()); } public function testCeil() { - $number = Value::pi; + $number = Number::pi(); $number = $number->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(4.0, $number->value()); } public function testModulo() { - $number = Value::pi; - $number = $number->modulo(Real::of(0.1)); + $number = Number::pi(); + $number = $number->modulo(Number::of(0.1)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(0.041592653589792944, $number->value()); } public function testAbsolute() { - $number = Value::pi; + $number = Number::pi(); $number = $number->absolute(); - $this->assertInstanceOf(Absolute::class, $number); - $this->assertSame(Value::pi->value(), $number->value()); + $this->assertSame(Number::pi()->value(), $number->value()); } public function testPower() { - $number = Value::pi; - $number = $number->power(Real::of(2)); + $number = Number::pi(); + $number = $number->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame( - Value::pi->multiplyBy(Value::pi)->value(), + Number::pi()->multiplyBy(Number::pi())->value(), $number->value(), ); } public function testSquareRoot() { - $number = Value::pi; + $number = Number::pi(); $number = $number->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertEqualsWithDelta(\M_SQRTPI, $number->value(), 0.000000000000001); $this->assertSame(1.7724538509055159, $number->value()); } public function testExponential() { - $number = Value::pi->exponential(); + $number = Number::pi()->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\exp(\M_PI), $number->value()); } - public function testBinaryLogarithm() - { - $number = Value::pi->binaryLogarithm(); - - $this->assertInstanceOf(BinaryLogarithm::class, $number); - $this->assertSame(\log(\M_PI, 2), $number->value()); - } - - public function testNaturalLogarithm() - { - $number = Value::pi->naturalLogarithm(); - - $this->assertInstanceOf(NaturalLogarithm::class, $number); - $this->assertSame(\log(\M_PI), $number->value()); - } - - public function testCommonLogarithm() - { - $number = Value::pi->commonLogarithm(); - - $this->assertInstanceOf(CommonLogarithm::class, $number); - $this->assertSame(\log10(\M_PI), $number->value()); - } - public function testSignum() { - $number = Value::pi->signum(); + $number = Number::pi()->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } + + private function assertEqualsWithDelta( + int|float $expected, + int|float $value, + int|float $delta, + ): void { + $this->assertGreaterThanOrEqual($expected-$delta, $value); + $this->assertLessThanOrEqual($expected+$delta, $value); + } } diff --git a/tests/Algebra/PowerTest.php b/tests/Algebra/PowerTest.php index d25cbdd..dd79ac6 100644 --- a/tests/Algebra/PowerTest.php +++ b/tests/Algebra/PowerTest.php @@ -3,60 +3,33 @@ namespace Tests\Innmind\Math\Algebra; -use Innmind\Math\Algebra\{ - Power, - Operation, - Number, - Addition, - Subtraction, - Multiplication, - Division, - Round, - Floor, - Ceil, - Modulo, - Absolute, - SquareRoot, - Exponential, - BinaryLogarithm, - NaturalLogarithm, - CommonLogarithm, - Signum, - Integer, - Real, -}; -use PHPUnit\Framework\TestCase; +use Innmind\Math\Algebra\Number; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class PowerTest extends TestCase { public function testInterface() { - $power = Power::of( - $this->createMock(Number::class), - $this->createMock(Number::class), + $power = Number::one()->power( + Number::one(), ); $this->assertInstanceOf(Number::class, $power); - $this->assertInstanceOf(Operation::class, $power); } public function testResult() { - $power = Power::of( - Real::of(42.24), - Real::of(2.1), + $power = Number::of(42.24)->power( + Number::of(2.1), ); - $result = $power->result(); - $this->assertInstanceOf(Number::class, $result); - $this->assertSame(2594.300085723648, $result->value()); + $this->assertSame(2594.300085723648, $power->value()); } public function testStringCast() { - $power = Power::of( - Real::of(42.24), - Real::of(2.1), + $power = Number::of(42.24)->power( + Number::of(2.1), ); $this->assertSame('42.24^2.1', $power->toString()); @@ -64,227 +37,150 @@ public function testStringCast() public function testStringCastOperations() { - $power = Power::of( - Addition::of( - Real::of(1), - Real::of(1), - ), - Addition::of( - Real::of(2), - Real::of(2), - ), - ); + $power = Number::of(1) + ->add(Number::of(1)) + ->power( + Number::of(2)->add(Number::of(2)), + ); $this->assertSame('(1 + 1)^(2 + 2)', $power->toString()); } public function testEquals() { - $power = Power::of( - Real::of(2), - Real::of(2.1), + $power = Number::of(2)->power( + Number::of(2.1), ); - $this->assertTrue($power->equals(Real::of(4.2870938501451725))); - $this->assertFalse($power->equals(Real::of(4))); + $this->assertTrue($power->equals(Number::of(4.2870938501451725))); + $this->assertFalse($power->equals(Number::of(4))); } public function testHigherThan() { - $power = Power::of( - Real::of(2), - Real::of(2.1), + $power = Number::of(2)->power( + Number::of(2.1), ); - $this->assertTrue($power->higherThan(Real::of(4.28709385))); - $this->assertFalse($power->higherThan(Real::of(4.2870938501451725))); + $this->assertTrue($power->higherThan(Number::of(4.28709385))); + $this->assertFalse($power->higherThan(Number::of(4.2870938501451725))); } public function testAdd() { - $power = Power::of( - Real::of(2), - Real::of(2), + $power = Number::of(2)->power( + Number::of(2), ); - $number = $power->add(Real::of(66)); + $number = $power->add(Number::of(66)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(70, $number->value()); } public function testSubtract() { - $power = Power::of( - Real::of(2), - Real::of(2), + $power = Number::of(2)->power( + Number::of(2), ); - $number = $power->subtract(Real::of(66)); + $number = $power->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-62, $number->value()); } public function testDivideBy() { - $power = Power::of( - Real::of(2), - Real::of(2), + $power = Number::of(2)->power( + Number::of(2), ); - $number = $power->divideBy(Real::of(2)); + $number = $power->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(2, $number->value()); } public function testMulitplyBy() { - $power = Power::of( - Real::of(2), - Real::of(2), + $power = Number::of(2)->power( + Number::of(2), ); - $number = $power->multiplyBy(Real::of(2)); + $number = $power->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(8, $number->value()); } - public function testRound() - { - $number = Power::of( - Real::of(2), - Real::of(2), - ); - - $this->assertEquals(Round::up($number, 2), $number->roundUp(2)); - $this->assertEquals(Round::down($number, 2), $number->roundDown(2)); - $this->assertEquals(Round::even($number, 2), $number->roundEven(2)); - $this->assertEquals(Round::odd($number, 2), $number->roundOdd(2)); - } - public function testFloor() { - $power = Power::of( - Real::of(2), - Real::of(2), + $power = Number::of(2)->power( + Number::of(2), ); $number = $power->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(4.0, $number->value()); } public function testCeil() { - $power = Power::of( - Real::of(2), - Real::of(2), + $power = Number::of(2)->power( + Number::of(2), ); $number = $power->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(4.0, $number->value()); } public function testModulo() { - $power = Power::of( - Real::of(2), - Real::of(2), + $power = Number::of(2)->power( + Number::of(2), ); - $number = $power->modulo(Real::of(0.5)); + $number = $power->modulo(Number::of(0.5)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(0.0, $number->value()); } public function testAbsolute() { - $power = Power::of( - Real::of(-2), - Real::of(3), + $power = Number::of(-2)->power( + Number::of(3), ); $number = $power->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(8, $number->value()); } public function testPower() { - $power = Power::of( - Real::of(2), - Real::of(2), + $power = Number::of(2)->power( + Number::of(2), ); - $number = $power->power(Real::of(2)); + $number = $power->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(16, $number->value()); } public function testSquareRoot() { - $power = Power::of( - Real::of(2), - Real::of(2), + $power = Number::of(2)->power( + Number::of(2), ); $number = $power->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(2.0, $number->value()); } public function testExponential() { - $number = Power::of( - Real::of(2), - Real::of(2), - )->exponential(); + $number = Number::of(2) + ->power(Number::of(2)) + ->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\exp(4), $number->value()); } - public function testBinaryLogarithm() - { - $number = Power::of( - Real::of(2), - Real::of(2), - )->binaryLogarithm(); - - $this->assertInstanceOf(BinaryLogarithm::class, $number); - $this->assertSame(\log(4, 2), $number->value()); - } - - public function testNaturalLogarithm() - { - $number = Power::of( - Real::of(2), - Real::of(2), - )->naturalLogarithm(); - - $this->assertInstanceOf(NaturalLogarithm::class, $number); - $this->assertSame(\log(4), $number->value()); - } - - public function testCommonLogarithm() - { - $number = Power::of( - Real::of(2), - Real::of(2), - )->commonLogarithm(); - - $this->assertInstanceOf(CommonLogarithm::class, $number); - $this->assertSame(\log10(4), $number->value()); - } - public function testSignum() { - $number = Power::of( - Real::of(2), - Real::of(2), - )->signum(); + $number = Number::of(2) + ->power(Number::of(2)) + ->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } @@ -292,10 +188,10 @@ public function testNegativePower() { //a^-n === 1/(a^n) $this->assertTrue( - ($a = Real::of(2)) - ->power($n = Real::of(-3)) + ($a = Number::of(2)) + ->power($n = Number::of(-3)) ->equals( - (Real::of(1))->divideBy( + Number::of(1)->divideBy( $a->power( $n->absolute(), ), @@ -308,9 +204,9 @@ public function testPowerAddition() { //x^(a+b) === x^a + x^b $this->assertTrue( - ($x = Real::of(2)) + ($x = Number::of(2)) ->power( - ($a = Real::of(3))->add($b = Real::of(4)), + ($a = Number::of(3))->add($b = Number::of(4)), ) ->equals( $x @@ -326,9 +222,9 @@ public function testPowerSubtraction() { //x^(a-b) === x^a / x^b $this->assertTrue( - ($x = Real::of(2)) + ($x = Number::of(2)) ->power( - ($a = Real::of(3))->subtract($b = Real::of(4)), + ($a = Number::of(3))->subtract($b = Number::of(4)), ) ->equals( $x @@ -344,9 +240,9 @@ public function testPowerMultiplication() { //(x^a)^b === x^(a*b) $this->assertTrue( - ($x = Real::of(2)) - ->power($a = Real::of(3)) - ->power($b = Real::of(4)) + ($x = Number::of(2)) + ->power($a = Number::of(3)) + ->power($b = Number::of(4)) ->equals( $x->power( $a->multiplyBy($b), @@ -359,9 +255,9 @@ public function testPowerDistribution() { //(a*b)^n = a^n * b^n $this->assertTrue( - ($a = Real::of(2)) - ->multiplyBy($b = Real::of(3)) - ->power($n = Real::of(4)) + ($a = Number::of(2)) + ->multiplyBy($b = Number::of(3)) + ->power($n = Number::of(4)) ->equals( $a ->power($n) @@ -374,20 +270,20 @@ public function testPowerDistribution() public function testCollapseSquareRoot() { - $result = Integer::of(2) + $result = Number::of(2) ->squareRoot() - ->power(Integer::of(2)) - ->collapse() + ->power(Number::of(2)) + ->optimize() ->value(); $this->assertSame(2, $result); - $result = Integer::of(2) + $result = Number::of(2) ->squareRoot() - ->power(Integer::of(2)) + ->power(Number::of(2)) ->squareRoot() - ->power(Integer::of(2)) - ->collapse() + ->power(Number::of(2)) + ->optimize() ->value(); $this->assertSame(2, $result); diff --git a/tests/Algebra/RealTest.php b/tests/Algebra/RealTest.php index 00fbbc3..db1a4e9 100644 --- a/tests/Algebra/RealTest.php +++ b/tests/Algebra/RealTest.php @@ -4,73 +4,44 @@ namespace Tests\Innmind\Math\Algebra; use Innmind\Math\{ - Algebra\Real, Algebra\Number, - Algebra\Addition, - Algebra\Subtraction, - Algebra\Multiplication, - Algebra\Division, - Algebra\Round, - Algebra\Floor, - Algebra\Ceil, - Algebra\Modulo, - Algebra\Absolute, - Algebra\Power, - Algebra\SquareRoot, - Algebra\Exponential, - Algebra\BinaryLogarithm, - Algebra\NaturalLogarithm, - Algebra\CommonLogarithm, - Algebra\Integer, - Algebra\Value, - Algebra\Signum, Exception\NotANumber, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class RealTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - Number::class, - Real::of(42), - ); - } - public function testThrowWhenNotANumber() { $this->expectException(NotANumber::class); - Real::of(\NAN); + Number::of(\NAN); } public function testWrap() { - $number = Real::of(42.1); + $number = Number::of(42.1); - $this->assertInstanceOf(Real::class, $number); $this->assertSame('42.1', $number->toString()); - $number = Real::of(42); + $number = Number::of(42); - $this->assertInstanceOf(Integer::class, $number); $this->assertSame('42', $number->toString()); - $number = Real::of(\INF); + $number = Number::of(\INF); - $this->assertSame(Value::infinite, $number); + $this->assertEquals(Number::infinite(), $number); $this->assertSame('+∞', $number->toString()); - $number = Real::of(-\INF); + $number = Number::of(-\INF); - $this->assertSame(Value::negativeInfinite, $number); + $this->assertEquals(Number::negativeInfinite(), $number); $this->assertSame('-∞', $number->toString()); } public function testInt() { - $number = Real::of(42); + $number = Number::of(42); $this->assertSame(42, $number->value()); $this->assertSame('42', $number->toString()); @@ -78,7 +49,7 @@ public function testInt() public function testFloat() { - $number = Real::of(42.24); + $number = Number::of(42.24); $this->assertSame(42.24, $number->value()); $this->assertSame('42.24', $number->toString()); @@ -86,160 +57,114 @@ public function testFloat() public function testEquals() { - $this->assertTrue((Real::of(42))->equals(Real::of(42))); - $this->assertTrue((Real::of(42))->equals(Real::of(42.0))); - $this->assertTrue((Real::of(42.0))->equals(Real::of(42))); + $this->assertTrue(Number::of(42)->equals(Number::of(42))); + $this->assertTrue(Number::of(42)->equals(Number::of(42.0))); + $this->assertTrue(Number::of(42.0)->equals(Number::of(42))); $this->assertTrue( - Real::of(42.1)->equals(Real::of( + Number::of(42.1)->equals(Number::of( 42.099999999999999, # with a precision over 14 digits php will round it )), ); - $this->assertFalse((Real::of(42))->equals(Real::of(42.24))); + $this->assertFalse(Number::of(42)->equals(Number::of(42.24))); } public function testHigherThan() { - $this->assertFalse(Real::of(42)->higherThan(Real::of(42))); - $this->assertTrue(Real::of(42)->higherThan(Real::of(41.24))); + $this->assertFalse(Number::of(42)->higherThan(Number::of(42))); + $this->assertTrue(Number::of(42)->higherThan(Number::of(41.24))); } public function testAdd() { - $number = Real::of(42); - $number = $number->add(Real::of(66)); + $number = Number::of(42); + $number = $number->add(Number::of(66)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(108, $number->value()); } public function testSubtract() { - $number = Real::of(42); - $number = $number->subtract(Real::of(66)); + $number = Number::of(42); + $number = $number->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-24, $number->value()); } public function testDivideBy() { - $number = Real::of(42); - $number = $number->divideBy(Real::of(2)); + $number = Number::of(42); + $number = $number->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(21, $number->value()); } public function testMulitplyBy() { - $number = Real::of(42); - $number = $number->multiplyBy(Real::of(2)); + $number = Number::of(42); + $number = $number->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(84, $number->value()); } - public function testRound() - { - $number = Real::of(42.25); - - $this->assertEquals(Round::up($number, 1), $number->roundUp(1)); - $this->assertEquals(Round::down($number, 1), $number->roundDown(1)); - $this->assertEquals(Round::even($number, 1), $number->roundEven(1)); - $this->assertEquals(Round::odd($number, 1), $number->roundOdd(1)); - } - public function testFloor() { - $number = Real::of(42.25); + $number = Number::of(42.25); $number = $number->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(42.0, $number->value()); } public function testCeil() { - $number = Real::of(42.25); + $number = Number::of(42.25); $number = $number->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(43.0, $number->value()); } public function testModulo() { - $number = Real::of(3); - $number = $number->modulo(Real::of(2)); + $number = Number::of(3); + $number = $number->modulo(Number::of(2)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(1.0, $number->value()); } public function testAbsolute() { - $number = Real::of(-9); + $number = Number::of(-9); $number = $number->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(9, $number->value()); } public function testPower() { - $number = Real::of(-9); - $number = $number->power(Real::of(2)); + $number = Number::of(-9); + $number = $number->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(81, $number->value()); } public function testSquareRoot() { - $number = Real::of(4); + $number = Number::of(4); $number = $number->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(2.0, $number->value()); } public function testExponential() { - $number = Real::of(4)->exponential(); + $number = Number::of(4)->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\exp(4), $number->value()); } - public function testBinaryLogarithm() - { - $number = Real::of(4)->binaryLogarithm(); - - $this->assertInstanceOf(BinaryLogarithm::class, $number); - $this->assertSame(\log(4, 2), $number->value()); - } - - public function testNaturalLogarithm() - { - $number = Real::of(4)->naturalLogarithm(); - - $this->assertInstanceOf(NaturalLogarithm::class, $number); - $this->assertSame(\log(4), $number->value()); - } - - public function testCommonLogarithm() - { - $number = Real::of(4)->commonLogarithm(); - - $this->assertInstanceOf(CommonLogarithm::class, $number); - $this->assertSame(\log10(4), $number->value()); - } - public function testSignum() { - $number = Real::of(2)->signum(); + $number = Number::of(2)->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } } diff --git a/tests/Algebra/RoundTest.php b/tests/Algebra/RoundTest.php index 0e7dad7..7c5f51d 100644 --- a/tests/Algebra/RoundTest.php +++ b/tests/Algebra/RoundTest.php @@ -3,51 +3,23 @@ namespace Tests\Innmind\Math\Algebra; -use Innmind\Math\{ - Algebra\Round, - Algebra\Number, - Algebra\Addition, - Algebra\Subtraction, - Algebra\Multiplication, - Algebra\Division, - Algebra\Floor, - Algebra\Ceil, - Algebra\Modulo, - Algebra\Absolute, - Algebra\Power, - Algebra\SquareRoot, - Algebra\Exponential, - Algebra\BinaryLogarithm, - Algebra\NaturalLogarithm, - Algebra\CommonLogarithm, - Algebra\Signum, - Algebra\Real, - Exception\PrecisionMustBePositive -}; -use PHPUnit\Framework\TestCase; +use Innmind\Math\Algebra\Number; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; class RoundTest extends TestCase { public function testInterface() { - $round = Round::up(Real::of(42.42)); + $round = Number::of(42.42)->roundUp(); $this->assertInstanceOf(Number::class, $round); } - public function testThrowWhenNegativePrecision() - { - $this->expectException(PrecisionMustBePositive::class); - - Round::up(Real::of(42), -1); - } - - /** - * @dataProvider values - */ + #[DataProvider('values')] public function testValue($number, $expected, $precision, $mode) { - $round = Round::$mode(Real::of($number), $precision); + $round = Number::of($number)->$mode($precision); $this->assertSame($expected, $round->value()); } @@ -56,184 +28,138 @@ public function testStringCast() { $this->assertSame( '42.5', - (Round::up(Real::of(42.45), 1))->toString(), + Number::of(42.45)->roundUp(1)->toString(), ); } public function testEquals() { - $round = Round::up(Real::of(42.45), 1); + $round = Number::of(42.45)->roundUp(1); - $this->assertTrue($round->equals(Real::of(42.5))); - $this->assertTrue($round->equals(Real::of( + $this->assertTrue($round->equals(Number::of(42.5))); + $this->assertTrue($round->equals(Number::of( 42.499999999999999, # with a precision over 14 digits php will round it ))); - $this->assertFalse($round->equals(Real::of(42))); + $this->assertFalse($round->equals(Number::of(42))); } public function testHigherThan() { - $round = Round::up(Real::of(42.45), 1); + $round = Number::of(42.45)->roundUp(1); - $this->assertTrue($round->higherThan(Real::of(42.4))); - $this->assertFalse($round->higherThan(Real::of(42.5))); + $this->assertTrue($round->higherThan(Number::of(42.4))); + $this->assertFalse($round->higherThan(Number::of(42.5))); } public function testAdd() { - $round = Round::up(Real::of(42.5)); - $number = $round->add(Real::of(7)); + $round = Number::of(42.5)->roundUp(); + $number = $round->add(Number::of(7)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(50.0, $number->value()); } public function testSubtract() { - $round = Round::up(Real::of(42.5)); - $number = $round->subtract(Real::of(7)); + $round = Number::of(42.5)->roundUp(); + $number = $round->subtract(Number::of(7)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(36.0, $number->value()); } public function testMultiplication() { - $round = Round::up(Real::of(42.5)); - $number = $round->multiplyBy(Real::of(2)); + $round = Number::of(42.5)->roundUp(); + $number = $round->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(86.0, $number->value()); } public function testDivision() { - $round = Round::up(Real::of(42.5)); - $number = $round->divideBy(Real::of(2)); + $round = Number::of(42.5)->roundUp(); + $number = $round->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(21.5, $number->value()); } - public function testRound() - { - $number = Round::up(Real::of(42.45), 1); - - $this->assertEquals(Round::up($number, 2), $number->roundUp(2)); - $this->assertEquals(Round::down($number, 2), $number->roundDown(2)); - $this->assertEquals(Round::even($number, 2), $number->roundEven(2)); - $this->assertEquals(Round::odd($number, 2), $number->roundOdd(2)); - } - public function testFloor() { - $round = Round::up(Real::of(42.45), 1); + $round = Number::of(42.45)->roundUp(1); $number = $round->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(42.0, $number->value()); } public function testCeil() { - $round = Round::up(Real::of(42.45), 1); + $round = Number::of(42.45)->roundUp(1); $number = $round->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(43.0, $number->value()); } public function testModulo() { - $round = Round::up(Real::of(42.45), 1); - $number = $round->modulo(Real::of(21)); + $round = Number::of(42.45)->roundUp(1); + $number = $round->modulo(Number::of(21)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(0.5, $number->value()); } public function testAbsolute() { - $round = Round::up(Real::of(-42.45), 1); + $round = Number::of(-42.45)->roundUp(1); $number = $round->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(42.5, $number->value()); } public function testPower() { - $round = Round::up(Real::of(2.45), 1); - $number = $round->power(Real::of(2)); + $round = Number::of(2.45)->roundUp(1); + $number = $round->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(6.25, $number->value()); } public function testSquareRoot() { - $round = Round::up(Real::of(4.3)); + $round = Number::of(4.3)->roundUp(); $number = $round->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(2.0, $number->value()); } public function testExponential() { - $number = Round::up(Real::of(3.6))->exponential(); + $number = Number::of(3.6)->roundUp()->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\exp(4), $number->value()); } - public function testBinaryLogarithm() - { - $number = Round::up(Real::of(3.6))->binaryLogarithm(); - - $this->assertInstanceOf(BinaryLogarithm::class, $number); - $this->assertSame(\log(4, 2), $number->value()); - } - - public function testNaturalLogarithm() - { - $number = Round::up(Real::of(3.6))->naturalLogarithm(); - - $this->assertInstanceOf(NaturalLogarithm::class, $number); - $this->assertSame(\log(4), $number->value()); - } - - public function testCommonLogarithm() - { - $number = Round::up(Real::of(3.6))->commonLogarithm(); - - $this->assertInstanceOf(CommonLogarithm::class, $number); - $this->assertSame(\log10(4), $number->value()); - } - public function testSignum() { - $number = Round::up(Real::of(1))->signum(); + $number = Number::of(1)->roundUp()->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } - public function values(): array + public static function values(): array { return [ - [42.5, 43.0, 0, 'up'], - [42.5, 42.0, 0, 'down'], - [42.5, 42.0, 0, 'even'], - [42.5, 43.0, 0, 'odd'], - [42.51, 42.5, 1, 'up'], - [42.51, 42.5, 1, 'down'], - [42.51, 42.5, 1, 'even'], - [42.51, 42.5, 1, 'odd'], - [42.51, 42.51, 2, 'up'], - [42.51, 42.51, 2, 'down'], - [42.51, 42.51, 2, 'even'], - [42.51, 42.51, 2, 'odd'], + [42.5, 43.0, 0, 'roundUp'], + [42.5, 42.0, 0, 'roundDown'], + [42.5, 42.0, 0, 'roundEven'], + [42.5, 43.0, 0, 'roundOdd'], + [42.51, 42.5, 1, 'roundUp'], + [42.51, 42.5, 1, 'roundDown'], + [42.51, 42.5, 1, 'roundEven'], + [42.51, 42.5, 1, 'roundOdd'], + [42.51, 42.51, 2, 'roundUp'], + [42.51, 42.51, 2, 'roundDown'], + [42.51, 42.51, 2, 'roundEven'], + [42.51, 42.51, 2, 'roundOdd'], ]; } } diff --git a/tests/Algebra/SignumTest.php b/tests/Algebra/SignumTest.php index 09a120e..df76223 100644 --- a/tests/Algebra/SignumTest.php +++ b/tests/Algebra/SignumTest.php @@ -3,263 +3,153 @@ namespace Tests\Innmind\Math\Algebra; -use Innmind\Math\Algebra\{ - Signum, - Power, - Operation, - Number, - Addition, - Subtraction, - Multiplication, - Division, - Round, - Floor, - Ceil, - Modulo, - Absolute, - SquareRoot, - Exponential, - BinaryLogarithm, - NaturalLogarithm, - CommonLogarithm, - Real, -}; -use PHPUnit\Framework\TestCase; +use Innmind\Math\Algebra\Number; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class SignumTest extends TestCase { public function testInterface() { - $sgn = Signum::of( - $this->createMock(Number::class), - ); + $sgn = Number::one()->signum(); $this->assertInstanceOf(Number::class, $sgn); - $this->assertInstanceOf(Operation::class, $sgn); } public function testResult() { - $sgn = Signum::of( - Real::of(42), - ); - $result = $sgn->result(); + $sgn = Number::of(42)->signum(); - $this->assertInstanceOf(Number::class, $result); - $this->assertSame(1, $result->value()); + $this->assertSame(1, $sgn->value()); - $this->assertSame(-1, (Signum::of(Real::of(-42)))->value()); - $this->assertSame(0, (Signum::of(Real::of(0)))->value()); + $this->assertSame(-1, Number::of(-42)->signum()->value()); + $this->assertSame(0, Number::of(0)->signum()->value()); } public function testStringCast() { - $sgn = Signum::of( - Real::of(42.24), - ); + $sgn = Number::of(42.24)->signum(); $this->assertSame('sgn(42.24)', $sgn->toString()); } public function testStringCastOperations() { - $sgn = Signum::of( - Addition::of( - Real::of(1), - Real::of(1), - ), - ); + $sgn = Number::of(1) + ->add(Number::of(1)) + ->signum(); $this->assertSame('sgn(1 + 1)', $sgn->toString()); } public function testEquals() { - $sgn = Signum::of( - Real::of(2), - ); + $sgn = Number::of(2)->signum(); - $this->assertTrue($sgn->equals(Real::of(1))); - $this->assertFalse($sgn->equals(Real::of(0))); + $this->assertTrue($sgn->equals(Number::of(1))); + $this->assertFalse($sgn->equals(Number::of(0))); } public function testHigherThan() { - $sgn = Signum::of( - Real::of(2), - ); + $sgn = Number::of(2)->signum(); - $this->assertTrue($sgn->higherThan(Real::of(0))); - $this->assertFalse($sgn->higherThan(Real::of(1))); + $this->assertTrue($sgn->higherThan(Number::of(0))); + $this->assertFalse($sgn->higherThan(Number::of(1))); } public function testAdd() { - $sgn = Signum::of( - Real::of(2), - ); - $number = $sgn->add(Real::of(66)); + $sgn = Number::of(2)->signum(); + $number = $sgn->add(Number::of(66)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(67, $number->value()); } public function testSubtract() { - $sgn = Signum::of( - Real::of(2), - ); - $number = $sgn->subtract(Real::of(66)); + $sgn = Number::of(2)->signum(); + $number = $sgn->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-65, $number->value()); } public function testDivideBy() { - $sgn = Signum::of( - Real::of(2), - ); - $number = $sgn->divideBy(Real::of(2)); + $sgn = Number::of(2)->signum(); + $number = $sgn->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(0.5, $number->value()); } public function testMulitplyBy() { - $sgn = Signum::of( - Real::of(2), - ); - $number = $sgn->multiplyBy(Real::of(2)); + $sgn = Number::of(2)->signum(); + $number = $sgn->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(2, $number->value()); } - public function testRound() - { - $number = Signum::of( - Real::of(2), - ); - - $this->assertEquals(Round::up($number, 2), $number->roundUp(2)); - $this->assertEquals(Round::down($number, 2), $number->roundDown(2)); - $this->assertEquals(Round::even($number, 2), $number->roundEven(2)); - $this->assertEquals(Round::odd($number, 2), $number->roundOdd(2)); - } - public function testFloor() { - $sgn = Signum::of( - Real::of(2), - ); + $sgn = Number::of(2)->signum(); $number = $sgn->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(1.0, $number->value()); } public function testCeil() { - $sgn = Signum::of( - Real::of(2), - ); + $sgn = Number::of(2)->signum(); $number = $sgn->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(1.0, $number->value()); } public function testModulo() { - $sgn = Signum::of( - Real::of(2), - ); - $number = $sgn->modulo(Real::of(0.5)); + $sgn = Number::of(2)->signum(); + $number = $sgn->modulo(Number::of(0.5)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(0.0, $number->value()); } public function testAbsolute() { - $sgn = Signum::of( - Real::of(-2), - ); + $sgn = Number::of(-2)->signum(); $number = $sgn->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(1, $number->value()); } public function testPower() { - $sgn = Signum::of( - Real::of(2), - ); - $number = $sgn->power(Real::of(2)); + $sgn = Number::of(2)->signum(); + $number = $sgn->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(1, $number->value()); } public function testSquareRoot() { - $sgn = Signum::of( - Real::of(2), - ); + $sgn = Number::of(2)->signum(); $number = $sgn->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(1.0, $number->value()); } public function testExponential() { - $number = Signum::of( - Real::of(2), - )->exponential(); + $number = Number::of(2) + ->signum() + ->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\exp(1), $number->value()); } - public function testBinaryLogarithm() - { - $number = Signum::of( - Real::of(2), - )->binaryLogarithm(); - - $this->assertInstanceOf(BinaryLogarithm::class, $number); - $this->assertSame(\log(1, 2), $number->value()); - } - - public function testNaturalLogarithm() - { - $number = Signum::of( - Real::of(2), - )->naturalLogarithm(); - - $this->assertInstanceOf(NaturalLogarithm::class, $number); - $this->assertSame(\log(1), $number->value()); - } - - public function testCommonLogarithm() - { - $number = Signum::of( - Real::of(2), - )->commonLogarithm(); - - $this->assertInstanceOf(CommonLogarithm::class, $number); - $this->assertSame(\log10(1), $number->value()); - } - public function testSignum() { - $number = Signum::of(Real::of(1))->signum(); + $number = Number::of(1)->signum()->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } } diff --git a/tests/Algebra/SquareRootTest.php b/tests/Algebra/SquareRootTest.php index 4b3a7a9..4f9598d 100644 --- a/tests/Algebra/SquareRootTest.php +++ b/tests/Algebra/SquareRootTest.php @@ -3,52 +3,28 @@ namespace Tests\Innmind\Math\Algebra; -use Innmind\Math\Algebra\{ - SquareRoot, - Ceil, - Floor, - Operation, - Number, - Addition, - Subtraction, - Multiplication, - Division, - Round, - Modulo, - Absolute, - Power, - Exponential, - BinaryLogarithm, - NaturalLogarithm, - CommonLogarithm, - Signum, - Integer, - Real, -}; -use PHPUnit\Framework\TestCase; +use Innmind\Math\Algebra\Number; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class SquareRootTest extends TestCase { public function testInterface() { - $sqrt = SquareRoot::of(Real::of(42.42)); + $sqrt = Number::of(42.42)->squareRoot(); $this->assertInstanceOf(Number::class, $sqrt); - $this->assertInstanceOf(Operation::class, $sqrt); } public function testResult() { - $sqrt = SquareRoot::of(Real::of(4)); - $result = $sqrt->result(); + $sqrt = Number::of(4)->squareRoot(); - $this->assertInstanceOf(Number::class, $result); - $this->assertSame(2.0, $result->value()); + $this->assertSame(2.0, $sqrt->value()); } public function testValue() { - $sqrt = SquareRoot::of(Real::of(4)); + $sqrt = Number::of(4)->squareRoot(); $this->assertSame(2.0, $sqrt->value()); } @@ -57,163 +33,117 @@ public function testStringCast() { $this->assertSame( '√4', - SquareRoot::of(Real::of(4))->toString(), + Number::of(4)->squareRoot()->toString(), ); } public function testEquals() { - $sqrt = SquareRoot::of(Real::of(4)); + $sqrt = Number::of(4)->squareRoot(); - $this->assertTrue($sqrt->equals(Real::of(2))); - $this->assertFalse($sqrt->equals(Real::of(4.1))); + $this->assertTrue($sqrt->equals(Number::of(2))); + $this->assertFalse($sqrt->equals(Number::of(4.1))); } public function testHigherThan() { - $sqrt = SquareRoot::of(Real::of(4)); + $sqrt = Number::of(4)->squareRoot(); - $this->assertTrue($sqrt->higherThan(Real::of(0))); - $this->assertFalse($sqrt->higherThan(Real::of(4))); + $this->assertTrue($sqrt->higherThan(Number::of(0))); + $this->assertFalse($sqrt->higherThan(Number::of(4))); } public function testAdd() { - $sqrt = SquareRoot::of(Real::of(4)); - $number = $sqrt->add(Real::of(7)); + $sqrt = Number::of(4)->squareRoot(); + $number = $sqrt->add(Number::of(7)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(9.0, $number->value()); } public function testSubtract() { - $sqrt = SquareRoot::of(Real::of(4)); - $number = $sqrt->subtract(Real::of(7)); + $sqrt = Number::of(4)->squareRoot(); + $number = $sqrt->subtract(Number::of(7)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-5.0, $number->value()); } public function testMultiplication() { - $sqrt = SquareRoot::of(Real::of(4)); - $number = $sqrt->multiplyBy(Real::of(2)); + $sqrt = Number::of(4)->squareRoot(); + $number = $sqrt->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(4.0, $number->value()); } public function testDivision() { - $sqrt = SquareRoot::of(Real::of(4)); - $number = $sqrt->divideBy(Real::of(2)); + $sqrt = Number::of(4)->squareRoot(); + $number = $sqrt->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(1.0, $number->value()); } - public function testRound() - { - $number = SquareRoot::of(Real::of(2)); - - $this->assertEquals(Round::up($number, 2), $number->roundUp(2)); - $this->assertEquals(Round::down($number, 2), $number->roundDown(2)); - $this->assertEquals(Round::even($number, 2), $number->roundEven(2)); - $this->assertEquals(Round::odd($number, 2), $number->roundOdd(2)); - } - public function testFloor() { - $sqrt = SquareRoot::of(Real::of(2)); + $sqrt = Number::of(2)->squareRoot(); $number = $sqrt->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(1.0, $number->value()); } public function testCeil() { - $sqrt = SquareRoot::of(Real::of(2)); + $sqrt = Number::of(2)->squareRoot(); $number = $sqrt->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(2.0, $number->value()); } public function testAbsolute() { - $sqrt = SquareRoot::of(Real::of(4)); + $sqrt = Number::of(4)->squareRoot(); $number = $sqrt->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(2.0, $number->value()); } public function testModulo() { - $sqrt = SquareRoot::of(Real::of(4)); - $number = $sqrt->modulo(Real::of(2)); + $sqrt = Number::of(4)->squareRoot(); + $number = $sqrt->modulo(Number::of(2)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(0.0, $number->value()); } public function testPower() { - $sqrt = SquareRoot::of(Real::of(4)); - $number = $sqrt->power(Real::of(2)); + $sqrt = Number::of(4)->squareRoot(); + $number = $sqrt->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(4.0, $number->value()); } public function testSquareRoot() { - $sqrt = SquareRoot::of(Real::of(16)); + $sqrt = Number::of(16)->squareRoot(); $number = $sqrt->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(2.0, $number->value()); } public function testExponential() { - $number = SquareRoot::of(Real::of(16))->exponential(); + $number = Number::of(16)->squareRoot()->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\exp(4), $number->value()); } - public function testBinaryLogarithm() - { - $number = SquareRoot::of(Real::of(16))->binaryLogarithm(); - - $this->assertInstanceOf(BinaryLogarithm::class, $number); - $this->assertSame(\log(4, 2), $number->value()); - } - - public function testNaturalLogarithm() - { - $number = SquareRoot::of(Real::of(16))->naturalLogarithm(); - - $this->assertInstanceOf(NaturalLogarithm::class, $number); - $this->assertSame(\log(4), $number->value()); - } - - public function testCommonLogarithm() - { - $number = SquareRoot::of(Real::of(16))->commonLogarithm(); - - $this->assertInstanceOf(CommonLogarithm::class, $number); - $this->assertSame(\log10(4), $number->value()); - } - public function testSignum() { - $number = SquareRoot::of(Real::of(16))->signum(); + $number = Number::of(16)->squareRoot()->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } @@ -221,9 +151,9 @@ public function testSquareOfSquareRoot() { //sqrt(a)^2 === a $this->assertTrue( - ($a = Real::of(9)) + ($a = Number::of(9)) ->squareRoot() - ->power(Real::of(2)) + ->power(Number::of(2)) ->equals($a), ); } @@ -232,8 +162,8 @@ public function testSquareRootOfMultiplication() { //sqrt(a*b) === sqrt(a) * sqrt(b) $this->assertTrue( - ($a = Real::of(4)) - ->multiplyBy($b = Real::of(9)) + ($a = Number::of(4)) + ->multiplyBy($b = Number::of(9)) ->squareRoot() ->equals( $a @@ -249,8 +179,8 @@ public function testSquareRootOfDivision() { //sqrt(a/b) === sqrt(a) / sqrt(b) $this->assertTrue( - ($a = Real::of(4)) - ->divideBy($b = Real::of(9)) + ($a = Number::of(4)) + ->divideBy($b = Number::of(9)) ->squareRoot() ->equals( $a @@ -266,11 +196,11 @@ public function testSquareRootAddition() { //sqrt(a*n) + sqrt(b*n) === sqrt((sqrt(a) + sqrt(b))^2 * n) $this->assertTrue( - ($a = Real::of(9)) - ->multiplyBy($n = Real::of(2)) + ($a = Number::of(9)) + ->multiplyBy($n = Number::of(2)) ->squareRoot() ->add( - ($b = Real::of(4)) + ($b = Number::of(4)) ->multiplyBy($n) ->squareRoot(), ) @@ -280,7 +210,7 @@ public function testSquareRootAddition() ->add( $b->squareRoot(), ) - ->power(Real::of(2)) + ->power(Number::of(2)) ->multiplyBy($n) ->squareRoot(), ), @@ -291,30 +221,30 @@ public function testSquareRootAsPowerNotation() { //sqrt(a) === a^0.5 $this->assertTrue( - ($a = Real::of(4)) + ($a = Number::of(4)) ->squareRoot() ->equals( - $a->power(Real::of(0.5)), + $a->power(Number::of(0.5)), ), ); } public function testCollapseSquare() { - $result = Integer::of(2) - ->power(Integer::of(2)) + $result = Number::of(2) + ->power(Number::of(2)) ->squareRoot() - ->collapse() + ->optimize() ->value(); $this->assertSame(2, $result); - $result = Integer::of(2) - ->power(Integer::of(2)) + $result = Number::of(2) + ->power(Number::of(2)) ->squareRoot() - ->power(Integer::of(2)) + ->power(Number::of(2)) ->squareRoot() - ->collapse() + ->optimize() ->value(); $this->assertSame(2, $result); diff --git a/tests/Algebra/SubtractionTest.php b/tests/Algebra/SubtractionTest.php index 038cecf..ab81499 100644 --- a/tests/Algebra/SubtractionTest.php +++ b/tests/Algebra/SubtractionTest.php @@ -3,61 +3,35 @@ namespace Tests\Innmind\Math\Algebra; -use Innmind\Math\Algebra\{ - Subtraction, - Number, - Operation, - Addition, - Multiplication, - Division, - Round, - Floor, - Ceil, - Modulo, - Absolute, - Power, - SquareRoot, - Exponential, - BinaryLogarithm, - NaturalLogarithm, - CommonLogarithm, - Signum, - Real, -}; -use PHPUnit\Framework\TestCase; +use Innmind\Math\Algebra\Number; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class SubtractionTest extends TestCase { public function testInterface() { - $subtraction = Subtraction::of( - Real::of(4), - Real::of(2), + $subtraction = Number::of(4)->subtract( + Number::of(2), ); - $this->assertInstanceOf(Operation::class, $subtraction); $this->assertInstanceOf(Number::class, $subtraction); $this->assertSame('4 - 2', $subtraction->toString()); } public function testResult() { - $subtraction = Subtraction::of( - Real::of(42), - Real::of(24), + $subtraction = Number::of(42)->subtract( + Number::of(24), ); - $result = $subtraction->result(); - $this->assertInstanceOf(Number::class, $result); - $this->assertSame(18, $result->value()); - $this->assertTrue($result->equals($subtraction->difference())); + $this->assertInstanceOf(Number::class, $subtraction); + $this->assertSame(18, $subtraction->value()); } public function testValue() { - $subtraction = Subtraction::of( - Real::of(42), - Real::of(24), + $subtraction = Number::of(42)->subtract( + Number::of(24), ); $this->assertSame(18, $subtraction->value()); @@ -65,224 +39,148 @@ public function testValue() public function testEquals() { - $subtraction = Subtraction::of( - Real::of(42), - Real::of(24), + $subtraction = Number::of(42)->subtract( + Number::of(24), ); - $this->assertTrue($subtraction->equals(Real::of(18))); - $this->assertFalse($subtraction->equals(Real::of(18.1))); + $this->assertTrue($subtraction->equals(Number::of(18))); + $this->assertFalse($subtraction->equals(Number::of(18.1))); } public function testHigherThan() { - $subtraction = Subtraction::of( - Real::of(42), - Real::of(24), + $subtraction = Number::of(42)->subtract( + Number::of(24), ); - $this->assertFalse($subtraction->higherThan(Real::of(18))); - $this->assertTrue($subtraction->higherThan(Real::of(17.9))); + $this->assertFalse($subtraction->higherThan(Number::of(18))); + $this->assertTrue($subtraction->higherThan(Number::of(17.9))); } public function testAdd() { - $subtraction = Subtraction::of( - Real::of(42), - Real::of(24), + $subtraction = Number::of(42)->subtract( + Number::of(24), ); - $number = $subtraction->add(Real::of(66)); + $number = $subtraction->add(Number::of(66)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(84, $number->value()); } public function testSubtract() { - $subtraction = Subtraction::of( - Real::of(42), - Real::of(24), + $subtraction = Number::of(42)->subtract( + Number::of(24), ); - $number = $subtraction->subtract(Real::of(66)); + $number = $subtraction->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-48, $number->value()); } public function testDivideBy() { - $subtraction = Subtraction::of( - Real::of(42), - Real::of(24), + $subtraction = Number::of(42)->subtract( + Number::of(24), ); - $number = $subtraction->divideBy(Real::of(3)); + $number = $subtraction->divideBy(Number::of(3)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(6, $number->value()); } public function testMulitplyBy() { - $subtraction = Subtraction::of( - Real::of(42), - Real::of(24), + $subtraction = Number::of(42)->subtract( + Number::of(24), ); - $number = $subtraction->multiplyBy(Real::of(2)); + $number = $subtraction->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(36, $number->value()); } - public function testRound() - { - $number = Subtraction::of( - Real::of(24.55), - Real::of(12.33), - ); - - $this->assertEquals(Round::up($number, 2), $number->roundUp(2)); - $this->assertEquals(Round::down($number, 2), $number->roundDown(2)); - $this->assertEquals(Round::even($number, 2), $number->roundEven(2)); - $this->assertEquals(Round::odd($number, 2), $number->roundOdd(2)); - } - public function testFloor() { - $subtraction = Subtraction::of( - Real::of(24.55), - Real::of(12.33), + $subtraction = Number::of(24.55)->subtract( + Number::of(12.33), ); $number = $subtraction->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(12.0, $number->value()); } public function testCeil() { - $subtraction = Subtraction::of( - Real::of(24.55), - Real::of(12.33), + $subtraction = Number::of(24.55)->subtract( + Number::of(12.33), ); $number = $subtraction->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(13.0, $number->value()); } public function testModulo() { - $subtraction = Subtraction::of( - Real::of(25), - Real::of(12), + $subtraction = Number::of(25)->subtract( + Number::of(12), ); - $number = $subtraction->modulo(Real::of(6)); + $number = $subtraction->modulo(Number::of(6)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(1.0, $number->value()); } public function testAbsolute() { - $subtraction = Subtraction::of( - Real::of(12), - Real::of(25), + $subtraction = Number::of(12)->subtract( + Number::of(25), ); $number = $subtraction->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(13, $number->value()); } public function testPower() { - $subtraction = Subtraction::of( - Real::of(12), - Real::of(6), + $subtraction = Number::of(12)->subtract( + Number::of(6), ); - $number = $subtraction->power(Real::of(2)); + $number = $subtraction->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(36, $number->value()); } public function testSquareRoot() { - $subtraction = Subtraction::of( - Real::of(8), - Real::of(4), + $subtraction = Number::of(8)->subtract( + Number::of(4), ); $number = $subtraction->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(2.0, $number->value()); } public function testExponential() { - $number = Subtraction::of( - Real::of(8), - Real::of(4), - )->exponential(); + $number = Number::of(8) + ->subtract(Number::of(4)) + ->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\exp(4), $number->value()); } - public function testBinaryLogarithm() - { - $number = Subtraction::of( - Real::of(8), - Real::of(4), - )->binaryLogarithm(); - - $this->assertInstanceOf(BinaryLogarithm::class, $number); - $this->assertSame(\log(4, 2), $number->value()); - } - - public function testNaturalLogarithm() - { - $number = Subtraction::of( - Real::of(8), - Real::of(4), - )->naturalLogarithm(); - - $this->assertInstanceOf(NaturalLogarithm::class, $number); - $this->assertSame(\log(4), $number->value()); - } - - public function testCommonLogarithm() - { - $number = Subtraction::of( - Real::of(8), - Real::of(4), - )->commonLogarithm(); - - $this->assertInstanceOf(CommonLogarithm::class, $number); - $this->assertSame(\log10(4), $number->value()); - } - public function testSignum() { - $number = Subtraction::of( - Real::of(4), - Real::of(3), - )->signum(); + $number = Number::of(4) + ->subtract(Number::of(3)) + ->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } public function testStringCast() { - $subtraction = Subtraction::of( - Addition::of( - Real::of(12), - Real::of(12), - ), - Real::of(42), - Real::of(66), - ); + $subtraction = Number::of(12) + ->add(Number::of(12)) + ->subtract(Number::of(42)) + ->subtract(Number::of(66)); $this->assertSame('(12 + 12) - 42 - 66', $subtraction->toString()); } diff --git a/tests/DefinitionSet/IntegersExceptZeroTest.php b/tests/DefinitionSet/IntegersExceptZeroTest.php index b738e7f..1ce347a 100644 --- a/tests/DefinitionSet/IntegersExceptZeroTest.php +++ b/tests/DefinitionSet/IntegersExceptZeroTest.php @@ -4,64 +4,54 @@ namespace Tests\Innmind\Math\DefinitionSet; use Innmind\Math\{ - DefinitionSet\IntegersExceptZero, DefinitionSet\Set, - DefinitionSet\Union, - DefinitionSet\Intersection, - Algebra\Integer, - Algebra\Real, + Algebra\Number, Exception\OutOfDefinitionSet, }; -use PHPUnit\Framework\TestCase; +use Innmind\Immutable\SideEffect; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class IntegersExceptZeroTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - Set::class, - new IntegersExceptZero, - ); - } - public function testStringCast() { - $this->assertSame('ℤ*', (new IntegersExceptZero)->toString()); + $this->assertSame('ℤ*', Set::integersExceptZero()->toString()); } public function testContains() { - $set = new IntegersExceptZero; + $set = Set::integersExceptZero(); - $this->assertTrue($set->contains(Integer::of(1))); - $this->assertTrue($set->contains(Integer::of(-1))); - $this->assertFalse($set->contains(Integer::of(0))); - $this->assertFalse($set->contains(Real::of(0.75))); + $this->assertTrue($set->contains(Number::of(1))); + $this->assertTrue($set->contains(Number::of(-1))); + $this->assertFalse($set->contains(Number::of(0))); + $this->assertFalse($set->contains(Number::of(0.75))); } public function testAccept() { - $this->assertNull((new IntegersExceptZero)->accept(Integer::of(1))); + $this->assertInstanceOf( + SideEffect::class, + Set::integersExceptZero()->accept(Number::of(1))->unwrap(), + ); $this->expectException(OutOfDefinitionSet::class); $this->expectExceptionMessage('0 ∉ ℤ*'); - (new IntegersExceptZero)->accept(Integer::of(0)); + $_ = Set::integersExceptZero()->accept(Number::of(0))->unwrap(); } public function testUnion() { - $union = (new IntegersExceptZero)->union(new IntegersExceptZero); + $union = Set::integersExceptZero()->union(Set::integersExceptZero()); - $this->assertInstanceOf(Union::class, $union); $this->assertSame('ℤ*∪ℤ*', $union->toString()); } public function testIntersect() { - $intersection = (new IntegersExceptZero)->intersect(new IntegersExceptZero); + $intersection = Set::integersExceptZero()->intersect(Set::integersExceptZero()); - $this->assertInstanceOf(Intersection::class, $intersection); $this->assertSame('ℤ*∩ℤ*', $intersection->toString()); } } diff --git a/tests/DefinitionSet/IntegersTest.php b/tests/DefinitionSet/IntegersTest.php index deb0db5..9ddf5ae 100644 --- a/tests/DefinitionSet/IntegersTest.php +++ b/tests/DefinitionSet/IntegersTest.php @@ -4,64 +4,54 @@ namespace Tests\Innmind\Math\DefinitionSet; use Innmind\Math\{ - DefinitionSet\Integers, DefinitionSet\Set, - DefinitionSet\Union, - DefinitionSet\Intersection, - Algebra\Integer, - Algebra\Real, + Algebra\Number, Exception\OutOfDefinitionSet, }; -use PHPUnit\Framework\TestCase; +use Innmind\Immutable\SideEffect; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class IntegersTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - Set::class, - new Integers, - ); - } - public function testStringCast() { - $this->assertSame('ℤ', (new Integers)->toString()); + $this->assertSame('ℤ', Set::integers()->toString()); } public function testContains() { - $set = new Integers; + $set = Set::integers(); - $this->assertTrue($set->contains(Integer::of(1))); - $this->assertTrue($set->contains(Integer::of(-1))); - $this->assertTrue($set->contains(Integer::of(0))); - $this->assertFalse($set->contains(Real::of(0.75))); + $this->assertTrue($set->contains(Number::of(1))); + $this->assertTrue($set->contains(Number::of(-1))); + $this->assertTrue($set->contains(Number::of(0))); + $this->assertFalse($set->contains(Number::of(0.75))); } public function testAccept() { - $this->assertNull((new Integers)->accept(Integer::of(1))); + $this->assertInstanceOf( + SideEffect::class, + Set::integers()->accept(Number::of(1))->unwrap(), + ); $this->expectException(OutOfDefinitionSet::class); $this->expectExceptionMessage('0.1 ∉ ℤ'); - (new Integers)->accept(Real::of(0.1)); + $_ = Set::integers()->accept(Number::of(0.1))->unwrap(); } public function testUnion() { - $union = (new Integers)->union(new Integers); + $union = Set::integers()->union(Set::integers()); - $this->assertInstanceOf(Union::class, $union); $this->assertSame('ℤ∪ℤ', $union->toString()); } public function testIntersect() { - $intersection = (new Integers)->intersect(new Integers); + $intersection = Set::integers()->intersect(Set::integers()); - $this->assertInstanceOf(Intersection::class, $intersection); $this->assertSame('ℤ∩ℤ', $intersection->toString()); } } diff --git a/tests/DefinitionSet/IntersectionTest.php b/tests/DefinitionSet/IntersectionTest.php index 3891604..0f87645 100644 --- a/tests/DefinitionSet/IntersectionTest.php +++ b/tests/DefinitionSet/IntersectionTest.php @@ -4,90 +4,70 @@ namespace Tests\Innmind\Math\DefinitionSet; use Innmind\Math\{ - DefinitionSet\Intersection, - DefinitionSet\Union, - DefinitionSet\Set as SetInterface, - DefinitionSet\Set\Set, - Algebra\Integer, - Algebra\Real, + DefinitionSet\Set, + Algebra\Number, Exception\OutOfDefinitionSet, }; -use PHPUnit\Framework\TestCase; +use Innmind\Immutable\SideEffect; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class IntersectionTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - SetInterface::class, - Intersection::of( - $this->createMock(SetInterface::class), - $this->createMock(SetInterface::class), - ), - ); - } - - public function testStringCast() - { - $this->assertSame('∅∩∅', (Intersection::of(Set::of(), Set::of()))->toString()); - } - public function testContains() { - $union = Intersection::of( - Set::of( - Integer::of(1), - Integer::of(2), - ), + $intersection = Set::of( + Number::of(1), + Number::of(2), + )->intersect( Set::of( - Integer::of(4), - Integer::of(5), + Number::of(4), + Number::of(5), ), ); - $this->assertFalse($union->contains(Real::of(1))); - $this->assertFalse($union->contains(Real::of(2))); - $this->assertFalse($union->contains(Real::of(4))); - $this->assertFalse($union->contains(Real::of(5))); - $this->assertFalse($union->contains(Real::of(6))); - $this->assertFalse($union->contains(Real::of(3))); - $this->assertFalse($union->contains(Real::of(0))); + $this->assertFalse($intersection->contains(Number::of(1))); + $this->assertFalse($intersection->contains(Number::of(2))); + $this->assertFalse($intersection->contains(Number::of(4))); + $this->assertFalse($intersection->contains(Number::of(5))); + $this->assertFalse($intersection->contains(Number::of(6))); + $this->assertFalse($intersection->contains(Number::of(3))); + $this->assertFalse($intersection->contains(Number::of(0))); } public function testAccept() { - $set = Intersection::of( + $set = Set::of( + Number::of(1), + Number::of(2), + )->intersect( Set::of( - Integer::of(1), - Integer::of(2), - ), - Set::of( - Integer::of(2), - Integer::of(5), + Number::of(2), + Number::of(5), ), ); - $this->assertNull($set->accept(Integer::of(2))); + $this->assertInstanceOf( + SideEffect::class, + $set->accept(Number::of(2))->unwrap(), + ); $this->expectException(OutOfDefinitionSet::class); $this->expectExceptionMessage('0.1 ∉ {1;2}∩{2;5}'); - $set->accept(Real::of(0.1)); + $_ = $set->accept(Number::of(0.1))->unwrap(); } public function testUnion() { - $union = Intersection::of(Set::of(), Set::of())->union(Set::of()); + $union = Set::of()->intersect(Set::of())->union(Set::of()); - $this->assertInstanceOf(Union::class, $union); $this->assertSame('∅∩∅∪∅', $union->toString()); } public function testIntersect() { - $intersection = Intersection::of(Set::of(), Set::of())->intersect(Set::of()); + $intersection = Set::of()->intersect(Set::of())->intersect(Set::of()); - $this->assertInstanceOf(Intersection::class, $intersection); $this->assertSame('∅∩∅∩∅', $intersection->toString()); } } diff --git a/tests/DefinitionSet/NaturalNumbersExceptZeroTest.php b/tests/DefinitionSet/NaturalNumbersExceptZeroTest.php index a230e79..7adafc8 100644 --- a/tests/DefinitionSet/NaturalNumbersExceptZeroTest.php +++ b/tests/DefinitionSet/NaturalNumbersExceptZeroTest.php @@ -4,68 +4,58 @@ namespace Tests\Innmind\Math\DefinitionSet; use Innmind\Math\{ - DefinitionSet\NaturalNumbersExceptZero, DefinitionSet\Set, - DefinitionSet\Union, - DefinitionSet\Intersection, - Algebra\Integer, - Algebra\Real, + Algebra\Number, Exception\OutOfDefinitionSet, }; -use PHPUnit\Framework\TestCase; +use Innmind\Immutable\SideEffect; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class NaturalNumbersExceptZeroTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - Set::class, - new NaturalNumbersExceptZero, - ); - } - public function testStringCast() { - $this->assertSame('ℕ*', (new NaturalNumbersExceptZero)->toString()); + $this->assertSame('ℕ*', Set::naturalNumbersExceptZero()->toString()); } public function testContains() { - $set = new NaturalNumbersExceptZero; - - $this->assertTrue($set->contains(Integer::of(1))); - $this->assertTrue($set->contains(Integer::of(2))); - $this->assertFalse($set->contains(Integer::of(0))); - $this->assertFalse($set->contains(Integer::of(-1))); - $this->assertFalse($set->contains(Real::of(0.75))); - $this->assertFalse($set->contains(Real::of(1.75))); + $set = Set::naturalNumbersExceptZero(); + + $this->assertTrue($set->contains(Number::of(1))); + $this->assertTrue($set->contains(Number::of(2))); + $this->assertFalse($set->contains(Number::of(0))); + $this->assertFalse($set->contains(Number::of(-1))); + $this->assertFalse($set->contains(Number::of(0.75))); + $this->assertFalse($set->contains(Number::of(1.75))); } public function testAccept() { - $set = new NaturalNumbersExceptZero; + $set = Set::naturalNumbersExceptZero(); - $this->assertNull($set->accept(Integer::of(1))); + $this->assertInstanceOf( + SideEffect::class, + $set->accept(Number::of(1))->unwrap(), + ); $this->expectException(OutOfDefinitionSet::class); $this->expectExceptionMessage('0.1 ∉ ℕ*'); - $set->accept(Real::of(0.1)); + $_ = $set->accept(Number::of(0.1))->unwrap(); } public function testUnion() { - $union = (new NaturalNumbersExceptZero)->union(new NaturalNumbersExceptZero); + $union = Set::naturalNumbersExceptZero()->union(Set::naturalNumbersExceptZero()); - $this->assertInstanceOf(Union::class, $union); $this->assertSame('ℕ*∪ℕ*', $union->toString()); } public function testIntersect() { - $intersection = (new NaturalNumbersExceptZero)->intersect(new NaturalNumbersExceptZero); + $intersection = Set::naturalNumbersExceptZero()->intersect(Set::naturalNumbersExceptZero()); - $this->assertInstanceOf(Intersection::class, $intersection); $this->assertSame('ℕ*∩ℕ*', $intersection->toString()); } } diff --git a/tests/DefinitionSet/NaturalNumbersTest.php b/tests/DefinitionSet/NaturalNumbersTest.php index d8b4d74..7edf49a 100644 --- a/tests/DefinitionSet/NaturalNumbersTest.php +++ b/tests/DefinitionSet/NaturalNumbersTest.php @@ -4,66 +4,56 @@ namespace Tests\Innmind\Math\DefinitionSet; use Innmind\Math\{ - DefinitionSet\NaturalNumbers, DefinitionSet\Set, - DefinitionSet\Union, - DefinitionSet\Intersection, - Algebra\Integer, - Algebra\Real, + Algebra\Number, Exception\OutOfDefinitionSet, }; -use PHPUnit\Framework\TestCase; +use Innmind\Immutable\SideEffect; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class NaturalNumbersTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - Set::class, - new NaturalNumbers, - ); - } - public function testStringCast() { - $this->assertSame('ℕ', (new NaturalNumbers)->toString()); + $this->assertSame('ℕ', Set::naturalNumbers()->toString()); } public function testContains() { - $set = new NaturalNumbers; + $set = Set::naturalNumbers(); - $this->assertTrue($set->contains(Integer::of(1))); - $this->assertTrue($set->contains(Integer::of(0))); - $this->assertFalse($set->contains(Integer::of(-1))); - $this->assertFalse($set->contains(Real::of(0.75))); + $this->assertTrue($set->contains(Number::of(1))); + $this->assertTrue($set->contains(Number::of(0))); + $this->assertFalse($set->contains(Number::of(-1))); + $this->assertFalse($set->contains(Number::of(0.75))); } public function testAccept() { - $set = new NaturalNumbers; + $set = Set::naturalNumbers(); - $this->assertNull($set->accept(Integer::of(1))); + $this->assertInstanceOf( + SideEffect::class, + $set->accept(Number::of(1))->unwrap(), + ); $this->expectException(OutOfDefinitionSet::class); $this->expectExceptionMessage('0.1 ∉ ℕ'); - $set->accept(Real::of(0.1)); + $_ = $set->accept(Number::of(0.1))->unwrap(); } public function testUnion() { - $union = (new NaturalNumbers)->union(new NaturalNumbers); + $union = Set::naturalNumbers()->union(Set::naturalNumbers()); - $this->assertInstanceOf(Union::class, $union); $this->assertSame('ℕ∪ℕ', $union->toString()); } public function testIntersect() { - $intersection = (new NaturalNumbers)->intersect(new NaturalNumbers); + $intersection = Set::naturalNumbers()->intersect(Set::naturalNumbers()); - $this->assertInstanceOf(Intersection::class, $intersection); $this->assertSame('ℕ∩ℕ', $intersection->toString()); } } diff --git a/tests/DefinitionSet/RangeTest.php b/tests/DefinitionSet/RangeTest.php index 6300675..2262235 100644 --- a/tests/DefinitionSet/RangeTest.php +++ b/tests/DefinitionSet/RangeTest.php @@ -4,102 +4,74 @@ namespace Tests\Innmind\Math\DefinitionSet; use Innmind\Math\{ - DefinitionSet\Range, DefinitionSet\Set, - DefinitionSet\Union, - DefinitionSet\Intersection, - Algebra\Integer, - Algebra\Real, + Algebra\Number, Exception\OutOfDefinitionSet, }; -use PHPUnit\Framework\TestCase; +use Innmind\Immutable\SideEffect; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class RangeTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - Set::class, - Range::inclusive(Integer::of(1), Integer::of(2)), - ); - } - public function testStringCast() { $this->assertSame( '[1;2]', - Range::inclusive(Integer::of(1), Integer::of(2))->toString(), + Set::inclusiveRange(Number::of(1), Number::of(2))->toString(), ); $this->assertSame( ']1;2[', - Range::exclusive(Integer::of(1), Integer::of(2))->toString(), - ); - $this->assertSame( - '[1;2[', - Range::inclusive( - Integer::of(1), - Integer::of(2), - ) - ->excludeUpperBound() - ->toString(), - ); - $this->assertSame( - ']1;2]', - Range::inclusive( - Integer::of(1), - Integer::of(2), - ) - ->excludeLowerBound() - ->toString(), + Set::exclusiveRange(Number::of(1), Number::of(2))->toString(), ); } public function testContains() { - $inclusive = Range::inclusive(Integer::of(1), Integer::of(2)); + $inclusive = Set::inclusiveRange(Number::of(1), Number::of(2)); - $this->assertTrue($inclusive->contains(Real::of(1))); - $this->assertTrue($inclusive->contains(Real::of(1.5))); - $this->assertTrue($inclusive->contains(Real::of(2))); - $this->assertFalse($inclusive->contains(Real::of(0))); - $this->assertFalse($inclusive->contains(Real::of(2.1))); + $this->assertTrue($inclusive->contains(Number::of(1))); + $this->assertTrue($inclusive->contains(Number::of(1.5))); + $this->assertTrue($inclusive->contains(Number::of(2))); + $this->assertFalse($inclusive->contains(Number::of(0))); + $this->assertFalse($inclusive->contains(Number::of(2.1))); - $exclusive = Range::exclusive(Integer::of(1), Integer::of(2)); + $exclusive = Set::exclusiveRange(Number::of(1), Number::of(2)); - $this->assertTrue($exclusive->contains(Real::of(1.5))); - $this->assertFalse($exclusive->contains(Real::of(1))); - $this->assertFalse($exclusive->contains(Real::of(2))); - $this->assertFalse($exclusive->contains(Real::of(0))); - $this->assertFalse($exclusive->contains(Real::of(2.1))); + $this->assertTrue($exclusive->contains(Number::of(1.5))); + $this->assertFalse($exclusive->contains(Number::of(1))); + $this->assertFalse($exclusive->contains(Number::of(2))); + $this->assertFalse($exclusive->contains(Number::of(0))); + $this->assertFalse($exclusive->contains(Number::of(2.1))); } public function testAccept() { - $set = Range::inclusive(Integer::of(1), Integer::of(2)); + $set = Set::inclusiveRange(Number::of(1), Number::of(2)); - $this->assertNull($set->accept(Integer::of(1))); + $this->assertInstanceOf( + SideEffect::class, + $set->accept(Number::of(1))->unwrap(), + ); $this->expectException(OutOfDefinitionSet::class); $this->expectExceptionMessage('0.1 ∉ [1;2]'); - $set->accept(Real::of(0.1)); + $_ = $set->accept(Number::of(0.1))->unwrap(); } public function testUnion() { - $range = Range::inclusive(Integer::of(1), Integer::of(2)); + $range = Set::inclusiveRange(Number::of(1), Number::of(2)); $union = $range->union($range); - $this->assertInstanceOf(Union::class, $union); $this->assertSame('[1;2]∪[1;2]', $union->toString()); } public function testIntersect() { - $range = Range::inclusive(Integer::of(1), Integer::of(2)); + $range = Set::inclusiveRange(Number::of(1), Number::of(2)); $intersection = $range->intersect($range); - $this->assertInstanceOf(Intersection::class, $intersection); $this->assertSame('[1;2]∩[1;2]', $intersection->toString()); } } diff --git a/tests/DefinitionSet/RealNumbersExceptZeroTest.php b/tests/DefinitionSet/RealNumbersExceptZeroTest.php index 0aedc9b..aac7d84 100644 --- a/tests/DefinitionSet/RealNumbersExceptZeroTest.php +++ b/tests/DefinitionSet/RealNumbersExceptZeroTest.php @@ -4,69 +4,58 @@ namespace Tests\Innmind\Math\DefinitionSet; use Innmind\Math\{ - DefinitionSet\RealNumbersExceptZero, DefinitionSet\Set, - DefinitionSet\Union, - DefinitionSet\Intersection, - Algebra\Integer, - Algebra\Real, - Algebra\Value, + Algebra\Number, Exception\OutOfDefinitionSet, }; -use PHPUnit\Framework\TestCase; +use Innmind\Immutable\SideEffect; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class RealNumbersExceptZeroTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - Set::class, - new RealNumbersExceptZero, - ); - } - public function testStringCast() { - $this->assertSame('ℝ*', (new RealNumbersExceptZero)->toString()); + $this->assertSame('ℝ*', Set::realNumbersExceptZero()->toString()); } public function testContains() { - $set = new RealNumbersExceptZero; - - $this->assertTrue($set->contains(Integer::of(1))); - $this->assertTrue($set->contains(Integer::of(-1))); - $this->assertTrue($set->contains(Real::of(0.75))); - $this->assertTrue($set->contains(Real::of(-0.75))); - $this->assertTrue($set->contains(Value::pi)); - $this->assertFalse($set->contains(Integer::of(0))); + $set = Set::realNumbersExceptZero(); + + $this->assertTrue($set->contains(Number::of(1))); + $this->assertTrue($set->contains(Number::of(-1))); + $this->assertTrue($set->contains(Number::of(0.75))); + $this->assertTrue($set->contains(Number::of(-0.75))); + $this->assertTrue($set->contains(Number::pi())); + $this->assertFalse($set->contains(Number::of(0))); } public function testAccept() { - $set = new RealNumbersExceptZero; + $set = Set::realNumbersExceptZero(); - $this->assertNull($set->accept(Integer::of(1))); + $this->assertInstanceOf( + SideEffect::class, + $set->accept(Number::of(1))->unwrap(), + ); $this->expectException(OutOfDefinitionSet::class); $this->expectExceptionMessage('0 ∉ ℝ*'); - $set->accept(Real::of(0)); + $_ = $set->accept(Number::of(0))->unwrap(); } public function testUnion() { - $union = (new RealNumbersExceptZero)->union(new RealNumbersExceptZero); + $union = Set::realNumbersExceptZero()->union(Set::realNumbersExceptZero()); - $this->assertInstanceOf(Union::class, $union); $this->assertSame('ℝ*∪ℝ*', $union->toString()); } public function testIntersect() { - $intersection = (new RealNumbersExceptZero)->intersect(new RealNumbersExceptZero); + $intersection = Set::realNumbersExceptZero()->intersect(Set::realNumbersExceptZero()); - $this->assertInstanceOf(Intersection::class, $intersection); $this->assertSame('ℝ*∩ℝ*', $intersection->toString()); } } diff --git a/tests/DefinitionSet/RealNumbersTest.php b/tests/DefinitionSet/RealNumbersTest.php index 92c7fa4..503dce7 100644 --- a/tests/DefinitionSet/RealNumbersTest.php +++ b/tests/DefinitionSet/RealNumbersTest.php @@ -4,68 +4,54 @@ namespace Tests\Innmind\Math\DefinitionSet; use Innmind\Math\{ - DefinitionSet\RealNumbers, DefinitionSet\Set, - DefinitionSet\Union, - DefinitionSet\Intersection, - Algebra\Integer, - Algebra\Real, - Algebra\Value, + Algebra\Number, }; -use PHPUnit\Framework\TestCase; +use Innmind\Immutable\SideEffect; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class RealNumbersTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - Set::class, - new RealNumbers, - ); - } - public function testStringCast() { - $this->assertSame('ℝ', (new RealNumbers)->toString()); + $this->assertSame('ℝ', Set::realNumbers()->toString()); } public function testContains() { - $set = new RealNumbers; - - $this->assertTrue($set->contains(Integer::of(1))); - $this->assertTrue($set->contains(Integer::of(0))); - $this->assertTrue($set->contains(Integer::of(-1))); - $this->assertTrue($set->contains(Real::of(0.75))); - $this->assertTrue($set->contains(Real::of(-0.75))); - $this->assertTrue($set->contains(Value::pi)); + $set = Set::realNumbers(); + + $this->assertTrue($set->contains(Number::of(1))); + $this->assertTrue($set->contains(Number::of(0))); + $this->assertTrue($set->contains(Number::of(-1))); + $this->assertTrue($set->contains(Number::of(0.75))); + $this->assertTrue($set->contains(Number::of(-0.75))); + $this->assertTrue($set->contains(Number::pi())); } public function testAccept() { - $set = new RealNumbers; - - $this->assertNull($set->accept(Integer::of(1))); - $this->assertNull($set->accept(Integer::of(0))); - $this->assertNull($set->accept(Integer::of(-1))); - $this->assertNull($set->accept(Real::of(0.75))); - $this->assertNull($set->accept(Real::of(-0.75))); - $this->assertNull($set->accept(Value::pi)); + $set = Set::realNumbers(); + + $this->assertInstanceOf(SideEffect::class, $set->accept(Number::of(1))->unwrap()); + $this->assertInstanceOf(SideEffect::class, $set->accept(Number::of(0))->unwrap()); + $this->assertInstanceOf(SideEffect::class, $set->accept(Number::of(-1))->unwrap()); + $this->assertInstanceOf(SideEffect::class, $set->accept(Number::of(0.75))->unwrap()); + $this->assertInstanceOf(SideEffect::class, $set->accept(Number::of(-0.75))->unwrap()); + $this->assertInstanceOf(SideEffect::class, $set->accept(Number::pi())->unwrap()); } public function testUnion() { - $union = (new RealNumbers)->union(new RealNumbers); + $union = Set::realNumbers()->union(Set::realNumbers()); - $this->assertInstanceOf(Union::class, $union); $this->assertSame('ℝ∪ℝ', $union->toString()); } public function testIntersect() { - $intersection = (new RealNumbers)->intersect(new RealNumbers); + $intersection = Set::realNumbers()->intersect(Set::realNumbers()); - $this->assertInstanceOf(Intersection::class, $intersection); $this->assertSame('ℝ∩ℝ', $intersection->toString()); } } diff --git a/tests/DefinitionSet/Set/SetTest.php b/tests/DefinitionSet/Set/SetTest.php index becd133..75eae45 100644 --- a/tests/DefinitionSet/Set/SetTest.php +++ b/tests/DefinitionSet/Set/SetTest.php @@ -4,65 +4,56 @@ namespace Tests\Innmind\Math\DefinitionSet\Set; use Innmind\Math\{ - DefinitionSet\Set\Set, - DefinitionSet\Set as SetInterface, - DefinitionSet\Union, - DefinitionSet\Intersection, - Algebra\Integer, - Algebra\Real, + DefinitionSet\Set, + Algebra\Number, Exception\OutOfDefinitionSet, }; -use PHPUnit\Framework\TestCase; +use Innmind\Immutable\SideEffect; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class SetTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - SetInterface::class, - Set::of(), - ); - } - public function testStringCast() { $this->assertSame('∅', Set::of()->toString()); - $this->assertSame('{1;2}', Set::of(Integer::of(1), Integer::of(2))->toString()); + $this->assertSame('{1;2}', Set::of(Number::of(1), Number::of(2))->toString()); } public function testContains() { $empty = Set::of(); - $this->assertFalse($empty->contains(Real::of(0))); - $this->assertFalse($empty->contains(Real::of(1))); - $this->assertFalse($empty->contains(Real::of(-1))); - $this->assertFalse($empty->contains(Real::of(-0.75))); - $this->assertFalse($empty->contains(Real::of(0.75))); + $this->assertFalse($empty->contains(Number::of(0))); + $this->assertFalse($empty->contains(Number::of(1))); + $this->assertFalse($empty->contains(Number::of(-1))); + $this->assertFalse($empty->contains(Number::of(-0.75))); + $this->assertFalse($empty->contains(Number::of(0.75))); - $set = Set::of(Integer::of(1), Integer::of(2)); + $set = Set::of(Number::of(1), Number::of(2)); - $this->assertTrue($set->contains(Integer::of(1))); - $this->assertTrue($set->contains(Integer::of(2))); - $this->assertFalse($set->contains(Integer::of(3))); - $this->assertFalse($set->contains(Integer::of(0))); + $this->assertTrue($set->contains(Number::of(1))); + $this->assertTrue($set->contains(Number::of(2))); + $this->assertFalse($set->contains(Number::of(3))); + $this->assertFalse($set->contains(Number::of(0))); } public function testAccept() { - $this->assertNull(Set::of(Integer::of(1))->accept(Integer::of(1))); + $this->assertInstanceOf( + SideEffect::class, + Set::of(Number::of(1))->accept(Number::of(1))->unwrap(), + ); $this->expectException(OutOfDefinitionSet::class); $this->expectExceptionMessage('2 ∉ {1}'); - Set::of(Integer::of(1))->accept(Integer::of(2)); + $_ = Set::of(Number::of(1))->accept(Number::of(2))->unwrap(); } public function testUnion() { $union = Set::of()->union(Set::of()); - $this->assertInstanceOf(Union::class, $union); $this->assertSame('∅∪∅', $union->toString()); } @@ -70,7 +61,6 @@ public function testIntersect() { $intersection = Set::of()->intersect(Set::of()); - $this->assertInstanceOf(Intersection::class, $intersection); $this->assertSame('∅∩∅', $intersection->toString()); } } diff --git a/tests/DefinitionSet/UnionTest.php b/tests/DefinitionSet/UnionTest.php index 35b7718..f10112a 100644 --- a/tests/DefinitionSet/UnionTest.php +++ b/tests/DefinitionSet/UnionTest.php @@ -4,90 +4,75 @@ namespace Tests\Innmind\Math\DefinitionSet; use Innmind\Math\{ - DefinitionSet\Union, - DefinitionSet\Set as SetInterface, - DefinitionSet\Set\Set, - DefinitionSet\Intersection, - Algebra\Integer, - Algebra\Real, + DefinitionSet\Set, + Algebra\Number, Exception\OutOfDefinitionSet, }; -use PHPUnit\Framework\TestCase; +use Innmind\Immutable\SideEffect; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class UnionTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - SetInterface::class, - Union::of( - $this->createMock(SetInterface::class), - $this->createMock(SetInterface::class), - ), - ); - } - public function testStringCast() { - $this->assertSame('∅∪∅', (Union::of(Set::of(), Set::of()))->toString()); + $this->assertSame('∅∪∅', Set::of()->union(Set::of())->toString()); } public function testContains() { - $union = Union::of( + $union = Set::of( + Number::of(1), + Number::of(2), + )->union( Set::of( - Integer::of(1), - Integer::of(2), - ), - Set::of( - Integer::of(4), - Integer::of(5), + Number::of(4), + Number::of(5), ), ); - $this->assertTrue($union->contains(Real::of(1))); - $this->assertTrue($union->contains(Real::of(2))); - $this->assertTrue($union->contains(Real::of(4))); - $this->assertTrue($union->contains(Real::of(5))); - $this->assertFalse($union->contains(Real::of(6))); - $this->assertFalse($union->contains(Real::of(3))); - $this->assertFalse($union->contains(Real::of(0))); + $this->assertTrue($union->contains(Number::of(1))); + $this->assertTrue($union->contains(Number::of(2))); + $this->assertTrue($union->contains(Number::of(4))); + $this->assertTrue($union->contains(Number::of(5))); + $this->assertFalse($union->contains(Number::of(6))); + $this->assertFalse($union->contains(Number::of(3))); + $this->assertFalse($union->contains(Number::of(0))); } public function testAccept() { - $set = Union::of( - Set::of( - Integer::of(1), - Integer::of(2), - ), + $set = Set::of( + Number::of(1), + Number::of(2), + )->union( Set::of( - Integer::of(4), - Integer::of(5), + Number::of(4), + Number::of(5), ), ); - $this->assertNull($set->accept(Integer::of(1))); + $this->assertInstanceOf( + SideEffect::class, + $set->accept(Number::of(1))->unwrap(), + ); $this->expectException(OutOfDefinitionSet::class); $this->expectExceptionMessage('0.1 ∉ {1;2}∪{4;5}'); - $set->accept(Real::of(0.1)); + $_ = $set->accept(Number::of(0.1))->unwrap(); } public function testUnion() { - $union = Union::of(Set::of(), Set::of())->union(Set::of()); + $union = Set::of()->union(Set::of())->union(Set::of()); - $this->assertInstanceOf(Union::class, $union); $this->assertSame('∅∪∅∪∅', $union->toString()); } public function testIntersect() { - $intersection = Union::of(Set::of(), Set::of())->intersect(Set::of()); + $intersection = Set::of()->union(Set::of())->intersect(Set::of()); - $this->assertInstanceOf(Intersection::class, $intersection); $this->assertSame('∅∪∅∩∅', $intersection->toString()); } } diff --git a/tests/FunctionsTest.php b/tests/FunctionsTest.php index 962c1ac..db83211 100644 --- a/tests/FunctionsTest.php +++ b/tests/FunctionsTest.php @@ -7,18 +7,18 @@ max as maximum, min as minimum, }; -use Innmind\Math\Algebra\Real; -use PHPUnit\Framework\TestCase; +use Innmind\Math\Algebra\Number; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class FunctionsTest extends TestCase { public function testMax() { $number = maximum( - Real::of(1), - Real::of(2), - $expected = Real::of(4), - Real::of(3), + Number::of(1), + Number::of(2), + $expected = Number::of(4), + Number::of(3), ); $this->assertSame($expected, $number); @@ -27,10 +27,10 @@ public function testMax() public function testMin() { $number = minimum( - Real::of(2), - $expected = Real::of(1), - Real::of(4), - Real::of(3), + Number::of(2), + $expected = Number::of(1), + Number::of(4), + Number::of(3), ); $this->assertSame($expected, $number); diff --git a/tests/Geometry/Angle/DegreeTest.php b/tests/Geometry/Angle/DegreeTest.php index b0f85f4..d238afb 100644 --- a/tests/Geometry/Angle/DegreeTest.php +++ b/tests/Geometry/Angle/DegreeTest.php @@ -6,47 +6,46 @@ use Innmind\Math\{ Geometry\Angle\Degree, Geometry\Angle\Radian, - Algebra\Real, + Algebra\Number, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; class DegreeTest extends TestCase { - /** - * @dataProvider degrees - */ + #[DataProvider('degrees')] public function testStringCast($degree, $expected) { - $this->assertSame($expected, (Degree::of(Real::of($degree)))->toString()); + $this->assertSame($expected, Degree::of(Number::of($degree))->toString()); } public function testIsRight() { - $this->assertTrue((Degree::of(Real::of(90)))->isRight()); - $this->assertFalse((Degree::of(Real::of(91)))->isRight()); + $this->assertTrue(Degree::of(Number::of(90))->right()); + $this->assertFalse(Degree::of(Number::of(91))->right()); } public function testIsObtuse() { - $this->assertTrue((Degree::of(Real::of(91)))->isObtuse()); - $this->assertFalse((Degree::of(Real::of(89)))->isObtuse()); + $this->assertTrue(Degree::of(Number::of(91))->obtuse()); + $this->assertFalse(Degree::of(Number::of(89))->obtuse()); } public function testIsAcuse() { - $this->assertTrue((Degree::of(Real::of(89)))->isAcuse()); - $this->assertFalse((Degree::of(Real::of(90)))->isAcuse()); + $this->assertTrue(Degree::of(Number::of(89))->acuse()); + $this->assertFalse(Degree::of(Number::of(90))->acuse()); } public function testIsFlat() { - $this->assertTrue((Degree::of(Real::of(180)))->isFlat()); - $this->assertFalse((Degree::of(Real::of(181)))->isFlat()); + $this->assertTrue(Degree::of(Number::of(180))->flat()); + $this->assertFalse(Degree::of(Number::of(181))->flat()); } public function testOpposite() { - $degree = Degree::of(Real::of(270)); + $degree = Degree::of(Number::of(270)); $opposite = $degree->opposite(); $this->assertNotSame($opposite, $degree); @@ -55,19 +54,17 @@ public function testOpposite() $this->assertSame('90°', $opposite->toString()); } - /** - * @dataProvider degrees - */ + #[DataProvider('degrees')] public function testToRadian($degree, $string, $expected) { - $degree = Degree::of(Real::of($degree)); + $degree = Degree::of(Number::of($degree)); $radian = $degree->toRadian(); $this->assertInstanceOf(Radian::class, $radian); $this->assertSame($expected, $radian->toString()); } - public function degrees(): array + public static function degrees(): array { return [ [0, '0°', '0 rad'], diff --git a/tests/Geometry/Angle/RadianTest.php b/tests/Geometry/Angle/RadianTest.php index 92db91d..8f0da8a 100644 --- a/tests/Geometry/Angle/RadianTest.php +++ b/tests/Geometry/Angle/RadianTest.php @@ -6,47 +6,46 @@ use Innmind\Math\{ Geometry\Angle\Radian, Geometry\Angle\Degree, - Algebra\Real, + Algebra\Number, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; class RadianTest extends TestCase { - /** - * @dataProvider radians - */ + #[DataProvider('radians')] public function testStringCast($radian, $expected) { - $this->assertSame($expected, (Radian::of(Real::of($radian)))->toString()); + $this->assertSame($expected, Radian::of(Number::of($radian))->toString()); } public function testIsRight() { - $this->assertTrue((Radian::of(Real::of(\M_PI_2)))->isRight()); - $this->assertFalse((Radian::of(Real::of(\M_PI)))->isRight()); + $this->assertTrue(Radian::of(Number::of(\M_PI_2))->right()); + $this->assertFalse(Radian::of(Number::of(\M_PI))->right()); } public function testIsObtuse() { - $this->assertTrue((Radian::of(Real::of((2 * \pi()) / 3)))->isObtuse()); - $this->assertFalse((Radian::of(Real::of(\pi() / 3)))->isObtuse()); + $this->assertTrue(Radian::of(Number::of((2 * \pi()) / 3))->obtuse()); + $this->assertFalse(Radian::of(Number::of(\pi() / 3))->obtuse()); } public function testIsAcuse() { - $this->assertTrue((Radian::of(Real::of(\pi() / 3)))->isAcuse()); - $this->assertFalse((Radian::of(Real::of(\M_PI_2)))->isAcuse()); + $this->assertTrue(Radian::of(Number::of(\pi() / 3))->acuse()); + $this->assertFalse(Radian::of(Number::of(\M_PI_2))->acuse()); } public function testIsFlat() { - $this->assertTrue((Radian::of(Real::of(\pi())))->isFlat()); - $this->assertFalse((Radian::of(Real::of((5 * \pi()) / 4)))->isFlat()); + $this->assertTrue(Radian::of(Number::of(\pi()))->flat()); + $this->assertFalse(Radian::of(Number::of((5 * \pi()) / 4))->flat()); } public function testOpposite() { - $radian = Radian::of(Real::of(\pi() / 4)); + $radian = Radian::of(Number::of(\pi() / 4)); $opposite = $radian->opposite(); $this->assertNotSame($opposite, $radian); @@ -54,19 +53,17 @@ public function testOpposite() $this->assertSame((5 * \pi()) / 4, $opposite->number()->value()); } - /** - * @dataProvider radians - */ + #[DataProvider('radians')] public function testToDegree($radian, $string, $expected) { - $radian = Radian::of(Real::of($radian)); + $radian = Radian::of(Number::of($radian)); $degree = $radian->toDegree(); $this->assertInstanceOf(Degree::class, $degree); $this->assertSame($expected, $degree->toString()); } - public function radians(): array + public static function radians(): array { return [ [0, '0 rad', '0°'], diff --git a/tests/Geometry/AngleTest.php b/tests/Geometry/AngleTest.php index 2c42717..68d123f 100644 --- a/tests/Geometry/AngleTest.php +++ b/tests/Geometry/AngleTest.php @@ -8,18 +8,17 @@ Geometry\Angle\Degree, Geometry\Segment, Algebra\Number, - Algebra\Integer }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class AngleTest extends TestCase { public function testInterface() { $angle = Angle::of( - $first = Segment::of(Integer::of(1)), - $degree = Degree::of(Integer::of(42)), - $second = Segment::of(Integer::of(1)), + $first = Segment::of(Number::of(1)), + $degree = Degree::of(Number::of(42)), + $second = Segment::of(Number::of(1)), ); $this->assertSame($first, $angle->firstSegment()); @@ -30,9 +29,9 @@ public function testInterface() public function testSum() { $angle = Angle::of( - Segment::of(Integer::of(5)), - Degree::of(Integer::of(49)), - Segment::of(Integer::of(7)), + Segment::of(Number::of(5)), + Degree::of(Number::of(49)), + Segment::of(Number::of(7)), ); $segment = $angle->sum(); @@ -47,9 +46,9 @@ public function testSum() public function testScalarProduct() { $angle = Angle::of( - Segment::of(Integer::of(5)), - Degree::of(Integer::of(49)), - Segment::of(Integer::of(7)), + Segment::of(Number::of(5)), + Degree::of(Number::of(49)), + Segment::of(Number::of(7)), ); $number = $angle->scalarProduct(); @@ -60,4 +59,13 @@ public function testScalarProduct() 0.00000000000001, ); } + + private function assertEqualsWithDelta( + int|float $expected, + int|float $value, + int|float $delta, + ): void { + $this->assertGreaterThanOrEqual($expected-$delta, $value); + $this->assertLessThanOrEqual($expected+$delta, $value); + } } diff --git a/tests/Geometry/Figure/CircleTest.php b/tests/Geometry/Figure/CircleTest.php index 244c5bd..e07ecc3 100644 --- a/tests/Geometry/Figure/CircleTest.php +++ b/tests/Geometry/Figure/CircleTest.php @@ -8,9 +8,8 @@ Geometry\Figure, Geometry\Segment, Algebra\Number, - Algebra\Integer }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class CircleTest extends TestCase { @@ -19,7 +18,7 @@ public function testInterface() $this->assertInstanceOf( Figure::class, Circle::of( - Segment::of(Integer::of(2)), + Segment::of(Number::of(2)), ), ); } @@ -27,7 +26,7 @@ public function testInterface() public function testPerimeter() { $circle = Circle::of( - Segment::of(Integer::of(4)), + Segment::of(Number::of(4)), ); $this->assertInstanceOf(Number::class, $circle->perimeter()); @@ -37,7 +36,7 @@ public function testPerimeter() public function testArea() { $circle = Circle::of( - Segment::of(Integer::of(4)), + Segment::of(Number::of(4)), ); $this->assertInstanceOf(Number::class, $circle->area()); @@ -47,7 +46,7 @@ public function testArea() public function testRadius() { $circle = Circle::of( - $expected = Segment::of(Integer::of(4)), + $expected = Segment::of(Number::of(4)), ); $this->assertSame($expected, $circle->radius()); @@ -56,7 +55,7 @@ public function testRadius() public function testDiameter() { $circle = Circle::of( - Segment::of(Integer::of(4)), + Segment::of(Number::of(4)), ); $this->assertInstanceOf(Segment::class, $circle->diameter()); diff --git a/tests/Geometry/Figure/RectangleTest.php b/tests/Geometry/Figure/RectangleTest.php index f032803..a2a9a82 100644 --- a/tests/Geometry/Figure/RectangleTest.php +++ b/tests/Geometry/Figure/RectangleTest.php @@ -8,9 +8,8 @@ Geometry\Figure, Geometry\Segment, Algebra\Number, - Algebra\Integer }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class RectangleTest extends TestCase { @@ -19,8 +18,8 @@ public function testInterface() $this->assertInstanceOf( Figure::class, Rectangle::of( - Segment::of(Integer::of(2)), - Segment::of(Integer::of(4)), + Segment::of(Number::of(2)), + Segment::of(Number::of(4)), ), ); } @@ -28,8 +27,8 @@ public function testInterface() public function testPerimeter() { $rectangle = Rectangle::of( - Segment::of(Integer::of(2)), - Segment::of(Integer::of(4)), + Segment::of(Number::of(2)), + Segment::of(Number::of(4)), ); $this->assertInstanceOf(Number::class, $rectangle->perimeter()); @@ -39,8 +38,8 @@ public function testPerimeter() public function testArea() { $rectangle = Rectangle::of( - Segment::of(Integer::of(2)), - Segment::of(Integer::of(4)), + Segment::of(Number::of(2)), + Segment::of(Number::of(4)), ); $this->assertInstanceOf(Number::class, $rectangle->area()); @@ -50,8 +49,8 @@ public function testArea() public function testLength() { $rectangle = Rectangle::of( - $expected = Segment::of(Integer::of(2)), - Segment::of(Integer::of(4)), + $expected = Segment::of(Number::of(2)), + Segment::of(Number::of(4)), ); $this->assertSame($expected, $rectangle->length()); @@ -60,8 +59,8 @@ public function testLength() public function testWidth() { $rectangle = Rectangle::of( - Segment::of(Integer::of(2)), - $expected = Segment::of(Integer::of(4)), + Segment::of(Number::of(2)), + $expected = Segment::of(Number::of(4)), ); $this->assertSame($expected, $rectangle->width()); diff --git a/tests/Geometry/Figure/SquareTest.php b/tests/Geometry/Figure/SquareTest.php index ee5ad2b..1c476b8 100644 --- a/tests/Geometry/Figure/SquareTest.php +++ b/tests/Geometry/Figure/SquareTest.php @@ -8,9 +8,8 @@ Geometry\Figure, Geometry\Segment, Algebra\Number, - Algebra\Integer }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class SquareTest extends TestCase { @@ -19,7 +18,7 @@ public function testInterface() $this->assertInstanceOf( Figure::class, Square::of( - Segment::of(Integer::of(2)), + Segment::of(Number::of(2)), ), ); } @@ -27,7 +26,7 @@ public function testInterface() public function testPerimeter() { $square = Square::of( - Segment::of(Integer::of(2)), + Segment::of(Number::of(2)), ); $this->assertInstanceOf(Number::class, $square->perimeter()); @@ -37,7 +36,7 @@ public function testPerimeter() public function testArea() { $square = Square::of( - Segment::of(Integer::of(2)), + Segment::of(Number::of(2)), ); $this->assertInstanceOf(Number::class, $square->area()); @@ -47,7 +46,7 @@ public function testArea() public function testSide() { $square = Square::of( - $expected = Segment::of(Integer::of(2)), + $expected = Segment::of(Number::of(2)), ); $this->assertSame($expected, $square->side()); diff --git a/tests/Geometry/Figure/TriangleTest.php b/tests/Geometry/Figure/TriangleTest.php index 698d56e..53b132d 100644 --- a/tests/Geometry/Figure/TriangleTest.php +++ b/tests/Geometry/Figure/TriangleTest.php @@ -8,19 +8,17 @@ Geometry\Figure, Geometry\Segment, Algebra\Number, - Algebra\Integer, - Algebra\Real, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class TriangleTest extends TestCase { public function testInterface() { $triangle = Triangle::of( - Segment::of(Integer::of(2)), - Segment::of(Integer::of(2)), - Segment::of(Integer::of(2)), + Segment::of(Number::of(2)), + Segment::of(Number::of(2)), + Segment::of(Number::of(2)), ); $this->assertInstanceOf(Figure::class, $triangle); @@ -29,9 +27,9 @@ public function testInterface() public function testPerimeter() { $triangle = Triangle::of( - Segment::of(Integer::of(2)), - Segment::of(Integer::of(3)), - Segment::of(Integer::of(4)), + Segment::of(Number::of(2)), + Segment::of(Number::of(3)), + Segment::of(Number::of(4)), ); $this->assertInstanceOf(Number::class, $triangle->perimeter()); @@ -41,9 +39,9 @@ public function testPerimeter() public function testArea() { $triangle = Triangle::of( - Segment::of(Real::of(9.8)), - Segment::of(Integer::of(24)), - Segment::of(Integer::of(16)), + Segment::of(Number::of(9.8)), + Segment::of(Number::of(24)), + Segment::of(Number::of(16)), ); $this->assertInstanceOf(Number::class, $triangle->area()); @@ -53,25 +51,25 @@ public function testArea() public function testBase() { $triangle = Triangle::of( - $expected = Segment::of(Integer::of(24)), - Segment::of(Real::of(9.8)), - Segment::of(Integer::of(16)), + $expected = Segment::of(Number::of(24)), + Segment::of(Number::of(9.8)), + Segment::of(Number::of(16)), ); $this->assertSame($expected, $triangle->base()); $triangle = Triangle::of( - Segment::of(Real::of(9.8)), - $expected = Segment::of(Integer::of(24)), - Segment::of(Integer::of(16)), + Segment::of(Number::of(9.8)), + $expected = Segment::of(Number::of(24)), + Segment::of(Number::of(16)), ); $this->assertSame($expected, $triangle->base()); $triangle = Triangle::of( - Segment::of(Real::of(9.8)), - Segment::of(Integer::of(16)), - $expected = Segment::of(Integer::of(24)), + Segment::of(Number::of(9.8)), + Segment::of(Number::of(16)), + $expected = Segment::of(Number::of(24)), ); $this->assertSame($expected, $triangle->base()); @@ -80,9 +78,9 @@ public function testBase() public function testHeight() { $triangle = Triangle::of( - Segment::of(Real::of(9.8)), - Segment::of(Integer::of(24)), - Segment::of(Integer::of(16)), + Segment::of(Number::of(9.8)), + Segment::of(Number::of(24)), + Segment::of(Number::of(16)), ); $this->assertInstanceOf(Segment::class, $triangle->height()); @@ -95,38 +93,38 @@ public function testHeight() public function testIsIsosceles() { $triangle = Triangle::of( - Segment::of(Real::of(9.8)), - Segment::of(Integer::of(16)), - Segment::of(Integer::of(16)), + Segment::of(Number::of(9.8)), + Segment::of(Number::of(16)), + Segment::of(Number::of(16)), ); - $this->assertTrue($triangle->isIsosceles()); + $this->assertTrue($triangle->isosceles()); $triangle = Triangle::of( - Segment::of(Real::of(9.8)), - Segment::of(Integer::of(15)), - Segment::of(Integer::of(16)), + Segment::of(Number::of(9.8)), + Segment::of(Number::of(15)), + Segment::of(Number::of(16)), ); - $this->assertFalse($triangle->isIsosceles()); + $this->assertFalse($triangle->isosceles()); } public function testIsEquilateral() { $triangle = Triangle::of( - Segment::of(Integer::of(16)), - Segment::of(Integer::of(16)), - Segment::of(Integer::of(16)), + Segment::of(Number::of(16)), + Segment::of(Number::of(16)), + Segment::of(Number::of(16)), ); - $this->assertTrue($triangle->isEquilateral()); + $this->assertTrue($triangle->equilateral()); $triangle = Triangle::of( - Segment::of(Real::of(9.8)), - Segment::of(Integer::of(16)), - Segment::of(Integer::of(16)), + Segment::of(Number::of(9.8)), + Segment::of(Number::of(16)), + Segment::of(Number::of(16)), ); - $this->assertFalse($triangle->isEquilateral()); + $this->assertFalse($triangle->equilateral()); } } diff --git a/tests/Geometry/SegmentTest.php b/tests/Geometry/SegmentTest.php index 424bde8..9dfb470 100644 --- a/tests/Geometry/SegmentTest.php +++ b/tests/Geometry/SegmentTest.php @@ -7,44 +7,44 @@ Geometry\Segment, Geometry\Angle, Geometry\Angle\Degree, - Algebra\Integer, - Exception\LengthMustBePositive + Algebra\Number, + Exception\LengthMustBePositive, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class SegmentTest extends TestCase { public function testLength() { - $segment = Segment::of($length = Integer::of(2)); + $segment = Segment::of($length = Number::of(2)); $this->assertSame($length, $segment->length()); } public function testStringCast() { - $this->assertSame('2', Segment::of(Integer::of(2))->toString()); + $this->assertSame('2', Segment::of(Number::of(2))->toString()); } public function testThrowWhenNullSegment() { $this->expectException(LengthMustBePositive::class); - Segment::of(Integer::of(0)); + Segment::of(Number::of(0)); } public function testThrowWhenNegativeLength() { $this->expectException(LengthMustBePositive::class); - Segment::of(Integer::of(-1)); + Segment::of(Number::of(-1)); } public function testJoin() { - $first = Segment::of(Integer::of(1)); - $second = Segment::of(Integer::of(1)); - $degree = Degree::of(Integer::of(42)); + $first = Segment::of(Number::of(1)); + $second = Segment::of(Number::of(1)); + $degree = Degree::of(Number::of(42)); $angle = $first->join($second, $degree); diff --git a/tests/Geometry/Theorem/AlKashiTest.php b/tests/Geometry/Theorem/AlKashiTest.php index e40c09a..556d627 100644 --- a/tests/Geometry/Theorem/AlKashiTest.php +++ b/tests/Geometry/Theorem/AlKashiTest.php @@ -7,10 +7,10 @@ Geometry\Theorem\AlKashi, Geometry\Angle\Degree, Geometry\Segment, - Algebra\Integer, - Exception\SegmentsCannotBeJoined + Algebra\Number, + Exception\SegmentsCannotBeJoined, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class AlKashiTest extends TestCase { @@ -22,14 +22,14 @@ public function testStringCast() public function testSide() { $c = AlKashi::side( - Segment::of(Integer::of(5)), - Degree::of(Integer::of(49)), - Segment::of(Integer::of(7)), + Segment::of(Number::of(5)), + Degree::of(Number::of(49)), + Segment::of(Number::of(7)), ); $this->assertInstanceOf(Segment::class, $c); $this->assertSame( - '√(((5^2) + (7^2)) - (((2 x 5) x 7) x cos(49°)))', + '√(((5^2) + (7^2)) - (2 x 5 x 7 x cos(49°)))', $c->length()->toString(), ); $this->assertEqualsWithDelta( @@ -42,9 +42,9 @@ public function testSide() public function testAngle() { $ab = AlKashi::angle( - Segment::of(Integer::of(6)), - Segment::of(Integer::of(7)), - Segment::of(Integer::of(8)), + Segment::of(Number::of(6)), + Segment::of(Number::of(7)), + Segment::of(Number::of(8)), ); $this->assertInstanceOf(Degree::class, $ab); @@ -56,9 +56,18 @@ public function testThrowWhenOpenTriangle() $this->expectException(SegmentsCannotBeJoined::class); AlKashi::angle( - Segment::of(Integer::of(1)), - Segment::of(Integer::of(42)), - Segment::of(Integer::of(20)), + Segment::of(Number::of(1)), + Segment::of(Number::of(42)), + Segment::of(Number::of(20)), ); } + + private function assertEqualsWithDelta( + int|float $expected, + int|float $value, + int|float $delta, + ): void { + $this->assertGreaterThanOrEqual($expected-$delta, $value); + $this->assertLessThanOrEqual($expected+$delta, $value); + } } diff --git a/tests/Geometry/Theorem/PythagoraTest.php b/tests/Geometry/Theorem/PythagoraTest.php index 72120e4..154d8aa 100644 --- a/tests/Geometry/Theorem/PythagoraTest.php +++ b/tests/Geometry/Theorem/PythagoraTest.php @@ -6,9 +6,9 @@ use Innmind\Math\{ Geometry\Theorem\Pythagora, Geometry\Segment, - Algebra\Real, + Algebra\Number, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class PythagoraTest extends TestCase { @@ -20,8 +20,8 @@ public function testStringCast() public function testHypotenuse() { $hypotenuse = Pythagora::hypotenuse( - Segment::of(Real::of(3)), - Segment::of(Real::of(2)), + Segment::of(Number::of(3)), + Segment::of(Number::of(2)), ); $this->assertInstanceOf(Segment::class, $hypotenuse); @@ -34,8 +34,8 @@ public function testHypotenuse() public function testAdjacentSide() { $side = Pythagora::adjacentSide( - Segment::of(Real::of(5)), - Segment::of(Real::of(2)), + Segment::of(Number::of(5)), + Segment::of(Number::of(2)), ); $this->assertInstanceOf(Segment::class, $side); diff --git a/tests/Geometry/Trigonometry/ArcCosineTest.php b/tests/Geometry/Trigonometry/ArcCosineTest.php index 797dbd5..12c2eba 100644 --- a/tests/Geometry/Trigonometry/ArcCosineTest.php +++ b/tests/Geometry/Trigonometry/ArcCosineTest.php @@ -4,114 +4,118 @@ namespace Tests\Innmind\Math\Geometry\Trigonometry; use Innmind\Math\{ - Geometry\Trigonometry\ArcCosine, - Geometry\Trigonometry\Cosine, + Geometry\Trigonometry, Geometry\Angle\Degree, + Geometry\Angle\Radian, Algebra\Number, - Algebra\Addition, - Algebra\Subtraction, - Algebra\Multiplication, - Algebra\Division, - Algebra\Floor, - Algebra\Ceil, - Algebra\Modulo, - Algebra\Absolute, - Algebra\Power, - Algebra\SquareRoot, - Algebra\Exponential, - Algebra\BinaryLogarithm, - Algebra\NaturalLogarithm, - Algebra\CommonLogarithm, - Algebra\Signum, - Algebra\Real, + Algebra\Logarithm, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class ArcCosineTest extends TestCase { public function testInterface() { - $acos = ArcCosine::of( - Cosine::of(Degree::of(Real::of(42))), - ); + $acos = Degree::of(Number::of(42)) + ->cosine() + ->apply(Trigonometry::arcCosine); - $this->assertInstanceOf(Number::class, $acos); - $this->assertInstanceOf(Degree::class, $acos->toDegree()); $this->assertSame('cos⁻¹(cos(42°))', $acos->toString()); - $this->assertSame('42°', $acos->toDegree()->toString()); } public function testEquals() { - $acos = ArcCosine::of( - Cosine::of(Degree::of(Real::of(42))), - ); - - $this->assertTrue($acos->equals($acos)); - $this->assertTrue($acos->equals(Real::of(42.0))); - $this->assertFalse($acos->equals(Real::of(0.74))); + $acos = Radian::of( + Degree::of(Number::of(42)) + ->cosine() + ->apply(Trigonometry::arcCosine), + ) + ->toDegree() + ->number(); + + $this->assertTrue($acos->equals(Number::of(42.0))); + $this->assertFalse($acos->equals(Number::of(0.74))); } public function testHigherThan() { - $acos = ArcCosine::of( - Cosine::of(Degree::of(Real::of(42))), - ); - - $this->assertFalse($acos->higherThan($acos)); - $this->assertFalse($acos->higherThan(Real::of(42.0))); - $this->assertTrue($acos->higherThan(Real::of(0.74))); + $acos = Radian::of( + Degree::of(Number::of(42)) + ->cosine() + ->apply(Trigonometry::arcCosine), + ) + ->toDegree() + ->number(); + + $this->assertFalse($acos->higherThan(Number::of(42.0))); + $this->assertTrue($acos->higherThan(Number::of(0.74))); } public function testAdd() { - $acos = ArcCosine::of( - Cosine::of(Degree::of(Real::of(42))), - ); - $number = $acos->add(Real::of(1)); + $acos = Radian::of( + Degree::of(Number::of(42)) + ->cosine() + ->apply(Trigonometry::arcCosine), + ) + ->toDegree() + ->number(); + $number = $acos->add(Number::of(1)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(43.0, $number->value()); } public function testSubtract() { - $acos = ArcCosine::of( - Cosine::of(Degree::of(Real::of(42))), - ); - $number = $acos->subtract(Real::of(66)); + $acos = Radian::of( + Degree::of(Number::of(42)) + ->cosine() + ->apply(Trigonometry::arcCosine), + ) + ->toDegree() + ->number(); + $number = $acos->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-24.0, $number->value()); } public function testDivideBy() { - $acos = ArcCosine::of( - Cosine::of(Degree::of(Real::of(42))), - ); - $number = $acos->divideBy(Real::of(2)); + $acos = Radian::of( + Degree::of(Number::of(42)) + ->cosine() + ->apply(Trigonometry::arcCosine), + ) + ->toDegree() + ->number(); + $number = $acos->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(21.0, $number->value()); } public function testMulitplyBy() { - $acos = ArcCosine::of( - Cosine::of(Degree::of(Real::of(42))), - ); - $number = $acos->multiplyBy(Real::of(2)); + $acos = Radian::of( + Degree::of(Number::of(42)) + ->cosine() + ->apply(Trigonometry::arcCosine), + ) + ->toDegree() + ->number(); + $number = $acos->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(84.0, $number->value()); } public function testRound() { - $number = ArcCosine::of( - Cosine::of(Degree::of(Real::of(42))), - ); + $number = Radian::of( + Degree::of(Number::of(42)) + ->cosine() + ->apply(Trigonometry::arcCosine), + ) + ->toDegree() + ->number(); $this->assertEquals(42.0, $number->roundUp(1)->value()); $this->assertEquals(42.0, $number->roundDown(1)->value()); @@ -121,118 +125,155 @@ public function testRound() public function testFloor() { - $acos = ArcCosine::of( - Cosine::of(Degree::of(Real::of(42))), - ); + $acos = Radian::of( + Degree::of(Number::of(42)) + ->cosine() + ->apply(Trigonometry::arcCosine), + ) + ->toDegree() + ->number(); $number = $acos->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(42.0, $number->value()); } public function testCeil() { - $acos = ArcCosine::of( - Cosine::of(Degree::of(Real::of(42))), - ); + $acos = Radian::of( + Degree::of(Number::of(42)) + ->cosine() + ->apply(Trigonometry::arcCosine), + ) + ->toDegree() + ->number(); $number = $acos->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(42.0, $number->value()); } public function testModulo() { - $acos = ArcCosine::of( - Cosine::of(Degree::of(Real::of(42))), - ); - $number = $acos->modulo(Real::of(3)); + $acos = Radian::of( + Degree::of(Number::of(42)) + ->cosine() + ->apply(Trigonometry::arcCosine), + ) + ->toDegree() + ->number(); + $number = $acos->modulo(Number::of(3)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(0.0, $number->value()); } public function testAbsolute() { - $acos = ArcCosine::of( - Cosine::of(Degree::of(Real::of(42))), - ); + $acos = Radian::of( + Degree::of(Number::of(42)) + ->cosine() + ->apply(Trigonometry::arcCosine), + ) + ->toDegree() + ->number(); $number = $acos->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(42.0, $number->value()); } public function testPower() { - $acos = ArcCosine::of( - Cosine::of(Degree::of(Real::of(42))), - ); - $number = $acos->power(Real::of(2)); + $acos = Radian::of( + Degree::of(Number::of(42)) + ->cosine() + ->apply(Trigonometry::arcCosine), + ) + ->toDegree() + ->number(); + $number = $acos->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(1764.0, $number->value()); } public function testSquareRoot() { - $acos = ArcCosine::of( - Cosine::of(Degree::of(Real::of(42))), - ); + $acos = Radian::of( + Degree::of(Number::of(42)) + ->cosine() + ->apply(Trigonometry::arcCosine), + ) + ->toDegree() + ->number(); $number = $acos->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(6.48074069840786, $number->value()); } public function testExponential() { - $acos = ArcCosine::of( - Cosine::of(Degree::of(Real::of(42))), - ); + $acos = Radian::of( + Degree::of(Number::of(42)) + ->cosine() + ->apply(Trigonometry::arcCosine), + ) + ->toDegree() + ->number(); $number = $acos->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(1.739274941520501E+18, $number->value()); } public function testBinaryLogarithm() { - $number = (ArcCosine::of( - Cosine::of(Degree::of(Real::of(42))), - ))->binaryLogarithm(); + $number = Radian::of( + Degree::of(Number::of(42)) + ->cosine() + ->apply(Trigonometry::arcCosine), + ) + ->toDegree() + ->number() + ->apply(Logarithm::binary); - $this->assertInstanceOf(BinaryLogarithm::class, $number); $this->assertSame(5.392317422778761, $number->value()); } public function testNaturalLogarithm() { - $number = (ArcCosine::of( - Cosine::of(Degree::of(Real::of(42))), - ))->naturalLogarithm(); + $number = Radian::of( + Degree::of(Number::of(42)) + ->cosine() + ->apply(Trigonometry::arcCosine), + ) + ->toDegree() + ->number() + ->apply(Logarithm::natural); - $this->assertInstanceOf(NaturalLogarithm::class, $number); $this->assertSame(3.7376696182833684, $number->value()); } public function testCommonLogarithm() { - $number = (ArcCosine::of( - Cosine::of(Degree::of(Real::of(42))), - ))->commonLogarithm(); + $number = Radian::of( + Degree::of(Number::of(42)) + ->cosine() + ->apply(Trigonometry::arcCosine), + ) + ->toDegree() + ->number() + ->apply(Logarithm::common); - $this->assertInstanceOf(CommonLogarithm::class, $number); $this->assertSame(1.6232492903979006, $number->value()); } public function testSignum() { - $number = (ArcCosine::of( - Cosine::of(Degree::of(Real::of(42))), - ))->signum(); + $number = Radian::of( + Degree::of(Number::of(42)) + ->cosine() + ->apply(Trigonometry::arcCosine), + ) + ->toDegree() + ->number() + ->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } } diff --git a/tests/Geometry/Trigonometry/ArcSineTest.php b/tests/Geometry/Trigonometry/ArcSineTest.php index 17553d7..b1a9d19 100644 --- a/tests/Geometry/Trigonometry/ArcSineTest.php +++ b/tests/Geometry/Trigonometry/ArcSineTest.php @@ -4,114 +4,118 @@ namespace Tests\Innmind\Math\Geometry\Trigonometry; use Innmind\Math\{ - Geometry\Trigonometry\ArcSine, - Geometry\Trigonometry\Sine, + Geometry\Trigonometry, Geometry\Angle\Degree, + Geometry\Angle\Radian, Algebra\Number, - Algebra\Addition, - Algebra\Subtraction, - Algebra\Multiplication, - Algebra\Division, - Algebra\Floor, - Algebra\Ceil, - Algebra\Modulo, - Algebra\Absolute, - Algebra\Power, - Algebra\SquareRoot, - Algebra\Exponential, - Algebra\BinaryLogarithm, - Algebra\NaturalLogarithm, - Algebra\CommonLogarithm, - Algebra\Signum, - Algebra\Real, + Algebra\Logarithm, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class ArcSineTest extends TestCase { public function testInterface() { - $asin = ArcSine::of( - Sine::of(Degree::of(Real::of(42))), - ); + $asin = Degree::of(Number::of(42)) + ->sine() + ->apply(Trigonometry::arcSine); - $this->assertInstanceOf(Number::class, $asin); - $this->assertInstanceOf(Degree::class, $asin->toDegree()); $this->assertSame('sin⁻¹(sin(42°))', $asin->toString()); - $this->assertSame('42°', $asin->toDegree()->toString()); } public function testEquals() { - $asin = ArcSine::of( - Sine::of(Degree::of(Real::of(42))), - ); - - $this->assertTrue($asin->equals($asin)); - $this->assertTrue($asin->equals(Real::of(42.0))); - $this->assertFalse($asin->equals(Real::of(0.74))); + $asin = Radian::of( + Degree::of(Number::of(42)) + ->sine() + ->apply(Trigonometry::arcSine), + ) + ->toDegree() + ->number(); + + $this->assertTrue($asin->equals(Number::of(42.0))); + $this->assertFalse($asin->equals(Number::of(0.74))); } public function testHigherThan() { - $asin = ArcSine::of( - Sine::of(Degree::of(Real::of(42))), - ); - - $this->assertFalse($asin->higherThan($asin)); - $this->assertFalse($asin->higherThan(Real::of(42.0))); - $this->assertTrue($asin->higherThan(Real::of(0.74))); + $asin = Radian::of( + Degree::of(Number::of(42)) + ->sine() + ->apply(Trigonometry::arcSine), + ) + ->toDegree() + ->number(); + + $this->assertFalse($asin->higherThan(Number::of(42.0))); + $this->assertTrue($asin->higherThan(Number::of(0.74))); } public function testAdd() { - $asin = ArcSine::of( - Sine::of(Degree::of(Real::of(42))), - ); - $number = $asin->add(Real::of(1)); + $asin = Radian::of( + Degree::of(Number::of(42)) + ->sine() + ->apply(Trigonometry::arcSine), + ) + ->toDegree() + ->number(); + $number = $asin->add(Number::of(1)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(43.0, $number->value()); } public function testSubtract() { - $asin = ArcSine::of( - Sine::of(Degree::of(Real::of(42))), - ); - $number = $asin->subtract(Real::of(66)); + $asin = Radian::of( + Degree::of(Number::of(42)) + ->sine() + ->apply(Trigonometry::arcSine), + ) + ->toDegree() + ->number(); + $number = $asin->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-24.0, $number->value()); } public function testDivideBy() { - $asin = ArcSine::of( - Sine::of(Degree::of(Real::of(42))), - ); - $number = $asin->divideBy(Real::of(2)); + $asin = Radian::of( + Degree::of(Number::of(42)) + ->sine() + ->apply(Trigonometry::arcSine), + ) + ->toDegree() + ->number(); + $number = $asin->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(21.0, $number->value()); } public function testMulitplyBy() { - $asin = ArcSine::of( - Sine::of(Degree::of(Real::of(42))), - ); - $number = $asin->multiplyBy(Real::of(2)); + $asin = Radian::of( + Degree::of(Number::of(42)) + ->sine() + ->apply(Trigonometry::arcSine), + ) + ->toDegree() + ->number(); + $number = $asin->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(84.0, $number->value()); } public function testRound() { - $number = ArcSine::of( - Sine::of(Degree::of(Real::of(42))), - ); + $number = Radian::of( + Degree::of(Number::of(42)) + ->sine() + ->apply(Trigonometry::arcSine), + ) + ->toDegree() + ->number(); $this->assertEquals(42.0, $number->roundUp(1)->value()); $this->assertEquals(42.0, $number->roundDown(1)->value()); @@ -121,118 +125,155 @@ public function testRound() public function testFloor() { - $asin = ArcSine::of( - Sine::of(Degree::of(Real::of(42))), - ); + $asin = Radian::of( + Degree::of(Number::of(42)) + ->sine() + ->apply(Trigonometry::arcSine), + ) + ->toDegree() + ->number(); $number = $asin->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(42.0, $number->value()); } public function testCeil() { - $asin = ArcSine::of( - Sine::of(Degree::of(Real::of(42))), - ); + $asin = Radian::of( + Degree::of(Number::of(42)) + ->sine() + ->apply(Trigonometry::arcSine), + ) + ->toDegree() + ->number(); $number = $asin->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(42.0, $number->value()); } public function testModulo() { - $asin = ArcSine::of( - Sine::of(Degree::of(Real::of(42))), - ); - $number = $asin->modulo(Real::of(3)); + $asin = Radian::of( + Degree::of(Number::of(42)) + ->sine() + ->apply(Trigonometry::arcSine), + ) + ->toDegree() + ->number(); + $number = $asin->modulo(Number::of(3)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(0.0, $number->value()); } public function testAbsolute() { - $asin = ArcSine::of( - Sine::of(Degree::of(Real::of(42))), - ); + $asin = Radian::of( + Degree::of(Number::of(42)) + ->sine() + ->apply(Trigonometry::arcSine), + ) + ->toDegree() + ->number(); $number = $asin->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(42.0, $number->value()); } public function testPower() { - $asin = ArcSine::of( - Sine::of(Degree::of(Real::of(42))), - ); - $number = $asin->power(Real::of(2)); + $asin = Radian::of( + Degree::of(Number::of(42)) + ->sine() + ->apply(Trigonometry::arcSine), + ) + ->toDegree() + ->number(); + $number = $asin->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(1764.0, $number->value()); } public function testSquareRoot() { - $asin = ArcSine::of( - Sine::of(Degree::of(Real::of(42))), - ); + $asin = Radian::of( + Degree::of(Number::of(42)) + ->sine() + ->apply(Trigonometry::arcSine), + ) + ->toDegree() + ->number(); $number = $asin->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(6.48074069840786, $number->value()); } public function testExponential() { - $asin = ArcSine::of( - Sine::of(Degree::of(Real::of(42))), - ); + $asin = Radian::of( + Degree::of(Number::of(42)) + ->sine() + ->apply(Trigonometry::arcSine), + ) + ->toDegree() + ->number(); $number = $asin->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(1.739274941520501E+18, $number->value()); } public function testBinaryLogarithm() { - $number = ArcSine::of( - Sine::of(Degree::of(Real::of(42))), - )->binaryLogarithm(); + $number = Radian::of( + Degree::of(Number::of(42)) + ->sine() + ->apply(Trigonometry::arcSine), + ) + ->toDegree() + ->number() + ->apply(Logarithm::binary); - $this->assertInstanceOf(BinaryLogarithm::class, $number); $this->assertSame(5.392317422778761, $number->value()); } public function testNaturalLogarithm() { - $number = ArcSine::of( - Sine::of(Degree::of(Real::of(42))), - )->naturalLogarithm(); + $number = Radian::of( + Degree::of(Number::of(42)) + ->sine() + ->apply(Trigonometry::arcSine), + ) + ->toDegree() + ->number() + ->apply(Logarithm::natural); - $this->assertInstanceOf(NaturalLogarithm::class, $number); $this->assertSame(3.7376696182833684, $number->value()); } public function testCommonLogarithm() { - $number = ArcSine::of( - Sine::of(Degree::of(Real::of(42))), - )->commonLogarithm(); + $number = Radian::of( + Degree::of(Number::of(42)) + ->sine() + ->apply(Trigonometry::arcSine), + ) + ->toDegree() + ->number() + ->apply(Logarithm::common); - $this->assertInstanceOf(CommonLogarithm::class, $number); $this->assertSame(1.6232492903979006, $number->value()); } public function testSignum() { - $number = ArcSine::of( - Sine::of(Degree::of(Real::of(42))), - )->signum(); + $number = Radian::of( + Degree::of(Number::of(42)) + ->sine() + ->apply(Trigonometry::arcSine), + ) + ->toDegree() + ->number() + ->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } } diff --git a/tests/Geometry/Trigonometry/ArcTangentTest.php b/tests/Geometry/Trigonometry/ArcTangentTest.php index 0fd3e79..fd577df 100644 --- a/tests/Geometry/Trigonometry/ArcTangentTest.php +++ b/tests/Geometry/Trigonometry/ArcTangentTest.php @@ -4,114 +4,118 @@ namespace Tests\Innmind\Math\Geometry\Trigonometry; use Innmind\Math\{ - Geometry\Trigonometry\ArcTangent, - Geometry\Trigonometry\Tangent, + Geometry\Trigonometry, Geometry\Angle\Degree, + Geometry\Angle\Radian, Algebra\Number, - Algebra\Addition, - Algebra\Subtraction, - Algebra\Multiplication, - Algebra\Division, - Algebra\Floor, - Algebra\Ceil, - Algebra\Modulo, - Algebra\Absolute, - Algebra\Power, - Algebra\SquareRoot, - Algebra\Exponential, - Algebra\BinaryLogarithm, - Algebra\NaturalLogarithm, - Algebra\CommonLogarithm, - Algebra\Signum, - Algebra\Real, + Algebra\Logarithm, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class ArcTangentTest extends TestCase { public function testInterface() { - $atan = ArcTangent::of( - Tangent::of(Degree::of(Real::of(42))), - ); + $atan = Degree::of(Number::of(42)) + ->tangent() + ->apply(Trigonometry::arcTangent); - $this->assertInstanceOf(Number::class, $atan); - $this->assertInstanceOf(Degree::class, $atan->toDegree()); $this->assertSame('tan⁻¹(tan(42°))', $atan->toString()); - $this->assertSame('42°', $atan->toDegree()->toString()); } public function testEquals() { - $atan = ArcTangent::of( - Tangent::of(Degree::of(Real::of(42))), - ); - - $this->assertTrue($atan->equals($atan)); - $this->assertTrue($atan->equals(Real::of(42.0))); - $this->assertFalse($atan->equals(Real::of(0.74))); + $atan = Radian::of( + Degree::of(Number::of(42)) + ->tangent() + ->apply(Trigonometry::arcTangent), + ) + ->toDegree() + ->number(); + + $this->assertTrue($atan->equals(Number::of(42.0))); + $this->assertFalse($atan->equals(Number::of(0.74))); } public function testHigherThan() { - $atan = ArcTangent::of( - Tangent::of(Degree::of(Real::of(42))), - ); - - $this->assertFalse($atan->higherThan($atan)); - $this->assertFalse($atan->higherThan(Real::of(42.0))); - $this->assertTrue($atan->higherThan(Real::of(0.74))); + $atan = Radian::of( + Degree::of(Number::of(42)) + ->tangent() + ->apply(Trigonometry::arcTangent), + ) + ->toDegree() + ->number(); + + $this->assertFalse($atan->higherThan(Number::of(42.0))); + $this->assertTrue($atan->higherThan(Number::of(0.74))); } public function testAdd() { - $atan = ArcTangent::of( - Tangent::of(Degree::of(Real::of(42))), - ); - $number = $atan->add(Real::of(1)); + $atan = Radian::of( + Degree::of(Number::of(42)) + ->tangent() + ->apply(Trigonometry::arcTangent), + ) + ->toDegree() + ->number(); + $number = $atan->add(Number::of(1)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(43.0, $number->value()); } public function testSubtract() { - $atan = ArcTangent::of( - Tangent::of(Degree::of(Real::of(42))), - ); - $number = $atan->subtract(Real::of(66)); + $atan = Radian::of( + Degree::of(Number::of(42)) + ->tangent() + ->apply(Trigonometry::arcTangent), + ) + ->toDegree() + ->number(); + $number = $atan->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-24.0, $number->value()); } public function testDivideBy() { - $atan = ArcTangent::of( - Tangent::of(Degree::of(Real::of(42))), - ); - $number = $atan->divideBy(Real::of(2)); + $atan = Radian::of( + Degree::of(Number::of(42)) + ->tangent() + ->apply(Trigonometry::arcTangent), + ) + ->toDegree() + ->number(); + $number = $atan->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(21.0, $number->value()); } public function testMulitplyBy() { - $atan = ArcTangent::of( - Tangent::of(Degree::of(Real::of(42))), - ); - $number = $atan->multiplyBy(Real::of(2)); + $atan = Radian::of( + Degree::of(Number::of(42)) + ->tangent() + ->apply(Trigonometry::arcTangent), + ) + ->toDegree() + ->number(); + $number = $atan->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(84.0, $number->value()); } public function testRound() { - $number = ArcTangent::of( - Tangent::of(Degree::of(Real::of(42))), - ); + $number = Radian::of( + Degree::of(Number::of(42)) + ->tangent() + ->apply(Trigonometry::arcTangent), + ) + ->toDegree() + ->number(); $this->assertEquals(42.0, $number->roundUp(1)->value()); $this->assertEquals(42.0, $number->roundDown(1)->value()); @@ -121,118 +125,155 @@ public function testRound() public function testFloor() { - $atan = ArcTangent::of( - Tangent::of(Degree::of(Real::of(42))), - ); + $atan = Radian::of( + Degree::of(Number::of(42)) + ->tangent() + ->apply(Trigonometry::arcTangent), + ) + ->toDegree() + ->number(); $number = $atan->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(42.0, $number->value()); } public function testCeil() { - $atan = ArcTangent::of( - Tangent::of(Degree::of(Real::of(42))), - ); + $atan = Radian::of( + Degree::of(Number::of(42)) + ->tangent() + ->apply(Trigonometry::arcTangent), + ) + ->toDegree() + ->number(); $number = $atan->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(42.0, $number->value()); } public function testModulo() { - $atan = ArcTangent::of( - Tangent::of(Degree::of(Real::of(42))), - ); - $number = $atan->modulo(Real::of(3)); + $atan = Radian::of( + Degree::of(Number::of(42)) + ->tangent() + ->apply(Trigonometry::arcTangent), + ) + ->toDegree() + ->number(); + $number = $atan->modulo(Number::of(3)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(0.0, $number->value()); } public function testAbsolute() { - $atan = ArcTangent::of( - Tangent::of(Degree::of(Real::of(42))), - ); + $atan = Radian::of( + Degree::of(Number::of(42)) + ->tangent() + ->apply(Trigonometry::arcTangent), + ) + ->toDegree() + ->number(); $number = $atan->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(42.0, $number->value()); } public function testPower() { - $atan = ArcTangent::of( - Tangent::of(Degree::of(Real::of(42))), - ); - $number = $atan->power(Real::of(2)); + $atan = Radian::of( + Degree::of(Number::of(42)) + ->tangent() + ->apply(Trigonometry::arcTangent), + ) + ->toDegree() + ->number(); + $number = $atan->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(1764.0, $number->value()); } public function testSquareRoot() { - $atan = ArcTangent::of( - Tangent::of(Degree::of(Real::of(42))), - ); + $atan = Radian::of( + Degree::of(Number::of(42)) + ->tangent() + ->apply(Trigonometry::arcTangent), + ) + ->toDegree() + ->number(); $number = $atan->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(6.48074069840786, $number->value()); } public function testExponential() { - $atan = ArcTangent::of( - Tangent::of(Degree::of(Real::of(42))), - ); + $atan = Radian::of( + Degree::of(Number::of(42)) + ->tangent() + ->apply(Trigonometry::arcTangent), + ) + ->toDegree() + ->number(); $number = $atan->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(1.739274941520501E+18, $number->value()); } public function testBinaryLogarithm() { - $number = ArcTangent::of( - Tangent::of(Degree::of(Real::of(42))), - )->binaryLogarithm(); + $number = Radian::of( + Degree::of(Number::of(42)) + ->tangent() + ->apply(Trigonometry::arcTangent), + ) + ->toDegree() + ->number() + ->apply(Logarithm::binary); - $this->assertInstanceOf(BinaryLogarithm::class, $number); $this->assertSame(5.392317422778761, $number->value()); } public function testNaturalLogarithm() { - $number = ArcTangent::of( - Tangent::of(Degree::of(Real::of(42))), - )->naturalLogarithm(); + $number = Radian::of( + Degree::of(Number::of(42)) + ->tangent() + ->apply(Trigonometry::arcTangent), + ) + ->toDegree() + ->number() + ->apply(Logarithm::natural); - $this->assertInstanceOf(NaturalLogarithm::class, $number); $this->assertSame(3.7376696182833684, $number->value()); } public function testCommonLogarithm() { - $number = ArcTangent::of( - Tangent::of(Degree::of(Real::of(42))), - )->commonLogarithm(); + $number = Radian::of( + Degree::of(Number::of(42)) + ->tangent() + ->apply(Trigonometry::arcTangent), + ) + ->toDegree() + ->number() + ->apply(Logarithm::common); - $this->assertInstanceOf(CommonLogarithm::class, $number); $this->assertSame(1.6232492903979006, $number->value()); } public function testSignum() { - $number = ArcTangent::of( - Tangent::of(Degree::of(Real::of(42))), - )->signum(); + $number = Radian::of( + Degree::of(Number::of(42)) + ->tangent() + ->apply(Trigonometry::arcTangent), + ) + ->toDegree() + ->number() + ->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } } diff --git a/tests/Geometry/Trigonometry/CosineTest.php b/tests/Geometry/Trigonometry/CosineTest.php index b41ea4b..ada9619 100644 --- a/tests/Geometry/Trigonometry/CosineTest.php +++ b/tests/Geometry/Trigonometry/CosineTest.php @@ -4,96 +4,75 @@ namespace Tests\Innmind\Math\Geometry\Trigonometry; use Innmind\Math\{ - Geometry\Trigonometry\Cosine, Geometry\Angle\Degree, Algebra\Number, - Algebra\Addition, - Algebra\Subtraction, - Algebra\Multiplication, - Algebra\Division, - Algebra\Floor, - Algebra\Ceil, - Algebra\Modulo, - Algebra\Absolute, - Algebra\Power, - Algebra\SquareRoot, - Algebra\Exponential, - Algebra\BinaryLogarithm, - Algebra\NaturalLogarithm, - Algebra\CommonLogarithm, - Algebra\Signum, - Algebra\Real, + Algebra\Logarithm, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class CosineTest extends TestCase { public function testInterface() { - $cos = Cosine::of(Degree::of(Real::of(42))); + $cos = Degree::of(Number::of(42))->cosine(); - $this->assertInstanceOf(Number::class, $cos); $this->assertSame(0.7431448254773942, $cos->value()); $this->assertSame('cos(42°)', $cos->toString()); } public function testEquals() { - $cos = Cosine::of(Degree::of(Real::of(42))); + $cos = Degree::of(Number::of(42))->cosine(); $this->assertTrue($cos->equals($cos)); - $this->assertTrue($cos->equals(Real::of(0.7431448254773942))); - $this->assertFalse($cos->equals(Real::of(0.74))); + $this->assertTrue($cos->equals(Number::of(0.7431448254773942))); + $this->assertFalse($cos->equals(Number::of(0.74))); } public function testHigherThan() { - $cos = Cosine::of(Degree::of(Real::of(42))); + $cos = Degree::of(Number::of(42))->cosine(); $this->assertFalse($cos->higherThan($cos)); - $this->assertFalse($cos->higherThan(Real::of(0.7431448254773942))); - $this->assertTrue($cos->higherThan(Real::of(0.74))); + $this->assertFalse($cos->higherThan(Number::of(0.7431448254773942))); + $this->assertTrue($cos->higherThan(Number::of(0.74))); } public function testAdd() { - $cos = Cosine::of(Degree::of(Real::of(42))); - $number = $cos->add(Real::of(1)); + $cos = Degree::of(Number::of(42))->cosine(); + $number = $cos->add(Number::of(1)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(1.7431448254773942, $number->value()); } public function testSubtract() { - $cos = Cosine::of(Degree::of(Real::of(42))); - $number = $cos->subtract(Real::of(66)); + $cos = Degree::of(Number::of(42))->cosine(); + $number = $cos->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-65.2568551745226, $number->value()); } public function testDivideBy() { - $cos = Cosine::of(Degree::of(Real::of(42))); - $number = $cos->divideBy(Real::of(2)); + $cos = Degree::of(Number::of(42))->cosine(); + $number = $cos->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(0.3715724127386971, $number->value()); } public function testMulitplyBy() { - $cos = Cosine::of(Degree::of(Real::of(42))); - $number = $cos->multiplyBy(Real::of(2)); + $cos = Degree::of(Number::of(42))->cosine(); + $number = $cos->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(1.4862896509547885, $number->value()); } public function testRound() { - $number = Cosine::of(Degree::of(Real::of(42))); + $number = Degree::of(Number::of(42))->cosine(); $this->assertEquals(0.7, $number->roundUp(1)->value()); $this->assertEquals(0.7, $number->roundDown(1)->value()); @@ -103,96 +82,93 @@ public function testRound() public function testFloor() { - $cos = Cosine::of(Degree::of(Real::of(42))); + $cos = Degree::of(Number::of(42))->cosine(); $number = $cos->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(0.0, $number->value()); } public function testCeil() { - $cos = Cosine::of(Degree::of(Real::of(42))); + $cos = Degree::of(Number::of(42))->cosine(); $number = $cos->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(1.0, $number->value()); } public function testModulo() { - $cos = Cosine::of(Degree::of(Real::of(42))); - $number = $cos->modulo(Real::of(3)); + $cos = Degree::of(Number::of(42))->cosine(); + $number = $cos->modulo(Number::of(3)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(0.7431448254773942, $number->value()); } public function testAbsolute() { - $cos = Cosine::of(Degree::of(Real::of(42))); + $cos = Degree::of(Number::of(42))->cosine(); $number = $cos->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(0.7431448254773942, $number->value()); } public function testPower() { - $cos = Cosine::of(Degree::of(Real::of(42))); - $number = $cos->power(Real::of(2)); + $cos = Degree::of(Number::of(42))->cosine(); + $number = $cos->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(0.5522642316338268, $number->value()); } public function testSquareRoot() { - $cos = Cosine::of(Degree::of(Real::of(42))); + $cos = Degree::of(Number::of(42))->cosine(); $number = $cos->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(0.8620584814717586, $number->value()); } public function testExponential() { - $cos = Cosine::of(Degree::of(Real::of(42))); + $cos = Degree::of(Number::of(42))->cosine(); $number = $cos->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(2.1025372410974477, $number->value()); } public function testBinaryLogarithm() { - $number = Cosine::of(Degree::of(Real::of(42)))->binaryLogarithm(); + $number = Degree::of(Number::of(42)) + ->cosine() + ->apply(Logarithm::binary); - $this->assertInstanceOf(BinaryLogarithm::class, $number); $this->assertSame(-0.42828470156966353, $number->value()); } public function testNaturalLogarithm() { - $number = Cosine::of(Degree::of(Real::of(42)))->naturalLogarithm(); + $number = Degree::of(Number::of(42)) + ->cosine() + ->apply(Logarithm::natural); - $this->assertInstanceOf(NaturalLogarithm::class, $number); $this->assertSame(-0.29686433336996987, $number->value()); } public function testCommonLogarithm() { - $number = Cosine::of(Degree::of(Real::of(42)))->commonLogarithm(); + $number = Degree::of(Number::of(42)) + ->cosine() + ->apply(Logarithm::common); - $this->assertInstanceOf(CommonLogarithm::class, $number); $this->assertSame(-0.1289265418564653, $number->value()); } public function testSignum() { - $number = Cosine::of(Degree::of(Real::of(42)))->signum(); + $number = Degree::of(Number::of(42)) + ->cosine() + ->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } } diff --git a/tests/Geometry/Trigonometry/SineTest.php b/tests/Geometry/Trigonometry/SineTest.php index dd86592..42f7592 100644 --- a/tests/Geometry/Trigonometry/SineTest.php +++ b/tests/Geometry/Trigonometry/SineTest.php @@ -4,96 +4,75 @@ namespace Tests\Innmind\Math\Geometry\Trigonometry; use Innmind\Math\{ - Geometry\Trigonometry\Sine, Geometry\Angle\Degree, Algebra\Number, - Algebra\Addition, - Algebra\Subtraction, - Algebra\Multiplication, - Algebra\Division, - Algebra\Floor, - Algebra\Ceil, - Algebra\Modulo, - Algebra\Absolute, - Algebra\Power, - Algebra\SquareRoot, - Algebra\Exponential, - Algebra\BinaryLogarithm, - Algebra\NaturalLogarithm, - Algebra\CommonLogarithm, - Algebra\Signum, - Algebra\Real, + Algebra\Logarithm, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class SineTest extends TestCase { public function testInterface() { - $sin = Sine::of(Degree::of(Real::of(42))); + $sin = Degree::of(Number::of(42))->sine(); - $this->assertInstanceOf(Number::class, $sin); $this->assertSame(0.6691306063588582, $sin->value()); $this->assertSame('sin(42°)', $sin->toString()); } public function testEquals() { - $sin = Sine::of(Degree::of(Real::of(42))); + $sin = Degree::of(Number::of(42))->sine(); $this->assertTrue($sin->equals($sin)); - $this->assertTrue($sin->equals(Real::of(0.6691306063588582))); - $this->assertFalse($sin->equals(Real::of(0.66))); + $this->assertTrue($sin->equals(Number::of(0.6691306063588582))); + $this->assertFalse($sin->equals(Number::of(0.66))); } public function testHigherThan() { - $sin = Sine::of(Degree::of(Real::of(42))); + $sin = Degree::of(Number::of(42))->sine(); $this->assertFalse($sin->higherThan($sin)); - $this->assertFalse($sin->higherThan(Real::of(0.6691306063588582))); - $this->assertTrue($sin->higherThan(Real::of(0.66))); + $this->assertFalse($sin->higherThan(Number::of(0.6691306063588582))); + $this->assertTrue($sin->higherThan(Number::of(0.66))); } public function testAdd() { - $sin = Sine::of(Degree::of(Real::of(42))); - $number = $sin->add(Real::of(1)); + $sin = Degree::of(Number::of(42))->sine(); + $number = $sin->add(Number::of(1)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(1.6691306063588582, $number->value()); } public function testSubtract() { - $sin = Sine::of(Degree::of(Real::of(42))); - $number = $sin->subtract(Real::of(66)); + $sin = Degree::of(Number::of(42))->sine(); + $number = $sin->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-65.33086939364114, $number->value()); } public function testDivideBy() { - $sin = Sine::of(Degree::of(Real::of(42))); - $number = $sin->divideBy(Real::of(2)); + $sin = Degree::of(Number::of(42))->sine(); + $number = $sin->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(0.3345653031794291, $number->value()); } public function testMulitplyBy() { - $sin = Sine::of(Degree::of(Real::of(42))); - $number = $sin->multiplyBy(Real::of(2)); + $sin = Degree::of(Number::of(42))->sine(); + $number = $sin->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(1.3382612127177165, $number->value()); } public function testRound() { - $number = Sine::of(Degree::of(Real::of(42))); + $number = Degree::of(Number::of(42))->sine(); $this->assertEquals(0.7, $number->roundUp(1)->value()); $this->assertEquals(0.7, $number->roundDown(1)->value()); @@ -103,96 +82,93 @@ public function testRound() public function testFloor() { - $sin = Sine::of(Degree::of(Real::of(42))); + $sin = Degree::of(Number::of(42))->sine(); $number = $sin->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(0.0, $number->value()); } public function testCeil() { - $sin = Sine::of(Degree::of(Real::of(42))); + $sin = Degree::of(Number::of(42))->sine(); $number = $sin->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(1.0, $number->value()); } public function testModulo() { - $sin = Sine::of(Degree::of(Real::of(42))); - $number = $sin->modulo(Real::of(3)); + $sin = Degree::of(Number::of(42))->sine(); + $number = $sin->modulo(Number::of(3)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(0.6691306063588582, $number->value()); } public function testAbsolute() { - $sin = Sine::of(Degree::of(Real::of(42))); + $sin = Degree::of(Number::of(42))->sine(); $number = $sin->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(0.6691306063588582, $number->value()); } public function testPower() { - $sin = Sine::of(Degree::of(Real::of(42))); - $number = $sin->power(Real::of(2)); + $sin = Degree::of(Number::of(42))->sine(); + $number = $sin->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(0.4477357683661733, $number->value()); } public function testSquareRoot() { - $sin = Sine::of(Degree::of(Real::of(42))); + $sin = Degree::of(Number::of(42))->sine(); $number = $sin->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(0.8180040381066943, $number->value()); } public function testExponential() { - $sin = Sine::of(Degree::of(Real::of(42))); + $sin = Degree::of(Number::of(42))->sine(); $number = $sin->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(1.9525390574726629, $number->value()); } public function testBinaryLogarithm() { - $number = Sine::of(Degree::of(Real::of(42)))->binaryLogarithm(); + $number = Degree::of(Number::of(42)) + ->sine() + ->apply(Logarithm::binary); - $this->assertInstanceOf(BinaryLogarithm::class, $number); $this->assertSame(-0.5796402595724052, $number->value()); } public function testNaturalLogarithm() { - $number = Sine::of(Degree::of(Real::of(42)))->naturalLogarithm(); + $number = Degree::of(Number::of(42)) + ->sine() + ->apply(Logarithm::natural); - $this->assertInstanceOf(NaturalLogarithm::class, $number); $this->assertSame(-0.4017760116616475, $number->value()); } public function testCommonLogarithm() { - $number = Sine::of(Degree::of(Real::of(42)))->commonLogarithm(); + $number = Degree::of(Number::of(42)) + ->sine() + ->apply(Logarithm::common); - $this->assertInstanceOf(CommonLogarithm::class, $number); $this->assertSame(-0.17448910482575006, $number->value()); } public function testSignum() { - $number = Sine::of(Degree::of(Real::of(42)))->signum(); + $number = Degree::of(Number::of(42)) + ->sine() + ->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } } diff --git a/tests/Geometry/Trigonometry/TangentTest.php b/tests/Geometry/Trigonometry/TangentTest.php index 82a2c58..0c1ba36 100644 --- a/tests/Geometry/Trigonometry/TangentTest.php +++ b/tests/Geometry/Trigonometry/TangentTest.php @@ -4,96 +4,75 @@ namespace Tests\Innmind\Math\Geometry\Trigonometry; use Innmind\Math\{ - Geometry\Trigonometry\Tangent, Geometry\Angle\Degree, Algebra\Number, - Algebra\Addition, - Algebra\Subtraction, - Algebra\Multiplication, - Algebra\Division, - Algebra\Floor, - Algebra\Ceil, - Algebra\Modulo, - Algebra\Absolute, - Algebra\Power, - Algebra\SquareRoot, - Algebra\Exponential, - Algebra\BinaryLogarithm, - Algebra\NaturalLogarithm, - Algebra\CommonLogarithm, - Algebra\Signum, - Algebra\Real, + Algebra\Logarithm, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class TangentTest extends TestCase { public function testInterface() { - $tan = Tangent::of(Degree::of(Real::of(42))); + $tan = Degree::of(Number::of(42))->tangent(); - $this->assertInstanceOf(Number::class, $tan); $this->assertSame(0.9004040442978399, $tan->value()); $this->assertSame('tan(42°)', $tan->toString()); } public function testEquals() { - $tan = Tangent::of(Degree::of(Real::of(42))); + $tan = Degree::of(Number::of(42))->tangent(); $this->assertTrue($tan->equals($tan)); - $this->assertTrue($tan->equals(Real::of(0.9004040442978399))); - $this->assertFalse($tan->equals(Real::of(0.9))); + $this->assertTrue($tan->equals(Number::of(0.9004040442978399))); + $this->assertFalse($tan->equals(Number::of(0.9))); } public function testHigherThan() { - $tan = Tangent::of(Degree::of(Real::of(42))); + $tan = Degree::of(Number::of(42))->tangent(); $this->assertFalse($tan->higherThan($tan)); - $this->assertFalse($tan->higherThan(Real::of(0.9004040442978399))); - $this->assertTrue($tan->higherThan(Real::of(0.9))); + $this->assertFalse($tan->higherThan(Number::of(0.9004040442978399))); + $this->assertTrue($tan->higherThan(Number::of(0.9))); } public function testAdd() { - $tan = Tangent::of(Degree::of(Real::of(42))); - $number = $tan->add(Real::of(1)); + $tan = Degree::of(Number::of(42))->tangent(); + $number = $tan->add(Number::of(1)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(1.90040404429784, $number->value()); } public function testSubtract() { - $tan = Tangent::of(Degree::of(Real::of(42))); - $number = $tan->subtract(Real::of(66)); + $tan = Degree::of(Number::of(42))->tangent(); + $number = $tan->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-65.09959595570216, $number->value()); } public function testDivideBy() { - $tan = Tangent::of(Degree::of(Real::of(42))); - $number = $tan->divideBy(Real::of(2)); + $tan = Degree::of(Number::of(42))->tangent(); + $number = $tan->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(0.45020202214891997, $number->value()); } public function testMulitplyBy() { - $tan = Tangent::of(Degree::of(Real::of(42))); - $number = $tan->multiplyBy(Real::of(2)); + $tan = Degree::of(Number::of(42))->tangent(); + $number = $tan->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(1.8008080885956799, $number->value()); } public function testRound() { - $number = Tangent::of(Degree::of(Real::of(42))); + $number = Degree::of(Number::of(42))->tangent(); $this->assertEquals(0.9, $number->roundUp(1)->value()); $this->assertEquals(0.9, $number->roundDown(1)->value()); @@ -103,88 +82,84 @@ public function testRound() public function testFloor() { - $tan = Tangent::of(Degree::of(Real::of(42))); + $tan = Degree::of(Number::of(42))->tangent(); $number = $tan->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(0.0, $number->value()); } public function testCeil() { - $tan = Tangent::of(Degree::of(Real::of(42))); + $tan = Degree::of(Number::of(42))->tangent(); $number = $tan->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(1.0, $number->value()); } public function testModulo() { - $tan = Tangent::of(Degree::of(Real::of(42))); - $number = $tan->modulo(Real::of(3)); + $tan = Degree::of(Number::of(42))->tangent(); + $number = $tan->modulo(Number::of(3)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(0.9004040442978399, $number->value()); } public function testAbsolute() { - $tan = Tangent::of(Degree::of(Real::of(42))); + $tan = Degree::of(Number::of(42))->tangent(); $number = $tan->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(0.9004040442978399, $number->value()); } public function testPower() { - $tan = Tangent::of(Degree::of(Real::of(42))); - $number = $tan->power(Real::of(2)); + $tan = Degree::of(Number::of(42))->tangent(); + $number = $tan->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(0.8107274429879064, $number->value()); } public function testSquareRoot() { - $tan = Tangent::of(Degree::of(Real::of(42))); + $tan = Degree::of(Number::of(42))->tangent(); $number = $tan->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(0.9488962241983261, $number->value()); } public function testExponential() { - $tan = Tangent::of(Degree::of(Real::of(42))); + $tan = Degree::of(Number::of(42))->tangent(); $number = $tan->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(2.4605971005633154, $number->value()); } public function testBinaryLogarithm() { - $number = Tangent::of(Degree::of(Real::of(42)))->binaryLogarithm(); + $number = Degree::of(Number::of(42)) + ->tangent() + ->apply(Logarithm::binary); - $this->assertInstanceOf(BinaryLogarithm::class, $number); $this->assertSame(-0.1513555580027417, $number->value()); } public function testNaturalLogarithm() { - $number = Tangent::of(Degree::of(Real::of(42)))->naturalLogarithm(); + $number = Degree::of(Number::of(42)) + ->tangent() + ->apply(Logarithm::natural); - $this->assertInstanceOf(NaturalLogarithm::class, $number); $this->assertSame(-0.10491167829167766, $number->value()); } public function testCommonLogarithm() { - $number = Tangent::of(Degree::of(Real::of(42)))->commonLogarithm(); + $number = Degree::of(Number::of(42)) + ->tangent() + ->apply(Logarithm::common); - $this->assertInstanceOf(CommonLogarithm::class, $number); $this->assertEqualsWithDelta( -0.04556256296928478, $number->value(), @@ -194,9 +169,19 @@ public function testCommonLogarithm() public function testSignum() { - $number = Tangent::of(Degree::of(Real::of(42)))->signum(); + $number = Degree::of(Number::of(42)) + ->tangent() + ->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } + + private function assertEqualsWithDelta( + int|float $expected, + int|float $value, + int|float $delta, + ): void { + $this->assertGreaterThanOrEqual($expected-$delta, $value); + $this->assertLessThanOrEqual($expected+$delta, $value); + } } diff --git a/tests/Matrix/ColumnVectorTest.php b/tests/Matrix/ColumnVectorTest.php index d5b7e8a..f05667f 100644 --- a/tests/Matrix/ColumnVectorTest.php +++ b/tests/Matrix/ColumnVectorTest.php @@ -9,11 +9,10 @@ Matrix\RowVector, Matrix, Algebra\Number, - Algebra\Integer, - Algebra\Real, Exception\VectorsMustMeOfTheSameDimension }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; class ColumnVectorTest extends TestCase { @@ -21,7 +20,7 @@ public function testInterface() { $vector = ColumnVector::of(...numerize(1, 2, 3)); - $this->assertSame(3, $vector->dimension()->value()); + $this->assertSame(3, $vector->dimension()); $this->assertInstanceOf(Number::class, $vector->get(0)); $this->assertInstanceOf(Number::class, $vector->get(1)); $this->assertInstanceOf(Number::class, $vector->get(2)); @@ -32,7 +31,7 @@ public function testInterface() [1, 2, 3], $vector ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); } @@ -76,7 +75,7 @@ public function testMultiplyBy() { $column = ColumnVector::of(...numerize(25, 5, 1)); $column2 = $column->multiplyBy( - ColumnVector::initialize(Integer::of(3), Real::of(2.56)), + ColumnVector::initialize(3, Number::of(2.56)), ); $this->assertInstanceOf(ColumnVector::class, $column2); @@ -84,14 +83,14 @@ public function testMultiplyBy() [25, 5, 1], $column ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); $this->assertSame( [64.0, 12.8, 2.56], $column2 ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); } @@ -100,8 +99,8 @@ public function testThrowWhenMultiplyingVectorsOfDifferentDimensions() { $this->expectException(VectorsMustMeOfTheSameDimension::class); - ColumnVector::initialize(Integer::of(1), Real::of(1))->multiplyBy( - ColumnVector::initialize(Integer::of(2), Real::of(1)), + ColumnVector::initialize(1, Number::of(1))->multiplyBy( + ColumnVector::initialize(2, Number::of(1)), ); } @@ -109,7 +108,7 @@ public function testDivideBy() { $column = ColumnVector::of(...numerize(25, 5, 1)); $column2 = $column->divideBy( - ColumnVector::initialize(Integer::of(3), Real::of(5)), + ColumnVector::initialize(3, Number::of(5)), ); $this->assertInstanceOf(ColumnVector::class, $column2); @@ -117,14 +116,14 @@ public function testDivideBy() [25, 5, 1], $column ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); $this->assertSame( [5, 1, 0.2], $column2 ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); } @@ -133,21 +132,21 @@ public function testThrowWhenDividingVectorsOfDifferentDimensions() { $this->expectException(VectorsMustMeOfTheSameDimension::class); - ColumnVector::initialize(Integer::of(1), Real::of(1))->divideBy( - ColumnVector::initialize(Integer::of(2), Real::of(1)), + ColumnVector::initialize(1, Number::of(1))->divideBy( + ColumnVector::initialize(2, Number::of(1)), ); } public function testInitialize() { - $vector = ColumnVector::initialize(Integer::of(4), Real::of(1.2)); + $vector = ColumnVector::initialize(4, Number::of(1.2)); $this->assertInstanceOf(ColumnVector::class, $vector); $this->assertSame( [1.2, 1.2, 1.2, 1.2], $vector ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); } @@ -166,7 +165,7 @@ public function testSubtract() [0.5, -0.5, 0.20000000000000018, -0.20000000000000018], $vector3 ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); } @@ -175,8 +174,8 @@ public function testThrowWhenSubtractingVectorsOfDifferentDimensions() { $this->expectException(VectorsMustMeOfTheSameDimension::class); - ColumnVector::initialize(Integer::of(1), Real::of(1))->subtract( - ColumnVector::initialize(Integer::of(2), Real::of(1)), + ColumnVector::initialize(1, Number::of(1))->subtract( + ColumnVector::initialize(2, Number::of(1)), ); } @@ -194,7 +193,7 @@ public function testAdd() [1.5, 4.5, 5.8, 8.2], $vector3 ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); } @@ -203,8 +202,8 @@ public function testThrowWhenAddingVectorsOfDifferentDimensions() { $this->expectException(VectorsMustMeOfTheSameDimension::class); - ColumnVector::initialize(Integer::of(1), Real::of(1))->add( - ColumnVector::initialize(Integer::of(2), Real::of(1)), + ColumnVector::initialize(1, Number::of(1))->add( + ColumnVector::initialize(2, Number::of(1)), ); } @@ -212,7 +211,7 @@ public function testPower() { $vector1 = ColumnVector::of(...numerize(1, 2, 3, -4)); - $vector2 = $vector1->power(Real::of(3)); + $vector2 = $vector1->power(Number::of(3)); $this->assertInstanceOf(ColumnVector::class, $vector2); $this->assertNotSame($vector2, $vector1); @@ -220,7 +219,7 @@ public function testPower() [1.0, 8.0, 27.0, -64.0], $vector2 ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); } @@ -257,14 +256,14 @@ public function testMap() [1, 2, 3, -4], $vector ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); $this->assertEquals( [1, 4, 9, 16], $vector2 ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); } @@ -294,9 +293,7 @@ public function testEquals() $this->assertFalse($vector->equals(ColumnVector::of(...numerize(1, 2)))); } - /** - * @dataProvider leads - */ + #[DataProvider('leads')] public function testLead($numbers, $expected) { $vector = ColumnVector::of(...numerize(...$numbers)); @@ -305,7 +302,7 @@ public function testLead($numbers, $expected) $this->assertSame($expected, $vector->lead()->value()); } - public function leads(): array + public static function leads(): array { return [ [ diff --git a/tests/Matrix/DimensionTest.php b/tests/Matrix/DimensionTest.php index a24a31a..23d69ae 100644 --- a/tests/Matrix/DimensionTest.php +++ b/tests/Matrix/DimensionTest.php @@ -3,41 +3,36 @@ namespace Tests\Innmind\Math\Matrix; -use Innmind\Math\{ - Matrix\Dimension, - Algebra\Integer, -}; -use PHPUnit\Framework\TestCase; +use Innmind\Math\Matrix\Dimension; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class DimensionTest extends TestCase { public function testInterface() { - $dimension = Dimension::of(Integer::of(2), Integer::of(3)); + $dimension = Dimension::of(2, 3); - $this->assertInstanceOf(Integer::class, $dimension->rows()); - $this->assertInstanceOf(Integer::class, $dimension->columns()); - $this->assertSame(2, $dimension->rows()->value()); - $this->assertSame(3, $dimension->columns()->value()); + $this->assertSame(2, $dimension->rows()); + $this->assertSame(3, $dimension->columns()); $this->assertSame('2 x 3', $dimension->toString()); } public function testEquals() { - $dimension = Dimension::of(Integer::of(2), Integer::of(3)); + $dimension = Dimension::of(2, 3); $this->assertTrue($dimension->equals($dimension)); $this->assertTrue($dimension->equals( - Dimension::of(Integer::of(2), Integer::of(3)), + Dimension::of(2, 3), )); $this->assertFalse($dimension->equals( - Dimension::of(Integer::of(1), Integer::of(3)), + Dimension::of(1, 3), )); $this->assertFalse($dimension->equals( - Dimension::of(Integer::of(2), Integer::of(2)), + Dimension::of(2, 2), )); $this->assertFalse($dimension->equals( - Dimension::of(Integer::of(1), Integer::of(2)), + Dimension::of(1, 2), )); } } diff --git a/tests/Matrix/RowVectorTest.php b/tests/Matrix/RowVectorTest.php index efc29ca..b915c21 100644 --- a/tests/Matrix/RowVectorTest.php +++ b/tests/Matrix/RowVectorTest.php @@ -9,11 +9,10 @@ Matrix\ColumnVector, Matrix, Algebra\Number, - Algebra\Integer, - Algebra\Real, Exception\VectorsMustMeOfTheSameDimension }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; class RowVectorTest extends TestCase { @@ -25,10 +24,10 @@ public function testInterface() [1, 2, 3], $vector ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); - $this->assertSame(3, $vector->dimension()->value()); + $this->assertSame(3, $vector->dimension()); $this->assertInstanceOf(Number::class, $vector->get(0)); $this->assertInstanceOf(Number::class, $vector->get(1)); $this->assertInstanceOf(Number::class, $vector->get(2)); @@ -77,7 +76,7 @@ public function testMultiplyBy() { $row = RowVector::of(...numerize(25, 5, 1)); $row2 = $row->multiplyBy( - RowVector::initialize(Integer::of(3), Real::of(2.56)), + RowVector::initialize(3, Number::of(2.56)), ); $this->assertInstanceOf(RowVector::class, $row2); @@ -85,14 +84,14 @@ public function testMultiplyBy() [25, 5, 1], $row ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); $this->assertSame( [64.0, 12.8, 2.56], $row2 ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); } @@ -101,8 +100,8 @@ public function testThrowWhenMultiplyingVectorsOfDifferentDimensions() { $this->expectException(VectorsMustMeOfTheSameDimension::class); - RowVector::initialize(Integer::of(1), Real::of(1))->multiplyBy( - RowVector::initialize(Integer::of(2), Real::of(1)), + RowVector::initialize(1, Number::of(1))->multiplyBy( + RowVector::initialize(2, Number::of(1)), ); } @@ -110,7 +109,7 @@ public function testDivideBy() { $row = RowVector::of(...numerize(25, 5, 1)); $row2 = $row->divideBy( - RowVector::initialize(Integer::of(3), Real::of(5)), + RowVector::initialize(3, Number::of(5)), ); $this->assertInstanceOf(RowVector::class, $row2); @@ -118,14 +117,14 @@ public function testDivideBy() [25, 5, 1], $row ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); $this->assertSame( [5, 1, 0.2], $row2 ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); } @@ -134,21 +133,21 @@ public function testThrowWhenDevidingVectorsOfDifferentDimensions() { $this->expectException(VectorsMustMeOfTheSameDimension::class); - RowVector::initialize(Integer::of(1), Real::of(1))->divideBy( - RowVector::initialize(Integer::of(2), Real::of(1)), + RowVector::initialize(1, Number::of(1))->divideBy( + RowVector::initialize(2, Number::of(1)), ); } public function testInitialize() { - $vector = RowVector::initialize(Integer::of(4), Real::of(1.2)); + $vector = RowVector::initialize(4, Number::of(1.2)); $this->assertInstanceOf(RowVector::class, $vector); $this->assertSame( [1.2, 1.2, 1.2, 1.2], $vector ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); } @@ -167,7 +166,7 @@ public function testSubtract() [0.5, -0.5, 0.20000000000000018, -0.20000000000000018], $vector3 ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); } @@ -176,8 +175,8 @@ public function testThrowWhenSubtractingVectorsOfDifferentDimensions() { $this->expectException(VectorsMustMeOfTheSameDimension::class); - RowVector::initialize(Integer::of(1), Real::of(1))->subtract( - RowVector::initialize(Integer::of(2), Real::of(1)), + RowVector::initialize(1, Number::of(1))->subtract( + RowVector::initialize(2, Number::of(1)), ); } @@ -195,7 +194,7 @@ public function testAdd() [1.5, 4.5, 5.8, 8.2], $vector3 ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); } @@ -204,8 +203,8 @@ public function testThrowWhenAddingVectorsOfDifferentDimensions() { $this->expectException(VectorsMustMeOfTheSameDimension::class); - RowVector::initialize(Integer::of(1), Real::of(1))->add( - RowVector::initialize(Integer::of(2), Real::of(1)), + RowVector::initialize(1, Number::of(1))->add( + RowVector::initialize(2, Number::of(1)), ); } @@ -213,7 +212,7 @@ public function testPower() { $vector1 = RowVector::of(...numerize(1, 2, 3, -4)); - $vector2 = $vector1->power(Real::of(3)); + $vector2 = $vector1->power(Number::of(3)); $this->assertInstanceOf(RowVector::class, $vector2); $this->assertNotSame($vector2, $vector1); @@ -221,7 +220,7 @@ public function testPower() [1, 8, 27, -64], $vector2 ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); } @@ -258,14 +257,14 @@ public function testMap() [1, 2, 3, -4], $vector ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); $this->assertSame( [1, 4, 9, 16], $vector2 ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); } @@ -295,9 +294,7 @@ public function testEquals() $this->assertFalse($vector->equals(RowVector::of(...numerize(1, 2)))); } - /** - * @dataProvider leads - */ + #[DataProvider('leads')] public function testLead($numbers, $expected) { $vector = RowVector::of(...numerize(...$numbers)); @@ -306,7 +303,7 @@ public function testLead($numbers, $expected) $this->assertSame($expected, $vector->lead()->value()); } - public function leads(): array + public static function leads(): array { return [ [ diff --git a/tests/Matrix/VectorTest.php b/tests/Matrix/VectorTest.php index 71943e7..35b5a45 100644 --- a/tests/Matrix/VectorTest.php +++ b/tests/Matrix/VectorTest.php @@ -7,11 +7,10 @@ use Innmind\Math\{ Matrix\Vector, Algebra\Number, - Algebra\Integer, - Algebra\Real, Exception\VectorsMustMeOfTheSameDimension }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; class VectorTest extends TestCase { @@ -19,7 +18,7 @@ public function testInterface() { $vector = Vector::of(...numerize(1, 2, 3)); - $this->assertSame(3, $vector->dimension()->value()); + $this->assertSame(3, $vector->dimension()); $this->assertInstanceOf(Number::class, $vector->get(0)); $this->assertInstanceOf(Number::class, $vector->get(1)); $this->assertInstanceOf(Number::class, $vector->get(2)); @@ -30,7 +29,7 @@ public function testInterface() [1, 2, 3], $vector ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); } @@ -58,7 +57,7 @@ public function testMultiplyBy() { $vector = Vector::of(...numerize(25, 5, 1)); $vector2 = $vector->multiplyBy( - Vector::initialize(Integer::of(3), Real::of(2.56)), + Vector::initialize(3, Number::of(2.56)), ); $this->assertInstanceOf(Vector::class, $vector2); @@ -74,8 +73,8 @@ public function testThrowWhenMultiplyingVectorsOfDifferentDimensions() { $this->expectException(VectorsMustMeOfTheSameDimension::class); - Vector::initialize(Integer::of(1), Real::of(1))->multiplyBy( - Vector::initialize(Integer::of(2), Real::of(1)), + Vector::initialize(1, Number::of(1))->multiplyBy( + Vector::initialize(2, Number::of(1)), ); } @@ -83,7 +82,7 @@ public function testDivideBy() { $vector = Vector::of(...numerize(25, 5, 1)); $vector2 = $vector->divideBy( - Vector::initialize(Integer::of(3), Real::of(5)), + Vector::initialize(3, Number::of(5)), ); $this->assertInstanceOf(Vector::class, $vector2); @@ -99,14 +98,14 @@ public function testThrowWhenDevidingVectorsOfDifferentDimensions() { $this->expectException(VectorsMustMeOfTheSameDimension::class); - Vector::initialize(Integer::of(1), Real::of(1))->divideBy( - Vector::initialize(Integer::of(2), Real::of(1)), + Vector::initialize(1, Number::of(1))->divideBy( + Vector::initialize(2, Number::of(1)), ); } public function testInitialize() { - $vector = Vector::initialize(Integer::of(4), Real::of(1.2)); + $vector = Vector::initialize(4, Number::of(1.2)); $this->assertInstanceOf(Vector::class, $vector); $this->assertSame(1.2, $vector->get(0)->value()); @@ -143,8 +142,8 @@ public function testThrowWhenSubtractingVectorsOfDifferentDimensions() { $this->expectException(VectorsMustMeOfTheSameDimension::class); - Vector::initialize(Integer::of(1), Real::of(1))->subtract( - Vector::initialize(Integer::of(2), Real::of(1)), + Vector::initialize(1, Number::of(1))->subtract( + Vector::initialize(2, Number::of(1)), ); } @@ -168,8 +167,8 @@ public function testThrowWhenAddingVectorsOfDifferentDimensions() { $this->expectException(VectorsMustMeOfTheSameDimension::class); - Vector::initialize(Integer::of(1), Real::of(1))->add( - Vector::initialize(Integer::of(2), Real::of(1)), + Vector::initialize(1, Number::of(1))->add( + Vector::initialize(2, Number::of(1)), ); } @@ -177,7 +176,7 @@ public function testPower() { $vector1 = Vector::of(...numerize(1, 2, 3, -4)); - $vector2 = $vector1->power(Real::of(3)); + $vector2 = $vector1->power(Number::of(3)); $this->assertInstanceOf(Vector::class, $vector2); $this->assertNotSame($vector2, $vector1); @@ -219,14 +218,14 @@ public function testMap() [1, 2, 3, -4], $vector ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); $this->assertSame( [1, 4, 9, 16], $vector2 ->toSequence() - ->map(static fn($number) => $number->collapse()->value()) + ->map(static fn($number) => $number->optimize()->value()) ->toList(), ); } @@ -257,9 +256,7 @@ public function testEquals() $this->assertFalse($vector->equals(Vector::of(...numerize(1, 2)))); } - /** - * @dataProvider leads - */ + #[DataProvider('leads')] public function testLead($numbers, $expected) { $vector = Vector::of(...numerize(...$numbers)); @@ -268,7 +265,7 @@ public function testLead($numbers, $expected) $this->assertSame($expected, $vector->lead()->value()); } - public function leads(): array + public static function leads(): array { return [ [ @@ -289,4 +286,13 @@ public function leads(): array ], ]; } + + private function assertEqualsWithDelta( + int|float $expected, + int|float $value, + int|float $delta, + ): void { + $this->assertGreaterThanOrEqual($expected-$delta, $value); + $this->assertLessThanOrEqual($expected+$delta, $value); + } } diff --git a/tests/MatrixTest.php b/tests/MatrixTest.php index 486590b..27213c8 100644 --- a/tests/MatrixTest.php +++ b/tests/MatrixTest.php @@ -9,15 +9,15 @@ Matrix\Dimension, Matrix\RowVector, Matrix\ColumnVector, - Algebra\Real, - Algebra\Integer, + Algebra\Number, Exception\VectorsMustMeOfTheSameDimension, Exception\MatricesMustBeOfTheSameDimension, Exception\MatrixMustBeSquare, Exception\MatrixNotInvertible }; use Innmind\Immutable\Sequence; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; class MatrixTest extends TestCase { @@ -47,7 +47,7 @@ public function testColumns() ]); $this->assertInstanceOf(Sequence::class, $matrix->columns()); - $this->assertCount(3, $matrix->columns()); + $this->assertSame(3, $matrix->columns()->size()); } public function testThrowWhenBuildingMatrixWithIncoherentRows() @@ -131,8 +131,8 @@ public function testDot() public function testInitialize() { $matrix = Matrix::initialize( - Dimension::of(Integer::of(3), Integer::of(2)), - Real::of(4.2), + Dimension::of(3, 2), + Number::of(4.2), ); $this->assertInstanceOf(Matrix::class, $matrix); @@ -290,7 +290,7 @@ public function testMultiplyBy() [1, 0, -1], [2, 3, 4], ]); - $result = $matrix->multiplyBy(Integer::of(3)); + $result = $matrix->multiplyBy(Number::of(3)); $this->assertInstanceOf(Matrix::class, $result); $this->assertNotSame($matrix, $result); @@ -407,9 +407,7 @@ public function testAugmentWith() ))); } - /** - * @dataProvider inverses - */ + #[DataProvider('inverses')] public function testInverse($initial, $expected) { $matrix = Matrix::of($initial); @@ -459,7 +457,7 @@ public function testThrowWhenMatrixNotInvertible() ])->inverse(); } - public function inverses(): array + public static function inverses(): array { return [ [ diff --git a/tests/Polynom/DegreeTest.php b/tests/Polynom/DegreeTest.php index c7ac886..507f54c 100644 --- a/tests/Polynom/DegreeTest.php +++ b/tests/Polynom/DegreeTest.php @@ -5,40 +5,39 @@ use Innmind\Math\{ Polynom\Degree, - Algebra\Real, - Algebra\Integer, + Algebra\Number, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class DegreeTest extends TestCase { public function testDegree() { - $d = Degree::of(Integer::of(8), Real::of(2)); + $d = Degree::of(8, Number::of(2)); - $this->assertSame(8, $d->degree()->value()); + $this->assertSame(8, $d->degree()); $this->assertSame(2, $d->coeff()->value()); - $this->assertSame(512, $d(Real::of(2))->value()); + $this->assertSame(512, $d(Number::of(2))->value()); } public function testStringCast() { - $d = Degree::of(Integer::of(8), Real::of(2)); + $d = Degree::of(8, Number::of(2)); $this->assertSame('2x^8', $d->toString()); - $d = Degree::of(Integer::of(1), Real::of(2)); + $d = Degree::of(1, Number::of(2)); $this->assertSame('2x', $d->toString()); - $d = Degree::of(Integer::of(8), Integer::of(1)->divideBy(Integer::of(4))); + $d = Degree::of(8, Number::of(1)->divideBy(Number::of(4))); $this->assertSame('(1 ÷ 4)x^8', $d->toString()); } public function testPrimitive() { - $d = Degree::of(Integer::of(8), Real::of(2)); + $d = Degree::of(8, Number::of(2)); $primitive = $d->primitive(); $this->assertInstanceOf(Degree::class, $primitive); @@ -49,7 +48,7 @@ public function testPrimitive() public function testDerivative() { - $degree = Degree::of(Integer::of(2), Integer::of(2)); + $degree = Degree::of(2, Number::of(2)); $derivative = $degree->derivative(); $this->assertInstanceOf(Degree::class, $derivative); diff --git a/tests/Polynom/IntegralTest.php b/tests/Polynom/IntegralTest.php index 9950d7b..1b14aad 100644 --- a/tests/Polynom/IntegralTest.php +++ b/tests/Polynom/IntegralTest.php @@ -6,16 +6,15 @@ use Innmind\Math\{ Polynom\Integral, Polynom\Polynom, - Algebra\Integer, - Algebra\Number + Algebra\Number, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class IntegralTest extends TestCase { public function testPolynom() { - $polynom = Polynom::interceptAt(Integer::of(42)); + $polynom = Polynom::interceptAt(Number::of(42)); $integral = Integral::of($polynom); $this->assertSame($polynom, $integral->polynom()); @@ -24,8 +23,8 @@ public function testPolynom() public function testStringCast() { $polynom = Polynom::zero() - ->withDegree(Integer::of(1), Integer::of(4)) - ->withDegree(Integer::of(2), Integer::of(-1)); + ->withDegree(1, Number::of(4)) + ->withDegree(2, Number::of(-1)); $integral = Integral::of($polynom); $this->assertSame( @@ -37,11 +36,11 @@ public function testStringCast() public function testInvokation() { $polynom = Polynom::zero() - ->withDegree(Integer::of(1), Integer::of(4)) - ->withDegree(Integer::of(2), Integer::of(-1)); + ->withDegree(1, Number::of(4)) + ->withDegree(2, Number::of(-1)); $integral = Integral::of($polynom); - $area = $integral(Integer::of(0), Integer::of(4)); + $area = $integral(Number::of(0), Number::of(4)); $this->assertInstanceOf(Number::class, $area); // 32/3 diff --git a/tests/Polynom/PolynomTest.php b/tests/Polynom/PolynomTest.php index 230f7f7..fa0d20f 100644 --- a/tests/Polynom/PolynomTest.php +++ b/tests/Polynom/PolynomTest.php @@ -8,9 +8,8 @@ Polynom\Tangent, Polynom\Integral, Algebra\Number, - Algebra\Integer }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class PolynomTest extends TestCase { @@ -20,13 +19,13 @@ public function testImmutability() $this->assertNotSame( $p, - $p->withDegree(Integer::of(1), Integer::of(1)), + $p->withDegree(1, Number::of(1)), ); } public function testIntercept() { - $p = Polynom::interceptAt($intercept = Integer::of(42)); + $p = Polynom::interceptAt($intercept = Number::of(42)); $this->assertSame($intercept, $p->intercept()); $this->assertSame(42, $p->intercept()->value()); @@ -34,82 +33,82 @@ public function testIntercept() public function testCompute() { - $p = Polynom::interceptAt(Integer::of(42)) - ->withDegree(Integer::of(1), Integer::of(2)) - ->withDegree(Integer::of(2), Integer::of(1)); + $p = Polynom::interceptAt(Number::of(42)) + ->withDegree(1, Number::of(2)) + ->withDegree(2, Number::of(1)); - $this->assertInstanceOf(Number::class, $p(Integer::of(2))); - $this->assertSame(50, $p(Integer::of(2))->value()); + $this->assertInstanceOf(Number::class, $p(Number::of(2))); + $this->assertSame(50, $p(Number::of(2))->value()); } public function testOnlyOneDegreePresentInAPolynom() { - $p = Polynom::interceptAt(Integer::of(42)) - ->withDegree(Integer::of(2), Integer::of(2)) - ->withDegree(Integer::of(2), Integer::of(3)); + $p = Polynom::interceptAt(Number::of(42)) + ->withDegree(2, Number::of(2)) + ->withDegree(2, Number::of(3)); - $this->assertSame(54, $p(Integer::of(2))->value()); + $this->assertSame(54, $p(Number::of(2))->value()); } public function testDerived() { $polynom = Polynom::zero() - ->withDegree(Integer::of(2), Integer::of(1)); + ->withDegree(2, Number::of(1)); $this->assertInstanceOf( Number::class, - $polynom->derived(Integer::of(2)), + $polynom->derived(Number::of(2)), ); $this->assertSame( 4.000, - $polynom->derived(Integer::of(2))->roundUp(3)->value(), + $polynom->derived(Number::of(2))->roundUp(3)->value(), ); } public function testTangent() { $polynom = Polynom::zero() - ->withDegree(Integer::of(2), Integer::of(1)); + ->withDegree(2, Number::of(1)); $this->assertInstanceOf( Tangent::class, - $polynom->tangent(Integer::of(2)), + $polynom->tangent(Number::of(2)), ); $this->assertSame( $polynom, $polynom - ->tangent(Integer::of(2)) + ->tangent(Number::of(2)) ->polynom(), ); $this->assertSame( 4.0, - $polynom->tangent(Integer::of(2))(Integer::of(2))->value(), + $polynom->tangent(Number::of(2))(Number::of(2))->value(), ); } public function testStringCast() { - $polynom = Polynom::interceptAt(Integer::of(42)) - ->withDegree(Integer::of(2), Integer::of(2)) - ->withDegree(Integer::of(1), Integer::of(3)) - ->withDegree(Integer::of(3), Integer::of(1)); + $polynom = Polynom::interceptAt(Number::of(42)) + ->withDegree(2, Number::of(2)) + ->withDegree(1, Number::of(3)) + ->withDegree(3, Number::of(1)); $this->assertSame('1x^3 + 2x^2 + 3x + 42', $polynom->toString()); $polynom = Polynom::zero() - ->withDegree(Integer::of(2), Integer::of(2)) - ->withDegree(Integer::of(1), Integer::of(3)) - ->withDegree(Integer::of(3), Integer::of(1)); + ->withDegree(2, Number::of(2)) + ->withDegree(1, Number::of(3)) + ->withDegree(3, Number::of(1)); $this->assertSame('1x^3 + 2x^2 + 3x', $polynom->toString()); } public function testPrimitive() { - $polynom = Polynom::interceptAt(Integer::of(42)) - ->withDegree(Integer::of(2), Integer::of(2)) - ->withDegree(Integer::of(1), Integer::of(3)) - ->withDegree(Integer::of(3), Integer::of(1)); + $polynom = Polynom::interceptAt(Number::of(42)) + ->withDegree(2, Number::of(2)) + ->withDegree(1, Number::of(3)) + ->withDegree(3, Number::of(1)); $primitive = $polynom->primitive(); $this->assertInstanceOf(Polynom::class, $primitive); @@ -126,7 +125,7 @@ public function testRemovePrimitiveFirstDegreeWhenNulCoefficient() $this->assertSame( '(2 ÷ (2 + 1))x^3', Polynom::zero() - ->withDegree(Integer::of(2), Integer::of(2)) + ->withDegree(2, Number::of(2)) ->primitive() ->toString(), ); @@ -135,9 +134,9 @@ public function testRemovePrimitiveFirstDegreeWhenNulCoefficient() public function testDerivative() { $polynom = Polynom::zero() - ->withDegree(Integer::of(1), Integer::of(4)) - ->withDegree(Integer::of(2), Integer::of(2)) - ->withDegree(Integer::of(3), Integer::of(1)); + ->withDegree(1, Number::of(4)) + ->withDegree(2, Number::of(2)) + ->withDegree(3, Number::of(1)); $derivative = $polynom->derivative(); $this->assertInstanceOf(Polynom::class, $derivative); @@ -152,7 +151,7 @@ public function testDerivative() public function testIntegral() { $integral = Polynom::zero() - ->withDegree(Integer::of(2), Integer::of(3)) + ->withDegree(2, Number::of(3)) ->integral(); $this->assertInstanceOf(Integral::class, $integral); diff --git a/tests/Polynom/TangentTest.php b/tests/Polynom/TangentTest.php index e0d225a..96b11e9 100644 --- a/tests/Polynom/TangentTest.php +++ b/tests/Polynom/TangentTest.php @@ -6,10 +6,9 @@ use Innmind\Math\{ Polynom\Tangent, Polynom\Polynom, - Algebra\Integer, - Algebra\Number + Algebra\Number, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class TangentTest extends TestCase { @@ -17,21 +16,21 @@ public function testInterface() { //f -> x^2 $polynom = Polynom::zero()->withDegree( - Integer::of(1), - Integer::of(2), + 1, + Number::of(2), ); //t -> f'(2)(x - 2) + f(2) $tangent = Tangent::of( $polynom, - $abscissa = Integer::of(2), + $abscissa = Number::of(2), ); $this->assertSame($polynom, $tangent->polynom()); $this->assertSame($abscissa, $tangent->abscissa()); - $this->assertInstanceOf(Number::class, $tangent(Integer::of(0))); + $this->assertInstanceOf(Number::class, $tangent(Number::of(0))); $this->assertSame( 4.0, - $tangent(Integer::of(2))->value(), + $tangent(Number::of(2))->value(), ); } } diff --git a/tests/Probabilities/BinomialDistributionTest.php b/tests/Probabilities/BinomialDistributionTest.php index 86f7112..383d2dd 100644 --- a/tests/Probabilities/BinomialDistributionTest.php +++ b/tests/Probabilities/BinomialDistributionTest.php @@ -6,18 +6,16 @@ use Innmind\Math\{ Probabilities\BinomialDistribution, Algebra\Number, - Algebra\Integer, - Algebra\Real, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class BinomialDistributionTest extends TestCase { public function testInvokation() { - $law = BinomialDistribution::of(Real::of(0.5)); + $law = BinomialDistribution::of(Number::of(0.5)); - $probability = $law(Integer::of(9), Integer::of(2)); + $probability = $law(9, 2); $this->assertInstanceOf(Number::class, $probability); $this->assertSame(0.0703125, $probability->value()); diff --git a/tests/Probabilities/ExpectationTest.php b/tests/Probabilities/ExpectationTest.php index c786d4c..29fb02f 100644 --- a/tests/Probabilities/ExpectationTest.php +++ b/tests/Probabilities/ExpectationTest.php @@ -8,7 +8,7 @@ Regression\Dataset, Algebra\Number }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class ExpectationTest extends TestCase { @@ -29,4 +29,13 @@ public function testInvokation() 0.00001, ); } + + private function assertEqualsWithDelta( + int|float $expected, + int|float $value, + int|float $delta, + ): void { + $this->assertGreaterThanOrEqual($expected-$delta, $value); + $this->assertLessThanOrEqual($expected+$delta, $value); + } } diff --git a/tests/Probabilities/StandardDeviationTest.php b/tests/Probabilities/StandardDeviationTest.php index 39dce80..6026277 100644 --- a/tests/Probabilities/StandardDeviationTest.php +++ b/tests/Probabilities/StandardDeviationTest.php @@ -8,7 +8,7 @@ Regression\Dataset, Algebra\Number }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class StandardDeviationTest extends TestCase { diff --git a/tests/Probabilities/VarianceTest.php b/tests/Probabilities/VarianceTest.php index 4898c6b..eb0643e 100644 --- a/tests/Probabilities/VarianceTest.php +++ b/tests/Probabilities/VarianceTest.php @@ -8,7 +8,7 @@ Regression\Dataset, Algebra\Number }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class VarianceTest extends TestCase { diff --git a/tests/Quantile/QuantileTest.php b/tests/Quantile/QuantileTest.php index 226d81d..96faa20 100644 --- a/tests/Quantile/QuantileTest.php +++ b/tests/Quantile/QuantileTest.php @@ -7,19 +7,17 @@ Quantile\Quantile, Quantile\Quartile, Algebra\Number, - Algebra\Real, }; use Innmind\Immutable\Sequence; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; class QuantileTest extends TestCase { - /** - * @dataProvider datasets - */ + #[DataProvider('datasets')] public function testQuartiles($dataset, $min, $max, $mean, $median, $first, $third) { - $quantile = Quantile::of(Sequence::of(...$dataset)->map(Real::of(...))); + $quantile = Quantile::of(Sequence::of(...$dataset)->map(Number::of(...))); $this->assertInstanceOf(Quartile::class, $quantile->min()); $this->assertInstanceOf(Number::class, $quantile->min()->value()); @@ -58,7 +56,7 @@ public function testQuartiles($dataset, $min, $max, $mean, $median, $first, $thi ); } - public function datasets() + public static function datasets() { return [ [ diff --git a/tests/Regression/DatasetTest.php b/tests/Regression/DatasetTest.php index b2f1aca..445520f 100644 --- a/tests/Regression/DatasetTest.php +++ b/tests/Regression/DatasetTest.php @@ -9,7 +9,7 @@ Matrix\ColumnVector, Matrix\Dimension, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class DatasetTest extends TestCase { diff --git a/tests/Regression/LinearRegressionTest.php b/tests/Regression/LinearRegressionTest.php index 7a6d2be..99c6d31 100644 --- a/tests/Regression/LinearRegressionTest.php +++ b/tests/Regression/LinearRegressionTest.php @@ -7,9 +7,8 @@ Regression\LinearRegression, Regression\Dataset, Algebra\Number, - Algebra\Integer, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class LinearRegressionTest extends TestCase { @@ -46,7 +45,7 @@ public function testComplexCase() $this->assertSame(-0.15, $r->slope()->value()); $this->assertSame(1.35, $r->intercept()->value()); - $this->assertEqualsWithDelta(0.0, $r(Integer::of(9))->value(), 0.0001); + $this->assertEqualsWithDelta(0.0, $r(Number::of(9))->value(), 0.0001); } public function testRootMeanSquareDeviation() @@ -67,4 +66,13 @@ public function testRootMeanSquareDeviation() $regression->rootMeanSquareDeviation()->value(), ); } + + private function assertEqualsWithDelta( + int|float $expected, + int|float $value, + int|float $delta, + ): void { + $this->assertGreaterThanOrEqual($expected-$delta, $value); + $this->assertLessThanOrEqual($expected+$delta, $value); + } } diff --git a/tests/Regression/PolynomialRegressionTest.php b/tests/Regression/PolynomialRegressionTest.php index c4dca6d..65b8bfe 100644 --- a/tests/Regression/PolynomialRegressionTest.php +++ b/tests/Regression/PolynomialRegressionTest.php @@ -6,11 +6,10 @@ use Innmind\Math\{ Regression\PolynomialRegression, Regression\Dataset, - Algebra\Integer, Algebra\Number, Polynom\Polynom }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class PolynomialRegressionTest extends TestCase { @@ -25,7 +24,7 @@ public function testSquareRegression() [8, 64], ]); - $regression = PolynomialRegression::of($dataset, Integer::of(2)); + $regression = PolynomialRegression::of($dataset, 2); $polynom = $regression->polynom(); $this->assertInstanceOf(Polynom::class, $polynom); @@ -42,7 +41,7 @@ public function testSquareRegression() static fn() => true, static fn() => false, )); - $this->assertSame(81.0, $polynom(Integer::of(9))->value()); + $this->assertSame(81.0, $polynom(Number::of(9))->value()); $this->assertInstanceOf( Number::class, $regression->rootMeanSquareDeviation(), @@ -65,7 +64,7 @@ public function testCubicRegression() [8, 512], ]); - $regression = PolynomialRegression::of($dataset, Integer::of(3)); + $regression = PolynomialRegression::of($dataset, 3); $polynom = $regression->polynom(); $this->assertInstanceOf(Polynom::class, $polynom); @@ -94,7 +93,7 @@ public function testCubicRegression() ), 0.0001, ); - $this->assertEqualsWithDelta(729.0, $polynom(Integer::of(9))->value(), 0.0001); + $this->assertEqualsWithDelta(729.0, $polynom(Number::of(9))->value(), 0.0001); } public function testRegression() @@ -114,7 +113,7 @@ public function testRegression() [11, 0], ]); - $regression = PolynomialRegression::of($dataset, Integer::of(4)); + $regression = PolynomialRegression::of($dataset, 4); $polynom = $regression->polynom(); $this->assertInstanceOf(Polynom::class, $polynom); @@ -145,4 +144,13 @@ public function testRegression() $regression->rootMeanSquareDeviation()->value(), ); } + + private function assertEqualsWithDelta( + int|float $expected, + int|float $value, + int|float $delta, + ): void { + $this->assertGreaterThanOrEqual($expected-$delta, $value); + $this->assertLessThanOrEqual($expected+$delta, $value); + } } diff --git a/tests/Statistics/FrequenceTest.php b/tests/Statistics/FrequenceTest.php index 66a087e..28cc112 100644 --- a/tests/Statistics/FrequenceTest.php +++ b/tests/Statistics/FrequenceTest.php @@ -6,28 +6,23 @@ use Innmind\Math\{ Statistics\Frequence, Algebra\Number, - Algebra\Integer, - Algebra\Division }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class FrequenceTest extends TestCase { public function testInvokation() { $frequence = Frequence::of( - Integer::of(1), - Integer::of(2), - Integer::of(2), - Integer::of(1), - Integer::of(3), + Number::of(1), + Number::of(2), + Number::of(2), + Number::of(1), + Number::of(3), ); - $this->assertInstanceOf(Division::class, $frequence(Integer::of(1))); - $this->assertInstanceOf(Division::class, $frequence(Integer::of(4))); - $this->assertSame('2 ÷ 5', $frequence(Integer::of(1))->toString()); - $this->assertSame('0 ÷ 5', $frequence(Integer::of(4))->toString()); - $this->assertInstanceOf(Number::class, $frequence->size()); - $this->assertSame(5, $frequence->size()->value()); + $this->assertSame('2 ÷ 5', $frequence(Number::of(1))->toString()); + $this->assertSame('0 ÷ 5', $frequence(Number::of(4))->toString()); + $this->assertSame(5, $frequence->size()); } } diff --git a/tests/Statistics/MeanTest.php b/tests/Statistics/MeanTest.php index 0b92848..3ec7656 100644 --- a/tests/Statistics/MeanTest.php +++ b/tests/Statistics/MeanTest.php @@ -6,40 +6,25 @@ use Innmind\Math\{ Statistics\Mean, Algebra\Number, - Algebra\Addition, - Algebra\Subtraction, - Algebra\Multiplication, - Algebra\Division, - Algebra\Floor, - Algebra\Ceil, - Algebra\Modulo, - Algebra\Absolute, - Algebra\Power, - Algebra\SquareRoot, - Algebra\Exponential, - Algebra\BinaryLogarithm, - Algebra\NaturalLogarithm, - Algebra\CommonLogarithm, - Algebra\Signum, - Algebra\Real, + Algebra\Logarithm, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class MeanTest extends TestCase { public function testResult() { $mean = Mean::of( - Real::of(1), - Real::of(2), - Real::of(2), - Real::of(2), - Real::of(3), - Real::of(5), - Real::of(5), - Real::of(6), - Real::of(6), - Real::of(7), + Number::of(1), + Number::of(2), + Number::of(2), + Number::of(2), + Number::of(3), + Number::of(5), + Number::of(5), + Number::of(6), + Number::of(6), + Number::of(7), ); $this->assertInstanceOf(Number::class, $mean->result()); @@ -50,80 +35,76 @@ public function testResult() public function testEquals() { $mean = Mean::of( - Real::of(1), - Real::of(7), - ); + Number::of(1), + Number::of(7), + )->result(); - $this->assertTrue($mean->equals(Real::of(4))); - $this->assertTrue($mean->equals(Real::of(4.0))); - $this->assertFalse($mean->equals(Real::of(4.1))); + $this->assertTrue($mean->equals(Number::of(4))); + $this->assertTrue($mean->equals(Number::of(4.0))); + $this->assertFalse($mean->equals(Number::of(4.1))); } public function testHigherThan() { $mean = Mean::of( - Real::of(1), - Real::of(7), - ); + Number::of(1), + Number::of(7), + )->result(); - $this->assertTrue($mean->higherThan(Real::of(3.9))); - $this->assertFalse($mean->higherThan(Real::of(4))); + $this->assertTrue($mean->higherThan(Number::of(3.9))); + $this->assertFalse($mean->higherThan(Number::of(4))); } public function testAdd() { $mean = Mean::of( - Real::of(1), - Real::of(7), - ); - $number = $mean->add(Real::of(66)); + Number::of(1), + Number::of(7), + )->result(); + $number = $mean->add(Number::of(66)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(70, $number->value()); } public function testSubtract() { $mean = Mean::of( - Real::of(1), - Real::of(7), - ); - $number = $mean->subtract(Real::of(66)); + Number::of(1), + Number::of(7), + )->result(); + $number = $mean->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-62, $number->value()); } public function testDivideBy() { $mean = Mean::of( - Real::of(1), - Real::of(7), - ); - $number = $mean->divideBy(Real::of(2)); + Number::of(1), + Number::of(7), + )->result(); + $number = $mean->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(2, $number->value()); } public function testMulitplyBy() { $mean = Mean::of( - Real::of(1), - Real::of(7), - ); - $number = $mean->multiplyBy(Real::of(2)); + Number::of(1), + Number::of(7), + )->result(); + $number = $mean->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(8, $number->value()); } public function testRound() { $number = Mean::of( - Real::of(1), - Real::of(7.12), - ); + Number::of(1), + Number::of(7.12), + )->result(); $this->assertEquals(4.1, $number->roundUp(1)->value()); $this->assertEquals(4.1, $number->roundDown(1)->value()); @@ -134,137 +115,134 @@ public function testRound() public function testFloor() { $mean = Mean::of( - Real::of(1), - Real::of(7.1), - ); + Number::of(1), + Number::of(7.1), + )->result(); $number = $mean->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(4.0, $number->value()); } public function testCeil() { $mean = Mean::of( - Real::of(1), - Real::of(7.1), - ); + Number::of(1), + Number::of(7.1), + )->result(); $number = $mean->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(5.0, $number->value()); } public function testModulo() { $mean = Mean::of( - Real::of(1), - Real::of(7), - ); - $number = $mean->modulo(Real::of(3)); + Number::of(1), + Number::of(7), + )->result(); + $number = $mean->modulo(Number::of(3)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(1.0, $number->value()); } public function testAbsolute() { $mean = Mean::of( - Real::of(-1), - Real::of(-7), - ); + Number::of(-1), + Number::of(-7), + )->result(); $number = $mean->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(4, $number->value()); } public function testPower() { $mean = Mean::of( - Real::of(1), - Real::of(7), - ); - $number = $mean->power(Real::of(2)); + Number::of(1), + Number::of(7), + )->result(); + $number = $mean->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(16, $number->value()); } public function testSquareRoot() { $mean = Mean::of( - Real::of(1), - Real::of(7), - ); + Number::of(1), + Number::of(7), + )->result(); $number = $mean->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(2.0, $number->value()); } public function testExponential() { $mean = Mean::of( - Real::of(1), - Real::of(7), - ); + Number::of(1), + Number::of(7), + )->result(); $number = $mean->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\exp(4), $number->value()); } public function testBinaryLogarithm() { $number = Mean::of( - Real::of(1), - Real::of(7), - )->binaryLogarithm(); + Number::of(1), + Number::of(7), + ) + ->result() + ->apply(Logarithm::binary); - $this->assertInstanceOf(BinaryLogarithm::class, $number); $this->assertSame(\log(4, 2), $number->value()); } public function testNaturalLogarithm() { $number = Mean::of( - Real::of(1), - Real::of(7), - )->naturalLogarithm(); + Number::of(1), + Number::of(7), + ) + ->result() + ->apply(Logarithm::natural); - $this->assertInstanceOf(NaturalLogarithm::class, $number); $this->assertSame(\log(4), $number->value()); } public function testCommonLogarithm() { $number = Mean::of( - Real::of(1), - Real::of(7), - )->commonLogarithm(); + Number::of(1), + Number::of(7), + ) + ->result() + ->apply(Logarithm::common); - $this->assertInstanceOf(CommonLogarithm::class, $number); $this->assertSame(\log10(4), $number->value()); } public function testSignum() { $number = Mean::of( - Real::of(1), - Real::of(7), - )->signum(); + Number::of(1), + Number::of(7), + ) + ->result() + ->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } public function testStringCast() { $mean = Mean::of( - Real::of(1), - Real::of(7.1), - ); + Number::of(1), + Number::of(7.1), + )->result(); $this->assertSame('(1 + 7.1) ÷ 2', $mean->toString()); } diff --git a/tests/Statistics/MedianTest.php b/tests/Statistics/MedianTest.php index bcc60d1..ecdd90c 100644 --- a/tests/Statistics/MedianTest.php +++ b/tests/Statistics/MedianTest.php @@ -6,40 +6,25 @@ use Innmind\Math\{ Statistics\Median, Algebra\Number, - Algebra\Addition, - Algebra\Subtraction, - Algebra\Multiplication, - Algebra\Division, - Algebra\Floor, - Algebra\Ceil, - Algebra\Modulo, - Algebra\Absolute, - Algebra\Power, - Algebra\SquareRoot, - Algebra\Exponential, - Algebra\BinaryLogarithm, - Algebra\NaturalLogarithm, - Algebra\CommonLogarithm, - Algebra\Signum, - Algebra\Real, + Algebra\Logarithm, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class MedianTest extends TestCase { public function testEvenSetResult() { $median = Median::of( - Real::of(1), - Real::of(2), - Real::of(2), - Real::of(2), - Real::of(3), - Real::of(5), - Real::of(5), - Real::of(6), - Real::of(6), - Real::of(7), + Number::of(1), + Number::of(2), + Number::of(2), + Number::of(2), + Number::of(3), + Number::of(5), + Number::of(5), + Number::of(6), + Number::of(6), + Number::of(7), ); $this->assertInstanceOf(Number::class, $median->result()); @@ -50,17 +35,17 @@ public function testEvenSetResult() public function testOddSetResult() { $median = Median::of( - Real::of(1), - Real::of(2), - Real::of(2), - Real::of(2), - Real::of(3), - $expected = Real::of(5), - Real::of(5), - Real::of(6), - Real::of(6), - Real::of(7), - Real::of(8), + Number::of(1), + Number::of(2), + Number::of(2), + Number::of(2), + Number::of(3), + $expected = Number::of(5), + Number::of(5), + Number::of(6), + Number::of(6), + Number::of(7), + Number::of(8), ); $this->assertInstanceOf(Number::class, $median->result()); @@ -72,80 +57,76 @@ public function testOddSetResult() public function testEquals() { $median = Median::of( - Real::of(1), - Real::of(7), - ); + Number::of(1), + Number::of(7), + )->result(); - $this->assertTrue($median->equals(Real::of(4))); - $this->assertTrue($median->equals(Real::of(4.0))); - $this->assertFalse($median->equals(Real::of(4.1))); + $this->assertTrue($median->equals(Number::of(4))); + $this->assertTrue($median->equals(Number::of(4.0))); + $this->assertFalse($median->equals(Number::of(4.1))); } public function testHigherThan() { $median = Median::of( - Real::of(1), - Real::of(7), - ); + Number::of(1), + Number::of(7), + )->result(); - $this->assertTrue($median->higherThan(Real::of(3.9))); - $this->assertFalse($median->higherThan(Real::of(4))); + $this->assertTrue($median->higherThan(Number::of(3.9))); + $this->assertFalse($median->higherThan(Number::of(4))); } public function testAdd() { $median = Median::of( - Real::of(1), - Real::of(7), - ); - $number = $median->add(Real::of(66)); + Number::of(1), + Number::of(7), + )->result(); + $number = $median->add(Number::of(66)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(70, $number->value()); } public function testSubtract() { $median = Median::of( - Real::of(1), - Real::of(7), - ); - $number = $median->subtract(Real::of(66)); + Number::of(1), + Number::of(7), + )->result(); + $number = $median->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-62, $number->value()); } public function testDivideBy() { $median = Median::of( - Real::of(1), - Real::of(7), - ); - $number = $median->divideBy(Real::of(2)); + Number::of(1), + Number::of(7), + )->result(); + $number = $median->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(2, $number->value()); } public function testMulitplyBy() { $median = Median::of( - Real::of(1), - Real::of(7), - ); - $number = $median->multiplyBy(Real::of(2)); + Number::of(1), + Number::of(7), + )->result(); + $number = $median->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(8, $number->value()); } public function testRound() { $number = Median::of( - Real::of(1), - Real::of(7.12), - ); + Number::of(1), + Number::of(7.12), + )->result(); $this->assertEquals(4.1, $number->roundUp(1)->value()); $this->assertEquals(4.1, $number->roundDown(1)->value()); @@ -156,137 +137,134 @@ public function testRound() public function testFloor() { $median = Median::of( - Real::of(1), - Real::of(7.1), - ); + Number::of(1), + Number::of(7.1), + )->result(); $number = $median->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(4.0, $number->value()); } public function testCeil() { $median = Median::of( - Real::of(1), - Real::of(7.1), - ); + Number::of(1), + Number::of(7.1), + )->result(); $number = $median->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(5.0, $number->value()); } public function testModulo() { $median = Median::of( - Real::of(1), - Real::of(7), - ); - $number = $median->modulo(Real::of(3)); + Number::of(1), + Number::of(7), + )->result(); + $number = $median->modulo(Number::of(3)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(1.0, $number->value()); } public function testAbsolute() { $median = Median::of( - Real::of(-1), - Real::of(-7), - ); + Number::of(-1), + Number::of(-7), + )->result(); $number = $median->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(4, $number->value()); } public function testPower() { $median = Median::of( - Real::of(1), - Real::of(7), - ); - $number = $median->power(Real::of(2)); + Number::of(1), + Number::of(7), + )->result(); + $number = $median->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(16, $number->value()); } public function testSquareRoot() { $median = Median::of( - Real::of(1), - Real::of(7), - ); + Number::of(1), + Number::of(7), + )->result(); $number = $median->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(2.0, $number->value()); } public function testExponential() { $mean = Median::of( - Real::of(1), - Real::of(7), - ); + Number::of(1), + Number::of(7), + )->result(); $number = $mean->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\exp(4), $number->value()); } public function testBinaryLogarithm() { $number = Median::of( - Real::of(1), - Real::of(7), - )->binaryLogarithm(); + Number::of(1), + Number::of(7), + ) + ->result() + ->apply(Logarithm::binary); - $this->assertInstanceOf(BinaryLogarithm::class, $number); $this->assertSame(\log(4, 2), $number->value()); } public function testNaturalLogarithm() { $number = Median::of( - Real::of(1), - Real::of(7), - )->naturalLogarithm(); + Number::of(1), + Number::of(7), + ) + ->result() + ->apply(Logarithm::natural); - $this->assertInstanceOf(NaturalLogarithm::class, $number); $this->assertSame(\log(4), $number->value()); } public function testCommonLogarithm() { $number = Median::of( - Real::of(1), - Real::of(7), - )->commonLogarithm(); + Number::of(1), + Number::of(7), + ) + ->result() + ->apply(Logarithm::common); - $this->assertInstanceOf(CommonLogarithm::class, $number); $this->assertSame(\log10(4), $number->value()); } public function testSignum() { $number = Median::of( - Real::of(1), - Real::of(7), - )->signum(); + Number::of(1), + Number::of(7), + ) + ->result() + ->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } public function testStringCast() { $median = Median::of( - Real::of(1), - Real::of(7.1), - ); + Number::of(1), + Number::of(7.1), + )->result(); $this->assertSame('(1 + 7.1) ÷ 2', $median->toString()); } diff --git a/tests/Statistics/ScopeTest.php b/tests/Statistics/ScopeTest.php index c8a1021..66390f0 100644 --- a/tests/Statistics/ScopeTest.php +++ b/tests/Statistics/ScopeTest.php @@ -6,125 +6,105 @@ use Innmind\Math\{ Statistics\Scope, Algebra\Number, - Algebra\Addition, - Algebra\Subtraction, - Algebra\Multiplication, - Algebra\Division, - Algebra\Floor, - Algebra\Ceil, - Algebra\Modulo, - Algebra\Absolute, - Algebra\Power, - Algebra\SquareRoot, - Algebra\Exponential, - Algebra\BinaryLogarithm, - Algebra\NaturalLogarithm, - Algebra\CommonLogarithm, - Algebra\Signum, - Algebra\Real, + Algebra\Logarithm, }; -use PHPUnit\Framework\TestCase; +use Innmind\BlackBox\PHPUnit\Framework\TestCase; class ScopeTest extends TestCase { public function testResult() { $scope = Scope::of( - Real::of(1), - Real::of(2), - Real::of(2), - Real::of(2), - Real::of(3), - Real::of(5), - Real::of(5), - Real::of(6), - Real::of(6), - Real::of(7), + Number::of(1), + Number::of(2), + Number::of(2), + Number::of(2), + Number::of(3), + Number::of(5), + Number::of(5), + Number::of(6), + Number::of(6), + Number::of(7), ); $this->assertInstanceOf(Number::class, $scope->result()); $this->assertSame($scope->result(), $scope->result()); $this->assertSame(6, $scope->result()->value()); - $this->assertSame(6, $scope->value()); } public function testEquals() { $scope = Scope::of( - Real::of(1), - Real::of(7), - ); + Number::of(1), + Number::of(7), + )->result(); - $this->assertTrue($scope->equals(Real::of(6))); - $this->assertTrue($scope->equals(Real::of(6.0))); - $this->assertFalse($scope->equals(Real::of(6.1))); + $this->assertTrue($scope->equals(Number::of(6))); + $this->assertTrue($scope->equals(Number::of(6.0))); + $this->assertFalse($scope->equals(Number::of(6.1))); } public function testHigherThan() { $scope = Scope::of( - Real::of(1), - Real::of(7), - ); + Number::of(1), + Number::of(7), + )->result(); - $this->assertTrue($scope->higherThan(Real::of(5.9))); - $this->assertFalse($scope->higherThan(Real::of(6))); + $this->assertTrue($scope->higherThan(Number::of(5.9))); + $this->assertFalse($scope->higherThan(Number::of(6))); } public function testAdd() { $scope = Scope::of( - Real::of(1), - Real::of(7), - ); - $number = $scope->add(Real::of(66)); + Number::of(1), + Number::of(7), + )->result(); + $number = $scope->add(Number::of(66)); - $this->assertInstanceOf(Addition::class, $number); $this->assertSame(72, $number->value()); } public function testSubtract() { $scope = Scope::of( - Real::of(1), - Real::of(7), - ); - $number = $scope->subtract(Real::of(66)); + Number::of(1), + Number::of(7), + )->result(); + $number = $scope->subtract(Number::of(66)); - $this->assertInstanceOf(Subtraction::class, $number); $this->assertSame(-60, $number->value()); } public function testDivideBy() { $scope = Scope::of( - Real::of(1), - Real::of(7), - ); - $number = $scope->divideBy(Real::of(2)); + Number::of(1), + Number::of(7), + )->result(); + $number = $scope->divideBy(Number::of(2)); - $this->assertInstanceOf(Division::class, $number); $this->assertSame(3, $number->value()); } public function testMulitplyBy() { $scope = Scope::of( - Real::of(1), - Real::of(7), - ); - $number = $scope->multiplyBy(Real::of(2)); + Number::of(1), + Number::of(7), + )->result(); + $number = $scope->multiplyBy(Number::of(2)); - $this->assertInstanceOf(Multiplication::class, $number); $this->assertSame(12, $number->value()); } public function testRound() { $number = Scope::of( - Real::of(1), - Real::of(7.12), - ); + Number::of(1), + Number::of(7.12), + )->result(); $this->assertEquals(6.1, $number->roundUp(1)->value()); $this->assertEquals(6.1, $number->roundDown(1)->value()); @@ -135,137 +115,134 @@ public function testRound() public function testFloor() { $scope = Scope::of( - Real::of(1), - Real::of(7.1), - ); + Number::of(1), + Number::of(7.1), + )->result(); $number = $scope->floor(); - $this->assertInstanceOf(Floor::class, $number); $this->assertSame(6.0, $number->value()); } public function testCeil() { $scope = Scope::of( - Real::of(1), - Real::of(7.1), - ); + Number::of(1), + Number::of(7.1), + )->result(); $number = $scope->ceil(); - $this->assertInstanceOf(Ceil::class, $number); $this->assertSame(7.0, $number->value()); } public function testModulo() { $scope = Scope::of( - Real::of(1), - Real::of(7), - ); - $number = $scope->modulo(Real::of(5)); + Number::of(1), + Number::of(7), + )->result(); + $number = $scope->modulo(Number::of(5)); - $this->assertInstanceOf(Modulo::class, $number); $this->assertSame(1.0, $number->value()); } public function testAbsolute() { $scope = Scope::of( - Real::of(-1), - Real::of(-7), - ); + Number::of(-1), + Number::of(-7), + )->result(); $number = $scope->absolute(); - $this->assertInstanceOf(Absolute::class, $number); $this->assertSame(6, $number->value()); } public function testPower() { $scope = Scope::of( - Real::of(1), - Real::of(7), - ); - $number = $scope->power(Real::of(2)); + Number::of(1), + Number::of(7), + )->result(); + $number = $scope->power(Number::of(2)); - $this->assertInstanceOf(Power::class, $number); $this->assertSame(36, $number->value()); } public function testSquareRoot() { $scope = Scope::of( - Real::of(1), - Real::of(5), - ); + Number::of(1), + Number::of(5), + )->result(); $number = $scope->squareRoot(); - $this->assertInstanceOf(SquareRoot::class, $number); $this->assertSame(2.0, $number->value()); } public function testExponential() { $mean = Scope::of( - Real::of(1), - Real::of(5), - ); + Number::of(1), + Number::of(5), + )->result(); $number = $mean->exponential(); - $this->assertInstanceOf(Exponential::class, $number); $this->assertSame(\exp(4), $number->value()); } public function testBinaryLogarithm() { $number = Scope::of( - Real::of(1), - Real::of(5), - )->binaryLogarithm(); + Number::of(1), + Number::of(5), + ) + ->result() + ->apply(Logarithm::binary); - $this->assertInstanceOf(BinaryLogarithm::class, $number); $this->assertSame(\log(4, 2), $number->value()); } public function testNaturalLogarithm() { $number = Scope::of( - Real::of(1), - Real::of(5), - )->naturalLogarithm(); + Number::of(1), + Number::of(5), + ) + ->result() + ->apply(Logarithm::natural); - $this->assertInstanceOf(NaturalLogarithm::class, $number); $this->assertSame(\log(4), $number->value()); } public function testCommonLogarithm() { $number = Scope::of( - Real::of(1), - Real::of(5), - )->commonLogarithm(); + Number::of(1), + Number::of(5), + ) + ->result() + ->apply(Logarithm::common); - $this->assertInstanceOf(CommonLogarithm::class, $number); $this->assertSame(\log10(4), $number->value()); } public function testSignum() { $number = Scope::of( - Real::of(1), - Real::of(7), - )->signum(); + Number::of(1), + Number::of(7), + ) + ->result() + ->signum(); - $this->assertInstanceOf(Signum::class, $number); $this->assertSame(1, $number->value()); } public function testStringCast() { $scope = Scope::of( - Real::of(1), - Real::of(7.1), - ); + Number::of(1), + Number::of(7.1), + )->result(); $this->assertSame('7.1 - 1', $scope->toString()); } diff --git a/tests/functions.php b/tests/functions.php index 30f1a40..a9eb121 100644 --- a/tests/functions.php +++ b/tests/functions.php @@ -3,10 +3,7 @@ namespace Innmind\Math; -use Innmind\Math\{ - Algebra\Number, - Algebra\Real, -}; +use Innmind\Math\Algebra\Number; /** * @no-named-arguments @@ -17,7 +14,7 @@ function numerize(int|float|Number ...$numbers): array { return \array_map( - static fn($number): Number => $number instanceof Number ? $number : Real::of($number), + static fn($number): Number => $number instanceof Number ? $number : Number::of($number), $numbers, ); }