# Polynomials Manipulation Module Reference¶

## Basic polynomial manipulation functions¶

sympy.polys.polytools.degree(f, *gens, **args)

Return the degree of f in the given variable.

Examples

>>> from sympy import degree
>>> from sympy.abc import x, y

>>> degree(x**2 + y*x + 1, gen=x)
2
>>> degree(x**2 + y*x + 1, gen=y)
1

sympy.polys.polytools.degree_list(f, *gens, **args)

Return a list of degrees of f in all variables.

Examples

>>> from sympy import degree_list
>>> from sympy.abc import x, y

>>> degree_list(x**2 + y*x + 1)
(2, 1)

sympy.polys.polytools.LC(f, *gens, **args)

Return the leading coefficient of f.

Examples

>>> from sympy import LC
>>> from sympy.abc import x, y

>>> LC(4*x**2 + 2*x*y**2 + x*y + 3*y)
4

sympy.polys.polytools.LM(f, *gens, **args)

Return the leading monomial of f.

Examples

>>> from sympy import LM
>>> from sympy.abc import x, y

>>> LM(4*x**2 + 2*x*y**2 + x*y + 3*y)
x**2

sympy.polys.polytools.LT(f, *gens, **args)

Return the leading term of f.

Examples

>>> from sympy import LT
>>> from sympy.abc import x, y

>>> LT(4*x**2 + 2*x*y**2 + x*y + 3*y)
4*x**2

sympy.polys.polytools.pdiv(f, g, *gens, **args)

Compute polynomial pseudo-division of f and g.

Examples

>>> from sympy import pdiv
>>> from sympy.abc import x

>>> pdiv(x**2 + 1, 2*x - 4)
(2*x + 4, 20)

sympy.polys.polytools.prem(f, g, *gens, **args)

Compute polynomial pseudo-remainder of f and g.

Examples

>>> from sympy import prem
>>> from sympy.abc import x

>>> prem(x**2 + 1, 2*x - 4)
20

sympy.polys.polytools.pquo(f, g, *gens, **args)

Compute polynomial pseudo-quotient of f and g.

Examples

>>> from sympy import pquo
>>> from sympy.abc import x

>>> pquo(x**2 + 1, 2*x - 4)
2*x + 4
>>> pquo(x**2 - 1, 2*x - 1)
2*x + 1

sympy.polys.polytools.pexquo(f, g, *gens, **args)

Compute polynomial exact pseudo-quotient of f and g.

Examples

>>> from sympy import pexquo
>>> from sympy.abc import x

>>> pexquo(x**2 - 1, 2*x - 2)
2*x + 2

>>> pexquo(x**2 + 1, 2*x - 4)
Traceback (most recent call last):
...
ExactQuotientFailed: 2*x - 4 does not divide x**2 + 1

sympy.polys.polytools.div(f, g, *gens, **args)

Compute polynomial division of f and g.

Examples

>>> from sympy import div, ZZ, QQ
>>> from sympy.abc import x

>>> div(x**2 + 1, 2*x - 4, domain=ZZ)
(0, x**2 + 1)
>>> div(x**2 + 1, 2*x - 4, domain=QQ)
(x/2 + 1, 5)

sympy.polys.polytools.rem(f, g, *gens, **args)

Compute polynomial remainder of f and g.

Examples

>>> from sympy import rem, ZZ, QQ
>>> from sympy.abc import x

>>> rem(x**2 + 1, 2*x - 4, domain=ZZ)
x**2 + 1
>>> rem(x**2 + 1, 2*x - 4, domain=QQ)
5

sympy.polys.polytools.quo(f, g, *gens, **args)

Compute polynomial quotient of f and g.

Examples

>>> from sympy import quo
>>> from sympy.abc import x

>>> quo(x**2 + 1, 2*x - 4)
x/2 + 1
>>> quo(x**2 - 1, x - 1)
x + 1

sympy.polys.polytools.exquo(f, g, *gens, **args)

Compute polynomial exact quotient of f and g.

Examples

>>> from sympy import exquo
>>> from sympy.abc import x

>>> exquo(x**2 - 1, x - 1)
x + 1

>>> exquo(x**2 + 1, 2*x - 4)
Traceback (most recent call last):
...
ExactQuotientFailed: 2*x - 4 does not divide x**2 + 1

sympy.polys.polytools.half_gcdex(f, g, *gens, **args)

Half extended Euclidean algorithm of f and g.

Returns (s, h) such that h = gcd(f, g) and s*f = h (mod g).

Examples

>>> from sympy import half_gcdex
>>> from sympy.abc import x

>>> half_gcdex(x**4 - 2*x**3 - 6*x**2 + 12*x + 15, x**3 + x**2 - 4*x - 4)
(-x/5 + 3/5, x + 1)

sympy.polys.polytools.gcdex(f, g, *gens, **args)

Extended Euclidean algorithm of f and g.

Returns (s, t, h) such that h = gcd(f, g) and s*f + t*g = h.

Examples

>>> from sympy import gcdex
>>> from sympy.abc import x

>>> gcdex(x**4 - 2*x**3 - 6*x**2 + 12*x + 15, x**3 + x**2 - 4*x - 4)
(-x/5 + 3/5, x**2/5 - 6*x/5 + 2, x + 1)

sympy.polys.polytools.invert(f, g, *gens, **args)

Invert f modulo g when possible.

Examples

>>> from sympy import invert
>>> from sympy.abc import x

>>> invert(x**2 - 1, 2*x - 1)
-4/3

>>> invert(x**2 - 1, x - 1)
Traceback (most recent call last):
...
NotInvertible: zero divisor

sympy.polys.polytools.subresultants(f, g, *gens, **args)

Compute subresultant PRS of f and g.

Examples

>>> from sympy import subresultants
>>> from sympy.abc import x

>>> subresultants(x**2 + 1, x**2 - 1)
[x**2 + 1, x**2 - 1, -2]

sympy.polys.polytools.resultant(f, g, *gens, **args)

Compute resultant of f and g.

Examples

>>> from sympy import resultant
>>> from sympy.abc import x

>>> resultant(x**2 + 1, x**2 - 1)
4

sympy.polys.polytools.discriminant(f, *gens, **args)

Compute discriminant of f.

Examples

>>> from sympy import discriminant
>>> from sympy.abc import x

>>> discriminant(x**2 + 2*x + 3)
-8

sympy.polys.polytools.terms_gcd(f, *gens, **args)

Remove GCD of terms from f.

Examples

>>> from sympy import terms_gcd
>>> from sympy.abc import x, y

>>> terms_gcd(x**6*y**2 + x**3*y, x, y)
x**3*y*(x**3*y + 1)

sympy.polys.polytools.cofactors(f, g, *gens, **args)

Compute GCD and cofactors of f and g.

Returns polynomials (h, cff, cfg) such that h = gcd(f, g), and cff = quo(f, h) and cfg = quo(g, h) are, so called, cofactors of f and g.

Examples

>>> from sympy import cofactors
>>> from sympy.abc import x

>>> cofactors(x**2 - 1, x**2 - 3*x + 2)
(x - 1, x + 1, x - 2)

sympy.polys.polytools.gcd(f, g=None, *gens, **args)

Compute GCD of f and g.

Examples

>>> from sympy import gcd
>>> from sympy.abc import x

>>> gcd(x**2 - 1, x**2 - 3*x + 2)
x - 1

sympy.polys.polytools.gcd_list(seq, *gens, **args)

Compute GCD of a list of polynomials.

Examples

>>> from sympy import gcd_list
>>> from sympy.abc import x

