test_normalforms.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from sympy.testing.pytest import warns_deprecated_sympy
  2. from sympy.core.symbol import Symbol
  3. from sympy.polys.polytools import Poly
  4. from sympy.matrices import Matrix, randMatrix
  5. from sympy.matrices.normalforms import (
  6. invariant_factors,
  7. smith_normal_form,
  8. smith_normal_decomp,
  9. hermite_normal_form,
  10. is_smith_normal_form,
  11. )
  12. from sympy.polys.domains import ZZ, QQ
  13. from sympy.core.numbers import Integer
  14. import random
  15. def test_smith_normal():
  16. m = Matrix([[12,6,4,8],[3,9,6,12],[2,16,14,28],[20,10,10,20]])
  17. smf = Matrix([[1, 0, 0, 0], [0, 10, 0, 0], [0, 0, 30, 0], [0, 0, 0, 0]])
  18. assert smith_normal_form(m) == smf
  19. a, s, t = smith_normal_decomp(m)
  20. assert a == s * m * t
  21. x = Symbol('x')
  22. with warns_deprecated_sympy():
  23. m = Matrix([[Poly(x-1), Poly(1, x),Poly(-1,x)],
  24. [0, Poly(x), Poly(-1,x)],
  25. [Poly(0,x),Poly(-1,x),Poly(x)]])
  26. invs = 1, x - 1, x**2 - 1
  27. assert invariant_factors(m, domain=QQ[x]) == invs
  28. m = Matrix([[2, 4]])
  29. smf = Matrix([[2, 0]])
  30. assert smith_normal_form(m) == smf
  31. prng = random.Random(0)
  32. for i in range(6):
  33. for j in range(6):
  34. for _ in range(10 if i*j else 1):
  35. m = randMatrix(i, j, max=5, percent=50, prng=prng)
  36. a, s, t = smith_normal_decomp(m)
  37. assert a == s * m * t
  38. assert is_smith_normal_form(a)
  39. s.inv().to_DM(ZZ)
  40. t.inv().to_DM(ZZ)
  41. a, s, t = smith_normal_decomp(m, QQ)
  42. assert a == s * m * t
  43. assert is_smith_normal_form(a)
  44. s.inv()
  45. t.inv()
  46. def test_smith_normal_deprecated():
  47. from sympy.polys.solvers import RawMatrix as Matrix
  48. with warns_deprecated_sympy():
  49. m = Matrix([[12, 6, 4,8],[3,9,6,12],[2,16,14,28],[20,10,10,20]])
  50. setattr(m, 'ring', ZZ)
  51. with warns_deprecated_sympy():
  52. smf = Matrix([[1, 0, 0, 0], [0, 10, 0, 0], [0, 0, 30, 0], [0, 0, 0, 0]])
  53. assert smith_normal_form(m) == smf
  54. x = Symbol('x')
  55. with warns_deprecated_sympy():
  56. m = Matrix([[Poly(x-1), Poly(1, x),Poly(-1,x)],
  57. [0, Poly(x), Poly(-1,x)],
  58. [Poly(0,x),Poly(-1,x),Poly(x)]])
  59. setattr(m, 'ring', QQ[x])
  60. invs = (Poly(1, x, domain='QQ'), Poly(x - 1, domain='QQ'), Poly(x**2 - 1, domain='QQ'))
  61. assert invariant_factors(m) == invs
  62. with warns_deprecated_sympy():
  63. m = Matrix([[2, 4]])
  64. setattr(m, 'ring', ZZ)
  65. with warns_deprecated_sympy():
  66. smf = Matrix([[2, 0]])
  67. assert smith_normal_form(m) == smf
  68. def test_hermite_normal():
  69. m = Matrix([[2, 7, 17, 29, 41], [3, 11, 19, 31, 43], [5, 13, 23, 37, 47]])
  70. hnf = Matrix([[1, 0, 0], [0, 2, 1], [0, 0, 1]])
  71. assert hermite_normal_form(m) == hnf
  72. tr_hnf = Matrix([[37, 0, 19], [222, -6, 113], [48, 0, 25], [0, 2, 1], [0, 0, 1]])
  73. assert hermite_normal_form(m.transpose()) == tr_hnf
  74. m = Matrix([[8, 28, 68, 116, 164], [3, 11, 19, 31, 43], [5, 13, 23, 37, 47]])
  75. hnf = Matrix([[4, 0, 0], [0, 2, 1], [0, 0, 1]])
  76. assert hermite_normal_form(m) == hnf
  77. assert hermite_normal_form(m, D=8) == hnf
  78. assert hermite_normal_form(m, D=ZZ(8)) == hnf
  79. assert hermite_normal_form(m, D=Integer(8)) == hnf
  80. m = Matrix([[10, 8, 6, 30, 2], [45, 36, 27, 18, 9], [5, 4, 3, 2, 1]])
  81. hnf = Matrix([[26, 2], [0, 9], [0, 1]])
  82. assert hermite_normal_form(m) == hnf
  83. m = Matrix([[2, 7], [0, 0], [0, 0]])
  84. hnf = Matrix([[1], [0], [0]])
  85. assert hermite_normal_form(m) == hnf
  86. def test_issue_23410():
  87. A = Matrix([[1, 12], [0, 8], [0, 5]])
  88. H = Matrix([[1, 0], [0, 8], [0, 5]])
  89. assert hermite_normal_form(A) == H