test_multivariate_resultants.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. """Tests for Dixon's and Macaulay's classes. """
  2. from sympy.matrices.dense import Matrix
  3. from sympy.polys.polytools import factor
  4. from sympy.core import symbols
  5. from sympy.tensor.indexed import IndexedBase
  6. from sympy.polys.multivariate_resultants import (DixonResultant,
  7. MacaulayResultant)
  8. c, d = symbols("a, b")
  9. x, y = symbols("x, y")
  10. p = c * x + y
  11. q = x + d * y
  12. dixon = DixonResultant(polynomials=[p, q], variables=[x, y])
  13. macaulay = MacaulayResultant(polynomials=[p, q], variables=[x, y])
  14. def test_dixon_resultant_init():
  15. """Test init method of DixonResultant."""
  16. a = IndexedBase("alpha")
  17. assert dixon.polynomials == [p, q]
  18. assert dixon.variables == [x, y]
  19. assert dixon.n == 2
  20. assert dixon.m == 2
  21. assert dixon.dummy_variables == [a[0], a[1]]
  22. def test_get_dixon_polynomial_numerical():
  23. """Test Dixon's polynomial for a numerical example."""
  24. a = IndexedBase("alpha")
  25. p = x + y
  26. q = x ** 2 + y **3
  27. h = x ** 2 + y
  28. dixon = DixonResultant([p, q, h], [x, y])
  29. polynomial = -x * y ** 2 * a[0] - x * y ** 2 * a[1] - x * y * a[0] \
  30. * a[1] - x * y * a[1] ** 2 - x * a[0] * a[1] ** 2 + x * a[0] - \
  31. y ** 2 * a[0] * a[1] + y ** 2 * a[1] - y * a[0] * a[1] ** 2 + y * \
  32. a[1] ** 2
  33. assert dixon.get_dixon_polynomial().as_expr().expand() == polynomial
  34. def test_get_max_degrees():
  35. """Tests max degrees function."""
  36. p = x + y
  37. q = x ** 2 + y **3
  38. h = x ** 2 + y
  39. dixon = DixonResultant(polynomials=[p, q, h], variables=[x, y])
  40. dixon_polynomial = dixon.get_dixon_polynomial()
  41. assert dixon.get_max_degrees(dixon_polynomial) == [1, 2]
  42. def test_get_dixon_matrix():
  43. """Test Dixon's resultant for a numerical example."""
  44. x, y = symbols('x, y')
  45. p = x + y
  46. q = x ** 2 + y ** 3
  47. h = x ** 2 + y
  48. dixon = DixonResultant([p, q, h], [x, y])
  49. polynomial = dixon.get_dixon_polynomial()
  50. assert dixon.get_dixon_matrix(polynomial).det() == 0
  51. def test_get_dixon_matrix_example_two():
  52. """Test Dixon's matrix for example from [Palancz08]_."""
  53. x, y, z = symbols('x, y, z')
  54. f = x ** 2 + y ** 2 - 1 + z * 0
  55. g = x ** 2 + z ** 2 - 1 + y * 0
  56. h = y ** 2 + z ** 2 - 1
  57. example_two = DixonResultant([f, g, h], [y, z])
  58. poly = example_two.get_dixon_polynomial()
  59. matrix = example_two.get_dixon_matrix(poly)
  60. expr = 1 - 8 * x ** 2 + 24 * x ** 4 - 32 * x ** 6 + 16 * x ** 8
  61. assert (matrix.det() - expr).expand() == 0
  62. def test_KSY_precondition():
  63. """Tests precondition for KSY Resultant."""
  64. A, B, C = symbols('A, B, C')
  65. m1 = Matrix([[1, 2, 3],
  66. [4, 5, 12],
  67. [6, 7, 18]])
  68. m2 = Matrix([[0, C**2],
  69. [-2 * C, -C ** 2]])
  70. m3 = Matrix([[1, 0],
  71. [0, 1]])
  72. m4 = Matrix([[A**2, 0, 1],
  73. [A, 1, 1 / A]])
  74. m5 = Matrix([[5, 1],
  75. [2, B],
  76. [0, 1],
  77. [0, 0]])
  78. assert dixon.KSY_precondition(m1) == False
  79. assert dixon.KSY_precondition(m2) == True
  80. assert dixon.KSY_precondition(m3) == True
  81. assert dixon.KSY_precondition(m4) == False
  82. assert dixon.KSY_precondition(m5) == True
  83. def test_delete_zero_rows_and_columns():
  84. """Tests method for deleting rows and columns containing only zeros."""
  85. A, B, C = symbols('A, B, C')
  86. m1 = Matrix([[0, 0],
  87. [0, 0],
  88. [1, 2]])
  89. m2 = Matrix([[0, 1, 2],
  90. [0, 3, 4],
  91. [0, 5, 6]])
  92. m3 = Matrix([[0, 0, 0, 0],
  93. [0, 1, 2, 0],
  94. [0, 3, 4, 0],
  95. [0, 0, 0, 0]])
  96. m4 = Matrix([[1, 0, 2],
  97. [0, 0, 0],
  98. [3, 0, 4]])
  99. m5 = Matrix([[0, 0, 0, 1],
  100. [0, 0, 0, 2],
  101. [0, 0, 0, 3],
  102. [0, 0, 0, 4]])
  103. m6 = Matrix([[0, 0, A],
  104. [B, 0, 0],
  105. [0, 0, C]])
  106. assert dixon.delete_zero_rows_and_columns(m1) == Matrix([[1, 2]])
  107. assert dixon.delete_zero_rows_and_columns(m2) == Matrix([[1, 2],
  108. [3, 4],
  109. [5, 6]])
  110. assert dixon.delete_zero_rows_and_columns(m3) == Matrix([[1, 2],
  111. [3, 4]])
  112. assert dixon.delete_zero_rows_and_columns(m4) == Matrix([[1, 2],
  113. [3, 4]])
  114. assert dixon.delete_zero_rows_and_columns(m5) == Matrix([[1],
  115. [2],
  116. [3],
  117. [4]])
  118. assert dixon.delete_zero_rows_and_columns(m6) == Matrix([[0, A],
  119. [B, 0],
  120. [0, C]])
  121. def test_product_leading_entries():
  122. """Tests product of leading entries method."""
  123. A, B = symbols('A, B')
  124. m1 = Matrix([[1, 2, 3],
  125. [0, 4, 5],
  126. [0, 0, 6]])
  127. m2 = Matrix([[0, 0, 1],
  128. [2, 0, 3]])
  129. m3 = Matrix([[0, 0, 0],
  130. [1, 2, 3],
  131. [0, 0, 0]])
  132. m4 = Matrix([[0, 0, A],
  133. [1, 2, 3],
  134. [B, 0, 0]])
  135. assert dixon.product_leading_entries(m1) == 24
  136. assert dixon.product_leading_entries(m2) == 2
  137. assert dixon.product_leading_entries(m3) == 1
  138. assert dixon.product_leading_entries(m4) == A * B
  139. def test_get_KSY_Dixon_resultant_example_one():
  140. """Tests the KSY Dixon resultant for example one"""
  141. x, y, z = symbols('x, y, z')
  142. p = x * y * z
  143. q = x**2 - z**2
  144. h = x + y + z
  145. dixon = DixonResultant([p, q, h], [x, y])
  146. dixon_poly = dixon.get_dixon_polynomial()
  147. dixon_matrix = dixon.get_dixon_matrix(dixon_poly)
  148. D = dixon.get_KSY_Dixon_resultant(dixon_matrix)
  149. assert D == -z**3
  150. def test_get_KSY_Dixon_resultant_example_two():
  151. """Tests the KSY Dixon resultant for example two"""
  152. x, y, A = symbols('x, y, A')
  153. p = x * y + x * A + x - A**2 - A + y**2 + y
  154. q = x**2 + x * A - x + x * y + y * A - y
  155. h = x**2 + x * y + 2 * x - x * A - y * A - 2 * A
  156. dixon = DixonResultant([p, q, h], [x, y])
  157. dixon_poly = dixon.get_dixon_polynomial()
  158. dixon_matrix = dixon.get_dixon_matrix(dixon_poly)
  159. D = factor(dixon.get_KSY_Dixon_resultant(dixon_matrix))
  160. assert D == -8*A*(A - 1)*(A + 2)*(2*A - 1)**2
  161. def test_macaulay_resultant_init():
  162. """Test init method of MacaulayResultant."""
  163. assert macaulay.polynomials == [p, q]
  164. assert macaulay.variables == [x, y]
  165. assert macaulay.n == 2
  166. assert macaulay.degrees == [1, 1]
  167. assert macaulay.degree_m == 1
  168. assert macaulay.monomials_size == 2
  169. def test_get_degree_m():
  170. assert macaulay._get_degree_m() == 1
  171. def test_get_size():
  172. assert macaulay.get_size() == 2
  173. def test_macaulay_example_one():
  174. """Tests the Macaulay for example from [Bruce97]_"""
  175. x, y, z = symbols('x, y, z')
  176. a_1_1, a_1_2, a_1_3 = symbols('a_1_1, a_1_2, a_1_3')
  177. a_2_2, a_2_3, a_3_3 = symbols('a_2_2, a_2_3, a_3_3')
  178. b_1_1, b_1_2, b_1_3 = symbols('b_1_1, b_1_2, b_1_3')
  179. b_2_2, b_2_3, b_3_3 = symbols('b_2_2, b_2_3, b_3_3')
  180. c_1, c_2, c_3 = symbols('c_1, c_2, c_3')
  181. f_1 = a_1_1 * x ** 2 + a_1_2 * x * y + a_1_3 * x * z + \
  182. a_2_2 * y ** 2 + a_2_3 * y * z + a_3_3 * z ** 2
  183. f_2 = b_1_1 * x ** 2 + b_1_2 * x * y + b_1_3 * x * z + \
  184. b_2_2 * y ** 2 + b_2_3 * y * z + b_3_3 * z ** 2
  185. f_3 = c_1 * x + c_2 * y + c_3 * z
  186. mac = MacaulayResultant([f_1, f_2, f_3], [x, y, z])
  187. assert mac.degrees == [2, 2, 1]
  188. assert mac.degree_m == 3
  189. assert mac.monomial_set == [x ** 3, x ** 2 * y, x ** 2 * z,
  190. x * y ** 2,
  191. x * y * z, x * z ** 2, y ** 3,
  192. y ** 2 *z, y * z ** 2, z ** 3]
  193. assert mac.monomials_size == 10
  194. assert mac.get_row_coefficients() == [[x, y, z], [x, y, z],
  195. [x * y, x * z, y * z, z ** 2]]
  196. matrix = mac.get_matrix()
  197. assert matrix.shape == (mac.monomials_size, mac.monomials_size)
  198. assert mac.get_submatrix(matrix) == Matrix([[a_1_1, a_2_2],
  199. [b_1_1, b_2_2]])
  200. def test_macaulay_example_two():
  201. """Tests the Macaulay formulation for example from [Stiller96]_."""
  202. x, y, z = symbols('x, y, z')
  203. a_0, a_1, a_2 = symbols('a_0, a_1, a_2')
  204. b_0, b_1, b_2 = symbols('b_0, b_1, b_2')
  205. c_0, c_1, c_2, c_3, c_4 = symbols('c_0, c_1, c_2, c_3, c_4')
  206. f = a_0 * y - a_1 * x + a_2 * z
  207. g = b_1 * x ** 2 + b_0 * y ** 2 - b_2 * z ** 2
  208. h = c_0 * y - c_1 * x ** 3 + c_2 * x ** 2 * z - c_3 * x * z ** 2 + \
  209. c_4 * z ** 3
  210. mac = MacaulayResultant([f, g, h], [x, y, z])
  211. assert mac.degrees == [1, 2, 3]
  212. assert mac.degree_m == 4
  213. assert mac.monomials_size == 15
  214. assert len(mac.get_row_coefficients()) == mac.n
  215. matrix = mac.get_matrix()
  216. assert matrix.shape == (mac.monomials_size, mac.monomials_size)
  217. assert mac.get_submatrix(matrix) == Matrix([[-a_1, a_0, a_2, 0],
  218. [0, -a_1, 0, 0],
  219. [0, 0, -a_1, 0],
  220. [0, 0, 0, -a_1]])