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_Modules(): from sympy.polys.domains import QQ from sympy.polys.agca import homomorphism R = QQ.old_poly_ring(x, y) F = R.free_module(2) M = F.submodule([x, y], [1, x ** 2]) assert latex(F) == r"{\mathbb{Q}\left[x, y\right]}^{2}" assert latex(M) == r"\left< {\left[ {x},{y} \right]},{\left[ {1},{x^{2}} \right]} \right>" I = R.ideal(x ** 2, y) assert latex(I) == r"\left< {x^{2}},{y} \right>" Q = F / M assert ( latex(Q) == r"\frac{{\mathbb{Q}\left[x, y\right]}^{2}}{\left< {\left[ {x},{y} \right]},{\left[ {1},{x^{2}} \right]} \right>}" ) assert ( latex(Q.submodule([1, x ** 3 / 2], [2, y])) == r"\left< {{\left[ {1},{\frac{x^{3}}{2}} \right]} + {\left< {\left[ {x},{y} \right]},{\left[ {1},{x^{2}} \right]} \right>}},{{\left[ {2},{y} \right]} + {\left< {\left[ {x},{y} \right]},{\left[ {1},{x^{2}} \right]} \right>}} \right>" ) h = homomorphism(QQ.old_poly_ring(x).free_module(2), QQ.old_poly_ring(x).free_module(2), [0, 0]) assert ( latex(h) == r"{\left[\begin{matrix}0 & 0\\0 & 0\end{matrix}\right]} : {{\mathbb{Q}\left[x\right]}^{2}} \to {{\mathbb{Q}\left[x\right]}^{2}}" )
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) raises(ExactQuotientFailed, lambda: X/Y) raises(ExactQuotientFailed, lambda: x/Y) 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.frac_field(x, y) R = QQ.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 raises(ExactQuotientFailed, lambda: X/Y) raises(ExactQuotientFailed, lambda: x/Y) 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[x, y].convert(x), ZZ[x, y]) == X assert R.from_FractionField(Qxy.convert(x), Qxy) == X raises(CoercionFailed, lambda: R.from_FractionField(Qxy.convert(x)/y, Qxy)) raises(ExactQuotientFailed, lambda: X/Y) 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_conversion(): L = QQ.old_poly_ring(x, y, order="ilex") G = QQ.old_poly_ring(x, y) assert L.convert(x) == L.convert(G.convert(x), G) assert G.convert(x) == G.convert(L.convert(x), L) 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 assert RR.get_ring() == RR # XXX: This should also be like RR raises(DomainError, lambda: ALG.get_ring())
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 raises(DomainError, lambda: RR.get_ring()) raises(DomainError, lambda: ALG.get_ring())
def test_PolyElement_gcd(): R, x, y = ring("x,y", QQ) f = QQ(1,2)*x**2 + x + QQ(1,2) g = QQ(1,2)*x + QQ(1,2) assert f.gcd(g) == x + 1
def test_Domain_get_ring(): assert ZZ.has_assoc_Ring == True assert QQ.has_assoc_Ring == True assert ZZ[x].has_assoc_Ring == True assert QQ[x].has_assoc_Ring == True assert ZZ[x,y].has_assoc_Ring == True assert QQ[x,y].has_assoc_Ring == True assert ZZ.frac_field(x).has_assoc_Ring == True assert QQ.frac_field(x).has_assoc_Ring == True assert ZZ.frac_field(x,y).has_assoc_Ring == True assert QQ.frac_field(x,y).has_assoc_Ring == True assert EX.has_assoc_Ring == False assert RR.has_assoc_Ring == False assert ALG.has_assoc_Ring == 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] raises(DomainError, "EX.get_ring()") raises(DomainError, "RR.get_ring()") raises(DomainError, "ALG.get_ring()")
def test_PolyElement_diff(): R, X = xring("x:11", QQ) f = QQ(288,5)*X[0]**8*X[1]**6*X[4]**3*X[10]**2 + 8*X[0]**2*X[2]**3*X[4]**3 +2*X[0]**2 - 2*X[1]**2 assert f.diff(X[0]) == QQ(2304,5)*X[0]**7*X[1]**6*X[4]**3*X[10]**2 + 16*X[0]*X[2]**3*X[4]**3 + 4*X[0] assert f.diff(X[4]) == QQ(864,5)*X[0]**8*X[1]**6*X[4]**2*X[10]**2 + 24*X[0]**2*X[2]**3*X[4]**2 assert f.diff(X[10]) == QQ(576,5)*X[0]**8*X[1]**6*X[4]**3*X[10]
def test_dup_euclidean_prs(): f = QQ.map([1, 0, 1, 0, -3, -3, 8, 2, -5]) g = QQ.map([3, 0, 5, 0, -4, -9, 21]) assert dup_euclidean_prs(f, g, QQ) == [f, g, [-QQ(5,9), QQ(0,1), QQ(1,9), QQ(0,1), -QQ(1,3)], [-QQ(117,25), -QQ(9,1), QQ(441,25)], [QQ(233150,19773), -QQ(102500,6591)], [-QQ(1288744821,543589225)]]
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 raises(NotAlgebraic, lambda: minimal_polynomial(y, x, domain=QQ))
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_Domain___eq__(): assert (ZZ[x,y] == ZZ[x,y]) == True assert (QQ[x,y] == QQ[x,y]) == True assert (ZZ[x,y] == QQ[x,y]) == False assert (QQ[x,y] == ZZ[x,y]) == False assert (ZZ.frac_field(x,y) == ZZ.frac_field(x,y)) == True assert (QQ.frac_field(x,y) == QQ.frac_field(x,y)) == True assert (ZZ.frac_field(x,y) == QQ.frac_field(x,y)) == False assert (QQ.frac_field(x,y) == ZZ.frac_field(x,y)) == False
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 assert RealField()[x] == RR[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[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_conversion(): L = QQ.poly_ring(x, y, order="ilex") G = QQ[x, y] assert L.convert(x) == L.convert(G.convert(x), G) assert G.convert(x) == G.convert(L.convert(x), L) raises(CoercionFailed, 'G.convert(L.convert(1/(1+x)), L)')
def test_QuotientRing(): from sympy.polys.domains import QQ R = QQ.old_poly_ring(x)/[x**2 + 1] assert latex( R) == r"\frac{\mathbb{Q}\left[x\right]}{\left< {x^{2} + 1} \right>}" assert latex(R.one) == r"{1} + {\left< {x^{2} + 1} \right>}"
def test_dmp_lift(): q = [QQ(1, 1), QQ(0, 1), QQ(1, 1)] f = [ ANP([QQ(1, 1)], q, QQ), ANP([], q, QQ), ANP([], q, QQ), ANP([QQ(1, 1), QQ(0, 1)], q, QQ), ANP([QQ(17, 1), QQ(0, 1)], q, QQ), ] assert dmp_lift(f, 0, QQ.algebraic_field(I)) == [ QQ(1), QQ(0), QQ(0), QQ(0), QQ(0), QQ(0), QQ(2), QQ(0), QQ(578), QQ(0), QQ(0), QQ(0), QQ(1), QQ(0), QQ(-578), QQ(0), QQ(83521), ] raises(DomainError, "dmp_lift([EX(1), EX(2)], 0, EX)")
def test_dmp_ext_factor(): R, x,y = ring("x,y", QQ.algebraic_field(sqrt(2))) def anp(x): return ANP(x, [QQ(1), QQ(0), QQ(-2)], QQ) assert R.dmp_ext_factor(0) == (anp([]), []) f = anp([QQ(1)])*x + anp([QQ(1)]) assert R.dmp_ext_factor(f) == (anp([QQ(1)]), [(f, 1)]) g = anp([QQ(2)])*x + anp([QQ(2)]) assert R.dmp_ext_factor(g) == (anp([QQ(2)]), [(f, 1)]) f = anp([QQ(1)])*x**2 + anp([QQ(-2)])*y**2 assert R.dmp_ext_factor(f) == \ (anp([QQ(1)]), [(anp([QQ(1)])*x + anp([QQ(-1), QQ(0)])*y, 1), (anp([QQ(1)])*x + anp([QQ( 1), QQ(0)])*y, 1)]) f = anp([QQ(2)])*x**2 + anp([QQ(-4)])*y**2 assert R.dmp_ext_factor(f) == \ (anp([QQ(2)]), [(anp([QQ(1)])*x + anp([QQ(-1), QQ(0)])*y, 1), (anp([QQ(1)])*x + anp([QQ( 1), QQ(0)])*y, 1)])
def test_PrettyPoly(): from sympy.polys.domains import QQ F = QQ.frac_field(x, y) R = QQ[x, y] assert latex(F.convert(x/(x + y))) == latex(x/(x + y)) assert latex(R.convert(x + y)) == latex(x + y)
def test_dmp_ext_factor(): h = [QQ(1),QQ(0),QQ(-2)] K = QQ.algebraic_field(sqrt(2)) assert dmp_ext_factor([], 0, K) == (ANP([], h, QQ), []) assert dmp_ext_factor([[]], 1, K) == (ANP([], h, QQ), []) f = [[ANP([QQ(1)], h, QQ)], [ANP([QQ(1)], h, QQ)]] assert dmp_ext_factor(f, 1, K) == (ANP([QQ(1)], h, QQ), [(f, 1)]) g = [[ANP([QQ(2)], h, QQ)], [ANP([QQ(2)], h, QQ)]] assert dmp_ext_factor(g, 1, K) == (ANP([QQ(2)], h, QQ), [(f, 1)]) f = [[ANP([QQ(1)], h, QQ)], [], [ANP([QQ(-2)], h, QQ), ANP([], h, QQ), ANP([], h, QQ)]] assert dmp_ext_factor(f, 1, K) == \ (ANP([QQ(1)], h, QQ), [ ([[ANP([QQ(1)], h, QQ)], [ANP([QQ(-1),QQ(0)], h, QQ), ANP([], h, QQ)]], 1), ([[ANP([QQ(1)], h, QQ)], [ANP([QQ( 1),QQ(0)], h, QQ), ANP([], h, QQ)]], 1), ]) f = [[ANP([QQ(2)], h, QQ)], [], [ANP([QQ(-4)], h, QQ), ANP([], h, QQ), ANP([], h, QQ)]] assert dmp_ext_factor(f, 1, K) == \ (ANP([QQ(2)], h, QQ), [ ([[ANP([QQ(1)], h, QQ)], [ANP([QQ(-1),QQ(0)], h, QQ), ANP([], h, QQ)]], 1), ([[ANP([QQ(1)], h, QQ)], [ANP([QQ( 1),QQ(0)], h, QQ), ANP([], h, QQ)]], 1), ])
def test_sring(): x, y, z, t = symbols("x,y,z,t") R = PolyRing("x,y,z", ZZ, lex) assert sring(x + 2*y + 3*z) == (R, R.x + 2*R.y + 3*R.z) R = PolyRing("x,y,z", QQ, lex) 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 = PolyRing("t", ZZ, lex) R = PolyRing("x,y,z", Rt, lex) 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 = PolyRing("t", QQ, lex) R = PolyRing("x,y,z", Rt, lex) 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 = FracField("t", ZZ, lex) R = PolyRing("x,y,z", Rt, lex) 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 = sqrt(2) - sqrt(3) R, a = sring(r, extension=True) assert R.domain == QQ.algebraic_field(r) assert R.gens == () assert a == R.domain.from_sympy(r)
def test_dup_factor_list(): assert dup_factor_list([], ZZ) == (ZZ(0), []) assert dup_factor_list([], QQ) == (QQ(0), []) assert dup_factor_list([], ZZ['y']) == (DMP([],ZZ), []) assert dup_factor_list([], QQ['y']) == (DMP([],QQ), []) assert dup_factor_list_include([], ZZ) == [([], 1)] assert dup_factor_list([ZZ(7)], ZZ) == (ZZ(7), []) assert dup_factor_list([QQ(1,7)], QQ) == (QQ(1,7), []) assert dup_factor_list([DMP([ZZ(7)],ZZ)], ZZ['y']) == (DMP([ZZ(7)],ZZ), []) assert dup_factor_list([DMP([QQ(1,7)],QQ)], QQ['y']) == (DMP([QQ(1,7)],QQ), []) assert dup_factor_list_include([ZZ(7)], ZZ) == [([ZZ(7)], 1)] assert dup_factor_list([ZZ(1),ZZ(2),ZZ(1)], ZZ) == \ (ZZ(1), [([ZZ(1), ZZ(1)], 2)]) assert dup_factor_list([QQ(1,2),QQ(1),QQ(1,2)], QQ) == \ (QQ(1,2), [([QQ(1),QQ(1)], 2)]) assert dup_factor_list_include([ZZ(1),ZZ(2),ZZ(1)], ZZ) == \ [([ZZ(1), ZZ(1)], 2)] K = FF(2) assert dup_factor_list([K(1),K(0),K(1)], K) == \ (K(1), [([K(1), K(1)], 2)]) assert dup_factor_list([RR(1.0),RR(2.0),RR(1.0)], RR) == \ (RR(1.0), [([RR(1.0),RR(1.0)], 2)]) assert dup_factor_list([RR(2.0),RR(4.0),RR(2.0)], RR) == \ (RR(2.0), [([RR(1.0),RR(1.0)], 2)]) f = [DMP([ZZ(4),ZZ(0)],ZZ),DMP([ZZ(4),ZZ(0),ZZ(0)],ZZ),DMP([],ZZ)] assert dup_factor_list(f, ZZ['y']) == \ (DMP([ZZ(4)],ZZ), [([DMP([ZZ(1),ZZ(0)],ZZ)], 1), ([DMP([ZZ(1)],ZZ),DMP([],ZZ)], 1), ([DMP([ZZ(1)],ZZ),DMP([ZZ(1),ZZ(0)],ZZ)], 1)]) f = [DMP([QQ(1,2),QQ(0)],ZZ),DMP([QQ(1,2),QQ(0),QQ(0)],ZZ),DMP([],ZZ)] assert dup_factor_list(f, QQ['y']) == \ (DMP([QQ(1,2)],QQ), [([DMP([QQ(1),QQ(0)],QQ)], 1), ([DMP([QQ(1)],QQ),DMP([],QQ)], 1), ([DMP([QQ(1)],QQ),DMP([QQ(1),QQ(0)],QQ)], 1)]) K = QQ.algebraic_field(I) h = [QQ(1,1), QQ(0,1), QQ(1,1)] f = [ANP([QQ(1,1)], h, QQ), ANP([], h, QQ), ANP([QQ(2,1)], h, QQ), ANP([], h, QQ), ANP([], h, QQ)] assert dup_factor_list(f, K) == \ (ANP([QQ(1,1)], h, QQ), [([ANP([QQ(1,1)], h, QQ), ANP([], h, QQ)], 2), ([ANP([QQ(1,1)], h, QQ), ANP([], h, QQ), ANP([QQ(2,1)], h, QQ)], 1)]) raises(DomainError, "dup_factor_list([EX(sin(1))], EX)")
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
def test_dup_sturm(): assert dup_sturm([QQ(5)], QQ) == [[QQ(1)]] assert dup_sturm([QQ(1),QQ(0)], QQ) == [[QQ(1),QQ(0)], [QQ(1)]] f = QQ.map([1,-2,3,-5]) assert dup_sturm(f, QQ) == \ [f, [QQ(3),QQ(-4),QQ(3)], [QQ(-10,9),QQ(13,3)], [QQ(-3303,100)]]
def test_units(): R = QQ.old_poly_ring(x) assert R.is_unit(R.convert(1)) assert R.is_unit(R.convert(2)) assert not R.is_unit(R.convert(x)) assert not R.is_unit(R.convert(1 + x)) R = QQ.old_poly_ring(x, order='ilex') assert R.is_unit(R.convert(1)) assert R.is_unit(R.convert(2)) assert not R.is_unit(R.convert(x)) assert R.is_unit(R.convert(1 + x)) R = ZZ.old_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_Domain_unify_algebraic(): sqrt5 = QQ.algebraic_field(sqrt(5)) sqrt7 = QQ.algebraic_field(sqrt(7)) sqrt57 = QQ.algebraic_field(sqrt(5), sqrt(7)) assert sqrt5.unify(sqrt7) == sqrt57 assert sqrt5.unify(sqrt5[x, y]) == sqrt5[x, y] assert sqrt5[x, y].unify(sqrt5) == sqrt5[x, y] assert sqrt5.unify(sqrt5.frac_field(x, y)) == sqrt5.frac_field(x, y) assert sqrt5.frac_field(x, y).unify(sqrt5) == sqrt5.frac_field(x, y) assert sqrt5.unify(sqrt7[x, y]) == sqrt57[x, y] assert sqrt5[x, y].unify(sqrt7) == sqrt57[x, y] assert sqrt5.unify(sqrt7.frac_field(x, y)) == sqrt57.frac_field(x, y) assert sqrt5.frac_field(x, y).unify(sqrt7) == sqrt57.frac_field(x, y)
def test_Gaussian_postprocess(): opt = {'gaussian': True} Gaussian.postprocess(opt) assert opt == { 'gaussian': True, 'extension': set([I]), 'domain': QQ.algebraic_field(I), }
def test_Domain_preprocess(): assert Domain.preprocess(ZZ) == ZZ assert Domain.preprocess(QQ) == QQ assert Domain.preprocess(EX) == EX assert Domain.preprocess(FF(2)) == FF(2) assert Domain.preprocess(ZZ[x, y]) == ZZ[x, y] assert Domain.preprocess('Z') == ZZ assert Domain.preprocess('Q') == QQ assert Domain.preprocess('ZZ') == ZZ assert Domain.preprocess('QQ') == QQ assert Domain.preprocess('EX') == EX assert Domain.preprocess('FF(23)') == FF(23) assert Domain.preprocess('GF(23)') == GF(23) raises(OptionError, lambda: Domain.preprocess('Z[]')) assert Domain.preprocess('Z[x]') == ZZ[x] assert Domain.preprocess('Q[x]') == QQ[x] assert Domain.preprocess('ZZ[x]') == ZZ[x] assert Domain.preprocess('QQ[x]') == QQ[x] assert Domain.preprocess('Z[x,y]') == ZZ[x, y] assert Domain.preprocess('Q[x,y]') == QQ[x, y] assert Domain.preprocess('ZZ[x,y]') == ZZ[x, y] assert Domain.preprocess('QQ[x,y]') == QQ[x, y] raises(OptionError, lambda: Domain.preprocess('Z()')) assert Domain.preprocess('Z(x)') == ZZ.frac_field(x) assert Domain.preprocess('Q(x)') == QQ.frac_field(x) assert Domain.preprocess('ZZ(x)') == ZZ.frac_field(x) assert Domain.preprocess('QQ(x)') == QQ.frac_field(x) assert Domain.preprocess('Z(x,y)') == ZZ.frac_field(x, y) assert Domain.preprocess('Q(x,y)') == QQ.frac_field(x, y) assert Domain.preprocess('ZZ(x,y)') == ZZ.frac_field(x, y) assert Domain.preprocess('QQ(x,y)') == QQ.frac_field(x, y) assert Domain.preprocess('Q<I>') == QQ.algebraic_field(I) assert Domain.preprocess('QQ<I>') == QQ.algebraic_field(I) assert Domain.preprocess('Q<sqrt(2), I>') == QQ.algebraic_field(sqrt(2), I) assert Domain.preprocess( 'QQ<sqrt(2), I>') == QQ.algebraic_field(sqrt(2), I) raises(OptionError, lambda: Domain.preprocess('abc'))
def test_eqs_to_matrix(): domain, x1,x2 = ring("x1,x2", QQ) eqs_coeff = [{x1: QQ(1), x2: QQ(1)}, {x1: QQ(2), x2: QQ(-1)}] eqs_rhs = [QQ(-5), QQ(0)] M = eqs_to_matrix(eqs_coeff, eqs_rhs, [x1, x2], QQ) assert M.to_Matrix() == Matrix([[1, 1, 5], [2, -1, 0]])
def test_PythonRational__sub__(): assert QQ(-1, 2) - QQ(1, 2) == QQ(-1) assert QQ(1, 2) - QQ(-1, 2) == QQ(1) assert QQ(1, 2) - QQ(1, 2) == QQ(0) assert QQ(1, 2) - QQ(3, 2) == QQ(-1) assert QQ(3, 2) - QQ(1, 2) == QQ(1) assert QQ(3, 2) - QQ(3, 2) == QQ(0) assert 1 - QQ(1, 2) == QQ(1, 2) assert QQ(1, 2) - 1 == QQ(-1, 2)
def test_sdp_LC(): assert sdp_LC([], QQ) == QQ(0) assert sdp_LC([((1, 0), QQ(1, 2))], QQ) == QQ(1, 2) assert sdp_LC([((1, 1), QQ(1, 4)), ((1, 0), QQ(1, 2))], QQ) == QQ(1, 4)
def test_sdp_monoms(): assert sdp_monoms([]) == [] assert sdp_monoms([((1, 0), QQ(1, 2))]) == [(1, 0)] assert sdp_monoms([((1, 1), QQ(1, 4)), ((1, 0), QQ(1, 2))]) == [(1, 1), (1, 0)]
def test_sdp_LM(): assert sdp_LM([], 1) == (0, 0) assert sdp_LM([((1, 0), QQ(1, 2))], 1) == (1, 0) assert sdp_LM([((1, 1), QQ(1, 4)), ((1, 0), QQ(1, 2))], 1) == (1, 1)
def test_PythonRational__neg__(): assert -QQ(-1, 2) == QQ(1, 2) assert -QQ(1, 2) == QQ(-1, 2)
def test_PythonRational__init__(): assert QQ(0).numerator == 0 assert QQ(0).denominator == 1 assert QQ(0, 1).numerator == 0 assert QQ(0, 1).denominator == 1 assert QQ(0, -1).numerator == 0 assert QQ(0, -1).denominator == 1 assert QQ(1).numerator == 1 assert QQ(1).denominator == 1 assert QQ(1, 1).numerator == 1 assert QQ(1, 1).denominator == 1 assert QQ(-1, -1).numerator == 1 assert QQ(-1, -1).denominator == 1 assert QQ(-1).numerator == -1 assert QQ(-1).denominator == 1 assert QQ(-1, 1).numerator == -1 assert QQ(-1, 1).denominator == 1 assert QQ(1, -1).numerator == -1 assert QQ(1, -1).denominator == 1 assert QQ(1, 2).numerator == 1 assert QQ(1, 2).denominator == 2 assert QQ(3, 4).numerator == 3 assert QQ(3, 4).denominator == 4 assert QQ(2, 2).numerator == 1 assert QQ(2, 2).denominator == 1 assert QQ(2, 4).numerator == 1 assert QQ(2, 4).denominator == 2
dup_l1_norm, dmp_l1_norm, dup_expand, dmp_expand, ) from sympy.polys.polyerrors import ( ExactQuotientFailed, ) from sympy.polys.specialpolys import f_polys from sympy.polys.domains import FF, ZZ, QQ from sympy.utilities.pytest import raises f_0, f_1, f_2, f_3, f_4, f_5, f_6 = [f.to_dense() for f in f_polys()] F_0 = dmp_mul_ground(dmp_normal(f_0, 2, QQ), QQ(1, 7), 2, QQ) def test_dup_add_term(): f = dup_normal([], ZZ) assert dup_add_term(f, ZZ(0), 0, ZZ) == dup_normal([], ZZ) assert dup_add_term(f, ZZ(1), 0, ZZ) == dup_normal([1], ZZ) assert dup_add_term(f, ZZ(1), 1, ZZ) == dup_normal([1, 0], ZZ) assert dup_add_term(f, ZZ(1), 2, ZZ) == dup_normal([1, 0, 0], ZZ) f = dup_normal([1, 1, 1], ZZ) assert dup_add_term(f, ZZ(1), 0, ZZ) == dup_normal([1, 1, 2], ZZ) assert dup_add_term(f, ZZ(1), 1, ZZ) == dup_normal([1, 2, 1], ZZ)
def test_dmp_mul_ground(): assert dmp_mul_ground(f_0, ZZ(2), 2, ZZ) == [[[ZZ(2), ZZ(4), ZZ(6)], [ZZ(4)]], [[ZZ(6)]], [[ZZ(8), ZZ(10), ZZ(12)], [ZZ(2), ZZ(4), ZZ(2)], [ZZ(2)]]] assert dmp_mul_ground(F_0, QQ(1, 2), 2, QQ) == [[[QQ(1, 14), QQ(2, 14), QQ(3, 14)], [QQ(2, 14)]], [[QQ(3, 14)]], [[QQ(4, 14), QQ(5, 14), QQ(6, 14)], [QQ(1, 14), QQ(2, 14), QQ(1, 14)], [QQ(1, 14)]]]
def test_sdp_groebner(): f = sdp_from_dict({(1, 2): QQ(2, ), (2, 0): QQ(1)}, O_lex) g = sdp_from_dict({(0, 3): QQ(2), (1, 1): QQ(1), (0, 0): QQ(-1)}, O_lex) a = sdp_from_dict({(1, 0): QQ(1, 1)}, O_lex) b = sdp_from_dict({(0, 3): QQ(1, 1), (0, 0): QQ(-1, 2)}, O_lex) assert sdp_groebner((f, g), 1, O_lex, QQ) == [a, b] f = sdp_from_dict({(2, 1): QQ(2, ), (0, 2): QQ(1)}, O_lex) g = sdp_from_dict({(3, 0): QQ(2), (1, 1): QQ(1), (0, 0): QQ(-1)}, O_lex) a = sdp_from_dict({(0, 1): QQ(1, 1)}, O_lex) b = sdp_from_dict({(3, 0): QQ(1, 1), (0, 0): QQ(-1, 2)}, O_lex) assert sdp_groebner((f, g), 1, O_lex, QQ) == [b, a] f = sdp_from_dict({(0, 0, 2): QQ(-1), (1, 0, 0): QQ(1)}, O_lex) g = sdp_from_dict({(0, 0, 3): QQ(-1), (0, 1, 0): QQ(1)}, O_lex) assert sdp_groebner((f, g), 1, O_lex, QQ) == [f, g] f = sdp_from_dict({(3, 0): QQ(1), (1, 1): QQ(-2)}, O_grlex) g = sdp_from_dict({(2, 1): QQ(1), (0, 2): QQ(-2), (1, 0): QQ(1)}, O_grlex) a = sdp_from_dict({(2, 0): QQ(1)}, O_grlex) b = sdp_from_dict({(1, 1): QQ(1)}, O_grlex) c = sdp_from_dict({(0, 2): QQ(1), (1, 0): QQ(-1, 2)}, O_grlex) assert sdp_groebner((f, g), 1, O_grlex, QQ) == [a, b, c] f = sdp_from_dict({(2, 0, 0): -QQ(1), (0, 1, 0): QQ(1)}, O_lex) g = sdp_from_dict({(3, 0, 0): -QQ(1), (0, 0, 1): QQ(1)}, O_lex) assert sdp_groebner((f, g), 2, O_lex, QQ) == [ sdp_from_dict({ (2, 0, 0): QQ(1), (0, 1, 0): -QQ(1) }, O_lex), sdp_from_dict({ (1, 1, 0): QQ(1), (0, 0, 1): -QQ(1) }, O_lex), sdp_from_dict({ (1, 0, 1): QQ(1), (0, 2, 0): -QQ(1) }, O_lex), sdp_from_dict({ (0, 3, 0): QQ(1), (0, 0, 2): -QQ(1) }, O_lex), ] f = sdp_from_dict({(2, 0, 0): -QQ(1), (0, 1, 0): QQ(1)}, O_grlex) g = sdp_from_dict({(3, 0, 0): -QQ(1), (0, 0, 1): QQ(1)}, O_grlex) assert sdp_groebner((f, g), 2, O_grlex, QQ) == [ sdp_from_dict({ (0, 3, 0): QQ(1), (0, 0, 2): -QQ(1) }, O_grlex), sdp_from_dict({ (2, 0, 0): QQ(1), (0, 1, 0): -QQ(1) }, O_grlex), sdp_from_dict({ (1, 1, 0): QQ(1), (0, 0, 1): -QQ(1) }, O_grlex), sdp_from_dict({ (1, 0, 1): QQ(1), (0, 2, 0): -QQ(1) }, O_grlex), ] f = sdp_from_dict({(2, 0, 0): -QQ(1), (0, 0, 1): QQ(1)}, O_lex) g = sdp_from_dict({(3, 0, 0): -QQ(1), (0, 1, 0): QQ(1)}, O_lex) assert sdp_groebner((f, g), 2, O_lex, QQ) == [ sdp_from_dict({ (2, 0, 0): QQ(1), (0, 0, 1): -QQ(1) }, O_lex), sdp_from_dict({ (1, 1, 0): QQ(1), (0, 0, 2): -QQ(1) }, O_lex), sdp_from_dict({ (1, 0, 1): QQ(1), (0, 1, 0): -QQ(1) }, O_lex), sdp_from_dict({ (0, 2, 0): QQ(1), (0, 0, 3): -QQ(1) }, O_lex), ] f = sdp_from_dict({(2, 0, 0): -QQ(1), (0, 0, 1): QQ(1)}, O_grlex) g = sdp_from_dict({(3, 0, 0): -QQ(1), (0, 1, 0): QQ(1)}, O_grlex) assert sdp_groebner((f, g), 2, O_grlex, QQ) == [ sdp_from_dict({ (0, 0, 3): QQ(1), (0, 2, 0): -QQ(1) }, O_grlex), sdp_from_dict({ (2, 0, 0): QQ(1), (0, 0, 1): -QQ(1) }, O_grlex), sdp_from_dict({ (1, 1, 0): QQ(1), (0, 0, 2): -QQ(1) }, O_grlex), sdp_from_dict({ (1, 0, 1): QQ(1), (0, 1, 0): -QQ(1) }, O_grlex), ] f = sdp_from_dict({(0, 2, 0): -QQ(1), (1, 0, 0): QQ(1)}, O_lex) g = sdp_from_dict({(0, 3, 0): -QQ(1), (0, 0, 1): QQ(1)}, O_lex) assert sdp_groebner((f, g), 2, O_lex, QQ) == [ sdp_from_dict({ (1, 0, 0): QQ(1), (0, 2, 0): -QQ(1) }, O_lex), sdp_from_dict({ (0, 3, 0): QQ(1), (0, 0, 1): -QQ(1) }, O_lex), ] f = sdp_from_dict({(0, 2, 0): -QQ(1), (1, 0, 0): QQ(1)}, O_grlex) g = sdp_from_dict({(0, 3, 0): -QQ(1), (0, 0, 1): QQ(1)}, O_grlex) assert sdp_groebner((f, g), 2, O_grlex, QQ) == [ sdp_from_dict({ (2, 0, 0): QQ(1), (0, 1, 1): -QQ(1) }, O_grlex), sdp_from_dict({ (1, 1, 0): QQ(1), (0, 0, 1): -QQ(1) }, O_grlex), sdp_from_dict({ (0, 2, 0): QQ(1), (1, 0, 0): -QQ(1) }, O_grlex), ] f = sdp_from_dict({(0, 0, 2): -QQ(1), (1, 0, 0): QQ(1)}, O_lex) g = sdp_from_dict({(0, 0, 3): -QQ(1), (0, 1, 0): QQ(1)}, O_lex) assert sdp_groebner((f, g), 2, O_lex, QQ) == [ sdp_from_dict({ (1, 0, 0): QQ(1), (0, 0, 2): -QQ(1) }, O_lex), sdp_from_dict({ (0, 1, 0): QQ(1), (0, 0, 3): -QQ(1) }, O_lex), ] f = sdp_from_dict({(0, 0, 2): -QQ(1), (1, 0, 0): QQ(1)}, O_grlex) g = sdp_from_dict({(0, 0, 3): -QQ(1), (0, 1, 0): QQ(1)}, O_grlex) assert sdp_groebner((f, g), 2, O_grlex, QQ) == [ sdp_from_dict({ (2, 0, 0): QQ(1), (0, 1, 1): -QQ(1) }, O_grlex), sdp_from_dict({ (1, 0, 1): QQ(1), (0, 1, 0): -QQ(1) }, O_grlex), sdp_from_dict({ (0, 0, 2): QQ(1), (1, 0, 0): -QQ(1) }, O_grlex), ] f = sdp_from_dict({(0, 2, 0): -QQ(1), (0, 0, 1): QQ(1)}, O_lex) g = sdp_from_dict({(0, 3, 0): -QQ(1), (1, 0, 0): QQ(1)}, O_lex) assert sdp_groebner((f, g), 2, O_lex, QQ) == [ sdp_from_dict({ (1, 0, 0): QQ(1), (0, 1, 1): -QQ(1) }, O_lex), sdp_from_dict({ (0, 2, 0): QQ(1), (0, 0, 1): -QQ(1) }, O_lex), ] f = sdp_from_dict({(0, 2, 0): -QQ(1), (0, 0, 1): QQ(1)}, O_grlex) g = sdp_from_dict({(0, 3, 0): -QQ(1), (1, 0, 0): QQ(1)}, O_grlex) assert sdp_groebner((f, g), 2, O_grlex, QQ) == [ sdp_from_dict({ (0, 0, 3): QQ(1), (2, 0, 0): -QQ(1) }, O_grlex), sdp_from_dict({ (1, 1, 0): QQ(1), (0, 0, 2): -QQ(1) }, O_grlex), sdp_from_dict({ (0, 2, 0): QQ(1), (0, 0, 1): -QQ(1) }, O_grlex), sdp_from_dict({ (0, 1, 1): QQ(1), (1, 0, 0): -QQ(1) }, O_grlex), ] f = sdp_from_dict({(0, 0, 2): -QQ(1), (0, 1, 0): QQ(1)}, O_lex) g = sdp_from_dict({(0, 0, 3): -QQ(1), (1, 0, 0): QQ(1)}, O_lex) assert sdp_groebner((f, g), 2, O_lex, QQ) == [ sdp_from_dict({ (1, 0, 0): QQ(1), (0, 0, 3): -QQ(1) }, O_lex), sdp_from_dict({ (0, 1, 0): QQ(1), (0, 0, 2): -QQ(1) }, O_lex), ] f = sdp_from_dict({(0, 0, 2): -QQ(1), (0, 1, 0): QQ(1)}, O_grlex) g = sdp_from_dict({(0, 0, 3): -QQ(1), (1, 0, 0): QQ(1)}, O_grlex) assert sdp_groebner((f, g), 2, O_grlex, QQ) == [ sdp_from_dict({ (0, 3, 0): QQ(1), (2, 0, 0): -QQ(1) }, O_grlex), sdp_from_dict({ (1, 0, 1): QQ(1), (0, 2, 0): -QQ(1) }, O_grlex), sdp_from_dict({ (0, 1, 1): QQ(1), (1, 0, 0): -QQ(1) }, O_grlex), sdp_from_dict({ (0, 0, 2): QQ(1), (0, 1, 0): -QQ(1) }, O_grlex), ] f = sdp_from_dict({(2, 2): QQ(4), (1, 1): QQ(4), (0, 0): QQ(1)}, O_lex) g = sdp_from_dict({(2, 0): QQ(1), (0, 2): QQ(1), (0, 0): -QQ(1)}, O_lex) assert sdp_groebner((f, g), 1, O_lex, QQ) == [ sdp_from_dict( { (1, 0): QQ(1, 1), (0, 7): QQ(-4, 1), (0, 5): QQ(8, 1), (0, 3): QQ(-7, 1), (0, 1): QQ(3, 1) }, O_lex), sdp_from_dict( { (0, 8): QQ(1, 1), (0, 6): QQ(-2, 1), (0, 4): QQ(3, 2), (0, 2): QQ(-1, 2), (0, 0): QQ(1, 16) }, O_lex), ] raises(DomainError, "sdp_groebner([], 1, O_lex, ZZ)")
def test_construct_domain(): assert construct_domain([1, 2, 3]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)]) assert construct_domain([1, 2, 3], field=True) == (QQ, [QQ(1), QQ(2), QQ(3)]) assert construct_domain([S.One, S(2), S(3)]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)]) assert construct_domain([S.One, S(2), S(3)], field=True) == (QQ, [QQ(1), QQ(2), QQ(3)]) assert construct_domain([S.Half, S(2)]) == (QQ, [QQ(1, 2), QQ(2)]) result = construct_domain([3.14, 1, S.Half]) assert isinstance(result[0], RealField) assert result[1] == [RR(3.14), RR(1.0), RR(0.5)] result = construct_domain([3.14, I, S.Half]) assert isinstance(result[0], ComplexField) assert result[1] == [CC(3.14), CC(1.0j), CC(0.5)] assert construct_domain([1.0 + I]) == (CC, [CC(1.0, 1.0)]) assert construct_domain([2.0 + 3.0 * I]) == (CC, [CC(2.0, 3.0)]) assert construct_domain([1, I]) == (ZZ_I, [ZZ_I(1, 0), ZZ_I(0, 1)]) assert construct_domain([1, I / 2]) == (QQ_I, [QQ_I(1, 0), QQ_I(0, S.Half)]) assert construct_domain([3.14, sqrt(2)], extension=None) == (EX, [EX(3.14), EX(sqrt(2))]) assert construct_domain([3.14, sqrt(2)], extension=True) == (EX, [EX(3.14), EX(sqrt(2))]) assert construct_domain([1, sqrt(2)], extension=None) == (EX, [EX(1), EX(sqrt(2))]) assert construct_domain([x, sqrt(x)]) == (EX, [EX(x), EX(sqrt(x))]) assert construct_domain([x, sqrt(x), sqrt(y) ]) == (EX, [EX(x), EX(sqrt(x)), EX(sqrt(y))]) alg = QQ.algebraic_field(sqrt(2)) assert construct_domain([7, S.Half, sqrt(2)], extension=True) == \ (alg, [alg.convert(7), alg.convert(S.Half), alg.convert(sqrt(2))]) alg = QQ.algebraic_field(sqrt(2) + sqrt(3)) assert construct_domain([7, sqrt(2), sqrt(3)], extension=True) == \ (alg, [alg.convert(7), alg.convert(sqrt(2)), alg.convert(sqrt(3))]) dom = ZZ[x] assert construct_domain([2*x, 3]) == \ (dom, [dom.convert(2*x), dom.convert(3)]) dom = ZZ[x, y] assert construct_domain([2*x, 3*y]) == \ (dom, [dom.convert(2*x), dom.convert(3*y)]) dom = QQ[x] assert construct_domain([x/2, 3]) == \ (dom, [dom.convert(x/2), dom.convert(3)]) dom = QQ[x, y] assert construct_domain([x/2, 3*y]) == \ (dom, [dom.convert(x/2), dom.convert(3*y)]) dom = ZZ_I[x] assert construct_domain([2*x, I]) == \ (dom, [dom.convert(2*x), dom.convert(I)]) dom = ZZ_I[x, y] assert construct_domain([2*x, I*y]) == \ (dom, [dom.convert(2*x), dom.convert(I*y)]) dom = QQ_I[x] assert construct_domain([x/2, I]) == \ (dom, [dom.convert(x/2), dom.convert(I)]) dom = QQ_I[x, y] assert construct_domain([x/2, I*y]) == \ (dom, [dom.convert(x/2), dom.convert(I*y)]) dom = RR[x] assert construct_domain([x/2, 3.5]) == \ (dom, [dom.convert(x/2), dom.convert(3.5)]) dom = RR[x, y] assert construct_domain([x/2, 3.5*y]) == \ (dom, [dom.convert(x/2), dom.convert(3.5*y)]) dom = CC[x] assert construct_domain([I*x/2, 3.5]) == \ (dom, [dom.convert(I*x/2), dom.convert(3.5)]) dom = CC[x, y] assert construct_domain([I*x/2, 3.5*y]) == \ (dom, [dom.convert(I*x/2), dom.convert(3.5*y)]) dom = CC[x] assert construct_domain([x/2, I*3.5]) == \ (dom, [dom.convert(x/2), dom.convert(I*3.5)]) dom = CC[x, y] assert construct_domain([x/2, I*3.5*y]) == \ (dom, [dom.convert(x/2), dom.convert(I*3.5*y)]) dom = ZZ.frac_field(x) assert construct_domain([2/x, 3]) == \ (dom, [dom.convert(2/x), dom.convert(3)]) dom = ZZ.frac_field(x, y) assert construct_domain([2/x, 3*y]) == \ (dom, [dom.convert(2/x), dom.convert(3*y)]) dom = RR.frac_field(x) assert construct_domain([2/x, 3.5]) == \ (dom, [dom.convert(2/x), dom.convert(3.5)]) dom = RR.frac_field(x, y) assert construct_domain([2/x, 3.5*y]) == \ (dom, [dom.convert(2/x), dom.convert(3.5*y)]) dom = RealField(prec=336)[x] assert construct_domain([pi.evalf(100)*x]) == \ (dom, [dom.convert(pi.evalf(100)*x)]) assert construct_domain(2) == (ZZ, ZZ(2)) assert construct_domain(S(2) / 3) == (QQ, QQ(2, 3)) assert construct_domain(Rational(2, 3)) == (QQ, QQ(2, 3)) assert construct_domain({}) == (ZZ, {})
def _do_test_benchmark_minpoly(): R, x, y, z = ring("x,y,z", QQ, lex) F = [x**3 + x + 1, y**2 + y + 1, (x + y) * z - (x**2 + y)] G = [ x + QQ(155, 2067) * z**5 - QQ(355, 689) * z**4 + QQ(6062, 2067) * z**3 - QQ(3687, 689) * z**2 + QQ(6878, 2067) * z - QQ(25, 53), y + QQ(4, 53) * z**5 - QQ(91, 159) * z**4 + QQ(523, 159) * z**3 - QQ(387, 53) * z**2 + QQ(1043, 159) * z - QQ(308, 159), z**6 - 7 * z**5 + 41 * z**4 - 82 * z**3 + 89 * z**2 - 46 * z + 13 ] assert groebner(F, R) == G
def test_PythonRational__lt_le_gt_ge__(): assert (QQ(1, 2) < QQ(1, 4)) is False assert (QQ(1, 2) <= QQ(1, 4)) is False assert (QQ(1, 2) > QQ(1, 4)) is True assert (QQ(1, 2) >= QQ(1, 4)) is True assert (QQ(1, 4) < QQ(1, 2)) is True assert (QQ(1, 4) <= QQ(1, 2)) is True assert (QQ(1, 4) > QQ(1, 2)) is False assert (QQ(1, 4) >= QQ(1, 2)) is False
def _do_test_groebner(): R, x, y = ring("x,y", QQ, lex) f = x**2 + 2 * x * y**2 g = x * y + 2 * y**3 - 1 assert groebner([f, g], R) == [x, y**3 - QQ(1, 2)] R, y, x = ring("y,x", QQ, lex) f = 2 * x**2 * y + y**2 g = 2 * x**3 + x * y - 1 assert groebner([f, g], R) == [y, x**3 - QQ(1, 2)] R, x, y, z = ring("x,y,z", QQ, lex) f = x - z**2 g = y - z**3 assert groebner([f, g], R) == [f, g] R, x, y = ring("x,y", QQ, grlex) f = x**3 - 2 * x * y g = x**2 * y + x - 2 * y**2 assert groebner([f, g], R) == [x**2, x * y, -QQ(1, 2) * x + y**2] R, x, y, z = ring("x,y,z", QQ, lex) f = -x**2 + y g = -x**3 + z assert groebner([f, g], R) == [x**2 - y, x * y - z, x * z - y**2, y**3 - z**2] R, x, y, z = ring("x,y,z", QQ, grlex) f = -x**2 + y g = -x**3 + z assert groebner([f, g], R) == [y**3 - z**2, x**2 - y, x * y - z, x * z - y**2] R, x, y, z = ring("x,y,z", QQ, lex) f = -x**2 + z g = -x**3 + y assert groebner([f, g], R) == [x**2 - z, x * y - z**2, x * z - y, y**2 - z**3] R, x, y, z = ring("x,y,z", QQ, grlex) f = -x**2 + z g = -x**3 + y assert groebner([f, g], R) == [-y**2 + z**3, x**2 - z, x * y - z**2, x * z - y] R, x, y, z = ring("x,y,z", QQ, lex) f = x - y**2 g = -y**3 + z assert groebner([f, g], R) == [x - y**2, y**3 - z] R, x, y, z = ring("x,y,z", QQ, grlex) f = x - y**2 g = -y**3 + z assert groebner([f, g], R) == [x**2 - y * z, x * y - z, -x + y**2] R, x, y, z = ring("x,y,z", QQ, lex) f = x - z**2 g = y - z**3 assert groebner([f, g], R) == [x - z**2, y - z**3] R, x, y, z = ring("x,y,z", QQ, grlex) f = x - z**2 g = y - z**3 assert groebner([f, g], R) == [x**2 - y * z, x * z - y, -x + z**2] R, x, y, z = ring("x,y,z", QQ, lex) f = -y**2 + z g = x - y**3 assert groebner([f, g], R) == [x - y * z, y**2 - z] R, x, y, z = ring("x,y,z", QQ, grlex) f = -y**2 + z g = x - y**3 assert groebner([f, g], R) == [-x**2 + z**3, x * y - z**2, y**2 - z, -x + y * z] R, x, y, z = ring("x,y,z", QQ, lex) f = y - z**2 g = x - z**3 assert groebner([f, g], R) == [x - z**3, y - z**2] R, x, y, z = ring("x,y,z", QQ, grlex) f = y - z**2 g = x - z**3 assert groebner([f, g], R) == [-x**2 + y**3, x * z - y**2, -x + y * z, -y + z**2] R, x, y, z = ring("x,y,z", QQ, lex) f = 4 * x**2 * y**2 + 4 * x * y + 1 g = x**2 + y**2 - 1 assert groebner([f, g], R) == [ x - 4 * y**7 + 8 * y**5 - 7 * y**3 + 3 * y, y**8 - 2 * y**6 + QQ(3, 2) * y**4 - QQ(1, 2) * y**2 + QQ(1, 16), ]
def test_sdp_div(): f = sdp_from_dict({ (2, 1): 4, (1, 1): -2, (1, 0): 4, (0, 1): -2, (0, 0): 8 }, O_grlex) assert sdp_div(f, [sdp_from_dict({(0,0): 2}, O_grlex)], 1, O_grlex, ZZ) == \ ([sdp_from_dict({(2,1): 2, (1,1): -1, (1,0): 2, (0,1): -1, (0,0): 4}, O_grlex)], []) assert sdp_div(f, [sdp_from_dict({(0,1): 2}, O_grlex)], 1, O_grlex, ZZ) == \ ([sdp_from_dict({(2,0): 2, (1,0): -1, (0,0): -1}, O_grlex)], sdp_from_dict({(1,0): 4, (0,0): 8}, O_grlex)) f = sdp_from_dict({(1, 0): 1, (0, 0): -1}, O_grlex) g = sdp_from_dict({(0, 1): 1, (0, 0): -1}, O_grlex) assert sdp_div(f, [g], 1, O_grlex, ZZ) == ([[]], f) f = sdp_from_dict({(3, ): 1, (2, ): -12, (0, ): -42}, O_grlex) g = sdp_from_dict({(1, ): 1, (0, ): -3}, O_grlex) q = sdp_from_dict({(2, ): 1, (1, ): -9, (0, ): -27}, O_grlex) r = sdp_from_dict({(0, ): -123}, O_grlex) assert sdp_div(f, [g], 0, O_grlex, ZZ) == ([q], r) f = sdp_from_dict({(2, ): QQ(1), (1, ): QQ(2), (0, ): QQ(2)}, O_grlex) g = sdp_from_dict({(0, ): QQ(1)}, O_grlex) h = sdp_from_dict({(0, ): QQ(2)}, O_grlex) q = sdp_from_dict({(2, ): QQ(1, 2), (1, ): QQ(1), (0, ): QQ(1)}, O_grlex) assert sdp_div(f, [g], 0, O_grlex, QQ) == ([f], []) assert sdp_div(f, [h], 0, O_grlex, QQ) == ([q], []) f = sdp_from_dict({(1, 2): 1, (0, 0): 1}, O_grlex) G = [ sdp_from_dict({ (1, 1): 1, (0, 0): 1 }, O_grlex), sdp_from_dict({ (0, 1): 1, (0, 0): 1 }, O_grlex) ] Q = [ sdp_from_dict({(0, 1): 1}, O_grlex), sdp_from_dict({(0, 0): -1}, O_grlex) ] r = sdp_from_dict({(0, 0): 2}, O_grlex) assert sdp_div(f, G, 1, O_grlex, ZZ) == (Q, r) f = sdp_from_dict({(2, 1): 1, (1, 2): 1, (0, 2): 1}, O_grlex) G = [ sdp_from_dict({ (1, 1): 1, (0, 0): -1 }, O_grlex), sdp_from_dict({ (0, 2): 1, (0, 0): -1 }, O_grlex) ] Q = [ sdp_from_dict({ (1, 0): 1, (0, 1): 1 }, O_grlex), sdp_from_dict({(0, 0): 1}, O_grlex) ] r = sdp_from_dict({(1, 0): 1, (0, 1): 1, (0, 0): 1}, O_grlex) assert sdp_div(f, G, 1, O_grlex, ZZ) == (Q, r) G = [ sdp_from_dict({ (0, 2): 1, (0, 0): -1 }, O_grlex), sdp_from_dict({ (1, 1): 1, (0, 0): -1 }, O_grlex) ] Q = [ sdp_from_dict({ (1, 0): 1, (0, 0): 1 }, O_grlex), sdp_from_dict({(1, 0): 1}, O_grlex) ] r = sdp_from_dict({(1, 0): 2, (0, 0): 1}, O_grlex) assert sdp_div(f, G, 1, O_grlex, ZZ) == (Q, r)
def test_PythonRational__float__(): assert float(QQ(-1, 2)) == -0.5 assert float(QQ(1, 2)) == 0.5
def test_PythonRational__hash__(): assert hash(QQ(0)) == hash(0) assert hash(QQ(1)) == hash(1) assert hash(QQ(117)) == hash(117)
def test_representing_matrices(): R, x, y = ring("x,y", QQ, grlex) basis = [(0, 0), (0, 1), (1, 0), (1, 1)] F = [x**2 - x - 3 * y + 1, -2 * x + y**2 + y - 1] assert _representing_matrices( basis, F, R) == [[[QQ(0, 1), QQ(0, 1), -QQ(1, 1), QQ(3, 1)], [QQ(0, 1), QQ(0, 1), QQ(3, 1), -QQ(4, 1)], [QQ(1, 1), QQ(0, 1), QQ(1, 1), QQ(6, 1)], [QQ(0, 1), QQ(1, 1), QQ(0, 1), QQ(1, 1)]], [[QQ(0, 1), QQ(1, 1), QQ(0, 1), -QQ(2, 1)], [QQ(1, 1), -QQ(1, 1), QQ(0, 1), QQ(6, 1)], [QQ(0, 1), QQ(2, 1), QQ(0, 1), QQ(3, 1)], [QQ(0, 1), QQ(0, 1), QQ(1, 1), -QQ(1, 1)]]]
def test_PythonRational__add__(): assert QQ(-1, 2) + QQ(1, 2) == QQ(0) assert QQ(1, 2) + QQ(-1, 2) == QQ(0) assert QQ(1, 2) + QQ(1, 2) == QQ(1) assert QQ(1, 2) + QQ(3, 2) == QQ(2) assert QQ(3, 2) + QQ(1, 2) == QQ(2) assert QQ(3, 2) + QQ(3, 2) == QQ(3) assert 1 + QQ(1, 2) == QQ(3, 2) assert QQ(1, 2) + 1 == QQ(3, 2)
def test_sdp_del_LT(): assert sdp_del_LT([]) == [] assert sdp_del_LT([((1, 0), QQ(1, 2))]) == [] assert sdp_del_LT([((1, 1), QQ(1, 4)), ((1, 0), QQ(1, 2))]) == [((1, 0), QQ(1, 2))]
def test_PythonRational__pos__(): assert +QQ(-1, 2) == QQ(-1, 2) assert +QQ(1, 2) == QQ(1, 2)
def test_sdp_LT(): assert sdp_LT([], 1, QQ) == ((0, 0), QQ(0)) assert sdp_LT([((1, 0), QQ(1, 2))], 1, QQ) == ((1, 0), QQ(1, 2)) assert sdp_LT([((1, 1), QQ(1, 4)), ((1, 0), QQ(1, 2))], 1, QQ) == ((1, 1), QQ(1, 4))
def test_PythonRational__abs__(): assert abs(QQ(-1, 2)) == QQ(1, 2) assert abs(QQ(1, 2)) == QQ(1, 2)
def test_sdp_coeffs(): assert sdp_coeffs([]) == [] assert sdp_coeffs([((1, 0), QQ(1, 2))]) == [QQ(1, 2)] assert sdp_coeffs([((1, 1), QQ(1, 4)), ((1, 0), QQ(1, 2))]) == [QQ(1, 4), QQ(1, 2)]
def test_PythonRational__int__(): assert int(QQ(-1, 4)) == 0 assert int(QQ(1, 4)) == 0 assert int(QQ(-5, 4)) == -1 assert int(QQ(5, 4)) == 1
def test_PythonRational__mul__(): assert QQ(-1, 2) * QQ(1, 2) == QQ(-1, 4) assert QQ(1, 2) * QQ(-1, 2) == QQ(-1, 4) assert QQ(1, 2) * QQ(1, 2) == QQ(1, 4) assert QQ(1, 2) * QQ(3, 2) == QQ(3, 4) assert QQ(3, 2) * QQ(1, 2) == QQ(3, 4) assert QQ(3, 2) * QQ(3, 2) == QQ(9, 4) assert 2 * QQ(1, 2) == QQ(1) assert QQ(1, 2) * 2 == QQ(1)
def test_PythonRational__pow__(): assert QQ(1)**10 == QQ(1) assert QQ(2)**10 == QQ(1024) assert QQ(1)**(-10) == QQ(1) assert QQ(2)**(-10) == QQ(1, 1024)
def test_is_rewritable_or_comparable(): # from katsura4 with grlex R, x, y, z, t = ring("x,y,z,t", QQ, grlex) p = lbp(sig((0, 0, 2, 1), 2), R.zero, 2) B = [ lbp( sig((0, 0, 0, 1), 2), QQ(2, 45) * y**2 + QQ(1, 5) * y * z + QQ(5, 63) * y * t + z**2 * t + QQ(4, 45) * z**2 + QQ(76, 35) * z * t**2 - QQ(32, 105) * z * t + QQ(13, 7) * t**3 - QQ(13, 21) * t**2, 6) ] # rewritable: assert is_rewritable_or_comparable(Sign(p), Num(p), B) is True p = lbp(sig((0, 1, 1, 0), 2), R.zero, 7) B = [ lbp( sig((0, 0, 0, 0), 3), QQ(10, 3) * y * z + QQ(4, 3) * y * t - QQ(1, 3) * y + 4 * z**2 + QQ(22, 3) * z * t - QQ(4, 3) * z + 4 * t**2 - QQ(4, 3) * t, 3) ] # comparable: assert is_rewritable_or_comparable(Sign(p), Num(p), B) is True
def test_dup_add(): assert dup_add([], [], ZZ) == [] assert dup_add([ZZ(1)], [], ZZ) == [ZZ(1)] assert dup_add([], [ZZ(1)], ZZ) == [ZZ(1)] assert dup_add([ZZ(1)], [ZZ(1)], ZZ) == [ZZ(2)] assert dup_add([ZZ(1)], [ZZ(2)], ZZ) == [ZZ(3)] assert dup_add([ZZ(1), ZZ(2)], [ZZ(1)], ZZ) == [ZZ(1), ZZ(3)] assert dup_add([ZZ(1)], [ZZ(1), ZZ(2)], ZZ) == [ZZ(1), ZZ(3)] assert dup_add([ZZ(1), ZZ(2), ZZ(3)], [ZZ(8), ZZ(9), ZZ(10)], ZZ) == [ZZ(9), ZZ(11), ZZ(13)] assert dup_add([], [], QQ) == [] assert dup_add([QQ(1, 2)], [], QQ) == [QQ(1, 2)] assert dup_add([], [QQ(1, 2)], QQ) == [QQ(1, 2)] assert dup_add([QQ(1, 4)], [QQ(1, 4)], QQ) == [QQ(1, 2)] assert dup_add([QQ(1, 4)], [QQ(1, 2)], QQ) == [QQ(3, 4)] assert dup_add([QQ(1, 2), QQ(2, 3)], [QQ(1)], QQ) == [QQ(1, 2), QQ(5, 3)] assert dup_add([QQ(1)], [QQ(1, 2), QQ(2, 3)], QQ) == [QQ(1, 2), QQ(5, 3)] assert dup_add([QQ(1, 7), QQ(2, 7), QQ(3, 7)], [QQ(8, 7), QQ(9, 7), QQ(10, 7)], QQ) == [QQ(9, 7), QQ(11, 7), QQ(13, 7)]
def test_PythonRational__eq__(): assert (QQ(1, 2) == QQ(1, 2)) is True assert (QQ(1, 2) != QQ(1, 2)) is False assert (QQ(1, 2) == QQ(1, 3)) is False assert (QQ(1, 2) != QQ(1, 3)) is True