def test_DMP_to_dict(): f = DMP([[3], [], [2], [], [8]], ZZ) assert f.to_dict() == \ {(4, 0): 3, (2, 0): 2, (0, 0): 8} assert f.to_diofant_dict() == \ {(4, 0): ZZ.to_expr(3), (2, 0): ZZ.to_expr(2), (0, 0): ZZ.to_expr(8)}
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_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_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_FractionField___init__(): F1 = ZZ.frac_field("x", "y") F2 = ZZ.frac_field("x", "y") F3 = ZZ.frac_field("x", "y", "z") assert F1.x == F1.gens[0] assert F1.y == F1.gens[1] assert F1.x == F2.x assert F1.y == F2.y assert F1.x != F3.x assert F1.y != F3.y F4 = ZZ.frac_field("gens") assert type(F4.gens) is tuple
def test_Domain_postprocess(): pytest.raises(GeneratorsError, lambda: Domain.postprocess({'gens': (x, y), 'domain': ZZ.poly_ring(y, z)})) pytest.raises(GeneratorsError, lambda: Domain.postprocess({'gens': (), 'domain': EX})) pytest.raises(GeneratorsError, lambda: Domain.postprocess({'domain': EX}))
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_FractionField_methods(): F = ZZ.frac_field("x") assert F.domain_new(2) == ZZ(2) x = symbols("x") assert F.field_new(x**2 + x) == F.x**2 + F.x
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_PolyElement_drop(): R, x, y, z = ring("x,y,z", ZZ) assert R(1).drop(0).ring == ZZ.poly_ring("y", "z") assert R(1).drop(0).drop(0).ring == ZZ.poly_ring("z") assert isinstance(R(1).drop(0).drop(0).drop(0), R.dtype) is False pytest.raises(ValueError, lambda: z.drop(0).drop(0).drop(0)) pytest.raises(ValueError, lambda: x.drop(0)) f = z**2*x + 2*z*y + x*z + 1 R2 = R.drop_to_ground(z) assert f.drop_to_ground(z) == z**2*R2.x + 2*z*R2.y + z*R2.x + 1 R3 = R.drop(y, z) assert R3 == ZZ.poly_ring('x') pytest.raises(ValueError, lambda: R3.x.drop_to_ground(R3.x))
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_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_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_FracElement___call__(): F, x, y, z = field("x,y,z", ZZ) f = (x**2 + 3*y)/z pytest.raises(ValueError, lambda: f(1, 1, 1, 1)) r = f(1, 1, 1) assert r == 4 and not isinstance(r, FracElement) pytest.raises(ZeroDivisionError, lambda: f(1, 1, 0)) Fz = ZZ.frac_field("z") assert f(1, 1) == 4/Fz.z
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__dict_from_expr_no_gens(): pytest.raises(GeneratorsNeeded, lambda: dict_from_expr(Integer(17))) assert dict_from_expr(x) == ({(1,): 1}, (x,)) assert dict_from_expr(y) == ({(1,): 1}, (y,)) assert dict_from_expr(x*y) == ({(1, 1): 1}, (x, y)) assert dict_from_expr(x + y) == ({(1, 0): 1, (0, 1): 1}, (x, y)) assert dict_from_expr(sqrt(2)) == ({(1,): 1}, (sqrt(2),)) pytest.raises(GeneratorsNeeded, lambda: dict_from_expr(sqrt(2), greedy=False)) assert dict_from_expr(x*y, domain=ZZ.poly_ring(x)) == ({(1,): x}, (y,)) assert dict_from_expr(x*y, domain=ZZ.poly_ring(y)) == ({(1,): y}, (x,)) assert dict_from_expr(3*sqrt( 2)*pi*x*y, extension=None) == ({(1, 1, 1, 1): 3}, (x, y, pi, sqrt(2))) assert dict_from_expr(3*sqrt( 2)*pi*x*y, extension=True) == ({(1, 1, 1): 3*sqrt(2)}, (x, y, pi)) f = cos(x)*sin(x) + cos(x)*sin(y) + cos(y)*sin(x) + cos(y)*sin(y) assert dict_from_expr(f) == ({(0, 1, 0, 1): 1, (0, 1, 1, 0): 1, (1, 0, 0, 1): 1, (1, 0, 1, 0): 1}, (cos(x), cos(y), sin(x), sin(y)))
def test_PolynomialRing_drop(): R, x, y, z = ring("x,y,z", ZZ) assert R.drop(x) == ZZ.poly_ring("y", "z") assert R.drop(y) == ZZ.poly_ring("x", "z") assert R.drop(z) == ZZ.poly_ring("x", "y") assert R.drop(0) == ZZ.poly_ring("y", "z") assert R.drop(0).drop(0) == ZZ.poly_ring("z") assert R.drop(0).drop(0).drop(0) == ZZ assert R.drop(1) == ZZ.poly_ring("x", "z") assert R.drop(2) == ZZ.poly_ring("x", "y") assert R.drop(2).drop(1) == ZZ.poly_ring("x") assert R.drop(2).drop(1).drop(0) == ZZ pytest.raises(ValueError, lambda: R.drop(3)) pytest.raises(ValueError, lambda: R.drop(x).drop(y))
def test_dup_isolate_all_roots(): R, x = ring("x", ZZ) f = 4*x**4 - x**3 + 2*x**2 + 5*x assert R.dup_isolate_all_roots(f) == \ ([((-1, 0), 1), ((0, 0), 1)], [(((0, -QQ(5, 2)), (QQ(5, 2), 0)), 1), (((0, 0), (QQ(5, 2), QQ(5, 2))), 1)]) assert R.dup_isolate_all_roots(f, eps=QQ(1, 10)) == \ ([((QQ(-7, 8), QQ(-6, 7)), 1), ((0, 0), 1)], [(((QQ(35, 64), -QQ(35, 32)), (QQ(5, 8), -QQ(65, 64))), 1), (((QQ(35, 64), QQ(65, 64)), (QQ(5, 8), QQ(35, 32))), 1)]) f = x**5 + x**4 - 2*x**3 - 2*x**2 + x + 1 pytest.raises(NotImplementedError, lambda: R.dup_isolate_all_roots(f)) D = ZZ.poly_ring("y") R, x = ring("x", D) y, = D.gens f = x**2 + y*x - 1 pytest.raises(DomainError, lambda: R.dup_isolate_all_roots(f))
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_FractionField__init(): pytest.raises(GeneratorsNeeded, lambda: ZZ.frac_field())
def test_construct_domain(): assert construct_domain([1, 2, 3]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)]) assert construct_domain([1, 2, 3], field=True) == (QQ, [QQ(1), QQ(2), QQ(3)]) assert construct_domain([Integer(1), Integer(2), Integer(3)]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)]) assert construct_domain([Integer(1), Integer(2), Integer(3)], field=True) == (QQ, [QQ(1), QQ(2), QQ(3)]) assert construct_domain([Rational(1, 2), Integer(2)]) == (QQ, [QQ(1, 2), QQ(2)]) assert construct_domain([3.14, 1, Rational(1, 2)]) == (RR, [RR(3.14), RR(1.0), RR(0.5)]) assert construct_domain([3.14, sqrt(2)], extension=False) == (EX, [EX(3.14), EX(sqrt(2))]) assert construct_domain([3.14, sqrt(2)]) == (EX, [EX(3.14), EX(sqrt(2))]) assert construct_domain([sqrt(2), 3.14]) == (EX, [EX(sqrt(2)), EX(3.14)]) assert construct_domain([1, sqrt(2)], extension=False) == (EX, [EX(1), EX(sqrt(2))]) assert construct_domain([x, sqrt(x)]) == (EX, [EX(x), EX(sqrt(x))]) assert construct_domain([x, sqrt(x), sqrt(y)]) == (EX, [EX(x), EX(sqrt(x)), EX(sqrt(y))]) alg = QQ.algebraic_field(sqrt(2)) assert (construct_domain([7, Rational(1, 2), sqrt(2)]) == (alg, [alg([7]), alg([Rational(1, 2)]), alg([1, 0])])) alg = QQ.algebraic_field(sqrt(2) + sqrt(3)) assert (construct_domain([7, sqrt(2), sqrt(3)]) == (alg, [alg([7]), alg.from_expr(sqrt(2)), alg.from_expr(sqrt(3))])) dom = ZZ.poly_ring(x) assert construct_domain([2*x, 3]) == (dom, [dom(2*x), dom(3)]) dom = ZZ.poly_ring(x, y) assert construct_domain([2*x, 3*y]) == (dom, [dom(2*x), dom(3*y)]) dom = QQ.poly_ring(x) assert construct_domain([x/2, 3]) == (dom, [dom(x/2), dom(3)]) dom = QQ.poly_ring(x, y) assert construct_domain([x/2, 3*y]) == (dom, [dom(x/2), dom(3*y)]) dom = RR.poly_ring(x) assert construct_domain([x/2, 3.5]) == (dom, [dom(x/2), dom(3.5)]) dom = RR.poly_ring(x, y) assert construct_domain([x/2, 3.5*y]) == (dom, [dom(x/2), dom(3.5*y)]) dom = ZZ.frac_field(x) assert construct_domain([2/x, 3]) == (dom, [dom(2/x), dom(3)]) dom = ZZ.frac_field(x, y) assert construct_domain([2/x, 3*y]) == (dom, [dom(2/x), dom(3*y)]) dom = RR.frac_field(x) assert construct_domain([2/x, 3.5]) == (dom, [dom(2/x), dom(3.5)]) dom = RR.frac_field(x, y) assert construct_domain([2/x, 3.5*y]) == (dom, [dom(2/x), dom(3.5*y)]) assert construct_domain(2) == (ZZ, ZZ(2)) assert construct_domain(Rational(2, 3)) == (QQ, QQ(2, 3)) assert construct_domain({}) == (ZZ, {})
def test_AlgebraicElement(): A = QQ.algebraic_field(I) rep = [QQ(1), QQ(1)] mod = [QQ(1), QQ(0), QQ(1)] f = A(rep) assert f.rep.to_dense() == rep assert f.mod.to_dense() == mod assert f.domain.domain == QQ f = A(1) assert f.rep.to_dense() == [QQ(1)] assert f.mod.to_dense() == mod assert f.domain.domain == QQ f = A([QQ(3, 2)]) assert f.rep.to_dense() == [QQ(3, 2)] assert f.mod.to_dense() == mod assert f.domain.domain == QQ B = QQ.algebraic_field(I * sqrt(2)) a = A([QQ(1), QQ(1)]) b = B([QQ(1), QQ(1)]) assert (a == a) is True assert (a != a) is False assert (a == b) is False assert (a != b) is True b = A([QQ(1), QQ(2)]) assert (a == b) is False assert (a != b) is True assert A([1, 1]) == A([int(1), int(1)]) assert hash(A([1, 1])) == hash(A([int(1), int(1)])) assert a.to_dict() == {(0, ): QQ(1), (1, ): QQ(1)} assert bool(A([])) is False assert bool(A([QQ(1)])) is True a = A([QQ(1), -QQ(1), QQ(2)]) assert a.LC() == -1 assert a.rep.to_dense() == [-1, 1] A = QQ.algebraic_field(root(2, 3)) assert A.unit > 0 assert A.unit >= 0 assert (A.unit < 0) is False assert (A.unit <= 0) is False pytest.raises(TypeError, lambda: A.unit > x) pytest.raises(TypeError, lambda: QQ.algebraic_field(I).unit > 0) assert abs(+A.unit) == A.unit assert abs(-A.unit) == A.unit a = A([QQ(2), QQ(-1), QQ(1)]) b = A([QQ(1), QQ(2)]) c = A([QQ(-2), QQ(1), QQ(-1)]) assert +a == a assert -a == c c = A([QQ(2), QQ(0), QQ(3)]) assert a + b == c assert b + a == c assert c + 1 == A([QQ(2), QQ(0), QQ(4)]) pytest.raises(TypeError, lambda: c + "x") pytest.raises(TypeError, lambda: "x" + c) c = A([QQ(2), QQ(-2), QQ(-1)]) assert a - b == c c = A([QQ(-2), QQ(2), QQ(1)]) assert b - a == c assert c - 1 == A([QQ(-2), QQ(2), QQ(0)]) pytest.raises(TypeError, lambda: c - "x") pytest.raises(TypeError, lambda: "x" - c) c = A([QQ(3), QQ(-1), QQ(6)]) assert a * b == c assert b * a == c assert c * 2 == A([QQ(6), QQ(-2), QQ(12)]) pytest.raises(TypeError, lambda: c * "x") pytest.raises(TypeError, lambda: "x" * c) c = A([QQ(11, 10), -QQ(1, 5), -QQ(3, 5)]) assert c / 2 == A([QQ(11, 20), -QQ(1, 10), -QQ(3, 10)]) pytest.raises(TypeError, lambda: c / "x") pytest.raises(TypeError, lambda: "x" / c) c = A([QQ(-1, 43), QQ(9, 43), QQ(5, 43)]) assert a**0 == A(1) assert a**1 == a assert a**-1 == c pytest.raises(TypeError, lambda: a**QQ(1, 2)) assert a * a**(-1) == A(1) assert 1 / a == a**(-1) A = QQ.algebraic_field(I) a = A([QQ(1, 2), QQ(1), QQ(2)]) b = A([ZZ(1), ZZ(1), ZZ(2)]) c = A([QQ(3, 2), QQ(2), QQ(4)]) assert a + b == b + a == c
def test_inject(): assert ZZ.inject(x, y, z) == ZZ.poly_ring(x, y, z) assert ZZ.poly_ring(x).inject(y, z) == ZZ.poly_ring(x, y, z) assert ZZ.frac_field(x).inject(y, z) == ZZ.frac_field(x, y, z) pytest.raises(GeneratorsError, lambda: ZZ.poly_ring(x).inject(x))
def test_PolynomialRing__init(): pytest.raises(GeneratorsNeeded, lambda: ZZ.poly_ring())
def test_Domain_unify(): F3 = GF(3) assert unify(F3, F3) == F3 assert unify(F3, ZZ) == F3 assert unify(F3, QQ) == QQ assert unify(F3, ALG) == ALG assert unify(F3, RR) == RR assert unify(F3, CC) == CC assert unify(F3, ZZ.poly_ring(x)) == F3.poly_ring(x) assert unify(F3, ZZ.frac_field(x)) == F3.frac_field(x) assert unify(F3, EX) == EX assert unify(ZZ, F3) == F3 assert unify(ZZ, ZZ) == ZZ assert unify(ZZ, QQ) == QQ assert unify(ZZ, ALG) == ALG assert unify(ZZ, RR) == RR assert unify(ZZ, CC) == CC assert unify(ZZ, ZZ.poly_ring(x)) == ZZ.poly_ring(x) assert unify(ZZ, ZZ.frac_field(x)) == ZZ.frac_field(x) assert unify(ZZ, EX) == EX assert unify(QQ, F3) == QQ assert unify(QQ, ZZ) == QQ assert unify(QQ, QQ) == QQ assert unify(QQ, ALG) == ALG assert unify(QQ, RR) == RR assert unify(QQ, CC) == CC assert unify(QQ, ZZ.poly_ring(x)) == QQ.poly_ring(x) assert unify(QQ, ZZ.frac_field(x)) == QQ.frac_field(x) assert unify(QQ, EX) == EX assert unify(RR, F3) == RR assert unify(RR, ZZ) == RR assert unify(RR, QQ) == RR assert unify(RR, ALG) == RR assert unify(RR, RR) == RR assert unify(RR, CC) == CC assert unify(RR, ZZ.poly_ring(x)) == RR.poly_ring(x) assert unify(RR, ZZ.frac_field(x)) == RR.frac_field(x) assert unify(RR, EX) == EX assert unify(CC, F3) == CC assert unify(CC, ZZ) == CC assert unify(CC, QQ) == CC assert unify(CC, ALG) == CC assert unify(CC, RR) == CC assert unify(CC, CC) == CC assert unify(CC, ZZ.poly_ring(x)) == CC.poly_ring(x) assert unify(CC, ZZ.frac_field(x)) == CC.frac_field(x) assert unify(CC, EX) == EX CC2 = ComplexField(prec=20) assert unify(CC, CC2) == unify(CC2, CC) == ComplexField(prec=CC.precision, tol=CC2.tolerance) RR2 = RealField(prec=20) assert unify(RR, RR2) == unify(RR2, RR) == RealField(prec=RR.precision, tol=RR2.tolerance) assert unify(ZZ.poly_ring(x), F3) == F3.poly_ring(x) assert unify(ZZ.poly_ring(x), ZZ) == ZZ.poly_ring(x) assert unify(ZZ.poly_ring(x), QQ) == QQ.poly_ring(x) assert unify(ZZ.poly_ring(x), ALG) == ALG.poly_ring(x) assert unify(ZZ.poly_ring(x), RR) == RR.poly_ring(x) assert unify(ZZ.poly_ring(x), CC) == CC.poly_ring(x) assert unify(ZZ.poly_ring(x), ZZ.poly_ring(x)) == ZZ.poly_ring(x) assert unify(ZZ.poly_ring(x), ZZ.frac_field(x)) == ZZ.frac_field(x) assert unify(ZZ.poly_ring(x), EX) == EX assert unify(ZZ.frac_field(x), F3) == F3.frac_field(x) assert unify(ZZ.frac_field(x), ZZ) == ZZ.frac_field(x) assert unify(ZZ.frac_field(x), QQ) == QQ.frac_field(x) assert unify(ZZ.frac_field(x), ALG) == ALG.frac_field(x) assert unify(ZZ.frac_field(x), RR) == RR.frac_field(x) assert unify(ZZ.frac_field(x), CC) == CC.frac_field(x) assert unify(ZZ.frac_field(x), ZZ.poly_ring(x)) == ZZ.frac_field(x) assert unify(ZZ.frac_field(x), ZZ.frac_field(x)) == ZZ.frac_field(x) assert unify(ZZ.frac_field(x), EX) == EX assert unify(EX, F3) == EX assert unify(EX, ZZ) == EX assert unify(EX, QQ) == EX assert unify(EX, ALG) == EX assert unify(EX, RR) == EX assert unify(EX, CC) == EX assert unify(EX, ZZ.poly_ring(x)) == EX assert unify(EX, ZZ.frac_field(x)) == EX assert unify(EX, EX) == EX
def test_Domain_ring(): assert ZZ.has_assoc_Ring is True assert QQ.has_assoc_Ring is True assert ZZ.poly_ring(x).has_assoc_Ring is True assert QQ.poly_ring(x).has_assoc_Ring is True assert ZZ.poly_ring(x, y).has_assoc_Ring is True assert QQ.poly_ring(x, y).has_assoc_Ring is True assert ZZ.frac_field(x).has_assoc_Ring is True assert QQ.frac_field(x).has_assoc_Ring is True assert ZZ.frac_field(x, y).has_assoc_Ring is True assert QQ.frac_field(x, y).has_assoc_Ring is True assert EX.has_assoc_Ring is False assert RR.has_assoc_Ring is False assert ALG.has_assoc_Ring is False assert ZZ.ring == ZZ assert QQ.ring == ZZ assert ZZ.poly_ring(x).ring == ZZ.poly_ring(x) assert QQ.poly_ring(x).ring == QQ.poly_ring(x) assert ZZ.poly_ring(x, y).ring == ZZ.poly_ring(x, y) assert QQ.poly_ring(x, y).ring == QQ.poly_ring(x, y) assert ZZ.frac_field(x).ring == ZZ.poly_ring(x) assert QQ.frac_field(x).ring == QQ.poly_ring(x) assert ZZ.frac_field(x, y).ring == ZZ.poly_ring(x, y) assert QQ.frac_field(x, y).ring == QQ.poly_ring(x, y) assert EX.ring == EX pytest.raises(AttributeError, lambda: RR.ring) pytest.raises(AttributeError, lambda: ALG.ring)
def test_dmp_normal(): assert dmp_normal([0, 1.5, 2, 3], 0, ZZ) == [ZZ(1), ZZ(2), ZZ(3)] assert (dmp_normal([0, 0, 2, 1, 0, 11, 0], 0, ZZ) == [ZZ(2), ZZ(1), ZZ(0), ZZ(11), ZZ(0)]) assert (dmp_normal([[0], [], [0, 2, 1], [0], [11], []], 1, ZZ) == [[ZZ(2), ZZ(1)], [], [ZZ(11)], []]) F5 = FF(5) assert dmp_normal([5, 10, 21, -3], 0, F5) == [F5(1), F5(2)] F11 = FF(11) assert dmp_normal([11, 22, 17, 1, 0], 0, F11) == [F11(6), F11(1), F11(0)]
def test_gf_factor(): assert gf_factor([], 11, ZZ) == (0, []) assert gf_factor([1], 11, ZZ) == (1, []) assert gf_factor([1, 1], 11, ZZ) == (1, [([1, 1], 1)]) assert gf_factor_sqf([], 11, ZZ) == (0, []) assert gf_factor_sqf([1], 11, ZZ) == (1, []) assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]]) config.setup('GF_FACTOR_METHOD', 'berlekamp') assert gf_factor_sqf([], 11, ZZ) == (0, []) assert gf_factor_sqf([1], 11, ZZ) == (1, []) assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]]) config.setup('GF_FACTOR_METHOD', 'zassenhaus') assert gf_factor_sqf([], 11, ZZ) == (0, []) assert gf_factor_sqf([1], 11, ZZ) == (1, []) assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]]) config.setup('GF_FACTOR_METHOD', 'shoup') assert gf_factor_sqf(ZZ.map([]), 11, ZZ) == (0, []) assert gf_factor_sqf(ZZ.map([1]), 11, ZZ) == (1, []) assert gf_factor_sqf(ZZ.map([1, 1]), 11, ZZ) == (1, [[1, 1]]) f, p = ZZ.map([1, 0, 0, 1, 0]), 2 g = (1, [([1, 0], 1), ([1, 1], 1), ([1, 1, 1], 1)]) config.setup('GF_FACTOR_METHOD', 'berlekamp') assert gf_factor(f, p, ZZ) == g config.setup('GF_FACTOR_METHOD', 'zassenhaus') assert gf_factor(f, p, ZZ) == g config.setup('GF_FACTOR_METHOD', 'shoup') assert gf_factor(f, p, ZZ) == g g = (1, [[1, 0], [1, 1], [1, 1, 1]]) config.setup('GF_FACTOR_METHOD', 'berlekamp') assert gf_factor_sqf(f, p, ZZ) == g config.setup('GF_FACTOR_METHOD', 'zassenhaus') assert gf_factor_sqf(f, p, ZZ) == g config.setup('GF_FACTOR_METHOD', 'shoup') assert gf_factor_sqf(f, p, ZZ) == g f, p = gf_from_int_poly([1, -3, 1, -3, -1, -3, 1], 11), 11 g = (1, [([1, 1], 1), ([1, 5, 3], 1), ([1, 2, 3, 4], 1)]) config.setup('GF_FACTOR_METHOD', 'berlekamp') assert gf_factor(f, p, ZZ) == g config.setup('GF_FACTOR_METHOD', 'zassenhaus') assert gf_factor(f, p, ZZ) == g config.setup('GF_FACTOR_METHOD', 'shoup') assert gf_factor(f, p, ZZ) == g f, p = [1, 5, 8, 4], 11 g = (1, [([1, 1], 1), ([1, 2], 2)]) config.setup('GF_FACTOR_METHOD', 'berlekamp') assert gf_factor(f, p, ZZ) == g config.setup('GF_FACTOR_METHOD', 'zassenhaus') assert gf_factor(f, p, ZZ) == g config.setup('GF_FACTOR_METHOD', 'shoup') assert gf_factor(f, p, ZZ) == g f, p = [1, 1, 10, 1, 0, 10, 10, 10, 0, 0], 11 g = (1, [([1, 0], 2), ([1, 9, 5], 1), ([1, 3, 0, 8, 5, 2], 1)]) config.setup('GF_FACTOR_METHOD', 'berlekamp') assert gf_factor(f, p, ZZ) == g config.setup('GF_FACTOR_METHOD', 'zassenhaus') assert gf_factor(f, p, ZZ) == g config.setup('GF_FACTOR_METHOD', 'shoup') assert gf_factor(f, p, ZZ) == g f, p = gf_from_dict({32: 1, 0: 1}, 11, ZZ), 11 g = (1, [([1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 10], 1), ([1, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 10], 1)]) config.setup('GF_FACTOR_METHOD', 'berlekamp') assert gf_factor(f, p, ZZ) == g config.setup('GF_FACTOR_METHOD', 'zassenhaus') assert gf_factor(f, p, ZZ) == g config.setup('GF_FACTOR_METHOD', 'shoup') assert gf_factor(f, p, ZZ) == g f, p = gf_from_dict({32: ZZ(8), 0: ZZ(5)}, 11, ZZ), 11 g = (8, [([1, 3], 1), ([1, 8], 1), ([1, 0, 9], 1), ([1, 2, 2], 1), ([1, 9, 2], 1), ([1, 0, 5, 0, 7], 1), ([1, 0, 6, 0, 7], 1), ([1, 0, 0, 0, 1, 0, 0, 0, 6], 1), ([1, 0, 0, 0, 10, 0, 0, 0, 6], 1)]) config.setup('GF_FACTOR_METHOD', 'berlekamp') assert gf_factor(f, p, ZZ) == g config.setup('GF_FACTOR_METHOD', 'zassenhaus') assert gf_factor(f, p, ZZ) == g config.setup('GF_FACTOR_METHOD', 'shoup') assert gf_factor(f, p, ZZ) == g f, p = gf_from_dict({63: ZZ(8), 0: ZZ(5)}, 11, ZZ), 11 g = (8, [([1, 7], 1), ([1, 4, 5], 1), ([1, 6, 8, 2], 1), ([1, 9, 9, 2], 1), ([1, 0, 0, 9, 0, 0, 4], 1), ([1, 2, 0, 8, 4, 6, 4], 1), ([1, 2, 3, 8, 0, 6, 4], 1), ([1, 2, 6, 0, 8, 4, 4], 1), ([1, 3, 3, 1, 6, 8, 4], 1), ([1, 5, 6, 0, 8, 6, 4], 1), ([1, 6, 2, 7, 9, 8, 4], 1), ([1, 10, 4, 7, 10, 7, 4], 1), ([1, 10, 10, 1, 4, 9, 4], 1)]) config.setup('GF_FACTOR_METHOD', 'berlekamp') assert gf_factor(f, p, ZZ) == g config.setup('GF_FACTOR_METHOD', 'zassenhaus') assert gf_factor(f, p, ZZ) == g config.setup('GF_FACTOR_METHOD', 'shoup') assert gf_factor(f, p, ZZ) == g # Gathen polynomials: x**n + x + 1 (mod p > 2**n * pi) p = ZZ(nextprime(int((2**15 * pi).evalf()))) f = gf_from_dict({15: 1, 1: 1, 0: 1}, p, ZZ) assert gf_sqf_p(f, p, ZZ) is True g = (1, [([1, 22730, 68144], 1), ([1, 81553, 77449, 86810, 4724], 1), ([1, 86276, 56779, 14859, 31575], 1), ([1, 15347, 95022, 84569, 94508, 92335], 1)]) config.setup('GF_FACTOR_METHOD', 'zassenhaus') assert gf_factor(f, p, ZZ) == g config.setup('GF_FACTOR_METHOD', 'shoup') assert gf_factor(f, p, ZZ) == g g = (1, [[1, 22730, 68144], [1, 81553, 77449, 86810, 4724], [1, 86276, 56779, 14859, 31575], [1, 15347, 95022, 84569, 94508, 92335]]) config.setup('GF_FACTOR_METHOD', 'zassenhaus') assert gf_factor_sqf(f, p, ZZ) == g config.setup('GF_FACTOR_METHOD', 'shoup') assert gf_factor_sqf(f, p, ZZ) == g # Shoup polynomials: f = a_0 x**n + a_1 x**(n-1) + ... + a_n # (mod p > 2**(n-2) * pi), where a_n = a_{n-1}**2 + 1, a_0 = 1 p = ZZ(nextprime(int((2**4 * pi).evalf()))) f = ZZ.map([1, 2, 5, 26, 41, 39, 38]) assert gf_sqf_p(f, p, ZZ) is True g = (1, [([1, 44, 26], 1), ([1, 11, 25, 18, 30], 1)]) config.setup('GF_FACTOR_METHOD', 'zassenhaus') assert gf_factor(f, p, ZZ) == g config.setup('GF_FACTOR_METHOD', 'shoup') assert gf_factor(f, p, ZZ) == g g = (1, [[1, 44, 26], [1, 11, 25, 18, 30]]) config.setup('GF_FACTOR_METHOD', 'zassenhaus') assert gf_factor_sqf(f, p, ZZ) == g config.setup('GF_FACTOR_METHOD', 'shoup') assert gf_factor_sqf(f, p, ZZ) == g config.setup('GF_FACTOR_METHOD', 'other') pytest.raises(KeyError, lambda: gf_factor([1, 1], 11, ZZ)) config.setup('GF_FACTOR_METHOD') # IPoly interface: R, t = ring("t", FF(11)) assert R.gf_factor_sqf(2 * t + 3) == (2, [t + 7])
def test_dmp_factor_list(): R, x, y = ring("x,y", ZZ) assert R.dmp_factor_list(0) == (ZZ(0), []) assert R.dmp_factor_list(7) == (7, []) R, x, y = ring("x,y", QQ) assert R.dmp_factor_list(0) == (QQ(0), []) assert R.dmp_factor_list(QQ(1, 7)) == (QQ(1, 7), []) Rt, t = ring("t", ZZ) R, x, y = ring("x,y", Rt) assert R.dmp_factor_list(0) == (0, []) assert R.dmp_factor_list(7) == (ZZ(7), []) Rt, t = ring("t", QQ) R, x, y = ring("x,y", Rt) assert R.dmp_factor_list(0) == (0, []) assert R.dmp_factor_list(QQ(1, 7)) == (QQ(1, 7), []) R, x, y = ring("x,y", ZZ) assert R.dmp_factor_list_include(0) == [(0, 1)] assert R.dmp_factor_list_include(7) == [(7, 1)] R, *X = ring("x:200", ZZ) f, g = X[0]**2 + 2 * X[0] + 1, X[0] + 1 assert R.dmp_factor_list(f) == (1, [(g, 2)]) f, g = X[-1]**2 + 2 * X[-1] + 1, X[-1] + 1 assert R.dmp_factor_list(f) == (1, [(g, 2)]) R, x = ring("x", ZZ) assert R.dmp_factor_list(x**2 + 2 * x + 1) == (1, [(x + 1, 2)]) R, x = ring("x", QQ) assert R.dmp_factor_list(x**2 / 2 + x + QQ(1, 2)) == (QQ(1, 2), [(x + 1, 2)]) R, x, y = ring("x,y", ZZ) assert R.dmp_factor_list(x**2 + 2 * x + 1) == (1, [(x + 1, 2)]) R, x, y = ring("x,y", QQ) assert R.dmp_factor_list(x**2 / 2 + x + QQ(1, 2)) == (QQ(1, 2), [(x + 1, 2)]) R, x, y = ring("x,y", ZZ) f = 4 * x**2 * y + 4 * x * y**2 assert R.dmp_factor_list(f) == \ (4, [(y, 1), (x, 1), (x + y, 1)]) assert R.dmp_factor_list_include(f) == \ [(4*y, 1), (x, 1), (x + y, 1)] R, x, y = ring("x,y", QQ) f = x**2 * y / 2 + x * y**2 / 2 assert R.dmp_factor_list(f) == \ (QQ(1, 2), [(y, 1), (x, 1), (x + y, 1)]) R, x, y = ring("x,y", RR) f = 2.0 * x**2 - 8.0 * y**2 assert R.dmp_factor_list(f) == \ (RR(2.0), [(1.0*x - 2.0*y, 1), (1.0*x + 2.0*y, 1)]) f = 6.7225336055071 * x**2 * y**2 - 10.6463972754741 * x * y - 0.33469524022264 coeff, factors = R.dmp_factor_list(f) assert coeff == RR(1.0) and len(factors) == 1 and factors[0][0].almosteq( f, 1e-10) and factors[0][1] == 1 # issue diofant/diofant#238 R, x, y, z = ring("x,y,z", RR) f = x * y + x * z + 0.1 * y + 0.1 * z assert R.dmp_factor_list(f) == (10.0, [(0.1 * y + 0.1 * z, 1), (x + 0.1, 1)]) f = 0.25 * x**2 + 1.0 * x * y * z + 1.0 * y**2 * z**2 assert R.dmp_factor_list(f) == (4.0, [(0.25 * x + 0.5 * y * z, 2)]) Rt, t = ring("t", ZZ) R, x, y = ring("x,y", Rt) f = 4 * t * x**2 + 4 * t**2 * x assert R.dmp_factor_list(f) == \ (4*t, [(x, 1), (x + t, 1)]) Rt, t = ring("t", QQ) R, x, y = ring("x,y", Rt) f = t * x**2 / 2 + t**2 * x / 2 assert R.dmp_factor_list(f) == (t / 2, [(x, 1), (x + t, 1)]) R, x, y = ring("x,y", FF(2)) pytest.raises(NotImplementedError, lambda: R.dmp_factor_list(x**2 + y**2)) R, x, y = ring("x,y", EX) pytest.raises(DomainError, lambda: R.dmp_factor_list(EX(sin(1))))
def test_Domain_unify_composite(): assert unify(ZZ.poly_ring(x), ZZ) == ZZ.poly_ring(x) assert unify(ZZ.poly_ring(x), QQ) == QQ.poly_ring(x) assert unify(QQ.poly_ring(x), ZZ) == QQ.poly_ring(x) assert unify(QQ.poly_ring(x), QQ) == QQ.poly_ring(x) assert unify(ZZ, ZZ.poly_ring(x)) == ZZ.poly_ring(x) assert unify(QQ, ZZ.poly_ring(x)) == QQ.poly_ring(x) assert unify(ZZ, QQ.poly_ring(x)) == QQ.poly_ring(x) assert unify(QQ, QQ.poly_ring(x)) == QQ.poly_ring(x) assert unify(ZZ.poly_ring(x, y), ZZ) == ZZ.poly_ring(x, y) assert unify(ZZ.poly_ring(x, y), QQ) == QQ.poly_ring(x, y) assert unify(QQ.poly_ring(x, y), ZZ) == QQ.poly_ring(x, y) assert unify(QQ.poly_ring(x, y), QQ) == QQ.poly_ring(x, y) assert unify(ZZ, ZZ.poly_ring(x, y)) == ZZ.poly_ring(x, y) assert unify(QQ, ZZ.poly_ring(x, y)) == QQ.poly_ring(x, y) assert unify(ZZ, QQ.poly_ring(x, y)) == QQ.poly_ring(x, y) assert unify(QQ, QQ.poly_ring(x, y)) == QQ.poly_ring(x, y) assert unify(ZZ.frac_field(x), ZZ) == ZZ.frac_field(x) assert unify(ZZ.frac_field(x), QQ) == QQ.frac_field(x) assert unify(QQ.frac_field(x), ZZ) == QQ.frac_field(x) assert unify(QQ.frac_field(x), QQ) == QQ.frac_field(x) assert unify(ZZ, ZZ.frac_field(x)) == ZZ.frac_field(x) assert unify(QQ, ZZ.frac_field(x)) == QQ.frac_field(x) assert unify(ZZ, QQ.frac_field(x)) == QQ.frac_field(x) assert unify(QQ, QQ.frac_field(x)) == QQ.frac_field(x) assert unify(ZZ.frac_field(x, y), ZZ) == ZZ.frac_field(x, y) assert unify(ZZ.frac_field(x, y), QQ) == QQ.frac_field(x, y) assert unify(QQ.frac_field(x, y), ZZ) == QQ.frac_field(x, y) assert unify(QQ.frac_field(x, y), QQ) == QQ.frac_field(x, y) assert unify(ZZ, ZZ.frac_field(x, y)) == ZZ.frac_field(x, y) assert unify(QQ, ZZ.frac_field(x, y)) == QQ.frac_field(x, y) assert unify(ZZ, QQ.frac_field(x, y)) == QQ.frac_field(x, y) assert unify(QQ, QQ.frac_field(x, y)) == QQ.frac_field(x, y) assert unify(ZZ.poly_ring(x), ZZ.poly_ring(x)) == ZZ.poly_ring(x) assert unify(ZZ.poly_ring(x), QQ.poly_ring(x)) == QQ.poly_ring(x) assert unify(QQ.poly_ring(x), ZZ.poly_ring(x)) == QQ.poly_ring(x) assert unify(QQ.poly_ring(x), QQ.poly_ring(x)) == QQ.poly_ring(x) assert unify(ZZ.poly_ring(x, y), ZZ.poly_ring(x)) == ZZ.poly_ring(x, y) assert unify(ZZ.poly_ring(x, y), QQ.poly_ring(x)) == QQ.poly_ring(x, y) assert unify(QQ.poly_ring(x, y), ZZ.poly_ring(x)) == QQ.poly_ring(x, y) assert unify(QQ.poly_ring(x, y), QQ.poly_ring(x)) == QQ.poly_ring(x, y) assert unify(ZZ.poly_ring(x), ZZ.poly_ring(x, y)) == ZZ.poly_ring(x, y) assert unify(ZZ.poly_ring(x), QQ.poly_ring(x, y)) == QQ.poly_ring(x, y) assert unify(QQ.poly_ring(x), ZZ.poly_ring(x, y)) == QQ.poly_ring(x, y) assert unify(QQ.poly_ring(x), QQ.poly_ring(x, y)) == QQ.poly_ring(x, y) assert unify(ZZ.poly_ring(x, y), ZZ.poly_ring(x, z)) == ZZ.poly_ring(x, y, z) assert unify(ZZ.poly_ring(x, y), QQ.poly_ring(x, z)) == QQ.poly_ring(x, y, z) assert unify(QQ.poly_ring(x, y), ZZ.poly_ring(x, z)) == QQ.poly_ring(x, y, z) assert unify(QQ.poly_ring(x, y), QQ.poly_ring(x, z)) == QQ.poly_ring(x, y, z) assert unify(ZZ.frac_field(x), ZZ.frac_field(x)) == ZZ.frac_field(x) assert unify(ZZ.frac_field(x), QQ.frac_field(x)) == QQ.frac_field(x) assert unify(QQ.frac_field(x), ZZ.frac_field(x)) == QQ.frac_field(x) assert unify(QQ.frac_field(x), QQ.frac_field(x)) == QQ.frac_field(x) assert unify(ZZ.frac_field(x, y), ZZ.frac_field(x)) == ZZ.frac_field(x, y) assert unify(ZZ.frac_field(x, y), QQ.frac_field(x)) == QQ.frac_field(x, y) assert unify(QQ.frac_field(x, y), ZZ.frac_field(x)) == QQ.frac_field(x, y) assert unify(QQ.frac_field(x, y), QQ.frac_field(x)) == QQ.frac_field(x, y) assert unify(ZZ.frac_field(x), ZZ.frac_field(x, y)) == ZZ.frac_field(x, y) assert unify(ZZ.frac_field(x), QQ.frac_field(x, y)) == QQ.frac_field(x, y) assert unify(QQ.frac_field(x), ZZ.frac_field(x, y)) == QQ.frac_field(x, y) assert unify(QQ.frac_field(x), QQ.frac_field(x, y)) == QQ.frac_field(x, y) assert unify(ZZ.frac_field(x, y), ZZ.frac_field(x, z)) == ZZ.frac_field(x, y, z) assert unify(ZZ.frac_field(x, y), QQ.frac_field(x, z)) == QQ.frac_field(x, y, z) assert unify(QQ.frac_field(x, y), ZZ.frac_field(x, z)) == QQ.frac_field(x, y, z) assert unify(QQ.frac_field(x, y), QQ.frac_field(x, z)) == QQ.frac_field(x, y, z) assert unify(ZZ.poly_ring(x), ZZ.frac_field(x)) == ZZ.frac_field(x) assert unify(ZZ.poly_ring(x), QQ.frac_field(x)) == ZZ.frac_field(x) assert unify(QQ.poly_ring(x), ZZ.frac_field(x)) == ZZ.frac_field(x) assert unify(QQ.poly_ring(x), QQ.frac_field(x)) == QQ.frac_field(x) assert unify(ZZ.poly_ring(x, y), ZZ.frac_field(x)) == ZZ.frac_field(x, y) assert unify(ZZ.poly_ring(x, y), QQ.frac_field(x)) == ZZ.frac_field(x, y) assert unify(QQ.poly_ring(x, y), ZZ.frac_field(x)) == ZZ.frac_field(x, y) assert unify(QQ.poly_ring(x, y), QQ.frac_field(x)) == QQ.frac_field(x, y) assert unify(ZZ.poly_ring(x), ZZ.frac_field(x, y)) == ZZ.frac_field(x, y) assert unify(ZZ.poly_ring(x), QQ.frac_field(x, y)) == ZZ.frac_field(x, y) assert unify(QQ.poly_ring(x), ZZ.frac_field(x, y)) == ZZ.frac_field(x, y) assert unify(QQ.poly_ring(x), QQ.frac_field(x, y)) == QQ.frac_field(x, y) assert unify(ZZ.poly_ring(x, y), ZZ.frac_field(x, z)) == ZZ.frac_field(x, y, z) assert unify(ZZ.poly_ring(x, y), QQ.frac_field(x, z)) == ZZ.frac_field(x, y, z) assert unify(QQ.poly_ring(x, y), ZZ.frac_field(x, z)) == ZZ.frac_field(x, y, z) assert unify(QQ.poly_ring(x, y), QQ.frac_field(x, z)) == QQ.frac_field(x, y, z) assert unify(ZZ.frac_field(x), ZZ.poly_ring(x)) == ZZ.frac_field(x) assert unify(ZZ.frac_field(x), QQ.poly_ring(x)) == ZZ.frac_field(x) assert unify(QQ.frac_field(x), ZZ.poly_ring(x)) == ZZ.frac_field(x) assert unify(QQ.frac_field(x), QQ.poly_ring(x)) == QQ.frac_field(x) assert unify(ZZ.frac_field(x, y), ZZ.poly_ring(x)) == ZZ.frac_field(x, y) assert unify(ZZ.frac_field(x, y), QQ.poly_ring(x)) == ZZ.frac_field(x, y) assert unify(QQ.frac_field(x, y), ZZ.poly_ring(x)) == ZZ.frac_field(x, y) assert unify(QQ.frac_field(x, y), QQ.poly_ring(x)) == QQ.frac_field(x, y) assert unify(ZZ.frac_field(x), ZZ.poly_ring(x, y)) == ZZ.frac_field(x, y) assert unify(ZZ.frac_field(x), QQ.poly_ring(x, y)) == ZZ.frac_field(x, y) assert unify(QQ.frac_field(x), ZZ.poly_ring(x, y)) == ZZ.frac_field(x, y) assert unify(QQ.frac_field(x), QQ.poly_ring(x, y)) == QQ.frac_field(x, y) assert unify(ZZ.frac_field(x, y), ZZ.poly_ring(x, z)) == ZZ.frac_field(x, y, z) assert unify(ZZ.frac_field(x, y), QQ.poly_ring(x, z)) == ZZ.frac_field(x, y, z) assert unify(QQ.frac_field(x, y), ZZ.poly_ring(x, z)) == ZZ.frac_field(x, y, z) assert unify(QQ.frac_field(x, y), QQ.poly_ring(x, z)) == QQ.frac_field(x, y, z)
def test_Domain_unify_with_symbols(): pytest.raises(UnificationFailed, lambda: ZZ.poly_ring(x, y).unify(ZZ, (y, z))) pytest.raises(UnificationFailed, lambda: ZZ.unify(ZZ.poly_ring(x, y), (y, z)))
def test_FractionField(): assert str(ZZ.frac_field("x")) == "ZZ(x)" assert str(QQ.frac_field("x", "y", order=grlex)) == "QQ(x,y)" assert str(ZZ.poly_ring("t").frac_field("x", "y", "z")) == "ZZ[t](x,y,z)"
def test_dmp_ground(): assert dmp_ground(ZZ(0), 2) == [[[]]] assert dmp_ground(ZZ(7), -1) == ZZ(7) assert dmp_ground(ZZ(7), 0) == [ZZ(7)] assert dmp_ground(ZZ(7), 2) == [[[ZZ(7)]]]
def test_dup_factor_list(): R, x = ring("x", ZZ) assert R.dup_factor_list(0) == (0, []) assert R.dup_factor_list(7) == (7, []) R, x = ring("x", QQ) assert R.dup_factor_list(0) == (0, []) assert R.dup_factor_list(QQ(1, 7)) == (QQ(1, 7), []) R, x = ring("x", ZZ.poly_ring('t')) assert R.dup_factor_list(0) == (0, []) assert R.dup_factor_list(7) == (7, []) R, x = ring("x", QQ.poly_ring('t')) assert R.dup_factor_list(0) == (0, []) assert R.dup_factor_list(QQ(1, 7)) == (QQ(1, 7), []) R, x = ring("x", ZZ) assert R.dup_factor_list_include(0) == [(0, 1)] assert R.dup_factor_list_include(7) == [(7, 1)] assert R.dup_factor_list(x**2 + 2 * x + 1) == (1, [(x + 1, 2)]) assert R.dup_factor_list_include(x**2 + 2 * x + 1) == [(x + 1, 2)] # issue sympy/sympy#8037 assert R.dup_factor_list(6 * x**2 - 5 * x - 6) == (1, [(2 * x - 3, 1), (3 * x + 2, 1)]) R, x = ring("x", QQ) assert R.dup_factor_list(x**2 / 2 + x + QQ(1, 2)) == (QQ(1, 2), [(x + 1, 2)]) R, x = ring("x", FF(2)) assert R.dup_factor_list(x**2 + 1) == (1, [(x + 1, 2)]) R, x = ring("x", RR) assert R.dup_factor_list(1.0 * x**2 + 2.0 * x + 1.0) == (1.0, [ (1.0 * x + 1.0, 2) ]) assert R.dup_factor_list(2.0 * x**2 + 4.0 * x + 2.0) == (2.0, [ (1.0 * x + 1.0, 2) ]) f = 6.7225336055071 * x**2 - 10.6463972754741 * x - 0.33469524022264 coeff, factors = R.dup_factor_list(f) assert coeff == RR(1.0) and len(factors) == 1 and factors[0][0].almosteq( f, 1e-10) and factors[0][1] == 1 # issue diofant/diofant#238 f = 0.1 * x**2 + 1.1 * x + 1.0 assert R.dup_factor_list(f) == (10.0, [(0.1 * x + 0.1, 1), (0.1 * x + 1.0, 1)]) f = 0.25 + 1.0 * x + 1.0 * x**2 assert R.dup_factor_list(f) == (4.0, [(0.25 + 0.5 * x, 2)]) Rt, t = ring("t", ZZ) R, x = ring("x", Rt) f = 4 * t * x**2 + 4 * t**2 * x assert R.dup_factor_list(f) == \ (4*t, [(x, 1), (x + t, 1)]) Rt, t = ring("t", QQ) R, x = ring("x", Rt) f = t * x**2 / 2 + t**2 * x / 2 assert R.dup_factor_list(f) == (t / 2, [(x, 1), (x + t, 1)]) R, x = ring("x", QQ.algebraic_field(I)) f = x**4 + 2 * x**2 assert R.dup_factor_list(f) == (R.domain(1), [(x, 2), (x**2 + 2, 1)]) R, x = ring("x", EX) pytest.raises(DomainError, lambda: R.dup_factor_list(EX(sin(1))))
def test_Domain__algebraic_field(): alg = ZZ.algebraic_field(sqrt(3)) assert alg.minpoly == Poly(x**2 - 3) assert alg.domain == QQ assert alg.from_expr(sqrt(3)).denominator == 1 assert alg.from_expr(2*sqrt(3)).denominator == 1 assert alg.from_expr(sqrt(3)/2).denominator == 2 assert alg([QQ(7, 38), QQ(3, 2)]).denominator == 38 alg = QQ.algebraic_field(sqrt(2)) assert alg.minpoly == Poly(x**2 - 2) assert alg.domain == QQ alg = QQ.algebraic_field(sqrt(2), sqrt(3)) assert alg.minpoly == Poly(x**4 - 10*x**2 + 1) assert alg.domain == QQ assert alg.is_nonpositive(alg([-1, 1])) is True assert alg.is_nonnegative(alg([2, -1])) is True assert alg(1).numerator == alg(1) assert alg.from_expr(sqrt(3)/2).numerator == alg.from_expr(2*sqrt(3)) assert alg.from_expr(sqrt(3)/2).denominator == 4 pytest.raises(DomainError, lambda: AlgebraicField(ZZ, sqrt(2))) assert alg.characteristic == 0 assert alg.is_RealAlgebraicField is True assert int(alg(2)) == 2 assert int(alg.from_expr(Rational(3, 2))) == 1 pytest.raises(TypeError, lambda: int(alg([1, 1]))) alg = QQ.algebraic_field(I) assert alg.algebraic_field(I) == alg assert alg.is_RealAlgebraicField is False alg = QQ.algebraic_field(sqrt(2)).algebraic_field(sqrt(3)) assert alg.minpoly == Poly(x**2 - 3, x, domain=QQ.algebraic_field(sqrt(2))) # issue sympy/sympy#14476 assert QQ.algebraic_field(Rational(1, 7)) is QQ alg = QQ.algebraic_field(sqrt(2)).algebraic_field(I) assert alg.from_expr(2*sqrt(2) + I/3) == alg([alg.domain(1)/3, alg.domain(2*sqrt(2))]) alg2 = QQ.algebraic_field(sqrt(2)) assert alg2.from_expr(sqrt(2)) == alg2.convert(alg.from_expr(sqrt(2))) eq = -x**3 + 2*x**2 + 3*x - 2 rs = roots(eq, multiple=True) alg = QQ.algebraic_field(rs[0]) assert alg.is_RealAlgebraicField alg1 = QQ.algebraic_field(I) alg2 = QQ.algebraic_field(sqrt(2)).algebraic_field(I) assert alg1 != alg2 alg3 = QQ.algebraic_field(RootOf(4*x**7 + x - 1, 0)) assert alg3.is_RealAlgebraicField assert 2.772 > alg3.unit > 2.771 alg4 = QQ.algebraic_field(sqrt(2) + I) assert alg4.convert(alg2.unit) == alg4.from_expr(I)
def test_Ring(): assert ZZ(3).numerator == 3 assert ZZ(3).denominator == 1
def test_Domain_preprocess(): assert Domain.preprocess(ZZ) == ZZ assert Domain.preprocess(QQ) == QQ assert Domain.preprocess(EX) == EX assert Domain.preprocess(FF(2)) == FF(2) assert Domain.preprocess(ZZ.poly_ring(x, y)) == ZZ.poly_ring(x, y) assert Domain.preprocess('Z') == ZZ assert Domain.preprocess('Q') == QQ assert Domain.preprocess('ZZ') == ZZ assert Domain.preprocess('QQ') == QQ assert Domain.preprocess('EX') == EX assert Domain.preprocess('FF(23)') == FF(23) assert Domain.preprocess('GF(23)') == GF(23) pytest.raises(OptionError, lambda: Domain.preprocess('Z[]')) assert Domain.preprocess('Z[x]') == ZZ.poly_ring(x) assert Domain.preprocess('Q[x]') == QQ.poly_ring(x) assert Domain.preprocess('ZZ[x]') == ZZ.poly_ring(x) assert Domain.preprocess('QQ[x]') == QQ.poly_ring(x) assert Domain.preprocess('Z[x,y]') == ZZ.poly_ring(x, y) assert Domain.preprocess('Q[x,y]') == QQ.poly_ring(x, y) assert Domain.preprocess('ZZ[x,y]') == ZZ.poly_ring(x, y) assert Domain.preprocess('QQ[x,y]') == QQ.poly_ring(x, y) pytest.raises(OptionError, lambda: Domain.preprocess('Z()')) assert Domain.preprocess('Z(x)') == ZZ.frac_field(x) assert Domain.preprocess('Q(x)') == QQ.frac_field(x) assert Domain.preprocess('ZZ(x)') == ZZ.frac_field(x) assert Domain.preprocess('QQ(x)') == QQ.frac_field(x) assert Domain.preprocess('Z(x,y)') == ZZ.frac_field(x, y) assert Domain.preprocess('Q(x,y)') == QQ.frac_field(x, y) assert Domain.preprocess('ZZ(x,y)') == ZZ.frac_field(x, y) assert Domain.preprocess('QQ(x,y)') == QQ.frac_field(x, y) assert Domain.preprocess('Q<I>') == QQ.algebraic_field(I) assert Domain.preprocess('QQ<I>') == QQ.algebraic_field(I) assert Domain.preprocess('Q<sqrt(2), I>') == QQ.algebraic_field(sqrt(2), I) assert Domain.preprocess('QQ<sqrt(2), I>') == QQ.algebraic_field( sqrt(2), I) pytest.raises(OptionError, lambda: Domain.preprocess('abc')) assert Domain.preprocess('RR') == RR assert Domain.preprocess('RR_5') == RealField(prec=5) assert Domain.preprocess('CC') == CC assert Domain.preprocess('CC_5') == ComplexField(prec=5) pytest.raises(OptionError, lambda: Domain.preprocess(()))
def test_gf_edf(): f = ZZ.map([1, 1, 0, 1, 2]) g = ZZ.map([[1, 0, 1], [1, 1, 2]]) assert gf_edf_zassenhaus(f, 2, 3, ZZ) == g assert gf_edf_shoup(f, 2, 3, ZZ) == g
def test_Domain__algebraic_field(): alg = ZZ.algebraic_field(sqrt(3)) assert alg.minpoly == Poly(x**2 - 3) assert alg.domain == QQ assert alg.from_expr(sqrt(3)).denominator == 1 assert alg.from_expr(2 * sqrt(3)).denominator == 1 assert alg.from_expr(sqrt(3) / 2).denominator == 2 assert alg([QQ(7, 38), QQ(3, 2)]).denominator == 38 alg = QQ.algebraic_field(sqrt(2)) assert alg.minpoly == Poly(x**2 - 2) assert alg.domain == QQ alg = QQ.algebraic_field(sqrt(2), sqrt(3)) assert alg.minpoly == Poly(x**4 - 10 * x**2 + 1) assert alg.domain == QQ assert alg(1).numerator == alg(1) assert alg.from_expr(sqrt(3) / 2).numerator == alg.from_expr(2 * sqrt(3)) assert alg.from_expr(sqrt(3) / 2).denominator == 4 pytest.raises(DomainError, lambda: AlgebraicField(ZZ, sqrt(2))) assert alg.characteristic == 0 assert alg.is_RealAlgebraicField is True assert int(alg(2)) == 2 assert int(alg.from_expr(Rational(3, 2))) == 1 alg = QQ.algebraic_field(I) assert alg.algebraic_field(I) == alg assert alg.is_RealAlgebraicField is False pytest.raises(TypeError, lambda: int(alg([1, 1]))) alg = QQ.algebraic_field(sqrt(2)).algebraic_field(sqrt(3)) assert alg.minpoly == Poly(x**2 - 3, x, domain=QQ.algebraic_field(sqrt(2))) # issue sympy/sympy#14476 assert QQ.algebraic_field(Rational(1, 7)) is QQ alg = QQ.algebraic_field(sqrt(2)).algebraic_field(I) assert alg.from_expr(2 * sqrt(2) + I / 3) == alg( [alg.domain([1]) / 3, alg.domain([2, 0])]) alg2 = QQ.algebraic_field(sqrt(2)) assert alg2.from_expr(sqrt(2)) == alg2.convert(alg.from_expr(sqrt(2))) eq = -x**3 + 2 * x**2 + 3 * x - 2 rs = roots(eq, multiple=True) alg = QQ.algebraic_field(rs[0]) assert alg.is_RealAlgebraicField alg1 = QQ.algebraic_field(I) alg2 = QQ.algebraic_field(sqrt(2)).algebraic_field(I) assert alg1 != alg2 alg3 = QQ.algebraic_field(RootOf(4 * x**7 + x - 1, 0)) assert alg3.is_RealAlgebraicField assert int(alg3.unit) == 2 assert 2.772 > alg3.unit > 2.771 assert int(alg3([3, 17, 11, -1, 2])) == 622 assert int( alg3([ 1, QQ(-11, 4), QQ(125326976730518, 44208605852241), QQ(-16742151878022, 12894796053515), QQ(2331359268715, 10459004949272) ])) == 18 alg4 = QQ.algebraic_field(sqrt(2) + I) assert alg4.convert(alg2.unit) == alg4.from_expr(I)
def test_dmp_factor_list(): R, x = ring("x", ZZ) assert R.dmp_factor_list(0) == (0, []) assert R.dmp_factor_list(7) == (7, []) R, x = ring("x", QQ) assert R.dmp_factor_list(0) == (0, []) assert R.dmp_factor_list(QQ(1, 7)) == (QQ(1, 7), []) R, x = ring("x", ZZ.poly_ring('t')) assert R.dmp_factor_list(0) == (0, []) assert R.dmp_factor_list(7) == (7, []) R, x = ring("x", QQ.poly_ring('t')) assert R.dmp_factor_list(0) == (0, []) assert R.dmp_factor_list(QQ(1, 7)) == (QQ(1, 7), []) R, x = ring("x", ZZ) assert R.dmp_factor_list(x**2 + 2*x + 1) == (1, [(x + 1, 2)]) # issue sympy/sympy#8037 assert R.dmp_factor_list(6*x**2 - 5*x - 6) == (1, [(2*x - 3, 1), (3*x + 2, 1)]) R, x = ring("x", QQ) assert R.dmp_factor_list(x**2/2 + x + QQ(1, 2)) == (QQ(1, 2), [(x + 1, 2)]) R, x = ring("x", FF(2)) assert R.dmp_factor_list(x**2 + 1) == (1, [(x + 1, 2)]) R, x = ring("x", RR) assert R.dmp_factor_list(1.0*x**2 + 2.0*x + 1.0) == (1.0, [(1.0*x + 1.0, 2)]) assert R.dmp_factor_list(2.0*x**2 + 4.0*x + 2.0) == (2.0, [(1.0*x + 1.0, 2)]) f = 6.7225336055071*x**2 - 10.6463972754741*x - 0.33469524022264 coeff, factors = R.dmp_factor_list(f) assert coeff == RR(1.0) and len(factors) == 1 and factors[0][0].almosteq(f, 1e-10) and factors[0][1] == 1 # issue diofant/diofant#238 f = 0.1*x**2 + 1.1*x + 1.0 assert R.dmp_factor_list(f) == (10.0, [(0.1*x + 0.1, 1), (0.1*x + 1.0, 1)]) f = 0.25 + 1.0*x + 1.0*x**2 assert R.dmp_factor_list(f) == (4.0, [(0.25 + 0.5*x, 2)]) Rt, t = ring("t", ZZ) R, x = ring("x", Rt) f = 4*t*x**2 + 4*t**2*x assert R.dmp_factor_list(f) == (4*t, [(x, 1), (x + t, 1)]) Rt, t = ring("t", QQ) R, x = ring("x", Rt) f = t*x**2/2 + t**2*x/2 assert R.dmp_factor_list(f) == (t/2, [(x, 1), (x + t, 1)]) R, x = ring("x", QQ.algebraic_field(I)) f = x**4 + 2*x**2 assert R.dmp_factor_list(f) == (R.domain(1), [(x, 2), (x**2 + 2, 1)]) R, x = ring("x", EX) pytest.raises(DomainError, lambda: R.dmp_factor_list(EX(sin(1)))) R, x, y = ring("x,y", ZZ) assert R.dmp_factor_list(0) == (ZZ(0), []) assert R.dmp_factor_list(7) == (7, []) R, x, y = ring("x,y", QQ) assert R.dmp_factor_list(0) == (QQ(0), []) assert R.dmp_factor_list(QQ(1, 7)) == (QQ(1, 7), []) Rt, t = ring("t", ZZ) R, x, y = ring("x,y", Rt) assert R.dmp_factor_list(0) == (0, []) assert R.dmp_factor_list(7) == (ZZ(7), []) Rt, t = ring("t", QQ) R, x, y = ring("x,y", Rt) assert R.dmp_factor_list(0) == (0, []) assert R.dmp_factor_list(QQ(1, 7)) == (QQ(1, 7), []) R, *X = ring("x:200", ZZ) f, g = X[0]**2 + 2*X[0] + 1, X[0] + 1 assert R.dmp_factor_list(f) == (1, [(g, 2)]) f, g = X[-1]**2 + 2*X[-1] + 1, X[-1] + 1 assert R.dmp_factor_list(f) == (1, [(g, 2)]) R, x = ring("x", ZZ) assert R.dmp_factor_list(x**2 + 2*x + 1) == (1, [(x + 1, 2)]) R, x = ring("x", QQ) assert R.dmp_factor_list(x**2/2 + x + QQ(1, 2)) == (QQ(1, 2), [(x + 1, 2)]) R, x, y = ring("x,y", ZZ) assert R.dmp_factor_list(x**2 + 2*x + 1) == (1, [(x + 1, 2)]) R, x, y = ring("x,y", QQ) assert R.dmp_factor_list(x**2/2 + x + QQ(1, 2)) == (QQ(1, 2), [(x + 1, 2)]) R, x, y = ring("x,y", ZZ) f = 4*x**2*y + 4*x*y**2 assert R.dmp_factor_list(f) == (4, [(y, 1), (x, 1), (x + y, 1)]) R, x, y = ring("x,y", QQ) f = x**2*y/2 + x*y**2/2 assert R.dmp_factor_list(f) == (QQ(1, 2), [(y, 1), (x, 1), (x + y, 1)]) R, x, y = ring("x,y", RR) f = 2.0*x**2 - 8.0*y**2 assert R.dmp_factor_list(f) == (RR(2.0), [(1.0*x - 2.0*y, 1), (1.0*x + 2.0*y, 1)]) f = 6.7225336055071*x**2*y**2 - 10.6463972754741*x*y - 0.33469524022264 coeff, factors = R.dmp_factor_list(f) assert coeff == RR(1.0) and len(factors) == 1 and factors[0][0].almosteq(f, 1e-10) and factors[0][1] == 1 # issue diofant/diofant#238 R, x, y, z = ring("x,y,z", RR) f = x*y + x*z + 0.1*y + 0.1*z assert R.dmp_factor_list(f) == (10.0, [(0.1*y + 0.1*z, 1), (x + 0.1, 1)]) f = 0.25*x**2 + 1.0*x*y*z + 1.0*y**2*z**2 assert R.dmp_factor_list(f) == (4.0, [(0.25*x + 0.5*y*z, 2)]) Rt, t = ring("t", ZZ) R, x, y = ring("x,y", Rt) f = 4*t*x**2 + 4*t**2*x assert R.dmp_factor_list(f) == (4*t, [(x, 1), (x + t, 1)]) Rt, t = ring("t", QQ) R, x, y = ring("x,y", Rt) f = t*x**2/2 + t**2*x/2 assert R.dmp_factor_list(f) == (t/2, [(x, 1), (x + t, 1)]) R, x, y = ring("x,y", FF(2)) pytest.raises(NotImplementedError, lambda: R.dmp_factor_list(x**2 + y**2)) R, x, y = ring("x,y", EX) pytest.raises(DomainError, lambda: R.dmp_factor_list(EX(sin(1))))
def test_sympyissue_11538(): assert construct_domain(E)[0] == ZZ.poly_ring(E) assert (construct_domain(x**2 + 2*x + E) == (ZZ.poly_ring(x, E), ZZ.poly_ring(x, E)(x**2 + 2*x + E))) assert (construct_domain(x + y + GoldenRatio) == (EX, EX(x + y + GoldenRatio)))
def test_PolynomialRing___init__(): assert len(PolynomialRing(ZZ, "x,y,z").gens) == 3 assert len(ZZ.poly_ring(x).gens) == 1 assert len(ZZ.poly_ring("x", "y", "z").gens) == 3 assert len(ZZ.poly_ring(x, y, z).gens) == 3 pytest.raises(GeneratorsNeeded, lambda: ZZ.poly_ring()) pytest.raises(GeneratorsError, lambda: ZZ.poly_ring(0)) assert ZZ.poly_ring(t).poly_ring("x").domain == ZZ.poly_ring(t) assert PolynomialRing('ZZ[t]', "x").domain == ZZ.poly_ring(t) pytest.raises(GeneratorsError, lambda: ZZ.poly_ring("x").poly_ring("x")) _lex = Symbol("lex") assert PolynomialRing(ZZ, "x").order == lex assert PolynomialRing(ZZ, "x", _lex).order == lex assert PolynomialRing(ZZ, "x", 'lex').order == lex R1 = ZZ.poly_ring("x", "y") R2 = ZZ.poly_ring("x", "y") R3 = ZZ.poly_ring("x", "y", "z") assert R1.x == R1.gens[0] assert R1.y == R1.gens[1] assert R1.x == R2.x assert R1.y == R2.y assert R1.x != R3.x assert R1.y != R3.y R4 = ZZ.poly_ring("gens") assert type(R4.gens) is tuple pytest.raises(GeneratorsError, lambda: PolynomialRing(ZZ, {1: 2})) pytest.raises(GeneratorsError, lambda: PolynomialRing(ZZ, ["x", ["y"]]))
def test_gf_factor_sqf(): assert gf_factor_sqf([], 11, ZZ) == (0, []) assert gf_factor_sqf([1], 11, ZZ) == (1, []) assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]]) assert gf_factor_sqf([2, 3], 11, ZZ) == (2, [[1, 7]]) with config.using(gf_factor_method='berlekamp'): assert gf_factor_sqf([], 11, ZZ) == (0, []) assert gf_factor_sqf([1], 11, ZZ) == (1, []) assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]]) assert gf_factor_sqf([1, 0], 11, ZZ) == (1, [[1, 0]]) with config.using(gf_factor_method='zassenhaus'): assert gf_factor_sqf([], 11, ZZ) == (0, []) assert gf_factor_sqf([1], 11, ZZ) == (1, []) assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]]) assert gf_factor_sqf([1, 0], 11, ZZ) == (1, [[1, 0]]) with config.using(gf_factor_method='shoup'): assert gf_factor_sqf([], 11, ZZ) == (0, []) assert gf_factor_sqf([1], 11, ZZ) == (1, []) assert gf_factor_sqf([1, 1], 11, ZZ) == (1, [[1, 1]]) assert gf_factor_sqf([1, 0], 11, ZZ) == (1, [[1, 0]]) f, p = [1, 0, 0, 1, 0], 2 g = (1, [[1, 0], [1, 1], [1, 1, 1]]) with config.using(gf_factor_method='berlekamp'): assert gf_factor_sqf(f, p, ZZ) == g with config.using(gf_factor_method='zassenhaus'): assert gf_factor_sqf(f, p, ZZ) == g with config.using(gf_factor_method='shoup'): assert gf_factor_sqf(f, p, ZZ) == g # Gathen polynomials: x**n + x + 1 (mod p > 2**n * pi) p = ZZ(nextprime(int((2**15*pi)))) f = gf_from_dict({15: 1, 1: 1, 0: 1}, p, ZZ) g = (1, [[1, 22730, 68144], [1, 81553, 77449, 86810, 4724], [1, 86276, 56779, 14859, 31575], [1, 15347, 95022, 84569, 94508, 92335]]) with config.using(gf_factor_method='zassenhaus'): assert gf_factor_sqf(f, p, ZZ) == g with config.using(gf_factor_method='shoup'): assert gf_factor_sqf(f, p, ZZ) == g # Shoup polynomials: f = a_0 x**n + a_1 x**(n-1) + ... + a_n # (mod p > 2**(n-2) * pi), where a_n = a_{n-1}**2 + 1, a_0 = 1 p = ZZ(nextprime(int((2**4*pi)))) f = [1, 2, 5, 26, 41, 39, 38] g = (1, [[1, 44, 26], [1, 11, 25, 18, 30]]) with config.using(gf_factor_method='zassenhaus'): assert gf_factor_sqf(f, p, ZZ) == g with config.using(gf_factor_method='shoup'): assert gf_factor_sqf(f, p, ZZ) == g
def test_dmp_factor_list(): R, x = ring("x", ZZ) assert R(0).factor_list() == (0, []) assert R(7).factor_list() == (7, []) R, x = ring("x", QQ) assert R(0).factor_list() == (0, []) assert R(QQ(1, 7)).factor_list() == (QQ(1, 7), []) R, x = ring("x", ZZ.poly_ring('t')) assert R(0).factor_list() == (0, []) assert R(7).factor_list() == (7, []) R, x = ring("x", QQ.poly_ring('t')) assert R(0).factor_list() == (0, []) assert R(QQ(1, 7)).factor_list() == (QQ(1, 7), []) R, x = ring("x", ZZ) assert (x**2 + 2 * x + 1).factor_list() == (1, [(x + 1, 2)]) # issue sympy/sympy#8037 assert (6 * x**2 - 5 * x - 6).factor_list() == (1, [(2 * x - 3, 1), (3 * x + 2, 1)]) R, x = ring("x", QQ) assert (x**2 / 2 + x + QQ(1, 2)).factor_list() == (QQ(1, 2), [(x + 1, 2)]) R, x = ring("x", FF(2)) assert (x**2 + 1).factor_list() == (1, [(x + 1, 2)]) R, x = ring("x", RR) assert (1.0 * x**2 + 2.0 * x + 1.0).factor_list() == (1.0, [(1.0 * x + 1.0, 2)]) assert (2.0 * x**2 + 4.0 * x + 2.0).factor_list() == (2.0, [(1.0 * x + 1.0, 2)]) f = 6.7225336055071 * x**2 - 10.6463972754741 * x - 0.33469524022264 coeff, factors = f.factor_list() assert coeff == 1.0 and len(factors) == 1 and factors[0][0].almosteq( f, 1e-10) and factors[0][1] == 1 # issue diofant/diofant#238 f = 0.1 * x**2 + 1.1 * x + 1.0 assert f.factor_list() == (10.0, [(0.1 * x + 0.1, 1), (0.1 * x + 1.0, 1)]) f = 0.25 + 1.0 * x + 1.0 * x**2 assert f.factor_list() == (4.0, [(0.25 + 0.5 * x, 2)]) Rt, t = ring("t", ZZ) R, x = ring("x", Rt) f = 4 * t * x**2 + 4 * t**2 * x assert f.factor_list() == (4 * t, [(x, 1), (x + t, 1)]) Rt, t = ring("t", QQ) R, x = ring("x", Rt) f = t * x**2 / 2 + t**2 * x / 2 assert f.factor_list() == (t / 2, [(x, 1), (x + t, 1)]) R, x = ring("x", QQ.algebraic_field(I)) f = x**4 + 2 * x**2 assert f.factor_list() == (1, [(x, 2), (x**2 + 2, 1)]) R, x = ring("x", EX) pytest.raises(DomainError, lambda: R(EX(sin(1))).factor_list()) R, x, y = ring("x,y", ZZ) assert R(0).factor_list() == (0, []) assert R(7).factor_list() == (7, []) R, x, y = ring("x,y", QQ) assert R(0).factor_list() == (0, []) assert R(QQ(1, 7)).factor_list() == (QQ(1, 7), []) Rt, t = ring("t", ZZ) R, x, y = ring("x,y", Rt) assert R(0).factor_list() == (0, []) assert R(7).factor_list() == (7, []) Rt, t = ring("t", QQ) R, x, y = ring("x,y", Rt) assert R(0).factor_list() == (0, []) assert R(QQ(1, 7)).factor_list() == (QQ(1, 7), []) R, *X = ring("x:200", ZZ) f, g = X[0]**2 + 2 * X[0] + 1, X[0] + 1 assert f.factor_list() == (1, [(g, 2)]) f, g = X[-1]**2 + 2 * X[-1] + 1, X[-1] + 1 assert f.factor_list() == (1, [(g, 2)]) R, x = ring("x", ZZ) assert (x**2 + 2 * x + 1).factor_list() == (1, [(x + 1, 2)]) R, x = ring("x", QQ) assert (x**2 / 2 + x + QQ(1, 2)).factor_list() == (QQ(1, 2), [(x + 1, 2)]) R, x, y = ring("x,y", ZZ) assert (x**2 + 2 * x + 1).factor_list() == (1, [(x + 1, 2)]) R, x, y = ring("x,y", QQ) assert (x**2 / 2 + x + QQ(1, 2)).factor_list() == (QQ(1, 2), [(x + 1, 2)]) R, x, y = ring("x,y", ZZ) f = 4 * x**2 * y + 4 * x * y**2 assert f.factor_list() == (4, [(y, 1), (x, 1), (x + y, 1)]) R, x, y = ring("x,y", QQ) f = x**2 * y / 2 + x * y**2 / 2 assert f.factor_list() == (QQ(1, 2), [(y, 1), (x, 1), (x + y, 1)]) R, x, y = ring("x,y", RR) f = 2.0 * x**2 - 8.0 * y**2 assert f.factor_list() == (2.0, [(1.0 * x - 2.0 * y, 1), (1.0 * x + 2.0 * y, 1)]) f = 6.7225336055071 * x**2 * y**2 - 10.6463972754741 * x * y - 0.33469524022264 coeff, factors = f.factor_list() assert coeff == 1.0 and len(factors) == 1 and factors[0][0].almosteq( f, 1e-10) and factors[0][1] == 1 # issue diofant/diofant#238 R, x, y, z = ring("x,y,z", RR) f = x * y + x * z + 0.1 * y + 0.1 * z assert f.factor_list() == (10.0, [(0.1 * y + 0.1 * z, 1), (x + 0.1, 1)]) f = 0.25 * x**2 + 1.0 * x * y * z + 1.0 * y**2 * z**2 assert f.factor_list() == (4.0, [(0.25 * x + 0.5 * y * z, 2)]) Rt, t = ring("t", ZZ) R, x, y = ring("x,y", Rt) f = 4 * t * x**2 + 4 * t**2 * x assert f.factor_list() == (4 * t, [(x, 1), (x + t, 1)]) Rt, t = ring("t", QQ) R, x, y = ring("x,y", Rt) f = t * x**2 / 2 + t**2 * x / 2 assert f.factor_list() == (t / 2, [(x, 1), (x + t, 1)]) R, x, y = ring("x,y", FF(2)) pytest.raises(NotImplementedError, lambda: (x**2 + y**2).factor_list()) R, x, y = ring("x,y", EX) pytest.raises(DomainError, lambda: R(EX(sin(1))).factor_list()) R, x, y = ring('x, y', QQ.algebraic_field(I)) f, r = x**2 + y**2, (1, [(x - I * y, 1), (x + I * y, 1)]) assert R.dmp_factor_list(f) == r with config.using(aa_factor_method='trager'): assert R.dmp_factor_list(f) == r
def test_gf_factor(): R, x = ring('x', FF(11)) assert R(0).factor_list() == (0, []) assert R(1).factor_list() == (1, []) assert (x + 1).factor_list() == (1, [(x + 1, 1)]) assert (5 * x**3 + 2 * x**2 + 7 * x + 2).factor_list() == (5, [(x + 2, 1), (x + 8, 2)]) f = x**6 + 8 * x**5 + x**4 + 8 * x**3 + 10 * x**2 + 8 * x + 1 g = (1, [(x + 1, 1), (x**2 + 5 * x + 3, 1), (x**3 + 2 * x**2 + 3 * x + 4, 1)]) with config.using(gf_factor_method='berlekamp'): assert f.factor_list() == g with config.using(gf_factor_method='zassenhaus'): assert f.factor_list() == g with config.using(gf_factor_method='shoup'): assert f.factor_list() == g f = x**3 + 5 * x**2 + 8 * x + 4 g = (1, [(x + 1, 1), (x + 2, 2)]) with config.using(gf_factor_method='berlekamp'): assert f.factor_list() == g with config.using(gf_factor_method='zassenhaus'): assert f.factor_list() == g with config.using(gf_factor_method='shoup'): assert f.factor_list() == g f = x**9 + x**8 + 10 * x**7 + x**6 + 10 * x**4 + 10 * x**3 + 10 * x**2 g = (1, [(x, 2), (x**2 + 9 * x + 5, 1), (x**5 + 3 * x**4 + 8 * x**2 + 5 * x + 2, 1)]) with config.using(gf_factor_method='berlekamp'): assert f.factor_list() == g with config.using(gf_factor_method='zassenhaus'): assert f.factor_list() == g with config.using(gf_factor_method='shoup'): assert f.factor_list() == g f = x**32 + 1 g = (1, [(x**16 + 3 * x**8 + 10, 1), (x**16 + 8 * x**8 + 10, 1)]) with config.using(gf_factor_method='berlekamp'): assert f.factor_list() == g with config.using(gf_factor_method='zassenhaus'): assert f.factor_list() == g with config.using(gf_factor_method='shoup'): assert f.factor_list() == g f = 8 * x**32 + 5 g = (8, [(x + 3, 1), (x + 8, 1), (x**2 + 9, 1), (x**2 + 2 * x + 2, 1), (x**2 + 9 * x + 2, 1), (x**4 + 5 * x**2 + 7, 1), (x**4 + 6 * x**2 + 7, 1), (x**8 + x**4 + 6, 1), (x**8 + 10 * x**4 + 6, 1)]) with config.using(gf_factor_method='berlekamp'): assert f.factor_list() == g with config.using(gf_factor_method='zassenhaus'): assert f.factor_list() == g with config.using(gf_factor_method='shoup'): assert f.factor_list() == g f = 8 * x**63 + 5 g = (8, [(x + 7, 1), (x**2 + 4 * x + 5, 1), (x**3 + 6 * x**2 + 8 * x + 2, 1), (x**3 + 9 * x**2 + 9 * x + 2, 1), (x**6 + 9 * x**3 + 4, 1), (x**6 + 2 * x**5 + 8 * x**3 + 4 * x**2 + 6 * x + 4, 1), (x**6 + 2 * x**5 + 3 * x**4 + 8 * x**3 + 6 * x + 4, 1), (x**6 + 2 * x**5 + 6 * x**4 + 8 * x**2 + 4 * x + 4, 1), (x**6 + 3 * x**5 + 3 * x**4 + x**3 + 6 * x**2 + 8 * x + 4, 1), (x**6 + 5 * x**5 + 6 * x**4 + 8 * x**2 + 6 * x + 4, 1), (x**6 + 6 * x**5 + 2 * x**4 + 7 * x**3 + 9 * x**2 + 8 * x + 4, 1), (x**6 + 10 * x**5 + 4 * x**4 + 7 * x**3 + 10 * x**2 + 7 * x + 4, 1), (x**6 + 10 * x**5 + 10 * x**4 + x**3 + 4 * x**2 + 9 * x + 4, 1)]) with config.using(gf_factor_method='berlekamp'): assert f.factor_list() == g with config.using(gf_factor_method='zassenhaus'): assert f.factor_list() == g with config.using(gf_factor_method='shoup'): assert f.factor_list() == g with config.using(gf_factor_method='other'): pytest.raises(KeyError, lambda: (x + 1).factor_list()) R, x = ring('x', FF(2)) f = x**4 + x g = (1, [(x, 1), (x + 1, 1), (x**2 + x + 1, 1)]) with config.using(gf_factor_method='berlekamp'): assert f.factor_list() == g with config.using(gf_factor_method='zassenhaus'): assert f.factor_list() == g with config.using(gf_factor_method='shoup'): assert f.factor_list() == g # Gathen polynomials: x**n + x + 1 (mod p > 2**n * pi) p = ZZ(nextprime(int((2**15 * pi)))) R, x = ring('x', FF(p)) f = x**15 + x + 1 g = (1, [ (x**2 + 22730 * x + 68144, 1), (x**4 + 81553 * x**3 + 77449 * x**2 + 86810 * x + 4724, 1), (x**4 + 86276 * x**3 + 56779 * x**2 + 14859 * x + 31575, 1), (x**5 + 15347 * x**4 + 95022 * x**3 + 84569 * x**2 + 94508 * x + 92335, 1) ]) with config.using(gf_factor_method='zassenhaus'): assert f.factor_list() == g with config.using(gf_factor_method='shoup'): assert f.factor_list() == g # Shoup polynomials: f = a_0 x**n + a_1 x**(n-1) + ... + a_n # (mod p > 2**(n-2) * pi), where a_n = a_{n-1}**2 + 1, a_0 = 1 p = ZZ(nextprime(int((2**4 * pi)))) R, x = ring('x', FF(p)) f = x**6 + 2 * x**5 + 5 * x**4 + 26 * x**3 + 41 * x**2 + 39 * x + 38 g = (1, [(x**2 + 44 * x + 26, 1), (x**4 + 11 * x**3 + 25 * x**2 + 18 * x + 30, 1)]) with config.using(gf_factor_method='zassenhaus'): assert f.factor_list() == g with config.using(gf_factor_method='shoup'): assert f.factor_list() == g
def test_residue(): assert n_order(2, 13) == 12 assert [n_order(a, 7) for a in range(1, 7)] == \ [1, 3, 6, 3, 6, 2] assert n_order(5, 17) == 16 assert n_order(17, 11) == n_order(6, 11) assert n_order(101, 119) == 6 assert n_order( 11, (10**50 + 151)**2 ) == 10000000000000000000000000000000000000000000000030100000000000000000000000000000000000000000000022650 pytest.raises(ValueError, lambda: n_order(6, 9)) assert is_primitive_root(2, 7) is False assert is_primitive_root(3, 8) is False assert is_primitive_root(11, 14) is False assert is_primitive_root(12, 17) == is_primitive_root(29, 17) pytest.raises(ValueError, lambda: is_primitive_root(3, 6)) assert [primitive_root(i) for i in range(2, 31)] == [ 1, 2, 3, 2, 5, 3, None, 2, 3, 2, None, 2, 3, None, None, 3, 5, 2, None, None, 7, 5, None, 2, 7, 2, None, 2, None ] for p in primerange(3, 100): it = _primitive_root_prime_iter(p) assert len(list(it)) == totient(totient(p)) assert primitive_root(97) == 5 assert primitive_root(97**2) == 5 assert primitive_root(40487) == 5 # note that primitive_root(40487) + 40487 = 40492 is a primitive root # of 40487**2, but it is not the smallest assert primitive_root(40487**2) == 10 assert primitive_root(82) == 7 p = 10**50 + 151 assert primitive_root(p) == 11 assert primitive_root(2 * p) == 11 assert primitive_root(p**2) == 11 pytest.raises(ValueError, lambda: primitive_root(-3)) assert is_quad_residue(3, 7) is False assert is_quad_residue(10, 13) is True assert is_quad_residue(12364, 139) == is_quad_residue(12364 % 139, 139) assert is_quad_residue(207, 251) is True assert is_quad_residue(0, 1) is True assert is_quad_residue(1, 1) is True assert is_quad_residue(0, 2) == is_quad_residue(1, 2) is True assert is_quad_residue(1, 4) is True assert is_quad_residue(2, 27) is False assert is_quad_residue(13122380800, 13604889600) is True assert [j for j in range(14) if is_quad_residue(j, 14)] == \ [0, 1, 2, 4, 7, 8, 9, 11] pytest.raises(ValueError, lambda: is_quad_residue(1.1, 2)) pytest.raises(ValueError, lambda: is_quad_residue(2, 0)) assert quadratic_residues(12) == [0, 1, 4, 9] assert quadratic_residues(13) == [0, 1, 3, 4, 9, 10, 12] assert [len(quadratic_residues(i)) for i in range(1, 20)] == \ [1, 2, 2, 2, 3, 4, 4, 3, 4, 6, 6, 4, 7, 8, 6, 4, 9, 8, 10] assert list(sqrt_mod_iter(6, 2)) == [0] assert sqrt_mod(3, 13) == 4 assert sqrt_mod(3, -13) == 4 assert sqrt_mod(6, 23) == 11 assert sqrt_mod(345, 690) == 345 assert sqrt_mod(9, 18) == 3 for p in range(3, 100): d = defaultdict(list) for i in range(p): d[pow(i, 2, p)].append(i) for i in range(1, p): it = sqrt_mod_iter(i, p) v = sqrt_mod(i, p, True) if v: v = sorted(v) assert d[i] == v else: assert not d[i] assert sqrt_mod(9, 27, True) == [3, 6, 12, 15, 21, 24] assert sqrt_mod(9, 81, True) == [3, 24, 30, 51, 57, 78] assert sqrt_mod(9, 3**5, True) == [3, 78, 84, 159, 165, 240] assert sqrt_mod(81, 3**4, True) == [0, 9, 18, 27, 36, 45, 54, 63, 72] assert sqrt_mod(81, 3**5, True) == [ 9, 18, 36, 45, 63, 72, 90, 99, 117, 126, 144, 153, 171, 180, 198, 207, 225, 234 ] assert sqrt_mod(81, 3**6, True) == [ 9, 72, 90, 153, 171, 234, 252, 315, 333, 396, 414, 477, 495, 558, 576, 639, 657, 720 ] assert sqrt_mod(81, 3**7, True) == [ 9, 234, 252, 477, 495, 720, 738, 963, 981, 1206, 1224, 1449, 1467, 1692, 1710, 1935, 1953, 2178 ] for a, p in [(26214400, 32768000000), (26214400, 16384000000), (262144, 1048576), (87169610025, 163443018796875), (22315420166400, 167365651248000000)]: assert pow(sqrt_mod(a, p), 2, p) == a n = 70 a, p = 5**2 * 3**n * 2**n, 5**6 * 3**(n + 1) * 2**(n + 2) it = sqrt_mod_iter(a, p) for i in range(10): assert pow(next(it), 2, p) == a a, p = 5**2 * 3**n * 2**n, 5**6 * 3**(n + 1) * 2**(n + 3) it = sqrt_mod_iter(a, p) for i in range(2): assert pow(next(it), 2, p) == a n = 100 a, p = 5**2 * 3**n * 2**n, 5**6 * 3**(n + 1) * 2**(n + 1) it = sqrt_mod_iter(a, p) for i in range(2): assert pow(next(it), 2, p) == a assert type(next(sqrt_mod_iter(9, 27))) is int assert isinstance(next(sqrt_mod_iter(9, 27, ZZ)), type(ZZ(1))) assert isinstance(next(sqrt_mod_iter(1, 7, ZZ)), type(ZZ(1))) assert list(sqrt_mod_iter(4, 919, ZZ)) == [2, 917] assert list(sqrt_mod_iter(6, 146, ZZ)) == [88, 58] pytest.raises(ValueError, lambda: is_nthpow_residue(+2, +1, 0)) pytest.raises(ValueError, lambda: is_nthpow_residue(+2, -1, 5)) pytest.raises(ValueError, lambda: is_nthpow_residue(-2, +1, 5)) assert is_nthpow_residue(2, 1, 5) assert is_nthpow_residue(1, 0, 1) is False assert is_nthpow_residue(1, 0, 2) is True assert is_nthpow_residue(3, 0, 2) is False assert is_nthpow_residue(0, 1, 8) is True assert is_nthpow_residue(2, 3, 2) is False assert is_nthpow_residue(2, 3, 9) is False assert is_nthpow_residue(3, 5, 30) is True assert is_nthpow_residue(21, 11, 20) is True assert is_nthpow_residue(7, 10, 20) is False assert is_nthpow_residue(5, 10, 20) is True assert is_nthpow_residue(3, 10, 48) is False assert is_nthpow_residue(1, 10, 40) is True assert is_nthpow_residue(3, 10, 24) is False assert is_nthpow_residue(1, 10, 24) is True assert is_nthpow_residue(3, 10, 24) is False assert is_nthpow_residue(2, 10, 48) is False assert is_nthpow_residue(81, 3, 972) is False assert is_nthpow_residue(243, 5, 5103) is True assert is_nthpow_residue(243, 3, 1240029) is False assert {a for a in range(1024) if is_nthpow_residue(a, 56, 1024) } == {pow(i, 56, 1024) for i in range(1024)} assert {a for a in range(2048) if is_nthpow_residue(a, 256, 2048) } == {pow(i, 256, 2048) for i in range(2048)} x = {pow(i, 11, 324000) for i in range(1000)} assert [is_nthpow_residue(a, 11, 324000) for a in x] x = {pow(i, 17, 22217575536) for i in range(1000)} assert [is_nthpow_residue(a, 17, 22217575536) for a in x] assert is_nthpow_residue(676, 3, 5364) assert is_nthpow_residue(9, 12, 36) assert is_nthpow_residue(32, 10, 41) assert is_nthpow_residue(4, 2, 64) assert is_nthpow_residue(31, 4, 41) assert not is_nthpow_residue(2, 2, 5) assert is_nthpow_residue(8547, 12, 10007) assert nthroot_mod(1801, 11, 2663) == 44 for a, q, p in [(51922, 2, 203017), (43, 3, 109), (1801, 11, 2663), (26118163, 1303, 33333347), (1499, 7, 2663), (595, 6, 2663), (1714, 12, 2663), (28477, 9, 33343)]: r = nthroot_mod(a, q, p) assert pow(r, q, p) == a assert nthroot_mod(11, 3, 109) is None assert nthroot_mod(6, 12, 5) == 1 pytest.raises(NotImplementedError, lambda: nthroot_mod(16, 5, 36)) pytest.raises(NotImplementedError, lambda: nthroot_mod(9, 16, 36)) for p in primerange(5, 100): qv = range(3, p, 4) for q in qv: d = defaultdict(list) for i in range(p): d[pow(i, q, p)].append(i) for a in range(1, p - 1): res = nthroot_mod(a, q, p, True) if d[a]: assert d[a] == res else: assert res is None assert legendre_symbol(5, 11) == 1 assert legendre_symbol(25, 41) == 1 assert legendre_symbol(67, 101) == -1 assert legendre_symbol(0, 13) == 0 assert legendre_symbol(9, 3) == 0 pytest.raises(ValueError, lambda: legendre_symbol(2, 4)) assert jacobi_symbol(25, 41) == 1 assert jacobi_symbol(-23, 83) == -1 assert jacobi_symbol(3, 9) == 0 assert jacobi_symbol(42, 97) == -1 assert jacobi_symbol(3, 5) == -1 assert jacobi_symbol(7, 9) == 1 assert jacobi_symbol(0, 3) == 0 assert jacobi_symbol(0, 1) == 1 assert jacobi_symbol(2, 1) == 1 assert jacobi_symbol(1, 3) == 1 pytest.raises(ValueError, lambda: jacobi_symbol(3, 8)) assert mobius(13 * 7) == 1 assert mobius(1) == 1 assert mobius(13 * 7 * 5) == -1 assert mobius(13**2) == 0 pytest.raises(ValueError, lambda: mobius(-3)) p = Symbol('p', integer=True, positive=True, prime=True) x = Symbol('x', positive=True) i = Symbol('i', integer=True) assert mobius(p) == -1 pytest.raises(TypeError, lambda: mobius(x)) pytest.raises(ValueError, lambda: mobius(i)) mobius(Symbol('p', positive=True, integer=True))
def test_PolynomialRing(): assert str(ZZ.poly_ring("x")) == "ZZ[x]" assert str(QQ.poly_ring("x", "y", order=grlex)) == "QQ[x,y]" assert str(ZZ.poly_ring("t").poly_ring("x", "y", "z")) == "ZZ[t][x,y,z]"
def test_dmp_copy(): f = [ZZ(1), ZZ(2), ZZ(3), ZZ(0)] g = dmp_copy(f, 0) assert f is not g assert f == g g[0], g[2] = ZZ(7), ZZ(0) assert f != g f = [ZZ(1), ZZ(0), ZZ(2)] g = dmp_copy(f, 0) assert f is not g assert f == g g[0], g[2] = ZZ(7), ZZ(0) assert f != g f = [[ZZ(1)], [ZZ(2), ZZ(0)]] g = dmp_copy(f, 1) assert f is not g assert f == g g[0][0], g[1][1] = ZZ(7), ZZ(1) assert f != g
def test_dmp_one(): assert dmp_one(0, ZZ) == [ZZ(1)] assert dmp_one(2, ZZ) == [[[ZZ(1)]]]
def test_Domain__contains__(): assert (0 in EX) is True assert (0 in ZZ) is True assert (0 in QQ) is True assert (0 in RR) is True assert (0 in CC) is True assert (0 in ALG) is True assert (0 in ZZ.poly_ring(x, y)) is True assert (0 in QQ.poly_ring(x, y)) is True assert (0 in RR.poly_ring(x, y)) is True assert (-7 in EX) is True assert (-7 in ZZ) is True assert (-7 in QQ) is True assert (-7 in RR) is True assert (-7 in CC) is True assert (-7 in ALG) is True assert (-7 in ZZ.poly_ring(x, y)) is True assert (-7 in QQ.poly_ring(x, y)) is True assert (-7 in RR.poly_ring(x, y)) is True assert (17 in EX) is True assert (17 in ZZ) is True assert (17 in QQ) is True assert (17 in RR) is True assert (17 in CC) is True assert (17 in ALG) is True assert (17 in ZZ.poly_ring(x, y)) is True assert (17 in QQ.poly_ring(x, y)) is True assert (17 in RR.poly_ring(x, y)) is True assert (-Rational(1, 7) in EX) is True assert (-Rational(1, 7) in ZZ) is False assert (-Rational(1, 7) in QQ) is True assert (-Rational(1, 7) in RR) is True assert (-Rational(1, 7) in CC) is True assert (-Rational(1, 7) in ALG) is True assert (-Rational(1, 7) in ZZ.poly_ring(x, y)) is False assert (-Rational(1, 7) in QQ.poly_ring(x, y)) is True assert (-Rational(1, 7) in RR.poly_ring(x, y)) is True assert (Rational(3, 5) in EX) is True assert (Rational(3, 5) in ZZ) is False assert (Rational(3, 5) in QQ) is True assert (Rational(3, 5) in RR) is True assert (Rational(3, 5) in CC) is True assert (Rational(3, 5) in ALG) is True assert (Rational(3, 5) in ZZ.poly_ring(x, y)) is False assert (Rational(3, 5) in QQ.poly_ring(x, y)) is True assert (Rational(3, 5) in RR.poly_ring(x, y)) is True assert (3.0 in EX) is True assert (3.0 in ZZ) is True assert (3.0 in QQ) is True assert (3.0 in RR) is True assert (3.0 in CC) is True assert (3.0 in ALG) is True assert (3.0 in ZZ.poly_ring(x, y)) is True assert (3.0 in QQ.poly_ring(x, y)) is True assert (3.0 in RR.poly_ring(x, y)) is True assert (3.14 in EX) is True assert (3.14 in ZZ) is False assert (3.14 in QQ) is True assert (3.14 in RR) is True assert (3.14 in CC) is True assert (3.14 in ALG) is True assert (3.14 in ZZ.poly_ring(x, y)) is False assert (3.14 in QQ.poly_ring(x, y)) is True assert (3.14 in RR.poly_ring(x, y)) is True assert (oo in EX) is True assert (oo in ZZ) is False assert (oo in QQ) is False assert (oo in RR) is True assert (oo in CC) is True assert (oo in ALG) is False assert (oo in ZZ.poly_ring(x, y)) is False assert (oo in QQ.poly_ring(x, y)) is False assert (oo in RR.poly_ring(x, y)) is True assert (-oo in EX) is True assert (-oo in ZZ) is False assert (-oo in QQ) is False assert (-oo in RR) is True assert (-oo in CC) is True assert (-oo in ALG) is False assert (-oo in ZZ.poly_ring(x, y)) is False assert (-oo in QQ.poly_ring(x, y)) is False assert (-oo in RR.poly_ring(x, y)) is True assert (sqrt(7) in EX) is True assert (sqrt(7) in ZZ) is False assert (sqrt(7) in QQ) is False assert (sqrt(7) in RR) is True assert (sqrt(7) in CC) is True assert (sqrt(7) in ALG) is False assert (sqrt(7) in ZZ.poly_ring(x, y)) is False assert (sqrt(7) in QQ.poly_ring(x, y)) is False assert (sqrt(7) in RR.poly_ring(x, y)) is True assert (2 * sqrt(3) + 1 in EX) is True assert (2 * sqrt(3) + 1 in ZZ) is False assert (2 * sqrt(3) + 1 in QQ) is False assert (2 * sqrt(3) + 1 in RR) is True assert (2 * sqrt(3) + 1 in CC) is True assert (2 * sqrt(3) + 1 in ALG) is True assert (2 * sqrt(3) + 1 in ZZ.poly_ring(x, y)) is False assert (2 * sqrt(3) + 1 in QQ.poly_ring(x, y)) is False assert (2 * sqrt(3) + 1 in RR.poly_ring(x, y)) is True assert (sin(1) in EX) is True assert (sin(1) in ZZ) is False assert (sin(1) in QQ) is False assert (sin(1) in RR) is True assert (sin(1) in CC) is True assert (sin(1) in ALG) is False assert (sin(1) in ZZ.poly_ring(x, y)) is False assert (sin(1) in QQ.poly_ring(x, y)) is False assert (sin(1) in RR.poly_ring(x, y)) is True assert (x**2 + 1 in EX) is True assert (x**2 + 1 in ZZ) is False assert (x**2 + 1 in QQ) is False assert (x**2 + 1 in RR) is False assert (x**2 + 1 in CC) is False assert (x**2 + 1 in ALG) is False assert (x**2 + 1 in ZZ.poly_ring(x)) is True assert (x**2 + 1 in QQ.poly_ring(x)) is True assert (x**2 + 1 in RR.poly_ring(x)) is True assert (x**2 + 1 in ZZ.poly_ring(x, y)) is True assert (x**2 + 1 in QQ.poly_ring(x, y)) is True assert (x**2 + 1 in RR.poly_ring(x, y)) is True assert (x**2 + y**2 in EX) is True assert (x**2 + y**2 in ZZ) is False assert (x**2 + y**2 in QQ) is False assert (x**2 + y**2 in RR) is False assert (x**2 + y**2 in CC) is False assert (x**2 + y**2 in ALG) is False assert (x**2 + y**2 in ZZ.poly_ring(x)) is False assert (x**2 + y**2 in QQ.poly_ring(x)) is False assert (x**2 + y**2 in RR.poly_ring(x)) is False assert (x**2 + y**2 in ZZ.poly_ring(x, y)) is True assert (x**2 + y**2 in QQ.poly_ring(x, y)) is True assert (x**2 + y**2 in RR.poly_ring(x, y)) is True assert (Rational(3, 2) * x / (y + 1) - z in QQ.poly_ring(x, y, z)) is False
def test_dup_refine_real_root(): R, x = ring("x", ZZ) f = x**2 - 2 assert R.dup_refine_real_root(f, QQ(1), QQ(1), steps=1) == (1, 1) assert R.dup_refine_real_root(f, QQ(1), QQ(1), steps=9) == (1, 1) pytest.raises(ValueError, lambda: R.dup_refine_real_root(f, QQ(-2), QQ(2))) s, t = QQ(1, 1), QQ(2, 1) assert R.dup_refine_real_root(f, s, t, steps=0) == (1, 2) assert R.dup_refine_real_root(f, s, t, steps=1) == (1, QQ(3, 2)) assert R.dup_refine_real_root(f, s, t, steps=2) == (QQ(4, 3), QQ(3, 2)) assert R.dup_refine_real_root(f, s, t, steps=3) == (QQ(7, 5), QQ(3, 2)) assert R.dup_refine_real_root(f, s, t, steps=4) == (QQ(7, 5), QQ(10, 7)) s, t = QQ(1, 1), QQ(3, 2) assert R.dup_refine_real_root(f, s, t, steps=0) == (1, QQ(3, 2)) assert R.dup_refine_real_root(f, s, t, steps=1) == (QQ(4, 3), QQ(3, 2)) assert R.dup_refine_real_root(f, s, t, steps=2) == (QQ(7, 5), QQ(3, 2)) assert R.dup_refine_real_root(f, s, t, steps=3) == (QQ(7, 5), QQ(10, 7)) assert R.dup_refine_real_root(f, s, t, steps=4) == (QQ(7, 5), QQ(17, 12)) s, t = QQ(1, 1), QQ(5, 3) assert R.dup_refine_real_root(f, s, t, steps=0) == (1, QQ(5, 3)) assert R.dup_refine_real_root(f, s, t, steps=1) == (1, QQ(3, 2)) assert R.dup_refine_real_root(f, s, t, steps=2) == (QQ(7, 5), QQ(3, 2)) assert R.dup_refine_real_root(f, s, t, steps=3) == (QQ(7, 5), QQ(13, 9)) assert R.dup_refine_real_root(f, s, t, steps=4) == (QQ(7, 5), QQ(27, 19)) s, t = QQ(-1, 1), QQ(-2, 1) assert R.dup_refine_real_root(f, s, t, steps=0) == (-QQ(2, 1), -QQ(1, 1)) assert R.dup_refine_real_root(f, s, t, steps=1) == (-QQ(3, 2), -QQ(1, 1)) assert R.dup_refine_real_root(f, s, t, steps=2) == (-QQ(3, 2), -QQ(4, 3)) assert R.dup_refine_real_root(f, s, t, steps=3) == (-QQ(3, 2), -QQ(7, 5)) assert R.dup_refine_real_root(f, s, t, steps=4) == (-QQ(10, 7), -QQ(7, 5)) pytest.raises(RefinementFailed, lambda: R.dup_refine_real_root(f, QQ(0), QQ(1))) s, t, u, v, w = QQ(1), QQ(2), QQ(24, 17), QQ(17, 12), QQ(7, 5) assert R.dup_refine_real_root(f, s, t, eps=QQ(1, 100)) == (u, v) assert R.dup_refine_real_root(f, s, t, steps=6) == (u, v) assert R.dup_refine_real_root(f, s, t, eps=QQ(1, 100), steps=5) == (w, v) assert R.dup_refine_real_root(f, s, t, eps=QQ(1, 100), steps=6) == (u, v) assert R.dup_refine_real_root(f, s, t, eps=QQ(1, 100), steps=7) == (u, v) s, t, u, v = QQ(-2), QQ(-1), QQ(-3, 2), QQ(-4, 3) assert R.dup_refine_real_root(f, s, t, disjoint=QQ(-5)) == (s, t) assert R.dup_refine_real_root(f, s, t, disjoint=-v) == (s, t) assert R.dup_refine_real_root(f, s, t, disjoint=v) == (u, v) s, t, u, v = QQ(1), QQ(2), QQ(4, 3), QQ(3, 2) assert R.dup_refine_real_root(f, s, t, disjoint=QQ(5)) == (s, t) assert R.dup_refine_real_root(f, s, t, disjoint=-u) == (s, t) assert R.dup_refine_real_root(f, s, t, disjoint=u) == (u, v) R, x = ring("x", QQ) f = x**2 - QQ(1, 4) assert R.dup_refine_real_root(f, QQ(0), QQ(1), steps=1) == (QQ(1, 2), QQ(1, 2)) D = ZZ.poly_ring("y") y, = D.gens R, x = ring("x", D) f = x**2 + y * x - 1 pytest.raises(DomainError, lambda: R.dup_refine_real_root(f, ZZ(0), ZZ(1)))
def test_Domain__contains__(): assert (0 in EX) is True assert (0 in ZZ) is True assert (0 in QQ) is True assert (0 in RR) is True assert (0 in CC) is True assert (0 in ALG) is True assert (0 in ZZ.poly_ring(x, y)) is True assert (0 in QQ.poly_ring(x, y)) is True assert (0 in RR.poly_ring(x, y)) is True assert (-7 in EX) is True assert (-7 in ZZ) is True assert (-7 in QQ) is True assert (-7 in RR) is True assert (-7 in CC) is True assert (-7 in ALG) is True assert (-7 in ZZ.poly_ring(x, y)) is True assert (-7 in QQ.poly_ring(x, y)) is True assert (-7 in RR.poly_ring(x, y)) is True assert (17 in EX) is True assert (17 in ZZ) is True assert (17 in QQ) is True assert (17 in RR) is True assert (17 in CC) is True assert (17 in ALG) is True assert (17 in ZZ.poly_ring(x, y)) is True assert (17 in QQ.poly_ring(x, y)) is True assert (17 in RR.poly_ring(x, y)) is True assert (-Rational(1, 7) in EX) is True assert (-Rational(1, 7) in ZZ) is False assert (-Rational(1, 7) in QQ) is True assert (-Rational(1, 7) in RR) is True assert (-Rational(1, 7) in CC) is True assert (-Rational(1, 7) in ALG) is True assert (-Rational(1, 7) in ZZ.poly_ring(x, y)) is False assert (-Rational(1, 7) in QQ.poly_ring(x, y)) is True assert (-Rational(1, 7) in RR.poly_ring(x, y)) is True assert (Rational(3, 5) in EX) is True assert (Rational(3, 5) in ZZ) is False assert (Rational(3, 5) in QQ) is True assert (Rational(3, 5) in RR) is True assert (Rational(3, 5) in CC) is True assert (Rational(3, 5) in ALG) is True assert (Rational(3, 5) in ZZ.poly_ring(x, y)) is False assert (Rational(3, 5) in QQ.poly_ring(x, y)) is True assert (Rational(3, 5) in RR.poly_ring(x, y)) is True assert (3.0 in EX) is True assert (3.0 in ZZ) is True assert (3.0 in QQ) is True assert (3.0 in RR) is True assert (3.0 in CC) is True assert (3.0 in ALG) is True assert (3.0 in ZZ.poly_ring(x, y)) is True assert (3.0 in QQ.poly_ring(x, y)) is True assert (3.0 in RR.poly_ring(x, y)) is True assert (3.14 in EX) is True assert (3.14 in ZZ) is False assert (3.14 in QQ) is True assert (3.14 in RR) is True assert (3.14 in CC) is True assert (3.14 in ALG) is True assert (3.14 in ZZ.poly_ring(x, y)) is False assert (3.14 in QQ.poly_ring(x, y)) is True assert (3.14 in RR.poly_ring(x, y)) is True assert (oo in EX) is True assert (oo in ZZ) is False assert (oo in QQ) is False assert (oo in RR) is True assert (oo in CC) is True assert (oo in ALG) is False assert (oo in ZZ.poly_ring(x, y)) is False assert (oo in QQ.poly_ring(x, y)) is False assert (oo in RR.poly_ring(x, y)) is True assert (-oo in EX) is True assert (-oo in ZZ) is False assert (-oo in QQ) is False assert (-oo in RR) is True assert (-oo in CC) is True assert (-oo in ALG) is False assert (-oo in ZZ.poly_ring(x, y)) is False assert (-oo in QQ.poly_ring(x, y)) is False assert (-oo in RR.poly_ring(x, y)) is True assert (sqrt(7) in EX) is True assert (sqrt(7) in ZZ) is False assert (sqrt(7) in QQ) is False assert (sqrt(7) in RR) is True assert (sqrt(7) in CC) is True assert (sqrt(7) in ALG) is False assert (sqrt(7) in ZZ.poly_ring(x, y)) is False assert (sqrt(7) in QQ.poly_ring(x, y)) is False assert (sqrt(7) in RR.poly_ring(x, y)) is True assert (2*sqrt(3) + 1 in EX) is True assert (2*sqrt(3) + 1 in ZZ) is False assert (2*sqrt(3) + 1 in QQ) is False assert (2*sqrt(3) + 1 in RR) is True assert (2*sqrt(3) + 1 in CC) is True assert (2*sqrt(3) + 1 in ALG) is True assert (2*sqrt(3) + 1 in ZZ.poly_ring(x, y)) is False assert (2*sqrt(3) + 1 in QQ.poly_ring(x, y)) is False assert (2*sqrt(3) + 1 in RR.poly_ring(x, y)) is True assert (sin(1) in EX) is True assert (sin(1) in ZZ) is False assert (sin(1) in QQ) is False assert (sin(1) in RR) is True assert (sin(1) in CC) is True assert (sin(1) in ALG) is False assert (sin(1) in ZZ.poly_ring(x, y)) is False assert (sin(1) in QQ.poly_ring(x, y)) is False assert (sin(1) in RR.poly_ring(x, y)) is True assert (x**2 + 1 in EX) is True assert (x**2 + 1 in ZZ) is False assert (x**2 + 1 in QQ) is False assert (x**2 + 1 in RR) is False assert (x**2 + 1 in CC) is False assert (x**2 + 1 in ALG) is False assert (x**2 + 1 in ZZ.poly_ring(x)) is True assert (x**2 + 1 in QQ.poly_ring(x)) is True assert (x**2 + 1 in RR.poly_ring(x)) is True assert (x**2 + 1 in ZZ.poly_ring(x, y)) is True assert (x**2 + 1 in QQ.poly_ring(x, y)) is True assert (x**2 + 1 in RR.poly_ring(x, y)) is True assert (x**2 + y**2 in EX) is True assert (x**2 + y**2 in ZZ) is False assert (x**2 + y**2 in QQ) is False assert (x**2 + y**2 in RR) is False assert (x**2 + y**2 in CC) is False assert (x**2 + y**2 in ALG) is False assert (x**2 + y**2 in ZZ.poly_ring(x)) is False assert (x**2 + y**2 in QQ.poly_ring(x)) is False assert (x**2 + y**2 in RR.poly_ring(x)) is False assert (x**2 + y**2 in ZZ.poly_ring(x, y)) is True assert (x**2 + y**2 in QQ.poly_ring(x, y)) is True assert (x**2 + y**2 in RR.poly_ring(x, y)) is True assert (Rational(3, 2)*x/(y + 1) - z in QQ.poly_ring(x, y, z)) is False