2017 © Pedro Peláez
 

library math-type-calculator

Calculators for the chippyash/strong-type numeric types

image

chippyash/math-type-calculator

Calculators for the chippyash/strong-type numeric types

  • Thursday, July 5, 2018
  • by chippyash
  • Repository
  • 1 Watchers
  • 1 Stars
  • 203 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 11 Versions
  • 3 % Grown

The README.md

chippyash/math-type-calculator

Quality Assurance

PHP 5.4 PHP 5.5 PHP 5.6 PHP 7 Build Status Test Coverage Code Climate, (*1)

See the test contract., (*2)

The above badges represent the current master branch. As a rule, I don't push to GitHub unless tests, coverage and usability are acceptable. This may not be true for short periods of time; on holiday, need code for some other downstream project etc. If you need stable code, use a tagged version. Read 'Further Documentation' and 'Installation'., (*3)

Current development branch is feature/gmp_support. Want to help? - that's where it is., (*4)

Please note that developer support for PHP5.4 & 5.5 was withdrawn at version 3.0.0 of this library. If you need support for PHP 5.4 or 5.5, please use a version >=2,<3, (*5)

What?

Provides arithmetic calculation support for chippyash/strong-type numeric types, (PHP native types only at this point.), (*6)

Types supported

  • FloatType
  • ComplexType
  • IntType
  • NaturalIntType
  • WholeIntType
  • RationalType
  • Conversion of PHP int and float to IntType and FloatType respectively

Arithmetic support provided

  • addition
  • subtraction
  • multiplication
  • division
  • reciprocal
  • equality comparison

The library is released under the GNU GPL V3 or later license, (*7)

Why?

Complements the strong-type library and a precursor to the forthcoming chippyash/math-matrix library which builds on the chippyash/matrix library., (*8)

When

The current library covers arithmetic operations using PHP native math support. Future versions will add support for the popular math extensions (gmp, bcmath etc)., (*9)

If you want more, either suggest it, or better still, fork it and provide a pull request., (*10)

Check out chippyash/Strong-Type for strong type including numeric, rational and complex type support, that this library operates on, (*11)

Check out chippyash/Matrix for Matrix data type support., (*12)

Check out chippyash/Logical-Matrix for logical matrix operations, (*13)

Check out chippyash/Math-Matrix for mathematical matrix operations, (*14)

Check out ZF4 Packages for more packages, (*15)

How

Coding Basics

Using the supplied calculator or comparator will set the underlying Strong Type numeric base to PHP Native. This library does not yet support GMP number types., (*16)

Calculations

Using the calculator is simplicity itself:, (*17)

    use Chippyash\Math\Type\Calculator;

    $calc = new Calculator()

Then you simply fire calculation requests at it:, (*18)

    use Chippyash\Type\TypeFactory;

    $r = TypeFactory::create('rational', 2, 3);
    $i = TypeFactory::create('int', 23);
    $w = TypeFactory::create('whole', 3);
    $n = TypeFactory::create('natural', 56);
    $f = TypeFactory::create('float', 19.6);
    $c1 = TypeFactory::create('complex', '2+3i');
    $c2 = TypeFactory::create('complex', '-6+4i');

    echo $calc->add($r, $w) . PHP_EOL;
    echo $calc->add($c1, $c2) . PHP_EOL;
    echo $calc->add($i, $f) . PHP_EOL;
    echo $calc->sub($c1, $c2) . PHP_EOL;
    echo $calc->sub($n, $w) . PHP_EOL;
    echo $calc->add($r, $w) . PHP_EOL;
    echo $calc->add($r, $w) . PHP_EOL;
    echo $calc->add($r, $w) . PHP_EOL;