>>> gcd_list([x**3 - 1, x**2 - 1, x**2 - 3*x + 2])
x - 1

sympy.polys.polytools.lcm(f, g=None, *gens, **args)

Compute LCM of f and g.

Examples

>>> from sympy import lcm
>>> from sympy.abc import x

>>> lcm(x**2 - 1, x**2 - 3*x + 2)
x**3 - 2*x**2 - x + 2

sympy.polys.polytools.lcm_list(seq, *gens, **args)

Compute LCM of a list of polynomials.

Examples

>>> from sympy import lcm_list
>>> from sympy.abc import x

>>> lcm_list([x**3 - 1, x**2 - 1, x**2 - 3*x + 2])
x**5 - x**4 - 2*x**3 - x**2 + x + 2

sympy.polys.polytools.trunc(f, p, *gens, **args)

Reduce f modulo a constant p.

Examples

>>> from sympy import trunc
>>> from sympy.abc import x

>>> trunc(2*x**3 + 3*x**2 + 5*x + 7, 3)
-x**3 - x + 1

sympy.polys.polytools.monic(f, *gens, **args)

Divide all coefficients of f by LC(f).

Examples

>>> from sympy import monic
>>> from sympy.abc import x

>>> monic(3*x**2 + 4*x + 2)
x**2 + 4*x/3 + 2/3

sympy.polys.polytools.content(f, *gens, **args)

Compute GCD of coefficients of f.

Examples

>>> from sympy import content
>>> from sympy.abc import x

>>> content(6*x**2 + 8*x + 12)
2

sympy.polys.polytools.primitive(f, *gens, **args)

Compute content and the primitive form of f.

Examples

>>> from sympy import primitive
>>> from sympy.abc import x

>>> primitive(6*x**2 + 8*x + 12)
(2, 3*x**2 + 4*x + 6)

sympy.polys.polytools.compose(f, g, *gens, **args)

Compute functional composition f(g).

Examples

>>> from sympy import compose
>>> from sympy.abc import x

>>> compose(x**2 + x, x - 1)
x**2 - x

sympy.polys.polytools.decompose(f, *gens, **args)

Compute functional decomposition of f.

Examples

>>> from sympy import decompose
>>> from sympy.abc import x

>>> decompose(x**4 + 2*x**3 - x - 1)
[x**2 - x - 1, x**2 + x]

sympy.polys.polytools.sturm(f, *gens, **args)

Compute Sturm sequence of f.

Examples

>>> from sympy import sturm
>>> from sympy.abc import x

>>> sturm(x**3 - 2*x**2 + x - 3)
[x**3 - 2*x**2 + x - 3, 3*x**2 - 4*x + 1, 2*x/9 + 25/9, -2079/4]

sympy.polys.polytools.gff_list(f, *gens, **args)

Compute a list of greatest factorial factors of f.

Examples

>>> from sympy import gff_list, ff
>>> from sympy.abc import x

>>> f = x**5 + 2*x**4 - x**3 - 2*x**2

>>> gff_list(f)
[(x, 1), (x + 2, 4)]

>>> (ff(x, 1)*ff(x + 2, 4)).expand() == f
True

sympy.polys.polytools.gff(f, *gens, **args)

Compute greatest factorial factorization of f.

sympy.polys.polytools.sqf_norm(f, *gens, **args)

Compute square-free norm of f.

Returns s, f, r, such that g(x) = f(x-sa) and r(x) = Norm(g(x)) is a square-free polynomial over K, where a is the algebraic extension of the ground domain.

Examples

>>> from sympy import sqf_norm, sqrt
>>> from sympy.abc import x

>>> sqf_norm(x**2 + 1, extension=[sqrt(3)])
(1, x**2 - 2*3**(1/2)*x + 4, x**4 - 4*x**2 + 16)

sympy.polys.polytools.sqf_part(f, *gens, **args)

Compute square-free part of f.

Examples

>>> from sympy import sqf_part
>>> from sympy.abc import x

>>> sqf_part(x**3 - 3*x - 2)
x**2 - x - 2

sympy.polys.polytools.sqf_list(f, *gens, **args)

Compute a list of square-free factors of f.

Examples

>>> from sympy import sqf_list
>>> from sympy.abc import x

>>> sqf_list(2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16)
(2, [(x + 1, 2), (x + 2, 3)])

sympy.polys.polytools.sqf(f, *gens, **args)

Compute square-free factorization of f.

Examples

>>> from sympy import sqf
>>> from sympy.abc import x

>>> sqf(2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16)
2*(x + 1)**2*(x + 2)**3

sympy.polys.polytools.factor_list(f, *gens, **args)

Compute a list of irreducible factors of f.

Examples

>>> from sympy import factor_list
>>> from sympy.abc import x, y

>>> factor_list(2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y)
(2, [(x + y, 1), (x**2 + 1, 2)])

sympy.polys.polytools.factor(f, *gens, **args)

Compute the factorization of f into irreducibles. (Use factorint to factor an integer.)

There two modes implemented: symbolic and formal. If f is not an instance of Poly and generators are not specified, then the former mode is used. Otherwise, the formal mode is used.

In symbolic mode, factor() will traverse the expression tree and factor its components without any prior expansion, unless an instance of Add is encountered (in this case formal factorization is used). This way factor() can handle large or symbolic exponents.

By default, the factorization is computed over the rationals. To factor over other domain, e.g. an algebraic or finite field, use appropriate options: extension, modulus or domain.

Examples

>>> from sympy import factor, sqrt
>>> from sympy.abc import x, y

>>> factor(2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y)
2*(x + y)*(x**2 + 1)**2

>>> factor(x**2 + 1)
x**2 + 1
>>> factor(x**2 + 1, modulus=2)
(x + 1)**2
>>> factor(x**2 + 1, gaussian=True)
(x - I)*(x + I)

>>> factor(x**2 - 2, extension=sqrt(2))
(x - 2**(1/2))*(x + 2**(1/2))

>>> factor((x**2 - 1)/(x**2 + 4*x + 4))
(x - 1)*(x + 1)/(x + 2)**2
>>> factor((x**2 + 4*x + 4)**10000000*(x**2 + 1))
(x + 2)**20000000*(x**2 + 1)

sympy.polys.polytools.intervals(F, all=False, eps=None, inf=None, sup=None, strict=False, fast=False, sqf=False)

Compute isolating intervals for roots of f.

Examples

>>> from sympy import intervals
>>> from sympy.abc import x

>>> intervals(x**2 - 3)
[((-2, -1), 1), ((1, 2), 1)]
>>> intervals(x**2 - 3, eps=1e-2)
[((-26/15, -19/11), 1), ((19/11, 26/15), 1)]

sympy.polys.polytools.refine_root(f, s, t, eps=None, steps=None, fast=False, check_sqf=False)

Refine an isolating interval of a root to the given precision.

Examples

>>> from sympy import refine_root
>>> from sympy.abc import x

>>> refine_root(x**2 - 3, 1, 2, eps=1e-2)
(19/11, 26/15)

sympy.polys.polytools.count_roots(f, inf=None, sup=None)

Return the number of roots of f in [inf, sup] interval.

If one of inf or sup is complex, it will return the number of roots in the complex rectangle with corners at inf and sup.

Examples

>>> from sympy import count_roots, I
>>> from sympy.abc import x

>>> count_roots(x**4 - 4, -3, 3)
2
>>> count_roots(x**4 - 4, 0, 1 + 3*I)
1

sympy.polys.polytools.real_roots(f, multiple=True)

