多项式操作模块参考#

多项式操作算法和代数对象。

See 多项式操作 for an index of documentation for the polys module and 模块的基本功能 for an introductory explanation.

基本多项式操作函数#

sympy.polys.polytools.poly(expr, *gens, **args)[源代码]#

有效地将表达式转换为多项式。

实例

>>> 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)[源代码]#

从表达式构造多项式。

sympy.polys.polytools.parallel_poly_from_expr(exprs, *gens, **args)[源代码]#

从表达式构造多项式。

sympy.polys.polytools.degree(f, gen=0)[源代码]#

返回度 f 在给定变量中。

0的次数是负无穷大。

实例

>>> 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)[源代码]#

返回的度数列表 f 在所有变量中。

实例

>>> 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)[源代码]#

返回前导系数 f .

实例

>>> 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)[源代码]#

返回的前导单项式 f .

实例

>>> 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)[源代码]#

返回的前导项 f .

实例

>>> 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)[源代码]#

计算多项式伪除法 fg .

实例

>>> 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)[源代码]#

计算多项式伪余数 fg .

实例

>>> 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)[源代码]#

计算多项式伪商 fg .

实例

>>> 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)[源代码]#

计算多项式精确伪商 fg .

实例

>>> 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)[源代码]#

计算多项式除法 fg .

实例

>>> 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)[源代码]#

计算多项式余数 fg .

实例

>>> 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)[源代码]#

计算多项式商 fg .

实例

>>> 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)[源代码]#

计算多项式精确商 fg .

实例

>>> 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)[源代码]#

半扩展欧几里德算法 fg .

返回 (s, h) 这样的话 h = gcd(f, g)s*f = h (mod g) .

实例

>>> 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)
(3/5 - x/5, x + 1)
sympy.polys.polytools.gcdex(f, g, *gens, **args)[源代码]#

扩展欧几里德算法 fg .

返回 (s, t, h) 这样的话 h = gcd(f, g)s*f + t*g = h .

实例

>>> 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)
(3/5 - x/5, x**2/5 - 6*x/5 + 2, x + 1)
sympy.polys.polytools.invert(f, g, *gens, **args)[源代码]#

使转化 fg 如果可能的话。

实例

>>> from sympy import invert, S, 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 sympy.core.intfunc.mod_inverse function:

>>> mod_inverse(3, 5)
2
>>> (S(2)/5).invert(S(7)/3)
5/2
sympy.polys.polytools.subresultants(f, g, *gens, **args)[源代码]#

计算 fg .

实例

>>> 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, includePRS=False, **args)[源代码]#

计算的结果 fg .

实例

>>> from sympy import resultant
>>> from sympy.abc import x
>>> resultant(x**2 + 1, x**2 - 1)
4
sympy.polys.polytools.discriminant(f, *gens, **args)[源代码]#

计算判别式 f .

实例

>>> from sympy import discriminant
>>> from sympy.abc import x
>>> discriminant(x**2 + 2*x + 3)
-8
sympy.polys.polytools.terms_gcd(f, *gens, **args)[源代码]#

从中删除术语的GCD f .

如果 deep flag为True,则 f 将有条款适用于他们。

如果将分数计算出来 ff 是一个Add,那么将返回一个未赋值的Mul,以便自动简化不会重新分发它。暗示 clear ,当设置为False时,可用于在所有系数都不是分数时阻止此类因子分解。

实例

>>> 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)

polys例程的默认操作是展开给定的表达式。术语u gcd遵循以下行为:

>>> terms_gcd((3+3*x)*(x+x*y))
3*x*(x*y + x + y + 1)

如果不希望这样,则提示 expand 可以设置为False。在这种情况下,表达式将被视为由一个或多个术语组成:

>>> terms_gcd((3+3*x)*(x+x*y), expand=False)
(3*x + 3)*(x*y + x)

为了遍历Mul的因子或其他函数的参数 deep 可以使用提示:

>>> 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))

默认情况下,理性因素被排除在外:

>>> terms_gcd(x + y/2)
(2*x + y)/2

只有y项的系数是一个分数;如果在这种情况下,不想将1/2的因子去掉,则标志 clear 可以设置为False:

>>> terms_gcd(x + y/2, clear=False)
x + y/2
>>> terms_gcd(x*y/2 + y**2, clear=False)
y*(x/2 + y)

这个 clear 如果所有系数都是分数,则忽略标志:

>>> terms_gcd(x/3 + y/2, clear=False)
(2*x + 3*y)/6
sympy.polys.polytools.cofactors(f, g, *gens, **args)[源代码]#

计算的GCD和余因子 fg .

返回多项式 (h, cff, cfg) 这样的话 h = gcd(f, g)cff = quo(f, h)cfg = quo(g, h) 所谓的,是 fg .

实例

>>> 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)[源代码]#

计算GCD fg .

实例

>>> 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)[源代码]#

计算多项式列表的GCD。

实例

>>> 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)[源代码]#

计算LCM fg .

实例

>>> 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)[源代码]#

计算多项式列表的LCM。

实例

>>> 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)[源代码]#

减少 f 模a常数 p .

实例

>>> 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)[源代码]#

除以 f 通过 LC(f) .

实例

>>> 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)[源代码]#

计算 f .

实例

>>> from sympy import content
>>> from sympy.abc import x
>>> content(6*x**2 + 8*x + 12)
2
sympy.polys.polytools.primitive(f, *gens, **args)[源代码]#

计算内容和原始形式 f .

实例

>>> 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

默认情况下展开:

>>> primitive(eq)
(2, x**2 + x + 1)

集合 expand 把这个关掉。请注意,提取不会是递归的;使用as_content_原语方法进行递归的、非破坏性的Rational提取。

>>> 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)[源代码]#

计算功能组合 f(g) .

实例

>>> 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)[源代码]#

计算函数分解 f .

实例

>>> 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)[源代码]#

计算的Sturm序列 f .

实例

>>> 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)[源代码]#

计算 f .

注意,ff()和rf()的输入应该是Poly实例,以使用这里的定义。

实例

>>> from sympy import gff_list, ff, Poly
>>> from sympy.abc import x
>>> f = Poly(x**5 + 2*x**4 - x**3 - 2*x**2, x)
>>> gff_list(f)
[(Poly(x, x, domain='ZZ'), 1), (Poly(x + 2, x, domain='ZZ'), 4)]
>>> (ff(Poly(x), 1)*ff(Poly(x + 2), 4)) == f
True
>>> f = Poly(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, x)
>>> gff_list(f)
[(Poly(x**3 + 7, x, domain='ZZ'), 2), (Poly(x**2 + 5*x, x, domain='ZZ'), 3)]
>>> ff(Poly(x**3 + 7, x), 2)*ff(Poly(x**2 + 5*x, x), 3) == f
True
sympy.polys.polytools.gff(f, *gens, **args)[源代码]#

计算最大因式分解 f .

sympy.polys.polytools.sqf_norm(f, *gens, **args)[源代码]#

计算平方自由范数 f .

返回 sfr ,这样 g(x) = f(x-sa)r(x) = Norm(g(x)) 一个平方自由多项式 K 在哪里 a 是基域的代数扩张。

实例

>>> 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)[源代码]#

计算平方自由部分 f .

实例

>>> 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)[源代码]#

计算平方自由因子的列表 f .

实例

>>> 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)[源代码]#

计算的平方无因式分解 f .

实例

>>> 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)[源代码]#

计算不可约因子的列表 f .

实例

>>> 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, deep=False, **args)[源代码]#

计算表达式的因式分解, f ,变成不可还原的。(要将整数分解为素数,请使用 factorint

实现了两种模式:符号化和形式化。如果 f 不是的实例 Poly 如果没有指定生成器,则使用前一种模式。否则,使用形式模式。

在符号模式下, factor() 将遍历表达式树并对其组件进行因子化,而不进行任何事先的扩展,除非 Add 遇到(在本例中使用形式分解)。这种方式 factor() 可以处理大指数或符号指数。

默认情况下,因式分解是在有理数上计算的。要对其他域(如代数域或有限域)进行因子分析,请使用适当的选项: extensionmodulusdomain .

实例

>>> from sympy import factor, sqrt, exp
>>> 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)

默认情况下,factor将表达式作为一个整体处理:

>>> eq = 2**(x**2 + 2*x + 1)
>>> factor(eq)
2**(x**2 + 2*x + 1)

如果 deep 标志为True,则子表达式将被分解:

>>> factor(eq, deep=True)
2**((x + 1)**2)

If the fraction flag is False then rational expressions will not be combined. By default it is True.

>>> factor(5*x + 3*exp(2 - 7*x), deep=True)
(5*x*exp(7*x) + 3*exp(2))*exp(-7*x)
>>> factor(5*x + 3*exp(2 - 7*x), deep=True, fraction=False)
5*x + 3*exp(2)*exp(-7*x)
sympy.polys.polytools.intervals(F, all=False, eps=None, inf=None, sup=None, strict=False, fast=False, sqf=False)[源代码]#

计算根的隔离间隔 f .

实例

>>> 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)[源代码]#

将根的隔离间隔细化到给定的精度。

实例

>>> 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)[源代码]#

返回的根数 f 在里面 [inf, sup] 间隔。

如果其中之一 infsup 是复杂的,它将返回角为的复杂矩形的根数 infsup .

实例

>>> 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.all_roots(f, multiple=True, radicals=True)[源代码]#

Returns the real and complex roots of f with multiplicities.

参数:

f : Expr or Poly

A univariate polynomial with rational (or Float) coefficients.

multiple : bool (default True).

Whether to return a list of roots or a list of root/multiplicity pairs.

radicals : bool (default True)

Use simple radical formulae rather than ComplexRootOf for some irrational roots.

返回:

A list of Expr (usually ComplexRootOf) representing

the roots is returned with each root repeated according to its multiplicity

as a root of f. The roots are always uniquely ordered with real roots

coming before complex roots. The real roots are in increasing order.

Complex roots are ordered by increasing real part and then increasing

imaginary part.

If multiple=False is passed then a list of root/multiplicity pairs is

returned instead.

If radicals=False is passed then all roots will be represented as

either rational numbers or ComplexRootOf.

解释

Finds all real and complex roots of a univariate polynomial with rational coefficients of any degree exactly. The roots are represented in the form given by rootof(). This is equivalent to using rootof() to find each of the indexed roots.

实例

>>> from sympy import all_roots
>>> from sympy.abc import x, y
>>> print(all_roots(x**3 + 1))
[-1, 1/2 - sqrt(3)*I/2, 1/2 + sqrt(3)*I/2]

Simple radical formulae are used in some cases but the cubic and quartic formulae are avoided. Instead most non-rational roots will be represented as ComplexRootOf:

>>> print(all_roots(x**3 + x + 1))
[CRootOf(x**3 + x + 1, 0), CRootOf(x**3 + x + 1, 1), CRootOf(x**3 + x + 1, 2)]

All roots of any polynomial with rational coefficients of any degree can be represented using ComplexRootOf. The use of ComplexRootOf bypasses limitations on the availability of radical formulae for quintic and higher degree polynomials _[1]:

