Polynomials Manipulation Module Reference¶
Basic polynomial manipulation functions¶
-
sympy.polys.polytools.poly(expr, *gens, **args)[source]¶ Efficiently transform an expression into a polynomial.
Examples
>>> from sympy import poly >>> from sympy.abc import x
>>> poly(x*(x**2 + x - 1)**2) Poly(x**5 + 2*x**4 - x**3 - 2*x**2 + x, x, domain='ZZ')
-
sympy.polys.polytools.poly_from_expr(expr, *gens, **args)[source]¶ Construct a polynomial from an expression.
-
sympy.polys.polytools.parallel_poly_from_expr(exprs, *gens, **args)[source]¶ Construct polynomials from expressions.
-
sympy.polys.polytools.degree(f, *gens, **args)[source]¶ Return the degree of
fin the given variable.The degree of 0 is negative infinity.
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 >>> degree(0, x) -oo
-
sympy.polys.polytools.degree_list(f, *gens, **args)[source]¶ Return a list of degrees of
fin 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)[source]¶ 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)[source]¶ 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)[source]¶ 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)[source]¶ Compute polynomial pseudo-division of
fandg.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)[source]¶ Compute polynomial pseudo-remainder of
fandg.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)[source]¶ Compute polynomial pseudo-quotient of
fandg.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)[source]¶ Compute polynomial exact pseudo-quotient of
fandg.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)[source]¶ Compute polynomial division of
fandg.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)[source]¶ Compute polynomial remainder of
fandg.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)[source]¶ Compute polynomial quotient of
fandg.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)[source]¶ Compute polynomial exact quotient of
fandg.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)[source]¶ Half extended Euclidean algorithm of
fandg.Returns
(s, h)such thath = gcd(f, g)ands*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)[source]¶ Extended Euclidean algorithm of
fandg.Returns
(s, t, h)such thath = gcd(f, g)ands*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)[source]¶ Invert
fmodulogwhen possible.See also
sympy.core.numbers.mod_inverseExamples
>>> from sympy import invert, S >>> from sympy.core.numbers import mod_inverse >>> 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
For more efficient inversion of Rationals, use the
mod_inversefunction:>>> mod_inverse(3, 5) 2 >>> (S(2)/5).invert(S(7)/3) 5/2
-
sympy.polys.polytools.subresultants(f, g, *gens, **args)[source]¶ Compute subresultant PRS of
fandg.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)[source]¶ Compute resultant of
fandg.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)[source]¶ Compute discriminant of
f.Examples
>>> from sympy import discriminant >>> from sympy.abc import x
>>> discriminant(x**2 + 2*x + 3) -8
-
sympy.polys.dispersion.dispersion(p, q=None, *gens, **args)[source]¶ Compute the dispersion of polynomials.
For two polynomials f(x) and g(x) with \deg f > 0 and \deg g > 0 the dispersion \operatorname{dis}(f, g) is defined as:
\operatorname{dis}(f, g) & := \max\{ J(f,g) \cup \{0\} \} \\ & = \max\{ \{a \in \mathbb{N} | \gcd(f(x), g(x+a)) \neq 1\} \cup \{0\} \}
and for a single polynomial \operatorname{dis}(f) := \operatorname{dis}(f, f). Note that we make the definition \max\{\} := -\infty.
See also
dispersionsetReferences
Examples
>>> from sympy import poly >>> from sympy.polys.dispersion import dispersion, dispersionset >>> from sympy.abc import x
Dispersion set and dispersion of a simple polynomial:
>>> fp = poly((x - 3)*(x + 3), x) >>> sorted(dispersionset(fp)) [0, 6] >>> dispersion(fp) 6
Note that the definition of the dispersion is not symmetric:
>>> fp = poly(x**4 - 3*x**2 + 1, x) >>> gp = fp.shift(-3) >>> sorted(dispersionset(fp, gp)) [2, 3, 4] >>> dispersion(fp, gp) 4 >>> sorted(dispersionset(gp, fp)) [] >>> dispersion(gp, fp) -oo
The maximum of an empty set is defined to be -\infty as seen in this example.
Computing the dispersion also works over field extensions:
>>> from sympy import sqrt >>> fp = poly(x**2 + sqrt(5)*x - 1, x, domain='QQ<sqrt(5)>') >>> gp = poly(x**2 + (2 + sqrt(5))*x + sqrt(5), x, domain='QQ<sqrt(5)>') >>> sorted(dispersionset(fp, gp)) [2] >>> sorted(dispersionset(gp, fp)) [1, 4]
We can even perform the computations for polynomials having symbolic coefficients:
>>> from sympy.abc import a >>> fp = poly(4*x**4 + (4*a + 8)*x**3 + (a**2 + 6*a + 4)*x**2 + (a**2 + 2*a)*x, x) >>> sorted(dispersionset(fp)) [0, 1]
-
sympy.polys.dispersion.dispersionset(p, q=None, *gens, **args)[source]¶ Compute the dispersion set of two polynomials.
For two polynomials f(x) and g(x) with \deg f > 0 and \deg g > 0 the dispersion set \operatorname{J}(f, g) is defined as:
\operatorname{J}(f, g) & := \{a \in \mathbb{N}_0 | \gcd(f(x), g(x+a)) \neq 1\} \\ & = \{a \in \mathbb{N}_0 | \deg \gcd(f(x), g(x+a)) \geq 1\}
For a single polynomial one defines \operatorname{J}(f) := \operatorname{J}(f, f).
See also
dispersionReferences
Examples
>>> from sympy import poly >>> from sympy.polys.dispersion import dispersion, dispersionset >>> from sympy.abc import x
Dispersion set and dispersion of a simple polynomial:
>>> fp = poly((x - 3)*(x + 3), x) >>> sorted(dispersionset(fp)) [0, 6] >>> dispersion(fp) 6
Note that the definition of the dispersion is not symmetric:
>>> fp = poly(x**4 - 3*x**2 + 1, x) >>> gp = fp.shift(-3) >>> sorted(dispersionset(fp, gp)) [2, 3, 4] >>> dispersion(fp, gp) 4 >>> sorted(dispersionset(gp, fp)) [] >>> dispersion(gp, fp) -oo
Computing the dispersion also works over field extensions:
>>> from sympy import sqrt >>> fp = poly(x**2 + sqrt(5)*x - 1, x, domain='QQ<sqrt(5)>') >>> gp = poly(x**2 + (2 + sqrt(5))*x + sqrt(5), x, domain='QQ<sqrt(5)>') >>> sorted(dispersionset(fp, gp)) [2] >>> sorted(dispersionset(gp, fp)) [1, 4]
We can even perform the computations for polynomials having symbolic coefficients:
>>> from sympy.abc import a >>> fp = poly(4*x**4 + (4*a + 8)*x**3 + (a**2 + 6*a + 4)*x**2 + (a**2 + 2*a)*x, x) >>> sorted(dispersionset(fp)) [0, 1]
-
sympy.polys.polytools.terms_gcd(f, *gens, **args)[source]¶ Remove GCD of terms from
f.If the
deepflag is True, then the arguments offwill have terms_gcd applied to them.If a fraction is factored out of
fandfis an Add, then an unevaluated Mul will be returned so that automatic simplification does not redistribute it. The hintclear, when set to False, can be used to prevent such factoring when all coefficients are not fractions.Examples
>>> from sympy import terms_gcd, cos >>> from sympy.abc import x, y >>> terms_gcd(x**6*y**2 + x**3*y, x, y) x**3*y*(x**3*y + 1)
The default action of polys routines is to expand the expression given to them. terms_gcd follows this behavior:
>>> terms_gcd((3+3*x)*(x+x*y)) 3*x*(x*y + x + y + 1)
If this is not desired then the hint
expandcan be set to False. In this case the expression will be treated as though it were comprised of one or more terms:>>> terms_gcd((3+3*x)*(x+x*y), expand=False) (3*x + 3)*(x*y + x)
In order to traverse factors of a Mul or the arguments of other functions, the
deephint can be used:>>> terms_gcd((3 + 3*x)*(x + x*y), expand=False, deep=True) 3*x*(x + 1)*(y + 1) >>> terms_gcd(cos(x + x*y), deep=True) cos(x*(y + 1))
Rationals are factored out by default:
>>> terms_gcd(x + y/2) (2*x + y)/2
Only the y-term had a coefficient that was a fraction; if one does not want to factor out the 1/2 in cases like this, the flag
clearcan be set to False:>>> terms_gcd(x + y/2, clear=False) x + y/2 >>> terms_gcd(x*y/2 + y**2, clear=False) y*(x/2 + y)
The
clearflag is ignored if all coefficients are fractions:>>> terms_gcd(x/3 + y/2, clear=False) (2*x + 3*y)/6
-
sympy.polys.polytools.cofactors(f, g, *gens, **args)[source]¶ Compute GCD and cofactors of
fandg.Returns polynomials
(h, cff, cfg)such thath = gcd(f, g), andcff = quo(f, h)andcfg = quo(g, h)are, so called, cofactors offandg.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)[source]¶ Compute GCD of
fandg.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)[source]¶ 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)[source]¶ Compute LCM of
fandg.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)[source]¶ 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)[source]¶ Reduce
fmodulo a constantp.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)[source]¶ Divide all coefficients of
fbyLC(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)[source]¶ 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)[source]¶ Compute content and the primitive form of
f.Examples
>>> from sympy.polys.polytools import primitive >>> from sympy.abc import x
>>> primitive(6*x**2 + 8*x + 12) (2, 3*x**2 + 4*x + 6)
>>> eq = (2 + 2*x)*x + 2
Expansion is performed by default:
>>> primitive(eq) (2, x**2 + x + 1)
Set
expandto False to shut this off. Note that the extraction will not be recursive; use the as_content_primitive method for recursive, non-destructive Rational extraction.>>> primitive(eq, expand=False) (1, x*(2*x + 2) + 2)
>>> eq.as_content_primitive() (2, x*(x + 1) + 1)
-
sympy.polys.polytools.compose(f, g, *gens, **args)[source]¶ 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)[source]¶ 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)[source]¶ 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)[source]¶ 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
>>> f = x**12 + 6*x**11 - 11*x**10 - 56*x**9 + 220*x**8 + 208*x**7 - 1401*x**6 + 1090*x**5 + 2715*x**4 - 6720*x**3 - 1092*x**2 + 5040*x
>>> gff_list(f) [(x**3 + 7, 2), (x**2 + 5*x, 3)]
>>> ff(x**3 + 7, 2)*ff(x**2 + 5*x, 3) == f True
-
sympy.polys.polytools.sqf_norm(f, *gens, **args)[source]¶ Compute square-free norm of
f.Returns
s,f,r, such thatg(x) = f(x-sa)andr(x) = Norm(g(x))is a square-free polynomial overK, whereais 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*sqrt(3)*x + 4, x**4 - 4*x**2 + 16)
-
sympy.polys.polytools.sqf_part(f, *gens, **args)[source]¶ 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)[source]¶ 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)[source]¶ 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)[source]¶ 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)[source]¶ Compute the factorization of expression,
f, into irreducibles. (To factor an integer into primes, usefactorint.)There two modes implemented: symbolic and formal. If
fis not an instance ofPolyand 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 ofAddis encountered (in this case formal factorization is used). This wayfactor()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,modulusordomain.See also
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 - sqrt(2))*(x + sqrt(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)
By default, factor deals with an expression as a whole:
>>> eq = 2**(x**2 + 2*x + 1) >>> factor(eq) 2**(x**2 + 2*x + 1)
If the
deepflag is True then subexpressions will be factored:>>> factor(eq, deep=True) 2**((x + 1)**2)
-
sympy.polys.polytools.intervals(F, all=False, eps=None, inf=None, sup=None, strict=False, fast=False, sqf=False)[source]¶ 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)[source]¶ 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)[source]¶ Return the number of roots of
fin[inf, sup]interval.If one of
inforsupis complex, it will return the number of roots in the complex rectangle with corners atinfandsup.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)[source]¶ 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, n=15, maxsteps=50, cleanup=True)[source]¶ Compute numerical approximations of roots of
f.Examples
>>> from sympy import nroots >>> from sympy.abc import x
>>> nroots(x**2 - 3, n=15) [-1.73205080756888, 1.73205080756888] >>> nroots(x**2 - 3, n=30) [-1.73205080756887729352744634151, 1.73205080756887729352744634151]
-
sympy.polys.polytools.ground_roots(f, *gens, **args)[source]¶ Compute roots of
fby 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.nth_power_roots_poly(f, n, *gens, **args)[source]¶ Construct a polynomial with n-th powers of roots of
f.Examples
>>> from sympy import nth_power_roots_poly, factor, roots >>> from sympy.abc import x
>>> f = x**4 - x**2 + 1 >>> g = factor(nth_power_roots_poly(f, 2))
>>> g (x**2 - x + 1)**2
>>> R_f = [ (r**2).expand() for r in roots(f) ] >>> R_g = roots(g).keys()
>>> set(R_f) == set(R_g) True
-
sympy.polys.polytools.cancel(f, *gens, **args)[source]¶ Cancel common factors in a rational function
f.Examples
>>> from sympy import cancel, sqrt, Symbol >>> from sympy.abc import x >>> A = Symbol('A', commutative=False)
>>> cancel((2*x**2 - 2)/(x**2 - 2*x + 1)) (2*x + 2)/(x - 1) >>> cancel((sqrt(3) + sqrt(15)*A)/(sqrt(2) + sqrt(10)*A)) sqrt(6)/2
-
sympy.polys.polytools.reduced(f, G, *gens, **args)[source]¶ Reduces a polynomial
fmodulo a set of polynomialsG.Given a polynomial
fand a set of polynomialsG = (g_1, ..., g_n), computes a set of quotientsq = (q_1, ..., q_n)and the remainderrsuch thatf = q_1*g_1 + ... + q_n*g_n + r, wherervanishes orris a completely reduced polynomial with respect toG.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)[source]¶ Computes the reduced Groebner basis for a set of polynomials.
Use the
orderargument to set the monomial ordering that will be used to compute the basis. Allowed orders arelex,grlexandgrevlex. If no order is specified, it defaults tolex.For more information on Groebner bases, see the references and the docstring of solve_poly_system().
References
Examples
Example taken from [1].
>>> from sympy import groebner >>> from sympy.abc import x, y
>>> F = [x*y - 2*y, 2*y**2 - x**2]
>>> groebner(F, x, y, order='lex') GroebnerBasis([x**2 - 2*y**2, x*y - 2*y, y**3 - 2*y], x, y, domain='ZZ', order='lex') >>> groebner(F, x, y, order='grlex') GroebnerBasis([y**3 - 2*y, x**2 - 2*y**2, x*y - 2*y], x, y, domain='ZZ', order='grlex') >>> groebner(F, x, y, order='grevlex') GroebnerBasis([y**3 - 2*y, x**2 - 2*y**2, x*y - 2*y], x, y, domain='ZZ', order='grevlex')
By default, an improved implementation of the Buchberger algorithm is used. Optionally, an implementation of the F5B algorithm can be used. The algorithm can be set using
methodflag or with thesetup()function fromsympy.polys.polyconfig:>>> F = [x**2 - x - 1, (2*x - 1) * y - (x**10 - (1 - x)**10)]
>>> groebner(F, x, y, method='buchberger') GroebnerBasis([x**2 - x - 1, y - 55], x, y, domain='ZZ', order='lex') >>> groebner(F, x, y, method='f5b') GroebnerBasis([x**2 - x - 1, y - 55], x, y, domain='ZZ', order='lex')
-
sympy.polys.polytools.is_zero_dimensional(F, *gens, **args)[source]¶ Checks if the ideal generated by a Groebner basis is zero-dimensional.
The algorithm checks if the set of monomials not divisible by the leading monomial of any element of
Fis bounded.References
David A. Cox, John B. Little, Donal O’Shea. Ideals, Varieties and Algorithms, 3rd edition, p. 230
-
class
sympy.polys.polytools.Poly[source]¶ Generic class for representing polynomial expressions.
-
EC(f, order=None)[source]¶ 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)[source]¶ 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() x**0*y**1
-
ET(f, order=None)[source]¶ 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() (x**0*y**1, 3)
-
LC(f, order=None)[source]¶ 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)[source]¶ 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() x**2*y**0
-
LT(f, order=None)[source]¶ 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() (x**2*y**0, 4)
-
TC(f)[source]¶ Returns the trailing coefficient 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)[source]¶ Make all coefficients in
fpositive.Examples
>>> from sympy import Poly >>> from sympy.abc import x
>>> Poly(x**2 - 1, x).abs() Poly(x**2 + 1, x, domain='ZZ')
-
add(f, g)[source]¶ Add two polynomials
fandg.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_ground(f, coeff)[source]¶ 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')
-