
    \hP                     0   S r SSKJr  SSKJr  SSKJr  SSKJ	r	  SSK
Jr  SSKJrJrJr  SS	KJr  SS
KJr  SSKJrJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJ r   SSK!Jr  SSK"J#r#  SSK$J%r%  S r&S r'S r(S r)S r*S r+SS.S jr,S r-S"S jr.SS.S  jr/g!)#zUtility functions for geometrical entities.

Contains
========
intersection
convex_hull
closest_points
farthest_points
are_coplanar
are_similar

    )deque)sqrt)	nsimplify   )GeometryEntity)GeometryError)PointPoint2DPoint3D)
OrderedSet)factor_terms)Function
expand_mul)Float)ordered)Symbol)S)cancel)is_sequence)prec_to_dpsc                    UR                   nU Vs/ s H.  n[        U [        5      (       a  UR                  OUU :X  d  M,  UPM0     nnU(       d  [	        SU -  5      e[        U5      S:w  a  [	        SU -  5      eUS   $ s  snf )ad  
Checks whether a Symbol matching ``x`` is present in ``equation``
or not. If present, the matching symbol is returned, else a
ValueError is raised. If ``x`` is a string the matching symbol
will have the same name; if ``x`` is a Symbol then it will be
returned if found.

Examples
========

>>> from sympy.geometry.util import find
>>> from sympy import Dummy
>>> from sympy.abc import x
>>> find('x', x)
x
>>> find('x', Dummy('x'))
_x

The dummy symbol is returned since it has a matching name:

>>> _.name == 'x'
True
>>> find(x, Dummy('x'))
Traceback (most recent call last):
...
ValueError: could not find x
zcould not find %sr   zambiguous %sr   )free_symbols
isinstancestrname
ValueErrorlen)xequationfreeixss        K/var/www/auris/envauris/lib/python3.13/site-packages/sympy/geometry/util.pyfindr$   #   sz    :   D	HT
1c(:(:affaG!TB	H,q011
2w!|!+,,a5L 
Is   +A? A?c                 *    [        [        U S S95      $ )z?Return the tuple of points sorted numerically according to argsc                     U R                   $ Nargsr   s    r#   <lambda>!_ordered_points.<locals>.<lambda>K   s        key)tuplesorted)ps    r#   _ordered_pointsr3   I   s    /011r-   c                    ^ SSK Jn  SSKJn  [	        U 5      n [        U 5       H<  m[        TU5      (       d  M  U R                  T5        [        U4S jU  5       5      s  $    [        S U  5       5      (       a  [        U 5      S:  a  gU R                  5       U R                  5       pC[        U 5       H2  m[        R                  " X4T5      (       d  M!  U R                  T5        M4     U (       d  gU" X4U R                  5       5      nU  H  mTU;  d  M    g   g/ nU  H  m[        T[        5      (       a  UR                  T5        M+  [        TU5      (       a  UR                  TR                  5        MY  [        T[         5      (       d  Mp  TR                   H>  n[        U["        5      (       d  M  UR                  [        UR                  S	-   6 5        M@     M     [%        U6 $ )
a  Returns True if the given entities are coplanar otherwise False

Parameters
==========

e: entities to be checked for being coplanar

Returns
=======

Boolean

Examples
========

>>> from sympy import Point3D, Line3D
>>> from sympy.geometry.util import are_coplanar
>>> a = Line3D(Point3D(5, 0, 0), Point3D(1, -1, 1))
>>> b = Line3D(Point3D(0, -2, 0), Point3D(3, 1, 1))
>>> c = Line3D(Point3D(0, -1, 0), Point3D(5, -1, 9))
>>> are_coplanar(a, b, c)
False

r   )LinearEntity3D)Planec              3   D   >#    U  H  oR                  T5      v   M     g 7fr'   )is_coplanar).0r2   r!   s     r#   	<genexpr>are_coplanar.<locals>.<genexpr>p   s     3A}}Q''s    c              3   B   #    U  H  n[        U[        5      v   M     g 7fr'   )r   r   )r9   r!   s     r#   r:   r;   r   s     
-1a:a!!1      FT)r   )liner5   planer6   setlistr   removeallr   popr   are_collinearappendextendr)   r   r	   are_coplanar)er5   r6   abr2   pt3dr!   s          @r#   rI   rI   N   sv   2 % 	AA!WaHHQK3333 
 
-1
---q6A: uuw1aA$$Q1--   aAEEG$AA:   A!W%%AA~..AFF#A~..A!!U++Gafftm$=>    T""r-   c                     X:X  a  g[        U SS5      nU(       a  U" U5      $ [        USS5      nU(       a  U" U 5      $ U R                  R                  nUR                  R                  n[        SU< SU< 35      e)a  Are two geometrical entities similar.

Can one geometrical entity be uniformly scaled to the other?

Parameters
==========

e1 : GeometryEntity
e2 : GeometryEntity

Returns
=======

are_similar : boolean

Raises
======

GeometryError
    When `e1` and `e2` cannot be compared.

Notes
=====

If the two objects are equal then they are similar.

See Also
========

sympy.geometry.entity.GeometryEntity.is_similar

Examples
========

>>> from sympy import Point, Circle, Triangle, are_similar
>>> c1, c2 = Circle(Point(0, 0), 4), Circle(Point(1, 4), 3)
>>> t1 = Triangle(Point(0, 0), Point(1, 0), Point(0, 1))
>>> t2 = Triangle(Point(0, 0), Point(2, 0), Point(0, 2))
>>> t3 = Triangle(Point(0, 0), Point(3, 0), Point(0, 1))
>>> are_similar(t1, t2)
True
>>> are_similar(t1, t3)
False

T
is_similarNzCannot test similarity between z and )getattr	__class____name__r   )e1e2is_similar1is_similar2n1n2s         r#   are_similarrY      sv    \ 
x"lD1K2"lD1K2			B			B
68"=? ?r-   c                  |  ^	^
 SSK Jm
  SSKJm	  U (       Ga!  [	        S U  5       5      (       a%  [        SS5      nU  H  nX-  nM	     [        U 5      nO[	        U
4S jU  5       5      (       a;  [        SS5      nSnU  H$  nUR                  nXR                  U-  -  nXE-  nM&     UnOT[	        U	4S jU  5       5      (       a:  [        SS5      nSnU  H$  nUR                  nXR                  U-  -  nXg-  nM&     UnWW-  nUR                  " UR                   Vs/ s H  oR                  5       PM     sn6 $ gs  snf )	a^  Find the centroid (center of mass) of the collection containing only Points,
Segments or Polygons. The centroid is the weighted average of the individual centroid
where the weights are the lengths (of segments) or areas (of polygons).
Overlapping regions will add to the weight of that region.

If there are no objects (or a mixture of objects) then None is returned.

See Also
========

sympy.geometry.point.Point, sympy.geometry.line.Segment,
sympy.geometry.polygon.Polygon

Examples
========

>>> from sympy import Point, Segment, Polygon
>>> from sympy.geometry.util import centroid
>>> p = Polygon((0, 0), (10, 0), (10, 10))
>>> q = p.translate(0, 20)
>>> p.centroid, q.centroid
(Point2D(20/3, 10/3), Point2D(20/3, 70/3))
>>> centroid(p, q)
Point2D(20/3, 40/3)
>>> p, q = Segment((0, 0), (2, 0)), Segment((0, 0), (2, 2))
>>> centroid(p, q)
Point2D(1, 2 - sqrt(2))
>>> centroid(Point(0, 0), Point(2, 0))
Point2D(1, 0)

Stacking 3 polygons on top of each other effectively triples the
weight of that polygon:

>>> p = Polygon((0, 0), (1, 0), (1, 1), (0, 1))
>>> q = Polygon((1, 0), (3, 0), (3, 1), (1, 1))
>>> centroid(p, q)
Point2D(3/2, 1/2)
>>> centroid(p, p, p, q) # centroid x-coord shifts left
Point2D(11/10, 1/2)

Stacking the squares vertically above and below p has the same
effect:

>>> centroid(p, p.translate(0, 1), p.translate(0, -1), q)
Point2D(11/10, 1/2)

r   SegmentPolygonc              3   B   #    U  H  n[        U[        5      v   M     g 7fr'   )r   r	   )r9   gs     r#   r:   centroid.<locals>.<genexpr>  s     2Tz!U##Tr=   r   c              3   <   >#    U  H  n[        UT5      v   M     g 7fr'   r   )r9   r`   r\   s     r#   r:   ra          6AAw''   c              3   <   >#    U  H  n[        UT5      v   M     g 7fr'   rc   )r9   r`   r^   s     r#   r:   ra     rd   re   N)r?   r\   polygonr^   rD   r	   r   lengthmidpointareacentroidfuncr)   simplify)r)   cr`   denLlArK   r!   r^   r\   s            @@r#   rk   rk      s"   `  2T222aA d)C6666aAAHHZZ\!  C6666aAAFFZZ\!  C	Svvaff5f

f566/ . 6s   D9c                     [        U 5       Vs/ s H  n[        U5      PM     nn[        U5      S:  a  [        S5      e UR	                  S S9  [        S U 5       5      (       d  S nOSS	KJn  S
/nU" US   R                  US   R                  -
  US   R                  US   R                  -
  5      nSn[        SS/5      n[        S[        U5      5       H  nXa:  aB  X!   S   X&   S   -
  U:  a/  UR                  5         US-  nXa:  a  X!   S   X&   S   -
  U:  a  M/  U Hh  nU" X!   R                  X(   R                  -
  X!   R                  X(   R                  -
  5      n	X:  a  X4/nOX:X  a  UR                  X45        OMf  U	nMj     UR                  U5        M     U V
Vs1 s H!  n
[        U
 Vs/ s H  oU   PM	     sn5      iM#     snn
$ s  snf ! [
         a    [        S5      ef = fs  snf s  snn
f )a  Return the subset of points from a set of points that were
the closest to each other in the 2D plane.

Parameters
==========

args
    A collection of Points on 2D plane.

Notes
=====

This can only be performed on a set of points whose coordinates can
be ordered on the number line. If there are no ties then a single
pair of Points will be in the set.

Examples
========

>>> from sympy import closest_points, Triangle
>>> Triangle(sss=(3, 4, 5)).args
(Point2D(0, 0), Point2D(3, 0), Point2D(3, 4))
>>> closest_points(*_)
{(Point2D(0, 0), Point2D(3, 0))}

References
==========

.. [1] https://www.cs.mcgill.ca/~cs251/ClosestPair/ClosestPairPS.html

.. [2] Sweep line algorithm
    https://en.wikipedia.org/wiki/Sweep_line_algorithm

   z)At least 2 distinct points must be given.c                     U R                   $ r'   r(   r*   s    r#   r+    closest_points.<locals>.<lambda>D      QVVr-   r.   The points could not be sorted.c              3   ^   #    U  H#  oR                     H  o"R                  v   M     M%     g 7fr'   r)   is_Rationalr9   jr!   s      r#   r:   !closest_points.<locals>.<genexpr>H       8aA}}}a   +-c                 b    X -  X-  -   nUR                   (       a  [        U5      $ [        U5      $ r'   r{   _sqrtr   r   yargs      r#   hypotclosest_points.<locals>.hypotI  *    #)CSz!9r-   r   r   )r   r   r   )rA   r
   r   r   sort	TypeErrorrD   mathr   r   r   r   rangepopleftrG   r0   )r)   r!   r2   r   rv	best_distleftboxr}   dpairs              r#   closest_pointsr     s   F !Y'YYA'
1vzDEE<	#$ 8a888	 	
Badffqtvvoqtvv!7ID
A-C1c!fh147QWQZ/);KKMAID h147QWQZ/); Aadffqtvvoqtvv7A}fX		1&!I  	

1   577BDE&AQ4&'B77M 	(  <:;;<@ '7s)   G G -G<<G7
G<G47G<T)rg   c                    SSK Jn  SSKJn  [	        5       nU H  n[        U[        5      (       d   [        U5      n[        U[        5      (       a  UR                  U5        ML  [        XR5      (       a  UR                  UR                  5        My  [        XS5      (       a  UR                  UR                  5        M  [        S[        U5      -  5      e   [!        S U 5       5      (       a  [        S5      e[#        U5      n[%        U5      S:X  a  U (       a  US   $ US   S	4$ [%        U5      S
:X  a  U" US   US   5      nU (       a  U$ US	4$ S n/ n/ n	 UR'                  S S9  U H  n
[%        U5      S:  aI  U" US   US   U
5      S::  a5  UR+                  5         [%        U5      S:  a  U" US   US   U
5      S::  a  M5  [%        U	5      S:  aI  U" U	S   U	S   U
5      S:  a5  U	R+                  5         [%        U	5      S:  a  U" U	S   U	S   U
5      S:  a  M5  UR-                  U
5        U	R-                  U
5        M     UR/                  5         [1        XSS -   5      n[%        U5      S
:X  a  U" US   US   5      nU (       a  U$ US	4$ U (       a  U" U6 $ UR/                  5         X4$ ! [         a    [        S[        U5      -  5      ef = f! [(         a    [        S5      ef = f)a  The convex hull surrounding the Points contained in the list of entities.

Parameters
==========

args : a collection of Points, Segments and/or Polygons

Optional parameters
===================

polygon : Boolean. If True, returns a Polygon, if false a tuple, see below.
          Default is True.

Returns
=======

convex_hull : Polygon if ``polygon`` is True else as a tuple `(U, L)` where
              ``L`` and ``U`` are the lower and upper hulls, respectively.

Notes
=====

This can only be performed on a set of points whose coordinates can
be ordered on the number line.

See Also
========

sympy.geometry.point.Point, sympy.geometry.polygon.Polygon

Examples
========

>>> from sympy import convex_hull
>>> points = [(1, 1), (1, 2), (3, 1), (-5, 2), (15, 4)]
>>> convex_hull(*points)
Polygon(Point2D(-5, 2), Point2D(1, 1), Point2D(3, 1), Point2D(15, 4))
>>> convex_hull(*points, **dict(polygon=False))
([Point2D(-5, 2), Point2D(15, 4)],
 [Point2D(-5, 2), Point2D(1, 1), Point2D(3, 1), Point2D(15, 4)])

References
==========

.. [1] https://en.wikipedia.org/wiki/Graham_scan

.. [2] Andrew's Monotone Chain Algorithm
  (A.M. Andrew,
  "Another Efficient Algorithm for Convex Hulls in Two Dimensions", 1979)
  https://web.archive.org/web/20210511015444/http://geomalgorithms.com/a10-_hull-1.html

r   r[   r]   z8%s is not a GeometryEntity and cannot be made into Pointz#Convex hull for %s not implemented.c              3   >   #    U  H  n[        U5      S :g  v   M     g7f)rt   N)r   )r9   r   s     r#   r:   convex_hull.<locals>.<genexpr>  s     
"13q6Q;s   z2Can only compute the convex hull in two dimensionsr   Nrt   c                     UR                   U R                   -
  UR                  U R                  -
  -  UR                  U R                  -
  UR                   U R                   -
  -  -
  $ )zFReturn positive if p-q-r are clockwise, neg if ccw, zero if
collinear.)r   r   )r2   qrs      r#   _orientation!convex_hull.<locals>._orientation  sI     acc	ACC!##I&!##)accACCi)@@@r-   c                     U R                   $ r'   r(   r*   s    r#   r+   convex_hull.<locals>.<lambda>  rw   r-   r.   rx   )r?   r\   rg   r^   r   r   r   r	   NotImplementedErrorr   r   addupdatepointsverticestypeanyrB   r   r   r   rE   rG   reverser0   )rg   r)   r\   r^   r2   rJ   sr   Urp   p_i
convexHulls               r#   convex_hullr   h  s   j  A!^,,f!H aEE!H##HHQXX##HHQZZ %5Q?A A " 
"
"""MNNQA
1v{qt0QqT4L0	Q1AaD!A$q*!T*A 	A
A<	#$ !fqj\!B%2<AEEG !fqj\!B%2<A!fqj\!B%2<AEEG !fqj\!B%2<A		  IIKqQr7{#J
:!JqM:a=1q*!T*
##			vi ' f ![^abc^d!deefB  <:;;<s   J/K /"KK*c                     S n[        U 5       Vs/ s H  n[        U5      PM     nn[        S U 5       5      (       d  S nOSSKJn  / nSnU" U 5       Hm  n[        U5      u  pU" UR                  U	R                  -
  UR                  U	R                  -
  5      n
X:  a  X4/nOX:X  a  UR                  X45        OMk  U
nMo     [        U5      $ s  snf )a  Return the subset of points from a set of points that were
the furthest apart from each other in the 2D plane.

Parameters
==========

args
    A collection of Points on 2D plane.

Notes
=====

This can only be performed on a set of points whose coordinates can
be ordered on the number line. If there are no ties then a single
pair of Points will be in the set.

Examples
========

>>> from sympy.geometry import farthest_points, Triangle
>>> Triangle(sss=(3, 4, 5)).args
(Point2D(0, 0), Point2D(3, 0), Point2D(3, 4))
>>> farthest_points(*_)
{(Point2D(0, 0), Point2D(3, 4))}

References
==========

.. [1] https://code.activestate.com/recipes/117225-convex-hull-and-diameter-of-2d-point-sets/

.. [2] Rotating Callipers Technique
    https://en.wikipedia.org/wiki/Rotating_calipers

c              3     #    [        U 0 SS0D6u  pUc/  [        U[        5      (       a  [        S5      eUR                  v   g Sn[        U5      S-
  nU[        U5      S-
  :  d  US:  a  X   X$   4v   U[        U5      S-
  :X  a  US-  nOUS:X  a  US-  nOXS-      R                  X   R                  -
  X$   R                  X$S-
     R                  -
  -  X$   R                  X$S-
     R                  -
  XS-      R                  X   R                  -
  -  :  a  US-  nOUS-  nU[        U5      S-
  :  a  M  US:  a  M  g g 7f)Nrg   Fz+At least two distinct points must be given.r   r   )r   r   r	   r   r)   r   r   r   )Pointsr   rp   r!   r}   s        r#   rotatingCalipers)farthest_points.<locals>.rotatingCalipers  s4    F9y%&899!U## !NOO&&LAA
Ac!fqj.AEdADj A
?FA!VFA !fhh'ADFFQsVXX,=>!aC&((*q1vxx!$&&/@ABFAFA c!fqj.AEs   D9E=EEc              3   ^   #    U  H#  oR                     H  o"R                  v   M     M%     g 7fr'   rz   r|   s      r#   r:   "farthest_points.<locals>.<genexpr>  r   r   c                 b    X -  X-  -   nUR                   (       a  [        U5      $ [        U5      $ r'   r   r   s      r#   r   farthest_points.<locals>.hypot  r   r-   r   r   )	rA   r
   rD   r   r   r3   r   r   rG   )r)   r   r!   r2   r   r   diamr   hr   r   s              r#   farthest_pointsr     s    H2 !Y'YYA'8a888	 		BD &t$!##)QSS133Y'8&BYIIqf ' r7N1 	(s   Cc           
         [        U5      (       a  [        U5      nUS   nO=[        U[        5      (       a  U1nO$[        U[        5      (       a  O[        SU-  5      eU R                   Vs0 s H.  nXR:w  d  M
  UW;   d  M  U[	        UR                  5      " U5      _M0     nn[        U[        5      (       a+  [	        UR                  5      " U5      R                  U5      nOUR                  U5      nU R                  U5      n 0 n[        U5       H  n	U R                  U5      n
U
R                  U[        R                  05      nX-
  R                  U[        R                  05      n[        [!        [#        U* U-  R                  U5      5      SS95      nXS-
  :X  a7  UR                  UR%                  5        VVs/ s H  u  pX4PM
     snn5      s  $ XU'   X}-
  n UR                  U5      nM     gs  snf s  snnf )aK  Return ``dy/dx`` assuming that ``eq == 0``.

Parameters
==========

y : the dependent variable or a list of dependent variables (with y first)
x : the variable that the derivative is being taken with respect to
n : the order of the derivative (default is 1)

Examples
========

>>> from sympy.abc import x, y, a
>>> from sympy.geometry.util import idiff

>>> circ = x**2 + y**2 - 4
>>> idiff(circ, y, x)
-x/y
>>> idiff(circ, y, x, 2).simplify()
(-x**2 - y**2)/y**3

Here, ``a`` is assumed to be independent of ``x``:

>>> idiff(x + a + y, y, x)
-1

Now the x-dependence of ``a`` is made explicit by listing ``a`` after
``y`` in a list.

>>> idiff(x + a + y, [y, a], x)
-Derivative(a, x) - 1

See Also
========

sympy.core.function.Derivative: represents unevaluated derivatives
sympy.core.function.diff: explicitly differentiates wrt symbols

r   z:expecting x-dependent symbol(s) or function(s) but got: %sF)deepr   N)r   rA   r   r   r   r   r   r   diffsubsr   xreplacer   ZeroOner   r   r   items)eqr   r   ndepr   fdydxderivsr!   deqrL   rK   ypkvs                   r#   idiffr   2  s   P 1~~!faD	Av		c	Ax	 	 UXYYZZ)+ 	 A6 
 3h 
 HQVVQ	A 	  !V"''*vvay	BF1XggajLL$(Waee}-*VaRTKK,?%@uMNA:77qwwy9ytqQFy9::tYyy| 	 " :s   -	G7:G7 G7 G<F)pairwisec           
         [        U5      S::  a  / $ [        U5      nSn[        U5       H  u  pE[        U[        5      (       d  [        U5      n0 nUR                  [        5       HB  nUc  UR                  O[        UR                  U5      nUR                  U[        USS95        MD     UR                  U5      X'   M     U (       dQ  US   R                  US   5      nUSS  H0  n	/ n
U H#  nU
R                  UR                  U	5      5        M%     U
nM2     Ox/ n[        [        U5      5       HA  n[        US-   [        U5      5       H"  nUR                  [        X   X   5      5        M$     MC     [        [!        [#        U5      5      5      nUb+  [%        U5      nU Vs/ s H  oDR'                  U5      PM     nnU$ s  snf )ad  The intersection of a collection of GeometryEntity instances.

Parameters
==========
entities : sequence of GeometryEntity
pairwise (keyword argument) : Can be either True or False

Returns
=======
intersection : list of GeometryEntity

Raises
======
NotImplementedError
    When unable to calculate intersection.

Notes
=====
The intersection of any geometrical entity with itself should return
a list with one item: the entity in question.
An intersection requires two or more entities. If only a single
entity is given then the function will return an empty list.
It is possible for `intersection` to miss intersections that one
knows exists because the required quantities were not fully
simplified internally.
Reals should be converted to Rationals, e.g. Rational(str(real_num))
or else failures due to floating point issues may result.

Case 1: When the keyword argument 'pairwise' is False (default value):
In this case, the function returns a list of intersections common to
all entities.

Case 2: When the keyword argument 'pairwise' is True:
In this case, the functions returns a list intersections that occur
between any pair of entities.

See Also
========

sympy.geometry.entity.GeometryEntity.intersection

Examples
========

>>> from sympy import Ray, Circle, intersection
>>> c = Circle((0, 1), 1)
>>> intersection(c, c.center)
[]
>>> right = Ray((0, 0), (1, 0))
>>> up = Ray((0, 0), (0, 1))
>>> intersection(c, right, up)
[Point2D(0, 0)]
>>> intersection(c, right, up, pairwise=True)
[Point2D(0, 0), Point2D(0, 2)]
>>> left = Ray((1, 0), (0, 0))
>>> intersection(right, left)
[Segment2D(Point2D(0, 0), Point2D(1, 0))]

r   NT)rationalr   rt   )r   rB   	enumerater   r   r	   atomsr   _precmin
setdefaultr   r   intersectionrH   r   r   rA   r   r   )r   entitieskwargsprecr!   rJ   r   r   resentitynewresr   ansr}   r   r2   s                   r#   r   r   {  s   x 8}	H~HD(#!^,,aAA"l177AGGT0BDLLIa$78   jjm $ qk&&x{3qrlFFannV45 C	 # s8}%A1q5#h-0

<X[AB 1 & 73s8$% "#s!ss1vs#J $s   %GN)r   )0__doc__collectionsr   r   r   r   sympyr   r   r   
exceptionsr   pointr	   r
   r   sympy.core.containersr   sympy.core.exprtoolsr   sympy.core.functionr   r   sympy.core.numbersr   sympy.core.sortingr   sympy.core.symbolr   sympy.core.singletonr   sympy.polys.polytoolsr   (sympy.functions.elementary.miscellaneoussympy.utilities.iterablesr   mpmath.libmp.libmpfr   r$   r3   rI   rY   rk   r   r   r   r   r    r-   r#   <module>r      s       " % * * , - 4 $ & $ " ( 9 1 +#L2
C#L9?xI7XI8X  $ pdUpFR &+ `r-   