test_eigen.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. """
  2. Tests for the sympy.polys.matrices.eigen module
  3. """
  4. from sympy.core.singleton import S
  5. from sympy.functions.elementary.miscellaneous import sqrt
  6. from sympy.matrices.dense import Matrix
  7. from sympy.polys.agca.extensions import FiniteExtension
  8. from sympy.polys.domains import QQ
  9. from sympy.polys.polytools import Poly
  10. from sympy.polys.rootoftools import CRootOf
  11. from sympy.polys.matrices.domainmatrix import DomainMatrix
  12. from sympy.polys.matrices.eigen import dom_eigenvects, dom_eigenvects_to_sympy
  13. def test_dom_eigenvects_rational():
  14. # Rational eigenvalues
  15. A = DomainMatrix([[QQ(1), QQ(2)], [QQ(1), QQ(2)]], (2, 2), QQ)
  16. rational_eigenvects = [
  17. (QQ, QQ(3), 1, DomainMatrix([[QQ(1), QQ(1)]], (1, 2), QQ)),
  18. (QQ, QQ(0), 1, DomainMatrix([[QQ(-2), QQ(1)]], (1, 2), QQ)),
  19. ]
  20. assert dom_eigenvects(A) == (rational_eigenvects, [])
  21. # Test converting to Expr:
  22. sympy_eigenvects = [
  23. (S(3), 1, [Matrix([1, 1])]),
  24. (S(0), 1, [Matrix([-2, 1])]),
  25. ]
  26. assert dom_eigenvects_to_sympy(rational_eigenvects, [], Matrix) == sympy_eigenvects
  27. def test_dom_eigenvects_algebraic():
  28. # Algebraic eigenvalues
  29. A = DomainMatrix([[QQ(1), QQ(2)], [QQ(3), QQ(4)]], (2, 2), QQ)
  30. Avects = dom_eigenvects(A)
  31. # Extract the dummy to build the expected result:
  32. lamda = Avects[1][0][1].gens[0]
  33. irreducible = Poly(lamda**2 - 5*lamda - 2, lamda, domain=QQ)
  34. K = FiniteExtension(irreducible)
  35. KK = K.from_sympy
  36. algebraic_eigenvects = [
  37. (K, irreducible, 1, DomainMatrix([[KK((lamda-4)/3), KK(1)]], (1, 2), K)),
  38. ]
  39. assert Avects == ([], algebraic_eigenvects)
  40. # Test converting to Expr:
  41. sympy_eigenvects = [
  42. (S(5)/2 - sqrt(33)/2, 1, [Matrix([[-sqrt(33)/6 - S(1)/2], [1]])]),
  43. (S(5)/2 + sqrt(33)/2, 1, [Matrix([[-S(1)/2 + sqrt(33)/6], [1]])]),
  44. ]
  45. assert dom_eigenvects_to_sympy([], algebraic_eigenvects, Matrix) == sympy_eigenvects
  46. def test_dom_eigenvects_rootof():
  47. # Algebraic eigenvalues
  48. A = DomainMatrix([
  49. [0, 0, 0, 0, -1],
  50. [1, 0, 0, 0, 1],
  51. [0, 1, 0, 0, 0],
  52. [0, 0, 1, 0, 0],
  53. [0, 0, 0, 1, 0]], (5, 5), QQ)
  54. Avects = dom_eigenvects(A)
  55. # Extract the dummy to build the expected result:
  56. lamda = Avects[1][0][1].gens[0]
  57. irreducible = Poly(lamda**5 - lamda + 1, lamda, domain=QQ)
  58. K = FiniteExtension(irreducible)
  59. KK = K.from_sympy
  60. algebraic_eigenvects = [
  61. (K, irreducible, 1,
  62. DomainMatrix([
  63. [KK(lamda**4-1), KK(lamda**3), KK(lamda**2), KK(lamda), KK(1)]
  64. ], (1, 5), K)),
  65. ]
  66. assert Avects == ([], algebraic_eigenvects)
  67. # Test converting to Expr (slow):
  68. l0, l1, l2, l3, l4 = [CRootOf(lamda**5 - lamda + 1, i) for i in range(5)]
  69. sympy_eigenvects = [
  70. (l0, 1, [Matrix([-1 + l0**4, l0**3, l0**2, l0, 1])]),
  71. (l1, 1, [Matrix([-1 + l1**4, l1**3, l1**2, l1, 1])]),
  72. (l2, 1, [Matrix([-1 + l2**4, l2**3, l2**2, l2, 1])]),
  73. (l3, 1, [Matrix([-1 + l3**4, l3**3, l3**2, l3, 1])]),
  74. (l4, 1, [Matrix([-1 + l4**4, l4**3, l4**2, l4, 1])]),
  75. ]
  76. assert dom_eigenvects_to_sympy([], algebraic_eigenvects, Matrix) == sympy_eigenvects