The Calculator supports the following methods (all operands are NumericTypeInterface, or PHP int or PHP float):, (*19)

  • add($a, $b) : NumericTypeInterface
  • sub($a, $b) : NumericTypeInterface
  • mul($a, $b : NumericTypeInterface
  • div($a, $b) : NumericTypeInterface
  • reciprocal($a) : NumericTypeInterface
  • pow($base, $exp) : NumericTypeInterface
  • sqrt($a) : NumericTypeInterface

The Calculator will arbitrate between types and return the lowest possible type based on the operand types. The order of precedence is, (*20)

  • ComplexType
  • RationalType
  • FloatType
  • IntType (including WholeIntType and NaturalIntType)

Be careful with complex types, they can only be converted down if they are real, i.e. the imaginary part == 0, (*21)

The sqrt() method is provided as a convenience, you can use pow(n, 1/e) e.g. pow(4, 1/2) == sqrt(4), (*22)

For a demonstration of all the available operations between types and their resultant types run the examples/example-calc.php file, (*23)

Comparisons

To compare two numeric types:, (*24)

    use Chippyash\Math\Type\Comparator;
    use Chippyash\Type\TypeFactory;

    $r = TypeFactory::create('rational', 2, 3);
    $i = TypeFactory::create('int', 23);
    $w = TypeFactory::create('whole', 3);
    $n = TypeFactory::create('natural', 56);
    $f = TypeFactory::create('float', 19.6);
    $c1 = TypeFactory::create('complex', '2+3i');
    $c2 = TypeFactory::create('complex', '-6+4i');

    $comp = new Comparator();

    if ($comp->compare($r, $i) == 0) {...}
    if ($comp->compare($c1, $c2) == -1) {...}
    if ($comp->compare($w, $n) == 1) {...}

The Comparator::compare($a, $b) method takes two NumericTypeInterface types and returns, (*25)

a == b: 0
a < b : -1
a > b : 1

It has convenience methods (all operands are NumericTypeInterface):, (*26)

  • eq($a, $b) : boolean: $a == $b
  • neq($a, $b) : boolean: $a != $b
  • lt($a, $b) : boolean: $a < $b
  • lte($a, $b) : boolean: $a <= $b
  • gt($a, $b) : boolean: $a > $b
  • gte($a, $b) : boolean: $a >= $b
    if ($comp->gt($w, $f) { ... }

Changing the library

  1. fork it
  2. write the test
  3. amend it
  4. do a pull request

Found a bug you can't figure out?, (*27)

  1. fork it
  2. write the test
  3. do a pull request

NB. Make sure you rebase to HEAD before your pull request, (*28)

Where?

The library is hosted at Github. It is available at Packagist.org, (*29)

Installation

Install Composer, (*30)

For production

add, (*31)

    "chippyash/math-type-calculator": ">=3,

to your composer.json "requires" section

#### For development

Clone this repo, and then run Composer in local repo root to pull in dependencies

    git clone git@github.com:chippyash/chippyash/Math-Type-Calculator.git TypeCalc
    cd TypeCalc
    composer update

To run the tests:, (*32)

    cd TypeCalc
    vendor/bin/phpunit -c test/phpunit.xml test/

License

This software library is released under the BSD 3 Clause license, (*33)

This software library is Copyright (c) 2015-2018, Ashley Kitson, UK, (*34)

History

V0... pre releases, (*35)

V1.0.0 Original release, (*36)

V1.0.1 Add ability to mix complex and non complex types as operands, (*37)

V1.0.2 Utilise chippyash/strong-type >= 1.0.10, (*38)

V1.1.0 Add comparator class for equality comparison, (*39)

V1.1.1 Fix bad comparator construction, (*40)

V1.1.2 Fix native int/float comparison by casting to rational, (*41)

V1.1.4 Refactor for dependent library, (*42)

V1.1.5 Update dependent version number, (*43)

V1.1.6 Add pow and square root functionality, (*44)

V1.1.7 Add complex pow using complex exponent, (*45)

V1.1.8 Update dependent version number, (*46)

V1.1.9 Update dependent version number V1.1.9a Bump to fix failing build, (*47)

V1.1.10 Fix calculator to use Native PHP numeric types until GMP calculator support is available, (*48)

V2.0.0 BC Break: change namespace from chippyash\Math to Chippyash\Math\Type, (*49)

V2.0.1 Add link to packages, (*50)

V2.0.2 Ensure compatibility with PHP7, (*51)

V2.0.3 Dependency update, (*52)

V3.0.0 BC Break. Withdraw support for old PHP versions, (*53)

V3.1.0 Change of license from GPL V3 to BSD 3 Clause, (*54)

The Versions

05/07 2018

dev-master

9999999-dev http://zf4.biz/packages?utm_source=packagist&utm_medium=web&utm_campaign=blinks&utm_content=typecalculator

Calculators for the chippyash/strong-type numeric types

  Sources   Download

BSD-3-Clause GPL-3.0+

The Requires

 

The Development Requires

type calculator maths polar complex

05/07 2018

3.1.0

3.1.0.0 http://zf4.biz/packages?utm_source=packagist&utm_medium=web&utm_campaign=blinks&utm_content=typecalculator

Calculators for the chippyash/strong-type numeric types

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

type calculator maths polar complex

03/04 2018

3.0.0

3.0.0.0 http://zf4.biz/packages?utm_source=packagist&utm_medium=web&utm_campaign=blinks&utm_content=typecalculator

Calculators for the chippyash/strong-type numeric types

  Sources   Download

GPL-3.0+

The Requires

 

The Development Requires

type calculator maths polar complex

25/05 2017

2.0.3

2.0.3.0 http://zf4.biz/packages?utm_source=packagist&utm_medium=web&utm_campaign=blinks&utm_content=typecalculator

Calculators for the chippyash/strong-type numeric types

  Sources   Download

GPL-3.0+

The Requires

 

The Development Requires

type calculator maths polar complex

14/06 2016

dev-feature/gmp_support

dev-feature/gmp_support http://zf4.biz/packages?utm_source=packagist&utm_medium=web&utm_campaign=blinks&utm_content=typecalculator

Calculators for the chippyash/strong-type numeric types

  Sources   Download

GPL-3.0+

The Requires

 

The Development Requires

type calculator maths polar complex

22/03 2016

2.0.1

2.0.1.0 http://zf4.biz/packages?utm_source=packagist&utm_medium=web&utm_campaign=blinks&utm_content=typecalculator

Calculators for the chippyash/strong-type numeric types

  Sources   Download

GPL-3.0+

The Requires

 

The Development Requires

type calculator maths polar complex

03/03 2016

2.0.0

2.0.0.0 http://the-matrix.github.io/

Calculators for the chippyash/strong-type numeric types

  Sources   Download

GPL-3.0+

The Requires

 

The Development Requires

type calculator maths polar complex

22/02 2016

1.1.10

1.1.10.0 http://the-matrix.github.io/

Calculators for the chippyash/strong-type numeric types

  Sources   Download

GPL-3.0+

The Requires

 

The Development Requires

type calculator maths polar complex

15/10 2014

1.1.9a

1.1.9.0-alpha

Calculators for the chippyash/strong-type numeric types

  Sources   Download

GPL-3.0+

The Requires

 

The Development Requires

type calculator maths polar complex

07/09 2014

1.1.9

1.1.9.0

Calculators for the chippyash/strong-type numeric types

  Sources   Download

GPL-3.0+

The Requires

 

The Development Requires

type calculator maths polar complex

04/08 2014

1.0.2

1.0.2.0

Calculators for the chippyash/strong-type numeric types

  Sources   Download

GPL-3.0+

The Requires

 

The Development Requires