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_globalring(): Qxy = QQ.old_frac_field(x, y) R = QQ.old_poly_ring(x, y) X = R.convert(x) Y = R.convert(y) assert x in R assert 1 / x not in R assert 1 / (1 + x) not in R assert Y in R assert X.ring == R assert X * (Y**2 + 1) == R.convert(x * (y**2 + 1)) assert X * y == X * Y == R.convert(x * y) == x * Y assert X + y == X + Y == R.convert(x + y) == x + Y assert X - y == X - Y == R.convert(x - y) == x - Y assert X + 1 == R.convert(x + 1) pytest.raises(ExactQuotientFailed, lambda: X / Y) pytest.raises(ExactQuotientFailed, lambda: x / Y) pytest.raises(ExactQuotientFailed, lambda: X / y) assert X**2 / X == X assert R.from_GlobalPolynomialRing( ZZ.old_poly_ring(x, y).convert(x), ZZ.old_poly_ring(x, y)) == X assert R.from_FractionField(Qxy.convert(x), Qxy) == X assert R.from_FractionField(Qxy.convert(x) / y, Qxy) is None assert R._sdm_to_vector(R._vector_to_sdm([X, Y], R.order), 2) == [X, Y]
def test_localring(): Qxy = QQ.old_frac_field(x, y) R = QQ.old_poly_ring(x, y, order="ilex") X = R.convert(x) Y = R.convert(y) assert x in R assert 1 / x not in R assert 1 / (1 + x) in R assert Y in R assert X.ring == R assert X * (Y**2 + 1) / (1 + X) == R.convert(x * (y**2 + 1) / (1 + x)) assert X * y == X * Y pytest.raises(ExactQuotientFailed, lambda: X / Y) pytest.raises(ExactQuotientFailed, lambda: x / Y) pytest.raises(ExactQuotientFailed, lambda: X / y) assert X + y == X + Y == R.convert(x + y) == x + Y assert X - y == X - Y == R.convert(x - y) == x - Y assert X + 1 == R.convert(x + 1) assert X**2 / X == X assert R.from_GlobalPolynomialRing( ZZ.old_poly_ring(x, y).convert(x), ZZ.old_poly_ring(x, y)) == X assert R.from_FractionField(Qxy.convert(x), Qxy) == X pytest.raises(CoercionFailed, lambda: R.from_FractionField(Qxy.convert(x) / y, Qxy)) pytest.raises(ExactQuotientFailed, lambda: X / Y) pytest.raises(NotReversible, lambda: X.invert()) assert R._sdm_to_vector( R._vector_to_sdm([X/(X + 1), Y/(1 + X*Y)], R.order), 2) == \ [X*(1 + X*Y), Y*(1 + X)]
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_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) assert x + F.ring.gens[0] == F.ring.gens[0] + x == 2 * x F, x, y = field("x,y", ZZ) assert x + 3 == 3 + x assert x + QQ(3, 7) == QQ(3, 7) + x == (7 * x + 3) / 7 Fuv, u, v = field("u,v", ZZ) Fxyzt, x, y, z, t = field("x,y,z,t", Fuv) f = (u * v + x) / (y + u * v) assert dict(f.numer) == {(1, 0, 0, 0): 1, (0, 0, 0, 0): u * v} assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0): u * v} Ruv, u, v = ring("u,v", ZZ) Fxyzt, x, y, z, t = field("x,y,z,t", Ruv) f = (u * v + x) / (y + u * v) assert dict(f.numer) == {(1, 0, 0, 0): 1, (0, 0, 0, 0): u * v} assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0): u * v}
def test_FracElement___sub__(): F, x, y = field("x,y", QQ) f, g = 1 / x, 1 / y assert f - g == (-x + y) / (x * y) assert x - F.ring.gens[0] == F.ring.gens[0] - x == 0 F, x, y = field("x,y", ZZ) assert x - 3 == -(3 - x) assert x - QQ(3, 7) == -(QQ(3, 7) - x) == (7 * x - 3) / 7 Fuv, u, v = field("u,v", ZZ) Fxyzt, x, y, z, t = field("x,y,z,t", Fuv) f = (u * v - x) / (y - u * v) assert dict(f.numer) == {(1, 0, 0, 0): -1, (0, 0, 0, 0): u * v} assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0): -u * v} Ruv, u, v = ring("u,v", ZZ) Fxyzt, x, y, z, t = field("x,y,z,t", Ruv) f = (u * v - x) / (y - u * v) assert dict(f.numer) == {(1, 0, 0, 0): -1, (0, 0, 0, 0): u * v} assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0): -u * v}
def test_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))
def test_Domain_get_ring(): assert ZZ.has_assoc_Ring is True assert QQ.has_assoc_Ring is True assert ZZ[x].has_assoc_Ring is True assert QQ[x].has_assoc_Ring is True assert ZZ[x, y].has_assoc_Ring is True assert QQ[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.get_ring() == ZZ assert QQ.get_ring() == ZZ assert ZZ[x].get_ring() == ZZ[x] assert QQ[x].get_ring() == QQ[x] assert ZZ[x, y].get_ring() == ZZ[x, y] assert QQ[x, y].get_ring() == QQ[x, y] assert ZZ.frac_field(x).get_ring() == ZZ[x] assert QQ.frac_field(x).get_ring() == QQ[x] assert ZZ.frac_field(x, y).get_ring() == ZZ[x, y] assert QQ.frac_field(x, y).get_ring() == QQ[x, y] assert EX.get_ring() == EX pytest.raises(DomainError, lambda: RR.get_ring()) pytest.raises(DomainError, lambda: ALG.get_ring())
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_conversion(): f = [x**2 + y**2, 2*z] g = [((1, 0, 0, 1), QQ(2)), ((0, 2, 0, 0), QQ(1)), ((0, 0, 2, 0), QQ(1))] assert sdm_to_vector(g, [x, y, z], QQ) == f assert sdm_from_vector(f, lex, QQ) == g assert sdm_from_vector( [x, 1], lex, QQ) == [((1, 0), QQ(1)), ((0, 1), QQ(1))] assert sdm_to_vector([((1, 1, 0, 0), 1)], [x, y, z], QQ, n=3) == [0, x, 0] assert sdm_from_vector([0, 0], lex, QQ, gens=[x, y]) == sdm_zero()
def test_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, -QQ(10, 9) * x + QQ(13, 3), -QQ(3303, 100) ]
def test_ANP_unify(): mod = [QQ(1), QQ(0), QQ(-2)] a = ANP([QQ(1)], mod, QQ) b = ANP([ZZ(1)], mod, ZZ) assert a.unify(b)[0] == QQ assert b.unify(a)[0] == QQ assert a.unify(a)[0] == QQ assert b.unify(b)[0] == ZZ
def test_dup_ff_div(): pytest.raises(ZeroDivisionError, lambda: dup_ff_div([1, 2, 3], [], QQ)) f = dup_normal([3, 1, 1, 5], QQ) g = dup_normal([5, -3, 1], QQ) q = [QQ(3, 5), QQ(14, 25)] r = [QQ(52, 25), QQ(111, 25)] assert dup_ff_div(f, g, QQ) == (q, r)
def test_dup_neg(): assert dup_neg([], ZZ) == [] assert dup_neg([ZZ(1)], ZZ) == [ZZ(-1)] assert dup_neg([ZZ(-7)], ZZ) == [ZZ(7)] assert dup_neg([ZZ(-1), ZZ(2), ZZ(3)], ZZ) == [ZZ(1), ZZ(-2), ZZ(-3)] assert dup_neg([], QQ) == [] assert dup_neg([QQ(1, 2)], QQ) == [QQ(-1, 2)] assert dup_neg([QQ(-7, 9)], QQ) == [QQ(7, 9)] assert dup_neg([QQ(-1, 7), QQ(2, 7), QQ(3, 7)], QQ) == [QQ(1, 7), QQ(-2, 7), QQ(-3, 7)]
def test_dup_abs(): assert dup_abs([], ZZ) == [] assert dup_abs([ZZ(1)], ZZ) == [ZZ(1)] assert dup_abs([ZZ(-7)], ZZ) == [ZZ(7)] assert dup_abs([ZZ(-1), ZZ(2), ZZ(3)], ZZ) == [ZZ(1), ZZ(2), ZZ(3)] assert dup_abs([], QQ) == [] assert dup_abs([QQ(1, 2)], QQ) == [QQ(1, 2)] assert dup_abs([QQ(-7, 3)], QQ) == [QQ(7, 3)] assert dup_abs([QQ(-1, 7), QQ(2, 7), QQ(3, 7)], QQ) == [QQ(1, 7), QQ(2, 7), QQ(3, 7)]
def test_sdm_from_dict(): dic = { (1, 2, 1, 1): QQ(1), (1, 1, 2, 1): QQ(1), (1, 0, 2, 1): QQ(1), (1, 0, 0, 3): QQ(1), (1, 1, 1, 0): QQ(1) } assert sdm_from_dict(dic, grlex) == \ [((1, 2, 1, 1), QQ(1)), ((1, 1, 2, 1), QQ(1)), ((1, 0, 2, 1), QQ(1)), ((1, 0, 0, 3), QQ(1)), ((1, 1, 1, 0), QQ(1))]
def test_minpoly_domain(): assert minimal_polynomial(sqrt(2), x, domain=QQ.algebraic_field(sqrt(2))) == \ x - sqrt(2) assert minimal_polynomial(sqrt(8), x, domain=QQ.algebraic_field(sqrt(2))) == \ x - 2*sqrt(2) assert minimal_polynomial(sqrt(Rational(3, 2)), x, domain=QQ.algebraic_field( sqrt(2))) == 2 * x**2 - 3 pytest.raises(NotAlgebraic, lambda: minimal_polynomial(y, x, domain=QQ))
def test_Domain___eq__(): assert (ZZ[x, y] == ZZ[x, y]) is True assert (QQ[x, y] == QQ[x, y]) is True assert (ZZ[x, y] == QQ[x, y]) is False assert (QQ[x, y] == ZZ[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_dmp_sqr(): assert dmp_sqr([ZZ(1), ZZ(2)], 0, ZZ) == \ dup_sqr([ZZ(1), ZZ(2)], ZZ) assert dmp_sqr([[[]]], 2, ZZ) == [[[]]] assert dmp_sqr([[[ZZ(2)]]], 2, ZZ) == [[[ZZ(4)]]] assert dmp_sqr([[[]]], 2, QQ) == [[[]]] assert dmp_sqr([[[QQ(2, 3)]]], 2, QQ) == [[[QQ(4, 9)]]] K = FF(9) assert dmp_sqr([[K(3)], [K(4)]], 1, K) == [[K(6)], [K(7)]]
def test_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[x].get_exact() == ZZ[x] assert QQ[x].get_exact() == QQ[x] assert ZZ[x, y].get_exact() == ZZ[x, y] assert QQ[x, y].get_exact() == QQ[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_dup_count_complex_roots_exclude(): R, x = ring("x", ZZ) # z*(z-1)*(z+1)*(z-I)*(z+I) f = x**5 - x a, b = (-QQ(1), QQ(0)), (QQ(1), QQ(1)) assert R.dup_count_complex_roots(f, a, b) == 4 assert R.dup_count_complex_roots(f, a, b, exclude=['S']) == 3 assert R.dup_count_complex_roots(f, a, b, exclude=['N']) == 3 assert R.dup_count_complex_roots(f, a, b, exclude=['S', 'N']) == 2 assert R.dup_count_complex_roots(f, a, b, exclude=['E']) == 4 assert R.dup_count_complex_roots(f, a, b, exclude=['W']) == 4 assert R.dup_count_complex_roots(f, a, b, exclude=['E', 'W']) == 4 assert R.dup_count_complex_roots(f, a, b, exclude=['N', 'S', 'E', 'W']) == 2 assert R.dup_count_complex_roots(f, a, b, exclude=['SW']) == 3 assert R.dup_count_complex_roots(f, a, b, exclude=['SE']) == 3 assert R.dup_count_complex_roots(f, a, b, exclude=['SW', 'SE']) == 2 assert R.dup_count_complex_roots(f, a, b, exclude=['SW', 'SE', 'S']) == 1 assert R.dup_count_complex_roots(f, a, b, exclude=['SW', 'SE', 'S', 'N']) == 0 a, b = (QQ(0), QQ(0)), (QQ(1), QQ(1)) assert R.dup_count_complex_roots(f, a, b, exclude=True) == 1
def test_PolyElement_cancel(): R, x, y = ring("x,y", ZZ) f = 2 * x**3 + 4 * x**2 + 2 * x g = 3 * x**2 + 3 * x F = 2 * x + 2 G = 3 assert f.cancel(g) == (F, G) assert (-f).cancel(g) == (-F, G) assert f.cancel(-g) == (-F, G) R, x, y = ring("x,y", QQ) f = QQ(1, 2) * x**3 + x**2 + QQ(1, 2) * x g = QQ(1, 3) * x**2 + QQ(1, 3) * x F = 3 * x + 3 G = 2 assert f.cancel(g) == (F, G) assert (-f).cancel(g) == (-F, G) assert f.cancel(-g) == (-F, G) Fx, x = field("x", ZZ) Rt, t = ring("t", Fx) f = (-x**2 - 4) / 4 * t g = t**2 + (x**2 + 2) / 2 assert f.cancel(g) == ((-x**2 - 4) * t, 4 * t**2 + 2 * x**2 + 4)
def test_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.to_domain()) == 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))
def test_units(): R = QQ.poly_ring(x) assert R.is_unit(R.convert(1)) assert not R.is_unit(R.convert(x)) assert not R.is_unit(R.convert(1 + x)) R = QQ.poly_ring(x, order='ilex') assert R.is_unit(R.convert(1)) assert not R.is_unit(R.convert(x)) R = ZZ.poly_ring(x) assert R.is_unit(R.convert(1)) assert not R.is_unit(R.convert(2)) assert not R.is_unit(R.convert(x)) assert not R.is_unit(R.convert(1 + x))
def test_dup_monic(): assert dup_monic([3, 6, 9], ZZ) == [1, 2, 3] pytest.raises(ExactQuotientFailed, lambda: dup_monic([3, 4, 5], ZZ)) assert dup_monic([], QQ) == [] assert dup_monic([QQ(1)], QQ) == [QQ(1)] assert dup_monic([QQ(7), QQ(1), QQ(21)], QQ) == [QQ(1), QQ(1, 7), QQ(3)]
def test_dup_isolate_real_roots_list_QQ(): R, x = ring("x", ZZ) f = x**5 - 200 g = x**5 - 201 assert R.dup_isolate_real_roots_list([f, g]) == \ [((QQ(75, 26), QQ(101, 35)), {0: 1}), ((QQ(309, 107), QQ(26, 9)), {1: 1})] R, x = ring("x", QQ) f = -QQ(1, 200) * x**5 + 1 g = -QQ(1, 201) * x**5 + 1 assert R.dup_isolate_real_roots_list([f, g]) == \ [((QQ(75, 26), QQ(101, 35)), {0: 1}), ((QQ(309, 107), QQ(26, 9)), {1: 1})]
def test_PolyElement_terms(): R, x, y, z = ring("x,y,z", QQ) terms = (x**2/3 + y**3/4 + z**4/5).terms() assert terms == [((2, 0, 0), QQ(1, 3)), ((0, 3, 0), QQ(1, 4)), ((0, 0, 4), QQ(1, 5))] R, x, y = ring("x,y", ZZ, lex) f = x*y**7 + 2*x**2*y**3 assert f.terms() == f.terms(lex) == f.terms('lex') == [((2, 3), 2), ((1, 7), 1)] assert f.terms(grlex) == f.terms('grlex') == [((1, 7), 1), ((2, 3), 2)] R, x, y = ring("x,y", ZZ, grlex) f = x*y**7 + 2*x**2*y**3 assert f.terms() == f.terms(grlex) == f.terms('grlex') == [((1, 7), 1), ((2, 3), 2)] assert f.terms(lex) == f.terms('lex') == [((2, 3), 2), ((1, 7), 1)]
def test_PolyElement_coeffs(): R, x, y, z = ring("x,y,z", QQ) coeffs = (x**2 / 3 + y**3 / 4 + z**4 / 5).coeffs() assert coeffs == [QQ(1, 3), QQ(1, 4), QQ(1, 5)] R, x, y = ring("x,y", ZZ, lex) f = x * y**7 + 2 * x**2 * y**3 assert f.coeffs() == f.coeffs(lex) == f.coeffs('lex') == [2, 1] assert f.coeffs(grlex) == f.coeffs('grlex') == [1, 2] R, x, y = ring("x,y", ZZ, grlex) f = x * y**7 + 2 * x**2 * y**3 assert f.coeffs() == f.coeffs(grlex) == f.coeffs('grlex') == [1, 2] assert f.coeffs(lex) == f.coeffs('lex') == [2, 1]