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_AlgebraicElement(): K = QQ.algebraic_field(sqrt(2)) a = K.unit sT(a, "AlgebraicField(%s, Pow(Integer(2), Rational(1, 2)))([Integer(1), Integer(0)])" % repr(QQ)) K = QQ.algebraic_field(root(-2, 3)) a = K.unit sT(a, "AlgebraicField(%s, Pow(Integer(-2), Rational(1, 3)))([Integer(1), Integer(0)])" % repr(QQ))
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_FracElement___add__(): F, x, y = field("x,y", QQ) f, g = 1 / x, 1 / y assert f + g == g + f == (x + y) / (x * y) z = symbols('z') pytest.raises(TypeError, lambda: x + z) 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_primitive_element(): assert primitive_element([sqrt(2)]) == (PurePoly(x**2 - 2), [1], [[1, 0]]) assert (primitive_element([sqrt(2), sqrt(3)]) == (PurePoly(x**4 - 10*x**2 + 1), [1, 1], [[QQ(+1, 2), 0, -QQ(9, 2), 0], [QQ(-1, 2), 0, QQ(11, 2), 0]])) A = QQ.algebraic_field(sqrt(2)) assert (primitive_element([sqrt(2), sqrt(3)], domain=A) == (PurePoly(x**2 - 2*sqrt(2)*x - 1, x, domain=A), [1, 1], [[A.unit], [1, -A.unit]])) assert (primitive_element([sqrt(2), sqrt(2 + sqrt(2))], domain=A) == (PurePoly(x**2 - 2*sqrt(2)*x - sqrt(2), x, domain=A), [1, 1], [[A.unit], [1, -A.unit]])) A = QQ.algebraic_field(sqrt(2) + sqrt(3)) assert (primitive_element([sqrt(2), sqrt(3)], domain=A) == (PurePoly(x - sqrt(2) - sqrt(3), x, domain=A), [1, 1], [[A([QQ(1, 2), 0, -QQ(9, 2), 0])], [A([-QQ(1, 2), 0, QQ(11, 2), 0])]])) pytest.raises(ValueError, lambda: primitive_element([])) # issue sympy/sympy#13849 assert (primitive_element([sqrt(2), sqrt(2) + sqrt(5)]) == (PurePoly(x**4 - 76*x**2 + 4), [1, 2], [[QQ(1, 12), 0, QQ(-37, 6), 0], [QQ(-1, 24), 0, QQ(43, 12), 0]])) # issue sympy/sympy#14117 assert (primitive_element([I*sqrt(2*sqrt(2) + 3), I*sqrt(-2*sqrt(2) + 3), I]) == (PurePoly(x**4 + 54*x**2 + 81), [1, 2, 4], [[QQ(1, 3), 0], [QQ(1, 27), 0, 2, 0], [QQ(-1, 54), 0, QQ(-5, 6), 0]]))
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 = -x / 5 + QQ(3, 5) t = x**2 / 5 - 6 * x / 5 + 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 = x / 32 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_modular(): assert solve_congruence(*list(zip([3, 4, 2], [12, 35, 17]))) == (1719, 7140) assert solve_congruence(*list(zip([3, 4, 2], [12, 6, 17]))) is None assert solve_congruence(*list(zip([3, 4, 2], [13, 7, 17]))) == (172, 1547) assert solve_congruence(*list(zip([-10, -3, -15], [13, 7, 17]))) == (172, 1547) assert solve_congruence( *list(zip([-10, -3, 1, -15], [13, 7, 7, 17]))) is None assert solve_congruence( *list(zip([-10, -5, 2, -15], [13, 7, 7, 17]))) == (835, 1547) assert solve_congruence( *list(zip([-10, -5, 2, -15], [13, 7, 14, 17]))) == (2382, 3094) assert solve_congruence( *list(zip([-10, 2, 2, -15], [13, 7, 14, 17]))) == (2382, 3094) assert solve_congruence(*list(zip((1, 1, 2), (3, 2, 4)))) is None pytest.raises( ValueError, lambda: solve_congruence(*list(zip([3, 4, 2], [12.1, 35, 17])))) assert integer_rational_reconstruction(ZZ(2), 3, ZZ) == QQ(-1) assert integer_rational_reconstruction(ZZ(21), 33, ZZ) == QQ(-1) assert integer_rational_reconstruction(ZZ(-21), 17, ZZ) == QQ(-4) assert integer_rational_reconstruction(ZZ(17), 333, ZZ) is None assert integer_rational_reconstruction(ZZ(49), 335, ZZ) == QQ(8, 7)
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_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_PolyElement_gcd(): R, x, y = ring("x,y", QQ) f = x**2/2 + x + QQ(1, 2) g = x/2 + QQ(1, 2) assert f.gcd(g) == x + 1
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_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 == 3 * x / 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_to_number_field(): A = QQ.algebraic_field(sqrt(2)) assert A.convert(sqrt(2)) == A([1, 0]) B = QQ.algebraic_field(sqrt(2), sqrt(3)) assert B.convert(sqrt(2) + sqrt(3)) == B([1, 0]) K = QQ.algebraic_field(sqrt(2) + sqrt(3)) a = K([Rational(1, 2), Integer(0), -Rational(9, 2), Integer(0)]) assert B.from_expr(sqrt(2)) == a pytest.raises(CoercionFailed, lambda: QQ.algebraic_field(sqrt(3)).convert(sqrt(2))) p = x**6 - 6 * x**4 - 6 * x**3 + 12 * x**2 - 36 * x + 1 r0, r1 = p.as_poly(x).all_roots()[:2] A = QQ.algebraic_field(r0) a = A([ Rational(-96, 755), Rational(-54, 755), Rational(128, 151), Rational(936, 755), Rational(-1003, 755), Rational(2184, 755) ]) assert A.from_expr(r1) == a
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_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_dmp_from_diofant(): assert dmp_from_diofant([Integer(1), Integer(2)], 0, ZZ) == [ZZ(1), ZZ(2)] assert dmp_from_diofant([Rational(1, 2), Integer(3)], 0, QQ) == [QQ(1, 2), QQ(3, 1)] assert dmp_from_diofant([[Integer(1), Integer(2)], [Integer(0)]], 1, ZZ) == [[ZZ(1), ZZ(2)], []] assert dmp_from_diofant([[Rational(1, 2), Integer(2)]], 1, QQ) == [[QQ(1, 2), QQ(2, 1)]]
def test___eq__(): assert not QQ.poly_ring(x) == ZZ.poly_ring(x) assert not QQ.frac_field(x) == ZZ.frac_field(x) assert EX(1) != EX(2) F11 = FF(11) assert F11(2) != F11(3) assert F11(2) != object()
def test_dmp_clear_denoms(): R0, X = ring('x', QQ) R1 = R0.domain.ring.poly_ring('x') assert R0.dmp_clear_denoms(0) == (1, 0) assert R0.dmp_clear_denoms(1) == (1, 1) assert R0.dmp_clear_denoms(7) == (1, 7) assert R0.dmp_clear_denoms(QQ(7, 3)) == (3, 7) assert R0.dmp_clear_denoms(3 * X**2 + X) == (1, 3 * X**2 + X) assert R0.dmp_clear_denoms(X**2 + X / 2) == (2, 2 * X**2 + X) assert R0.dmp_clear_denoms(3 * X**2 + X, convert=True) == (1, 3 * R1.x**2 + R1.x) assert R0.dmp_clear_denoms(X**2 + X / 2, convert=True) == (2, 2 * R1.x**2 + R1.x) assert R0.dmp_clear_denoms(X / 2 + QQ(1, 3)) == (6, 3 * X + 2) assert R0.dmp_clear_denoms(X / 2 + QQ(1, 3), convert=True) == (6, 3 * R1.x + 2) assert R0.dmp_clear_denoms(3 * X**2 + X, convert=True) == (1, 3 * R1.x**2 + R1.x) assert R0.dmp_clear_denoms(X**2 + X / 2, convert=True) == (2, 2 * R1.x**2 + R1.x) R0, a = ring('a', EX) assert R0.dmp_clear_denoms(3 * a / 2 + Rational(9, 4)) == (4, 6 * a + 9) assert R0.dmp_clear_denoms(7) == (1, 7) assert R0.dmp_clear_denoms(sin(x) / x * a) == (x, a * sin(x)) R0, X, Y = ring('x y', QQ) R1 = R0.domain.ring.poly_ring('x', 'y') assert R0.dmp_clear_denoms(0) == (1, 0) assert R0.dmp_clear_denoms(1) == (1, 1) assert R0.dmp_clear_denoms(7) == (1, 7) assert R0.dmp_clear_denoms(QQ(7, 3)) == (3, 7) assert R0.dmp_clear_denoms(3 * X**2 + X) == (1, 3 * X**2 + X) assert R0.dmp_clear_denoms(X**2 + X / 2) == (2, 2 * X**2 + X) assert R0.dmp_clear_denoms(3 * X**2 + X, convert=True) == (1, 3 * R1.x**2 + R1.x) assert R0.dmp_clear_denoms(X**2 + X / 2, convert=True) == (2, 2 * R1.x**2 + R1.x) R0, a, b = ring('a b', EX) assert R0.dmp_clear_denoms(3 * a / 2 + Rational(9, 4)) == (4, 6 * a + 9) assert R0.dmp_clear_denoms(7) == (1, 7) assert R0.dmp_clear_denoms(sin(x) / x * b) == (x, b * sin(x))
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_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, -10 * x / 9 + QQ(13, 3), -QQ(3303, 100) ]
def test_PolynomialRing_is_(): R = QQ.poly_ring("x") assert R.is_univariate is True assert R.is_multivariate is False R = QQ.poly_ring("x", "y", "z") assert R.is_univariate is False assert R.is_multivariate is True
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, -5 * x**4 / 9 + x**2 / 9 - QQ(1, 3), -117 * x**2 / 25 - 9 * x + QQ(441, 25), 233150 * x / 19773 - QQ(102500, 6591), -QQ(1288744821, 543589225) ]
def test_dup_count_complex_roots_implicit(): R, x = ring("x", ZZ) f = (x**2 + 1) * (x - 1) * (x + 1) * 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 assert R.dup_count_complex_roots(f, inf=QQ(-2), sup=QQ(-1)) == 1
def test_conversion(): L = QQ.poly_ring(x, y, order="ilex") G = QQ.poly_ring(x, y) assert L.convert(x) == L.convert(G.convert(x), G) assert G.convert(x) == G.convert(L.convert(x), L) pytest.raises(CoercionFailed, lambda: G.convert(L.convert(1 / (1 + x)), L)) R = ALG.poly_ring(x, y) assert R.convert(ALG(1), ALG) == R(1) pytest.raises(CoercionFailed, lambda: R.convert(ALG(1), QQ.algebraic_field(sqrt(2))))
def test_modgcd_algebraic_field(): A = QQ.algebraic_field(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 = QQ.algebraic_field(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))) h = x**4 * y**9 + sqrt(6) / 22 * z f, g = h * (21 * y**3 + 1), h * (y + x) assert func_field_modgcd(f, g) == (x**4 * y**9 + sqrt(6) / 22 * z, 21 * y**3 + 1, x + y) h = x**4 * y**3 + sqrt(6) / 22 * z f, g = h * (11 * y**3 + 1), h * (y + x) assert func_field_modgcd(f, g) == (x**4 * y**3 + sqrt(6) / 22 * z, 11 * y**3 + 1, x + y) A = QQ.algebraic_field(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)
def test_AlgebraicElement(): K = QQ.algebraic_field(sqrt(2)) a = K.unit sT( a, "AlgebraicField(%s, Pow(Integer(2), Rational(1, 2)))([Integer(1), Integer(0)])" % repr(QQ)) K = QQ.algebraic_field(root(-2, 3)) a = K.unit sT( a, "AlgebraicField(%s, Pow(Integer(-2), Rational(1, 3)))([Integer(1), Integer(0)])" % repr(QQ))
def test_dup_count_complex_roots_9(): R, x = ring("x", QQ.algebraic_field(sqrt(2))) f = -x**3 + sqrt(2) * x - 1 assert R.dup_count_complex_roots(f, a, b) == 2 assert R.dup_count_complex_roots(f, c, d) == 1 R, x = ring("x", QQ.algebraic_field(sqrt(2)).algebraic_field(I)) f = -x**3 + I * x**2 + sqrt(2) * x - 1 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_9(): R, x = ring("x", QQ.algebraic_field(sqrt(2))) f = -x**3 + sqrt(2)*x - 1 assert R.dup_count_complex_roots(f, a, b) == 2 assert R.dup_count_complex_roots(f, c, d) == 1 R, x = ring("x", QQ.algebraic_field(sqrt(2)).algebraic_field(I)) f = -x**3 + I*x**2 + sqrt(2)*x - 1 assert R.dup_count_complex_roots(f, a, b) == 2 assert R.dup_count_complex_roots(f, c, d) == 1
def test_modgcd_algebraic_field(): A = QQ.algebraic_field(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 = QQ.algebraic_field(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))) h = x**4*y**9 + sqrt(6)/22*z f, g = h*(21*y**3 + 1), h*(y + x) assert func_field_modgcd(f, g) == (x**4*y**9 + sqrt(6)/22*z, 21*y**3 + 1, x + y) h = x**4*y**3 + sqrt(6)/22*z f, g = h*(11*y**3 + 1), h*(y + x) assert func_field_modgcd(f, g) == (x**4*y**3 + sqrt(6)/22*z, 11*y**3 + 1, x + y) A = QQ.algebraic_field(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)
def test_units(): R = QQ.poly_ring(x) assert R.convert(1) == R.one assert R.convert(x) != R.one assert R.convert(1 + x) != R.one R = QQ.poly_ring(x, order='ilex') assert R.convert(1) == R.one assert R.convert(x) != R.one R = ZZ.poly_ring(x) assert R.convert(1) == R.one assert R.convert(2) != R.one assert R.convert(x) != R.one assert R.convert(1 + x) != R.one
def test_dup_ext_factor(): R, x = ring("x", QQ.algebraic_field(I)) assert R.dup_ext_factor(0) == (R.domain(0), []) f = x + 1 assert R.dup_ext_factor(f) == (R.domain(1), [(f, 1)]) g = 2 * x + 2 assert R.dup_ext_factor(g) == (R.domain(2), [(f, 1)]) f = 7 * x**4 + 1 g = x**4 + QQ(1, 7) assert R.dup_ext_factor(f) == (R.domain(7), [(g, 1)]) f = x**4 + 1 assert R.dup_ext_factor(f) == (R.domain(1), [(x**2 - I, 1), (x**2 + I, 1)]) f = 4 * x**2 + 9 assert R.dup_ext_factor(f) == (R.domain(4), [(x - 3 * I / 2, 1), (x + 3 * I / 2, 1)]) f = 4 * x**4 + 8 * x**3 + 77 * x**2 + 18 * x + 153 assert R.dup_ext_factor(f) == (4, [(x - 3 * I / 2, 1), (x + 1 + 4 * I, 1), (x + 1 - 4 * I, 1), (x + 3 * I / 2, 1)]) R, x = ring("x", QQ.algebraic_field(sqrt(2))) f = x**4 + 1 assert R.dup_ext_factor(f) == (R.domain(1), [(x**2 - sqrt(2) * x + 1, 1), (x**2 + sqrt(2) * x + 1, 1)]) f = x**2 + 2 * sqrt(2) * x + 2 assert R.dup_ext_factor(f) == (R.domain(1), [(x + sqrt(2), 2)]) assert R.dup_ext_factor(f**3) == (R.domain(1), [(x + sqrt(2), 6)]) f *= 2 assert R.dup_ext_factor(f) == (R.domain(2), [(x + sqrt(2), 2)]) assert R.dup_ext_factor(f**3) == (R.domain(8), [(x + sqrt(2), 6)])
def test_minpoly_domain(): F = QQ.algebraic_field(sqrt(2)) assert minimal_polynomial(sqrt(2), domain=F) == PurePoly(x - sqrt(2), x, domain=F) assert minimal_polynomial(sqrt(8), domain=F)(x) == x - 2*sqrt(2) assert minimal_polynomial(sqrt(Rational(3, 2)), domain=F)(x) == 2*x**2 - 3 pytest.raises(NotAlgebraic, lambda: minimal_polynomial(y, domain=QQ)) # issue sympy/sympy#14494 F = QQ.algebraic_field(I) assert minimal_polynomial(I, domain=F)(x) == x - I F = QQ.algebraic_field(sqrt(3)*I) assert minimal_polynomial(exp(I*pi/3), domain=F)(x) == 2*x - sqrt(3)*I - 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 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_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_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_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_FracElement_diff(): F, x, y, z = field("x,y,z", ZZ) assert ((x**2 + y)/(z + 1)).diff(x) == 2*x/(z + 1) F, x, y = field('x,y', QQ.algebraic_field(I)) assert ((x - y)/x).diff(x) == y/x**2
def test_Domain_get_exact(): assert EX.get_exact() == EX assert ZZ.get_exact() == ZZ assert QQ.get_exact() == QQ assert RR.get_exact() == QQ assert ALG.get_exact() == ALG assert ZZ.poly_ring(x).get_exact() == ZZ.poly_ring(x) assert QQ.poly_ring(x).get_exact() == QQ.poly_ring(x) assert ZZ.poly_ring(x, y).get_exact() == ZZ.poly_ring(x, y) assert QQ.poly_ring(x, y).get_exact() == QQ.poly_ring(x, y) assert ZZ.frac_field(x).get_exact() == ZZ.frac_field(x) assert QQ.frac_field(x).get_exact() == QQ.frac_field(x) assert ZZ.frac_field(x, y).get_exact() == ZZ.frac_field(x, y) assert QQ.frac_field(x, y).get_exact() == QQ.frac_field(x, y)
def test_Gaussian_postprocess(): opt = {'gaussian': True} Gaussian.postprocess(opt) assert opt == { 'gaussian': True, 'extension': {I}, 'domain': QQ.algebraic_field(I), }
def test_FractionField(): sT(ZZ.frac_field("x"), "FractionField(%s, (Symbol('x'),), " "LexOrder())" % repr(ZZ)) sT(QQ.frac_field("x", "y", order=grlex), "FractionField(%s, (Symbol('x'), Symbol('y')), " "GradedLexOrder())" % repr(QQ)) sT(ZZ.poly_ring("t").frac_field("x", "y", "z"), "FractionField(PolynomialRing(%s, (Symbol('t'),), LexOrder()), " "(Symbol('x'), Symbol('y'), Symbol('z')), LexOrder())" % repr(ZZ))
def test_Domain_convert(): assert QQ.convert(10e-52) == QQ(1684996666696915, 1684996666696914987166688442938726917102321526408785780068975640576) R, x = ring("x", ZZ) assert ZZ.convert(x - x) == 0 assert ZZ.convert(x - x, R) == 0 F3 = FF(3) assert F3.convert(Float(2.0)) == F3.dtype(2) assert F3.convert(PythonRational(2, 1)) == F3.dtype(2) pytest.raises(CoercionFailed, lambda: F3.convert(PythonRational(1, 2))) assert F3.convert(2.0) == F3.dtype(2) pytest.raises(CoercionFailed, lambda: F3.convert(2.1)) assert RR.convert(CC(1)) == RR(1) pytest.raises(CoercionFailed, lambda: RR.convert(CC(1, 2))) assert QQ.convert(ALG.new(1), ALG) == QQ(1) pytest.raises(CoercionFailed, lambda: QQ.convert(ALG.new([1, 1]), ALG)) assert ZZ.convert(ALG.new(1), ALG) == ZZ(1) pytest.raises(CoercionFailed, lambda: ZZ.convert(ALG.new([1, 1]), ALG)) assert EX.convert(ALG.new([1, 1]), ALG) == sqrt(2) + sqrt(3) + 1 ALG2 = QQ.algebraic_field(sqrt(2)) a2 = ALG2.convert(sqrt(2)) a = ALG.convert(a2, ALG2) assert a.rep.to_dense() == [QQ(1, 2), 0, -QQ(9, 2), 0] assert ZZ_python.convert(3.0) == ZZ_python.dtype(3) pytest.raises(CoercionFailed, lambda: ZZ_python.convert(3.2)) assert CC.convert(QQ_python(1, 2)) == CC(0.5) CC01 = ComplexField(tol=0.1) assert CC.convert(CC01(0.3)) == CC(0.3) assert RR.convert(complex(2 + 0j)) == RR(2) pytest.raises(CoercionFailed, lambda: RR.convert(complex(2 + 3j))) assert ALG.convert(EX(sqrt(2)), EX) == ALG.from_expr(sqrt(2)) pytest.raises(CoercionFailed, lambda: ALG.convert(EX(sqrt(5)), EX)) pytest.raises(CoercionFailed, lambda: ALG2.convert(ALG.unit))
def test_arithmetics(): assert ZZ.rem(ZZ(2), ZZ(3)) == 2 assert ZZ.div(ZZ(2), ZZ(3)) == (0, 2) assert QQ.rem(QQ(2, 3), QQ(4, 7)) == 0 assert QQ.div(QQ(2, 3), QQ(4, 7)) == (QQ(7, 6), 0) assert QQ_python.factorial(QQ_python(7, 2)) == 6 assert CC.gcd(CC(1), CC(2)) == 1 assert CC.lcm(CC(1), CC(2)) == 2 assert EX(Rational(2, 3)).numerator == 2 assert EX(Rational(2, 3)).denominator == 3 assert abs(EX(-2)) == 2 assert -EX(2) == -2 assert 2 + EX(3) == EX(3) + 2 == 5 assert 2 - EX(3) == EX(2) - 3 == -1 assert 2*EX(3) == EX(3)*2 == 6 assert 2/EX(3) == EX(2)/3 == EX(Rational(2, 3)) assert EX(2)**2 == 4 pytest.raises(TypeError, lambda: EX(2) + object()) pytest.raises(TypeError, lambda: EX(2) - object()) pytest.raises(TypeError, lambda: EX(2)*object()) pytest.raises(TypeError, lambda: EX(2)**object()) pytest.raises(TypeError, lambda: EX(2)/object()) F11 = FF(11) assert +F11(2) == F11(2) assert F11(5) + 7 == 7 + F11(5) == F11(1) assert F11(5) - 7 == 5 - F11(7) == F11(9) assert F11(5)*7 == 7*F11(5) == F11(2) assert F11(5)/9 == 5/F11(9) == F11(3) assert F11(4) % 9 == 4 % F11(9) == F11(4) pytest.raises(TypeError, lambda: F11(2) + object()) pytest.raises(TypeError, lambda: F11(2) - object()) pytest.raises(TypeError, lambda: F11(2)*object()) pytest.raises(TypeError, lambda: F11(2)**object()) pytest.raises(TypeError, lambda: F11(2)/object()) pytest.raises(TypeError, lambda: F11(2) % object()) pytest.raises(TypeError, lambda: object() % F11(2))
def test_to_number_field(): A = QQ.algebraic_field(sqrt(2)) assert A.convert(sqrt(2)) == A([1, 0]) B = QQ.algebraic_field(sqrt(2), sqrt(3)) assert B.convert(sqrt(2) + sqrt(3)) == B([1, 0]) K = QQ.algebraic_field(sqrt(2) + sqrt(3)) a = K([Rational(1, 2), Integer(0), -Rational(9, 2), Integer(0)]) assert B.from_expr(sqrt(2)) == a pytest.raises(CoercionFailed, lambda: QQ.algebraic_field(sqrt(3)).convert(sqrt(2))) p = x**6 - 6*x**4 - 6*x**3 + 12*x**2 - 36*x + 1 r0, r1 = p.as_poly(x).all_roots()[:2] A = QQ.algebraic_field(r0) a = A([Rational(-96, 755), Rational(-54, 755), Rational(128, 151), Rational(936, 755), Rational(-1003, 755), Rational(2184, 755)]) assert A.from_expr(r1) == a
def test_dup_real_imag(): R, x, y = ring("x y", ZZ) assert R.dup_real_imag(R.zero) == (0, 0) assert R.dup_real_imag(R.one) == (1, 0) assert R.dup_real_imag(x + 1) == (x + 1, y) assert R.dup_real_imag(x + 2) == (x + 2, y) assert R.dup_real_imag(x**2 + 2*x + 3) == (x**2 - y**2 + 2*x + 3, 2*x*y + 2*y) f = x**3 + x**2 + x + 1 assert R.dup_real_imag(f) == (x**3 + x**2 - 3*x*y**2 + x - y**2 + 1, 3*x**2*y + 2*x*y - y**3 + y) R, x, y = ring("x y", EX) pytest.raises(DomainError, lambda: R.dup_real_imag(x + 1)) R = QQ.algebraic_field(I).poly_ring("x", "y") x, y = R.to_ground().gens f = R.x**4 + I*R.x**3 - R.x + 1 r = x**4 - 6*x**2*y**2 - 3*x**2*y - x + y**4 + y**3 + 1 i = 4*x**3*y + x**3 - 4*x*y**3 - 3*x*y**2 - y assert R.dup_real_imag(f) == (r, i) K = QQ.algebraic_field(sqrt(2)) R = K.poly_ring("x", "y") x, y = R.gens f = R.x**2 + sqrt(2)*R.x - 1 assert R.dup_real_imag(f) == (x**2 - y**2 + sqrt(2)*x - 1, 2*x*y + sqrt(2)*y) K = K.algebraic_field(I) R = K.poly_ring("x", "y") x, y = R.to_ground().gens f = R.x**2 + 2*sqrt(2)*I*R.x - 1 + I assert R.dup_real_imag(f) == (x**2 - y**2 - 2*sqrt(2)*y - 1, 2*x*y + 2*sqrt(2)*x + 1)
def test_PolynomialRing(): sT(ZZ.poly_ring("x"), "PolynomialRing(%s, (Symbol('x'),), " "LexOrder())" % repr(ZZ)) sT(QQ.poly_ring("x", "y", order=grlex), "PolynomialRing(%s, (Symbol('x'), Symbol('y')), " "GradedLexOrder())" % repr(QQ)) sT(ZZ.poly_ring("t").poly_ring("x", "y", "z"), "PolynomialRing(PolynomialRing(%s, (Symbol('t'),), " "LexOrder()), (Symbol('x'), Symbol('y'), Symbol('z')), " "LexOrder())" % repr(ZZ))
def test_Domain___eq__(): assert (ZZ.poly_ring(x, y) == ZZ.poly_ring(x, y)) is True assert (QQ.poly_ring(x, y) == QQ.poly_ring(x, y)) is True assert (ZZ.poly_ring(x, y) == QQ.poly_ring(x, y)) is False assert (QQ.poly_ring(x, y) == ZZ.poly_ring(x, y)) is False assert (ZZ.frac_field(x, y) == ZZ.frac_field(x, y)) is True assert (QQ.frac_field(x, y) == QQ.frac_field(x, y)) is True assert (ZZ.frac_field(x, y) == QQ.frac_field(x, y)) is False assert (QQ.frac_field(x, y) == ZZ.frac_field(x, y)) is False
def test_sympyissue_5786(): R, x, y, z, t = ring("x, y, z, t", QQ.algebraic_field(I)) f = (z - I*t)*(x - I*y) assert (R.dmp_ext_factor(f) == (R.domain(1), [(z - I*t, 1), (x - I*y, 1)])) f = (z - I*t)**2*(x - I*y) assert (R.dmp_ext_factor(f) == (R.domain(1), [(z - I*t, 2), (x - I*y, 1)])) f = (z - I*t)*(x - I*y)**3 assert (R.dmp_ext_factor(f) == (R.domain(1), [(z - I*t, 1), (x - I*y, 3)]))
def test_Domain_unify_algebraic(): sqrt5 = QQ.algebraic_field(sqrt(5)) sqrt7 = QQ.algebraic_field(sqrt(7)) sqrt57 = QQ.algebraic_field(sqrt(5), sqrt(7)) assert sqrt5.unify(sqrt7) == sqrt57 assert sqrt5.unify(sqrt5.poly_ring(x, y)) == sqrt5.poly_ring(x, y) assert sqrt5.poly_ring(x, y).unify(sqrt5) == sqrt5.poly_ring(x, y) assert sqrt5.unify(sqrt5.frac_field(x, y)) == sqrt5.frac_field(x, y) assert sqrt5.frac_field(x, y).unify(sqrt5) == sqrt5.frac_field(x, y) assert sqrt5.unify(sqrt7.poly_ring(x, y)) == sqrt57.poly_ring(x, y) assert sqrt5.poly_ring(x, y).unify(sqrt7) == sqrt57.poly_ring(x, y) assert sqrt5.unify(sqrt7.frac_field(x, y)) == sqrt57.frac_field(x, y) assert sqrt5.frac_field(x, y).unify(sqrt7) == sqrt57.frac_field(x, y) sqrt2 = QQ.algebraic_field(sqrt(2)) r = RootOf(x**7 - x + 1, 0) rootof = QQ.algebraic_field(r) ans = QQ.algebraic_field(r + sqrt(2)) assert sqrt2.unify(rootof) == rootof.unify(sqrt2) == ans # here domain created from tuple, not Expr p = Poly(x**3 - sqrt(2)*x - 1, x) sqrt2 = p.domain assert sqrt2.unify(rootof) == rootof.unify(sqrt2) == ans
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_ComplexInterval(): R, x = ring("x", QQ.algebraic_field(I)) f = x**3 + x + I _, r1, r2 = R.dup_isolate_complex_roots_sqf(f, blackbox=True) assert r1.is_disjoint(r2) is True assert r1.is_disjoint(r2, check_re_refinement=True) is False for i in range(4): r1, r2 = r1.refine(), r2.refine() assert r1.is_disjoint(r2, check_re_refinement=True) is True
def test_sring(): x, y, z, t = symbols("x,y,z,t") R = ZZ.poly_ring("x", "y", "z") assert sring(x + 2*y + 3*z) == (R, R.x + 2*R.y + 3*R.z) R = QQ.poly_ring("x", "y", "z") assert sring(x + 2*y + z/3) == (R, R.x + 2*R.y + R.z/3) assert sring([x, 2*y, z/3]) == (R, [R.x, 2*R.y, R.z/3]) Rt = ZZ.poly_ring("t") R = Rt.poly_ring("x", "y", "z") assert sring(x + 2*t*y + 3*t**2*z, x, y, z) == (R, R.x + 2*Rt.t*R.y + 3*Rt.t**2*R.z) Rt = QQ.poly_ring("t") R = Rt.poly_ring("x", "y", "z") assert sring(x + t*y/2 + t**2*z/3, x, y, z) == (R, R.x + Rt.t*R.y/2 + Rt.t**2*R.z/3) Rt = ZZ.frac_field("t") R = Rt.poly_ring("x", "y", "z") assert sring(x + 2*y/t + t**2*z/3, x, y, z) == (R, R.x + 2*R.y/Rt.t + Rt.t**2*R.z/3) R = QQ.poly_ring("x", "y") assert sring(x + y, domain=QQ) == (R, R.x + R.y)
def test_dup_count_complex_roots_exclude(): R, x = ring("x", ZZ) f = (x**2 + 1)*(x**2 - 1)*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 R, x = ring("x", QQ.algebraic_field(I)) f = x**4 + I*x**3 - x + 1 assert R.dup_count_complex_roots(f, inf=(QQ(0), QQ(0)), sup=(QQ(1), QQ(1))) == 1 r = R.dup_isolate_complex_roots_sqf(f) assert r == [((QQ(-201, 100), QQ(-201, 100)), (0, 0)), ((QQ(-201, 100), 0), (0, QQ(201, 100))), ((0, QQ(-201, 100)), (QQ(201, 100), 0)), ((0, 0), (QQ(201, 100), QQ(201, 100)))] assert all(R.dup_count_complex_roots(f, inf=i, sup=s) == 1 for i, s in r)
def test_to_ZZ_ANP_poly(): A = QQ.algebraic_field(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_PolyElement_gcd(): R, x, y = ring("x,y", QQ) f = x**2/2 + x + QQ(1, 2) g = x/2 + QQ(1, 2) assert f.gcd(g) == x + 1 with using(use_heu_gcd=False): assert f.gcd(g) == x + 1 with using(use_heu_gcd=False, fallback_gcd_zz_method='modgcd'): assert f.gcd(g) == x + 1 R, x, y = ring("x,y", QQ.algebraic_field(sqrt(2))) f, g = (x + sqrt(2)*y)**2, x + sqrt(2)*y assert f.gcd(g) == g with using(gcd_aa_method='modgcd'): assert f.gcd(g) == g
def test_Domain_field(): assert EX.has_assoc_Field is True assert ZZ.has_assoc_Field is True assert QQ.has_assoc_Field is True assert RR.has_assoc_Field is True assert ALG.has_assoc_Field is True assert ZZ.poly_ring(x).has_assoc_Field is True assert QQ.poly_ring(x).has_assoc_Field is True assert ZZ.poly_ring(x, y).has_assoc_Field is True assert QQ.poly_ring(x, y).has_assoc_Field is True assert EX.field == EX assert ZZ.field == QQ assert QQ.field == QQ assert RR.field == RR assert ALG.field == ALG assert ZZ.poly_ring(x).field == ZZ.frac_field(x) assert QQ.poly_ring(x).field == QQ.frac_field(x) assert ZZ.poly_ring(x, y).field == ZZ.frac_field(x, y) assert QQ.poly_ring(x, y).field == QQ.frac_field(x, y)
def test_dmp_gcd(): R, x = ring("x", ZZ) f, g = 0, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (0, 0, 0) f, g = 2, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, 0) f, g = -2, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, -1, 0) f, g = 0, -2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 0, -1) f, g = 0, 2*x + 4 assert R.dmp_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.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2*x + 4, 1, 0) f, g = 2, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, 1) f, g = -2, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, -1, 1) f, g = 2, -2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, -1) f, g = -2, -2 assert R.dmp_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.dmp_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.dmp_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.dmp_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.dmp_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.dmp_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.dmp_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.dmp_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.dmp_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.dmp_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.dmp_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.dmp_qq_heu_gcd(f, g) == (h, cff, cfg) assert R.dup_ff_prs_gcd(f, g) == (h, cff, cfg) assert R.dup_ff_prs_gcd(R.zero, R.zero) == ([], [], []) 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.dmp_zz_heu_gcd(f, R.dmp_diff_in(f, 1, 0))[0] == g assert R.dup_rr_prs_gcd(f, R.dmp_diff_in(f, 1, 0))[0] == g R, x = ring("x", QQ) f = x**2/2 + x + QQ(1, 2) g = x/2 + QQ(1, 2) h = x + 1 assert R.dmp_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.dmp_zz_heu_gcd(f, g) == (h, cff, cfg) with using(heu_gcd_max=0): pytest.raises(HeuristicGCDFailed, lambda: R.dmp_zz_heu_gcd(f, g)) R, x = ring("x", CC) f, g = (x**2 - 1, x**3 - 3*x + 2) assert R.dmp_inner_gcd(f, g) == (1, f, g) R, x, y = ring("x,y", CC) f, g = (x**2 - y, x**3 - y*x + 2) assert R.dmp_inner_gcd(f, g) == (1, f, g) 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) with using(use_simplify_gcd=0): 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) with using(heu_gcd_max=0): pytest.raises(HeuristicGCDFailed, lambda: R.dmp_zz_heu_gcd(f, g)) f = x**2 + 2*x*y + y**2 g = x**2 + x*y assert R.dmp_rr_prs_gcd(f, g) == (x + y, x + y, x) 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 = x**2/2 + x + QQ(1, 2) g = x/2 + 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)) with using(use_simplify_gcd=0): 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)) assert R.dmp_ff_prs_gcd(R.zero, R.zero) == (0, 0, 0) assert R.dmp_qq_heu_gcd(R.zero, R.zero) == (0, 0, 0) assert R.dmp_ff_prs_gcd(R.zero, g) == (x + 1, R.zero, QQ(1, 2)) assert R.dmp_qq_heu_gcd(R.zero, g) == (x + 1, R.zero, 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) R, x, y = ring("x,y", ZZ) f = (-17434367009167300000000000000000000000000000000000000000000000000000000*x**4*y - 250501827896299135568887342575961783764139560000000000000000000000000000000000000000000*x**3*y - 2440935909299672540738135183426056447877858000000000000000000000000000000*x**3 - 1349729941723537919695626818065131519270095220127010623905326719279566297660000000000000000000000000000*x**2*y - 26304033868956978374552886858060487282904504027042515077682955951658838800000000000000000*x**2 - 3232215785736369696036755035364398565076440134133908303058376297547504030528179314849416971379040931276000000000000000*x*y - 94485916261760032526508027937078714464844205539023800247528621905831259414691631156161537919255129011800*x - 2902585888465621357542575571971656665554321652262249362701116665830760628936600958940851960635161420991047110815678789984677193092993*y - 113133324167442997472440652189550843502029192913459268196939183295294085146407870078840385860571627108778756267503630290) g = (10000000000000000000000000000*x**2 + 71841388839807267676152024786000000000000000*x + 129029628760809605749020969023932901278290735413660734705971) assert (R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (g, -1743436700916730000000000000000000000000000*x**2*y - 12525091394814956778444367128798089188206978000000000000000*x*y - 244093590929967254073813518342605644787785800*x - 22495499028725631994927113634418779135935898997901327211111875586270479483*y - 876801128965234839118530545935732755107147297241756982389990, 1)) R, x = ring("x", ZZ) f, g = x**2 - 1, x**2 - 3*x + 2 assert R.dmp_gcd(f, g) == x - 1 with using(use_heu_gcd=False, fallback_gcd_zz_method='modgcd'): R.dmp_gcd(f, g) == x - 1 R, x = ring("x", QQ) f, g = x**2/2 + x + QQ(1, 2), x/2 + QQ(1, 2) assert R.dmp_gcd(f, g) == x + 1 with using(use_heu_gcd=False): R.dmp_gcd(f, g) == x + 1 R, x, y = ring("x,y", QQ.algebraic_field(sqrt(2))) f, g = (x + sqrt(2)*y)**2, x + sqrt(2)*y assert R.dmp_gcd(f, g) == g with using(gcd_aa_method='modgcd'): assert R.dmp_gcd(f, g) == g
def test_dmp_sqf(): R, x = ring("x", ZZ) assert R(0).sqf_part() == 0 assert R(0).is_squarefree is True assert R(7).sqf_part() == 1 assert R(7).is_squarefree is True assert (2*x + 2).sqf_part() == x + 1 assert (2*x + 2).is_squarefree is True assert (x**3 + x + 1).sqf_part() == x**3 + x + 1 assert (x**3 + x + 1).is_squarefree is True assert (-x**3 + x + 1).sqf_part() == x**3 - x - 1 assert (-x**3 + x + 1).is_squarefree is True assert (2*x**3 + 3*x**2).sqf_part() == 2*x**2 + 3*x assert (2*x**3 + 3*x**2).is_squarefree is False assert (-2*x**3 + 3*x**2).sqf_part() == 2*x**2 - 3*x assert (-2*x**3 + 3*x**2).is_squarefree is False assert (x**3 - 3*x - 2).sqf_part() == x**2 - x - 2 assert (x**3 - 3*x - 2).is_squarefree is False assert R(0).sqf_list() == (0, []) assert R(1).sqf_list() == (1, []) assert x.sqf_list() == (1, [(x, 1)]) assert (2*x**2).sqf_list() == (2, [(x, 2)]) assert (3*x**3).sqf_list() == (3, [(x, 3)]) assert (-x**5 + x**4 + x - 1).sqf_list() == (-1, [(x**3 + x**2 + x + 1, 1), (x - 1, 2)]) assert (x**8 + 6*x**6 + 12*x**4 + 8*x**2).sqf_list() == (1, [(x, 2), (x**2 + 2, 3)]) assert (2*x**2 + 4*x + 2).sqf_list() == (2, [(x + 1, 2)]) R, x = ring("x", QQ) assert (2*x**2 + 4*x + 2).sqf_list() == (2, [(x + 1, 2)]) R, x = ring("x", FF(2)) assert (x**2 + 1).sqf_list() == (1, [(x + 1, 2)]) R, x = ring("x", FF(3)) assert (x**10 + 2*x**7 + 2*x**4 + x).sqf_list() == (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 f.sqf_part() == f assert g.sqf_part() == y + 1 assert f.is_squarefree is True assert g.is_squarefree is False R, x = ring("x", FF(5)) f = x**8 + x**7 + 3*x**6 + x**4 + 2*x**2 + 2*x + 1 assert f.sqf_part() == x**2 + 4*x + 3 f = 3*x**2 + 2*x + 4 assert f.is_squarefree is True f = 2*x**6 + 4*x**5 + 4*x**4 + 2*x**3 + 2*x**2 + x + 4 assert f.is_squarefree is False R, x = ring("x", FF(11)) f = x**3 + 5*x**2 + 8*x + 4 assert f.sqf_part() == x**2 + 3*x + 2 assert R(0).is_squarefree is True assert R(0).sqf_list() == (0, []) assert R(1).is_squarefree is True assert R(1).sqf_list() == (1, []) assert (x + 1).is_squarefree is True assert (x + 1).sqf_list() == (1, [(x + 1, 1)]) f = x**11 + 1 assert f.is_squarefree is False assert f.sqf_list() == (1, [(x + 1, 11)]) f = x**3 + 5*x**2 + 8*x + 4 assert f.is_squarefree is False assert f.sqf_list() == (1, [(x + 1, 1), (x + 2, 2)]) R, x = ring('x', FF(3)) f = x**10 + 2*x**7 + 2*x**4 + x assert f.is_squarefree is False assert f.sqf_list() == (1, [(x, 1), (x + 1, 3), (x + 2, 6)]) R, x = ring('x', FF(53)) f = x**6 + 2*x**5 + 5*x**4 + 26*x**3 + 41*x**2 + 39*x + 38 assert f.is_squarefree is True R, x = ring('x', FF(102953)) f = x**15 + x + 1 assert f.is_squarefree is True 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_in(D, 1, 0), y)) res = R.dmp_resultant(f, g) h = (4*y**2 + 1).drop(x) assert res.sqf_list() == (45796, [(h, 3)]) pytest.raises(DomainError, lambda: (x**2 - 1).sqf_norm()) K = QQ.algebraic_field(sqrt(3)) R, x = ring("x", K) _, X = ring("x", QQ) assert (x**2 - 2).sqf_norm() == (1, x**2 + K([QQ(-2), QQ(0)])*x + 1, X**4 - 10*X**2 + 1) R, x, y = ring("x,y", ZZ) assert R(0).sqf_part() == 0 assert R(0).is_squarefree is True assert R(7).sqf_part() == 1 assert R(7).is_squarefree is True assert R(3).sqf_list() == (3, []) R, x, y, z = ring("x,y,z", ZZ) assert f_0.is_squarefree is True assert (f_0**2).is_squarefree is False assert f_1.is_squarefree is True assert (f_1**2).is_squarefree is False assert f_2.is_squarefree is True assert (f_2**2).is_squarefree is False assert f_3.is_squarefree is True assert (f_3**2).is_squarefree is False assert f_5.is_squarefree is False assert (f_5**2).is_squarefree is False assert f_4.is_squarefree is True assert f_4.sqf_part() == -f_4 assert f_5.sqf_part() == x + y - z R, x, y, z, t = ring("x,y,z,t", ZZ) assert f_6.is_squarefree is True assert f_6.sqf_part() == f_6 R, x = ring("x", ZZ) f = -x**5 + x**4 + x - 1 assert f.sqf_list() == (-1, [(x**3 + x**2 + x + 1, 1), (x - 1, 2)]) f = 2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16 assert f.sqf_list() == (2, [(x + 1, 2), (x + 2, 3)]) R, x, y = ring("x,y", ZZ) f = -x**5 + x**4 + x - 1 assert f.sqf_list() == (-1, [(x**3 + x**2 + x + 1, 1), (x - 1, 2)]) pytest.raises(DomainError, lambda: (x**2 + y**2).sqf_norm()) R, x, y = ring("x,y", FF(2)) pytest.raises(NotImplementedError, lambda: (y**2 + 1).sqf_list()) pytest.raises(NotImplementedError, lambda: (x**3 + 2*x**2*y + x*y**2).sqf_part()) R, x, y = ring("x,y", QQ.algebraic_field(I)) assert (x**2 + 2*I*x - 1).sqf_list() == (1, [(x + I, 2)])