>>> p = x**5 - x - 1
>>> for r in all_roots(p): print(r)
CRootOf(x**5 - x - 1, 0)
CRootOf(x**5 - x - 1, 1)
CRootOf(x**5 - x - 1, 2)
CRootOf(x**5 - x - 1, 3)
CRootOf(x**5 - x - 1, 4)
>>> [r.evalf(3) for r in all_roots(p)]
[1.17, -0.765 - 0.352*I, -0.765 + 0.352*I, 0.181 - 1.08*I, 0.181 + 1.08*I]

Irrational algebraic or transcendental coefficients cannot currently be handled by all_roots() (or rootof() more generally):

>>> from sympy import sqrt, expand
>>> p = expand((x - sqrt(2))*(x - sqrt(3)))
>>> print(p)
x**2 - sqrt(3)*x - sqrt(2)*x + sqrt(6)
>>> all_roots(p)
Traceback (most recent call last):
...
NotImplementedError: sorted roots not supported over EX

In the case of algebraic or transcendental coefficients ground_roots() might be able to find some roots by factorisation:

>>> from sympy import ground_roots
>>> ground_roots(p, x, extension=True)
{sqrt(2): 1, sqrt(3): 1}

If the coefficients are numeric then nroots() can be used to find all roots approximately:

>>> from sympy import nroots
>>> nroots(p, 5)
[1.4142, 1.732]

If the coefficients are symbolic then sympy.polys.polyroots.roots() or ground_roots() should be used instead:

>>> from sympy import roots, ground_roots
>>> p = x**2 - 3*x*y + 2*y**2
>>> roots(p, x)
{y: 1, 2*y: 1}
>>> ground_roots(p, x)
{y: 1, 2*y: 1}

参见

Poly.all_roots

The underlying Poly method used by all_roots().

rootof

Compute a single numbered root of a univariate polynomial.

real_roots

Compute all the real roots using rootof().

ground_roots

Compute some roots in the ground domain by factorisation.

nroots

Compute all roots using approximate numerical techniques.

sympy.polys.polyroots.roots

Compute symbolic expressions for roots using radical formulae.

工具书类

sympy.polys.polytools.real_roots(f, multiple=True, radicals=True)[源代码]#

Returns the real roots of f with multiplicities.

参数:

f : Expr or Poly

A univariate polynomial with rational (or Float) coefficients.

multiple : bool (default True).

Whether to return a list of roots or a list of root/multiplicity pairs.

radicals : bool (default True)

Use simple radical formulae rather than ComplexRootOf for some irrational roots.

返回:

A list of Expr (usually ComplexRootOf) representing

the real roots is returned. The roots are arranged in increasing order and

are repeated according to their multiplicities as roots of f.

If multiple=False is passed then a list of root/multiplicity pairs is

returned instead.

If radicals=False is passed then all roots will be represented as

either rational numbers or ComplexRootOf.

解释

Finds all real roots of a univariate polynomial with rational coefficients of any degree exactly. The roots are represented in the form given by rootof(). This is equivalent to using rootof() or all_roots() and filtering out only the real roots. However if only the real roots are needed then real_roots() is more efficient than all_roots() because it computes only the real roots and avoids costly complex root isolation routines.

实例

>>> from sympy import real_roots
>>> from sympy.abc import x, y
>>> real_roots(2*x**3 - 7*x**2 + 4*x + 4)
[-1/2, 2, 2]
>>> real_roots(2*x**3 - 7*x**2 + 4*x + 4, multiple=False)
[(-1/2, 1), (2, 2)]

Real roots of any polynomial with rational coefficients of any degree can be represented using ComplexRootOf:

>>> p = x**9 + 2*x + 2
>>> print(real_roots(p))
[CRootOf(x**9 + 2*x + 2, 0)]
>>> [r.evalf(3) for r in real_roots(p)]
[-0.865]

All rational roots will be returned as rational numbers. Roots of some simple factors will be expressed using radical or other formulae (unless radicals=False is passed). All other roots will be expressed as ComplexRootOf.

>>> p = (x + 7)*(x**2 - 2)*(x**3 + x + 1)
>>> print(real_roots(p))
[-7, -sqrt(2), CRootOf(x**3 + x + 1, 0), sqrt(2)]
>>> print(real_roots(p, radicals=False))
[-7, CRootOf(x**2 - 2, 0), CRootOf(x**3 + x + 1, 0), CRootOf(x**2 - 2, 1)]

All returned root expressions will numerically evaluate to real numbers with no imaginary part. This is in contrast to the expressions generated by the cubic or quartic formulae as used by roots() which suffer from casus irreducibilis [R809]:

>>> from sympy import roots
>>> p = 2*x**3 - 9*x**2 - 6*x + 3
>>> [r.evalf(5) for r in roots(p, multiple=True)]
[5.0365 - 0.e-11*I, 0.33984 + 0.e-13*I, -0.87636 + 0.e-10*I]
>>> [r.evalf(5) for r in real_roots(p, x)]
[-0.87636, 0.33984, 5.0365]
>>> [r.is_real for r in roots(p, multiple=True)]
[None, None, None]
>>> [r.is_real for r in real_roots(p)]
[True, True, True]

Using real_roots() is equivalent to using all_roots() (or rootof()) and filtering out only the real roots:

>>> from sympy import all_roots
>>> r = [r for r in all_roots(p) if r.is_real]
>>> real_roots(p) == r
True

If only the real roots are wanted then using real_roots() is faster than using all_roots(). Using real_roots() avoids complex root isolation which can be a lot slower than real root isolation especially for polynomials of high degree which typically have many more complex roots than real roots.

Irrational algebraic or transcendental coefficients cannot be handled by real_roots() (or rootof() more generally):

>>> from sympy import sqrt, expand
>>> p = expand((x - sqrt(2))*(x - sqrt(3)))
>>> print(p)
x**2 - sqrt(3)*x - sqrt(2)*x + sqrt(6)
>>> real_roots(p)
Traceback (most recent call last):
...
NotImplementedError: sorted roots not supported over EX

In the case of algebraic or transcendental coefficients ground_roots() might be able to find some roots by factorisation:

>>> from sympy import ground_roots
>>> ground_roots(p, x, extension=True)
{sqrt(2): 1, sqrt(3): 1}

If the coefficients are numeric then nroots() can be used to find all roots approximately:

>>> from sympy import nroots
>>> nroots(p, 5)
[1.4142, 1.732]

If the coefficients are symbolic then sympy.polys.polyroots.roots() or ground_roots() should be used instead.

>>> from sympy import roots, ground_roots
>>> p = x**2 - 3*x*y + 2*y**2
>>> roots(p, x)
{y: 1, 2*y: 1}
>>> ground_roots(p, x)
{y: 1, 2*y: 1}

参见

Poly.real_roots

The underlying Poly method used by real_roots().

rootof

Compute a single numbered root of a univariate polynomial.

all_roots

Compute all real and non-real roots using rootof().

ground_roots

Compute some roots in the ground domain by factorisation.

nroots

Compute all roots using approximate numerical techniques.

sympy.polys.polyroots.roots

Compute symbolic expressions for roots using radical formulae.

工具书类

sympy.polys.polytools.nroots(f, n=15, maxsteps=50, cleanup=True)[源代码]#

计算 f .

实例

>>> 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)[源代码]#

计算的根 f 通过在基域分解。

实例

>>> 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)[源代码]#

构造根的n次方多项式 f .

实例

>>> 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, _signsimp=True, **args)[源代码]#

消去有理函数中的公因式 f .

实例

>>> from sympy import cancel, sqrt, Symbol, together
>>> 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

注意:由于有理数的自动分布,一个和除以一个整数将显示为和。恢复合理的表单使用 \(together\) 结果是:

>>> cancel(x/2 + 1)
x/2 + 1
>>> together(_)
(x + 2)/2
sympy.polys.polytools.reduced(f, G, *gens, **args)[源代码]#

减少多项式 f 多项式的模 G .

给定多项式 f 以及一组多项式 G = (g_1, ..., g_n) ,计算一组商 q = (q_1, ..., q_n) 剩下的呢 r 这样的话 f = q_1*g_1 + ... + q_n*g_n + r 在哪里 r 消失或 r 是关于 G .

实例

>>> 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)[源代码]#

计算一组多项式的约化Groebner基。

使用 order 参数设置将用于计算基的单项式顺序。允许的订单是 lexgrlexgrevlex . 如果未指定顺序,则默认为 lex .

有关Groebner基础的更多信息,请参阅的参考资料和docstring solve_poly_system() .

实例

示例取自 [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')

默认情况下,将使用改进的Buchberger算法实现。或者,可以使用F5B算法的实现。可以使用 method 标志或 sympy.polys.polyconfig.setup() 功能。

>>> 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')

工具书类

  1. [Buchberger01]

  2. [Cox97]

sympy.polys.polytools.is_zero_dimensional(F, *gens, **args)[源代码]#

检查由Groebner基生成的理想是否为零维。

该算法检查单项式集是否不能被任何元素的前导单项式整除 F 是有界的。

工具书类

大卫·A·考克斯,约翰·B·利特尔,多纳尔·奥谢。理想,多样性和算法,第三版,第230页

class sympy.polys.polytools.Poly(rep, *gens, **args)[源代码]#

用于表示和操作多项式表达式的泛型类。

See 多项式操作 for general documentation.

Poly is a subclass of Basic rather than Expr but instances can be converted to Expr with the as_expr() method.

自 1.6 版本弃用: Combining Poly with non-Poly objects in binary operations is deprecated. Explicitly convert both objects to either Poly or Expr first. See Mixing Poly and non-polynomial expressions in binary operations.

实例

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

创建一元多项式:

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

创建具有特定域的一元多项式:

>>> from sympy import sqrt
>>> Poly(x**2 + 2*x + sqrt(3), domain='R')
Poly(1.0*x**2 + 2.0*x + 1.73205080756888, x, domain='RR')

创建多元多项式:

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

创建一元多项式,其中y是常数:

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

可以将上述多项式求值为y的函数:

>>> Poly(y*x**2 + x*y + 1,x).eval(2)
6*y + 1
EC(order=None)[源代码]#

返回的最后一个非零系数 f .

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(x**3 + 2*x**2 + 3*x, x).EC()
3
EM(order=None)[源代码]#

返回的最后一个非零单项式 f .

实例

>>> 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(order=None)[源代码]#

返回的最后一个非零项 f .

实例

>>> 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(order=None)[源代码]#

返回的前导系数 f .

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(4*x**3 + 2*x**2 + 3*x, x).LC()
4
LM(order=None)[源代码]#

返回的前导单项式 f .

前导单项式表示在表达式f中主生成元的幂最高的单项式。

实例

>>> 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(order=None)[源代码]#

返回的前导项 f .

前导项表示表达式f中主生成元的最大幂及其系数。

实例

>>> 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 .

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(x**3 + 2*x**2 + 3*x, x).TC()
0
abs()[源代码]#

使所有系数 f 肯定的。

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(x**2 - 1, x).abs()
Poly(x**2 + 1, x, domain='ZZ')
add(g)[源代码]#

加两个多项式 fg .

实例

>>> 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(coeff)[源代码]#

将一个基域元素添加到 f .

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(x + 1).add_ground(2)
Poly(x + 3, x, domain='ZZ')
all_coeffs()[源代码]#

返回一元多项式的所有系数 f .

实例

>>> 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 .

实例

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

参见

all_terms

all_roots(multiple=True, radicals=True)[源代码]#

返回实数和复数根的列表。

See all_roots() for more explanation.

实例

>>> 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()
[CRootOf(x**3 + x + 1, 0),
 CRootOf(x**3 + x + 1, 1),
 CRootOf(x**3 + x + 1, 2)]
all_terms()[源代码]#

返回一元多项式中的所有项 f .

实例

>>> 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)]
as_dict(native=False, zero=False)[源代码]#