Return a list of real roots with multiplicities of f.

Examples

>>> from sympy import real_roots
>>> from sympy.abc import x

>>> real_roots(2*x**3 - 7*x**2 + 4*x + 4)
[-1/2, 2, 2]

sympy.polys.polytools.nroots(f, maxsteps=50, cleanup=True, error=False)

Compute numerical approximations of roots of f.

Examples

>>> from sympy import nroots
>>> from sympy.abc import x

>>> nroots(x**2 - 3)
[-1.73205080756888, 1.73205080756888]

sympy.polys.polytools.ground_roots(f, *gens, **args)

Compute roots of f by factorization in the ground domain.

Examples

>>> from sympy import ground_roots
>>> from sympy.abc import x

>>> ground_roots(x**6 - 4*x**4 + 4*x**3 - x**2)
{0: 2, 1: 2}

sympy.polys.polytools.cancel(f, *gens, **args)

Cancel common factors in a rational function f.

Examples

>>> from sympy import cancel
>>> from sympy.abc import x

>>> cancel((2*x**2 - 2)/(x**2 - 2*x + 1))
(2*x + 2)/(x - 1)

sympy.polys.polytools.reduced(f, G, *gens, **args)

Reduces a polynomial f modulo a set of polynomials G.

Given a polynomial f and a set of polynomials G = (g_1, ..., g_n), computes a set of quotients q = (q_1, ..., q_n) and the remainder r such that f = q_1*f_1 + ... + q_n*f_n + r, where r vanishes or r is a completely reduced polynomial with respect to G.

Examples

>>> from sympy import reduced
>>> from sympy.abc import x, y

>>> reduced(2*x**4 + y**2 - x**2 + y**3, [x**3 - x, y**3 - y])
([2*x, 1], x**2 + y**2 + y)

sympy.polys.polytools.groebner(F, *gens, **args)

Computes the reduced Groebner basis for a set of polynomials.

Use the order argument to set the monomial ordering that will be used to compute the basis. Allowed orders are lex, grlex and grevlex. If no order is specified, it defaults to lex.

Examples

>>> from sympy import groebner
>>> from sympy.abc import x, y

>>> groebner([x*y - 2*y, 2*y**2 - x**2], order='lex')
[x**2 - 2*y**2, x*y - 2*y, y**3 - 2*y]
>>> groebner([x*y - 2*y, 2*y**2 - x**2], order='grlex')
[y**3 - 2*y, x**2 - 2*y**2, x*y - 2*y]
>>> groebner([x*y - 2*y, 2*y**2 - x**2], order='grevlex')
[x**3 - 2*x**2, -x**2 + 2*y**2, x*y - 2*y]


References

class sympy.polys.polytools.Poly

Generic class for representing polynomial expressions.

EC(f, order=None)

Returns the last non-zero coefficient of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**3 + 2*x**2 + 3*x, x).EC()
3

EM(f, order=None)

Returns the last non-zero monomial of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).EM()
(0, 1)

ET(f, order=None)

Returns the last non-zero term of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).ET()
((0, 1), 3)

LC(f, order=None)

Returns the leading coefficient of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(4*x**3 + 2*x**2 + 3*x, x).LC()
4

LM(f, order=None)

Returns the leading monomial of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).LM()
(2, 0)

LT(f, order=None)

Returns the leading term of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).LT()
((2, 0), 4)

TC(f)

Returns the trailing coefficent of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**3 + 2*x**2 + 3*x, x).TC()
0

abs(f)

Make all coefficients in f positive.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 - 1, x).abs()
Poly(x**2 + 1, x, domain='ZZ')


Add two polynomials f and g.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 + 1, x).add(Poly(x - 2, x))
Poly(x**2 + x - 1, x, domain='ZZ')

>>> Poly(x**2 + 1, x) + Poly(x - 2, x)
Poly(x**2 + x - 1, x, domain='ZZ')


Add an element of the ground domain to f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x + 1).add_ground(2)
Poly(x + 3, x, domain='ZZ')

all_coeffs(f)

Returns all coefficients from a univariate polynomial f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**3 + 2*x - 1, x).all_coeffs()
[1, 0, 2, -1]

all_monoms(f)

Returns all monomials from a univariate polynomial f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**3 + 2*x - 1, x).all_monoms()
[(3,), (2,), (1,), (0,)]


Return a list of real and complex roots with multiplicities.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(2*x**3 - 7*x**2 + 4*x + 4).all_roots()
[-1/2, 2, 2]
>>> Poly(x**3 + x + 1).all_roots()
[RootOf(x**3 + x + 1, 0), RootOf(x**3 + x + 1, 1), RootOf(x**3 + x + 1, 2)]

all_terms(f)

Returns all terms from a univariate polynomial f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**3 + 2*x - 1, x).all_terms()
[((3,), 1), ((2,), 0), ((1,), 2), ((0,), -1)]

args

Don’t mess up with the core.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 + 1, x).args
[x**2 + 1]

as_dict(f, native=False, zero=False)

Switch to a dict representation.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x**2 + 2*x*y**2 - y, x, y).as_dict()
{(0, 1): -1, (1, 2): 2, (2, 0): 1}

as_expr(f, *gens)

Convert a polynomial an expression.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> f = Poly(x**2 + 2*x*y**2 - y, x, y)

>>> f.as_expr()
x**2 + 2*x*y**2 - y
>>> f.as_expr({x: 5})
10*y**2 - y + 25
>>> f.as_expr(5, 6)
379

as_list(f, native=False)

Switch to a list representation.

cancel(f, g, include=False)

Cancel common factors in a rational function f/g.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(2*x**2 - 2, x).cancel(Poly(x**2 - 2*x + 1, x))
(1, Poly(2*x + 2, x, domain='ZZ'), Poly(x - 1, x, domain='ZZ'))

>>> Poly(2*x**2 - 2, x).cancel(Poly(x**2 - 2*x + 1, x), include=True)
(Poly(2*x + 2, x, domain='ZZ'), Poly(x - 1, x, domain='ZZ'))

clear_denoms(f, convert=False)

Clear denominators, but keep the ground domain.

Examples

>>> from sympy import Poly, S, QQ
>>> from sympy.abc import x

>>> f = Poly(x/2 + S(1)/3, x, domain=QQ)

>>> f.clear_denoms()
(6, Poly(3*x + 2, x, domain='QQ'))
>>> f.clear_denoms(convert=True)
(6, Poly(3*x + 2, x, domain='ZZ'))

coeffs(f, order=None)

Returns all non-zero coefficients from f in lex order.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**3 + 2*x + 3, x).coeffs()
[1, 2, 3]

cofactors(f, g)

Returns the GCD of f and g and their cofactors.

Returns polynomials (h, cff, cfg) such that h = gcd(f, g), and cff = quo(f, h) and cfg = quo(g, h) are, so called, cofactors of f and g.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 - 1, x).cofactors(Poly(x**2 - 3*x + 2, x))
(Poly(x - 1, x, domain='ZZ'),
Poly(x + 1, x, domain='ZZ'),
Poly(x - 2, x, domain='ZZ'))

compose(f, g)

Computes the functional composition of f and g.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 + x, x).compose(Poly(x - 1, x))
Poly(x**2 - x, x, domain='ZZ')

content(f)

Returns the GCD of polynomial coefficients.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(6*x**2 + 8*x + 12, x).content()
2

count_roots(f, inf=None, sup=None)

Return the number of roots of f in [inf, sup] interval.

Examples

>>> from sympy import Poly, I
>>> from sympy.abc import x

