图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
5.2 KiB

  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.polytools import Poly
  8. from sympy.solvers.solvers import solve
  9. from sympy.utilities.iterables import flatten
  10. from sympy.abc import x, y, z
  11. from sympy.polys import PolynomialError
  12. from sympy.solvers.polysys import (solve_poly_system,
  13. solve_triangulated, solve_biquadratic, SolveFailed)
  14. from sympy.polys.polytools import parallel_poly_from_expr
  15. from sympy.testing.pytest import raises
  16. def test_solve_poly_system():
  17. assert solve_poly_system([x - 1], x) == [(S.One,)]
  18. assert solve_poly_system([y - x, y - x - 1], x, y) is None
  19. assert solve_poly_system([y - x**2, y + x**2], x, y) == [(S.Zero, S.Zero)]
  20. assert solve_poly_system([2*x - 3, y*Rational(3, 2) - 2*x, z - 5*y], x, y, z) == \
  21. [(Rational(3, 2), Integer(2), Integer(10))]
  22. assert solve_poly_system([x*y - 2*y, 2*y**2 - x**2], x, y) == \
  23. [(0, 0), (2, -sqrt(2)), (2, sqrt(2))]
  24. assert solve_poly_system([y - x**2, y + x**2 + 1], x, y) == \
  25. [(-I*sqrt(S.Half), Rational(-1, 2)), (I*sqrt(S.Half), Rational(-1, 2))]
  26. f_1 = x**2 + y + z - 1
  27. f_2 = x + y**2 + z - 1
  28. f_3 = x + y + z**2 - 1
  29. a, b = sqrt(2) - 1, -sqrt(2) - 1
  30. assert solve_poly_system([f_1, f_2, f_3], x, y, z) == \
  31. [(0, 0, 1), (0, 1, 0), (1, 0, 0), (a, a, a), (b, b, b)]
  32. solution = [(1, -1), (1, 1)]
  33. assert solve_poly_system([Poly(x**2 - y**2), Poly(x - 1)]) == solution
  34. assert solve_poly_system([x**2 - y**2, x - 1], x, y) == solution
  35. assert solve_poly_system([x**2 - y**2, x - 1]) == solution
  36. assert solve_poly_system(
  37. [x + x*y - 3, y + x*y - 4], x, y) == [(-3, -2), (1, 2)]
  38. raises(NotImplementedError, lambda: solve_poly_system([x**3 - y**3], x, y))
  39. raises(NotImplementedError, lambda: solve_poly_system(
  40. [z, -2*x*y**2 + x + y**2*z, y**2*(-z - 4) + 2]))
  41. raises(PolynomialError, lambda: solve_poly_system([1/x], x))
  42. raises(NotImplementedError, lambda: solve_poly_system(
  43. [x-1,], (x, y)))
  44. raises(NotImplementedError, lambda: solve_poly_system(
  45. [y-1,], (x, y)))
  46. def test_solve_biquadratic():
  47. x0, y0, x1, y1, r = symbols('x0 y0 x1 y1 r')
  48. f_1 = (x - 1)**2 + (y - 1)**2 - r**2
  49. f_2 = (x - 2)**2 + (y - 2)**2 - r**2
  50. s = sqrt(2*r**2 - 1)
  51. a = (3 - s)/2
  52. b = (3 + s)/2
  53. assert solve_poly_system([f_1, f_2], x, y) == [(a, b), (b, a)]
  54. f_1 = (x - 1)**2 + (y - 2)**2 - r**2
  55. f_2 = (x - 1)**2 + (y - 1)**2 - r**2
  56. assert solve_poly_system([f_1, f_2], x, y) == \
  57. [(1 - sqrt((2*r - 1)*(2*r + 1))/2, Rational(3, 2)),
  58. (1 + sqrt((2*r - 1)*(2*r + 1))/2, Rational(3, 2))]
  59. query = lambda expr: expr.is_Pow and expr.exp is S.Half
  60. f_1 = (x - 1 )**2 + (y - 2)**2 - r**2
  61. f_2 = (x - x1)**2 + (y - 1)**2 - r**2
  62. result = solve_poly_system([f_1, f_2], x, y)
  63. assert len(result) == 2 and all(len(r) == 2 for r in result)
  64. assert all(r.count(query) == 1 for r in flatten(result))
  65. f_1 = (x - x0)**2 + (y - y0)**2 - r**2
  66. f_2 = (x - x1)**2 + (y - y1)**2 - r**2
  67. result = solve_poly_system([f_1, f_2], x, y)
  68. assert len(result) == 2 and all(len(r) == 2 for r in result)
  69. assert all(len(r.find(query)) == 1 for r in flatten(result))
  70. s1 = (x*y - y, x**2 - x)
  71. assert solve(s1) == [{x: 1}, {x: 0, y: 0}]
  72. s2 = (x*y - x, y**2 - y)
  73. assert solve(s2) == [{y: 1}, {x: 0, y: 0}]
  74. gens = (x, y)
  75. for seq in (s1, s2):
  76. (f, g), opt = parallel_poly_from_expr(seq, *gens)
  77. raises(SolveFailed, lambda: solve_biquadratic(f, g, opt))
  78. seq = (x**2 + y**2 - 2, y**2 - 1)
  79. (f, g), opt = parallel_poly_from_expr(seq, *gens)
  80. assert solve_biquadratic(f, g, opt) == [
  81. (-1, -1), (-1, 1), (1, -1), (1, 1)]
  82. ans = [(0, -1), (0, 1)]
  83. seq = (x**2 + y**2 - 1, y**2 - 1)
  84. (f, g), opt = parallel_poly_from_expr(seq, *gens)
  85. assert solve_biquadratic(f, g, opt) == ans
  86. seq = (x**2 + y**2 - 1, x**2 - x + y**2 - 1)
  87. (f, g), opt = parallel_poly_from_expr(seq, *gens)
  88. assert solve_biquadratic(f, g, opt) == ans
  89. def test_solve_triangulated():
  90. f_1 = x**2 + y + z - 1
  91. f_2 = x + y**2 + z - 1
  92. f_3 = x + y + z**2 - 1
  93. a, b = sqrt(2) - 1, -sqrt(2) - 1
  94. assert solve_triangulated([f_1, f_2, f_3], x, y, z) == \
  95. [(0, 0, 1), (0, 1, 0), (1, 0, 0)]
  96. dom = QQ.algebraic_field(sqrt(2))
  97. assert solve_triangulated([f_1, f_2, f_3], x, y, z, domain=dom) == \
  98. [(0, 0, 1), (0, 1, 0), (1, 0, 0), (a, a, a), (b, b, b)]
  99. def test_solve_issue_3686():
  100. roots = solve_poly_system([((x - 5)**2/250000 + (y - Rational(5, 10))**2/250000) - 1, x], x, y)
  101. assert roots == [(0, S.Half - 15*sqrt(1111)), (0, S.Half + 15*sqrt(1111))]
  102. roots = solve_poly_system([((x - 5)**2/250000 + (y - 5.0/10)**2/250000) - 1, x], x, y)
  103. # TODO: does this really have to be so complicated?!
  104. assert len(roots) == 2
  105. assert roots[0][0] == 0
  106. assert roots[0][1].epsilon_eq(-499.474999374969, 1e12)
  107. assert roots[1][0] == 0
  108. assert roots[1][1].epsilon_eq(500.474999374969, 1e12)