test_point.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. from sympy.core.basic import Basic
  2. from sympy.core.numbers import (I, Rational, pi)
  3. from sympy.core.parameters import evaluate
  4. from sympy.core.singleton import S
  5. from sympy.core.symbol import Symbol
  6. from sympy.core.sympify import sympify
  7. from sympy.functions.elementary.miscellaneous import sqrt
  8. from sympy.geometry import Line, Point, Point2D, Point3D, Line3D, Plane
  9. from sympy.geometry.entity import rotate, scale, translate, GeometryEntity
  10. from sympy.matrices import Matrix
  11. from sympy.utilities.iterables import subsets, permutations, cartes
  12. from sympy.utilities.misc import Undecidable
  13. from sympy.testing.pytest import raises, warns
  14. def test_point():
  15. x = Symbol('x', real=True)
  16. y = Symbol('y', real=True)
  17. x1 = Symbol('x1', real=True)
  18. x2 = Symbol('x2', real=True)
  19. y1 = Symbol('y1', real=True)
  20. y2 = Symbol('y2', real=True)
  21. half = S.Half
  22. p1 = Point(x1, x2)
  23. p2 = Point(y1, y2)
  24. p3 = Point(0, 0)
  25. p4 = Point(1, 1)
  26. p5 = Point(0, 1)
  27. line = Line(Point(1, 0), slope=1)
  28. assert p1 in p1
  29. assert p1 not in p2
  30. assert p2.y == y2
  31. assert (p3 + p4) == p4
  32. assert (p2 - p1) == Point(y1 - x1, y2 - x2)
  33. assert -p2 == Point(-y1, -y2)
  34. raises(TypeError, lambda: Point(1))
  35. raises(ValueError, lambda: Point([1]))
  36. raises(ValueError, lambda: Point(3, I))
  37. raises(ValueError, lambda: Point(2*I, I))
  38. raises(ValueError, lambda: Point(3 + I, I))
  39. assert Point(34.05, sqrt(3)) == Point(Rational(681, 20), sqrt(3))
  40. assert Point.midpoint(p3, p4) == Point(half, half)
  41. assert Point.midpoint(p1, p4) == Point(half + half*x1, half + half*x2)
  42. assert Point.midpoint(p2, p2) == p2
  43. assert p2.midpoint(p2) == p2
  44. assert p1.origin == Point(0, 0)
  45. assert Point.distance(p3, p4) == sqrt(2)
  46. assert Point.distance(p1, p1) == 0
  47. assert Point.distance(p3, p2) == sqrt(p2.x**2 + p2.y**2)
  48. raises(TypeError, lambda: Point.distance(p1, 0))
  49. raises(TypeError, lambda: Point.distance(p1, GeometryEntity()))
  50. # distance should be symmetric
  51. assert p1.distance(line) == line.distance(p1)
  52. assert p4.distance(line) == line.distance(p4)
  53. assert Point.taxicab_distance(p4, p3) == 2
  54. assert Point.canberra_distance(p4, p5) == 1
  55. raises(ValueError, lambda: Point.canberra_distance(p3, p3))
  56. p1_1 = Point(x1, x1)
  57. p1_2 = Point(y2, y2)
  58. p1_3 = Point(x1 + 1, x1)
  59. assert Point.is_collinear(p3)
  60. with warns(UserWarning, test_stacklevel=False):
  61. assert Point.is_collinear(p3, Point(p3, dim=4))
  62. assert p3.is_collinear()
  63. assert Point.is_collinear(p3, p4)
  64. assert Point.is_collinear(p3, p4, p1_1, p1_2)
  65. assert Point.is_collinear(p3, p4, p1_1, p1_3) is False
  66. assert Point.is_collinear(p3, p3, p4, p5) is False
  67. raises(TypeError, lambda: Point.is_collinear(line))
  68. raises(TypeError, lambda: p1_1.is_collinear(line))
  69. assert p3.intersection(Point(0, 0)) == [p3]
  70. assert p3.intersection(p4) == []
  71. assert p3.intersection(line) == []
  72. with warns(UserWarning, test_stacklevel=False):
  73. assert Point.intersection(Point(0, 0, 0), Point(0, 0)) == [Point(0, 0, 0)]
  74. x_pos = Symbol('x', positive=True)
  75. p2_1 = Point(x_pos, 0)
  76. p2_2 = Point(0, x_pos)
  77. p2_3 = Point(-x_pos, 0)
  78. p2_4 = Point(0, -x_pos)
  79. p2_5 = Point(x_pos, 5)
  80. assert Point.is_concyclic(p2_1)
  81. assert Point.is_concyclic(p2_1, p2_2)
  82. assert Point.is_concyclic(p2_1, p2_2, p2_3, p2_4)
  83. for pts in permutations((p2_1, p2_2, p2_3, p2_5)):
  84. assert Point.is_concyclic(*pts) is False
  85. assert Point.is_concyclic(p4, p4 * 2, p4 * 3) is False
  86. assert Point(0, 0).is_concyclic((1, 1), (2, 2), (2, 1)) is False
  87. assert Point.is_concyclic(Point(0, 0, 0, 0), Point(1, 0, 0, 0), Point(1, 1, 0, 0), Point(1, 1, 1, 0)) is False
  88. assert p1.is_scalar_multiple(p1)
  89. assert p1.is_scalar_multiple(2*p1)
  90. assert not p1.is_scalar_multiple(p2)
  91. assert Point.is_scalar_multiple(Point(1, 1), (-1, -1))
  92. assert Point.is_scalar_multiple(Point(0, 0), (0, -1))
  93. # test when is_scalar_multiple can't be determined
  94. raises(Undecidable, lambda: Point.is_scalar_multiple(Point(sympify("x1%y1"), sympify("x2%y2")), Point(0, 1)))
  95. assert Point(0, 1).orthogonal_direction == Point(1, 0)
  96. assert Point(1, 0).orthogonal_direction == Point(0, 1)
  97. assert p1.is_zero is None
  98. assert p3.is_zero
  99. assert p4.is_zero is False
  100. assert p1.is_nonzero is None
  101. assert p3.is_nonzero is False
  102. assert p4.is_nonzero
  103. assert p4.scale(2, 3) == Point(2, 3)
  104. assert p3.scale(2, 3) == p3
  105. assert p4.rotate(pi, Point(0.5, 0.5)) == p3
  106. assert p1.__radd__(p2) == p1.midpoint(p2).scale(2, 2)
  107. assert (-p3).__rsub__(p4) == p3.midpoint(p4).scale(2, 2)
  108. assert p4 * 5 == Point(5, 5)
  109. assert p4 / 5 == Point(0.2, 0.2)
  110. assert 5 * p4 == Point(5, 5)
  111. raises(ValueError, lambda: Point(0, 0) + 10)
  112. # Point differences should be simplified
  113. assert Point(x*(x - 1), y) - Point(x**2 - x, y + 1) == Point(0, -1)
  114. a, b = S.Half, Rational(1, 3)
  115. assert Point(a, b).evalf(2) == \
  116. Point(a.n(2), b.n(2), evaluate=False)
  117. raises(ValueError, lambda: Point(1, 2) + 1)
  118. # test project
  119. assert Point.project((0, 1), (1, 0)) == Point(0, 0)
  120. assert Point.project((1, 1), (1, 0)) == Point(1, 0)
  121. raises(ValueError, lambda: Point.project(p1, Point(0, 0)))
  122. # test transformations
  123. p = Point(1, 0)
  124. assert p.rotate(pi/2) == Point(0, 1)
  125. assert p.rotate(pi/2, p) == p
  126. p = Point(1, 1)
  127. assert p.scale(2, 3) == Point(2, 3)
  128. assert p.translate(1, 2) == Point(2, 3)
  129. assert p.translate(1) == Point(2, 1)
  130. assert p.translate(y=1) == Point(1, 2)
  131. assert p.translate(*p.args) == Point(2, 2)
  132. # Check invalid input for transform
  133. raises(ValueError, lambda: p3.transform(p3))
  134. raises(ValueError, lambda: p.transform(Matrix([[1, 0], [0, 1]])))
  135. # test __contains__
  136. assert 0 in Point(0, 0, 0, 0)
  137. assert 1 not in Point(0, 0, 0, 0)
  138. # test affine_rank
  139. assert Point.affine_rank() == -1
  140. def test_point3D():
  141. x = Symbol('x', real=True)
  142. y = Symbol('y', real=True)
  143. x1 = Symbol('x1', real=True)
  144. x2 = Symbol('x2', real=True)
  145. x3 = Symbol('x3', real=True)
  146. y1 = Symbol('y1', real=True)
  147. y2 = Symbol('y2', real=True)
  148. y3 = Symbol('y3', real=True)
  149. half = S.Half
  150. p1 = Point3D(x1, x2, x3)
  151. p2 = Point3D(y1, y2, y3)
  152. p3 = Point3D(0, 0, 0)
  153. p4 = Point3D(1, 1, 1)
  154. p5 = Point3D(0, 1, 2)
  155. assert p1 in p1
  156. assert p1 not in p2
  157. assert p2.y == y2
  158. assert (p3 + p4) == p4
  159. assert (p2 - p1) == Point3D(y1 - x1, y2 - x2, y3 - x3)
  160. assert -p2 == Point3D(-y1, -y2, -y3)
  161. assert Point(34.05, sqrt(3)) == Point(Rational(681, 20), sqrt(3))
  162. assert Point3D.midpoint(p3, p4) == Point3D(half, half, half)
  163. assert Point3D.midpoint(p1, p4) == Point3D(half + half*x1, half + half*x2,
  164. half + half*x3)
  165. assert Point3D.midpoint(p2, p2) == p2
  166. assert p2.midpoint(p2) == p2
  167. assert Point3D.distance(p3, p4) == sqrt(3)
  168. assert Point3D.distance(p1, p1) == 0
  169. assert Point3D.distance(p3, p2) == sqrt(p2.x**2 + p2.y**2 + p2.z**2)
  170. p1_1 = Point3D(x1, x1, x1)
  171. p1_2 = Point3D(y2, y2, y2)
  172. p1_3 = Point3D(x1 + 1, x1, x1)
  173. Point3D.are_collinear(p3)
  174. assert Point3D.are_collinear(p3, p4)
  175. assert Point3D.are_collinear(p3, p4, p1_1, p1_2)
  176. assert Point3D.are_collinear(p3, p4, p1_1, p1_3) is False
  177. assert Point3D.are_collinear(p3, p3, p4, p5) is False
  178. assert p3.intersection(Point3D(0, 0, 0)) == [p3]
  179. assert p3.intersection(p4) == []
  180. assert p4 * 5 == Point3D(5, 5, 5)
  181. assert p4 / 5 == Point3D(0.2, 0.2, 0.2)
  182. assert 5 * p4 == Point3D(5, 5, 5)
  183. raises(ValueError, lambda: Point3D(0, 0, 0) + 10)
  184. # Test coordinate properties
  185. assert p1.coordinates == (x1, x2, x3)
  186. assert p2.coordinates == (y1, y2, y3)
  187. assert p3.coordinates == (0, 0, 0)
  188. assert p4.coordinates == (1, 1, 1)
  189. assert p5.coordinates == (0, 1, 2)
  190. assert p5.x == 0
  191. assert p5.y == 1
  192. assert p5.z == 2
  193. # Point differences should be simplified
  194. assert Point3D(x*(x - 1), y, 2) - Point3D(x**2 - x, y + 1, 1) == \
  195. Point3D(0, -1, 1)
  196. a, b, c = S.Half, Rational(1, 3), Rational(1, 4)
  197. assert Point3D(a, b, c).evalf(2) == \
  198. Point(a.n(2), b.n(2), c.n(2), evaluate=False)
  199. raises(ValueError, lambda: Point3D(1, 2, 3) + 1)
  200. # test transformations
  201. p = Point3D(1, 1, 1)
  202. assert p.scale(2, 3) == Point3D(2, 3, 1)
  203. assert p.translate(1, 2) == Point3D(2, 3, 1)
  204. assert p.translate(1) == Point3D(2, 1, 1)
  205. assert p.translate(z=1) == Point3D(1, 1, 2)
  206. assert p.translate(*p.args) == Point3D(2, 2, 2)
  207. # Test __new__
  208. assert Point3D(0.1, 0.2, evaluate=False, on_morph='ignore').args[0].is_Float
  209. # Test length property returns correctly
  210. assert p.length == 0
  211. assert p1_1.length == 0
  212. assert p1_2.length == 0
  213. # Test are_colinear type error
  214. raises(TypeError, lambda: Point3D.are_collinear(p, x))
  215. # Test are_coplanar
  216. assert Point.are_coplanar()
  217. assert Point.are_coplanar((1, 2, 0), (1, 2, 0), (1, 3, 0))
  218. assert Point.are_coplanar((1, 2, 0), (1, 2, 3))
  219. with warns(UserWarning, test_stacklevel=False):
  220. raises(ValueError, lambda: Point2D.are_coplanar((1, 2), (1, 2, 3)))
  221. assert Point3D.are_coplanar((1, 2, 0), (1, 2, 3))
  222. assert Point.are_coplanar((0, 0, 0), (1, 1, 0), (1, 1, 1), (1, 2, 1)) is False
  223. planar2 = Point3D(1, -1, 1)
  224. planar3 = Point3D(-1, 1, 1)
  225. assert Point3D.are_coplanar(p, planar2, planar3) == True
  226. assert Point3D.are_coplanar(p, planar2, planar3, p3) == False
  227. assert Point.are_coplanar(p, planar2)
  228. planar2 = Point3D(1, 1, 2)
  229. planar3 = Point3D(1, 1, 3)
  230. assert Point3D.are_coplanar(p, planar2, planar3) # line, not plane
  231. plane = Plane((1, 2, 1), (2, 1, 0), (3, 1, 2))
  232. assert Point.are_coplanar(*[plane.projection(((-1)**i, i)) for i in range(4)])
  233. # all 2D points are coplanar
  234. assert Point.are_coplanar(Point(x, y), Point(x, x + y), Point(y, x + 2)) is True
  235. # Test Intersection
  236. assert planar2.intersection(Line3D(p, planar3)) == [Point3D(1, 1, 2)]
  237. # Test Scale
  238. assert planar2.scale(1, 1, 1) == planar2
  239. assert planar2.scale(2, 2, 2, planar3) == Point3D(1, 1, 1)
  240. assert planar2.scale(1, 1, 1, p3) == planar2
  241. # Test Transform
  242. identity = Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
  243. assert p.transform(identity) == p
  244. trans = Matrix([[1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1], [0, 0, 0, 1]])
  245. assert p.transform(trans) == Point3D(2, 2, 2)
  246. raises(ValueError, lambda: p.transform(p))
  247. raises(ValueError, lambda: p.transform(Matrix([[1, 0], [0, 1]])))
  248. # Test Equals
  249. assert p.equals(x1) == False
  250. # Test __sub__
  251. p_4d = Point(0, 0, 0, 1)
  252. with warns(UserWarning, test_stacklevel=False):
  253. assert p - p_4d == Point(1, 1, 1, -1)
  254. p_4d3d = Point(0, 0, 1, 0)
  255. with warns(UserWarning, test_stacklevel=False):
  256. assert p - p_4d3d == Point(1, 1, 0, 0)
  257. def test_Point2D():
  258. # Test Distance
  259. p1 = Point2D(1, 5)
  260. p2 = Point2D(4, 2.5)
  261. p3 = (6, 3)
  262. assert p1.distance(p2) == sqrt(61)/2
  263. assert p2.distance(p3) == sqrt(17)/2
  264. # Test coordinates
  265. assert p1.x == 1
  266. assert p1.y == 5
  267. assert p2.x == 4
  268. assert p2.y == S(5)/2
  269. assert p1.coordinates == (1, 5)
  270. assert p2.coordinates == (4, S(5)/2)
  271. # test bounds
  272. assert p1.bounds == (1, 5, 1, 5)
  273. def test_issue_9214():
  274. p1 = Point3D(4, -2, 6)
  275. p2 = Point3D(1, 2, 3)
  276. p3 = Point3D(7, 2, 3)
  277. assert Point3D.are_collinear(p1, p2, p3) is False
  278. def test_issue_11617():
  279. p1 = Point3D(1,0,2)
  280. p2 = Point2D(2,0)
  281. with warns(UserWarning, test_stacklevel=False):
  282. assert p1.distance(p2) == sqrt(5)
  283. def test_transform():
  284. p = Point(1, 1)
  285. assert p.transform(rotate(pi/2)) == Point(-1, 1)
  286. assert p.transform(scale(3, 2)) == Point(3, 2)
  287. assert p.transform(translate(1, 2)) == Point(2, 3)
  288. assert Point(1, 1).scale(2, 3, (4, 5)) == \
  289. Point(-2, -7)
  290. assert Point(1, 1).translate(4, 5) == \
  291. Point(5, 6)
  292. def test_concyclic_doctest_bug():
  293. p1, p2 = Point(-1, 0), Point(1, 0)
  294. p3, p4 = Point(0, 1), Point(-1, 2)
  295. assert Point.is_concyclic(p1, p2, p3)
  296. assert not Point.is_concyclic(p1, p2, p3, p4)
  297. def test_arguments():
  298. """Functions accepting `Point` objects in `geometry`
  299. should also accept tuples and lists and
  300. automatically convert them to points."""
  301. singles2d = ((1,2), [1,2], Point(1,2))
  302. singles2d2 = ((1,3), [1,3], Point(1,3))
  303. doubles2d = cartes(singles2d, singles2d2)
  304. p2d = Point2D(1,2)
  305. singles3d = ((1,2,3), [1,2,3], Point(1,2,3))
  306. doubles3d = subsets(singles3d, 2)
  307. p3d = Point3D(1,2,3)
  308. singles4d = ((1,2,3,4), [1,2,3,4], Point(1,2,3,4))
  309. doubles4d = subsets(singles4d, 2)
  310. p4d = Point(1,2,3,4)
  311. # test 2D
  312. test_single = ['distance', 'is_scalar_multiple', 'taxicab_distance', 'midpoint', 'intersection', 'dot', 'equals', '__add__', '__sub__']
  313. test_double = ['is_concyclic', 'is_collinear']
  314. for p in singles2d:
  315. Point2D(p)
  316. for func in test_single:
  317. for p in singles2d:
  318. getattr(p2d, func)(p)
  319. for func in test_double:
  320. for p in doubles2d:
  321. getattr(p2d, func)(*p)
  322. # test 3D
  323. test_double = ['is_collinear']
  324. for p in singles3d:
  325. Point3D(p)
  326. for func in test_single:
  327. for p in singles3d:
  328. getattr(p3d, func)(p)
  329. for func in test_double:
  330. for p in doubles3d:
  331. getattr(p3d, func)(*p)
  332. # test 4D
  333. test_double = ['is_collinear']
  334. for p in singles4d:
  335. Point(p)
  336. for func in test_single:
  337. for p in singles4d:
  338. getattr(p4d, func)(p)
  339. for func in test_double:
  340. for p in doubles4d:
  341. getattr(p4d, func)(*p)
  342. # test evaluate=False for ops
  343. x = Symbol('x')
  344. a = Point(0, 1)
  345. assert a + (0.1, x) == Point(0.1, 1 + x, evaluate=False)
  346. a = Point(0, 1)
  347. assert a/10.0 == Point(0, 0.1, evaluate=False)
  348. a = Point(0, 1)
  349. assert a*10.0 == Point(0.0, 10.0, evaluate=False)
  350. # test evaluate=False when changing dimensions
  351. u = Point(.1, .2, evaluate=False)
  352. u4 = Point(u, dim=4, on_morph='ignore')
  353. assert u4.args == (.1, .2, 0, 0)
  354. assert all(i.is_Float for i in u4.args[:2])
  355. # and even when *not* changing dimensions
  356. assert all(i.is_Float for i in Point(u).args)
  357. # never raise error if creating an origin
  358. assert Point(dim=3, on_morph='error')
  359. # raise error with unmatched dimension
  360. raises(ValueError, lambda: Point(1, 1, dim=3, on_morph='error'))
  361. # test unknown on_morph
  362. raises(ValueError, lambda: Point(1, 1, dim=3, on_morph='unknown'))
  363. # test invalid expressions
  364. raises(TypeError, lambda: Point(Basic(), Basic()))
  365. def test_unit():
  366. assert Point(1, 1).unit == Point(sqrt(2)/2, sqrt(2)/2)
  367. def test_dot():
  368. raises(TypeError, lambda: Point(1, 2).dot(Line((0, 0), (1, 1))))
  369. def test__normalize_dimension():
  370. assert Point._normalize_dimension(Point(1, 2), Point(3, 4)) == [
  371. Point(1, 2), Point(3, 4)]
  372. assert Point._normalize_dimension(
  373. Point(1, 2), Point(3, 4, 0), on_morph='ignore') == [
  374. Point(1, 2, 0), Point(3, 4, 0)]
  375. def test_issue_22684():
  376. # Used to give an error
  377. with evaluate(False):
  378. Point(1, 2)
  379. def test_direction_cosine():
  380. p1 = Point3D(0, 0, 0)
  381. p2 = Point3D(1, 1, 1)
  382. assert p1.direction_cosine(Point3D(1, 0, 0)) == [1, 0, 0]
  383. assert p1.direction_cosine(Point3D(0, 1, 0)) == [0, 1, 0]
  384. assert p1.direction_cosine(Point3D(0, 0, pi)) == [0, 0, 1]
  385. assert p1.direction_cosine(Point3D(5, 0, 0)) == [1, 0, 0]
  386. assert p1.direction_cosine(Point3D(0, sqrt(3), 0)) == [0, 1, 0]
  387. assert p1.direction_cosine(Point3D(0, 0, 5)) == [0, 0, 1]
  388. assert p1.direction_cosine(Point3D(2.4, 2.4, 0)) == [sqrt(2)/2, sqrt(2)/2, 0]
  389. assert p1.direction_cosine(Point3D(1, 1, 1)) == [sqrt(3) / 3, sqrt(3) / 3, sqrt(3) / 3]
  390. assert p1.direction_cosine(Point3D(-12, 0 -15)) == [-4*sqrt(41)/41, -5*sqrt(41)/41, 0]
  391. assert p2.direction_cosine(Point3D(0, 0, 0)) == [-sqrt(3) / 3, -sqrt(3) / 3, -sqrt(3) / 3]
  392. assert p2.direction_cosine(Point3D(1, 1, 12)) == [0, 0, 1]
  393. assert p2.direction_cosine(Point3D(12, 1, 12)) == [sqrt(2) / 2, 0, sqrt(2) / 2]