Skip to main content

Python package defining single-variable polynomials and operations with them

PyPI version Downloads PyPI pyversions PyPI license

Unit Tests Code Documentation Style CodeFactor codecov

Installation

pip install py-polynomial

Documentation

Click here for code-derived documentation and help

Quick examples

Flexible initialization

>>> from polynomial import Polynomial

>>> a = Polynomial(1, 2, 3, 4)
>>> str(a)
x^3 + 2x^2 + 3x + 4

>>> b = Polynomial([4 - x for x in range(4)])
>>> str(b)
4x^3 + 3x^2 + 2x + 1

First derivative

>>> b.derivative
Polynomial(12, 6, 2)

>>> str(b.derivative)
12x^2 + 6x + 2

Second or higher derivative

>>> str(b.nth_derivative(2))
24x + 6

Addition

>>> str(a + b)
5x^3 + 5x^2 + 5x + 5

Calculating value for a given x

>>> (a + b).calculate(5)
780

>>> а(2)  #  equivalent to a.calculate(2)
26

Multiplication

>>> p = Polynomial(1, 2) * Polynomial(1, 2)
>>> p
Polynomial(1, 4, 4)

Accessing coefficient by degree

>>> p[0] = -4
>>> p
Polynomial(1, 4, -4)

Slicing

>>> p[1:] = [4, -1]
>>> p
Polynomial(-1, 4, -4)

Accessing coefficients by name convention

>>> (p.a, p.b, p.c)
(-1, 4, -4)

>>> p.a, p.c = 1, 4
>>> (p.A, p.B, p.C)
(1, 4, 4)

Division and remainder

>>> q, remainder = divmod(p, Polynomial(1, 2))
>>> q
Polynomial(1.0, 2.0)
>>> remainder
Polynomial()

>>> p // Polynomial(1, 2)
Polynomial(1.0, 2.0)

>>> P(1, 2, 3) % Polynomial(1, 2)
Polynomial(3)

Check whether it contains given terms

>>> Polynomial(2, 1) in Polynomial(4, 3, 2, 1)
True

Definite integral

>>> Polynomial(3, 2, 1).integral(0, 1)
3

Misc

>>> str(Polynomial("abc"))
ax^2 + bx + c

Roots and discriminants

>>> from polynomial import QuadraticTrinomial, Monomial
>>> y = QuadraticTrinomial(1, -2, 1)
>>> str(y)
x^2 - 2x + 1

>>> y.discriminant
0

>>> y.real_roots
(1, 1)

>>> y.real_factors
(1, Polynomial(1, -1), Polynomial(1, -1))

>>> str(Monomial(5, 3))
5x^3

>>> y += Monomial(9, 2)
>>> y
Polynomial(10, -2, 1)

>>> str(y)
10x^2 - 2x + 1

>>> (y.a, y.b, y.c)
(10, -2, 1)

>>> (y.A, y.B, y.C)
(10, -2, 1)

>>> y.complex_roots
((0.1 + 0.3j), (0.1 - 0.3j))

Release files for py-polynomial 0.6.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-polynomial 0.6.2
File Size Uploaded
py-polynomial-0.6.2.tar.gz 13.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py-polynomial 0.6.2
File Interpreter ABI Platform
py_polynomial-0.6.2-py3-none-any.whl Python 3 none any Details

Total release size: 27.7 kB

Release files / py-polynomial-0.6.2.tar.gz

Download URL py-polynomial-0.6.2.tar.gz
Size 13.9 kB
Tags Source
SHA-256 checksum
How to use checksums
2a641dca63678d05d27ffd9bfd7242e37ec0197045c2126afc47459f949f3aad
BLAKE2b-256 checksum
How to use checksums
1776bee6bded85e454e47bde6d1a700d920ca1f45879ba5b0f438ca988730848
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Release files / py_polynomial-0.6.2-py3-none-any.whl

Download URL py_polynomial-0.6.2-py3-none-any.whl
Size 13.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fe9fe1f3901cd84b1b327d041f1321ac0ebf863ce367e63569305eca95bfac25
BLAKE2b-256 checksum
How to use checksums
e47497f38fb78527d32e0d1cd7ec1285370a535c83382aca97fd942586d6a678
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Release history Release notifications | RSS feed

This release

0.6.2 This release

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3

2 release files

0.2

2 release files

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page