test_polyutils.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. """Tests for useful utilities for higher level polynomial classes. """
  2. from sympy.core.mul import Mul
  3. from sympy.core.numbers import (Integer, pi)
  4. from sympy.core.relational import Eq
  5. from sympy.core.singleton import S
  6. from sympy.core.symbol import (Symbol, symbols)
  7. from sympy.functions.elementary.exponential import exp
  8. from sympy.functions.elementary.miscellaneous import sqrt
  9. from sympy.functions.elementary.trigonometric import (cos, sin)
  10. from sympy.integrals.integrals import Integral
  11. from sympy.testing.pytest import raises
  12. from sympy.polys.polyutils import (
  13. _nsort,
  14. _sort_gens,
  15. _unify_gens,
  16. _analyze_gens,
  17. _sort_factors,
  18. parallel_dict_from_expr,
  19. dict_from_expr,
  20. )
  21. from sympy.polys.polyerrors import PolynomialError
  22. from sympy.polys.domains import ZZ
  23. x, y, z, p, q, r, s, t, u, v, w = symbols('x,y,z,p,q,r,s,t,u,v,w')
  24. A, B = symbols('A,B', commutative=False)
  25. def test__nsort():
  26. # issue 6137
  27. r = S('''[3/2 + sqrt(-14/3 - 2*(-415/216 + 13*I/12)**(1/3) - 4/sqrt(-7/3 +
  28. 61/(18*(-415/216 + 13*I/12)**(1/3)) + 2*(-415/216 + 13*I/12)**(1/3)) -
  29. 61/(18*(-415/216 + 13*I/12)**(1/3)))/2 - sqrt(-7/3 + 61/(18*(-415/216
  30. + 13*I/12)**(1/3)) + 2*(-415/216 + 13*I/12)**(1/3))/2, 3/2 - sqrt(-7/3
  31. + 61/(18*(-415/216 + 13*I/12)**(1/3)) + 2*(-415/216 +
  32. 13*I/12)**(1/3))/2 - sqrt(-14/3 - 2*(-415/216 + 13*I/12)**(1/3) -
  33. 4/sqrt(-7/3 + 61/(18*(-415/216 + 13*I/12)**(1/3)) + 2*(-415/216 +
  34. 13*I/12)**(1/3)) - 61/(18*(-415/216 + 13*I/12)**(1/3)))/2, 3/2 +
  35. sqrt(-14/3 - 2*(-415/216 + 13*I/12)**(1/3) + 4/sqrt(-7/3 +
  36. 61/(18*(-415/216 + 13*I/12)**(1/3)) + 2*(-415/216 + 13*I/12)**(1/3)) -
  37. 61/(18*(-415/216 + 13*I/12)**(1/3)))/2 + sqrt(-7/3 + 61/(18*(-415/216
  38. + 13*I/12)**(1/3)) + 2*(-415/216 + 13*I/12)**(1/3))/2, 3/2 + sqrt(-7/3
  39. + 61/(18*(-415/216 + 13*I/12)**(1/3)) + 2*(-415/216 +
  40. 13*I/12)**(1/3))/2 - sqrt(-14/3 - 2*(-415/216 + 13*I/12)**(1/3) +
  41. 4/sqrt(-7/3 + 61/(18*(-415/216 + 13*I/12)**(1/3)) + 2*(-415/216 +
  42. 13*I/12)**(1/3)) - 61/(18*(-415/216 + 13*I/12)**(1/3)))/2]''')
  43. ans = [r[1], r[0], r[-1], r[-2]]
  44. assert _nsort(r) == ans
  45. assert len(_nsort(r, separated=True)[0]) == 0
  46. b, c, a = exp(-1000), exp(-999), exp(-1001)
  47. assert _nsort((b, c, a)) == [a, b, c]
  48. # issue 12560
  49. a = cos(1)**2 + sin(1)**2 - 1
  50. assert _nsort([a]) == [a]
  51. def test__sort_gens():
  52. assert _sort_gens([]) == ()
  53. assert _sort_gens([x]) == (x,)
  54. assert _sort_gens([p]) == (p,)
  55. assert _sort_gens([q]) == (q,)
  56. assert _sort_gens([x, p]) == (x, p)
  57. assert _sort_gens([p, x]) == (x, p)
  58. assert _sort_gens([q, p]) == (p, q)
  59. assert _sort_gens([q, p, x]) == (x, p, q)
  60. assert _sort_gens([x, p, q], wrt=x) == (x, p, q)
  61. assert _sort_gens([x, p, q], wrt=p) == (p, x, q)
  62. assert _sort_gens([x, p, q], wrt=q) == (q, x, p)
  63. assert _sort_gens([x, p, q], wrt='x') == (x, p, q)
  64. assert _sort_gens([x, p, q], wrt='p') == (p, x, q)
  65. assert _sort_gens([x, p, q], wrt='q') == (q, x, p)
  66. assert _sort_gens([x, p, q], wrt='x,q') == (x, q, p)
  67. assert _sort_gens([x, p, q], wrt='q,x') == (q, x, p)
  68. assert _sort_gens([x, p, q], wrt='p,q') == (p, q, x)
  69. assert _sort_gens([x, p, q], wrt='q,p') == (q, p, x)
  70. assert _sort_gens([x, p, q], wrt='x, q') == (x, q, p)
  71. assert _sort_gens([x, p, q], wrt='q, x') == (q, x, p)
  72. assert _sort_gens([x, p, q], wrt='p, q') == (p, q, x)
  73. assert _sort_gens([x, p, q], wrt='q, p') == (q, p, x)
  74. assert _sort_gens([x, p, q], wrt=[x, 'q']) == (x, q, p)
  75. assert _sort_gens([x, p, q], wrt=[q, 'x']) == (q, x, p)
  76. assert _sort_gens([x, p, q], wrt=[p, 'q']) == (p, q, x)
  77. assert _sort_gens([x, p, q], wrt=[q, 'p']) == (q, p, x)
  78. assert _sort_gens([x, p, q], wrt=['x', 'q']) == (x, q, p)
  79. assert _sort_gens([x, p, q], wrt=['q', 'x']) == (q, x, p)
  80. assert _sort_gens([x, p, q], wrt=['p', 'q']) == (p, q, x)
  81. assert _sort_gens([x, p, q], wrt=['q', 'p']) == (q, p, x)
  82. assert _sort_gens([x, p, q], sort='x > p > q') == (x, p, q)
  83. assert _sort_gens([x, p, q], sort='p > x > q') == (p, x, q)
  84. assert _sort_gens([x, p, q], sort='p > q > x') == (p, q, x)
  85. assert _sort_gens([x, p, q], wrt='x', sort='q > p') == (x, q, p)
  86. assert _sort_gens([x, p, q], wrt='p', sort='q > x') == (p, q, x)
  87. assert _sort_gens([x, p, q], wrt='q', sort='p > x') == (q, p, x)
  88. # https://github.com/sympy/sympy/issues/19353
  89. n1 = Symbol('\n1')
  90. assert _sort_gens([n1]) == (n1,)
  91. assert _sort_gens([x, n1]) == (x, n1)
  92. X = symbols('x0,x1,x2,x10,x11,x12,x20,x21,x22')
  93. assert _sort_gens(X) == X
  94. def test__unify_gens():
  95. assert _unify_gens([], []) == ()
  96. assert _unify_gens([x], [x]) == (x,)
  97. assert _unify_gens([y], [y]) == (y,)
  98. assert _unify_gens([x, y], [x]) == (x, y)
  99. assert _unify_gens([x], [x, y]) == (x, y)
  100. assert _unify_gens([x, y], [x, y]) == (x, y)
  101. assert _unify_gens([y, x], [y, x]) == (y, x)
  102. assert _unify_gens([x], [y]) == (x, y)
  103. assert _unify_gens([y], [x]) == (y, x)
  104. assert _unify_gens([x], [y, x]) == (y, x)
  105. assert _unify_gens([y, x], [x]) == (y, x)
  106. assert _unify_gens([x, y, z], [x, y, z]) == (x, y, z)
  107. assert _unify_gens([z, y, x], [x, y, z]) == (z, y, x)
  108. assert _unify_gens([x, y, z], [z, y, x]) == (x, y, z)
  109. assert _unify_gens([z, y, x], [z, y, x]) == (z, y, x)
  110. assert _unify_gens([x, y, z], [t, x, p, q, z]) == (t, x, y, p, q, z)
  111. def test__analyze_gens():
  112. assert _analyze_gens((x, y, z)) == (x, y, z)
  113. assert _analyze_gens([x, y, z]) == (x, y, z)
  114. assert _analyze_gens(([x, y, z],)) == (x, y, z)
  115. assert _analyze_gens(((x, y, z),)) == (x, y, z)
  116. def test__sort_factors():
  117. assert _sort_factors([], multiple=True) == []
  118. assert _sort_factors([], multiple=False) == []
  119. F = [[1, 2, 3], [1, 2], [1]]
  120. G = [[1], [1, 2], [1, 2, 3]]
  121. assert _sort_factors(F, multiple=False) == G
  122. F = [[1, 2], [1, 2, 3], [1, 2], [1]]
  123. G = [[1], [1, 2], [1, 2], [1, 2, 3]]
  124. assert _sort_factors(F, multiple=False) == G
  125. F = [[2, 2], [1, 2, 3], [1, 2], [1]]
  126. G = [[1], [1, 2], [2, 2], [1, 2, 3]]
  127. assert _sort_factors(F, multiple=False) == G
  128. F = [([1, 2, 3], 1), ([1, 2], 1), ([1], 1)]
  129. G = [([1], 1), ([1, 2], 1), ([1, 2, 3], 1)]
  130. assert _sort_factors(F, multiple=True) == G
  131. F = [([1, 2], 1), ([1, 2, 3], 1), ([1, 2], 1), ([1], 1)]
  132. G = [([1], 1), ([1, 2], 1), ([1, 2], 1), ([1, 2, 3], 1)]
  133. assert _sort_factors(F, multiple=True) == G
  134. F = [([2, 2], 1), ([1, 2, 3], 1), ([1, 2], 1), ([1], 1)]
  135. G = [([1], 1), ([1, 2], 1), ([2, 2], 1), ([1, 2, 3], 1)]
  136. assert _sort_factors(F, multiple=True) == G
  137. F = [([2, 2], 1), ([1, 2, 3], 1), ([1, 2], 2), ([1], 1)]
  138. G = [([1], 1), ([2, 2], 1), ([1, 2], 2), ([1, 2, 3], 1)]
  139. assert _sort_factors(F, multiple=True) == G
  140. def test__dict_from_expr_if_gens():
  141. assert dict_from_expr(
  142. Integer(17), gens=(x,)) == ({(0,): Integer(17)}, (x,))
  143. assert dict_from_expr(
  144. Integer(17), gens=(x, y)) == ({(0, 0): Integer(17)}, (x, y))
  145. assert dict_from_expr(
  146. Integer(17), gens=(x, y, z)) == ({(0, 0, 0): Integer(17)}, (x, y, z))
  147. assert dict_from_expr(
  148. Integer(-17), gens=(x,)) == ({(0,): Integer(-17)}, (x,))
  149. assert dict_from_expr(
  150. Integer(-17), gens=(x, y)) == ({(0, 0): Integer(-17)}, (x, y))
  151. assert dict_from_expr(Integer(
  152. -17), gens=(x, y, z)) == ({(0, 0, 0): Integer(-17)}, (x, y, z))
  153. assert dict_from_expr(
  154. Integer(17)*x, gens=(x,)) == ({(1,): Integer(17)}, (x,))
  155. assert dict_from_expr(
  156. Integer(17)*x, gens=(x, y)) == ({(1, 0): Integer(17)}, (x, y))
  157. assert dict_from_expr(Integer(
  158. 17)*x, gens=(x, y, z)) == ({(1, 0, 0): Integer(17)}, (x, y, z))
  159. assert dict_from_expr(
  160. Integer(17)*x**7, gens=(x,)) == ({(7,): Integer(17)}, (x,))
  161. assert dict_from_expr(
  162. Integer(17)*x**7*y, gens=(x, y)) == ({(7, 1): Integer(17)}, (x, y))
  163. assert dict_from_expr(Integer(17)*x**7*y*z**12, gens=(
  164. x, y, z)) == ({(7, 1, 12): Integer(17)}, (x, y, z))
  165. assert dict_from_expr(x + 2*y + 3*z, gens=(x,)) == \
  166. ({(1,): Integer(1), (0,): 2*y + 3*z}, (x,))
  167. assert dict_from_expr(x + 2*y + 3*z, gens=(x, y)) == \
  168. ({(1, 0): Integer(1), (0, 1): Integer(2), (0, 0): 3*z}, (x, y))
  169. assert dict_from_expr(x + 2*y + 3*z, gens=(x, y, z)) == \
  170. ({(1, 0, 0): Integer(
  171. 1), (0, 1, 0): Integer(2), (0, 0, 1): Integer(3)}, (x, y, z))
  172. assert dict_from_expr(x*y + 2*x*z + 3*y*z, gens=(x,)) == \
  173. ({(1,): y + 2*z, (0,): 3*y*z}, (x,))
  174. assert dict_from_expr(x*y + 2*x*z + 3*y*z, gens=(x, y)) == \
  175. ({(1, 1): Integer(1), (1, 0): 2*z, (0, 1): 3*z}, (x, y))
  176. assert dict_from_expr(x*y + 2*x*z + 3*y*z, gens=(x, y, z)) == \
  177. ({(1, 1, 0): Integer(
  178. 1), (1, 0, 1): Integer(2), (0, 1, 1): Integer(3)}, (x, y, z))
  179. assert dict_from_expr(2**y*x, gens=(x,)) == ({(1,): 2**y}, (x,))
  180. assert dict_from_expr(Integral(x, (x, 1, 2)) + x) == (
  181. {(0, 1): 1, (1, 0): 1}, (x, Integral(x, (x, 1, 2))))
  182. raises(PolynomialError, lambda: dict_from_expr(2**y*x, gens=(x, y)))
  183. def test__dict_from_expr_no_gens():
  184. assert dict_from_expr(Integer(17)) == ({(): Integer(17)}, ())
  185. assert dict_from_expr(x) == ({(1,): Integer(1)}, (x,))
  186. assert dict_from_expr(y) == ({(1,): Integer(1)}, (y,))
  187. assert dict_from_expr(x*y) == ({(1, 1): Integer(1)}, (x, y))
  188. assert dict_from_expr(
  189. x + y) == ({(1, 0): Integer(1), (0, 1): Integer(1)}, (x, y))
  190. assert dict_from_expr(sqrt(2)) == ({(1,): Integer(1)}, (sqrt(2),))
  191. assert dict_from_expr(sqrt(2), greedy=False) == ({(): sqrt(2)}, ())
  192. assert dict_from_expr(x*y, domain=ZZ[x]) == ({(1,): x}, (y,))
  193. assert dict_from_expr(x*y, domain=ZZ[y]) == ({(1,): y}, (x,))
  194. assert dict_from_expr(3*sqrt(
  195. 2)*pi*x*y, extension=None) == ({(1, 1, 1, 1): 3}, (x, y, pi, sqrt(2)))
  196. assert dict_from_expr(3*sqrt(
  197. 2)*pi*x*y, extension=True) == ({(1, 1, 1): 3*sqrt(2)}, (x, y, pi))
  198. assert dict_from_expr(3*sqrt(
  199. 2)*pi*x*y, extension=True) == ({(1, 1, 1): 3*sqrt(2)}, (x, y, pi))
  200. f = cos(x)*sin(x) + cos(x)*sin(y) + cos(y)*sin(x) + cos(y)*sin(y)
  201. assert dict_from_expr(f) == ({(0, 1, 0, 1): 1, (0, 1, 1, 0): 1,
  202. (1, 0, 0, 1): 1, (1, 0, 1, 0): 1}, (cos(x), cos(y), sin(x), sin(y)))
  203. def test__parallel_dict_from_expr_if_gens():
  204. assert parallel_dict_from_expr([x + 2*y + 3*z, Integer(7)], gens=(x,)) == \
  205. ([{(1,): Integer(1), (0,): 2*y + 3*z}, {(0,): Integer(7)}], (x,))
  206. def test__parallel_dict_from_expr_no_gens():
  207. assert parallel_dict_from_expr([x*y, Integer(3)]) == \
  208. ([{(1, 1): Integer(1)}, {(0, 0): Integer(3)}], (x, y))
  209. assert parallel_dict_from_expr([x*y, 2*z, Integer(3)]) == \
  210. ([{(1, 1, 0): Integer(
  211. 1)}, {(0, 0, 1): Integer(2)}, {(0, 0, 0): Integer(3)}], (x, y, z))
  212. assert parallel_dict_from_expr((Mul(x, x**2, evaluate=False),)) == \
  213. ([{(3,): 1}], (x,))
  214. def test_parallel_dict_from_expr():
  215. assert parallel_dict_from_expr([Eq(x, 1), Eq(
  216. x**2, 2)]) == ([{(0,): -Integer(1), (1,): Integer(1)},
  217. {(0,): -Integer(2), (2,): Integer(1)}], (x,))
  218. raises(PolynomialError, lambda: parallel_dict_from_expr([A*B - B*A]))
  219. def test_dict_from_expr():
  220. assert dict_from_expr(Eq(x, 1)) == \
  221. ({(0,): -Integer(1), (1,): Integer(1)}, (x,))
  222. raises(PolynomialError, lambda: dict_from_expr(A*B - B*A))
  223. raises(PolynomialError, lambda: dict_from_expr(S.true))