Search by

phpmath / polynomial

waltertamboer

A PHP library to work with immutable univariate polynomials.

Package info

github.com/phpmath/polynomial

pkg:composer/phpmath/polynomial

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-11-23 11:30 UTC

This package is auto-updated.

Last update: 2026-11-24 12:59:45 UTC


README

Latest Version on Packagist Software License CI Total Downloads

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() and x().
  • degree(), coefficient(), coefficients() and isZero().
  • add(), subtract(), multiply(), scale(), negate() and pow().
  • Long division with divide(), which returns a DivisionResult (quotient and remainder).
  • Composition with compose() and evaluation with evaluate() (Horner's method).
  • derivative() and integral().
  • 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.