test_homomorphisms.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. """Tests for homomorphisms."""
  2. from sympy.core.singleton import S
  3. from sympy.polys.domains.rationalfield import QQ
  4. from sympy.abc import x, y
  5. from sympy.polys.agca import homomorphism
  6. from sympy.testing.pytest import raises
  7. def test_printing():
  8. R = QQ.old_poly_ring(x)
  9. assert str(homomorphism(R.free_module(1), R.free_module(1), [0])) == \
  10. 'Matrix([[0]]) : QQ[x]**1 -> QQ[x]**1'
  11. assert str(homomorphism(R.free_module(2), R.free_module(2), [0, 0])) == \
  12. 'Matrix([ \n[0, 0], : QQ[x]**2 -> QQ[x]**2\n[0, 0]]) '
  13. assert str(homomorphism(R.free_module(1), R.free_module(1) / [[x]], [0])) == \
  14. 'Matrix([[0]]) : QQ[x]**1 -> QQ[x]**1/<[x]>'
  15. assert str(R.free_module(0).identity_hom()) == 'Matrix(0, 0, []) : QQ[x]**0 -> QQ[x]**0'
  16. def test_operations():
  17. F = QQ.old_poly_ring(x).free_module(2)
  18. G = QQ.old_poly_ring(x).free_module(3)
  19. f = F.identity_hom()
  20. g = homomorphism(F, F, [0, [1, x]])
  21. h = homomorphism(F, F, [[1, 0], 0])
  22. i = homomorphism(F, G, [[1, 0, 0], [0, 1, 0]])
  23. assert f == f
  24. assert f != g
  25. assert f != i
  26. assert (f != F.identity_hom()) is False
  27. assert 2*f == f*2 == homomorphism(F, F, [[2, 0], [0, 2]])
  28. assert f/2 == homomorphism(F, F, [[S.Half, 0], [0, S.Half]])
  29. assert f + g == homomorphism(F, F, [[1, 0], [1, x + 1]])
  30. assert f - g == homomorphism(F, F, [[1, 0], [-1, 1 - x]])
  31. assert f*g == g == g*f
  32. assert h*g == homomorphism(F, F, [0, [1, 0]])
  33. assert g*h == homomorphism(F, F, [0, 0])
  34. assert i*f == i
  35. assert f([1, 2]) == [1, 2]
  36. assert g([1, 2]) == [2, 2*x]
  37. assert i.restrict_domain(F.submodule([x, x]))([x, x]) == i([x, x])
  38. h1 = h.quotient_domain(F.submodule([0, 1]))
  39. assert h1([1, 0]) == h([1, 0])
  40. assert h1.restrict_domain(h1.domain.submodule([x, 0]))([x, 0]) == h([x, 0])
  41. raises(TypeError, lambda: f/g)
  42. raises(TypeError, lambda: f + 1)
  43. raises(TypeError, lambda: f + i)
  44. raises(TypeError, lambda: f - 1)
  45. raises(TypeError, lambda: f*i)
  46. def test_creation():
  47. F = QQ.old_poly_ring(x).free_module(3)
  48. G = QQ.old_poly_ring(x).free_module(2)
  49. SM = F.submodule([1, 1, 1])
  50. Q = F / SM
  51. SQ = Q.submodule([1, 0, 0])
  52. matrix = [[1, 0], [0, 1], [-1, -1]]
  53. h = homomorphism(F, G, matrix)
  54. h2 = homomorphism(Q, G, matrix)
  55. assert h.quotient_domain(SM) == h2
  56. raises(ValueError, lambda: h.quotient_domain(F.submodule([1, 0, 0])))
  57. assert h2.restrict_domain(SQ) == homomorphism(SQ, G, matrix)
  58. raises(ValueError, lambda: h.restrict_domain(G))
  59. raises(ValueError, lambda: h.restrict_codomain(G.submodule([1, 0])))
  60. raises(ValueError, lambda: h.quotient_codomain(F))
  61. im = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
  62. for M in [F, SM, Q, SQ]:
  63. assert M.identity_hom() == homomorphism(M, M, im)
  64. assert SM.inclusion_hom() == homomorphism(SM, F, im)
  65. assert SQ.inclusion_hom() == homomorphism(SQ, Q, im)
  66. assert Q.quotient_hom() == homomorphism(F, Q, im)
  67. assert SQ.quotient_hom() == homomorphism(SQ.base, SQ, im)
  68. class conv:
  69. def convert(x, y=None):
  70. return x
  71. class dummy:
  72. container = conv()
  73. def submodule(*args):
  74. return None
  75. raises(TypeError, lambda: homomorphism(dummy(), G, matrix))
  76. raises(TypeError, lambda: homomorphism(F, dummy(), matrix))
  77. raises(
  78. ValueError, lambda: homomorphism(QQ.old_poly_ring(x, y).free_module(3), G, matrix))
  79. raises(ValueError, lambda: homomorphism(F, G, [0, 0]))
  80. def test_properties():
  81. R = QQ.old_poly_ring(x, y)
  82. F = R.free_module(2)
  83. h = homomorphism(F, F, [[x, 0], [y, 0]])
  84. assert h.kernel() == F.submodule([-y, x])
  85. assert h.image() == F.submodule([x, 0], [y, 0])
  86. assert not h.is_injective()
  87. assert not h.is_surjective()
  88. assert h.restrict_codomain(h.image()).is_surjective()
  89. assert h.restrict_domain(F.submodule([1, 0])).is_injective()
  90. assert h.quotient_domain(
  91. h.kernel()).restrict_codomain(h.image()).is_isomorphism()
  92. R2 = QQ.old_poly_ring(x, y, order=(("lex", x), ("ilex", y))) / [x**2 + 1]
  93. F = R2.free_module(2)
  94. h = homomorphism(F, F, [[x, 0], [y, y + 1]])
  95. assert h.is_isomorphism()