>>> Poly(x**4 - 4, x).count_roots(-3, 3)
2
>>> Poly(x**4 - 4, x).count_roots(0, 1 + 3*I)
1

decompose(f)

Computes a functional decomposition of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**4 + 2*x**3 - x - 1, x, domain='ZZ').decompose()
[Poly(x**2 - x - 1, x, domain='ZZ'), Poly(x**2 + x, x, domain='ZZ')]

deflate(f)

Reduce degree of f by mapping x_i**m to y_i.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x**6*y**2 + x**3 + 1, x, y).deflate()
((3, 2), Poly(x**2*y + x + 1, x, y, domain='ZZ'))

degree(f, gen=0)

Returns degree of f in x_j.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x**2 + y*x + 1, x, y).degree()
2
>>> Poly(x**2 + y*x + y, x, y).degree(y)
1

degree_list(f)

Returns a list of degrees of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x**2 + y*x + 1, x, y).degree_list()
(2, 1)

diff(f, *specs)

Computes partial derivative of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x**2 + 2*x + 1, x).diff()
Poly(2*x + 2, x, domain='ZZ')

>>> Poly(x*y**2 + x, x, y).diff((0, 0), (1, 1))
Poly(2*x*y, x, y, domain='ZZ')

discriminant(f)

Computes the discriminant of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 + 2*x + 3, x).discriminant()
-8

div(f, g, auto=True)

Polynomial division with remainder of f by g.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 + 1, x).div(Poly(2*x - 4, x))
(Poly(1/2*x + 1, x, domain='QQ'), Poly(5, x, domain='QQ'))

>>> Poly(x**2 + 1, x).div(Poly(2*x - 4, x), auto=False)
(Poly(0, x, domain='ZZ'), Poly(x**2 + 1, x, domain='ZZ'))

domain

Get the ground domain of self.

eject(f, *gens)

Eject selected generators into the ground domain.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> f = Poly(x**2*y + x*y**3 + x*y + 1, x, y)

>>> f.eject(x)
Poly(x*y**3 + (x**2 + x)*y + 1, y, domain='ZZ[x]')
>>> f.eject(y)
Poly(y*x**2 + (y**3 + y)*x + 1, x, domain='ZZ[y]')

eval(f, x, a=None, auto=True)

Evaluate f at a in the given variable.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y, z

>>> Poly(x**2 + 2*x + 3, x).eval(2)
11

>>> Poly(2*x*y + 3*x + y + 2, x, y).eval(x, 2)
Poly(5*y + 8, y, domain='ZZ')

>>> f = Poly(2*x*y + 3*x + y + 2*z, x, y, z)

>>> f.eval({x: 2})
Poly(5*y + 2*z + 6, y, z, domain='ZZ')
>>> f.eval({x: 2, y: 5})
Poly(2*z + 31, z, domain='ZZ')
>>> f.eval({x: 2, y: 5, z: 7})
45

exclude(f)

Remove unnecessary generators from f.

Example

>>> from sympy import Poly
>>> from sympy.abc import a, b, c, d, x

>>> Poly(a + x, a, b, c, d, x).exclude()
Poly(a + x, a, x, domain='ZZ')

exquo(f, g, auto=True)

Computes polynomial exact quotient of f by g.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 - 1, x).exquo(Poly(x - 1, x))
Poly(x + 1, x, domain='ZZ')

>>> Poly(x**2 + 1, x).exquo(Poly(2*x - 4, x))
Traceback (most recent call last):
...
ExactQuotientFailed: 2*x - 4 does not divide x**2 + 1

exquo_ground(f, coeff)

Exact quotient of f by a an element of the ground domain.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(2*x + 4).exquo_ground(2)
Poly(x + 2, x, domain='ZZ')

>>> Poly(2*x + 3).exquo_ground(2)
Traceback (most recent call last):
...
ExactQuotientFailed: 2 does not divide 3 in ZZ

factor_list(f)

Returns a list of irreducible factors of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> f = 2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y

>>> Poly(f).factor_list()
(2, [(Poly(x + y, x, y, domain='ZZ'), 1),
(Poly(x**2 + 1, x, y, domain='ZZ'), 2)])

factor_list_include(f)

Returns a list of irreducible factors of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> f = 2*x**5 + 2*x**4*y + 4*x**3 + 4*x**2*y + 2*x + 2*y

>>> Poly(f).factor_list_include()
[(Poly(2*x + 2*y, x, y, domain='ZZ'), 1),
(Poly(x**2 + 1, x, y, domain='ZZ'), 2)]

free_symbols

Free symbols of a polynomial expression.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x**2 + 1).free_symbols
set([x])
>>> Poly(x**2 + y).free_symbols
set([x, y])
>>> Poly(x**2 + y, x).free_symbols
set([x, y])

free_symbols_in_domain

Free symbols of the domain of self.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x**2 + 1).free_symbols_in_domain
set()
>>> Poly(x**2 + y).free_symbols_in_domain
set()
>>> Poly(x**2 + y, x).free_symbols_in_domain
set([y])

classmethod from_dict(rep, *gens, **args)

Construct a polynomial from a dict.

classmethod from_expr(rep, *gens, **args)

Construct a polynomial from an expression.

classmethod from_list(rep, *gens, **args)

Construct a polynomial from a list.

classmethod from_poly(rep, *gens, **args)

Construct a polynomial from a polynomial.

gcd(f, g)

Returns the polynomial GCD of f and g.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 - 1, x).gcd(Poly(x**2 - 3*x + 2, x))
Poly(x - 1, x, domain='ZZ')

gcdex(f, g, auto=True)

Extended Euclidean algorithm of f and g.

Returns (s, t, h) such that h = gcd(f, g) and s*f + t*g = h.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15
>>> g = x**3 + x**2 - 4*x - 4

>>> Poly(f).gcdex(Poly(g))
(Poly(-1/5*x + 3/5, x, domain='QQ'),
Poly(1/5*x**2 - 6/5*x + 2, x, domain='QQ'),
Poly(x + 1, x, domain='QQ'))

gen

Return the principal generator.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 + 1, x).gen
x

get_domain(f)

Get the ground domain of f.

get_modulus(f)

Get the modulus of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 + 1, modulus=2).get_modulus()
2

gff_list(f)

Computes greatest factorial factorization of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> f = x**5 + 2*x**4 - x**3 - 2*x**2

>>> Poly(f).gff_list()
[(Poly(x, x, domain='ZZ'), 1), (Poly(x + 2, x, domain='ZZ'), 4)]

ground_roots(f)

Compute roots of f by factorization in the ground domain.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**6 - 4*x**4 + 4*x**3 - x**2).ground_roots()
{0: 2, 1: 2}

half_gcdex(f, g, auto=True)

Half extended Euclidean algorithm of f and g.

Returns (s, h) such that h = gcd(f, g) and s*f = h (mod g).

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15
>>> g = x**3 + x**2 - 4*x - 4

>>> Poly(f).half_gcdex(Poly(g))
(Poly(-1/5*x + 3/5, x, domain='QQ'), Poly(x + 1, x, domain='QQ'))

has_only_gens(f, *gens)

Return True if Poly(f, *gens) retains ground domain.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y, z

>>> Poly(x*y + 1, x, y, z).has_only_gens(x, y)
True
>>> Poly(x*y + z, x, y, z).has_only_gens(x, y)
False

inject(f, front=False)

Inject ground domain generators into f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> f = Poly(x**2*y + x*y**3 + x*y + 1, x)

