def test_Submodule_mul(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) C = A.submodule_from_matrix(DomainMatrix([ [0, 10, 0, 0], [0, 0, 7, 0], ], (2, 4), ZZ).transpose(), denom=15) C1 = A.submodule_from_matrix(DomainMatrix([ [0, 20, 0, 0], [0, 0, 14, 0], ], (2, 4), ZZ).transpose(), denom=3) C2 = A.submodule_from_matrix(DomainMatrix([ [0, 0, 10, 0], [0, 0, 0, 7], ], (2, 4), ZZ).transpose(), denom=15) C3_unred = A.submodule_from_matrix(DomainMatrix( [[0, 0, 100, 0], [0, 0, 0, 70], [0, 0, 0, 70], [-49, -49, -49, -49]], (4, 4), ZZ).transpose(), denom=225) C3 = A.submodule_from_matrix(DomainMatrix( [[4900, 4900, 0, 0], [4410, 4410, 10, 0], [2107, 2107, 7, 7]], (3, 4), ZZ).transpose(), denom=225) assert C * 1 == C assert C**1 == C assert C * 10 == C1 assert C * A(1) == C2 assert C.mul(C, hnf=False) == C3_unred assert C * C == C3 assert C**2 == C3
def test_Submodule_represent(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ)) C = B.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ)) a0 = A(to_col([6, 12, 18, 24])) a1 = A(to_col([2, 4, 6, 8])) a2 = A(to_col([1, 3, 5, 7])) b1 = B.represent(a1) assert b1.flat() == [1, 2, 3, 4] c0 = C.represent(a0) assert c0.flat() == [1, 2, 3, 4] Y = A.submodule_from_matrix( DomainMatrix([ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], ], (3, 4), ZZ).transpose()) U = Poly(cyclotomic_poly(7, x)) Z = PowerBasis(U) z0 = Z(to_col([1, 2, 3, 4, 5, 6])) raises(ClosureFailure, lambda: Y.represent(A(3))) raises(ClosureFailure, lambda: B.represent(a2)) raises(ClosureFailure, lambda: B.represent(z0))
def test_Submodule_reduced(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ)) C = A.submodule_from_matrix(6 * DomainMatrix.eye(4, ZZ), denom=3) D = C.reduced() assert D.denom == 1 and D == C == B
def test_Submodule_add(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) B = A.submodule_from_matrix(DomainMatrix([ [4, 0, 0, 0], [0, 4, 0, 0], ], (2, 4), ZZ).transpose(), denom=6) C = A.submodule_from_matrix(DomainMatrix([ [0, 10, 0, 0], [0, 0, 7, 0], ], (2, 4), ZZ).transpose(), denom=15) D = A.submodule_from_matrix(DomainMatrix([ [20, 0, 0, 0], [0, 20, 0, 0], [0, 0, 14, 0], ], (3, 4), ZZ).transpose(), denom=30) assert B + C == D U = Poly(cyclotomic_poly(7, x)) Z = PowerBasis(U) Y = Z.submodule_from_gens([Z(0), Z(1)]) raises(TypeError, lambda: B + Y)
def test_Module_submodule_from_matrix(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ)) e = B(to_col([1, 2, 3, 4])) f = e.to_parent() assert f.col.flat() == [2, 4, 6, 8] # Matrix must be over ZZ: raises(ValueError, lambda: A.submodule_from_matrix(DomainMatrix.eye(4, QQ))) # Number of rows of matrix must equal number of generators of module A: raises(ValueError, lambda: A.submodule_from_matrix(2 * DomainMatrix.eye(5, ZZ)))
def test_check_formal_conditions_for_maximal_order(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ)) C = B.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ)) D = A.submodule_from_matrix(DomainMatrix.eye(4, ZZ)[:, :-1]) # Is a direct submodule of a power basis, but lacks 1 as first generator: raises(StructureError, lambda: _check_formal_conditions_for_maximal_order(B)) # Is not a direct submodule of a power basis: raises(StructureError, lambda: _check_formal_conditions_for_maximal_order(C)) # Is direct submod of pow basis, and starts with 1, but not sq/max rank/HNF: raises(StructureError, lambda: _check_formal_conditions_for_maximal_order(D))
def test_Submodule_repr(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ), denom=3) assert repr( B ) == 'Submodule[[2, 0, 0, 0], [0, 2, 0, 0], [0, 0, 2, 0], [0, 0, 0, 2]]/3'
def test_Module_one(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ)) assert A.one().col.flat() == [1, 0, 0, 0] assert A.one().module == A assert B.one().col.flat() == [1, 0, 0, 0] assert B.one().module == A
def test_make_mod_elt(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ)) col = to_col([1, 2, 3, 4]) eA = make_mod_elt(A, col) eB = make_mod_elt(B, col) assert isinstance(eA, PowerBasisElement) assert not isinstance(eB, PowerBasisElement)
def test_Submodule_is_compat_submodule(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ)) C = A.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ)) D = C.submodule_from_matrix(5 * DomainMatrix.eye(4, ZZ)) assert B.is_compat_submodule(C) is True assert B.is_compat_submodule(A) is False assert B.is_compat_submodule(D) is False
def test_ModuleElement_div(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) C = A.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ)) e = A(to_col([0, 2, 0, 0]), denom=3) f = A(to_col([0, 0, 0, 7]), denom=5) g = C(to_col([1, 1, 1, 1])) assert e // f == 10 * A(3) // 21 assert e // g == -2 * A(2) // 9 assert 3 // g == -A(1)
def test_Submodule_discard_before(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ)) B.compute_mult_tab() C = B.discard_before(2) assert C.parent == B.parent assert B.is_sq_maxrank_HNF() and not C.is_sq_maxrank_HNF() assert C.matrix == B.matrix[:, 2:] assert C.mult_tab() == {0: {0: [-2, -2], 1: [0, 0]}, 1: {1: [0, 0]}}
def test_Module_element_from_rational(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ)) rA = A.element_from_rational(QQ(22, 7)) rB = B.element_from_rational(QQ(22, 7)) assert rA.coeffs == [22, 0, 0, 0] assert rA.denom == 7 assert rA.module == A assert rB.coeffs == [22, 0, 0, 0] assert rB.denom == 7 assert rB.module == A
def test_Module_ancestors(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ)) C = B.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ)) D = B.submodule_from_matrix(5 * DomainMatrix.eye(4, ZZ)) assert C.ancestors(include_self=True) == [A, B, C] assert D.ancestors(include_self=True) == [A, B, D] assert C.power_basis_ancestor() == A assert C.nearest_common_ancestor(D) == B M = Module() assert M.power_basis_ancestor() is None
def test_ModuleElement_pow(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) C = A.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ)) e = A(to_col([0, 2, 0, 0]), denom=3) g = C(to_col([0, 0, 0, 1]), denom=2) assert e**3 == A(to_col([0, 0, 0, 8]), denom=27) assert g**2 == C(to_col([0, 3, 0, 0]), denom=4) assert e**0 == A(to_col([1, 0, 0, 0])) assert g**0 == A(to_col([1, 0, 0, 0])) assert e**1 == e assert g**1 == g
def test_find_min_poly(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) powers = [] m = find_min_poly(A(1), QQ, x=x, powers=powers) assert m == Poly(T, domain=QQ) assert len(powers) == 5 # powers list need not be passed m = find_min_poly(A(1), QQ, x=x) assert m == Poly(T, domain=QQ) B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ)) raises(MissingUnityError, lambda: find_min_poly(B(1), QQ))
def test_Module_basis_elements(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ)) basis = B.basis_elements() bp = B.basis_element_pullbacks() for i, (e, p) in enumerate(zip(basis, bp)): c = [0] * 4 assert e.module == B assert p.module == A c[i] = 1 assert e == B(to_col(c)) c[i] = 2 assert p == A(to_col(c))
def test_EndomorphismRing_represent(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) R = A.endomorphism_ring() phi = R.inner_endomorphism(A(1)) col = R.represent(phi) assert col.transpose() == DomainMatrix( [[0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, -1, -1, -1, -1]], (1, 16), ZZ) B = A.submodule_from_matrix(DomainMatrix.zeros((4, 0), ZZ)) S = B.endomorphism_ring() psi = S.inner_endomorphism(A(1)) col = S.represent(psi) assert col == DomainMatrix([], (0, 0), ZZ) raises(NotImplementedError, lambda: R.represent(3.14))
def test_ModuleElement_to_ancestors(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ)) C = B.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ)) D = C.submodule_from_matrix(5 * DomainMatrix.eye(4, ZZ)) eD = D(0) eC = eD.to_parent() eB = eD.to_ancestor(B) eA = eD.over_power_basis() assert eC.module is C and eC.coeffs == [5, 0, 0, 0] assert eB.module is B and eB.coeffs == [15, 0, 0, 0] assert eA.module is A and eA.coeffs == [30, 0, 0, 0] a = A(0) raises(ValueError, lambda: a.to_parent())
def test_ModuleElement_add(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) C = A.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ)) e = A(to_col([1, 2, 3, 4]), denom=6) f = A(to_col([5, 6, 7, 8]), denom=10) g = C(to_col([1, 1, 1, 1]), denom=2) assert e + f == A(to_col([10, 14, 18, 22]), denom=15) assert e - f == A(to_col([-5, -4, -3, -2]), denom=15) assert e + g == A(to_col([10, 11, 12, 13]), denom=6) assert e + QQ(7, 10) == A(to_col([26, 10, 15, 20]), denom=30) assert g + QQ(7, 10) == A(to_col([22, 15, 15, 15]), denom=10) U = Poly(cyclotomic_poly(7, x)) Z = PowerBasis(U) raises(TypeError, lambda: e + Z(0)) raises(TypeError, lambda: e + 3.14)
def test_ModuleElement_compatibility(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ)) C = B.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ)) D = B.submodule_from_matrix(5 * DomainMatrix.eye(4, ZZ)) assert C(0).is_compat(C(1)) is True assert C(0).is_compat(D(0)) is False u, v = C(0).unify(D(0)) assert u.module is B and v.module is B assert C(C.represent(u)) == C(0) and D(D.represent(v)) == D(0) u, v = C(0).unify(C(1)) assert u == C(0) and v == C(1) U = Poly(cyclotomic_poly(7, x)) Z = PowerBasis(U) raises(UnificationFailed, lambda: C(0).unify(Z(1)))
def test_ModuleElement_equiv(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) e = A(to_col([1, 2, 3, 4]), denom=1) f = A(to_col([3, 6, 9, 12]), denom=3) assert e.equiv(f) C = A.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ)) g = C(to_col([1, 2, 3, 4]), denom=1) h = A(to_col([3, 6, 9, 12]), denom=1) assert g.equiv(h) assert C(to_col([5, 0, 0, 0]), denom=7).equiv(QQ(15, 7)) U = Poly(cyclotomic_poly(7, x)) Z = PowerBasis(U) raises(UnificationFailed, lambda: e.equiv(Z(0))) assert e.equiv(3.14) is False
def test_ModuleElement_mul(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) C = A.submodule_from_matrix(3 * DomainMatrix.eye(4, ZZ)) e = A(to_col([0, 2, 0, 0]), denom=3) f = A(to_col([0, 0, 0, 7]), denom=5) g = C(to_col([0, 0, 0, 1]), denom=2) h = A(to_col([0, 0, 3, 1]), denom=7) assert e * f == A(to_col([-14, -14, -14, -14]), denom=15) assert e * g == A(to_col([-1, -1, -1, -1])) assert e * h == A(to_col([-2, -2, -2, 4]), denom=21) assert e * QQ(6, 5) == A(to_col([0, 4, 0, 0]), denom=5) assert (g * QQ(10, 21)).equiv(A(to_col([0, 0, 0, 5]), denom=7)) assert e // QQ(6, 5) == A(to_col([0, 5, 0, 0]), denom=9) U = Poly(cyclotomic_poly(7, x)) Z = PowerBasis(U) raises(TypeError, lambda: e * Z(0)) raises(TypeError, lambda: e * 3.14) raises(TypeError, lambda: e // 3.14) raises(ZeroDivisionError, lambda: e // 0)
def test_Submodule_eq(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ)) C = A.submodule_from_matrix(6 * DomainMatrix.eye(4, ZZ), denom=3) assert C == B
def test_Module_starts_with_unity(): T = Poly(cyclotomic_poly(5, x)) A = PowerBasis(T) B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ)) assert A.starts_with_unity() is True assert B.starts_with_unity() is False