test_util.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. from sympy.core.function import (Derivative, Function)
  2. from sympy.core.singleton import S
  3. from sympy.core.symbol import Symbol
  4. from sympy.functions import exp, cos, sin, tan, cosh, sinh
  5. from sympy.functions.elementary.miscellaneous import sqrt
  6. from sympy.geometry import Point, Point2D, Line, Polygon, Segment, convex_hull,\
  7. intersection, centroid, Point3D, Line3D
  8. from sympy.geometry.util import idiff, closest_points, farthest_points, _ordered_points, are_coplanar
  9. from sympy.solvers.solvers import solve
  10. from sympy.testing.pytest import raises
  11. def test_idiff():
  12. x = Symbol('x', real=True)
  13. y = Symbol('y', real=True)
  14. t = Symbol('t', real=True)
  15. f = Function('f')
  16. g = Function('g')
  17. # the use of idiff in ellipse also provides coverage
  18. circ = x**2 + y**2 - 4
  19. ans = -3*x*(x**2/y**2 + 1)/y**3
  20. assert ans == idiff(circ, y, x, 3), idiff(circ, y, x, 3)
  21. assert ans == idiff(circ, [y], x, 3)
  22. assert idiff(circ, y, x, 3) == ans
  23. explicit = 12*x/sqrt(-x**2 + 4)**5
  24. assert ans.subs(y, solve(circ, y)[0]).equals(explicit)
  25. assert True in [sol.diff(x, 3).equals(explicit) for sol in solve(circ, y)]
  26. assert idiff(x + t + y, [y, t], x) == -Derivative(t, x) - 1
  27. assert idiff(f(x) * exp(f(x)) - x * exp(x), f(x), x) == (x + 1)*exp(x)*exp(-f(x))/(f(x) + 1)
  28. assert idiff(f(x) - y * exp(x), [f(x), y], x) == (y + Derivative(y, x))*exp(x)
  29. assert idiff(f(x) - y * exp(x), [y, f(x)], x) == -y + Derivative(f(x), x)*exp(-x)
  30. assert idiff(f(x) - g(x), [f(x), g(x)], x) == Derivative(g(x), x)
  31. # this should be fast
  32. fxy = y - (-10*(-sin(x) + 1/x)**2 + tan(x)**2 + 2*cosh(x/10))
  33. assert idiff(fxy, y, x) == -20*sin(x)*cos(x) + 2*tan(x)**3 + \
  34. 2*tan(x) + sinh(x/10)/5 + 20*cos(x)/x - 20*sin(x)/x**2 + 20/x**3
  35. def test_intersection():
  36. assert intersection(Point(0, 0)) == []
  37. raises(TypeError, lambda: intersection(Point(0, 0), 3))
  38. assert intersection(
  39. Segment((0, 0), (2, 0)),
  40. Segment((-1, 0), (1, 0)),
  41. Line((0, 0), (0, 1)), pairwise=True) == [
  42. Point(0, 0), Segment((0, 0), (1, 0))]
  43. assert intersection(
  44. Line((0, 0), (0, 1)),
  45. Segment((0, 0), (2, 0)),
  46. Segment((-1, 0), (1, 0)), pairwise=True) == [
  47. Point(0, 0), Segment((0, 0), (1, 0))]
  48. assert intersection(
  49. Line((0, 0), (0, 1)),
  50. Segment((0, 0), (2, 0)),
  51. Segment((-1, 0), (1, 0)),
  52. Line((0, 0), slope=1), pairwise=True) == [
  53. Point(0, 0), Segment((0, 0), (1, 0))]
  54. def test_convex_hull():
  55. raises(TypeError, lambda: convex_hull(Point(0, 0), 3))
  56. points = [(1, -1), (1, -2), (3, -1), (-5, -2), (15, -4)]
  57. assert convex_hull(*points, **{"polygon": False}) == (
  58. [Point2D(-5, -2), Point2D(1, -1), Point2D(3, -1), Point2D(15, -4)],
  59. [Point2D(-5, -2), Point2D(15, -4)])
  60. def test_centroid():
  61. p = Polygon((0, 0), (10, 0), (10, 10))
  62. q = p.translate(0, 20)
  63. assert centroid(p, q) == Point(20, 40)/3
  64. p = Segment((0, 0), (2, 0))
  65. q = Segment((0, 0), (2, 2))
  66. assert centroid(p, q) == Point(1, -sqrt(2) + 2)
  67. assert centroid(Point(0, 0), Point(2, 0)) == Point(2, 0)/2
  68. assert centroid(Point(0, 0), Point(0, 0), Point(2, 0)) == Point(2, 0)/3
  69. def test_farthest_points_closest_points():
  70. from sympy.core.random import randint
  71. from sympy.utilities.iterables import subsets
  72. for how in (min, max):
  73. if how == min:
  74. func = closest_points
  75. else:
  76. func = farthest_points
  77. raises(ValueError, lambda: func(Point2D(0, 0), Point2D(0, 0)))
  78. # 3rd pt dx is close and pt is closer to 1st pt
  79. p1 = [Point2D(0, 0), Point2D(3, 0), Point2D(1, 1)]
  80. # 3rd pt dx is close and pt is closer to 2nd pt
  81. p2 = [Point2D(0, 0), Point2D(3, 0), Point2D(2, 1)]
  82. # 3rd pt dx is close and but pt is not closer
  83. p3 = [Point2D(0, 0), Point2D(3, 0), Point2D(1, 10)]
  84. # 3rd pt dx is not closer and it's closer to 2nd pt
  85. p4 = [Point2D(0, 0), Point2D(3, 0), Point2D(4, 0)]
  86. # 3rd pt dx is not closer and it's closer to 1st pt
  87. p5 = [Point2D(0, 0), Point2D(3, 0), Point2D(-1, 0)]
  88. # duplicate point doesn't affect outcome
  89. dup = [Point2D(0, 0), Point2D(3, 0), Point2D(3, 0), Point2D(-1, 0)]
  90. # symbolic
  91. x = Symbol('x', positive=True)
  92. s = [Point2D(a) for a in ((x, 1), (x + 3, 2), (x + 2, 2))]
  93. for points in (p1, p2, p3, p4, p5, dup, s):
  94. d = how(i.distance(j) for i, j in subsets(set(points), 2))
  95. ans = a, b = list(func(*points))[0]
  96. assert a.distance(b) == d
  97. assert ans == _ordered_points(ans)
  98. # if the following ever fails, the above tests were not sufficient
  99. # and the logical error in the routine should be fixed
  100. points = set()
  101. while len(points) != 7:
  102. points.add(Point2D(randint(1, 100), randint(1, 100)))
  103. points = list(points)
  104. d = how(i.distance(j) for i, j in subsets(points, 2))
  105. ans = a, b = list(func(*points))[0]
  106. assert a.distance(b) == d
  107. assert ans == _ordered_points(ans)
  108. # equidistant points
  109. a, b, c = (
  110. Point2D(0, 0), Point2D(1, 0), Point2D(S.Half, sqrt(3)/2))
  111. ans = {_ordered_points((i, j))
  112. for i, j in subsets((a, b, c), 2)}
  113. assert closest_points(b, c, a) == ans
  114. assert farthest_points(b, c, a) == ans
  115. # unique to farthest
  116. points = [(1, 1), (1, 2), (3, 1), (-5, 2), (15, 4)]
  117. assert farthest_points(*points) == {
  118. (Point2D(-5, 2), Point2D(15, 4))}
  119. points = [(1, -1), (1, -2), (3, -1), (-5, -2), (15, -4)]
  120. assert farthest_points(*points) == {
  121. (Point2D(-5, -2), Point2D(15, -4))}
  122. assert farthest_points((1, 1), (0, 0)) == {
  123. (Point2D(0, 0), Point2D(1, 1))}
  124. raises(ValueError, lambda: farthest_points((1, 1)))
  125. def test_are_coplanar():
  126. a = Line3D(Point3D(5, 0, 0), Point3D(1, -1, 1))
  127. b = Line3D(Point3D(0, -2, 0), Point3D(3, 1, 1))
  128. c = Line3D(Point3D(0, -1, 0), Point3D(5, -1, 9))
  129. d = Line(Point2D(0, 3), Point2D(1, 5))
  130. assert are_coplanar(a, b, c) == False
  131. assert are_coplanar(a, d) == False