切换到a dict 表示。

实例

>>> 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(*gens)[源代码]#

将多边形实例转换为表达式实例。

实例

>>> 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(native=False)[源代码]#

切换到a list 表示。

as_poly(*gens, **args)[源代码]#

皈依者 self 多项式或返回 None .

>>> from sympy import sin
>>> from sympy.abc import x, y
>>> print((x**2 + x*y).as_poly())
Poly(x**2 + x*y, x, y, domain='ZZ')
>>> print((x**2 + x*y).as_poly(x, y))
Poly(x**2 + x*y, x, y, domain='ZZ')
>>> print((x**2 + sin(y)).as_poly(x, y))
None
cancel(g, include=False)[源代码]#

消去有理函数中的公因式 f/g .

实例

>>> 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(convert=False)[源代码]#

清除分母,但保留基域。

实例

>>> 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'))
coeff_monomial(monom)[源代码]#

返回的系数 monom 在里面 f 如果有,就没有了。

实例

>>> from sympy import Poly, exp
>>> from sympy.abc import x, y
>>> p = Poly(24*x*y*exp(8) + 23*x, x, y)
>>> p.coeff_monomial(x)
23
>>> p.coeff_monomial(y)
0
>>> p.coeff_monomial(x*y)
24*exp(8)

注意 Expr.coeff() 行为不同,收集术语(如果可能);多边形必须转换为表达式才能使用该方法,但是:

>>> p.as_expr().coeff(x)
24*y*exp(8) + 23
>>> p.as_expr().coeff(y)
24*x*exp(8)
>>> p.as_expr().coeff(x*y)
24*exp(8)

参见

nth

利用单项式生成元的指数进行更有效的查询

coeffs(order=None)[源代码]#

返回所有非零系数 f 以法律程序。

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(x**3 + 2*x + 3, x).coeffs()
[1, 2, 3]
cofactors(g)[源代码]#

返回的GCD fg 以及它们的辅助因子。

返回多项式 (h, cff, cfg) 这样的话 h = gcd(f, g)cff = quo(f, h)cfg = quo(g, h) 所谓的,是 fg .

实例

>>> 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(g)[源代码]#

计算 fg .

实例

>>> 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()[源代码]#

返回多项式系数的GCD。

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(6*x**2 + 8*x + 12, x).content()
2
count_roots(inf=None, sup=None)[源代码]#

返回的根数 f 在里面 [inf, sup] 间隔。

实例

>>> 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 .

实例

>>> 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 通过映射 x_i**my_i .

实例

>>> 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(gen=0)[源代码]#

返回度 f 在里面 x_j .

0的次数是负无穷大。

实例

>>> 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
>>> Poly(0, x).degree()
-oo
degree_list()[源代码]#

返回的度数列表 f .

实例

>>> from sympy import Poly
>>> from sympy.abc import x, y
>>> Poly(x**2 + y*x + 1, x, y).degree_list()
(2, 1)
diff(*specs, **kwargs)[源代码]#

计算的偏导数 f .

实例

>>> 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 .

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(x**2 + 2*x + 3, x).discriminant()
-8
dispersion(g=None)[源代码]#

计算 分散 多项式。

对于两个多项式 \(f(x)\)\(g(x)\) 具有 \(\deg f > 0\)\(\deg g > 0\) 分散 \(\operatorname{{dis}}(f, g)\) 定义为:

\[\运算符名称{dis}(f,g)\]

对于一个多项式 \(\operatorname{{dis}}(f) := \operatorname{{dis}}(f, f)\) .

实例

>>> from sympy import poly
>>> from sympy.polys.dispersion import dispersion, dispersionset
>>> from sympy.abc import x

一个简单多项式的色散集和色散:

>>> fp = poly((x - 3)*(x + 3), x)
>>> sorted(dispersionset(fp))
[0, 6]
>>> dispersion(fp)
6

注意,色散的定义不是对称的:

>>> 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

计算色散也适用于场扩展:

>>> 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]

我们甚至可以计算具有符号系数的多项式:

>>> 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]

参见

dispersionset

工具书类

  1. [ManWright94]

  2. [Koepf98]

  3. [Abramov71]

  4. [Man93]

dispersionset(g=None)[源代码]#

计算 分散装置 两个多项式的。

对于两个多项式 \(f(x)\)\(g(x)\) 具有 \(\deg f > 0\)\(\deg g > 0\) 色散集 \(\operatorname{{J}}(f, g)\) 定义为:

\[\{operator名称(f)\]

对于一个多项式 \(\operatorname{{J}}(f) := \operatorname{{J}}(f, f)\) .

实例

>>> from sympy import poly
>>> from sympy.polys.dispersion import dispersion, dispersionset
>>> from sympy.abc import x

一个简单多项式的色散集和色散:

>>> fp = poly((x - 3)*(x + 3), x)
>>> sorted(dispersionset(fp))
[0, 6]
>>> dispersion(fp)
6

注意,色散的定义不是对称的:

>>> 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

计算色散也适用于场扩展:

>>> 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]

我们甚至可以计算具有符号系数的多项式:

>>> 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]

参见

dispersion

工具书类

  1. [ManWright94]

  2. [Koepf98]

  3. [Abramov71]

  4. [Man93]

div(g, auto=True)[源代码]#

余数多项式除法 f 通过 g .

实例

>>> 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'))
property domain#

Get the ground domain of a Poly

返回:

Domain:

Ground domain of the Poly.

实例

>>> from sympy import Poly, Symbol
>>> x = Symbol('x')
>>> p = Poly(x**2 + x)
>>> p
Poly(x**2 + x, x, domain='ZZ')
>>> p.domain
ZZ
eject(*gens)[源代码]#

将选定的发电机发射到地面区域。

实例

>>> 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(x, a=None, auto=True)[源代码]#

评估 fa 在给定变量中。

实例

>>> 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
>>> f.eval((2, 5))
Poly(2*z + 31, z, domain='ZZ')
>>> f(2, 5)
Poly(2*z + 31, z, domain='ZZ')
exclude()[源代码]#

移除不必要的发电机 f .

实例

>>> 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(g, auto=True)[源代码]#

计算多项式精确商 f 通过 g .

实例

>>> 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(coeff)[源代码]#

精确商 f 通过一个基域的元素。

实例

>>> 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 .

实例

>>> 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 .

实例

>>> 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)]
property free_symbols#

多项式表达式的自由符号。

实例

>>> from sympy import Poly
>>> from sympy.abc import x, y, z
>>> Poly(x**2 + 1).free_symbols
{x}
>>> Poly(x**2 + y).free_symbols
{x, y}
>>> Poly(x**2 + y, x).free_symbols
{x, y}
>>> Poly(x**2 + y, x, z).free_symbols
{x, y}
property free_symbols_in_domain#

领域的自由符号 self .

实例

>>> 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
{y}
classmethod from_dict(rep, *gens, **args)[源代码]#

dict .

classmethod from_expr(rep, *gens, **args)[源代码]#

从表达式构造多项式。

classmethod from_list(rep, *gens, **args)[源代码]#

list .

classmethod from_poly(rep, *gens, **args)[源代码]#

从多项式构造多项式。

galois_group(by_name=False, max_tries=30, randomize=False)[源代码]#

Compute the Galois group of this polynomial.

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> f = Poly(x**4 - 2)
>>> G, _ = f.galois_group(by_name=True)
>>> print(G)
S4TransitiveSubgroups.D4
gcd(g)[源代码]#

返回的多项式GCD fg .

实例

>>> 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(g, auto=True)[源代码]#

扩展欧几里德算法 fg .

返回 (s, t, h) 这样的话 h = gcd(f, g)s*f + t*g = h .

实例

>>> 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'))
property gen#

返回主发电机。

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(x**2 + 1, x).gen
x
get_domain()[源代码]#

获得 f .

get_modulus()[源代码]#

得到 f .

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(x**2 + 1, modulus=2).get_modulus()
2
gff_list()[源代码]#

计算的最大因式分解 f .

实例

>>> 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 通过在基域分解。

实例

>>> 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(g, auto=True)[源代码]#

半扩展欧几里德算法 fg .

返回 (s, h) 这样的话 h = gcd(f, g)s*f = h (mod g) .

实例

>>> 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(*gens)[源代码]#

返回 True 如果 Poly(f, *gens) 保留地域。

实例

>>> 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
homogeneous_order()[源代码]#

返回的同构顺序 f .

齐次多项式是所有非零系数单项式具有相同的总次数的多项式。这个度是 f . 如果你只想检查一个多项式是否是齐次的,那么使用 Poly.is_homogeneous() .

实例

>>> from sympy import Poly
>>> from sympy.abc import x, y
>>> f = Poly(x**5 + 2*x**3*y**2 + 9*x*y**4)
>>> f.homogeneous_order()
5
homogenize(s)[源代码]#

返回的齐次多项式 f .

齐次多项式是所有非零系数单项式具有相同的总次数的多项式。如果你只想检查一个多项式是否是齐次的,那么使用 Poly.is_homogeneous() . 如果你不仅要检查一个多项式是否齐次,而且还要计算它的齐次阶,那么使用 Poly.homogeneous_order() .

实例

>>> from sympy import Poly
>>> from sympy.abc import x, y, z
>>> f = Poly(x**5 + 2*x**2*y**2 + 9*x*y**3)
>>> f.homogenize(z)
Poly(x**5 + 2*x**2*y**2*z + 9*x*y**3*z, x, y, z, domain='ZZ')
inject(front=False)[源代码]#

将地域发生器注入 f .

实例

>>> 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(*specs, **args)[源代码]#

计算的不定积分 f .

实例

>>> 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(all=False, eps=None, inf=None, sup=None, fast=False, sqf=False)[源代码]#

计算根的隔离间隔 f .

对于实根,使用文森特·阿克里塔斯·斯特兹本斯基(VAS)连分式法。

实例

>>> 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(g, auto=True)[源代码]#

使转化 fg 如果可能的话。

实例

>>> 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
property is_cyclotomic#

返回 True 如果 f 是一个分圆多项式。

实例

>>> 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
property is_ground#

返回 True 如果 f 是地域的一个元素。

实例

>>> 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
property is_homogeneous#

返回 True 如果 f 是一个齐次多项式。

齐次多项式是所有非零系数单项式具有相同的总次数的多项式。如果你不仅要检查一个多项式是否齐次,而且还要计算它的齐次阶,那么使用 Poly.homogeneous_order() .

实例

