test_curve.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from sympy.core.containers import Tuple
  2. from sympy.core.numbers import (Rational, pi)
  3. from sympy.core.singleton import S
  4. from sympy.core.symbol import (Symbol, symbols)
  5. from sympy.functions.elementary.hyperbolic import asinh
  6. from sympy.functions.elementary.miscellaneous import sqrt
  7. from sympy.geometry import Curve, Line, Point, Ellipse, Ray, Segment, Circle, Polygon, RegularPolygon
  8. from sympy.testing.pytest import raises, slow
  9. def test_curve():
  10. x = Symbol('x', real=True)
  11. s = Symbol('s')
  12. z = Symbol('z')
  13. # this curve is independent of the indicated parameter
  14. c = Curve([2*s, s**2], (z, 0, 2))
  15. assert c.parameter == z
  16. assert c.functions == (2*s, s**2)
  17. assert c.arbitrary_point() == Point(2*s, s**2)
  18. assert c.arbitrary_point(z) == Point(2*s, s**2)
  19. # this is how it is normally used
  20. c = Curve([2*s, s**2], (s, 0, 2))
  21. assert c.parameter == s
  22. assert c.functions == (2*s, s**2)
  23. t = Symbol('t')
  24. # the t returned as assumptions
  25. assert c.arbitrary_point() != Point(2*t, t**2)
  26. t = Symbol('t', real=True)
  27. # now t has the same assumptions so the test passes
  28. assert c.arbitrary_point() == Point(2*t, t**2)
  29. assert c.arbitrary_point(z) == Point(2*z, z**2)
  30. assert c.arbitrary_point(c.parameter) == Point(2*s, s**2)
  31. assert c.arbitrary_point(None) == Point(2*s, s**2)
  32. assert c.plot_interval() == [t, 0, 2]
  33. assert c.plot_interval(z) == [z, 0, 2]
  34. assert Curve([x, x], (x, 0, 1)).rotate(pi/2) == Curve([-x, x], (x, 0, 1))
  35. assert Curve([x, x], (x, 0, 1)).rotate(pi/2, (1, 2)).scale(2, 3).translate(
  36. 1, 3).arbitrary_point(s) == \
  37. Line((0, 0), (1, 1)).rotate(pi/2, (1, 2)).scale(2, 3).translate(
  38. 1, 3).arbitrary_point(s) == \
  39. Point(-2*s + 7, 3*s + 6)
  40. raises(ValueError, lambda: Curve((s), (s, 1, 2)))
  41. raises(ValueError, lambda: Curve((x, x * 2), (1, x)))
  42. raises(ValueError, lambda: Curve((s, s + t), (s, 1, 2)).arbitrary_point())
  43. raises(ValueError, lambda: Curve((s, s + t), (t, 1, 2)).arbitrary_point(s))
  44. @slow
  45. def test_free_symbols():
  46. a, b, c, d, e, f, s = symbols('a:f,s')
  47. assert Point(a, b).free_symbols == {a, b}
  48. assert Line((a, b), (c, d)).free_symbols == {a, b, c, d}
  49. assert Ray((a, b), (c, d)).free_symbols == {a, b, c, d}
  50. assert Ray((a, b), angle=c).free_symbols == {a, b, c}
  51. assert Segment((a, b), (c, d)).free_symbols == {a, b, c, d}
  52. assert Line((a, b), slope=c).free_symbols == {a, b, c}
  53. assert Curve((a*s, b*s), (s, c, d)).free_symbols == {a, b, c, d}
  54. assert Ellipse((a, b), c, d).free_symbols == {a, b, c, d}
  55. assert Ellipse((a, b), c, eccentricity=d).free_symbols == \
  56. {a, b, c, d}
  57. assert Ellipse((a, b), vradius=c, eccentricity=d).free_symbols == \
  58. {a, b, c, d}
  59. assert Circle((a, b), c).free_symbols == {a, b, c}
  60. assert Circle((a, b), (c, d), (e, f)).free_symbols == \
  61. {e, d, c, b, f, a}
  62. assert Polygon((a, b), (c, d), (e, f)).free_symbols == \
  63. {e, b, d, f, a, c}
  64. assert RegularPolygon((a, b), c, d, e).free_symbols == {e, a, b, c, d}
  65. def test_transform():
  66. x = Symbol('x', real=True)
  67. y = Symbol('y', real=True)
  68. c = Curve((x, x**2), (x, 0, 1))
  69. cout = Curve((2*x - 4, 3*x**2 - 10), (x, 0, 1))
  70. pts = [Point(0, 0), Point(S.Half, Rational(1, 4)), Point(1, 1)]
  71. pts_out = [Point(-4, -10), Point(-3, Rational(-37, 4)), Point(-2, -7)]
  72. assert c.scale(2, 3, (4, 5)) == cout
  73. assert [c.subs(x, xi/2) for xi in Tuple(0, 1, 2)] == pts
  74. assert [cout.subs(x, xi/2) for xi in Tuple(0, 1, 2)] == pts_out
  75. assert Curve((x + y, 3*x), (x, 0, 1)).subs(y, S.Half) == \
  76. Curve((x + S.Half, 3*x), (x, 0, 1))
  77. assert Curve((x, 3*x), (x, 0, 1)).translate(4, 5) == \
  78. Curve((x + 4, 3*x + 5), (x, 0, 1))
  79. def test_length():
  80. t = Symbol('t', real=True)
  81. c1 = Curve((t, 0), (t, 0, 1))
  82. assert c1.length == 1
  83. c2 = Curve((t, t), (t, 0, 1))
  84. assert c2.length == sqrt(2)
  85. c3 = Curve((t ** 2, t), (t, 2, 5))
  86. assert c3.length == -sqrt(17) - asinh(4) / 4 + asinh(10) / 4 + 5 * sqrt(101) / 2
  87. def test_parameter_value():
  88. t = Symbol('t')
  89. C = Curve([2*t, t**2], (t, 0, 2))
  90. assert C.parameter_value((2, 1), t) == {t: 1}
  91. raises(ValueError, lambda: C.parameter_value((2, 0), t))
  92. def test_issue_17997():
  93. t, s = symbols('t s')
  94. c = Curve((t, t**2), (t, 0, 10))
  95. p = Curve([2*s, s**2], (s, 0, 2))
  96. assert c(2) == Point(2, 4)
  97. assert p(1) == Point(2, 1)