I am done

This commit is contained in:
2024-10-30 22:14:35 +01:00
parent 720dc28c09
commit 40e2a747cf
36901 changed files with 5011519 additions and 0 deletions

View File

@ -0,0 +1,91 @@
"""Tests for efficient functions for generating Appell sequences."""
from sympy.core.numbers import Rational as Q
from sympy.polys.polytools import Poly
from sympy.testing.pytest import raises
from sympy.polys.appellseqs import (bernoulli_poly, bernoulli_c_poly,
euler_poly, genocchi_poly, andre_poly)
from sympy.abc import x
def test_bernoulli_poly():
raises(ValueError, lambda: bernoulli_poly(-1, x))
assert bernoulli_poly(1, x, polys=True) == Poly(x - Q(1,2))
assert bernoulli_poly(0, x) == 1
assert bernoulli_poly(1, x) == x - Q(1,2)
assert bernoulli_poly(2, x) == x**2 - x + Q(1,6)
assert bernoulli_poly(3, x) == x**3 - Q(3,2)*x**2 + Q(1,2)*x
assert bernoulli_poly(4, x) == x**4 - 2*x**3 + x**2 - Q(1,30)
assert bernoulli_poly(5, x) == x**5 - Q(5,2)*x**4 + Q(5,3)*x**3 - Q(1,6)*x
assert bernoulli_poly(6, x) == x**6 - 3*x**5 + Q(5,2)*x**4 - Q(1,2)*x**2 + Q(1,42)
assert bernoulli_poly(1).dummy_eq(x - Q(1,2))
assert bernoulli_poly(1, polys=True) == Poly(x - Q(1,2))
def test_bernoulli_c_poly():
raises(ValueError, lambda: bernoulli_c_poly(-1, x))
assert bernoulli_c_poly(1, x, polys=True) == Poly(x, domain='QQ')
assert bernoulli_c_poly(0, x) == 1
assert bernoulli_c_poly(1, x) == x
assert bernoulli_c_poly(2, x) == x**2 - Q(1,3)
assert bernoulli_c_poly(3, x) == x**3 - x
assert bernoulli_c_poly(4, x) == x**4 - 2*x**2 + Q(7,15)
assert bernoulli_c_poly(5, x) == x**5 - Q(10,3)*x**3 + Q(7,3)*x
assert bernoulli_c_poly(6, x) == x**6 - 5*x**4 + 7*x**2 - Q(31,21)
assert bernoulli_c_poly(1).dummy_eq(x)
assert bernoulli_c_poly(1, polys=True) == Poly(x, domain='QQ')
assert 2**8 * bernoulli_poly(8, (x+1)/2).expand() == bernoulli_c_poly(8, x)
assert 2**9 * bernoulli_poly(9, (x+1)/2).expand() == bernoulli_c_poly(9, x)
def test_genocchi_poly():
raises(ValueError, lambda: genocchi_poly(-1, x))
assert genocchi_poly(2, x, polys=True) == Poly(-2*x + 1)
assert genocchi_poly(0, x) == 0
assert genocchi_poly(1, x) == -1
assert genocchi_poly(2, x) == 1 - 2*x
assert genocchi_poly(3, x) == 3*x - 3*x**2
assert genocchi_poly(4, x) == -1 + 6*x**2 - 4*x**3
assert genocchi_poly(5, x) == -5*x + 10*x**3 - 5*x**4
assert genocchi_poly(6, x) == 3 - 15*x**2 + 15*x**4 - 6*x**5
assert genocchi_poly(2).dummy_eq(-2*x + 1)
assert genocchi_poly(2, polys=True) == Poly(-2*x + 1)
assert 2 * (bernoulli_poly(8, x) - bernoulli_c_poly(8, x)) == genocchi_poly(8, x)
assert 2 * (bernoulli_poly(9, x) - bernoulli_c_poly(9, x)) == genocchi_poly(9, x)
def test_euler_poly():
raises(ValueError, lambda: euler_poly(-1, x))
assert euler_poly(1, x, polys=True) == Poly(x - Q(1,2))
assert euler_poly(0, x) == 1
assert euler_poly(1, x) == x - Q(1,2)
assert euler_poly(2, x) == x**2 - x
assert euler_poly(3, x) == x**3 - Q(3,2)*x**2 + Q(1,4)
assert euler_poly(4, x) == x**4 - 2*x**3 + x
assert euler_poly(5, x) == x**5 - Q(5,2)*x**4 + Q(5,2)*x**2 - Q(1,2)
assert euler_poly(6, x) == x**6 - 3*x**5 + 5*x**3 - 3*x
assert euler_poly(1).dummy_eq(x - Q(1,2))
assert euler_poly(1, polys=True) == Poly(x - Q(1,2))
assert genocchi_poly(9, x) == euler_poly(8, x) * -9
assert genocchi_poly(10, x) == euler_poly(9, x) * -10
def test_andre_poly():
raises(ValueError, lambda: andre_poly(-1, x))
assert andre_poly(1, x, polys=True) == Poly(x)
assert andre_poly(0, x) == 1
assert andre_poly(1, x) == x
assert andre_poly(2, x) == x**2 - 1
assert andre_poly(3, x) == x**3 - 3*x
assert andre_poly(4, x) == x**4 - 6*x**2 + 5
assert andre_poly(5, x) == x**5 - 10*x**3 + 25*x
assert andre_poly(6, x) == x**6 - 15*x**4 + 75*x**2 - 61
assert andre_poly(1).dummy_eq(x)
assert andre_poly(1, polys=True) == Poly(x)

View File

@ -0,0 +1,208 @@
"""Tests for tools for constructing domains for expressions. """
from sympy.polys.constructor import construct_domain
from sympy.polys.domains import ZZ, QQ, ZZ_I, QQ_I, RR, CC, EX
from sympy.polys.domains.realfield import RealField
from sympy.polys.domains.complexfield import ComplexField
from sympy.core import (Catalan, GoldenRatio)
from sympy.core.numbers import (E, Float, I, Rational, pi)
from sympy.core.singleton import S
from sympy.functions.elementary.exponential import exp
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.functions.elementary.trigonometric import sin
from sympy.abc import x, y
def test_construct_domain():
assert construct_domain([1, 2, 3]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)])
assert construct_domain([1, 2, 3], field=True) == (QQ, [QQ(1), QQ(2), QQ(3)])
assert construct_domain([S.One, S(2), S(3)]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)])
assert construct_domain([S.One, S(2), S(3)], field=True) == (QQ, [QQ(1), QQ(2), QQ(3)])
assert construct_domain([S.Half, S(2)]) == (QQ, [QQ(1, 2), QQ(2)])
result = construct_domain([3.14, 1, S.Half])
assert isinstance(result[0], RealField)
assert result[1] == [RR(3.14), RR(1.0), RR(0.5)]
result = construct_domain([3.14, I, S.Half])
assert isinstance(result[0], ComplexField)
assert result[1] == [CC(3.14), CC(1.0j), CC(0.5)]
assert construct_domain([1.0+I]) == (CC, [CC(1.0, 1.0)])
assert construct_domain([2.0+3.0*I]) == (CC, [CC(2.0, 3.0)])
assert construct_domain([1, I]) == (ZZ_I, [ZZ_I(1, 0), ZZ_I(0, 1)])
assert construct_domain([1, I/2]) == (QQ_I, [QQ_I(1, 0), QQ_I(0, S.Half)])
assert construct_domain([3.14, sqrt(2)], extension=None) == (EX, [EX(3.14), EX(sqrt(2))])
assert construct_domain([3.14, sqrt(2)], extension=True) == (EX, [EX(3.14), EX(sqrt(2))])
assert construct_domain([1, sqrt(2)], extension=None) == (EX, [EX(1), EX(sqrt(2))])
assert construct_domain([x, sqrt(x)]) == (EX, [EX(x), EX(sqrt(x))])
assert construct_domain([x, sqrt(x), sqrt(y)]) == (EX, [EX(x), EX(sqrt(x)), EX(sqrt(y))])
alg = QQ.algebraic_field(sqrt(2))
assert construct_domain([7, S.Half, sqrt(2)], extension=True) == \
(alg, [alg.convert(7), alg.convert(S.Half), alg.convert(sqrt(2))])
alg = QQ.algebraic_field(sqrt(2) + sqrt(3))
assert construct_domain([7, sqrt(2), sqrt(3)], extension=True) == \
(alg, [alg.convert(7), alg.convert(sqrt(2)), alg.convert(sqrt(3))])
dom = ZZ[x]
assert construct_domain([2*x, 3]) == \
(dom, [dom.convert(2*x), dom.convert(3)])
dom = ZZ[x, y]
assert construct_domain([2*x, 3*y]) == \
(dom, [dom.convert(2*x), dom.convert(3*y)])
dom = QQ[x]
assert construct_domain([x/2, 3]) == \
(dom, [dom.convert(x/2), dom.convert(3)])
dom = QQ[x, y]
assert construct_domain([x/2, 3*y]) == \
(dom, [dom.convert(x/2), dom.convert(3*y)])
dom = ZZ_I[x]
assert construct_domain([2*x, I]) == \
(dom, [dom.convert(2*x), dom.convert(I)])
dom = ZZ_I[x, y]
assert construct_domain([2*x, I*y]) == \
(dom, [dom.convert(2*x), dom.convert(I*y)])
dom = QQ_I[x]
assert construct_domain([x/2, I]) == \
(dom, [dom.convert(x/2), dom.convert(I)])
dom = QQ_I[x, y]
assert construct_domain([x/2, I*y]) == \
(dom, [dom.convert(x/2), dom.convert(I*y)])
dom = RR[x]
assert construct_domain([x/2, 3.5]) == \
(dom, [dom.convert(x/2), dom.convert(3.5)])
dom = RR[x, y]
assert construct_domain([x/2, 3.5*y]) == \
(dom, [dom.convert(x/2), dom.convert(3.5*y)])
dom = CC[x]
assert construct_domain([I*x/2, 3.5]) == \
(dom, [dom.convert(I*x/2), dom.convert(3.5)])
dom = CC[x, y]
assert construct_domain([I*x/2, 3.5*y]) == \
(dom, [dom.convert(I*x/2), dom.convert(3.5*y)])
dom = CC[x]
assert construct_domain([x/2, I*3.5]) == \
(dom, [dom.convert(x/2), dom.convert(I*3.5)])
dom = CC[x, y]
assert construct_domain([x/2, I*3.5*y]) == \
(dom, [dom.convert(x/2), dom.convert(I*3.5*y)])
dom = ZZ.frac_field(x)
assert construct_domain([2/x, 3]) == \
(dom, [dom.convert(2/x), dom.convert(3)])
dom = ZZ.frac_field(x, y)
assert construct_domain([2/x, 3*y]) == \
(dom, [dom.convert(2/x), dom.convert(3*y)])
dom = RR.frac_field(x)
assert construct_domain([2/x, 3.5]) == \
(dom, [dom.convert(2/x), dom.convert(3.5)])
dom = RR.frac_field(x, y)
assert construct_domain([2/x, 3.5*y]) == \
(dom, [dom.convert(2/x), dom.convert(3.5*y)])
dom = RealField(prec=336)[x]
assert construct_domain([pi.evalf(100)*x]) == \
(dom, [dom.convert(pi.evalf(100)*x)])
assert construct_domain(2) == (ZZ, ZZ(2))
assert construct_domain(S(2)/3) == (QQ, QQ(2, 3))
assert construct_domain(Rational(2, 3)) == (QQ, QQ(2, 3))
assert construct_domain({}) == (ZZ, {})
def test_complex_exponential():
w = exp(-I*2*pi/3, evaluate=False)
alg = QQ.algebraic_field(w)
assert construct_domain([w**2, w, 1], extension=True) == (
alg,
[alg.convert(w**2),
alg.convert(w),
alg.convert(1)]
)
def test_composite_option():
assert construct_domain({(1,): sin(y)}, composite=False) == \
(EX, {(1,): EX(sin(y))})
assert construct_domain({(1,): y}, composite=False) == \
(EX, {(1,): EX(y)})
assert construct_domain({(1, 1): 1}, composite=False) == \
(ZZ, {(1, 1): 1})
assert construct_domain({(1, 0): y}, composite=False) == \
(EX, {(1, 0): EX(y)})
def test_precision():
f1 = Float("1.01")
f2 = Float("1.0000000000000000000001")
for u in [1, 1e-2, 1e-6, 1e-13, 1e-14, 1e-16, 1e-20, 1e-100, 1e-300,
f1, f2]:
result = construct_domain([u])
v = float(result[1][0])
assert abs(u - v) / u < 1e-14 # Test relative accuracy
result = construct_domain([f1])
y = result[1][0]
assert y-1 > 1e-50
result = construct_domain([f2])
y = result[1][0]
assert y-1 > 1e-50
def test_issue_11538():
for n in [E, pi, Catalan]:
assert construct_domain(n)[0] == ZZ[n]
assert construct_domain(x + n)[0] == ZZ[x, n]
assert construct_domain(GoldenRatio)[0] == EX
assert construct_domain(x + GoldenRatio)[0] == EX

View File

@ -0,0 +1,997 @@
"""Tests for dense recursive polynomials' arithmetics. """
from sympy.external.gmpy import GROUND_TYPES
from sympy.polys.densebasic import (
dup_normal, dmp_normal,
)
from sympy.polys.densearith import (
dup_add_term, dmp_add_term,
dup_sub_term, dmp_sub_term,
dup_mul_term, dmp_mul_term,
dup_add_ground, dmp_add_ground,
dup_sub_ground, dmp_sub_ground,
dup_mul_ground, dmp_mul_ground,
dup_quo_ground, dmp_quo_ground,
dup_exquo_ground, dmp_exquo_ground,
dup_lshift, dup_rshift,
dup_abs, dmp_abs,
dup_neg, dmp_neg,
dup_add, dmp_add,
dup_sub, dmp_sub,
dup_mul, dmp_mul,
dup_sqr, dmp_sqr,
dup_pow, dmp_pow,
dup_add_mul, dmp_add_mul,
dup_sub_mul, dmp_sub_mul,
dup_pdiv, dup_prem, dup_pquo, dup_pexquo,
dmp_pdiv, dmp_prem, dmp_pquo, dmp_pexquo,
dup_rr_div, dmp_rr_div,
dup_ff_div, dmp_ff_div,
dup_div, dup_rem, dup_quo, dup_exquo,
dmp_div, dmp_rem, dmp_quo, dmp_exquo,
dup_max_norm, dmp_max_norm,
dup_l1_norm, dmp_l1_norm,
dup_l2_norm_squared, dmp_l2_norm_squared,
dup_expand, dmp_expand,
)
from sympy.polys.polyerrors import (
ExactQuotientFailed,
)
from sympy.polys.specialpolys import f_polys
from sympy.polys.domains import FF, ZZ, QQ
from sympy.testing.pytest import raises
f_0, f_1, f_2, f_3, f_4, f_5, f_6 = [ f.to_dense() for f in f_polys() ]
F_0 = dmp_mul_ground(dmp_normal(f_0, 2, QQ), QQ(1, 7), 2, QQ)
def test_dup_add_term():
f = dup_normal([], ZZ)
assert dup_add_term(f, ZZ(0), 0, ZZ) == dup_normal([], ZZ)
assert dup_add_term(f, ZZ(1), 0, ZZ) == dup_normal([1], ZZ)
assert dup_add_term(f, ZZ(1), 1, ZZ) == dup_normal([1, 0], ZZ)
assert dup_add_term(f, ZZ(1), 2, ZZ) == dup_normal([1, 0, 0], ZZ)
f = dup_normal([1, 1, 1], ZZ)
assert dup_add_term(f, ZZ(1), 0, ZZ) == dup_normal([1, 1, 2], ZZ)
assert dup_add_term(f, ZZ(1), 1, ZZ) == dup_normal([1, 2, 1], ZZ)
assert dup_add_term(f, ZZ(1), 2, ZZ) == dup_normal([2, 1, 1], ZZ)
assert dup_add_term(f, ZZ(1), 3, ZZ) == dup_normal([1, 1, 1, 1], ZZ)
assert dup_add_term(f, ZZ(1), 4, ZZ) == dup_normal([1, 0, 1, 1, 1], ZZ)
assert dup_add_term(f, ZZ(1), 5, ZZ) == dup_normal([1, 0, 0, 1, 1, 1], ZZ)
assert dup_add_term(
f, ZZ(1), 6, ZZ) == dup_normal([1, 0, 0, 0, 1, 1, 1], ZZ)
assert dup_add_term(f, ZZ(-1), 2, ZZ) == dup_normal([1, 1], ZZ)
def test_dmp_add_term():
assert dmp_add_term([ZZ(1), ZZ(1), ZZ(1)], ZZ(1), 2, 0, ZZ) == \
dup_add_term([ZZ(1), ZZ(1), ZZ(1)], ZZ(1), 2, ZZ)
assert dmp_add_term(f_0, [[]], 3, 2, ZZ) == f_0
assert dmp_add_term(F_0, [[]], 3, 2, QQ) == F_0
def test_dup_sub_term():
f = dup_normal([], ZZ)
assert dup_sub_term(f, ZZ(0), 0, ZZ) == dup_normal([], ZZ)
assert dup_sub_term(f, ZZ(1), 0, ZZ) == dup_normal([-1], ZZ)
assert dup_sub_term(f, ZZ(1), 1, ZZ) == dup_normal([-1, 0], ZZ)
assert dup_sub_term(f, ZZ(1), 2, ZZ) == dup_normal([-1, 0, 0], ZZ)
f = dup_normal([1, 1, 1], ZZ)
assert dup_sub_term(f, ZZ(2), 0, ZZ) == dup_normal([ 1, 1, -1], ZZ)
assert dup_sub_term(f, ZZ(2), 1, ZZ) == dup_normal([ 1, -1, 1], ZZ)
assert dup_sub_term(f, ZZ(2), 2, ZZ) == dup_normal([-1, 1, 1], ZZ)
assert dup_sub_term(f, ZZ(1), 3, ZZ) == dup_normal([-1, 1, 1, 1], ZZ)
assert dup_sub_term(f, ZZ(1), 4, ZZ) == dup_normal([-1, 0, 1, 1, 1], ZZ)
assert dup_sub_term(f, ZZ(1), 5, ZZ) == dup_normal([-1, 0, 0, 1, 1, 1], ZZ)
assert dup_sub_term(
f, ZZ(1), 6, ZZ) == dup_normal([-1, 0, 0, 0, 1, 1, 1], ZZ)
assert dup_sub_term(f, ZZ(1), 2, ZZ) == dup_normal([1, 1], ZZ)
def test_dmp_sub_term():
assert dmp_sub_term([ZZ(1), ZZ(1), ZZ(1)], ZZ(1), 2, 0, ZZ) == \
dup_sub_term([ZZ(1), ZZ(1), ZZ(1)], ZZ(1), 2, ZZ)
assert dmp_sub_term(f_0, [[]], 3, 2, ZZ) == f_0
assert dmp_sub_term(F_0, [[]], 3, 2, QQ) == F_0
def test_dup_mul_term():
f = dup_normal([], ZZ)
assert dup_mul_term(f, ZZ(2), 3, ZZ) == dup_normal([], ZZ)
f = dup_normal([1, 1], ZZ)
assert dup_mul_term(f, ZZ(0), 3, ZZ) == dup_normal([], ZZ)
f = dup_normal([1, 2, 3], ZZ)
assert dup_mul_term(f, ZZ(2), 0, ZZ) == dup_normal([2, 4, 6], ZZ)
assert dup_mul_term(f, ZZ(2), 1, ZZ) == dup_normal([2, 4, 6, 0], ZZ)
assert dup_mul_term(f, ZZ(2), 2, ZZ) == dup_normal([2, 4, 6, 0, 0], ZZ)
assert dup_mul_term(f, ZZ(2), 3, ZZ) == dup_normal([2, 4, 6, 0, 0, 0], ZZ)
def test_dmp_mul_term():
assert dmp_mul_term([ZZ(1), ZZ(2), ZZ(3)], ZZ(2), 1, 0, ZZ) == \
dup_mul_term([ZZ(1), ZZ(2), ZZ(3)], ZZ(2), 1, ZZ)
assert dmp_mul_term([[]], [ZZ(2)], 3, 1, ZZ) == [[]]
assert dmp_mul_term([[ZZ(1)]], [], 3, 1, ZZ) == [[]]
assert dmp_mul_term([[ZZ(1), ZZ(2)], [ZZ(3)]], [ZZ(2)], 2, 1, ZZ) == \
[[ZZ(2), ZZ(4)], [ZZ(6)], [], []]
assert dmp_mul_term([[]], [QQ(2, 3)], 3, 1, QQ) == [[]]
assert dmp_mul_term([[QQ(1, 2)]], [], 3, 1, QQ) == [[]]
assert dmp_mul_term([[QQ(1, 5), QQ(2, 5)], [QQ(3, 5)]], [QQ(2, 3)], 2, 1, QQ) == \
[[QQ(2, 15), QQ(4, 15)], [QQ(6, 15)], [], []]
def test_dup_add_ground():
f = ZZ.map([1, 2, 3, 4])
g = ZZ.map([1, 2, 3, 8])
assert dup_add_ground(f, ZZ(4), ZZ) == g
def test_dmp_add_ground():
f = ZZ.map([[1], [2], [3], [4]])
g = ZZ.map([[1], [2], [3], [8]])
assert dmp_add_ground(f, ZZ(4), 1, ZZ) == g
def test_dup_sub_ground():
f = ZZ.map([1, 2, 3, 4])
g = ZZ.map([1, 2, 3, 0])
assert dup_sub_ground(f, ZZ(4), ZZ) == g
def test_dmp_sub_ground():
f = ZZ.map([[1], [2], [3], [4]])
g = ZZ.map([[1], [2], [3], []])
assert dmp_sub_ground(f, ZZ(4), 1, ZZ) == g
def test_dup_mul_ground():
f = dup_normal([], ZZ)
assert dup_mul_ground(f, ZZ(2), ZZ) == dup_normal([], ZZ)
f = dup_normal([1, 2, 3], ZZ)
assert dup_mul_ground(f, ZZ(0), ZZ) == dup_normal([], ZZ)
assert dup_mul_ground(f, ZZ(2), ZZ) == dup_normal([2, 4, 6], ZZ)
def test_dmp_mul_ground():
assert dmp_mul_ground(f_0, ZZ(2), 2, ZZ) == [
[[ZZ(2), ZZ(4), ZZ(6)], [ZZ(4)]],
[[ZZ(6)]],
[[ZZ(8), ZZ(10), ZZ(12)], [ZZ(2), ZZ(4), ZZ(2)], [ZZ(2)]]
]
assert dmp_mul_ground(F_0, QQ(1, 2), 2, QQ) == [
[[QQ(1, 14), QQ(2, 14), QQ(3, 14)], [QQ(2, 14)]],
[[QQ(3, 14)]],
[[QQ(4, 14), QQ(5, 14), QQ(6, 14)], [QQ(1, 14), QQ(2, 14),
QQ(1, 14)], [QQ(1, 14)]]
]
def test_dup_quo_ground():
raises(ZeroDivisionError, lambda: dup_quo_ground(dup_normal([1, 2,
3], ZZ), ZZ(0), ZZ))
f = dup_normal([], ZZ)
assert dup_quo_ground(f, ZZ(3), ZZ) == dup_normal([], ZZ)
f = dup_normal([6, 2, 8], ZZ)
assert dup_quo_ground(f, ZZ(1), ZZ) == f
assert dup_quo_ground(f, ZZ(2), ZZ) == dup_normal([3, 1, 4], ZZ)
assert dup_quo_ground(f, ZZ(3), ZZ) == dup_normal([2, 0, 2], ZZ)
f = dup_normal([6, 2, 8], QQ)
assert dup_quo_ground(f, QQ(1), QQ) == f
assert dup_quo_ground(f, QQ(2), QQ) == [QQ(3), QQ(1), QQ(4)]
assert dup_quo_ground(f, QQ(7), QQ) == [QQ(6, 7), QQ(2, 7), QQ(8, 7)]
def test_dup_exquo_ground():
raises(ZeroDivisionError, lambda: dup_exquo_ground(dup_normal([1,
2, 3], ZZ), ZZ(0), ZZ))
raises(ExactQuotientFailed, lambda: dup_exquo_ground(dup_normal([1,
2, 3], ZZ), ZZ(3), ZZ))
f = dup_normal([], ZZ)
assert dup_exquo_ground(f, ZZ(3), ZZ) == dup_normal([], ZZ)
f = dup_normal([6, 2, 8], ZZ)
assert dup_exquo_ground(f, ZZ(1), ZZ) == f
assert dup_exquo_ground(f, ZZ(2), ZZ) == dup_normal([3, 1, 4], ZZ)
f = dup_normal([6, 2, 8], QQ)
assert dup_exquo_ground(f, QQ(1), QQ) == f
assert dup_exquo_ground(f, QQ(2), QQ) == [QQ(3), QQ(1), QQ(4)]
assert dup_exquo_ground(f, QQ(7), QQ) == [QQ(6, 7), QQ(2, 7), QQ(8, 7)]
def test_dmp_quo_ground():
f = dmp_normal([[6], [2], [8]], 1, ZZ)
assert dmp_quo_ground(f, ZZ(1), 1, ZZ) == f
assert dmp_quo_ground(
f, ZZ(2), 1, ZZ) == dmp_normal([[3], [1], [4]], 1, ZZ)
assert dmp_normal(dmp_quo_ground(
f, ZZ(3), 1, ZZ), 1, ZZ) == dmp_normal([[2], [], [2]], 1, ZZ)
def test_dmp_exquo_ground():
f = dmp_normal([[6], [2], [8]], 1, ZZ)
assert dmp_exquo_ground(f, ZZ(1), 1, ZZ) == f
assert dmp_exquo_ground(
f, ZZ(2), 1, ZZ) == dmp_normal([[3], [1], [4]], 1, ZZ)
def test_dup_lshift():
assert dup_lshift([], 3, ZZ) == []
assert dup_lshift([1], 3, ZZ) == [1, 0, 0, 0]
def test_dup_rshift():
assert dup_rshift([], 3, ZZ) == []
assert dup_rshift([1, 0, 0, 0], 3, ZZ) == [1]
def test_dup_abs():
assert dup_abs([], ZZ) == []
assert dup_abs([ZZ( 1)], ZZ) == [ZZ(1)]
assert dup_abs([ZZ(-7)], ZZ) == [ZZ(7)]
assert dup_abs([ZZ(-1), ZZ(2), ZZ(3)], ZZ) == [ZZ(1), ZZ(2), ZZ(3)]
assert dup_abs([], QQ) == []
assert dup_abs([QQ( 1, 2)], QQ) == [QQ(1, 2)]
assert dup_abs([QQ(-7, 3)], QQ) == [QQ(7, 3)]
assert dup_abs(
[QQ(-1, 7), QQ(2, 7), QQ(3, 7)], QQ) == [QQ(1, 7), QQ(2, 7), QQ(3, 7)]
def test_dmp_abs():
assert dmp_abs([ZZ(-1)], 0, ZZ) == [ZZ(1)]
assert dmp_abs([QQ(-1, 2)], 0, QQ) == [QQ(1, 2)]
assert dmp_abs([[[]]], 2, ZZ) == [[[]]]
assert dmp_abs([[[ZZ(1)]]], 2, ZZ) == [[[ZZ(1)]]]
assert dmp_abs([[[ZZ(-7)]]], 2, ZZ) == [[[ZZ(7)]]]
assert dmp_abs([[[]]], 2, QQ) == [[[]]]
assert dmp_abs([[[QQ(1, 2)]]], 2, QQ) == [[[QQ(1, 2)]]]
assert dmp_abs([[[QQ(-7, 9)]]], 2, QQ) == [[[QQ(7, 9)]]]
def test_dup_neg():
assert dup_neg([], ZZ) == []
assert dup_neg([ZZ(1)], ZZ) == [ZZ(-1)]
assert dup_neg([ZZ(-7)], ZZ) == [ZZ(7)]
assert dup_neg([ZZ(-1), ZZ(2), ZZ(3)], ZZ) == [ZZ(1), ZZ(-2), ZZ(-3)]
assert dup_neg([], QQ) == []
assert dup_neg([QQ(1, 2)], QQ) == [QQ(-1, 2)]
assert dup_neg([QQ(-7, 9)], QQ) == [QQ(7, 9)]
assert dup_neg([QQ(
-1, 7), QQ(2, 7), QQ(3, 7)], QQ) == [QQ(1, 7), QQ(-2, 7), QQ(-3, 7)]
def test_dmp_neg():
assert dmp_neg([ZZ(-1)], 0, ZZ) == [ZZ(1)]
assert dmp_neg([QQ(-1, 2)], 0, QQ) == [QQ(1, 2)]
assert dmp_neg([[[]]], 2, ZZ) == [[[]]]
assert dmp_neg([[[ZZ(1)]]], 2, ZZ) == [[[ZZ(-1)]]]
assert dmp_neg([[[ZZ(-7)]]], 2, ZZ) == [[[ZZ(7)]]]
assert dmp_neg([[[]]], 2, QQ) == [[[]]]
assert dmp_neg([[[QQ(1, 9)]]], 2, QQ) == [[[QQ(-1, 9)]]]
assert dmp_neg([[[QQ(-7, 9)]]], 2, QQ) == [[[QQ(7, 9)]]]
def test_dup_add():
assert dup_add([], [], ZZ) == []
assert dup_add([ZZ(1)], [], ZZ) == [ZZ(1)]
assert dup_add([], [ZZ(1)], ZZ) == [ZZ(1)]
assert dup_add([ZZ(1)], [ZZ(1)], ZZ) == [ZZ(2)]
assert dup_add([ZZ(1)], [ZZ(2)], ZZ) == [ZZ(3)]
assert dup_add([ZZ(1), ZZ(2)], [ZZ(1)], ZZ) == [ZZ(1), ZZ(3)]
assert dup_add([ZZ(1)], [ZZ(1), ZZ(2)], ZZ) == [ZZ(1), ZZ(3)]
assert dup_add([ZZ(1), ZZ(
2), ZZ(3)], [ZZ(8), ZZ(9), ZZ(10)], ZZ) == [ZZ(9), ZZ(11), ZZ(13)]
assert dup_add([], [], QQ) == []
assert dup_add([QQ(1, 2)], [], QQ) == [QQ(1, 2)]
assert dup_add([], [QQ(1, 2)], QQ) == [QQ(1, 2)]
assert dup_add([QQ(1, 4)], [QQ(1, 4)], QQ) == [QQ(1, 2)]
assert dup_add([QQ(1, 4)], [QQ(1, 2)], QQ) == [QQ(3, 4)]
assert dup_add([QQ(1, 2), QQ(2, 3)], [QQ(1)], QQ) == [QQ(1, 2), QQ(5, 3)]
assert dup_add([QQ(1)], [QQ(1, 2), QQ(2, 3)], QQ) == [QQ(1, 2), QQ(5, 3)]
assert dup_add([QQ(1, 7), QQ(2, 7), QQ(3, 7)], [QQ(
8, 7), QQ(9, 7), QQ(10, 7)], QQ) == [QQ(9, 7), QQ(11, 7), QQ(13, 7)]
def test_dmp_add():
assert dmp_add([ZZ(1), ZZ(2)], [ZZ(1)], 0, ZZ) == \
dup_add([ZZ(1), ZZ(2)], [ZZ(1)], ZZ)
assert dmp_add([QQ(1, 2), QQ(2, 3)], [QQ(1)], 0, QQ) == \
dup_add([QQ(1, 2), QQ(2, 3)], [QQ(1)], QQ)
assert dmp_add([[[]]], [[[]]], 2, ZZ) == [[[]]]
assert dmp_add([[[ZZ(1)]]], [[[]]], 2, ZZ) == [[[ZZ(1)]]]
assert dmp_add([[[]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(1)]]]
assert dmp_add([[[ZZ(2)]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(3)]]]
assert dmp_add([[[ZZ(1)]]], [[[ZZ(2)]]], 2, ZZ) == [[[ZZ(3)]]]
assert dmp_add([[[]]], [[[]]], 2, QQ) == [[[]]]
assert dmp_add([[[QQ(1, 2)]]], [[[]]], 2, QQ) == [[[QQ(1, 2)]]]
assert dmp_add([[[]]], [[[QQ(1, 2)]]], 2, QQ) == [[[QQ(1, 2)]]]
assert dmp_add([[[QQ(2, 7)]]], [[[QQ(1, 7)]]], 2, QQ) == [[[QQ(3, 7)]]]
assert dmp_add([[[QQ(1, 7)]]], [[[QQ(2, 7)]]], 2, QQ) == [[[QQ(3, 7)]]]
def test_dup_sub():
assert dup_sub([], [], ZZ) == []
assert dup_sub([ZZ(1)], [], ZZ) == [ZZ(1)]
assert dup_sub([], [ZZ(1)], ZZ) == [ZZ(-1)]
assert dup_sub([ZZ(1)], [ZZ(1)], ZZ) == []
assert dup_sub([ZZ(1)], [ZZ(2)], ZZ) == [ZZ(-1)]
assert dup_sub([ZZ(1), ZZ(2)], [ZZ(1)], ZZ) == [ZZ(1), ZZ(1)]
assert dup_sub([ZZ(1)], [ZZ(1), ZZ(2)], ZZ) == [ZZ(-1), ZZ(-1)]
assert dup_sub([ZZ(3), ZZ(
2), ZZ(1)], [ZZ(8), ZZ(9), ZZ(10)], ZZ) == [ZZ(-5), ZZ(-7), ZZ(-9)]
assert dup_sub([], [], QQ) == []
assert dup_sub([QQ(1, 2)], [], QQ) == [QQ(1, 2)]
assert dup_sub([], [QQ(1, 2)], QQ) == [QQ(-1, 2)]
assert dup_sub([QQ(1, 3)], [QQ(1, 3)], QQ) == []
assert dup_sub([QQ(1, 3)], [QQ(2, 3)], QQ) == [QQ(-1, 3)]
assert dup_sub([QQ(1, 7), QQ(2, 7)], [QQ(1)], QQ) == [QQ(1, 7), QQ(-5, 7)]
assert dup_sub([QQ(1)], [QQ(1, 7), QQ(2, 7)], QQ) == [QQ(-1, 7), QQ(5, 7)]
assert dup_sub([QQ(3, 7), QQ(2, 7), QQ(1, 7)], [QQ(
8, 7), QQ(9, 7), QQ(10, 7)], QQ) == [QQ(-5, 7), QQ(-7, 7), QQ(-9, 7)]
def test_dmp_sub():
assert dmp_sub([ZZ(1), ZZ(2)], [ZZ(1)], 0, ZZ) == \
dup_sub([ZZ(1), ZZ(2)], [ZZ(1)], ZZ)
assert dmp_sub([QQ(1, 2), QQ(2, 3)], [QQ(1)], 0, QQ) == \
dup_sub([QQ(1, 2), QQ(2, 3)], [QQ(1)], QQ)
assert dmp_sub([[[]]], [[[]]], 2, ZZ) == [[[]]]
assert dmp_sub([[[ZZ(1)]]], [[[]]], 2, ZZ) == [[[ZZ(1)]]]
assert dmp_sub([[[]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(-1)]]]
assert dmp_sub([[[ZZ(2)]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(1)]]]
assert dmp_sub([[[ZZ(1)]]], [[[ZZ(2)]]], 2, ZZ) == [[[ZZ(-1)]]]
assert dmp_sub([[[]]], [[[]]], 2, QQ) == [[[]]]
assert dmp_sub([[[QQ(1, 2)]]], [[[]]], 2, QQ) == [[[QQ(1, 2)]]]
assert dmp_sub([[[]]], [[[QQ(1, 2)]]], 2, QQ) == [[[QQ(-1, 2)]]]
assert dmp_sub([[[QQ(2, 7)]]], [[[QQ(1, 7)]]], 2, QQ) == [[[QQ(1, 7)]]]
assert dmp_sub([[[QQ(1, 7)]]], [[[QQ(2, 7)]]], 2, QQ) == [[[QQ(-1, 7)]]]
def test_dup_add_mul():
assert dup_add_mul([ZZ(1), ZZ(2), ZZ(3)], [ZZ(3), ZZ(2), ZZ(1)],
[ZZ(1), ZZ(2)], ZZ) == [ZZ(3), ZZ(9), ZZ(7), ZZ(5)]
assert dmp_add_mul([[ZZ(1), ZZ(2)], [ZZ(3)]], [[ZZ(3)], [ZZ(2), ZZ(1)]],
[[ZZ(1)], [ZZ(2)]], 1, ZZ) == [[ZZ(3)], [ZZ(3), ZZ(9)], [ZZ(4), ZZ(5)]]
def test_dup_sub_mul():
assert dup_sub_mul([ZZ(1), ZZ(2), ZZ(3)], [ZZ(3), ZZ(2), ZZ(1)],
[ZZ(1), ZZ(2)], ZZ) == [ZZ(-3), ZZ(-7), ZZ(-3), ZZ(1)]
assert dmp_sub_mul([[ZZ(1), ZZ(2)], [ZZ(3)]], [[ZZ(3)], [ZZ(2), ZZ(1)]],
[[ZZ(1)], [ZZ(2)]], 1, ZZ) == [[ZZ(-3)], [ZZ(-1), ZZ(-5)], [ZZ(-4), ZZ(1)]]
def test_dup_mul():
assert dup_mul([], [], ZZ) == []
assert dup_mul([], [ZZ(1)], ZZ) == []
assert dup_mul([ZZ(1)], [], ZZ) == []
assert dup_mul([ZZ(1)], [ZZ(1)], ZZ) == [ZZ(1)]
assert dup_mul([ZZ(5)], [ZZ(7)], ZZ) == [ZZ(35)]
assert dup_mul([], [], QQ) == []
assert dup_mul([], [QQ(1, 2)], QQ) == []
assert dup_mul([QQ(1, 2)], [], QQ) == []
assert dup_mul([QQ(1, 2)], [QQ(4, 7)], QQ) == [QQ(2, 7)]
assert dup_mul([QQ(5, 7)], [QQ(3, 7)], QQ) == [QQ(15, 49)]
f = dup_normal([3, 0, 0, 6, 1, 2], ZZ)
g = dup_normal([4, 0, 1, 0], ZZ)
h = dup_normal([12, 0, 3, 24, 4, 14, 1, 2, 0], ZZ)
assert dup_mul(f, g, ZZ) == h
assert dup_mul(g, f, ZZ) == h
f = dup_normal([2, 0, 0, 1, 7], ZZ)
h = dup_normal([4, 0, 0, 4, 28, 0, 1, 14, 49], ZZ)
assert dup_mul(f, f, ZZ) == h
K = FF(6)
assert dup_mul([K(2), K(1)], [K(3), K(4)], K) == [K(5), K(4)]
p1 = dup_normal([79, -1, 78, -94, -10, 11, 32, -19, 78, 2, -89, 30, 73, 42,
85, 77, 83, -30, -34, -2, 95, -81, 37, -49, -46, -58, -16, 37, 35, -11,
-57, -15, -31, 67, -20, 27, 76, 2, 70, 67, -65, 65, -26, -93, -44, -12,
-92, 57, -90, -57, -11, -67, -98, -69, 97, -41, 89, 33, 89, -50, 81,
-31, 60, -27, 43, 29, -77, 44, 21, -91, 32, -57, 33, 3, 53, -51, -38,
-99, -84, 23, -50, 66, -100, 1, -75, -25, 27, -60, 98, -51, -87, 6, 8,
78, -28, -95, -88, 12, -35, 26, -9, 16, -92, 55, -7, -86, 68, -39, -46,
84, 94, 45, 60, 92, 68, -75, -74, -19, 8, 75, 78, 91, 57, 34, 14, -3,
-49, 65, 78, -18, 6, -29, -80, -98, 17, 13, 58, 21, 20, 9, 37, 7, -30,
-53, -20, 34, 67, -42, 89, -22, 73, 43, -6, 5, 51, -8, -15, -52, -22,
-58, -72, -3, 43, -92, 82, 83, -2, -13, -23, -60, 16, -94, -8, -28,
-95, -72, 63, -90, 76, 6, -43, -100, -59, 76, 3, 3, 46, -85, 75, 62,
-71, -76, 88, 97, -72, -1, 30, -64, 72, -48, 14, -78, 58, 63, -91, 24,
-87, -27, -80, -100, -44, 98, 70, 100, -29, -38, 11, 77, 100, 52, 86,
65, -5, -42, -81, -38, -42, 43, -2, -70, -63, -52], ZZ)
p2 = dup_normal([65, -19, -47, 1, 90, 81, -15, -34, 25, -75, 9, -83, 50, -5,
-44, 31, 1, 70, -7, 78, 74, 80, 85, 65, 21, 41, 66, 19, -40, 63, -21,
-27, 32, 69, 83, 34, -35, 14, 81, 57, -75, 32, -67, -89, -100, -61, 46,
84, -78, -29, -50, -94, -24, -32, -68, -16, 100, -7, -72, -89, 35, 82,
58, 81, -92, 62, 5, -47, -39, -58, -72, -13, 84, 44, 55, -25, 48, -54,
-31, -56, -11, -50, -84, 10, 67, 17, 13, -14, 61, 76, -64, -44, -40,
-96, 11, -11, -94, 2, 6, 27, -6, 68, -54, 66, -74, -14, -1, -24, -73,
96, 89, -11, -89, 56, -53, 72, -43, 96, 25, 63, -31, 29, 68, 83, 91,
-93, -19, -38, -40, 40, -12, -19, -79, 44, 100, -66, -29, -77, 62, 39,
-8, 11, -97, 14, 87, 64, 21, -18, 13, 15, -59, -75, -99, -88, 57, 54,
56, -67, 6, -63, -59, -14, 28, 87, -20, -39, 84, -91, -2, 49, -75, 11,
-24, -95, 36, 66, 5, 25, -72, -40, 86, 90, 37, -33, 57, -35, 29, -18,
4, -79, 64, -17, -27, 21, 29, -5, -44, -87, -24, 52, 78, 11, -23, -53,
36, 42, 21, -68, 94, -91, -51, -21, 51, -76, 72, 31, 24, -48, -80, -9,
37, -47, -6, -8, -63, -91, 79, -79, -100, 38, -20, 38, 100, 83, -90,
87, 63, -36, 82, -19, 18, -98, -38, 26, 98, -70, 79, 92, 12, 12, 70,
74, 36, 48, -13, 31, 31, -47, -71, -12, -64, 36, -42, 32, -86, 60, 83,
70, 55, 0, 1, 29, -35, 8, -82, 8, -73, -46, -50, 43, 48, -5, -86, -72,
44, -90, 19, 19, 5, -20, 97, -13, -66, -5, 5, -69, 64, -30, 41, 51, 36,
13, -99, -61, 94, -12, 74, 98, 68, 24, 46, -97, -87, -6, -27, 82, 62,
-11, -77, 86, 66, -47, -49, -50, 13, 18, 89, -89, 46, -80, 13, 98, -35,
-36, -25, 12, 20, 26, -52, 79, 27, 79, 100, 8, 62, -58, -28, 37], ZZ)
res = dup_normal([5135, -1566, 1376, -7466, 4579, 11710, 8001, -7183,
-3737, -7439, 345, -10084, 24522, -1201, 1070, -10245, 9582, 9264,
1903, 23312, 18953, 10037, -15268, -5450, 6442, -6243, -3777, 5110,
10936, -16649, -6022, 16255, 31300, 24818, 31922, 32760, 7854, 27080,
15766, 29596, 7139, 31945, -19810, 465, -38026, -3971, 9641, 465,
-19375, 5524, -30112, -11960, -12813, 13535, 30670, 5925, -43725,
-14089, 11503, -22782, 6371, 43881, 37465, -33529, -33590, -39798,
-37854, -18466, -7908, -35825, -26020, -36923, -11332, -5699, 25166,
-3147, 19885, 12962, -20659, -1642, 27723, -56331, -24580, -11010,
-20206, 20087, -23772, -16038, 38580, 20901, -50731, 32037, -4299,
26508, 18038, -28357, 31846, -7405, -20172, -15894, 2096, 25110,
-45786, 45918, -55333, -31928, -49428, -29824, -58796, -24609, -15408,
69, -35415, -18439, 10123, -20360, -65949, 33356, -20333, 26476,
-32073, 33621, 930, 28803, -42791, 44716, 38164, 12302, -1739, 11421,
73385, -7613, 14297, 38155, -414, 77587, 24338, -21415, 29367, 42639,
13901, -288, 51027, -11827, 91260, 43407, 88521, -15186, 70572, -12049,
5090, -12208, -56374, 15520, -623, -7742, 50825, 11199, -14894, 40892,
59591, -31356, -28696, -57842, -87751, -33744, -28436, -28945, -40287,
37957, -35638, 33401, -61534, 14870, 40292, 70366, -10803, 102290,
-71719, -85251, 7902, -22409, 75009, 99927, 35298, -1175, -762, -34744,
-10587, -47574, -62629, -19581, -43659, -54369, -32250, -39545, 15225,
-24454, 11241, -67308, -30148, 39929, 37639, 14383, -73475, -77636,
-81048, -35992, 41601, -90143, 76937, -8112, 56588, 9124, -40094,
-32340, 13253, 10898, -51639, 36390, 12086, -1885, 100714, -28561,
-23784, -18735, 18916, 16286, 10742, -87360, -13697, 10689, -19477,
-29770, 5060, 20189, -8297, 112407, 47071, 47743, 45519, -4109, 17468,
-68831, 78325, -6481, -21641, -19459, 30919, 96115, 8607, 53341, 32105,
-16211, 23538, 57259, -76272, -40583, 62093, 38511, -34255, -40665,
-40604, -37606, -15274, 33156, -13885, 103636, 118678, -14101, -92682,
-100791, 2634, 63791, 98266, 19286, -34590, -21067, -71130, 25380,
-40839, -27614, -26060, 52358, -15537, 27138, -6749, 36269, -33306,
13207, -91084, -5540, -57116, 69548, 44169, -57742, -41234, -103327,
-62904, -8566, 41149, -12866, 71188, 23980, 1838, 58230, 73950, 5594,
43113, -8159, -15925, 6911, 85598, -75016, -16214, -62726, -39016,
8618, -63882, -4299, 23182, 49959, 49342, -3238, -24913, -37138, 78361,
32451, 6337, -11438, -36241, -37737, 8169, -3077, -24829, 57953, 53016,
-31511, -91168, 12599, -41849, 41576, 55275, -62539, 47814, -62319,
12300, -32076, -55137, -84881, -27546, 4312, -3433, -54382, 113288,
-30157, 74469, 18219, 79880, -2124, 98911, 17655, -33499, -32861,
47242, -37393, 99765, 14831, -44483, 10800, -31617, -52710, 37406,
22105, 29704, -20050, 13778, 43683, 36628, 8494, 60964, -22644, 31550,
-17693, 33805, -124879, -12302, 19343, 20400, -30937, -21574, -34037,
-33380, 56539, -24993, -75513, -1527, 53563, 65407, -101, 53577, 37991,
18717, -23795, -8090, -47987, -94717, 41967, 5170, -14815, -94311,
17896, -17734, -57718, -774, -38410, 24830, 29682, 76480, 58802,
-46416, -20348, -61353, -68225, -68306, 23822, -31598, 42972, 36327,
28968, -65638, -21638, 24354, -8356, 26777, 52982, -11783, -44051,
-26467, -44721, -28435, -53265, -25574, -2669, 44155, 22946, -18454,
-30718, -11252, 58420, 8711, 67447, 4425, 41749, 67543, 43162, 11793,
-41907, 20477, -13080, 6559, -6104, -13244, 42853, 42935, 29793, 36730,
-28087, 28657, 17946, 7503, 7204, 21491, -27450, -24241, -98156,
-18082, -42613, -24928, 10775, -14842, -44127, 55910, 14777, 31151, -2194,
39206, -2100, -4211, 11827, -8918, -19471, 72567, 36447, -65590, -34861,
-17147, -45303, 9025, -7333, -35473, 11101, 11638, 3441, 6626, -41800,
9416, 13679, 33508, 40502, -60542, 16358, 8392, -43242, -35864, -34127,
-48721, 35878, 30598, 28630, 20279, -19983, -14638, -24455, -1851, -11344,
45150, 42051, 26034, -28889, -32382, -3527, -14532, 22564, -22346, 477,
11706, 28338, -25972, -9185, -22867, -12522, 32120, -4424, 11339, -33913,
-7184, 5101, -23552, -17115, -31401, -6104, 21906, 25708, 8406, 6317,
-7525, 5014, 20750, 20179, 22724, 11692, 13297, 2493, -253, -16841, -17339,
-6753, -4808, 2976, -10881, -10228, -13816, -12686, 1385, 2316, 2190, -875,
-1924], ZZ)
assert dup_mul(p1, p2, ZZ) == res
p1 = dup_normal([83, -61, -86, -24, 12, 43, -88, -9, 42, 55, -66, 74, 95,
-25, -12, 68, -99, 4, 45, 6, -15, -19, 78, 65, -55, 47, -13, 17, 86,
81, -58, -27, 50, -40, -24, 39, -41, -92, 75, 90, -1, 40, -15, -27,
-35, 68, 70, -64, -40, 78, -88, -58, -39, 69, 46, 12, 28, -94, -37,
-50, -80, -96, -61, 25, 1, 71, 4, 12, 48, 4, 34, -47, -75, 5, 48, 82,
88, 23, 98, 35, 17, -10, 48, -61, -95, 47, 65, -19, -66, -57, -6, -51,
-42, -89, 66, -13, 18, 37, 90, -23, 72, 96, -53, 0, 40, -73, -52, -68,
32, -25, -53, 79, -52, 18, 44, 73, -81, 31, -90, 70, 3, 36, 48, 76,
-24, -44, 23, 98, -4, 73, 69, 88, -70, 14, -68, 94, -78, -15, -64, -97,
-70, -35, 65, 88, 49, -53, -7, 12, -45, -7, 59, -94, 99, -2, 67, -60,
-71, 29, -62, -77, 1, 51, 17, 80, -20, -47, -19, 24, -9, 39, -23, 21,
-84, 10, 84, 56, -17, -21, -66, 85, 70, 46, -51, -22, -95, 78, -60,
-96, -97, -45, 72, 35, 30, -61, -92, -93, -60, -61, 4, -4, -81, -73,
46, 53, -11, 26, 94, 45, 14, -78, 55, 84, -68, 98, 60, 23, 100, -63,
68, 96, -16, 3, 56, 21, -58, 62, -67, 66, 85, 41, -79, -22, 97, -67,
82, 82, -96, -20, -7, 48, -67, 48, -9, -39, 78], ZZ)
p2 = dup_normal([52, 88, 76, 66, 9, -64, 46, -20, -28, 69, 60, 96, -36,
-92, -30, -11, -35, 35, 55, 63, -92, -7, 25, -58, 74, 55, -6, 4, 47,
-92, -65, 67, -45, 74, -76, 59, -6, 69, 39, 24, -71, -7, 39, -45, 60,
-68, 98, 97, -79, 17, 4, 94, -64, 68, -100, -96, -2, 3, 22, 96, 54,
-77, -86, 67, 6, 57, 37, 40, 89, -78, 64, -94, -45, -92, 57, 87, -26,
36, 19, 97, 25, 77, -87, 24, 43, -5, 35, 57, 83, 71, 35, 63, 61, 96,
-22, 8, -1, 96, 43, 45, 94, -93, 36, 71, -41, -99, 85, -48, 59, 52,
-17, 5, 87, -16, -68, -54, 76, -18, 100, 91, -42, -70, -66, -88, -12,
1, 95, -82, 52, 43, -29, 3, 12, 72, -99, -43, -32, -93, -51, 16, -20,
-12, -11, 5, 33, -38, 93, -5, -74, 25, 74, -58, 93, 59, -63, -86, 63,
-20, -4, -74, -73, -95, 29, -28, 93, -91, -2, -38, -62, 77, -58, -85,
-28, 95, 38, 19, -69, 86, 94, 25, -2, -4, 47, 34, -59, 35, -48, 29,
-63, -53, 34, 29, 66, 73, 6, 92, -84, 89, 15, 81, 93, 97, 51, -72, -78,
25, 60, 90, -45, 39, 67, -84, -62, 57, 26, -32, -56, -14, -83, 76, 5,
-2, 99, -100, 28, 46, 94, -7, 53, -25, 16, -23, -36, 89, -78, -63, 31,
1, 84, -99, -52, 76, 48, 90, -76, 44, -19, 54, -36, -9, -73, -100, -69,
31, 42, 25, -39, 76, -26, -8, -14, 51, 3, 37, 45, 2, -54, 13, -34, -92,
17, -25, -65, 53, -63, 30, 4, -70, -67, 90, 52, 51, 18, -3, 31, -45,
-9, 59, 63, -87, 22, -32, 29, -38, 21, 36, -82, 27, -11], ZZ)
res = dup_normal([4316, 4132, -3532, -7974, -11303, -10069, 5484, -3330,
-5874, 7734, 4673, 11327, -9884, -8031, 17343, 21035, -10570, -9285,
15893, 3780, -14083, 8819, 17592, 10159, 7174, -11587, 8598, -16479,
3602, 25596, 9781, 12163, 150, 18749, -21782, -12307, 27578, -2757,
-12573, 12565, 6345, -18956, 19503, -15617, 1443, -16778, 36851, 23588,
-28474, 5749, 40695, -7521, -53669, -2497, -18530, 6770, 57038, 3926,
-6927, -15399, 1848, -64649, -27728, 3644, 49608, 15187, -8902, -9480,
-7398, -40425, 4824, 23767, -7594, -6905, 33089, 18786, 12192, 24670,
31114, 35334, -4501, -14676, 7107, -59018, -21352, 20777, 19661, 20653,
33754, -885, -43758, 6269, 51897, -28719, -97488, -9527, 13746, 11644,
17644, -21720, 23782, -10481, 47867, 20752, 33810, -1875, 39918, -7710,
-40840, 19808, -47075, 23066, 46616, 25201, 9287, 35436, -1602, 9645,
-11978, 13273, 15544, 33465, 20063, 44539, 11687, 27314, -6538, -37467,
14031, 32970, -27086, 41323, 29551, 65910, -39027, -37800, -22232,
8212, 46316, -28981, -55282, 50417, -44929, -44062, 73879, 37573,
-2596, -10877, -21893, -133218, -33707, -25753, -9531, 17530, 61126,
2748, -56235, 43874, -10872, -90459, -30387, 115267, -7264, -44452,
122626, 14839, -599, 10337, 57166, -67467, -54957, 63669, 1202, 18488,
52594, 7205, -97822, 612, 78069, -5403, -63562, 47236, 36873, -154827,
-26188, 82427, -39521, 5628, 7416, 5276, -53095, 47050, 26121, -42207,
79021, -13035, 2499, -66943, 29040, -72355, -23480, 23416, -12885,
-44225, -42688, -4224, 19858, 55299, 15735, 11465, 101876, -39169,
51786, 14723, 43280, -68697, 16410, 92295, 56767, 7183, 111850, 4550,
115451, -38443, -19642, -35058, 10230, 93829, 8925, 63047, 3146, 29250,
8530, 5255, -98117, -115517, -76817, -8724, 41044, 1312, -35974, 79333,
-28567, 7547, -10580, -24559, -16238, 10794, -3867, 24848, 57770,
-51536, -35040, 71033, 29853, 62029, -7125, -125585, -32169, -47907,
156811, -65176, -58006, -15757, -57861, 11963, 30225, -41901, -41681,
31310, 27982, 18613, 61760, 60746, -59096, 33499, 30097, -17997, 24032,
56442, -83042, 23747, -20931, -21978, -158752, -9883, -73598, -7987,
-7333, -125403, -116329, 30585, 53281, 51018, -29193, 88575, 8264,
-40147, -16289, 113088, 12810, -6508, 101552, -13037, 34440, -41840,
101643, 24263, 80532, 61748, 65574, 6423, -20672, 6591, -10834, -71716,
86919, -92626, 39161, 28490, 81319, 46676, 106720, 43530, 26998, 57456,
-8862, 60989, 13982, 3119, -2224, 14743, 55415, -49093, -29303, 28999,
1789, 55953, -84043, -7780, -65013, 57129, -47251, 61484, 61994,
-78361, -82778, 22487, -26894, 9756, -74637, -15519, -4360, 30115,
42433, 35475, 15286, 69768, 21509, -20214, 78675, -21163, 13596, 11443,
-10698, -53621, -53867, -24155, 64500, -42784, -33077, -16500, 873,
-52788, 14546, -38011, 36974, -39849, -34029, -94311, 83068, -50437,
-26169, -46746, 59185, 42259, -101379, -12943, 30089, -59086, 36271,
22723, -30253, -52472, -70826, -23289, 3331, -31687, 14183, -857,
-28627, 35246, -51284, 5636, -6933, 66539, 36654, 50927, 24783, 3457,
33276, 45281, 45650, -4938, -9968, -22590, 47995, 69229, 5214, -58365,
-17907, -14651, 18668, 18009, 12649, -11851, -13387, 20339, 52472,
-1087, -21458, -68647, 52295, 15849, 40608, 15323, 25164, -29368,
10352, -7055, 7159, 21695, -5373, -54849, 101103, -24963, -10511,
33227, 7659, 41042, -69588, 26718, -20515, 6441, 38135, -63, 24088,
-35364, -12785, -18709, 47843, 48533, -48575, 17251, -19394, 32878,
-9010, -9050, 504, -12407, 28076, -3429, 25324, -4210, -26119, 752,
-29203, 28251, -11324, -32140, -3366, -25135, 18702, -31588, -7047,
-24267, 49987, -14975, -33169, 37744, -7720, -9035, 16964, -2807, -421,
14114, -17097, -13662, 40628, -12139, -9427, 5369, 17551, -13232, -16211,
9804, -7422, 2677, 28635, -8280, -4906, 2908, -22558, 5604, 12459, 8756,
-3980, -4745, -18525, 7913, 5970, -16457, 20230, -6247, -13812, 2505,
11899, 1409, -15094, 22540, -18863, 137, 11123, -4516, 2290, -8594, 12150,
-10380, 3005, 5235, -7350, 2535, -858], ZZ)
assert dup_mul(p1, p2, ZZ) == res
def test_dmp_mul():
assert dmp_mul([ZZ(5)], [ZZ(7)], 0, ZZ) == \
dup_mul([ZZ(5)], [ZZ(7)], ZZ)
assert dmp_mul([QQ(5, 7)], [QQ(3, 7)], 0, QQ) == \
dup_mul([QQ(5, 7)], [QQ(3, 7)], QQ)
assert dmp_mul([[[]]], [[[]]], 2, ZZ) == [[[]]]
assert dmp_mul([[[ZZ(1)]]], [[[]]], 2, ZZ) == [[[]]]
assert dmp_mul([[[]]], [[[ZZ(1)]]], 2, ZZ) == [[[]]]
assert dmp_mul([[[ZZ(2)]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(2)]]]
assert dmp_mul([[[ZZ(1)]]], [[[ZZ(2)]]], 2, ZZ) == [[[ZZ(2)]]]
assert dmp_mul([[[]]], [[[]]], 2, QQ) == [[[]]]
assert dmp_mul([[[QQ(1, 2)]]], [[[]]], 2, QQ) == [[[]]]
assert dmp_mul([[[]]], [[[QQ(1, 2)]]], 2, QQ) == [[[]]]
assert dmp_mul([[[QQ(2, 7)]]], [[[QQ(1, 3)]]], 2, QQ) == [[[QQ(2, 21)]]]
assert dmp_mul([[[QQ(1, 7)]]], [[[QQ(2, 3)]]], 2, QQ) == [[[QQ(2, 21)]]]
K = FF(6)
assert dmp_mul(
[[K(2)], [K(1)]], [[K(3)], [K(4)]], 1, K) == [[K(5)], [K(4)]]
def test_dup_sqr():
assert dup_sqr([], ZZ) == []
assert dup_sqr([ZZ(2)], ZZ) == [ZZ(4)]
assert dup_sqr([ZZ(1), ZZ(2)], ZZ) == [ZZ(1), ZZ(4), ZZ(4)]
assert dup_sqr([], QQ) == []
assert dup_sqr([QQ(2, 3)], QQ) == [QQ(4, 9)]
assert dup_sqr([QQ(1, 3), QQ(2, 3)], QQ) == [QQ(1, 9), QQ(4, 9), QQ(4, 9)]
f = dup_normal([2, 0, 0, 1, 7], ZZ)
assert dup_sqr(f, ZZ) == dup_normal([4, 0, 0, 4, 28, 0, 1, 14, 49], ZZ)
K = FF(9)
assert dup_sqr([K(3), K(4)], K) == [K(6), K(7)]
def test_dmp_sqr():
assert dmp_sqr([ZZ(1), ZZ(2)], 0, ZZ) == \
dup_sqr([ZZ(1), ZZ(2)], ZZ)
assert dmp_sqr([[[]]], 2, ZZ) == [[[]]]
assert dmp_sqr([[[ZZ(2)]]], 2, ZZ) == [[[ZZ(4)]]]
assert dmp_sqr([[[]]], 2, QQ) == [[[]]]
assert dmp_sqr([[[QQ(2, 3)]]], 2, QQ) == [[[QQ(4, 9)]]]
K = FF(9)
assert dmp_sqr([[K(3)], [K(4)]], 1, K) == [[K(6)], [K(7)]]
def test_dup_pow():
assert dup_pow([], 0, ZZ) == [ZZ(1)]
assert dup_pow([], 0, QQ) == [QQ(1)]
assert dup_pow([], 1, ZZ) == []
assert dup_pow([], 7, ZZ) == []
assert dup_pow([ZZ(1)], 0, ZZ) == [ZZ(1)]
assert dup_pow([ZZ(1)], 1, ZZ) == [ZZ(1)]
assert dup_pow([ZZ(1)], 7, ZZ) == [ZZ(1)]
assert dup_pow([ZZ(3)], 0, ZZ) == [ZZ(1)]
assert dup_pow([ZZ(3)], 1, ZZ) == [ZZ(3)]
assert dup_pow([ZZ(3)], 7, ZZ) == [ZZ(2187)]
assert dup_pow([QQ(1, 1)], 0, QQ) == [QQ(1, 1)]
assert dup_pow([QQ(1, 1)], 1, QQ) == [QQ(1, 1)]
assert dup_pow([QQ(1, 1)], 7, QQ) == [QQ(1, 1)]
assert dup_pow([QQ(3, 7)], 0, QQ) == [QQ(1, 1)]
assert dup_pow([QQ(3, 7)], 1, QQ) == [QQ(3, 7)]
assert dup_pow([QQ(3, 7)], 7, QQ) == [QQ(2187, 823543)]
f = dup_normal([2, 0, 0, 1, 7], ZZ)
assert dup_pow(f, 0, ZZ) == dup_normal([1], ZZ)
assert dup_pow(f, 1, ZZ) == dup_normal([2, 0, 0, 1, 7], ZZ)
assert dup_pow(f, 2, ZZ) == dup_normal([4, 0, 0, 4, 28, 0, 1, 14, 49], ZZ)
assert dup_pow(f, 3, ZZ) == dup_normal(
[8, 0, 0, 12, 84, 0, 6, 84, 294, 1, 21, 147, 343], ZZ)
def test_dmp_pow():
assert dmp_pow([[]], 0, 1, ZZ) == [[ZZ(1)]]
assert dmp_pow([[]], 0, 1, QQ) == [[QQ(1)]]
assert dmp_pow([[]], 1, 1, ZZ) == [[]]
assert dmp_pow([[]], 7, 1, ZZ) == [[]]
assert dmp_pow([[ZZ(1)]], 0, 1, ZZ) == [[ZZ(1)]]
assert dmp_pow([[ZZ(1)]], 1, 1, ZZ) == [[ZZ(1)]]
assert dmp_pow([[ZZ(1)]], 7, 1, ZZ) == [[ZZ(1)]]
assert dmp_pow([[QQ(3, 7)]], 0, 1, QQ) == [[QQ(1, 1)]]
assert dmp_pow([[QQ(3, 7)]], 1, 1, QQ) == [[QQ(3, 7)]]
assert dmp_pow([[QQ(3, 7)]], 7, 1, QQ) == [[QQ(2187, 823543)]]
f = dup_normal([2, 0, 0, 1, 7], ZZ)
assert dmp_pow(f, 2, 0, ZZ) == dup_pow(f, 2, ZZ)
def test_dup_pdiv():
f = dup_normal([3, 1, 1, 5], ZZ)
g = dup_normal([5, -3, 1], ZZ)
q = dup_normal([15, 14], ZZ)
r = dup_normal([52, 111], ZZ)
assert dup_pdiv(f, g, ZZ) == (q, r)
assert dup_pquo(f, g, ZZ) == q
assert dup_prem(f, g, ZZ) == r
raises(ExactQuotientFailed, lambda: dup_pexquo(f, g, ZZ))
f = dup_normal([3, 1, 1, 5], QQ)
g = dup_normal([5, -3, 1], QQ)
q = dup_normal([15, 14], QQ)
r = dup_normal([52, 111], QQ)
assert dup_pdiv(f, g, QQ) == (q, r)
assert dup_pquo(f, g, QQ) == q
assert dup_prem(f, g, QQ) == r
raises(ExactQuotientFailed, lambda: dup_pexquo(f, g, QQ))
def test_dmp_pdiv():
f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ)
g = dmp_normal([[1], [-1, 0]], 1, ZZ)
q = dmp_normal([[1], [1, 0]], 1, ZZ)
r = dmp_normal([[2, 0, 0]], 1, ZZ)
assert dmp_pdiv(f, g, 1, ZZ) == (q, r)
assert dmp_pquo(f, g, 1, ZZ) == q
assert dmp_prem(f, g, 1, ZZ) == r
raises(ExactQuotientFailed, lambda: dmp_pexquo(f, g, 1, ZZ))
f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ)
g = dmp_normal([[2], [-2, 0]], 1, ZZ)
q = dmp_normal([[2], [2, 0]], 1, ZZ)
r = dmp_normal([[8, 0, 0]], 1, ZZ)
assert dmp_pdiv(f, g, 1, ZZ) == (q, r)
assert dmp_pquo(f, g, 1, ZZ) == q
assert dmp_prem(f, g, 1, ZZ) == r
raises(ExactQuotientFailed, lambda: dmp_pexquo(f, g, 1, ZZ))
def test_dup_rr_div():
raises(ZeroDivisionError, lambda: dup_rr_div([1, 2, 3], [], ZZ))
f = dup_normal([3, 1, 1, 5], ZZ)
g = dup_normal([5, -3, 1], ZZ)
q, r = [], f
assert dup_rr_div(f, g, ZZ) == (q, r)
def test_dmp_rr_div():
raises(ZeroDivisionError, lambda: dmp_rr_div([[1, 2], [3]], [[]], 1, ZZ))
f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ)
g = dmp_normal([[1], [-1, 0]], 1, ZZ)
q = dmp_normal([[1], [1, 0]], 1, ZZ)
r = dmp_normal([[2, 0, 0]], 1, ZZ)
assert dmp_rr_div(f, g, 1, ZZ) == (q, r)
f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ)
g = dmp_normal([[-1], [1, 0]], 1, ZZ)
q = dmp_normal([[-1], [-1, 0]], 1, ZZ)
r = dmp_normal([[2, 0, 0]], 1, ZZ)
assert dmp_rr_div(f, g, 1, ZZ) == (q, r)
f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ)
g = dmp_normal([[2], [-2, 0]], 1, ZZ)
q, r = [[]], f
assert dmp_rr_div(f, g, 1, ZZ) == (q, r)
def test_dup_ff_div():
raises(ZeroDivisionError, lambda: dup_ff_div([1, 2, 3], [], QQ))
f = dup_normal([3, 1, 1, 5], QQ)
g = dup_normal([5, -3, 1], QQ)
q = [QQ(3, 5), QQ(14, 25)]
r = [QQ(52, 25), QQ(111, 25)]
assert dup_ff_div(f, g, QQ) == (q, r)
def test_dup_ff_div_gmpy2():
if GROUND_TYPES != 'gmpy2':
return
from gmpy2 import mpq
from sympy.polys.domains import GMPYRationalField
K = GMPYRationalField()
f = [mpq(1,3), mpq(3,2)]
g = [mpq(2,1)]
assert dmp_ff_div(f, g, 0, K) == ([mpq(1,6), mpq(3,4)], [])
f = [mpq(1,2), mpq(1,3), mpq(1,4), mpq(1,5)]
g = [mpq(-1,1), mpq(1,1), mpq(-1,1)]
assert dmp_ff_div(f, g, 0, K) == ([mpq(-1,2), mpq(-5,6)], [mpq(7,12), mpq(-19,30)])
def test_dmp_ff_div():
raises(ZeroDivisionError, lambda: dmp_ff_div([[1, 2], [3]], [[]], 1, QQ))
f = dmp_normal([[1], [], [1, 0, 0]], 1, QQ)
g = dmp_normal([[1], [-1, 0]], 1, QQ)
q = [[QQ(1, 1)], [QQ(1, 1), QQ(0, 1)]]
r = [[QQ(2, 1), QQ(0, 1), QQ(0, 1)]]
assert dmp_ff_div(f, g, 1, QQ) == (q, r)
f = dmp_normal([[1], [], [1, 0, 0]], 1, QQ)
g = dmp_normal([[-1], [1, 0]], 1, QQ)
q = [[QQ(-1, 1)], [QQ(-1, 1), QQ(0, 1)]]
r = [[QQ(2, 1), QQ(0, 1), QQ(0, 1)]]
assert dmp_ff_div(f, g, 1, QQ) == (q, r)
f = dmp_normal([[1], [], [1, 0, 0]], 1, QQ)
g = dmp_normal([[2], [-2, 0]], 1, QQ)
q = [[QQ(1, 2)], [QQ(1, 2), QQ(0, 1)]]
r = [[QQ(2, 1), QQ(0, 1), QQ(0, 1)]]
assert dmp_ff_div(f, g, 1, QQ) == (q, r)
def test_dup_div():
f, g, q, r = [5, 4, 3, 2, 1], [1, 2, 3], [5, -6, 0], [20, 1]
assert dup_div(f, g, ZZ) == (q, r)
assert dup_quo(f, g, ZZ) == q
assert dup_rem(f, g, ZZ) == r
raises(ExactQuotientFailed, lambda: dup_exquo(f, g, ZZ))
f, g, q, r = [5, 4, 3, 2, 1, 0], [1, 2, 0, 0, 9], [5, -6], [15, 2, -44, 54]
assert dup_div(f, g, ZZ) == (q, r)
assert dup_quo(f, g, ZZ) == q
assert dup_rem(f, g, ZZ) == r
raises(ExactQuotientFailed, lambda: dup_exquo(f, g, ZZ))
def test_dmp_div():
f, g, q, r = [5, 4, 3, 2, 1], [1, 2, 3], [5, -6, 0], [20, 1]
assert dmp_div(f, g, 0, ZZ) == (q, r)
assert dmp_quo(f, g, 0, ZZ) == q
assert dmp_rem(f, g, 0, ZZ) == r
raises(ExactQuotientFailed, lambda: dmp_exquo(f, g, 0, ZZ))
f, g, q, r = [[[1]]], [[[2]], [1]], [[[]]], [[[1]]]
assert dmp_div(f, g, 2, ZZ) == (q, r)
assert dmp_quo(f, g, 2, ZZ) == q
assert dmp_rem(f, g, 2, ZZ) == r
raises(ExactQuotientFailed, lambda: dmp_exquo(f, g, 2, ZZ))
def test_dup_max_norm():
assert dup_max_norm([], ZZ) == 0
assert dup_max_norm([1], ZZ) == 1
assert dup_max_norm([1, 4, 2, 3], ZZ) == 4
def test_dmp_max_norm():
assert dmp_max_norm([[[]]], 2, ZZ) == 0
assert dmp_max_norm([[[1]]], 2, ZZ) == 1
assert dmp_max_norm(f_0, 2, ZZ) == 6
def test_dup_l1_norm():
assert dup_l1_norm([], ZZ) == 0
assert dup_l1_norm([1], ZZ) == 1
assert dup_l1_norm([1, 4, 2, 3], ZZ) == 10
def test_dmp_l1_norm():
assert dmp_l1_norm([[[]]], 2, ZZ) == 0
assert dmp_l1_norm([[[1]]], 2, ZZ) == 1
assert dmp_l1_norm(f_0, 2, ZZ) == 31
def test_dup_l2_norm_squared():
assert dup_l2_norm_squared([], ZZ) == 0
assert dup_l2_norm_squared([1], ZZ) == 1
assert dup_l2_norm_squared([1, 4, 2, 3], ZZ) == 30
def test_dmp_l2_norm_squared():
assert dmp_l2_norm_squared([[[]]], 2, ZZ) == 0
assert dmp_l2_norm_squared([[[1]]], 2, ZZ) == 1
assert dmp_l2_norm_squared(f_0, 2, ZZ) == 111
def test_dup_expand():
assert dup_expand((), ZZ) == [1]
assert dup_expand(([1, 2, 3], [1, 2], [7, 5, 4, 3]), ZZ) == \
dup_mul([1, 2, 3], dup_mul([1, 2], [7, 5, 4, 3], ZZ), ZZ)
def test_dmp_expand():
assert dmp_expand((), 1, ZZ) == [[1]]
assert dmp_expand(([[1], [2], [3]], [[1], [2]], [[7], [5], [4], [3]]), 1, ZZ) == \
dmp_mul([[1], [2], [3]], dmp_mul([[1], [2]], [[7], [5], [
4], [3]], 1, ZZ), 1, ZZ)

View File

@ -0,0 +1,730 @@
"""Tests for dense recursive polynomials' basic tools. """
from sympy.polys.densebasic import (
ninf,
dup_LC, dmp_LC,
dup_TC, dmp_TC,
dmp_ground_LC, dmp_ground_TC,
dmp_true_LT,
dup_degree, dmp_degree,
dmp_degree_in, dmp_degree_list,
dup_strip, dmp_strip,
dmp_validate,
dup_reverse,
dup_copy, dmp_copy,
dup_normal, dmp_normal,
dup_convert, dmp_convert,
dup_from_sympy, dmp_from_sympy,
dup_nth, dmp_nth, dmp_ground_nth,
dmp_zero_p, dmp_zero,
dmp_one_p, dmp_one,
dmp_ground_p, dmp_ground,
dmp_negative_p, dmp_positive_p,
dmp_zeros, dmp_grounds,
dup_from_dict, dup_from_raw_dict,
dup_to_dict, dup_to_raw_dict,
dmp_from_dict, dmp_to_dict,
dmp_swap, dmp_permute,
dmp_nest, dmp_raise,
dup_deflate, dmp_deflate,
dup_multi_deflate, dmp_multi_deflate,
dup_inflate, dmp_inflate,
dmp_exclude, dmp_include,
dmp_inject, dmp_eject,
dup_terms_gcd, dmp_terms_gcd,
dmp_list_terms, dmp_apply_pairs,
dup_slice,
dup_random,
)
from sympy.polys.specialpolys import f_polys
from sympy.polys.domains import ZZ, QQ
from sympy.polys.rings import ring
from sympy.core.singleton import S
from sympy.testing.pytest import raises
from sympy.core.numbers import oo
f_0, f_1, f_2, f_3, f_4, f_5, f_6 = [ f.to_dense() for f in f_polys() ]
def test_dup_LC():
assert dup_LC([], ZZ) == 0
assert dup_LC([2, 3, 4, 5], ZZ) == 2
def test_dup_TC():
assert dup_TC([], ZZ) == 0
assert dup_TC([2, 3, 4, 5], ZZ) == 5
def test_dmp_LC():
assert dmp_LC([[]], ZZ) == []
assert dmp_LC([[2, 3, 4], [5]], ZZ) == [2, 3, 4]
assert dmp_LC([[[]]], ZZ) == [[]]
assert dmp_LC([[[2], [3, 4]], [[5]]], ZZ) == [[2], [3, 4]]
def test_dmp_TC():
assert dmp_TC([[]], ZZ) == []
assert dmp_TC([[2, 3, 4], [5]], ZZ) == [5]
assert dmp_TC([[[]]], ZZ) == [[]]
assert dmp_TC([[[2], [3, 4]], [[5]]], ZZ) == [[5]]
def test_dmp_ground_LC():
assert dmp_ground_LC([[]], 1, ZZ) == 0
assert dmp_ground_LC([[2, 3, 4], [5]], 1, ZZ) == 2
assert dmp_ground_LC([[[]]], 2, ZZ) == 0
assert dmp_ground_LC([[[2], [3, 4]], [[5]]], 2, ZZ) == 2
def test_dmp_ground_TC():
assert dmp_ground_TC([[]], 1, ZZ) == 0
assert dmp_ground_TC([[2, 3, 4], [5]], 1, ZZ) == 5
assert dmp_ground_TC([[[]]], 2, ZZ) == 0
assert dmp_ground_TC([[[2], [3, 4]], [[5]]], 2, ZZ) == 5
def test_dmp_true_LT():
assert dmp_true_LT([[]], 1, ZZ) == ((0, 0), 0)
assert dmp_true_LT([[7]], 1, ZZ) == ((0, 0), 7)
assert dmp_true_LT([[1, 0]], 1, ZZ) == ((0, 1), 1)
assert dmp_true_LT([[1], []], 1, ZZ) == ((1, 0), 1)
assert dmp_true_LT([[1, 0], []], 1, ZZ) == ((1, 1), 1)
def test_dup_degree():
assert ninf == float('-inf')
assert dup_degree([]) is ninf
assert dup_degree([1]) == 0
assert dup_degree([1, 0]) == 1
assert dup_degree([1, 0, 0, 0, 1]) == 4
def test_dmp_degree():
assert dmp_degree([[]], 1) is ninf
assert dmp_degree([[[]]], 2) is ninf
assert dmp_degree([[1]], 1) == 0
assert dmp_degree([[2], [1]], 1) == 1
def test_dmp_degree_in():
assert dmp_degree_in([[[]]], 0, 2) is ninf
assert dmp_degree_in([[[]]], 1, 2) is ninf
assert dmp_degree_in([[[]]], 2, 2) is ninf
assert dmp_degree_in([[[1]]], 0, 2) == 0
assert dmp_degree_in([[[1]]], 1, 2) == 0
assert dmp_degree_in([[[1]]], 2, 2) == 0
assert dmp_degree_in(f_4, 0, 2) == 9
assert dmp_degree_in(f_4, 1, 2) == 12
assert dmp_degree_in(f_4, 2, 2) == 8
assert dmp_degree_in(f_6, 0, 2) == 4
assert dmp_degree_in(f_6, 1, 2) == 4
assert dmp_degree_in(f_6, 2, 2) == 6
assert dmp_degree_in(f_6, 3, 3) == 3
raises(IndexError, lambda: dmp_degree_in([[1]], -5, 1))
def test_dmp_degree_list():
assert dmp_degree_list([[[[ ]]]], 3) == (-oo, -oo, -oo, -oo)
assert dmp_degree_list([[[[1]]]], 3) == ( 0, 0, 0, 0)
assert dmp_degree_list(f_0, 2) == (2, 2, 2)
assert dmp_degree_list(f_1, 2) == (3, 3, 3)
assert dmp_degree_list(f_2, 2) == (5, 3, 3)
assert dmp_degree_list(f_3, 2) == (5, 4, 7)
assert dmp_degree_list(f_4, 2) == (9, 12, 8)
assert dmp_degree_list(f_5, 2) == (3, 3, 3)
assert dmp_degree_list(f_6, 3) == (4, 4, 6, 3)
def test_dup_strip():
assert dup_strip([]) == []
assert dup_strip([0]) == []
assert dup_strip([0, 0, 0]) == []
assert dup_strip([1]) == [1]
assert dup_strip([0, 1]) == [1]
assert dup_strip([0, 0, 0, 1]) == [1]
assert dup_strip([1, 2, 0]) == [1, 2, 0]
assert dup_strip([0, 1, 2, 0]) == [1, 2, 0]
assert dup_strip([0, 0, 0, 1, 2, 0]) == [1, 2, 0]
def test_dmp_strip():
assert dmp_strip([0, 1, 0], 0) == [1, 0]
assert dmp_strip([[]], 1) == [[]]
assert dmp_strip([[], []], 1) == [[]]
assert dmp_strip([[], [], []], 1) == [[]]
assert dmp_strip([[[]]], 2) == [[[]]]
assert dmp_strip([[[]], [[]]], 2) == [[[]]]
assert dmp_strip([[[]], [[]], [[]]], 2) == [[[]]]
assert dmp_strip([[[1]]], 2) == [[[1]]]
assert dmp_strip([[[]], [[1]]], 2) == [[[1]]]
assert dmp_strip([[[]], [[1]], [[]]], 2) == [[[1]], [[]]]
def test_dmp_validate():
assert dmp_validate([]) == ([], 0)
assert dmp_validate([0, 0, 0, 1, 0]) == ([1, 0], 0)
assert dmp_validate([[[]]]) == ([[[]]], 2)
assert dmp_validate([[0], [], [0], [1], [0]]) == ([[1], []], 1)
raises(ValueError, lambda: dmp_validate([[0], 0, [0], [1], [0]]))
def test_dup_reverse():
assert dup_reverse([1, 2, 0, 3]) == [3, 0, 2, 1]
assert dup_reverse([1, 2, 3, 0]) == [3, 2, 1]
def test_dup_copy():
f = [ZZ(1), ZZ(0), ZZ(2)]
g = dup_copy(f)
g[0], g[2] = ZZ(7), ZZ(0)
assert f != g
def test_dmp_copy():
f = [[ZZ(1)], [ZZ(2), ZZ(0)]]
g = dmp_copy(f, 1)
g[0][0], g[1][1] = ZZ(7), ZZ(1)
assert f != g
def test_dup_normal():
assert dup_normal([0, 0, 2, 1, 0, 11, 0], ZZ) == \
[ZZ(2), ZZ(1), ZZ(0), ZZ(11), ZZ(0)]
def test_dmp_normal():
assert dmp_normal([[0], [], [0, 2, 1], [0], [11], []], 1, ZZ) == \
[[ZZ(2), ZZ(1)], [], [ZZ(11)], []]
def test_dup_convert():
K0, K1 = ZZ['x'], ZZ
f = [K0(1), K0(2), K0(0), K0(3)]
assert dup_convert(f, K0, K1) == \
[ZZ(1), ZZ(2), ZZ(0), ZZ(3)]
def test_dmp_convert():
K0, K1 = ZZ['x'], ZZ
f = [[K0(1)], [K0(2)], [], [K0(3)]]
assert dmp_convert(f, 1, K0, K1) == \
[[ZZ(1)], [ZZ(2)], [], [ZZ(3)]]
def test_dup_from_sympy():
assert dup_from_sympy([S.One, S(2)], ZZ) == \
[ZZ(1), ZZ(2)]
assert dup_from_sympy([S.Half, S(3)], QQ) == \
[QQ(1, 2), QQ(3, 1)]
def test_dmp_from_sympy():
assert dmp_from_sympy([[S.One, S(2)], [S.Zero]], 1, ZZ) == \
[[ZZ(1), ZZ(2)], []]
assert dmp_from_sympy([[S.Half, S(2)]], 1, QQ) == \
[[QQ(1, 2), QQ(2, 1)]]
def test_dup_nth():
assert dup_nth([1, 2, 3], 0, ZZ) == 3
assert dup_nth([1, 2, 3], 1, ZZ) == 2
assert dup_nth([1, 2, 3], 2, ZZ) == 1
assert dup_nth([1, 2, 3], 9, ZZ) == 0
raises(IndexError, lambda: dup_nth([3, 4, 5], -1, ZZ))
def test_dmp_nth():
assert dmp_nth([[1], [2], [3]], 0, 1, ZZ) == [3]
assert dmp_nth([[1], [2], [3]], 1, 1, ZZ) == [2]
assert dmp_nth([[1], [2], [3]], 2, 1, ZZ) == [1]
assert dmp_nth([[1], [2], [3]], 9, 1, ZZ) == []
raises(IndexError, lambda: dmp_nth([[3], [4], [5]], -1, 1, ZZ))
def test_dmp_ground_nth():
assert dmp_ground_nth([[]], (0, 0), 1, ZZ) == 0
assert dmp_ground_nth([[1], [2], [3]], (0, 0), 1, ZZ) == 3
assert dmp_ground_nth([[1], [2], [3]], (1, 0), 1, ZZ) == 2
assert dmp_ground_nth([[1], [2], [3]], (2, 0), 1, ZZ) == 1
assert dmp_ground_nth([[1], [2], [3]], (2, 1), 1, ZZ) == 0
assert dmp_ground_nth([[1], [2], [3]], (3, 0), 1, ZZ) == 0
raises(IndexError, lambda: dmp_ground_nth([[3], [4], [5]], (2, -1), 1, ZZ))
def test_dmp_zero_p():
assert dmp_zero_p([], 0) is True
assert dmp_zero_p([[]], 1) is True
assert dmp_zero_p([[[]]], 2) is True
assert dmp_zero_p([[[1]]], 2) is False
def test_dmp_zero():
assert dmp_zero(0) == []
assert dmp_zero(2) == [[[]]]
def test_dmp_one_p():
assert dmp_one_p([1], 0, ZZ) is True
assert dmp_one_p([[1]], 1, ZZ) is True
assert dmp_one_p([[[1]]], 2, ZZ) is True
assert dmp_one_p([[[12]]], 2, ZZ) is False
def test_dmp_one():
assert dmp_one(0, ZZ) == [ZZ(1)]
assert dmp_one(2, ZZ) == [[[ZZ(1)]]]
def test_dmp_ground_p():
assert dmp_ground_p([], 0, 0) is True
assert dmp_ground_p([[]], 0, 1) is True
assert dmp_ground_p([[]], 1, 1) is False
assert dmp_ground_p([[ZZ(1)]], 1, 1) is True
assert dmp_ground_p([[[ZZ(2)]]], 2, 2) is True
assert dmp_ground_p([[[ZZ(2)]]], 3, 2) is False
assert dmp_ground_p([[[ZZ(3)], []]], 3, 2) is False
assert dmp_ground_p([], None, 0) is True
assert dmp_ground_p([[]], None, 1) is True
assert dmp_ground_p([ZZ(1)], None, 0) is True
assert dmp_ground_p([[[ZZ(1)]]], None, 2) is True
assert dmp_ground_p([[[ZZ(3)], []]], None, 2) is False
def test_dmp_ground():
assert dmp_ground(ZZ(0), 2) == [[[]]]
assert dmp_ground(ZZ(7), -1) == ZZ(7)
assert dmp_ground(ZZ(7), 0) == [ZZ(7)]
assert dmp_ground(ZZ(7), 2) == [[[ZZ(7)]]]
def test_dmp_zeros():
assert dmp_zeros(4, 0, ZZ) == [[], [], [], []]
assert dmp_zeros(0, 2, ZZ) == []
assert dmp_zeros(1, 2, ZZ) == [[[[]]]]
assert dmp_zeros(2, 2, ZZ) == [[[[]]], [[[]]]]
assert dmp_zeros(3, 2, ZZ) == [[[[]]], [[[]]], [[[]]]]
assert dmp_zeros(3, -1, ZZ) == [0, 0, 0]
def test_dmp_grounds():
assert dmp_grounds(ZZ(7), 0, 2) == []
assert dmp_grounds(ZZ(7), 1, 2) == [[[[7]]]]
assert dmp_grounds(ZZ(7), 2, 2) == [[[[7]]], [[[7]]]]
assert dmp_grounds(ZZ(7), 3, 2) == [[[[7]]], [[[7]]], [[[7]]]]
assert dmp_grounds(ZZ(7), 3, -1) == [7, 7, 7]
def test_dmp_negative_p():
assert dmp_negative_p([[[]]], 2, ZZ) is False
assert dmp_negative_p([[[1], [2]]], 2, ZZ) is False
assert dmp_negative_p([[[-1], [2]]], 2, ZZ) is True
def test_dmp_positive_p():
assert dmp_positive_p([[[]]], 2, ZZ) is False
assert dmp_positive_p([[[1], [2]]], 2, ZZ) is True
assert dmp_positive_p([[[-1], [2]]], 2, ZZ) is False
def test_dup_from_to_dict():
assert dup_from_raw_dict({}, ZZ) == []
assert dup_from_dict({}, ZZ) == []
assert dup_to_raw_dict([]) == {}
assert dup_to_dict([]) == {}
assert dup_to_raw_dict([], ZZ, zero=True) == {0: ZZ(0)}
assert dup_to_dict([], ZZ, zero=True) == {(0,): ZZ(0)}
f = [3, 0, 0, 2, 0, 0, 0, 0, 8]
g = {8: 3, 5: 2, 0: 8}
h = {(8,): 3, (5,): 2, (0,): 8}
assert dup_from_raw_dict(g, ZZ) == f
assert dup_from_dict(h, ZZ) == f
assert dup_to_raw_dict(f) == g
assert dup_to_dict(f) == h
R, x,y = ring("x,y", ZZ)
K = R.to_domain()
f = [R(3), R(0), R(2), R(0), R(0), R(8)]
g = {5: R(3), 3: R(2), 0: R(8)}
h = {(5,): R(3), (3,): R(2), (0,): R(8)}
assert dup_from_raw_dict(g, K) == f
assert dup_from_dict(h, K) == f
assert dup_to_raw_dict(f) == g
assert dup_to_dict(f) == h
def test_dmp_from_to_dict():
assert dmp_from_dict({}, 1, ZZ) == [[]]
assert dmp_to_dict([[]], 1) == {}
assert dmp_to_dict([], 0, ZZ, zero=True) == {(0,): ZZ(0)}
assert dmp_to_dict([[]], 1, ZZ, zero=True) == {(0, 0): ZZ(0)}
f = [[3], [], [], [2], [], [], [], [], [8]]
g = {(8, 0): 3, (5, 0): 2, (0, 0): 8}
assert dmp_from_dict(g, 1, ZZ) == f
assert dmp_to_dict(f, 1) == g
def test_dmp_swap():
f = dmp_normal([[1, 0, 0], [], [1, 0], [], [1]], 1, ZZ)
g = dmp_normal([[1, 0, 0, 0, 0], [1, 0, 0], [1]], 1, ZZ)
assert dmp_swap(f, 1, 1, 1, ZZ) == f
assert dmp_swap(f, 0, 1, 1, ZZ) == g
assert dmp_swap(g, 0, 1, 1, ZZ) == f
raises(IndexError, lambda: dmp_swap(f, -1, -7, 1, ZZ))
def test_dmp_permute():
f = dmp_normal([[1, 0, 0], [], [1, 0], [], [1]], 1, ZZ)
g = dmp_normal([[1, 0, 0, 0, 0], [1, 0, 0], [1]], 1, ZZ)
assert dmp_permute(f, [0, 1], 1, ZZ) == f
assert dmp_permute(g, [0, 1], 1, ZZ) == g
assert dmp_permute(f, [1, 0], 1, ZZ) == g
assert dmp_permute(g, [1, 0], 1, ZZ) == f
def test_dmp_nest():
assert dmp_nest(ZZ(1), 2, ZZ) == [[[1]]]
assert dmp_nest([[1]], 0, ZZ) == [[1]]
assert dmp_nest([[1]], 1, ZZ) == [[[1]]]
assert dmp_nest([[1]], 2, ZZ) == [[[[1]]]]
def test_dmp_raise():
assert dmp_raise([], 2, 0, ZZ) == [[[]]]
assert dmp_raise([[1]], 0, 1, ZZ) == [[1]]
assert dmp_raise([[1, 2, 3], [], [2, 3]], 2, 1, ZZ) == \
[[[[1]], [[2]], [[3]]], [[[]]], [[[2]], [[3]]]]
def test_dup_deflate():
assert dup_deflate([], ZZ) == (1, [])
assert dup_deflate([2], ZZ) == (1, [2])
assert dup_deflate([1, 2, 3], ZZ) == (1, [1, 2, 3])
assert dup_deflate([1, 0, 2, 0, 3], ZZ) == (2, [1, 2, 3])
assert dup_deflate(dup_from_raw_dict({7: 1, 1: 1}, ZZ), ZZ) == \
(1, [1, 0, 0, 0, 0, 0, 1, 0])
assert dup_deflate(dup_from_raw_dict({7: 1, 0: 1}, ZZ), ZZ) == \
(7, [1, 1])
assert dup_deflate(dup_from_raw_dict({7: 1, 3: 1}, ZZ), ZZ) == \
(1, [1, 0, 0, 0, 1, 0, 0, 0])
assert dup_deflate(dup_from_raw_dict({7: 1, 4: 1}, ZZ), ZZ) == \
(1, [1, 0, 0, 1, 0, 0, 0, 0])
assert dup_deflate(dup_from_raw_dict({8: 1, 4: 1}, ZZ), ZZ) == \
(4, [1, 1, 0])
assert dup_deflate(dup_from_raw_dict({8: 1}, ZZ), ZZ) == \
(8, [1, 0])
assert dup_deflate(dup_from_raw_dict({7: 1}, ZZ), ZZ) == \
(7, [1, 0])
assert dup_deflate(dup_from_raw_dict({1: 1}, ZZ), ZZ) == \
(1, [1, 0])
def test_dmp_deflate():
assert dmp_deflate([[]], 1, ZZ) == ((1, 1), [[]])
assert dmp_deflate([[2]], 1, ZZ) == ((1, 1), [[2]])
f = [[1, 0, 0], [], [1, 0], [], [1]]
assert dmp_deflate(f, 1, ZZ) == ((2, 1), [[1, 0, 0], [1, 0], [1]])
def test_dup_multi_deflate():
assert dup_multi_deflate(([2],), ZZ) == (1, ([2],))
assert dup_multi_deflate(([], []), ZZ) == (1, ([], []))
assert dup_multi_deflate(([1, 2, 3],), ZZ) == (1, ([1, 2, 3],))
assert dup_multi_deflate(([1, 0, 2, 0, 3],), ZZ) == (2, ([1, 2, 3],))
assert dup_multi_deflate(([1, 0, 2, 0, 3], [2, 0, 0]), ZZ) == \
(2, ([1, 2, 3], [2, 0]))
assert dup_multi_deflate(([1, 0, 2, 0, 3], [2, 1, 0]), ZZ) == \
(1, ([1, 0, 2, 0, 3], [2, 1, 0]))
def test_dmp_multi_deflate():
assert dmp_multi_deflate(([[]],), 1, ZZ) == \
((1, 1), ([[]],))
assert dmp_multi_deflate(([[]], [[]]), 1, ZZ) == \
((1, 1), ([[]], [[]]))
assert dmp_multi_deflate(([[1]], [[]]), 1, ZZ) == \
((1, 1), ([[1]], [[]]))
assert dmp_multi_deflate(([[1]], [[2]]), 1, ZZ) == \
((1, 1), ([[1]], [[2]]))
assert dmp_multi_deflate(([[1]], [[2, 0]]), 1, ZZ) == \
((1, 1), ([[1]], [[2, 0]]))
assert dmp_multi_deflate(([[2, 0]], [[2, 0]]), 1, ZZ) == \
((1, 1), ([[2, 0]], [[2, 0]]))
assert dmp_multi_deflate(
([[2]], [[2, 0, 0]]), 1, ZZ) == ((1, 2), ([[2]], [[2, 0]]))
assert dmp_multi_deflate(
([[2, 0, 0]], [[2, 0, 0]]), 1, ZZ) == ((1, 2), ([[2, 0]], [[2, 0]]))
assert dmp_multi_deflate(([2, 0, 0], [1, 0, 4, 0, 1]), 0, ZZ) == \
((2,), ([2, 0], [1, 4, 1]))
f = [[1, 0, 0], [], [1, 0], [], [1]]
g = [[1, 0, 1, 0], [], [1]]
assert dmp_multi_deflate((f,), 1, ZZ) == \
((2, 1), ([[1, 0, 0], [1, 0], [1]],))
assert dmp_multi_deflate((f, g), 1, ZZ) == \
((2, 1), ([[1, 0, 0], [1, 0], [1]],
[[1, 0, 1, 0], [1]]))
def test_dup_inflate():
assert dup_inflate([], 17, ZZ) == []
assert dup_inflate([1, 2, 3], 1, ZZ) == [1, 2, 3]
assert dup_inflate([1, 2, 3], 2, ZZ) == [1, 0, 2, 0, 3]
assert dup_inflate([1, 2, 3], 3, ZZ) == [1, 0, 0, 2, 0, 0, 3]
assert dup_inflate([1, 2, 3], 4, ZZ) == [1, 0, 0, 0, 2, 0, 0, 0, 3]
raises(IndexError, lambda: dup_inflate([1, 2, 3], 0, ZZ))
def test_dmp_inflate():
assert dmp_inflate([1], (3,), 0, ZZ) == [1]
assert dmp_inflate([[]], (3, 7), 1, ZZ) == [[]]
assert dmp_inflate([[2]], (1, 2), 1, ZZ) == [[2]]
assert dmp_inflate([[2, 0]], (1, 1), 1, ZZ) == [[2, 0]]
assert dmp_inflate([[2, 0]], (1, 2), 1, ZZ) == [[2, 0, 0]]
assert dmp_inflate([[2, 0]], (1, 3), 1, ZZ) == [[2, 0, 0, 0]]
assert dmp_inflate([[1, 0, 0], [1], [1, 0]], (2, 1), 1, ZZ) == \
[[1, 0, 0], [], [1], [], [1, 0]]
raises(IndexError, lambda: dmp_inflate([[]], (-3, 7), 1, ZZ))
def test_dmp_exclude():
assert dmp_exclude([[[]]], 2, ZZ) == ([], [[[]]], 2)
assert dmp_exclude([[[7]]], 2, ZZ) == ([], [[[7]]], 2)
assert dmp_exclude([1, 2, 3], 0, ZZ) == ([], [1, 2, 3], 0)
assert dmp_exclude([[1], [2, 3]], 1, ZZ) == ([], [[1], [2, 3]], 1)
assert dmp_exclude([[1, 2, 3]], 1, ZZ) == ([0], [1, 2, 3], 0)
assert dmp_exclude([[1], [2], [3]], 1, ZZ) == ([1], [1, 2, 3], 0)
assert dmp_exclude([[[1, 2, 3]]], 2, ZZ) == ([0, 1], [1, 2, 3], 0)
assert dmp_exclude([[[1]], [[2]], [[3]]], 2, ZZ) == ([1, 2], [1, 2, 3], 0)
def test_dmp_include():
assert dmp_include([1, 2, 3], [], 0, ZZ) == [1, 2, 3]
assert dmp_include([1, 2, 3], [0], 0, ZZ) == [[1, 2, 3]]
assert dmp_include([1, 2, 3], [1], 0, ZZ) == [[1], [2], [3]]
assert dmp_include([1, 2, 3], [0, 1], 0, ZZ) == [[[1, 2, 3]]]
assert dmp_include([1, 2, 3], [1, 2], 0, ZZ) == [[[1]], [[2]], [[3]]]
def test_dmp_inject():
R, x,y = ring("x,y", ZZ)
K = R.to_domain()
assert dmp_inject([], 0, K) == ([[[]]], 2)
assert dmp_inject([[]], 1, K) == ([[[[]]]], 3)
assert dmp_inject([R(1)], 0, K) == ([[[1]]], 2)
assert dmp_inject([[R(1)]], 1, K) == ([[[[1]]]], 3)
assert dmp_inject([R(1), 2*x + 3*y + 4], 0, K) == ([[[1]], [[2], [3, 4]]], 2)
f = [3*x**2 + 7*x*y + 5*y**2, 2*x, R(0), x*y**2 + 11]
g = [[[3], [7, 0], [5, 0, 0]], [[2], []], [[]], [[1, 0, 0], [11]]]
assert dmp_inject(f, 0, K) == (g, 2)
def test_dmp_eject():
R, x,y = ring("x,y", ZZ)
K = R.to_domain()
assert dmp_eject([[[]]], 2, K) == []
assert dmp_eject([[[[]]]], 3, K) == [[]]
assert dmp_eject([[[1]]], 2, K) == [R(1)]
assert dmp_eject([[[[1]]]], 3, K) == [[R(1)]]
assert dmp_eject([[[1]], [[2], [3, 4]]], 2, K) == [R(1), 2*x + 3*y + 4]
f = [3*x**2 + 7*x*y + 5*y**2, 2*x, R(0), x*y**2 + 11]
g = [[[3], [7, 0], [5, 0, 0]], [[2], []], [[]], [[1, 0, 0], [11]]]
assert dmp_eject(g, 2, K) == f
def test_dup_terms_gcd():
assert dup_terms_gcd([], ZZ) == (0, [])
assert dup_terms_gcd([1, 0, 1], ZZ) == (0, [1, 0, 1])
assert dup_terms_gcd([1, 0, 1, 0], ZZ) == (1, [1, 0, 1])
def test_dmp_terms_gcd():
assert dmp_terms_gcd([[]], 1, ZZ) == ((0, 0), [[]])
assert dmp_terms_gcd([1, 0, 1, 0], 0, ZZ) == ((1,), [1, 0, 1])
assert dmp_terms_gcd([[1], [], [1], []], 1, ZZ) == ((1, 0), [[1], [], [1]])
assert dmp_terms_gcd(
[[1, 0], [], [1]], 1, ZZ) == ((0, 0), [[1, 0], [], [1]])
assert dmp_terms_gcd(
[[1, 0], [1, 0, 0], [], []], 1, ZZ) == ((2, 1), [[1], [1, 0]])
def test_dmp_list_terms():
assert dmp_list_terms([[[]]], 2, ZZ) == [((0, 0, 0), 0)]
assert dmp_list_terms([[[1]]], 2, ZZ) == [((0, 0, 0), 1)]
assert dmp_list_terms([1, 2, 4, 3, 5], 0, ZZ) == \
[((4,), 1), ((3,), 2), ((2,), 4), ((1,), 3), ((0,), 5)]
assert dmp_list_terms([[1], [2, 4], [3, 5, 0]], 1, ZZ) == \
[((2, 0), 1), ((1, 1), 2), ((1, 0), 4), ((0, 2), 3), ((0, 1), 5)]
f = [[2, 0, 0, 0], [1, 0, 0], []]
assert dmp_list_terms(f, 1, ZZ, order='lex') == [((2, 3), 2), ((1, 2), 1)]
assert dmp_list_terms(
f, 1, ZZ, order='grlex') == [((2, 3), 2), ((1, 2), 1)]
f = [[2, 0, 0, 0], [1, 0, 0, 0, 0, 0], []]
assert dmp_list_terms(f, 1, ZZ, order='lex') == [((2, 3), 2), ((1, 5), 1)]
assert dmp_list_terms(
f, 1, ZZ, order='grlex') == [((1, 5), 1), ((2, 3), 2)]
def test_dmp_apply_pairs():
h = lambda a, b: a*b
assert dmp_apply_pairs([1, 2, 3], [4, 5, 6], h, [], 0, ZZ) == [4, 10, 18]
assert dmp_apply_pairs([2, 3], [4, 5, 6], h, [], 0, ZZ) == [10, 18]
assert dmp_apply_pairs([1, 2, 3], [5, 6], h, [], 0, ZZ) == [10, 18]
assert dmp_apply_pairs(
[[1, 2], [3]], [[4, 5], [6]], h, [], 1, ZZ) == [[4, 10], [18]]
assert dmp_apply_pairs(
[[1, 2], [3]], [[4], [5, 6]], h, [], 1, ZZ) == [[8], [18]]
assert dmp_apply_pairs(
[[1], [2, 3]], [[4, 5], [6]], h, [], 1, ZZ) == [[5], [18]]
def test_dup_slice():
f = [1, 2, 3, 4]
assert dup_slice(f, 0, 0, ZZ) == []
assert dup_slice(f, 0, 1, ZZ) == [4]
assert dup_slice(f, 0, 2, ZZ) == [3, 4]
assert dup_slice(f, 0, 3, ZZ) == [2, 3, 4]
assert dup_slice(f, 0, 4, ZZ) == [1, 2, 3, 4]
assert dup_slice(f, 0, 4, ZZ) == f
assert dup_slice(f, 0, 9, ZZ) == f
assert dup_slice(f, 1, 0, ZZ) == []
assert dup_slice(f, 1, 1, ZZ) == []
assert dup_slice(f, 1, 2, ZZ) == [3, 0]
assert dup_slice(f, 1, 3, ZZ) == [2, 3, 0]
assert dup_slice(f, 1, 4, ZZ) == [1, 2, 3, 0]
assert dup_slice([1, 2], 0, 3, ZZ) == [1, 2]
g = [1, 0, 0, 2]
assert dup_slice(g, 0, 3, ZZ) == [2]
def test_dup_random():
f = dup_random(0, -10, 10, ZZ)
assert dup_degree(f) == 0
assert all(-10 <= c <= 10 for c in f)
f = dup_random(1, -20, 20, ZZ)
assert dup_degree(f) == 1
assert all(-20 <= c <= 20 for c in f)
f = dup_random(2, -30, 30, ZZ)
assert dup_degree(f) == 2
assert all(-30 <= c <= 30 for c in f)
f = dup_random(3, -40, 40, ZZ)
assert dup_degree(f) == 3
assert all(-40 <= c <= 40 for c in f)

View File

@ -0,0 +1,715 @@
"""Tests for dense recursive polynomials' tools. """
from sympy.polys.densebasic import (
dup_normal, dmp_normal,
dup_from_raw_dict,
dmp_convert, dmp_swap,
)
from sympy.polys.densearith import dmp_mul_ground
from sympy.polys.densetools import (
dup_clear_denoms, dmp_clear_denoms,
dup_integrate, dmp_integrate, dmp_integrate_in,
dup_diff, dmp_diff, dmp_diff_in,
dup_eval, dmp_eval, dmp_eval_in,
dmp_eval_tail, dmp_diff_eval_in,
dup_trunc, dmp_trunc, dmp_ground_trunc,
dup_monic, dmp_ground_monic,
dup_content, dmp_ground_content,
dup_primitive, dmp_ground_primitive,
dup_extract, dmp_ground_extract,
dup_real_imag,
dup_mirror, dup_scale, dup_shift, dmp_shift,
dup_transform,
dup_compose, dmp_compose,
dup_decompose,
dmp_lift,
dup_sign_variations,
dup_revert, dmp_revert,
)
from sympy.polys.polyclasses import ANP
from sympy.polys.polyerrors import (
MultivariatePolynomialError,
ExactQuotientFailed,
NotReversible,
DomainError,
)
from sympy.polys.specialpolys import f_polys
from sympy.polys.domains import FF, ZZ, QQ, ZZ_I, QQ_I, EX, RR
from sympy.polys.rings import ring
from sympy.core.numbers import I
from sympy.core.singleton import S
from sympy.functions.elementary.trigonometric import sin
from sympy.abc import x
from sympy.testing.pytest import raises
f_0, f_1, f_2, f_3, f_4, f_5, f_6 = [ f.to_dense() for f in f_polys() ]
def test_dup_integrate():
assert dup_integrate([], 1, QQ) == []
assert dup_integrate([], 2, QQ) == []
assert dup_integrate([QQ(1)], 1, QQ) == [QQ(1), QQ(0)]
assert dup_integrate([QQ(1)], 2, QQ) == [QQ(1, 2), QQ(0), QQ(0)]
assert dup_integrate([QQ(1), QQ(2), QQ(3)], 0, QQ) == \
[QQ(1), QQ(2), QQ(3)]
assert dup_integrate([QQ(1), QQ(2), QQ(3)], 1, QQ) == \
[QQ(1, 3), QQ(1), QQ(3), QQ(0)]
assert dup_integrate([QQ(1), QQ(2), QQ(3)], 2, QQ) == \
[QQ(1, 12), QQ(1, 3), QQ(3, 2), QQ(0), QQ(0)]
assert dup_integrate([QQ(1), QQ(2), QQ(3)], 3, QQ) == \
[QQ(1, 60), QQ(1, 12), QQ(1, 2), QQ(0), QQ(0), QQ(0)]
assert dup_integrate(dup_from_raw_dict({29: QQ(17)}, QQ), 3, QQ) == \
dup_from_raw_dict({32: QQ(17, 29760)}, QQ)
assert dup_integrate(dup_from_raw_dict({29: QQ(17), 5: QQ(1, 2)}, QQ), 3, QQ) == \
dup_from_raw_dict({32: QQ(17, 29760), 8: QQ(1, 672)}, QQ)
def test_dmp_integrate():
assert dmp_integrate([QQ(1)], 2, 0, QQ) == [QQ(1, 2), QQ(0), QQ(0)]
assert dmp_integrate([[[]]], 1, 2, QQ) == [[[]]]
assert dmp_integrate([[[]]], 2, 2, QQ) == [[[]]]
assert dmp_integrate([[[QQ(1)]]], 1, 2, QQ) == [[[QQ(1)]], [[]]]
assert dmp_integrate([[[QQ(1)]]], 2, 2, QQ) == [[[QQ(1, 2)]], [[]], [[]]]
assert dmp_integrate([[QQ(1)], [QQ(2)], [QQ(3)]], 0, 1, QQ) == \
[[QQ(1)], [QQ(2)], [QQ(3)]]
assert dmp_integrate([[QQ(1)], [QQ(2)], [QQ(3)]], 1, 1, QQ) == \
[[QQ(1, 3)], [QQ(1)], [QQ(3)], []]
assert dmp_integrate([[QQ(1)], [QQ(2)], [QQ(3)]], 2, 1, QQ) == \
[[QQ(1, 12)], [QQ(1, 3)], [QQ(3, 2)], [], []]
assert dmp_integrate([[QQ(1)], [QQ(2)], [QQ(3)]], 3, 1, QQ) == \
[[QQ(1, 60)], [QQ(1, 12)], [QQ(1, 2)], [], [], []]
def test_dmp_integrate_in():
f = dmp_convert(f_6, 3, ZZ, QQ)
assert dmp_integrate_in(f, 2, 1, 3, QQ) == \
dmp_swap(
dmp_integrate(dmp_swap(f, 0, 1, 3, QQ), 2, 3, QQ), 0, 1, 3, QQ)
assert dmp_integrate_in(f, 3, 1, 3, QQ) == \
dmp_swap(
dmp_integrate(dmp_swap(f, 0, 1, 3, QQ), 3, 3, QQ), 0, 1, 3, QQ)
assert dmp_integrate_in(f, 2, 2, 3, QQ) == \
dmp_swap(
dmp_integrate(dmp_swap(f, 0, 2, 3, QQ), 2, 3, QQ), 0, 2, 3, QQ)
assert dmp_integrate_in(f, 3, 2, 3, QQ) == \
dmp_swap(
dmp_integrate(dmp_swap(f, 0, 2, 3, QQ), 3, 3, QQ), 0, 2, 3, QQ)
raises(IndexError, lambda: dmp_integrate_in(f, 1, -1, 3, QQ))
raises(IndexError, lambda: dmp_integrate_in(f, 1, 4, 3, QQ))
def test_dup_diff():
assert dup_diff([], 1, ZZ) == []
assert dup_diff([7], 1, ZZ) == []
assert dup_diff([2, 7], 1, ZZ) == [2]
assert dup_diff([1, 2, 1], 1, ZZ) == [2, 2]
assert dup_diff([1, 2, 3, 4], 1, ZZ) == [3, 4, 3]
assert dup_diff([1, -1, 0, 0, 2], 1, ZZ) == [4, -3, 0, 0]
f = dup_normal([17, 34, 56, -345, 23, 76, 0, 0, 12, 3, 7], ZZ)
assert dup_diff(f, 0, ZZ) == f
assert dup_diff(f, 1, ZZ) == [170, 306, 448, -2415, 138, 380, 0, 0, 24, 3]
assert dup_diff(f, 2, ZZ) == dup_diff(dup_diff(f, 1, ZZ), 1, ZZ)
assert dup_diff(
f, 3, ZZ) == dup_diff(dup_diff(dup_diff(f, 1, ZZ), 1, ZZ), 1, ZZ)
K = FF(3)
f = dup_normal([17, 34, 56, -345, 23, 76, 0, 0, 12, 3, 7], K)
assert dup_diff(f, 1, K) == dup_normal([2, 0, 1, 0, 0, 2, 0, 0, 0, 0], K)
assert dup_diff(f, 2, K) == dup_normal([1, 0, 0, 2, 0, 0, 0], K)
assert dup_diff(f, 3, K) == dup_normal([], K)
assert dup_diff(f, 0, K) == f
assert dup_diff(f, 2, K) == dup_diff(dup_diff(f, 1, K), 1, K)
assert dup_diff(
f, 3, K) == dup_diff(dup_diff(dup_diff(f, 1, K), 1, K), 1, K)
def test_dmp_diff():
assert dmp_diff([], 1, 0, ZZ) == []
assert dmp_diff([[]], 1, 1, ZZ) == [[]]
assert dmp_diff([[[]]], 1, 2, ZZ) == [[[]]]
assert dmp_diff([[[1], [2]]], 1, 2, ZZ) == [[[]]]
assert dmp_diff([[[1]], [[]]], 1, 2, ZZ) == [[[1]]]
assert dmp_diff([[[3]], [[1]], [[]]], 1, 2, ZZ) == [[[6]], [[1]]]
assert dmp_diff([1, -1, 0, 0, 2], 1, 0, ZZ) == \
dup_diff([1, -1, 0, 0, 2], 1, ZZ)
assert dmp_diff(f_6, 0, 3, ZZ) == f_6
assert dmp_diff(f_6, 1, 3, ZZ) == [[[[8460]], [[]]],
[[[135, 0, 0], [], [], [-135, 0, 0]]],
[[[]]],
[[[-423]], [[-47]], [[]], [[141], [], [94, 0], []], [[]]]]
assert dmp_diff(
f_6, 2, 3, ZZ) == dmp_diff(dmp_diff(f_6, 1, 3, ZZ), 1, 3, ZZ)
assert dmp_diff(f_6, 3, 3, ZZ) == dmp_diff(
dmp_diff(dmp_diff(f_6, 1, 3, ZZ), 1, 3, ZZ), 1, 3, ZZ)
K = FF(23)
F_6 = dmp_normal(f_6, 3, K)
assert dmp_diff(F_6, 0, 3, K) == F_6
assert dmp_diff(F_6, 1, 3, K) == dmp_diff(F_6, 1, 3, K)
assert dmp_diff(F_6, 2, 3, K) == dmp_diff(dmp_diff(F_6, 1, 3, K), 1, 3, K)
assert dmp_diff(F_6, 3, 3, K) == dmp_diff(
dmp_diff(dmp_diff(F_6, 1, 3, K), 1, 3, K), 1, 3, K)
def test_dmp_diff_in():
assert dmp_diff_in(f_6, 2, 1, 3, ZZ) == \
dmp_swap(dmp_diff(dmp_swap(f_6, 0, 1, 3, ZZ), 2, 3, ZZ), 0, 1, 3, ZZ)
assert dmp_diff_in(f_6, 3, 1, 3, ZZ) == \
dmp_swap(dmp_diff(dmp_swap(f_6, 0, 1, 3, ZZ), 3, 3, ZZ), 0, 1, 3, ZZ)
assert dmp_diff_in(f_6, 2, 2, 3, ZZ) == \
dmp_swap(dmp_diff(dmp_swap(f_6, 0, 2, 3, ZZ), 2, 3, ZZ), 0, 2, 3, ZZ)
assert dmp_diff_in(f_6, 3, 2, 3, ZZ) == \
dmp_swap(dmp_diff(dmp_swap(f_6, 0, 2, 3, ZZ), 3, 3, ZZ), 0, 2, 3, ZZ)
raises(IndexError, lambda: dmp_diff_in(f_6, 1, -1, 3, ZZ))
raises(IndexError, lambda: dmp_diff_in(f_6, 1, 4, 3, ZZ))
def test_dup_eval():
assert dup_eval([], 7, ZZ) == 0
assert dup_eval([1, 2], 0, ZZ) == 2
assert dup_eval([1, 2, 3], 7, ZZ) == 66
def test_dmp_eval():
assert dmp_eval([], 3, 0, ZZ) == 0
assert dmp_eval([[]], 3, 1, ZZ) == []
assert dmp_eval([[[]]], 3, 2, ZZ) == [[]]
assert dmp_eval([[1, 2]], 0, 1, ZZ) == [1, 2]
assert dmp_eval([[[1]]], 3, 2, ZZ) == [[1]]
assert dmp_eval([[[1, 2]]], 3, 2, ZZ) == [[1, 2]]
assert dmp_eval([[3, 2], [1, 2]], 3, 1, ZZ) == [10, 8]
assert dmp_eval([[[3, 2]], [[1, 2]]], 3, 2, ZZ) == [[10, 8]]
def test_dmp_eval_in():
assert dmp_eval_in(
f_6, -2, 1, 3, ZZ) == dmp_eval(dmp_swap(f_6, 0, 1, 3, ZZ), -2, 3, ZZ)
assert dmp_eval_in(
f_6, 7, 1, 3, ZZ) == dmp_eval(dmp_swap(f_6, 0, 1, 3, ZZ), 7, 3, ZZ)
assert dmp_eval_in(f_6, -2, 2, 3, ZZ) == dmp_swap(
dmp_eval(dmp_swap(f_6, 0, 2, 3, ZZ), -2, 3, ZZ), 0, 1, 2, ZZ)
assert dmp_eval_in(f_6, 7, 2, 3, ZZ) == dmp_swap(
dmp_eval(dmp_swap(f_6, 0, 2, 3, ZZ), 7, 3, ZZ), 0, 1, 2, ZZ)
f = [[[int(45)]], [[]], [[]], [[int(-9)], [-1], [], [int(3), int(0), int(10), int(0)]]]
assert dmp_eval_in(f, -2, 2, 2, ZZ) == \
[[45], [], [], [-9, -1, 0, -44]]
raises(IndexError, lambda: dmp_eval_in(f_6, ZZ(1), -1, 3, ZZ))
raises(IndexError, lambda: dmp_eval_in(f_6, ZZ(1), 4, 3, ZZ))
def test_dmp_eval_tail():
assert dmp_eval_tail([[]], [1], 1, ZZ) == []
assert dmp_eval_tail([[[]]], [1], 2, ZZ) == [[]]
assert dmp_eval_tail([[[]]], [1, 2], 2, ZZ) == []
assert dmp_eval_tail(f_0, [], 2, ZZ) == f_0
assert dmp_eval_tail(f_0, [1, -17, 8], 2, ZZ) == 84496
assert dmp_eval_tail(f_0, [-17, 8], 2, ZZ) == [-1409, 3, 85902]
assert dmp_eval_tail(f_0, [8], 2, ZZ) == [[83, 2], [3], [302, 81, 1]]
assert dmp_eval_tail(f_1, [-17, 8], 2, ZZ) == [-136, 15699, 9166, -27144]
assert dmp_eval_tail(
f_2, [-12, 3], 2, ZZ) == [-1377, 0, -702, -1224, 0, -624]
assert dmp_eval_tail(
f_3, [-12, 3], 2, ZZ) == [144, 82, -5181, -28872, -14868, -540]
assert dmp_eval_tail(
f_4, [25, -1], 2, ZZ) == [152587890625, 9765625, -59605407714843750,
-3839159765625, -1562475, 9536712644531250, 610349546750, -4, 24414375000, 1562520]
assert dmp_eval_tail(f_5, [25, -1], 2, ZZ) == [-1, -78, -2028, -17576]
assert dmp_eval_tail(f_6, [0, 2, 4], 3, ZZ) == [5040, 0, 0, 4480]
def test_dmp_diff_eval_in():
assert dmp_diff_eval_in(f_6, 2, 7, 1, 3, ZZ) == \
dmp_eval(dmp_diff(dmp_swap(f_6, 0, 1, 3, ZZ), 2, 3, ZZ), 7, 3, ZZ)
assert dmp_diff_eval_in(f_6, 2, 7, 0, 3, ZZ) == \
dmp_eval(dmp_diff(f_6, 2, 3, ZZ), 7, 3, ZZ)
raises(IndexError, lambda: dmp_diff_eval_in(f_6, 1, ZZ(1), 4, 3, ZZ))
def test_dup_revert():
f = [-QQ(1, 720), QQ(0), QQ(1, 24), QQ(0), -QQ(1, 2), QQ(0), QQ(1)]
g = [QQ(61, 720), QQ(0), QQ(5, 24), QQ(0), QQ(1, 2), QQ(0), QQ(1)]
assert dup_revert(f, 8, QQ) == g
raises(NotReversible, lambda: dup_revert([QQ(1), QQ(0)], 3, QQ))
def test_dmp_revert():
f = [-QQ(1, 720), QQ(0), QQ(1, 24), QQ(0), -QQ(1, 2), QQ(0), QQ(1)]
g = [QQ(61, 720), QQ(0), QQ(5, 24), QQ(0), QQ(1, 2), QQ(0), QQ(1)]
assert dmp_revert(f, 8, 0, QQ) == g
raises(MultivariatePolynomialError, lambda: dmp_revert([[1]], 2, 1, QQ))
def test_dup_trunc():
assert dup_trunc([1, 2, 3, 4, 5, 6], ZZ(3), ZZ) == [1, -1, 0, 1, -1, 0]
assert dup_trunc([6, 5, 4, 3, 2, 1], ZZ(3), ZZ) == [-1, 1, 0, -1, 1]
R = ZZ_I
assert dup_trunc([R(3), R(4), R(5)], R(3), R) == [R(1), R(-1)]
K = FF(5)
assert dup_trunc([K(3), K(4), K(5)], K(3), K) == [K(1), K(0)]
def test_dmp_trunc():
assert dmp_trunc([[]], [1, 2], 2, ZZ) == [[]]
assert dmp_trunc([[1, 2], [1, 4, 1], [1]], [1, 2], 1, ZZ) == [[-3], [1]]
def test_dmp_ground_trunc():
assert dmp_ground_trunc(f_0, ZZ(3), 2, ZZ) == \
dmp_normal(
[[[1, -1, 0], [-1]], [[]], [[1, -1, 0], [1, -1, 1], [1]]], 2, ZZ)
def test_dup_monic():
assert dup_monic([3, 6, 9], ZZ) == [1, 2, 3]
raises(ExactQuotientFailed, lambda: dup_monic([3, 4, 5], ZZ))
assert dup_monic([], QQ) == []
assert dup_monic([QQ(1)], QQ) == [QQ(1)]
assert dup_monic([QQ(7), QQ(1), QQ(21)], QQ) == [QQ(1), QQ(1, 7), QQ(3)]
def test_dmp_ground_monic():
assert dmp_ground_monic([3, 6, 9], 0, ZZ) == [1, 2, 3]
assert dmp_ground_monic([[3], [6], [9]], 1, ZZ) == [[1], [2], [3]]
raises(
ExactQuotientFailed, lambda: dmp_ground_monic([[3], [4], [5]], 1, ZZ))
assert dmp_ground_monic([[]], 1, QQ) == [[]]
assert dmp_ground_monic([[QQ(1)]], 1, QQ) == [[QQ(1)]]
assert dmp_ground_monic(
[[QQ(7)], [QQ(1)], [QQ(21)]], 1, QQ) == [[QQ(1)], [QQ(1, 7)], [QQ(3)]]
def test_dup_content():
assert dup_content([], ZZ) == ZZ(0)
assert dup_content([1], ZZ) == ZZ(1)
assert dup_content([-1], ZZ) == ZZ(1)
assert dup_content([1, 1], ZZ) == ZZ(1)
assert dup_content([2, 2], ZZ) == ZZ(2)
assert dup_content([1, 2, 1], ZZ) == ZZ(1)
assert dup_content([2, 4, 2], ZZ) == ZZ(2)
assert dup_content([QQ(2, 3), QQ(4, 9)], QQ) == QQ(2, 9)
assert dup_content([QQ(2, 3), QQ(4, 5)], QQ) == QQ(2, 15)
def test_dmp_ground_content():
assert dmp_ground_content([[]], 1, ZZ) == ZZ(0)
assert dmp_ground_content([[]], 1, QQ) == QQ(0)
assert dmp_ground_content([[1]], 1, ZZ) == ZZ(1)
assert dmp_ground_content([[-1]], 1, ZZ) == ZZ(1)
assert dmp_ground_content([[1], [1]], 1, ZZ) == ZZ(1)
assert dmp_ground_content([[2], [2]], 1, ZZ) == ZZ(2)
assert dmp_ground_content([[1], [2], [1]], 1, ZZ) == ZZ(1)
assert dmp_ground_content([[2], [4], [2]], 1, ZZ) == ZZ(2)
assert dmp_ground_content([[QQ(2, 3)], [QQ(4, 9)]], 1, QQ) == QQ(2, 9)
assert dmp_ground_content([[QQ(2, 3)], [QQ(4, 5)]], 1, QQ) == QQ(2, 15)
assert dmp_ground_content(f_0, 2, ZZ) == ZZ(1)
assert dmp_ground_content(
dmp_mul_ground(f_0, ZZ(2), 2, ZZ), 2, ZZ) == ZZ(2)
assert dmp_ground_content(f_1, 2, ZZ) == ZZ(1)
assert dmp_ground_content(
dmp_mul_ground(f_1, ZZ(3), 2, ZZ), 2, ZZ) == ZZ(3)
assert dmp_ground_content(f_2, 2, ZZ) == ZZ(1)
assert dmp_ground_content(
dmp_mul_ground(f_2, ZZ(4), 2, ZZ), 2, ZZ) == ZZ(4)
assert dmp_ground_content(f_3, 2, ZZ) == ZZ(1)
assert dmp_ground_content(
dmp_mul_ground(f_3, ZZ(5), 2, ZZ), 2, ZZ) == ZZ(5)
assert dmp_ground_content(f_4, 2, ZZ) == ZZ(1)
assert dmp_ground_content(
dmp_mul_ground(f_4, ZZ(6), 2, ZZ), 2, ZZ) == ZZ(6)
assert dmp_ground_content(f_5, 2, ZZ) == ZZ(1)
assert dmp_ground_content(
dmp_mul_ground(f_5, ZZ(7), 2, ZZ), 2, ZZ) == ZZ(7)
assert dmp_ground_content(f_6, 3, ZZ) == ZZ(1)
assert dmp_ground_content(
dmp_mul_ground(f_6, ZZ(8), 3, ZZ), 3, ZZ) == ZZ(8)
def test_dup_primitive():
assert dup_primitive([], ZZ) == (ZZ(0), [])
assert dup_primitive([ZZ(1)], ZZ) == (ZZ(1), [ZZ(1)])
assert dup_primitive([ZZ(1), ZZ(1)], ZZ) == (ZZ(1), [ZZ(1), ZZ(1)])
assert dup_primitive([ZZ(2), ZZ(2)], ZZ) == (ZZ(2), [ZZ(1), ZZ(1)])
assert dup_primitive(
[ZZ(1), ZZ(2), ZZ(1)], ZZ) == (ZZ(1), [ZZ(1), ZZ(2), ZZ(1)])
assert dup_primitive(
[ZZ(2), ZZ(4), ZZ(2)], ZZ) == (ZZ(2), [ZZ(1), ZZ(2), ZZ(1)])
assert dup_primitive([], QQ) == (QQ(0), [])
assert dup_primitive([QQ(1)], QQ) == (QQ(1), [QQ(1)])
assert dup_primitive([QQ(1), QQ(1)], QQ) == (QQ(1), [QQ(1), QQ(1)])
assert dup_primitive([QQ(2), QQ(2)], QQ) == (QQ(2), [QQ(1), QQ(1)])
assert dup_primitive(
[QQ(1), QQ(2), QQ(1)], QQ) == (QQ(1), [QQ(1), QQ(2), QQ(1)])
assert dup_primitive(
[QQ(2), QQ(4), QQ(2)], QQ) == (QQ(2), [QQ(1), QQ(2), QQ(1)])
assert dup_primitive(
[QQ(2, 3), QQ(4, 9)], QQ) == (QQ(2, 9), [QQ(3), QQ(2)])
assert dup_primitive(
[QQ(2, 3), QQ(4, 5)], QQ) == (QQ(2, 15), [QQ(5), QQ(6)])
def test_dmp_ground_primitive():
assert dmp_ground_primitive([ZZ(1)], 0, ZZ) == (ZZ(1), [ZZ(1)])
assert dmp_ground_primitive([[]], 1, ZZ) == (ZZ(0), [[]])
assert dmp_ground_primitive(f_0, 2, ZZ) == (ZZ(1), f_0)
assert dmp_ground_primitive(
dmp_mul_ground(f_0, ZZ(2), 2, ZZ), 2, ZZ) == (ZZ(2), f_0)
assert dmp_ground_primitive(f_1, 2, ZZ) == (ZZ(1), f_1)
assert dmp_ground_primitive(
dmp_mul_ground(f_1, ZZ(3), 2, ZZ), 2, ZZ) == (ZZ(3), f_1)
assert dmp_ground_primitive(f_2, 2, ZZ) == (ZZ(1), f_2)
assert dmp_ground_primitive(
dmp_mul_ground(f_2, ZZ(4), 2, ZZ), 2, ZZ) == (ZZ(4), f_2)
assert dmp_ground_primitive(f_3, 2, ZZ) == (ZZ(1), f_3)
assert dmp_ground_primitive(
dmp_mul_ground(f_3, ZZ(5), 2, ZZ), 2, ZZ) == (ZZ(5), f_3)
assert dmp_ground_primitive(f_4, 2, ZZ) == (ZZ(1), f_4)
assert dmp_ground_primitive(
dmp_mul_ground(f_4, ZZ(6), 2, ZZ), 2, ZZ) == (ZZ(6), f_4)
assert dmp_ground_primitive(f_5, 2, ZZ) == (ZZ(1), f_5)
assert dmp_ground_primitive(
dmp_mul_ground(f_5, ZZ(7), 2, ZZ), 2, ZZ) == (ZZ(7), f_5)
assert dmp_ground_primitive(f_6, 3, ZZ) == (ZZ(1), f_6)
assert dmp_ground_primitive(
dmp_mul_ground(f_6, ZZ(8), 3, ZZ), 3, ZZ) == (ZZ(8), f_6)
assert dmp_ground_primitive([[ZZ(2)]], 1, ZZ) == (ZZ(2), [[ZZ(1)]])
assert dmp_ground_primitive([[QQ(2)]], 1, QQ) == (QQ(2), [[QQ(1)]])
assert dmp_ground_primitive(
[[QQ(2, 3)], [QQ(4, 9)]], 1, QQ) == (QQ(2, 9), [[QQ(3)], [QQ(2)]])
assert dmp_ground_primitive(
[[QQ(2, 3)], [QQ(4, 5)]], 1, QQ) == (QQ(2, 15), [[QQ(5)], [QQ(6)]])
def test_dup_extract():
f = dup_normal([2930944, 0, 2198208, 0, 549552, 0, 45796], ZZ)
g = dup_normal([17585664, 0, 8792832, 0, 1099104, 0], ZZ)
F = dup_normal([64, 0, 48, 0, 12, 0, 1], ZZ)
G = dup_normal([384, 0, 192, 0, 24, 0], ZZ)
assert dup_extract(f, g, ZZ) == (45796, F, G)
def test_dmp_ground_extract():
f = dmp_normal(
[[2930944], [], [2198208], [], [549552], [], [45796]], 1, ZZ)
g = dmp_normal([[17585664], [], [8792832], [], [1099104], []], 1, ZZ)
F = dmp_normal([[64], [], [48], [], [12], [], [1]], 1, ZZ)
G = dmp_normal([[384], [], [192], [], [24], []], 1, ZZ)
assert dmp_ground_extract(f, g, 1, ZZ) == (45796, F, G)
def test_dup_real_imag():
assert dup_real_imag([], ZZ) == ([[]], [[]])
assert dup_real_imag([1], ZZ) == ([[1]], [[]])
assert dup_real_imag([1, 1], ZZ) == ([[1], [1]], [[1, 0]])
assert dup_real_imag([1, 2], ZZ) == ([[1], [2]], [[1, 0]])
assert dup_real_imag(
[1, 2, 3], ZZ) == ([[1], [2], [-1, 0, 3]], [[2, 0], [2, 0]])
assert dup_real_imag([ZZ(1), ZZ(0), ZZ(1), ZZ(3)], ZZ) == (
[[ZZ(1)], [], [ZZ(-3), ZZ(0), ZZ(1)], [ZZ(3)]],
[[ZZ(3), ZZ(0)], [], [ZZ(-1), ZZ(0), ZZ(1), ZZ(0)]]
)
raises(DomainError, lambda: dup_real_imag([EX(1), EX(2)], EX))
def test_dup_mirror():
assert dup_mirror([], ZZ) == []
assert dup_mirror([1], ZZ) == [1]
assert dup_mirror([1, 2, 3, 4, 5], ZZ) == [1, -2, 3, -4, 5]
assert dup_mirror([1, 2, 3, 4, 5, 6], ZZ) == [-1, 2, -3, 4, -5, 6]
def test_dup_scale():
assert dup_scale([], -1, ZZ) == []
assert dup_scale([1], -1, ZZ) == [1]
assert dup_scale([1, 2, 3, 4, 5], -1, ZZ) == [1, -2, 3, -4, 5]
assert dup_scale([1, 2, 3, 4, 5], -7, ZZ) == [2401, -686, 147, -28, 5]
def test_dup_shift():
assert dup_shift([], 1, ZZ) == []
assert dup_shift([1], 1, ZZ) == [1]
assert dup_shift([1, 2, 3, 4, 5], 1, ZZ) == [1, 6, 15, 20, 15]
assert dup_shift([1, 2, 3, 4, 5], 7, ZZ) == [1, 30, 339, 1712, 3267]
def test_dmp_shift():
assert dmp_shift([ZZ(1), ZZ(2)], [ZZ(1)], 0, ZZ) == [ZZ(1), ZZ(3)]
assert dmp_shift([[]], [ZZ(1), ZZ(2)], 1, ZZ) == [[]]
xy = [[ZZ(1), ZZ(0)], []] # x*y
x1y2 = [[ZZ(1), ZZ(2)], [ZZ(1), ZZ(2)]] # (x+1)*(y+2)
assert dmp_shift(xy, [ZZ(1), ZZ(2)], 1, ZZ) == x1y2
def test_dup_transform():
assert dup_transform([], [], [1, 1], ZZ) == []
assert dup_transform([], [1], [1, 1], ZZ) == []
assert dup_transform([], [1, 2], [1, 1], ZZ) == []
assert dup_transform([6, -5, 4, -3, 17], [1, -3, 4], [2, -3], ZZ) == \
[6, -82, 541, -2205, 6277, -12723, 17191, -13603, 4773]
def test_dup_compose():
assert dup_compose([], [], ZZ) == []
assert dup_compose([], [1], ZZ) == []
assert dup_compose([], [1, 2], ZZ) == []
assert dup_compose([1], [], ZZ) == [1]
assert dup_compose([1, 2, 0], [], ZZ) == []
assert dup_compose([1, 2, 1], [], ZZ) == [1]
assert dup_compose([1, 2, 1], [1], ZZ) == [4]
assert dup_compose([1, 2, 1], [7], ZZ) == [64]
assert dup_compose([1, 2, 1], [1, -1], ZZ) == [1, 0, 0]
assert dup_compose([1, 2, 1], [1, 1], ZZ) == [1, 4, 4]
assert dup_compose([1, 2, 1], [1, 2, 1], ZZ) == [1, 4, 8, 8, 4]
def test_dmp_compose():
assert dmp_compose([1, 2, 1], [1, 2, 1], 0, ZZ) == [1, 4, 8, 8, 4]
assert dmp_compose([[[]]], [[[]]], 2, ZZ) == [[[]]]
assert dmp_compose([[[]]], [[[1]]], 2, ZZ) == [[[]]]
assert dmp_compose([[[]]], [[[1]], [[2]]], 2, ZZ) == [[[]]]
assert dmp_compose([[[1]]], [], 2, ZZ) == [[[1]]]
assert dmp_compose([[1], [2], [ ]], [[]], 1, ZZ) == [[]]
assert dmp_compose([[1], [2], [1]], [[]], 1, ZZ) == [[1]]
assert dmp_compose([[1], [2], [1]], [[1]], 1, ZZ) == [[4]]
assert dmp_compose([[1], [2], [1]], [[7]], 1, ZZ) == [[64]]
assert dmp_compose([[1], [2], [1]], [[1], [-1]], 1, ZZ) == [[1], [ ], [ ]]
assert dmp_compose([[1], [2], [1]], [[1], [ 1]], 1, ZZ) == [[1], [4], [4]]
assert dmp_compose(
[[1], [2], [1]], [[1], [2], [1]], 1, ZZ) == [[1], [4], [8], [8], [4]]
def test_dup_decompose():
assert dup_decompose([1], ZZ) == [[1]]
assert dup_decompose([1, 0], ZZ) == [[1, 0]]
assert dup_decompose([1, 0, 0, 0], ZZ) == [[1, 0, 0, 0]]
assert dup_decompose([1, 0, 0, 0, 0], ZZ) == [[1, 0, 0], [1, 0, 0]]
assert dup_decompose(
[1, 0, 0, 0, 0, 0, 0], ZZ) == [[1, 0, 0, 0], [1, 0, 0]]
assert dup_decompose([7, 0, 0, 0, 1], ZZ) == [[7, 0, 1], [1, 0, 0]]
assert dup_decompose([4, 0, 3, 0, 2], ZZ) == [[4, 3, 2], [1, 0, 0]]
f = [1, 0, 20, 0, 150, 0, 500, 0, 625, -2, 0, -10, 9]
assert dup_decompose(f, ZZ) == [[1, 0, 0, -2, 9], [1, 0, 5, 0]]
f = [2, 0, 40, 0, 300, 0, 1000, 0, 1250, -4, 0, -20, 18]
assert dup_decompose(f, ZZ) == [[2, 0, 0, -4, 18], [1, 0, 5, 0]]
f = [1, 0, 20, -8, 150, -120, 524, -600, 865, -1034, 600, -170, 29]
assert dup_decompose(f, ZZ) == [[1, -8, 24, -34, 29], [1, 0, 5, 0]]
R, t = ring("t", ZZ)
f = [6*t**2 - 42,
48*t**2 + 96,
144*t**2 + 648*t + 288,
624*t**2 + 864*t + 384,
108*t**3 + 312*t**2 + 432*t + 192]
assert dup_decompose(f, R.to_domain()) == [f]
def test_dmp_lift():
q = [QQ(1, 1), QQ(0, 1), QQ(1, 1)]
f_a = [ANP([QQ(1, 1)], q, QQ), ANP([], q, QQ), ANP([], q, QQ),
ANP([QQ(1, 1), QQ(0, 1)], q, QQ), ANP([QQ(17, 1), QQ(0, 1)], q, QQ)]
f_lift = [QQ(1), QQ(0), QQ(0), QQ(0), QQ(0), QQ(0), QQ(2), QQ(0), QQ(578),
QQ(0), QQ(0), QQ(0), QQ(1), QQ(0), QQ(-578), QQ(0), QQ(83521)]
assert dmp_lift(f_a, 0, QQ.algebraic_field(I)) == f_lift
f_g = [QQ_I(1), QQ_I(0), QQ_I(0), QQ_I(0, 1), QQ_I(0, 17)]
assert dmp_lift(f_g, 0, QQ_I) == f_lift
raises(DomainError, lambda: dmp_lift([EX(1), EX(2)], 0, EX))
def test_dup_sign_variations():
assert dup_sign_variations([], ZZ) == 0
assert dup_sign_variations([1, 0], ZZ) == 0
assert dup_sign_variations([1, 0, 2], ZZ) == 0
assert dup_sign_variations([1, 0, 3, 0], ZZ) == 0
assert dup_sign_variations([1, 0, 4, 0, 5], ZZ) == 0
assert dup_sign_variations([-1, 0, 2], ZZ) == 1
assert dup_sign_variations([-1, 0, 3, 0], ZZ) == 1
assert dup_sign_variations([-1, 0, 4, 0, 5], ZZ) == 1
assert dup_sign_variations([-1, -4, -5], ZZ) == 0
assert dup_sign_variations([ 1, -4, -5], ZZ) == 1
assert dup_sign_variations([ 1, 4, -5], ZZ) == 1
assert dup_sign_variations([ 1, -4, 5], ZZ) == 2
assert dup_sign_variations([-1, 4, -5], ZZ) == 2
assert dup_sign_variations([-1, 4, 5], ZZ) == 1
assert dup_sign_variations([-1, -4, 5], ZZ) == 1
assert dup_sign_variations([ 1, 4, 5], ZZ) == 0
assert dup_sign_variations([-1, 0, -4, 0, -5], ZZ) == 0
assert dup_sign_variations([ 1, 0, -4, 0, -5], ZZ) == 1
assert dup_sign_variations([ 1, 0, 4, 0, -5], ZZ) == 1
assert dup_sign_variations([ 1, 0, -4, 0, 5], ZZ) == 2
assert dup_sign_variations([-1, 0, 4, 0, -5], ZZ) == 2
assert dup_sign_variations([-1, 0, 4, 0, 5], ZZ) == 1
assert dup_sign_variations([-1, 0, -4, 0, 5], ZZ) == 1
assert dup_sign_variations([ 1, 0, 4, 0, 5], ZZ) == 0
def test_dup_clear_denoms():
assert dup_clear_denoms([], QQ, ZZ) == (ZZ(1), [])
assert dup_clear_denoms([QQ(1)], QQ, ZZ) == (ZZ(1), [QQ(1)])
assert dup_clear_denoms([QQ(7)], QQ, ZZ) == (ZZ(1), [QQ(7)])
assert dup_clear_denoms([QQ(7, 3)], QQ) == (ZZ(3), [QQ(7)])
assert dup_clear_denoms([QQ(7, 3)], QQ, ZZ) == (ZZ(3), [QQ(7)])
assert dup_clear_denoms(
[QQ(3), QQ(1), QQ(0)], QQ, ZZ) == (ZZ(1), [QQ(3), QQ(1), QQ(0)])
assert dup_clear_denoms(
[QQ(1), QQ(1, 2), QQ(0)], QQ, ZZ) == (ZZ(2), [QQ(2), QQ(1), QQ(0)])
assert dup_clear_denoms([QQ(3), QQ(
1), QQ(0)], QQ, ZZ, convert=True) == (ZZ(1), [ZZ(3), ZZ(1), ZZ(0)])
assert dup_clear_denoms([QQ(1), QQ(
1, 2), QQ(0)], QQ, ZZ, convert=True) == (ZZ(2), [ZZ(2), ZZ(1), ZZ(0)])
assert dup_clear_denoms(
[EX(S(3)/2), EX(S(9)/4)], EX) == (EX(4), [EX(6), EX(9)])
assert dup_clear_denoms([EX(7)], EX) == (EX(1), [EX(7)])
assert dup_clear_denoms([EX(sin(x)/x), EX(0)], EX) == (EX(x), [EX(sin(x)), EX(0)])
F = RR.frac_field(x)
result = dup_clear_denoms([F(8.48717/(8.0089*x + 2.83)), F(0.0)], F)
assert str(result) == "(x + 0.353356890459364, [1.05971731448763, 0.0])"
def test_dmp_clear_denoms():
assert dmp_clear_denoms([[]], 1, QQ, ZZ) == (ZZ(1), [[]])
assert dmp_clear_denoms([[QQ(1)]], 1, QQ, ZZ) == (ZZ(1), [[QQ(1)]])
assert dmp_clear_denoms([[QQ(7)]], 1, QQ, ZZ) == (ZZ(1), [[QQ(7)]])
assert dmp_clear_denoms([[QQ(7, 3)]], 1, QQ) == (ZZ(3), [[QQ(7)]])
assert dmp_clear_denoms([[QQ(7, 3)]], 1, QQ, ZZ) == (ZZ(3), [[QQ(7)]])
assert dmp_clear_denoms(
[[QQ(3)], [QQ(1)], []], 1, QQ, ZZ) == (ZZ(1), [[QQ(3)], [QQ(1)], []])
assert dmp_clear_denoms([[QQ(
1)], [QQ(1, 2)], []], 1, QQ, ZZ) == (ZZ(2), [[QQ(2)], [QQ(1)], []])
assert dmp_clear_denoms([QQ(3), QQ(
1), QQ(0)], 0, QQ, ZZ, convert=True) == (ZZ(1), [ZZ(3), ZZ(1), ZZ(0)])
assert dmp_clear_denoms([QQ(1), QQ(1, 2), QQ(
0)], 0, QQ, ZZ, convert=True) == (ZZ(2), [ZZ(2), ZZ(1), ZZ(0)])
assert dmp_clear_denoms([[QQ(3)], [QQ(
1)], []], 1, QQ, ZZ, convert=True) == (ZZ(1), [[QQ(3)], [QQ(1)], []])
assert dmp_clear_denoms([[QQ(1)], [QQ(1, 2)], []], 1, QQ, ZZ,
convert=True) == (ZZ(2), [[QQ(2)], [QQ(1)], []])
assert dmp_clear_denoms(
[[EX(S(3)/2)], [EX(S(9)/4)]], 1, EX) == (EX(4), [[EX(6)], [EX(9)]])
assert dmp_clear_denoms([[EX(7)]], 1, EX) == (EX(1), [[EX(7)]])
assert dmp_clear_denoms([[EX(sin(x)/x), EX(0)]], 1, EX) == (EX(x), [[EX(sin(x)), EX(0)]])

View File

@ -0,0 +1,95 @@
from sympy.core import Symbol, S, oo
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.polys import poly
from sympy.polys.dispersion import dispersion, dispersionset
def test_dispersion():
x = Symbol("x")
a = Symbol("a")
fp = poly(S.Zero, x)
assert sorted(dispersionset(fp)) == [0]
fp = poly(S(2), x)
assert sorted(dispersionset(fp)) == [0]
fp = poly(x + 1, x)
assert sorted(dispersionset(fp)) == [0]
assert dispersion(fp) == 0
fp = poly((x + 1)*(x + 2), x)
assert sorted(dispersionset(fp)) == [0, 1]
assert dispersion(fp) == 1
fp = poly(x*(x + 3), x)
assert sorted(dispersionset(fp)) == [0, 3]
assert dispersion(fp) == 3
fp = poly((x - 3)*(x + 3), x)
assert sorted(dispersionset(fp)) == [0, 6]
assert dispersion(fp) == 6
fp = poly(x**4 - 3*x**2 + 1, x)
gp = fp.shift(-3)
assert sorted(dispersionset(fp, gp)) == [2, 3, 4]
assert dispersion(fp, gp) == 4
assert sorted(dispersionset(gp, fp)) == []
assert dispersion(gp, fp) is -oo
fp = poly(x*(3*x**2+a)*(x-2536)*(x**3+a), x)
gp = fp.as_expr().subs(x, x-345).as_poly(x)
assert sorted(dispersionset(fp, gp)) == [345, 2881]
assert sorted(dispersionset(gp, fp)) == [2191]
gp = poly((x-2)**2*(x-3)**3*(x-5)**3, x)
assert sorted(dispersionset(gp)) == [0, 1, 2, 3]
assert sorted(dispersionset(gp, (gp+4)**2)) == [1, 2]
fp = poly(x*(x+2)*(x-1), x)
assert sorted(dispersionset(fp)) == [0, 1, 2, 3]
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)>')
assert sorted(dispersionset(fp, gp)) == [2]
assert sorted(dispersionset(gp, fp)) == [1, 4]
# There are some difficulties if we compute over Z[a]
# and alpha happenes to lie in Z[a] instead of simply Z.
# Hence we can not decide if alpha is indeed integral
# in general.
fp = poly(4*x**4 + (4*a + 8)*x**3 + (a**2 + 6*a + 4)*x**2 + (a**2 + 2*a)*x, x)
assert sorted(dispersionset(fp)) == [0, 1]
# For any specific value of a, the dispersion is 3*a
# but the algorithm can not find this in general.
# This is the point where the resultant based Ansatz
# is superior to the current one.
fp = poly(a**2*x**3 + (a**3 + a**2 + a + 1)*x, x)
gp = fp.as_expr().subs(x, x - 3*a).as_poly(x)
assert sorted(dispersionset(fp, gp)) == []
fpa = fp.as_expr().subs(a, 2).as_poly(x)
gpa = gp.as_expr().subs(a, 2).as_poly(x)
assert sorted(dispersionset(fpa, gpa)) == [6]
# Work with Expr instead of Poly
f = (x + 1)*(x + 2)
assert sorted(dispersionset(f)) == [0, 1]
assert dispersion(f) == 1
f = x**4 - 3*x**2 + 1
g = x**4 - 12*x**3 + 51*x**2 - 90*x + 55
assert sorted(dispersionset(f, g)) == [2, 3, 4]
assert dispersion(f, g) == 4
# Work with Expr and specify a generator
f = (x + 1)*(x + 2)
assert sorted(dispersionset(f, None, x)) == [0, 1]
assert dispersion(f, None, x) == 1
f = x**4 - 3*x**2 + 1
g = x**4 - 12*x**3 + 51*x**2 - 90*x + 55
assert sorted(dispersionset(f, g, x)) == [2, 3, 4]
assert dispersion(f, g, x) == 4

View File

@ -0,0 +1,208 @@
"""Tests for sparse distributed modules. """
from sympy.polys.distributedmodules import (
sdm_monomial_mul, sdm_monomial_deg, sdm_monomial_divides,
sdm_add, sdm_LM, sdm_LT, sdm_mul_term, sdm_zero, sdm_deg,
sdm_LC, sdm_from_dict,
sdm_spoly, sdm_ecart, sdm_nf_mora, sdm_groebner,
sdm_from_vector, sdm_to_vector, sdm_monomial_lcm
)
from sympy.polys.orderings import lex, grlex, InverseOrder
from sympy.polys.domains import QQ
from sympy.abc import x, y, z
def test_sdm_monomial_mul():
assert sdm_monomial_mul((1, 1, 0), (1, 3)) == (1, 2, 3)
def test_sdm_monomial_deg():
assert sdm_monomial_deg((5, 2, 1)) == 3
def test_sdm_monomial_lcm():
assert sdm_monomial_lcm((1, 2, 3), (1, 5, 0)) == (1, 5, 3)
def test_sdm_monomial_divides():
assert sdm_monomial_divides((1, 0, 0), (1, 0, 0)) is True
assert sdm_monomial_divides((1, 0, 0), (1, 2, 1)) is True
assert sdm_monomial_divides((5, 1, 1), (5, 2, 1)) is True
assert sdm_monomial_divides((1, 0, 0), (2, 0, 0)) is False
assert sdm_monomial_divides((1, 1, 0), (1, 0, 0)) is False
assert sdm_monomial_divides((5, 1, 2), (5, 0, 1)) is False
def test_sdm_LC():
assert sdm_LC([((1, 2, 3), QQ(5))], QQ) == QQ(5)
def test_sdm_from_dict():
dic = {(1, 2, 1, 1): QQ(1), (1, 1, 2, 1): QQ(1), (1, 0, 2, 1): QQ(1),
(1, 0, 0, 3): QQ(1), (1, 1, 1, 0): QQ(1)}
assert sdm_from_dict(dic, grlex) == \
[((1, 2, 1, 1), QQ(1)), ((1, 1, 2, 1), QQ(1)),
((1, 0, 2, 1), QQ(1)), ((1, 0, 0, 3), QQ(1)), ((1, 1, 1, 0), QQ(1))]
# TODO test to_dict?
def test_sdm_add():
assert sdm_add([((1, 1, 1), QQ(1))], [((2, 0, 0), QQ(1))], lex, QQ) == \
[((2, 0, 0), QQ(1)), ((1, 1, 1), QQ(1))]
assert sdm_add([((1, 1, 1), QQ(1))], [((1, 1, 1), QQ(-1))], lex, QQ) == []
assert sdm_add([((1, 0, 0), QQ(1))], [((1, 0, 0), QQ(2))], lex, QQ) == \
[((1, 0, 0), QQ(3))]
assert sdm_add([((1, 0, 1), QQ(1))], [((1, 1, 0), QQ(1))], lex, QQ) == \
[((1, 1, 0), QQ(1)), ((1, 0, 1), QQ(1))]
def test_sdm_LM():
dic = {(1, 2, 3): QQ(1), (4, 0, 0): QQ(1), (4, 0, 1): QQ(1)}
assert sdm_LM(sdm_from_dict(dic, lex)) == (4, 0, 1)
def test_sdm_LT():
dic = {(1, 2, 3): QQ(1), (4, 0, 0): QQ(2), (4, 0, 1): QQ(3)}
assert sdm_LT(sdm_from_dict(dic, lex)) == ((4, 0, 1), QQ(3))
def test_sdm_mul_term():
assert sdm_mul_term([((1, 0, 0), QQ(1))], ((0, 0), QQ(0)), lex, QQ) == []
assert sdm_mul_term([], ((1, 0), QQ(1)), lex, QQ) == []
assert sdm_mul_term([((1, 0, 0), QQ(1))], ((1, 0), QQ(1)), lex, QQ) == \
[((1, 1, 0), QQ(1))]
f = [((2, 0, 1), QQ(4)), ((1, 1, 0), QQ(3))]
assert sdm_mul_term(f, ((1, 1), QQ(2)), lex, QQ) == \
[((2, 1, 2), QQ(8)), ((1, 2, 1), QQ(6))]
def test_sdm_zero():
assert sdm_zero() == []
def test_sdm_deg():
assert sdm_deg([((1, 2, 3), 1), ((10, 0, 1), 1), ((2, 3, 4), 4)]) == 7
def test_sdm_spoly():
f = [((2, 1, 1), QQ(1)), ((1, 0, 1), QQ(1))]
g = [((2, 3, 0), QQ(1))]
h = [((1, 2, 3), QQ(1))]
assert sdm_spoly(f, h, lex, QQ) == []
assert sdm_spoly(f, g, lex, QQ) == [((1, 2, 1), QQ(1))]
def test_sdm_ecart():
assert sdm_ecart([((1, 2, 3), 1), ((1, 0, 1), 1)]) == 0
assert sdm_ecart([((2, 2, 1), 1), ((1, 5, 1), 1)]) == 3
def test_sdm_nf_mora():
f = sdm_from_dict({(1, 2, 1, 1): QQ(1), (1, 1, 2, 1): QQ(1),
(1, 0, 2, 1): QQ(1), (1, 0, 0, 3): QQ(1), (1, 1, 1, 0): QQ(1)},
grlex)
f1 = sdm_from_dict({(1, 1, 1, 0): QQ(1), (1, 0, 2, 0): QQ(1),
(1, 0, 0, 0): QQ(-1)}, grlex)
f2 = sdm_from_dict({(1, 1, 1, 0): QQ(1)}, grlex)
(id0, id1, id2) = [sdm_from_dict({(i, 0, 0, 0): QQ(1)}, grlex)
for i in range(3)]
assert sdm_nf_mora(f, [f1, f2], grlex, QQ, phantom=(id0, [id1, id2])) == \
([((1, 0, 2, 1), QQ(1)), ((1, 0, 0, 3), QQ(1)), ((1, 1, 1, 0), QQ(1)),
((1, 1, 0, 1), QQ(1))],
[((1, 1, 0, 1), QQ(-1)), ((0, 0, 0, 0), QQ(1))])
assert sdm_nf_mora(f, [f2, f1], grlex, QQ, phantom=(id0, [id2, id1])) == \
([((1, 0, 2, 1), QQ(1)), ((1, 0, 0, 3), QQ(1)), ((1, 1, 1, 0), QQ(1))],
[((2, 1, 0, 1), QQ(-1)), ((2, 0, 1, 1), QQ(-1)), ((0, 0, 0, 0), QQ(1))])
f = sdm_from_vector([x*z, y**2 + y*z - z, y], lex, QQ, gens=[x, y, z])
f1 = sdm_from_vector([x, y, 1], lex, QQ, gens=[x, y, z])
f2 = sdm_from_vector([x*y, z, z**2], lex, QQ, gens=[x, y, z])
assert sdm_nf_mora(f, [f1, f2], lex, QQ) == \
sdm_nf_mora(f, [f2, f1], lex, QQ) == \
[((1, 0, 1, 1), QQ(1)), ((1, 0, 0, 1), QQ(-1)), ((0, 1, 1, 0), QQ(-1)),
((0, 1, 0, 1), QQ(1))]
def test_conversion():
f = [x**2 + y**2, 2*z]
g = [((1, 0, 0, 1), QQ(2)), ((0, 2, 0, 0), QQ(1)), ((0, 0, 2, 0), QQ(1))]
assert sdm_to_vector(g, [x, y, z], QQ) == f
assert sdm_from_vector(f, lex, QQ) == g
assert sdm_from_vector(
[x, 1], lex, QQ) == [((1, 0), QQ(1)), ((0, 1), QQ(1))]
assert sdm_to_vector([((1, 1, 0, 0), 1)], [x, y, z], QQ, n=3) == [0, x, 0]
assert sdm_from_vector([0, 0], lex, QQ, gens=[x, y]) == sdm_zero()
def test_nontrivial():
gens = [x, y, z]
def contains(I, f):
S = [sdm_from_vector([g], lex, QQ, gens=gens) for g in I]
G = sdm_groebner(S, sdm_nf_mora, lex, QQ)
return sdm_nf_mora(sdm_from_vector([f], lex, QQ, gens=gens),
G, lex, QQ) == sdm_zero()
assert contains([x, y], x)
assert contains([x, y], x + y)
assert not contains([x, y], 1)
assert not contains([x, y], z)
assert contains([x**2 + y, x**2 + x], x - y)
assert not contains([x + y + z, x*y + x*z + y*z, x*y*z], x**2)
assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x**3)
assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x**4)
assert not contains([x + y + z, x*y + x*z + y*z, x*y*z], x*y**2)
assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x**4 + y**3 + 2*z*y*x)
assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x*y*z)
assert contains([x, 1 + x + y, 5 - 7*y], 1)
assert contains(
[x**3 + y**3, y**3 + z**3, z**3 + x**3, x**2*y + x**2*z + y**2*z],
x**3)
assert not contains(
[x**3 + y**3, y**3 + z**3, z**3 + x**3, x**2*y + x**2*z + y**2*z],
x**2 + y**2)
# compare local order
assert not contains([x*(1 + x + y), y*(1 + z)], x)
assert not contains([x*(1 + x + y), y*(1 + z)], x + y)
def test_local():
igrlex = InverseOrder(grlex)
gens = [x, y, z]
def contains(I, f):
S = [sdm_from_vector([g], igrlex, QQ, gens=gens) for g in I]
G = sdm_groebner(S, sdm_nf_mora, igrlex, QQ)
return sdm_nf_mora(sdm_from_vector([f], lex, QQ, gens=gens),
G, lex, QQ) == sdm_zero()
assert contains([x, y], x)
assert contains([x, y], x + y)
assert not contains([x, y], 1)
assert not contains([x, y], z)
assert contains([x**2 + y, x**2 + x], x - y)
assert not contains([x + y + z, x*y + x*z + y*z, x*y*z], x**2)
assert contains([x*(1 + x + y), y*(1 + z)], x)
assert contains([x*(1 + x + y), y*(1 + z)], x + y)
def test_uncovered_line():
gens = [x, y]
f1 = sdm_zero()
f2 = sdm_from_vector([x, 0], lex, QQ, gens=gens)
f3 = sdm_from_vector([0, y], lex, QQ, gens=gens)
assert sdm_spoly(f1, f2, lex, QQ) == sdm_zero()
assert sdm_spoly(f3, f2, lex, QQ) == sdm_zero()
def test_chain_criterion():
gens = [x]
f1 = sdm_from_vector([1, x], grlex, QQ, gens=gens)
f2 = sdm_from_vector([0, x - 2], grlex, QQ, gens=gens)
assert len(sdm_groebner([f1, f2], sdm_nf_mora, grlex, QQ)) == 2

View File

@ -0,0 +1,712 @@
"""Tests for Euclidean algorithms, GCDs, LCMs and polynomial remainder sequences. """
from sympy.polys.rings import ring
from sympy.polys.domains import ZZ, QQ, RR
from sympy.polys.specialpolys import (
f_polys,
dmp_fateman_poly_F_1,
dmp_fateman_poly_F_2,
dmp_fateman_poly_F_3)
f_0, f_1, f_2, f_3, f_4, f_5, f_6 = f_polys()
def test_dup_gcdex():
R, x = ring("x", QQ)
f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15
g = x**3 + x**2 - 4*x - 4
s = -QQ(1,5)*x + QQ(3,5)
t = QQ(1,5)*x**2 - QQ(6,5)*x + 2
h = x + 1
assert R.dup_half_gcdex(f, g) == (s, h)
assert R.dup_gcdex(f, g) == (s, t, h)
f = x**4 + 4*x**3 - x + 1
g = x**3 - x + 1
s, t, h = R.dup_gcdex(f, g)
S, T, H = R.dup_gcdex(g, f)
assert R.dup_add(R.dup_mul(s, f),
R.dup_mul(t, g)) == h
assert R.dup_add(R.dup_mul(S, g),
R.dup_mul(T, f)) == H
f = 2*x
g = x**2 - 16
s = QQ(1,32)*x
t = -QQ(1,16)
h = 1
assert R.dup_half_gcdex(f, g) == (s, h)
assert R.dup_gcdex(f, g) == (s, t, h)
def test_dup_invert():
R, x = ring("x", QQ)
assert R.dup_invert(2*x, x**2 - 16) == QQ(1,32)*x
def test_dup_euclidean_prs():
R, x = ring("x", QQ)
f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
assert R.dup_euclidean_prs(f, g) == [
f,
g,
-QQ(5,9)*x**4 + QQ(1,9)*x**2 - QQ(1,3),
-QQ(117,25)*x**2 - 9*x + QQ(441,25),
QQ(233150,19773)*x - QQ(102500,6591),
-QQ(1288744821,543589225)]
def test_dup_primitive_prs():
R, x = ring("x", ZZ)
f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
assert R.dup_primitive_prs(f, g) == [
f,
g,
-5*x**4 + x**2 - 3,
13*x**2 + 25*x - 49,
4663*x - 6150,
1]
def test_dup_subresultants():
R, x = ring("x", ZZ)
assert R.dup_resultant(0, 0) == 0
assert R.dup_resultant(1, 0) == 0
assert R.dup_resultant(0, 1) == 0
f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
a = 15*x**4 - 3*x**2 + 9
b = 65*x**2 + 125*x - 245
c = 9326*x - 12300
d = 260708
assert R.dup_subresultants(f, g) == [f, g, a, b, c, d]
assert R.dup_resultant(f, g) == R.dup_LC(d)
f = x**2 - 2*x + 1
g = x**2 - 1
a = 2*x - 2
assert R.dup_subresultants(f, g) == [f, g, a]
assert R.dup_resultant(f, g) == 0
f = x**2 + 1
g = x**2 - 1
a = -2
assert R.dup_subresultants(f, g) == [f, g, a]
assert R.dup_resultant(f, g) == 4
f = x**2 - 1
g = x**3 - x**2 + 2
assert R.dup_resultant(f, g) == 0
f = 3*x**3 - x
g = 5*x**2 + 1
assert R.dup_resultant(f, g) == 64
f = x**2 - 2*x + 7
g = x**3 - x + 5
assert R.dup_resultant(f, g) == 265
f = x**3 - 6*x**2 + 11*x - 6
g = x**3 - 15*x**2 + 74*x - 120
assert R.dup_resultant(f, g) == -8640
f = x**3 - 6*x**2 + 11*x - 6
g = x**3 - 10*x**2 + 29*x - 20
assert R.dup_resultant(f, g) == 0
f = x**3 - 1
g = x**3 + 2*x**2 + 2*x - 1
assert R.dup_resultant(f, g) == 16
f = x**8 - 2
g = x - 1
assert R.dup_resultant(f, g) == -1
def test_dmp_subresultants():
R, x, y = ring("x,y", ZZ)
assert R.dmp_resultant(0, 0) == 0
assert R.dmp_prs_resultant(0, 0)[0] == 0
assert R.dmp_zz_collins_resultant(0, 0) == 0
assert R.dmp_qq_collins_resultant(0, 0) == 0
assert R.dmp_resultant(1, 0) == 0
assert R.dmp_resultant(1, 0) == 0
assert R.dmp_resultant(1, 0) == 0
assert R.dmp_resultant(0, 1) == 0
assert R.dmp_prs_resultant(0, 1)[0] == 0
assert R.dmp_zz_collins_resultant(0, 1) == 0
assert R.dmp_qq_collins_resultant(0, 1) == 0
f = 3*x**2*y - y**3 - 4
g = x**2 + x*y**3 - 9
a = 3*x*y**4 + y**3 - 27*y + 4
b = -3*y**10 - 12*y**7 + y**6 - 54*y**4 + 8*y**3 + 729*y**2 - 216*y + 16
r = R.dmp_LC(b)
assert R.dmp_subresultants(f, g) == [f, g, a, b]
assert R.dmp_resultant(f, g) == r
assert R.dmp_prs_resultant(f, g)[0] == r
assert R.dmp_zz_collins_resultant(f, g) == r
assert R.dmp_qq_collins_resultant(f, g) == r
f = -x**3 + 5
g = 3*x**2*y + x**2
a = 45*y**2 + 30*y + 5
b = 675*y**3 + 675*y**2 + 225*y + 25
r = R.dmp_LC(b)
assert R.dmp_subresultants(f, g) == [f, g, a]
assert R.dmp_resultant(f, g) == r
assert R.dmp_prs_resultant(f, g)[0] == r
assert R.dmp_zz_collins_resultant(f, g) == r
assert R.dmp_qq_collins_resultant(f, g) == r
R, x, y, z, u, v = ring("x,y,z,u,v", ZZ)
f = 6*x**2 - 3*x*y - 2*x*z + y*z
g = x**2 - x*u - x*v + u*v
r = y**2*z**2 - 3*y**2*z*u - 3*y**2*z*v + 9*y**2*u*v - 2*y*z**2*u \
- 2*y*z**2*v + 6*y*z*u**2 + 12*y*z*u*v + 6*y*z*v**2 - 18*y*u**2*v \
- 18*y*u*v**2 + 4*z**2*u*v - 12*z*u**2*v - 12*z*u*v**2 + 36*u**2*v**2
assert R.dmp_zz_collins_resultant(f, g) == r.drop(x)
R, x, y, z, u, v = ring("x,y,z,u,v", QQ)
f = x**2 - QQ(1,2)*x*y - QQ(1,3)*x*z + QQ(1,6)*y*z
g = x**2 - x*u - x*v + u*v
r = QQ(1,36)*y**2*z**2 - QQ(1,12)*y**2*z*u - QQ(1,12)*y**2*z*v + QQ(1,4)*y**2*u*v \
- QQ(1,18)*y*z**2*u - QQ(1,18)*y*z**2*v + QQ(1,6)*y*z*u**2 + QQ(1,3)*y*z*u*v \
+ QQ(1,6)*y*z*v**2 - QQ(1,2)*y*u**2*v - QQ(1,2)*y*u*v**2 + QQ(1,9)*z**2*u*v \
- QQ(1,3)*z*u**2*v - QQ(1,3)*z*u*v**2 + u**2*v**2
assert R.dmp_qq_collins_resultant(f, g) == r.drop(x)
Rt, t = ring("t", ZZ)
Rx, x = ring("x", Rt)
f = x**6 - 5*x**4 + 5*x**2 + 4
g = -6*t*x**5 + x**4 + 20*t*x**3 - 3*x**2 - 10*t*x + 6
assert Rx.dup_resultant(f, g) == 2930944*t**6 + 2198208*t**4 + 549552*t**2 + 45796
def test_dup_discriminant():
R, x = ring("x", ZZ)
assert R.dup_discriminant(0) == 0
assert R.dup_discriminant(x) == 1
assert R.dup_discriminant(x**3 + 3*x**2 + 9*x - 13) == -11664
assert R.dup_discriminant(5*x**5 + x**3 + 2) == 31252160
assert R.dup_discriminant(x**4 + 2*x**3 + 6*x**2 - 22*x + 13) == 0
assert R.dup_discriminant(12*x**7 + 15*x**4 + 30*x**3 + x**2 + 1) == -220289699947514112
def test_dmp_discriminant():
R, x = ring("x", ZZ)
assert R.dmp_discriminant(0) == 0
R, x, y = ring("x,y", ZZ)
assert R.dmp_discriminant(0) == 0
assert R.dmp_discriminant(y) == 0
assert R.dmp_discriminant(x**3 + 3*x**2 + 9*x - 13) == -11664
assert R.dmp_discriminant(5*x**5 + x**3 + 2) == 31252160
assert R.dmp_discriminant(x**4 + 2*x**3 + 6*x**2 - 22*x + 13) == 0
assert R.dmp_discriminant(12*x**7 + 15*x**4 + 30*x**3 + x**2 + 1) == -220289699947514112
assert R.dmp_discriminant(x**2*y + 2*y) == (-8*y**2).drop(x)
assert R.dmp_discriminant(x*y**2 + 2*x) == 1
R, x, y, z = ring("x,y,z", ZZ)
assert R.dmp_discriminant(x*y + z) == 1
R, x, y, z, u = ring("x,y,z,u", ZZ)
assert R.dmp_discriminant(x**2*y + x*z + u) == (-4*y*u + z**2).drop(x)
R, x, y, z, u, v = ring("x,y,z,u,v", ZZ)
assert R.dmp_discriminant(x**3*y + x**2*z + x*u + v) == \
(-27*y**2*v**2 + 18*y*z*u*v - 4*y*u**3 - 4*z**3*v + z**2*u**2).drop(x)
def test_dup_gcd():
R, x = ring("x", ZZ)
f, g = 0, 0
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (0, 0, 0)
f, g = 2, 0
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, 0)
f, g = -2, 0
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, -1, 0)
f, g = 0, -2
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 0, -1)
f, g = 0, 2*x + 4
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2*x + 4, 0, 1)
f, g = 2*x + 4, 0
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2*x + 4, 1, 0)
f, g = 2, 2
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, 1)
f, g = -2, 2
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, -1, 1)
f, g = 2, -2
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, -1)
f, g = -2, -2
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, -1, -1)
f, g = x**2 + 2*x + 1, 1
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 1)
f, g = x**2 + 2*x + 1, 2
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 2)
f, g = 2*x**2 + 4*x + 2, 2
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, x**2 + 2*x + 1, 1)
f, g = 2, 2*x**2 + 4*x + 2
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, x**2 + 2*x + 1)
f, g = 2*x**2 + 4*x + 2, x + 1
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (x + 1, 2*x + 2, 1)
f, g = x + 1, 2*x**2 + 4*x + 2
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (x + 1, 1, 2*x + 2)
f, g = x - 31, x
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (1, f, g)
f = x**4 + 8*x**3 + 21*x**2 + 22*x + 8
g = x**3 + 6*x**2 + 11*x + 6
h = x**2 + 3*x + 2
cff = x**2 + 5*x + 4
cfg = x + 3
assert R.dup_zz_heu_gcd(f, g) == (h, cff, cfg)
assert R.dup_rr_prs_gcd(f, g) == (h, cff, cfg)
f = x**4 - 4
g = x**4 + 4*x**2 + 4
h = x**2 + 2
cff = x**2 - 2
cfg = x**2 + 2
assert R.dup_zz_heu_gcd(f, g) == (h, cff, cfg)
assert R.dup_rr_prs_gcd(f, g) == (h, cff, cfg)
f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
h = 1
cff = f
cfg = g
assert R.dup_zz_heu_gcd(f, g) == (h, cff, cfg)
assert R.dup_rr_prs_gcd(f, g) == (h, cff, cfg)
R, x = ring("x", QQ)
f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
h = 1
cff = f
cfg = g
assert R.dup_qq_heu_gcd(f, g) == (h, cff, cfg)
assert R.dup_ff_prs_gcd(f, g) == (h, cff, cfg)
R, x = ring("x", ZZ)
f = - 352518131239247345597970242177235495263669787845475025293906825864749649589178600387510272*x**49 \
+ 46818041807522713962450042363465092040687472354933295397472942006618953623327997952*x**42 \
+ 378182690892293941192071663536490788434899030680411695933646320291525827756032*x**35 \
+ 112806468807371824947796775491032386836656074179286744191026149539708928*x**28 \
- 12278371209708240950316872681744825481125965781519138077173235712*x**21 \
+ 289127344604779611146960547954288113529690984687482920704*x**14 \
+ 19007977035740498977629742919480623972236450681*x**7 \
+ 311973482284542371301330321821976049
g = 365431878023781158602430064717380211405897160759702125019136*x**21 \
+ 197599133478719444145775798221171663643171734081650688*x**14 \
- 9504116979659010018253915765478924103928886144*x**7 \
- 311973482284542371301330321821976049
assert R.dup_zz_heu_gcd(f, R.dup_diff(f, 1))[0] == g
assert R.dup_rr_prs_gcd(f, R.dup_diff(f, 1))[0] == g
R, x = ring("x", QQ)
f = QQ(1,2)*x**2 + x + QQ(1,2)
g = QQ(1,2)*x + QQ(1,2)
h = x + 1
assert R.dup_qq_heu_gcd(f, g) == (h, g, QQ(1,2))
assert R.dup_ff_prs_gcd(f, g) == (h, g, QQ(1,2))
R, x = ring("x", ZZ)
f = 1317378933230047068160*x + 2945748836994210856960
g = 120352542776360960*x + 269116466014453760
h = 120352542776360960*x + 269116466014453760
cff = 10946
cfg = 1
assert R.dup_zz_heu_gcd(f, g) == (h, cff, cfg)
def test_dmp_gcd():
R, x, y = ring("x,y", ZZ)
f, g = 0, 0
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (0, 0, 0)
f, g = 2, 0
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, 0)
f, g = -2, 0
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, -1, 0)
f, g = 0, -2
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 0, -1)
f, g = 0, 2*x + 4
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2*x + 4, 0, 1)
f, g = 2*x + 4, 0
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2*x + 4, 1, 0)
f, g = 2, 2
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, 1)
f, g = -2, 2
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, -1, 1)
f, g = 2, -2
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, -1)
f, g = -2, -2
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, -1, -1)
f, g = x**2 + 2*x + 1, 1
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 1)
f, g = x**2 + 2*x + 1, 2
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 2)
f, g = 2*x**2 + 4*x + 2, 2
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, x**2 + 2*x + 1, 1)
f, g = 2, 2*x**2 + 4*x + 2
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, x**2 + 2*x + 1)
f, g = 2*x**2 + 4*x + 2, x + 1
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (x + 1, 2*x + 2, 1)
f, g = x + 1, 2*x**2 + 4*x + 2
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (x + 1, 1, 2*x + 2)
R, x, y, z, u = ring("x,y,z,u", ZZ)
f, g = u**2 + 2*u + 1, 2*u + 2
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (u + 1, u + 1, 2)
f, g = z**2*u**2 + 2*z**2*u + z**2 + z*u + z, u**2 + 2*u + 1
h, cff, cfg = u + 1, z**2*u + z**2 + z, u + 1
assert R.dmp_zz_heu_gcd(f, g) == (h, cff, cfg)
assert R.dmp_rr_prs_gcd(f, g) == (h, cff, cfg)
assert R.dmp_zz_heu_gcd(g, f) == (h, cfg, cff)
assert R.dmp_rr_prs_gcd(g, f) == (h, cfg, cff)
R, x, y, z = ring("x,y,z", ZZ)
f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(2, ZZ))
H, cff, cfg = R.dmp_zz_heu_gcd(f, g)
assert H == h and R.dmp_mul(H, cff) == f \
and R.dmp_mul(H, cfg) == g
H, cff, cfg = R.dmp_rr_prs_gcd(f, g)
assert H == h and R.dmp_mul(H, cff) == f \
and R.dmp_mul(H, cfg) == g
R, x, y, z, u, v = ring("x,y,z,u,v", ZZ)
f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(4, ZZ))
H, cff, cfg = R.dmp_zz_heu_gcd(f, g)
assert H == h and R.dmp_mul(H, cff) == f \
and R.dmp_mul(H, cfg) == g
R, x, y, z, u, v, a, b = ring("x,y,z,u,v,a,b", ZZ)
f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(6, ZZ))
H, cff, cfg = R.dmp_zz_heu_gcd(f, g)
assert H == h and R.dmp_mul(H, cff) == f \
and R.dmp_mul(H, cfg) == g
R, x, y, z, u, v, a, b, c, d = ring("x,y,z,u,v,a,b,c,d", ZZ)
f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(8, ZZ))
H, cff, cfg = R.dmp_zz_heu_gcd(f, g)
assert H == h and R.dmp_mul(H, cff) == f \
and R.dmp_mul(H, cfg) == g
R, x, y, z = ring("x,y,z", ZZ)
f, g, h = map(R.from_dense, dmp_fateman_poly_F_2(2, ZZ))
H, cff, cfg = R.dmp_zz_heu_gcd(f, g)
assert H == h and R.dmp_mul(H, cff) == f \
and R.dmp_mul(H, cfg) == g
H, cff, cfg = R.dmp_rr_prs_gcd(f, g)
assert H == h and R.dmp_mul(H, cff) == f \
and R.dmp_mul(H, cfg) == g
f, g, h = map(R.from_dense, dmp_fateman_poly_F_3(2, ZZ))
H, cff, cfg = R.dmp_zz_heu_gcd(f, g)
assert H == h and R.dmp_mul(H, cff) == f \
and R.dmp_mul(H, cfg) == g
H, cff, cfg = R.dmp_rr_prs_gcd(f, g)
assert H == h and R.dmp_mul(H, cff) == f \
and R.dmp_mul(H, cfg) == g
R, x, y, z, u, v = ring("x,y,z,u,v", ZZ)
f, g, h = map(R.from_dense, dmp_fateman_poly_F_3(4, ZZ))
H, cff, cfg = R.dmp_inner_gcd(f, g)
assert H == h and R.dmp_mul(H, cff) == f \
and R.dmp_mul(H, cfg) == g
R, x, y = ring("x,y", QQ)
f = QQ(1,2)*x**2 + x + QQ(1,2)
g = QQ(1,2)*x + QQ(1,2)
h = x + 1
assert R.dmp_qq_heu_gcd(f, g) == (h, g, QQ(1,2))
assert R.dmp_ff_prs_gcd(f, g) == (h, g, QQ(1,2))
R, x, y = ring("x,y", RR)
f = 2.1*x*y**2 - 2.2*x*y + 2.1*x
g = 1.0*x**3
assert R.dmp_ff_prs_gcd(f, g) == \
(1.0*x, 2.1*y**2 - 2.2*y + 2.1, 1.0*x**2)
def test_dup_lcm():
R, x = ring("x", ZZ)
assert R.dup_lcm(2, 6) == 6
assert R.dup_lcm(2*x**3, 6*x) == 6*x**3
assert R.dup_lcm(2*x**3, 3*x) == 6*x**3
assert R.dup_lcm(x**2 + x, x) == x**2 + x
assert R.dup_lcm(x**2 + x, 2*x) == 2*x**2 + 2*x
assert R.dup_lcm(x**2 + 2*x, x) == x**2 + 2*x
assert R.dup_lcm(2*x**2 + x, x) == 2*x**2 + x
assert R.dup_lcm(2*x**2 + x, 2*x) == 4*x**2 + 2*x
def test_dmp_lcm():
R, x, y = ring("x,y", ZZ)
assert R.dmp_lcm(2, 6) == 6
assert R.dmp_lcm(x, y) == x*y
assert R.dmp_lcm(2*x**3, 6*x*y**2) == 6*x**3*y**2
assert R.dmp_lcm(2*x**3, 3*x*y**2) == 6*x**3*y**2
assert R.dmp_lcm(x**2*y, x*y**2) == x**2*y**2
f = 2*x*y**5 - 3*x*y**4 - 2*x*y**3 + 3*x*y**2
g = y**5 - 2*y**3 + y
h = 2*x*y**7 - 3*x*y**6 - 4*x*y**5 + 6*x*y**4 + 2*x*y**3 - 3*x*y**2
assert R.dmp_lcm(f, g) == h
f = x**3 - 3*x**2*y - 9*x*y**2 - 5*y**3
g = x**4 + 6*x**3*y + 12*x**2*y**2 + 10*x*y**3 + 3*y**4
h = x**5 + x**4*y - 18*x**3*y**2 - 50*x**2*y**3 - 47*x*y**4 - 15*y**5
assert R.dmp_lcm(f, g) == h
def test_dmp_content():
R, x,y = ring("x,y", ZZ)
assert R.dmp_content(-2) == 2
f, g, F = 3*y**2 + 2*y + 1, 1, 0
for i in range(0, 5):
g *= f
F += x**i*g
assert R.dmp_content(F) == f.drop(x)
R, x,y,z = ring("x,y,z", ZZ)
assert R.dmp_content(f_4) == 1
assert R.dmp_content(f_5) == 1
R, x,y,z,t = ring("x,y,z,t", ZZ)
assert R.dmp_content(f_6) == 1
def test_dmp_primitive():
R, x,y = ring("x,y", ZZ)
assert R.dmp_primitive(0) == (0, 0)
assert R.dmp_primitive(1) == (1, 1)
f, g, F = 3*y**2 + 2*y + 1, 1, 0
for i in range(0, 5):
g *= f
F += x**i*g
assert R.dmp_primitive(F) == (f.drop(x), F / f)
R, x,y,z = ring("x,y,z", ZZ)
cont, f = R.dmp_primitive(f_4)
assert cont == 1 and f == f_4
cont, f = R.dmp_primitive(f_5)
assert cont == 1 and f == f_5
R, x,y,z,t = ring("x,y,z,t", ZZ)
cont, f = R.dmp_primitive(f_6)
assert cont == 1 and f == f_6
def test_dup_cancel():
R, x = ring("x", ZZ)
f = 2*x**2 - 2
g = x**2 - 2*x + 1
p = 2*x + 2
q = x - 1
assert R.dup_cancel(f, g) == (p, q)
assert R.dup_cancel(f, g, include=False) == (1, 1, p, q)
f = -x - 2
g = 3*x - 4
F = x + 2
G = -3*x + 4
assert R.dup_cancel(f, g) == (f, g)
assert R.dup_cancel(F, G) == (f, g)
assert R.dup_cancel(0, 0) == (0, 0)
assert R.dup_cancel(0, 0, include=False) == (1, 1, 0, 0)
assert R.dup_cancel(x, 0) == (1, 0)
assert R.dup_cancel(x, 0, include=False) == (1, 1, 1, 0)
assert R.dup_cancel(0, x) == (0, 1)
assert R.dup_cancel(0, x, include=False) == (1, 1, 0, 1)
f = 0
g = x
one = 1
assert R.dup_cancel(f, g, include=True) == (f, one)
def test_dmp_cancel():
R, x, y = ring("x,y", ZZ)
f = 2*x**2 - 2
g = x**2 - 2*x + 1
p = 2*x + 2
q = x - 1
assert R.dmp_cancel(f, g) == (p, q)
assert R.dmp_cancel(f, g, include=False) == (1, 1, p, q)
assert R.dmp_cancel(0, 0) == (0, 0)
assert R.dmp_cancel(0, 0, include=False) == (1, 1, 0, 0)
assert R.dmp_cancel(y, 0) == (1, 0)
assert R.dmp_cancel(y, 0, include=False) == (1, 1, 1, 0)
assert R.dmp_cancel(0, y) == (0, 1)
assert R.dmp_cancel(0, y, include=False) == (1, 1, 0, 1)

View File

@ -0,0 +1,784 @@
"""Tools for polynomial factorization routines in characteristic zero. """
from sympy.polys.rings import ring, xring
from sympy.polys.domains import FF, ZZ, QQ, ZZ_I, QQ_I, RR, EX
from sympy.polys import polyconfig as config
from sympy.polys.polyerrors import DomainError
from sympy.polys.polyclasses import ANP
from sympy.polys.specialpolys import f_polys, w_polys
from sympy.core.numbers import I
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.functions.elementary.trigonometric import sin
from sympy.ntheory.generate import nextprime
from sympy.testing.pytest import raises, XFAIL
f_0, f_1, f_2, f_3, f_4, f_5, f_6 = f_polys()
w_1, w_2 = w_polys()
def test_dup_trial_division():
R, x = ring("x", ZZ)
assert R.dup_trial_division(x**5 + 8*x**4 + 25*x**3 + 38*x**2 + 28*x + 8, (x + 1, x + 2)) == [(x + 1, 2), (x + 2, 3)]
def test_dmp_trial_division():
R, x, y = ring("x,y", ZZ)
assert R.dmp_trial_division(x**5 + 8*x**4 + 25*x**3 + 38*x**2 + 28*x + 8, (x + 1, x + 2)) == [(x + 1, 2), (x + 2, 3)]
def test_dup_zz_mignotte_bound():
R, x = ring("x", ZZ)
assert R.dup_zz_mignotte_bound(2*x**2 + 3*x + 4) == 6
assert R.dup_zz_mignotte_bound(x**3 + 14*x**2 + 56*x + 64) == 152
def test_dmp_zz_mignotte_bound():
R, x, y = ring("x,y", ZZ)
assert R.dmp_zz_mignotte_bound(2*x**2 + 3*x + 4) == 32
def test_dup_zz_hensel_step():
R, x = ring("x", ZZ)
f = x**4 - 1
g = x**3 + 2*x**2 - x - 2
h = x - 2
s = -2
t = 2*x**2 - 2*x - 1
G, H, S, T = R.dup_zz_hensel_step(5, f, g, h, s, t)
assert G == x**3 + 7*x**2 - x - 7
assert H == x - 7
assert S == 8
assert T == -8*x**2 - 12*x - 1
def test_dup_zz_hensel_lift():
R, x = ring("x", ZZ)
f = x**4 - 1
F = [x - 1, x - 2, x + 2, x + 1]
assert R.dup_zz_hensel_lift(ZZ(5), f, F, 4) == \
[x - 1, x - 182, x + 182, x + 1]
def test_dup_zz_irreducible_p():
R, x = ring("x", ZZ)
assert R.dup_zz_irreducible_p(3*x**4 + 2*x**3 + 6*x**2 + 8*x + 7) is None
assert R.dup_zz_irreducible_p(3*x**4 + 2*x**3 + 6*x**2 + 8*x + 4) is None
assert R.dup_zz_irreducible_p(3*x**4 + 2*x**3 + 6*x**2 + 8*x + 10) is True
assert R.dup_zz_irreducible_p(3*x**4 + 2*x**3 + 6*x**2 + 8*x + 14) is True
def test_dup_cyclotomic_p():
R, x = ring("x", ZZ)
assert R.dup_cyclotomic_p(x - 1) is True
assert R.dup_cyclotomic_p(x + 1) is True
assert R.dup_cyclotomic_p(x**2 + x + 1) is True
assert R.dup_cyclotomic_p(x**2 + 1) is True
assert R.dup_cyclotomic_p(x**4 + x**3 + x**2 + x + 1) is True
assert R.dup_cyclotomic_p(x**2 - x + 1) is True
assert R.dup_cyclotomic_p(x**6 + x**5 + x**4 + x**3 + x**2 + x + 1) is True
assert R.dup_cyclotomic_p(x**4 + 1) is True
assert R.dup_cyclotomic_p(x**6 + x**3 + 1) is True
assert R.dup_cyclotomic_p(0) is False
assert R.dup_cyclotomic_p(1) is False
assert R.dup_cyclotomic_p(x) is False
assert R.dup_cyclotomic_p(x + 2) is False
assert R.dup_cyclotomic_p(3*x + 1) is False
assert R.dup_cyclotomic_p(x**2 - 1) is False
f = x**16 + x**14 - x**10 + x**8 - x**6 + x**2 + 1
assert R.dup_cyclotomic_p(f) is False
g = x**16 + x**14 - x**10 - x**8 - x**6 + x**2 + 1
assert R.dup_cyclotomic_p(g) is True
R, x = ring("x", QQ)
assert R.dup_cyclotomic_p(x**2 + x + 1) is True
assert R.dup_cyclotomic_p(QQ(1,2)*x**2 + x + 1) is False
R, x = ring("x", ZZ["y"])
assert R.dup_cyclotomic_p(x**2 + x + 1) is False
def test_dup_zz_cyclotomic_poly():
R, x = ring("x", ZZ)
assert R.dup_zz_cyclotomic_poly(1) == x - 1
assert R.dup_zz_cyclotomic_poly(2) == x + 1
assert R.dup_zz_cyclotomic_poly(3) == x**2 + x + 1
assert R.dup_zz_cyclotomic_poly(4) == x**2 + 1
assert R.dup_zz_cyclotomic_poly(5) == x**4 + x**3 + x**2 + x + 1
assert R.dup_zz_cyclotomic_poly(6) == x**2 - x + 1
assert R.dup_zz_cyclotomic_poly(7) == x**6 + x**5 + x**4 + x**3 + x**2 + x + 1
assert R.dup_zz_cyclotomic_poly(8) == x**4 + 1
assert R.dup_zz_cyclotomic_poly(9) == x**6 + x**3 + 1
def test_dup_zz_cyclotomic_factor():
R, x = ring("x", ZZ)
assert R.dup_zz_cyclotomic_factor(0) is None
assert R.dup_zz_cyclotomic_factor(1) is None
assert R.dup_zz_cyclotomic_factor(2*x**10 - 1) is None
assert R.dup_zz_cyclotomic_factor(x**10 - 3) is None
assert R.dup_zz_cyclotomic_factor(x**10 + x**5 - 1) is None
assert R.dup_zz_cyclotomic_factor(x + 1) == [x + 1]
assert R.dup_zz_cyclotomic_factor(x - 1) == [x - 1]
assert R.dup_zz_cyclotomic_factor(x**2 + 1) == [x**2 + 1]
assert R.dup_zz_cyclotomic_factor(x**2 - 1) == [x - 1, x + 1]
assert R.dup_zz_cyclotomic_factor(x**27 + 1) == \
[x + 1, x**2 - x + 1, x**6 - x**3 + 1, x**18 - x**9 + 1]
assert R.dup_zz_cyclotomic_factor(x**27 - 1) == \
[x - 1, x**2 + x + 1, x**6 + x**3 + 1, x**18 + x**9 + 1]
def test_dup_zz_factor():
R, x = ring("x", ZZ)
assert R.dup_zz_factor(0) == (0, [])
assert R.dup_zz_factor(7) == (7, [])
assert R.dup_zz_factor(-7) == (-7, [])
assert R.dup_zz_factor_sqf(0) == (0, [])
assert R.dup_zz_factor_sqf(7) == (7, [])
assert R.dup_zz_factor_sqf(-7) == (-7, [])
assert R.dup_zz_factor(2*x + 4) == (2, [(x + 2, 1)])
assert R.dup_zz_factor_sqf(2*x + 4) == (2, [x + 2])
f = x**4 + x + 1
for i in range(0, 20):
assert R.dup_zz_factor(f) == (1, [(f, 1)])
assert R.dup_zz_factor(x**2 + 2*x + 2) == \
(1, [(x**2 + 2*x + 2, 1)])
assert R.dup_zz_factor(18*x**2 + 12*x + 2) == \
(2, [(3*x + 1, 2)])
assert R.dup_zz_factor(-9*x**2 + 1) == \
(-1, [(3*x - 1, 1),
(3*x + 1, 1)])
assert R.dup_zz_factor_sqf(-9*x**2 + 1) == \
(-1, [3*x - 1,
3*x + 1])
# The order of the factors will be different when the ground types are
# flint. At the higher level dup_factor_list will sort the factors.
c, factors = R.dup_zz_factor(x**3 - 6*x**2 + 11*x - 6)
assert c == 1
assert set(factors) == {(x - 3, 1), (x - 2, 1), (x - 1, 1)}
assert R.dup_zz_factor_sqf(x**3 - 6*x**2 + 11*x - 6) == \
(1, [x - 3,
x - 2,
x - 1])
assert R.dup_zz_factor(3*x**3 + 10*x**2 + 13*x + 10) == \
(1, [(x + 2, 1),
(3*x**2 + 4*x + 5, 1)])
assert R.dup_zz_factor_sqf(3*x**3 + 10*x**2 + 13*x + 10) == \
(1, [x + 2,
3*x**2 + 4*x + 5])
c, factors = R.dup_zz_factor(-x**6 + x**2)
assert c == -1
assert set(factors) == {(x, 2), (x - 1, 1), (x + 1, 1), (x**2 + 1, 1)}
f = 1080*x**8 + 5184*x**7 + 2099*x**6 + 744*x**5 + 2736*x**4 - 648*x**3 + 129*x**2 - 324
assert R.dup_zz_factor(f) == \
(1, [(5*x**4 + 24*x**3 + 9*x**2 + 12, 1),
(216*x**4 + 31*x**2 - 27, 1)])
f = -29802322387695312500000000000000000000*x**25 \
+ 2980232238769531250000000000000000*x**20 \
+ 1743435859680175781250000000000*x**15 \
+ 114142894744873046875000000*x**10 \
- 210106372833251953125*x**5 \
+ 95367431640625
c, factors = R.dup_zz_factor(f)
assert c == -95367431640625
assert set(factors) == {
(5*x - 1, 1),
(100*x**2 + 10*x - 1, 2),
(625*x**4 + 125*x**3 + 25*x**2 + 5*x + 1, 1),
(10000*x**4 - 3000*x**3 + 400*x**2 - 20*x + 1, 2),
(10000*x**4 + 2000*x**3 + 400*x**2 + 30*x + 1, 2),
}
f = x**10 - 1
config.setup('USE_CYCLOTOMIC_FACTOR', True)
c0, F_0 = R.dup_zz_factor(f)
config.setup('USE_CYCLOTOMIC_FACTOR', False)
c1, F_1 = R.dup_zz_factor(f)
assert c0 == c1 == 1
assert set(F_0) == set(F_1) == {
(x - 1, 1),
(x + 1, 1),
(x**4 - x**3 + x**2 - x + 1, 1),
(x**4 + x**3 + x**2 + x + 1, 1),
}
config.setup('USE_CYCLOTOMIC_FACTOR')
f = x**10 + 1
config.setup('USE_CYCLOTOMIC_FACTOR', True)
F_0 = R.dup_zz_factor(f)
config.setup('USE_CYCLOTOMIC_FACTOR', False)
F_1 = R.dup_zz_factor(f)
assert F_0 == F_1 == \
(1, [(x**2 + 1, 1),
(x**8 - x**6 + x**4 - x**2 + 1, 1)])
config.setup('USE_CYCLOTOMIC_FACTOR')
def test_dmp_zz_wang():
R, x,y,z = ring("x,y,z", ZZ)
UV, _x = ring("x", ZZ)
p = ZZ(nextprime(R.dmp_zz_mignotte_bound(w_1)))
assert p == 6291469
t_1, k_1, e_1 = y, 1, ZZ(-14)
t_2, k_2, e_2 = z, 2, ZZ(3)
t_3, k_3, e_3 = y + z, 2, ZZ(-11)
t_4, k_4, e_4 = y - z, 1, ZZ(-17)
T = [t_1, t_2, t_3, t_4]
K = [k_1, k_2, k_3, k_4]
E = [e_1, e_2, e_3, e_4]
T = zip([ t.drop(x) for t in T ], K)
A = [ZZ(-14), ZZ(3)]
S = R.dmp_eval_tail(w_1, A)
cs, s = UV.dup_primitive(S)
assert cs == 1 and s == S == \
1036728*_x**6 + 915552*_x**5 + 55748*_x**4 + 105621*_x**3 - 17304*_x**2 - 26841*_x - 644
assert R.dmp_zz_wang_non_divisors(E, cs, ZZ(4)) == [7, 3, 11, 17]
assert UV.dup_sqf_p(s) and UV.dup_degree(s) == R.dmp_degree(w_1)
_, H = UV.dup_zz_factor_sqf(s)
h_1 = 44*_x**2 + 42*_x + 1
h_2 = 126*_x**2 - 9*_x + 28
h_3 = 187*_x**2 - 23
assert H == [h_1, h_2, h_3]
LC = [ lc.drop(x) for lc in [-4*y - 4*z, -y*z**2, y**2 - z**2] ]
assert R.dmp_zz_wang_lead_coeffs(w_1, T, cs, E, H, A) == (w_1, H, LC)
factors = R.dmp_zz_wang_hensel_lifting(w_1, H, LC, A, p)
assert R.dmp_expand(factors) == w_1
@XFAIL
def test_dmp_zz_wang_fail():
R, x,y,z = ring("x,y,z", ZZ)
UV, _x = ring("x", ZZ)
p = ZZ(nextprime(R.dmp_zz_mignotte_bound(w_1)))
assert p == 6291469
H_1 = [44*x**2 + 42*x + 1, 126*x**2 - 9*x + 28, 187*x**2 - 23]
H_2 = [-4*x**2*y - 12*x**2 - 3*x*y + 1, -9*x**2*y - 9*x - 2*y, x**2*y**2 - 9*x**2 + y - 9]
H_3 = [-4*x**2*y - 12*x**2 - 3*x*y + 1, -9*x**2*y - 9*x - 2*y, x**2*y**2 - 9*x**2 + y - 9]
c_1 = -70686*x**5 - 5863*x**4 - 17826*x**3 + 2009*x**2 + 5031*x + 74
c_2 = 9*x**5*y**4 + 12*x**5*y**3 - 45*x**5*y**2 - 108*x**5*y - 324*x**5 + 18*x**4*y**3 - 216*x**4*y**2 - 810*x**4*y + 2*x**3*y**4 + 9*x**3*y**3 - 252*x**3*y**2 - 288*x**3*y - 945*x**3 - 30*x**2*y**2 - 414*x**2*y + 2*x*y**3 - 54*x*y**2 - 3*x*y + 81*x + 12*y
c_3 = -36*x**4*y**2 - 108*x**4*y - 27*x**3*y**2 - 36*x**3*y - 108*x**3 - 8*x**2*y**2 - 42*x**2*y - 6*x*y**2 + 9*x + 2*y
assert R.dmp_zz_diophantine(H_1, c_1, [], 5, p) == [-3*x, -2, 1]
assert R.dmp_zz_diophantine(H_2, c_2, [ZZ(-14)], 5, p) == [-x*y, -3*x, -6]
assert R.dmp_zz_diophantine(H_3, c_3, [ZZ(-14)], 5, p) == [0, 0, -1]
def test_issue_6355():
# This tests a bug in the Wang algorithm that occurred only with a very
# specific set of random numbers.
random_sequence = [-1, -1, 0, 0, 0, 0, -1, -1, 0, -1, 3, -1, 3, 3, 3, 3, -1, 3]
R, x, y, z = ring("x,y,z", ZZ)
f = 2*x**2 + y*z - y - z**2 + z
assert R.dmp_zz_wang(f, seed=random_sequence) == [f]
def test_dmp_zz_factor():
R, x = ring("x", ZZ)
assert R.dmp_zz_factor(0) == (0, [])
assert R.dmp_zz_factor(7) == (7, [])
assert R.dmp_zz_factor(-7) == (-7, [])
assert R.dmp_zz_factor(x**2 - 9) == (1, [(x - 3, 1), (x + 3, 1)])
R, x, y = ring("x,y", ZZ)
assert R.dmp_zz_factor(0) == (0, [])
assert R.dmp_zz_factor(7) == (7, [])
assert R.dmp_zz_factor(-7) == (-7, [])
assert R.dmp_zz_factor(x) == (1, [(x, 1)])
assert R.dmp_zz_factor(4*x) == (4, [(x, 1)])
assert R.dmp_zz_factor(4*x + 2) == (2, [(2*x + 1, 1)])
assert R.dmp_zz_factor(x*y + 1) == (1, [(x*y + 1, 1)])
assert R.dmp_zz_factor(y**2 + 1) == (1, [(y**2 + 1, 1)])
assert R.dmp_zz_factor(y**2 - 1) == (1, [(y - 1, 1), (y + 1, 1)])
assert R.dmp_zz_factor(x**2*y**2 + 6*x**2*y + 9*x**2 - 1) == (1, [(x*y + 3*x - 1, 1), (x*y + 3*x + 1, 1)])
assert R.dmp_zz_factor(x**2*y**2 - 9) == (1, [(x*y - 3, 1), (x*y + 3, 1)])
R, x, y, z = ring("x,y,z", ZZ)
assert R.dmp_zz_factor(x**2*y**2*z**2 - 9) == \
(1, [(x*y*z - 3, 1),
(x*y*z + 3, 1)])
R, x, y, z, u = ring("x,y,z,u", ZZ)
assert R.dmp_zz_factor(x**2*y**2*z**2*u**2 - 9) == \
(1, [(x*y*z*u - 3, 1),
(x*y*z*u + 3, 1)])
R, x, y, z = ring("x,y,z", ZZ)
assert R.dmp_zz_factor(f_1) == \
(1, [(x + y*z + 20, 1),
(x*y + z + 10, 1),
(x*z + y + 30, 1)])
assert R.dmp_zz_factor(f_2) == \
(1, [(x**2*y**2 + x**2*z**2 + y + 90, 1),
(x**3*y + x**3*z + z - 11, 1)])
assert R.dmp_zz_factor(f_3) == \
(1, [(x**2*y**2 + x*z**4 + x + z, 1),
(x**3 + x*y*z + y**2 + y*z**3, 1)])
assert R.dmp_zz_factor(f_4) == \
(-1, [(x*y**3 + z**2, 1),
(x**2*z + y**4*z**2 + 5, 1),
(x**3*y - z**2 - 3, 1),
(x**3*y**4 + z**2, 1)])
assert R.dmp_zz_factor(f_5) == \
(-1, [(x + y - z, 3)])
R, x, y, z, t = ring("x,y,z,t", ZZ)
assert R.dmp_zz_factor(f_6) == \
(1, [(47*x*y + z**3*t**2 - t**2, 1),
(45*x**3 - 9*y**3 - y**2 + 3*z**3 + 2*z*t, 1)])
R, x, y, z = ring("x,y,z", ZZ)
assert R.dmp_zz_factor(w_1) == \
(1, [(x**2*y**2 - x**2*z**2 + y - z**2, 1),
(x**2*y*z**2 + 3*x*z + 2*y, 1),
(4*x**2*y + 4*x**2*z + x*y*z - 1, 1)])
R, x, y = ring("x,y", ZZ)
f = -12*x**16*y + 240*x**12*y**3 - 768*x**10*y**4 + 1080*x**8*y**5 - 768*x**6*y**6 + 240*x**4*y**7 - 12*y**9
assert R.dmp_zz_factor(f) == \
(-12, [(y, 1),
(x**2 - y, 6),
(x**4 + 6*x**2*y + y**2, 1)])
def test_dup_qq_i_factor():
R, x = ring("x", QQ_I)
i = QQ_I(0, 1)
assert R.dup_qq_i_factor(x**2 - 2) == (QQ_I(1, 0), [(x**2 - 2, 1)])
assert R.dup_qq_i_factor(x**2 - 1) == (QQ_I(1, 0), [(x - 1, 1), (x + 1, 1)])
assert R.dup_qq_i_factor(x**2 + 1) == (QQ_I(1, 0), [(x - i, 1), (x + i, 1)])
assert R.dup_qq_i_factor(x**2/4 + 1) == \
(QQ_I(QQ(1, 4), 0), [(x - 2*i, 1), (x + 2*i, 1)])
assert R.dup_qq_i_factor(x**2 + 4) == \
(QQ_I(1, 0), [(x - 2*i, 1), (x + 2*i, 1)])
assert R.dup_qq_i_factor(x**2 + 2*x + 1) == \
(QQ_I(1, 0), [(x + 1, 2)])
assert R.dup_qq_i_factor(x**2 + 2*i*x - 1) == \
(QQ_I(1, 0), [(x + i, 2)])
f = 8192*x**2 + x*(22656 + 175232*i) - 921416 + 242313*i
assert R.dup_qq_i_factor(f) == \
(QQ_I(8192, 0), [(x + QQ_I(QQ(177, 128), QQ(1369, 128)), 2)])
def test_dmp_qq_i_factor():
R, x, y = ring("x, y", QQ_I)
i = QQ_I(0, 1)
assert R.dmp_qq_i_factor(x**2 + 2*y**2) == \
(QQ_I(1, 0), [(x**2 + 2*y**2, 1)])
assert R.dmp_qq_i_factor(x**2 + y**2) == \
(QQ_I(1, 0), [(x - i*y, 1), (x + i*y, 1)])
assert R.dmp_qq_i_factor(x**2 + y**2/4) == \
(QQ_I(1, 0), [(x - i*y/2, 1), (x + i*y/2, 1)])
assert R.dmp_qq_i_factor(4*x**2 + y**2) == \
(QQ_I(4, 0), [(x - i*y/2, 1), (x + i*y/2, 1)])
def test_dup_zz_i_factor():
R, x = ring("x", ZZ_I)
i = ZZ_I(0, 1)
assert R.dup_zz_i_factor(x**2 - 2) == (ZZ_I(1, 0), [(x**2 - 2, 1)])
assert R.dup_zz_i_factor(x**2 - 1) == (ZZ_I(1, 0), [(x - 1, 1), (x + 1, 1)])
assert R.dup_zz_i_factor(x**2 + 1) == (ZZ_I(1, 0), [(x - i, 1), (x + i, 1)])
assert R.dup_zz_i_factor(x**2 + 4) == \
(ZZ_I(1, 0), [(x - 2*i, 1), (x + 2*i, 1)])
assert R.dup_zz_i_factor(x**2 + 2*x + 1) == \
(ZZ_I(1, 0), [(x + 1, 2)])
assert R.dup_zz_i_factor(x**2 + 2*i*x - 1) == \
(ZZ_I(1, 0), [(x + i, 2)])
f = 8192*x**2 + x*(22656 + 175232*i) - 921416 + 242313*i
assert R.dup_zz_i_factor(f) == \
(ZZ_I(0, 1), [((64 - 64*i)*x + (773 + 596*i), 2)])
def test_dmp_zz_i_factor():
R, x, y = ring("x, y", ZZ_I)
i = ZZ_I(0, 1)
assert R.dmp_zz_i_factor(x**2 + 2*y**2) == \
(ZZ_I(1, 0), [(x**2 + 2*y**2, 1)])
assert R.dmp_zz_i_factor(x**2 + y**2) == \
(ZZ_I(1, 0), [(x - i*y, 1), (x + i*y, 1)])
assert R.dmp_zz_i_factor(4*x**2 + y**2) == \
(ZZ_I(1, 0), [(2*x - i*y, 1), (2*x + i*y, 1)])
def test_dup_ext_factor():
R, x = ring("x", QQ.algebraic_field(I))
def anp(element):
return ANP(element, [QQ(1), QQ(0), QQ(1)], QQ)
assert R.dup_ext_factor(0) == (anp([]), [])
f = anp([QQ(1)])*x + anp([QQ(1)])
assert R.dup_ext_factor(f) == (anp([QQ(1)]), [(f, 1)])
g = anp([QQ(2)])*x + anp([QQ(2)])
assert R.dup_ext_factor(g) == (anp([QQ(2)]), [(f, 1)])
f = anp([QQ(7)])*x**4 + anp([QQ(1, 1)])
g = anp([QQ(1)])*x**4 + anp([QQ(1, 7)])
assert R.dup_ext_factor(f) == (anp([QQ(7)]), [(g, 1)])
f = anp([QQ(1)])*x**4 + anp([QQ(1)])
assert R.dup_ext_factor(f) == \
(anp([QQ(1, 1)]), [(anp([QQ(1)])*x**2 + anp([QQ(-1), QQ(0)]), 1),
(anp([QQ(1)])*x**2 + anp([QQ( 1), QQ(0)]), 1)])
f = anp([QQ(4, 1)])*x**2 + anp([QQ(9, 1)])
assert R.dup_ext_factor(f) == \
(anp([QQ(4, 1)]), [(anp([QQ(1, 1)])*x + anp([-QQ(3, 2), QQ(0, 1)]), 1),
(anp([QQ(1, 1)])*x + anp([ QQ(3, 2), QQ(0, 1)]), 1)])
f = anp([QQ(4, 1)])*x**4 + anp([QQ(8, 1)])*x**3 + anp([QQ(77, 1)])*x**2 + anp([QQ(18, 1)])*x + anp([QQ(153, 1)])
assert R.dup_ext_factor(f) == \
(anp([QQ(4, 1)]), [(anp([QQ(1, 1)])*x + anp([-QQ(4, 1), QQ(1, 1)]), 1),
(anp([QQ(1, 1)])*x + anp([-QQ(3, 2), QQ(0, 1)]), 1),
(anp([QQ(1, 1)])*x + anp([ QQ(3, 2), QQ(0, 1)]), 1),
(anp([QQ(1, 1)])*x + anp([ QQ(4, 1), QQ(1, 1)]), 1)])
R, x = ring("x", QQ.algebraic_field(sqrt(2)))
def anp(element):
return ANP(element, [QQ(1), QQ(0), QQ(-2)], QQ)
f = anp([QQ(1)])*x**4 + anp([QQ(1, 1)])
assert R.dup_ext_factor(f) == \
(anp([QQ(1)]), [(anp([QQ(1)])*x**2 + anp([QQ(-1), QQ(0)])*x + anp([QQ(1)]), 1),
(anp([QQ(1)])*x**2 + anp([QQ( 1), QQ(0)])*x + anp([QQ(1)]), 1)])
f = anp([QQ(1, 1)])*x**2 + anp([QQ(2), QQ(0)])*x + anp([QQ(2, 1)])
assert R.dup_ext_factor(f) == \
(anp([QQ(1, 1)]), [(anp([1])*x + anp([1, 0]), 2)])
assert R.dup_ext_factor(f**3) == \
(anp([QQ(1, 1)]), [(anp([1])*x + anp([1, 0]), 6)])
f *= anp([QQ(2, 1)])
assert R.dup_ext_factor(f) == \
(anp([QQ(2, 1)]), [(anp([1])*x + anp([1, 0]), 2)])
assert R.dup_ext_factor(f**3) == \
(anp([QQ(8, 1)]), [(anp([1])*x + anp([1, 0]), 6)])
def test_dmp_ext_factor():
K = QQ.algebraic_field(sqrt(2))
R, x,y = ring("x,y", K)
sqrt2 = K.unit
def anp(x):
return ANP(x, [QQ(1), QQ(0), QQ(-2)], QQ)
assert R.dmp_ext_factor(0) == (anp([]), [])
f = anp([QQ(1)])*x + anp([QQ(1)])
assert R.dmp_ext_factor(f) == (anp([QQ(1)]), [(f, 1)])
g = anp([QQ(2)])*x + anp([QQ(2)])
assert R.dmp_ext_factor(g) == (anp([QQ(2)]), [(f, 1)])
f = anp([QQ(1)])*x**2 + anp([QQ(-2)])*y**2
assert R.dmp_ext_factor(f) == \
(anp([QQ(1)]), [(anp([QQ(1)])*x + anp([QQ(-1), QQ(0)])*y, 1),
(anp([QQ(1)])*x + anp([QQ( 1), QQ(0)])*y, 1)])
f = anp([QQ(2)])*x**2 + anp([QQ(-4)])*y**2
assert R.dmp_ext_factor(f) == \
(anp([QQ(2)]), [(anp([QQ(1)])*x + anp([QQ(-1), QQ(0)])*y, 1),
(anp([QQ(1)])*x + anp([QQ( 1), QQ(0)])*y, 1)])
f1 = y + 1
f2 = y + sqrt2
f3 = x**2 + x + 2 + 3*sqrt2
f = f1**2 * f2**2 * f3**2
assert R.dmp_ext_factor(f) == (K.one, [(f1, 2), (f2, 2), (f3, 2)])
def test_dup_factor_list():
R, x = ring("x", ZZ)
assert R.dup_factor_list(0) == (0, [])
assert R.dup_factor_list(7) == (7, [])
R, x = ring("x", QQ)
assert R.dup_factor_list(0) == (0, [])
assert R.dup_factor_list(QQ(1, 7)) == (QQ(1, 7), [])
R, x = ring("x", ZZ['t'])
assert R.dup_factor_list(0) == (0, [])
assert R.dup_factor_list(7) == (7, [])
R, x = ring("x", QQ['t'])
assert R.dup_factor_list(0) == (0, [])
assert R.dup_factor_list(QQ(1, 7)) == (QQ(1, 7), [])
R, x = ring("x", ZZ)
assert R.dup_factor_list_include(0) == [(0, 1)]
assert R.dup_factor_list_include(7) == [(7, 1)]
assert R.dup_factor_list(x**2 + 2*x + 1) == (1, [(x + 1, 2)])
assert R.dup_factor_list_include(x**2 + 2*x + 1) == [(x + 1, 2)]
# issue 8037
assert R.dup_factor_list(6*x**2 - 5*x - 6) == (1, [(2*x - 3, 1), (3*x + 2, 1)])
R, x = ring("x", QQ)
assert R.dup_factor_list(QQ(1,2)*x**2 + x + QQ(1,2)) == (QQ(1, 2), [(x + 1, 2)])
R, x = ring("x", FF(2))
assert R.dup_factor_list(x**2 + 1) == (1, [(x + 1, 2)])
R, x = ring("x", RR)
assert R.dup_factor_list(1.0*x**2 + 2.0*x + 1.0) == (1.0, [(1.0*x + 1.0, 2)])
assert R.dup_factor_list(2.0*x**2 + 4.0*x + 2.0) == (2.0, [(1.0*x + 1.0, 2)])
f = 6.7225336055071*x**2 - 10.6463972754741*x - 0.33469524022264
coeff, factors = R.dup_factor_list(f)
assert coeff == RR(10.6463972754741)
assert len(factors) == 1
assert factors[0][0].max_norm() == RR(1.0)
assert factors[0][1] == 1
Rt, t = ring("t", ZZ)
R, x = ring("x", Rt)
f = 4*t*x**2 + 4*t**2*x
assert R.dup_factor_list(f) == \
(4*t, [(x, 1),
(x + t, 1)])
Rt, t = ring("t", QQ)
R, x = ring("x", Rt)
f = QQ(1, 2)*t*x**2 + QQ(1, 2)*t**2*x
assert R.dup_factor_list(f) == \
(QQ(1, 2)*t, [(x, 1),
(x + t, 1)])
R, x = ring("x", QQ.algebraic_field(I))
def anp(element):
return ANP(element, [QQ(1), QQ(0), QQ(1)], QQ)
f = anp([QQ(1, 1)])*x**4 + anp([QQ(2, 1)])*x**2
assert R.dup_factor_list(f) == \
(anp([QQ(1, 1)]), [(anp([QQ(1, 1)])*x, 2),
(anp([QQ(1, 1)])*x**2 + anp([])*x + anp([QQ(2, 1)]), 1)])
R, x = ring("x", EX)
raises(DomainError, lambda: R.dup_factor_list(EX(sin(1))))
def test_dmp_factor_list():
R, x, y = ring("x,y", ZZ)
assert R.dmp_factor_list(0) == (ZZ(0), [])
assert R.dmp_factor_list(7) == (7, [])
R, x, y = ring("x,y", QQ)
assert R.dmp_factor_list(0) == (QQ(0), [])
assert R.dmp_factor_list(QQ(1, 7)) == (QQ(1, 7), [])
Rt, t = ring("t", ZZ)
R, x, y = ring("x,y", Rt)
assert R.dmp_factor_list(0) == (0, [])
assert R.dmp_factor_list(7) == (ZZ(7), [])
Rt, t = ring("t", QQ)
R, x, y = ring("x,y", Rt)
assert R.dmp_factor_list(0) == (0, [])
assert R.dmp_factor_list(QQ(1, 7)) == (QQ(1, 7), [])
R, x, y = ring("x,y", ZZ)
assert R.dmp_factor_list_include(0) == [(0, 1)]
assert R.dmp_factor_list_include(7) == [(7, 1)]
R, X = xring("x:200", ZZ)
f, g = X[0]**2 + 2*X[0] + 1, X[0] + 1
assert R.dmp_factor_list(f) == (1, [(g, 2)])
f, g = X[-1]**2 + 2*X[-1] + 1, X[-1] + 1
assert R.dmp_factor_list(f) == (1, [(g, 2)])
R, x = ring("x", ZZ)
assert R.dmp_factor_list(x**2 + 2*x + 1) == (1, [(x + 1, 2)])
R, x = ring("x", QQ)
assert R.dmp_factor_list(QQ(1,2)*x**2 + x + QQ(1,2)) == (QQ(1,2), [(x + 1, 2)])
R, x, y = ring("x,y", ZZ)
assert R.dmp_factor_list(x**2 + 2*x + 1) == (1, [(x + 1, 2)])
R, x, y = ring("x,y", QQ)
assert R.dmp_factor_list(QQ(1,2)*x**2 + x + QQ(1,2)) == (QQ(1,2), [(x + 1, 2)])
R, x, y = ring("x,y", ZZ)
f = 4*x**2*y + 4*x*y**2
assert R.dmp_factor_list(f) == \
(4, [(y, 1),
(x, 1),
(x + y, 1)])
assert R.dmp_factor_list_include(f) == \
[(4*y, 1),
(x, 1),
(x + y, 1)]
R, x, y = ring("x,y", QQ)
f = QQ(1,2)*x**2*y + QQ(1,2)*x*y**2
assert R.dmp_factor_list(f) == \
(QQ(1,2), [(y, 1),
(x, 1),
(x + y, 1)])
R, x, y = ring("x,y", RR)
f = 2.0*x**2 - 8.0*y**2
assert R.dmp_factor_list(f) == \
(RR(8.0), [(0.5*x - y, 1),
(0.5*x + y, 1)])
f = 6.7225336055071*x**2*y**2 - 10.6463972754741*x*y - 0.33469524022264
coeff, factors = R.dmp_factor_list(f)
assert coeff == RR(10.6463972754741)
assert len(factors) == 1
assert factors[0][0].max_norm() == RR(1.0)
assert factors[0][1] == 1
Rt, t = ring("t", ZZ)
R, x, y = ring("x,y", Rt)
f = 4*t*x**2 + 4*t**2*x
assert R.dmp_factor_list(f) == \
(4*t, [(x, 1),
(x + t, 1)])
Rt, t = ring("t", QQ)
R, x, y = ring("x,y", Rt)
f = QQ(1, 2)*t*x**2 + QQ(1, 2)*t**2*x
assert R.dmp_factor_list(f) == \
(QQ(1, 2)*t, [(x, 1),
(x + t, 1)])
R, x, y = ring("x,y", FF(2))
raises(NotImplementedError, lambda: R.dmp_factor_list(x**2 + y**2))
R, x, y = ring("x,y", EX)
raises(DomainError, lambda: R.dmp_factor_list(EX(sin(1))))
def test_dup_irreducible_p():
R, x = ring("x", ZZ)
assert R.dup_irreducible_p(x**2 + x + 1) is True
assert R.dup_irreducible_p(x**2 + 2*x + 1) is False
def test_dmp_irreducible_p():
R, x, y = ring("x,y", ZZ)
assert R.dmp_irreducible_p(x**2 + x + 1) is True
assert R.dmp_irreducible_p(x**2 + 2*x + 1) is False

View File

@ -0,0 +1,362 @@
"""Test sparse rational functions. """
from sympy.polys.fields import field, sfield, FracField, FracElement
from sympy.polys.rings import ring
from sympy.polys.domains import ZZ, QQ
from sympy.polys.orderings import lex
from sympy.testing.pytest import raises, XFAIL
from sympy.core import symbols, E
from sympy.core.numbers import Rational
from sympy.functions.elementary.exponential import (exp, log)
from sympy.functions.elementary.miscellaneous import sqrt
def test_FracField___init__():
F1 = FracField("x,y", ZZ, lex)
F2 = FracField("x,y", ZZ, lex)
F3 = FracField("x,y,z", ZZ, lex)
assert F1.x == F1.gens[0]
assert F1.y == F1.gens[1]
assert F1.x == F2.x
assert F1.y == F2.y
assert F1.x != F3.x
assert F1.y != F3.y
def test_FracField___hash__():
F, x, y, z = field("x,y,z", QQ)
assert hash(F)
def test_FracField___eq__():
assert field("x,y,z", QQ)[0] == field("x,y,z", QQ)[0]
assert field("x,y,z", QQ)[0] is field("x,y,z", QQ)[0]
assert field("x,y,z", QQ)[0] != field("x,y,z", ZZ)[0]
assert field("x,y,z", QQ)[0] is not field("x,y,z", ZZ)[0]
assert field("x,y,z", ZZ)[0] != field("x,y,z", QQ)[0]
assert field("x,y,z", ZZ)[0] is not field("x,y,z", QQ)[0]
assert field("x,y,z", QQ)[0] != field("x,y", QQ)[0]
assert field("x,y,z", QQ)[0] is not field("x,y", QQ)[0]
assert field("x,y", QQ)[0] != field("x,y,z", QQ)[0]
assert field("x,y", QQ)[0] is not field("x,y,z", QQ)[0]
def test_sfield():
x = symbols("x")
F = FracField((E, exp(exp(x)), exp(x)), ZZ, lex)
e, exex, ex = F.gens
assert sfield(exp(x)*exp(exp(x) + 1 + log(exp(x) + 3)/2)**2/(exp(x) + 3)) \
== (F, e**2*exex**2*ex)
F = FracField((x, exp(1/x), log(x), x**QQ(1, 3)), ZZ, lex)
_, ex, lg, x3 = F.gens
assert sfield(((x-3)*log(x)+4*x**2)*exp(1/x+log(x)/3)/x**2) == \
(F, (4*F.x**2*ex + F.x*ex*lg - 3*ex*lg)/x3**5)
F = FracField((x, log(x), sqrt(x + log(x))), ZZ, lex)
_, lg, srt = F.gens
assert sfield((x + 1) / (x * (x + log(x))**QQ(3, 2)) - 1/(x * log(x)**2)) \
== (F, (F.x*lg**2 - F.x*srt + lg**2 - lg*srt)/
(F.x**2*lg**2*srt + F.x*lg**3*srt))
def test_FracElement___hash__():
F, x, y, z = field("x,y,z", QQ)
assert hash(x*y/z)
def test_FracElement_copy():
F, x, y, z = field("x,y,z", ZZ)
f = x*y/3*z
g = f.copy()
assert f == g
g.numer[(1, 1, 1)] = 7
assert f != g
def test_FracElement_as_expr():
F, x, y, z = field("x,y,z", ZZ)
f = (3*x**2*y - x*y*z)/(7*z**3 + 1)
X, Y, Z = F.symbols
g = (3*X**2*Y - X*Y*Z)/(7*Z**3 + 1)
assert f != g
assert f.as_expr() == g
X, Y, Z = symbols("x,y,z")
g = (3*X**2*Y - X*Y*Z)/(7*Z**3 + 1)
assert f != g
assert f.as_expr(X, Y, Z) == g
raises(ValueError, lambda: f.as_expr(X))
def test_FracElement_from_expr():
x, y, z = symbols("x,y,z")
F, X, Y, Z = field((x, y, z), ZZ)
f = F.from_expr(1)
assert f == 1 and isinstance(f, F.dtype)
f = F.from_expr(Rational(3, 7))
assert f == F(3)/7 and isinstance(f, F.dtype)
f = F.from_expr(x)
assert f == X and isinstance(f, F.dtype)
f = F.from_expr(Rational(3,7)*x)
assert f == X*Rational(3, 7) and isinstance(f, F.dtype)
f = F.from_expr(1/x)
assert f == 1/X and isinstance(f, F.dtype)
f = F.from_expr(x*y*z)
assert f == X*Y*Z and isinstance(f, F.dtype)
f = F.from_expr(x*y/z)
assert f == X*Y/Z and isinstance(f, F.dtype)
f = F.from_expr(x*y*z + x*y + x)
assert f == X*Y*Z + X*Y + X and isinstance(f, F.dtype)
f = F.from_expr((x*y*z + x*y + x)/(x*y + 7))
assert f == (X*Y*Z + X*Y + X)/(X*Y + 7) and isinstance(f, F.dtype)
f = F.from_expr(x**3*y*z + x**2*y**7 + 1)
assert f == X**3*Y*Z + X**2*Y**7 + 1 and isinstance(f, F.dtype)
raises(ValueError, lambda: F.from_expr(2**x))
raises(ValueError, lambda: F.from_expr(7*x + sqrt(2)))
assert isinstance(ZZ[2**x].get_field().convert(2**(-x)),
FracElement)
assert isinstance(ZZ[x**2].get_field().convert(x**(-6)),
FracElement)
assert isinstance(ZZ[exp(Rational(1, 3))].get_field().convert(E),
FracElement)
def test_FracField_nested():
a, b, x = symbols('a b x')
F1 = ZZ.frac_field(a, b)
F2 = F1.frac_field(x)
frac = F2(a + b)
assert frac.numer == F1.poly_ring(x)(a + b)
assert frac.numer.coeffs() == [F1(a + b)]
assert frac.denom == F1.poly_ring(x)(1)
F3 = ZZ.poly_ring(a, b)
F4 = F3.frac_field(x)
frac = F4(a + b)
assert frac.numer == F3.poly_ring(x)(a + b)
assert frac.numer.coeffs() == [F3(a + b)]
assert frac.denom == F3.poly_ring(x)(1)
frac = F2(F3(a + b))
assert frac.numer == F1.poly_ring(x)(a + b)
assert frac.numer.coeffs() == [F1(a + b)]
assert frac.denom == F1.poly_ring(x)(1)
frac = F4(F1(a + b))
assert frac.numer == F3.poly_ring(x)(a + b)
assert frac.numer.coeffs() == [F3(a + b)]
assert frac.denom == F3.poly_ring(x)(1)
def test_FracElement__lt_le_gt_ge__():
F, x, y = field("x,y", ZZ)
assert F(1) < 1/x < 1/x**2 < 1/x**3
assert F(1) <= 1/x <= 1/x**2 <= 1/x**3
assert -7/x < 1/x < 3/x < y/x < 1/x**2
assert -7/x <= 1/x <= 3/x <= y/x <= 1/x**2
assert 1/x**3 > 1/x**2 > 1/x > F(1)
assert 1/x**3 >= 1/x**2 >= 1/x >= F(1)
assert 1/x**2 > y/x > 3/x > 1/x > -7/x
assert 1/x**2 >= y/x >= 3/x >= 1/x >= -7/x
def test_FracElement___neg__():
F, x,y = field("x,y", QQ)
f = (7*x - 9)/y
g = (-7*x + 9)/y
assert -f == g
assert -g == f
def test_FracElement___add__():
F, x,y = field("x,y", QQ)
f, g = 1/x, 1/y
assert f + g == g + f == (x + y)/(x*y)
assert x + F.ring.gens[0] == F.ring.gens[0] + x == 2*x
F, x,y = field("x,y", ZZ)
assert x + 3 == 3 + x
assert x + QQ(3,7) == QQ(3,7) + x == (7*x + 3)/7
Fuv, u,v = field("u,v", ZZ)
Fxyzt, x,y,z,t = field("x,y,z,t", Fuv)
f = (u*v + x)/(y + u*v)
assert dict(f.numer) == {(1, 0, 0, 0): 1, (0, 0, 0, 0): u*v}
assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0): u*v}
Ruv, u,v = ring("u,v", ZZ)
Fxyzt, x,y,z,t = field("x,y,z,t", Ruv)
f = (u*v + x)/(y + u*v)
assert dict(f.numer) == {(1, 0, 0, 0): 1, (0, 0, 0, 0): u*v}
assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0): u*v}
def test_FracElement___sub__():
F, x,y = field("x,y", QQ)
f, g = 1/x, 1/y
assert f - g == (-x + y)/(x*y)
assert x - F.ring.gens[0] == F.ring.gens[0] - x == 0
F, x,y = field("x,y", ZZ)
assert x - 3 == -(3 - x)
assert x - QQ(3,7) == -(QQ(3,7) - x) == (7*x - 3)/7
Fuv, u,v = field("u,v", ZZ)
Fxyzt, x,y,z,t = field("x,y,z,t", Fuv)
f = (u*v - x)/(y - u*v)
assert dict(f.numer) == {(1, 0, 0, 0):-1, (0, 0, 0, 0): u*v}
assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0):-u*v}
Ruv, u,v = ring("u,v", ZZ)
Fxyzt, x,y,z,t = field("x,y,z,t", Ruv)
f = (u*v - x)/(y - u*v)
assert dict(f.numer) == {(1, 0, 0, 0):-1, (0, 0, 0, 0): u*v}
assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0):-u*v}
def test_FracElement___mul__():
F, x,y = field("x,y", QQ)
f, g = 1/x, 1/y
assert f*g == g*f == 1/(x*y)
assert x*F.ring.gens[0] == F.ring.gens[0]*x == x**2
F, x,y = field("x,y", ZZ)
assert x*3 == 3*x
assert x*QQ(3,7) == QQ(3,7)*x == x*Rational(3, 7)
Fuv, u,v = field("u,v", ZZ)
Fxyzt, x,y,z,t = field("x,y,z,t", Fuv)
f = ((u + 1)*x*y + 1)/((v - 1)*z - t*u*v - 1)
assert dict(f.numer) == {(1, 1, 0, 0): u + 1, (0, 0, 0, 0): 1}
assert dict(f.denom) == {(0, 0, 1, 0): v - 1, (0, 0, 0, 1): -u*v, (0, 0, 0, 0): -1}
Ruv, u,v = ring("u,v", ZZ)
Fxyzt, x,y,z,t = field("x,y,z,t", Ruv)
f = ((u + 1)*x*y + 1)/((v - 1)*z - t*u*v - 1)
assert dict(f.numer) == {(1, 1, 0, 0): u + 1, (0, 0, 0, 0): 1}
assert dict(f.denom) == {(0, 0, 1, 0): v - 1, (0, 0, 0, 1): -u*v, (0, 0, 0, 0): -1}
def test_FracElement___truediv__():
F, x,y = field("x,y", QQ)
f, g = 1/x, 1/y
assert f/g == y/x
assert x/F.ring.gens[0] == F.ring.gens[0]/x == 1
F, x,y = field("x,y", ZZ)
assert x*3 == 3*x
assert x/QQ(3,7) == (QQ(3,7)/x)**-1 == x*Rational(7, 3)
raises(ZeroDivisionError, lambda: x/0)
raises(ZeroDivisionError, lambda: 1/(x - x))
raises(ZeroDivisionError, lambda: x/(x - x))
Fuv, u,v = field("u,v", ZZ)
Fxyzt, x,y,z,t = field("x,y,z,t", Fuv)
f = (u*v)/(x*y)
assert dict(f.numer) == {(0, 0, 0, 0): u*v}
assert dict(f.denom) == {(1, 1, 0, 0): 1}
g = (x*y)/(u*v)
assert dict(g.numer) == {(1, 1, 0, 0): 1}
assert dict(g.denom) == {(0, 0, 0, 0): u*v}
Ruv, u,v = ring("u,v", ZZ)
Fxyzt, x,y,z,t = field("x,y,z,t", Ruv)
f = (u*v)/(x*y)
assert dict(f.numer) == {(0, 0, 0, 0): u*v}
assert dict(f.denom) == {(1, 1, 0, 0): 1}
g = (x*y)/(u*v)
assert dict(g.numer) == {(1, 1, 0, 0): 1}
assert dict(g.denom) == {(0, 0, 0, 0): u*v}
def test_FracElement___pow__():
F, x,y = field("x,y", QQ)
f, g = 1/x, 1/y
assert f**3 == 1/x**3
assert g**3 == 1/y**3
assert (f*g)**3 == 1/(x**3*y**3)
assert (f*g)**-3 == (x*y)**3
raises(ZeroDivisionError, lambda: (x - x)**-3)
def test_FracElement_diff():
F, x,y,z = field("x,y,z", ZZ)
assert ((x**2 + y)/(z + 1)).diff(x) == 2*x/(z + 1)
@XFAIL
def test_FracElement___call__():
F, x,y,z = field("x,y,z", ZZ)
f = (x**2 + 3*y)/z
r = f(1, 1, 1)
assert r == 4 and not isinstance(r, FracElement)
raises(ZeroDivisionError, lambda: f(1, 1, 0))
def test_FracElement_evaluate():
F, x,y,z = field("x,y,z", ZZ)
Fyz = field("y,z", ZZ)[0]
f = (x**2 + 3*y)/z
assert f.evaluate(x, 0) == 3*Fyz.y/Fyz.z
raises(ZeroDivisionError, lambda: f.evaluate(z, 0))
def test_FracElement_subs():
F, x,y,z = field("x,y,z", ZZ)
f = (x**2 + 3*y)/z
assert f.subs(x, 0) == 3*y/z
raises(ZeroDivisionError, lambda: f.subs(z, 0))
def test_FracElement_compose():
pass
def test_FracField_index():
a = symbols("a")
F, x, y, z = field('x y z', QQ)
assert F.index(x) == 0
assert F.index(y) == 1
raises(ValueError, lambda: F.index(1))
raises(ValueError, lambda: F.index(a))
pass

View File

@ -0,0 +1,875 @@
from sympy.polys.galoistools import (
gf_crt, gf_crt1, gf_crt2, gf_int,
gf_degree, gf_strip, gf_trunc, gf_normal,
gf_from_dict, gf_to_dict,
gf_from_int_poly, gf_to_int_poly,
gf_neg, gf_add_ground, gf_sub_ground, gf_mul_ground,
gf_add, gf_sub, gf_add_mul, gf_sub_mul, gf_mul, gf_sqr,
gf_div, gf_rem, gf_quo, gf_exquo,
gf_lshift, gf_rshift, gf_expand,
gf_pow, gf_pow_mod,
gf_gcdex, gf_gcd, gf_lcm, gf_cofactors,
gf_LC, gf_TC, gf_monic,
gf_eval, gf_multi_eval,
gf_compose, gf_compose_mod,
gf_trace_map,
gf_diff,
gf_irreducible, gf_irreducible_p,
gf_irred_p_ben_or, gf_irred_p_rabin,
gf_sqf_list, gf_sqf_part, gf_sqf_p,
gf_Qmatrix, gf_Qbasis,
gf_ddf_zassenhaus, gf_ddf_shoup,
gf_edf_zassenhaus, gf_edf_shoup,
gf_berlekamp,
gf_factor_sqf, gf_factor,
gf_value, linear_congruence, _csolve_prime_las_vegas,
csolve_prime, gf_csolve, gf_frobenius_map, gf_frobenius_monomial_base
)
from sympy.polys.polyerrors import (
ExactQuotientFailed,
)
from sympy.polys import polyconfig as config
from sympy.polys.domains import ZZ
from sympy.core.numbers import pi
from sympy.ntheory.generate import nextprime
from sympy.testing.pytest import raises
def test_gf_crt():
U = [49, 76, 65]
M = [99, 97, 95]
p = 912285
u = 639985
assert gf_crt(U, M, ZZ) == u
E = [9215, 9405, 9603]
S = [62, 24, 12]
assert gf_crt1(M, ZZ) == (p, E, S)
assert gf_crt2(U, M, p, E, S, ZZ) == u
def test_gf_int():
assert gf_int(0, 5) == 0
assert gf_int(1, 5) == 1
assert gf_int(2, 5) == 2
assert gf_int(3, 5) == -2
assert gf_int(4, 5) == -1
assert gf_int(5, 5) == 0
def test_gf_degree():
assert gf_degree([]) == -1
assert gf_degree([1]) == 0
assert gf_degree([1, 0]) == 1
assert gf_degree([1, 0, 0, 0, 1]) == 4
def test_gf_strip():
assert gf_strip([]) == []
assert gf_strip([0]) == []
assert gf_strip([0, 0, 0]) == []
assert gf_strip([1]) == [1]
assert gf_strip([0, 1]) == [1]
assert gf_strip([0, 0, 0, 1]) == [1]
assert gf_strip([1, 2, 0]) == [1, 2, 0]
assert gf_strip([0, 1, 2, 0]) == [1, 2, 0]
assert gf_strip([0, 0, 0, 1, 2, 0]) == [1, 2, 0]
def test_gf_trunc():
assert gf_trunc([], 11) == []
assert gf_trunc([1], 11) == [1]
assert gf_trunc([22], 11) == []
assert gf_trunc([12], 11) == [1]
assert gf_trunc([11, 22, 17, 1, 0], 11) == [6, 1, 0]
assert gf_trunc([12, 23, 17, 1, 0], 11) == [1, 1, 6, 1, 0]
def test_gf_normal():
assert gf_normal([11, 22, 17, 1, 0], 11, ZZ) == [6, 1, 0]
def test_gf_from_to_dict():
f = {11: 12, 6: 2, 0: 25}
F = {11: 1, 6: 2, 0: 3}
g = [1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 3]
assert gf_from_dict(f, 11, ZZ) == g
assert gf_to_dict(g, 11) == F
f = {11: -5, 4: 0, 3: 1, 0: 12}
F = {11: -5, 3: 1, 0: 1}
g = [6, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1]
assert gf_from_dict(f, 11, ZZ) == g
assert gf_to_dict(g, 11) == F
assert gf_to_dict([10], 11, symmetric=True) == {0: -1}
assert gf_to_dict([10], 11, symmetric=False) == {0: 10}
def test_gf_from_to_int_poly():
assert gf_from_int_poly([1, 0, 7, 2, 20], 5) == [1, 0, 2, 2, 0]
assert gf_to_int_poly([1, 0, 4, 2, 3], 5) == [1, 0, -1, 2, -2]
assert gf_to_int_poly([10], 11, symmetric=True) == [-1]
assert gf_to_int_poly([10], 11, symmetric=False) == [10]
def test_gf_LC():
assert gf_LC([], ZZ) == 0
assert gf_LC([1], ZZ) == 1
assert gf_LC([1, 2], ZZ) == 1
def test_gf_TC():
assert gf_TC([], ZZ) == 0
assert gf_TC([1], ZZ) == 1
assert gf_TC([1, 2], ZZ) == 2
def test_gf_monic():
assert gf_monic(ZZ.map([]), 11, ZZ) == (0, [])
assert gf_monic(ZZ.map([1]), 11, ZZ) == (1, [1])
assert gf_monic(ZZ.map([2]), 11, ZZ) == (2, [1])
assert gf_monic(ZZ.map([1, 2, 3, 4]), 11, ZZ) == (1, [1, 2, 3, 4])
assert gf_monic(ZZ.map([2, 3, 4, 5]), 11, ZZ) == (2, [1, 7, 2, 8])
def test_gf_arith():
assert gf_neg([], 11, ZZ) == []
assert gf_neg([1], 11, ZZ) == [10]
assert gf_neg([1, 2, 3], 11, ZZ) == [10, 9, 8]
assert gf_add_ground([], 0, 11, ZZ) == []
assert gf_sub_ground([], 0, 11, ZZ) == []
assert gf_add_ground([], 3, 11, ZZ) == [3]
assert gf_sub_ground([], 3, 11, ZZ) == [8]
assert gf_add_ground([1], 3, 11, ZZ) == [4]
assert gf_sub_ground([1], 3, 11, ZZ) == [9]
assert gf_add_ground([8], 3, 11, ZZ) == []
assert gf_sub_ground([3], 3, 11, ZZ) == []
assert gf_add_ground([1, 2, 3], 3, 11, ZZ) == [1, 2, 6]
assert gf_sub_ground([1, 2, 3], 3, 11, ZZ) == [1, 2, 0]
assert gf_mul_ground([], 0, 11, ZZ) == []
assert gf_mul_ground([], 1, 11, ZZ) == []
assert gf_mul_ground([1], 0, 11, ZZ) == []
assert gf_mul_ground([1], 1, 11, ZZ) == [1]
assert gf_mul_ground([1, 2, 3], 0, 11, ZZ) == []
assert gf_mul_ground([1, 2, 3], 1, 11, ZZ) == [1, 2, 3]
assert gf_mul_ground([1, 2, 3], 7, 11, ZZ) == [7, 3, 10]
assert gf_add([], [], 11, ZZ) == []
assert gf_add([1], [], 11, ZZ) == [1]
assert gf_add([], [1], 11, ZZ) == [1]
assert gf_add([1], [1], 11, ZZ) == [2]
assert gf_add([1], [2], 11, ZZ) == [3]
assert gf_add([1, 2], [1], 11, ZZ) == [1, 3]
assert gf_add([1], [1, 2], 11, ZZ) == [1, 3]
assert gf_add([1, 2, 3], [8, 9, 10], 11, ZZ) == [9, 0, 2]
assert gf_sub([], [], 11, ZZ) == []
assert gf_sub([1], [], 11, ZZ) == [1]
assert gf_sub([], [1], 11, ZZ) == [10]
assert gf_sub([1], [1], 11, ZZ) == []
assert gf_sub([1], [2], 11, ZZ) == [10]
assert gf_sub([1, 2], [1], 11, ZZ) == [1, 1]
assert gf_sub([1], [1, 2], 11, ZZ) == [10, 10]
assert gf_sub([3, 2, 1], [8, 9, 10], 11, ZZ) == [6, 4, 2]
assert gf_add_mul(
[1, 5, 6], [7, 3], [8, 0, 6, 1], 11, ZZ) == [1, 2, 10, 8, 9]
assert gf_sub_mul(
[1, 5, 6], [7, 3], [8, 0, 6, 1], 11, ZZ) == [10, 9, 3, 2, 3]
assert gf_mul([], [], 11, ZZ) == []
assert gf_mul([], [1], 11, ZZ) == []
assert gf_mul([1], [], 11, ZZ) == []
assert gf_mul([1], [1], 11, ZZ) == [1]
assert gf_mul([5], [7], 11, ZZ) == [2]
assert gf_mul([3, 0, 0, 6, 1, 2], [4, 0, 1, 0], 11, ZZ) == [1, 0,
3, 2, 4, 3, 1, 2, 0]
assert gf_mul([4, 0, 1, 0], [3, 0, 0, 6, 1, 2], 11, ZZ) == [1, 0,
3, 2, 4, 3, 1, 2, 0]
assert gf_mul([2, 0, 0, 1, 7], [2, 0, 0, 1, 7], 11, ZZ) == [4, 0,
0, 4, 6, 0, 1, 3, 5]
assert gf_sqr([], 11, ZZ) == []
assert gf_sqr([2], 11, ZZ) == [4]
assert gf_sqr([1, 2], 11, ZZ) == [1, 4, 4]
assert gf_sqr([2, 0, 0, 1, 7], 11, ZZ) == [4, 0, 0, 4, 6, 0, 1, 3, 5]
def test_gf_division():
raises(ZeroDivisionError, lambda: gf_div([1, 2, 3], [], 11, ZZ))
raises(ZeroDivisionError, lambda: gf_rem([1, 2, 3], [], 11, ZZ))
raises(ZeroDivisionError, lambda: gf_quo([1, 2, 3], [], 11, ZZ))
raises(ZeroDivisionError, lambda: gf_quo([1, 2, 3], [], 11, ZZ))
assert gf_div([1], [1, 2, 3], 7, ZZ) == ([], [1])
assert gf_rem([1], [1, 2, 3], 7, ZZ) == [1]
assert gf_quo([1], [1, 2, 3], 7, ZZ) == []
f = ZZ.map([5, 4, 3, 2, 1, 0])
g = ZZ.map([1, 2, 3])
q = [5, 1, 0, 6]
r = [3, 3]
assert gf_div(f, g, 7, ZZ) == (q, r)
assert gf_rem(f, g, 7, ZZ) == r
assert gf_quo(f, g, 7, ZZ) == q
raises(ExactQuotientFailed, lambda: gf_exquo(f, g, 7, ZZ))
f = ZZ.map([5, 4, 3, 2, 1, 0])
g = ZZ.map([1, 2, 3, 0])
q = [5, 1, 0]
r = [6, 1, 0]
assert gf_div(f, g, 7, ZZ) == (q, r)
assert gf_rem(f, g, 7, ZZ) == r
assert gf_quo(f, g, 7, ZZ) == q
raises(ExactQuotientFailed, lambda: gf_exquo(f, g, 7, ZZ))
assert gf_quo(ZZ.map([1, 2, 1]), ZZ.map([1, 1]), 11, ZZ) == [1, 1]
def test_gf_shift():
f = [1, 2, 3, 4, 5]
assert gf_lshift([], 5, ZZ) == []
assert gf_rshift([], 5, ZZ) == ([], [])
assert gf_lshift(f, 1, ZZ) == [1, 2, 3, 4, 5, 0]
assert gf_lshift(f, 2, ZZ) == [1, 2, 3, 4, 5, 0, 0]
assert gf_rshift(f, 0, ZZ) == (f, [])
assert gf_rshift(f, 1, ZZ) == ([1, 2, 3, 4], [5])
assert gf_rshift(f, 3, ZZ) == ([1, 2], [3, 4, 5])
assert gf_rshift(f, 5, ZZ) == ([], f)
def test_gf_expand():
F = [([1, 1], 2), ([1, 2], 3)]
assert gf_expand(F, 11, ZZ) == [1, 8, 3, 5, 6, 8]
assert gf_expand((4, F), 11, ZZ) == [4, 10, 1, 9, 2, 10]
def test_gf_powering():
assert gf_pow([1, 0, 0, 1, 8], 0, 11, ZZ) == [1]
assert gf_pow([1, 0, 0, 1, 8], 1, 11, ZZ) == [1, 0, 0, 1, 8]
assert gf_pow([1, 0, 0, 1, 8], 2, 11, ZZ) == [1, 0, 0, 2, 5, 0, 1, 5, 9]
assert gf_pow([1, 0, 0, 1, 8], 5, 11, ZZ) == \
[1, 0, 0, 5, 7, 0, 10, 6, 2, 10, 9, 6, 10, 6, 6, 0, 5, 2, 5, 9, 10]
assert gf_pow([1, 0, 0, 1, 8], 8, 11, ZZ) == \
[1, 0, 0, 8, 9, 0, 6, 8, 10, 1, 2, 5, 10, 7, 7, 9, 1, 2, 0, 0, 6, 2,
5, 2, 5, 7, 7, 9, 10, 10, 7, 5, 5]
assert gf_pow([1, 0, 0, 1, 8], 45, 11, ZZ) == \
[ 1, 0, 0, 1, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 4, 10, 0, 0, 0, 0, 0, 0,
10, 0, 0, 10, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6, 0, 0, 6, 4, 0, 0, 0, 0, 0, 0, 8, 0, 0, 8, 9, 0, 0, 0, 0, 0, 0,
10, 0, 0, 10, 3, 0, 0, 0, 0, 0, 0, 4, 0, 0, 4, 10, 0, 0, 0, 0, 0, 0,
8, 0, 0, 8, 9, 0, 0, 0, 0, 0, 0, 9, 0, 0, 9, 6, 0, 0, 0, 0, 0, 0,
3, 0, 0, 3, 2, 0, 0, 0, 0, 0, 0, 10, 0, 0, 10, 3, 0, 0, 0, 0, 0, 0,
10, 0, 0, 10, 3, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 5, 0, 0, 0, 0, 0, 0,
4, 0, 0, 4, 10]
assert gf_pow_mod(ZZ.map([1, 0, 0, 1, 8]), 0, ZZ.map([2, 0, 7]), 11, ZZ) == [1]
assert gf_pow_mod(ZZ.map([1, 0, 0, 1, 8]), 1, ZZ.map([2, 0, 7]), 11, ZZ) == [1, 1]
assert gf_pow_mod(ZZ.map([1, 0, 0, 1, 8]), 2, ZZ.map([2, 0, 7]), 11, ZZ) == [2, 3]
assert gf_pow_mod(ZZ.map([1, 0, 0, 1, 8]), 5, ZZ.map([2, 0, 7]), 11, ZZ) == [7, 8]
assert gf_pow_mod(ZZ.map([1, 0, 0, 1, 8]), 8, ZZ.map([2, 0, 7]), 11, ZZ) == [1, 5]
assert gf_pow_mod(ZZ.map([1, 0, 0, 1, 8]), 45, ZZ.map([2, 0, 7]), 11, ZZ) == [5, 4]
def test_gf_gcdex():
assert gf_gcdex(ZZ.map([]), ZZ.map([]), 11, ZZ) == ([1], [], [])
assert gf_gcdex(ZZ.map([2]), ZZ.map([]), 11, ZZ) == ([6], [], [1])
assert gf_gcdex(ZZ.map([]), ZZ.map([2]), 11, ZZ) == ([], [6], [1])
assert gf_gcdex(ZZ.map([2]), ZZ.map([2]), 11, ZZ) == ([], [6], [1])
assert gf_gcdex(ZZ.map([]), ZZ.map([3, 0]), 11, ZZ) == ([], [4], [1, 0])
assert gf_gcdex(ZZ.map([3, 0]), ZZ.map([]), 11, ZZ) == ([4], [], [1, 0])
assert gf_gcdex(ZZ.map([3, 0]), ZZ.map([3, 0]), 11, ZZ) == ([], [4], [1, 0])
assert gf_gcdex(ZZ.map([1, 8, 7]), ZZ.map([1, 7, 1, 7]), 11, ZZ) == ([5, 6], [6], [1, 7])
def test_gf_gcd():
assert gf_gcd(ZZ.map([]), ZZ.map([]), 11, ZZ) == []
assert gf_gcd(ZZ.map([2]), ZZ.map([]), 11, ZZ) == [1]
assert gf_gcd(ZZ.map([]), ZZ.map([2]), 11, ZZ) == [1]
assert gf_gcd(ZZ.map([2]), ZZ.map([2]), 11, ZZ) == [1]
assert gf_gcd(ZZ.map([]), ZZ.map([1, 0]), 11, ZZ) == [1, 0]
assert gf_gcd(ZZ.map([1, 0]), ZZ.map([]), 11, ZZ) == [1, 0]
assert gf_gcd(ZZ.map([3, 0]), ZZ.map([3, 0]), 11, ZZ) == [1, 0]
assert gf_gcd(ZZ.map([1, 8, 7]), ZZ.map([1, 7, 1, 7]), 11, ZZ) == [1, 7]
def test_gf_lcm():
assert gf_lcm(ZZ.map([]), ZZ.map([]), 11, ZZ) == []
assert gf_lcm(ZZ.map([2]), ZZ.map([]), 11, ZZ) == []
assert gf_lcm(ZZ.map([]), ZZ.map([2]), 11, ZZ) == []
assert gf_lcm(ZZ.map([2]), ZZ.map([2]), 11, ZZ) == [1]
assert gf_lcm(ZZ.map([]), ZZ.map([1, 0]), 11, ZZ) == []
assert gf_lcm(ZZ.map([1, 0]), ZZ.map([]), 11, ZZ) == []
assert gf_lcm(ZZ.map([3, 0]), ZZ.map([3, 0]), 11, ZZ) == [1, 0]
assert gf_lcm(ZZ.map([1, 8, 7]), ZZ.map([1, 7, 1, 7]), 11, ZZ) == [1, 8, 8, 8, 7]
def test_gf_cofactors():
assert gf_cofactors(ZZ.map([]), ZZ.map([]), 11, ZZ) == ([], [], [])
assert gf_cofactors(ZZ.map([2]), ZZ.map([]), 11, ZZ) == ([1], [2], [])
assert gf_cofactors(ZZ.map([]), ZZ.map([2]), 11, ZZ) == ([1], [], [2])
assert gf_cofactors(ZZ.map([2]), ZZ.map([2]), 11, ZZ) == ([1], [2], [2])
assert gf_cofactors(ZZ.map([]), ZZ.map([1, 0]), 11, ZZ) == ([1, 0], [], [1])
assert gf_cofactors(ZZ.map([1, 0]), ZZ.map([]), 11, ZZ) == ([1, 0], [1], [])
assert gf_cofactors(ZZ.map([3, 0]), ZZ.map([3, 0]), 11, ZZ) == (
[1, 0], [3], [3])
assert gf_cofactors(ZZ.map([1, 8, 7]), ZZ.map([1, 7, 1, 7]), 11, ZZ) == (
([1, 7], [1, 1], [1, 0, 1]))
def test_gf_diff():
assert gf_diff([], 11, ZZ) == []
assert gf_diff([7], 11, ZZ) == []
assert gf_diff([7, 3], 11, ZZ) == [7]
assert gf_diff([7, 3, 1], 11, ZZ) == [3, 3]
assert gf_diff([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], 11, ZZ) == []
def test_gf_eval():
assert gf_eval([], 4, 11, ZZ) == 0
assert gf_eval([], 27, 11, ZZ) == 0
assert gf_eval([7], 4, 11, ZZ) == 7
assert gf_eval([7], 27, 11, ZZ) == 7
assert gf_eval([1, 0, 3, 2, 4, 3, 1, 2, 0], 0, 11, ZZ) == 0
assert gf_eval([1, 0, 3, 2, 4, 3, 1, 2, 0], 4, 11, ZZ) == 9
assert gf_eval([1, 0, 3, 2, 4, 3, 1, 2, 0], 27, 11, ZZ) == 5
assert gf_eval([4, 0, 0, 4, 6, 0, 1, 3, 5], 0, 11, ZZ) == 5
assert gf_eval([4, 0, 0, 4, 6, 0, 1, 3, 5], 4, 11, ZZ) == 3
assert gf_eval([4, 0, 0, 4, 6, 0, 1, 3, 5], 27, 11, ZZ) == 9
assert gf_multi_eval([3, 2, 1], [0, 1, 2, 3], 11, ZZ) == [1, 6, 6, 1]
def test_gf_compose():
assert gf_compose([], [1, 0], 11, ZZ) == []
assert gf_compose_mod([], [1, 0], [1, 0], 11, ZZ) == []
assert gf_compose([1], [], 11, ZZ) == [1]
assert gf_compose([1, 0], [], 11, ZZ) == []
assert gf_compose([1, 0], [1, 0], 11, ZZ) == [1, 0]
f = ZZ.map([1, 1, 4, 9, 1])
g = ZZ.map([1, 1, 1])
h = ZZ.map([1, 0, 0, 2])
assert gf_compose(g, h, 11, ZZ) == [1, 0, 0, 5, 0, 0, 7]
assert gf_compose_mod(g, h, f, 11, ZZ) == [3, 9, 6, 10]
def test_gf_trace_map():
f = ZZ.map([1, 1, 4, 9, 1])
a = [1, 1, 1]
c = ZZ.map([1, 0])
b = gf_pow_mod(c, 11, f, 11, ZZ)
assert gf_trace_map(a, b, c, 0, f, 11, ZZ) == \
([1, 1, 1], [1, 1, 1])
assert gf_trace_map(a, b, c, 1, f, 11, ZZ) == \
([5, 2, 10, 3], [5, 3, 0, 4])
assert gf_trace_map(a, b, c, 2, f, 11, ZZ) == \
([5, 9, 5, 3], [10, 1, 5, 7])
assert gf_trace_map(a, b, c, 3, f, 11, ZZ) == \
([1, 10, 6, 0], [7])
assert gf_trace_map(a, b, c, 4, f, 11, ZZ) == \
([1, 1, 1], [1, 1, 8])
assert gf_trace_map(a, b, c, 5, f, 11, ZZ) == \
([5, 2, 10, 3], [5, 3, 0, 0])
assert gf_trace_map(a, b, c, 11, f, 11, ZZ) == \
([1, 10, 6, 0], [10])
def test_gf_irreducible():
assert gf_irreducible_p(gf_irreducible(1, 11, ZZ), 11, ZZ) is True
assert gf_irreducible_p(gf_irreducible(2, 11, ZZ), 11, ZZ) is True
assert gf_irreducible_p(gf_irreducible(3, 11, ZZ), 11, ZZ) is True
assert gf_irreducible_p(gf_irreducible(4, 11, ZZ), 11, ZZ) is True
assert gf_irreducible_p(gf_irreducible(5, 11, ZZ), 11, ZZ) is True
assert gf_irreducible_p(gf_irreducible(6, 11, ZZ), 11, ZZ) is True
assert gf_irreducible_p(gf_irreducible(7, 11, ZZ), 11, ZZ) is True
def test_gf_irreducible_p():
assert gf_irred_p_ben_or(ZZ.map([7]), 11, ZZ) is True
assert gf_irred_p_ben_or(ZZ.map([7, 3]), 11, ZZ) is True
assert gf_irred_p_ben_or(ZZ.map([7, 3, 1]), 11, ZZ) is False
assert gf_irred_p_rabin(ZZ.map([7]), 11, ZZ) is True
assert gf_irred_p_rabin(ZZ.map([7, 3]), 11, ZZ) is True
assert gf_irred_p_rabin(ZZ.map([7, 3, 1]), 11, ZZ) is False
config.setup('GF_IRRED_METHOD', 'ben-or')
assert gf_irreducible_p(ZZ.map([7]), 11, ZZ) is True
assert gf_irreducible_p(ZZ.map([7, 3]), 11, ZZ) is True
assert gf_irreducible_p(ZZ.map([7, 3, 1]), 11, ZZ) is False
config.setup('GF_IRRED_METHOD', 'rabin')
assert gf_irreducible_p(ZZ.map([7]), 11, ZZ) is True
assert gf_irreducible_p(ZZ.map([7, 3]), 11, ZZ) is True
assert gf_irreducible_p(ZZ.map([7, 3, 1]), 11, ZZ) is False
config.setup('GF_IRRED_METHOD', 'other')
raises(KeyError, lambda: gf_irreducible_p([7], 11, ZZ))
config.setup('GF_IRRED_METHOD')
f = ZZ.map([1, 9, 9, 13, 16, 15, 6, 7, 7, 7, 10])
g = ZZ.map([1, 7, 16, 7, 15, 13, 13, 11, 16, 10, 9])
h = gf_mul(f, g, 17, ZZ)
assert gf_irred_p_ben_or(f, 17, ZZ) is True
assert gf_irred_p_ben_or(g, 17, ZZ) is True
assert gf_irred_p_ben_or(h, 17, ZZ) is False
assert gf_irred_p_rabin(f, 17, ZZ) is True
assert gf_irred_p_rabin(g, 17, ZZ) is True
assert gf_irred_p_rabin(h, 17, ZZ) is False
def test_gf_squarefree():
assert gf_sqf_list([], 11, ZZ) == (0, [])
assert gf_sqf_list([1], 11, ZZ) == (1, [])
assert gf_sqf_list([1, 1], 11, ZZ) == (1, [([1, 1], 1)])
assert gf_sqf_p([], 11, ZZ) is True
assert gf_sqf_p([1], 11, ZZ) is True
assert gf_sqf_p([1, 1], 11, ZZ) is True
f = gf_from_dict({11: 1, 0: 1}, 11, ZZ)
assert gf_sqf_p(f, 11, ZZ) is False
assert gf_sqf_list(f, 11, ZZ) == \
(1, [([1, 1], 11)])
f = [1, 5, 8, 4]
assert gf_sqf_p(f, 11, ZZ) is False
assert gf_sqf_list(f, 11, ZZ) == \
(1, [([1, 1], 1),
([1, 2], 2)])
assert gf_sqf_part(f, 11, ZZ) == [1, 3, 2]
f = [1, 0, 0, 2, 0, 0, 2, 0, 0, 1, 0]
assert gf_sqf_list(f, 3, ZZ) == \
(1, [([1, 0], 1),
([1, 1], 3),
([1, 2], 6)])
def test_gf_frobenius_map():
f = ZZ.map([2, 0, 1, 0, 2, 2, 0, 2, 2, 2])
g = ZZ.map([1,1,0,2,0,1,0,2,0,1])
p = 3
b = gf_frobenius_monomial_base(g, p, ZZ)
h = gf_frobenius_map(f, g, b, p, ZZ)
h1 = gf_pow_mod(f, p, g, p, ZZ)
assert h == h1
def test_gf_berlekamp():
f = gf_from_int_poly([1, -3, 1, -3, -1, -3, 1], 11)
Q = [[1, 0, 0, 0, 0, 0],
[3, 5, 8, 8, 6, 5],
[3, 6, 6, 1, 10, 0],
[9, 4, 10, 3, 7, 9],
[7, 8, 10, 0, 0, 8],
[8, 10, 7, 8, 10, 8]]
V = [[1, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 0],
[0, 0, 7, 9, 0, 1]]
assert gf_Qmatrix(f, 11, ZZ) == Q
assert gf_Qbasis(Q, 11, ZZ) == V
assert gf_berlekamp(f, 11, ZZ) == \
[[1, 1], [1, 5, 3], [1, 2, 3, 4]]
f = ZZ.map([1, 0, 1, 0, 10, 10, 8, 2, 8])
Q = ZZ.map([[1, 0, 0, 0, 0, 0, 0, 0],
[2, 1, 7, 11, 10, 12, 5, 11],
[3, 6, 4, 3, 0, 4, 7, 2],
[4, 3, 6, 5, 1, 6, 2, 3],
[2, 11, 8, 8, 3, 1, 3, 11],
[6, 11, 8, 6, 2, 7, 10, 9],
[5, 11, 7, 10, 0, 11, 7, 12],
[3, 3, 12, 5, 0, 11, 9, 12]])
V = [[1, 0, 0, 0, 0, 0, 0, 0],
[0, 5, 5, 0, 9, 5, 1, 0],
[0, 9, 11, 9, 10, 12, 0, 1]]
assert gf_Qmatrix(f, 13, ZZ) == Q
assert gf_Qbasis(Q, 13, ZZ) == V
assert gf_berlekamp(f, 13, ZZ) == \
[[1, 3], [1, 8, 4, 12], [1, 2, 3, 4, 6]]
def test_gf_ddf():
f = gf_from_dict({15: ZZ(1), 0: ZZ(-1)}, 11, ZZ)
g = [([1, 0, 0, 0, 0, 10], 1),
([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], 2)]
assert gf_ddf_zassenhaus(f, 11, ZZ) == g
assert gf_ddf_shoup(f, 11, ZZ) == g
f = gf_from_dict({63: ZZ(1), 0: ZZ(1)}, 2, ZZ)
g = [([1, 1], 1),
([1, 1, 1], 2),
([1, 1, 1, 1, 1, 1, 1], 3),
([1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1], 6)]
assert gf_ddf_zassenhaus(f, 2, ZZ) == g
assert gf_ddf_shoup(f, 2, ZZ) == g
f = gf_from_dict({6: ZZ(1), 5: ZZ(-1), 4: ZZ(1), 3: ZZ(1), 1: ZZ(-1)}, 3, ZZ)
g = [([1, 1, 0], 1),
([1, 1, 0, 1, 2], 2)]
assert gf_ddf_zassenhaus(f, 3, ZZ) == g
assert gf_ddf_shoup(f, 3, ZZ) == g
f = ZZ.map([1, 2, 5, 26, 677, 436, 791, 325, 456, 24, 577])
g = [([1, 701], 1),
([1, 110, 559, 532, 694, 151, 110, 70, 735, 122], 9)]
assert gf_ddf_zassenhaus(f, 809, ZZ) == g
assert gf_ddf_shoup(f, 809, ZZ) == g
p = ZZ(nextprime(int((2**15 * pi).evalf())))
f = gf_from_dict({15: 1, 1: 1, 0: 1}, p, ZZ)
g = [([1, 22730, 68144], 2),
([1, 64876, 83977, 10787, 12561, 68608, 52650, 88001, 84356], 4),
([1, 15347, 95022, 84569, 94508, 92335], 5)]
assert gf_ddf_zassenhaus(f, p, ZZ) == g
assert gf_ddf_shoup(f, p, ZZ) == g
def test_gf_edf():
f = ZZ.map([1, 1, 0, 1, 2])
g = ZZ.map([[1, 0, 1], [1, 1, 2]])
assert gf_edf_zassenhaus(f, 2, 3, ZZ) == g
assert gf_edf_shoup(f, 2, 3, ZZ) == g
def test_issue_23174():
f = ZZ.map([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
g = ZZ.map([[1, 0, 0, 1, 1, 1, 0, 0, 1], [1, 1, 1, 0, 1, 0, 1, 1, 1]])
assert gf_edf_zassenhaus(f, 8, 2, ZZ) == g
def test_gf_factor():
assert gf_factor([], 11, ZZ) == (0, [])
assert gf_factor([1], 11, ZZ) == (1, [])
assert gf_factor([1, 1], 11, ZZ) == (1, [([1, 1], 1)])
assert gf_factor_sqf([], 11, ZZ) == (0, [])
assert gf_factor_sqf([1], 11, ZZ) == (1, [])
assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])
config.setup('GF_FACTOR_METHOD', 'berlekamp')
assert gf_factor_sqf([], 11, ZZ) == (0, [])
assert gf_factor_sqf([1], 11, ZZ) == (1, [])
assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])
config.setup('GF_FACTOR_METHOD', 'zassenhaus')
assert gf_factor_sqf([], 11, ZZ) == (0, [])
assert gf_factor_sqf([1], 11, ZZ) == (1, [])
assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]])
config.setup('GF_FACTOR_METHOD', 'shoup')
assert gf_factor_sqf(ZZ.map([]), 11, ZZ) == (0, [])
assert gf_factor_sqf(ZZ.map([1]), 11, ZZ) == (1, [])
assert gf_factor_sqf(ZZ.map([1, 1]), 11, ZZ) == (1, [[1, 1]])
f, p = ZZ.map([1, 0, 0, 1, 0]), 2
g = (1, [([1, 0], 1),
([1, 1], 1),
([1, 1, 1], 1)])
config.setup('GF_FACTOR_METHOD', 'berlekamp')
assert gf_factor(f, p, ZZ) == g
config.setup('GF_FACTOR_METHOD', 'zassenhaus')
assert gf_factor(f, p, ZZ) == g
config.setup('GF_FACTOR_METHOD', 'shoup')
assert gf_factor(f, p, ZZ) == g
g = (1, [[1, 0],
[1, 1],
[1, 1, 1]])
config.setup('GF_FACTOR_METHOD', 'berlekamp')
assert gf_factor_sqf(f, p, ZZ) == g
config.setup('GF_FACTOR_METHOD', 'zassenhaus')
assert gf_factor_sqf(f, p, ZZ) == g
config.setup('GF_FACTOR_METHOD', 'shoup')
assert gf_factor_sqf(f, p, ZZ) == g
f, p = gf_from_int_poly([1, -3, 1, -3, -1, -3, 1], 11), 11
g = (1, [([1, 1], 1),
([1, 5, 3], 1),
([1, 2, 3, 4], 1)])
config.setup('GF_FACTOR_METHOD', 'berlekamp')
assert gf_factor(f, p, ZZ) == g
config.setup('GF_FACTOR_METHOD', 'zassenhaus')
assert gf_factor(f, p, ZZ) == g
config.setup('GF_FACTOR_METHOD', 'shoup')
assert gf_factor(f, p, ZZ) == g
f, p = [1, 5, 8, 4], 11
g = (1, [([1, 1], 1), ([1, 2], 2)])
config.setup('GF_FACTOR_METHOD', 'berlekamp')
assert gf_factor(f, p, ZZ) == g
config.setup('GF_FACTOR_METHOD', 'zassenhaus')
assert gf_factor(f, p, ZZ) == g
config.setup('GF_FACTOR_METHOD', 'shoup')
assert gf_factor(f, p, ZZ) == g
f, p = [1, 1, 10, 1, 0, 10, 10, 10, 0, 0], 11
g = (1, [([1, 0], 2), ([1, 9, 5], 1), ([1, 3, 0, 8, 5, 2], 1)])
config.setup('GF_FACTOR_METHOD', 'berlekamp')
assert gf_factor(f, p, ZZ) == g
config.setup('GF_FACTOR_METHOD', 'zassenhaus')
assert gf_factor(f, p, ZZ) == g
config.setup('GF_FACTOR_METHOD', 'shoup')
assert gf_factor(f, p, ZZ) == g
f, p = gf_from_dict({32: 1, 0: 1}, 11, ZZ), 11
g = (1, [([1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 10], 1),
([1, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 10], 1)])
config.setup('GF_FACTOR_METHOD', 'berlekamp')
assert gf_factor(f, p, ZZ) == g
config.setup('GF_FACTOR_METHOD', 'zassenhaus')
assert gf_factor(f, p, ZZ) == g
config.setup('GF_FACTOR_METHOD', 'shoup')
assert gf_factor(f, p, ZZ) == g
f, p = gf_from_dict({32: ZZ(8), 0: ZZ(5)}, 11, ZZ), 11
g = (8, [([1, 3], 1),
([1, 8], 1),
([1, 0, 9], 1),
([1, 2, 2], 1),
([1, 9, 2], 1),
([1, 0, 5, 0, 7], 1),
([1, 0, 6, 0, 7], 1),
([1, 0, 0, 0, 1, 0, 0, 0, 6], 1),
([1, 0, 0, 0, 10, 0, 0, 0, 6], 1)])
config.setup('GF_FACTOR_METHOD', 'berlekamp')
assert gf_factor(f, p, ZZ) == g
config.setup('GF_FACTOR_METHOD', 'zassenhaus')
assert gf_factor(f, p, ZZ) == g
config.setup('GF_FACTOR_METHOD', 'shoup')
assert gf_factor(f, p, ZZ) == g
f, p = gf_from_dict({63: ZZ(8), 0: ZZ(5)}, 11, ZZ), 11
g = (8, [([1, 7], 1),
([1, 4, 5], 1),
([1, 6, 8, 2], 1),
([1, 9, 9, 2], 1),
([1, 0, 0, 9, 0, 0, 4], 1),
([1, 2, 0, 8, 4, 6, 4], 1),
([1, 2, 3, 8, 0, 6, 4], 1),
([1, 2, 6, 0, 8, 4, 4], 1),
([1, 3, 3, 1, 6, 8, 4], 1),
([1, 5, 6, 0, 8, 6, 4], 1),
([1, 6, 2, 7, 9, 8, 4], 1),
([1, 10, 4, 7, 10, 7, 4], 1),
([1, 10, 10, 1, 4, 9, 4], 1)])
config.setup('GF_FACTOR_METHOD', 'berlekamp')
assert gf_factor(f, p, ZZ) == g
config.setup('GF_FACTOR_METHOD', 'zassenhaus')
assert gf_factor(f, p, ZZ) == g
config.setup('GF_FACTOR_METHOD', 'shoup')
assert gf_factor(f, p, ZZ) == g
# Gathen polynomials: x**n + x + 1 (mod p > 2**n * pi)
p = ZZ(nextprime(int((2**15 * pi).evalf())))
f = gf_from_dict({15: 1, 1: 1, 0: 1}, p, ZZ)
assert gf_sqf_p(f, p, ZZ) is True
g = (1, [([1, 22730, 68144], 1),
([1, 81553, 77449, 86810, 4724], 1),
([1, 86276, 56779, 14859, 31575], 1),
([1, 15347, 95022, 84569, 94508, 92335], 1)])
config.setup('GF_FACTOR_METHOD', 'zassenhaus')
assert gf_factor(f, p, ZZ) == g
config.setup('GF_FACTOR_METHOD', 'shoup')
assert gf_factor(f, p, ZZ) == g
g = (1, [[1, 22730, 68144],
[1, 81553, 77449, 86810, 4724],
[1, 86276, 56779, 14859, 31575],
[1, 15347, 95022, 84569, 94508, 92335]])
config.setup('GF_FACTOR_METHOD', 'zassenhaus')
assert gf_factor_sqf(f, p, ZZ) == g
config.setup('GF_FACTOR_METHOD', 'shoup')
assert gf_factor_sqf(f, p, ZZ) == g
# Shoup polynomials: f = a_0 x**n + a_1 x**(n-1) + ... + a_n
# (mod p > 2**(n-2) * pi), where a_n = a_{n-1}**2 + 1, a_0 = 1
p = ZZ(nextprime(int((2**4 * pi).evalf())))
f = ZZ.map([1, 2, 5, 26, 41, 39, 38])
assert gf_sqf_p(f, p, ZZ) is True
g = (1, [([1, 44, 26], 1),
([1, 11, 25, 18, 30], 1)])
config.setup('GF_FACTOR_METHOD', 'zassenhaus')
assert gf_factor(f, p, ZZ) == g
config.setup('GF_FACTOR_METHOD', 'shoup')
assert gf_factor(f, p, ZZ) == g
g = (1, [[1, 44, 26],
[1, 11, 25, 18, 30]])
config.setup('GF_FACTOR_METHOD', 'zassenhaus')
assert gf_factor_sqf(f, p, ZZ) == g
config.setup('GF_FACTOR_METHOD', 'shoup')
assert gf_factor_sqf(f, p, ZZ) == g
config.setup('GF_FACTOR_METHOD', 'other')
raises(KeyError, lambda: gf_factor([1, 1], 11, ZZ))
config.setup('GF_FACTOR_METHOD')
def test_gf_csolve():
assert gf_value([1, 7, 2, 4], 11) == 2204
assert linear_congruence(4, 3, 5) == [2]
assert linear_congruence(0, 3, 5) == []
assert linear_congruence(6, 1, 4) == []
assert linear_congruence(0, 5, 5) == [0, 1, 2, 3, 4]
assert linear_congruence(3, 12, 15) == [4, 9, 14]
assert linear_congruence(6, 0, 18) == [0, 3, 6, 9, 12, 15]
# _csolve_prime_las_vegas
assert _csolve_prime_las_vegas([2, 3, 1], 5) == [2, 4]
assert _csolve_prime_las_vegas([2, 0, 1], 5) == []
from sympy.ntheory import primerange
for p in primerange(2, 100):
# f = x**(p-1) - 1
f = gf_sub_ground(gf_pow([1, 0], p - 1, p, ZZ), 1, p, ZZ)
assert _csolve_prime_las_vegas(f, p) == list(range(1, p))
# with power = 1
assert csolve_prime([1, 3, 2, 17], 7) == [3]
assert csolve_prime([1, 3, 1, 5], 5) == [0, 1]
assert csolve_prime([3, 6, 9, 3], 3) == [0, 1, 2]
# with power > 1
assert csolve_prime(
[1, 1, 223], 3, 4) == [4, 13, 22, 31, 40, 49, 58, 67, 76]
assert csolve_prime([3, 5, 2, 25], 5, 3) == [16, 50, 99]
assert csolve_prime([3, 2, 2, 49], 7, 3) == [147, 190, 234]
assert gf_csolve([1, 1, 7], 189) == [13, 49, 76, 112, 139, 175]
assert gf_csolve([1, 3, 4, 1, 30], 60) == [10, 30]
assert gf_csolve([1, 1, 7], 15) == []

View File

@ -0,0 +1,533 @@
"""Tests for Groebner bases. """
from sympy.polys.groebnertools import (
groebner, sig, sig_key,
lbp, lbp_key, critical_pair,
cp_key, is_rewritable_or_comparable,
Sign, Polyn, Num, s_poly, f5_reduce,
groebner_lcm, groebner_gcd, is_groebner,
is_reduced
)
from sympy.polys.fglmtools import _representing_matrices
from sympy.polys.orderings import lex, grlex
from sympy.polys.rings import ring, xring
from sympy.polys.domains import ZZ, QQ
from sympy.testing.pytest import slow
from sympy.polys import polyconfig as config
def _do_test_groebner():
R, x,y = ring("x,y", QQ, lex)
f = x**2 + 2*x*y**2
g = x*y + 2*y**3 - 1
assert groebner([f, g], R) == [x, y**3 - QQ(1,2)]
R, y,x = ring("y,x", QQ, lex)
f = 2*x**2*y + y**2
g = 2*x**3 + x*y - 1
assert groebner([f, g], R) == [y, x**3 - QQ(1,2)]
R, x,y,z = ring("x,y,z", QQ, lex)
f = x - z**2
g = y - z**3
assert groebner([f, g], R) == [f, g]
R, x,y = ring("x,y", QQ, grlex)
f = x**3 - 2*x*y
g = x**2*y + x - 2*y**2
assert groebner([f, g], R) == [x**2, x*y, -QQ(1,2)*x + y**2]
R, x,y,z = ring("x,y,z", QQ, lex)
f = -x**2 + y
g = -x**3 + z
assert groebner([f, g], R) == [x**2 - y, x*y - z, x*z - y**2, y**3 - z**2]
R, x,y,z = ring("x,y,z", QQ, grlex)
f = -x**2 + y
g = -x**3 + z
assert groebner([f, g], R) == [y**3 - z**2, x**2 - y, x*y - z, x*z - y**2]
R, x,y,z = ring("x,y,z", QQ, lex)
f = -x**2 + z
g = -x**3 + y
assert groebner([f, g], R) == [x**2 - z, x*y - z**2, x*z - y, y**2 - z**3]
R, x,y,z = ring("x,y,z", QQ, grlex)
f = -x**2 + z
g = -x**3 + y
assert groebner([f, g], R) == [-y**2 + z**3, x**2 - z, x*y - z**2, x*z - y]
R, x,y,z = ring("x,y,z", QQ, lex)
f = x - y**2
g = -y**3 + z
assert groebner([f, g], R) == [x - y**2, y**3 - z]
R, x,y,z = ring("x,y,z", QQ, grlex)
f = x - y**2
g = -y**3 + z
assert groebner([f, g], R) == [x**2 - y*z, x*y - z, -x + y**2]
R, x,y,z = ring("x,y,z", QQ, lex)
f = x - z**2
g = y - z**3
assert groebner([f, g], R) == [x - z**2, y - z**3]
R, x,y,z = ring("x,y,z", QQ, grlex)
f = x - z**2
g = y - z**3
assert groebner([f, g], R) == [x**2 - y*z, x*z - y, -x + z**2]
R, x,y,z = ring("x,y,z", QQ, lex)
f = -y**2 + z
g = x - y**3
assert groebner([f, g], R) == [x - y*z, y**2 - z]
R, x,y,z = ring("x,y,z", QQ, grlex)
f = -y**2 + z
g = x - y**3
assert groebner([f, g], R) == [-x**2 + z**3, x*y - z**2, y**2 - z, -x + y*z]
R, x,y,z = ring("x,y,z", QQ, lex)
f = y - z**2
g = x - z**3
assert groebner([f, g], R) == [x - z**3, y - z**2]
R, x,y,z = ring("x,y,z", QQ, grlex)
f = y - z**2
g = x - z**3
assert groebner([f, g], R) == [-x**2 + y**3, x*z - y**2, -x + y*z, -y + z**2]
R, x,y,z = ring("x,y,z", QQ, lex)
f = 4*x**2*y**2 + 4*x*y + 1
g = x**2 + y**2 - 1
assert groebner([f, g], R) == [
x - 4*y**7 + 8*y**5 - 7*y**3 + 3*y,
y**8 - 2*y**6 + QQ(3,2)*y**4 - QQ(1,2)*y**2 + QQ(1,16),
]
def test_groebner_buchberger():
with config.using(groebner='buchberger'):
_do_test_groebner()
def test_groebner_f5b():
with config.using(groebner='f5b'):
_do_test_groebner()
def _do_test_benchmark_minpoly():
R, x,y,z = ring("x,y,z", QQ, lex)
F = [x**3 + x + 1, y**2 + y + 1, (x + y) * z - (x**2 + y)]
G = [x + QQ(155,2067)*z**5 - QQ(355,689)*z**4 + QQ(6062,2067)*z**3 - QQ(3687,689)*z**2 + QQ(6878,2067)*z - QQ(25,53),
y + QQ(4,53)*z**5 - QQ(91,159)*z**4 + QQ(523,159)*z**3 - QQ(387,53)*z**2 + QQ(1043,159)*z - QQ(308,159),
z**6 - 7*z**5 + 41*z**4 - 82*z**3 + 89*z**2 - 46*z + 13]
assert groebner(F, R) == G
def test_benchmark_minpoly_buchberger():
with config.using(groebner='buchberger'):
_do_test_benchmark_minpoly()
def test_benchmark_minpoly_f5b():
with config.using(groebner='f5b'):
_do_test_benchmark_minpoly()
def test_benchmark_coloring():
V = range(1, 12 + 1)
E = [(1, 2), (2, 3), (1, 4), (1, 6), (1, 12), (2, 5), (2, 7), (3, 8), (3, 10),
(4, 11), (4, 9), (5, 6), (6, 7), (7, 8), (8, 9), (9, 10), (10, 11),
(11, 12), (5, 12), (5, 9), (6, 10), (7, 11), (8, 12), (3, 4)]
R, V = xring([ "x%d" % v for v in V ], QQ, lex)
E = [(V[i - 1], V[j - 1]) for i, j in E]
x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12 = V
I3 = [x**3 - 1 for x in V]
Ig = [x**2 + x*y + y**2 for x, y in E]
I = I3 + Ig
assert groebner(I[:-1], R) == [
x1 + x11 + x12,
x2 - x11,
x3 - x12,
x4 - x12,
x5 + x11 + x12,
x6 - x11,
x7 - x12,
x8 + x11 + x12,
x9 - x11,
x10 + x11 + x12,
x11**2 + x11*x12 + x12**2,
x12**3 - 1,
]
assert groebner(I, R) == [1]
def _do_test_benchmark_katsura_3():
R, x0,x1,x2 = ring("x:3", ZZ, lex)
I = [x0 + 2*x1 + 2*x2 - 1,
x0**2 + 2*x1**2 + 2*x2**2 - x0,
2*x0*x1 + 2*x1*x2 - x1]
assert groebner(I, R) == [
-7 + 7*x0 + 8*x2 + 158*x2**2 - 420*x2**3,
7*x1 + 3*x2 - 79*x2**2 + 210*x2**3,
x2 + x2**2 - 40*x2**3 + 84*x2**4,
]
R, x0,x1,x2 = ring("x:3", ZZ, grlex)
I = [ i.set_ring(R) for i in I ]
assert groebner(I, R) == [
7*x1 + 3*x2 - 79*x2**2 + 210*x2**3,
-x1 + x2 - 3*x2**2 + 5*x1**2,
-x1 - 4*x2 + 10*x1*x2 + 12*x2**2,
-1 + x0 + 2*x1 + 2*x2,
]
def test_benchmark_katsura3_buchberger():
with config.using(groebner='buchberger'):
_do_test_benchmark_katsura_3()
def test_benchmark_katsura3_f5b():
with config.using(groebner='f5b'):
_do_test_benchmark_katsura_3()
def _do_test_benchmark_katsura_4():
R, x0,x1,x2,x3 = ring("x:4", ZZ, lex)
I = [x0 + 2*x1 + 2*x2 + 2*x3 - 1,
x0**2 + 2*x1**2 + 2*x2**2 + 2*x3**2 - x0,
2*x0*x1 + 2*x1*x2 + 2*x2*x3 - x1,
x1**2 + 2*x0*x2 + 2*x1*x3 - x2]
assert groebner(I, R) == [
5913075*x0 - 159690237696*x3**7 + 31246269696*x3**6 + 27439610544*x3**5 - 6475723368*x3**4 - 838935856*x3**3 + 275119624*x3**2 + 4884038*x3 - 5913075,
1971025*x1 - 97197721632*x3**7 + 73975630752*x3**6 - 12121915032*x3**5 - 2760941496*x3**4 + 814792828*x3**3 - 1678512*x3**2 - 9158924*x3,
5913075*x2 + 371438283744*x3**7 - 237550027104*x3**6 + 22645939824*x3**5 + 11520686172*x3**4 - 2024910556*x3**3 - 132524276*x3**2 + 30947828*x3,
128304*x3**8 - 93312*x3**7 + 15552*x3**6 + 3144*x3**5 -
1120*x3**4 + 36*x3**3 + 15*x3**2 - x3,
]
R, x0,x1,x2,x3 = ring("x:4", ZZ, grlex)
I = [ i.set_ring(R) for i in I ]
assert groebner(I, R) == [
393*x1 - 4662*x2**2 + 4462*x2*x3 - 59*x2 + 224532*x3**4 - 91224*x3**3 - 678*x3**2 + 2046*x3,
-x1 + 196*x2**3 - 21*x2**2 + 60*x2*x3 - 18*x2 - 168*x3**3 + 83*x3**2 - 9*x3,
-6*x1 + 1134*x2**2*x3 - 189*x2**2 - 466*x2*x3 + 32*x2 - 630*x3**3 + 57*x3**2 + 51*x3,
33*x1 + 63*x2**2 + 2268*x2*x3**2 - 188*x2*x3 + 34*x2 + 2520*x3**3 - 849*x3**2 + 3*x3,
7*x1**2 - x1 - 7*x2**2 - 24*x2*x3 + 3*x2 - 15*x3**2 + 5*x3,
14*x1*x2 - x1 + 14*x2**2 + 18*x2*x3 - 4*x2 + 6*x3**2 - 2*x3,
14*x1*x3 - x1 + 7*x2**2 + 32*x2*x3 - 4*x2 + 27*x3**2 - 9*x3,
x0 + 2*x1 + 2*x2 + 2*x3 - 1,
]
def test_benchmark_kastura_4_buchberger():
with config.using(groebner='buchberger'):
_do_test_benchmark_katsura_4()
def test_benchmark_kastura_4_f5b():
with config.using(groebner='f5b'):
_do_test_benchmark_katsura_4()
def _do_test_benchmark_czichowski():
R, x,t = ring("x,t", ZZ, lex)
I = [9*x**8 + 36*x**7 - 32*x**6 - 252*x**5 - 78*x**4 + 468*x**3 + 288*x**2 - 108*x + 9,
(-72 - 72*t)*x**7 + (-256 - 252*t)*x**6 + (192 + 192*t)*x**5 + (1280 + 1260*t)*x**4 + (312 + 312*t)*x**3 + (-404*t)*x**2 + (-576 - 576*t)*x + 96 + 108*t]
assert groebner(I, R) == [
3725588592068034903797967297424801242396746870413359539263038139343329273586196480000*x -
160420835591776763325581422211936558925462474417709511019228211783493866564923546661604487873*t**7 -
1406108495478033395547109582678806497509499966197028487131115097902188374051595011248311352864*t**6 -
5241326875850889518164640374668786338033653548841427557880599579174438246266263602956254030352*t**5 -
10758917262823299139373269714910672770004760114329943852726887632013485035262879510837043892416*t**4 -
13119383576444715672578819534846747735372132018341964647712009275306635391456880068261130581248*t**3 -
9491412317016197146080450036267011389660653495578680036574753839055748080962214787557853941760*t**2 -
3767520915562795326943800040277726397326609797172964377014046018280260848046603967211258368000*t -
632314652371226552085897259159210286886724229880266931574701654721512325555116066073245696000,
610733380717522355121*t**8 +
6243748742141230639968*t**7 +
27761407182086143225024*t**6 +
70066148869420956398592*t**5 +
109701225644313784229376*t**4 +
109009005495588442152960*t**3 +
67072101084384786432000*t**2 +
23339979742629593088000*t +
3513592776846090240000,
]
R, x,t = ring("x,t", ZZ, grlex)
I = [ i.set_ring(R) for i in I ]
assert groebner(I, R) == [
16996618586000601590732959134095643086442*t**3*x -
32936701459297092865176560282688198064839*t**3 +
78592411049800639484139414821529525782364*t**2*x -
120753953358671750165454009478961405619916*t**2 +
120988399875140799712152158915653654637280*t*x -
144576390266626470824138354942076045758736*t +
60017634054270480831259316163620768960*x**2 +
61976058033571109604821862786675242894400*x -
56266268491293858791834120380427754600960,
576689018321912327136790519059646508441672750656050290242749*t**4 +
2326673103677477425562248201573604572527893938459296513327336*t**3 +
110743790416688497407826310048520299245819959064297990236000*t**2*x +
3308669114229100853338245486174247752683277925010505284338016*t**2 +
323150205645687941261103426627818874426097912639158572428800*t*x +
1914335199925152083917206349978534224695445819017286960055680*t +
861662882561803377986838989464278045397192862768588480000*x**2 +
235296483281783440197069672204341465480107019878814196672000*x +
361850798943225141738895123621685122544503614946436727532800,
-117584925286448670474763406733005510014188341867*t**3 +
68566565876066068463853874568722190223721653044*t**2*x -
435970731348366266878180788833437896139920683940*t**2 +
196297602447033751918195568051376792491869233408*t*x -
525011527660010557871349062870980202067479780112*t +
517905853447200553360289634770487684447317120*x**3 +
569119014870778921949288951688799397569321920*x**2 +
138877356748142786670127389526667463202210102080*x -
205109210539096046121625447192779783475018619520,
-3725142681462373002731339445216700112264527*t**3 +
583711207282060457652784180668273817487940*t**2*x -
12381382393074485225164741437227437062814908*t**2 +
151081054097783125250959636747516827435040*t*x**2 +
1814103857455163948531448580501928933873280*t*x -
13353115629395094645843682074271212731433648*t +
236415091385250007660606958022544983766080*x**2 +
1390443278862804663728298060085399578417600*x -
4716885828494075789338754454248931750698880,
]
# NOTE: This is very slow (> 2 minutes on 3.4 GHz) without GMPY
@slow
def test_benchmark_czichowski_buchberger():
with config.using(groebner='buchberger'):
_do_test_benchmark_czichowski()
def test_benchmark_czichowski_f5b():
with config.using(groebner='f5b'):
_do_test_benchmark_czichowski()
def _do_test_benchmark_cyclic_4():
R, a,b,c,d = ring("a,b,c,d", ZZ, lex)
I = [a + b + c + d,
a*b + a*d + b*c + b*d,
a*b*c + a*b*d + a*c*d + b*c*d,
a*b*c*d - 1]
assert groebner(I, R) == [
4*a + 3*d**9 - 4*d**5 - 3*d,
4*b + 4*c - 3*d**9 + 4*d**5 + 7*d,
4*c**2 + 3*d**10 - 4*d**6 - 3*d**2,
4*c*d**4 + 4*c - d**9 + 4*d**5 + 5*d, d**12 - d**8 - d**4 + 1
]
R, a,b,c,d = ring("a,b,c,d", ZZ, grlex)
I = [ i.set_ring(R) for i in I ]
assert groebner(I, R) == [
3*b*c - c**2 + d**6 - 3*d**2,
-b + 3*c**2*d**3 - c - d**5 - 4*d,
-b + 3*c*d**4 + 2*c + 2*d**5 + 2*d,
c**4 + 2*c**2*d**2 - d**4 - 2,
c**3*d + c*d**3 + d**4 + 1,
b*c**2 - c**3 - c**2*d - 2*c*d**2 - d**3,
b**2 - c**2, b*d + c**2 + c*d + d**2,
a + b + c + d
]
def test_benchmark_cyclic_4_buchberger():
with config.using(groebner='buchberger'):
_do_test_benchmark_cyclic_4()
def test_benchmark_cyclic_4_f5b():
with config.using(groebner='f5b'):
_do_test_benchmark_cyclic_4()
def test_sig_key():
s1 = sig((0,) * 3, 2)
s2 = sig((1,) * 3, 4)
s3 = sig((2,) * 3, 2)
assert sig_key(s1, lex) > sig_key(s2, lex)
assert sig_key(s2, lex) < sig_key(s3, lex)
def test_lbp_key():
R, x,y,z,t = ring("x,y,z,t", ZZ, lex)
p1 = lbp(sig((0,) * 4, 3), R.zero, 12)
p2 = lbp(sig((0,) * 4, 4), R.zero, 13)
p3 = lbp(sig((0,) * 4, 4), R.zero, 12)
assert lbp_key(p1) > lbp_key(p2)
assert lbp_key(p2) < lbp_key(p3)
def test_critical_pair():
# from cyclic4 with grlex
R, x,y,z,t = ring("x,y,z,t", QQ, grlex)
p1 = (((0, 0, 0, 0), 4), y*z*t**2 + z**2*t**2 - t**4 - 1, 4)
q1 = (((0, 0, 0, 0), 2), -y**2 - y*t - z*t - t**2, 2)
p2 = (((0, 0, 0, 2), 3), z**3*t**2 + z**2*t**3 - z - t, 5)
q2 = (((0, 0, 2, 2), 2), y*z + z*t**5 + z*t + t**6, 13)
assert critical_pair(p1, q1, R) == (
((0, 0, 1, 2), 2), ((0, 0, 1, 2), QQ(-1, 1)), (((0, 0, 0, 0), 2), -y**2 - y*t - z*t - t**2, 2),
((0, 1, 0, 0), 4), ((0, 1, 0, 0), QQ(1, 1)), (((0, 0, 0, 0), 4), y*z*t**2 + z**2*t**2 - t**4 - 1, 4)
)
assert critical_pair(p2, q2, R) == (
((0, 0, 4, 2), 2), ((0, 0, 2, 0), QQ(1, 1)), (((0, 0, 2, 2), 2), y*z + z*t**5 + z*t + t**6, 13),
((0, 0, 0, 5), 3), ((0, 0, 0, 3), QQ(1, 1)), (((0, 0, 0, 2), 3), z**3*t**2 + z**2*t**3 - z - t, 5)
)
def test_cp_key():
# from cyclic4 with grlex
R, x,y,z,t = ring("x,y,z,t", QQ, grlex)
p1 = (((0, 0, 0, 0), 4), y*z*t**2 + z**2*t**2 - t**4 - 1, 4)
q1 = (((0, 0, 0, 0), 2), -y**2 - y*t - z*t - t**2, 2)
p2 = (((0, 0, 0, 2), 3), z**3*t**2 + z**2*t**3 - z - t, 5)
q2 = (((0, 0, 2, 2), 2), y*z + z*t**5 + z*t + t**6, 13)
cp1 = critical_pair(p1, q1, R)
cp2 = critical_pair(p2, q2, R)
assert cp_key(cp1, R) < cp_key(cp2, R)
cp1 = critical_pair(p1, p2, R)
cp2 = critical_pair(q1, q2, R)
assert cp_key(cp1, R) < cp_key(cp2, R)
def test_is_rewritable_or_comparable():
# from katsura4 with grlex
R, x,y,z,t = ring("x,y,z,t", QQ, grlex)
p = lbp(sig((0, 0, 2, 1), 2), R.zero, 2)
B = [lbp(sig((0, 0, 0, 1), 2), QQ(2,45)*y**2 + QQ(1,5)*y*z + QQ(5,63)*y*t + z**2*t + QQ(4,45)*z**2 + QQ(76,35)*z*t**2 - QQ(32,105)*z*t + QQ(13,7)*t**3 - QQ(13,21)*t**2, 6)]
# rewritable:
assert is_rewritable_or_comparable(Sign(p), Num(p), B) is True
p = lbp(sig((0, 1, 1, 0), 2), R.zero, 7)
B = [lbp(sig((0, 0, 0, 0), 3), QQ(10,3)*y*z + QQ(4,3)*y*t - QQ(1,3)*y + 4*z**2 + QQ(22,3)*z*t - QQ(4,3)*z + 4*t**2 - QQ(4,3)*t, 3)]
# comparable:
assert is_rewritable_or_comparable(Sign(p), Num(p), B) is True
def test_f5_reduce():
# katsura3 with lex
R, x,y,z = ring("x,y,z", QQ, lex)
F = [(((0, 0, 0), 1), x + 2*y + 2*z - 1, 1),
(((0, 0, 0), 2), 6*y**2 + 8*y*z - 2*y + 6*z**2 - 2*z, 2),
(((0, 0, 0), 3), QQ(10,3)*y*z - QQ(1,3)*y + 4*z**2 - QQ(4,3)*z, 3),
(((0, 0, 1), 2), y + 30*z**3 - QQ(79,7)*z**2 + QQ(3,7)*z, 4),
(((0, 0, 2), 2), z**4 - QQ(10,21)*z**3 + QQ(1,84)*z**2 + QQ(1,84)*z, 5)]
cp = critical_pair(F[0], F[1], R)
s = s_poly(cp)
assert f5_reduce(s, F) == (((0, 2, 0), 1), R.zero, 1)
s = lbp(sig(Sign(s)[0], 100), Polyn(s), Num(s))
assert f5_reduce(s, F) == s
def test_representing_matrices():
R, x,y = ring("x,y", QQ, grlex)
basis = [(0, 0), (0, 1), (1, 0), (1, 1)]
F = [x**2 - x - 3*y + 1, -2*x + y**2 + y - 1]
assert _representing_matrices(basis, F, R) == [
[[QQ(0, 1), QQ(0, 1),-QQ(1, 1), QQ(3, 1)],
[QQ(0, 1), QQ(0, 1), QQ(3, 1),-QQ(4, 1)],
[QQ(1, 1), QQ(0, 1), QQ(1, 1), QQ(6, 1)],
[QQ(0, 1), QQ(1, 1), QQ(0, 1), QQ(1, 1)]],
[[QQ(0, 1), QQ(1, 1), QQ(0, 1),-QQ(2, 1)],
[QQ(1, 1),-QQ(1, 1), QQ(0, 1), QQ(6, 1)],
[QQ(0, 1), QQ(2, 1), QQ(0, 1), QQ(3, 1)],
[QQ(0, 1), QQ(0, 1), QQ(1, 1),-QQ(1, 1)]]]
def test_groebner_lcm():
R, x,y,z = ring("x,y,z", ZZ)
assert groebner_lcm(x**2 - y**2, x - y) == x**2 - y**2
assert groebner_lcm(2*x**2 - 2*y**2, 2*x - 2*y) == 2*x**2 - 2*y**2
R, x,y,z = ring("x,y,z", QQ)
assert groebner_lcm(x**2 - y**2, x - y) == x**2 - y**2
assert groebner_lcm(2*x**2 - 2*y**2, 2*x - 2*y) == 2*x**2 - 2*y**2
R, x,y = ring("x,y", ZZ)
assert groebner_lcm(x**2*y, x*y**2) == x**2*y**2
f = 2*x*y**5 - 3*x*y**4 - 2*x*y**3 + 3*x*y**2
g = y**5 - 2*y**3 + y
h = 2*x*y**7 - 3*x*y**6 - 4*x*y**5 + 6*x*y**4 + 2*x*y**3 - 3*x*y**2
assert groebner_lcm(f, g) == h
f = x**3 - 3*x**2*y - 9*x*y**2 - 5*y**3
g = x**4 + 6*x**3*y + 12*x**2*y**2 + 10*x*y**3 + 3*y**4
h = x**5 + x**4*y - 18*x**3*y**2 - 50*x**2*y**3 - 47*x*y**4 - 15*y**5
assert groebner_lcm(f, g) == h
def test_groebner_gcd():
R, x,y,z = ring("x,y,z", ZZ)
assert groebner_gcd(x**2 - y**2, x - y) == x - y
assert groebner_gcd(2*x**2 - 2*y**2, 2*x - 2*y) == 2*x - 2*y
R, x,y,z = ring("x,y,z", QQ)
assert groebner_gcd(x**2 - y**2, x - y) == x - y
assert groebner_gcd(2*x**2 - 2*y**2, 2*x - 2*y) == x - y
def test_is_groebner():
R, x,y = ring("x,y", QQ, grlex)
valid_groebner = [x**2, x*y, -QQ(1,2)*x + y**2]
invalid_groebner = [x**3, x*y, -QQ(1,2)*x + y**2]
assert is_groebner(valid_groebner, R) is True
assert is_groebner(invalid_groebner, R) is False
def test_is_reduced():
R, x, y = ring("x,y", QQ, lex)
f = x**2 + 2*x*y**2
g = x*y + 2*y**3 - 1
assert is_reduced([f, g], R) == False
G = groebner([f, g], R)
assert is_reduced(G, R) == True

View File

@ -0,0 +1,152 @@
from sympy.polys.rings import ring
from sympy.polys.domains import ZZ
from sympy.polys.heuristicgcd import heugcd
def test_heugcd_univariate_integers():
R, x = ring("x", ZZ)
f = x**4 + 8*x**3 + 21*x**2 + 22*x + 8
g = x**3 + 6*x**2 + 11*x + 6
h = x**2 + 3*x + 2
cff = x**2 + 5*x + 4
cfg = x + 3
assert heugcd(f, g) == (h, cff, cfg)
f = x**4 - 4
g = x**4 + 4*x**2 + 4
h = x**2 + 2
cff = x**2 - 2
cfg = x**2 + 2
assert heugcd(f, g) == (h, cff, cfg)
f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
h = 1
cff = f
cfg = g
assert heugcd(f, g) == (h, cff, cfg)
f = - 352518131239247345597970242177235495263669787845475025293906825864749649589178600387510272*x**49 \
+ 46818041807522713962450042363465092040687472354933295397472942006618953623327997952*x**42 \
+ 378182690892293941192071663536490788434899030680411695933646320291525827756032*x**35 \
+ 112806468807371824947796775491032386836656074179286744191026149539708928*x**28 \
- 12278371209708240950316872681744825481125965781519138077173235712*x**21 \
+ 289127344604779611146960547954288113529690984687482920704*x**14 \
+ 19007977035740498977629742919480623972236450681*x**7 \
+ 311973482284542371301330321821976049
g = 365431878023781158602430064717380211405897160759702125019136*x**21 \
+ 197599133478719444145775798221171663643171734081650688*x**14 \
- 9504116979659010018253915765478924103928886144*x**7 \
- 311973482284542371301330321821976049
# TODO: assert heugcd(f, f.diff(x))[0] == g
f = 1317378933230047068160*x + 2945748836994210856960
g = 120352542776360960*x + 269116466014453760
h = 120352542776360960*x + 269116466014453760
cff = 10946
cfg = 1
assert heugcd(f, g) == (h, cff, cfg)
def test_heugcd_multivariate_integers():
R, x, y = ring("x,y", ZZ)
f, g = 2*x**2 + 4*x + 2, x + 1
assert heugcd(f, g) == (x + 1, 2*x + 2, 1)
f, g = x + 1, 2*x**2 + 4*x + 2
assert heugcd(f, g) == (x + 1, 1, 2*x + 2)
R, x, y, z, u = ring("x,y,z,u", ZZ)
f, g = u**2 + 2*u + 1, 2*u + 2
assert heugcd(f, g) == (u + 1, u + 1, 2)
f, g = z**2*u**2 + 2*z**2*u + z**2 + z*u + z, u**2 + 2*u + 1
h, cff, cfg = u + 1, z**2*u + z**2 + z, u + 1
assert heugcd(f, g) == (h, cff, cfg)
assert heugcd(g, f) == (h, cfg, cff)
R, x, y, z = ring("x,y,z", ZZ)
f, g, h = R.fateman_poly_F_1()
H, cff, cfg = heugcd(f, g)
assert H == h and H*cff == f and H*cfg == g
R, x, y, z, u, v = ring("x,y,z,u,v", ZZ)
f, g, h = R.fateman_poly_F_1()
H, cff, cfg = heugcd(f, g)
assert H == h and H*cff == f and H*cfg == g
R, x, y, z, u, v, a, b = ring("x,y,z,u,v,a,b", ZZ)
f, g, h = R.fateman_poly_F_1()
H, cff, cfg = heugcd(f, g)
assert H == h and H*cff == f and H*cfg == g
R, x, y, z, u, v, a, b, c, d = ring("x,y,z,u,v,a,b,c,d", ZZ)
f, g, h = R.fateman_poly_F_1()
H, cff, cfg = heugcd(f, g)
assert H == h and H*cff == f and H*cfg == g
R, x, y, z = ring("x,y,z", ZZ)
f, g, h = R.fateman_poly_F_2()
H, cff, cfg = heugcd(f, g)
assert H == h and H*cff == f and H*cfg == g
f, g, h = R.fateman_poly_F_3()
H, cff, cfg = heugcd(f, g)
assert H == h and H*cff == f and H*cfg == g
R, x, y, z, t = ring("x,y,z,t", ZZ)
f, g, h = R.fateman_poly_F_3()
H, cff, cfg = heugcd(f, g)
assert H == h and H*cff == f and H*cfg == g
def test_issue_10996():
R, x, y, z = ring("x,y,z", ZZ)
f = 12*x**6*y**7*z**3 - 3*x**4*y**9*z**3 + 12*x**3*y**5*z**4
g = -48*x**7*y**8*z**3 + 12*x**5*y**10*z**3 - 48*x**5*y**7*z**2 + \
36*x**4*y**7*z - 48*x**4*y**6*z**4 + 12*x**3*y**9*z**2 - 48*x**3*y**4 \
- 9*x**2*y**9*z - 48*x**2*y**5*z**3 + 12*x*y**6 + 36*x*y**5*z**2 - 48*y**2*z
H, cff, cfg = heugcd(f, g)
assert H == 12*x**3*y**4 - 3*x*y**6 + 12*y**2*z
assert H*cff == f and H*cfg == g
def test_issue_25793():
R, x = ring("x", ZZ)
f = x - 4851 # failure starts for values more than 4850
g = f*(2*x + 1)
H, cff, cfg = R.dup_zz_heu_gcd(f, g)
assert H == f
# needs a test for dmp, too, that fails in master before this change

View File

@ -0,0 +1,36 @@
from hypothesis import given
from hypothesis import strategies as st
from sympy.abc import x
from sympy.polys.polytools import Poly
def polys(*, nonzero=False, domain="ZZ"):
# This is a simple strategy, but sufficient the tests below
elems = {"ZZ": st.integers(), "QQ": st.fractions()}
coeff_st = st.lists(elems[domain])
if nonzero:
coeff_st = coeff_st.filter(any)
return st.builds(Poly, coeff_st, st.just(x), domain=st.just(domain))
@given(f=polys(), g=polys(), r=polys())
def test_gcd_hypothesis(f, g, r):
gcd_1 = f.gcd(g)
gcd_2 = g.gcd(f)
assert gcd_1 == gcd_2
# multiply by r
gcd_3 = g.gcd(f + r * g)
assert gcd_1 == gcd_3
@given(f_z=polys(), g_z=polys(nonzero=True))
def test_poly_hypothesis_integers(f_z, g_z):
remainder_z = f_z.rem(g_z)
assert g_z.degree() >= remainder_z.degree() or remainder_z.degree() == 0
@given(f_q=polys(domain="QQ"), g_q=polys(nonzero=True, domain="QQ"))
def test_poly_hypothesis_rationals(f_q, g_q):
remainder_q = f_q.rem(g_q)
assert g_q.degree() >= remainder_q.degree() or remainder_q.degree() == 0

View File

@ -0,0 +1,39 @@
"""Tests for functions that inject symbols into the global namespace. """
from sympy.polys.rings import vring
from sympy.polys.fields import vfield
from sympy.polys.domains import QQ
def test_vring():
ns = {'vring':vring, 'QQ':QQ}
exec('R = vring("r", QQ)', ns)
exec('assert r == R.gens[0]', ns)
exec('R = vring("rb rbb rcc rzz _rx", QQ)', ns)
exec('assert rb == R.gens[0]', ns)
exec('assert rbb == R.gens[1]', ns)
exec('assert rcc == R.gens[2]', ns)
exec('assert rzz == R.gens[3]', ns)
exec('assert _rx == R.gens[4]', ns)
exec('R = vring(["rd", "re", "rfg"], QQ)', ns)
exec('assert rd == R.gens[0]', ns)
exec('assert re == R.gens[1]', ns)
exec('assert rfg == R.gens[2]', ns)
def test_vfield():
ns = {'vfield':vfield, 'QQ':QQ}
exec('F = vfield("f", QQ)', ns)
exec('assert f == F.gens[0]', ns)
exec('F = vfield("fb fbb fcc fzz _fx", QQ)', ns)
exec('assert fb == F.gens[0]', ns)
exec('assert fbb == F.gens[1]', ns)
exec('assert fcc == F.gens[2]', ns)
exec('assert fzz == F.gens[3]', ns)
exec('assert _fx == F.gens[4]', ns)
exec('F = vfield(["fd", "fe", "ffg"], QQ)', ns)
exec('assert fd == F.gens[0]', ns)
exec('assert fe == F.gens[1]', ns)
exec('assert ffg == F.gens[2]', ns)

View File

@ -0,0 +1,325 @@
from sympy.polys.rings import ring
from sympy.polys.domains import ZZ, QQ, AlgebraicField
from sympy.polys.modulargcd import (
modgcd_univariate,
modgcd_bivariate,
_chinese_remainder_reconstruction_multivariate,
modgcd_multivariate,
_to_ZZ_poly,
_to_ANP_poly,
func_field_modgcd,
_func_field_modgcd_m)
from sympy.functions.elementary.miscellaneous import sqrt
def test_modgcd_univariate_integers():
R, x = ring("x", ZZ)
f, g = R.zero, R.zero
assert modgcd_univariate(f, g) == (0, 0, 0)
f, g = R.zero, x
assert modgcd_univariate(f, g) == (x, 0, 1)
assert modgcd_univariate(g, f) == (x, 1, 0)
f, g = R.zero, -x
assert modgcd_univariate(f, g) == (x, 0, -1)
assert modgcd_univariate(g, f) == (x, -1, 0)
f, g = 2*x, R(2)
assert modgcd_univariate(f, g) == (2, x, 1)
f, g = 2*x + 2, 6*x**2 - 6
assert modgcd_univariate(f, g) == (2*x + 2, 1, 3*x - 3)
f = x**4 + 8*x**3 + 21*x**2 + 22*x + 8
g = x**3 + 6*x**2 + 11*x + 6
h = x**2 + 3*x + 2
cff = x**2 + 5*x + 4
cfg = x + 3
assert modgcd_univariate(f, g) == (h, cff, cfg)
f = x**4 - 4
g = x**4 + 4*x**2 + 4
h = x**2 + 2
cff = x**2 - 2
cfg = x**2 + 2
assert modgcd_univariate(f, g) == (h, cff, cfg)
f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
h = 1
cff = f
cfg = g
assert modgcd_univariate(f, g) == (h, cff, cfg)
f = - 352518131239247345597970242177235495263669787845475025293906825864749649589178600387510272*x**49 \
+ 46818041807522713962450042363465092040687472354933295397472942006618953623327997952*x**42 \
+ 378182690892293941192071663536490788434899030680411695933646320291525827756032*x**35 \
+ 112806468807371824947796775491032386836656074179286744191026149539708928*x**28 \
- 12278371209708240950316872681744825481125965781519138077173235712*x**21 \
+ 289127344604779611146960547954288113529690984687482920704*x**14 \
+ 19007977035740498977629742919480623972236450681*x**7 \
+ 311973482284542371301330321821976049
g = 365431878023781158602430064717380211405897160759702125019136*x**21 \
+ 197599133478719444145775798221171663643171734081650688*x**14 \
- 9504116979659010018253915765478924103928886144*x**7 \
- 311973482284542371301330321821976049
assert modgcd_univariate(f, f.diff(x))[0] == g
f = 1317378933230047068160*x + 2945748836994210856960
g = 120352542776360960*x + 269116466014453760
h = 120352542776360960*x + 269116466014453760
cff = 10946
cfg = 1
assert modgcd_univariate(f, g) == (h, cff, cfg)
def test_modgcd_bivariate_integers():
R, x, y = ring("x,y", ZZ)
f, g = R.zero, R.zero
assert modgcd_bivariate(f, g) == (0, 0, 0)
f, g = 2*x, R(2)
assert modgcd_bivariate(f, g) == (2, x, 1)
f, g = x + 2*y, x + y
assert modgcd_bivariate(f, g) == (1, f, g)
f, g = x**2 + 2*x*y + y**2, x**3 + y**3
assert modgcd_bivariate(f, g) == (x + y, x + y, x**2 - x*y + y**2)
f, g = x*y**2 + 2*x*y + x, x*y**3 + x
assert modgcd_bivariate(f, g) == (x*y + x, y + 1, y**2 - y + 1)
f, g = x**2*y**2 + x**2*y + 1, x*y**2 + x*y + 1
assert modgcd_bivariate(f, g) == (1, f, g)
f = 2*x*y**2 + 4*x*y + 2*x + y**2 + 2*y + 1
g = 2*x*y**3 + 2*x + y**3 + 1
assert modgcd_bivariate(f, g) == (2*x*y + 2*x + y + 1, y + 1, y**2 - y + 1)
f, g = 2*x**2 + 4*x + 2, x + 1
assert modgcd_bivariate(f, g) == (x + 1, 2*x + 2, 1)
f, g = x + 1, 2*x**2 + 4*x + 2
assert modgcd_bivariate(f, g) == (x + 1, 1, 2*x + 2)
f = 2*x**2 + 4*x*y - 2*x - 4*y
g = x**2 + x - 2
assert modgcd_bivariate(f, g) == (x - 1, 2*x + 4*y, x + 2)
f = 2*x**2 + 2*x*y - 3*x - 3*y
g = 4*x*y - 2*x + 4*y**2 - 2*y
assert modgcd_bivariate(f, g) == (x + y, 2*x - 3, 4*y - 2)
def test_chinese_remainder():
R, x, y = ring("x, y", ZZ)
p, q = 3, 5
hp = x**3*y - x**2 - 1
hq = -x**3*y - 2*x*y**2 + 2
hpq = _chinese_remainder_reconstruction_multivariate(hp, hq, p, q)
assert hpq.trunc_ground(p) == hp
assert hpq.trunc_ground(q) == hq
T, z = ring("z", R)
p, q = 3, 7
hp = (x*y + 1)*z**2 + x
hq = (x**2 - 3*y)*z + 2
hpq = _chinese_remainder_reconstruction_multivariate(hp, hq, p, q)
assert hpq.trunc_ground(p) == hp
assert hpq.trunc_ground(q) == hq
def test_modgcd_multivariate_integers():
R, x, y = ring("x,y", ZZ)
f, g = R.zero, R.zero
assert modgcd_multivariate(f, g) == (0, 0, 0)
f, g = 2*x**2 + 4*x + 2, x + 1
assert modgcd_multivariate(f, g) == (x + 1, 2*x + 2, 1)
f, g = x + 1, 2*x**2 + 4*x + 2
assert modgcd_multivariate(f, g) == (x + 1, 1, 2*x + 2)
f = 2*x**2 + 2*x*y - 3*x - 3*y
g = 4*x*y - 2*x + 4*y**2 - 2*y
assert modgcd_multivariate(f, g) == (x + y, 2*x - 3, 4*y - 2)
f, g = x*y**2 + 2*x*y + x, x*y**3 + x
assert modgcd_multivariate(f, g) == (x*y + x, y + 1, y**2 - y + 1)
f, g = x**2*y**2 + x**2*y + 1, x*y**2 + x*y + 1
assert modgcd_multivariate(f, g) == (1, f, g)
f = x**4 + 8*x**3 + 21*x**2 + 22*x + 8
g = x**3 + 6*x**2 + 11*x + 6
h = x**2 + 3*x + 2
cff = x**2 + 5*x + 4
cfg = x + 3
assert modgcd_multivariate(f, g) == (h, cff, cfg)
R, x, y, z, u = ring("x,y,z,u", ZZ)
f, g = x + y + z, -x - y - z - u
assert modgcd_multivariate(f, g) == (1, f, g)
f, g = u**2 + 2*u + 1, 2*u + 2
assert modgcd_multivariate(f, g) == (u + 1, u + 1, 2)
f, g = z**2*u**2 + 2*z**2*u + z**2 + z*u + z, u**2 + 2*u + 1
h, cff, cfg = u + 1, z**2*u + z**2 + z, u + 1
assert modgcd_multivariate(f, g) == (h, cff, cfg)
assert modgcd_multivariate(g, f) == (h, cfg, cff)
R, x, y, z = ring("x,y,z", ZZ)
f, g = x - y*z, x - y*z
assert modgcd_multivariate(f, g) == (x - y*z, 1, 1)
f, g, h = R.fateman_poly_F_1()
H, cff, cfg = modgcd_multivariate(f, g)
assert H == h and H*cff == f and H*cfg == g
R, x, y, z, u, v = ring("x,y,z,u,v", ZZ)
f, g, h = R.fateman_poly_F_1()
H, cff, cfg = modgcd_multivariate(f, g)
assert H == h and H*cff == f and H*cfg == g
R, x, y, z, u, v, a, b = ring("x,y,z,u,v,a,b", ZZ)
f, g, h = R.fateman_poly_F_1()
H, cff, cfg = modgcd_multivariate(f, g)
assert H == h and H*cff == f and H*cfg == g
R, x, y, z, u, v, a, b, c, d = ring("x,y,z,u,v,a,b,c,d", ZZ)
f, g, h = R.fateman_poly_F_1()
H, cff, cfg = modgcd_multivariate(f, g)
assert H == h and H*cff == f and H*cfg == g
R, x, y, z = ring("x,y,z", ZZ)
f, g, h = R.fateman_poly_F_2()
H, cff, cfg = modgcd_multivariate(f, g)
assert H == h and H*cff == f and H*cfg == g
f, g, h = R.fateman_poly_F_3()
H, cff, cfg = modgcd_multivariate(f, g)
assert H == h and H*cff == f and H*cfg == g
R, x, y, z, t = ring("x,y,z,t", ZZ)
f, g, h = R.fateman_poly_F_3()
H, cff, cfg = modgcd_multivariate(f, g)
assert H == h and H*cff == f and H*cfg == g
def test_to_ZZ_ANP_poly():
A = AlgebraicField(QQ, sqrt(2))
R, x = ring("x", A)
f = x*(sqrt(2) + 1)
T, x_, z_ = ring("x_, z_", ZZ)
f_ = x_*z_ + x_
assert _to_ZZ_poly(f, T) == f_
assert _to_ANP_poly(f_, R) == f
R, x, t, s = ring("x, t, s", A)
f = x*t**2 + x*s + sqrt(2)
D, t_, s_ = ring("t_, s_", ZZ)
T, x_, z_ = ring("x_, z_", D)
f_ = (t_**2 + s_)*x_ + z_
assert _to_ZZ_poly(f, T) == f_
assert _to_ANP_poly(f_, R) == f
def test_modgcd_algebraic_field():
A = AlgebraicField(QQ, sqrt(2))
R, x = ring("x", A)
one = A.one
f, g = 2*x, R(2)
assert func_field_modgcd(f, g) == (one, f, g)
f, g = 2*x, R(sqrt(2))
assert func_field_modgcd(f, g) == (one, f, g)
f, g = 2*x + 2, 6*x**2 - 6
assert func_field_modgcd(f, g) == (x + 1, R(2), 6*x - 6)
R, x, y = ring("x, y", A)
f, g = x + sqrt(2)*y, x + y
assert func_field_modgcd(f, g) == (one, f, g)
f, g = x*y + sqrt(2)*y**2, R(sqrt(2))*y
assert func_field_modgcd(f, g) == (y, x + sqrt(2)*y, R(sqrt(2)))
f, g = x**2 + 2*sqrt(2)*x*y + 2*y**2, x + sqrt(2)*y
assert func_field_modgcd(f, g) == (g, g, one)
A = AlgebraicField(QQ, sqrt(2), sqrt(3))
R, x, y, z = ring("x, y, z", A)
h = x**2*y**7 + sqrt(6)/21*z
f, g = h*(27*y**3 + 1), h*(y + x)
assert func_field_modgcd(f, g) == (h, 27*y**3+1, y+x)
h = x**13*y**3 + 1/2*x**10 + 1/sqrt(2)
f, g = h*(x + 1), h*sqrt(2)/sqrt(3)
assert func_field_modgcd(f, g) == (h, x + 1, R(sqrt(2)/sqrt(3)))
A = AlgebraicField(QQ, sqrt(2)**(-1)*sqrt(3))
R, x = ring("x", A)
f, g = x + 1, x - 1
assert func_field_modgcd(f, g) == (A.one, f, g)
# when func_field_modgcd suppors function fields, this test can be changed
def test_modgcd_func_field():
D, t = ring("t", ZZ)
R, x, z = ring("x, z", D)
minpoly = (z**2*t**2 + z**2*t - 1).drop(0)
f, g = x + 1, x - 1
assert _func_field_modgcd_m(f, g, minpoly) == R.one

View File

@ -0,0 +1,269 @@
"""Tests for tools and arithmetics for monomials of distributed polynomials. """
from sympy.polys.monomials import (
itermonomials, monomial_count,
monomial_mul, monomial_div,
monomial_gcd, monomial_lcm,
monomial_max, monomial_min,
monomial_divides, monomial_pow,
Monomial,
)
from sympy.polys.polyerrors import ExactQuotientFailed
from sympy.abc import a, b, c, x, y, z
from sympy.core import S, symbols
from sympy.testing.pytest import raises
def test_monomials():
# total_degree tests
assert set(itermonomials([], 0)) == {S.One}
assert set(itermonomials([], 1)) == {S.One}
assert set(itermonomials([], 2)) == {S.One}
assert set(itermonomials([], 0, 0)) == {S.One}
assert set(itermonomials([], 1, 0)) == {S.One}
assert set(itermonomials([], 2, 0)) == {S.One}
raises(StopIteration, lambda: next(itermonomials([], 0, 1)))
raises(StopIteration, lambda: next(itermonomials([], 0, 2)))
raises(StopIteration, lambda: next(itermonomials([], 0, 3)))
assert set(itermonomials([], 0, 1)) == set()
assert set(itermonomials([], 0, 2)) == set()
assert set(itermonomials([], 0, 3)) == set()
raises(ValueError, lambda: set(itermonomials([], -1)))
raises(ValueError, lambda: set(itermonomials([x], -1)))
raises(ValueError, lambda: set(itermonomials([x, y], -1)))
assert set(itermonomials([x], 0)) == {S.One}
assert set(itermonomials([x], 1)) == {S.One, x}
assert set(itermonomials([x], 2)) == {S.One, x, x**2}
assert set(itermonomials([x], 3)) == {S.One, x, x**2, x**3}
assert set(itermonomials([x, y], 0)) == {S.One}
assert set(itermonomials([x, y], 1)) == {S.One, x, y}
assert set(itermonomials([x, y], 2)) == {S.One, x, y, x**2, y**2, x*y}
assert set(itermonomials([x, y], 3)) == \
{S.One, x, y, x**2, x**3, y**2, y**3, x*y, x*y**2, y*x**2}
i, j, k = symbols('i j k', commutative=False)
assert set(itermonomials([i, j, k], 0)) == {S.One}
assert set(itermonomials([i, j, k], 1)) == {S.One, i, j, k}
assert set(itermonomials([i, j, k], 2)) == \
{S.One, i, j, k, i**2, j**2, k**2, i*j, i*k, j*i, j*k, k*i, k*j}
assert set(itermonomials([i, j, k], 3)) == \
{S.One, i, j, k, i**2, j**2, k**2, i*j, i*k, j*i, j*k, k*i, k*j,
i**3, j**3, k**3,
i**2 * j, i**2 * k, j * i**2, k * i**2,
j**2 * i, j**2 * k, i * j**2, k * j**2,
k**2 * i, k**2 * j, i * k**2, j * k**2,
i*j*i, i*k*i, j*i*j, j*k*j, k*i*k, k*j*k,
i*j*k, i*k*j, j*i*k, j*k*i, k*i*j, k*j*i,
}
assert set(itermonomials([x, i, j], 0)) == {S.One}
assert set(itermonomials([x, i, j], 1)) == {S.One, x, i, j}
assert set(itermonomials([x, i, j], 2)) == {S.One, x, i, j, x*i, x*j, i*j, j*i, x**2, i**2, j**2}
assert set(itermonomials([x, i, j], 3)) == \
{S.One, x, i, j, x*i, x*j, i*j, j*i, x**2, i**2, j**2,
x**3, i**3, j**3,
x**2 * i, x**2 * j,
x * i**2, j * i**2, i**2 * j, i*j*i,
x * j**2, i * j**2, j**2 * i, j*i*j,
x * i * j, x * j * i
}
# degree_list tests
assert set(itermonomials([], [])) == {S.One}
raises(ValueError, lambda: set(itermonomials([], [0])))
raises(ValueError, lambda: set(itermonomials([], [1])))
raises(ValueError, lambda: set(itermonomials([], [2])))
raises(ValueError, lambda: set(itermonomials([x], [1], [])))
raises(ValueError, lambda: set(itermonomials([x], [1, 2], [])))
raises(ValueError, lambda: set(itermonomials([x], [1, 2, 3], [])))
raises(ValueError, lambda: set(itermonomials([x], [], [1])))
raises(ValueError, lambda: set(itermonomials([x], [], [1, 2])))
raises(ValueError, lambda: set(itermonomials([x], [], [1, 2, 3])))
raises(ValueError, lambda: set(itermonomials([x, y], [1, 2], [1, 2, 3])))
raises(ValueError, lambda: set(itermonomials([x, y, z], [1, 2, 3], [0, 1])))
raises(ValueError, lambda: set(itermonomials([x], [1], [-1])))
raises(ValueError, lambda: set(itermonomials([x, y], [1, 2], [1, -1])))
raises(ValueError, lambda: set(itermonomials([], [], 1)))
raises(ValueError, lambda: set(itermonomials([], [], 2)))
raises(ValueError, lambda: set(itermonomials([], [], 3)))
raises(ValueError, lambda: set(itermonomials([x, y], [0, 1], [1, 2])))
raises(ValueError, lambda: set(itermonomials([x, y, z], [0, 0, 3], [0, 1, 2])))
assert set(itermonomials([x], [0])) == {S.One}
assert set(itermonomials([x], [1])) == {S.One, x}
assert set(itermonomials([x], [2])) == {S.One, x, x**2}
assert set(itermonomials([x], [3])) == {S.One, x, x**2, x**3}
assert set(itermonomials([x], [3], [1])) == {x, x**3, x**2}
assert set(itermonomials([x], [3], [2])) == {x**3, x**2}
assert set(itermonomials([x, y], 3, 3)) == {x**3, x**2*y, x*y**2, y**3}
assert set(itermonomials([x, y], 3, 2)) == {x**2, x*y, y**2, x**3, x**2*y, x*y**2, y**3}
assert set(itermonomials([x, y], [0, 0])) == {S.One}
assert set(itermonomials([x, y], [0, 1])) == {S.One, y}
assert set(itermonomials([x, y], [0, 2])) == {S.One, y, y**2}
assert set(itermonomials([x, y], [0, 2], [0, 1])) == {y, y**2}
assert set(itermonomials([x, y], [0, 2], [0, 2])) == {y**2}
assert set(itermonomials([x, y], [1, 0])) == {S.One, x}
assert set(itermonomials([x, y], [1, 1])) == {S.One, x, y, x*y}
assert set(itermonomials([x, y], [1, 2])) == {S.One, x, y, x*y, y**2, x*y**2}
assert set(itermonomials([x, y], [1, 2], [1, 1])) == {x*y, x*y**2}
assert set(itermonomials([x, y], [1, 2], [1, 2])) == {x*y**2}
assert set(itermonomials([x, y], [2, 0])) == {S.One, x, x**2}
assert set(itermonomials([x, y], [2, 1])) == {S.One, x, y, x*y, x**2, x**2*y}
assert set(itermonomials([x, y], [2, 2])) == \
{S.One, y**2, x*y**2, x, x*y, x**2, x**2*y**2, y, x**2*y}
i, j, k = symbols('i j k', commutative=False)
assert set(itermonomials([i, j, k], 2, 2)) == \
{k*i, i**2, i*j, j*k, j*i, k**2, j**2, k*j, i*k}
assert set(itermonomials([i, j, k], 3, 2)) == \
{j*k**2, i*k**2, k*i*j, k*i**2, k**2, j*k*j, k*j**2, i*k*i, i*j,
j**2*k, i**2*j, j*i*k, j**3, i**3, k*j*i, j*k*i, j*i,
k**2*j, j*i**2, k*j, k*j*k, i*j*i, j*i*j, i*j**2, j**2,
k*i*k, i**2, j*k, i*k, i*k*j, k**3, i**2*k, j**2*i, k**2*i,
i*j*k, k*i
}
assert set(itermonomials([i, j, k], [0, 0, 0])) == {S.One}
assert set(itermonomials([i, j, k], [0, 0, 1])) == {1, k}
assert set(itermonomials([i, j, k], [0, 1, 0])) == {1, j}
assert set(itermonomials([i, j, k], [1, 0, 0])) == {i, 1}
assert set(itermonomials([i, j, k], [0, 0, 2])) == {k**2, 1, k}
assert set(itermonomials([i, j, k], [0, 2, 0])) == {1, j, j**2}
assert set(itermonomials([i, j, k], [2, 0, 0])) == {i, 1, i**2}
assert set(itermonomials([i, j, k], [1, 1, 1])) == {1, k, j, j*k, i*k, i, i*j, i*j*k}
assert set(itermonomials([i, j, k], [2, 2, 2])) == \
{1, k, i**2*k**2, j*k, j**2, i, i*k, j*k**2, i*j**2*k**2,
i**2*j, i**2*j**2, k**2, j**2*k, i*j**2*k,
j**2*k**2, i*j, i**2*k, i**2*j**2*k, j, i**2*j*k,
i*j**2, i*k**2, i*j*k, i**2*j**2*k**2, i*j*k**2, i**2, i**2*j*k**2
}
assert set(itermonomials([x, j, k], [0, 0, 0])) == {S.One}
assert set(itermonomials([x, j, k], [0, 0, 1])) == {1, k}
assert set(itermonomials([x, j, k], [0, 1, 0])) == {1, j}
assert set(itermonomials([x, j, k], [1, 0, 0])) == {x, 1}
assert set(itermonomials([x, j, k], [0, 0, 2])) == {k**2, 1, k}
assert set(itermonomials([x, j, k], [0, 2, 0])) == {1, j, j**2}
assert set(itermonomials([x, j, k], [2, 0, 0])) == {x, 1, x**2}
assert set(itermonomials([x, j, k], [1, 1, 1])) == {1, k, j, j*k, x*k, x, x*j, x*j*k}
assert set(itermonomials([x, j, k], [2, 2, 2])) == \
{1, k, x**2*k**2, j*k, j**2, x, x*k, j*k**2, x*j**2*k**2,
x**2*j, x**2*j**2, k**2, j**2*k, x*j**2*k,
j**2*k**2, x*j, x**2*k, x**2*j**2*k, j, x**2*j*k,
x*j**2, x*k**2, x*j*k, x**2*j**2*k**2, x*j*k**2, x**2, x**2*j*k**2
}
def test_monomial_count():
assert monomial_count(2, 2) == 6
assert monomial_count(2, 3) == 10
def test_monomial_mul():
assert monomial_mul((3, 4, 1), (1, 2, 0)) == (4, 6, 1)
def test_monomial_div():
assert monomial_div((3, 4, 1), (1, 2, 0)) == (2, 2, 1)
def test_monomial_gcd():
assert monomial_gcd((3, 4, 1), (1, 2, 0)) == (1, 2, 0)
def test_monomial_lcm():
assert monomial_lcm((3, 4, 1), (1, 2, 0)) == (3, 4, 1)
def test_monomial_max():
assert monomial_max((3, 4, 5), (0, 5, 1), (6, 3, 9)) == (6, 5, 9)
def test_monomial_pow():
assert monomial_pow((1, 2, 3), 3) == (3, 6, 9)
def test_monomial_min():
assert monomial_min((3, 4, 5), (0, 5, 1), (6, 3, 9)) == (0, 3, 1)
def test_monomial_divides():
assert monomial_divides((1, 2, 3), (4, 5, 6)) is True
assert monomial_divides((1, 2, 3), (0, 5, 6)) is False
def test_Monomial():
m = Monomial((3, 4, 1), (x, y, z))
n = Monomial((1, 2, 0), (x, y, z))
assert m.as_expr() == x**3*y**4*z
assert n.as_expr() == x**1*y**2
assert m.as_expr(a, b, c) == a**3*b**4*c
assert n.as_expr(a, b, c) == a**1*b**2
assert m.exponents == (3, 4, 1)
assert m.gens == (x, y, z)
assert n.exponents == (1, 2, 0)
assert n.gens == (x, y, z)
assert m == (3, 4, 1)
assert n != (3, 4, 1)
assert m != (1, 2, 0)
assert n == (1, 2, 0)
assert (m == 1) is False
assert m[0] == m[-3] == 3
assert m[1] == m[-2] == 4
assert m[2] == m[-1] == 1
assert n[0] == n[-3] == 1
assert n[1] == n[-2] == 2
assert n[2] == n[-1] == 0
assert m[:2] == (3, 4)
assert n[:2] == (1, 2)
assert m*n == Monomial((4, 6, 1))
assert m/n == Monomial((2, 2, 1))
assert m*(1, 2, 0) == Monomial((4, 6, 1))
assert m/(1, 2, 0) == Monomial((2, 2, 1))
assert m.gcd(n) == Monomial((1, 2, 0))
assert m.lcm(n) == Monomial((3, 4, 1))
assert m.gcd((1, 2, 0)) == Monomial((1, 2, 0))
assert m.lcm((1, 2, 0)) == Monomial((3, 4, 1))
assert m**0 == Monomial((0, 0, 0))
assert m**1 == m
assert m**2 == Monomial((6, 8, 2))
assert m**3 == Monomial((9, 12, 3))
_a = Monomial((0, 0, 0))
for n in range(10):
assert _a == m**n
_a *= m
raises(ExactQuotientFailed, lambda: m/Monomial((5, 2, 0)))
mm = Monomial((1, 2, 3))
raises(ValueError, lambda: mm.as_expr())
assert str(mm) == 'Monomial((1, 2, 3))'
assert str(m) == 'x**3*y**4*z**1'
raises(NotImplementedError, lambda: m*1)
raises(NotImplementedError, lambda: m/1)
raises(ValueError, lambda: m**-1)
raises(TypeError, lambda: m.gcd(3))
raises(TypeError, lambda: m.lcm(3))

View File

@ -0,0 +1,294 @@
"""Tests for Dixon's and Macaulay's classes. """
from sympy.matrices.dense import Matrix
from sympy.polys.polytools import factor
from sympy.core import symbols
from sympy.tensor.indexed import IndexedBase
from sympy.polys.multivariate_resultants import (DixonResultant,
MacaulayResultant)
c, d = symbols("a, b")
x, y = symbols("x, y")
p = c * x + y
q = x + d * y
dixon = DixonResultant(polynomials=[p, q], variables=[x, y])
macaulay = MacaulayResultant(polynomials=[p, q], variables=[x, y])
def test_dixon_resultant_init():
"""Test init method of DixonResultant."""
a = IndexedBase("alpha")
assert dixon.polynomials == [p, q]
assert dixon.variables == [x, y]
assert dixon.n == 2
assert dixon.m == 2
assert dixon.dummy_variables == [a[0], a[1]]
def test_get_dixon_polynomial_numerical():
"""Test Dixon's polynomial for a numerical example."""
a = IndexedBase("alpha")
p = x + y
q = x ** 2 + y **3
h = x ** 2 + y
dixon = DixonResultant([p, q, h], [x, y])
polynomial = -x * y ** 2 * a[0] - x * y ** 2 * a[1] - x * y * a[0] \
* a[1] - x * y * a[1] ** 2 - x * a[0] * a[1] ** 2 + x * a[0] - \
y ** 2 * a[0] * a[1] + y ** 2 * a[1] - y * a[0] * a[1] ** 2 + y * \
a[1] ** 2
assert dixon.get_dixon_polynomial().as_expr().expand() == polynomial
def test_get_max_degrees():
"""Tests max degrees function."""
p = x + y
q = x ** 2 + y **3
h = x ** 2 + y
dixon = DixonResultant(polynomials=[p, q, h], variables=[x, y])
dixon_polynomial = dixon.get_dixon_polynomial()
assert dixon.get_max_degrees(dixon_polynomial) == [1, 2]
def test_get_dixon_matrix():
"""Test Dixon's resultant for a numerical example."""
x, y = symbols('x, y')
p = x + y
q = x ** 2 + y ** 3
h = x ** 2 + y
dixon = DixonResultant([p, q, h], [x, y])
polynomial = dixon.get_dixon_polynomial()
assert dixon.get_dixon_matrix(polynomial).det() == 0
def test_get_dixon_matrix_example_two():
"""Test Dixon's matrix for example from [Palancz08]_."""
x, y, z = symbols('x, y, z')
f = x ** 2 + y ** 2 - 1 + z * 0
g = x ** 2 + z ** 2 - 1 + y * 0
h = y ** 2 + z ** 2 - 1
example_two = DixonResultant([f, g, h], [y, z])
poly = example_two.get_dixon_polynomial()
matrix = example_two.get_dixon_matrix(poly)
expr = 1 - 8 * x ** 2 + 24 * x ** 4 - 32 * x ** 6 + 16 * x ** 8
assert (matrix.det() - expr).expand() == 0
def test_KSY_precondition():
"""Tests precondition for KSY Resultant."""
A, B, C = symbols('A, B, C')
m1 = Matrix([[1, 2, 3],
[4, 5, 12],
[6, 7, 18]])
m2 = Matrix([[0, C**2],
[-2 * C, -C ** 2]])
m3 = Matrix([[1, 0],
[0, 1]])
m4 = Matrix([[A**2, 0, 1],
[A, 1, 1 / A]])
m5 = Matrix([[5, 1],
[2, B],
[0, 1],
[0, 0]])
assert dixon.KSY_precondition(m1) == False
assert dixon.KSY_precondition(m2) == True
assert dixon.KSY_precondition(m3) == True
assert dixon.KSY_precondition(m4) == False
assert dixon.KSY_precondition(m5) == True
def test_delete_zero_rows_and_columns():
"""Tests method for deleting rows and columns containing only zeros."""
A, B, C = symbols('A, B, C')
m1 = Matrix([[0, 0],
[0, 0],
[1, 2]])
m2 = Matrix([[0, 1, 2],
[0, 3, 4],
[0, 5, 6]])
m3 = Matrix([[0, 0, 0, 0],
[0, 1, 2, 0],
[0, 3, 4, 0],
[0, 0, 0, 0]])
m4 = Matrix([[1, 0, 2],
[0, 0, 0],
[3, 0, 4]])
m5 = Matrix([[0, 0, 0, 1],
[0, 0, 0, 2],
[0, 0, 0, 3],
[0, 0, 0, 4]])
m6 = Matrix([[0, 0, A],
[B, 0, 0],
[0, 0, C]])
assert dixon.delete_zero_rows_and_columns(m1) == Matrix([[1, 2]])
assert dixon.delete_zero_rows_and_columns(m2) == Matrix([[1, 2],
[3, 4],
[5, 6]])
assert dixon.delete_zero_rows_and_columns(m3) == Matrix([[1, 2],
[3, 4]])
assert dixon.delete_zero_rows_and_columns(m4) == Matrix([[1, 2],
[3, 4]])
assert dixon.delete_zero_rows_and_columns(m5) == Matrix([[1],
[2],
[3],
[4]])
assert dixon.delete_zero_rows_and_columns(m6) == Matrix([[0, A],
[B, 0],
[0, C]])
def test_product_leading_entries():
"""Tests product of leading entries method."""
A, B = symbols('A, B')
m1 = Matrix([[1, 2, 3],
[0, 4, 5],
[0, 0, 6]])
m2 = Matrix([[0, 0, 1],
[2, 0, 3]])
m3 = Matrix([[0, 0, 0],
[1, 2, 3],
[0, 0, 0]])
m4 = Matrix([[0, 0, A],
[1, 2, 3],
[B, 0, 0]])
assert dixon.product_leading_entries(m1) == 24
assert dixon.product_leading_entries(m2) == 2
assert dixon.product_leading_entries(m3) == 1
assert dixon.product_leading_entries(m4) == A * B
def test_get_KSY_Dixon_resultant_example_one():
"""Tests the KSY Dixon resultant for example one"""
x, y, z = symbols('x, y, z')
p = x * y * z
q = x**2 - z**2
h = x + y + z
dixon = DixonResultant([p, q, h], [x, y])
dixon_poly = dixon.get_dixon_polynomial()
dixon_matrix = dixon.get_dixon_matrix(dixon_poly)
D = dixon.get_KSY_Dixon_resultant(dixon_matrix)
assert D == -z**3
def test_get_KSY_Dixon_resultant_example_two():
"""Tests the KSY Dixon resultant for example two"""
x, y, A = symbols('x, y, A')
p = x * y + x * A + x - A**2 - A + y**2 + y
q = x**2 + x * A - x + x * y + y * A - y
h = x**2 + x * y + 2 * x - x * A - y * A - 2 * A
dixon = DixonResultant([p, q, h], [x, y])
dixon_poly = dixon.get_dixon_polynomial()
dixon_matrix = dixon.get_dixon_matrix(dixon_poly)
D = factor(dixon.get_KSY_Dixon_resultant(dixon_matrix))
assert D == -8*A*(A - 1)*(A + 2)*(2*A - 1)**2
def test_macaulay_resultant_init():
"""Test init method of MacaulayResultant."""
assert macaulay.polynomials == [p, q]
assert macaulay.variables == [x, y]
assert macaulay.n == 2
assert macaulay.degrees == [1, 1]
assert macaulay.degree_m == 1
assert macaulay.monomials_size == 2
def test_get_degree_m():
assert macaulay._get_degree_m() == 1
def test_get_size():
assert macaulay.get_size() == 2
def test_macaulay_example_one():
"""Tests the Macaulay for example from [Bruce97]_"""
x, y, z = symbols('x, y, z')
a_1_1, a_1_2, a_1_3 = symbols('a_1_1, a_1_2, a_1_3')
a_2_2, a_2_3, a_3_3 = symbols('a_2_2, a_2_3, a_3_3')
b_1_1, b_1_2, b_1_3 = symbols('b_1_1, b_1_2, b_1_3')
b_2_2, b_2_3, b_3_3 = symbols('b_2_2, b_2_3, b_3_3')
c_1, c_2, c_3 = symbols('c_1, c_2, c_3')
f_1 = a_1_1 * x ** 2 + a_1_2 * x * y + a_1_3 * x * z + \
a_2_2 * y ** 2 + a_2_3 * y * z + a_3_3 * z ** 2
f_2 = b_1_1 * x ** 2 + b_1_2 * x * y + b_1_3 * x * z + \
b_2_2 * y ** 2 + b_2_3 * y * z + b_3_3 * z ** 2
f_3 = c_1 * x + c_2 * y + c_3 * z
mac = MacaulayResultant([f_1, f_2, f_3], [x, y, z])
assert mac.degrees == [2, 2, 1]
assert mac.degree_m == 3
assert mac.monomial_set == [x ** 3, x ** 2 * y, x ** 2 * z,
x * y ** 2,
x * y * z, x * z ** 2, y ** 3,
y ** 2 *z, y * z ** 2, z ** 3]
assert mac.monomials_size == 10
assert mac.get_row_coefficients() == [[x, y, z], [x, y, z],
[x * y, x * z, y * z, z ** 2]]
matrix = mac.get_matrix()
assert matrix.shape == (mac.monomials_size, mac.monomials_size)
assert mac.get_submatrix(matrix) == Matrix([[a_1_1, a_2_2],
[b_1_1, b_2_2]])
def test_macaulay_example_two():
"""Tests the Macaulay formulation for example from [Stiller96]_."""
x, y, z = symbols('x, y, z')
a_0, a_1, a_2 = symbols('a_0, a_1, a_2')
b_0, b_1, b_2 = symbols('b_0, b_1, b_2')
c_0, c_1, c_2, c_3, c_4 = symbols('c_0, c_1, c_2, c_3, c_4')
f = a_0 * y - a_1 * x + a_2 * z
g = b_1 * x ** 2 + b_0 * y ** 2 - b_2 * z ** 2
h = c_0 * y - c_1 * x ** 3 + c_2 * x ** 2 * z - c_3 * x * z ** 2 + \
c_4 * z ** 3
mac = MacaulayResultant([f, g, h], [x, y, z])
assert mac.degrees == [1, 2, 3]
assert mac.degree_m == 4
assert mac.monomials_size == 15
assert len(mac.get_row_coefficients()) == mac.n
matrix = mac.get_matrix()
assert matrix.shape == (mac.monomials_size, mac.monomials_size)
assert mac.get_submatrix(matrix) == Matrix([[-a_1, a_0, a_2, 0],
[0, -a_1, 0, 0],
[0, 0, -a_1, 0],
[0, 0, 0, -a_1]])

View File

@ -0,0 +1,124 @@
"""Tests of monomial orderings. """
from sympy.polys.orderings import (
monomial_key, lex, grlex, grevlex, ilex, igrlex,
LexOrder, InverseOrder, ProductOrder, build_product_order,
)
from sympy.abc import x, y, z, t
from sympy.core import S
from sympy.testing.pytest import raises
def test_lex_order():
assert lex((1, 2, 3)) == (1, 2, 3)
assert str(lex) == 'lex'
assert lex((1, 2, 3)) == lex((1, 2, 3))
assert lex((2, 2, 3)) > lex((1, 2, 3))
assert lex((1, 3, 3)) > lex((1, 2, 3))
assert lex((1, 2, 4)) > lex((1, 2, 3))
assert lex((0, 2, 3)) < lex((1, 2, 3))
assert lex((1, 1, 3)) < lex((1, 2, 3))
assert lex((1, 2, 2)) < lex((1, 2, 3))
assert lex.is_global is True
assert lex == LexOrder()
assert lex != grlex
def test_grlex_order():
assert grlex((1, 2, 3)) == (6, (1, 2, 3))
assert str(grlex) == 'grlex'
assert grlex((1, 2, 3)) == grlex((1, 2, 3))
assert grlex((2, 2, 3)) > grlex((1, 2, 3))
assert grlex((1, 3, 3)) > grlex((1, 2, 3))
assert grlex((1, 2, 4)) > grlex((1, 2, 3))
assert grlex((0, 2, 3)) < grlex((1, 2, 3))
assert grlex((1, 1, 3)) < grlex((1, 2, 3))
assert grlex((1, 2, 2)) < grlex((1, 2, 3))
assert grlex((2, 2, 3)) > grlex((1, 2, 4))
assert grlex((1, 3, 3)) > grlex((1, 2, 4))
assert grlex((0, 2, 3)) < grlex((1, 2, 2))
assert grlex((1, 1, 3)) < grlex((1, 2, 2))
assert grlex((0, 1, 1)) > grlex((0, 0, 2))
assert grlex((0, 3, 1)) < grlex((2, 2, 1))
assert grlex.is_global is True
def test_grevlex_order():
assert grevlex((1, 2, 3)) == (6, (-3, -2, -1))
assert str(grevlex) == 'grevlex'
assert grevlex((1, 2, 3)) == grevlex((1, 2, 3))
assert grevlex((2, 2, 3)) > grevlex((1, 2, 3))
assert grevlex((1, 3, 3)) > grevlex((1, 2, 3))
assert grevlex((1, 2, 4)) > grevlex((1, 2, 3))
assert grevlex((0, 2, 3)) < grevlex((1, 2, 3))
assert grevlex((1, 1, 3)) < grevlex((1, 2, 3))
assert grevlex((1, 2, 2)) < grevlex((1, 2, 3))
assert grevlex((2, 2, 3)) > grevlex((1, 2, 4))
assert grevlex((1, 3, 3)) > grevlex((1, 2, 4))
assert grevlex((0, 2, 3)) < grevlex((1, 2, 2))
assert grevlex((1, 1, 3)) < grevlex((1, 2, 2))
assert grevlex((0, 1, 1)) > grevlex((0, 0, 2))
assert grevlex((0, 3, 1)) < grevlex((2, 2, 1))
assert grevlex.is_global is True
def test_InverseOrder():
ilex = InverseOrder(lex)
igrlex = InverseOrder(grlex)
assert ilex((1, 2, 3)) > ilex((2, 0, 3))
assert igrlex((1, 2, 3)) < igrlex((0, 2, 3))
assert str(ilex) == "ilex"
assert str(igrlex) == "igrlex"
assert ilex.is_global is False
assert igrlex.is_global is False
assert ilex != igrlex
assert ilex == InverseOrder(LexOrder())
def test_ProductOrder():
P = ProductOrder((grlex, lambda m: m[:2]), (grlex, lambda m: m[2:]))
assert P((1, 3, 3, 4, 5)) > P((2, 1, 5, 5, 5))
assert str(P) == "ProductOrder(grlex, grlex)"
assert P.is_global is True
assert ProductOrder((grlex, None), (ilex, None)).is_global is None
assert ProductOrder((igrlex, None), (ilex, None)).is_global is False
def test_monomial_key():
assert monomial_key() == lex
assert monomial_key('lex') == lex
assert monomial_key('grlex') == grlex
assert monomial_key('grevlex') == grevlex
raises(ValueError, lambda: monomial_key('foo'))
raises(ValueError, lambda: monomial_key(1))
M = [x, x**2*z**2, x*y, x**2, S.One, y**2, x**3, y, z, x*y**2*z, x**2*y**2]
assert sorted(M, key=monomial_key('lex', [z, y, x])) == \
[S.One, x, x**2, x**3, y, x*y, y**2, x**2*y**2, z, x*y**2*z, x**2*z**2]
assert sorted(M, key=monomial_key('grlex', [z, y, x])) == \
[S.One, x, y, z, x**2, x*y, y**2, x**3, x**2*y**2, x*y**2*z, x**2*z**2]
assert sorted(M, key=monomial_key('grevlex', [z, y, x])) == \
[S.One, x, y, z, x**2, x*y, y**2, x**3, x**2*y**2, x**2*z**2, x*y**2*z]
def test_build_product_order():
assert build_product_order((("grlex", x, y), ("grlex", z, t)), [x, y, z, t])((4, 5, 6, 7)) == \
((9, (4, 5)), (13, (6, 7)))
assert build_product_order((("grlex", x, y), ("grlex", z, t)), [x, y, z, t]) == \
build_product_order((("grlex", x, y), ("grlex", z, t)), [x, y, z, t])

View File

@ -0,0 +1,175 @@
"""Tests for efficient functions for generating orthogonal polynomials. """
from sympy.core.numbers import Rational as Q
from sympy.core.singleton import S
from sympy.core.symbol import symbols
from sympy.polys.polytools import Poly
from sympy.testing.pytest import raises
from sympy.polys.orthopolys import (
jacobi_poly,
gegenbauer_poly,
chebyshevt_poly,
chebyshevu_poly,
hermite_poly,
hermite_prob_poly,
legendre_poly,
laguerre_poly,
spherical_bessel_fn,
)
from sympy.abc import x, a, b
def test_jacobi_poly():
raises(ValueError, lambda: jacobi_poly(-1, a, b, x))
assert jacobi_poly(1, a, b, x, polys=True) == Poly(
(a/2 + b/2 + 1)*x + a/2 - b/2, x, domain='ZZ(a,b)')
assert jacobi_poly(0, a, b, x) == 1
assert jacobi_poly(1, a, b, x) == a/2 - b/2 + x*(a/2 + b/2 + 1)
assert jacobi_poly(2, a, b, x) == (a**2/8 - a*b/4 - a/8 + b**2/8 - b/8 +
x**2*(a**2/8 + a*b/4 + a*Q(7, 8) + b**2/8 +
b*Q(7, 8) + Q(3, 2)) + x*(a**2/4 +
a*Q(3, 4) - b**2/4 - b*Q(3, 4)) - S.Half)
assert jacobi_poly(1, a, b, polys=True) == Poly(
(a/2 + b/2 + 1)*x + a/2 - b/2, x, domain='ZZ(a,b)')
def test_gegenbauer_poly():
raises(ValueError, lambda: gegenbauer_poly(-1, a, x))
assert gegenbauer_poly(
1, a, x, polys=True) == Poly(2*a*x, x, domain='ZZ(a)')
assert gegenbauer_poly(0, a, x) == 1
assert gegenbauer_poly(1, a, x) == 2*a*x
assert gegenbauer_poly(2, a, x) == -a + x**2*(2*a**2 + 2*a)
assert gegenbauer_poly(
3, a, x) == x**3*(4*a**3/3 + 4*a**2 + a*Q(8, 3)) + x*(-2*a**2 - 2*a)
assert gegenbauer_poly(1, S.Half).dummy_eq(x)
assert gegenbauer_poly(1, a, polys=True) == Poly(2*a*x, x, domain='ZZ(a)')
def test_chebyshevt_poly():
raises(ValueError, lambda: chebyshevt_poly(-1, x))
assert chebyshevt_poly(1, x, polys=True) == Poly(x)
assert chebyshevt_poly(0, x) == 1
assert chebyshevt_poly(1, x) == x
assert chebyshevt_poly(2, x) == 2*x**2 - 1
assert chebyshevt_poly(3, x) == 4*x**3 - 3*x
assert chebyshevt_poly(4, x) == 8*x**4 - 8*x**2 + 1
assert chebyshevt_poly(5, x) == 16*x**5 - 20*x**3 + 5*x
assert chebyshevt_poly(6, x) == 32*x**6 - 48*x**4 + 18*x**2 - 1
assert chebyshevt_poly(75, x) == (2*chebyshevt_poly(37, x)*chebyshevt_poly(38, x) - x).expand()
assert chebyshevt_poly(100, x) == (2*chebyshevt_poly(50, x)**2 - 1).expand()
assert chebyshevt_poly(1).dummy_eq(x)
assert chebyshevt_poly(1, polys=True) == Poly(x)
def test_chebyshevu_poly():
raises(ValueError, lambda: chebyshevu_poly(-1, x))
assert chebyshevu_poly(1, x, polys=True) == Poly(2*x)
assert chebyshevu_poly(0, x) == 1
assert chebyshevu_poly(1, x) == 2*x
assert chebyshevu_poly(2, x) == 4*x**2 - 1
assert chebyshevu_poly(3, x) == 8*x**3 - 4*x
assert chebyshevu_poly(4, x) == 16*x**4 - 12*x**2 + 1
assert chebyshevu_poly(5, x) == 32*x**5 - 32*x**3 + 6*x
assert chebyshevu_poly(6, x) == 64*x**6 - 80*x**4 + 24*x**2 - 1
assert chebyshevu_poly(1).dummy_eq(2*x)
assert chebyshevu_poly(1, polys=True) == Poly(2*x)
def test_hermite_poly():
raises(ValueError, lambda: hermite_poly(-1, x))
assert hermite_poly(1, x, polys=True) == Poly(2*x)
assert hermite_poly(0, x) == 1
assert hermite_poly(1, x) == 2*x
assert hermite_poly(2, x) == 4*x**2 - 2
assert hermite_poly(3, x) == 8*x**3 - 12*x
assert hermite_poly(4, x) == 16*x**4 - 48*x**2 + 12
assert hermite_poly(5, x) == 32*x**5 - 160*x**3 + 120*x
assert hermite_poly(6, x) == 64*x**6 - 480*x**4 + 720*x**2 - 120
assert hermite_poly(1).dummy_eq(2*x)
assert hermite_poly(1, polys=True) == Poly(2*x)
def test_hermite_prob_poly():
raises(ValueError, lambda: hermite_prob_poly(-1, x))
assert hermite_prob_poly(1, x, polys=True) == Poly(x)
assert hermite_prob_poly(0, x) == 1
assert hermite_prob_poly(1, x) == x
assert hermite_prob_poly(2, x) == x**2 - 1
assert hermite_prob_poly(3, x) == x**3 - 3*x
assert hermite_prob_poly(4, x) == x**4 - 6*x**2 + 3
assert hermite_prob_poly(5, x) == x**5 - 10*x**3 + 15*x
assert hermite_prob_poly(6, x) == x**6 - 15*x**4 + 45*x**2 - 15
assert hermite_prob_poly(1).dummy_eq(x)
assert hermite_prob_poly(1, polys=True) == Poly(x)
def test_legendre_poly():
raises(ValueError, lambda: legendre_poly(-1, x))
assert legendre_poly(1, x, polys=True) == Poly(x, domain='QQ')
assert legendre_poly(0, x) == 1
assert legendre_poly(1, x) == x
assert legendre_poly(2, x) == Q(3, 2)*x**2 - Q(1, 2)
assert legendre_poly(3, x) == Q(5, 2)*x**3 - Q(3, 2)*x
assert legendre_poly(4, x) == Q(35, 8)*x**4 - Q(30, 8)*x**2 + Q(3, 8)
assert legendre_poly(5, x) == Q(63, 8)*x**5 - Q(70, 8)*x**3 + Q(15, 8)*x
assert legendre_poly(6, x) == Q(
231, 16)*x**6 - Q(315, 16)*x**4 + Q(105, 16)*x**2 - Q(5, 16)
assert legendre_poly(1).dummy_eq(x)
assert legendre_poly(1, polys=True) == Poly(x)
def test_laguerre_poly():
raises(ValueError, lambda: laguerre_poly(-1, x))
assert laguerre_poly(1, x, polys=True) == Poly(-x + 1, domain='QQ')
assert laguerre_poly(0, x) == 1
assert laguerre_poly(1, x) == -x + 1
assert laguerre_poly(2, x) == Q(1, 2)*x**2 - Q(4, 2)*x + 1
assert laguerre_poly(3, x) == -Q(1, 6)*x**3 + Q(9, 6)*x**2 - Q(18, 6)*x + 1
assert laguerre_poly(4, x) == Q(
1, 24)*x**4 - Q(16, 24)*x**3 + Q(72, 24)*x**2 - Q(96, 24)*x + 1
assert laguerre_poly(5, x) == -Q(1, 120)*x**5 + Q(25, 120)*x**4 - Q(
200, 120)*x**3 + Q(600, 120)*x**2 - Q(600, 120)*x + 1
assert laguerre_poly(6, x) == Q(1, 720)*x**6 - Q(36, 720)*x**5 + Q(450, 720)*x**4 - Q(2400, 720)*x**3 + Q(5400, 720)*x**2 - Q(4320, 720)*x + 1
assert laguerre_poly(0, x, a) == 1
assert laguerre_poly(1, x, a) == -x + a + 1
assert laguerre_poly(2, x, a) == x**2/2 + (-a - 2)*x + a**2/2 + a*Q(3, 2) + 1
assert laguerre_poly(3, x, a) == -x**3/6 + (a/2 + Q(
3)/2)*x**2 + (-a**2/2 - a*Q(5, 2) - 3)*x + a**3/6 + a**2 + a*Q(11, 6) + 1
assert laguerre_poly(1).dummy_eq(-x + 1)
assert laguerre_poly(1, polys=True) == Poly(-x + 1)
def test_spherical_bessel_fn():
x, z = symbols("x z")
assert spherical_bessel_fn(1, z) == 1/z**2
assert spherical_bessel_fn(2, z) == -1/z + 3/z**3
assert spherical_bessel_fn(3, z) == -6/z**2 + 15/z**4
assert spherical_bessel_fn(4, z) == 1/z - 45/z**3 + 105/z**5

View File

@ -0,0 +1,249 @@
"""Tests for algorithms for partial fraction decomposition of rational
functions. """
from sympy.polys.partfrac import (
apart_undetermined_coeffs,
apart,
apart_list, assemble_partfrac_list
)
from sympy.core.expr import Expr
from sympy.core.function import Lambda
from sympy.core.numbers import (E, I, Rational, pi, all_close)
from sympy.core.relational import Eq
from sympy.core.singleton import S
from sympy.core.symbol import (Dummy, Symbol)
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.matrices.dense import Matrix
from sympy.polys.polytools import (Poly, factor)
from sympy.polys.rationaltools import together
from sympy.polys.rootoftools import RootSum
from sympy.testing.pytest import raises, XFAIL
from sympy.abc import x, y, a, b, c
def test_apart():
assert apart(1) == 1
assert apart(1, x) == 1
f, g = (x**2 + 1)/(x + 1), 2/(x + 1) + x - 1
assert apart(f, full=False) == g
assert apart(f, full=True) == g
f, g = 1/(x + 2)/(x + 1), 1/(1 + x) - 1/(2 + x)
assert apart(f, full=False) == g
assert apart(f, full=True) == g
f, g = 1/(x + 1)/(x + 5), -1/(5 + x)/4 + 1/(1 + x)/4
assert apart(f, full=False) == g
assert apart(f, full=True) == g
assert apart((E*x + 2)/(x - pi)*(x - 1), x) == \
2 - E + E*pi + E*x + (E*pi + 2)*(pi - 1)/(x - pi)
assert apart(Eq((x**2 + 1)/(x + 1), x), x) == Eq(x - 1 + 2/(x + 1), x)
assert apart(x/2, y) == x/2
f, g = (x+y)/(2*x - y), Rational(3, 2)*y/(2*x - y) + S.Half
assert apart(f, x, full=False) == g
assert apart(f, x, full=True) == g
f, g = (x+y)/(2*x - y), 3*x/(2*x - y) - 1
assert apart(f, y, full=False) == g
assert apart(f, y, full=True) == g
raises(NotImplementedError, lambda: apart(1/(x + 1)/(y + 2)))
def test_apart_matrix():
M = Matrix(2, 2, lambda i, j: 1/(x + i + 1)/(x + j))
assert apart(M) == Matrix([
[1/x - 1/(x + 1), (x + 1)**(-2)],
[1/(2*x) - (S.Half)/(x + 2), 1/(x + 1) - 1/(x + 2)],
])
def test_apart_symbolic():
f = a*x**4 + (2*b + 2*a*c)*x**3 + (4*b*c - a**2 + a*c**2)*x**2 + \
(-2*a*b + 2*b*c**2)*x - b**2
g = a**2*x**4 + (2*a*b + 2*c*a**2)*x**3 + (4*a*b*c + b**2 +
a**2*c**2)*x**2 + (2*c*b**2 + 2*a*b*c**2)*x + b**2*c**2
assert apart(f/g, x) == 1/a - 1/(x + c)**2 - b**2/(a*(a*x + b)**2)
assert apart(1/((x + a)*(x + b)*(x + c)), x) == \
1/((a - c)*(b - c)*(c + x)) - 1/((a - b)*(b - c)*(b + x)) + \
1/((a - b)*(a - c)*(a + x))
def _make_extension_example():
# https://github.com/sympy/sympy/issues/18531
from sympy.core import Mul
def mul2(expr):
# 2-arg mul hack...
return Mul(2, expr, evaluate=False)
f = ((x**2 + 1)**3/((x - 1)**2*(x + 1)**2*(-x**2 + 2*x + 1)*(x**2 + 2*x - 1)))
g = (1/mul2(x - sqrt(2) + 1)
- 1/mul2(x - sqrt(2) - 1)
+ 1/mul2(x + 1 + sqrt(2))
- 1/mul2(x - 1 + sqrt(2))
+ 1/mul2((x + 1)**2)
+ 1/mul2((x - 1)**2))
return f, g
def test_apart_extension():
f = 2/(x**2 + 1)
g = I/(x + I) - I/(x - I)
assert apart(f, extension=I) == g
assert apart(f, gaussian=True) == g
f = x/((x - 2)*(x + I))
assert factor(together(apart(f)).expand()) == f
f, g = _make_extension_example()
# XXX: Only works with dotprodsimp. See test_apart_extension_xfail below
from sympy.matrices import dotprodsimp
with dotprodsimp(True):
assert apart(f, x, extension={sqrt(2)}) == g
def test_apart_extension_xfail():
f, g = _make_extension_example()
assert apart(f, x, extension={sqrt(2)}) == g
def test_apart_full():
f = 1/(x**2 + 1)
assert apart(f, full=False) == f
assert apart(f, full=True).dummy_eq(
-RootSum(x**2 + 1, Lambda(a, a/(x - a)), auto=False)/2)
f = 1/(x**3 + x + 1)
assert apart(f, full=False) == f
assert apart(f, full=True).dummy_eq(
RootSum(x**3 + x + 1,
Lambda(a, (a**2*Rational(6, 31) - a*Rational(9, 31) + Rational(4, 31))/(x - a)), auto=False))
f = 1/(x**5 + 1)
assert apart(f, full=False) == \
(Rational(-1, 5))*((x**3 - 2*x**2 + 3*x - 4)/(x**4 - x**3 + x**2 -
x + 1)) + (Rational(1, 5))/(x + 1)
assert apart(f, full=True).dummy_eq(
-RootSum(x**4 - x**3 + x**2 - x + 1,
Lambda(a, a/(x - a)), auto=False)/5 + (Rational(1, 5))/(x + 1))
def test_apart_full_floats():
# https://github.com/sympy/sympy/issues/26648
f = (
6.43369157032015e-9*x**3 + 1.35203404799555e-5*x**2
+ 0.00357538393743079*x + 0.085
)/(
4.74334912634438e-11*x**4 + 4.09576274286244e-6*x**3
+ 0.00334241812250921*x**2 + 0.15406018058983*x + 1.0
)
expected = (
133.599202650992/(x + 85524.0054884464)
+ 1.07757928431867/(x + 774.88576677949)
+ 0.395006955518971/(x + 40.7977016133126)
+ 0.564264854137341/(x + 7.79746609204661)
)
f_apart = apart(f, full=True).evalf()
# There is a significant floating point error in this operation.
assert all_close(f_apart, expected, rtol=1e-3, atol=1e-5)
def test_apart_undetermined_coeffs():
p = Poly(2*x - 3)
q = Poly(x**9 - x**8 - x**6 + x**5 - 2*x**2 + 3*x - 1)
r = (-x**7 - x**6 - x**5 + 4)/(x**8 - x**5 - 2*x + 1) + 1/(x - 1)
assert apart_undetermined_coeffs(p, q) == r
p = Poly(1, x, domain='ZZ[a,b]')
q = Poly((x + a)*(x + b), x, domain='ZZ[a,b]')
r = 1/((a - b)*(b + x)) - 1/((a - b)*(a + x))
assert apart_undetermined_coeffs(p, q) == r
def test_apart_list():
from sympy.utilities.iterables import numbered_symbols
def dummy_eq(i, j):
if type(i) in (list, tuple):
return all(dummy_eq(i, j) for i, j in zip(i, j))
return i == j or i.dummy_eq(j)
w0, w1, w2 = Symbol("w0"), Symbol("w1"), Symbol("w2")
_a = Dummy("a")
f = (-2*x - 2*x**2) / (3*x**2 - 6*x)
got = apart_list(f, x, dummies=numbered_symbols("w"))
ans = (-1, Poly(Rational(2, 3), x, domain='QQ'),
[(Poly(w0 - 2, w0, domain='ZZ'), Lambda(_a, 2), Lambda(_a, -_a + x), 1)])
assert dummy_eq(got, ans)
got = apart_list(2/(x**2-2), x, dummies=numbered_symbols("w"))
ans = (1, Poly(0, x, domain='ZZ'), [(Poly(w0**2 - 2, w0, domain='ZZ'),
Lambda(_a, _a/2),
Lambda(_a, -_a + x), 1)])
assert dummy_eq(got, ans)
f = 36 / (x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2)
got = apart_list(f, x, dummies=numbered_symbols("w"))
ans = (1, Poly(0, x, domain='ZZ'),
[(Poly(w0 - 2, w0, domain='ZZ'), Lambda(_a, 4), Lambda(_a, -_a + x), 1),
(Poly(w1**2 - 1, w1, domain='ZZ'), Lambda(_a, -3*_a - 6), Lambda(_a, -_a + x), 2),
(Poly(w2 + 1, w2, domain='ZZ'), Lambda(_a, -4), Lambda(_a, -_a + x), 1)])
assert dummy_eq(got, ans)
def test_assemble_partfrac_list():
f = 36 / (x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2)
pfd = apart_list(f)
assert assemble_partfrac_list(pfd) == -4/(x + 1) - 3/(x + 1)**2 - 9/(x - 1)**2 + 4/(x - 2)
a = Dummy("a")
pfd = (1, Poly(0, x, domain='ZZ'), [([sqrt(2),-sqrt(2)], Lambda(a, a/2), Lambda(a, -a + x), 1)])
assert assemble_partfrac_list(pfd) == -1/(sqrt(2)*(x + sqrt(2))) + 1/(sqrt(2)*(x - sqrt(2)))
@XFAIL
def test_noncommutative_pseudomultivariate():
# apart doesn't go inside noncommutative expressions
class foo(Expr):
is_commutative=False
e = x/(x + x*y)
c = 1/(1 + y)
assert apart(e + foo(e)) == c + foo(c)
assert apart(e*foo(e)) == c*foo(c)
def test_noncommutative():
class foo(Expr):
is_commutative=False
e = x/(x + x*y)
c = 1/(1 + y)
assert apart(e + foo()) == c + foo()
def test_issue_5798():
assert apart(
2*x/(x**2 + 1) - (x - 1)/(2*(x**2 + 1)) + 1/(2*(x + 1)) - 2/x) == \
(3*x + 1)/(x**2 + 1)/2 + 1/(x + 1)/2 - 2/x

View File

@ -0,0 +1,588 @@
"""Tests for OO layer of several polynomial representations. """
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.polys.domains import ZZ, QQ
from sympy.polys.polyclasses import DMP, DMF, ANP
from sympy.polys.polyerrors import (CoercionFailed, ExactQuotientFailed,
NotInvertible)
from sympy.polys.specialpolys import f_polys
from sympy.testing.pytest import raises, warns_deprecated_sympy
f_0, f_1, f_2, f_3, f_4, f_5, f_6 = [ f.to_dense() for f in f_polys() ]
def test_DMP___init__():
f = DMP([[ZZ(0)], [], [ZZ(0), ZZ(1), ZZ(2)], [ZZ(3)]], ZZ)
assert f._rep == [[1, 2], [3]]
assert f.dom == ZZ
assert f.lev == 1
f = DMP([[ZZ(1), ZZ(2)], [ZZ(3)]], ZZ, 1)
assert f._rep == [[1, 2], [3]]
assert f.dom == ZZ
assert f.lev == 1
f = DMP.from_dict({(1, 1): ZZ(1), (0, 0): ZZ(2)}, 1, ZZ)
assert f._rep == [[1, 0], [2]]
assert f.dom == ZZ
assert f.lev == 1
def test_DMP_rep_deprecation():
f = DMP([1, 2, 3], ZZ)
with warns_deprecated_sympy():
assert f.rep == [1, 2, 3]
def test_DMP___eq__():
assert DMP([[ZZ(1), ZZ(2)], [ZZ(3)]], ZZ) == \
DMP([[ZZ(1), ZZ(2)], [ZZ(3)]], ZZ)
assert DMP([[ZZ(1), ZZ(2)], [ZZ(3)]], ZZ) == \
DMP([[QQ(1), QQ(2)], [QQ(3)]], QQ)
assert DMP([[QQ(1), QQ(2)], [QQ(3)]], QQ) == \
DMP([[ZZ(1), ZZ(2)], [ZZ(3)]], ZZ)
assert DMP([[[ZZ(1)]]], ZZ) != DMP([[ZZ(1)]], ZZ)
assert DMP([[ZZ(1)]], ZZ) != DMP([[[ZZ(1)]]], ZZ)
def test_DMP___bool__():
assert bool(DMP([[]], ZZ)) is False
assert bool(DMP([[ZZ(1)]], ZZ)) is True
def test_DMP_to_dict():
f = DMP([[ZZ(3)], [], [ZZ(2)], [], [ZZ(8)]], ZZ)
assert f.to_dict() == \
{(4, 0): 3, (2, 0): 2, (0, 0): 8}
assert f.to_sympy_dict() == \
{(4, 0): ZZ.to_sympy(3), (2, 0): ZZ.to_sympy(2), (0, 0):
ZZ.to_sympy(8)}
def test_DMP_properties():
assert DMP([[]], ZZ).is_zero is True
assert DMP([[ZZ(1)]], ZZ).is_zero is False
assert DMP([[ZZ(1)]], ZZ).is_one is True
assert DMP([[ZZ(2)]], ZZ).is_one is False
assert DMP([[ZZ(1)]], ZZ).is_ground is True
assert DMP([[ZZ(1)], [ZZ(2)], [ZZ(1)]], ZZ).is_ground is False
assert DMP([[ZZ(1)], [ZZ(2), ZZ(0)], [ZZ(1), ZZ(0)]], ZZ).is_sqf is True
assert DMP([[ZZ(1)], [ZZ(2), ZZ(0)], [ZZ(1), ZZ(0), ZZ(0)]], ZZ).is_sqf is False
assert DMP([[ZZ(1), ZZ(2)], [ZZ(3)]], ZZ).is_monic is True
assert DMP([[ZZ(2), ZZ(2)], [ZZ(3)]], ZZ).is_monic is False
assert DMP([[ZZ(1), ZZ(2)], [ZZ(3)]], ZZ).is_primitive is True
assert DMP([[ZZ(2), ZZ(4)], [ZZ(6)]], ZZ).is_primitive is False
def test_DMP_arithmetics():
f = DMP([[ZZ(2)], [ZZ(2), ZZ(0)]], ZZ)
assert f.mul_ground(2) == DMP([[ZZ(4)], [ZZ(4), ZZ(0)]], ZZ)
assert f.quo_ground(2) == DMP([[ZZ(1)], [ZZ(1), ZZ(0)]], ZZ)
raises(ExactQuotientFailed, lambda: f.exquo_ground(3))
f = DMP([[ZZ(-5)]], ZZ)
g = DMP([[ZZ(5)]], ZZ)
assert f.abs() == g
assert abs(f) == g
assert g.neg() == f
assert -g == f
h = DMP([[]], ZZ)
assert f.add(g) == h
assert f + g == h
assert g + f == h
assert f + 5 == h
assert 5 + f == h
h = DMP([[ZZ(-10)]], ZZ)
assert f.sub(g) == h
assert f - g == h
assert g - f == -h
assert f - 5 == h
assert 5 - f == -h
h = DMP([[ZZ(-25)]], ZZ)
assert f.mul(g) == h
assert f * g == h
assert g * f == h
assert f * 5 == h
assert 5 * f == h
h = DMP([[ZZ(25)]], ZZ)
assert f.sqr() == h
assert f.pow(2) == h
assert f**2 == h
raises(TypeError, lambda: f.pow('x'))
f = DMP([[ZZ(1)], [], [ZZ(1), ZZ(0), ZZ(0)]], ZZ)
g = DMP([[ZZ(2)], [ZZ(-2), ZZ(0)]], ZZ)
q = DMP([[ZZ(2)], [ZZ(2), ZZ(0)]], ZZ)
r = DMP([[ZZ(8), ZZ(0), ZZ(0)]], ZZ)
assert f.pdiv(g) == (q, r)
assert f.pquo(g) == q
assert f.prem(g) == r
raises(ExactQuotientFailed, lambda: f.pexquo(g))
f = DMP([[ZZ(1)], [], [ZZ(1), ZZ(0), ZZ(0)]], ZZ)
g = DMP([[ZZ(1)], [ZZ(-1), ZZ(0)]], ZZ)
q = DMP([[ZZ(1)], [ZZ(1), ZZ(0)]], ZZ)
r = DMP([[ZZ(2), ZZ(0), ZZ(0)]], ZZ)
assert f.div(g) == (q, r)
assert f.quo(g) == q
assert f.rem(g) == r
assert divmod(f, g) == (q, r)
assert f // g == q
assert f % g == r
raises(ExactQuotientFailed, lambda: f.exquo(g))
f = DMP([ZZ(1), ZZ(0), ZZ(-1)], ZZ)
g = DMP([ZZ(2), ZZ(-2)], ZZ)
q = DMP([], ZZ)
r = f
pq = DMP([ZZ(2), ZZ(2)], ZZ)
pr = DMP([], ZZ)
assert f.div(g) == (q, r)
assert f.quo(g) == q
assert f.rem(g) == r
assert divmod(f, g) == (q, r)
assert f // g == q
assert f % g == r
raises(ExactQuotientFailed, lambda: f.exquo(g))
assert f.pdiv(g) == (pq, pr)
assert f.pquo(g) == pq
assert f.prem(g) == pr
assert f.pexquo(g) == pq
def test_DMP_functionality():
f = DMP([[ZZ(1)], [ZZ(2), ZZ(0)], [ZZ(1), ZZ(0), ZZ(0)]], ZZ)
g = DMP([[ZZ(1)], [ZZ(1), ZZ(0)]], ZZ)
h = DMP([[ZZ(1)]], ZZ)
assert f.degree() == 2
assert f.degree_list() == (2, 2)
assert f.total_degree() == 2
assert f.LC() == ZZ(1)
assert f.TC() == ZZ(0)
assert f.nth(1, 1) == ZZ(2)
raises(TypeError, lambda: f.nth(0, 'x'))
assert f.max_norm() == 2
assert f.l1_norm() == 4
u = DMP([[ZZ(2)], [ZZ(2), ZZ(0)]], ZZ)
assert f.diff(m=1, j=0) == u
assert f.diff(m=1, j=1) == u
raises(TypeError, lambda: f.diff(m='x', j=0))
u = DMP([ZZ(1), ZZ(2), ZZ(1)], ZZ)
v = DMP([ZZ(1), ZZ(2), ZZ(1)], ZZ)
assert f.eval(a=1, j=0) == u
assert f.eval(a=1, j=1) == v
assert f.eval(1).eval(1) == ZZ(4)
assert f.cofactors(g) == (g, g, h)
assert f.gcd(g) == g
assert f.lcm(g) == f
u = DMP([[QQ(45), QQ(30), QQ(5)]], QQ)
v = DMP([[QQ(1), QQ(2, 3), QQ(1, 9)]], QQ)
assert u.monic() == v
assert (4*f).content() == ZZ(4)
assert (4*f).primitive() == (ZZ(4), f)
f = DMP([QQ(1,3), QQ(1)], QQ)
g = DMP([QQ(1,7), QQ(1)], QQ)
assert f.cancel(g) == f.cancel(g, include=True) == (
DMP([QQ(7), QQ(21)], QQ),
DMP([QQ(3), QQ(21)], QQ)
)
assert f.cancel(g, include=False) == (
QQ(7),
QQ(3),
DMP([QQ(1), QQ(3)], QQ),
DMP([QQ(1), QQ(7)], QQ)
)
f = DMP([[ZZ(1)], [ZZ(2)], [ZZ(3)], [ZZ(4)], [ZZ(5)], [ZZ(6)]], ZZ)
assert f.trunc(3) == DMP([[ZZ(1)], [ZZ(-1)], [], [ZZ(1)], [ZZ(-1)], []], ZZ)
f = DMP(f_4, ZZ)
assert f.sqf_part() == -f
assert f.sqf_list() == (ZZ(-1), [(-f, 1)])
f = DMP([[ZZ(-1)], [], [], [ZZ(5)]], ZZ)
g = DMP([[ZZ(3), ZZ(1)], [], []], ZZ)
h = DMP([[ZZ(45), ZZ(30), ZZ(5)]], ZZ)
r = DMP([ZZ(675), ZZ(675), ZZ(225), ZZ(25)], ZZ)
assert f.subresultants(g) == [f, g, h]
assert f.resultant(g) == r
f = DMP([ZZ(1), ZZ(3), ZZ(9), ZZ(-13)], ZZ)
assert f.discriminant() == -11664
f = DMP([QQ(2), QQ(0)], QQ)
g = DMP([QQ(1), QQ(0), QQ(-16)], QQ)
s = DMP([QQ(1, 32), QQ(0)], QQ)
t = DMP([QQ(-1, 16)], QQ)
h = DMP([QQ(1)], QQ)
assert f.half_gcdex(g) == (s, h)
assert f.gcdex(g) == (s, t, h)
assert f.invert(g) == s
f = DMP([[QQ(1)], [QQ(2)], [QQ(3)]], QQ)
raises(ValueError, lambda: f.half_gcdex(f))
raises(ValueError, lambda: f.gcdex(f))
raises(ValueError, lambda: f.invert(f))
f = DMP(ZZ.map([1, 0, 20, 0, 150, 0, 500, 0, 625, -2, 0, -10, 9]), ZZ)
g = DMP([ZZ(1), ZZ(0), ZZ(0), ZZ(-2), ZZ(9)], ZZ)
h = DMP([ZZ(1), ZZ(0), ZZ(5), ZZ(0)], ZZ)
assert g.compose(h) == f
assert f.decompose() == [g, h]
f = DMP([[QQ(1)], [QQ(2)], [QQ(3)]], QQ)
raises(ValueError, lambda: f.decompose())
raises(ValueError, lambda: f.sturm())
def test_DMP_exclude():
f = [[[[[[[[[[[[[[[[[[[[[[[[[[ZZ(1)]], [[]]]]]]]]]]]]]]]]]]]]]]]]]]
J = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 24, 25]
assert DMP(f, ZZ).exclude() == (J, DMP([ZZ(1), ZZ(0)], ZZ))
assert DMP([[ZZ(1)], [ZZ(1), ZZ(0)]], ZZ).exclude() ==\
([], DMP([[ZZ(1)], [ZZ(1), ZZ(0)]], ZZ))
def test_DMF__init__():
f = DMF(([[0], [], [0, 1, 2], [3]], [[1, 2, 3]]), ZZ)
assert f.num == [[1, 2], [3]]
assert f.den == [[1, 2, 3]]
assert f.lev == 1
assert f.dom == ZZ
f = DMF(([[1, 2], [3]], [[1, 2, 3]]), ZZ, 1)
assert f.num == [[1, 2], [3]]
assert f.den == [[1, 2, 3]]
assert f.lev == 1
assert f.dom == ZZ
f = DMF(([[-1], [-2]], [[3], [-4]]), ZZ)
assert f.num == [[-1], [-2]]
assert f.den == [[3], [-4]]
assert f.lev == 1
assert f.dom == ZZ
f = DMF(([[1], [2]], [[-3], [4]]), ZZ)
assert f.num == [[-1], [-2]]
assert f.den == [[3], [-4]]
assert f.lev == 1
assert f.dom == ZZ
f = DMF(([[1], [2]], [[-3], [4]]), ZZ)
assert f.num == [[-1], [-2]]
assert f.den == [[3], [-4]]
assert f.lev == 1
assert f.dom == ZZ
f = DMF(([[]], [[-3], [4]]), ZZ)
assert f.num == [[]]
assert f.den == [[1]]
assert f.lev == 1
assert f.dom == ZZ
f = DMF(17, ZZ, 1)
assert f.num == [[17]]
assert f.den == [[1]]
assert f.lev == 1
assert f.dom == ZZ
f = DMF(([[1], [2]]), ZZ)
assert f.num == [[1], [2]]
assert f.den == [[1]]
assert f.lev == 1
assert f.dom == ZZ
f = DMF([[0], [], [0, 1, 2], [3]], ZZ)
assert f.num == [[1, 2], [3]]
assert f.den == [[1]]
assert f.lev == 1
assert f.dom == ZZ
f = DMF({(1, 1): 1, (0, 0): 2}, ZZ, 1)
assert f.num == [[1, 0], [2]]
assert f.den == [[1]]
assert f.lev == 1
assert f.dom == ZZ
f = DMF(([[QQ(1)], [QQ(2)]], [[-QQ(3)], [QQ(4)]]), QQ)
assert f.num == [[-QQ(1)], [-QQ(2)]]
assert f.den == [[QQ(3)], [-QQ(4)]]
assert f.lev == 1
assert f.dom == QQ
f = DMF(([[QQ(1, 5)], [QQ(2, 5)]], [[-QQ(3, 7)], [QQ(4, 7)]]), QQ)
assert f.num == [[-QQ(7)], [-QQ(14)]]
assert f.den == [[QQ(15)], [-QQ(20)]]
assert f.lev == 1
assert f.dom == QQ
raises(ValueError, lambda: DMF(([1], [[1]]), ZZ))
raises(ZeroDivisionError, lambda: DMF(([1], []), ZZ))
def test_DMF__bool__():
assert bool(DMF([[]], ZZ)) is False
assert bool(DMF([[1]], ZZ)) is True
def test_DMF_properties():
assert DMF([[]], ZZ).is_zero is True
assert DMF([[]], ZZ).is_one is False
assert DMF([[1]], ZZ).is_zero is False
assert DMF([[1]], ZZ).is_one is True
assert DMF(([[1]], [[2]]), ZZ).is_one is False
def test_DMF_arithmetics():
f = DMF([[7], [-9]], ZZ)
g = DMF([[-7], [9]], ZZ)
assert f.neg() == -f == g
f = DMF(([[1]], [[1], []]), ZZ)
g = DMF(([[1]], [[1, 0]]), ZZ)
h = DMF(([[1], [1, 0]], [[1, 0], []]), ZZ)
assert f.add(g) == f + g == h
assert g.add(f) == g + f == h
h = DMF(([[-1], [1, 0]], [[1, 0], []]), ZZ)
assert f.sub(g) == f - g == h
h = DMF(([[1]], [[1, 0], []]), ZZ)
assert f.mul(g) == f*g == h
assert g.mul(f) == g*f == h
h = DMF(([[1, 0]], [[1], []]), ZZ)
assert f.quo(g) == f/g == h
h = DMF(([[1]], [[1], [], [], []]), ZZ)
assert f.pow(3) == f**3 == h
h = DMF(([[1]], [[1, 0, 0, 0]]), ZZ)
assert g.pow(3) == g**3 == h
h = DMF(([[1, 0]], [[1]]), ZZ)
assert g.pow(-1) == g**-1 == h
def test_ANP___init__():
rep = [QQ(1), QQ(1)]
mod = [QQ(1), QQ(0), QQ(1)]
f = ANP(rep, mod, QQ)
assert f.to_list() == [QQ(1), QQ(1)]
assert f.mod_to_list() == [QQ(1), QQ(0), QQ(1)]
assert f.dom == QQ
rep = {1: QQ(1), 0: QQ(1)}
mod = {2: QQ(1), 0: QQ(1)}
f = ANP(rep, mod, QQ)
assert f.to_list() == [QQ(1), QQ(1)]
assert f.mod_to_list() == [QQ(1), QQ(0), QQ(1)]
assert f.dom == QQ
f = ANP(1, mod, QQ)
assert f.to_list() == [QQ(1)]
assert f.mod_to_list() == [QQ(1), QQ(0), QQ(1)]
assert f.dom == QQ
f = ANP([1, 0.5], mod, QQ)
assert all(QQ.of_type(a) for a in f.to_list())
raises(CoercionFailed, lambda: ANP([sqrt(2)], mod, QQ))
def test_ANP___eq__():
a = ANP([QQ(1), QQ(1)], [QQ(1), QQ(0), QQ(1)], QQ)
b = ANP([QQ(1), QQ(1)], [QQ(1), QQ(0), QQ(2)], QQ)
assert (a == a) is True
assert (a != a) is False
assert (a == b) is False
assert (a != b) is True
b = ANP([QQ(1), QQ(2)], [QQ(1), QQ(0), QQ(1)], QQ)
assert (a == b) is False
assert (a != b) is True
def test_ANP___bool__():
assert bool(ANP([], [QQ(1), QQ(0), QQ(1)], QQ)) is False
assert bool(ANP([QQ(1)], [QQ(1), QQ(0), QQ(1)], QQ)) is True
def test_ANP_properties():
mod = [QQ(1), QQ(0), QQ(1)]
assert ANP([QQ(0)], mod, QQ).is_zero is True
assert ANP([QQ(1)], mod, QQ).is_zero is False
assert ANP([QQ(1)], mod, QQ).is_one is True
assert ANP([QQ(2)], mod, QQ).is_one is False
def test_ANP_arithmetics():
mod = [QQ(1), QQ(0), QQ(0), QQ(-2)]
a = ANP([QQ(2), QQ(-1), QQ(1)], mod, QQ)
b = ANP([QQ(1), QQ(2)], mod, QQ)
c = ANP([QQ(-2), QQ(1), QQ(-1)], mod, QQ)
assert a.neg() == -a == c
c = ANP([QQ(2), QQ(0), QQ(3)], mod, QQ)
assert a.add(b) == a + b == c
assert b.add(a) == b + a == c
c = ANP([QQ(2), QQ(-2), QQ(-1)], mod, QQ)
assert a.sub(b) == a - b == c
c = ANP([QQ(-2), QQ(2), QQ(1)], mod, QQ)
assert b.sub(a) == b - a == c
c = ANP([QQ(3), QQ(-1), QQ(6)], mod, QQ)
assert a.mul(b) == a*b == c
assert b.mul(a) == b*a == c
c = ANP([QQ(-1, 43), QQ(9, 43), QQ(5, 43)], mod, QQ)
assert a.pow(0) == a**(0) == ANP(1, mod, QQ)
assert a.pow(1) == a**(1) == a
assert a.pow(-1) == a**(-1) == c
assert a.quo(a) == a.mul(a.pow(-1)) == a*a**(-1) == ANP(1, mod, QQ)
c = ANP([], [1, 0, 0, -2], QQ)
r1 = a.rem(b)
(q, r2) = a.div(b)
assert r1 == r2 == c == a % b
raises(NotInvertible, lambda: a.div(c))
raises(NotInvertible, lambda: a.rem(c))
# Comparison with "hard-coded" value fails despite looking identical
# from sympy import Rational
# c = ANP([Rational(11, 10), Rational(-1, 5), Rational(-3, 5)], [1, 0, 0, -2], QQ)
assert q == a/b # == c
def test_ANP_unify():
mod_z = [ZZ(1), ZZ(0), ZZ(-2)]
mod_q = [QQ(1), QQ(0), QQ(-2)]
a = ANP([QQ(1)], mod_q, QQ)
b = ANP([ZZ(1)], mod_z, ZZ)
assert a.unify(b)[0] == QQ
assert b.unify(a)[0] == QQ
assert a.unify(a)[0] == QQ
assert b.unify(b)[0] == ZZ
assert a.unify_ANP(b)[-1] == QQ
assert b.unify_ANP(a)[-1] == QQ
assert a.unify_ANP(a)[-1] == QQ
assert b.unify_ANP(b)[-1] == ZZ

View File

@ -0,0 +1,126 @@
"""Tests for high-level polynomials manipulation functions. """
from sympy.polys.polyfuncs import (
symmetrize, horner, interpolate, rational_interpolate, viete,
)
from sympy.polys.polyerrors import (
MultivariatePolynomialError,
)
from sympy.core.singleton import S
from sympy.core.symbol import symbols
from sympy.testing.pytest import raises
from sympy.abc import a, b, c, d, e, x, y, z
def test_symmetrize():
assert symmetrize(0, x, y, z) == (0, 0)
assert symmetrize(1, x, y, z) == (1, 0)
s1 = x + y + z
s2 = x*y + x*z + y*z
assert symmetrize(1) == (1, 0)
assert symmetrize(1, formal=True) == (1, 0, [])
assert symmetrize(x) == (x, 0)
assert symmetrize(x + 1) == (x + 1, 0)
assert symmetrize(x, x, y) == (x + y, -y)
assert symmetrize(x + 1, x, y) == (x + y + 1, -y)
assert symmetrize(x, x, y, z) == (s1, -y - z)
assert symmetrize(x + 1, x, y, z) == (s1 + 1, -y - z)
assert symmetrize(x**2, x, y, z) == (s1**2 - 2*s2, -y**2 - z**2)
assert symmetrize(x**2 + y**2) == (-2*x*y + (x + y)**2, 0)
assert symmetrize(x**2 - y**2) == (-2*x*y + (x + y)**2, -2*y**2)
assert symmetrize(x**3 + y**2 + a*x**2 + b*y**3, x, y) == \
(-3*x*y*(x + y) - 2*a*x*y + a*(x + y)**2 + (x + y)**3,
y**2*(1 - a) + y**3*(b - 1))
U = [u0, u1, u2] = symbols('u:3')
assert symmetrize(x + 1, x, y, z, formal=True, symbols=U) == \
(u0 + 1, -y - z, [(u0, x + y + z), (u1, x*y + x*z + y*z), (u2, x*y*z)])
assert symmetrize([1, 2, 3]) == [(1, 0), (2, 0), (3, 0)]
assert symmetrize([1, 2, 3], formal=True) == ([(1, 0), (2, 0), (3, 0)], [])
assert symmetrize([x + y, x - y]) == [(x + y, 0), (x + y, -2*y)]
def test_horner():
assert horner(0) == 0
assert horner(1) == 1
assert horner(x) == x
assert horner(x + 1) == x + 1
assert horner(x**2 + 1) == x**2 + 1
assert horner(x**2 + x) == (x + 1)*x
assert horner(x**2 + x + 1) == (x + 1)*x + 1
assert horner(
9*x**4 + 8*x**3 + 7*x**2 + 6*x + 5) == (((9*x + 8)*x + 7)*x + 6)*x + 5
assert horner(
a*x**4 + b*x**3 + c*x**2 + d*x + e) == (((a*x + b)*x + c)*x + d)*x + e
assert horner(4*x**2*y**2 + 2*x**2*y + 2*x*y**2 + x*y, wrt=x) == ((
4*y + 2)*x*y + (2*y + 1)*y)*x
assert horner(4*x**2*y**2 + 2*x**2*y + 2*x*y**2 + x*y, wrt=y) == ((
4*x + 2)*y*x + (2*x + 1)*x)*y
def test_interpolate():
assert interpolate([1, 4, 9, 16], x) == x**2
assert interpolate([1, 4, 9, 25], x) == S(3)*x**3/2 - S(8)*x**2 + S(33)*x/2 - 9
assert interpolate([(1, 1), (2, 4), (3, 9)], x) == x**2
assert interpolate([(1, 2), (2, 5), (3, 10)], x) == 1 + x**2
assert interpolate({1: 2, 2: 5, 3: 10}, x) == 1 + x**2
assert interpolate({5: 2, 7: 5, 8: 10, 9: 13}, x) == \
-S(13)*x**3/24 + S(12)*x**2 - S(2003)*x/24 + 187
assert interpolate([(1, 3), (0, 6), (2, 5), (5, 7), (-2, 4)], x) == \
S(-61)*x**4/280 + S(247)*x**3/210 + S(139)*x**2/280 - S(1871)*x/420 + 6
assert interpolate((9, 4, 9), 3) == 9
assert interpolate((1, 9, 16), 1) is S.One
assert interpolate(((x, 1), (2, 3)), x) is S.One
assert interpolate({x: 1, 2: 3}, x) is S.One
assert interpolate(((2, x), (1, 3)), x) == x**2 - 4*x + 6
def test_rational_interpolate():
x, y = symbols('x,y')
xdata = [1, 2, 3, 4, 5, 6]
ydata1 = [120, 150, 200, 255, 312, 370]
ydata2 = [-210, -35, 105, 231, 350, 465]
assert rational_interpolate(list(zip(xdata, ydata1)), 2) == (
(60*x**2 + 60)/x )
assert rational_interpolate(list(zip(xdata, ydata1)), 3) == (
(60*x**2 + 60)/x )
assert rational_interpolate(list(zip(xdata, ydata2)), 2, X=y) == (
(105*y**2 - 525)/(y + 1) )
xdata = list(range(1,11))
ydata = [-1923885361858460, -5212158811973685, -9838050145867125,
-15662936261217245, -22469424125057910, -30073793365223685,
-38332297297028735, -47132954289530109, -56387719094026320,
-66026548943876885]
assert rational_interpolate(list(zip(xdata, ydata)), 5) == (
(-12986226192544605*x**4 +
8657484128363070*x**3 - 30301194449270745*x**2 + 4328742064181535*x
- 4328742064181535)/(x**3 + 9*x**2 - 3*x + 11))
def test_viete():
r1, r2 = symbols('r1, r2')
assert viete(
a*x**2 + b*x + c, [r1, r2], x) == [(r1 + r2, -b/a), (r1*r2, c/a)]
raises(ValueError, lambda: viete(1, [], x))
raises(ValueError, lambda: viete(x**2 + 1, [r1]))
raises(MultivariatePolynomialError, lambda: viete(x + y, [r1]))

View File

@ -0,0 +1,185 @@
from sympy.testing.pytest import raises
from sympy.polys.polymatrix import PolyMatrix
from sympy.polys import Poly
from sympy.core.singleton import S
from sympy.matrices.dense import Matrix
from sympy.polys.domains.integerring import ZZ
from sympy.polys.domains.rationalfield import QQ
from sympy.abc import x, y
def _test_polymatrix():
pm1 = PolyMatrix([[Poly(x**2, x), Poly(-x, x)], [Poly(x**3, x), Poly(-1 + x, x)]])
v1 = PolyMatrix([[1, 0], [-1, 0]], ring='ZZ[x]')
m1 = PolyMatrix([[1, 0], [-1, 0]], ring='ZZ[x]')
A = PolyMatrix([[Poly(x**2 + x, x), Poly(0, x)], \
[Poly(x**3 - x + 1, x), Poly(0, x)]])
B = PolyMatrix([[Poly(x**2, x), Poly(-x, x)], [Poly(-x**2, x), Poly(x, x)]])
assert A.ring == ZZ[x]
assert isinstance(pm1*v1, PolyMatrix)
assert pm1*v1 == A
assert pm1*m1 == A
assert v1*pm1 == B
pm2 = PolyMatrix([[Poly(x**2, x, domain='QQ'), Poly(0, x, domain='QQ'), Poly(-x**2, x, domain='QQ'), \
Poly(x**3, x, domain='QQ'), Poly(0, x, domain='QQ'), Poly(-x**3, x, domain='QQ')]])
assert pm2.ring == QQ[x]
v2 = PolyMatrix([1, 0, 0, 0, 0, 0], ring='ZZ[x]')
m2 = PolyMatrix([1, 0, 0, 0, 0, 0], ring='ZZ[x]')
C = PolyMatrix([[Poly(x**2, x, domain='QQ')]])
assert pm2*v2 == C
assert pm2*m2 == C
pm3 = PolyMatrix([[Poly(x**2, x), S.One]], ring='ZZ[x]')
v3 = S.Half*pm3
assert v3 == PolyMatrix([[Poly(S.Half*x**2, x, domain='QQ'), S.Half]], ring='QQ[x]')
assert pm3*S.Half == v3
assert v3.ring == QQ[x]
pm4 = PolyMatrix([[Poly(x**2, x, domain='ZZ'), Poly(-x**2, x, domain='ZZ')]])
v4 = PolyMatrix([1, -1], ring='ZZ[x]')
assert pm4*v4 == PolyMatrix([[Poly(2*x**2, x, domain='ZZ')]])
assert len(PolyMatrix(ring=ZZ[x])) == 0
assert PolyMatrix([1, 0, 0, 1], x)/(-1) == PolyMatrix([-1, 0, 0, -1], x)
def test_polymatrix_constructor():
M1 = PolyMatrix([[x, y]], ring=QQ[x,y])
assert M1.ring == QQ[x,y]
assert M1.domain == QQ
assert M1.gens == (x, y)
assert M1.shape == (1, 2)
assert M1.rows == 1
assert M1.cols == 2
assert len(M1) == 2
assert list(M1) == [Poly(x, (x, y), domain=QQ), Poly(y, (x, y), domain=QQ)]
M2 = PolyMatrix([[x, y]], ring=QQ[x][y])
assert M2.ring == QQ[x][y]
assert M2.domain == QQ[x]
assert M2.gens == (y,)
assert M2.shape == (1, 2)
assert M2.rows == 1
assert M2.cols == 2
assert len(M2) == 2
assert list(M2) == [Poly(x, (y,), domain=QQ[x]), Poly(y, (y,), domain=QQ[x])]
assert PolyMatrix([[x, y]], y) == PolyMatrix([[x, y]], ring=ZZ.frac_field(x)[y])
assert PolyMatrix([[x, y]], ring='ZZ[x,y]') == PolyMatrix([[x, y]], ring=ZZ[x,y])
assert PolyMatrix([[x, y]], (x, y)) == PolyMatrix([[x, y]], ring=QQ[x,y])
assert PolyMatrix([[x, y]], x, y) == PolyMatrix([[x, y]], ring=QQ[x,y])
assert PolyMatrix([x, y]) == PolyMatrix([[x], [y]], ring=QQ[x,y])
assert PolyMatrix(1, 2, [x, y]) == PolyMatrix([[x, y]], ring=QQ[x,y])
assert PolyMatrix(1, 2, lambda i,j: [x,y][j]) == PolyMatrix([[x, y]], ring=QQ[x,y])
assert PolyMatrix(0, 2, [], x, y).shape == (0, 2)
assert PolyMatrix(2, 0, [], x, y).shape == (2, 0)
assert PolyMatrix([[], []], x, y).shape == (2, 0)
assert PolyMatrix(ring=QQ[x,y]) == PolyMatrix(0, 0, [], ring=QQ[x,y]) == PolyMatrix([], ring=QQ[x,y])
raises(TypeError, lambda: PolyMatrix())
raises(TypeError, lambda: PolyMatrix(1))
assert PolyMatrix([Poly(x), Poly(y)]) == PolyMatrix([[x], [y]], ring=ZZ[x,y])
# XXX: Maybe a bug in parallel_poly_from_expr (x lost from gens and domain):
assert PolyMatrix([Poly(y, x), 1]) == PolyMatrix([[y], [1]], ring=QQ[y])
def test_polymatrix_eq():
assert (PolyMatrix([x]) == PolyMatrix([x])) is True
assert (PolyMatrix([y]) == PolyMatrix([x])) is False
assert (PolyMatrix([x]) != PolyMatrix([x])) is False
assert (PolyMatrix([y]) != PolyMatrix([x])) is True
assert PolyMatrix([[x, y]]) != PolyMatrix([x, y]) == PolyMatrix([[x], [y]])
assert PolyMatrix([x], ring=QQ[x]) != PolyMatrix([x], ring=ZZ[x])
assert PolyMatrix([x]) != Matrix([x])
assert PolyMatrix([x]).to_Matrix() == Matrix([x])
assert PolyMatrix([1], x) == PolyMatrix([1], x)
assert PolyMatrix([1], x) != PolyMatrix([1], y)
def test_polymatrix_from_Matrix():
assert PolyMatrix.from_Matrix(Matrix([1, 2]), x) == PolyMatrix([1, 2], x, ring=QQ[x])
assert PolyMatrix.from_Matrix(Matrix([1]), ring=QQ[x]) == PolyMatrix([1], x)
pmx = PolyMatrix([1, 2], x)
pmy = PolyMatrix([1, 2], y)
assert pmx != pmy
assert pmx.set_gens(y) == pmy
def test_polymatrix_repr():
assert repr(PolyMatrix([[1, 2]], x)) == 'PolyMatrix([[1, 2]], ring=QQ[x])'
assert repr(PolyMatrix(0, 2, [], x)) == 'PolyMatrix(0, 2, [], ring=QQ[x])'
def test_polymatrix_getitem():
M = PolyMatrix([[1, 2], [3, 4]], x)
assert M[:, :] == M
assert M[0, :] == PolyMatrix([[1, 2]], x)
assert M[:, 0] == PolyMatrix([1, 3], x)
assert M[0, 0] == Poly(1, x, domain=QQ)
assert M[0] == Poly(1, x, domain=QQ)
assert M[:2] == [Poly(1, x, domain=QQ), Poly(2, x, domain=QQ)]
def test_polymatrix_arithmetic():
M = PolyMatrix([[1, 2], [3, 4]], x)
assert M + M == PolyMatrix([[2, 4], [6, 8]], x)
assert M - M == PolyMatrix([[0, 0], [0, 0]], x)
assert -M == PolyMatrix([[-1, -2], [-3, -4]], x)
raises(TypeError, lambda: M + 1)
raises(TypeError, lambda: M - 1)
raises(TypeError, lambda: 1 + M)
raises(TypeError, lambda: 1 - M)
assert M * M == PolyMatrix([[7, 10], [15, 22]], x)
assert 2 * M == PolyMatrix([[2, 4], [6, 8]], x)
assert M * 2 == PolyMatrix([[2, 4], [6, 8]], x)
assert S(2) * M == PolyMatrix([[2, 4], [6, 8]], x)
assert M * S(2) == PolyMatrix([[2, 4], [6, 8]], x)
raises(TypeError, lambda: [] * M)
raises(TypeError, lambda: M * [])
M2 = PolyMatrix([[1, 2]], ring=ZZ[x])
assert S.Half * M2 == PolyMatrix([[S.Half, 1]], ring=QQ[x])
assert M2 * S.Half == PolyMatrix([[S.Half, 1]], ring=QQ[x])
assert M / 2 == PolyMatrix([[S(1)/2, 1], [S(3)/2, 2]], x)
assert M / Poly(2, x) == PolyMatrix([[S(1)/2, 1], [S(3)/2, 2]], x)
raises(TypeError, lambda: M / [])
def test_polymatrix_manipulations():
M1 = PolyMatrix([[1, 2], [3, 4]], x)
assert M1.transpose() == PolyMatrix([[1, 3], [2, 4]], x)
M2 = PolyMatrix([[5, 6], [7, 8]], x)
assert M1.row_join(M2) == PolyMatrix([[1, 2, 5, 6], [3, 4, 7, 8]], x)
assert M1.col_join(M2) == PolyMatrix([[1, 2], [3, 4], [5, 6], [7, 8]], x)
assert M1.applyfunc(lambda e: 2*e) == PolyMatrix([[2, 4], [6, 8]], x)
def test_polymatrix_ones_zeros():
assert PolyMatrix.zeros(1, 2, x) == PolyMatrix([[0, 0]], x)
assert PolyMatrix.eye(2, x) == PolyMatrix([[1, 0], [0, 1]], x)
def test_polymatrix_rref():
M = PolyMatrix([[1, 2], [3, 4]], x)
assert M.rref() == (PolyMatrix.eye(2, x), (0, 1))
raises(ValueError, lambda: PolyMatrix([1, 2], ring=ZZ[x]).rref())
raises(ValueError, lambda: PolyMatrix([1, x], ring=QQ[x]).rref())
def test_polymatrix_nullspace():
M = PolyMatrix([[1, 2], [3, 6]], x)
assert M.nullspace() == [PolyMatrix([-2, 1], x)]
raises(ValueError, lambda: PolyMatrix([1, 2], ring=ZZ[x]).nullspace())
raises(ValueError, lambda: PolyMatrix([1, x], ring=QQ[x]).nullspace())
assert M.rank() == 1

View File

@ -0,0 +1,485 @@
"""Tests for options manager for :class:`Poly` and public API functions. """
from sympy.polys.polyoptions import (
Options, Expand, Gens, Wrt, Sort, Order, Field, Greedy, Domain,
Split, Gaussian, Extension, Modulus, Symmetric, Strict, Auto,
Frac, Formal, Polys, Include, All, Gen, Symbols, Method)
from sympy.polys.orderings import lex
from sympy.polys.domains import FF, GF, ZZ, QQ, QQ_I, RR, CC, EX
from sympy.polys.polyerrors import OptionError, GeneratorsError
from sympy.core.numbers import (I, Integer)
from sympy.core.symbol import Symbol
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.testing.pytest import raises
from sympy.abc import x, y, z
def test_Options_clone():
opt = Options((x, y, z), {'domain': 'ZZ'})
assert opt.gens == (x, y, z)
assert opt.domain == ZZ
assert ('order' in opt) is False
new_opt = opt.clone({'gens': (x, y), 'order': 'lex'})
assert opt.gens == (x, y, z)
assert opt.domain == ZZ
assert ('order' in opt) is False
assert new_opt.gens == (x, y)
assert new_opt.domain == ZZ
assert ('order' in new_opt) is True
def test_Expand_preprocess():
assert Expand.preprocess(False) is False
assert Expand.preprocess(True) is True
assert Expand.preprocess(0) is False
assert Expand.preprocess(1) is True
raises(OptionError, lambda: Expand.preprocess(x))
def test_Expand_postprocess():
opt = {'expand': True}
Expand.postprocess(opt)
assert opt == {'expand': True}
def test_Gens_preprocess():
assert Gens.preprocess((None,)) == ()
assert Gens.preprocess((x, y, z)) == (x, y, z)
assert Gens.preprocess(((x, y, z),)) == (x, y, z)
a = Symbol('a', commutative=False)
raises(GeneratorsError, lambda: Gens.preprocess((x, x, y)))
raises(GeneratorsError, lambda: Gens.preprocess((x, y, a)))
def test_Gens_postprocess():
opt = {'gens': (x, y)}
Gens.postprocess(opt)
assert opt == {'gens': (x, y)}
def test_Wrt_preprocess():
assert Wrt.preprocess(x) == ['x']
assert Wrt.preprocess('') == []
assert Wrt.preprocess(' ') == []
assert Wrt.preprocess('x,y') == ['x', 'y']
assert Wrt.preprocess('x y') == ['x', 'y']
assert Wrt.preprocess('x, y') == ['x', 'y']
assert Wrt.preprocess('x , y') == ['x', 'y']
assert Wrt.preprocess(' x, y') == ['x', 'y']
assert Wrt.preprocess(' x, y') == ['x', 'y']
assert Wrt.preprocess([x, y]) == ['x', 'y']
raises(OptionError, lambda: Wrt.preprocess(','))
raises(OptionError, lambda: Wrt.preprocess(0))
def test_Wrt_postprocess():
opt = {'wrt': ['x']}
Wrt.postprocess(opt)
assert opt == {'wrt': ['x']}
def test_Sort_preprocess():
assert Sort.preprocess([x, y, z]) == ['x', 'y', 'z']
assert Sort.preprocess((x, y, z)) == ['x', 'y', 'z']
assert Sort.preprocess('x > y > z') == ['x', 'y', 'z']
assert Sort.preprocess('x>y>z') == ['x', 'y', 'z']
raises(OptionError, lambda: Sort.preprocess(0))
raises(OptionError, lambda: Sort.preprocess({x, y, z}))
def test_Sort_postprocess():
opt = {'sort': 'x > y'}
Sort.postprocess(opt)
assert opt == {'sort': 'x > y'}
def test_Order_preprocess():
assert Order.preprocess('lex') == lex
def test_Order_postprocess():
opt = {'order': True}
Order.postprocess(opt)
assert opt == {'order': True}
def test_Field_preprocess():
assert Field.preprocess(False) is False
assert Field.preprocess(True) is True
assert Field.preprocess(0) is False
assert Field.preprocess(1) is True
raises(OptionError, lambda: Field.preprocess(x))
def test_Field_postprocess():
opt = {'field': True}
Field.postprocess(opt)
assert opt == {'field': True}
def test_Greedy_preprocess():
assert Greedy.preprocess(False) is False
assert Greedy.preprocess(True) is True
assert Greedy.preprocess(0) is False
assert Greedy.preprocess(1) is True
raises(OptionError, lambda: Greedy.preprocess(x))
def test_Greedy_postprocess():
opt = {'greedy': True}
Greedy.postprocess(opt)
assert opt == {'greedy': True}
def test_Domain_preprocess():
assert Domain.preprocess(ZZ) == ZZ
assert Domain.preprocess(QQ) == QQ
assert Domain.preprocess(EX) == EX
assert Domain.preprocess(FF(2)) == FF(2)
assert Domain.preprocess(ZZ[x, y]) == ZZ[x, y]
assert Domain.preprocess('Z') == ZZ
assert Domain.preprocess('Q') == QQ
assert Domain.preprocess('ZZ') == ZZ
assert Domain.preprocess('QQ') == QQ
assert Domain.preprocess('EX') == EX
assert Domain.preprocess('FF(23)') == FF(23)
assert Domain.preprocess('GF(23)') == GF(23)
raises(OptionError, lambda: Domain.preprocess('Z[]'))
assert Domain.preprocess('Z[x]') == ZZ[x]
assert Domain.preprocess('Q[x]') == QQ[x]
assert Domain.preprocess('R[x]') == RR[x]
assert Domain.preprocess('C[x]') == CC[x]
assert Domain.preprocess('ZZ[x]') == ZZ[x]
assert Domain.preprocess('QQ[x]') == QQ[x]
assert Domain.preprocess('RR[x]') == RR[x]
assert Domain.preprocess('CC[x]') == CC[x]
assert Domain.preprocess('Z[x,y]') == ZZ[x, y]
assert Domain.preprocess('Q[x,y]') == QQ[x, y]
assert Domain.preprocess('R[x,y]') == RR[x, y]
assert Domain.preprocess('C[x,y]') == CC[x, y]
assert Domain.preprocess('ZZ[x,y]') == ZZ[x, y]
assert Domain.preprocess('QQ[x,y]') == QQ[x, y]
assert Domain.preprocess('RR[x,y]') == RR[x, y]
assert Domain.preprocess('CC[x,y]') == CC[x, y]
raises(OptionError, lambda: Domain.preprocess('Z()'))
assert Domain.preprocess('Z(x)') == ZZ.frac_field(x)
assert Domain.preprocess('Q(x)') == QQ.frac_field(x)
assert Domain.preprocess('ZZ(x)') == ZZ.frac_field(x)
assert Domain.preprocess('QQ(x)') == QQ.frac_field(x)
assert Domain.preprocess('Z(x,y)') == ZZ.frac_field(x, y)
assert Domain.preprocess('Q(x,y)') == QQ.frac_field(x, y)
assert Domain.preprocess('ZZ(x,y)') == ZZ.frac_field(x, y)
assert Domain.preprocess('QQ(x,y)') == QQ.frac_field(x, y)
assert Domain.preprocess('Q<I>') == QQ.algebraic_field(I)
assert Domain.preprocess('QQ<I>') == QQ.algebraic_field(I)
assert Domain.preprocess('Q<sqrt(2), I>') == QQ.algebraic_field(sqrt(2), I)
assert Domain.preprocess(
'QQ<sqrt(2), I>') == QQ.algebraic_field(sqrt(2), I)
raises(OptionError, lambda: Domain.preprocess('abc'))
def test_Domain_postprocess():
raises(GeneratorsError, lambda: Domain.postprocess({'gens': (x, y),
'domain': ZZ[y, z]}))
raises(GeneratorsError, lambda: Domain.postprocess({'gens': (),
'domain': EX}))
raises(GeneratorsError, lambda: Domain.postprocess({'domain': EX}))
def test_Split_preprocess():
assert Split.preprocess(False) is False
assert Split.preprocess(True) is True
assert Split.preprocess(0) is False
assert Split.preprocess(1) is True
raises(OptionError, lambda: Split.preprocess(x))
def test_Split_postprocess():
raises(NotImplementedError, lambda: Split.postprocess({'split': True}))
def test_Gaussian_preprocess():
assert Gaussian.preprocess(False) is False
assert Gaussian.preprocess(True) is True
assert Gaussian.preprocess(0) is False
assert Gaussian.preprocess(1) is True
raises(OptionError, lambda: Gaussian.preprocess(x))
def test_Gaussian_postprocess():
opt = {'gaussian': True}
Gaussian.postprocess(opt)
assert opt == {
'gaussian': True,
'domain': QQ_I,
}
def test_Extension_preprocess():
assert Extension.preprocess(True) is True
assert Extension.preprocess(1) is True
assert Extension.preprocess([]) is None
assert Extension.preprocess(sqrt(2)) == {sqrt(2)}
assert Extension.preprocess([sqrt(2)]) == {sqrt(2)}
assert Extension.preprocess([sqrt(2), I]) == {sqrt(2), I}
raises(OptionError, lambda: Extension.preprocess(False))
raises(OptionError, lambda: Extension.preprocess(0))
def test_Extension_postprocess():
opt = {'extension': {sqrt(2)}}
Extension.postprocess(opt)
assert opt == {
'extension': {sqrt(2)},
'domain': QQ.algebraic_field(sqrt(2)),
}
opt = {'extension': True}
Extension.postprocess(opt)
assert opt == {'extension': True}
def test_Modulus_preprocess():
assert Modulus.preprocess(23) == 23
assert Modulus.preprocess(Integer(23)) == 23
raises(OptionError, lambda: Modulus.preprocess(0))
raises(OptionError, lambda: Modulus.preprocess(x))
def test_Modulus_postprocess():
opt = {'modulus': 5}
Modulus.postprocess(opt)
assert opt == {
'modulus': 5,
'domain': FF(5),
}
opt = {'modulus': 5, 'symmetric': False}
Modulus.postprocess(opt)
assert opt == {
'modulus': 5,
'domain': FF(5, False),
'symmetric': False,
}
def test_Symmetric_preprocess():
assert Symmetric.preprocess(False) is False
assert Symmetric.preprocess(True) is True
assert Symmetric.preprocess(0) is False
assert Symmetric.preprocess(1) is True
raises(OptionError, lambda: Symmetric.preprocess(x))
def test_Symmetric_postprocess():
opt = {'symmetric': True}
Symmetric.postprocess(opt)
assert opt == {'symmetric': True}
def test_Strict_preprocess():
assert Strict.preprocess(False) is False
assert Strict.preprocess(True) is True
assert Strict.preprocess(0) is False
assert Strict.preprocess(1) is True
raises(OptionError, lambda: Strict.preprocess(x))
def test_Strict_postprocess():
opt = {'strict': True}
Strict.postprocess(opt)
assert opt == {'strict': True}
def test_Auto_preprocess():
assert Auto.preprocess(False) is False
assert Auto.preprocess(True) is True
assert Auto.preprocess(0) is False
assert Auto.preprocess(1) is True
raises(OptionError, lambda: Auto.preprocess(x))
def test_Auto_postprocess():
opt = {'auto': True}
Auto.postprocess(opt)
assert opt == {'auto': True}
def test_Frac_preprocess():
assert Frac.preprocess(False) is False
assert Frac.preprocess(True) is True
assert Frac.preprocess(0) is False
assert Frac.preprocess(1) is True
raises(OptionError, lambda: Frac.preprocess(x))
def test_Frac_postprocess():
opt = {'frac': True}
Frac.postprocess(opt)
assert opt == {'frac': True}
def test_Formal_preprocess():
assert Formal.preprocess(False) is False
assert Formal.preprocess(True) is True
assert Formal.preprocess(0) is False
assert Formal.preprocess(1) is True
raises(OptionError, lambda: Formal.preprocess(x))
def test_Formal_postprocess():
opt = {'formal': True}
Formal.postprocess(opt)
assert opt == {'formal': True}
def test_Polys_preprocess():
assert Polys.preprocess(False) is False
assert Polys.preprocess(True) is True
assert Polys.preprocess(0) is False
assert Polys.preprocess(1) is True
raises(OptionError, lambda: Polys.preprocess(x))
def test_Polys_postprocess():
opt = {'polys': True}
Polys.postprocess(opt)
assert opt == {'polys': True}
def test_Include_preprocess():
assert Include.preprocess(False) is False
assert Include.preprocess(True) is True
assert Include.preprocess(0) is False
assert Include.preprocess(1) is True
raises(OptionError, lambda: Include.preprocess(x))
def test_Include_postprocess():
opt = {'include': True}
Include.postprocess(opt)
assert opt == {'include': True}
def test_All_preprocess():
assert All.preprocess(False) is False
assert All.preprocess(True) is True
assert All.preprocess(0) is False
assert All.preprocess(1) is True
raises(OptionError, lambda: All.preprocess(x))
def test_All_postprocess():
opt = {'all': True}
All.postprocess(opt)
assert opt == {'all': True}
def test_Gen_postprocess():
opt = {'gen': x}
Gen.postprocess(opt)
assert opt == {'gen': x}
def test_Symbols_preprocess():
raises(OptionError, lambda: Symbols.preprocess(x))
def test_Symbols_postprocess():
opt = {'symbols': [x, y, z]}
Symbols.postprocess(opt)
assert opt == {'symbols': [x, y, z]}
def test_Method_preprocess():
raises(OptionError, lambda: Method.preprocess(10))
def test_Method_postprocess():
opt = {'method': 'f5b'}
Method.postprocess(opt)
assert opt == {'method': 'f5b'}

View File

@ -0,0 +1,758 @@
"""Tests for algorithms for computing symbolic roots of polynomials. """
from sympy.core.numbers import (I, Rational, pi)
from sympy.core.singleton import S
from sympy.core.symbol import (Symbol, Wild, symbols)
from sympy.functions.elementary.complexes import (conjugate, im, re)
from sympy.functions.elementary.exponential import exp
from sympy.functions.elementary.miscellaneous import (root, sqrt)
from sympy.functions.elementary.piecewise import Piecewise
from sympy.functions.elementary.trigonometric import (acos, cos, sin)
from sympy.polys.domains.integerring import ZZ
from sympy.sets.sets import Interval
from sympy.simplify.powsimp import powsimp
from sympy.polys import Poly, cyclotomic_poly, intervals, nroots, rootof
from sympy.polys.polyroots import (root_factors, roots_linear,
roots_quadratic, roots_cubic, roots_quartic, roots_quintic,
roots_cyclotomic, roots_binomial, preprocess_roots, roots)
from sympy.polys.orthopolys import legendre_poly
from sympy.polys.polyerrors import PolynomialError, \
UnsolvableFactorError
from sympy.polys.polyutils import _nsort
from sympy.testing.pytest import raises, slow
from sympy.core.random import verify_numerically
import mpmath
from itertools import product
a, b, c, d, e, q, t, x, y, z = symbols('a,b,c,d,e,q,t,x,y,z')
def _check(roots):
# this is the desired invariant for roots returned
# by all_roots. It is trivially true for linear
# polynomials.
nreal = sum(1 if i.is_real else 0 for i in roots)
assert sorted(roots[:nreal]) == list(roots[:nreal])
for ix in range(nreal, len(roots), 2):
if not (
roots[ix + 1] == roots[ix] or
roots[ix + 1] == conjugate(roots[ix])):
return False
return True
def test_roots_linear():
assert roots_linear(Poly(2*x + 1, x)) == [Rational(-1, 2)]
def test_roots_quadratic():
assert roots_quadratic(Poly(2*x**2, x)) == [0, 0]
assert roots_quadratic(Poly(2*x**2 + 3*x, x)) == [Rational(-3, 2), 0]
assert roots_quadratic(Poly(2*x**2 + 3, x)) == [-I*sqrt(6)/2, I*sqrt(6)/2]
assert roots_quadratic(Poly(2*x**2 + 4*x + 3, x)) == [-1 - I*sqrt(2)/2, -1 + I*sqrt(2)/2]
_check(Poly(2*x**2 + 4*x + 3, x).all_roots())
f = x**2 + (2*a*e + 2*c*e)/(a - c)*x + (d - b + a*e**2 - c*e**2)/(a - c)
assert roots_quadratic(Poly(f, x)) == \
[-e*(a + c)/(a - c) - sqrt(a*b + c*d - a*d - b*c + 4*a*c*e**2)/(a - c),
-e*(a + c)/(a - c) + sqrt(a*b + c*d - a*d - b*c + 4*a*c*e**2)/(a - c)]
# check for simplification
f = Poly(y*x**2 - 2*x - 2*y, x)
assert roots_quadratic(f) == \
[-sqrt(2*y**2 + 1)/y + 1/y, sqrt(2*y**2 + 1)/y + 1/y]
f = Poly(x**2 + (-y**2 - 2)*x + y**2 + 1, x)
assert roots_quadratic(f) == \
[1,y**2 + 1]
f = Poly(sqrt(2)*x**2 - 1, x)
r = roots_quadratic(f)
assert r == _nsort(r)
# issue 8255
f = Poly(-24*x**2 - 180*x + 264)
assert [w.n(2) for w in f.all_roots(radicals=True)] == \
[w.n(2) for w in f.all_roots(radicals=False)]
for _a, _b, _c in product((-2, 2), (-2, 2), (0, -1)):
f = Poly(_a*x**2 + _b*x + _c)
roots = roots_quadratic(f)
assert roots == _nsort(roots)
def test_issue_7724():
eq = Poly(x**4*I + x**2 + I, x)
assert roots(eq) == {
sqrt(I/2 + sqrt(5)*I/2): 1,
sqrt(-sqrt(5)*I/2 + I/2): 1,
-sqrt(I/2 + sqrt(5)*I/2): 1,
-sqrt(-sqrt(5)*I/2 + I/2): 1}
def test_issue_8438():
p = Poly([1, y, -2, -3], x).as_expr()
roots = roots_cubic(Poly(p, x), x)
z = Rational(-3, 2) - I*7/2 # this will fail in code given in commit msg
post = [r.subs(y, z) for r in roots]
assert set(post) == \
set(roots_cubic(Poly(p.subs(y, z), x)))
# /!\ if p is not made an expression, this is *very* slow
assert all(p.subs({y: z, x: i}).n(2, chop=True) == 0 for i in post)
def test_issue_8285():
roots = (Poly(4*x**8 - 1, x)*Poly(x**2 + 1)).all_roots()
assert _check(roots)
f = Poly(x**4 + 5*x**2 + 6, x)
ro = [rootof(f, i) for i in range(4)]
roots = Poly(x**4 + 5*x**2 + 6, x).all_roots()
assert roots == ro
assert _check(roots)
# more than 2 complex roots from which to identify the
# imaginary ones
roots = Poly(2*x**8 - 1).all_roots()
assert _check(roots)
assert len(Poly(2*x**10 - 1).all_roots()) == 10 # doesn't fail
def test_issue_8289():
roots = (Poly(x**2 + 2)*Poly(x**4 + 2)).all_roots()
assert _check(roots)
roots = Poly(x**6 + 3*x**3 + 2, x).all_roots()
assert _check(roots)
roots = Poly(x**6 - x + 1).all_roots()
assert _check(roots)
# all imaginary roots with multiplicity of 2
roots = Poly(x**4 + 4*x**2 + 4, x).all_roots()
assert _check(roots)
def test_issue_14291():
assert Poly(((x - 1)**2 + 1)*((x - 1)**2 + 2)*(x - 1)
).all_roots() == [1, 1 - I, 1 + I, 1 - sqrt(2)*I, 1 + sqrt(2)*I]
p = x**4 + 10*x**2 + 1
ans = [rootof(p, i) for i in range(4)]
assert Poly(p).all_roots() == ans
_check(ans)
def test_issue_13340():
eq = Poly(y**3 + exp(x)*y + x, y, domain='EX')
roots_d = roots(eq)
assert len(roots_d) == 3
def test_issue_14522():
eq = Poly(x**4 + x**3*(16 + 32*I) + x**2*(-285 + 386*I) + x*(-2824 - 448*I) - 2058 - 6053*I, x)
roots_eq = roots(eq)
assert all(eq(r) == 0 for r in roots_eq)
def test_issue_15076():
sol = roots_quartic(Poly(t**4 - 6*t**2 + t/x - 3, t))
assert sol[0].has(x)
def test_issue_16589():
eq = Poly(x**4 - 8*sqrt(2)*x**3 + 4*x**3 - 64*sqrt(2)*x**2 + 1024*x, x)
roots_eq = roots(eq)
assert 0 in roots_eq
def test_roots_cubic():
assert roots_cubic(Poly(2*x**3, x)) == [0, 0, 0]
assert roots_cubic(Poly(x**3 - 3*x**2 + 3*x - 1, x)) == [1, 1, 1]
# valid for arbitrary y (issue 21263)
r = root(y, 3)
assert roots_cubic(Poly(x**3 - y, x)) == [r,
r*(-S.Half + sqrt(3)*I/2),
r*(-S.Half - sqrt(3)*I/2)]
# simpler form when y is negative
assert roots_cubic(Poly(x**3 - -1, x)) == \
[-1, S.Half - I*sqrt(3)/2, S.Half + I*sqrt(3)/2]
assert roots_cubic(Poly(2*x**3 - 3*x**2 - 3*x - 1, x))[0] == \
S.Half + 3**Rational(1, 3)/2 + 3**Rational(2, 3)/2
eq = -x**3 + 2*x**2 + 3*x - 2
assert roots(eq, trig=True, multiple=True) == \
roots_cubic(Poly(eq, x), trig=True) == [
Rational(2, 3) + 2*sqrt(13)*cos(acos(8*sqrt(13)/169)/3)/3,
-2*sqrt(13)*sin(-acos(8*sqrt(13)/169)/3 + pi/6)/3 + Rational(2, 3),
-2*sqrt(13)*cos(-acos(8*sqrt(13)/169)/3 + pi/3)/3 + Rational(2, 3),
]
def test_roots_quartic():
assert roots_quartic(Poly(x**4, x)) == [0, 0, 0, 0]
assert roots_quartic(Poly(x**4 + x**3, x)) in [
[-1, 0, 0, 0],
[0, -1, 0, 0],
[0, 0, -1, 0],
[0, 0, 0, -1]
]
assert roots_quartic(Poly(x**4 - x**3, x)) in [
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
]
lhs = roots_quartic(Poly(x**4 + x, x))
rhs = [S.Half + I*sqrt(3)/2, S.Half - I*sqrt(3)/2, S.Zero, -S.One]
assert sorted(lhs, key=hash) == sorted(rhs, key=hash)
# test of all branches of roots quartic
for i, (a, b, c, d) in enumerate([(1, 2, 3, 0),
(3, -7, -9, 9),
(1, 2, 3, 4),
(1, 2, 3, 4),
(-7, -3, 3, -6),
(-3, 5, -6, -4),
(6, -5, -10, -3)]):
if i == 2:
c = -a*(a**2/S(8) - b/S(2))
elif i == 3:
d = a*(a*(a**2*Rational(3, 256) - b/S(16)) + c/S(4))
eq = x**4 + a*x**3 + b*x**2 + c*x + d
ans = roots_quartic(Poly(eq, x))
assert all(eq.subs(x, ai).n(chop=True) == 0 for ai in ans)
# not all symbolic quartics are unresolvable
eq = Poly(q*x + q/4 + x**4 + x**3 + 2*x**2 - Rational(1, 3), x)
sol = roots_quartic(eq)
assert all(verify_numerically(eq.subs(x, i), 0) for i in sol)
z = symbols('z', negative=True)
eq = x**4 + 2*x**3 + 3*x**2 + x*(z + 11) + 5
zans = roots_quartic(Poly(eq, x))
assert all(verify_numerically(eq.subs(((x, i), (z, -1))), 0) for i in zans)
# but some are (see also issue 4989)
# it's ok if the solution is not Piecewise, but the tests below should pass
eq = Poly(y*x**4 + x**3 - x + z, x)
ans = roots_quartic(eq)
assert all(type(i) == Piecewise for i in ans)
reps = (
{"y": Rational(-1, 3), "z": Rational(-1, 4)}, # 4 real
{"y": Rational(-1, 3), "z": Rational(-1, 2)}, # 2 real
{"y": Rational(-1, 3), "z": -2}) # 0 real
for rep in reps:
sol = roots_quartic(Poly(eq.subs(rep), x))
assert all(verify_numerically(w.subs(rep) - s, 0) for w, s in zip(ans, sol))
def test_issue_21287():
assert not any(isinstance(i, Piecewise) for i in roots_quartic(
Poly(x**4 - x**2*(3 + 5*I) + 2*x*(-1 + I) - 1 + 3*I, x)))
def test_roots_quintic():
eqs = (x**5 - 2,
(x/2 + 1)**5 - 5*(x/2 + 1) + 12,
x**5 - 110*x**3 - 55*x**2 + 2310*x + 979)
for eq in eqs:
roots = roots_quintic(Poly(eq))
assert len(roots) == 5
assert all(eq.subs(x, r.n(10)).n(chop = 1e-5) == 0 for r in roots)
def test_roots_cyclotomic():
assert roots_cyclotomic(cyclotomic_poly(1, x, polys=True)) == [1]
assert roots_cyclotomic(cyclotomic_poly(2, x, polys=True)) == [-1]
assert roots_cyclotomic(cyclotomic_poly(
3, x, polys=True)) == [Rational(-1, 2) - I*sqrt(3)/2, Rational(-1, 2) + I*sqrt(3)/2]
assert roots_cyclotomic(cyclotomic_poly(4, x, polys=True)) == [-I, I]
assert roots_cyclotomic(cyclotomic_poly(
6, x, polys=True)) == [S.Half - I*sqrt(3)/2, S.Half + I*sqrt(3)/2]
assert roots_cyclotomic(cyclotomic_poly(7, x, polys=True)) == [
-cos(pi/7) - I*sin(pi/7),
-cos(pi/7) + I*sin(pi/7),
-cos(pi*Rational(3, 7)) - I*sin(pi*Rational(3, 7)),
-cos(pi*Rational(3, 7)) + I*sin(pi*Rational(3, 7)),
cos(pi*Rational(2, 7)) - I*sin(pi*Rational(2, 7)),
cos(pi*Rational(2, 7)) + I*sin(pi*Rational(2, 7)),
]
assert roots_cyclotomic(cyclotomic_poly(8, x, polys=True)) == [
-sqrt(2)/2 - I*sqrt(2)/2,
-sqrt(2)/2 + I*sqrt(2)/2,
sqrt(2)/2 - I*sqrt(2)/2,
sqrt(2)/2 + I*sqrt(2)/2,
]
assert roots_cyclotomic(cyclotomic_poly(12, x, polys=True)) == [
-sqrt(3)/2 - I/2,
-sqrt(3)/2 + I/2,
sqrt(3)/2 - I/2,
sqrt(3)/2 + I/2,
]
assert roots_cyclotomic(
cyclotomic_poly(1, x, polys=True), factor=True) == [1]
assert roots_cyclotomic(
cyclotomic_poly(2, x, polys=True), factor=True) == [-1]
assert roots_cyclotomic(cyclotomic_poly(3, x, polys=True), factor=True) == \
[-root(-1, 3), -1 + root(-1, 3)]
assert roots_cyclotomic(cyclotomic_poly(4, x, polys=True), factor=True) == \
[-I, I]
assert roots_cyclotomic(cyclotomic_poly(5, x, polys=True), factor=True) == \
[-root(-1, 5), -root(-1, 5)**3, root(-1, 5)**2, -1 - root(-1, 5)**2 + root(-1, 5) + root(-1, 5)**3]
assert roots_cyclotomic(cyclotomic_poly(6, x, polys=True), factor=True) == \
[1 - root(-1, 3), root(-1, 3)]
def test_roots_binomial():
assert roots_binomial(Poly(5*x, x)) == [0]
assert roots_binomial(Poly(5*x**4, x)) == [0, 0, 0, 0]
assert roots_binomial(Poly(5*x + 2, x)) == [Rational(-2, 5)]
A = 10**Rational(3, 4)/10
assert roots_binomial(Poly(5*x**4 + 2, x)) == \
[-A - A*I, -A + A*I, A - A*I, A + A*I]
_check(roots_binomial(Poly(x**8 - 2)))
a1 = Symbol('a1', nonnegative=True)
b1 = Symbol('b1', nonnegative=True)
r0 = roots_quadratic(Poly(a1*x**2 + b1, x))
r1 = roots_binomial(Poly(a1*x**2 + b1, x))
assert powsimp(r0[0]) == powsimp(r1[0])
assert powsimp(r0[1]) == powsimp(r1[1])
for a, b, s, n in product((1, 2), (1, 2), (-1, 1), (2, 3, 4, 5)):
if a == b and a != 1: # a == b == 1 is sufficient
continue
p = Poly(a*x**n + s*b)
ans = roots_binomial(p)
assert ans == _nsort(ans)
# issue 8813
assert roots(Poly(2*x**3 - 16*y**3, x)) == {
2*y*(Rational(-1, 2) - sqrt(3)*I/2): 1,
2*y: 1,
2*y*(Rational(-1, 2) + sqrt(3)*I/2): 1}
def test_roots_preprocessing():
f = a*y*x**2 + y - b
coeff, poly = preprocess_roots(Poly(f, x))
assert coeff == 1
assert poly == Poly(a*y*x**2 + y - b, x)
f = c**3*x**3 + c**2*x**2 + c*x + a
coeff, poly = preprocess_roots(Poly(f, x))
assert coeff == 1/c
assert poly == Poly(x**3 + x**2 + x + a, x)
f = c**3*x**3 + c**2*x**2 + a
coeff, poly = preprocess_roots(Poly(f, x))
assert coeff == 1/c
assert poly == Poly(x**3 + x**2 + a, x)
f = c**3*x**3 + c*x + a
coeff, poly = preprocess_roots(Poly(f, x))
assert coeff == 1/c
assert poly == Poly(x**3 + x + a, x)
f = c**3*x**3 + a
coeff, poly = preprocess_roots(Poly(f, x))
assert coeff == 1/c
assert poly == Poly(x**3 + a, x)
E, F, J, L = symbols("E,F,J,L")
f = -21601054687500000000*E**8*J**8/L**16 + \
508232812500000000*F*x*E**7*J**7/L**14 - \
4269543750000000*E**6*F**2*J**6*x**2/L**12 + \
16194716250000*E**5*F**3*J**5*x**3/L**10 - \
27633173750*E**4*F**4*J**4*x**4/L**8 + \
14840215*E**3*F**5*J**3*x**5/L**6 + \
54794*E**2*F**6*J**2*x**6/(5*L**4) - \
1153*E*J*F**7*x**7/(80*L**2) + \
633*F**8*x**8/160000
coeff, poly = preprocess_roots(Poly(f, x))
assert coeff == 20*E*J/(F*L**2)
assert poly == 633*x**8 - 115300*x**7 + 4383520*x**6 + 296804300*x**5 - 27633173750*x**4 + \
809735812500*x**3 - 10673859375000*x**2 + 63529101562500*x - 135006591796875
f = Poly(-y**2 + x**2*exp(x), y, domain=ZZ[x, exp(x)])
g = Poly(-y**2 + exp(x), y, domain=ZZ[exp(x)])
assert preprocess_roots(f) == (x, g)
def test_roots0():
assert roots(1, x) == {}
assert roots(x, x) == {S.Zero: 1}
assert roots(x**9, x) == {S.Zero: 9}
assert roots(((x - 2)*(x + 3)*(x - 4)).expand(), x) == {-S(3): 1, S(2): 1, S(4): 1}
assert roots(2*x + 1, x) == {Rational(-1, 2): 1}
assert roots((2*x + 1)**2, x) == {Rational(-1, 2): 2}
assert roots((2*x + 1)**5, x) == {Rational(-1, 2): 5}
assert roots((2*x + 1)**10, x) == {Rational(-1, 2): 10}
assert roots(x**4 - 1, x) == {I: 1, S.One: 1, -S.One: 1, -I: 1}
assert roots((x**4 - 1)**2, x) == {I: 2, S.One: 2, -S.One: 2, -I: 2}
assert roots(((2*x - 3)**2).expand(), x) == {Rational( 3, 2): 2}
assert roots(((2*x + 3)**2).expand(), x) == {Rational(-3, 2): 2}
assert roots(((2*x - 3)**3).expand(), x) == {Rational( 3, 2): 3}
assert roots(((2*x + 3)**3).expand(), x) == {Rational(-3, 2): 3}
assert roots(((2*x - 3)**5).expand(), x) == {Rational( 3, 2): 5}
assert roots(((2*x + 3)**5).expand(), x) == {Rational(-3, 2): 5}
assert roots(((a*x - b)**5).expand(), x) == { b/a: 5}
assert roots(((a*x + b)**5).expand(), x) == {-b/a: 5}
assert roots(x**2 + (-a - 1)*x + a, x) == {a: 1, S.One: 1}
assert roots(x**4 - 2*x**2 + 1, x) == {S.One: 2, S.NegativeOne: 2}
assert roots(x**6 - 4*x**4 + 4*x**3 - x**2, x) == \
{S.One: 2, -1 - sqrt(2): 1, S.Zero: 2, -1 + sqrt(2): 1}
assert roots(x**8 - 1, x) == {
sqrt(2)/2 + I*sqrt(2)/2: 1,
sqrt(2)/2 - I*sqrt(2)/2: 1,
-sqrt(2)/2 + I*sqrt(2)/2: 1,
-sqrt(2)/2 - I*sqrt(2)/2: 1,
S.One: 1, -S.One: 1, I: 1, -I: 1
}
f = -2016*x**2 - 5616*x**3 - 2056*x**4 + 3324*x**5 + 2176*x**6 - \
224*x**7 - 384*x**8 - 64*x**9
assert roots(f) == {S.Zero: 2, -S(2): 2, S(2): 1, Rational(-7, 2): 1,
Rational(-3, 2): 1, Rational(-1, 2): 1, Rational(3, 2): 1}
assert roots((a + b + c)*x - (a + b + c + d), x) == {(a + b + c + d)/(a + b + c): 1}
assert roots(x**3 + x**2 - x + 1, x, cubics=False) == {}
assert roots(((x - 2)*(
x + 3)*(x - 4)).expand(), x, cubics=False) == {-S(3): 1, S(2): 1, S(4): 1}
assert roots(((x - 2)*(x + 3)*(x - 4)*(x - 5)).expand(), x, cubics=False) == \
{-S(3): 1, S(2): 1, S(4): 1, S(5): 1}
assert roots(x**3 + 2*x**2 + 4*x + 8, x) == {-S(2): 1, -2*I: 1, 2*I: 1}
assert roots(x**3 + 2*x**2 + 4*x + 8, x, cubics=True) == \
{-2*I: 1, 2*I: 1, -S(2): 1}
assert roots((x**2 - x)*(x**3 + 2*x**2 + 4*x + 8), x ) == \
{S.One: 1, S.Zero: 1, -S(2): 1, -2*I: 1, 2*I: 1}
r1_2, r1_3 = S.Half, Rational(1, 3)
x0 = (3*sqrt(33) + 19)**r1_3
x1 = 4/x0/3
x2 = x0/3
x3 = sqrt(3)*I/2
x4 = x3 - r1_2
x5 = -x3 - r1_2
assert roots(x**3 + x**2 - x + 1, x, cubics=True) == {
-x1 - x2 - r1_3: 1,
-x1/x4 - x2*x4 - r1_3: 1,
-x1/x5 - x2*x5 - r1_3: 1,
}
f = (x**2 + 2*x + 3).subs(x, 2*x**2 + 3*x).subs(x, 5*x - 4)
r13_20, r1_20 = [ Rational(*r)
for r in ((13, 20), (1, 20)) ]
s2 = sqrt(2)
assert roots(f, x) == {
r13_20 + r1_20*sqrt(1 - 8*I*s2): 1,
r13_20 - r1_20*sqrt(1 - 8*I*s2): 1,
r13_20 + r1_20*sqrt(1 + 8*I*s2): 1,
r13_20 - r1_20*sqrt(1 + 8*I*s2): 1,
}
f = x**4 + x**3 + x**2 + x + 1
r1_4, r1_8, r5_8 = [ Rational(*r) for r in ((1, 4), (1, 8), (5, 8)) ]
assert roots(f, x) == {
-r1_4 + r1_4*5**r1_2 + I*(r5_8 + r1_8*5**r1_2)**r1_2: 1,
-r1_4 + r1_4*5**r1_2 - I*(r5_8 + r1_8*5**r1_2)**r1_2: 1,
-r1_4 - r1_4*5**r1_2 + I*(r5_8 - r1_8*5**r1_2)**r1_2: 1,
-r1_4 - r1_4*5**r1_2 - I*(r5_8 - r1_8*5**r1_2)**r1_2: 1,
}
f = z**3 + (-2 - y)*z**2 + (1 + 2*y - 2*x**2)*z - y + 2*x**2
assert roots(f, z) == {
S.One: 1,
S.Half + S.Half*y + S.Half*sqrt(1 - 2*y + y**2 + 8*x**2): 1,
S.Half + S.Half*y - S.Half*sqrt(1 - 2*y + y**2 + 8*x**2): 1,
}
assert roots(a*b*c*x**3 + 2*x**2 + 4*x + 8, x, cubics=False) == {}
assert roots(a*b*c*x**3 + 2*x**2 + 4*x + 8, x, cubics=True) != {}
assert roots(x**4 - 1, x, filter='Z') == {S.One: 1, -S.One: 1}
assert roots(x**4 - 1, x, filter='I') == {I: 1, -I: 1}
assert roots((x - 1)*(x + 1), x) == {S.One: 1, -S.One: 1}
assert roots(
(x - 1)*(x + 1), x, predicate=lambda r: r.is_positive) == {S.One: 1}
assert roots(x**4 - 1, x, filter='Z', multiple=True) == [-S.One, S.One]
assert roots(x**4 - 1, x, filter='I', multiple=True) == [I, -I]
ar, br = symbols('a, b', real=True)
p = x**2*(ar-br)**2 + 2*x*(br-ar) + 1
assert roots(p, x, filter='R') == {1/(ar - br): 2}
assert roots(x**3, x, multiple=True) == [S.Zero, S.Zero, S.Zero]
assert roots(1234, x, multiple=True) == []
f = x**6 - x**5 + x**4 - x**3 + x**2 - x + 1
assert roots(f) == {
-I*sin(pi/7) + cos(pi/7): 1,
-I*sin(pi*Rational(2, 7)) - cos(pi*Rational(2, 7)): 1,
-I*sin(pi*Rational(3, 7)) + cos(pi*Rational(3, 7)): 1,
I*sin(pi/7) + cos(pi/7): 1,
I*sin(pi*Rational(2, 7)) - cos(pi*Rational(2, 7)): 1,
I*sin(pi*Rational(3, 7)) + cos(pi*Rational(3, 7)): 1,
}
g = ((x**2 + 1)*f**2).expand()
assert roots(g) == {
-I*sin(pi/7) + cos(pi/7): 2,
-I*sin(pi*Rational(2, 7)) - cos(pi*Rational(2, 7)): 2,
-I*sin(pi*Rational(3, 7)) + cos(pi*Rational(3, 7)): 2,
I*sin(pi/7) + cos(pi/7): 2,
I*sin(pi*Rational(2, 7)) - cos(pi*Rational(2, 7)): 2,
I*sin(pi*Rational(3, 7)) + cos(pi*Rational(3, 7)): 2,
-I: 1, I: 1,
}
r = roots(x**3 + 40*x + 64)
real_root = [rx for rx in r if rx.is_real][0]
cr = 108 + 6*sqrt(1074)
assert real_root == -2*root(cr, 3)/3 + 20/root(cr, 3)
eq = Poly((7 + 5*sqrt(2))*x**3 + (-6 - 4*sqrt(2))*x**2 + (-sqrt(2) - 1)*x + 2, x, domain='EX')
assert roots(eq) == {-1 + sqrt(2): 1, -2 + 2*sqrt(2): 1, -sqrt(2) + 1: 1}
eq = Poly(41*x**5 + 29*sqrt(2)*x**5 - 153*x**4 - 108*sqrt(2)*x**4 +
175*x**3 + 125*sqrt(2)*x**3 - 45*x**2 - 30*sqrt(2)*x**2 - 26*sqrt(2)*x -
26*x + 24, x, domain='EX')
assert roots(eq) == {-sqrt(2) + 1: 1, -2 + 2*sqrt(2): 1, -1 + sqrt(2): 1,
-4 + 4*sqrt(2): 1, -3 + 3*sqrt(2): 1}
eq = Poly(x**3 - 2*x**2 + 6*sqrt(2)*x**2 - 8*sqrt(2)*x + 23*x - 14 +
14*sqrt(2), x, domain='EX')
assert roots(eq) == {-2*sqrt(2) + 2: 1, -2*sqrt(2) + 1: 1, -2*sqrt(2) - 1: 1}
assert roots(Poly((x + sqrt(2))**3 - 7, x, domain='EX')) == \
{-sqrt(2) + root(7, 3)*(-S.Half - sqrt(3)*I/2): 1,
-sqrt(2) + root(7, 3)*(-S.Half + sqrt(3)*I/2): 1,
-sqrt(2) + root(7, 3): 1}
def test_roots_slow():
"""Just test that calculating these roots does not hang. """
a, b, c, d, x = symbols("a,b,c,d,x")
f1 = x**2*c + (a/b) + x*c*d - a
f2 = x**2*(a + b*(c - d)*a) + x*a*b*c/(b*d - d) + (a*d - c/d)
assert list(roots(f1, x).values()) == [1, 1]
assert list(roots(f2, x).values()) == [1, 1]
(zz, yy, xx, zy, zx, yx, k) = symbols("zz,yy,xx,zy,zx,yx,k")
e1 = (zz - k)*(yy - k)*(xx - k) + zy*yx*zx + zx - zy - yx
e2 = (zz - k)*yx*yx + zx*(yy - k)*zx + zy*zy*(xx - k)
assert list(roots(e1 - e2, k).values()) == [1, 1, 1]
f = x**3 + 2*x**2 + 8
R = list(roots(f).keys())
assert not any(i for i in [f.subs(x, ri).n(chop=True) for ri in R])
def test_roots_inexact():
R1 = roots(x**2 + x + 1, x, multiple=True)
R2 = roots(x**2 + x + 1.0, x, multiple=True)
for r1, r2 in zip(R1, R2):
assert abs(r1 - r2) < 1e-12
f = x**4 + 3.0*sqrt(2.0)*x**3 - (78.0 + 24.0*sqrt(3.0))*x**2 \
+ 144.0*(2*sqrt(3.0) + 9.0)
R1 = roots(f, multiple=True)
R2 = (-12.7530479110482, -3.85012393732929,
4.89897948556636, 7.46155167569183)
for r1, r2 in zip(R1, R2):
assert abs(r1 - r2) < 1e-10
def test_roots_preprocessed():
E, F, J, L = symbols("E,F,J,L")
f = -21601054687500000000*E**8*J**8/L**16 + \
508232812500000000*F*x*E**7*J**7/L**14 - \
4269543750000000*E**6*F**2*J**6*x**2/L**12 + \
16194716250000*E**5*F**3*J**5*x**3/L**10 - \
27633173750*E**4*F**4*J**4*x**4/L**8 + \
14840215*E**3*F**5*J**3*x**5/L**6 + \
54794*E**2*F**6*J**2*x**6/(5*L**4) - \
1153*E*J*F**7*x**7/(80*L**2) + \
633*F**8*x**8/160000
assert roots(f, x) == {}
R1 = roots(f.evalf(), x, multiple=True)
R2 = [-1304.88375606366, 97.1168816800648, 186.946430171876, 245.526792947065,
503.441004174773, 791.549343830097, 1273.16678129348, 1850.10650616851]
w = Wild('w')
p = w*E*J/(F*L**2)
assert len(R1) == len(R2)
for r1, r2 in zip(R1, R2):
match = r1.match(p)
assert match is not None and abs(match[w] - r2) < 1e-10
def test_roots_strict():
assert roots(x**2 - 2*x + 1, strict=False) == {1: 2}
assert roots(x**2 - 2*x + 1, strict=True) == {1: 2}
assert roots(x**6 - 2*x**5 - x**2 + 3*x - 2, strict=False) == {2: 1}
raises(UnsolvableFactorError, lambda: roots(x**6 - 2*x**5 - x**2 + 3*x - 2, strict=True))
def test_roots_mixed():
f = -1936 - 5056*x - 7592*x**2 + 2704*x**3 - 49*x**4
_re, _im = intervals(f, all=True)
_nroots = nroots(f)
_sroots = roots(f, multiple=True)
_re = [ Interval(a, b) for (a, b), _ in _re ]
_im = [ Interval(re(a), re(b))*Interval(im(a), im(b)) for (a, b),
_ in _im ]
_intervals = _re + _im
_sroots = [ r.evalf() for r in _sroots ]
_nroots = sorted(_nroots, key=lambda x: x.sort_key())
_sroots = sorted(_sroots, key=lambda x: x.sort_key())
for _roots in (_nroots, _sroots):
for i, r in zip(_intervals, _roots):
if r.is_real:
assert r in i
else:
assert (re(r), im(r)) in i
def test_root_factors():
assert root_factors(Poly(1, x)) == [Poly(1, x)]
assert root_factors(Poly(x, x)) == [Poly(x, x)]
assert root_factors(x**2 - 1, x) == [x + 1, x - 1]
assert root_factors(x**2 - y, x) == [x - sqrt(y), x + sqrt(y)]
assert root_factors((x**4 - 1)**2) == \
[x + 1, x + 1, x - 1, x - 1, x - I, x - I, x + I, x + I]
assert root_factors(Poly(x**4 - 1, x), filter='Z') == \
[Poly(x + 1, x), Poly(x - 1, x), Poly(x**2 + 1, x)]
assert root_factors(8*x**2 + 12*x**4 + 6*x**6 + x**8, x, filter='Q') == \
[x, x, x**6 + 6*x**4 + 12*x**2 + 8]
@slow
def test_nroots1():
n = 64
p = legendre_poly(n, x, polys=True)
raises(mpmath.mp.NoConvergence, lambda: p.nroots(n=3, maxsteps=5))
roots = p.nroots(n=3)
# The order of roots matters. They are ordered from smallest to the
# largest.
assert [str(r) for r in roots] == \
['-0.999', '-0.996', '-0.991', '-0.983', '-0.973', '-0.961',
'-0.946', '-0.930', '-0.911', '-0.889', '-0.866', '-0.841',
'-0.813', '-0.784', '-0.753', '-0.720', '-0.685', '-0.649',
'-0.611', '-0.572', '-0.531', '-0.489', '-0.446', '-0.402',
'-0.357', '-0.311', '-0.265', '-0.217', '-0.170', '-0.121',
'-0.0730', '-0.0243', '0.0243', '0.0730', '0.121', '0.170',
'0.217', '0.265', '0.311', '0.357', '0.402', '0.446', '0.489',
'0.531', '0.572', '0.611', '0.649', '0.685', '0.720', '0.753',
'0.784', '0.813', '0.841', '0.866', '0.889', '0.911', '0.930',
'0.946', '0.961', '0.973', '0.983', '0.991', '0.996', '0.999']
def test_nroots2():
p = Poly(x**5 + 3*x + 1, x)
roots = p.nroots(n=3)
# The order of roots matters. The roots are ordered by their real
# components (if they agree, then by their imaginary components),
# with real roots appearing first.
assert [str(r) for r in roots] == \
['-0.332', '-0.839 - 0.944*I', '-0.839 + 0.944*I',
'1.01 - 0.937*I', '1.01 + 0.937*I']
roots = p.nroots(n=5)
assert [str(r) for r in roots] == \
['-0.33199', '-0.83907 - 0.94385*I', '-0.83907 + 0.94385*I',
'1.0051 - 0.93726*I', '1.0051 + 0.93726*I']
def test_roots_composite():
assert len(roots(Poly(y**3 + y**2*sqrt(x) + y + x, y, composite=True))) == 3
def test_issue_19113():
eq = cos(x)**3 - cos(x) + 1
raises(PolynomialError, lambda: roots(eq))
def test_issue_17454():
assert roots([1, -3*(-4 - 4*I)**2/8 + 12*I, 0], multiple=True) == [0, 0]
def test_issue_20913():
assert Poly(x + 9671406556917067856609794, x).real_roots() == [-9671406556917067856609794]
assert Poly(x**3 + 4, x).real_roots() == [-2**(S(2)/3)]
def test_issue_22768():
e = Rational(1, 3)
r = (-1/a)**e*(a + 1)**(5*e)
assert roots(Poly(a*x**3 + (a + 1)**5, x)) == {
r: 1,
-r*(1 + sqrt(3)*I)/2: 1,
r*(-1 + sqrt(3)*I)/2: 1}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,300 @@
"""Tests for useful utilities for higher level polynomial classes. """
from sympy.core.mul import Mul
from sympy.core.numbers import (Integer, pi)
from sympy.core.relational import Eq
from sympy.core.singleton import S
from sympy.core.symbol import (Symbol, symbols)
from sympy.functions.elementary.exponential import exp
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.functions.elementary.trigonometric import (cos, sin)
from sympy.integrals.integrals import Integral
from sympy.testing.pytest import raises
from sympy.polys.polyutils import (
_nsort,
_sort_gens,
_unify_gens,
_analyze_gens,
_sort_factors,
parallel_dict_from_expr,
dict_from_expr,
)
from sympy.polys.polyerrors import PolynomialError
from sympy.polys.domains import ZZ
x, y, z, p, q, r, s, t, u, v, w = symbols('x,y,z,p,q,r,s,t,u,v,w')
A, B = symbols('A,B', commutative=False)
def test__nsort():
# issue 6137
r = S('''[3/2 + sqrt(-14/3 - 2*(-415/216 + 13*I/12)**(1/3) - 4/sqrt(-7/3 +
61/(18*(-415/216 + 13*I/12)**(1/3)) + 2*(-415/216 + 13*I/12)**(1/3)) -
61/(18*(-415/216 + 13*I/12)**(1/3)))/2 - sqrt(-7/3 + 61/(18*(-415/216
+ 13*I/12)**(1/3)) + 2*(-415/216 + 13*I/12)**(1/3))/2, 3/2 - sqrt(-7/3
+ 61/(18*(-415/216 + 13*I/12)**(1/3)) + 2*(-415/216 +
13*I/12)**(1/3))/2 - sqrt(-14/3 - 2*(-415/216 + 13*I/12)**(1/3) -
4/sqrt(-7/3 + 61/(18*(-415/216 + 13*I/12)**(1/3)) + 2*(-415/216 +
13*I/12)**(1/3)) - 61/(18*(-415/216 + 13*I/12)**(1/3)))/2, 3/2 +
sqrt(-14/3 - 2*(-415/216 + 13*I/12)**(1/3) + 4/sqrt(-7/3 +
61/(18*(-415/216 + 13*I/12)**(1/3)) + 2*(-415/216 + 13*I/12)**(1/3)) -
61/(18*(-415/216 + 13*I/12)**(1/3)))/2 + sqrt(-7/3 + 61/(18*(-415/216
+ 13*I/12)**(1/3)) + 2*(-415/216 + 13*I/12)**(1/3))/2, 3/2 + sqrt(-7/3
+ 61/(18*(-415/216 + 13*I/12)**(1/3)) + 2*(-415/216 +
13*I/12)**(1/3))/2 - sqrt(-14/3 - 2*(-415/216 + 13*I/12)**(1/3) +
4/sqrt(-7/3 + 61/(18*(-415/216 + 13*I/12)**(1/3)) + 2*(-415/216 +
13*I/12)**(1/3)) - 61/(18*(-415/216 + 13*I/12)**(1/3)))/2]''')
ans = [r[1], r[0], r[-1], r[-2]]
assert _nsort(r) == ans
assert len(_nsort(r, separated=True)[0]) == 0
b, c, a = exp(-1000), exp(-999), exp(-1001)
assert _nsort((b, c, a)) == [a, b, c]
# issue 12560
a = cos(1)**2 + sin(1)**2 - 1
assert _nsort([a]) == [a]
def test__sort_gens():
assert _sort_gens([]) == ()
assert _sort_gens([x]) == (x,)
assert _sort_gens([p]) == (p,)
assert _sort_gens([q]) == (q,)
assert _sort_gens([x, p]) == (x, p)
assert _sort_gens([p, x]) == (x, p)
assert _sort_gens([q, p]) == (p, q)
assert _sort_gens([q, p, x]) == (x, p, q)
assert _sort_gens([x, p, q], wrt=x) == (x, p, q)
assert _sort_gens([x, p, q], wrt=p) == (p, x, q)
assert _sort_gens([x, p, q], wrt=q) == (q, x, p)
assert _sort_gens([x, p, q], wrt='x') == (x, p, q)
assert _sort_gens([x, p, q], wrt='p') == (p, x, q)
assert _sort_gens([x, p, q], wrt='q') == (q, x, p)
assert _sort_gens([x, p, q], wrt='x,q') == (x, q, p)
assert _sort_gens([x, p, q], wrt='q,x') == (q, x, p)
assert _sort_gens([x, p, q], wrt='p,q') == (p, q, x)
assert _sort_gens([x, p, q], wrt='q,p') == (q, p, x)
assert _sort_gens([x, p, q], wrt='x, q') == (x, q, p)
assert _sort_gens([x, p, q], wrt='q, x') == (q, x, p)
assert _sort_gens([x, p, q], wrt='p, q') == (p, q, x)
assert _sort_gens([x, p, q], wrt='q, p') == (q, p, x)
assert _sort_gens([x, p, q], wrt=[x, 'q']) == (x, q, p)
assert _sort_gens([x, p, q], wrt=[q, 'x']) == (q, x, p)
assert _sort_gens([x, p, q], wrt=[p, 'q']) == (p, q, x)
assert _sort_gens([x, p, q], wrt=[q, 'p']) == (q, p, x)
assert _sort_gens([x, p, q], wrt=['x', 'q']) == (x, q, p)
assert _sort_gens([x, p, q], wrt=['q', 'x']) == (q, x, p)
assert _sort_gens([x, p, q], wrt=['p', 'q']) == (p, q, x)
assert _sort_gens([x, p, q], wrt=['q', 'p']) == (q, p, x)
assert _sort_gens([x, p, q], sort='x > p > q') == (x, p, q)
assert _sort_gens([x, p, q], sort='p > x > q') == (p, x, q)
assert _sort_gens([x, p, q], sort='p > q > x') == (p, q, x)
assert _sort_gens([x, p, q], wrt='x', sort='q > p') == (x, q, p)
assert _sort_gens([x, p, q], wrt='p', sort='q > x') == (p, q, x)
assert _sort_gens([x, p, q], wrt='q', sort='p > x') == (q, p, x)
# https://github.com/sympy/sympy/issues/19353
n1 = Symbol('\n1')
assert _sort_gens([n1]) == (n1,)
assert _sort_gens([x, n1]) == (x, n1)
X = symbols('x0,x1,x2,x10,x11,x12,x20,x21,x22')
assert _sort_gens(X) == X
def test__unify_gens():
assert _unify_gens([], []) == ()
assert _unify_gens([x], [x]) == (x,)
assert _unify_gens([y], [y]) == (y,)
assert _unify_gens([x, y], [x]) == (x, y)
assert _unify_gens([x], [x, y]) == (x, y)
assert _unify_gens([x, y], [x, y]) == (x, y)
assert _unify_gens([y, x], [y, x]) == (y, x)
assert _unify_gens([x], [y]) == (x, y)
assert _unify_gens([y], [x]) == (y, x)
assert _unify_gens([x], [y, x]) == (y, x)
assert _unify_gens([y, x], [x]) == (y, x)
assert _unify_gens([x, y, z], [x, y, z]) == (x, y, z)
assert _unify_gens([z, y, x], [x, y, z]) == (z, y, x)
assert _unify_gens([x, y, z], [z, y, x]) == (x, y, z)
assert _unify_gens([z, y, x], [z, y, x]) == (z, y, x)
assert _unify_gens([x, y, z], [t, x, p, q, z]) == (t, x, y, p, q, z)
def test__analyze_gens():
assert _analyze_gens((x, y, z)) == (x, y, z)
assert _analyze_gens([x, y, z]) == (x, y, z)
assert _analyze_gens(([x, y, z],)) == (x, y, z)
assert _analyze_gens(((x, y, z),)) == (x, y, z)
def test__sort_factors():
assert _sort_factors([], multiple=True) == []
assert _sort_factors([], multiple=False) == []
F = [[1, 2, 3], [1, 2], [1]]
G = [[1], [1, 2], [1, 2, 3]]
assert _sort_factors(F, multiple=False) == G
F = [[1, 2], [1, 2, 3], [1, 2], [1]]
G = [[1], [1, 2], [1, 2], [1, 2, 3]]
assert _sort_factors(F, multiple=False) == G
F = [[2, 2], [1, 2, 3], [1, 2], [1]]
G = [[1], [1, 2], [2, 2], [1, 2, 3]]
assert _sort_factors(F, multiple=False) == G
F = [([1, 2, 3], 1), ([1, 2], 1), ([1], 1)]
G = [([1], 1), ([1, 2], 1), ([1, 2, 3], 1)]
assert _sort_factors(F, multiple=True) == G
F = [([1, 2], 1), ([1, 2, 3], 1), ([1, 2], 1), ([1], 1)]
G = [([1], 1), ([1, 2], 1), ([1, 2], 1), ([1, 2, 3], 1)]
assert _sort_factors(F, multiple=True) == G
F = [([2, 2], 1), ([1, 2, 3], 1), ([1, 2], 1), ([1], 1)]
G = [([1], 1), ([1, 2], 1), ([2, 2], 1), ([1, 2, 3], 1)]
assert _sort_factors(F, multiple=True) == G
F = [([2, 2], 1), ([1, 2, 3], 1), ([1, 2], 2), ([1], 1)]
G = [([1], 1), ([2, 2], 1), ([1, 2], 2), ([1, 2, 3], 1)]
assert _sort_factors(F, multiple=True) == G
def test__dict_from_expr_if_gens():
assert dict_from_expr(
Integer(17), gens=(x,)) == ({(0,): Integer(17)}, (x,))
assert dict_from_expr(
Integer(17), gens=(x, y)) == ({(0, 0): Integer(17)}, (x, y))
assert dict_from_expr(
Integer(17), gens=(x, y, z)) == ({(0, 0, 0): Integer(17)}, (x, y, z))
assert dict_from_expr(
Integer(-17), gens=(x,)) == ({(0,): Integer(-17)}, (x,))
assert dict_from_expr(
Integer(-17), gens=(x, y)) == ({(0, 0): Integer(-17)}, (x, y))
assert dict_from_expr(Integer(
-17), gens=(x, y, z)) == ({(0, 0, 0): Integer(-17)}, (x, y, z))
assert dict_from_expr(
Integer(17)*x, gens=(x,)) == ({(1,): Integer(17)}, (x,))
assert dict_from_expr(
Integer(17)*x, gens=(x, y)) == ({(1, 0): Integer(17)}, (x, y))
assert dict_from_expr(Integer(
17)*x, gens=(x, y, z)) == ({(1, 0, 0): Integer(17)}, (x, y, z))
assert dict_from_expr(
Integer(17)*x**7, gens=(x,)) == ({(7,): Integer(17)}, (x,))
assert dict_from_expr(
Integer(17)*x**7*y, gens=(x, y)) == ({(7, 1): Integer(17)}, (x, y))
assert dict_from_expr(Integer(17)*x**7*y*z**12, gens=(
x, y, z)) == ({(7, 1, 12): Integer(17)}, (x, y, z))
assert dict_from_expr(x + 2*y + 3*z, gens=(x,)) == \
({(1,): Integer(1), (0,): 2*y + 3*z}, (x,))
assert dict_from_expr(x + 2*y + 3*z, gens=(x, y)) == \
({(1, 0): Integer(1), (0, 1): Integer(2), (0, 0): 3*z}, (x, y))
assert dict_from_expr(x + 2*y + 3*z, gens=(x, y, z)) == \
({(1, 0, 0): Integer(
1), (0, 1, 0): Integer(2), (0, 0, 1): Integer(3)}, (x, y, z))
assert dict_from_expr(x*y + 2*x*z + 3*y*z, gens=(x,)) == \
({(1,): y + 2*z, (0,): 3*y*z}, (x,))
assert dict_from_expr(x*y + 2*x*z + 3*y*z, gens=(x, y)) == \
({(1, 1): Integer(1), (1, 0): 2*z, (0, 1): 3*z}, (x, y))
assert dict_from_expr(x*y + 2*x*z + 3*y*z, gens=(x, y, z)) == \
({(1, 1, 0): Integer(
1), (1, 0, 1): Integer(2), (0, 1, 1): Integer(3)}, (x, y, z))
assert dict_from_expr(2**y*x, gens=(x,)) == ({(1,): 2**y}, (x,))
assert dict_from_expr(Integral(x, (x, 1, 2)) + x) == (
{(0, 1): 1, (1, 0): 1}, (x, Integral(x, (x, 1, 2))))
raises(PolynomialError, lambda: dict_from_expr(2**y*x, gens=(x, y)))
def test__dict_from_expr_no_gens():
assert dict_from_expr(Integer(17)) == ({(): Integer(17)}, ())
assert dict_from_expr(x) == ({(1,): Integer(1)}, (x,))
assert dict_from_expr(y) == ({(1,): Integer(1)}, (y,))
assert dict_from_expr(x*y) == ({(1, 1): Integer(1)}, (x, y))
assert dict_from_expr(
x + y) == ({(1, 0): Integer(1), (0, 1): Integer(1)}, (x, y))
assert dict_from_expr(sqrt(2)) == ({(1,): Integer(1)}, (sqrt(2),))
assert dict_from_expr(sqrt(2), greedy=False) == ({(): sqrt(2)}, ())
assert dict_from_expr(x*y, domain=ZZ[x]) == ({(1,): x}, (y,))
assert dict_from_expr(x*y, domain=ZZ[y]) == ({(1,): y}, (x,))
assert dict_from_expr(3*sqrt(
2)*pi*x*y, extension=None) == ({(1, 1, 1, 1): 3}, (x, y, pi, sqrt(2)))
assert dict_from_expr(3*sqrt(
2)*pi*x*y, extension=True) == ({(1, 1, 1): 3*sqrt(2)}, (x, y, pi))
assert dict_from_expr(3*sqrt(
2)*pi*x*y, extension=True) == ({(1, 1, 1): 3*sqrt(2)}, (x, y, pi))
f = cos(x)*sin(x) + cos(x)*sin(y) + cos(y)*sin(x) + cos(y)*sin(y)
assert dict_from_expr(f) == ({(0, 1, 0, 1): 1, (0, 1, 1, 0): 1,
(1, 0, 0, 1): 1, (1, 0, 1, 0): 1}, (cos(x), cos(y), sin(x), sin(y)))
def test__parallel_dict_from_expr_if_gens():
assert parallel_dict_from_expr([x + 2*y + 3*z, Integer(7)], gens=(x,)) == \
([{(1,): Integer(1), (0,): 2*y + 3*z}, {(0,): Integer(7)}], (x,))
def test__parallel_dict_from_expr_no_gens():
assert parallel_dict_from_expr([x*y, Integer(3)]) == \
([{(1, 1): Integer(1)}, {(0, 0): Integer(3)}], (x, y))
assert parallel_dict_from_expr([x*y, 2*z, Integer(3)]) == \
([{(1, 1, 0): Integer(
1)}, {(0, 0, 1): Integer(2)}, {(0, 0, 0): Integer(3)}], (x, y, z))
assert parallel_dict_from_expr((Mul(x, x**2, evaluate=False),)) == \
([{(3,): 1}], (x,))
def test_parallel_dict_from_expr():
assert parallel_dict_from_expr([Eq(x, 1), Eq(
x**2, 2)]) == ([{(0,): -Integer(1), (1,): Integer(1)},
{(0,): -Integer(2), (2,): Integer(1)}], (x,))
raises(PolynomialError, lambda: parallel_dict_from_expr([A*B - B*A]))
def test_dict_from_expr():
assert dict_from_expr(Eq(x, 1)) == \
({(0,): -Integer(1), (1,): Integer(1)}, (x,))
raises(PolynomialError, lambda: dict_from_expr(A*B - B*A))
raises(PolynomialError, lambda: dict_from_expr(S.true))

View File

@ -0,0 +1,139 @@
"""Tests for PythonRational type. """
from sympy.polys.domains import PythonRational as QQ
from sympy.testing.pytest import raises
def test_PythonRational__init__():
assert QQ(0).numerator == 0
assert QQ(0).denominator == 1
assert QQ(0, 1).numerator == 0
assert QQ(0, 1).denominator == 1
assert QQ(0, -1).numerator == 0
assert QQ(0, -1).denominator == 1
assert QQ(1).numerator == 1
assert QQ(1).denominator == 1
assert QQ(1, 1).numerator == 1
assert QQ(1, 1).denominator == 1
assert QQ(-1, -1).numerator == 1
assert QQ(-1, -1).denominator == 1
assert QQ(-1).numerator == -1
assert QQ(-1).denominator == 1
assert QQ(-1, 1).numerator == -1
assert QQ(-1, 1).denominator == 1
assert QQ( 1, -1).numerator == -1
assert QQ( 1, -1).denominator == 1
assert QQ(1, 2).numerator == 1
assert QQ(1, 2).denominator == 2
assert QQ(3, 4).numerator == 3
assert QQ(3, 4).denominator == 4
assert QQ(2, 2).numerator == 1
assert QQ(2, 2).denominator == 1
assert QQ(2, 4).numerator == 1
assert QQ(2, 4).denominator == 2
def test_PythonRational__hash__():
assert hash(QQ(0)) == hash(0)
assert hash(QQ(1)) == hash(1)
assert hash(QQ(117)) == hash(117)
def test_PythonRational__int__():
assert int(QQ(-1, 4)) == 0
assert int(QQ( 1, 4)) == 0
assert int(QQ(-5, 4)) == -1
assert int(QQ( 5, 4)) == 1
def test_PythonRational__float__():
assert float(QQ(-1, 2)) == -0.5
assert float(QQ( 1, 2)) == 0.5
def test_PythonRational__abs__():
assert abs(QQ(-1, 2)) == QQ(1, 2)
assert abs(QQ( 1, 2)) == QQ(1, 2)
def test_PythonRational__pos__():
assert +QQ(-1, 2) == QQ(-1, 2)
assert +QQ( 1, 2) == QQ( 1, 2)
def test_PythonRational__neg__():
assert -QQ(-1, 2) == QQ( 1, 2)
assert -QQ( 1, 2) == QQ(-1, 2)
def test_PythonRational__add__():
assert QQ(-1, 2) + QQ( 1, 2) == QQ(0)
assert QQ( 1, 2) + QQ(-1, 2) == QQ(0)
assert QQ(1, 2) + QQ(1, 2) == QQ(1)
assert QQ(1, 2) + QQ(3, 2) == QQ(2)
assert QQ(3, 2) + QQ(1, 2) == QQ(2)
assert QQ(3, 2) + QQ(3, 2) == QQ(3)
assert 1 + QQ(1, 2) == QQ(3, 2)
assert QQ(1, 2) + 1 == QQ(3, 2)
def test_PythonRational__sub__():
assert QQ(-1, 2) - QQ( 1, 2) == QQ(-1)
assert QQ( 1, 2) - QQ(-1, 2) == QQ( 1)
assert QQ(1, 2) - QQ(1, 2) == QQ( 0)
assert QQ(1, 2) - QQ(3, 2) == QQ(-1)
assert QQ(3, 2) - QQ(1, 2) == QQ( 1)
assert QQ(3, 2) - QQ(3, 2) == QQ( 0)
assert 1 - QQ(1, 2) == QQ( 1, 2)
assert QQ(1, 2) - 1 == QQ(-1, 2)
def test_PythonRational__mul__():
assert QQ(-1, 2) * QQ( 1, 2) == QQ(-1, 4)
assert QQ( 1, 2) * QQ(-1, 2) == QQ(-1, 4)
assert QQ(1, 2) * QQ(1, 2) == QQ(1, 4)
assert QQ(1, 2) * QQ(3, 2) == QQ(3, 4)
assert QQ(3, 2) * QQ(1, 2) == QQ(3, 4)
assert QQ(3, 2) * QQ(3, 2) == QQ(9, 4)
assert 2 * QQ(1, 2) == QQ(1)
assert QQ(1, 2) * 2 == QQ(1)
def test_PythonRational__truediv__():
assert QQ(-1, 2) / QQ( 1, 2) == QQ(-1)
assert QQ( 1, 2) / QQ(-1, 2) == QQ(-1)
assert QQ(1, 2) / QQ(1, 2) == QQ(1)
assert QQ(1, 2) / QQ(3, 2) == QQ(1, 3)
assert QQ(3, 2) / QQ(1, 2) == QQ(3)
assert QQ(3, 2) / QQ(3, 2) == QQ(1)
assert 2 / QQ(1, 2) == QQ(4)
assert QQ(1, 2) / 2 == QQ(1, 4)
raises(ZeroDivisionError, lambda: QQ(1, 2) / QQ(0))
raises(ZeroDivisionError, lambda: QQ(1, 2) / 0)
def test_PythonRational__pow__():
assert QQ(1)**10 == QQ(1)
assert QQ(2)**10 == QQ(1024)
assert QQ(1)**(-10) == QQ(1)
assert QQ(2)**(-10) == QQ(1, 1024)
def test_PythonRational__eq__():
assert (QQ(1, 2) == QQ(1, 2)) is True
assert (QQ(1, 2) != QQ(1, 2)) is False
assert (QQ(1, 2) == QQ(1, 3)) is False
assert (QQ(1, 2) != QQ(1, 3)) is True
def test_PythonRational__lt_le_gt_ge__():
assert (QQ(1, 2) < QQ(1, 4)) is False
assert (QQ(1, 2) <= QQ(1, 4)) is False
assert (QQ(1, 2) > QQ(1, 4)) is True
assert (QQ(1, 2) >= QQ(1, 4)) is True
assert (QQ(1, 4) < QQ(1, 2)) is True
assert (QQ(1, 4) <= QQ(1, 2)) is True
assert (QQ(1, 4) > QQ(1, 2)) is False
assert (QQ(1, 4) >= QQ(1, 2)) is False

View File

@ -0,0 +1,63 @@
"""Tests for tools for manipulation of rational expressions. """
from sympy.polys.rationaltools import together
from sympy.core.mul import Mul
from sympy.core.numbers import Rational
from sympy.core.relational import Eq
from sympy.core.singleton import S
from sympy.core.symbol import symbols
from sympy.functions.elementary.exponential import exp
from sympy.functions.elementary.trigonometric import sin
from sympy.integrals.integrals import Integral
from sympy.abc import x, y, z
A, B = symbols('A,B', commutative=False)
def test_together():
assert together(0) == 0
assert together(1) == 1
assert together(x*y*z) == x*y*z
assert together(x + y) == x + y
assert together(1/x) == 1/x
assert together(1/x + 1) == (x + 1)/x
assert together(1/x + 3) == (3*x + 1)/x
assert together(1/x + x) == (x**2 + 1)/x
assert together(1/x + S.Half) == (x + 2)/(2*x)
assert together(S.Half + x/2) == Mul(S.Half, x + 1, evaluate=False)
assert together(1/x + 2/y) == (2*x + y)/(y*x)
assert together(1/(1 + 1/x)) == x/(1 + x)
assert together(x/(1 + 1/x)) == x**2/(1 + x)
assert together(1/x + 1/y + 1/z) == (x*y + x*z + y*z)/(x*y*z)
assert together(1/(1 + x + 1/y + 1/z)) == y*z/(y + z + y*z + x*y*z)
assert together(1/(x*y) + 1/(x*y)**2) == y**(-2)*x**(-2)*(1 + x*y)
assert together(1/(x*y) + 1/(x*y)**4) == y**(-4)*x**(-4)*(1 + x**3*y**3)
assert together(1/(x**7*y) + 1/(x*y)**4) == y**(-4)*x**(-7)*(x**3 + y**3)
assert together(5/(2 + 6/(3 + 7/(4 + 8/(5 + 9/x))))) == \
Rational(5, 2)*((171 + 119*x)/(279 + 203*x))
assert together(1 + 1/(x + 1)**2) == (1 + (x + 1)**2)/(x + 1)**2
assert together(1 + 1/(x*(1 + x))) == (1 + x*(1 + x))/(x*(1 + x))
assert together(
1/(x*(x + 1)) + 1/(x*(x + 2))) == (3 + 2*x)/(x*(1 + x)*(2 + x))
assert together(1 + 1/(2*x + 2)**2) == (4*(x + 1)**2 + 1)/(4*(x + 1)**2)
assert together(sin(1/x + 1/y)) == sin(1/x + 1/y)
assert together(sin(1/x + 1/y), deep=True) == sin((x + y)/(x*y))
assert together(1/exp(x) + 1/(x*exp(x))) == (1 + x)/(x*exp(x))
assert together(1/exp(2*x) + 1/(x*exp(3*x))) == (1 + exp(x)*x)/(x*exp(3*x))
assert together(Integral(1/x + 1/y, x)) == Integral((x + y)/(x*y), x)
assert together(Eq(1/x + 1/y, 1 + 1/z)) == Eq((x + y)/(x*y), (z + 1)/z)
assert together((A*B)**-1 + (B*A)**-1) == (A*B)**-1 + (B*A)**-1

View File

@ -0,0 +1,635 @@
from sympy.polys.domains import QQ, EX, RR
from sympy.polys.rings import ring
from sympy.polys.ring_series import (_invert_monoms, rs_integrate,
rs_trunc, rs_mul, rs_square, rs_pow, _has_constant_term, rs_hadamard_exp,
rs_series_from_list, rs_exp, rs_log, rs_newton, rs_series_inversion,
rs_compose_add, rs_asin, rs_atan, rs_atanh, rs_tan, rs_cot, rs_sin, rs_cos,
rs_cos_sin, rs_sinh, rs_cosh, rs_tanh, _tan1, rs_fun, rs_nth_root,
rs_LambertW, rs_series_reversion, rs_is_puiseux, rs_series)
from sympy.testing.pytest import raises, slow
from sympy.core.symbol import symbols
from sympy.functions import (sin, cos, exp, tan, cot, atan, atanh,
tanh, log, sqrt)
from sympy.core.numbers import Rational
from sympy.core import expand, S
def is_close(a, b):
tol = 10**(-10)
assert abs(a - b) < tol
def test_ring_series1():
R, x = ring('x', QQ)
p = x**4 + 2*x**3 + 3*x + 4
assert _invert_monoms(p) == 4*x**4 + 3*x**3 + 2*x + 1
assert rs_hadamard_exp(p) == x**4/24 + x**3/3 + 3*x + 4
R, x = ring('x', QQ)
p = x**4 + 2*x**3 + 3*x + 4
assert rs_integrate(p, x) == x**5/5 + x**4/2 + 3*x**2/2 + 4*x
R, x, y = ring('x, y', QQ)
p = x**2*y**2 + x + 1
assert rs_integrate(p, x) == x**3*y**2/3 + x**2/2 + x
assert rs_integrate(p, y) == x**2*y**3/3 + x*y + y
def test_trunc():
R, x, y, t = ring('x, y, t', QQ)
p = (y + t*x)**4
p1 = rs_trunc(p, x, 3)
assert p1 == y**4 + 4*y**3*t*x + 6*y**2*t**2*x**2
def test_mul_trunc():
R, x, y, t = ring('x, y, t', QQ)
p = 1 + t*x + t*y
for i in range(2):
p = rs_mul(p, p, t, 3)
assert p == 6*x**2*t**2 + 12*x*y*t**2 + 6*y**2*t**2 + 4*x*t + 4*y*t + 1
p = 1 + t*x + t*y + t**2*x*y
p1 = rs_mul(p, p, t, 2)
assert p1 == 1 + 2*t*x + 2*t*y
R1, z = ring('z', QQ)
raises(ValueError, lambda: rs_mul(p, z, x, 2))
p1 = 2 + 2*x + 3*x**2
p2 = 3 + x**2
assert rs_mul(p1, p2, x, 4) == 2*x**3 + 11*x**2 + 6*x + 6
def test_square_trunc():
R, x, y, t = ring('x, y, t', QQ)
p = (1 + t*x + t*y)*2
p1 = rs_mul(p, p, x, 3)
p2 = rs_square(p, x, 3)
assert p1 == p2
p = 1 + x + x**2 + x**3
assert rs_square(p, x, 4) == 4*x**3 + 3*x**2 + 2*x + 1
def test_pow_trunc():
R, x, y, z = ring('x, y, z', QQ)
p0 = y + x*z
p = p0**16
for xx in (x, y, z):
p1 = rs_trunc(p, xx, 8)
p2 = rs_pow(p0, 16, xx, 8)
assert p1 == p2
p = 1 + x
p1 = rs_pow(p, 3, x, 2)
assert p1 == 1 + 3*x
assert rs_pow(p, 0, x, 2) == 1
assert rs_pow(p, -2, x, 2) == 1 - 2*x
p = x + y
assert rs_pow(p, 3, y, 3) == x**3 + 3*x**2*y + 3*x*y**2
assert rs_pow(1 + x, Rational(2, 3), x, 4) == 4*x**3/81 - x**2/9 + x*Rational(2, 3) + 1
def test_has_constant_term():
R, x, y, z = ring('x, y, z', QQ)
p = y + x*z
assert _has_constant_term(p, x)
p = x + x**4
assert not _has_constant_term(p, x)
p = 1 + x + x**4
assert _has_constant_term(p, x)
p = x + y + x*z
def test_inversion():
R, x = ring('x', QQ)
p = 2 + x + 2*x**2
n = 5
p1 = rs_series_inversion(p, x, n)
assert rs_trunc(p*p1, x, n) == 1
R, x, y = ring('x, y', QQ)
p = 2 + x + 2*x**2 + y*x + x**2*y
p1 = rs_series_inversion(p, x, n)
assert rs_trunc(p*p1, x, n) == 1
R, x, y = ring('x, y', QQ)
p = 1 + x + y
raises(NotImplementedError, lambda: rs_series_inversion(p, x, 4))
p = R.zero
raises(ZeroDivisionError, lambda: rs_series_inversion(p, x, 3))
def test_series_reversion():
R, x, y = ring('x, y', QQ)
p = rs_tan(x, x, 10)
assert rs_series_reversion(p, x, 8, y) == rs_atan(y, y, 8)
p = rs_sin(x, x, 10)
assert rs_series_reversion(p, x, 8, y) == 5*y**7/112 + 3*y**5/40 + \
y**3/6 + y
def test_series_from_list():
R, x = ring('x', QQ)
p = 1 + 2*x + x**2 + 3*x**3
c = [1, 2, 0, 4, 4]
r = rs_series_from_list(p, c, x, 5)
pc = R.from_list(list(reversed(c)))
r1 = rs_trunc(pc.compose(x, p), x, 5)
assert r == r1
R, x, y = ring('x, y', QQ)
c = [1, 3, 5, 7]
p1 = rs_series_from_list(x + y, c, x, 3, concur=0)
p2 = rs_trunc((1 + 3*(x+y) + 5*(x+y)**2 + 7*(x+y)**3), x, 3)
assert p1 == p2
R, x = ring('x', QQ)
h = 25
p = rs_exp(x, x, h) - 1
p1 = rs_series_from_list(p, c, x, h)
p2 = 0
for i, cx in enumerate(c):
p2 += cx*rs_pow(p, i, x, h)
assert p1 == p2
def test_log():
R, x = ring('x', QQ)
p = 1 + x
p1 = rs_log(p, x, 4)/x**2
assert p1 == Rational(1, 3)*x - S.Half + x**(-1)
p = 1 + x +2*x**2/3
p1 = rs_log(p, x, 9)
assert p1 == -17*x**8/648 + 13*x**7/189 - 11*x**6/162 - x**5/45 + \
7*x**4/36 - x**3/3 + x**2/6 + x
p2 = rs_series_inversion(p, x, 9)
p3 = rs_log(p2, x, 9)
assert p3 == -p1
R, x, y = ring('x, y', QQ)
p = 1 + x + 2*y*x**2
p1 = rs_log(p, x, 6)
assert p1 == (4*x**5*y**2 - 2*x**5*y - 2*x**4*y**2 + x**5/5 + 2*x**4*y -
x**4/4 - 2*x**3*y + x**3/3 + 2*x**2*y - x**2/2 + x)
# Constant term in series
a = symbols('a')
R, x, y = ring('x, y', EX)
assert rs_log(x + a, x, 5) == -EX(1/(4*a**4))*x**4 + EX(1/(3*a**3))*x**3 \
- EX(1/(2*a**2))*x**2 + EX(1/a)*x + EX(log(a))
assert rs_log(x + x**2*y + a, x, 4) == -EX(a**(-2))*x**3*y + \
EX(1/(3*a**3))*x**3 + EX(1/a)*x**2*y - EX(1/(2*a**2))*x**2 + \
EX(1/a)*x + EX(log(a))
p = x + x**2 + 3
assert rs_log(p, x, 10).compose(x, 5) == EX(log(3) + Rational(19281291595, 9920232))
def test_exp():
R, x = ring('x', QQ)
p = x + x**4
for h in [10, 30]:
q = rs_series_inversion(1 + p, x, h) - 1
p1 = rs_exp(q, x, h)
q1 = rs_log(p1, x, h)
assert q1 == q
p1 = rs_exp(p, x, 30)
assert p1.coeff(x**29) == QQ(74274246775059676726972369, 353670479749588078181744640000)
prec = 21
p = rs_log(1 + x, x, prec)
p1 = rs_exp(p, x, prec)
assert p1 == x + 1
# Constant term in series
a = symbols('a')
R, x, y = ring('x, y', QQ[exp(a), a])
assert rs_exp(x + a, x, 5) == exp(a)*x**4/24 + exp(a)*x**3/6 + \
exp(a)*x**2/2 + exp(a)*x + exp(a)
assert rs_exp(x + x**2*y + a, x, 5) == exp(a)*x**4*y**2/2 + \
exp(a)*x**4*y/2 + exp(a)*x**4/24 + exp(a)*x**3*y + \
exp(a)*x**3/6 + exp(a)*x**2*y + exp(a)*x**2/2 + exp(a)*x + exp(a)
R, x, y = ring('x, y', EX)
assert rs_exp(x + a, x, 5) == EX(exp(a)/24)*x**4 + EX(exp(a)/6)*x**3 + \
EX(exp(a)/2)*x**2 + EX(exp(a))*x + EX(exp(a))
assert rs_exp(x + x**2*y + a, x, 5) == EX(exp(a)/2)*x**4*y**2 + \
EX(exp(a)/2)*x**4*y + EX(exp(a)/24)*x**4 + EX(exp(a))*x**3*y + \
EX(exp(a)/6)*x**3 + EX(exp(a))*x**2*y + EX(exp(a)/2)*x**2 + \
EX(exp(a))*x + EX(exp(a))
def test_newton():
R, x = ring('x', QQ)
p = x**2 - 2
r = rs_newton(p, x, 4)
assert r == 8*x**4 + 4*x**2 + 2
def test_compose_add():
R, x = ring('x', QQ)
p1 = x**3 - 1
p2 = x**2 - 2
assert rs_compose_add(p1, p2) == x**6 - 6*x**4 - 2*x**3 + 12*x**2 - 12*x - 7
def test_fun():
R, x, y = ring('x, y', QQ)
p = x*y + x**2*y**3 + x**5*y
assert rs_fun(p, rs_tan, x, 10) == rs_tan(p, x, 10)
assert rs_fun(p, _tan1, x, 10) == _tan1(p, x, 10)
def test_nth_root():
R, x, y = ring('x, y', QQ)
assert rs_nth_root(1 + x**2*y, 4, x, 10) == -77*x**8*y**4/2048 + \
7*x**6*y**3/128 - 3*x**4*y**2/32 + x**2*y/4 + 1
assert rs_nth_root(1 + x*y + x**2*y**3, 3, x, 5) == -x**4*y**6/9 + \
5*x**4*y**5/27 - 10*x**4*y**4/243 - 2*x**3*y**4/9 + 5*x**3*y**3/81 + \
x**2*y**3/3 - x**2*y**2/9 + x*y/3 + 1
assert rs_nth_root(8*x, 3, x, 3) == 2*x**QQ(1, 3)
assert rs_nth_root(8*x + x**2 + x**3, 3, x, 3) == x**QQ(4,3)/12 + 2*x**QQ(1,3)
r = rs_nth_root(8*x + x**2*y + x**3, 3, x, 4)
assert r == -x**QQ(7,3)*y**2/288 + x**QQ(7,3)/12 + x**QQ(4,3)*y/12 + 2*x**QQ(1,3)
# Constant term in series
a = symbols('a')
R, x, y = ring('x, y', EX)
assert rs_nth_root(x + a, 3, x, 4) == EX(5/(81*a**QQ(8, 3)))*x**3 - \
EX(1/(9*a**QQ(5, 3)))*x**2 + EX(1/(3*a**QQ(2, 3)))*x + EX(a**QQ(1, 3))
assert rs_nth_root(x**QQ(2, 3) + x**2*y + 5, 2, x, 3) == -EX(sqrt(5)/100)*\
x**QQ(8, 3)*y - EX(sqrt(5)/16000)*x**QQ(8, 3) + EX(sqrt(5)/10)*x**2*y + \
EX(sqrt(5)/2000)*x**2 - EX(sqrt(5)/200)*x**QQ(4, 3) + \
EX(sqrt(5)/10)*x**QQ(2, 3) + EX(sqrt(5))
def test_atan():
R, x, y = ring('x, y', QQ)
assert rs_atan(x, x, 9) == -x**7/7 + x**5/5 - x**3/3 + x
assert rs_atan(x*y + x**2*y**3, x, 9) == 2*x**8*y**11 - x**8*y**9 + \
2*x**7*y**9 - x**7*y**7/7 - x**6*y**9/3 + x**6*y**7 - x**5*y**7 + \
x**5*y**5/5 - x**4*y**5 - x**3*y**3/3 + x**2*y**3 + x*y
# Constant term in series
a = symbols('a')
R, x, y = ring('x, y', EX)
assert rs_atan(x + a, x, 5) == -EX((a**3 - a)/(a**8 + 4*a**6 + 6*a**4 + \
4*a**2 + 1))*x**4 + EX((3*a**2 - 1)/(3*a**6 + 9*a**4 + \
9*a**2 + 3))*x**3 - EX(a/(a**4 + 2*a**2 + 1))*x**2 + \
EX(1/(a**2 + 1))*x + EX(atan(a))
assert rs_atan(x + x**2*y + a, x, 4) == -EX(2*a/(a**4 + 2*a**2 + 1)) \
*x**3*y + EX((3*a**2 - 1)/(3*a**6 + 9*a**4 + 9*a**2 + 3))*x**3 + \
EX(1/(a**2 + 1))*x**2*y - EX(a/(a**4 + 2*a**2 + 1))*x**2 + EX(1/(a**2 \
+ 1))*x + EX(atan(a))
def test_asin():
R, x, y = ring('x, y', QQ)
assert rs_asin(x + x*y, x, 5) == x**3*y**3/6 + x**3*y**2/2 + x**3*y/2 + \
x**3/6 + x*y + x
assert rs_asin(x*y + x**2*y**3, x, 6) == x**5*y**7/2 + 3*x**5*y**5/40 + \
x**4*y**5/2 + x**3*y**3/6 + x**2*y**3 + x*y
def test_tan():
R, x, y = ring('x, y', QQ)
assert rs_tan(x, x, 9)/x**5 == \
Rational(17, 315)*x**2 + Rational(2, 15) + Rational(1, 3)*x**(-2) + x**(-4)
assert rs_tan(x*y + x**2*y**3, x, 9) == 4*x**8*y**11/3 + 17*x**8*y**9/45 + \
4*x**7*y**9/3 + 17*x**7*y**7/315 + x**6*y**9/3 + 2*x**6*y**7/3 + \
x**5*y**7 + 2*x**5*y**5/15 + x**4*y**5 + x**3*y**3/3 + x**2*y**3 + x*y
# Constant term in series
a = symbols('a')
R, x, y = ring('x, y', QQ[tan(a), a])
assert rs_tan(x + a, x, 5) == (tan(a)**5 + 5*tan(a)**3/3 +
2*tan(a)/3)*x**4 + (tan(a)**4 + 4*tan(a)**2/3 + Rational(1, 3))*x**3 + \
(tan(a)**3 + tan(a))*x**2 + (tan(a)**2 + 1)*x + tan(a)
assert rs_tan(x + x**2*y + a, x, 4) == (2*tan(a)**3 + 2*tan(a))*x**3*y + \
(tan(a)**4 + Rational(4, 3)*tan(a)**2 + Rational(1, 3))*x**3 + (tan(a)**2 + 1)*x**2*y + \
(tan(a)**3 + tan(a))*x**2 + (tan(a)**2 + 1)*x + tan(a)
R, x, y = ring('x, y', EX)
assert rs_tan(x + a, x, 5) == EX(tan(a)**5 + 5*tan(a)**3/3 +
2*tan(a)/3)*x**4 + EX(tan(a)**4 + 4*tan(a)**2/3 + EX(1)/3)*x**3 + \
EX(tan(a)**3 + tan(a))*x**2 + EX(tan(a)**2 + 1)*x + EX(tan(a))
assert rs_tan(x + x**2*y + a, x, 4) == EX(2*tan(a)**3 +
2*tan(a))*x**3*y + EX(tan(a)**4 + 4*tan(a)**2/3 + EX(1)/3)*x**3 + \
EX(tan(a)**2 + 1)*x**2*y + EX(tan(a)**3 + tan(a))*x**2 + \
EX(tan(a)**2 + 1)*x + EX(tan(a))
p = x + x**2 + 5
assert rs_atan(p, x, 10).compose(x, 10) == EX(atan(5) + S(67701870330562640) / \
668083460499)
def test_cot():
R, x, y = ring('x, y', QQ)
assert rs_cot(x**6 + x**7, x, 8) == x**(-6) - x**(-5) + x**(-4) - \
x**(-3) + x**(-2) - x**(-1) + 1 - x + x**2 - x**3 + x**4 - x**5 + \
2*x**6/3 - 4*x**7/3
assert rs_cot(x + x**2*y, x, 5) == -x**4*y**5 - x**4*y/15 + x**3*y**4 - \
x**3/45 - x**2*y**3 - x**2*y/3 + x*y**2 - x/3 - y + x**(-1)
def test_sin():
R, x, y = ring('x, y', QQ)
assert rs_sin(x, x, 9)/x**5 == \
Rational(-1, 5040)*x**2 + Rational(1, 120) - Rational(1, 6)*x**(-2) + x**(-4)
assert rs_sin(x*y + x**2*y**3, x, 9) == x**8*y**11/12 - \
x**8*y**9/720 + x**7*y**9/12 - x**7*y**7/5040 - x**6*y**9/6 + \
x**6*y**7/24 - x**5*y**7/2 + x**5*y**5/120 - x**4*y**5/2 - \
x**3*y**3/6 + x**2*y**3 + x*y
# Constant term in series
a = symbols('a')
R, x, y = ring('x, y', QQ[sin(a), cos(a), a])
assert rs_sin(x + a, x, 5) == sin(a)*x**4/24 - cos(a)*x**3/6 - \
sin(a)*x**2/2 + cos(a)*x + sin(a)
assert rs_sin(x + x**2*y + a, x, 5) == -sin(a)*x**4*y**2/2 - \
cos(a)*x**4*y/2 + sin(a)*x**4/24 - sin(a)*x**3*y - cos(a)*x**3/6 + \
cos(a)*x**2*y - sin(a)*x**2/2 + cos(a)*x + sin(a)
R, x, y = ring('x, y', EX)
assert rs_sin(x + a, x, 5) == EX(sin(a)/24)*x**4 - EX(cos(a)/6)*x**3 - \
EX(sin(a)/2)*x**2 + EX(cos(a))*x + EX(sin(a))
assert rs_sin(x + x**2*y + a, x, 5) == -EX(sin(a)/2)*x**4*y**2 - \
EX(cos(a)/2)*x**4*y + EX(sin(a)/24)*x**4 - EX(sin(a))*x**3*y - \
EX(cos(a)/6)*x**3 + EX(cos(a))*x**2*y - EX(sin(a)/2)*x**2 + \
EX(cos(a))*x + EX(sin(a))
def test_cos():
R, x, y = ring('x, y', QQ)
assert rs_cos(x, x, 9)/x**5 == \
Rational(1, 40320)*x**3 - Rational(1, 720)*x + Rational(1, 24)*x**(-1) - S.Half*x**(-3) + x**(-5)
assert rs_cos(x*y + x**2*y**3, x, 9) == x**8*y**12/24 - \
x**8*y**10/48 + x**8*y**8/40320 + x**7*y**10/6 - \
x**7*y**8/120 + x**6*y**8/4 - x**6*y**6/720 + x**5*y**6/6 - \
x**4*y**6/2 + x**4*y**4/24 - x**3*y**4 - x**2*y**2/2 + 1
# Constant term in series
a = symbols('a')
R, x, y = ring('x, y', QQ[sin(a), cos(a), a])
assert rs_cos(x + a, x, 5) == cos(a)*x**4/24 + sin(a)*x**3/6 - \
cos(a)*x**2/2 - sin(a)*x + cos(a)
assert rs_cos(x + x**2*y + a, x, 5) == -cos(a)*x**4*y**2/2 + \
sin(a)*x**4*y/2 + cos(a)*x**4/24 - cos(a)*x**3*y + sin(a)*x**3/6 - \
sin(a)*x**2*y - cos(a)*x**2/2 - sin(a)*x + cos(a)
R, x, y = ring('x, y', EX)
assert rs_cos(x + a, x, 5) == EX(cos(a)/24)*x**4 + EX(sin(a)/6)*x**3 - \
EX(cos(a)/2)*x**2 - EX(sin(a))*x + EX(cos(a))
assert rs_cos(x + x**2*y + a, x, 5) == -EX(cos(a)/2)*x**4*y**2 + \
EX(sin(a)/2)*x**4*y + EX(cos(a)/24)*x**4 - EX(cos(a))*x**3*y + \
EX(sin(a)/6)*x**3 - EX(sin(a))*x**2*y - EX(cos(a)/2)*x**2 - \
EX(sin(a))*x + EX(cos(a))
def test_cos_sin():
R, x, y = ring('x, y', QQ)
cos, sin = rs_cos_sin(x, x, 9)
assert cos == rs_cos(x, x, 9)
assert sin == rs_sin(x, x, 9)
cos, sin = rs_cos_sin(x + x*y, x, 5)
assert cos == rs_cos(x + x*y, x, 5)
assert sin == rs_sin(x + x*y, x, 5)
def test_atanh():
R, x, y = ring('x, y', QQ)
assert rs_atanh(x, x, 9)/x**5 == Rational(1, 7)*x**2 + Rational(1, 5) + Rational(1, 3)*x**(-2) + x**(-4)
assert rs_atanh(x*y + x**2*y**3, x, 9) == 2*x**8*y**11 + x**8*y**9 + \
2*x**7*y**9 + x**7*y**7/7 + x**6*y**9/3 + x**6*y**7 + x**5*y**7 + \
x**5*y**5/5 + x**4*y**5 + x**3*y**3/3 + x**2*y**3 + x*y
# Constant term in series
a = symbols('a')
R, x, y = ring('x, y', EX)
assert rs_atanh(x + a, x, 5) == EX((a**3 + a)/(a**8 - 4*a**6 + 6*a**4 - \
4*a**2 + 1))*x**4 - EX((3*a**2 + 1)/(3*a**6 - 9*a**4 + \
9*a**2 - 3))*x**3 + EX(a/(a**4 - 2*a**2 + 1))*x**2 - EX(1/(a**2 - \
1))*x + EX(atanh(a))
assert rs_atanh(x + x**2*y + a, x, 4) == EX(2*a/(a**4 - 2*a**2 + \
1))*x**3*y - EX((3*a**2 + 1)/(3*a**6 - 9*a**4 + 9*a**2 - 3))*x**3 - \
EX(1/(a**2 - 1))*x**2*y + EX(a/(a**4 - 2*a**2 + 1))*x**2 - \
EX(1/(a**2 - 1))*x + EX(atanh(a))
p = x + x**2 + 5
assert rs_atanh(p, x, 10).compose(x, 10) == EX(Rational(-733442653682135, 5079158784) \
+ atanh(5))
def test_sinh():
R, x, y = ring('x, y', QQ)
assert rs_sinh(x, x, 9)/x**5 == Rational(1, 5040)*x**2 + Rational(1, 120) + Rational(1, 6)*x**(-2) + x**(-4)
assert rs_sinh(x*y + x**2*y**3, x, 9) == x**8*y**11/12 + \
x**8*y**9/720 + x**7*y**9/12 + x**7*y**7/5040 + x**6*y**9/6 + \
x**6*y**7/24 + x**5*y**7/2 + x**5*y**5/120 + x**4*y**5/2 + \
x**3*y**3/6 + x**2*y**3 + x*y
def test_cosh():
R, x, y = ring('x, y', QQ)
assert rs_cosh(x, x, 9)/x**5 == Rational(1, 40320)*x**3 + Rational(1, 720)*x + Rational(1, 24)*x**(-1) + \
S.Half*x**(-3) + x**(-5)
assert rs_cosh(x*y + x**2*y**3, x, 9) == x**8*y**12/24 + \
x**8*y**10/48 + x**8*y**8/40320 + x**7*y**10/6 + \
x**7*y**8/120 + x**6*y**8/4 + x**6*y**6/720 + x**5*y**6/6 + \
x**4*y**6/2 + x**4*y**4/24 + x**3*y**4 + x**2*y**2/2 + 1
def test_tanh():
R, x, y = ring('x, y', QQ)
assert rs_tanh(x, x, 9)/x**5 == Rational(-17, 315)*x**2 + Rational(2, 15) - Rational(1, 3)*x**(-2) + x**(-4)
assert rs_tanh(x*y + x**2*y**3, x, 9) == 4*x**8*y**11/3 - \
17*x**8*y**9/45 + 4*x**7*y**9/3 - 17*x**7*y**7/315 - x**6*y**9/3 + \
2*x**6*y**7/3 - x**5*y**7 + 2*x**5*y**5/15 - x**4*y**5 - \
x**3*y**3/3 + x**2*y**3 + x*y
# Constant term in series
a = symbols('a')
R, x, y = ring('x, y', EX)
assert rs_tanh(x + a, x, 5) == EX(tanh(a)**5 - 5*tanh(a)**3/3 +
2*tanh(a)/3)*x**4 + EX(-tanh(a)**4 + 4*tanh(a)**2/3 - QQ(1, 3))*x**3 + \
EX(tanh(a)**3 - tanh(a))*x**2 + EX(-tanh(a)**2 + 1)*x + EX(tanh(a))
p = rs_tanh(x + x**2*y + a, x, 4)
assert (p.compose(x, 10)).compose(y, 5) == EX(-1000*tanh(a)**4 + \
10100*tanh(a)**3 + 2470*tanh(a)**2/3 - 10099*tanh(a) + QQ(530, 3))
def test_RR():
rs_funcs = [rs_sin, rs_cos, rs_tan, rs_cot, rs_atan, rs_tanh]
sympy_funcs = [sin, cos, tan, cot, atan, tanh]
R, x, y = ring('x, y', RR)
a = symbols('a')
for rs_func, sympy_func in zip(rs_funcs, sympy_funcs):
p = rs_func(2 + x, x, 5).compose(x, 5)
q = sympy_func(2 + a).series(a, 0, 5).removeO()
is_close(p.as_expr(), q.subs(a, 5).n())
p = rs_nth_root(2 + x, 5, x, 5).compose(x, 5)
q = ((2 + a)**QQ(1, 5)).series(a, 0, 5).removeO()
is_close(p.as_expr(), q.subs(a, 5).n())
def test_is_regular():
R, x, y = ring('x, y', QQ)
p = 1 + 2*x + x**2 + 3*x**3
assert not rs_is_puiseux(p, x)
p = x + x**QQ(1,5)*y
assert rs_is_puiseux(p, x)
assert not rs_is_puiseux(p, y)
p = x + x**2*y**QQ(1,5)*y
assert not rs_is_puiseux(p, x)
def test_puiseux():
R, x, y = ring('x, y', QQ)
p = x**QQ(2,5) + x**QQ(2,3) + x
r = rs_series_inversion(p, x, 1)
r1 = -x**QQ(14,15) + x**QQ(4,5) - 3*x**QQ(11,15) + x**QQ(2,3) + \
2*x**QQ(7,15) - x**QQ(2,5) - x**QQ(1,5) + x**QQ(2,15) - x**QQ(-2,15) \
+ x**QQ(-2,5)
assert r == r1
r = rs_nth_root(1 + p, 3, x, 1)
assert r == -x**QQ(4,5)/9 + x**QQ(2,3)/3 + x**QQ(2,5)/3 + 1
r = rs_log(1 + p, x, 1)
assert r == -x**QQ(4,5)/2 + x**QQ(2,3) + x**QQ(2,5)
r = rs_LambertW(p, x, 1)
assert r == -x**QQ(4,5) + x**QQ(2,3) + x**QQ(2,5)
p1 = x + x**QQ(1,5)*y
r = rs_exp(p1, x, 1)
assert r == x**QQ(4,5)*y**4/24 + x**QQ(3,5)*y**3/6 + x**QQ(2,5)*y**2/2 + \
x**QQ(1,5)*y + 1
r = rs_atan(p, x, 2)
assert r == -x**QQ(9,5) - x**QQ(26,15) - x**QQ(22,15) - x**QQ(6,5)/3 + \
x + x**QQ(2,3) + x**QQ(2,5)
r = rs_atan(p1, x, 2)
assert r == x**QQ(9,5)*y**9/9 + x**QQ(9,5)*y**4 - x**QQ(7,5)*y**7/7 - \
x**QQ(7,5)*y**2 + x*y**5/5 + x - x**QQ(3,5)*y**3/3 + x**QQ(1,5)*y
r = rs_asin(p, x, 2)
assert r == x**QQ(9,5)/2 + x**QQ(26,15)/2 + x**QQ(22,15)/2 + \
x**QQ(6,5)/6 + x + x**QQ(2,3) + x**QQ(2,5)
r = rs_cot(p, x, 1)
assert r == -x**QQ(14,15) + x**QQ(4,5) - 3*x**QQ(11,15) + \
2*x**QQ(2,3)/3 + 2*x**QQ(7,15) - 4*x**QQ(2,5)/3 - x**QQ(1,5) + \
x**QQ(2,15) - x**QQ(-2,15) + x**QQ(-2,5)
r = rs_cos_sin(p, x, 2)
assert r[0] == x**QQ(28,15)/6 - x**QQ(5,3) + x**QQ(8,5)/24 - x**QQ(7,5) - \
x**QQ(4,3)/2 - x**QQ(16,15) - x**QQ(4,5)/2 + 1
assert r[1] == -x**QQ(9,5)/2 - x**QQ(26,15)/2 - x**QQ(22,15)/2 - \
x**QQ(6,5)/6 + x + x**QQ(2,3) + x**QQ(2,5)
r = rs_atanh(p, x, 2)
assert r == x**QQ(9,5) + x**QQ(26,15) + x**QQ(22,15) + x**QQ(6,5)/3 + x + \
x**QQ(2,3) + x**QQ(2,5)
r = rs_sinh(p, x, 2)
assert r == x**QQ(9,5)/2 + x**QQ(26,15)/2 + x**QQ(22,15)/2 + \
x**QQ(6,5)/6 + x + x**QQ(2,3) + x**QQ(2,5)
r = rs_cosh(p, x, 2)
assert r == x**QQ(28,15)/6 + x**QQ(5,3) + x**QQ(8,5)/24 + x**QQ(7,5) + \
x**QQ(4,3)/2 + x**QQ(16,15) + x**QQ(4,5)/2 + 1
r = rs_tanh(p, x, 2)
assert r == -x**QQ(9,5) - x**QQ(26,15) - x**QQ(22,15) - x**QQ(6,5)/3 + \
x + x**QQ(2,3) + x**QQ(2,5)
def test_puiseux_algebraic(): # https://github.com/sympy/sympy/issues/24395
K = QQ.algebraic_field(sqrt(2))
sqrt2 = K.from_sympy(sqrt(2))
x, y = symbols('x, y')
R, xr, yr = ring([x, y], K)
p = (1+sqrt2)*xr**QQ(1,2) + (1-sqrt2)*yr**QQ(2,3)
assert dict(p) == {(QQ(1,2),QQ(0)):1+sqrt2, (QQ(0),QQ(2,3)):1-sqrt2}
assert p.as_expr() == (1 + sqrt(2))*x**(S(1)/2) + (1 - sqrt(2))*y**(S(2)/3)
def test1():
R, x = ring('x', QQ)
r = rs_sin(x, x, 15)*x**(-5)
assert r == x**8/6227020800 - x**6/39916800 + x**4/362880 - x**2/5040 + \
QQ(1,120) - x**-2/6 + x**-4
p = rs_sin(x, x, 10)
r = rs_nth_root(p, 2, x, 10)
assert r == -67*x**QQ(17,2)/29030400 - x**QQ(13,2)/24192 + \
x**QQ(9,2)/1440 - x**QQ(5,2)/12 + x**QQ(1,2)
p = rs_sin(x, x, 10)
r = rs_nth_root(p, 7, x, 10)
r = rs_pow(r, 5, x, 10)
assert r == -97*x**QQ(61,7)/124467840 - x**QQ(47,7)/16464 + \
11*x**QQ(33,7)/3528 - 5*x**QQ(19,7)/42 + x**QQ(5,7)
r = rs_exp(x**QQ(1,2), x, 10)
assert r == x**QQ(19,2)/121645100408832000 + x**9/6402373705728000 + \
x**QQ(17,2)/355687428096000 + x**8/20922789888000 + \
x**QQ(15,2)/1307674368000 + x**7/87178291200 + \
x**QQ(13,2)/6227020800 + x**6/479001600 + x**QQ(11,2)/39916800 + \
x**5/3628800 + x**QQ(9,2)/362880 + x**4/40320 + x**QQ(7,2)/5040 + \
x**3/720 + x**QQ(5,2)/120 + x**2/24 + x**QQ(3,2)/6 + x/2 + \
x**QQ(1,2) + 1
def test_puiseux2():
R, y = ring('y', QQ)
S, x = ring('x', R)
p = x + x**QQ(1,5)*y
r = rs_atan(p, x, 3)
assert r == (y**13/13 + y**8 + 2*y**3)*x**QQ(13,5) - (y**11/11 + y**6 +
y)*x**QQ(11,5) + (y**9/9 + y**4)*x**QQ(9,5) - (y**7/7 +
y**2)*x**QQ(7,5) + (y**5/5 + 1)*x - y**3*x**QQ(3,5)/3 + y*x**QQ(1,5)
@slow
def test_rs_series():
x, a, b, c = symbols('x, a, b, c')
assert rs_series(a, a, 5).as_expr() == a
assert rs_series(sin(a), a, 5).as_expr() == (sin(a).series(a, 0,
5)).removeO()
assert rs_series(sin(a) + cos(a), a, 5).as_expr() == ((sin(a) +
cos(a)).series(a, 0, 5)).removeO()
assert rs_series(sin(a)*cos(a), a, 5).as_expr() == ((sin(a)*
cos(a)).series(a, 0, 5)).removeO()
p = (sin(a) - a)*(cos(a**2) + a**4/2)
assert expand(rs_series(p, a, 10).as_expr()) == expand(p.series(a, 0,
10).removeO())
p = sin(a**2/2 + a/3) + cos(a/5)*sin(a/2)**3
assert expand(rs_series(p, a, 5).as_expr()) == expand(p.series(a, 0,
5).removeO())
p = sin(x**2 + a)*(cos(x**3 - 1) - a - a**2)
assert expand(rs_series(p, a, 5).as_expr()) == expand(p.series(a, 0,
5).removeO())
p = sin(a**2 - a/3 + 2)**5*exp(a**3 - a/2)
assert expand(rs_series(p, a, 10).as_expr()) == expand(p.series(a, 0,
10).removeO())
p = sin(a + b + c)
assert expand(rs_series(p, a, 5).as_expr()) == expand(p.series(a, 0,
5).removeO())
p = tan(sin(a**2 + 4) + b + c)
assert expand(rs_series(p, a, 6).as_expr()) == expand(p.series(a, 0,
6).removeO())
p = a**QQ(2,5) + a**QQ(2,3) + a
r = rs_series(tan(p), a, 2)
assert r.as_expr() == a**QQ(9,5) + a**QQ(26,15) + a**QQ(22,15) + a**QQ(6,5)/3 + \
a + a**QQ(2,3) + a**QQ(2,5)
r = rs_series(exp(p), a, 1)
assert r.as_expr() == a**QQ(4,5)/2 + a**QQ(2,3) + a**QQ(2,5) + 1
r = rs_series(sin(p), a, 2)
assert r.as_expr() == -a**QQ(9,5)/2 - a**QQ(26,15)/2 - a**QQ(22,15)/2 - \
a**QQ(6,5)/6 + a + a**QQ(2,3) + a**QQ(2,5)
r = rs_series(cos(p), a, 2)
assert r.as_expr() == a**QQ(28,15)/6 - a**QQ(5,3) + a**QQ(8,5)/24 - a**QQ(7,5) - \
a**QQ(4,3)/2 - a**QQ(16,15) - a**QQ(4,5)/2 + 1
assert rs_series(sin(a)/7, a, 5).as_expr() == (sin(a)/7).series(a, 0,
5).removeO()
assert rs_series(log(1 + x), x, 5).as_expr() == -x**4/4 + x**3/3 - \
x**2/2 + x
assert rs_series(log(1 + 4*x), x, 5).as_expr() == -64*x**4 + 64*x**3/3 - \
8*x**2 + 4*x
assert rs_series(log(1 + x + x**2), x, 10).as_expr() == -2*x**9/9 + \
x**8/8 + x**7/7 - x**6/3 + x**5/5 + x**4/4 - 2*x**3/3 + \
x**2/2 + x
assert rs_series(log(1 + x*a**2), x, 7).as_expr() == -x**6*a**12/6 + \
x**5*a**10/5 - x**4*a**8/4 + x**3*a**6/3 - \
x**2*a**4/2 + x*a**2

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,823 @@
"""Tests for real and complex root isolation and refinement algorithms. """
from sympy.polys.rings import ring
from sympy.polys.domains import ZZ, QQ, ZZ_I, EX
from sympy.polys.polyerrors import DomainError, RefinementFailed, PolynomialError
from sympy.polys.rootisolation import (
dup_cauchy_upper_bound, dup_cauchy_lower_bound,
dup_mignotte_sep_bound_squared,
)
from sympy.testing.pytest import raises
def test_dup_sturm():
R, x = ring("x", QQ)
assert R.dup_sturm(5) == [1]
assert R.dup_sturm(x) == [x, 1]
f = x**3 - 2*x**2 + 3*x - 5
assert R.dup_sturm(f) == [f, 3*x**2 - 4*x + 3, -QQ(10,9)*x + QQ(13,3), -QQ(3303,100)]
def test_dup_cauchy_upper_bound():
raises(PolynomialError, lambda: dup_cauchy_upper_bound([], QQ))
raises(PolynomialError, lambda: dup_cauchy_upper_bound([QQ(1)], QQ))
raises(DomainError, lambda: dup_cauchy_upper_bound([ZZ_I(1), ZZ_I(1)], ZZ_I))
assert dup_cauchy_upper_bound([QQ(1), QQ(0), QQ(0)], QQ) == QQ.zero
assert dup_cauchy_upper_bound([QQ(1), QQ(0), QQ(-2)], QQ) == QQ(3)
def test_dup_cauchy_lower_bound():
raises(PolynomialError, lambda: dup_cauchy_lower_bound([], QQ))
raises(PolynomialError, lambda: dup_cauchy_lower_bound([QQ(1)], QQ))
raises(PolynomialError, lambda: dup_cauchy_lower_bound([QQ(1), QQ(0), QQ(0)], QQ))
raises(DomainError, lambda: dup_cauchy_lower_bound([ZZ_I(1), ZZ_I(1)], ZZ_I))
assert dup_cauchy_lower_bound([QQ(1), QQ(0), QQ(-2)], QQ) == QQ(2, 3)
def test_dup_mignotte_sep_bound_squared():
raises(PolynomialError, lambda: dup_mignotte_sep_bound_squared([], QQ))
raises(PolynomialError, lambda: dup_mignotte_sep_bound_squared([QQ(1)], QQ))
assert dup_mignotte_sep_bound_squared([QQ(1), QQ(0), QQ(-2)], QQ) == QQ(3, 5)
def test_dup_refine_real_root():
R, x = ring("x", ZZ)
f = x**2 - 2
assert R.dup_refine_real_root(f, QQ(1), QQ(1), steps=1) == (QQ(1), QQ(1))
assert R.dup_refine_real_root(f, QQ(1), QQ(1), steps=9) == (QQ(1), QQ(1))
raises(ValueError, lambda: R.dup_refine_real_root(f, QQ(-2), QQ(2)))
s, t = QQ(1, 1), QQ(2, 1)
assert R.dup_refine_real_root(f, s, t, steps=0) == (QQ(1, 1), QQ(2, 1))
assert R.dup_refine_real_root(f, s, t, steps=1) == (QQ(1, 1), QQ(3, 2))
assert R.dup_refine_real_root(f, s, t, steps=2) == (QQ(4, 3), QQ(3, 2))
assert R.dup_refine_real_root(f, s, t, steps=3) == (QQ(7, 5), QQ(3, 2))
assert R.dup_refine_real_root(f, s, t, steps=4) == (QQ(7, 5), QQ(10, 7))
s, t = QQ(1, 1), QQ(3, 2)
assert R.dup_refine_real_root(f, s, t, steps=0) == (QQ(1, 1), QQ(3, 2))
assert R.dup_refine_real_root(f, s, t, steps=1) == (QQ(4, 3), QQ(3, 2))
assert R.dup_refine_real_root(f, s, t, steps=2) == (QQ(7, 5), QQ(3, 2))
assert R.dup_refine_real_root(f, s, t, steps=3) == (QQ(7, 5), QQ(10, 7))
assert R.dup_refine_real_root(f, s, t, steps=4) == (QQ(7, 5), QQ(17, 12))
s, t = QQ(1, 1), QQ(5, 3)
assert R.dup_refine_real_root(f, s, t, steps=0) == (QQ(1, 1), QQ(5, 3))
assert R.dup_refine_real_root(f, s, t, steps=1) == (QQ(1, 1), QQ(3, 2))
assert R.dup_refine_real_root(f, s, t, steps=2) == (QQ(7, 5), QQ(3, 2))
assert R.dup_refine_real_root(f, s, t, steps=3) == (QQ(7, 5), QQ(13, 9))
assert R.dup_refine_real_root(f, s, t, steps=4) == (QQ(7, 5), QQ(27, 19))
s, t = QQ(-1, 1), QQ(-2, 1)
assert R.dup_refine_real_root(f, s, t, steps=0) == (-QQ(2, 1), -QQ(1, 1))
assert R.dup_refine_real_root(f, s, t, steps=1) == (-QQ(3, 2), -QQ(1, 1))
assert R.dup_refine_real_root(f, s, t, steps=2) == (-QQ(3, 2), -QQ(4, 3))
assert R.dup_refine_real_root(f, s, t, steps=3) == (-QQ(3, 2), -QQ(7, 5))
assert R.dup_refine_real_root(f, s, t, steps=4) == (-QQ(10, 7), -QQ(7, 5))
raises(RefinementFailed, lambda: R.dup_refine_real_root(f, QQ(0), QQ(1)))
s, t, u, v, w = QQ(1), QQ(2), QQ(24, 17), QQ(17, 12), QQ(7, 5)
assert R.dup_refine_real_root(f, s, t, eps=QQ(1, 100)) == (u, v)
assert R.dup_refine_real_root(f, s, t, steps=6) == (u, v)
assert R.dup_refine_real_root(f, s, t, eps=QQ(1, 100), steps=5) == (w, v)
assert R.dup_refine_real_root(f, s, t, eps=QQ(1, 100), steps=6) == (u, v)
assert R.dup_refine_real_root(f, s, t, eps=QQ(1, 100), steps=7) == (u, v)
s, t, u, v = QQ(-2), QQ(-1), QQ(-3, 2), QQ(-4, 3)
assert R.dup_refine_real_root(f, s, t, disjoint=QQ(-5)) == (s, t)
assert R.dup_refine_real_root(f, s, t, disjoint=-v) == (s, t)
assert R.dup_refine_real_root(f, s, t, disjoint=v) == (u, v)
s, t, u, v = QQ(1), QQ(2), QQ(4, 3), QQ(3, 2)
assert R.dup_refine_real_root(f, s, t, disjoint=QQ(5)) == (s, t)
assert R.dup_refine_real_root(f, s, t, disjoint=-u) == (s, t)
assert R.dup_refine_real_root(f, s, t, disjoint=u) == (u, v)
def test_dup_isolate_real_roots_sqf():
R, x = ring("x", ZZ)
assert R.dup_isolate_real_roots_sqf(0) == []
assert R.dup_isolate_real_roots_sqf(5) == []
assert R.dup_isolate_real_roots_sqf(x**2 + x) == [(-1, -1), (0, 0)]
assert R.dup_isolate_real_roots_sqf(x**2 - x) == [( 0, 0), (1, 1)]
assert R.dup_isolate_real_roots_sqf(x**4 + x + 1) == []
I = [(-2, -1), (1, 2)]
assert R.dup_isolate_real_roots_sqf(x**2 - 2) == I
assert R.dup_isolate_real_roots_sqf(-x**2 + 2) == I
assert R.dup_isolate_real_roots_sqf(x - 1) == \
[(1, 1)]
assert R.dup_isolate_real_roots_sqf(x**2 - 3*x + 2) == \
[(1, 1), (2, 2)]
assert R.dup_isolate_real_roots_sqf(x**3 - 6*x**2 + 11*x - 6) == \
[(1, 1), (2, 2), (3, 3)]
assert R.dup_isolate_real_roots_sqf(x**4 - 10*x**3 + 35*x**2 - 50*x + 24) == \
[(1, 1), (2, 2), (3, 3), (4, 4)]
assert R.dup_isolate_real_roots_sqf(x**5 - 15*x**4 + 85*x**3 - 225*x**2 + 274*x - 120) == \
[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]
assert R.dup_isolate_real_roots_sqf(x - 10) == \
[(10, 10)]
assert R.dup_isolate_real_roots_sqf(x**2 - 30*x + 200) == \
[(10, 10), (20, 20)]
assert R.dup_isolate_real_roots_sqf(x**3 - 60*x**2 + 1100*x - 6000) == \
[(10, 10), (20, 20), (30, 30)]
assert R.dup_isolate_real_roots_sqf(x**4 - 100*x**3 + 3500*x**2 - 50000*x + 240000) == \
[(10, 10), (20, 20), (30, 30), (40, 40)]
assert R.dup_isolate_real_roots_sqf(x**5 - 150*x**4 + 8500*x**3 - 225000*x**2 + 2740000*x - 12000000) == \
[(10, 10), (20, 20), (30, 30), (40, 40), (50, 50)]
assert R.dup_isolate_real_roots_sqf(x + 1) == \
[(-1, -1)]
assert R.dup_isolate_real_roots_sqf(x**2 + 3*x + 2) == \
[(-2, -2), (-1, -1)]
assert R.dup_isolate_real_roots_sqf(x**3 + 6*x**2 + 11*x + 6) == \
[(-3, -3), (-2, -2), (-1, -1)]
assert R.dup_isolate_real_roots_sqf(x**4 + 10*x**3 + 35*x**2 + 50*x + 24) == \
[(-4, -4), (-3, -3), (-2, -2), (-1, -1)]
assert R.dup_isolate_real_roots_sqf(x**5 + 15*x**4 + 85*x**3 + 225*x**2 + 274*x + 120) == \
[(-5, -5), (-4, -4), (-3, -3), (-2, -2), (-1, -1)]
assert R.dup_isolate_real_roots_sqf(x + 10) == \
[(-10, -10)]
assert R.dup_isolate_real_roots_sqf(x**2 + 30*x + 200) == \
[(-20, -20), (-10, -10)]
assert R.dup_isolate_real_roots_sqf(x**3 + 60*x**2 + 1100*x + 6000) == \
[(-30, -30), (-20, -20), (-10, -10)]
assert R.dup_isolate_real_roots_sqf(x**4 + 100*x**3 + 3500*x**2 + 50000*x + 240000) == \
[(-40, -40), (-30, -30), (-20, -20), (-10, -10)]
assert R.dup_isolate_real_roots_sqf(x**5 + 150*x**4 + 8500*x**3 + 225000*x**2 + 2740000*x + 12000000) == \
[(-50, -50), (-40, -40), (-30, -30), (-20, -20), (-10, -10)]
assert R.dup_isolate_real_roots_sqf(x**2 - 5) == [(-3, -2), (2, 3)]
assert R.dup_isolate_real_roots_sqf(x**3 - 5) == [(1, 2)]
assert R.dup_isolate_real_roots_sqf(x**4 - 5) == [(-2, -1), (1, 2)]
assert R.dup_isolate_real_roots_sqf(x**5 - 5) == [(1, 2)]
assert R.dup_isolate_real_roots_sqf(x**6 - 5) == [(-2, -1), (1, 2)]
assert R.dup_isolate_real_roots_sqf(x**7 - 5) == [(1, 2)]
assert R.dup_isolate_real_roots_sqf(x**8 - 5) == [(-2, -1), (1, 2)]
assert R.dup_isolate_real_roots_sqf(x**9 - 5) == [(1, 2)]
assert R.dup_isolate_real_roots_sqf(x**2 - 1) == \
[(-1, -1), (1, 1)]
assert R.dup_isolate_real_roots_sqf(x**3 + 2*x**2 - x - 2) == \
[(-2, -2), (-1, -1), (1, 1)]
assert R.dup_isolate_real_roots_sqf(x**4 - 5*x**2 + 4) == \
[(-2, -2), (-1, -1), (1, 1), (2, 2)]
assert R.dup_isolate_real_roots_sqf(x**5 + 3*x**4 - 5*x**3 - 15*x**2 + 4*x + 12) == \
[(-3, -3), (-2, -2), (-1, -1), (1, 1), (2, 2)]
assert R.dup_isolate_real_roots_sqf(x**6 - 14*x**4 + 49*x**2 - 36) == \
[(-3, -3), (-2, -2), (-1, -1), (1, 1), (2, 2), (3, 3)]
assert R.dup_isolate_real_roots_sqf(2*x**7 + x**6 - 28*x**5 - 14*x**4 + 98*x**3 + 49*x**2 - 72*x - 36) == \
[(-3, -3), (-2, -2), (-1, -1), (-1, 0), (1, 1), (2, 2), (3, 3)]
assert R.dup_isolate_real_roots_sqf(4*x**8 - 57*x**6 + 210*x**4 - 193*x**2 + 36) == \
[(-3, -3), (-2, -2), (-1, -1), (-1, 0), (0, 1), (1, 1), (2, 2), (3, 3)]
f = 9*x**2 - 2
assert R.dup_isolate_real_roots_sqf(f) == \
[(-1, 0), (0, 1)]
assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 10)) == \
[(QQ(-1, 2), QQ(-3, 7)), (QQ(3, 7), QQ(1, 2))]
assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 100)) == \
[(QQ(-9, 19), QQ(-8, 17)), (QQ(8, 17), QQ(9, 19))]
assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 1000)) == \
[(QQ(-33, 70), QQ(-8, 17)), (QQ(8, 17), QQ(33, 70))]
assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 10000)) == \
[(QQ(-33, 70), QQ(-107, 227)), (QQ(107, 227), QQ(33, 70))]
assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 100000)) == \
[(QQ(-305, 647), QQ(-272, 577)), (QQ(272, 577), QQ(305, 647))]
assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 1000000)) == \
[(QQ(-1121, 2378), QQ(-272, 577)), (QQ(272, 577), QQ(1121, 2378))]
f = 200100012*x**5 - 700390052*x**4 + 700490079*x**3 - 200240054*x**2 + 40017*x - 2
assert R.dup_isolate_real_roots_sqf(f) == \
[(QQ(0), QQ(1, 10002)), (QQ(1, 10002), QQ(1, 10002)),
(QQ(1, 2), QQ(1, 2)), (QQ(1), QQ(1)), (QQ(2), QQ(2))]
assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 100000)) == \
[(QQ(1, 10003), QQ(1, 10003)), (QQ(1, 10002), QQ(1, 10002)),
(QQ(1, 2), QQ(1, 2)), (QQ(1), QQ(1)), (QQ(2), QQ(2))]
a, b, c, d = 10000090000001, 2000100003, 10000300007, 10000005000008
f = 20001600074001600021*x**4 \
+ 1700135866278935491773999857*x**3 \
- 2000179008931031182161141026995283662899200197*x**2 \
- 800027600594323913802305066986600025*x \
+ 100000950000540000725000008
assert R.dup_isolate_real_roots_sqf(f) == \
[(-a, -a), (-1, 0), (0, 1), (d, d)]
assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 100000000000)) == \
[(-QQ(a), -QQ(a)), (-QQ(1, b), -QQ(1, b)), (QQ(1, c), QQ(1, c)), (QQ(d), QQ(d))]
(u, v), B, C, (s, t) = R.dup_isolate_real_roots_sqf(f, fast=True)
assert u < -a < v and B == (-QQ(1), QQ(0)) and C == (QQ(0), QQ(1)) and s < d < t
assert R.dup_isolate_real_roots_sqf(f, fast=True, eps=QQ(1, 100000000000000000000000000000)) == \
[(-QQ(a), -QQ(a)), (-QQ(1, b), -QQ(1, b)), (QQ(1, c), QQ(1, c)), (QQ(d), QQ(d))]
f = -10*x**4 + 8*x**3 + 80*x**2 - 32*x - 160
assert R.dup_isolate_real_roots_sqf(f) == \
[(-2, -2), (-2, -1), (2, 2), (2, 3)]
assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 100)) == \
[(-QQ(2), -QQ(2)), (-QQ(23, 14), -QQ(18, 11)), (QQ(2), QQ(2)), (QQ(39, 16), QQ(22, 9))]
f = x - 1
assert R.dup_isolate_real_roots_sqf(f, inf=2) == []
assert R.dup_isolate_real_roots_sqf(f, sup=0) == []
assert R.dup_isolate_real_roots_sqf(f) == [(1, 1)]
assert R.dup_isolate_real_roots_sqf(f, inf=1) == [(1, 1)]
assert R.dup_isolate_real_roots_sqf(f, sup=1) == [(1, 1)]
assert R.dup_isolate_real_roots_sqf(f, inf=1, sup=1) == [(1, 1)]
f = x**2 - 2
assert R.dup_isolate_real_roots_sqf(f, inf=QQ(7, 4)) == []
assert R.dup_isolate_real_roots_sqf(f, inf=QQ(7, 5)) == [(QQ(7, 5), QQ(3, 2))]
assert R.dup_isolate_real_roots_sqf(f, sup=QQ(7, 5)) == [(-2, -1)]
assert R.dup_isolate_real_roots_sqf(f, sup=QQ(7, 4)) == [(-2, -1), (1, QQ(3, 2))]
assert R.dup_isolate_real_roots_sqf(f, sup=-QQ(7, 4)) == []
assert R.dup_isolate_real_roots_sqf(f, sup=-QQ(7, 5)) == [(-QQ(3, 2), -QQ(7, 5))]
assert R.dup_isolate_real_roots_sqf(f, inf=-QQ(7, 5)) == [(1, 2)]
assert R.dup_isolate_real_roots_sqf(f, inf=-QQ(7, 4)) == [(-QQ(3, 2), -1), (1, 2)]
I = [(-2, -1), (1, 2)]
assert R.dup_isolate_real_roots_sqf(f, inf=-2) == I
assert R.dup_isolate_real_roots_sqf(f, sup=+2) == I
assert R.dup_isolate_real_roots_sqf(f, inf=-2, sup=2) == I
R, x = ring("x", QQ)
f = QQ(8, 5)*x**2 - QQ(87374, 3855)*x - QQ(17, 771)
assert R.dup_isolate_real_roots_sqf(f) == [(-1, 0), (14, 15)]
R, x = ring("x", EX)
raises(DomainError, lambda: R.dup_isolate_real_roots_sqf(x + 3))
def test_dup_isolate_real_roots():
R, x = ring("x", ZZ)
assert R.dup_isolate_real_roots(0) == []
assert R.dup_isolate_real_roots(3) == []
assert R.dup_isolate_real_roots(5*x) == [((0, 0), 1)]
assert R.dup_isolate_real_roots(7*x**4) == [((0, 0), 4)]
assert R.dup_isolate_real_roots(x**2 + x) == [((-1, -1), 1), ((0, 0), 1)]
assert R.dup_isolate_real_roots(x**2 - x) == [((0, 0), 1), ((1, 1), 1)]
assert R.dup_isolate_real_roots(x**4 + x + 1) == []
I = [((-2, -1), 1), ((1, 2), 1)]
assert R.dup_isolate_real_roots(x**2 - 2) == I
assert R.dup_isolate_real_roots(-x**2 + 2) == I
f = 16*x**14 - 96*x**13 + 24*x**12 + 936*x**11 - 1599*x**10 - 2880*x**9 + 9196*x**8 \
+ 552*x**7 - 21831*x**6 + 13968*x**5 + 21690*x**4 - 26784*x**3 - 2916*x**2 + 15552*x - 5832
g = R.dup_sqf_part(f)
assert R.dup_isolate_real_roots(f) == \
[((-QQ(2), -QQ(3, 2)), 2), ((-QQ(3, 2), -QQ(1, 1)), 3), ((QQ(1), QQ(3, 2)), 3),
((QQ(3, 2), QQ(3, 2)), 4), ((QQ(5, 3), QQ(2)), 2)]
assert R.dup_isolate_real_roots_sqf(g) == \
[(-QQ(2), -QQ(3, 2)), (-QQ(3, 2), -QQ(1, 1)), (QQ(1), QQ(3, 2)),
(QQ(3, 2), QQ(3, 2)), (QQ(3, 2), QQ(2))]
assert R.dup_isolate_real_roots(g) == \
[((-QQ(2), -QQ(3, 2)), 1), ((-QQ(3, 2), -QQ(1, 1)), 1), ((QQ(1), QQ(3, 2)), 1),
((QQ(3, 2), QQ(3, 2)), 1), ((QQ(3, 2), QQ(2)), 1)]
f = x - 1
assert R.dup_isolate_real_roots(f, inf=2) == []
assert R.dup_isolate_real_roots(f, sup=0) == []
assert R.dup_isolate_real_roots(f) == [((1, 1), 1)]
assert R.dup_isolate_real_roots(f, inf=1) == [((1, 1), 1)]
assert R.dup_isolate_real_roots(f, sup=1) == [((1, 1), 1)]
assert R.dup_isolate_real_roots(f, inf=1, sup=1) == [((1, 1), 1)]
f = x**4 - 4*x**2 + 4
assert R.dup_isolate_real_roots(f, inf=QQ(7, 4)) == []
assert R.dup_isolate_real_roots(f, inf=QQ(7, 5)) == [((QQ(7, 5), QQ(3, 2)), 2)]
assert R.dup_isolate_real_roots(f, sup=QQ(7, 5)) == [((-2, -1), 2)]
assert R.dup_isolate_real_roots(f, sup=QQ(7, 4)) == [((-2, -1), 2), ((1, QQ(3, 2)), 2)]
assert R.dup_isolate_real_roots(f, sup=-QQ(7, 4)) == []
assert R.dup_isolate_real_roots(f, sup=-QQ(7, 5)) == [((-QQ(3, 2), -QQ(7, 5)), 2)]
assert R.dup_isolate_real_roots(f, inf=-QQ(7, 5)) == [((1, 2), 2)]
assert R.dup_isolate_real_roots(f, inf=-QQ(7, 4)) == [((-QQ(3, 2), -1), 2), ((1, 2), 2)]
I = [((-2, -1), 2), ((1, 2), 2)]
assert R.dup_isolate_real_roots(f, inf=-2) == I
assert R.dup_isolate_real_roots(f, sup=+2) == I
assert R.dup_isolate_real_roots(f, inf=-2, sup=2) == I
f = x**11 - 3*x**10 - x**9 + 11*x**8 - 8*x**7 - 8*x**6 + 12*x**5 - 4*x**4
assert R.dup_isolate_real_roots(f, basis=False) == \
[((-2, -1), 2), ((0, 0), 4), ((1, 1), 3), ((1, 2), 2)]
assert R.dup_isolate_real_roots(f, basis=True) == \
[((-2, -1), 2, [1, 0, -2]), ((0, 0), 4, [1, 0]), ((1, 1), 3, [1, -1]), ((1, 2), 2, [1, 0, -2])]
f = (x**45 - 45*x**44 + 990*x**43 - 1)
g = (x**46 - 15180*x**43 + 9366819*x**40 - 53524680*x**39 + 260932815*x**38 - 1101716330*x**37 + 4076350421*x**36 - 13340783196*x**35 + 38910617655*x**34 - 101766230790*x**33 + 239877544005*x**32 - 511738760544*x**31 + 991493848554*x**30 - 1749695026860*x**29 + 2818953098830*x**28 - 4154246671960*x**27 + 5608233007146*x**26 - 6943526580276*x**25 + 7890371113950*x**24 - 8233430727600*x**23 + 7890371113950*x**22 - 6943526580276*x**21 + 5608233007146*x**20 - 4154246671960*x**19 + 2818953098830*x**18 - 1749695026860*x**17 + 991493848554*x**16 - 511738760544*x**15 + 239877544005*x**14 - 101766230790*x**13 + 38910617655*x**12 - 13340783196*x**11 + 4076350421*x**10 - 1101716330*x**9 + 260932815*x**8 - 53524680*x**7 + 9366819*x**6 - 1370754*x**5 + 163185*x**4 - 15180*x**3 + 1035*x**2 - 47*x + 1)
assert R.dup_isolate_real_roots(f*g) == \
[((0, QQ(1, 2)), 1), ((QQ(2, 3), QQ(3, 4)), 1), ((QQ(3, 4), 1), 1), ((6, 7), 1), ((24, 25), 1)]
R, x = ring("x", EX)
raises(DomainError, lambda: R.dup_isolate_real_roots(x + 3))
def test_dup_isolate_real_roots_list():
R, x = ring("x", ZZ)
assert R.dup_isolate_real_roots_list([x**2 + x, x]) == \
[((-1, -1), {0: 1}), ((0, 0), {0: 1, 1: 1})]
assert R.dup_isolate_real_roots_list([x**2 - x, x]) == \
[((0, 0), {0: 1, 1: 1}), ((1, 1), {0: 1})]
assert R.dup_isolate_real_roots_list([x + 1, x + 2, x - 1, x + 1, x - 1, x - 1]) == \
[((-QQ(2), -QQ(2)), {1: 1}), ((-QQ(1), -QQ(1)), {0: 1, 3: 1}), ((QQ(1), QQ(1)), {2: 1, 4: 1, 5: 1})]
assert R.dup_isolate_real_roots_list([x + 1, x + 2, x - 1, x + 1, x - 1, x + 2]) == \
[((-QQ(2), -QQ(2)), {1: 1, 5: 1}), ((-QQ(1), -QQ(1)), {0: 1, 3: 1}), ((QQ(1), QQ(1)), {2: 1, 4: 1})]
f, g = x**4 - 4*x**2 + 4, x - 1
assert R.dup_isolate_real_roots_list([f, g], inf=QQ(7, 4)) == []
assert R.dup_isolate_real_roots_list([f, g], inf=QQ(7, 5)) == \
[((QQ(7, 5), QQ(3, 2)), {0: 2})]
assert R.dup_isolate_real_roots_list([f, g], sup=QQ(7, 5)) == \
[((-2, -1), {0: 2}), ((1, 1), {1: 1})]
assert R.dup_isolate_real_roots_list([f, g], sup=QQ(7, 4)) == \
[((-2, -1), {0: 2}), ((1, 1), {1: 1}), ((1, QQ(3, 2)), {0: 2})]
assert R.dup_isolate_real_roots_list([f, g], sup=-QQ(7, 4)) == []
assert R.dup_isolate_real_roots_list([f, g], sup=-QQ(7, 5)) == \
[((-QQ(3, 2), -QQ(7, 5)), {0: 2})]
assert R.dup_isolate_real_roots_list([f, g], inf=-QQ(7, 5)) == \
[((1, 1), {1: 1}), ((1, 2), {0: 2})]
assert R.dup_isolate_real_roots_list([f, g], inf=-QQ(7, 4)) == \
[((-QQ(3, 2), -1), {0: 2}), ((1, 1), {1: 1}), ((1, 2), {0: 2})]
f, g = 2*x**2 - 1, x**2 - 2
assert R.dup_isolate_real_roots_list([f, g]) == \
[((-QQ(2), -QQ(1)), {1: 1}), ((-QQ(1), QQ(0)), {0: 1}),
((QQ(0), QQ(1)), {0: 1}), ((QQ(1), QQ(2)), {1: 1})]
assert R.dup_isolate_real_roots_list([f, g], strict=True) == \
[((-QQ(3, 2), -QQ(4, 3)), {1: 1}), ((-QQ(1), -QQ(2, 3)), {0: 1}),
((QQ(2, 3), QQ(1)), {0: 1}), ((QQ(4, 3), QQ(3, 2)), {1: 1})]
f, g = x**2 - 2, x**3 - x**2 - 2*x + 2
assert R.dup_isolate_real_roots_list([f, g]) == \
[((-QQ(2), -QQ(1)), {1: 1, 0: 1}), ((QQ(1), QQ(1)), {1: 1}), ((QQ(1), QQ(2)), {1: 1, 0: 1})]
f, g = x**3 - 2*x, x**5 - x**4 - 2*x**3 + 2*x**2
assert R.dup_isolate_real_roots_list([f, g]) == \
[((-QQ(2), -QQ(1)), {1: 1, 0: 1}), ((QQ(0), QQ(0)), {0: 1, 1: 2}),
((QQ(1), QQ(1)), {1: 1}), ((QQ(1), QQ(2)), {1: 1, 0: 1})]
f, g = x**9 - 3*x**8 - x**7 + 11*x**6 - 8*x**5 - 8*x**4 + 12*x**3 - 4*x**2, x**5 - 2*x**4 + 3*x**3 - 4*x**2 + 2*x
assert R.dup_isolate_real_roots_list([f, g], basis=False) == \
[((-2, -1), {0: 2}), ((0, 0), {0: 2, 1: 1}), ((1, 1), {0: 3, 1: 2}), ((1, 2), {0: 2})]
assert R.dup_isolate_real_roots_list([f, g], basis=True) == \
[((-2, -1), {0: 2}, [1, 0, -2]), ((0, 0), {0: 2, 1: 1}, [1, 0]),
((1, 1), {0: 3, 1: 2}, [1, -1]), ((1, 2), {0: 2}, [1, 0, -2])]
R, x = ring("x", EX)
raises(DomainError, lambda: R.dup_isolate_real_roots_list([x + 3]))
def test_dup_isolate_real_roots_list_QQ():
R, x = ring("x", ZZ)
f = x**5 - 200
g = x**5 - 201
assert R.dup_isolate_real_roots_list([f, g]) == \
[((QQ(75, 26), QQ(101, 35)), {0: 1}), ((QQ(309, 107), QQ(26, 9)), {1: 1})]
R, x = ring("x", QQ)
f = -QQ(1, 200)*x**5 + 1
g = -QQ(1, 201)*x**5 + 1
assert R.dup_isolate_real_roots_list([f, g]) == \
[((QQ(75, 26), QQ(101, 35)), {0: 1}), ((QQ(309, 107), QQ(26, 9)), {1: 1})]
def test_dup_count_real_roots():
R, x = ring("x", ZZ)
assert R.dup_count_real_roots(0) == 0
assert R.dup_count_real_roots(7) == 0
f = x - 1
assert R.dup_count_real_roots(f) == 1
assert R.dup_count_real_roots(f, inf=1) == 1
assert R.dup_count_real_roots(f, sup=0) == 0
assert R.dup_count_real_roots(f, sup=1) == 1
assert R.dup_count_real_roots(f, inf=0, sup=1) == 1
assert R.dup_count_real_roots(f, inf=0, sup=2) == 1
assert R.dup_count_real_roots(f, inf=1, sup=2) == 1
f = x**2 - 2
assert R.dup_count_real_roots(f) == 2
assert R.dup_count_real_roots(f, sup=0) == 1
assert R.dup_count_real_roots(f, inf=-1, sup=1) == 0
# parameters for test_dup_count_complex_roots_n(): n = 1..8
a, b = (-QQ(1), -QQ(1)), (QQ(1), QQ(1))
c, d = ( QQ(0), QQ(0)), (QQ(1), QQ(1))
def test_dup_count_complex_roots_1():
R, x = ring("x", ZZ)
# z-1
f = x - 1
assert R.dup_count_complex_roots(f, a, b) == 1
assert R.dup_count_complex_roots(f, c, d) == 1
# z+1
f = x + 1
assert R.dup_count_complex_roots(f, a, b) == 1
assert R.dup_count_complex_roots(f, c, d) == 0
def test_dup_count_complex_roots_2():
R, x = ring("x", ZZ)
# (z-1)*(z)
f = x**2 - x
assert R.dup_count_complex_roots(f, a, b) == 2
assert R.dup_count_complex_roots(f, c, d) == 2
# (z-1)*(-z)
f = -x**2 + x
assert R.dup_count_complex_roots(f, a, b) == 2
assert R.dup_count_complex_roots(f, c, d) == 2
# (z+1)*(z)
f = x**2 + x
assert R.dup_count_complex_roots(f, a, b) == 2
assert R.dup_count_complex_roots(f, c, d) == 1
# (z+1)*(-z)
f = -x**2 - x
assert R.dup_count_complex_roots(f, a, b) == 2
assert R.dup_count_complex_roots(f, c, d) == 1
def test_dup_count_complex_roots_3():
R, x = ring("x", ZZ)
# (z-1)*(z+1)
f = x**2 - 1
assert R.dup_count_complex_roots(f, a, b) == 2
assert R.dup_count_complex_roots(f, c, d) == 1
# (z-1)*(z+1)*(z)
f = x**3 - x
assert R.dup_count_complex_roots(f, a, b) == 3
assert R.dup_count_complex_roots(f, c, d) == 2
# (z-1)*(z+1)*(-z)
f = -x**3 + x
assert R.dup_count_complex_roots(f, a, b) == 3
assert R.dup_count_complex_roots(f, c, d) == 2
def test_dup_count_complex_roots_4():
R, x = ring("x", ZZ)
# (z-I)*(z+I)
f = x**2 + 1
assert R.dup_count_complex_roots(f, a, b) == 2
assert R.dup_count_complex_roots(f, c, d) == 1
# (z-I)*(z+I)*(z)
f = x**3 + x
assert R.dup_count_complex_roots(f, a, b) == 3
assert R.dup_count_complex_roots(f, c, d) == 2
# (z-I)*(z+I)*(-z)
f = -x**3 - x
assert R.dup_count_complex_roots(f, a, b) == 3
assert R.dup_count_complex_roots(f, c, d) == 2
# (z-I)*(z+I)*(z-1)
f = x**3 - x**2 + x - 1
assert R.dup_count_complex_roots(f, a, b) == 3
assert R.dup_count_complex_roots(f, c, d) == 2
# (z-I)*(z+I)*(z-1)*(z)
f = x**4 - x**3 + x**2 - x
assert R.dup_count_complex_roots(f, a, b) == 4
assert R.dup_count_complex_roots(f, c, d) == 3
# (z-I)*(z+I)*(z-1)*(-z)
f = -x**4 + x**3 - x**2 + x
assert R.dup_count_complex_roots(f, a, b) == 4
assert R.dup_count_complex_roots(f, c, d) == 3
# (z-I)*(z+I)*(z-1)*(z+1)
f = x**4 - 1
assert R.dup_count_complex_roots(f, a, b) == 4
assert R.dup_count_complex_roots(f, c, d) == 2
# (z-I)*(z+I)*(z-1)*(z+1)*(z)
f = x**5 - x
assert R.dup_count_complex_roots(f, a, b) == 5
assert R.dup_count_complex_roots(f, c, d) == 3
# (z-I)*(z+I)*(z-1)*(z+1)*(-z)
f = -x**5 + x
assert R.dup_count_complex_roots(f, a, b) == 5
assert R.dup_count_complex_roots(f, c, d) == 3
def test_dup_count_complex_roots_5():
R, x = ring("x", ZZ)
# (z-I+1)*(z+I+1)
f = x**2 + 2*x + 2
assert R.dup_count_complex_roots(f, a, b) == 2
assert R.dup_count_complex_roots(f, c, d) == 0
# (z-I+1)*(z+I+1)*(z-1)
f = x**3 + x**2 - 2
assert R.dup_count_complex_roots(f, a, b) == 3
assert R.dup_count_complex_roots(f, c, d) == 1
# (z-I+1)*(z+I+1)*(z-1)*z
f = x**4 + x**3 - 2*x
assert R.dup_count_complex_roots(f, a, b) == 4
assert R.dup_count_complex_roots(f, c, d) == 2
# (z-I+1)*(z+I+1)*(z+1)
f = x**3 + 3*x**2 + 4*x + 2
assert R.dup_count_complex_roots(f, a, b) == 3
assert R.dup_count_complex_roots(f, c, d) == 0
# (z-I+1)*(z+I+1)*(z+1)*z
f = x**4 + 3*x**3 + 4*x**2 + 2*x
assert R.dup_count_complex_roots(f, a, b) == 4
assert R.dup_count_complex_roots(f, c, d) == 1
# (z-I+1)*(z+I+1)*(z-1)*(z+1)
f = x**4 + 2*x**3 + x**2 - 2*x - 2
assert R.dup_count_complex_roots(f, a, b) == 4
assert R.dup_count_complex_roots(f, c, d) == 1
# (z-I+1)*(z+I+1)*(z-1)*(z+1)*z
f = x**5 + 2*x**4 + x**3 - 2*x**2 - 2*x
assert R.dup_count_complex_roots(f, a, b) == 5
assert R.dup_count_complex_roots(f, c, d) == 2
def test_dup_count_complex_roots_6():
R, x = ring("x", ZZ)
# (z-I-1)*(z+I-1)
f = x**2 - 2*x + 2
assert R.dup_count_complex_roots(f, a, b) == 2
assert R.dup_count_complex_roots(f, c, d) == 1
# (z-I-1)*(z+I-1)*(z-1)
f = x**3 - 3*x**2 + 4*x - 2
assert R.dup_count_complex_roots(f, a, b) == 3
assert R.dup_count_complex_roots(f, c, d) == 2
# (z-I-1)*(z+I-1)*(z-1)*z
f = x**4 - 3*x**3 + 4*x**2 - 2*x
assert R.dup_count_complex_roots(f, a, b) == 4
assert R.dup_count_complex_roots(f, c, d) == 3
# (z-I-1)*(z+I-1)*(z+1)
f = x**3 - x**2 + 2
assert R.dup_count_complex_roots(f, a, b) == 3
assert R.dup_count_complex_roots(f, c, d) == 1
# (z-I-1)*(z+I-1)*(z+1)*z
f = x**4 - x**3 + 2*x
assert R.dup_count_complex_roots(f, a, b) == 4
assert R.dup_count_complex_roots(f, c, d) == 2
# (z-I-1)*(z+I-1)*(z-1)*(z+1)
f = x**4 - 2*x**3 + x**2 + 2*x - 2
assert R.dup_count_complex_roots(f, a, b) == 4
assert R.dup_count_complex_roots(f, c, d) == 2
# (z-I-1)*(z+I-1)*(z-1)*(z+1)*z
f = x**5 - 2*x**4 + x**3 + 2*x**2 - 2*x
assert R.dup_count_complex_roots(f, a, b) == 5
assert R.dup_count_complex_roots(f, c, d) == 3
def test_dup_count_complex_roots_7():
R, x = ring("x", ZZ)
# (z-I-1)*(z+I-1)*(z-I+1)*(z+I+1)
f = x**4 + 4
assert R.dup_count_complex_roots(f, a, b) == 4
assert R.dup_count_complex_roots(f, c, d) == 1
# (z-I-1)*(z+I-1)*(z-I+1)*(z+I+1)*(z-2)
f = x**5 - 2*x**4 + 4*x - 8
assert R.dup_count_complex_roots(f, a, b) == 4
assert R.dup_count_complex_roots(f, c, d) == 1
# (z-I-1)*(z+I-1)*(z-I+1)*(z+I+1)*(z**2-2)
f = x**6 - 2*x**4 + 4*x**2 - 8
assert R.dup_count_complex_roots(f, a, b) == 4
assert R.dup_count_complex_roots(f, c, d) == 1
# (z-I-1)*(z+I-1)*(z-I+1)*(z+I+1)*(z-1)
f = x**5 - x**4 + 4*x - 4
assert R.dup_count_complex_roots(f, a, b) == 5
assert R.dup_count_complex_roots(f, c, d) == 2
# (z-I-1)*(z+I-1)*(z-I+1)*(z+I+1)*(z-1)*z
f = x**6 - x**5 + 4*x**2 - 4*x
assert R.dup_count_complex_roots(f, a, b) == 6
assert R.dup_count_complex_roots(f, c, d) == 3
# (z-I-1)*(z+I-1)*(z-I+1)*(z+I+1)*(z+1)
f = x**5 + x**4 + 4*x + 4
assert R.dup_count_complex_roots(f, a, b) == 5
assert R.dup_count_complex_roots(f, c, d) == 1
# (z-I-1)*(z+I-1)*(z-I+1)*(z+I+1)*(z+1)*z
f = x**6 + x**5 + 4*x**2 + 4*x
assert R.dup_count_complex_roots(f, a, b) == 6
assert R.dup_count_complex_roots(f, c, d) == 2
# (z-I-1)*(z+I-1)*(z-I+1)*(z+I+1)*(z-1)*(z+1)
f = x**6 - x**4 + 4*x**2 - 4
assert R.dup_count_complex_roots(f, a, b) == 6
assert R.dup_count_complex_roots(f, c, d) == 2
# (z-I-1)*(z+I-1)*(z-I+1)*(z+I+1)*(z-1)*(z+1)*z
f = x**7 - x**5 + 4*x**3 - 4*x
assert R.dup_count_complex_roots(f, a, b) == 7
assert R.dup_count_complex_roots(f, c, d) == 3
# (z-I-1)*(z+I-1)*(z-I+1)*(z+I+1)*(z-1)*(z+1)*(z-I)*(z+I)
f = x**8 + 3*x**4 - 4
assert R.dup_count_complex_roots(f, a, b) == 8
assert R.dup_count_complex_roots(f, c, d) == 3
def test_dup_count_complex_roots_8():
R, x = ring("x", ZZ)
# (z-I-1)*(z+I-1)*(z-I+1)*(z+I+1)*(z-1)*(z+1)*(z-I)*(z+I)*z
f = x**9 + 3*x**5 - 4*x
assert R.dup_count_complex_roots(f, a, b) == 9
assert R.dup_count_complex_roots(f, c, d) == 4
# (z-I-1)*(z+I-1)*(z-I+1)*(z+I+1)*(z-1)*(z+1)*(z-I)*(z+I)*(z**2-2)*z
f = x**11 - 2*x**9 + 3*x**7 - 6*x**5 - 4*x**3 + 8*x
assert R.dup_count_complex_roots(f, a, b) == 9
assert R.dup_count_complex_roots(f, c, d) == 4
def test_dup_count_complex_roots_implicit():
R, x = ring("x", ZZ)
# z*(z-1)*(z+1)*(z-I)*(z+I)
f = x**5 - x
assert R.dup_count_complex_roots(f) == 5
assert R.dup_count_complex_roots(f, sup=(0, 0)) == 3
assert R.dup_count_complex_roots(f, inf=(0, 0)) == 3
def test_dup_count_complex_roots_exclude():
R, x = ring("x", ZZ)
# z*(z-1)*(z+1)*(z-I)*(z+I)
f = x**5 - x
a, b = (-QQ(1), QQ(0)), (QQ(1), QQ(1))
assert R.dup_count_complex_roots(f, a, b) == 4
assert R.dup_count_complex_roots(f, a, b, exclude=['S']) == 3
assert R.dup_count_complex_roots(f, a, b, exclude=['N']) == 3
assert R.dup_count_complex_roots(f, a, b, exclude=['S', 'N']) == 2
assert R.dup_count_complex_roots(f, a, b, exclude=['E']) == 4
assert R.dup_count_complex_roots(f, a, b, exclude=['W']) == 4
assert R.dup_count_complex_roots(f, a, b, exclude=['E', 'W']) == 4
assert R.dup_count_complex_roots(f, a, b, exclude=['N', 'S', 'E', 'W']) == 2
assert R.dup_count_complex_roots(f, a, b, exclude=['SW']) == 3
assert R.dup_count_complex_roots(f, a, b, exclude=['SE']) == 3
assert R.dup_count_complex_roots(f, a, b, exclude=['SW', 'SE']) == 2
assert R.dup_count_complex_roots(f, a, b, exclude=['SW', 'SE', 'S']) == 1
assert R.dup_count_complex_roots(f, a, b, exclude=['SW', 'SE', 'S', 'N']) == 0
a, b = (QQ(0), QQ(0)), (QQ(1), QQ(1))
assert R.dup_count_complex_roots(f, a, b, exclude=True) == 1
def test_dup_isolate_complex_roots_sqf():
R, x = ring("x", ZZ)
f = x**2 - 2*x + 3
assert R.dup_isolate_complex_roots_sqf(f) == \
[((0, -6), (6, 0)), ((0, 0), (6, 6))]
assert [ r.as_tuple() for r in R.dup_isolate_complex_roots_sqf(f, blackbox=True) ] == \
[((0, -6), (6, 0)), ((0, 0), (6, 6))]
assert R.dup_isolate_complex_roots_sqf(f, eps=QQ(1, 10)) == \
[((QQ(15, 16), -QQ(3, 2)), (QQ(33, 32), -QQ(45, 32))),
((QQ(15, 16), QQ(45, 32)), (QQ(33, 32), QQ(3, 2)))]
assert R.dup_isolate_complex_roots_sqf(f, eps=QQ(1, 100)) == \
[((QQ(255, 256), -QQ(363, 256)), (QQ(513, 512), -QQ(723, 512))),
((QQ(255, 256), QQ(723, 512)), (QQ(513, 512), QQ(363, 256)))]
f = 7*x**4 - 19*x**3 + 20*x**2 + 17*x + 20
assert R.dup_isolate_complex_roots_sqf(f) == \
[((-QQ(40, 7), -QQ(40, 7)), (0, 0)), ((-QQ(40, 7), 0), (0, QQ(40, 7))),
((0, -QQ(40, 7)), (QQ(40, 7), 0)), ((0, 0), (QQ(40, 7), QQ(40, 7)))]
def test_dup_isolate_all_roots_sqf():
R, x = ring("x", ZZ)
f = 4*x**4 - x**3 + 2*x**2 + 5*x
assert R.dup_isolate_all_roots_sqf(f) == \
([(-1, 0), (0, 0)],
[((0, -QQ(5, 2)), (QQ(5, 2), 0)), ((0, 0), (QQ(5, 2), QQ(5, 2)))])
assert R.dup_isolate_all_roots_sqf(f, eps=QQ(1, 10)) == \
([(QQ(-7, 8), QQ(-6, 7)), (0, 0)],
[((QQ(35, 64), -QQ(35, 32)), (QQ(5, 8), -QQ(65, 64))), ((QQ(35, 64), QQ(65, 64)), (QQ(5, 8), QQ(35, 32)))])
def test_dup_isolate_all_roots():
R, x = ring("x", ZZ)
f = 4*x**4 - x**3 + 2*x**2 + 5*x
assert R.dup_isolate_all_roots(f) == \
([((-1, 0), 1), ((0, 0), 1)],
[(((0, -QQ(5, 2)), (QQ(5, 2), 0)), 1),
(((0, 0), (QQ(5, 2), QQ(5, 2))), 1)])
assert R.dup_isolate_all_roots(f, eps=QQ(1, 10)) == \
([((QQ(-7, 8), QQ(-6, 7)), 1), ((0, 0), 1)],
[(((QQ(35, 64), -QQ(35, 32)), (QQ(5, 8), -QQ(65, 64))), 1),
(((QQ(35, 64), QQ(65, 64)), (QQ(5, 8), QQ(35, 32))), 1)])
f = x**5 + x**4 - 2*x**3 - 2*x**2 + x + 1
raises(NotImplementedError, lambda: R.dup_isolate_all_roots(f))

View File

@ -0,0 +1,653 @@
"""Tests for the implementation of RootOf class and related tools. """
from sympy.polys.polytools import Poly
import sympy.polys.rootoftools as rootoftools
from sympy.polys.rootoftools import (rootof, RootOf, CRootOf, RootSum,
_pure_key_dict as D)
from sympy.polys.polyerrors import (
MultivariatePolynomialError,
GeneratorsNeeded,
PolynomialError,
)
from sympy.core.function import (Function, Lambda)
from sympy.core.numbers import (Float, I, Rational)
from sympy.core.relational import Eq
from sympy.core.singleton import S
from sympy.functions.elementary.exponential import (exp, log)
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.functions.elementary.trigonometric import tan
from sympy.integrals.integrals import Integral
from sympy.polys.orthopolys import legendre_poly
from sympy.solvers.solvers import solve
from sympy.testing.pytest import raises, slow
from sympy.core.expr import unchanged
from sympy.abc import a, b, x, y, z, r
def test_CRootOf___new__():
assert rootof(x, 0) == 0
assert rootof(x, -1) == 0
assert rootof(x, S.Zero) == 0
assert rootof(x - 1, 0) == 1
assert rootof(x - 1, -1) == 1
assert rootof(x + 1, 0) == -1
assert rootof(x + 1, -1) == -1
assert rootof(x**2 + 2*x + 3, 0) == -1 - I*sqrt(2)
assert rootof(x**2 + 2*x + 3, 1) == -1 + I*sqrt(2)
assert rootof(x**2 + 2*x + 3, -1) == -1 + I*sqrt(2)
assert rootof(x**2 + 2*x + 3, -2) == -1 - I*sqrt(2)
r = rootof(x**2 + 2*x + 3, 0, radicals=False)
assert isinstance(r, RootOf) is True
r = rootof(x**2 + 2*x + 3, 1, radicals=False)
assert isinstance(r, RootOf) is True
r = rootof(x**2 + 2*x + 3, -1, radicals=False)
assert isinstance(r, RootOf) is True
r = rootof(x**2 + 2*x + 3, -2, radicals=False)
assert isinstance(r, RootOf) is True
assert rootof((x - 1)*(x + 1), 0, radicals=False) == -1
assert rootof((x - 1)*(x + 1), 1, radicals=False) == 1
assert rootof((x - 1)*(x + 1), -1, radicals=False) == 1
assert rootof((x - 1)*(x + 1), -2, radicals=False) == -1
assert rootof((x - 1)*(x + 1), 0, radicals=True) == -1
assert rootof((x - 1)*(x + 1), 1, radicals=True) == 1
assert rootof((x - 1)*(x + 1), -1, radicals=True) == 1
assert rootof((x - 1)*(x + 1), -2, radicals=True) == -1
assert rootof((x - 1)*(x**3 + x + 3), 0) == rootof(x**3 + x + 3, 0)
assert rootof((x - 1)*(x**3 + x + 3), 1) == 1
assert rootof((x - 1)*(x**3 + x + 3), 2) == rootof(x**3 + x + 3, 1)
assert rootof((x - 1)*(x**3 + x + 3), 3) == rootof(x**3 + x + 3, 2)
assert rootof((x - 1)*(x**3 + x + 3), -1) == rootof(x**3 + x + 3, 2)
assert rootof((x - 1)*(x**3 + x + 3), -2) == rootof(x**3 + x + 3, 1)
assert rootof((x - 1)*(x**3 + x + 3), -3) == 1
assert rootof((x - 1)*(x**3 + x + 3), -4) == rootof(x**3 + x + 3, 0)
assert rootof(x**4 + 3*x**3, 0) == -3
assert rootof(x**4 + 3*x**3, 1) == 0
assert rootof(x**4 + 3*x**3, 2) == 0
assert rootof(x**4 + 3*x**3, 3) == 0
raises(GeneratorsNeeded, lambda: rootof(0, 0))
raises(GeneratorsNeeded, lambda: rootof(1, 0))
raises(PolynomialError, lambda: rootof(Poly(0, x), 0))
raises(PolynomialError, lambda: rootof(Poly(1, x), 0))
raises(PolynomialError, lambda: rootof(x - y, 0))
# issue 8617
raises(PolynomialError, lambda: rootof(exp(x), 0))
raises(NotImplementedError, lambda: rootof(x**3 - x + sqrt(2), 0))
raises(NotImplementedError, lambda: rootof(x**3 - x + I, 0))
raises(IndexError, lambda: rootof(x**2 - 1, -4))
raises(IndexError, lambda: rootof(x**2 - 1, -3))
raises(IndexError, lambda: rootof(x**2 - 1, 2))
raises(IndexError, lambda: rootof(x**2 - 1, 3))
raises(ValueError, lambda: rootof(x**2 - 1, x))
assert rootof(Poly(x - y, x), 0) == y
assert rootof(Poly(x**2 - y, x), 0) == -sqrt(y)
assert rootof(Poly(x**2 - y, x), 1) == sqrt(y)
assert rootof(Poly(x**3 - y, x), 0) == y**Rational(1, 3)
assert rootof(y*x**3 + y*x + 2*y, x, 0) == -1
raises(NotImplementedError, lambda: rootof(x**3 + x + 2*y, x, 0))
assert rootof(x**3 + x + 1, 0).is_commutative is True
def test_CRootOf_attributes():
r = rootof(x**3 + x + 3, 0)
assert r.is_number
assert r.free_symbols == set()
# if the following assertion fails then multivariate polynomials
# are apparently supported and the RootOf.free_symbols routine
# should be changed to return whatever symbols would not be
# the PurePoly dummy symbol
raises(NotImplementedError, lambda: rootof(Poly(x**3 + y*x + 1, x), 0))
def test_CRootOf___eq__():
assert (rootof(x**3 + x + 3, 0) == rootof(x**3 + x + 3, 0)) is True
assert (rootof(x**3 + x + 3, 0) == rootof(x**3 + x + 3, 1)) is False
assert (rootof(x**3 + x + 3, 1) == rootof(x**3 + x + 3, 1)) is True
assert (rootof(x**3 + x + 3, 1) == rootof(x**3 + x + 3, 2)) is False
assert (rootof(x**3 + x + 3, 2) == rootof(x**3 + x + 3, 2)) is True
assert (rootof(x**3 + x + 3, 0) == rootof(y**3 + y + 3, 0)) is True
assert (rootof(x**3 + x + 3, 0) == rootof(y**3 + y + 3, 1)) is False
assert (rootof(x**3 + x + 3, 1) == rootof(y**3 + y + 3, 1)) is True
assert (rootof(x**3 + x + 3, 1) == rootof(y**3 + y + 3, 2)) is False
assert (rootof(x**3 + x + 3, 2) == rootof(y**3 + y + 3, 2)) is True
def test_CRootOf___eval_Eq__():
f = Function('f')
eq = x**3 + x + 3
r = rootof(eq, 2)
r1 = rootof(eq, 1)
assert Eq(r, r1) is S.false
assert Eq(r, r) is S.true
assert unchanged(Eq, r, x)
assert Eq(r, 0) is S.false
assert Eq(r, S.Infinity) is S.false
assert Eq(r, I) is S.false
assert unchanged(Eq, r, f(0))
sol = solve(eq)
for s in sol:
if s.is_real:
assert Eq(r, s) is S.false
r = rootof(eq, 0)
for s in sol:
if s.is_real:
assert Eq(r, s) is S.true
eq = x**3 + x + 1
sol = solve(eq)
assert [Eq(rootof(eq, i), j) for i in range(3) for j in sol
].count(True) == 3
assert Eq(rootof(eq, 0), 1 + S.ImaginaryUnit) == False
def test_CRootOf_is_real():
assert rootof(x**3 + x + 3, 0).is_real is True
assert rootof(x**3 + x + 3, 1).is_real is False
assert rootof(x**3 + x + 3, 2).is_real is False
def test_CRootOf_is_complex():
assert rootof(x**3 + x + 3, 0).is_complex is True
def test_CRootOf_subs():
assert rootof(x**3 + x + 1, 0).subs(x, y) == rootof(y**3 + y + 1, 0)
def test_CRootOf_diff():
assert rootof(x**3 + x + 1, 0).diff(x) == 0
assert rootof(x**3 + x + 1, 0).diff(y) == 0
@slow
def test_CRootOf_evalf():
real = rootof(x**3 + x + 3, 0).evalf(n=20)
assert real.epsilon_eq(Float("-1.2134116627622296341"))
re, im = rootof(x**3 + x + 3, 1).evalf(n=20).as_real_imag()
assert re.epsilon_eq( Float("0.60670583138111481707"))
assert im.epsilon_eq(-Float("1.45061224918844152650"))
re, im = rootof(x**3 + x + 3, 2).evalf(n=20).as_real_imag()
assert re.epsilon_eq(Float("0.60670583138111481707"))
assert im.epsilon_eq(Float("1.45061224918844152650"))
p = legendre_poly(4, x, polys=True)
roots = [str(r.n(17)) for r in p.real_roots()]
# magnitudes are given by
# sqrt(3/S(7) - 2*sqrt(6/S(5))/7)
# and
# sqrt(3/S(7) + 2*sqrt(6/S(5))/7)
assert roots == [
"-0.86113631159405258",
"-0.33998104358485626",
"0.33998104358485626",
"0.86113631159405258",
]
re = rootof(x**5 - 5*x + 12, 0).evalf(n=20)
assert re.epsilon_eq(Float("-1.84208596619025438271"))
re, im = rootof(x**5 - 5*x + 12, 1).evalf(n=20).as_real_imag()
assert re.epsilon_eq(Float("-0.351854240827371999559"))
assert im.epsilon_eq(Float("-1.709561043370328882010"))
re, im = rootof(x**5 - 5*x + 12, 2).evalf(n=20).as_real_imag()
assert re.epsilon_eq(Float("-0.351854240827371999559"))
assert im.epsilon_eq(Float("+1.709561043370328882010"))
re, im = rootof(x**5 - 5*x + 12, 3).evalf(n=20).as_real_imag()
assert re.epsilon_eq(Float("+1.272897223922499190910"))
assert im.epsilon_eq(Float("-0.719798681483861386681"))
re, im = rootof(x**5 - 5*x + 12, 4).evalf(n=20).as_real_imag()
assert re.epsilon_eq(Float("+1.272897223922499190910"))
assert im.epsilon_eq(Float("+0.719798681483861386681"))
# issue 6393
assert str(rootof(x**5 + 2*x**4 + x**3 - 68719476736, 0).n(3)) == '147.'
eq = (531441*x**11 + 3857868*x**10 + 13730229*x**9 + 32597882*x**8 +
55077472*x**7 + 60452000*x**6 + 32172064*x**5 - 4383808*x**4 -
11942912*x**3 - 1506304*x**2 + 1453312*x + 512)
a, b = rootof(eq, 1).n(2).as_real_imag()
c, d = rootof(eq, 2).n(2).as_real_imag()
assert a == c
assert b < d
assert b == -d
# issue 6451
r = rootof(legendre_poly(64, x), 7)
assert r.n(2) == r.n(100).n(2)
# issue 9019
r0 = rootof(x**2 + 1, 0, radicals=False)
r1 = rootof(x**2 + 1, 1, radicals=False)
assert r0.n(4) == Float(-1.0, 4) * I
assert r1.n(4) == Float(1.0, 4) * I
# make sure verification is used in case a max/min traps the "root"
assert str(rootof(4*x**5 + 16*x**3 + 12*x**2 + 7, 0).n(3)) == '-0.976'
# watch out for UnboundLocalError
c = CRootOf(90720*x**6 - 4032*x**4 + 84*x**2 - 1, 0)
assert c._eval_evalf(2) # doesn't fail
# watch out for imaginary parts that don't want to evaluate
assert str(RootOf(x**16 + 32*x**14 + 508*x**12 + 5440*x**10 +
39510*x**8 + 204320*x**6 + 755548*x**4 + 1434496*x**2 +
877969, 10).n(2)) == '-3.4*I'
assert abs(RootOf(x**4 + 10*x**2 + 1, 0).n(2)) < 0.4
# check reset and args
r = [RootOf(x**3 + x + 3, i) for i in range(3)]
r[0]._reset()
for ri in r:
i = ri._get_interval()
ri.n(2)
assert i != ri._get_interval()
ri._reset()
assert i == ri._get_interval()
assert i == i.func(*i.args)
def test_issue_24978():
# Irreducible poly with negative leading coeff is normalized
# (factor of -1 is extracted), before being stored as CRootOf.poly.
f = -x**2 + 2
r = CRootOf(f, 0)
assert r.poly.as_expr() == x**2 - 2
# An action that prompts calculation of an interval puts r.poly in
# the cache.
r.n()
assert r.poly in rootoftools._reals_cache
def test_CRootOf_evalf_caching_bug():
r = rootof(x**5 - 5*x + 12, 1)
r.n()
a = r._get_interval()
r = rootof(x**5 - 5*x + 12, 1)
r.n()
b = r._get_interval()
assert a == b
def test_CRootOf_real_roots():
assert Poly(x**5 + x + 1).real_roots() == [rootof(x**3 - x**2 + 1, 0)]
assert Poly(x**5 + x + 1).real_roots(radicals=False) == [rootof(
x**3 - x**2 + 1, 0)]
# https://github.com/sympy/sympy/issues/20902
p = Poly(-3*x**4 - 10*x**3 - 12*x**2 - 6*x - 1, x, domain='ZZ')
assert CRootOf.real_roots(p) == [S(-1), S(-1), S(-1), S(-1)/3]
def test_CRootOf_all_roots():
assert Poly(x**5 + x + 1).all_roots() == [
rootof(x**3 - x**2 + 1, 0),
Rational(-1, 2) - sqrt(3)*I/2,
Rational(-1, 2) + sqrt(3)*I/2,
rootof(x**3 - x**2 + 1, 1),
rootof(x**3 - x**2 + 1, 2),
]
assert Poly(x**5 + x + 1).all_roots(radicals=False) == [
rootof(x**3 - x**2 + 1, 0),
rootof(x**2 + x + 1, 0, radicals=False),
rootof(x**2 + x + 1, 1, radicals=False),
rootof(x**3 - x**2 + 1, 1),
rootof(x**3 - x**2 + 1, 2),
]
def test_CRootOf_eval_rational():
p = legendre_poly(4, x, polys=True)
roots = [r.eval_rational(n=18) for r in p.real_roots()]
for root in roots:
assert isinstance(root, Rational)
roots = [str(root.n(17)) for root in roots]
assert roots == [
"-0.86113631159405258",
"-0.33998104358485626",
"0.33998104358485626",
"0.86113631159405258",
]
def test_CRootOf_lazy():
# irreducible poly with both real and complex roots:
f = Poly(x**3 + 2*x + 2)
# real root:
CRootOf.clear_cache()
r = CRootOf(f, 0)
# Not yet in cache, after construction:
assert r.poly not in rootoftools._reals_cache
assert r.poly not in rootoftools._complexes_cache
r.evalf()
# In cache after evaluation:
assert r.poly in rootoftools._reals_cache
assert r.poly not in rootoftools._complexes_cache
# complex root:
CRootOf.clear_cache()
r = CRootOf(f, 1)
# Not yet in cache, after construction:
assert r.poly not in rootoftools._reals_cache
assert r.poly not in rootoftools._complexes_cache
r.evalf()
# In cache after evaluation:
assert r.poly in rootoftools._reals_cache
assert r.poly in rootoftools._complexes_cache
# composite poly with both real and complex roots:
f = Poly((x**2 - 2)*(x**2 + 1))
# real root:
CRootOf.clear_cache()
r = CRootOf(f, 0)
# In cache immediately after construction:
assert r.poly in rootoftools._reals_cache
assert r.poly not in rootoftools._complexes_cache
# complex root:
CRootOf.clear_cache()
r = CRootOf(f, 2)
# In cache immediately after construction:
assert r.poly in rootoftools._reals_cache
assert r.poly in rootoftools._complexes_cache
def test_RootSum___new__():
f = x**3 + x + 3
g = Lambda(r, log(r*x))
s = RootSum(f, g)
assert isinstance(s, RootSum) is True
assert RootSum(f**2, g) == 2*RootSum(f, g)
assert RootSum((x - 7)*f**3, g) == log(7*x) + 3*RootSum(f, g)
# issue 5571
assert hash(RootSum((x - 7)*f**3, g)) == hash(log(7*x) + 3*RootSum(f, g))
raises(MultivariatePolynomialError, lambda: RootSum(x**3 + x + y))
raises(ValueError, lambda: RootSum(x**2 + 3, lambda x: x))
assert RootSum(f, exp) == RootSum(f, Lambda(x, exp(x)))
assert RootSum(f, log) == RootSum(f, Lambda(x, log(x)))
assert isinstance(RootSum(f, auto=False), RootSum) is True
assert RootSum(f) == 0
assert RootSum(f, Lambda(x, x)) == 0
assert RootSum(f, Lambda(x, x**2)) == -2
assert RootSum(f, Lambda(x, 1)) == 3
assert RootSum(f, Lambda(x, 2)) == 6
assert RootSum(f, auto=False).is_commutative is True
assert RootSum(f, Lambda(x, 1/(x + x**2))) == Rational(11, 3)
assert RootSum(f, Lambda(x, y/(x + x**2))) == Rational(11, 3)*y
assert RootSum(x**2 - 1, Lambda(x, 3*x**2), x) == 6
assert RootSum(x**2 - y, Lambda(x, 3*x**2), x) == 6*y
assert RootSum(x**2 - 1, Lambda(x, z*x**2), x) == 2*z
assert RootSum(x**2 - y, Lambda(x, z*x**2), x) == 2*z*y
assert RootSum(
x**2 - 1, Lambda(x, exp(x)), quadratic=True) == exp(-1) + exp(1)
assert RootSum(x**3 + a*x + a**3, tan, x) == \
RootSum(x**3 + x + 1, Lambda(x, tan(a*x)))
assert RootSum(a**3*x**3 + a*x + 1, tan, x) == \
RootSum(x**3 + x + 1, Lambda(x, tan(x/a)))
def test_RootSum_free_symbols():
assert RootSum(x**3 + x + 3, Lambda(r, exp(r))).free_symbols == set()
assert RootSum(x**3 + x + 3, Lambda(r, exp(a*r))).free_symbols == {a}
assert RootSum(
x**3 + x + y, Lambda(r, exp(a*r)), x).free_symbols == {a, y}
def test_RootSum___eq__():
f = Lambda(x, exp(x))
assert (RootSum(x**3 + x + 1, f) == RootSum(x**3 + x + 1, f)) is True
assert (RootSum(x**3 + x + 1, f) == RootSum(y**3 + y + 1, f)) is True
assert (RootSum(x**3 + x + 1, f) == RootSum(x**3 + x + 2, f)) is False
assert (RootSum(x**3 + x + 1, f) == RootSum(y**3 + y + 2, f)) is False
def test_RootSum_doit():
rs = RootSum(x**2 + 1, exp)
assert isinstance(rs, RootSum) is True
assert rs.doit() == exp(-I) + exp(I)
rs = RootSum(x**2 + a, exp, x)
assert isinstance(rs, RootSum) is True
assert rs.doit() == exp(-sqrt(-a)) + exp(sqrt(-a))
def test_RootSum_evalf():
rs = RootSum(x**2 + 1, exp)
assert rs.evalf(n=20, chop=True).epsilon_eq(Float("1.0806046117362794348"))
assert rs.evalf(n=15, chop=True).epsilon_eq(Float("1.08060461173628"))
rs = RootSum(x**2 + a, exp, x)
assert rs.evalf() == rs
def test_RootSum_diff():
f = x**3 + x + 3
g = Lambda(r, exp(r*x))
h = Lambda(r, r*exp(r*x))
assert RootSum(f, g).diff(x) == RootSum(f, h)
def test_RootSum_subs():
f = x**3 + x + 3
g = Lambda(r, exp(r*x))
F = y**3 + y + 3
G = Lambda(r, exp(r*y))
assert RootSum(f, g).subs(y, 1) == RootSum(f, g)
assert RootSum(f, g).subs(x, y) == RootSum(F, G)
def test_RootSum_rational():
assert RootSum(
z**5 - z + 1, Lambda(z, z/(x - z))) == (4*x - 5)/(x**5 - x + 1)
f = 161*z**3 + 115*z**2 + 19*z + 1
g = Lambda(z, z*log(
-3381*z**4/4 - 3381*z**3/4 - 625*z**2/2 - z*Rational(125, 2) - 5 + exp(x)))
assert RootSum(f, g).diff(x) == -(
(5*exp(2*x) - 6*exp(x) + 4)*exp(x)/(exp(3*x) - exp(2*x) + 1))/7
def test_RootSum_independent():
f = (x**3 - a)**2*(x**4 - b)**3
g = Lambda(x, 5*tan(x) + 7)
h = Lambda(x, tan(x))
r0 = RootSum(x**3 - a, h, x)
r1 = RootSum(x**4 - b, h, x)
assert RootSum(f, g, x).as_ordered_terms() == [10*r0, 15*r1, 126]
def test_issue_7876():
l1 = Poly(x**6 - x + 1, x).all_roots()
l2 = [rootof(x**6 - x + 1, i) for i in range(6)]
assert frozenset(l1) == frozenset(l2)
def test_issue_8316():
f = Poly(7*x**8 - 9)
assert len(f.all_roots()) == 8
f = Poly(7*x**8 - 10)
assert len(f.all_roots()) == 8
def test__imag_count():
from sympy.polys.rootoftools import _imag_count_of_factor
def imag_count(p):
return sum(_imag_count_of_factor(f)*m for f, m in
p.factor_list()[1])
assert imag_count(Poly(x**6 + 10*x**2 + 1)) == 2
assert imag_count(Poly(x**2)) == 0
assert imag_count(Poly([1]*3 + [-1], x)) == 0
assert imag_count(Poly(x**3 + 1)) == 0
assert imag_count(Poly(x**2 + 1)) == 2
assert imag_count(Poly(x**2 - 1)) == 0
assert imag_count(Poly(x**4 - 1)) == 2
assert imag_count(Poly(x**4 + 1)) == 0
assert imag_count(Poly([1, 2, 3], x)) == 0
assert imag_count(Poly(x**3 + x + 1)) == 0
assert imag_count(Poly(x**4 + x + 1)) == 0
def q(r1, r2, p):
return Poly(((x - r1)*(x - r2)).subs(x, x**p), x)
assert imag_count(q(-1, -2, 2)) == 4
assert imag_count(q(-1, 2, 2)) == 2
assert imag_count(q(1, 2, 2)) == 0
assert imag_count(q(1, 2, 4)) == 4
assert imag_count(q(-1, 2, 4)) == 2
assert imag_count(q(-1, -2, 4)) == 0
def test_RootOf_is_imaginary():
r = RootOf(x**4 + 4*x**2 + 1, 1)
i = r._get_interval()
assert r.is_imaginary and i.ax*i.bx <= 0
def test_is_disjoint():
eq = x**3 + 5*x + 1
ir = rootof(eq, 0)._get_interval()
ii = rootof(eq, 1)._get_interval()
assert ir.is_disjoint(ii)
assert ii.is_disjoint(ir)
def test_pure_key_dict():
p = D()
assert (x in p) is False
assert (1 in p) is False
p[x] = 1
assert x in p
assert y in p
assert p[y] == 1
raises(KeyError, lambda: p[1])
def dont(k):
p[k] = 2
raises(ValueError, lambda: dont(1))
@slow
def test_eval_approx_relative():
CRootOf.clear_cache()
t = [CRootOf(x**3 + 10*x + 1, i) for i in range(3)]
assert [i.eval_rational(1e-1) for i in t] == [
Rational(-21, 220), Rational(15, 256) - I*805/256,
Rational(15, 256) + I*805/256]
t[0]._reset()
assert [i.eval_rational(1e-1, 1e-4) for i in t] == [
Rational(-21, 220), Rational(3275, 65536) - I*414645/131072,
Rational(3275, 65536) + I*414645/131072]
assert S(t[0]._get_interval().dx) < 1e-1
assert S(t[1]._get_interval().dx) < 1e-1
assert S(t[1]._get_interval().dy) < 1e-4
assert S(t[2]._get_interval().dx) < 1e-1
assert S(t[2]._get_interval().dy) < 1e-4
t[0]._reset()
assert [i.eval_rational(1e-4, 1e-4) for i in t] == [
Rational(-2001, 20020), Rational(6545, 131072) - I*414645/131072,
Rational(6545, 131072) + I*414645/131072]
assert S(t[0]._get_interval().dx) < 1e-4
assert S(t[1]._get_interval().dx) < 1e-4
assert S(t[1]._get_interval().dy) < 1e-4
assert S(t[2]._get_interval().dx) < 1e-4
assert S(t[2]._get_interval().dy) < 1e-4
# in the following, the actual relative precision is
# less than tested, but it should never be greater
t[0]._reset()
assert [i.eval_rational(n=2) for i in t] == [
Rational(-202201, 2024022), Rational(104755, 2097152) - I*6634255/2097152,
Rational(104755, 2097152) + I*6634255/2097152]
assert abs(S(t[0]._get_interval().dx)/t[0]) < 1e-2
assert abs(S(t[1]._get_interval().dx)/t[1]).n() < 1e-2
assert abs(S(t[1]._get_interval().dy)/t[1]).n() < 1e-2
assert abs(S(t[2]._get_interval().dx)/t[2]).n() < 1e-2
assert abs(S(t[2]._get_interval().dy)/t[2]).n() < 1e-2
t[0]._reset()
assert [i.eval_rational(n=3) for i in t] == [
Rational(-202201, 2024022), Rational(1676045, 33554432) - I*106148135/33554432,
Rational(1676045, 33554432) + I*106148135/33554432]
assert abs(S(t[0]._get_interval().dx)/t[0]) < 1e-3
assert abs(S(t[1]._get_interval().dx)/t[1]).n() < 1e-3
assert abs(S(t[1]._get_interval().dy)/t[1]).n() < 1e-3
assert abs(S(t[2]._get_interval().dx)/t[2]).n() < 1e-3
assert abs(S(t[2]._get_interval().dy)/t[2]).n() < 1e-3
t[0]._reset()
a = [i.eval_approx(2) for i in t]
assert [str(i) for i in a] == [
'-0.10', '0.05 - 3.2*I', '0.05 + 3.2*I']
assert all(abs(((a[i] - t[i])/t[i]).n()) < 1e-2 for i in range(len(a)))
def test_issue_15920():
r = rootof(x**5 - x + 1, 0)
p = Integral(x, (x, 1, y))
assert unchanged(Eq, r, p)
def test_issue_19113():
eq = y**3 - y + 1
# generator is a canonical x in RootOf
assert str(Poly(eq).real_roots()) == '[CRootOf(x**3 - x + 1, 0)]'
assert str(Poly(eq.subs(y, tan(y))).real_roots()
) == '[CRootOf(x**3 - x + 1, 0)]'
assert str(Poly(eq.subs(y, tan(x))).real_roots()
) == '[CRootOf(x**3 - x + 1, 0)]'

View File

@ -0,0 +1,112 @@
"""Tests for low-level linear systems solver. """
from sympy.matrices import Matrix
from sympy.polys.domains import ZZ, QQ
from sympy.polys.fields import field
from sympy.polys.rings import ring
from sympy.polys.solvers import solve_lin_sys, eqs_to_matrix
def test_solve_lin_sys_2x2_one():
domain, x1,x2 = ring("x1,x2", QQ)
eqs = [x1 + x2 - 5,
2*x1 - x2]
sol = {x1: QQ(5, 3), x2: QQ(10, 3)}
_sol = solve_lin_sys(eqs, domain)
assert _sol == sol and all(isinstance(s, domain.dtype) for s in _sol)
def test_solve_lin_sys_2x4_none():
domain, x1,x2 = ring("x1,x2", QQ)
eqs = [x1 - 1,
x1 - x2,
x1 - 2*x2,
x2 - 1]
assert solve_lin_sys(eqs, domain) is None
def test_solve_lin_sys_3x4_one():
domain, x1,x2,x3 = ring("x1,x2,x3", QQ)
eqs = [x1 + 2*x2 + 3*x3,
2*x1 - x2 + x3,
3*x1 + x2 + x3,
5*x2 + 2*x3]
sol = {x1: 0, x2: 0, x3: 0}
assert solve_lin_sys(eqs, domain) == sol
def test_solve_lin_sys_3x3_inf():
domain, x1,x2,x3 = ring("x1,x2,x3", QQ)
eqs = [x1 - x2 + 2*x3 - 1,
2*x1 + x2 + x3 - 8,
x1 + x2 - 5]
sol = {x1: -x3 + 3, x2: x3 + 2}
assert solve_lin_sys(eqs, domain) == sol
def test_solve_lin_sys_3x4_none():
domain, x1,x2,x3,x4 = ring("x1,x2,x3,x4", QQ)
eqs = [2*x1 + x2 + 7*x3 - 7*x4 - 2,
-3*x1 + 4*x2 - 5*x3 - 6*x4 - 3,
x1 + x2 + 4*x3 - 5*x4 - 2]
assert solve_lin_sys(eqs, domain) is None
def test_solve_lin_sys_4x7_inf():
domain, x1,x2,x3,x4,x5,x6,x7 = ring("x1,x2,x3,x4,x5,x6,x7", QQ)
eqs = [x1 + 4*x2 - x4 + 7*x6 - 9*x7 - 3,
2*x1 + 8*x2 - x3 + 3*x4 + 9*x5 - 13*x6 + 7*x7 - 9,
2*x3 - 3*x4 - 4*x5 + 12*x6 - 8*x7 - 1,
-x1 - 4*x2 + 2*x3 + 4*x4 + 8*x5 - 31*x6 + 37*x7 - 4]
sol = {x1: 4 - 4*x2 - 2*x5 - x6 + 3*x7,
x3: 2 - x5 + 3*x6 - 5*x7,
x4: 1 - 2*x5 + 6*x6 - 6*x7}
assert solve_lin_sys(eqs, domain) == sol
def test_solve_lin_sys_5x5_inf():
domain, x1,x2,x3,x4,x5 = ring("x1,x2,x3,x4,x5", QQ)
eqs = [x1 - x2 - 2*x3 + x4 + 11*x5 - 13,
x1 - x2 + x3 + x4 + 5*x5 - 16,
2*x1 - 2*x2 + x4 + 10*x5 - 21,
2*x1 - 2*x2 - x3 + 3*x4 + 20*x5 - 38,
2*x1 - 2*x2 + x3 + x4 + 8*x5 - 22]
sol = {x1: 6 + x2 - 3*x5,
x3: 1 + 2*x5,
x4: 9 - 4*x5}
assert solve_lin_sys(eqs, domain) == sol
def test_solve_lin_sys_6x6_1():
ground, d,r,e,g,i,j,l,o,m,p,q = field("d,r,e,g,i,j,l,o,m,p,q", ZZ)
domain, c,f,h,k,n,b = ring("c,f,h,k,n,b", ground)
eqs = [b + q/d - c/d, c*(1/d + 1/e + 1/g) - f/g - q/d, f*(1/g + 1/i + 1/j) - c/g - h/i, h*(1/i + 1/l + 1/m) - f/i - k/m, k*(1/m + 1/o + 1/p) - h/m - n/p, n/p - k/p]
sol = {
b: (e*i*l*q + e*i*m*q + e*i*o*q + e*j*l*q + e*j*m*q + e*j*o*q + e*l*m*q + e*l*o*q + g*i*l*q + g*i*m*q + g*i*o*q + g*j*l*q + g*j*m*q + g*j*o*q + g*l*m*q + g*l*o*q + i*j*l*q + i*j*m*q + i*j*o*q + j*l*m*q + j*l*o*q)/(-d*e*i*l - d*e*i*m - d*e*i*o - d*e*j*l - d*e*j*m - d*e*j*o - d*e*l*m - d*e*l*o - d*g*i*l - d*g*i*m - d*g*i*o - d*g*j*l - d*g*j*m - d*g*j*o - d*g*l*m - d*g*l*o - d*i*j*l - d*i*j*m - d*i*j*o - d*j*l*m - d*j*l*o - e*g*i*l - e*g*i*m - e*g*i*o - e*g*j*l - e*g*j*m - e*g*j*o - e*g*l*m - e*g*l*o - e*i*j*l - e*i*j*m - e*i*j*o - e*j*l*m - e*j*l*o),
c: (-e*g*i*l*q - e*g*i*m*q - e*g*i*o*q - e*g*j*l*q - e*g*j*m*q - e*g*j*o*q - e*g*l*m*q - e*g*l*o*q - e*i*j*l*q - e*i*j*m*q - e*i*j*o*q - e*j*l*m*q - e*j*l*o*q)/(-d*e*i*l - d*e*i*m - d*e*i*o - d*e*j*l - d*e*j*m - d*e*j*o - d*e*l*m - d*e*l*o - d*g*i*l - d*g*i*m - d*g*i*o - d*g*j*l - d*g*j*m - d*g*j*o - d*g*l*m - d*g*l*o - d*i*j*l - d*i*j*m - d*i*j*o - d*j*l*m - d*j*l*o - e*g*i*l - e*g*i*m - e*g*i*o - e*g*j*l - e*g*j*m - e*g*j*o - e*g*l*m - e*g*l*o - e*i*j*l - e*i*j*m - e*i*j*o - e*j*l*m - e*j*l*o),
f: (-e*i*j*l*q - e*i*j*m*q - e*i*j*o*q - e*j*l*m*q - e*j*l*o*q)/(-d*e*i*l - d*e*i*m - d*e*i*o - d*e*j*l - d*e*j*m - d*e*j*o - d*e*l*m - d*e*l*o - d*g*i*l - d*g*i*m - d*g*i*o - d*g*j*l - d*g*j*m - d*g*j*o - d*g*l*m - d*g*l*o - d*i*j*l - d*i*j*m - d*i*j*o - d*j*l*m - d*j*l*o - e*g*i*l - e*g*i*m - e*g*i*o - e*g*j*l - e*g*j*m - e*g*j*o - e*g*l*m - e*g*l*o - e*i*j*l - e*i*j*m - e*i*j*o - e*j*l*m - e*j*l*o),
h: (-e*j*l*m*q - e*j*l*o*q)/(-d*e*i*l - d*e*i*m - d*e*i*o - d*e*j*l - d*e*j*m - d*e*j*o - d*e*l*m - d*e*l*o - d*g*i*l - d*g*i*m - d*g*i*o - d*g*j*l - d*g*j*m - d*g*j*o - d*g*l*m - d*g*l*o - d*i*j*l - d*i*j*m - d*i*j*o - d*j*l*m - d*j*l*o - e*g*i*l - e*g*i*m - e*g*i*o - e*g*j*l - e*g*j*m - e*g*j*o - e*g*l*m - e*g*l*o - e*i*j*l - e*i*j*m - e*i*j*o - e*j*l*m - e*j*l*o),
k: e*j*l*o*q/(d*e*i*l + d*e*i*m + d*e*i*o + d*e*j*l + d*e*j*m + d*e*j*o + d*e*l*m + d*e*l*o + d*g*i*l + d*g*i*m + d*g*i*o + d*g*j*l + d*g*j*m + d*g*j*o + d*g*l*m + d*g*l*o + d*i*j*l + d*i*j*m + d*i*j*o + d*j*l*m + d*j*l*o + e*g*i*l + e*g*i*m + e*g*i*o + e*g*j*l + e*g*j*m + e*g*j*o + e*g*l*m + e*g*l*o + e*i*j*l + e*i*j*m + e*i*j*o + e*j*l*m + e*j*l*o),
n: e*j*l*o*q/(d*e*i*l + d*e*i*m + d*e*i*o + d*e*j*l + d*e*j*m + d*e*j*o + d*e*l*m + d*e*l*o + d*g*i*l + d*g*i*m + d*g*i*o + d*g*j*l + d*g*j*m + d*g*j*o + d*g*l*m + d*g*l*o + d*i*j*l + d*i*j*m + d*i*j*o + d*j*l*m + d*j*l*o + e*g*i*l + e*g*i*m + e*g*i*o + e*g*j*l + e*g*j*m + e*g*j*o + e*g*l*m + e*g*l*o + e*i*j*l + e*i*j*m + e*i*j*o + e*j*l*m + e*j*l*o),
}
assert solve_lin_sys(eqs, domain) == sol
def test_solve_lin_sys_6x6_2():
ground, d,r,e,g,i,j,l,o,m,p,q = field("d,r,e,g,i,j,l,o,m,p,q", ZZ)
domain, c,f,h,k,n,b = ring("c,f,h,k,n,b", ground)
eqs = [b + r/d - c/d, c*(1/d + 1/e + 1/g) - f/g - r/d, f*(1/g + 1/i + 1/j) - c/g - h/i, h*(1/i + 1/l + 1/m) - f/i - k/m, k*(1/m + 1/o + 1/p) - h/m - n/p, n*(1/p + 1/q) - k/p]
sol = {
b: -((l*q*e*o + l*q*g*o + i*m*q*e + i*l*q*e + i*l*p*e + i*j*o*q + j*e*o*q + g*j*o*q + i*e*o*q + g*i*o*q + e*l*o*p + e*l*m*p + e*l*m*o + e*i*o*p + e*i*m*p + e*i*m*o + e*i*l*o + j*e*o*p + j*e*m*q + j*e*m*p + j*e*m*o + j*l*m*q + j*l*m*p + j*l*m*o + i*j*m*p + i*j*m*o + i*j*l*q + i*j*l*o + i*j*m*q + j*l*o*p + j*e*l*o + g*j*o*p + g*j*m*q + g*j*m*p + i*j*l*p + i*j*o*p + j*e*l*q + j*e*l*p + j*l*o*q + g*j*m*o + g*j*l*q + g*j*l*p + g*j*l*o + g*l*o*p + g*l*m*p + g*l*m*o + g*i*m*o + g*i*o*p + g*i*m*q + g*i*m*p + g*i*l*q + g*i*l*p + g*i*l*o + l*m*q*e + l*m*q*g)*r)/(l*q*d*e*o + l*q*d*g*o + l*q*e*g*o + i*j*d*o*q + i*j*e*o*q + j*d*e*o*q + g*j*d*o*q + g*j*e*o*q + g*i*e*o*q + i*d*e*o*q + g*i*d*o*q + g*i*d*o*p + g*i*d*m*q + g*i*d*m*p + g*i*d*m*o + g*i*d*l*q + g*i*d*l*p + g*i*d*l*o + g*e*l*m*p + g*e*l*o*p + g*j*e*l*q + g*e*l*m*o + g*j*e*m*p + g*j*e*m*o + d*e*l*m*p + d*e*l*m*o + i*d*e*m*p + g*j*e*l*p + g*j*e*l*o + d*e*l*o*p + i*j*d*l*o + i*j*e*o*p + i*j*e*m*q + i*j*d*m*q + i*j*d*m*p + i*j*d*m*o + i*j*d*l*q + i*j*d*l*p + i*j*e*m*p + i*j*e*m*o + i*j*e*l*q + i*j*e*l*p + i*j*e*l*o + i*d*e*m*q + i*d*e*m*o + i*d*e*l*q + i*d*e*l*p + j*d*l*o*p + j*d*e*l*o + g*j*d*o*p + g*j*d*m*q + g*j*d*m*p + g*j*d*m*o + g*j*d*l*q + g*j*d*l*p + g*j*d*l*o + g*j*e*o*p + g*j*e*m*q + g*d*l*o*p + g*d*l*m*p + g*d*l*m*o + j*d*e*m*p + i*d*e*o*p + j*e*o*q*l + j*e*o*p*l + j*e*m*q*l + j*d*e*o*p + j*d*e*m*q + i*j*d*o*p + g*i*e*o*p + j*d*e*m*o + j*d*e*l*q + j*d*e*l*p + j*e*m*p*l + j*e*m*o*l + g*i*e*m*q + g*i*e*m*p + g*i*e*m*o + g*i*e*l*q + g*i*e*l*p + g*i*e*l*o + j*d*l*o*q + j*d*l*m*q + j*d*l*m*p + j*d*l*m*o + i*d*e*l*o + l*m*q*d*e + l*m*q*d*g + l*m*q*e*g),
c: (r*e*(l*q*g*o + i*j*o*q + g*j*o*q + g*i*o*q + j*l*m*q + j*l*m*p + j*l*m*o + i*j*m*p + i*j*m*o + i*j*l*q + i*j*l*o + i*j*m*q + j*l*o*p + g*j*o*p + g*j*m*q + g*j*m*p + i*j*l*p + i*j*o*p + j*l*o*q + g*j*m*o + g*j*l*q + g*j*l*p + g*j*l*o + g*l*o*p + g*l*m*p + g*l*m*o + g*i*m*o + g*i*o*p + g*i*m*q + g*i*m*p + g*i*l*q + g*i*l*p + g*i*l*o + l*m*q*g))/(l*q*d*e*o + l*q*d*g*o + l*q*e*g*o + i*j*d*o*q + i*j*e*o*q + j*d*e*o*q + g*j*d*o*q + g*j*e*o*q + g*i*e*o*q + i*d*e*o*q + g*i*d*o*q + g*i*d*o*p + g*i*d*m*q + g*i*d*m*p + g*i*d*m*o + g*i*d*l*q + g*i*d*l*p + g*i*d*l*o + g*e*l*m*p + g*e*l*o*p + g*j*e*l*q + g*e*l*m*o + g*j*e*m*p + g*j*e*m*o + d*e*l*m*p + d*e*l*m*o + i*d*e*m*p + g*j*e*l*p + g*j*e*l*o + d*e*l*o*p + i*j*d*l*o + i*j*e*o*p + i*j*e*m*q + i*j*d*m*q + i*j*d*m*p + i*j*d*m*o + i*j*d*l*q + i*j*d*l*p + i*j*e*m*p + i*j*e*m*o + i*j*e*l*q + i*j*e*l*p + i*j*e*l*o + i*d*e*m*q + i*d*e*m*o + i*d*e*l*q + i*d*e*l*p + j*d*l*o*p + j*d*e*l*o + g*j*d*o*p + g*j*d*m*q + g*j*d*m*p + g*j*d*m*o + g*j*d*l*q + g*j*d*l*p + g*j*d*l*o + g*j*e*o*p + g*j*e*m*q + g*d*l*o*p + g*d*l*m*p + g*d*l*m*o + j*d*e*m*p + i*d*e*o*p + j*e*o*q*l + j*e*o*p*l + j*e*m*q*l + j*d*e*o*p + j*d*e*m*q + i*j*d*o*p + g*i*e*o*p + j*d*e*m*o + j*d*e*l*q + j*d*e*l*p + j*e*m*p*l + j*e*m*o*l + g*i*e*m*q + g*i*e*m*p + g*i*e*m*o + g*i*e*l*q + g*i*e*l*p + g*i*e*l*o + j*d*l*o*q + j*d*l*m*q + j*d*l*m*p + j*d*l*m*o + i*d*e*l*o + l*m*q*d*e + l*m*q*d*g + l*m*q*e*g),
f: (r*e*j*(l*q*o + l*o*p + l*m*q + l*m*p + l*m*o + i*o*q + i*o*p + i*m*q + i*m*p + i*m*o + i*l*q + i*l*p + i*l*o))/(l*q*d*e*o + l*q*d*g*o + l*q*e*g*o + i*j*d*o*q + i*j*e*o*q + j*d*e*o*q + g*j*d*o*q + g*j*e*o*q + g*i*e*o*q + i*d*e*o*q + g*i*d*o*q + g*i*d*o*p + g*i*d*m*q + g*i*d*m*p + g*i*d*m*o + g*i*d*l*q + g*i*d*l*p + g*i*d*l*o + g*e*l*m*p + g*e*l*o*p + g*j*e*l*q + g*e*l*m*o + g*j*e*m*p + g*j*e*m*o + d*e*l*m*p + d*e*l*m*o + i*d*e*m*p + g*j*e*l*p + g*j*e*l*o + d*e*l*o*p + i*j*d*l*o + i*j*e*o*p + i*j*e*m*q + i*j*d*m*q + i*j*d*m*p + i*j*d*m*o + i*j*d*l*q + i*j*d*l*p + i*j*e*m*p + i*j*e*m*o + i*j*e*l*q + i*j*e*l*p + i*j*e*l*o + i*d*e*m*q + i*d*e*m*o + i*d*e*l*q + i*d*e*l*p + j*d*l*o*p + j*d*e*l*o + g*j*d*o*p + g*j*d*m*q + g*j*d*m*p + g*j*d*m*o + g*j*d*l*q + g*j*d*l*p + g*j*d*l*o + g*j*e*o*p + g*j*e*m*q + g*d*l*o*p + g*d*l*m*p + g*d*l*m*o + j*d*e*m*p + i*d*e*o*p + j*e*o*q*l + j*e*o*p*l + j*e*m*q*l + j*d*e*o*p + j*d*e*m*q + i*j*d*o*p + g*i*e*o*p + j*d*e*m*o + j*d*e*l*q + j*d*e*l*p + j*e*m*p*l + j*e*m*o*l + g*i*e*m*q + g*i*e*m*p + g*i*e*m*o + g*i*e*l*q + g*i*e*l*p + g*i*e*l*o + j*d*l*o*q + j*d*l*m*q + j*d*l*m*p + j*d*l*m*o + i*d*e*l*o + l*m*q*d*e + l*m*q*d*g + l*m*q*e*g),
h: (j*e*r*l*(o*q + o*p + m*q + m*p + m*o))/(l*q*d*e*o + l*q*d*g*o + l*q*e*g*o + i*j*d*o*q + i*j*e*o*q + j*d*e*o*q + g*j*d*o*q + g*j*e*o*q + g*i*e*o*q + i*d*e*o*q + g*i*d*o*q + g*i*d*o*p + g*i*d*m*q + g*i*d*m*p + g*i*d*m*o + g*i*d*l*q + g*i*d*l*p + g*i*d*l*o + g*e*l*m*p + g*e*l*o*p + g*j*e*l*q + g*e*l*m*o + g*j*e*m*p + g*j*e*m*o + d*e*l*m*p + d*e*l*m*o + i*d*e*m*p + g*j*e*l*p + g*j*e*l*o + d*e*l*o*p + i*j*d*l*o + i*j*e*o*p + i*j*e*m*q + i*j*d*m*q + i*j*d*m*p + i*j*d*m*o + i*j*d*l*q + i*j*d*l*p + i*j*e*m*p + i*j*e*m*o + i*j*e*l*q + i*j*e*l*p + i*j*e*l*o + i*d*e*m*q + i*d*e*m*o + i*d*e*l*q + i*d*e*l*p + j*d*l*o*p + j*d*e*l*o + g*j*d*o*p + g*j*d*m*q + g*j*d*m*p + g*j*d*m*o + g*j*d*l*q + g*j*d*l*p + g*j*d*l*o + g*j*e*o*p + g*j*e*m*q + g*d*l*o*p + g*d*l*m*p + g*d*l*m*o + j*d*e*m*p + i*d*e*o*p + j*e*o*q*l + j*e*o*p*l + j*e*m*q*l + j*d*e*o*p + j*d*e*m*q + i*j*d*o*p + g*i*e*o*p + j*d*e*m*o + j*d*e*l*q + j*d*e*l*p + j*e*m*p*l + j*e*m*o*l + g*i*e*m*q + g*i*e*m*p + g*i*e*m*o + g*i*e*l*q + g*i*e*l*p + g*i*e*l*o + j*d*l*o*q + j*d*l*m*q + j*d*l*m*p + j*d*l*m*o + i*d*e*l*o + l*m*q*d*e + l*m*q*d*g + l*m*q*e*g),
k: (j*e*r*o*l*(q + p))/(l*q*d*e*o + l*q*d*g*o + l*q*e*g*o + i*j*d*o*q + i*j*e*o*q + j*d*e*o*q + g*j*d*o*q + g*j*e*o*q + g*i*e*o*q + i*d*e*o*q + g*i*d*o*q + g*i*d*o*p + g*i*d*m*q + g*i*d*m*p + g*i*d*m*o + g*i*d*l*q + g*i*d*l*p + g*i*d*l*o + g*e*l*m*p + g*e*l*o*p + g*j*e*l*q + g*e*l*m*o + g*j*e*m*p + g*j*e*m*o + d*e*l*m*p + d*e*l*m*o + i*d*e*m*p + g*j*e*l*p + g*j*e*l*o + d*e*l*o*p + i*j*d*l*o + i*j*e*o*p + i*j*e*m*q + i*j*d*m*q + i*j*d*m*p + i*j*d*m*o + i*j*d*l*q + i*j*d*l*p + i*j*e*m*p + i*j*e*m*o + i*j*e*l*q + i*j*e*l*p + i*j*e*l*o + i*d*e*m*q + i*d*e*m*o + i*d*e*l*q + i*d*e*l*p + j*d*l*o*p + j*d*e*l*o + g*j*d*o*p + g*j*d*m*q + g*j*d*m*p + g*j*d*m*o + g*j*d*l*q + g*j*d*l*p + g*j*d*l*o + g*j*e*o*p + g*j*e*m*q + g*d*l*o*p + g*d*l*m*p + g*d*l*m*o + j*d*e*m*p + i*d*e*o*p + j*e*o*q*l + j*e*o*p*l + j*e*m*q*l + j*d*e*o*p + j*d*e*m*q + i*j*d*o*p + g*i*e*o*p + j*d*e*m*o + j*d*e*l*q + j*d*e*l*p + j*e*m*p*l + j*e*m*o*l + g*i*e*m*q + g*i*e*m*p + g*i*e*m*o + g*i*e*l*q + g*i*e*l*p + g*i*e*l*o + j*d*l*o*q + j*d*l*m*q + j*d*l*m*p + j*d*l*m*o + i*d*e*l*o + l*m*q*d*e + l*m*q*d*g + l*m*q*e*g),
n: (j*e*r*o*q*l)/(l*q*d*e*o + l*q*d*g*o + l*q*e*g*o + i*j*d*o*q + i*j*e*o*q + j*d*e*o*q + g*j*d*o*q + g*j*e*o*q + g*i*e*o*q + i*d*e*o*q + g*i*d*o*q + g*i*d*o*p + g*i*d*m*q + g*i*d*m*p + g*i*d*m*o + g*i*d*l*q + g*i*d*l*p + g*i*d*l*o + g*e*l*m*p + g*e*l*o*p + g*j*e*l*q + g*e*l*m*o + g*j*e*m*p + g*j*e*m*o + d*e*l*m*p + d*e*l*m*o + i*d*e*m*p + g*j*e*l*p + g*j*e*l*o + d*e*l*o*p + i*j*d*l*o + i*j*e*o*p + i*j*e*m*q + i*j*d*m*q + i*j*d*m*p + i*j*d*m*o + i*j*d*l*q + i*j*d*l*p + i*j*e*m*p + i*j*e*m*o + i*j*e*l*q + i*j*e*l*p + i*j*e*l*o + i*d*e*m*q + i*d*e*m*o + i*d*e*l*q + i*d*e*l*p + j*d*l*o*p + j*d*e*l*o + g*j*d*o*p + g*j*d*m*q + g*j*d*m*p + g*j*d*m*o + g*j*d*l*q + g*j*d*l*p + g*j*d*l*o + g*j*e*o*p + g*j*e*m*q + g*d*l*o*p + g*d*l*m*p + g*d*l*m*o + j*d*e*m*p + i*d*e*o*p + j*e*o*q*l + j*e*o*p*l + j*e*m*q*l + j*d*e*o*p + j*d*e*m*q + i*j*d*o*p + g*i*e*o*p + j*d*e*m*o + j*d*e*l*q + j*d*e*l*p + j*e*m*p*l + j*e*m*o*l + g*i*e*m*q + g*i*e*m*p + g*i*e*m*o + g*i*e*l*q + g*i*e*l*p + g*i*e*l*o + j*d*l*o*q + j*d*l*m*q + j*d*l*m*p + j*d*l*m*o + i*d*e*l*o + l*m*q*d*e + l*m*q*d*g + l*m*q*e*g),
}
assert solve_lin_sys(eqs, domain) == sol
def test_eqs_to_matrix():
domain, x1,x2 = ring("x1,x2", QQ)
eqs_coeff = [{x1: QQ(1), x2: QQ(1)}, {x1: QQ(2), x2: QQ(-1)}]
eqs_rhs = [QQ(-5), QQ(0)]
M = eqs_to_matrix(eqs_coeff, eqs_rhs, [x1, x2], QQ)
assert M.to_Matrix() == Matrix([[1, 1, 5], [2, -1, 0]])

View File

@ -0,0 +1,152 @@
"""Tests for functions for generating interesting polynomials. """
from sympy.core.add import Add
from sympy.core.symbol import symbols
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.ntheory.generate import prime
from sympy.polys.domains.integerring import ZZ
from sympy.polys.polytools import Poly
from sympy.utilities.iterables import permute_signs
from sympy.testing.pytest import raises
from sympy.polys.specialpolys import (
swinnerton_dyer_poly,
cyclotomic_poly,
symmetric_poly,
random_poly,
interpolating_poly,
fateman_poly_F_1,
dmp_fateman_poly_F_1,
fateman_poly_F_2,
dmp_fateman_poly_F_2,
fateman_poly_F_3,
dmp_fateman_poly_F_3,
)
from sympy.abc import x, y, z
def test_swinnerton_dyer_poly():
raises(ValueError, lambda: swinnerton_dyer_poly(0, x))
assert swinnerton_dyer_poly(1, x, polys=True) == Poly(x**2 - 2)
assert swinnerton_dyer_poly(1, x) == x**2 - 2
assert swinnerton_dyer_poly(2, x) == x**4 - 10*x**2 + 1
assert swinnerton_dyer_poly(
3, x) == x**8 - 40*x**6 + 352*x**4 - 960*x**2 + 576
# we only need to check that the polys arg works but
# we may as well test that the roots are correct
p = [sqrt(prime(i)) for i in range(1, 5)]
assert str([i.n(3) for i in
swinnerton_dyer_poly(4, polys=True).all_roots()]
) == str(sorted([Add(*i).n(3) for i in permute_signs(p)]))
def test_cyclotomic_poly():
raises(ValueError, lambda: cyclotomic_poly(0, x))
assert cyclotomic_poly(1, x, polys=True) == Poly(x - 1)
assert cyclotomic_poly(1, x) == x - 1
assert cyclotomic_poly(2, x) == x + 1
assert cyclotomic_poly(3, x) == x**2 + x + 1
assert cyclotomic_poly(4, x) == x**2 + 1
assert cyclotomic_poly(5, x) == x**4 + x**3 + x**2 + x + 1
assert cyclotomic_poly(6, x) == x**2 - x + 1
def test_symmetric_poly():
raises(ValueError, lambda: symmetric_poly(-1, x, y, z))
raises(ValueError, lambda: symmetric_poly(5, x, y, z))
assert symmetric_poly(1, x, y, z, polys=True) == Poly(x + y + z)
assert symmetric_poly(1, (x, y, z), polys=True) == Poly(x + y + z)
assert symmetric_poly(0, x, y, z) == 1
assert symmetric_poly(1, x, y, z) == x + y + z
assert symmetric_poly(2, x, y, z) == x*y + x*z + y*z
assert symmetric_poly(3, x, y, z) == x*y*z
def test_random_poly():
poly = random_poly(x, 10, -100, 100, polys=False)
assert Poly(poly).degree() == 10
assert all(-100 <= coeff <= 100 for coeff in Poly(poly).coeffs()) is True
poly = random_poly(x, 10, -100, 100, polys=True)
assert poly.degree() == 10
assert all(-100 <= coeff <= 100 for coeff in poly.coeffs()) is True
def test_interpolating_poly():
x0, x1, x2, x3, y0, y1, y2, y3 = symbols('x:4, y:4')
assert interpolating_poly(0, x) == 0
assert interpolating_poly(1, x) == y0
assert interpolating_poly(2, x) == \
y0*(x - x1)/(x0 - x1) + y1*(x - x0)/(x1 - x0)
assert interpolating_poly(3, x) == \
y0*(x - x1)*(x - x2)/((x0 - x1)*(x0 - x2)) + \
y1*(x - x0)*(x - x2)/((x1 - x0)*(x1 - x2)) + \
y2*(x - x0)*(x - x1)/((x2 - x0)*(x2 - x1))
assert interpolating_poly(4, x) == \
y0*(x - x1)*(x - x2)*(x - x3)/((x0 - x1)*(x0 - x2)*(x0 - x3)) + \
y1*(x - x0)*(x - x2)*(x - x3)/((x1 - x0)*(x1 - x2)*(x1 - x3)) + \
y2*(x - x0)*(x - x1)*(x - x3)/((x2 - x0)*(x2 - x1)*(x2 - x3)) + \
y3*(x - x0)*(x - x1)*(x - x2)/((x3 - x0)*(x3 - x1)*(x3 - x2))
raises(ValueError, lambda:
interpolating_poly(2, x, (x, 2), (1, 3)))
raises(ValueError, lambda:
interpolating_poly(2, x, (x + y, 2), (1, 3)))
raises(ValueError, lambda:
interpolating_poly(2, x + y, (x, 2), (1, 3)))
raises(ValueError, lambda:
interpolating_poly(2, 3, (4, 5), (6, 7)))
raises(ValueError, lambda:
interpolating_poly(2, 3, (4, 5), (6, 7, 8)))
assert interpolating_poly(0, x, (1, 2), (3, 4)) == 0
assert interpolating_poly(1, x, (1, 2), (3, 4)) == 3
assert interpolating_poly(2, x, (1, 2), (3, 4)) == x + 2
def test_fateman_poly_F_1():
f, g, h = fateman_poly_F_1(1)
F, G, H = dmp_fateman_poly_F_1(1, ZZ)
assert [ t.rep.to_list() for t in [f, g, h] ] == [F, G, H]
f, g, h = fateman_poly_F_1(3)
F, G, H = dmp_fateman_poly_F_1(3, ZZ)
assert [ t.rep.to_list() for t in [f, g, h] ] == [F, G, H]
def test_fateman_poly_F_2():
f, g, h = fateman_poly_F_2(1)
F, G, H = dmp_fateman_poly_F_2(1, ZZ)
assert [ t.rep.to_list() for t in [f, g, h] ] == [F, G, H]
f, g, h = fateman_poly_F_2(3)
F, G, H = dmp_fateman_poly_F_2(3, ZZ)
assert [ t.rep.to_list() for t in [f, g, h] ] == [F, G, H]
def test_fateman_poly_F_3():
f, g, h = fateman_poly_F_3(1)
F, G, H = dmp_fateman_poly_F_3(1, ZZ)
assert [ t.rep.to_list() for t in [f, g, h] ] == [F, G, H]
f, g, h = fateman_poly_F_3(3)
F, G, H = dmp_fateman_poly_F_3(3, ZZ)
assert [ t.rep.to_list() for t in [f, g, h] ] == [F, G, H]

View File

@ -0,0 +1,160 @@
"""Tests for square-free decomposition algorithms and related tools. """
from sympy.polys.rings import ring
from sympy.polys.domains import FF, ZZ, QQ
from sympy.polys.specialpolys import f_polys
from sympy.testing.pytest import raises
from sympy.external.gmpy import MPQ
f_0, f_1, f_2, f_3, f_4, f_5, f_6 = f_polys()
def test_dup_sqf():
R, x = ring("x", ZZ)
assert R.dup_sqf_part(0) == 0
assert R.dup_sqf_p(0) is True
assert R.dup_sqf_part(7) == 1
assert R.dup_sqf_p(7) is True
assert R.dup_sqf_part(2*x + 2) == x + 1
assert R.dup_sqf_p(2*x + 2) is True
assert R.dup_sqf_part(x**3 + x + 1) == x**3 + x + 1
assert R.dup_sqf_p(x**3 + x + 1) is True
assert R.dup_sqf_part(-x**3 + x + 1) == x**3 - x - 1
assert R.dup_sqf_p(-x**3 + x + 1) is True
assert R.dup_sqf_part(2*x**3 + 3*x**2) == 2*x**2 + 3*x
assert R.dup_sqf_p(2*x**3 + 3*x**2) is False
assert R.dup_sqf_part(-2*x**3 + 3*x**2) == 2*x**2 - 3*x
assert R.dup_sqf_p(-2*x**3 + 3*x**2) is False
assert R.dup_sqf_list(0) == (0, [])
assert R.dup_sqf_list(1) == (1, [])
assert R.dup_sqf_list(x) == (1, [(x, 1)])
assert R.dup_sqf_list(2*x**2) == (2, [(x, 2)])
assert R.dup_sqf_list(3*x**3) == (3, [(x, 3)])
assert R.dup_sqf_list(-x**5 + x**4 + x - 1) == \
(-1, [(x**3 + x**2 + x + 1, 1), (x - 1, 2)])
assert R.dup_sqf_list(x**8 + 6*x**6 + 12*x**4 + 8*x**2) == \
( 1, [(x, 2), (x**2 + 2, 3)])
assert R.dup_sqf_list(2*x**2 + 4*x + 2) == (2, [(x + 1, 2)])
R, x = ring("x", QQ)
assert R.dup_sqf_list(2*x**2 + 4*x + 2) == (2, [(x + 1, 2)])
R, x = ring("x", FF(2))
assert R.dup_sqf_list(x**2 + 1) == (1, [(x + 1, 2)])
R, x = ring("x", FF(3))
assert R.dup_sqf_list(x**10 + 2*x**7 + 2*x**4 + x) == \
(1, [(x, 1),
(x + 1, 3),
(x + 2, 6)])
R1, x = ring("x", ZZ)
R2, y = ring("y", FF(3))
f = x**3 + 1
g = y**3 + 1
assert R1.dup_sqf_part(f) == f
assert R2.dup_sqf_part(g) == y + 1
assert R1.dup_sqf_p(f) is True
assert R2.dup_sqf_p(g) is False
R, x, y = ring("x,y", ZZ)
A = x**4 - 3*x**2 + 6
D = x**6 - 5*x**4 + 5*x**2 + 4
f, g = D, R.dmp_sub(A, R.dmp_mul(R.dmp_diff(D, 1), y))
res = R.dmp_resultant(f, g)
h = (4*y**2 + 1).drop(x)
assert R.drop(x).dup_sqf_list(res) == (45796, [(h, 3)])
Rt, t = ring("t", ZZ)
R, x = ring("x", Rt)
assert R.dup_sqf_list_include(t**3*x**2) == [(t**3, 1), (x, 2)]
def test_dmp_sqf():
R, x, y = ring("x,y", ZZ)
assert R.dmp_sqf_part(0) == 0
assert R.dmp_sqf_p(0) is True
assert R.dmp_sqf_part(7) == 1
assert R.dmp_sqf_p(7) is True
assert R.dmp_sqf_list(3) == (3, [])
assert R.dmp_sqf_list_include(3) == [(3, 1)]
R, x, y, z = ring("x,y,z", ZZ)
assert R.dmp_sqf_p(f_0) is True
assert R.dmp_sqf_p(f_0**2) is False
assert R.dmp_sqf_p(f_1) is True
assert R.dmp_sqf_p(f_1**2) is False
assert R.dmp_sqf_p(f_2) is True
assert R.dmp_sqf_p(f_2**2) is False
assert R.dmp_sqf_p(f_3) is True
assert R.dmp_sqf_p(f_3**2) is False
assert R.dmp_sqf_p(f_5) is False
assert R.dmp_sqf_p(f_5**2) is False
assert R.dmp_sqf_p(f_4) is True
assert R.dmp_sqf_part(f_4) == -f_4
assert R.dmp_sqf_part(f_5) == x + y - z
R, x, y, z, t = ring("x,y,z,t", ZZ)
assert R.dmp_sqf_p(f_6) is True
assert R.dmp_sqf_part(f_6) == f_6
R, x = ring("x", ZZ)
f = -x**5 + x**4 + x - 1
assert R.dmp_sqf_list(f) == (-1, [(x**3 + x**2 + x + 1, 1), (x - 1, 2)])
assert R.dmp_sqf_list_include(f) == [(-x**3 - x**2 - x - 1, 1), (x - 1, 2)]
R, x, y = ring("x,y", ZZ)
f = -x**5 + x**4 + x - 1
assert R.dmp_sqf_list(f) == (-1, [(x**3 + x**2 + x + 1, 1), (x - 1, 2)])
assert R.dmp_sqf_list_include(f) == [(-x**3 - x**2 - x - 1, 1), (x - 1, 2)]
f = -x**2 + 2*x - 1
assert R.dmp_sqf_list_include(f) == [(-1, 1), (x - 1, 2)]
f = (y**2 + 1)**2*(x**2 + 2*x + 2)
assert R.dmp_sqf_p(f) is False
assert R.dmp_sqf_list(f) == (1, [(x**2 + 2*x + 2, 1), (y**2 + 1, 2)])
R, x, y = ring("x,y", FF(2))
raises(NotImplementedError, lambda: R.dmp_sqf_list(y**2 + 1))
def test_dup_gff_list():
R, x = ring("x", ZZ)
f = x**5 + 2*x**4 - x**3 - 2*x**2
assert R.dup_gff_list(f) == [(x, 1), (x + 2, 4)]
g = x**9 - 20*x**8 + 166*x**7 - 744*x**6 + 1965*x**5 - 3132*x**4 + 2948*x**3 - 1504*x**2 + 320*x
assert R.dup_gff_list(g) == [(x**2 - 5*x + 4, 1), (x**2 - 5*x + 4, 2), (x, 3)]
raises(ValueError, lambda: R.dup_gff_list(0))
def test_issue_26178():
R, x, y, z = ring(['x', 'y', 'z'], QQ)
assert (x**2 - 2*y**2 + 1).sqf_list() == (MPQ(1,1), [(x**2 - 2*y**2 + 1, 1)])
assert (x**2 - 2*z**2 + 1).sqf_list() == (MPQ(1,1), [(x**2 - 2*z**2 + 1, 1)])
assert (y**2 - 2*z**2 + 1).sqf_list() == (MPQ(1,1), [(y**2 - 2*z**2 + 1, 1)])

View File

@ -0,0 +1,347 @@
from sympy.core.symbol import var
from sympy.polys.polytools import (pquo, prem, sturm, subresultants)
from sympy.matrices import Matrix
from sympy.polys.subresultants_qq_zz import (sylvester, res, res_q, res_z, bezout,
subresultants_sylv, modified_subresultants_sylv,
subresultants_bezout, modified_subresultants_bezout,
backward_eye,
sturm_pg, sturm_q, sturm_amv, euclid_pg, euclid_q,
euclid_amv, modified_subresultants_pg, subresultants_pg,
subresultants_amv_q, quo_z, rem_z, subresultants_amv,
modified_subresultants_amv, subresultants_rem,
subresultants_vv, subresultants_vv_2)
def test_sylvester():
x = var('x')
assert sylvester(x**3 -7, 0, x) == sylvester(x**3 -7, 0, x, 1) == Matrix([[0]])
assert sylvester(0, x**3 -7, x) == sylvester(0, x**3 -7, x, 1) == Matrix([[0]])
assert sylvester(x**3 -7, 0, x, 2) == Matrix([[0]])
assert sylvester(0, x**3 -7, x, 2) == Matrix([[0]])
assert sylvester(x**3 -7, 7, x).det() == sylvester(x**3 -7, 7, x, 1).det() == 343
assert sylvester(7, x**3 -7, x).det() == sylvester(7, x**3 -7, x, 1).det() == 343
assert sylvester(x**3 -7, 7, x, 2).det() == -343
assert sylvester(7, x**3 -7, x, 2).det() == 343
assert sylvester(3, 7, x).det() == sylvester(3, 7, x, 1).det() == sylvester(3, 7, x, 2).det() == 1
assert sylvester(3, 0, x).det() == sylvester(3, 0, x, 1).det() == sylvester(3, 0, x, 2).det() == 1
assert sylvester(x - 3, x - 8, x) == sylvester(x - 3, x - 8, x, 1) == sylvester(x - 3, x - 8, x, 2) == Matrix([[1, -3], [1, -8]])
assert sylvester(x**3 - 7*x + 7, 3*x**2 - 7, x) == sylvester(x**3 - 7*x + 7, 3*x**2 - 7, x, 1) == Matrix([[1, 0, -7, 7, 0], [0, 1, 0, -7, 7], [3, 0, -7, 0, 0], [0, 3, 0, -7, 0], [0, 0, 3, 0, -7]])
assert sylvester(x**3 - 7*x + 7, 3*x**2 - 7, x, 2) == Matrix([
[1, 0, -7, 7, 0, 0], [0, 3, 0, -7, 0, 0], [0, 1, 0, -7, 7, 0], [0, 0, 3, 0, -7, 0], [0, 0, 1, 0, -7, 7], [0, 0, 0, 3, 0, -7]])
def test_subresultants_sylv():
x = var('x')
p = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
q = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
assert subresultants_sylv(p, q, x) == subresultants(p, q, x)
assert subresultants_sylv(p, q, x)[-1] == res(p, q, x)
assert subresultants_sylv(p, q, x) != euclid_amv(p, q, x)
amv_factors = [1, 1, -1, 1, -1, 1]
assert subresultants_sylv(p, q, x) == [i*j for i, j in zip(amv_factors, modified_subresultants_amv(p, q, x))]
p = x**3 - 7*x + 7
q = 3*x**2 - 7
assert subresultants_sylv(p, q, x) == euclid_amv(p, q, x)
def test_modified_subresultants_sylv():
x = var('x')
p = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
q = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
amv_factors = [1, 1, -1, 1, -1, 1]
assert modified_subresultants_sylv(p, q, x) == [i*j for i, j in zip(amv_factors, subresultants_amv(p, q, x))]
assert modified_subresultants_sylv(p, q, x)[-1] != res_q(p + x**8, q, x)
assert modified_subresultants_sylv(p, q, x) != sturm_amv(p, q, x)
p = x**3 - 7*x + 7
q = 3*x**2 - 7
assert modified_subresultants_sylv(p, q, x) == sturm_amv(p, q, x)
assert modified_subresultants_sylv(-p, q, x) != sturm_amv(-p, q, x)
def test_res():
x = var('x')
assert res(3, 5, x) == 1
def test_res_q():
x = var('x')
assert res_q(3, 5, x) == 1
def test_res_z():
x = var('x')
assert res_z(3, 5, x) == 1
assert res(3, 5, x) == res_q(3, 5, x) == res_z(3, 5, x)
def test_bezout():
x = var('x')
p = -2*x**5+7*x**3+9*x**2-3*x+1
q = -10*x**4+21*x**2+18*x-3
assert bezout(p, q, x, 'bz').det() == sylvester(p, q, x, 2).det()
assert bezout(p, q, x, 'bz').det() != sylvester(p, q, x, 1).det()
assert bezout(p, q, x, 'prs') == backward_eye(5) * bezout(p, q, x, 'bz') * backward_eye(5)
def test_subresultants_bezout():
x = var('x')
p = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
q = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
assert subresultants_bezout(p, q, x) == subresultants(p, q, x)
assert subresultants_bezout(p, q, x)[-1] == sylvester(p, q, x).det()
assert subresultants_bezout(p, q, x) != euclid_amv(p, q, x)
amv_factors = [1, 1, -1, 1, -1, 1]
assert subresultants_bezout(p, q, x) == [i*j for i, j in zip(amv_factors, modified_subresultants_amv(p, q, x))]
p = x**3 - 7*x + 7
q = 3*x**2 - 7
assert subresultants_bezout(p, q, x) == euclid_amv(p, q, x)
def test_modified_subresultants_bezout():
x = var('x')
p = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
q = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
amv_factors = [1, 1, -1, 1, -1, 1]
assert modified_subresultants_bezout(p, q, x) == [i*j for i, j in zip(amv_factors, subresultants_amv(p, q, x))]
assert modified_subresultants_bezout(p, q, x)[-1] != sylvester(p + x**8, q, x).det()
assert modified_subresultants_bezout(p, q, x) != sturm_amv(p, q, x)
p = x**3 - 7*x + 7
q = 3*x**2 - 7
assert modified_subresultants_bezout(p, q, x) == sturm_amv(p, q, x)
assert modified_subresultants_bezout(-p, q, x) != sturm_amv(-p, q, x)
def test_sturm_pg():
x = var('x')
p = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
q = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
assert sturm_pg(p, q, x)[-1] != sylvester(p, q, x, 2).det()
sam_factors = [1, 1, -1, -1, 1, 1]
assert sturm_pg(p, q, x) == [i*j for i,j in zip(sam_factors, euclid_pg(p, q, x))]
p = -9*x**5 - 5*x**3 - 9
q = -45*x**4 - 15*x**2
assert sturm_pg(p, q, x, 1)[-1] == sylvester(p, q, x, 1).det()
assert sturm_pg(p, q, x)[-1] != sylvester(p, q, x, 2).det()
assert sturm_pg(-p, q, x)[-1] == sylvester(-p, q, x, 2).det()
assert sturm_pg(-p, q, x) == modified_subresultants_pg(-p, q, x)
def test_sturm_q():
x = var('x')
p = x**3 - 7*x + 7
q = 3*x**2 - 7
assert sturm_q(p, q, x) == sturm(p)
assert sturm_q(-p, -q, x) != sturm(-p)
def test_sturm_amv():
x = var('x')
p = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
q = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
assert sturm_amv(p, q, x)[-1] != sylvester(p, q, x, 2).det()
sam_factors = [1, 1, -1, -1, 1, 1]
assert sturm_amv(p, q, x) == [i*j for i,j in zip(sam_factors, euclid_amv(p, q, x))]
p = -9*x**5 - 5*x**3 - 9
q = -45*x**4 - 15*x**2
assert sturm_amv(p, q, x, 1)[-1] == sylvester(p, q, x, 1).det()
assert sturm_amv(p, q, x)[-1] != sylvester(p, q, x, 2).det()
assert sturm_amv(-p, q, x)[-1] == sylvester(-p, q, x, 2).det()
assert sturm_pg(-p, q, x) == modified_subresultants_pg(-p, q, x)
def test_euclid_pg():
x = var('x')
p = x**6+x**5-x**4-x**3+x**2-x+1
q = 6*x**5+5*x**4-4*x**3-3*x**2+2*x-1
assert euclid_pg(p, q, x)[-1] == sylvester(p, q, x).det()
assert euclid_pg(p, q, x) == subresultants_pg(p, q, x)
p = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
q = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
assert euclid_pg(p, q, x)[-1] != sylvester(p, q, x, 2).det()
sam_factors = [1, 1, -1, -1, 1, 1]
assert euclid_pg(p, q, x) == [i*j for i,j in zip(sam_factors, sturm_pg(p, q, x))]
def test_euclid_q():
x = var('x')
p = x**3 - 7*x + 7
q = 3*x**2 - 7
assert euclid_q(p, q, x)[-1] == -sturm(p)[-1]
def test_euclid_amv():
x = var('x')
p = x**3 - 7*x + 7
q = 3*x**2 - 7
assert euclid_amv(p, q, x)[-1] == sylvester(p, q, x).det()
assert euclid_amv(p, q, x) == subresultants_amv(p, q, x)
p = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
q = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
assert euclid_amv(p, q, x)[-1] != sylvester(p, q, x, 2).det()
sam_factors = [1, 1, -1, -1, 1, 1]
assert euclid_amv(p, q, x) == [i*j for i,j in zip(sam_factors, sturm_amv(p, q, x))]
def test_modified_subresultants_pg():
x = var('x')
p = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
q = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
amv_factors = [1, 1, -1, 1, -1, 1]
assert modified_subresultants_pg(p, q, x) == [i*j for i, j in zip(amv_factors, subresultants_pg(p, q, x))]
assert modified_subresultants_pg(p, q, x)[-1] != sylvester(p + x**8, q, x).det()
assert modified_subresultants_pg(p, q, x) != sturm_pg(p, q, x)
p = x**3 - 7*x + 7
q = 3*x**2 - 7
assert modified_subresultants_pg(p, q, x) == sturm_pg(p, q, x)
assert modified_subresultants_pg(-p, q, x) != sturm_pg(-p, q, x)
def test_subresultants_pg():
x = var('x')
p = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
q = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
assert subresultants_pg(p, q, x) == subresultants(p, q, x)
assert subresultants_pg(p, q, x)[-1] == sylvester(p, q, x).det()
assert subresultants_pg(p, q, x) != euclid_pg(p, q, x)
amv_factors = [1, 1, -1, 1, -1, 1]
assert subresultants_pg(p, q, x) == [i*j for i, j in zip(amv_factors, modified_subresultants_amv(p, q, x))]
p = x**3 - 7*x + 7
q = 3*x**2 - 7
assert subresultants_pg(p, q, x) == euclid_pg(p, q, x)
def test_subresultants_amv_q():
x = var('x')
p = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
q = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
assert subresultants_amv_q(p, q, x) == subresultants(p, q, x)
assert subresultants_amv_q(p, q, x)[-1] == sylvester(p, q, x).det()
assert subresultants_amv_q(p, q, x) != euclid_amv(p, q, x)
amv_factors = [1, 1, -1, 1, -1, 1]
assert subresultants_amv_q(p, q, x) == [i*j for i, j in zip(amv_factors, modified_subresultants_amv(p, q, x))]
p = x**3 - 7*x + 7
q = 3*x**2 - 7
assert subresultants_amv(p, q, x) == euclid_amv(p, q, x)
def test_rem_z():
x = var('x')
p = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
q = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
assert rem_z(p, -q, x) != prem(p, -q, x)
def test_quo_z():
x = var('x')
p = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
q = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
assert quo_z(p, -q, x) != pquo(p, -q, x)
y = var('y')
q = 3*x**6 + 5*y**4 - 4*x**2 - 9*x + 21
assert quo_z(p, -q, x) == pquo(p, -q, x)
def test_subresultants_amv():
x = var('x')
p = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
q = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
assert subresultants_amv(p, q, x) == subresultants(p, q, x)
assert subresultants_amv(p, q, x)[-1] == sylvester(p, q, x).det()
assert subresultants_amv(p, q, x) != euclid_amv(p, q, x)
amv_factors = [1, 1, -1, 1, -1, 1]
assert subresultants_amv(p, q, x) == [i*j for i, j in zip(amv_factors, modified_subresultants_amv(p, q, x))]
p = x**3 - 7*x + 7
q = 3*x**2 - 7
assert subresultants_amv(p, q, x) == euclid_amv(p, q, x)
def test_modified_subresultants_amv():
x = var('x')
p = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
q = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
amv_factors = [1, 1, -1, 1, -1, 1]
assert modified_subresultants_amv(p, q, x) == [i*j for i, j in zip(amv_factors, subresultants_amv(p, q, x))]
assert modified_subresultants_amv(p, q, x)[-1] != sylvester(p + x**8, q, x).det()
assert modified_subresultants_amv(p, q, x) != sturm_amv(p, q, x)
p = x**3 - 7*x + 7
q = 3*x**2 - 7
assert modified_subresultants_amv(p, q, x) == sturm_amv(p, q, x)
assert modified_subresultants_amv(-p, q, x) != sturm_amv(-p, q, x)
def test_subresultants_rem():
x = var('x')
p = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
q = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
assert subresultants_rem(p, q, x) == subresultants(p, q, x)
assert subresultants_rem(p, q, x)[-1] == sylvester(p, q, x).det()
assert subresultants_rem(p, q, x) != euclid_amv(p, q, x)
amv_factors = [1, 1, -1, 1, -1, 1]
assert subresultants_rem(p, q, x) == [i*j for i, j in zip(amv_factors, modified_subresultants_amv(p, q, x))]
p = x**3 - 7*x + 7
q = 3*x**2 - 7
assert subresultants_rem(p, q, x) == euclid_amv(p, q, x)
def test_subresultants_vv():
x = var('x')
p = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
q = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
assert subresultants_vv(p, q, x) == subresultants(p, q, x)
assert subresultants_vv(p, q, x)[-1] == sylvester(p, q, x).det()
assert subresultants_vv(p, q, x) != euclid_amv(p, q, x)
amv_factors = [1, 1, -1, 1, -1, 1]
assert subresultants_vv(p, q, x) == [i*j for i, j in zip(amv_factors, modified_subresultants_amv(p, q, x))]
p = x**3 - 7*x + 7
q = 3*x**2 - 7
assert subresultants_vv(p, q, x) == euclid_amv(p, q, x)
def test_subresultants_vv_2():
x = var('x')
p = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
q = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
assert subresultants_vv_2(p, q, x) == subresultants(p, q, x)
assert subresultants_vv_2(p, q, x)[-1] == sylvester(p, q, x).det()
assert subresultants_vv_2(p, q, x) != euclid_amv(p, q, x)
amv_factors = [1, 1, -1, 1, -1, 1]
assert subresultants_vv_2(p, q, x) == [i*j for i, j in zip(amv_factors, modified_subresultants_amv(p, q, x))]
p = x**3 - 7*x + 7
q = 3*x**2 - 7
assert subresultants_vv_2(p, q, x) == euclid_amv(p, q, x)