>>> from sympy import Poly
>>> from sympy.abc import x, y
>>> Poly(x**2 + x*y, x, y).is_homogeneous
True
>>> Poly(x**3 + x*y, x, y).is_homogeneous
False
property is_irreducible#

返回 True 如果 f 在它的领域内没有任何因素。

实例

>>> 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
property is_linear#

返回 True 如果 f 所有变量都是线性的。

实例

>>> 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
property is_monic#

返回 True 如果 f 是一个。

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(x + 2, x).is_monic
True
>>> Poly(2*x + 2, x).is_monic
False
property is_monomial#

返回 True 如果 f 为零或只有一个项。

实例

>>> 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
property is_multivariate#

返回 True 如果 f 是一个多元多项式。

实例

>>> 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
property is_one#

返回 True 如果 f 是单位多项式。

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(0, x).is_one
False
>>> Poly(1, x).is_one
True
property is_primitive#

返回 True if的GCD系数 f 是一个。

实例

>>> 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
property is_quadratic#

返回 True 如果 f 所有变量都是二次的。

实例

>>> 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
property is_sqf#

返回 True 如果 f 是一个无平方多项式。

实例

>>> 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
property is_univariate#

返回 True 如果 f 是一元多项式。

实例

>>> 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
property is_zero#

返回 True 如果 f 是一个零多项式。

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(0, x).is_zero
True
>>> Poly(1, x).is_zero
False
l1_norm()[源代码]#

返回的l1范数 f .

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(-x**2 + 2*x - 3, x).l1_norm()
6
lcm(g)[源代码]#

返回的多项式LCM fg .

实例

>>> 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 .

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(x**2 + 2*x - 1).length()
3
lift()[源代码]#

将代数系数转换为有理数。

实例

>>> 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(gen)[源代码]#

从中移除虚拟生成器 f 在指定的左边 gen 按命令在发电机里。什么时候? gen 是一个整数,它是指位于生成器元组内该位置的生成器 f .

实例

>>> 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')
>>> Poly(z, x, y, z).ltrim(-1)
Poly(z, z, domain='ZZ')
make_monic_over_integers_by_scaling_roots()[源代码]#

Turn any univariate polynomial over QQ or ZZ into a monic polynomial over ZZ, by scaling the roots as necessary.

返回:

Pair (g, c)

g is the polynomial

c is the integer by which the roots had to be scaled

解释

This operation can be performed whether or not f is irreducible; when it is, this can be understood as determining an algebraic integer generating the same field as a root of f.

实例

>>> from sympy import Poly, S
>>> from sympy.abc import x
>>> f = Poly(x**2/2 + S(1)/4 * x + S(1)/8, x, domain='QQ')
>>> f.make_monic_over_integers_by_scaling_roots()
(Poly(x**2 + 2*x + 4, x, domain='ZZ'), 4)
match(*args, **kwargs)[源代码]#

匹配Poly中的表达式。看到了吗基本匹配()

max_norm()[源代码]#

返回最大范数 f .

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(-x**2 + 2*x - 3, x).max_norm()
3
monic(auto=True)[源代码]#

将所有系数除以 LC(f) .

实例

>>> 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(order=None)[源代码]#

从返回所有非零单项式 f 以法律程序。

实例

>>> 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)]

参见

all_monoms

mul(g)[源代码]#

两个多项式相乘 fg .

实例

>>> 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(coeff)[源代码]#

乘法 f 通过一个基域的元素。

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(x + 1).mul_ground(2)
Poly(2*x + 2, x, domain='ZZ')
neg()[源代码]#

取中的所有系数 f .

实例

>>> 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)[源代码]#

构建 Poly 来自原始表示的实例。

norm()[源代码]#

计算产品, Norm(f) ,多项式共轭 f 在数字字段上定义 K .

实例

>>> from sympy import Poly, sqrt
>>> from sympy.abc import x
>>> a, b = sqrt(2), sqrt(3)

二次扩张上的多项式。两个共轭物x-a和x+a。

>>> f = Poly(x - a, x, extension=a)
>>> f.norm()
Poly(x**2 - 2, x, domain='QQ')

四次扩张上的多项式。四个共轭物x-a,x-a,x+a和x+a。

>>> f = Poly(x - a, x, extension=(a, b))
>>> f.norm()
Poly(x**4 - 4*x**2 + 4, x, domain='QQ')
nroots(n=15, maxsteps=50, cleanup=True)[源代码]#

计算 f .

参数:

n。。。要计算的位数

最大步数。。。要执行的最大迭代数

If the accuracy `n` cannot be reached in `maxsteps`, it will raise an

例外情况。你需要用更高的最大步数重新运行。

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(x**2 - 3).nroots(n=15)
[-1.73205080756888, 1.73205080756888]
>>> Poly(x**2 - 3).nroots(n=30)
[-1.73205080756887729352744634151, 1.73205080756887729352744634151]
nth(*N)[源代码]#

返回 n -th系数 f 在哪里? N 是感兴趣项中生成元的指数。

实例

>>> from sympy import Poly, sqrt
>>> 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
>>> Poly(4*sqrt(x)*y)
Poly(4*y*(sqrt(x)), y, sqrt(x), domain='ZZ')
>>> _.nth(1, 1)
4
nth_power_roots_poly(n)[源代码]#

构造根的n次方多项式 f .

实例

>>> 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')
property one#

返回一个多项式 self 的属性。

pdiv(g)[源代码]#

多项式伪除法 f 通过 g .

实例

>>> 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(rep, gens=None, remove=None)[源代码]#

用给定的表示法创建多边形。

实例

>>> 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(g)[源代码]#

多项式精确伪商 f 通过 g .

实例

>>> 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(n)[源代码]#

提高 f 非负幂 n .

实例

>>> 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(g)[源代码]#

多项式伪商 f 通过 g .

请参见函数prem(f,g)中的注意事项。

实例

>>> 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(g)[源代码]#

多项式伪余数 f 通过 g .

注意:函数prem(f,g,x)可以安全地用于计算

在Z轴 [x] _only_ 次合成多项式余数序列(prs)。

安全地计算Z中的欧几里得和斯图曼prs [x] 使用模块中的任何相应功能sympy.polys.子结果. 后缀为u pg的模块中的函数计算Z中的prs [x] 使用rem(f,g,x),而后缀为?amv的函数计算Z中的pr [x] 使用rem_z(f,g,x)。

函数rem_z(f,g,x)与prem(f,g,x)的不同之处在于计算z中的余项多项式 [x] 它将除数乘以除数的前导系数的绝对值乘以幂次(f,x)-次(g,x)+1。

实例

>>> 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 .

实例

>>> 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(g, auto=True)[源代码]#

计算的多项式商 f 通过 g .

实例

>>> 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(coeff)[源代码]#

f 通过一个基域的元素。

实例

>>> 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(g)[源代码]#

有理函数中的分母 f/g .

实例

>>> 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]')
real_roots(multiple=True, radicals=True)[源代码]#

返回具有重数的实根的列表。

See real_roots() for more explanation.

实例

>>> 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()
[CRootOf(x**3 + x + 1, 0)]
refine_root(s, t, eps=None, steps=None, fast=False, check_sqf=False)[源代码]#

将根的隔离间隔细化到给定的精度。

实例

>>> 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(g, auto=True)[源代码]#

计算 f 通过 g .

实例

>>> from sympy import Poly
>>> 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(*gens, **args)[源代码]#

高效使用新的发电机。

实例

>>> 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(x, y=None, **_ignore)[源代码]#

替换 x 具有 y 在发电机列表中。

实例

>>> 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(g, includePRS=False)[源代码]#

计算的结果 fg 通过PRS。

如果includePRS=True,则结果中包含子结果PRS。因为PRS用于计算结果,因此比调用更有效 subresultants() 分别地。

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> f = Poly(x**2 + 1, x)
>>> f.resultant(Poly(x**2 - 1, x))
4
>>> f.resultant(Poly(x**2 - 1, x), includePRS=True)
(4, [Poly(x**2 + 1, x, domain='ZZ'), Poly(x**2 - 1, x, domain='ZZ'),
     Poly(-2, x, domain='ZZ')])
retract(field=None)[源代码]#

重新计算多项式的基域。

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> 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(n)[源代码]#

计算 f**(-1) 国防部 x**n .

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(1, x).revert(2)
Poly(1, x, domain='ZZ')
>>> Poly(1 + x, x).revert(1)
Poly(1, x, domain='ZZ')
>>> Poly(x**2 - 2, x).revert(2)
Traceback (most recent call last):
...
NotReversible: only units are reversible in a ring
>>> Poly(1/x, x).revert(1)
Traceback (most recent call last):
...
PolynomialError: 1/x contains an element of the generators set
root(index, radicals=True)[源代码]#

求一个多项式的指数根。

实例

>>> 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)
CRootOf(x**3 - x**2 + 1, 0)
same_root(a, b)[源代码]#

Decide whether two roots of this polynomial are equal.

加薪:

DomainError

If the domain of the polynomial is not ZZ, QQ, RR, or CC.

MultivariatePolynomialError

If the polynomial is not univariate.

PolynomialError

If the polynomial is of degree < 2.

实例

>>> from sympy import Poly, cyclotomic_poly, exp, I, pi
>>> f = Poly(cyclotomic_poly(5))
>>> r0 = exp(2*I*pi/5)
>>> indices = [i for i, r in enumerate(f.all_roots()) if f.same_root(r, r0)]
>>> print(indices)
[3]
set_domain(domain)[源代码]#

设置基域 f .

set_modulus(modulus)[源代码]#

设置 f .

实例

>>> 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(a)[源代码]#

有效计算泰勒位移 f(x + a) .

实例

>>> 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(x, m, n=None)[源代码]#

取连续的子项序列 f .

sqf_list(all=False)[源代码]#

返回无平方因子的列表 f .

实例

>>> 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(all=False)[源代码]#

返回无平方因子的列表 f .

实例

>>> 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 .

返回 sfr ,这样 g(x) = f(x-sa)r(x) = Norm(g(x)) 一个平方自由多项式 K 在哪里 a 是基域的代数扩张。

实例

>>> 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*sqrt(3)*x + 4, x, domain='QQ<sqrt(3)>')
>>> r
Poly(x**4 - 4*x**2 + 16, x, domain='QQ')
sqf_part()[源代码]#

计算的平方自由部分 f .

实例

>>> 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 .

实例

>>> 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(auto=True)[源代码]#

计算的Sturm序列 f .

实例

>>> 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(g)[源代码]#

减去两个多项式 fg .

实例

>>> 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(coeff)[源代码]#

从中减去基域的一个元素 f .

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(x + 1).sub_ground(2)
Poly(x - 1, x, domain='ZZ')
subresultants(g)[源代码]#

计算的子结果pr fg .

实例

>>> 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(order=None)[源代码]#

从返回所有非零项 f 以法律程序。

实例

>>> 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)]

参见

all_terms

terms_gcd()[源代码]#

从多项式中删除项的GCD f .

实例

>>> 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(func, *gens, **args)[源代码]#

将函数应用于 f .

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> def func(k, coeff):
...     k = k[0]
...     return coeff//10**(2-k)
>>> Poly(x**2 + 20*x + 400).termwise(func)
Poly(x**2 + 2*x + 4, x, domain='ZZ')
to_exact()[源代码]#

