test_polymatrix.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. from sympy.testing.pytest import raises
  2. from sympy.polys.polymatrix import PolyMatrix
  3. from sympy.polys import Poly
  4. from sympy.core.singleton import S
  5. from sympy.matrices.dense import Matrix
  6. from sympy.polys.domains.integerring import ZZ
  7. from sympy.polys.domains.rationalfield import QQ
  8. from sympy.abc import x, y
  9. def _test_polymatrix():
  10. pm1 = PolyMatrix([[Poly(x**2, x), Poly(-x, x)], [Poly(x**3, x), Poly(-1 + x, x)]])
  11. v1 = PolyMatrix([[1, 0], [-1, 0]], ring='ZZ[x]')
  12. m1 = PolyMatrix([[1, 0], [-1, 0]], ring='ZZ[x]')
  13. A = PolyMatrix([[Poly(x**2 + x, x), Poly(0, x)], \
  14. [Poly(x**3 - x + 1, x), Poly(0, x)]])
  15. B = PolyMatrix([[Poly(x**2, x), Poly(-x, x)], [Poly(-x**2, x), Poly(x, x)]])
  16. assert A.ring == ZZ[x]
  17. assert isinstance(pm1*v1, PolyMatrix)
  18. assert pm1*v1 == A
  19. assert pm1*m1 == A
  20. assert v1*pm1 == B
  21. pm2 = PolyMatrix([[Poly(x**2, x, domain='QQ'), Poly(0, x, domain='QQ'), Poly(-x**2, x, domain='QQ'), \
  22. Poly(x**3, x, domain='QQ'), Poly(0, x, domain='QQ'), Poly(-x**3, x, domain='QQ')]])
  23. assert pm2.ring == QQ[x]
  24. v2 = PolyMatrix([1, 0, 0, 0, 0, 0], ring='ZZ[x]')
  25. m2 = PolyMatrix([1, 0, 0, 0, 0, 0], ring='ZZ[x]')
  26. C = PolyMatrix([[Poly(x**2, x, domain='QQ')]])
  27. assert pm2*v2 == C
  28. assert pm2*m2 == C
  29. pm3 = PolyMatrix([[Poly(x**2, x), S.One]], ring='ZZ[x]')
  30. v3 = S.Half*pm3
  31. assert v3 == PolyMatrix([[Poly(S.Half*x**2, x, domain='QQ'), S.Half]], ring='QQ[x]')
  32. assert pm3*S.Half == v3
  33. assert v3.ring == QQ[x]
  34. pm4 = PolyMatrix([[Poly(x**2, x, domain='ZZ'), Poly(-x**2, x, domain='ZZ')]])
  35. v4 = PolyMatrix([1, -1], ring='ZZ[x]')
  36. assert pm4*v4 == PolyMatrix([[Poly(2*x**2, x, domain='ZZ')]])
  37. assert len(PolyMatrix(ring=ZZ[x])) == 0
  38. assert PolyMatrix([1, 0, 0, 1], x)/(-1) == PolyMatrix([-1, 0, 0, -1], x)
  39. def test_polymatrix_constructor():
  40. M1 = PolyMatrix([[x, y]], ring=QQ[x,y])
  41. assert M1.ring == QQ[x,y]
  42. assert M1.domain == QQ
  43. assert M1.gens == (x, y)
  44. assert M1.shape == (1, 2)
  45. assert M1.rows == 1
  46. assert M1.cols == 2
  47. assert len(M1) == 2
  48. assert list(M1) == [Poly(x, (x, y), domain=QQ), Poly(y, (x, y), domain=QQ)]
  49. M2 = PolyMatrix([[x, y]], ring=QQ[x][y])
  50. assert M2.ring == QQ[x][y]
  51. assert M2.domain == QQ[x]
  52. assert M2.gens == (y,)
  53. assert M2.shape == (1, 2)
  54. assert M2.rows == 1
  55. assert M2.cols == 2
  56. assert len(M2) == 2
  57. assert list(M2) == [Poly(x, (y,), domain=QQ[x]), Poly(y, (y,), domain=QQ[x])]
  58. assert PolyMatrix([[x, y]], y) == PolyMatrix([[x, y]], ring=ZZ.frac_field(x)[y])
  59. assert PolyMatrix([[x, y]], ring='ZZ[x,y]') == PolyMatrix([[x, y]], ring=ZZ[x,y])
  60. assert PolyMatrix([[x, y]], (x, y)) == PolyMatrix([[x, y]], ring=QQ[x,y])
  61. assert PolyMatrix([[x, y]], x, y) == PolyMatrix([[x, y]], ring=QQ[x,y])
  62. assert PolyMatrix([x, y]) == PolyMatrix([[x], [y]], ring=QQ[x,y])
  63. assert PolyMatrix(1, 2, [x, y]) == PolyMatrix([[x, y]], ring=QQ[x,y])
  64. assert PolyMatrix(1, 2, lambda i,j: [x,y][j]) == PolyMatrix([[x, y]], ring=QQ[x,y])
  65. assert PolyMatrix(0, 2, [], x, y).shape == (0, 2)
  66. assert PolyMatrix(2, 0, [], x, y).shape == (2, 0)
  67. assert PolyMatrix([[], []], x, y).shape == (2, 0)
  68. assert PolyMatrix(ring=QQ[x,y]) == PolyMatrix(0, 0, [], ring=QQ[x,y]) == PolyMatrix([], ring=QQ[x,y])
  69. raises(TypeError, lambda: PolyMatrix())
  70. raises(TypeError, lambda: PolyMatrix(1))
  71. assert PolyMatrix([Poly(x), Poly(y)]) == PolyMatrix([[x], [y]], ring=ZZ[x,y])
  72. # XXX: Maybe a bug in parallel_poly_from_expr (x lost from gens and domain):
  73. assert PolyMatrix([Poly(y, x), 1]) == PolyMatrix([[y], [1]], ring=QQ[y])
  74. def test_polymatrix_eq():
  75. assert (PolyMatrix([x]) == PolyMatrix([x])) is True
  76. assert (PolyMatrix([y]) == PolyMatrix([x])) is False
  77. assert (PolyMatrix([x]) != PolyMatrix([x])) is False
  78. assert (PolyMatrix([y]) != PolyMatrix([x])) is True
  79. assert PolyMatrix([[x, y]]) != PolyMatrix([x, y]) == PolyMatrix([[x], [y]])
  80. assert PolyMatrix([x], ring=QQ[x]) != PolyMatrix([x], ring=ZZ[x])
  81. assert PolyMatrix([x]) != Matrix([x])
  82. assert PolyMatrix([x]).to_Matrix() == Matrix([x])
  83. assert PolyMatrix([1], x) == PolyMatrix([1], x)
  84. assert PolyMatrix([1], x) != PolyMatrix([1], y)
  85. def test_polymatrix_from_Matrix():
  86. assert PolyMatrix.from_Matrix(Matrix([1, 2]), x) == PolyMatrix([1, 2], x, ring=QQ[x])
  87. assert PolyMatrix.from_Matrix(Matrix([1]), ring=QQ[x]) == PolyMatrix([1], x)
  88. pmx = PolyMatrix([1, 2], x)
  89. pmy = PolyMatrix([1, 2], y)
  90. assert pmx != pmy
  91. assert pmx.set_gens(y) == pmy
  92. def test_polymatrix_repr():
  93. assert repr(PolyMatrix([[1, 2]], x)) == 'PolyMatrix([[1, 2]], ring=QQ[x])'
  94. assert repr(PolyMatrix(0, 2, [], x)) == 'PolyMatrix(0, 2, [], ring=QQ[x])'
  95. def test_polymatrix_getitem():
  96. M = PolyMatrix([[1, 2], [3, 4]], x)
  97. assert M[:, :] == M
  98. assert M[0, :] == PolyMatrix([[1, 2]], x)
  99. assert M[:, 0] == PolyMatrix([1, 3], x)
  100. assert M[0, 0] == Poly(1, x, domain=QQ)
  101. assert M[0] == Poly(1, x, domain=QQ)
  102. assert M[:2] == [Poly(1, x, domain=QQ), Poly(2, x, domain=QQ)]
  103. def test_polymatrix_arithmetic():
  104. M = PolyMatrix([[1, 2], [3, 4]], x)
  105. assert M + M == PolyMatrix([[2, 4], [6, 8]], x)
  106. assert M - M == PolyMatrix([[0, 0], [0, 0]], x)
  107. assert -M == PolyMatrix([[-1, -2], [-3, -4]], x)
  108. raises(TypeError, lambda: M + 1)
  109. raises(TypeError, lambda: M - 1)
  110. raises(TypeError, lambda: 1 + M)
  111. raises(TypeError, lambda: 1 - M)
  112. assert M * M == PolyMatrix([[7, 10], [15, 22]], x)
  113. assert 2 * M == PolyMatrix([[2, 4], [6, 8]], x)
  114. assert M * 2 == PolyMatrix([[2, 4], [6, 8]], x)
  115. assert S(2) * M == PolyMatrix([[2, 4], [6, 8]], x)
  116. assert M * S(2) == PolyMatrix([[2, 4], [6, 8]], x)
  117. raises(TypeError, lambda: [] * M)
  118. raises(TypeError, lambda: M * [])
  119. M2 = PolyMatrix([[1, 2]], ring=ZZ[x])
  120. assert S.Half * M2 == PolyMatrix([[S.Half, 1]], ring=QQ[x])
  121. assert M2 * S.Half == PolyMatrix([[S.Half, 1]], ring=QQ[x])
  122. assert M / 2 == PolyMatrix([[S(1)/2, 1], [S(3)/2, 2]], x)
  123. assert M / Poly(2, x) == PolyMatrix([[S(1)/2, 1], [S(3)/2, 2]], x)
  124. raises(TypeError, lambda: M / [])
  125. def test_polymatrix_manipulations():
  126. M1 = PolyMatrix([[1, 2], [3, 4]], x)
  127. assert M1.transpose() == PolyMatrix([[1, 3], [2, 4]], x)
  128. M2 = PolyMatrix([[5, 6], [7, 8]], x)
  129. assert M1.row_join(M2) == PolyMatrix([[1, 2, 5, 6], [3, 4, 7, 8]], x)
  130. assert M1.col_join(M2) == PolyMatrix([[1, 2], [3, 4], [5, 6], [7, 8]], x)
  131. assert M1.applyfunc(lambda e: 2*e) == PolyMatrix([[2, 4], [6, 8]], x)
  132. def test_polymatrix_ones_zeros():
  133. assert PolyMatrix.zeros(1, 2, x) == PolyMatrix([[0, 0]], x)
  134. assert PolyMatrix.eye(2, x) == PolyMatrix([[1, 0], [0, 1]], x)
  135. def test_polymatrix_rref():
  136. M = PolyMatrix([[1, 2], [3, 4]], x)
  137. assert M.rref() == (PolyMatrix.eye(2, x), (0, 1))
  138. raises(ValueError, lambda: PolyMatrix([1, 2], ring=ZZ[x]).rref())
  139. raises(ValueError, lambda: PolyMatrix([1, x], ring=QQ[x]).rref())
  140. def test_polymatrix_nullspace():
  141. M = PolyMatrix([[1, 2], [3, 6]], x)
  142. assert M.nullspace() == [PolyMatrix([-2, 1], x)]
  143. raises(ValueError, lambda: PolyMatrix([1, 2], ring=ZZ[x]).nullspace())
  144. raises(ValueError, lambda: PolyMatrix([1, x], ring=QQ[x]).nullspace())
  145. assert M.rank() == 1