>>> f.inject()
Poly(x**2*y + x*y**3 + x*y + 1, x, y, domain='ZZ')
>>> f.inject(front=True)
Poly(y**3*x + y*x**2 + y*x + 1, y, x, domain='ZZ')

integrate(f, *specs, **args)

Computes indefinite integral of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x**2 + 2*x + 1, x).integrate()
Poly(1/3*x**3 + x**2 + x, x, domain='QQ')

>>> Poly(x*y**2 + x, x, y).integrate((0, 1), (1, 0))
Poly(1/2*x**2*y**2 + 1/2*x**2, x, y, domain='QQ')

intervals(f, all=False, eps=None, inf=None, sup=None, fast=False, sqf=False)

Compute isolating intervals for roots of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 - 3, x).intervals()
[((-2, -1), 1), ((1, 2), 1)]
>>> Poly(x**2 - 3, x).intervals(eps=1e-2)
[((-26/15, -19/11), 1), ((19/11, 26/15), 1)]

invert(f, g, auto=True)

Invert f modulo g when possible.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 - 1, x).invert(Poly(2*x - 1, x))
Poly(-4/3, x, domain='QQ')

>>> Poly(x**2 - 1, x).invert(Poly(x - 1, x))
Traceback (most recent call last):
...
NotInvertible: zero divisor

is_cyclotomic

Returns True if f is a cyclotomic polnomial.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> f = x**16 + x**14 - x**10 + x**8 - x**6 + x**2 + 1

>>> Poly(f).is_cyclotomic
False

>>> g = x**16 + x**14 - x**10 - x**8 - x**6 + x**2 + 1

>>> Poly(g).is_cyclotomic
True

is_ground

Returns True if f is an element of the ground domain.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x, x).is_ground
False
>>> Poly(2, x).is_ground
True
>>> Poly(y, x).is_ground
True

is_homogeneous

Returns True if f has zero trailing coefficient.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x*y + x + y, x, y).is_homogeneous
True
>>> Poly(x*y + x + y + 1, x, y).is_homogeneous
False

is_irreducible

Returns True if f has no factors over its domain.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x


>> Poly(x**2 + x + 1, x, modulus=2).is_irreducible True >> Poly(x**2 + 1, x, modulus=2).is_irreducible False

is_linear

Returns True if f is linear in all its variables.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x + y + 2, x, y).is_linear
True
>>> Poly(x*y + 2, x, y).is_linear
False

is_monic

Returns True if the leading coefficient of f is one.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x + 2, x).is_monic
True
>>> Poly(2*x + 2, x).is_monic
False

is_monomial

Returns True if f is zero or has only one term.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(3*x**2, x).is_monomial
True
>>> Poly(3*x**2 + 1, x).is_monomial
False

is_multivariate

Returns True if f is a multivariate polynomial.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x**2 + x + 1, x).is_multivariate
False
>>> Poly(x*y**2 + x*y + 1, x, y).is_multivariate
True
>>> Poly(x*y**2 + x*y + 1, x).is_multivariate
False
>>> Poly(x**2 + x + 1, x, y).is_multivariate
True

is_one

Returns True if f is a unit polynomial.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(0, x).is_one
False
>>> Poly(1, x).is_one
True

is_primitive

Returns True if GCD of the coefficients of f is one.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(2*x**2 + 6*x + 12, x).is_primitive
False
>>> Poly(x**2 + 3*x + 6, x).is_primitive
True


Returns True if f is quadratic in all its variables.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x*y + 2, x, y).is_quadratic
True
>>> Poly(x*y**2 + 2, x, y).is_quadratic
False

is_sqf

Returns True if f is a square-free polynomial.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 - 2*x + 1, x).is_sqf
False
>>> Poly(x**2 - 1, x).is_sqf
True

is_univariate

Returns True if f is a univariate polynomial.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x**2 + x + 1, x).is_univariate
True
>>> Poly(x*y**2 + x*y + 1, x, y).is_univariate
False
>>> Poly(x*y**2 + x*y + 1, x).is_univariate
True
>>> Poly(x**2 + x + 1, x, y).is_univariate
False

is_zero

Returns True if f is a zero polynomial.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(0, x).is_zero
True
>>> Poly(1, x).is_zero
False

l1_norm(f)

Returns l1 norm of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(-x**2 + 2*x - 3, x).l1_norm()
6

lcm(f, g)

Returns polynomial LCM of f and g.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 - 1, x).lcm(Poly(x**2 - 3*x + 2, x))
Poly(x**3 - 2*x**2 - x + 2, x, domain='ZZ')

length(f)

Returns the number of non-zero terms in f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 + 2*x - 1).length()
3

lift(f)

Convert algebraic coefficients to rationals.

Examples

>>> from sympy import Poly, I
>>> from sympy.abc import x

>>> Poly(x**2 + I*x + 1, x, extension=I).lift()
Poly(x**4 + 3*x**2 + 1, x, domain='QQ')

ltrim(f, gen)

Remove dummy generators from the “left” of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y, z

>>> Poly(y**2 + y*z**2, x, y, z).ltrim(y)
Poly(y**2 + y*z**2, y, z, domain='ZZ')

max_norm(f)

Returns maximum norm of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(-x**2 + 2*x - 3, x).max_norm()
3

monic(f, auto=True)

Divides all coefficients by LC(f).

Examples

>>> from sympy import Poly, ZZ
>>> from sympy.abc import x

>>> Poly(3*x**2 + 6*x + 9, x, domain=ZZ).monic()
Poly(x**2 + 2*x + 3, x, domain='QQ')

>>> Poly(3*x**2 + 4*x + 2, x, domain=ZZ).monic()
Poly(x**2 + 4/3*x + 2/3, x, domain='QQ')

monoms(f, order=None)

Returns all non-zero monomials from f in lex order.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x**2 + 2*x*y**2 + x*y + 3*y, x, y).monoms()
[(2, 0), (1, 2), (1, 1), (0, 1)]

mul(f, g)

Multiply two polynomials f and g.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 + 1, x).mul(Poly(x - 2, x))
Poly(x**3 - 2*x**2 + x - 2, x, domain='ZZ')

>>> Poly(x**2 + 1, x)*Poly(x - 2, x)
Poly(x**3 - 2*x**2 + x - 2, x, domain='ZZ')

mul_ground(f, coeff)

Multiply f by a an element of the ground domain.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x + 1).mul_ground(2)
Poly(2*x + 2, x, domain='ZZ')

neg(f)

Negate all coefficients in f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 - 1, x).neg()
Poly(-x**2 + 1, x, domain='ZZ')

>>> -Poly(x**2 - 1, x)
Poly(-x**2 + 1, x, domain='ZZ')

classmethod new(rep, *gens)

Construct Poly instance from raw representation.

nroots(f, maxsteps=50, cleanup=True, error=False)

Compute numerical approximations of roots of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 - 3).nroots()
[-1.73205080756888, 1.73205080756888]

nth(f, *N)

Returns the n-th coefficient of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x**3 + 2*x**2 + 3*x, x).nth(2)
2
>>> Poly(x**3 + 2*x*y**2 + y**2, x, y).nth(1, 2)
2

nth_power_roots_poly(f, n)

Construct a polynomial with n-th powers of roots of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> f = Poly(x**4 - x**2 + 1)