使地面域精确。

实例

>>> 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()[源代码]#

使地面域成为一个场。

实例

>>> 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()[源代码]#

把地域变成一个环。

实例

>>> 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 .

实例

>>> from sympy import Poly
>>> from sympy.abc import x, y
>>> Poly(x**2 + y*x + 1, x, y).total_degree()
2
>>> Poly(x + y**5, x, y).total_degree()
5
transform(p, q)[源代码]#

有效评估功能转换 q**n * f(p/q) .

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> Poly(x**2 - 2*x + 1, x).transform(Poly(x + 1, x), Poly(x - 1, x))
Poly(4, x, domain='ZZ')
trunc(p)[源代码]#

减少 f 模a常数 p .

实例

>>> 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(g)[源代码]#

制作 fg 属于同一个域。

实例

>>> from sympy import Poly
>>> from sympy.abc import x
>>> 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')
property unit#

返回单位多项式 self 的属性。

property zero#

返回零多项式 self 的属性。

class sympy.polys.polytools.PurePoly(rep, *gens, **args)[源代码]#

表示纯多项式的类。

property free_symbols#

多项式的自由符号。

实例

>>> from sympy import PurePoly
>>> from sympy.abc import x, y
>>> PurePoly(x**2 + 1).free_symbols
set()
>>> PurePoly(x**2 + y).free_symbols
set()
>>> PurePoly(x**2 + y, x).free_symbols
{y}
class sympy.polys.polytools.GroebnerBasis(F, *gens, **args)[源代码]#

表示减少的Groebner基础。

contains(poly)[源代码]#

检查是否 poly 属于理想产生的 self .

实例

>>> from sympy import groebner
>>> from sympy.abc import x, y
>>> f = 2*x**3 + y**3 + 3*y
>>> G = groebner([x**2 + y**2 - 1, x*y - 2])
>>> G.contains(f)
True
>>> G.contains(f + 1)
False
fglm(order)[源代码]#

将Groebner基础从一个订单转换为另一个订单。

FGLM算法将零维理想的约化Groebner基从一个序转换成另一个序。当不可能直接计算与特定排序相关的Groebner基时,通常使用这种方法。

实例

>>> from sympy.abc import x, y
>>> from sympy import groebner
>>> F = [x**2 - 3*y - x + 1, y**2 - 2*x + y - 1]
>>> G = groebner(F, x, y, order='grlex')
>>> list(G.fglm('lex'))
[2*x - y**2 - y + 1, y**4 + 2*y**3 - 3*y**2 - 16*y + 7]
>>> list(groebner(F, x, y, order='lex'))
[2*x - y**2 - y + 1, y**4 + 2*y**3 - 3*y**2 - 16*y + 7]

工具书类

[R810]

J、 C.Faugere,P.Gianni,D.Lazard,T.Mora(1994年)。零维Groebner基的序变换高效计算

property is_zero_dimensional#

检查由Groebner基生成的理想是否为零维。

该算法检查单项式集是否不能被任何元素的前导单项式整除 F 是有界的。

工具书类

大卫·A·考克斯,约翰·B·利特尔,多纳尔·奥谢。理想,多样性和算法,第三版,第230页

reduce(expr, auto=True)[源代码]#

将多项式模化为Groebner基。

给定多项式 f 以及一组多项式 G = (g_1, ..., g_n) ,计算一组商 q = (q_1, ..., q_n) 剩下的呢 r 这样的话 f = q_1*f_1 + ... + q_n*f_n + r 在哪里 r 消失或 r 是关于 G .

实例

>>> from sympy import groebner, expand
>>> from sympy.abc import x, y
>>> f = 2*x**4 - x**2 + y**3 + y**2
>>> G = groebner([x**3 - x, y**3 - y])
>>> G.reduce(f)
([2*x, 1], x**2 + y**2 + y)
>>> Q, r = _
>>> expand(sum(q*g for q, g in zip(Q, G)) + r)
2*x**4 - x**2 + y**3 + y**2
>>> _ == f
True

额外多项式操作函数#

sympy.polys.polyfuncs.symmetrize(F, *gens, **args)[源代码]#

用初等对称多项式重写多项式。

A symmetric polynomial is a multivariate polynomial that remains invariant under any variable permutation, i.e., if \(f = f(x_1, x_2, \dots, x_n)\), then \(f = f(x_{i_1}, x_{i_2}, \dots, x_{i_n})\), where \((i_1, i_2, \dots, i_n)\) is a permutation of \((1, 2, \dots, n)\) (an element of the group \(S_n\)).

返回对称多项式的元组 (f1, f2, ..., fn) 这样的话 f = f1 + f2 + ... + fn .

实例

>>> 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)[源代码]#

重写Horner形式的多项式。

在其他应用中,当使用Horner格式时,对一个点的多项式求值是最优的( [1] )

实例

>>> 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))

工具书类

[1] - https://en.wikipedia.org/wiki/Horner_方案

sympy.polys.polyfuncs.interpolate(data, x)[源代码]#

为在点x处计算的数据点构造一个插值多项式(可以是符号或数字)。

实例

>>> from sympy.polys.polyfuncs import interpolate
>>> from sympy.abc import a, b, x

列表被解释为与从1开始的范围配对:

>>> interpolate([1, 4, 9, 16], x)
x**2

这可以通过给出坐标列表来明确:

>>> interpolate([(1, 1), (2, 4), (3, 9)], x)
x**2

(x,y)坐标也可以作为字典的键和值给出(并且这些点不需要等距):

>>> interpolate([(-1, 2), (1, 2), (2, 5)], x)
x**2 + 1
>>> interpolate({-1: 2, 1: 2, 2: 5}, x)
x**2 + 1

如果插值只使用一次,则可以传递感兴趣的值,而不是传递符号:

>>> interpolate([1, 4, 9], 5)
25

还支持符号坐标:

>>> [(i,interpolate((a, b), i)) for i in range(1, 4)]
[(1, a), (2, b), (3, -a + 2*b)]
sympy.polys.polyfuncs.viete(f, roots=None, *gens, **args)[源代码]#

生成Viete公式 f .

实例

>>> 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)]

域构造函数#

sympy.polys.constructor.construct_domain(obj, **args)[源代码]#

Construct a minimal domain for a list of expressions.

参数:

obj: list or dict

The expressions to build a domain for.

**args: keyword arguments

Options that affect the choice of domain.

返回:

(K, elements): Domain and list of domain elements

The domain K that can represent the expressions and the list or dict of domain elements representing the same expressions as elements of K.

解释

Given a list of normal SymPy expressions (of type Expr) construct_domain will find a minimal Domain that can represent those expressions. The expressions will be converted to elements of the domain and both the domain and the domain elements are returned.

实例

Given a list of Integer construct_domain will return the domain ZZ and a list of integers as elements of ZZ.

>>> from sympy import construct_domain, S
>>> expressions = [S(2), S(3), S(4)]
>>> K, elements = construct_domain(expressions)
>>> K
ZZ
>>> elements
[2, 3, 4]
>>> type(elements[0])  
<class 'int'>
>>> type(expressions[0])
<class 'sympy.core.numbers.Integer'>

If there are any Rational then QQ is returned instead.

>>> construct_domain([S(1)/2, S(3)/4])
(QQ, [1/2, 3/4])

If there are symbols then a polynomial ring K[x] is returned.

>>> from sympy import symbols
>>> x, y = symbols('x, y')
>>> construct_domain([2*x + 1, S(3)/4])
(QQ[x], [2*x + 1, 3/4])
>>> construct_domain([2*x + 1, y])
(ZZ[x,y], [2*x + 1, y])

If any symbols appear with negative powers then a rational function field K(x) will be returned.

>>> construct_domain([y/x, x/(1 - y)])
(ZZ(x,y), [y/x, -x/(y - 1)])

Irrational algebraic numbers will result in the EX domain by default. The keyword argument extension=True leads to the construction of an algebraic number field QQ<a>.

>>> from sympy import sqrt
>>> construct_domain([sqrt(2)])
(EX, [EX(sqrt(2))])
>>> construct_domain([sqrt(2)], extension=True)  
(QQ<sqrt(2)>, [ANP([1, 0], [1, 0, -2], QQ)])

参见

Domain, Expr

作为元组编码的单项式#

class sympy.polys.monomials.Monomial(monom, gens=None)[源代码]#

表示单项式的类,即幂的乘积。

as_expr(*gens)[源代码]#

Convert a monomial instance to a SymPy expression.

gcd(other)[源代码]#

Greatest common divisor of monomials.

lcm(other)[源代码]#

Least common multiple of monomials.

sympy.polys.monomials.itermonomials(variables, max_degrees, min_degrees=None)[源代码]#

max_degrees and min_degrees are either both integers or both lists. Unless otherwise specified, min_degrees is either 0 or [0, ..., 0].

A generator of all monomials monom is returned, such that either min_degree <= total_degree(monom) <= max_degree, or min_degrees[i] <= degree_list(monom)[i] <= max_degrees[i], for all i.

Case I. max_degrees And min_degrees Are Both Integers

Given a set of variables \(V\) and a min_degree \(N\) and a max_degree \(M\) generate a set of monomials of degree less than or equal to \(N\) and greater than or equal to \(M\). The total number of monomials in commutative variables is huge and is given by the following formula if \(M = 0\):

