def test_QuotientRing(): I = QQ.old_poly_ring(x).ideal(x**2 + 1) R = QQ.old_poly_ring(x) / I assert R == QQ.old_poly_ring(x) / [x**2 + 1] assert R == QQ.old_poly_ring(x) / QQ.old_poly_ring(x).ideal(x**2 + 1) assert R != QQ.old_poly_ring(x) assert R.convert(1) / x == -x + I assert -1 + I == x**2 + I assert R.convert(ZZ(1), ZZ) == 1 + I assert R.convert(R.convert(x), R) == R.convert(x) X = R.convert(x) Y = QQ.old_poly_ring(x).convert(x) assert -1 + I == X**2 + I assert -1 + I == Y**2 + I assert R.to_diofant(X) == x pytest.raises( ValueError, lambda: QQ.old_poly_ring(x) / QQ.old_poly_ring(x, y).ideal(x)) R = QQ.old_poly_ring(x, order="ilex") I = R.ideal(x) assert R.convert(1) + I == (R / I).convert(1)
def test_operations(): F = QQ.old_poly_ring(x).free_module(2) G = QQ.old_poly_ring(x).free_module(3) f = F.identity_hom() g = homomorphism(F, F, [0, [1, x]]) h = homomorphism(F, F, [[1, 0], 0]) i = homomorphism(F, G, [[1, 0, 0], [0, 1, 0]]) assert f == f assert f != g assert f != i assert (f != F.identity_hom()) is False assert 2 * f == f * 2 == homomorphism(F, F, [[2, 0], [0, 2]]) assert f / 2 == homomorphism(F, F, [[Rational(1, 2), 0], [0, Rational(1, 2)]]) assert f + g == homomorphism(F, F, [[1, 0], [1, x + 1]]) assert f - g == homomorphism(F, F, [[1, 0], [-1, 1 - x]]) assert f * g == g == g * f assert h * g == homomorphism(F, F, [0, [1, 0]]) assert g * h == homomorphism(F, F, [0, 0]) assert i * f == i assert f([1, 2]) == [1, 2] assert g([1, 2]) == [2, 2 * x] assert i.restrict_domain(F.submodule([x, x]))([x, x]) == i([x, x]) h1 = h.quotient_domain(F.submodule([0, 1])) assert h1([1, 0]) == h([1, 0]) assert h1.restrict_domain(h1.domain.submodule([x, 0]))([x, 0]) == h([x, 0]) pytest.raises(TypeError, lambda: f / g) pytest.raises(TypeError, lambda: f + 1) pytest.raises(TypeError, lambda: f + i) pytest.raises(TypeError, lambda: f - 1) pytest.raises(TypeError, lambda: f * i)
def test_properties(): R = QQ.old_poly_ring(x, y) F = R.free_module(2) h = homomorphism(F, F, [[x, 0], [y, 0]]) assert h.kernel() == F.submodule([-y, x]) assert h.image() == F.submodule([x, 0], [y, 0]) assert not h.is_injective() assert not h.is_surjective() assert h.restrict_codomain(h.image()).is_surjective() assert h.restrict_domain(F.submodule([1, 0])).is_injective() assert h.quotient_domain(h.kernel()).restrict_codomain( h.image()).is_isomorphism() R2 = QQ.old_poly_ring(x, y, order=(("lex", x), ("ilex", y))) / [x**2 + 1] F = R2.free_module(2) h = homomorphism(F, F, [[x, 0], [y, y + 1]]) assert h.is_isomorphism()
def test_creation(): F = QQ.old_poly_ring(x).free_module(3) G = QQ.old_poly_ring(x).free_module(2) SM = F.submodule([1, 1, 1]) Q = F / SM SQ = Q.submodule([1, 0, 0]) matrix = [[1, 0], [0, 1], [-1, -1]] h = homomorphism(F, G, matrix) h2 = homomorphism(Q, G, matrix) assert h.quotient_domain(SM) == h2 pytest.raises(ValueError, lambda: h.quotient_domain(F.submodule([1, 0, 0]))) assert h2.restrict_domain(SQ) == homomorphism(SQ, G, matrix) pytest.raises(ValueError, lambda: h.restrict_domain(G)) pytest.raises(ValueError, lambda: h.restrict_codomain(G.submodule([1, 0]))) pytest.raises(ValueError, lambda: h.quotient_codomain(F)) im = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] for M in [F, SM, Q, SQ]: assert M.identity_hom() == homomorphism(M, M, im) assert SM.inclusion_hom() == homomorphism(SM, F, im) assert SQ.inclusion_hom() == homomorphism(SQ, Q, im) assert Q.quotient_hom() == homomorphism(F, Q, im) assert SQ.quotient_hom() == homomorphism(SQ.base, SQ, im) class conv(object): def convert(self, y=None): return self class dummy(object): container = conv() def submodule(*args): return pytest.raises(TypeError, lambda: homomorphism(dummy(), G, matrix)) pytest.raises(TypeError, lambda: homomorphism(F, dummy(), matrix)) pytest.raises( ValueError, lambda: homomorphism(QQ.old_poly_ring(x, y).free_module(3), G, matrix)) pytest.raises(ValueError, lambda: homomorphism(F, G, [0, 0]))
def test_printing(): R = QQ.old_poly_ring(x) assert str(homomorphism(R.free_module(1), R.free_module(1), [0])) == \ 'Matrix([[0]]) : QQ[x]**1 -> QQ[x]**1' assert str(homomorphism(R.free_module(2), R.free_module(2), [0, 0])) == \ 'Matrix([ \n[0, 0], : QQ[x]**2 -> QQ[x]**2\n[0, 0]]) ' assert str(homomorphism(R.free_module(1), R.free_module(1) / [[x]], [0])) == \ 'Matrix([[0]]) : QQ[x]**1 -> QQ[x]**1/<[x]>' assert str(R.free_module( 0).identity_hom()) == 'Matrix(0, 0, []) : QQ[x]**0 -> QQ[x]**0'
def test_QuotientRingElement(): R = QQ.old_poly_ring(x) / [x**10] X = R.convert(x) assert X * (X + 1) == R.convert(x**2 + x) assert X * x == R.convert(x**2) assert x * X == R.convert(x**2) assert X + x == R.convert(2 * x) assert x + X == 2 * X assert X**2 == R.convert(x**2) assert 1 / (1 - X) == R.convert(sum(x**i for i in range(10))) assert X**10 == R.zero assert X != x pytest.raises(NotReversible, lambda: 1 / X)