>>> f.nth_power_roots_poly(2)
Poly(x**4 - 2*x**3 + 3*x**2 - 2*x + 1, x, domain='ZZ')
>>> f.nth_power_roots_poly(3)
Poly(x**4 + 2*x**2 + 1, x, domain='ZZ')
>>> f.nth_power_roots_poly(4)
Poly(x**4 + 2*x**3 + 3*x**2 + 2*x + 1, x, domain='ZZ')
>>> f.nth_power_roots_poly(12)
Poly(x**4 - 4*x**3 + 6*x**2 - 4*x + 1, x, domain='ZZ')

one

Return one polynomial with self‘s properties.

pdiv(f, g)

Polynomial pseudo-division of f by g.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 + 1, x).pdiv(Poly(2*x - 4, x))
(Poly(2*x + 4, x, domain='ZZ'), Poly(20, x, domain='ZZ'))

per(f, rep, gens=None, remove=None)

Create a Poly out of the given representation.

Examples

>>> from sympy import Poly, ZZ
>>> from sympy.abc import x, y

>>> from sympy.polys.polyclasses import DMP

>>> a = Poly(x**2 + 1)

>>> a.per(DMP([ZZ(1), ZZ(1)], ZZ), gens=[y])
Poly(y + 1, y, domain='ZZ')

pexquo(f, g)

Polynomial exact pseudo-quotient of f by g.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 - 1, x).pexquo(Poly(2*x - 2, x))
Poly(2*x + 2, x, domain='ZZ')

>>> Poly(x**2 + 1, x).pexquo(Poly(2*x - 4, x))
Traceback (most recent call last):
...
ExactQuotientFailed: 2*x - 4 does not divide x**2 + 1

pow(f, n)

Raise f to a non-negative power n.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x - 2, x).pow(3)
Poly(x**3 - 6*x**2 + 12*x - 8, x, domain='ZZ')

>>> Poly(x - 2, x)**3
Poly(x**3 - 6*x**2 + 12*x - 8, x, domain='ZZ')

pquo(f, g)

Polynomial pseudo-quotient of f by g.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 + 1, x).pquo(Poly(2*x - 4, x))
Poly(2*x + 4, x, domain='ZZ')

>>> Poly(x**2 - 1, x).pquo(Poly(2*x - 2, x))
Poly(2*x + 2, x, domain='ZZ')

prem(f, g)

Polynomial pseudo-remainder of f by g.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 + 1, x).prem(Poly(2*x - 4, x))
Poly(20, x, domain='ZZ')

primitive(f)

Returns the content and a primitive form of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(2*x**2 + 8*x + 12, x).primitive()
(2, Poly(x**2 + 4*x + 6, x, domain='ZZ'))

quo(f, g, auto=True)

Computes polynomial quotient of f by g.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 + 1, x).quo(Poly(2*x - 4, x))
Poly(1/2*x + 1, x, domain='QQ')

>>> Poly(x**2 - 1, x).quo(Poly(x - 1, x))
Poly(x + 1, x, domain='ZZ')

quo_ground(f, coeff)

Quotient of f by a an element of the ground domain.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(2*x + 4).quo_ground(2)
Poly(x + 2, x, domain='ZZ')

>>> Poly(2*x + 3).quo_ground(2)
Poly(x + 1, x, domain='ZZ')

rat_clear_denoms(f, g)

Clear denominators in a rational function f/g.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> f = Poly(x**2/y + 1, x)
>>> g = Poly(x**3 + y, x)

>>> p, q = f.rat_clear_denoms(g)

>>> p
Poly(x**2 + y, x, domain='ZZ[y]')
>>> q
Poly(y*x**3 + y**2, x, domain='ZZ[y]')


Return a list of real roots with multiplicities.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(2*x**3 - 7*x**2 + 4*x + 4).real_roots()
[-1/2, 2, 2]
>>> Poly(x**3 + x + 1).real_roots()
[RootOf(x**3 + x + 1, 0)]

refine_root(f, s, t, eps=None, steps=None, fast=False, check_sqf=False)

Refine an isolating interval of a root to the given precision.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 - 3, x).refine_root(1, 2, eps=1e-2)
(19/11, 26/15)

rem(f, g, auto=True)

Computes the polynomial remainder of f by g.

Examples

>>> from sympy import Poly, ZZ, QQ
>>> from sympy.abc import x

>>> Poly(x**2 + 1, x).rem(Poly(2*x - 4, x))
Poly(5, x, domain='ZZ')

>>> Poly(x**2 + 1, x).rem(Poly(2*x - 4, x), auto=False)
Poly(x**2 + 1, x, domain='ZZ')

reorder(f, *gens, **args)

Efficiently apply new order of generators.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x**2 + x*y**2, x, y).reorder(y, x)
Poly(y**2*x + x**2, y, x, domain='ZZ')

replace(f, x, y=None)

Replace x with y in generators list.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x**2 + 1, x).replace(x, y)
Poly(y**2 + 1, y, domain='ZZ')

resultant(f, g)

Computes the resultant of f and g via PRS.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 + 1, x).resultant(Poly(x**2 - 1, x))
4

retract(f, field=None)

Recalculate the ground domain of a polynomial.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> f = Poly(x**2 + 1, x, domain='QQ[y]')
>>> f
Poly(x**2 + 1, x, domain='QQ[y]')

>>> f.retract()
Poly(x**2 + 1, x, domain='ZZ')
>>> f.retract(field=True)
Poly(x**2 + 1, x, domain='QQ')

revert(f, n)

Compute f**(-1) mod x**n.

Get an indexed root of a polynomial.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> f = Poly(2*x**3 - 7*x**2 + 4*x + 4)

>>> f.root(0)
-1/2
>>> f.root(1)
2
>>> f.root(2)
2
>>> f.root(3)
Traceback (most recent call last):
...
IndexError: root index out of [-3, 2] range, got 3

>>> Poly(x**5 + x + 1).root(0)
RootOf(x**3 - x**2 + 1, 0)

set_domain(f, domain)

Set the ground domain of f.

set_modulus(f, modulus)

Set the modulus of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(5*x**2 + 2*x - 1, x).set_modulus(2)
Poly(x**2 + 1, x, modulus=2)

shift(f, a)

Efficiently compute Taylor shift f(x + a).

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 - 2*x + 1, x).shift(2)
Poly(x**2 + 2*x + 1, x, domain='ZZ')

slice(f, x, m, n=None)

Take a continuous subsequence of terms of f.

sqf_list(f, all=False)

Returns a list of square-free factors of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> f = 2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16

>>> Poly(f).sqf_list()
(2, [(Poly(x + 1, x, domain='ZZ'), 2),
(Poly(x + 2, x, domain='ZZ'), 3)])

>>> Poly(f).sqf_list(all=True)
(2, [(Poly(1, x, domain='ZZ'), 1),
(Poly(x + 1, x, domain='ZZ'), 2),
(Poly(x + 2, x, domain='ZZ'), 3)])

sqf_list_include(f, all=False)

Returns a list of square-free factors of f.

Examples

>>> from sympy import Poly, expand
>>> from sympy.abc import x

>>> f = expand(2*(x + 1)**3*x**4)
>>> f
2*x**7 + 6*x**6 + 6*x**5 + 2*x**4

>>> Poly(f).sqf_list_include()
[(Poly(2, x, domain='ZZ'), 1),
(Poly(x + 1, x, domain='ZZ'), 3),
(Poly(x, x, domain='ZZ'), 4)]

>>> Poly(f).sqf_list_include(all=True)
[(Poly(2, x, domain='ZZ'), 1),
(Poly(1, x, domain='ZZ'), 2),
(Poly(x + 1, x, domain='ZZ'), 3),
(Poly(x, x, domain='ZZ'), 4)]

sqf_norm(f)

Computes square-free norm of f.

