test_implicitregion.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from sympy.core.relational import Eq
  2. from sympy.core.singleton import S
  3. from sympy.abc import x, y, z, s, t
  4. from sympy.sets import FiniteSet, EmptySet
  5. from sympy.geometry import Point
  6. from sympy.vector import ImplicitRegion
  7. from sympy.testing.pytest import raises
  8. def test_ImplicitRegion():
  9. ellipse = ImplicitRegion((x, y), (x**2/4 + y**2/16 - 1))
  10. assert ellipse.equation == x**2/4 + y**2/16 - 1
  11. assert ellipse.variables == (x, y)
  12. assert ellipse.degree == 2
  13. r = ImplicitRegion((x, y, z), Eq(x**4 + y**2 - x*y, 6))
  14. assert r.equation == x**4 + y**2 - x*y - 6
  15. assert r.variables == (x, y, z)
  16. assert r.degree == 4
  17. def test_regular_point():
  18. r1 = ImplicitRegion((x,), x**2 - 16)
  19. assert r1.regular_point() == (-4,)
  20. c1 = ImplicitRegion((x, y), x**2 + y**2 - 4)
  21. assert c1.regular_point() == (0, -2)
  22. c2 = ImplicitRegion((x, y), (x - S(5)/2)**2 + y**2 - (S(1)/4)**2)
  23. assert c2.regular_point() == (S(5)/2, -S(1)/4)
  24. c3 = ImplicitRegion((x, y), (y - 5)**2 - 16*(x - 5))
  25. assert c3.regular_point() == (5, 5)
  26. r2 = ImplicitRegion((x, y), x**2 - 4*x*y - 3*y**2 + 4*x + 8*y - 5)
  27. assert r2.regular_point() == (S(4)/7, S(9)/7)
  28. r3 = ImplicitRegion((x, y), x**2 - 2*x*y + 3*y**2 - 2*x - 5*y + 3/2)
  29. raises(ValueError, lambda: r3.regular_point())
  30. def test_singular_points_and_multiplicty():
  31. r1 = ImplicitRegion((x, y, z), Eq(x + y + z, 0))
  32. assert r1.singular_points() == EmptySet
  33. r2 = ImplicitRegion((x, y, z), x*y*z + y**4 -x**2*z**2)
  34. assert r2.singular_points() == FiniteSet((0, 0, z), (x, 0, 0))
  35. assert r2.multiplicity((0, 0, 0)) == 3
  36. assert r2.multiplicity((0, 0, 6)) == 2
  37. r3 = ImplicitRegion((x, y, z), z**2 - x**2 - y**2)
  38. assert r3.singular_points() == FiniteSet((0, 0, 0))
  39. assert r3.multiplicity((0, 0, 0)) == 2
  40. r4 = ImplicitRegion((x, y), x**2 + y**2 - 2*x)
  41. assert r4.singular_points() == EmptySet
  42. assert r4.multiplicity(Point(1, 3)) == 0
  43. def test_rational_parametrization():
  44. p = ImplicitRegion((x,), x - 2)
  45. assert p.rational_parametrization() == (x - 2,)
  46. line = ImplicitRegion((x, y), Eq(y, 3*x + 2))
  47. assert line.rational_parametrization() == (x, 3*x + 2)
  48. circle1 = ImplicitRegion((x, y), (x-2)**2 + (y+3)**2 - 4)
  49. assert circle1.rational_parametrization(parameters=t) == (4*t/(t**2 + 1) + 2, 4*t**2/(t**2 + 1) - 5)
  50. circle2 = ImplicitRegion((x, y), (x - S.Half)**2 + y**2 - (S(1)/2)**2)
  51. assert circle2.rational_parametrization(parameters=t) == (t/(t**2 + 1) + S(1)/2, t**2/(t**2 + 1) - S(1)/2)
  52. circle3 = ImplicitRegion((x, y), Eq(x**2 + y**2, 2*x))
  53. assert circle3.rational_parametrization(parameters=(t,)) == (2*t/(t**2 + 1) + 1, 2*t**2/(t**2 + 1) - 1)
  54. parabola = ImplicitRegion((x, y), (y - 3)**2 - 4*(x + 6))
  55. assert parabola.rational_parametrization(t) == (-6 + 4/t**2, 3 + 4/t)
  56. rect_hyperbola = ImplicitRegion((x, y), x*y - 1)
  57. assert rect_hyperbola.rational_parametrization(t) == (-1 + (t + 1)/t, t)
  58. cubic_curve = ImplicitRegion((x, y), x**3 + x**2 - y**2)
  59. assert cubic_curve.rational_parametrization(parameters=(t)) == (t**2 - 1, t*(t**2 - 1))
  60. cuspidal = ImplicitRegion((x, y), (x**3 - y**2))
  61. assert cuspidal.rational_parametrization(t) == (t**2, t**3)
  62. I = ImplicitRegion((x, y), x**3 + x**2 - y**2)
  63. assert I.rational_parametrization(t) == (t**2 - 1, t*(t**2 - 1))
  64. sphere = ImplicitRegion((x, y, z), Eq(x**2 + y**2 + z**2, 2*x))
  65. assert sphere.rational_parametrization(parameters=(s, t)) == (2/(s**2 + t**2 + 1), 2*t/(s**2 + t**2 + 1), 2*s/(s**2 + t**2 + 1))
  66. conic = ImplicitRegion((x, y), Eq(x**2 + 4*x*y + 3*y**2 + x - y + 10, 0))
  67. assert conic.rational_parametrization(t) == (
  68. S(17)/2 + 4/(3*t**2 + 4*t + 1), 4*t/(3*t**2 + 4*t + 1) - S(11)/2)
  69. r1 = ImplicitRegion((x, y), y**2 - x**3 + x)
  70. raises(NotImplementedError, lambda: r1.rational_parametrization())
  71. r2 = ImplicitRegion((x, y), y**2 - x**3 - x**2 + 1)
  72. raises(NotImplementedError, lambda: r2.rational_parametrization())