test_domains.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Test Matrix/DomainMatrix interaction.
  2. from sympy import GF, ZZ, QQ, EXRAW
  3. from sympy.polys.matrices import DomainMatrix, DM
  4. from sympy import (
  5. Matrix,
  6. MutableMatrix,
  7. ImmutableMatrix,
  8. SparseMatrix,
  9. MutableDenseMatrix,
  10. ImmutableDenseMatrix,
  11. MutableSparseMatrix,
  12. ImmutableSparseMatrix,
  13. )
  14. from sympy import symbols, S, sqrt
  15. from sympy.testing.pytest import raises
  16. x, y = symbols('x y')
  17. MATRIX_TYPES = (
  18. Matrix,
  19. MutableMatrix,
  20. ImmutableMatrix,
  21. SparseMatrix,
  22. MutableDenseMatrix,
  23. ImmutableDenseMatrix,
  24. MutableSparseMatrix,
  25. ImmutableSparseMatrix,
  26. )
  27. IMMUTABLE = (
  28. ImmutableMatrix,
  29. ImmutableDenseMatrix,
  30. ImmutableSparseMatrix,
  31. )
  32. def DMs(items, domain):
  33. return DM(items, domain).to_sparse()
  34. def test_Matrix_rep_domain():
  35. for Mat in MATRIX_TYPES:
  36. M = Mat([[1, 2], [3, 4]])
  37. assert M._rep == DMs([[1, 2], [3, 4]], ZZ)
  38. assert (M / 2)._rep == DMs([[(1,2), 1], [(3,2), 2]], QQ)
  39. if not isinstance(M, IMMUTABLE):
  40. M[0, 0] = x
  41. assert M._rep == DMs([[x, 2], [3, 4]], EXRAW)
  42. M = Mat([[S(1)/2, 2], [3, 4]])
  43. assert M._rep == DMs([[(1,2), 2], [3, 4]], QQ)
  44. if not isinstance(M, IMMUTABLE):
  45. M[0, 0] = x
  46. assert M._rep == DMs([[x, 2], [3, 4]], EXRAW)
  47. dM = DMs([[1, 2], [3, 4]], ZZ)
  48. assert Mat._fromrep(dM)._rep == dM
  49. # XXX: This is not intended. Perhaps it should be coerced to EXRAW?
  50. # The private _fromrep method is never called like this but perhaps it
  51. # should be guarded.
  52. #
  53. # It is not clear how to integrate domains other than ZZ, QQ and EXRAW with
  54. # the rest of Matrix or if the public type for this needs to be something
  55. # different from Matrix somehow.
  56. K = QQ.algebraic_field(sqrt(2))
  57. dM = DM([[1, 2], [3, 4]], K)
  58. assert Mat._fromrep(dM)._rep.domain == K
  59. def test_Matrix_to_DM():
  60. M = Matrix([[1, 2], [3, 4]])
  61. assert M.to_DM() == DMs([[1, 2], [3, 4]], ZZ)
  62. assert M.to_DM() is not M._rep
  63. assert M.to_DM(field=True) == DMs([[1, 2], [3, 4]], QQ)
  64. assert M.to_DM(domain=QQ) == DMs([[1, 2], [3, 4]], QQ)
  65. assert M.to_DM(domain=QQ[x]) == DMs([[1, 2], [3, 4]], QQ[x])
  66. assert M.to_DM(domain=GF(3)) == DMs([[1, 2], [0, 1]], GF(3))
  67. M = Matrix([[1, 2], [3, 4]])
  68. M[0, 0] = x
  69. assert M._rep.domain == EXRAW
  70. M[0, 0] = 1
  71. assert M.to_DM() == DMs([[1, 2], [3, 4]], ZZ)
  72. M = Matrix([[S(1)/2, 2], [3, 4]])
  73. assert M.to_DM() == DMs([[QQ(1,2), 2], [3, 4]], QQ)
  74. M = Matrix([[x, 2], [3, 4]])
  75. assert M.to_DM() == DMs([[x, 2], [3, 4]], ZZ[x])
  76. assert M.to_DM(field=True) == DMs([[x, 2], [3, 4]], ZZ.frac_field(x))
  77. M = Matrix([[1/x, 2], [3, 4]])
  78. assert M.to_DM() == DMs([[1/x, 2], [3, 4]], ZZ.frac_field(x))
  79. M = Matrix([[1, sqrt(2)], [3, 4]])
  80. K = QQ.algebraic_field(sqrt(2))
  81. sqrt2 = K.from_sympy(sqrt(2)) # XXX: Maybe K(sqrt(2)) should work
  82. M_K = DomainMatrix([[K(1), sqrt2], [K(3), K(4)]], (2, 2), K)
  83. assert M.to_DM() == DMs([[1, sqrt(2)], [3, 4]], EXRAW)
  84. assert M.to_DM(extension=True) == M_K.to_sparse()
  85. # Options cannot be used with the domain parameter
  86. M = Matrix([[1, 2], [3, 4]])
  87. raises(TypeError, lambda: M.to_DM(domain=QQ, field=True))