Returns s, f, r, such that g(x) = f(x-sa) and r(x) = Norm(g(x)) is a square-free polynomial over K, where a is the algebraic extension of the ground domain.

Examples

>>> from sympy import Poly, sqrt
>>> from sympy.abc import x

>>> s, f, r = Poly(x**2 + 1, x, extension=[sqrt(3)]).sqf_norm()

>>> s
1
>>> f
Poly(x**2 - 2*3**(1/2)*x + 4, x, domain='QQ<3**(1/2)>')
>>> r
Poly(x**4 - 4*x**2 + 16, x, domain='QQ')

sqf_part(f)

Computes square-free part of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**3 - 3*x - 2, x).sqf_part()
Poly(x**2 - x - 2, x, domain='ZZ')

sqr(f)

Square a polynomial f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x - 2, x).sqr()
Poly(x**2 - 4*x + 4, x, domain='ZZ')

>>> Poly(x - 2, x)**2
Poly(x**2 - 4*x + 4, x, domain='ZZ')

sturm(f, auto=True)

Computes the Sturm sequence of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**3 - 2*x**2 + x - 3, x).sturm()
[Poly(x**3 - 2*x**2 + x - 3, x, domain='QQ'),
Poly(3*x**2 - 4*x + 1, x, domain='QQ'),
Poly(2/9*x + 25/9, x, domain='QQ'),
Poly(-2079/4, x, domain='QQ')]

sub(f, g)

Subtract two polynomials f and g.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 + 1, x).sub(Poly(x - 2, x))
Poly(x**2 - x + 3, x, domain='ZZ')

>>> Poly(x**2 + 1, x) - Poly(x - 2, x)
Poly(x**2 - x + 3, x, domain='ZZ')

sub_ground(f, coeff)

Subtract an element of the ground domain from f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x + 1).sub_ground(2)
Poly(x - 1, x, domain='ZZ')

subresultants(f, g)

Computes the subresultant PRS sequence of f and g.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(x**2 + 1, x).subresultants(Poly(x**2 - 1, x))
[Poly(x**2 + 1, x, domain='ZZ'),
Poly(x**2 - 1, x, domain='ZZ'),
Poly(-2, x, domain='ZZ')]

terms(f, order=None)

Returns all non-zero terms from f in lex order.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x**2 + 2*x*y**2 + x*y + 3*y, x, y).terms()
[((2, 0), 1), ((1, 2), 2), ((1, 1), 1), ((0, 1), 3)]

terms_gcd(f)

Remove GCD of terms from the polynomial f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x**6*y**2 + x**3*y, x, y).terms_gcd()
((3, 1), Poly(x**3*y + 1, x, y, domain='ZZ'))

termwise(f, func, *gens, **args)

Apply a function to all terms of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> def func((k,), coeff):
...     return coeff//10**(2-k)

>>> Poly(x**2 + 20*x + 400).termwise(func)
Poly(x**2 + 2*x + 4, x, domain='ZZ')

to_exact(f)

Make the ground domain exact.

Examples

>>> from sympy import Poly, RR
>>> from sympy.abc import x

>>> Poly(x**2 + 1.0, x, domain=RR).to_exact()
Poly(x**2 + 1, x, domain='QQ')

to_field(f)

Make the ground domain a field.

Examples

>>> from sympy import Poly, ZZ
>>> from sympy.abc import x

>>> Poly(x**2 + 1, x, domain=ZZ).to_field()
Poly(x**2 + 1, x, domain='QQ')

to_ring(f)

Make the ground domain a ring.

Examples

>>> from sympy import Poly, QQ
>>> from sympy.abc import x

>>> Poly(x**2 + 1, domain=QQ).to_ring()
Poly(x**2 + 1, x, domain='ZZ')

total_degree(f)

Returns the total degree of f.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> Poly(x**2 + y*x + 1, x, y).total_degree()
3

trunc(f, p)

Reduce f modulo a constant p.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x

>>> Poly(2*x**3 + 3*x**2 + 5*x + 7, x).trunc(3)
Poly(-x**3 - x + 1, x, domain='ZZ')

unify(f, g)

Make f and g belong to the same domain.

Examples

>>> from sympy import Poly
>>> from sympy.abc import x, y

>>> f, g = Poly(x/2 + 1), Poly(2*x + 1)

>>> f
Poly(1/2*x + 1, x, domain='QQ')
>>> g
Poly(2*x + 1, x, domain='ZZ')

>>> F, G = f.unify(g)

>>> F
Poly(1/2*x + 1, x, domain='QQ')
>>> G
Poly(2*x + 1, x, domain='QQ')

unit

Return unit polynomial with self‘s properties.

zero

Return zero polynomial with self‘s properties.

## Extra polynomial manipulation functions¶

sympy.polys.polyfuncs.symmetrize(F, *gens, **args)

Rewrite a polynomial in terms of elementary symmetric polynomials.

Examples

>>> from sympy.polys.polyfuncs import symmetrize
>>> from sympy.abc import x, y

>>> symmetrize(x**2 + y**2)
(-2*x*y + (x + y)**2, 0)

>>> symmetrize(x**2 + y**2, formal=True)
(s1**2 - 2*s2, 0, [(s1, x + y), (s2, x*y)])

>>> symmetrize(x**2 - y**2)
(-2*x*y + (x + y)**2, -2*y**2)

>>> symmetrize(x**2 - y**2, formal=True)
(s1**2 - 2*s2, -2*y**2, [(s1, x + y), (s2, x*y)])

sympy.polys.polyfuncs.horner(f, *gens, **args)

Rewrite a polynomial in Horner form.

Examples

>>> from sympy.polys.polyfuncs import horner
>>> from sympy.abc import x, y, a, b, c, d, e

>>> horner(9*x**4 + 8*x**3 + 7*x**2 + 6*x + 5)
x*(x*(x*(9*x + 8) + 7) + 6) + 5

>>> horner(a*x**4 + b*x**3 + c*x**2 + d*x + e)
e + x*(d + x*(c + x*(a*x + b)))

>>> f = 4*x**2*y**2 + 2*x**2*y + 2*x*y**2 + x*y

>>> horner(f, wrt=x)
x*(x*y*(4*y + 2) + y*(2*y + 1))

>>> horner(f, wrt=y)
y*(x*y*(4*x + 2) + x*(2*x + 1))

sympy.polys.polyfuncs.interpolate(data, x)

Construct an interpolating polynomial for the data points.

Examples

>>> from sympy.polys.polyfuncs import interpolate
>>> from sympy.abc import x

>>> interpolate([1, 4, 9, 16], x)
x**2
>>> interpolate([(1, 1), (2, 4), (3, 9)], x)
x**2
>>> interpolate([(1, 2), (2, 5), (3, 10)], x)
x**2 + 1
>>> interpolate({1: 2, 2: 5, 3: 10}, x)
x**2 + 1

sympy.polys.polyfuncs.viete(f, roots=None, *gens, **args)

Generate Viete’s formulas for f.

Examples

>>> from sympy.polys.polyfuncs import viete
>>> from sympy import symbols

>>> x, a, b, c, r1, r2 = symbols('x,a:c,r1:3')

>>> viete(a*x**2 + b*x + c, [r1, r2], x)
[(r1 + r2, -b/a), (r1*r2, c/a)]


## Domain constructors¶

sympy.polys.constructor.construct_domain(obj, **args)

Construct a minimal domain for the list of coefficients.

## Algebraic number fields¶

sympy.polys.numberfields.minimal_polynomial(ex, x=None, **args)

