phpmath / polynomial
A PHP library to work with immutable univariate polynomials.
Requires
- php: ^8.4
Requires (Dev)
- phpmath/coding-standard: ^2.0
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-24 12:59:45 UTC
README
A PHP library to work with immutable univariate polynomials with float coefficients.
Documentation
The full documentation is at https://phpmath.github.io/polynomial/. Every change is listed in the changelog and every release on the releases page.
Install
composer require phpmath/polynomial
Requirements
- PHP 8.4 or newer
Usage
Every polynomial is an immutable value object. Operations never change the instance they are called on, they return a new instance.
Coefficients are given in ascending power order: the first coefficient belongs to x^0.
Trailing zeros are trimmed and the zero polynomial has no coefficients and degree -1.
use PHPMath\Polynomial\Polynomial; $p = Polynomial::of(5, -1, 0, 2); // 2x^3 - x + 5 echo $p; // "2x^3 - x + 5" echo $p->degree(); // 3 echo $p->evaluate(2); // 19 $q = Polynomial::fromRoots(1, 2); // x^2 - 3x + 2 $sum = $p->add($q); $product = $p->multiply($q); $composed = $p->compose($q); $division = $p->divide($q); $division->quotient; // Polynomial $division->remainder; // Polynomial $p->derivative(); // 6x^2 - 1 $p->integral(7); // antiderivative with constant term 7 Polynomial::fromRoots(-2, 1, 3)->realRoots(); // [-2.0, 1.0, 3.0]
Features
- Construction with
of(),fromRoots(),zero(),one()andx(). degree(),coefficient(),coefficients()andisZero().add(),subtract(),multiply(),scale(),negate()andpow().- Long division with
divide(), which returns aDivisionResult(quotient and remainder). - Composition with
compose()and evaluation withevaluate()(Horner's method). derivative()andintegral().realRoots(): the distinct real roots in ascending order.equals()with a tolerance and__toString().
Real roots
Degrees 0 to 2 use closed forms. For higher degrees the roots of the derivative split the real
line into intervals on which the polynomial is monotonic, so each interval holds at most one
root, which is refined by bisection. The tolerance (default 1e-9) is relative: a polynomial
value counts as zero when it is at most the tolerance times the sum of the absolute values of the
terms at that point. A multiple root is reported once. Roots of even multiplicity are only
located to about the square root of the floating point precision. Roots that differ by at most
the tolerance are merged into their mean, and a tolerance of 0.0 never merges roots.
Exceptions
All exceptions live in PHPMath\Polynomial\Exception and implement PolynomialException:
InvalidCoefficientException (NaN or infinity), InvalidPowerException (negative power or
exponent), InvalidToleranceException, DivisionByZeroException and ZeroPolynomialException
(roots of the zero polynomial, a DomainException).
Development
composer test
composer test-coverage
composer analyse
composer check-style
composer fix-style
Contributing
All contributions are welcome. Open an issue or a pull request on GitHub. Please follow the coding standard and add tests.
Security
If you discover a security issue, please create an issue in the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.