test_polysys.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. """Tests for solvers of systems of polynomial equations. """
  2. from sympy.core.numbers import (I, Integer, Rational)
  3. from sympy.core.singleton import S
  4. from sympy.core.symbol import symbols
  5. from sympy.functions.elementary.miscellaneous import sqrt
  6. from sympy.polys.domains.rationalfield import QQ
  7. from sympy.polys.polyerrors import UnsolvableFactorError
  8. from sympy.polys.polyoptions import Options
  9. from sympy.polys.polytools import Poly
  10. from sympy.solvers.solvers import solve
  11. from sympy.utilities.iterables import flatten
  12. from sympy.abc import x, y, z
  13. from sympy.polys import PolynomialError
  14. from sympy.solvers.polysys import (solve_poly_system,
  15. solve_triangulated,
  16. solve_biquadratic, SolveFailed,
  17. solve_generic)
  18. from sympy.polys.polytools import parallel_poly_from_expr
  19. from sympy.testing.pytest import raises
  20. def test_solve_poly_system():
  21. assert solve_poly_system([x - 1], x) == [(S.One,)]
  22. assert solve_poly_system([y - x, y - x - 1], x, y) is None
  23. assert solve_poly_system([y - x**2, y + x**2], x, y) == [(S.Zero, S.Zero)]
  24. assert solve_poly_system([2*x - 3, y*Rational(3, 2) - 2*x, z - 5*y], x, y, z) == \
  25. [(Rational(3, 2), Integer(2), Integer(10))]
  26. assert solve_poly_system([x*y - 2*y, 2*y**2 - x**2], x, y) == \
  27. [(0, 0), (2, -sqrt(2)), (2, sqrt(2))]
  28. assert solve_poly_system([y - x**2, y + x**2 + 1], x, y) == \
  29. [(-I*sqrt(S.Half), Rational(-1, 2)), (I*sqrt(S.Half), Rational(-1, 2))]
  30. f_1 = x**2 + y + z - 1
  31. f_2 = x + y**2 + z - 1
  32. f_3 = x + y + z**2 - 1
  33. a, b = sqrt(2) - 1, -sqrt(2) - 1
  34. assert solve_poly_system([f_1, f_2, f_3], x, y, z) == \
  35. [(0, 0, 1), (0, 1, 0), (1, 0, 0), (a, a, a), (b, b, b)]
  36. solution = [(1, -1), (1, 1)]
  37. assert solve_poly_system([Poly(x**2 - y**2), Poly(x - 1)]) == solution
  38. assert solve_poly_system([x**2 - y**2, x - 1], x, y) == solution
  39. assert solve_poly_system([x**2 - y**2, x - 1]) == solution
  40. assert solve_poly_system(
  41. [x + x*y - 3, y + x*y - 4], x, y) == [(-3, -2), (1, 2)]
  42. raises(NotImplementedError, lambda: solve_poly_system([x**3 - y**3], x, y))
  43. raises(NotImplementedError, lambda: solve_poly_system(
  44. [z, -2*x*y**2 + x + y**2*z, y**2*(-z - 4) + 2]))
  45. raises(PolynomialError, lambda: solve_poly_system([1/x], x))
  46. raises(NotImplementedError, lambda: solve_poly_system(
  47. [x-1,], (x, y)))
  48. raises(NotImplementedError, lambda: solve_poly_system(
  49. [y-1,], (x, y)))
  50. # solve_poly_system should ideally construct solutions using
  51. # CRootOf for the following four tests
  52. assert solve_poly_system([x**5 - x + 1], [x], strict=False) == []
  53. raises(UnsolvableFactorError, lambda: solve_poly_system(
  54. [x**5 - x + 1], [x], strict=True))
  55. assert solve_poly_system([(x - 1)*(x**5 - x + 1), y**2 - 1], [x, y],
  56. strict=False) == [(1, -1), (1, 1)]
  57. raises(UnsolvableFactorError,
  58. lambda: solve_poly_system([(x - 1)*(x**5 - x + 1), y**2-1],
  59. [x, y], strict=True))
  60. def test_solve_generic():
  61. NewOption = Options((x, y), {'domain': 'ZZ'})
  62. assert solve_generic([x**2 - 2*y**2, y**2 - y + 1], NewOption) == \
  63. [(-sqrt(-1 - sqrt(3)*I), Rational(1, 2) - sqrt(3)*I/2),
  64. (sqrt(-1 - sqrt(3)*I), Rational(1, 2) - sqrt(3)*I/2),
  65. (-sqrt(-1 + sqrt(3)*I), Rational(1, 2) + sqrt(3)*I/2),
  66. (sqrt(-1 + sqrt(3)*I), Rational(1, 2) + sqrt(3)*I/2)]
  67. # solve_generic should ideally construct solutions using
  68. # CRootOf for the following two tests
  69. assert solve_generic(
  70. [2*x - y, (y - 1)*(y**5 - y + 1)], NewOption, strict=False) == \
  71. [(Rational(1, 2), 1)]
  72. raises(UnsolvableFactorError, lambda: solve_generic(
  73. [2*x - y, (y - 1)*(y**5 - y + 1)], NewOption, strict=True))
  74. def test_solve_biquadratic():
  75. x0, y0, x1, y1, r = symbols('x0 y0 x1 y1 r')
  76. f_1 = (x - 1)**2 + (y - 1)**2 - r**2
  77. f_2 = (x - 2)**2 + (y - 2)**2 - r**2
  78. s = sqrt(2*r**2 - 1)
  79. a = (3 - s)/2
  80. b = (3 + s)/2
  81. assert solve_poly_system([f_1, f_2], x, y) == [(a, b), (b, a)]
  82. f_1 = (x - 1)**2 + (y - 2)**2 - r**2
  83. f_2 = (x - 1)**2 + (y - 1)**2 - r**2
  84. assert solve_poly_system([f_1, f_2], x, y) == \
  85. [(1 - sqrt((2*r - 1)*(2*r + 1))/2, Rational(3, 2)),
  86. (1 + sqrt((2*r - 1)*(2*r + 1))/2, Rational(3, 2))]
  87. query = lambda expr: expr.is_Pow and expr.exp is S.Half
  88. f_1 = (x - 1 )**2 + (y - 2)**2 - r**2
  89. f_2 = (x - x1)**2 + (y - 1)**2 - r**2
  90. result = solve_poly_system([f_1, f_2], x, y)
  91. assert len(result) == 2 and all(len(r) == 2 for r in result)
  92. assert all(r.count(query) == 1 for r in flatten(result))
  93. f_1 = (x - x0)**2 + (y - y0)**2 - r**2
  94. f_2 = (x - x1)**2 + (y - y1)**2 - r**2
  95. result = solve_poly_system([f_1, f_2], x, y)
  96. assert len(result) == 2 and all(len(r) == 2 for r in result)
  97. assert all(len(r.find(query)) == 1 for r in flatten(result))
  98. s1 = (x*y - y, x**2 - x)
  99. assert solve(s1) == [{x: 1}, {x: 0, y: 0}]
  100. s2 = (x*y - x, y**2 - y)
  101. assert solve(s2) == [{y: 1}, {x: 0, y: 0}]
  102. gens = (x, y)
  103. for seq in (s1, s2):
  104. (f, g), opt = parallel_poly_from_expr(seq, *gens)
  105. raises(SolveFailed, lambda: solve_biquadratic(f, g, opt))
  106. seq = (x**2 + y**2 - 2, y**2 - 1)
  107. (f, g), opt = parallel_poly_from_expr(seq, *gens)
  108. assert solve_biquadratic(f, g, opt) == [
  109. (-1, -1), (-1, 1), (1, -1), (1, 1)]
  110. ans = [(0, -1), (0, 1)]
  111. seq = (x**2 + y**2 - 1, y**2 - 1)
  112. (f, g), opt = parallel_poly_from_expr(seq, *gens)
  113. assert solve_biquadratic(f, g, opt) == ans
  114. seq = (x**2 + y**2 - 1, x**2 - x + y**2 - 1)
  115. (f, g), opt = parallel_poly_from_expr(seq, *gens)
  116. assert solve_biquadratic(f, g, opt) == ans
  117. def test_solve_triangulated():
  118. f_1 = x**2 + y + z - 1
  119. f_2 = x + y**2 + z - 1
  120. f_3 = x + y + z**2 - 1
  121. a, b = sqrt(2) - 1, -sqrt(2) - 1
  122. assert solve_triangulated([f_1, f_2, f_3], x, y, z) == \
  123. [(0, 0, 1), (0, 1, 0), (1, 0, 0)]
  124. dom = QQ.algebraic_field(sqrt(2))
  125. assert solve_triangulated([f_1, f_2, f_3], x, y, z, domain=dom) == \
  126. [(0, 0, 1), (0, 1, 0), (1, 0, 0), (a, a, a), (b, b, b)]
  127. def test_solve_issue_3686():
  128. roots = solve_poly_system([((x - 5)**2/250000 + (y - Rational(5, 10))**2/250000) - 1, x], x, y)
  129. assert roots == [(0, S.Half - 15*sqrt(1111)), (0, S.Half + 15*sqrt(1111))]
  130. roots = solve_poly_system([((x - 5)**2/250000 + (y - 5.0/10)**2/250000) - 1, x], x, y)
  131. # TODO: does this really have to be so complicated?!
  132. assert len(roots) == 2
  133. assert roots[0][0] == 0
  134. assert roots[0][1].epsilon_eq(-499.474999374969, 1e12)
  135. assert roots[1][0] == 0
  136. assert roots[1][1].epsilon_eq(500.474999374969, 1e12)