test_normalforms.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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
  5. from sympy.matrices.normalforms import (
  6. invariant_factors,
  7. smith_normal_form,
  8. hermite_normal_form,
  9. )
  10. from sympy.polys.domains import ZZ, QQ
  11. from sympy.core.numbers import Integer
  12. def test_smith_normal():
  13. m = Matrix([[12,6,4,8],[3,9,6,12],[2,16,14,28],[20,10,10,20]])
  14. smf = Matrix([[1, 0, 0, 0], [0, 10, 0, 0], [0, 0, -30, 0], [0, 0, 0, 0]])
  15. assert smith_normal_form(m) == smf
  16. x = Symbol('x')
  17. with warns_deprecated_sympy():
  18. m = Matrix([[Poly(x-1), Poly(1, x),Poly(-1,x)],
  19. [0, Poly(x), Poly(-1,x)],
  20. [Poly(0,x),Poly(-1,x),Poly(x)]])
  21. invs = 1, x - 1, x**2 - 1
  22. assert invariant_factors(m, domain=QQ[x]) == invs
  23. m = Matrix([[2, 4]])
  24. smf = Matrix([[2, 0]])
  25. assert smith_normal_form(m) == smf
  26. def test_smith_normal_deprecated():
  27. from sympy.polys.solvers import RawMatrix as Matrix
  28. with warns_deprecated_sympy():
  29. m = Matrix([[12, 6, 4,8],[3,9,6,12],[2,16,14,28],[20,10,10,20]])
  30. setattr(m, 'ring', ZZ)
  31. with warns_deprecated_sympy():
  32. smf = Matrix([[1, 0, 0, 0], [0, 10, 0, 0], [0, 0, -30, 0], [0, 0, 0, 0]])
  33. assert smith_normal_form(m) == smf
  34. x = Symbol('x')
  35. with warns_deprecated_sympy():
  36. m = Matrix([[Poly(x-1), Poly(1, x),Poly(-1,x)],
  37. [0, Poly(x), Poly(-1,x)],
  38. [Poly(0,x),Poly(-1,x),Poly(x)]])
  39. setattr(m, 'ring', QQ[x])
  40. invs = (Poly(1, x, domain='QQ'), Poly(x - 1, domain='QQ'), Poly(x**2 - 1, domain='QQ'))
  41. assert invariant_factors(m) == invs
  42. with warns_deprecated_sympy():
  43. m = Matrix([[2, 4]])
  44. setattr(m, 'ring', ZZ)
  45. with warns_deprecated_sympy():
  46. smf = Matrix([[2, 0]])
  47. assert smith_normal_form(m) == smf
  48. def test_hermite_normal():
  49. m = Matrix([[2, 7, 17, 29, 41], [3, 11, 19, 31, 43], [5, 13, 23, 37, 47]])
  50. hnf = Matrix([[1, 0, 0], [0, 2, 1], [0, 0, 1]])
  51. assert hermite_normal_form(m) == hnf
  52. tr_hnf = Matrix([[37, 0, 19], [222, -6, 113], [48, 0, 25], [0, 2, 1], [0, 0, 1]])
  53. assert hermite_normal_form(m.transpose()) == tr_hnf
  54. m = Matrix([[8, 28, 68, 116, 164], [3, 11, 19, 31, 43], [5, 13, 23, 37, 47]])
  55. hnf = Matrix([[4, 0, 0], [0, 2, 1], [0, 0, 1]])
  56. assert hermite_normal_form(m) == hnf
  57. assert hermite_normal_form(m, D=8) == hnf
  58. assert hermite_normal_form(m, D=ZZ(8)) == hnf
  59. assert hermite_normal_form(m, D=Integer(8)) == hnf
  60. m = Matrix([[10, 8, 6, 30, 2], [45, 36, 27, 18, 9], [5, 4, 3, 2, 1]])
  61. hnf = Matrix([[26, 2], [0, 9], [0, 1]])
  62. assert hermite_normal_form(m) == hnf
  63. m = Matrix([[2, 7], [0, 0], [0, 0]])
  64. hnf = Matrix([[1], [0], [0]])
  65. assert hermite_normal_form(m) == hnf
  66. def test_issue_23410():
  67. A = Matrix([[1, 12], [0, 8], [0, 5]])
  68. H = Matrix([[1, 0], [0, 8], [0, 5]])
  69. assert hermite_normal_form(A) == H