test_entity.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from sympy.core.numbers import (Rational, pi)
  2. from sympy.core.singleton import S
  3. from sympy.core.symbol import Symbol
  4. from sympy.geometry import (Circle, Ellipse, Point, Line, Parabola,
  5. Polygon, Ray, RegularPolygon, Segment, Triangle, Plane, Curve)
  6. from sympy.geometry.entity import scale, GeometryEntity
  7. from sympy.testing.pytest import raises
  8. def test_entity():
  9. x = Symbol('x', real=True)
  10. y = Symbol('y', real=True)
  11. assert GeometryEntity(x, y) in GeometryEntity(x, y)
  12. raises(NotImplementedError, lambda: Point(0, 0) in GeometryEntity(x, y))
  13. assert GeometryEntity(x, y) == GeometryEntity(x, y)
  14. assert GeometryEntity(x, y).equals(GeometryEntity(x, y))
  15. c = Circle((0, 0), 5)
  16. assert GeometryEntity.encloses(c, Point(0, 0))
  17. assert GeometryEntity.encloses(c, Segment((0, 0), (1, 1)))
  18. assert GeometryEntity.encloses(c, Line((0, 0), (1, 1))) is False
  19. assert GeometryEntity.encloses(c, Circle((0, 0), 4))
  20. assert GeometryEntity.encloses(c, Polygon(Point(0, 0), Point(1, 0), Point(0, 1)))
  21. assert GeometryEntity.encloses(c, RegularPolygon(Point(8, 8), 1, 3)) is False
  22. def test_svg():
  23. a = Symbol('a')
  24. b = Symbol('b')
  25. d = Symbol('d')
  26. entity = Circle(Point(a, b), d)
  27. assert entity._repr_svg_() is None
  28. entity = Circle(Point(0, 0), S.Infinity)
  29. assert entity._repr_svg_() is None
  30. def test_subs():
  31. x = Symbol('x', real=True)
  32. y = Symbol('y', real=True)
  33. p = Point(x, 2)
  34. q = Point(1, 1)
  35. r = Point(3, 4)
  36. for o in [p,
  37. Segment(p, q),
  38. Ray(p, q),
  39. Line(p, q),
  40. Triangle(p, q, r),
  41. RegularPolygon(p, 3, 6),
  42. Polygon(p, q, r, Point(5, 4)),
  43. Circle(p, 3),
  44. Ellipse(p, 3, 4)]:
  45. assert 'y' in str(o.subs(x, y))
  46. assert p.subs({x: 1}) == Point(1, 2)
  47. assert Point(1, 2).subs(Point(1, 2), Point(3, 4)) == Point(3, 4)
  48. assert Point(1, 2).subs((1, 2), Point(3, 4)) == Point(3, 4)
  49. assert Point(1, 2).subs(Point(1, 2), Point(3, 4)) == Point(3, 4)
  50. assert Point(1, 2).subs({(1, 2)}) == Point(2, 2)
  51. raises(ValueError, lambda: Point(1, 2).subs(1))
  52. raises(ValueError, lambda: Point(1, 1).subs((Point(1, 1), Point(1,
  53. 2)), 1, 2))
  54. def test_transform():
  55. assert scale(1, 2, (3, 4)).tolist() == \
  56. [[1, 0, 0], [0, 2, 0], [0, -4, 1]]
  57. def test_reflect_entity_overrides():
  58. x = Symbol('x', real=True)
  59. y = Symbol('y', real=True)
  60. b = Symbol('b')
  61. m = Symbol('m')
  62. l = Line((0, b), slope=m)
  63. p = Point(x, y)
  64. r = p.reflect(l)
  65. c = Circle((x, y), 3)
  66. cr = c.reflect(l)
  67. assert cr == Circle(r, -3)
  68. assert c.area == -cr.area
  69. pent = RegularPolygon((1, 2), 1, 5)
  70. slope = S.ComplexInfinity
  71. while slope is S.ComplexInfinity:
  72. slope = Rational(*(x._random()/2).as_real_imag())
  73. l = Line(pent.vertices[1], slope=slope)
  74. rpent = pent.reflect(l)
  75. assert rpent.center == pent.center.reflect(l)
  76. rvert = [i.reflect(l) for i in pent.vertices]
  77. for v in rpent.vertices:
  78. for i in range(len(rvert)):
  79. ri = rvert[i]
  80. if ri.equals(v):
  81. rvert.remove(ri)
  82. break
  83. assert not rvert
  84. assert pent.area.equals(-rpent.area)
  85. def test_geometry_EvalfMixin():
  86. x = pi
  87. t = Symbol('t')
  88. for g in [
  89. Point(x, x),
  90. Plane(Point(0, x, 0), (0, 0, x)),
  91. Curve((x*t, x), (t, 0, x)),
  92. Ellipse((x, x), x, -x),
  93. Circle((x, x), x),
  94. Line((0, x), (x, 0)),
  95. Segment((0, x), (x, 0)),
  96. Ray((0, x), (x, 0)),
  97. Parabola((0, x), Line((-x, 0), (x, 0))),
  98. Polygon((0, 0), (0, x), (x, 0), (x, x)),
  99. RegularPolygon((0, x), x, 4, x),
  100. Triangle((0, 0), (x, 0), (x, x)),
  101. ]:
  102. assert str(g).replace('pi', '3.1') == str(g.n(2))