def test_PolyElement_subs(): R, x = ring("x", ZZ) f = x**3 + 4*x**2 + 2*x + 3 assert f == f.subs([]) pytest.raises(ValueError, lambda: f.subs(object())) r = f.subs({x: 0}) assert r == 3 and isinstance(r, R.dtype) assert f.subs({(x, 0)}) == r pytest.raises(CoercionFailed, lambda: f.subs({x: QQ(1, 7)})) R, x, y, z = ring("x,y,z", ZZ) f = x**3 + 4*x**2 + 2*x + 3 r = f.subs({x: 0}) assert r == 3 and isinstance(r, R.dtype) r = f.subs({x: 0, y: 0}) assert r == 3 and isinstance(r, R.dtype) pytest.raises(CoercionFailed, lambda: f.subs({x: 1, y: QQ(1, 7)})) pytest.raises(CoercionFailed, lambda: f.subs({x: QQ(1, 7), y: 1})) pytest.raises(CoercionFailed, lambda: f.subs({x: QQ(1, 7), y: QQ(1, 7)})) f = 1 - x - y - z r = f.subs({y: 1}) assert r == -x - z
def test_PolyElement_compose(): R, x = ring("x", ZZ) f = x**3 + 4*x**2 + 2*x + 3 r = f.compose(x, 0) assert r == 3 and isinstance(r, R.dtype) assert f.compose(x, x) == f assert f.compose(x, x**2) == x**6 + 4*x**4 + 2*x**2 + 3 pytest.raises(CoercionFailed, lambda: f.compose(x, QQ(1, 7))) R, x, y, z = ring("x,y,z", ZZ) f = x**3 + 4*x**2 + 2*x + 3 r = f.compose(x, 0) assert r == 3 and isinstance(r, R.dtype) r = f.compose([(x, 0), (y, 0)]) assert r == 3 and isinstance(r, R.dtype) r = f.compose({x: 0, y: 0}) assert r == 3 and isinstance(r, R.dtype) pytest.raises(ValueError, lambda: f.compose("spam")) r = (x**3 + 4*x**2 + 2*x*y*z + 3).compose(x, y*z**2 - 1) q = (y*z**2 - 1)**3 + 4*(y*z**2 - 1)**2 + 2*(y*z**2 - 1)*y*z + 3 assert r == q and isinstance(r, R.dtype)
def test_PolyElement_eval(): R, x = ring("x", ZZ) f = x**3 + 4*x**2 + 2*x + 3 r = f.eval(x, 0) assert r == 3 and not isinstance(r, PolyElement) pytest.raises(CoercionFailed, lambda: f.eval(x, QQ(1, 7))) R, x, y, z = ring("x,y,z", ZZ) f = (x*y)**3 + 4*(x*y)**2 + 2*x*y + 3 r = f.eval(x, 0) assert r == 3 and isinstance(r, R.drop(x).dtype) r = f.eval([(x, 0), (y, 0)]) assert r == 3 and isinstance(r, R.drop(x, y).dtype) r = f.eval(y, 0) assert r == 3 and isinstance(r, R.drop(y).dtype) r = f.eval([(y, 0), (x, 0)]) assert r == 3 and isinstance(r, R.drop(y, x).dtype) r = f.eval([(x, 0), (y, 0), (z, 0)]) assert r == 3 and not isinstance(r, PolyElement) pytest.raises(CoercionFailed, lambda: f.eval([(x, 1), (y, QQ(1, 7))])) pytest.raises(CoercionFailed, lambda: f.eval([(x, QQ(1, 7)), (y, 1)])) pytest.raises(CoercionFailed, lambda: f.eval([(x, QQ(1, 7)), (y, QQ(1, 7))]))
def test_PolyElement_from_expr(): x, y, z = symbols("x,y,z") R, X, Y, Z = ring((x, y, z), ZZ) f = R.from_expr(1) assert f == 1 and isinstance(f, R.dtype) f = R.from_expr(x) assert f == X and isinstance(f, R.dtype) f = R.from_expr(x * y * z) assert f == X * Y * Z and isinstance(f, R.dtype) f = R.from_expr(x * y * z + x * y + x) assert f == X * Y * Z + X * Y + X and isinstance(f, R.dtype) f = R.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, R.dtype) pytest.raises(ValueError, lambda: R.from_expr(1 / x)) pytest.raises(ValueError, lambda: R.from_expr(2**x)) pytest.raises(ValueError, lambda: R.from_expr(7 * x + sqrt(2))) R, X, Y = ring((2**x, y), ZZ) f = R.from_expr(2**(2 * x) + 1) assert f == X**2 + 1
def test_dmp_mul_ground(): R, x = ring('x', ZZ) f = 0 assert R.dmp_mul_ground(f, ZZ(2)) == 0 f = x**2 + 2 * x - 1 assert R.dmp_mul_ground(f, ZZ(3)) == 3 * x**2 + 6 * x - 3 f = x**2 + 2 * x + 3 assert R.dmp_mul_ground(f, ZZ(0)) == 0 assert R.dmp_mul_ground(f, ZZ(2)) == 2 * x**2 + 4 * x + 6 R, x, y, z = ring('x y z', ZZ) f = f_polys()[0] assert (R.dmp_mul_ground(f, ZZ(2)) == 2 * x**2 * y * z**2 + 4 * x**2 * y * z + 6 * x**2 * y + 4 * x**2 + 6 * x + 8 * y**2 * z**2 + 10 * y**2 * z + 12 * y**2 + 2 * y * z**2 + 4 * y * z + 2 * y + 2) R, x, y, z = ring('x y z', QQ) f = f.set_ring(R) / 7 assert (R.dmp_mul_ground(f, QQ( 1, 2)) == x**2 * y * z**2 / 14 + x**2 * y * z / 7 + 3 * x**2 * y / 14 + x**2 / 7 + 3 * x / 14 + 2 * y**2 * z**2 / 7 + 5 * y**2 * z / 14 + 3 * y**2 / 7 + y * z**2 / 14 + y * z / 7 + y / 14 + QQ(1, 14))
def test_PolyElement_from_expr(): x, y, z = symbols("x,y,z") R, X, Y, Z = ring((x, y, z), ZZ) f = R.convert(1) assert f == 1 and isinstance(f, R.dtype) f = R.convert(x) assert f == X and isinstance(f, R.dtype) f = R.convert(x*y*z) assert f == X*Y*Z and isinstance(f, R.dtype) f = R.convert(x*y*z + x*y + x) assert f == X*Y*Z + X*Y + X and isinstance(f, R.dtype) f = R.convert(x**3*y*z + x**2*y**7 + 1) assert f == X**3*Y*Z + X**2*Y**7 + 1 and isinstance(f, R.dtype) pytest.raises(CoercionFailed, lambda: R.convert(1/x)) pytest.raises(CoercionFailed, lambda: R.convert(2**x)) pytest.raises(CoercionFailed, lambda: R.convert(7*x + sqrt(2))) R, X, Y = ring((2**x, y), ZZ) f = R.convert(2**(2*x) + 1) assert f == X**2 + 1
def test_PolyElement___sub__(): Rt, t = ring("t", ZZ) Ruv, u, v = ring("u,v", ZZ) Rxyz, x, y, z = ring("x,y,z", Ruv) assert u - x == -x + u assert (x + u) - 2*u == x - u assert dict(x - 3*y) == {(1, 0, 0): 1, (0, 1, 0): -3} assert dict(-u + x) == dict(x - u) == {(1, 0, 0): 1, (0, 0, 0): -u} assert dict(-u + x*y) == dict(x*y - u) == {(1, 1, 0): 1, (0, 0, 0): -u} assert dict(-u + x*y + z) == dict(x*y + z - u) == {(1, 1, 0): 1, (0, 0, 1): 1, (0, 0, 0): -u} assert dict(-u*x + x) == dict(x - u*x) == {(1, 0, 0): -u + 1} assert dict(-u*x + x*y) == dict(x*y - u*x) == {(1, 1, 0): 1, (1, 0, 0): -u} assert dict(-u*x + x*y + z) == dict(x*y + z - u*x) == {(1, 1, 0): 1, (0, 0, 1): 1, (1, 0, 0): -u} pytest.raises(TypeError, lambda: t - x) pytest.raises(TypeError, lambda: x - t) pytest.raises(TypeError, lambda: t - u) pytest.raises(TypeError, lambda: u - t) Fuv, u, v = field("u,v", ZZ) Rxyz, x, y, z = ring("x,y,z", Fuv) assert dict(-u + x) == dict(x - u) == {(1, 0, 0): 1, (0, 0, 0): -u} Rxyz, x, y, z = ring("x,y,z", EX) assert dict(-EX(pi) + x*y*z) == dict(x*y*z - EX(pi)) == {(1, 1, 1): EX(1), (0, 0, 0): -EX(pi)}
def test_PolyElement___eq__(): R, x, y = ring("x,y", ZZ) assert ((x*y + 5*x*y) == 6) is False assert ((x*y + 5*x*y) == 6*x*y) is True assert (6 == (x*y + 5*x*y)) is False assert (6*x*y == (x*y + 5*x*y)) is True assert ((x*y - x*y) == 0) is True assert (0 == (x*y - x*y)) is True assert ((x*y - x*y) == 1) is False assert (1 == (x*y - x*y)) is False assert ((x*y - x*y) == 1) is False assert (1 == (x*y - x*y)) is False assert ((x*y + 5*x*y) != 6) is True assert ((x*y + 5*x*y) != 6*x*y) is False assert (6 != (x*y + 5*x*y)) is True assert (6*x*y != (x*y + 5*x*y)) is False assert ((x*y - x*y) != 0) is False assert (0 != (x*y - x*y)) is False assert ((x*y - x*y) != 1) is True assert (1 != (x*y - x*y)) is True Rt, t = ring("t", ZZ) R, x, y = ring("x,y", Rt) assert (t**3*x//x == t**3) is True assert (t**3*x//x == t**4) is False
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 # First example from Monagan2004algebraic. m = z**2 - t f = 3 * t * x**2 - (2 * t**2 - 3 * t) * x * z + 15 * x + 15 * z - 2 * t**3 g = 3 * t * x**2 * z + 15 * x * z + (-2 * t**3 + 3 * t) * x - 2 * t**2 * z + 15 assert _func_field_modgcd_m(f, g, m.drop(0)) == 3 * t * x - 2 * t**2 * z + 15 g = 3 * t * x - 2 * t**2 * z + 15 a = x + z b = x * z + 1 assert _func_field_modgcd_m(a * g, b * g, m.drop(0)) == g % m assert _func_field_modgcd_m(a * g**2, b * g**2, m.drop(0)) == g**2 % m # issue diofant/diofant#850 assert _func_field_modgcd_m(a * g**3, b * g**3, m.drop(0)) == g**3 % m
def test_PolyElement_cancel(): R, x, y = ring("x,y", ZZ) f = 2*x**3 + 4*x**2 + 2*x g = 3*x**2 + 3*x F = 2*x + 2 G = 3 assert f.cancel(g) == (F, G) assert (-f).cancel(g) == (-F, G) assert f.cancel(-g) == (-F, G) R, x, y = ring("x,y", QQ) f = x**3/2 + x**2 + x/2 g = x**2/3 + x/3 F = 3*x + 3 G = 2 assert f.cancel(g) == (F, G) assert (-f).cancel(g) == (-F, G) assert f.cancel(-g) == (-F, G) Fx, x = field("x", ZZ) Rt, t = ring("t", Fx) f = (-x**2 - 4)/4*t g = t**2 + (x**2 + 2)/2 assert f.cancel(g) == ((-x**2 - 4)*t, 4*t**2 + 2*x**2 + 4)
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 R, x = ring("x", QQ.algebraic_field(I)) f = x**3 + I*x + 2 assert R.dup_count_real_roots(f) == 0 f *= x**2 - 1 assert R.dup_count_real_roots(f) == 2
def test_PolyElement_compose(): R, x = ring("x", ZZ) f = x**3 + 4*x**2 + 2*x + 3 r = f.compose(x, 0) assert r == 3 and isinstance(r, R.dtype) assert f.compose(x, x) == f assert f.compose(x, x**2) == x**6 + 4*x**4 + 2*x**2 + 3 pytest.raises(CoercionFailed, lambda: f.compose(x, QQ(1, 7))) R, x, y, z = ring("x,y,z", ZZ) f = x**3 + 4*x**2 + 2*x + 3 r = f.compose(x, 0) assert r == 3 and isinstance(r, R.dtype) r = f.compose([(x, 0), (y, 0)]) assert r == 3 and isinstance(r, R.dtype) r = f.compose({x: 0, y: 0}) assert r == 3 and isinstance(r, R.dtype) pytest.raises(ValueError, lambda: f.compose("spam")) r = (x**3 + 4*x**2 + 2*x*y*z + 3).compose(x, y*z**2 - 1) q = (y*z**2 - 1)**3 + 4*(y*z**2 - 1)**2 + 2*(y*z**2 - 1)*y*z + 3 assert r == q and isinstance(r, R.dtype)
def test_groebner_lcm(): R, x, y, z = ring("x,y,z", ZZ) assert groebner_lcm(x**2 - y**2, R.zero) == 0 assert groebner_lcm(R.zero, x - y) == 0 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 Rz, X, Y, Z = ring("x,y,z", QQ) pytest.raises(ValueError, lambda: groebner_lcm(x, X))
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**2 + 1, irreducible=True) 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(x**2 / 2 + x + 1) is False R, x = ring("x", ZZ.poly_ring("y")) assert R.dup_cyclotomic_p(x**2 + x + 1) is False
def test_dup_count_complex_roots_1(): R, x = ring("x", ZZ) f = x - 1 assert R.dup_count_complex_roots(f, a, b) == 1 assert R.dup_count_complex_roots(f, c, d) == 1 f = -f assert R.dup_count_complex_roots(f, a, b) == 1 assert R.dup_count_complex_roots(f, c, d) == 1 f = x + 1 assert R.dup_count_complex_roots(f, a, b) == 1 assert R.dup_count_complex_roots(f, c, d) == 0 R, x = ring("x", QQ) f = x - QQ(1, 2) assert R.dup_count_complex_roots(f, c, d) == 1 R, x = ring("x", EX) pytest.raises(DomainError, lambda: R.dup_count_complex_roots(x))
def test_PolyElement___add__(): Rt, t = ring("t", ZZ) Ruv, u, v = ring("u,v", ZZ) Rxyz, x, y, z = ring("x,y,z", Ruv) assert dict(x + 3*y) == {(1, 0, 0): 1, (0, 1, 0): 3} assert dict(u + x) == dict(x + u) == {(1, 0, 0): 1, (0, 0, 0): u} assert dict(u + x*y) == dict(x*y + u) == {(1, 1, 0): 1, (0, 0, 0): u} assert dict(u + x*y + z) == dict(x*y + z + u) == {(1, 1, 0): 1, (0, 0, 1): 1, (0, 0, 0): u} assert dict(u*x + x) == dict(x + u*x) == {(1, 0, 0): u + 1} assert dict(u*x + x*y) == dict(x*y + u*x) == {(1, 1, 0): 1, (1, 0, 0): u} assert dict(u*x + x*y + z) == dict(x*y + z + u*x) == {(1, 1, 0): 1, (0, 0, 1): 1, (1, 0, 0): u} pytest.raises(TypeError, lambda: t + x) pytest.raises(TypeError, lambda: x + t) pytest.raises(TypeError, lambda: t + u) pytest.raises(TypeError, lambda: u + t) Fuv, u, v = field("u,v", ZZ) Rxyz, x, y, z = ring("x,y,z", Fuv) assert dict(u + x) == dict(x + u) == {(1, 0, 0): 1, (0, 0, 0): u} Rxyz, x, y, z = ring("x,y,z", EX) assert dict(EX(pi) + x*y*z) == dict(x*y*z + EX(pi)) == {(1, 1, 1): EX(1), (0, 0, 0): EX(pi)}
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_dmp_ext_factor(): R, x, y = ring("x,y", QQ.algebraic_field(sqrt(2))) assert R.dmp_ext_factor(0) == (R.domain(0), []) f = x + 1 assert R.dmp_ext_factor(f) == (R.domain(1), [(f, 1)]) g = 2 * x + 2 assert R.dmp_ext_factor(g) == (R.domain(2), [(f, 1)]) f = x**2 - 2 * y**2 assert R.dmp_ext_factor(f) == (R.domain(1), [(x - sqrt(2) * y, 1), (x + sqrt(2) * y, 1)]) f = 2 * x**2 - 4 * y**2 assert R.dmp_ext_factor(f) == (R.domain(2), [(x - sqrt(2) * y, 1), (x + sqrt(2) * y, 1)]) # issue sympy/sympy#5786 R, x, y, z, t = ring("x, y, z, t", QQ.algebraic_field(I)) f = -I * t * x - t * y + x * z - I * y * z assert (R.dmp_ext_factor(f) == (R.domain(1), [(z - I * t, 1), (x - I * y, 1)])) R, x = ring("x", QQ.algebraic_field(I)) f = x**2 + 1 assert R.dmp_ext_factor(f) == (R.domain(1), [(x - I, 1), (x + I, 1)])
def test_dup_decompose(): R, x = ring('x', ZZ) assert R.dup_decompose(1) == [1] assert R.dup_decompose(x) == [x] assert R.dup_decompose(x**3) == [x**3] assert R.dup_decompose(x**4) == [x**2, x**2] assert R.dup_decompose(x**6) == [x**3, x**2] assert R.dup_decompose(7*x**4 + 1) == [7*x**2 + 1, x**2] assert R.dup_decompose(4*x**4 + 3*x**2 + 2) == [4*x**2 + 3*x + 2, x**2] f = x**12 + 20*x**10 + 150*x**8 + 500*x**6 + 625*x**4 - 2*x**3 - 10*x + 9 assert R.dup_decompose(f) == [x**4 - 2*x + 9, x**3 + 5*x] f = 2*x**12 + 40*x**10 + 300*x**8 + 1000*x**6 + 1250*x**4 - 4*x**3 - 20*x + 18 assert R.dup_decompose(f) == [2*x**4 - 4*x + 18, x**3 + 5*x] f = (x**12 + 20*x**10 - 8*x**9 + 150*x**8 - 120*x**7 + 524*x**6 - 600*x**5 + 865*x**4 - 1034*x**3 + 600*x**2 - 170*x + 29) assert R.dup_decompose(f) == [x**4 - 8*x**3 + 24*x**2 - 34*x + 29, x**3 + 5*x] Rt, t = ring("t", ZZ) R, x = ring('x', Rt) f = ((6*t**2 - 42)*x**4 + (48*t**2 + 96)*x**3 + (144*t**2 + 648*t + 288)*x**2 + (624*t**2 + 864*t + 384)*x + 108*t**3 + 312*t**2 + 432*t + 192) assert R.dup_decompose(f) == [f]
def test_dmp_ground_extract(): R, x = ring('x', ZZ) f = 2930944*x**6 + 2198208*x**4 + 549552*x**2 + 45796 g = 17585664*x**5 + 8792832*x**3 + 1099104*x F = 64*x**6 + 48*x**4 + 12*x**2 + 1 G = 384*x**5 + 192*x**3 + 24*x assert R.dmp_ground_extract(f, g) == (45796, F, G) f, g = 6*x**2 + 12*x + 18, 4*x**2 + 8*x + 12 F, G = 3*x**2 + 6*x + 9, 2*x**2 + 4*x + 6 assert R.dmp_ground_extract(f, g) == (2, F, G) f, g = x + 2, 3*x + 4 assert R.dmp_ground_extract(f, g) == (1, f, g) R, x, y = ring('x y', ZZ) f = 2930944*x**6 + 2198208*x**4 + 549552*x**2 + 45796 g = 17585664*x**5 + 8792832*x**3 + 1099104*x F = 64*x**6 + 48*x**4 + 12*x**2 + 1 G = 384*x**5 + 192*x**3 + 24*x assert R.dmp_ground_extract(f, g) == (45796, F, G)
def test_dmp_ground_monic(): R, x = ring('x', ZZ) assert R.dmp_ground_monic(3*x**2 + 6*x + 9) == x**2 + 2*x + 3 pytest.raises(ExactQuotientFailed, lambda: R.dmp_ground_monic(3*x**2 + 4*x + 5)) R, x = ring('x', QQ) assert R.dmp_ground_monic(0) == 0 assert R.dmp_ground_monic(1) == 1 assert R.dmp_ground_monic(7*x**2 + x + 21) == x**2 + x/7 + 3 assert R.dmp_ground_monic(3*x**2 + 4*x + 2) == x**2 + 4*x/3 + QQ(2, 3) R, x, y = ring('x y', ZZ) assert R.dmp_ground_monic(3*x**2 + 6*x + 9) == x**2 + 2*x + 3 pytest.raises(ExactQuotientFailed, lambda: R.dmp_ground_monic(3*x**2 + 4*x + 5)) R, x, y = ring('x y', QQ) assert R.dmp_ground_monic(0) == 0 assert R.dmp_ground_monic(1) == 1 assert R.dmp_ground_monic(7*x**2 + x + 21) == x**2 + x/7 + 3
def test_PolyElement___sub__(): Rt, t = ring("t", ZZ) Ruv, u, v = ring("u,v", ZZ) Rxyz, x, y, z = ring("x,y,z", Ruv) assert dict(x - 3*y) == {(1, 0, 0): 1, (0, 1, 0): -3} assert dict(-u + x) == dict(x - u) == {(1, 0, 0): 1, (0, 0, 0): -u} assert dict(-u + x*y) == dict(x*y - u) == {(1, 1, 0): 1, (0, 0, 0): -u} assert dict(-u + x*y + z) == dict(x*y + z - u) == {(1, 1, 0): 1, (0, 0, 1): 1, (0, 0, 0): -u} assert dict(-u*x + x) == dict(x - u*x) == {(1, 0, 0): -u + 1} assert dict(-u*x + x*y) == dict(x*y - u*x) == {(1, 1, 0): 1, (1, 0, 0): -u} assert dict(-u*x + x*y + z) == dict(x*y + z - u*x) == {(1, 1, 0): 1, (0, 0, 1): 1, (1, 0, 0): -u} pytest.raises(TypeError, lambda: t - x) pytest.raises(TypeError, lambda: x - t) pytest.raises(TypeError, lambda: t - u) pytest.raises(TypeError, lambda: u - t) Fuv, u, v = field("u,v", ZZ) Rxyz, x, y, z = ring("x,y,z", Fuv) assert dict(-u + x) == dict(x - u) == {(1, 0, 0): 1, (0, 0, 0): -u} Rxyz, x, y, z = ring("x,y,z", EX) assert dict(-EX(pi) + x*y*z) == dict(x*y*z - EX(pi)) == {(1, 1, 1): EX(1), (0, 0, 0): -EX(pi)}
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_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} Fuv, u, v = field("u,v", ZZ) Rxyz, x, y, z = ring("x,y,z", Fuv) f = u - x assert dict(f) == {(0, 0, 0): u, (1, 0, 0): -Fuv.one}
def test_PolyElement___eq__(): R, x, y = ring("x,y", ZZ, lex) assert ((x * y + 5 * x * y) == 6) is False assert ((x * y + 5 * x * y) == 6 * x * y) is True assert (6 == (x * y + 5 * x * y)) is False assert (6 * x * y == (x * y + 5 * x * y)) is True assert ((x * y - x * y) == 0) is True assert (0 == (x * y - x * y)) is True assert ((x * y - x * y) == 1) is False assert (1 == (x * y - x * y)) is False assert ((x * y - x * y) == 1) is False assert (1 == (x * y - x * y)) is False assert ((x * y + 5 * x * y) != 6) is True assert ((x * y + 5 * x * y) != 6 * x * y) is False assert (6 != (x * y + 5 * x * y)) is True assert (6 * x * y != (x * y + 5 * x * y)) is False assert ((x * y - x * y) != 0) is False assert (0 != (x * y - x * y)) is False assert ((x * y - x * y) != 1) is True assert (1 != (x * y - x * y)) is True Rt, t = ring("t", ZZ) R, x, y = ring("x,y", Rt) assert (t**3 * x / x == t**3) is True assert (t**3 * x / x == t**4) is False
def test_PolyElement___call__(): R, x = ring("x", ZZ) f = 3*x + 1 assert f(0) == 1 assert f(1) == 4 pytest.raises(ValueError, lambda: f()) pytest.raises(ValueError, lambda: f(0, 1)) pytest.raises(CoercionFailed, lambda: f(QQ(1, 7))) R, x, y = ring("x,y", ZZ) f = 3*x + y**2 + 1 assert f(0, 0) == 1 assert f(1, 7) == 53 Ry = R.drop(x) assert f(0) == Ry.y**2 + 1 assert f(1) == Ry.y**2 + 4 pytest.raises(ValueError, lambda: f()) pytest.raises(ValueError, lambda: f(0, 1, 2)) pytest.raises(CoercionFailed, lambda: f(1, QQ(1, 7))) pytest.raises(CoercionFailed, lambda: f(QQ(1, 7), 1)) pytest.raises(CoercionFailed, lambda: f(QQ(1, 7), QQ(1, 7)))
def test_PolyElement_evaluate(): R, x = ring("x", ZZ) f = x**3 + 4 * x**2 + 2 * x + 3 r = f.evaluate(x, 0) assert r == 3 and not isinstance(r, PolyElement) pytest.raises(CoercionFailed, lambda: f.evaluate(x, QQ(1, 7))) R, x, y, z = ring("x,y,z", ZZ) f = (x * y)**3 + 4 * (x * y)**2 + 2 * x * y + 3 r = f.evaluate(x, 0) assert r == 3 and isinstance(r, R.drop(x).dtype) r = f.evaluate([(x, 0), (y, 0)]) assert r == 3 and isinstance(r, R.drop(x, y).dtype) r = f.evaluate(y, 0) assert r == 3 and isinstance(r, R.drop(y).dtype) r = f.evaluate([(y, 0), (x, 0)]) assert r == 3 and isinstance(r, R.drop(y, x).dtype) r = f.evaluate([(x, 0), (y, 0), (z, 0)]) assert r == 3 and not isinstance(r, PolyElement) pytest.raises(CoercionFailed, lambda: f.evaluate([(x, 1), (y, QQ(1, 7))])) pytest.raises(CoercionFailed, lambda: f.evaluate([(x, QQ(1, 7)), (y, 1)])) pytest.raises(CoercionFailed, lambda: f.evaluate([(x, QQ(1, 7)), (y, QQ(1, 7))]))
def test_PolyElement___call__(): R, x = ring("x", ZZ) f = 3 * x + 1 assert f(0) == 1 assert f(1) == 4 pytest.raises(ValueError, lambda: f()) pytest.raises(ValueError, lambda: f(0, 1)) pytest.raises(CoercionFailed, lambda: f(QQ(1, 7))) R, x, y = ring("x,y", ZZ) f = 3 * x + y**2 + 1 assert f(0, 0) == 1 assert f(1, 7) == 53 Ry = R.drop(x) assert f(0) == Ry.y**2 + 1 assert f(1) == Ry.y**2 + 4 pytest.raises(ValueError, lambda: f()) pytest.raises(ValueError, lambda: f(0, 1, 2)) pytest.raises(CoercionFailed, lambda: f(1, QQ(1, 7))) pytest.raises(CoercionFailed, lambda: f(QQ(1, 7), 1)) pytest.raises(CoercionFailed, lambda: f(QQ(1, 7), QQ(1, 7)))
def test_PolyElement_cancel(): R, x, y = ring("x,y", ZZ) f = 2 * x**3 + 4 * x**2 + 2 * x g = 3 * x**2 + 3 * x F = 2 * x + 2 G = 3 assert f.cancel(g) == (F, G) assert (-f).cancel(g) == (-F, G) assert f.cancel(-g) == (-F, G) R, x, y = ring("x,y", QQ) f = QQ(1, 2) * x**3 + x**2 + QQ(1, 2) * x g = QQ(1, 3) * x**2 + QQ(1, 3) * x F = 3 * x + 3 G = 2 assert f.cancel(g) == (F, G) assert (-f).cancel(g) == (-F, G) assert f.cancel(-g) == (-F, G) Fx, x = field("x", ZZ) Rt, t = ring("t", Fx) f = (-x**2 - 4) / 4 * t g = t**2 + (x**2 + 2) / 2 assert f.cancel(g) == ((-x**2 - 4) * t, 4 * t**2 + 2 * x**2 + 4)
def test_PolyElement___pow__(): R, x = ring("x", ZZ, grlex) f = 2 * x + 3 assert f**0 == 1 assert f**1 == f assert f**2 == f._pow_generic(2) == f._pow_multinomial( 2) == 4 * x**2 + 12 * x + 9 assert f**3 == f._pow_generic(3) == f._pow_multinomial( 3) == 8 * x**3 + 36 * x**2 + 54 * x + 27 assert f**4 == f._pow_generic(4) == f._pow_multinomial( 4) == 16 * x**4 + 96 * x**3 + 216 * x**2 + 216 * x + 81 assert f**5 == f._pow_generic(5) == f._pow_multinomial( 5) == 32 * x**5 + 240 * x**4 + 720 * x**3 + 1080 * x**2 + 810 * x + 243 pytest.raises(ValueError, lambda: f**-2) R, x, y, z = ring("x,y,z", ZZ, grlex) f = x**3 * y - 2 * x * y**2 - 3 * z + 1 g = x**6 * y**2 - 4 * x**4 * y**3 - 6 * x**3 * y * z + 2 * x**3 * y + 4 * x**2 * y**4 + 12 * x * y**2 * z - 4 * x * y**2 + 9 * z**2 - 6 * z + 1 assert f**2 == f._pow_generic(2) == f._pow_multinomial(2) == g R, t = ring("t", ZZ) f = -11200 * t**4 - 2604 * t**2 + 49 g = 15735193600000000*t**16 + 14633730048000000*t**14 + 4828147466240000*t**12 \ + 598976863027200*t**10 + 3130812416256*t**8 - 2620523775744*t**6 \ + 92413760096*t**4 - 1225431984*t**2 + 5764801 assert f**4 == f._pow_generic(4) == f._pow_multinomial(4) == g
def test_dmp_abs(): R, x = ring('x', ZZ) assert R.dmp_abs(0) == 0 assert R.dmp_abs(x**2 - 1) == x**2 + 1 assert R.dmp_abs(1) == 1 assert R.dmp_abs(-7) == 7 assert R.dmp_abs(-x**2 + 2*x + 3) == x**2 + 2*x + 3 assert R.dmp_abs(-1) == 1 R, x = ring('x', QQ) assert R.dmp_abs(0) == 0 assert R.dmp_abs(QQ(+1, 2)) == QQ(1, 2) assert R.dmp_abs(QQ(-7, 3)) == QQ(7, 3) assert R.dmp_abs(-x**2/7 + 2*x/7 + QQ(3, 7)) == x**2/7 + 2*x/7 + QQ(3, 7) assert R.dmp_abs(QQ(-1, 2)) == QQ(1, 2) R, x, y, z = ring('x y z', ZZ) assert R.dmp_abs(0) == 0 assert R.dmp_abs(1) == 1 assert R.dmp_abs(-7) == 7 R, x, y, z = ring('x y z', QQ) assert R.dmp_abs(0) == 0 assert R.dmp_abs(QQ(1, 2)) == QQ(1, 2) assert R.dmp_abs(QQ(-7, 9)) == QQ(7, 9)
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_dmp_abs(): R, x = ring('x', ZZ) assert R.dmp_abs(0) == 0 assert R.dmp_abs(x**2 - 1) == x**2 + 1 assert R.dmp_abs(1) == 1 assert R.dmp_abs(-7) == 7 assert R.dmp_abs(-x**2 + 2 * x + 3) == x**2 + 2 * x + 3 assert R.dmp_abs(-1) == 1 R, x = ring('x', QQ) assert R.dmp_abs(0) == 0 assert R.dmp_abs(QQ(+1, 2)) == QQ(1, 2) assert R.dmp_abs(QQ(-7, 3)) == QQ(7, 3) assert R.dmp_abs(-x**2 / 7 + 2 * x / 7 + QQ(3, 7)) == x**2 / 7 + 2 * x / 7 + QQ(3, 7) assert R.dmp_abs(QQ(-1, 2)) == QQ(1, 2) R, x, y, z = ring('x y z', ZZ) assert R.dmp_abs(0) == 0 assert R.dmp_abs(1) == 1 assert R.dmp_abs(-7) == 7 R, x, y, z = ring('x y z', QQ) assert R.dmp_abs(0) == 0 assert R.dmp_abs(QQ(1, 2)) == QQ(1, 2) assert R.dmp_abs(QQ(-7, 9)) == QQ(7, 9)
def test_dmp_ground_monic(): R, x = ring('x', ZZ) assert R.dmp_ground_monic(3 * x**2 + 6 * x + 9) == x**2 + 2 * x + 3 pytest.raises(ExactQuotientFailed, lambda: R.dmp_ground_monic(3 * x**2 + 4 * x + 5)) R, x = ring('x', QQ) assert R.dmp_ground_monic(0) == 0 assert R.dmp_ground_monic(1) == 1 assert R.dmp_ground_monic(7 * x**2 + x + 21) == x**2 + x / 7 + 3 assert R.dmp_ground_monic(3 * x**2 + 4 * x + 2) == x**2 + 4 * x / 3 + QQ(2, 3) R, x, y = ring('x y', ZZ) assert R.dmp_ground_monic(3 * x**2 + 6 * x + 9) == x**2 + 2 * x + 3 pytest.raises(ExactQuotientFailed, lambda: R.dmp_ground_monic(3 * x**2 + 4 * x + 5)) R, x, y = ring('x y', QQ) assert R.dmp_ground_monic(0) == 0 assert R.dmp_ground_monic(1) == 1 assert R.dmp_ground_monic(7 * x**2 + x + 21) == x**2 + x / 7 + 3
def test_dmp_discriminant(): R, x = ring("x", ZZ) assert R.dmp_discriminant(0) == 0 assert R.dmp_discriminant(x) == 1 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 + 2*x + 3) == -8 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_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(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 _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_dmp_add_term(): R, x = ring('x', ZZ) f = 0 assert R.dmp_add_term(f, 0, 0) == 0 assert R.dmp_add_term(f, 1, 0) == 1 assert R.dmp_add_term(f, 1, 1) == x assert R.dmp_add_term(f, 1, 2) == x**2 f = x**2 + x + 1 assert R.dmp_add_term(f, 1, 0) == x**2 + x + 2 assert R.dmp_add_term(f, 1, 1) == x**2 + 2 * x + 1 assert R.dmp_add_term(f, 1, 2) == 2 * x**2 + x + 1 assert R.dmp_add_term(f, 1, 3) == x**3 + x**2 + x + 1 assert R.dmp_add_term(f, 1, 4) == x**4 + x**2 + x + 1 assert R.dmp_add_term(f, 1, 5) == x**5 + x**2 + x + 1 assert R.dmp_add_term(f, 1, 6) == x**6 + x**2 + x + 1 assert R.dmp_add_term(f, -1, 2) == x + 1 R, x, y, z = ring('x y z', ZZ) f = f_polys()[0] assert R.dmp_add_term(f, 0, 3) == f R, x, y, z = ring('x y z', QQ) f = f.set_ring(R) / 7 assert R.dmp_add_term(f, 0, 3) == f
def test_dmp_neg(): R, x = ring('x', ZZ) assert R.dmp_neg(0) == 0 assert R.dmp_neg(x**2 - 1) == 1 - x**2 assert R.dmp_neg(1) == -1 assert R.dmp_neg(-7) == 7 assert R.dmp_neg(-x**2 + 2*x + 3) == x**2 - 2*x - 3 assert R.dmp_neg(-1) == 1 R, x = ring('x', QQ) assert R.dmp_neg(0) == 0 assert R.dmp_neg(QQ(1, 2)) == QQ(-1, 2) assert R.dmp_neg(QQ(-7, 9)) == QQ(7, 9) assert R.dmp_neg(-x**2/7 + 2*x/7 + QQ(3, 7)) == x**2/7 - 2*x/7 - QQ(3, 7) assert R.dmp_neg(QQ(-1, 2)) == QQ(1, 2) R, x, y, z = ring('x y z', ZZ) assert R.dmp_neg(0) == 0 assert R.dmp_neg(1) == -1 assert R.dmp_neg(-7) == 7 R, x, y, z = ring('x y z', QQ) assert R.dmp_neg(0) == 0 assert R.dmp_neg(QQ(1, 9)) == QQ(-1, 9) assert R.dmp_neg(QQ(-7, 9)) == QQ(7, 9)
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 R, x, y = ring("x,y", QQ) f = 2 * x * y - x**2 / 2 + QQ(1, 3) g = 3 * x**3 - x * y**2 - QQ(1, 2) h = (x**5 - 4 * x**4 * y - x**3 * y**2 / 3 - 2 * x**3 / 3 + 4 * x**2 * y**3 / 3 - x**2 / 6 + 2 * x * y**2 / 9 + 2 * x * y / 3 + QQ(1, 9)) assert R.dmp_lcm(f, g) == h
def test_dmp_add_term(): R, x = ring('x', ZZ) f = 0 assert R.dmp_add_term(f, 0, 0) == 0 assert R.dmp_add_term(f, 1, 0) == 1 assert R.dmp_add_term(f, 1, 1) == x assert R.dmp_add_term(f, 1, 2) == x**2 f = x**2 + x + 1 assert R.dmp_add_term(f, 1, 0) == x**2 + x + 2 assert R.dmp_add_term(f, 1, 1) == x**2 + 2*x + 1 assert R.dmp_add_term(f, 1, 2) == 2*x**2 + x + 1 assert R.dmp_add_term(f, 1, 3) == x**3 + x**2 + x + 1 assert R.dmp_add_term(f, 1, 4) == x**4 + x**2 + x + 1 assert R.dmp_add_term(f, 1, 5) == x**5 + x**2 + x + 1 assert R.dmp_add_term(f, 1, 6) == x**6 + x**2 + x + 1 assert R.dmp_add_term(f, -1, 2) == x + 1 R, x, y, z = ring('x y z', ZZ) f = f_polys()[0] assert R.dmp_add_term(f, 0, 3) == f R, x, y, z = ring('x y z', QQ) f = f.set_ring(R)/7 assert R.dmp_add_term(f, 0, 3) == f
def test_dup_isolate_all_roots(): R, x = ring("x", ZZ) f = (4 * x**3 - x**2 + 2 * x + 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 - 1)**2 * (x + 1)**3 pytest.raises(NotImplementedError, lambda: R.dup_isolate_all_roots(f)) D, y = ring("y", ZZ) R, x = ring("x", D) f = x**2 + y * x - 1 pytest.raises(DomainError, lambda: R.dup_isolate_all_roots(f))
def test_dmp_mul_ground(): R, x = ring('x', ZZ) f = 0 assert R.dmp_mul_ground(f, ZZ(2)) == 0 f = x**2 + 2*x - 1 assert R.dmp_mul_ground(f, ZZ(3)) == 3*x**2 + 6*x - 3 f = x**2 + 2*x + 3 assert R.dmp_mul_ground(f, ZZ(0)) == 0 assert R.dmp_mul_ground(f, ZZ(2)) == 2*x**2 + 4*x + 6 R, x, y, z = ring('x y z', ZZ) f = f_polys()[0] assert (R.dmp_mul_ground(f, ZZ(2)) == 2*x**2*y*z**2 + 4*x**2*y*z + 6*x**2*y + 4*x**2 + 6*x + 8*y**2*z**2 + 10*y**2*z + 12*y**2 + 2*y*z**2 + 4*y*z + 2*y + 2) R, x, y, z = ring('x y z', QQ) f = f.set_ring(R)/7 assert (R.dmp_mul_ground(f, QQ(1, 2)) == x**2*y*z**2/14 + x**2*y*z/7 + 3*x**2*y/14 + x**2/7 + 3*x/14 + 2*y**2*z**2/7 + 5*y**2*z/14 + 3*y**2/7 + y*z**2/14 + y*z/7 + y/14 + QQ(1, 14))
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 R, x = ring("x", QQ.algebraic_field(I)) f = x**3 + I * x + 2 assert R.dup_count_real_roots(f) == 0 f *= (x - 1) * (x + 1) assert R.dup_count_real_roots(f) == 2
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} Fuv, u, v = field("u,v", ZZ) Rxyz, x, y, z = ring("x,y,z", Fuv) f = u - x assert dict(f) == {(0, 0, 0): u, (1, 0, 0): -Fuv.one}
def test_dmp_slice_in(): R, x = ring('x', ZZ) f = x**3 + 2 * x**2 + 3 * x + 4 assert f.slice(0, 0) == 0 assert f.slice(0, 1) == 4 assert f.slice(0, 2) == 3 * x + 4 assert f.slice(0, 3) == 2 * x**2 + 3 * x + 4 assert f.slice(0, 4) == f assert f.slice(0, 9) == f assert f.slice(1, 0) == 0 assert f.slice(1, 1) == 0 assert f.slice(1, 2) == 3 * x assert f.slice(1, 3) == 2 * x**2 + 3 * x assert f.slice(1, 4) == x**3 + 2 * x**2 + 3 * x pytest.raises(IndexError, lambda: R.dmp_slice_in(f, 0, 0, -1)) assert (x + 2).slice(0, 3) == x + 2 R, x, y = ring('x y', ZZ) f = x + 2 * y**2 + 3 * y + 4 assert f.slice(1, 2) == f assert f.slice(2, 1) == 2 * y**2 + 3 * y + 5
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**2 + 1, irreducible=True) 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(x**2/2 + x + 1) is False R, x = ring("x", ZZ.poly_ring("y")) assert R.dup_cyclotomic_p(x**2 + x + 1) is False
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_dmp_pow(): R, x = ring('x', ZZ) assert R.dmp_pow(0, 0) == 1 assert R.dmp_pow(0, 1) == 0 assert R.dmp_pow(0, 7) == 0 pytest.raises(ValueError, lambda: R.dmp_pow(1, -1)) assert R.dmp_pow(1, 0) == 1 assert R.dmp_pow(1, 1) == 1 assert R.dmp_pow(1, 7) == 1 assert R.dmp_pow(3, 0) == 1 assert R.dmp_pow(3, 1) == 3 assert R.dmp_pow(3, 7) == 2187 f = 2*x**4 + x + 7 assert R.dmp_pow(f, 0) == 1 assert R.dmp_pow(f, 1) == f assert R.dmp_pow(f, 2) == 4*x**8 + 4*x**5 + 28*x**4 + x**2 + 14*x + 49 assert R.dmp_pow(f, 3) == (8*x**12 + 12*x**9 + 84*x**8 + 6*x**6 + 84*x**5 + 294*x**4 + x**3 + 21*x**2 + 147*x + 343) assert R.dmp_pow(x - 2, 3) == x**3 - 6*x**2 + 12*x - 8 R, x = ring('x', QQ) assert R.dmp_pow(0, 0) == 1 assert R.dmp_pow(1, 0) == 1 assert R.dmp_pow(1, 1) == 1 assert R.dmp_pow(1, 7) == 1 assert R.dmp_pow(QQ(3, 7), 0) == 1 assert R.dmp_pow(QQ(3, 7), 1) == QQ(3, 7) assert R.dmp_pow(QQ(3, 7), 7) == QQ(2187, 823543) R, x, y = ring('x y', ZZ) assert R.dmp_pow(0, 0) == 1 assert R.dmp_pow(0, 1) == 0 assert R.dmp_pow(0, 7) == 0 assert R.dmp_pow(1, 0) == 1 assert R.dmp_pow(1, 1) == 1 assert R.dmp_pow(1, 7) == 1 pytest.raises(ValueError, lambda: R.dmp_pow(1, -1)) R, x, y = ring('x y', QQ) assert R.dmp_pow(0, 0) == 1 assert R.dmp_pow(QQ(3, 7), 0) == 1 assert R.dmp_pow(QQ(3, 7), 1) == QQ(3, 7) assert R.dmp_pow(QQ(3, 7), 7) == QQ(2187, 823543)
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(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_dmp_mul_term(): R, x = ring('x', ZZ) f = 0 assert R.dmp_mul_term(f, 2, 3) == 0 f = x + 1 assert R.dmp_mul_term(f, 0, 3) == 0 f = x**2 + 2*x + 3 assert R.dmp_mul_term(f, 2, 0) == 2*x**2 + 4*x + 6 assert R.dmp_mul_term(f, 2, 1) == 2*x**3 + 4*x**2 + 6*x assert R.dmp_mul_term(f, 2, 2) == 2*x**4 + 4*x**3 + 6*x**2 assert R.dmp_mul_term(f, 2, 3) == 2*x**5 + 4*x**4 + 6*x**3 R, x, y = ring('x y', ZZ) assert R.dmp_mul_term(0, 2, 3) == 0 assert R.dmp_mul_term(1, 0, 3) == 0 f = x*y + 2*x + 3 assert R.dmp_mul_term(f, 2, 2) == 2*x**3*y + 4*x**3 + 6*x**2 R, x, y = ring('x y', QQ) assert R.dmp_mul_term(0, QQ(2, 3), 3) == 0 assert R.dmp_mul_term(QQ(1, 2), 0, 3) == 0 f = x*y/5 + 2*x/5 + QQ(3, 5) assert R.dmp_mul_term(f, QQ(2, 3), 2) == 2*x**3*y/15 + 4*x**3/15 + 2*x**2/5
def test_dmp_neg(): R, x = ring('x', ZZ) assert R.dmp_neg(0) == 0 assert R.dmp_neg(x**2 - 1) == 1 - x**2 assert R.dmp_neg(1) == -1 assert R.dmp_neg(-7) == 7 assert R.dmp_neg(-x**2 + 2 * x + 3) == x**2 - 2 * x - 3 assert R.dmp_neg(-1) == 1 R, x = ring('x', QQ) assert R.dmp_neg(0) == 0 assert R.dmp_neg(QQ(1, 2)) == QQ(-1, 2) assert R.dmp_neg(QQ(-7, 9)) == QQ(7, 9) assert R.dmp_neg(-x**2 / 7 + 2 * x / 7 + QQ(3, 7)) == x**2 / 7 - 2 * x / 7 - QQ(3, 7) assert R.dmp_neg(QQ(-1, 2)) == QQ(1, 2) R, x, y, z = ring('x y z', ZZ) assert R.dmp_neg(0) == 0 assert R.dmp_neg(1) == -1 assert R.dmp_neg(-7) == 7 R, x, y, z = ring('x y z', QQ) assert R.dmp_neg(0) == 0 assert R.dmp_neg(QQ(1, 9)) == QQ(-1, 9) assert R.dmp_neg(QQ(-7, 9)) == QQ(7, 9)
def test_PolynomialRing_to_ground(): R, x = ring("x", ZZ) pytest.raises(ValueError, lambda: R.to_ground()) R2, x, y = ring("x,y", ZZ) assert R2.drop_to_ground(x) == ZZ.poly_ring("x").poly_ring("y") assert R2.drop_to_ground(x, y) == R2
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
def test_dmp_eval_in(): R, x = ring('x', ZZ) assert R.dmp_eval_in(0, ZZ(7), 0) == 0 assert R.dmp_eval_in(x + 2, ZZ(0), 0) == 2 assert R.dmp_eval_in(x**2 + 2*x + 3, ZZ(7), 0) == 66 assert R.dmp_eval_in(x**2 + 2*x + 3, ZZ(2), 0) == 11 assert R.dmp_eval_in(0, ZZ(3), 0) == 0 R, x, y = ring('x y', ZZ) R1 = R.drop(x) assert R.dmp_eval_in(0, 3, 0) == 0 assert R.dmp_eval_in(y + 2, 0, 0) == R1.y + 2 assert R.dmp_eval_in(3*x*y + 2*x + y + 2, 3, 0) == 10*R1.y + 8 assert R.dmp_eval_in(2*x*y + 3*x + y + 2, 2, 0) == 5*R1.y + 8 R, x, y, z = ring('x y z', ZZ) R1 = R.drop(x) R3 = R.drop(z) assert R.dmp_eval_in(0, 3, 0) == 0 assert R.dmp_eval_in(1, 3, 0) == 1 assert R.dmp_eval_in(z + 2, 3, 0) == R1.z + 2 assert R.dmp_eval_in(3*x*z + 2*x + z + 2, 3, 0) == 10*R1.z + 8 f = 45*x**3 - 9*y**3 - y**2 + 3*z**3 + 10*z assert R.dmp_eval_in(f, -2, 2) == 45*R3.x**3 - 9*R3.y**3 - R3.y**2 - 44 pytest.raises(IndexError, lambda: R.dmp_eval_in(f, -2, -1)) R, x, y, z, t = ring('x y z t', ZZ) f = f_polys()[6] R2 = R.drop(y) R3 = R.drop(z) x, z, t = R2.gens assert (R.dmp_eval_in(f, -2, 1) == -4230*x**4 + 45*x**3*z**3*t**2 - 45*x**3*t**2 - 282*x*z**3 - 188*x*z*t - 6392*x + 3*z**6*t**2 + 2*z**4*t**3 + 65*z**3*t**2 - 2*z*t**3 - 68*t**2) assert (R.dmp_eval_in(f, 7, 1) == 14805*x**4 + 45*x**3*z**3*t**2 - 45*x**3*t**2 + 987*x*z**3 + 658*x*z*t - 1031744*x + 3*z**6*t**2 + 2*z**4*t**3 - 3139*z**3*t**2 - 2*z*t**3 + 3136*t**2) x, y, t = R3.gens assert (R.dmp_eval_in(f, -2, 2) == 2115*x**4*y - 405*x**3*t**2 - 423*x*y**4 - 47*x*y**3 - 188*x*y*t - 1128*x*y + 81*y**3*t**2 + 9*y**2*t**2 + 36*t**3 + 216*t**2) assert (R.dmp_eval_in(f, 7, 2) == 2115*x**4*y + 15390*x**3*t**2 - 423*x*y**4 - 47*x*y**3 + 658*x*y*t + 48363*x*y - 3078*y**3*t**2 - 342*y**2*t**2 + 4788*t**3 + 351918*t**2)
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.dmp_ground_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 s.is_squarefree and UV.dmp_degree_in(s, 0) == R.dmp_degree_in(w_1, 0) _, 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) # 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 # TODO # 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] factors = R.dmp_zz_wang_hensel_lifting(w_1, H, LC, A, p) assert R.dmp_expand(factors) == w_1
def test_PolyElement_sqf_norm(): R, x = ring("x", QQ.algebraic_field(sqrt(3))) X = R.to_ground().x assert (x**2 - 2).sqf_norm() == (1, x**2 - 2*sqrt(3)*x + 1, X**4 - 10*X**2 + 1) R, x = ring("x", QQ.algebraic_field(sqrt(2))) X = R.to_ground().x assert (x**2 - 3).sqf_norm() == (1, x**2 - 2*sqrt(2)*x - 1, X**4 - 10*X**2 + 1)
def test_PolyElement_monic(): R, x = ring("x", ZZ) assert (2*x + 2).monic() == x + 1 pytest.raises(ExactQuotientFailed, lambda: (2*x + 1).monic()) R, x = ring("x", QQ) assert (2*x + 1).monic() == x + QQ(1, 2)
def test_dmp_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.dmp_cancel(f, g) == (p, q) assert R.dmp_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.dmp_cancel(f, g) == (f, g) assert R.dmp_cancel(F, G) == (f, g) assert R.dmp_cancel(0, 0) == (0, 0) assert R.dmp_cancel(0, 0, include=False) == (1, 1, 0, 0) assert R.dmp_cancel(x, 0) == (1, 0) assert R.dmp_cancel(x, 0, include=False) == (1, 1, 1, 0) assert R.dmp_cancel(0, x) == (0, 1) assert R.dmp_cancel(0, x, include=False) == (1, 1, 0, 1) f = 0 g = x one = 1 assert R.dmp_cancel(f, g, include=True) == (f, one) 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)
def test_dmp_sub_mul(): R, x = ring('x', ZZ) assert R.dmp_sub_mul(x**2 + 2*x + 3, 3*x**2 + 2*x + 1, x + 2) == -3*x**3 - 7*x**2 - 3*x + 1 assert R.dmp_sub_mul(x**2 - 1, x - 2, x + 2) == 3 R, x, y = ring('x y', ZZ) assert R.dmp_sub_mul(x*y + 2*x + 3, 3*x + 2*y + 1, x + 2) == -3*x**2 - x*y - 5*x - 4*y + 1