Computes the minimal polynomial of an algebraic number.

Example

>>> from sympy import minimal_polynomial, sqrt
>>> from sympy.abc import x

>>> minimal_polynomial(sqrt(2), x)
x**2 - 2
>>> minimal_polynomial(sqrt(2) + sqrt(3), x)
x**4 - 10*x**2 + 1

sympy.polys.numberfields.primitive_element(extension, x=None, **args)

Construct a common number field for all extensions.

sympy.polys.numberfields.field_isomorphism(a, b, **args)

Construct an isomorphism between two number fields.

sympy.polys.numberfields.to_number_field(extension, theta=None, **args)

Express in the field generated by .

sympy.polys.numberfields.isolate(alg, eps=None, fast=False)

Give a rational isolating interval for an algebraic number.

class sympy.polys.numberfields.AlgebraicNumber

Class for representing algebraic numbers in SymPy.

## Monomials encoded as tuples¶

class sympy.polys.monomialtools.Monomial(*data)

Class representing a monomial, i.e. a product of powers.

sympy.polys.monomialtools.monomials(variables, degree)

Generate a set of monomials of the given total degree or less.

Given a set of variables V and a total degree N generate a set of monomials of degree at most N. The total number of monomials is huge and is given by the following formula:

For example if we would like to generate a dense polynomial of a total degree $N = 50$ in 5 variables, assuming that exponents and all of coefficients are 32-bit long and stored in an array we would need almost 80 GiB of memory! Fortunately most polynomials, that we will encounter, are sparse.

Examples

Consider monomials in variables x and y:

>>> from sympy import monomials
>>> from sympy.abc import x, y

>>> sorted(monomials([x, y], 2))
[1, x, y, x**2, y**2, x*y]

>>> sorted(monomials([x, y], 3))
[1, x, y, x**2, x**3, y**2, y**3, x*y, x*y**2, x**2*y]

sympy.polys.monomialtools.monomial_count(V, N)

Computes the number of monomials.

The number of monomials is given by the following formula:

where N is a total degree and V is a set of variables.

Examples

>>> from sympy import monomials, monomial_count
>>> from sympy.abc import x, y

>>> monomial_count(2, 2)
6

>>> M = monomials([x, y], 2)

>>> sorted(M)
[1, x, y, x**2, y**2, x*y]
>>> len(M)
6


## Formal manipulation of roots of polynomials¶

class sympy.polys.rootoftools.RootOf

Represents k-th root of a univariate polynomial.

class sympy.polys.rootoftools.RootSum

Represents a sum of all roots of a univariate polynomial.

## Symbolic root-finding algorithms¶

sympy.polys.polyroots.roots(f, *gens, **flags)

Computes symbolic roots of a univariate polynomial.

Given a univariate polynomial f with symbolic coefficients (or a list of the polynomial’s coefficients), returns a dictionary with its roots and their multiplicities.

Only roots expressible via radicals will be returned. To get a complete set of roots use RootOf class or numerical methods instead. By default cubic and quartic formulas are used in the algorithm. To disable them because of unreadable output set or respectively.

To get roots from a specific domain set the flag with one of the following specifiers: Z, Q, R, I, C. By default all roots are returned (this is equivalent to setting ).

By default a dictionary is returned giving a compact result in case of multiple roots. However to get a tuple containing all those roots set the flag to True.

Examples

>>> from sympy import Poly, roots
>>> from sympy.abc import x, y

>>> roots(x**2 - 1, x)
{-1: 1, 1: 1}

>>> p = Poly(x**2-1, x)
>>> roots(p)
{-1: 1, 1: 1}

>>> p = Poly(x**2-y, x, y)

>>> roots(Poly(p, x))
{-y**(1/2): 1, y**(1/2): 1}

>>> roots(x**2 - y, x)
{-y**(1/2): 1, y**(1/2): 1}

>>> roots([1, 0, -1])
{-1: 1, 1: 1}


## Special polynomials¶

sympy.polys.specialpolys.swinnerton_dyer_poly(n, x=None, **args)

Generates n-th Swinnerton-Dyer polynomial in .

sympy.polys.specialpolys.interpolating_poly(n, x, X='x', Y='y')

Construct Lagrange interpolating polynomial for n data points.

sympy.polys.specialpolys.cyclotomic_poly(n, x=None, **args)

Generates cyclotomic polynomial of order in .

sympy.polys.specialpolys.symmetric_poly(n, *gens, **args)

Generates symmetric polynomial of order .

sympy.polys.specialpolys.random_poly(x, n, inf, sup, domain=ZZ, polys=False)

Return a polynomial of degree n with coefficients in [inf, sup].

## Orthogonal polynomials¶

sympy.polys.orthopolys.chebyshevt_poly(n, x=None, **args)

Generates Chebyshev polynomial of the first kind of degree in .

sympy.polys.orthopolys.chebyshevu_poly(n, x=None, **args)

Generates Chebyshev polynomial of the second kind of degree in .

sympy.polys.orthopolys.hermite_poly(n, x=None, **args)

Generates Hermite polynomial of degree in .

sympy.polys.orthopolys.legendre_poly(n, x=None, **args)

Generates Legendre polynomial of degree in .

sympy.polys.orthopolys.laguerre_poly(n, x=None, alpha=None, **args)

Generates Laguerre polynomial of degree in .

## Manipulation of rational functions¶

sympy.polys.rationaltools.together(expr, deep=False)

Denest and combine rational expressions using symbolic methods.

This function takes an expression or a container of expressions and puts it (them) together by denesting and combining rational subexpressions. No heroic measures are taken to minimize degree of the resulting numerator and denominator. To obtain completely reduced expression use cancel(). However, together() can preserve as much as possible of the structure of the input expression in the output (no expansion is performed).

A wide variety of objects can be put together including lists, tuples, sets, relational objects, integrals and others. It is also possible to transform interior of function applications, by setting deep flag to True.

By definition, together() is a complement to apart(), so apart(together(expr)) should return expr unchanged. Note however, that together() uses only symbolic methods, so it might be necessary to use cancel() to perform algebraic simplification and minimise degree of the numerator and denominator.

Example

>>> from sympy import together, exp
>>> from sympy.abc import x, y, z

>>> together(1/x + 1/y)
(x + y)/(x*y)
>>> together(1/x + 1/y + 1/z)
(x*y + x*z + y*z)/(x*y*z)

>>> together(1/(x*y) + 1/y**2)
(x + y)/(x*y**2)

>>> together(1/(1 + 1/x) + 1/(1 + 1/y))
(x*(y + 1) + y*(x + 1))/((x + 1)*(y + 1))

>>> together(exp(1/x + 1/y))
exp(1/y + 1/x)
>>> together(exp(1/x + 1/y), deep=True)
exp((x + y)/(x*y))

>>> together(1/exp(x) + 1/(x*exp(x)))
(x + 1)*exp(-x)/x

>>> together(1/exp(2*x) + 1/(x*exp(3*x)))
(x*exp(x) + 1)*exp(-3*x)/x


## Partial fraction decomposition¶

sympy.polys.partfrac.apart(expr, *args, **kwargs)

Compute partial fraction decomposition of a rational function.

Given a rational function f compute partial fraction decomposition of f. Two algorithms are available: one is based on undetermined coefficients method and the other is Bronstein’s full partial fraction decomposition algorithm.

Examples

>>> from sympy.polys.partfrac import apart
>>> from sympy.abc import x, y

>>> apart(y/(x + 2)/(x + 1), x)
-y/(x + 2) + y/(x + 1)