\[\frac{(\#V + N)!}{\#V! N!}\]

For example if we would like to generate a dense polynomial of a total degree \(N = 50\) and \(M = 0\), which is the worst case, 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.

Consider monomials in commutative variables \(x\) and \(y\) and non-commutative variables \(a\) and \(b\):

>>> from sympy import symbols
>>> from sympy.polys.monomials import itermonomials
>>> from sympy.polys.orderings import monomial_key
>>> from sympy.abc import x, y

>>> sorted(itermonomials([x, y], 2), key=monomial_key('grlex', [y, x]))
[1, x, y, x**2, x*y, y**2]

>>> sorted(itermonomials([x, y], 3), key=monomial_key('grlex', [y, x]))
[1, x, y, x**2, x*y, y**2, x**3, x**2*y, x*y**2, y**3]

>>> a, b = symbols('a, b', commutative=False)
>>> set(itermonomials([a, b, x], 2))
{1, a, a**2, b, b**2, x, x**2, a*b, b*a, x*a, x*b}

>>> sorted(itermonomials([x, y], 2, 1), key=monomial_key('grlex', [y, x]))
[x, y, x**2, x*y, y**2]

Case Ii. max_degrees And min_degrees Are Both Lists

If max_degrees = [d_1, ..., d_n] and min_degrees = [e_1, ..., e_n], the number of monomials generated is:

\[(d_1 - e_1 + 1) (d_2 - e_2 + 1) \cdots (d_n - e_n + 1)\]

Let us generate all monomials monom in variables \(x\) and \(y\) such that [1, 2][i] <= degree_list(monom)[i] <= [2, 4][i], i = 0, 1

>>> from sympy import symbols
>>> from sympy.polys.monomials import itermonomials
>>> from sympy.polys.orderings import monomial_key
>>> from sympy.abc import x, y

>>> sorted(itermonomials([x, y], [2, 4], [1, 2]), reverse=True, key=monomial_key('lex', [x, y]))
[x**2*y**4, x**2*y**3, x**2*y**2, x*y**4, x*y**3, x*y**2]
sympy.polys.monomials.monomial_count(V, N)[源代码]#

计算单项式的数目。

单项式的个数由下式给出:

\[\begin{split}\压裂{(\\#V+N)!}{V!N!}\end{split}\]

在哪里? \(N\) 是一个完整的学位 \(V\) 是一组变量。

实例

>>> from sympy.polys.monomials import itermonomials, monomial_count
>>> from sympy.polys.orderings import monomial_key
>>> from sympy.abc import x, y
>>> monomial_count(2, 2)
6
>>> M = list(itermonomials([x, y], 2))
>>> sorted(M, key=monomial_key('grlex', [y, x]))
[1, x, y, x**2, x*y, y**2]
>>> len(M)
6

单项式序#

class sympy.polys.orderings.MonomialOrder[源代码]#

单项式序的基类。

class sympy.polys.orderings.LexOrder[源代码]#

单项式的字典序。

class sympy.polys.orderings.GradedLexOrder[源代码]#

单项式的分级词典序。

class sympy.polys.orderings.ReversedGradedLexOrder[源代码]#

单项式的反向分级词典序。

多项式根的形式化处理#

sympy.polys.rootoftools.rootof(f, x, index=None, radicals=True, expand=True)[源代码]#

一元多项式的索引根。

返回 ComplexRootOf 包含部首的对象或显式表达式。

参数:

f :表达式

一元多项式。

x :符号,可选

发电机 f .

指数 :int或Integer

激进分子 布尔

如果可能,返回一个激进表达式。

扩大 布尔

展开 f .

class sympy.polys.rootoftools.RootOf(f, x, index=None, radicals=True, expand=True)[源代码]#

表示一元多项式的根。

不同多项式根的基类。当前只支持复杂根。

class sympy.polys.rootoftools.ComplexRootOf(f, x, index=None, radicals=False, expand=True)[源代码]#

表示多项式的索引复根。

Roots of a univariate polynomial separated into disjoint real or complex intervals and indexed in a fixed order:

  • real roots come first and are sorted in increasing order;

  • complex roots come next and are sorted primarily by increasing real part, secondarily by increasing imaginary part.

Currently only rational coefficients are allowed. Can be imported as CRootOf. To avoid confusion, the generator must be a Symbol.

实例

>>> from sympy import CRootOf, rootof
>>> from sympy.abc import x

CRootOf是一种引用多项式特定根的方法。如果有一个有理根,则返回:

>>> CRootOf.clear_cache()  # for doctest reproducibility
>>> CRootOf(x**2 - 4, 0)
-2

包含根的根是否返回取决于 radicals 标志为true(使用rootof设置为true):

>>> CRootOf(x**2 - 3, 0)
CRootOf(x**2 - 3, 0)
>>> CRootOf(x**2 - 3, 0, radicals=True)
-sqrt(3)
>>> rootof(x**2 - 3, 0)
-sqrt(3)

以下不能用部首来表达:

>>> r = rootof(4*x**5 + 16*x**3 + 12*x**2 + 7, 0); r
CRootOf(4*x**5 + 16*x**3 + 12*x**2 + 7, 0)

但是,可以看到根边界,并且求值方法使用它们来获得根的数值近似值。

>>> interval = r._get_interval(); interval
(-1, 0)
>>> r.evalf(2)
-0.98

evalf方法细化根边界的宽度,直到它保证这些边界内的任何十进制近似值都能满足所需的精度。然后,它存储经过优化的间隔,以便达到或低于请求精度的后续请求将不必重新计算根边界,并将很快返回。

在上述评估之前,间隔时间为

>>> interval
(-1, 0)

经过评估,现在是

>>> r._get_interval() 
(-165/169, -206/211)

若要重置给定多项式的所有间隔,则 _reset() 方法可以从多项式的任何CRootOf实例调用:

>>> r._reset()
>>> r._get_interval()
(-1, 0)

这个 eval_approx() 方法还将按给定精度查找根,但间隔不会修改,除非对根的搜索未能在根边界内收敛。用割线法求根。(在 evalf 方法使用对分并始终更新间隔。)

>>> r.eval_approx(2)
-0.98

需要稍微更新间隔以找到该根:

>>> r._get_interval()
(-1, -1/2)

这个 evalf_rational 将计算根的有理逼近到所需的精度或精度。

>>> r.eval_rational(n=2)
-69629/71318
>>> t = CRootOf(x**3 + 10*x + 1, 1)
>>> t.eval_rational(1e-1)
15/256 - 805*I/256
>>> t.eval_rational(1e-1, 1e-4)
3275/65536 - 414645*I/131072
>>> t.eval_rational(1e-4, 1e-4)
6545/131072 - 414645*I/131072
>>> t.eval_rational(n=2)
104755/2097152 - 6634255*I/2097152

笔记

尽管PurePoly可以从非符号生成器构造,但不允许非符号实例的根,以避免混淆所表示的根。

>>> from sympy import exp, PurePoly
>>> PurePoly(x) == PurePoly(exp(x))
True
>>> CRootOf(x - 1, 0)
1
>>> CRootOf(exp(x) - 1, 0)  # would correspond to x == 0
Traceback (most recent call last):
...
sympy.polys.polyerrors.PolynomialError: generator must be a Symbol
classmethod _all_roots(poly, use_cache=True)[源代码]#

求复合多项式的实根和复根。

classmethod _complexes_index(complexes, index)[源代码]#

将初始复杂根索引映射到根所属因子中的索引。

classmethod _complexes_sorted(complexes)[源代码]#

使复杂的隔离间隔不相交并对根进行排序。

classmethod _count_roots(roots)[源代码]#

计算具有多重性的实根或复杂根的数量。

_ensure_complexes_init()[源代码]#

Ensure that our poly has entries in the complexes cache.

_ensure_reals_init()[源代码]#

Ensure that our poly has entries in the reals cache.

_eval_evalf(prec, **kwargs)[源代码]#

计算这个复数根到给定的精度。

_eval_is_imaginary()[源代码]#

返回 True 如果根是假想的。

_eval_is_real()[源代码]#

返回 True 如果根是真的。

classmethod _get_complexes(factors, use_cache=True)[源代码]#

计算因子列表的复杂根隔离间隔。

classmethod _get_complexes_sqf(currentfactor, use_cache=True)[源代码]#

求平方自由因子的复根隔离区间。

_get_interval()[源代码]#

用于从缓存检索隔离间隔的内部函数。

classmethod _get_reals(factors, use_cache=True)[源代码]#

计算因子列表的实际根隔离间隔。

classmethod _get_reals_sqf(currentfactor, use_cache=True)[源代码]#

得到平方自由因子的实根隔离间隔。

classmethod _get_roots(method, poly, radicals)[源代码]#

返回指定类型的后处理根。

classmethod _indexed_root(poly, index, lazy=False)[源代码]#

用指数法求复合多项式的根。

classmethod _new(poly, index)[源代码]#

新建 CRootOf 对象。

classmethod _postprocess_root(root, radicals)[源代码]#

如果根是琐碎的或 CRootOf 对象。

classmethod _preprocess_roots(poly)[源代码]#

采取英勇的措施 poly 兼容 CRootOf .

classmethod _real_roots(poly)[源代码]#

求复合多项式的实根。

classmethod _reals_index(reals, index)[源代码]#

将初始实际根索引映射到根所属因子中的索引。

classmethod _reals_sorted(reals)[源代码]#

使真正的隔离间隔不相交并对根进行排序。

classmethod _refine_complexes(complexes)[源代码]#

返回复数,使非共轭根的边界矩形不会相交。另外,确保ay和by都不是0,以保证非实数根在y-界上与实根不同。

_reset()[源代码]#

重置所有间隔

classmethod _roots_trivial(poly, radicals)[源代码]#

在线性,二次和二项的情况下计算根。

_set_interval(interval)[源代码]#

更新缓存中隔离间隔的内部函数。

classmethod all_roots(poly, radicals=True)[源代码]#

求多项式的实根和复根。

classmethod clear_cache()[源代码]#

重置Real和Complex的缓存。

用于近似根实例的间隔将根据需要进行更新。当请求查看间隔时,将显示最新的值。 \(clear_cache\) 将所有CRootOf实例重置回其原始状态。

参见

_reset

eval_approx(n, return_mpmath=False)[源代码]#

计算这个复数根到给定的精度。

它使用割线方法,根边界用于生成初始猜测,并检查返回的根是否有效。如果方法收敛到根边界之外,边界将变小并更新。

eval_rational(dx=None, dy=None, n=15)[源代码]#

返回有理逼近 self 它有实部和虚部的近似值 dxdy 分别是真值。或者, n 可以指定精度位数。

区间用对分法进行了细化,并且一定会收敛。当精化完成时,根边界会更新,因此以相同或更低精度重新计算将不必重复求精,并且应该快得多。

下面的例子首先得到了4阶勒让德多项式所有根的1-8精度的有理逼近。由于根都小于1,这将确保近似值的十进制表示正确(包括舍入)到6位数:

>>> from sympy import legendre_poly, Symbol
>>> x = Symbol("x")
>>> p = legendre_poly(4, x, polys=True)
>>> r = p.real_roots()[-1]
>>> r.eval_rational(10**-8).n(6)
0.861136

但是,不需要两步计算:十进制表示可以直接计算:

>>> r.evalf(17)
0.86113631159405258
classmethod real_roots(poly, radicals=True)[源代码]#

求多项式的实根。

class sympy.polys.rootoftools.RootSum(expr, func=None, x=None, auto=True, quadratic=False)[源代码]#

表示一元多项式的所有根的和。

classmethod new(poly, func, auto=True)[源代码]#

Construct new RootSum instance.

符号寻根算法#

sympy.polys.polyroots.roots(f, *gens, auto=True, cubics=True, trig=False, quartics=True, quintics=False, multiple=False, filter=None, predicate=None, strict=False, **flags)[源代码]#

计算一元多项式的符号根。

给定一个具有符号系数的单变量多项式f(或多项式系数列表),返回一个字典,字典的根及其多重性。

只返回通过部首表达的根。要获得完整的根集,请使用类的RootOf或数值方法。默认情况下,算法中使用三次和四次公式。因为无法读取输出集而禁用它们 cubics=Falsequartics=False 分别。如果立方根是实的,但用复数表示(casus不可约 [1] ) trig flag可以设置为True,以便根据余弦函数和反余弦函数返回解。

要从特定域获取根,请设置 filter 使用以下说明符之一标记:Z、Q、R、I、C。默认情况下返回所有根(这相当于设置 filter='C'

默认情况下,返回一个字典,在多个根的情况下给出一个紧凑的结果。但是,要获得包含所有这些根的列表,请设置 multiple 标志为True;列表将具有相同的根在结果中并排出现。(对于给定的多边形,所有根方法将按排序的数字顺序给出根。)

If the strict flag is True, UnsolvableFactorError will be raised if the roots found are known to be incomplete (because some roots are not expressible in radicals).

实例

>>> from sympy import Poly, roots, degree
>>> 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))
{-sqrt(y): 1, sqrt(y): 1}
>>> roots(x**2 - y, x)
{-sqrt(y): 1, sqrt(y): 1}
>>> roots([1, 0, -1])
{-1: 1, 1: 1}

roots will only return roots expressible in radicals. If the given polynomial has some or all of its roots inexpressible in radicals, the result of roots will be incomplete or empty respectively.

Example where result is incomplete:

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

In this case, the polynomial has an unsolvable quintic factor whose roots cannot be expressed by radicals. The polynomial has a rational root (due to the factor \((x-1)\)), which is returned since roots always finds all rational roots.

Example where result is empty:

>>> roots(x**7-3*x**2+1, x)
{}

Here, the polynomial has no roots expressible in radicals, so roots returns an empty dictionary.

The result produced by roots is complete if and only if the sum of the multiplicity of each root is equal to the degree of the polynomial. If strict=True, UnsolvableFactorError will be raised if the result is incomplete.

The result can be be checked for completeness as follows:

>>> f = x**3-2*x**2+1
>>> sum(roots(f, x).values()) == degree(f, x)
True
>>> f = (x-1)*(x**5-x+1)
>>> sum(roots(f, x).values()) == degree(f, x)
False

工具书类

特殊多项式#

sympy.polys.specialpolys.swinnerton_dyer_poly(n, x=None, polys=False)[源代码]#

生成第n个Swinnerton-Dyer多项式 \(x\) .

参数:

n :内景

\(n\) 决定多项式的阶数

x :可选

多边形 :bool,可选

polys=True 返回表达式,否则(默认)返回表达式。

sympy.polys.specialpolys.interpolating_poly(n, x, X='x', Y='y')[源代码]#

构造拉格朗日插值多项式 n 数据点。如果给定的值序列 XY 然后是第一个 n 将使用值。

sympy.polys.specialpolys.cyclotomic_poly(n, x=None, polys=False)[源代码]#

生成序的分圆多项式 \(n\) 在里面 \(x\) .

参数:

n :内景

\(n\) 决定多项式的阶数

x :可选

多边形 :bool,可选

polys=True 返回表达式,否则(默认)返回表达式。

sympy.polys.specialpolys.symmetric_poly(n, *gens, polys=False)[源代码]#

生成序的对称多项式 \(n\) .

参数:

polys: bool, optional (default: False)

返回多边形对象 polys=True ,否则(默认)返回一个表达式。

sympy.polys.specialpolys.random_poly(x, n, inf, sup, domain=ZZ, polys=False)[源代码]#

生成次数多项式 n 系数为 [inf, sup] .

参数:

x

\(x\) 是多项式的独立项

n :内景

\(n\) 决定多项式的阶数

inf

系数所处范围的下限

sup

系数所在范围的上限

:可选

决定系数应该属于哪个环。默认设置为整数。

多边形 :bool,可选

polys=True 返回表达式,否则(默认)返回表达式。

正交多项式#

sympy.polys.orthopolys.chebyshevt_poly(n, x=None, polys=False)[源代码]#

Generates the Chebyshev polynomial of the first kind \(T_n(x)\).

参数:

n :内景

Degree of the polynomial.

x :可选

多边形 :bool,可选

If True, return a Poly, otherwise (default) return an expression.

sympy.polys.orthopolys.chebyshevu_poly(n, x=None, polys=False)[源代码]#

Generates the Chebyshev polynomial of the second kind \(U_n(x)\).

参数:

n :内景

Degree of the polynomial.

x :可选

多边形 :bool,可选

If True, return a Poly, otherwise (default) return an expression.

sympy.polys.orthopolys.gegenbauer_poly(n, a, x=None, polys=False)[源代码]#

Generates the Gegenbauer polynomial \(C_n^{(a)}(x)\).

参数:

n :内景

Degree of the polynomial.

x :可选

a

决定系数列表的最小域。

多边形 :bool,可选

If True, return a Poly, otherwise (default) return an expression.

sympy.polys.orthopolys.hermite_poly(n, x=None, polys=False)[源代码]#

Generates the Hermite polynomial \(H_n(x)\).

参数:

n :内景

Degree of the polynomial.

x :可选

多边形 :bool,可选

If True, return a Poly, otherwise (default) return an expression.

sympy.polys.orthopolys.hermite_prob_poly(n, x=None, polys=False)[源代码]#

Generates the probabilist's Hermite polynomial \(He_n(x)\).

参数:

n :内景

Degree of the polynomial.

x :可选

多边形 :bool,可选

If True, return a Poly, otherwise (default) return an expression.

sympy.polys.orthopolys.jacobi_poly(n, a, b, x=None, polys=False)[源代码]#

Generates the Jacobi polynomial \(P_n^{(a,b)}(x)\).

参数:

n :内景

Degree of the polynomial.

a

系数列表最小域的下限。

b

系数列表最小域的上限。

x :可选

多边形 :bool,可选

If True, return a Poly, otherwise (default) return an expression.

sympy.polys.orthopolys.legendre_poly(n, x=None, polys=False)[源代码]#

Generates the Legendre polynomial \(P_n(x)\).

参数:

n :内景

Degree of the polynomial.

x :可选

多边形 :bool,可选

If True, return a Poly, otherwise (default) return an expression.

sympy.polys.orthopolys.laguerre_poly(n, x=None, alpha=0, polys=False)[源代码]#

Generates the Laguerre polynomial \(L_n^{(\alpha)}(x)\).

参数:

n :内景

Degree of the polynomial.

x :可选

alpha : optional

决定系数列表的最小域。

多边形 :bool,可选

If True, return a Poly, otherwise (default) return an expression.

sympy.polys.orthopolys.spherical_bessel_fn(n, x=None, polys=False)[源代码]#

球面贝塞尔函数的系数。

These are only needed in the jn() function.

系数计算公式如下:

fn(0,z)=1/z fn(1,z)=1/z **2 fn(n-1, z) + fn(n+1, z) == (2* n+1)/z*fn(n,z)

参数:

n :内景

Degree of the polynomial.

x :可选

多边形 :bool,可选

If True, return a Poly, otherwise (default) return an expression.

实例

>>> from sympy.polys.orthopolys import spherical_bessel_fn as fn
>>> from sympy import Symbol
>>> z = Symbol("z")
>>> fn(1, z)
z**(-2)
>>> fn(2, z)
-1/z + 3/z**3
>>> fn(3, z)
-6/z**2 + 15/z**4
>>> fn(4, z)
1/z - 45/z**3 + 105/z**5

Appell sequences#

sympy.polys.appellseqs.bernoulli_poly(n, x=None, polys=False)[源代码]#

Generates the Bernoulli polynomial \(\operatorname{B}_n(x)\).

\(\operatorname{B}_n(x)\) is the unique polynomial satisfying

\[\int_{x}^{x+1} \operatorname{B}_n(t) \,dt = x^n.\]

Based on this, we have for nonnegative integer \(s\) and integer \(a\) and \(b\)

\[\sum_{k=a}^{b} k^s = \frac{\operatorname{B}_{s+1}(b+1) - \operatorname{B}_{s+1}(a)}{s+1}\]

which is related to Jakob Bernoulli's original motivation for introducing the Bernoulli numbers, the values of these polynomials at \(x = 1\).

参数:

n :内景

Degree of the polynomial.

x :可选

多边形 :bool,可选

If True, return a Poly, otherwise (default) return an expression.

实例

>>> from sympy import summation
>>> from sympy.abc import x
>>> from sympy.polys import bernoulli_poly
>>> bernoulli_poly(5, x)
x**5 - 5*x**4/2 + 5*x**3/3 - x/6
>>> def psum(p, a, b):
...     return (bernoulli_poly(p+1,b+1) - bernoulli_poly(p+1,a)) / (p+1)
>>> psum(4, -6, 27)
3144337
>>> summation(x**4, (x, -6, 27))
3144337
>>> psum(1, 1, x).factor()
x*(x + 1)/2
>>> psum(2, 1, x).factor()
x*(x + 1)*(2*x + 1)/6
>>> psum(3, 1, x).factor()
x**2*(x + 1)**2/4

工具书类

sympy.polys.appellseqs.bernoulli_c_poly(n, x=None, polys=False)[源代码]#

Generates the central Bernoulli polynomial \(\operatorname{B}_n^c(x)\).

These are scaled and shifted versions of the plain Bernoulli polynomials, done in such a way that \(\operatorname{B}_n^c(x)\) is an even or odd function for even or odd \(n\) respectively:

\[\operatorname{B}_n^c(x) = 2^n \operatorname{B}_n \left(\frac{x+1}{2}\right)\]
参数:

n :内景

Degree of the polynomial.

x :可选

多边形 :bool,可选

If True, return a Poly, otherwise (default) return an expression.

sympy.polys.appellseqs.genocchi_poly(n, x=None, polys=False)[源代码]#

Generates the Genocchi polynomial \(\operatorname{G}_n(x)\).

\(\operatorname{G}_n(x)\) is twice the difference between the plain and central Bernoulli polynomials, so has degree \(n-1\):

\[\operatorname{G}_n(x) = 2 (\operatorname{B}_n(x) - \operatorname{B}_n^c(x))\]

The factor of 2 in the definition endows \(\operatorname{G}_n(x)\) with integer coefficients.

参数:

n :内景

Degree of the polynomial plus one.

x :可选

多边形 :bool,可选

If True, return a Poly, otherwise (default) return an expression.

sympy.polys.appellseqs.euler_poly(n, x=None, polys=False)[源代码]#

Generates the Euler polynomial \(\operatorname{E}_n(x)\).

These are scaled and reindexed versions of the Genocchi polynomials:

\[\operatorname{E}_n(x) = -\frac{\operatorname{G}_{n+1}(x)}{n+1}\]
参数:

n :内景

Degree of the polynomial.

x :可选

多边形 :bool,可选

If True, return a Poly, otherwise (default) return an expression.

sympy.polys.appellseqs.andre_poly(n, x=None, polys=False)[源代码]#

Generates the Andre polynomial \(\mathcal{A}_n(x)\).

This is the Appell sequence where the constant coefficients form the sequence of Euler numbers euler(n). As such they have integer coefficients and parities matching the parity of \(n\).

Luschny calls these the Swiss-knife polynomials because their values at 0 and 1 can be simply transformed into both the Bernoulli and Euler numbers. Here they are called the Andre polynomials because \(|\mathcal{A}_n(n\bmod 2)|\) for \(n \ge 0\) generates what Luschny calls the Andre numbers, A000111 in the OEIS.

参数:

n :内景

Degree of the polynomial.

x :可选

多边形 :bool,可选

If True, return a Poly, otherwise (default) return an expression.

实例

>>> from sympy import bernoulli, euler, genocchi
>>> from sympy.abc import x
>>> from sympy.polys import andre_poly
>>> andre_poly(9, x)
x**9 - 36*x**7 + 630*x**5 - 5124*x**3 + 12465*x
>>> [andre_poly(n, 0) for n in range(11)]
[1, 0, -1, 0, 5, 0, -61, 0, 1385, 0, -50521]
>>> [euler(n) for n in range(11)]
[1, 0, -1, 0, 5, 0, -61, 0, 1385, 0, -50521]
>>> [andre_poly(n-1, 1) * n / (4**n - 2**n) for n in range(1, 11)]
[1/2, 1/6, 0, -1/30, 0, 1/42, 0, -1/30, 0, 5/66]
>>> [bernoulli(n) for n in range(1, 11)]
[1/2, 1/6, 0, -1/30, 0, 1/42, 0, -1/30, 0, 5/66]
>>> [-andre_poly(n-1, -1) * n / (-2)**(n-1) for n in range(1, 11)]
[-1, -1, 0, 1, 0, -3, 0, 17, 0, -155]
>>> [genocchi(n) for n in range(1, 11)]
[-1, -1, 0, 1, 0, -3, 0, 17, 0, -155]
>>> [abs(andre_poly(n, n%2)) for n in range(11)]
[1, 1, 1, 2, 5, 16, 61, 272, 1385, 7936, 50521]

工具书类

[R813]

Peter Luschny, "An introduction to the Bernoulli function", https://arxiv.org/abs/2009.06743

有理函数的操作#

sympy.polys.rationaltools.together(expr, deep=False, fraction=True)[源代码]#

使用符号方法对有理表达式进行加密和组合。

此函数获取一个表达式或表达式的容器,并通过对有理子表达式进行归集和组合,将它们组合在一起。并没有采取任何英勇的措施来尽量减少由此产生的分子和分母的程度。完全还原得到表达式 cancel() . 然而, together() 可以在输出中尽可能保留输入表达式的结构(不执行扩展)。

可以把各种各样的对象集合起来,包括各种各样的对象。也可以通过设置 deep 旗到 True .

根据定义, together() 是对 apart() 如此 apart(together(expr)) 应返回expr unchanged。但是请注意 together() 仅使用符号方法,因此可能需要使用 cancel() 进行代数化简,使分子分母的阶数最小。

实例

>>> 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

部分分数分解#

sympy.polys.partfrac.apart(f, x=None, full=False, **options)[源代码]#

计算有理函数的部分分式分解。

给定有理函数 f ,计算 f . 有两种算法:一种是基于欠定系数法的,另一种是Bronstein的全部分分数分解算法。

待定系数法(由 full=False )对分母使用多项式因式分解(因此接受与factor相同的选项)。默认情况下,它在有理数上工作,因此默认情况下不支持分解具有非有理根的分母(例如,无理根、复根)(参见因子选项)。

Bronstein算法可以用 full=True 允许用非有理根分解分母。人类可读的结果可以通过 doit() (见以下示例)。

实例

>>> 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)

当分母根不有理时,待定系数法不提供结果:

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

您可以通过设置 full=True

>>> apart(y/(x**2 + x + 1), x, full=True)
RootSum(_w**2 + _w + 1, Lambda(_a, (-2*_a*y/3 - y/3)/(-_a + x)))

调用 doit() 产生人类可读的结果:

>>> apart(y/(x**2 + x + 1), x, full=True).doit()
(-y/3 - 2*y*(-1/2 - sqrt(3)*I/2)/3)/(x + 1/2 + sqrt(3)*I/2) + (-y/3 -
    2*y*(-1/2 + sqrt(3)*I/2)/3)/(x + 1/2 - sqrt(3)*I/2)
sympy.polys.partfrac.apart_list(f, x=None, dummies=None, **options)[源代码]#

计算有理函数的部分分式分解并以结构化形式返回结果。

给定有理函数 f 计算 f . 该方法只支持Bronstein的完全部分分式分解算法。返回值是高度结构化的,非常适合进一步的算法处理,而不是人类可读的。函数返回包含三个元素的元组:

  • 第一项是公共系数,不含变量 \(x\) 用于分解。(它是基字段的一个元素 \(K\)

  • 第二项是分解的多项式部分。这可以是零多项式。(它是 \(K[x]\)

  • 第三部分本身就是一个四人列表。每个四元组按以下顺序包含以下元素:

    • 多项式(不一定是不可约的) \(D\) 谁的根 \(w_i\) 出现在一组相关分数项的线性分母中。(此项也可以是显式根的列表。然而,目前 apart_list 从不以这种方式返回结果,但是 assemble_partfrac_list 函数接受此格式作为输入。)

    • 分数的分子,写为根的函数 \(w\)

    • 分数的线性分母 不包括它的幂指数 ,写为根的函数 \(w\) .

    • 分母必须提高的幂。

On始终可以使用函数重新生成纯表达式 assemble_partfrac_list .

实例

第一个例子:

>>> from sympy.polys.partfrac import apart_list, assemble_partfrac_list
>>> from sympy.abc import x, t
>>> f = (2*x**3 - 2*x) / (x**2 - 2*x + 1)
>>> pfd = apart_list(f)
>>> pfd
(1,
Poly(2*x + 4, x, domain='ZZ'),
[(Poly(_w - 1, _w, domain='ZZ'), Lambda(_a, 4), Lambda(_a, -_a + x), 1)])
>>> assemble_partfrac_list(pfd)
2*x + 4 + 4/(x - 1)

第二个例子:

>>> f = (-2*x - 2*x**2) / (3*x**2 - 6*x)
>>> pfd = apart_list(f)
>>> pfd
(-1,
Poly(2/3, x, domain='QQ'),
[(Poly(_w - 2, _w, domain='ZZ'), Lambda(_a, 2), Lambda(_a, -_a + x), 1)])
>>> assemble_partfrac_list(pfd)
-2/3 - 2/(x - 2)

另一个示例,显示符号参数:

>>> pfd = apart_list(t/(x**2 + x + t), x)
>>> pfd
(1,
Poly(0, x, domain='ZZ[t]'),
[(Poly(_w**2 + _w + t, _w, domain='ZZ[t]'),
Lambda(_a, -2*_a*t/(4*t - 1) - t/(4*t - 1)),
Lambda(_a, -_a + x),
1)])
>>> assemble_partfrac_list(pfd)
RootSum(_w**2 + _w + t, Lambda(_a, (-2*_a*t/(4*t - 1) - t/(4*t - 1))/(-_a + x)))

这个例子取自Bronstein的原始论文:

>>> f = 36 / (x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2)
>>> pfd = apart_list(f)
>>> pfd
(1,
Poly(0, x, domain='ZZ'),
[(Poly(_w - 2, _w, domain='ZZ'), Lambda(_a, 4), Lambda(_a, -_a + x), 1),
(Poly(_w**2 - 1, _w, domain='ZZ'), Lambda(_a, -3*_a - 6), Lambda(_a, -_a + x), 2),
(Poly(_w + 1, _w, domain='ZZ'), Lambda(_a, -4), Lambda(_a, -_a + x), 1)])
>>> assemble_partfrac_list(pfd)
-4/(x + 1) - 3/(x + 1)**2 - 9/(x - 1)**2 + 4/(x - 2)

工具书类

sympy.polys.partfrac.assemble_partfrac_list(partial_list)[源代码]#

从函数获得的结构化结果重新组合完整的部分分数分解 apart_list .

实例

这个例子取自Bronstein的原始论文:

>>> from sympy.polys.partfrac import apart_list, assemble_partfrac_list
>>> from sympy.abc import x
>>> f = 36 / (x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2)
>>> pfd = apart_list(f)
>>> pfd
(1,
Poly(0, x, domain='ZZ'),
[(Poly(_w - 2, _w, domain='ZZ'), Lambda(_a, 4), Lambda(_a, -_a + x), 1),
(Poly(_w**2 - 1, _w, domain='ZZ'), Lambda(_a, -3*_a - 6), Lambda(_a, -_a + x), 2),
(Poly(_w + 1, _w, domain='ZZ'), Lambda(_a, -4), Lambda(_a, -_a + x), 1)])
>>> assemble_partfrac_list(pfd)
-4/(x + 1) - 3/(x + 1)**2 - 9/(x - 1)**2 + 4/(x - 2)

如果我们碰巧知道一些根,我们可以很容易地在结构内部提供它们:

>>> pfd = apart_list(2/(x**2-2))
>>> pfd
(1,
Poly(0, x, domain='ZZ'),
[(Poly(_w**2 - 2, _w, domain='ZZ'),
Lambda(_a, _a/2),
Lambda(_a, -_a + x),
1)])
>>> pfda = assemble_partfrac_list(pfd)
>>> pfda
RootSum(_w**2 - 2, Lambda(_a, _a/(-_a + x)))/2
>>> pfda.doit()
-sqrt(2)/(2*(x + sqrt(2))) + sqrt(2)/(2*(x - sqrt(2)))
>>> from sympy import Dummy, Poly, Lambda, sqrt
>>> a = Dummy("a")
>>> pfd = (1, Poly(0, x, domain='ZZ'), [([sqrt(2),-sqrt(2)], Lambda(a, a/2), Lambda(a, -a + x), 1)])
>>> assemble_partfrac_list(pfd)
-sqrt(2)/(2*(x + sqrt(2))) + sqrt(2)/(2*(x - sqrt(2)))

参见

apart, apart_list

多项式的色散#

sympy.polys.dispersion.dispersionset(p, q=None, *gens, **args)[源代码]#

计算 分散装置 两个多项式的。

对于两个多项式 \(f(x)\)\(g(x)\) 具有 \(\deg f > 0\)\(\deg g > 0\) 色散集 \(\operatorname{{J}}(f, g)\) 定义为:

\[\{operator名称(f)\]

对于一个多项式 \(\operatorname{{J}}(f) := \operatorname{{J}}(f, f)\) .

实例

>>> from sympy import poly
>>> from sympy.polys.dispersion import dispersion, dispersionset
>>> from sympy.abc import x

一个简单多项式的色散集和色散:

>>> fp = poly((x - 3)*(x + 3), x)
>>> sorted(dispersionset(fp))
[0, 6]
>>> dispersion(fp)
6

注意,色散的定义不是对称的:

>>> 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

计算色散也适用于场扩展:

>>> 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]

我们甚至可以计算具有符号系数的多项式:

>>> 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]

参见

dispersion

工具书类

sympy.polys.dispersion.dispersion(p, q=None, *gens, **args)[源代码]#

计算 分散 多项式。

对于两个多项式 \(f(x)\)\(g(x)\) 具有 \(\deg f > 0\)\(\deg g > 0\) 分散 \(\operatorname{{dis}}(f, g)\) 定义为:

\[\运算符名称{dis}(f,g)\]

对于一个多项式 \(\operatorname{{dis}}(f) := \operatorname{{dis}}(f, f)\) . 请注意,我们定义了 \(\max\{{\}} := -\infty\) .

实例

>>> from sympy import poly
>>> from sympy.polys.dispersion import dispersion, dispersionset
>>> from sympy.abc import x

一个简单多项式的色散集和色散:

>>> fp = poly((x - 3)*(x + 3), x)
>>> sorted(dispersionset(fp))
[0, 6]
>>> dispersion(fp)
6

注意,色散的定义不是对称的:

>>> 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

空集的最大值定义为 \(-\infty\) 如本例所示。

计算色散也适用于场扩展:

>>> 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]

我们甚至可以计算具有符号系数的多项式:

>>> 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]

参见

dispersionset

工具书类