K iP*dZddlmZddlmZddlmZddlm Z ddl m Z ddl m Z mZmZdd lmZdd lmZdd lmZmZdd lmZdd lmZddlmZddlmZddlm Z ddl!mZddl"m#Z#ddl$m%Z%dZ&dZ'dZ(dZ)dZ*dZ+dddZ,dZ-d"dZ.ddd Z/y!)#zUtility functions for geometrical entities. Contains ======== intersection convex_hull closest_points farthest_points are_coplanar are_similar )deque)sqrt) nsimplify)GeometryEntity) GeometryError)PointPoint2DPoint3D) OrderedSet) factor_terms)Function expand_mul)Float)ordered)Symbol)S)cancel) is_sequence) prec_to_dpsc|j}|Dcgc]&}t|tr |jn||k(s%|(}}|st d|zt |dk7rt d|z|dScc}w)a 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 zcould not find %srz ambiguous %sr) free_symbols isinstancestrname ValueErrorlen)xequationfreeixss Y/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/geometry/util.pyfindr$#sv:  D H 1c(:affaG! HB H ,q011 2w!|!+,, a5L Is &A/A/c0tt|dS)z?Return the tuple of points sorted numerically according to argsc|jSNargsrs r#z!_ordered_points..Ks key)tuplesorted)ps r#_ordered_pointsr2Is /0 11r,cbddlm}ddlm}t |}t |D]5t |s|jtfd|DcStd|Drt|dkry|j|j}}t |D]+tj||s|j-|sy||||j}|D]|vsyyg}|D]t tr|j%t |r|jjMt t s^jD]7}t |t"s|jt|jd z9t%|S) 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)Planec3@K|]}|jywr') is_coplanar).0r1r!s r# zare_coplanar..ps3Aq}}Q'3sc3<K|]}t|tywr')rr )r8r!s r#r9zare_coplanar..rs -a:a ! -FT)r)liner4planer5setlistrremoveallrpopr are_collinearappendextendr)rr are_coplanar)er4r5abr1pt3dr!s @r#rGrGNs2% AA !W4 a  HHQK333 34  -1 -- q6A:uuw1a A$$Q1-  aAEEG$A !A:  ! ?A!W% AA~. AFF#A~.?A!!U+ Gafftm$=>? ?T""r,c||k(ryt|dd}|r||St|dd}|r||S|jj}|jj}td|d|)aAre 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_similarNzCannot test similarity between z and )getattr __class____name__r)e1e2 is_similar1 is_similar2n1n2s r# are_similarrWsx\ Rx"lD1K2"lD1K2   B   B 68"= ??r,cJ ddlm ddlm |r t d|Dr$t dd}|D]}||z } t |}nt fd|Dr;t dd}d}|D]%}|j}||j|zz }||z }'|}nNt fd|Dr:t dd}d}|D]%}|j}||j|zz }||z }'|}z}|j|jDcgc]}|jc}Sycc}w) aFind 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) rSegmentPolygonc3<K|]}t|tywr')rr )r8gs r#r9zcentroid..s2z!U#2r;rc36K|]}t|ywr'r)r8r^rZs r#r9zcentroid..6AAw'6c36K|]}t|ywr'r`)r8r^r\s r#r9zcentroid..rarbN)r=rZpolygonr\rBr rlengthmidpointareacentroidfuncr)simplify) r)cr^denLlArIr!r\rZs @@r#rhrhs2`  2T2 2a A Q d)C 66 6a AA HHQZZ\!Q C 66 6a AA FFQZZ\!Q C Sqvvaff5 566/ .6sD c t|Dcgc] }t|}}t|dkr td |j dt d|Dsd}ndd lm}d g}||d j|djz |d j|djz }d}tdd g}tdt|D]}||krC||d||dz |kDr/|j|d z }||kr||d||dz |kDr/|D]l}|||j||jz ||j||jz } | |kr||fg}n| |k(r|j||fnk| }n|j||D chc]} t| Dcgc]}|| c} c}} Scc}w#t $r tdwxYwcc}wcc}} w) 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|jSr'r(r*s r#r+z closest_points..D QVVr,r-The points could not be sorted.c3VK|]!}|jD]}|j#ywr'r) is_Rationalr8jr!s r#r9z!closest_points..H$88Aq}}8}8')c\||z||zz}|jr t|St|Sr'rw_sqrtrryargs r#hypotzclosest_points..hypotI.A#!)CSz!9 r,rr)rrr)r?r rrsort TypeErrorrBmathrrrrrangepopleftrEr/) r)r!r1rrv best_distleftboxrydpairs r#closest_pointsrsF!Y''A' 1vzDEE< #$ 8a8 8   Badffqtvvoqtvv!7I D A-C 1c!f Qh1Q47QtWQZ/); KKM AIDQh1Q47QtWQZ/); Aadffqtvvoqtvv!7A9}!fXi 1a&!I  1  57 7DE&A1Q4& ' 77M ( <:;;<@' 7s(GG)G27 G- G2G*-G2T)rdcdddlm}ddlm}t }|D]}t |t s t|}t |tr|j|At ||r|j|jit ||r|j|jtdt|zt!d|Dr tdt#|}t%|dk(r|r|dS|dd fSt%|d k(r||d|d}|r|S|d fSd }g}g} |j'd |D]} t%|dkDrE||d|d| dkr2|j+t%|dkDr||d|d| dkr2t%| dkDrE|| d| d| dk\r2| j+t%| dkDr|| d| d| dk\r2|j-| | j-| |j/t1| |ddz} t%| d k(r|| d| d}|r|S|d fS|r|| S|j/|| fS#t$rtdt|zwxYw#t($r tdwxYw)aThe 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 rrYr[z8%s is not a GeometryEntity and cannot be made into Pointz#Convex hull for %s not implemented.c38K|]}t|dk7yw)rqN)r)r8rs r#r9zconvex_hull..s "13q6Q; "sz2Can only compute the convex hull in two dimensionsrNrqc|j|jz |j|jz z|j|jz |j|jz zz S)zNReturn positive if p-q-r are clockwise, neg if ccw, zero if collinear.)rr)r1qrs r# _orientationz!convex_hull.._orientationsIacc ACC!##I&!##)accACCi)@@@r,c|jSr'r(r*s r#r+zconvex_hull..rsr,r-rt)r=rZrdr\r rrr NotImplementedErrorrraddupdatepointsverticestypeanyr@rrrrCrEreverser/) rdr)rZr\r1rHsrUrmp_i convexHulls r# convex_hullrhsj  A A!^, f!H a  EE!H 7 # HHQXX  7 # HHQZZ %5Q?A AA" " ""MNN QA 1v{qt0QqT4L0 Q1 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    IIKq1Qr7{#J :! JqM:a= 1q*!T* ## 1v i' f ![^abc^d!dee fB <:;;>> 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 c3Kt|iddi\}}|*t|tr td|jyd}t |dz }|t |dz ks|dkDr||||f|t |dz k(r|dz}n|dk(r|dz }n||dzj ||j z ||j||dz jz z||j ||dz j z ||dzj||jz zkDr|dz }n|dz}|t |dz kr|dkDryyw)NrdFz+At least two distinct points must be given.rr)rrr rr)rrr)Pointsrrmr!rys r#rotatingCalipersz)farthest_points..rotatingCaliperssHF9y%&891 9!U# !NOO&&LAA Ac!fqj.AEdAaDj A ?FA!VFA!fhh1'AaDFFQqsVXX,=>1!AaC&((*q1vxx!A$&&/@ABFAFAc!fqj.AEsD8E;EEc3VK|]!}|jD]}|j#ywr'rvrxs r#r9z"farthest_points..rzr{c\||z||zz}|jr t|St|Sr'r}rs r#rzfarthest_points..hypotrr,rr) r?r rBrrr2rrrE) r)rr!r1rrdiamrhrrs r#farthest_pointsrsH2!Y''A' 8a8 8   B D & t$1 !##)QSS133Y ' t8a&B $Y IIq!f    r7N1 (sC c t|rt|}|d}n3t|tr|h}nt|trnt d|z|j Dcic]'}||k7r |vr|t |j|)}}t|tr+t |j|j|}n|j|}|j|}i}t|D]} |j|} | j|tji} | | z j|tji} tt!t#| | z j|d} | |dz k(r6| j|j%Dcgc] \}}||f c}}cS| ||<|| z }|j|}ycc}wcc}}w)aReturn ``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 rz:expecting x-dependent symbol(s) or function(s) but got: %sF)deeprN)rr?rrrrrrdiffsubsrxreplacerZeroOner rritems)eqrrndeprfdydxderivsr!deqrJrIypkvs r#idiffr2sP1~!f aD Av c Ax  UXYYZZ)+ A 6a3h HQVV Q   A !Vx"''*vvay B F 1X ggaj LL$ ( 1W  aee} - *VaRTKK,?%@uM N A:77qwwy9tq!QF9: :t BYyy|  ":s ,G'G" F)pairwisec ht|dkrgSt|}d}t|D]\}}t|ts t |}i}|j tD]C}| |jnt|j|}|j|t|dE|j|||<|sM|dj|d}|ddD]-} g} |D]"} | j| j| $| }/nvg} tt|D]@} t| dzt|D]#}| jt|| ||%Btt!t#| }|)t%|}|Dcgc]}|j'|}}|Scc}w)a 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))] rNT)rationalrrq)rr@ enumeraterrr atomsr_precmin setdefaultrr intersectionrFrrr?rr)rentitieskwargsprecr!rHrrresentitynewresransryrr1s r#rr{sx 8} H~H D(# $1!^,aA  9A"l177AGGT0BD LLIa$7 8 9jjm  $ qk&&x{3qrl FF 6 annV45 6C  s8}% CA1q5#h-0 C < Xa[AB C C73s8$%   "#!qss1v## J$sF/N)r)0__doc__ collectionsrrrr~sympyrrr exceptionsrpointr r r sympy.core.containersr sympy.core.exprtoolsr sympy.core.functionrrsympy.core.numbersrsympy.core.sortingrsympy.core.symbolrsympy.core.singletonrsympy.polys.polytoolsr(sympy.functions.elementary.miscellaneoussympy.utilities.iterablesrmpmath.libmp.libmpfrr$r2rGrWrhrrrrrr,r#rs "%**,-4$&$"(91+#L2 C#L9?xI7XI8X $pdUpFR&+`r,