K i.dZddlmZddlmZmZmZddlmZm Z m Z m Z ddl m Z ddlmZmZmZmZddlmZmZmZmZddlmZd#d d d d Zd ZdZd#dZd#dZdZdZ dZ!dZ"dZ# d$dZ$dZ%d%dZ&d#dZ'dZ(dZ)d&dZ*d'dZ+dZ,dZ-d Z.d!Z/d"Z0y )(a Module to implement integration of uni/bivariate polynomials over 2D Polytopes and uni/bi/trivariate polynomials over 3D Polytopes. Uses evaluation techniques as described in Chin et al. (2015) [1]. References =========== .. [1] Chin, Eric B., Jean B. Lasserre, and N. Sukumar. "Numerical integration of homogeneous functions on convex and nonconvex polygons and polyhedra." Computational Mechanics 56.6 (2015): 967-981 PDF link : http://dilbert.engr.ucdavis.edu/~suku/quadrature/cls-integration.pdf ) cmp_to_key)xyz)SdiffExprSymbol)_sympify) Segment2DPolygonPointPoint2D)LCgcd_list degree_listPoly) nsimplifyNF) clockwise max_degreec |r:t|trtt|jddi}n t dt|trt |}|j }nt|ddk(rt|}t|dddk(rltd|Dcgc]}t||dz |z||d}}|}t|} t| Dcgc]}t||||dz| z}}n>td|d} |dd }t || }|| t d t||| |S|bi} |wg} |D]n} t| }t|dk(r%|jds| j| At!| j#|ks^| j| p| }t|t$s | t d t|ddd k(rtd| ||}nt'd|||}||S|D]}t)|}|| vs|j*r"t,j.| t,j.<At,j.}t|d }|D]&}t1|}t3|\}}||||zz }(|| |<| S| t d t'|||Scc}wcc}w)aIntegrates polynomials over 2/3-Polytopes. Explanation =========== This function accepts the polytope in ``poly`` and the function in ``expr`` (uni/bi/trivariate polynomials are implemented) and returns the exact integral of ``expr`` over ``poly``. Parameters ========== poly : The input Polygon. expr : The input polynomial. clockwise : Binary value to sort input points of 2-Polytope clockwise.(Optional) max_degree : The maximum degree of any monomial of the input polynomial.(Optional) Examples ======== >>> from sympy.abc import x, y >>> from sympy import Point, Polygon >>> from sympy.integrals.intpoly import polytope_integrate >>> polygon = Polygon(Point(0, 0), Point(0, 1), Point(1, 1), Point(1, 0)) >>> polys = [1, x, y, x*y, x**2*y, x*y**2] >>> expr = x*y >>> polytope_integrate(polygon, expr) 1/4 >>> polytope_integrate(polygon, polys, max_degree=3) {1: 1, x: 1/2, y: 1/2, x*y: 1/4, x*y**2: 1/6, x**2*y: 1/6} evaluateFz>clockwise=True works for only 2-PolytopeV-representation inputrplane2DzQ*356M6I &E %U|- a 0 -q1uo >@-F-&'BC C7ab)&(;  | STT#D&(IF F  F %aLq6Q;qyy{1~MM!$!W))+z9MM!$  % D$%$*:KL L y|A 1 $*1fh +57K)FIzJK <  .DD>D6!<<%&VVF166N!""4$7#=E%e,E$U|HE1"k!nu&<Kc|jr tjtjfS|jr|tjfSt |}|||z fSN)r/rr0 is_numberOner)rArBs rDr1r1sH }}vvqvv~ aee|5 eem##c  ttf}t|}tj}|D]a}tj}d}|D];} t ||| d| d||||} || | dt | dz zz }|dz }=|||zz}||z }c|S)Nrr)rrr$rr0integration_reductionnorm) polynomialsr5r4dims dim_lengthr?degpoly_contribute facet_counthpvalue_over_boundarys rD_polynomial_integraterVs q6DTJVVN *&&  B"78C8:1r!u8CC8H8>> from sympy.integrals.intpoly import main_integrate3d, hyperplane_parameters >>> cube = [[(0, 0, 0), (0, 0, 5), (0, 5, 0), (0, 5, 5), (5, 0, 0), (5, 0, 5), (5, 5, 0), (5, 5, 5)], [2, 6, 7, 3], [3, 7, 5, 1], [7, 6, 4, 5], [1, 5, 4, 0], [3, 1, 0, 2], [0, 4, 6, 2]] >>> vertices = cube[0] >>> faces = cube[1:] >>> hp_params = hyperplane_parameters(faces, vertices) >>> main_integrate3d(1, faces, vertices, hp_params) -125 rrr)rrrr$gradient_terms enumerater/rr0integration_reduction_dynamicrMr)polygon_integratetuple)#r3r5r r4rr:rOrP grad_termsz_termsx_termterm flat_listrSrTabx0r7rAx_dy_dz_dz_indexy_indexx_indexr=degreevalue_over_facer?rNrQrRfacetpis# rDr(r(spDF q!9DTJ#J2 )3))g#*)!'))T)T) ) DF47O  )3 COKa5"Q%qA&-a01B%i0 C5EJAc3WgwsS99&'ffO6fk167vt6=w6=r:672 G$ +at a[!"%/#%5%;c%A!CC  C  C* o  .CffOK%f- !5q\a5==&ub!VXtSQ2UT%1,//$11q  !  S 0 1O o -N . [)sF4cttf}t|}i}|rgdgt|z}t |D]\}} | d| d} } ||j d} t |D]\} }|\}}}}|j |d}tj}| jrtj}n||z}t||| | ||||||| || }||d<|#||xx|| t| z z||zz z cc<|| t| z z||zz ||<|St|tst|}t|||S|Dcic]}|tt|||c}Scc}w)ayFunction to translate the problem of integrating univariate/bivariate polynomials over a 2-Polytope to integrating over its boundary facets. This is done using Generalized Stokes's Theorem and Euler's Theorem. Parameters ========== expr : The input polynomial. facets : Facets(Line Segments) of the 2-Polytope. hp_params : Hyperplane Parameters of the facets. max_degree : optional The maximum degree of any monomial of the input polynomial. >>> from sympy.abc import x, y >>> from sympy.integrals.intpoly import main_integrate, hyperplane_parameters >>> from sympy import Point, Polygon >>> triangle = Polygon(Point(0, 3), Point(5, 3), Point(1, 1)) >>> facets = triangle.sides >>> hp_params = hyperplane_parameters(triangle) >>> main_integrate(x**2 + y**2, facets, hp_params) 325/6 )rrrrrrNr)rrr$rYrZpointsgetrr0r/r[rMrr-r)rV)r3r5r4rrOrPr:r^rSrTrcrdrer7rArCrfrgr=valuerlrUrNr<s rDr.r.s6 q6DTJ F"^nZ&@@ (3 FOKa5"Q%qA $++A.B%j1 F5"'3Q 1d+99*+&&' 3YF5fk167FD#69:r6@!E( /a$1I!4)*T!W"69Cf9L"NNI!4!"T!W!.1;f1D!FF1I) F  F4 $%#D/K(fiH HW[\RSA,Yq\69MM\ \\sE$c  t|}|jrtjStj}||d}t|} t |D];\} } || ||| dz| zf} |t || |dt || | ||zz }=|js[t|t|dzt|t|dzzt|t|dzz}|t|||||||dz z }||dzz}|S)aHelper function to integrate the input uni/bi/trivariate polynomial over a certain face of the 3-Polytope. Parameters ========== facet : Particular face of the 3-Polytope over which ``expr`` is integrated. index : The index of ``facet`` in ``facets``. facets : Faces of the 3-Polytope(expressed as indices of `vertices`). vertices : Vertices that constitute the facet. expr : The input polynomial. degree : Degree of ``expr``. Examples ======== >>> from sympy.integrals.intpoly import polygon_integrate >>> cube = [[(0, 0, 0), (0, 0, 5), (0, 5, 0), (0, 5, 5), (5, 0, 0), (5, 0, 5), (5, 5, 0), (5, 5, 5)], [2, 6, 7, 3], [3, 7, 5, 1], [7, 6, 4, 5], [1, 5, 4, 0], [3, 1, 0, 2], [0, 4, 6, 2]] >>> facet = cube[1] >>> facets = cube[1:] >>> vertices = cube[0] >>> polygon_integrate(facet, [(0, 1, 0), 5], 0, facets, vertices, 1, 0) -25 rrr) rr/r0r$rZdistance_to_sidelineseg_integraterHrrrrr\) rnhp_paramindexr5r r3rlr:re facet_lenr7facsides rDr\r\Ks&D T7D ||vv VVF %( BE IE"<3 xq1u .A(BCD"2tXa[9 eQdF ;< << >>D!}r!u$tD!}r!u'<< qMBqE !"#E8UFH$(&1*6 6 vzF MrJc |\}}|Dcgc]}dt|zt|z }}tddDcgc] }||||z }}tddDcgc]}||t|z }}td|| tddDcgc]}|d|||z c} t fdtddD}|Scc}wcc}wcc}wcc}w)axHelper function to compute the signed distance between given 3D point and a line segment. Parameters ========== point : 3D Point line_seg : Line Segment Examples ======== >>> from sympy.integrals.intpoly import distance_to_side >>> point = (0, 0, 0) >>> distance_to_side(point, [(0, 0, 1), (0, 1, 0)], (1, 0, 0)) -sqrt(2)/2 rr)rrrc34K|]}||zywrG).0r7n_sidevectorx0s rD z#distance_to_side..sC!hqkF1I-C)rrMr% cross_productsum) pointline_segAx1x2r7 rev_normalvector dot_productrrs @@rDrurus$FB-./"qt)DG#/J/%*1a[ 1bebem 1F 1.3Aqk :fQiV $ :F : 9j& 9F38A;?a Aq)?HCuQ{CCK 0 1 :@s!C CCCc jt|}|jrtjStj}|d}t t t dDcgc]}|d||d|z c}}t|tr>t|ddt|ddt|ddi} |||j| zz }n|||zz }t|t|dzt|t|dzzt|t|dzz}|t|||||dz z }||dzz}|Scc}w)aHelper function to compute the line integral of ``expr`` over ``line_seg``. Parameters =========== polygon : Face of a 3-Polytope. index : Index of line_seg in polygon. line_seg : Line Segment. Examples ======== >>> from sympy.integrals.intpoly import lineseg_integrate >>> polygon = [(0, 5, 0), (5, 5, 0), (5, 5, 5), (0, 5, 5)] >>> line_seg = [(0, 5, 0), (5, 5, 0)] >>> lineseg_integrate(polygon, 0, line_seg, 1, 0) 5 rrrr)r r/rr0rMr]r%rr rrrsubsrrv) polygonrxrr3rlr:rer7distance expr_dicts rDrvrvs@, D>D ||vv VVF !BE 8%q8A;q>HQKN:%&'H$ A A A(  (TYYy111(T/! a=2a5 4a=2a5#8 8 T1 1 D $ KKF vzF M%sD0 c t|}|jr|Stj}||jd}t |} t tf} t|| d|dzt|| d|dzz} | dk7r|t||||| ||dz z }|t| ||||| z }|t ||zdz z S)aHelper method for main_integrate. Returns the value of the input expression evaluated over the polytope facet referenced by a given index. Parameters =========== facets : List of facets of the polytope. index : Index referencing the facet to integrate the expression over. a : Hyperplane parameter denoting direction. b : Hyperplane parameter denoting distance. expr : The expression to integrate over the facet. dims : List of symbols denoting axes. degree : Degree of the homogeneous polynomial. Examples ======== >>> from sympy.abc import x, y >>> from sympy.integrals.intpoly import integration_reduction, hyperplane_parameters >>> from sympy import Point, Polygon >>> triangle = Polygon(Point(0, 3), Point(5, 3), Point(1, 1)) >>> facets = triangle.sides >>> a, b = hyperplane_parameters(triangle)[0] >>> integration_reduction(facets, 0, a, b, 1, (x, y), 0) 5 rr) r r/rr0rqr$rrrrLleft_integral2D) r5rxrcrdr3rOrlrsrerCgens inner_products rDrLrLsF D>D || FFE   a B F A q6DtAw'"Q%/$tT!W2E12MMM &vua'4dFQJH H _Qvr4 >>E #d)f$q( ))rJc tj}t|D]}d}||dz |z|dz|zfvrt||||d}|s-t t t d||} t|sYt|trSt|dk(r|d|d|d|d|d|di} n|d|d|d|di} || |j| zz }|| |zz }|S)aComputes the left integral of Eq 10 in Chin et al. For the 2D case, the integral is just an evaluation of the polynomial at the intersection of two facets which is multiplied by the distance between the first point of facet and that intersection. Parameters ========== m : No. of hyperplanes. index : Index of facet to find intersections with. facets : List of facets(Line Segments in 2D case). x0 : First point on facet referenced by index. expr : Input polynomial gens : Generators which generate the polynomial Examples ======== >>> from sympy.abc import x, y >>> from sympy.integrals.intpoly import left_integral2D >>> from sympy import Point, Polygon >>> triangle = Polygon(Point(0, 3), Point(5, 3), Point(1, 1)) >>> facets = triangle.sides >>> left_integral2D(3, 0, facets, facets[0].points[0], 1, (x, y)) 5 rr segment2Dc ||z SrGr)rrs rDz!left_integral2D..)s !a%rJrrr) rr0r%r&rMr]map is_vertexrr r$r) rCrxr5rer3rrsj intersectdistance_originrs rDrrsB FFE 1X4 %!)q519/2 2$VE]F1I{KI "5-?-6*<$=>O#dD)4yA~%)!Wil%)!Wil%)!Wil%< &*!Wil%)!Wil%< _tyy/CCCE_t33E%4& LrJc tj}t|}|tjk(r|St|dk(rr|jsR| | \}}}}|dkDr | | z |zdz nd}|dkDr| dz nd}| |d| |d}}|||z| dz||z| dzzz }|t |||| ||z }n| }|js|||z |z |}}}|dkDr| |dz |dz |dnd}|dkDr| |dz ||dnd}|dkDr| |dz ||dz dnd}|||z| dz||z| dzz||z| dzzz }|t |||| ||z }|t||zdz z S)aThe same integration_reduction function which uses a dynamic programming approach to compute terms by using the values of the integral of previously computed terms. Parameters ========== facets : Facets of the Polytope. index : Index of facet to find intersections with.(Used in left_integral()). a, b : Hyperplane parameters. expr : Input monomial. degree : Total degree of ``expr``. dims : Tuple denoting axes variables. x_index : Exponent of 'x' in ``expr``. y_index : Exponent of 'y' in ``expr``. max_index : Maximum exponent of any monomial in ``monomial_values``. x0 : First point on ``facets[index]``. monomial_values : List of monomial values constituting the polynomial. monom_index : Index of monomial whose integration is being found. vertices : optional Coordinates of vertices constituting the 3-Polytope. hp_param : optional Hyperplane Parameter of the face of the facets[index]. Examples ======== >>> from sympy.abc import x, y >>> from sympy.integrals.intpoly import (integration_reduction_dynamic, hyperplane_parameters) >>> from sympy import Point, Polygon >>> triangle = Polygon(Point(0, 3), Point(5, 3), Point(1, 1)) >>> facets = triangle.sides >>> a, b = hyperplane_parameters(triangle)[0] >>> x0 = facets[0].points[0] >>> monomial_values = [[0, 0, 0, 0], [1, 0, 0, 5], [y, 0, 1, 15], [x, 1, 0, None]] >>> integration_reduction_dynamic(facets, 0, a, b, x, 1, (x, y), 1, 0, 1, x0, monomial_values, 3) 25/2 rrrrrX)rr0r$rHrleft_integral3D)r5rxrcrdr3rlrOrkrj max_indexremonomial_values monom_indexr rwrsrCr=x_degreey_degreex_valuey_valueriz_degreez_values rDr[r[:s%r FFE F A qvv~  4yA~~~'6{'C $Ax1'!|"I-12 )1AkAo1G(+_W-Ea-HG X'"Q%/(W2Dr!u2LL LE E62tTBB~~+2+2W+>> from sympy.integrals.intpoly import left_integral3D >>> cube = [[(0, 0, 0), (0, 0, 5), (0, 5, 0), (0, 5, 5), (5, 0, 0), (5, 0, 5), (5, 5, 0), (5, 5, 5)], [2, 6, 7, 3], [3, 7, 5, 1], [7, 6, 4, 5], [1, 5, 4, 0], [3, 1, 0, 2], [0, 4, 6, 2]] >>> facets = cube[1:] >>> vertices = cube[0] >>> left_integral3D(facets, 3, 1, vertices, ([0, -1, 0], -5), 0) -50 rr)rr0r$rZrurv) r5rxr3r rwrlrsrnreryr7rzr{s rDrrsN FFE 5ME %( BE IE"<3 xq1u .A(BCD !"dHQK8 eQdF ;< << LrJc(|dk(rld}dgt|dzd|zzdzdz z}td|dzD]8}td||z dzD]!}t|zt|zz||dg||<|dz }#:|Std|dzDcgc]r}td|dzDcgc]U}t||z ddDcgc]9}t|zt|zzt||z |z zz||||z |z ||||z |z dg;c}Wc}}t}}}}|Scc}wcc}}wcc}}}w)anReturns a list of all the possible monomials between 0 and y**binomial_power for 2D case and z**binomial_power for 3D case. Parameters ========== binomial_power : Power upto which terms are generated. no_of_gens : Denotes whether terms are being generated for 2D or 3D case. Examples ======== >>> from sympy.integrals.intpoly import gradient_terms >>> gradient_terms(2) [[1, 0, 0, 0], [y, 0, 1, 0], [y**2, 0, 2, 0], [x, 1, 0, 0], [x*y, 1, 1, 0], [x**2, 2, 0, 0]] >>> gradient_terms(2, 3) [[[[1, 0, 0, 0, 0, 0, 0, 0]]], [[[y, 0, 1, 0, 1, 0, 0, 0], [z, 0, 0, 1, 1, 0, 1, 0]], [[x, 1, 0, 0, 1, 1, 0, 0]]], [[[y**2, 0, 2, 0, 2, 0, 0, 0], [y*z, 0, 1, 1, 2, 0, 1, 0], [z**2, 0, 0, 2, 2, 0, 2, 0]], [[x*y, 1, 1, 0, 2, 1, 0, 0], [x*z, 1, 0, 1, 2, 1, 1, 0]], [[x**2, 2, 0, 0, 2, 2, 0, 0]]]] rrNrrr})intr%rrr)binomial_power no_of_genscounttermsx_county_countz_counts rDrYrYs6Qn1A4FFJaOPPQ 23 G NW$> !&a1 5 7 !&g&7R @ B L1</'G+g567Wg&7'&AWg&7'&A1FB7>> LB7>s*D D3>D1D6D DD ct|trt|j|jdgz}dgt |dz z}t t |dz D]{}||||dz}d|dz }|ddz }|ddz|ddzz }t |||g}t||z }t||z t||z f} | |f||<}|Sdgt |z}t|D]\}} | ddD cgc]} ||  c} \}} t|| tfdt ddD}t } | jrd} Dcgc]}|| z  c}|| z }|f||<|Scc} wcc}w)aA helper function to return the hyperplane parameters of which the facets of the polytope are a part of. Parameters ========== poly : The input 2/3-Polytope. vertices : Vertex indices of 3-Polytope. Examples ======== >>> from sympy import Point, Polygon >>> from sympy.integrals.intpoly import hyperplane_parameters >>> hyperplane_parameters(Polygon(Point(0, 3), Point(5, 3), Point(1, 1))) [((0, 1), 3), ((1, -2), -1), ((-2, -1), -3)] >>> cube = [[(0, 0, 0), (0, 0, 5), (0, 5, 0), (0, 5, 5), (5, 0, 0), (5, 0, 5), (5, 5, 0), (5, 5, 5)], [2, 6, 7, 3], [3, 7, 5, 1], [7, 6, 4, 5], [1, 5, 4, 0], [3, 1, 0, 2], [0, 4, 6, 2]] >>> hyperplane_parameters(cube[1:], cube[0]) [([0, -1, 0], -5), ([0, 0, -1], -5), ([-1, 0, 0], -5), ([0, 1, 0], 0), ([1, 0, 0], 0), ([0, 0, 1], 0)] rNrrc34K|]}||zywrGr)rrnormalv1s rDrz(hyperplane_parameters..+s;!F1I1%;r) rr r-r r$r%rrrZrrr/)r2r paramsr7v2a1a2rdfactorrcrvertexv3rzrrrs @@rDr"r"s6$  &$--*:);;3x=1,-s8}q() A!B!a%BAABAAB11 11 -Ar2qk*F!v A220AAF1I 2 M#d)##D/ $JAw9@!Ev(6*EJBB"2r2.F;uQ{;;A6"C{{'-.!a#g.FCA F1I $ MF /s F% F c&tddDcgc] }||||z }}tddDcgc] }||||z }}|d|dz|d|dzz |d|dz|d|dzz |d|dz|d|dzz gScc}wcc}w)zeReturns the cross-product of vectors (v2 - v1) and (v3 - v1) That is : (v2 - v1) X (v3 - v1) rrrr)r%)rrrrs rDrr5s"'q! -A"Q%"Q%- -B -!&q! -A"Q%"Q%- -B - qEBqEMBqEBqEM ) qEBqEMBqEBqEM ) qEBqEMBqEBqEM ) ++ . -s B Bc|jd\}}d}d}ttf}i} |D]} tj| | <t |dkDrt |dk(rX|ddk(r$||rtj||dz fS||fS|ddk(r$||r||dz tjfS||fSt |tr|jr|jD]} | jr:| jd|vs!| | jdxx| jdz cc<I| jD]m} t | j} | dk(r| |vr| | xxdz cc</| dk(s5| jd|vsG| | jdxx| jdz cc<on|jr}|jD]m}t |j} | dk(r||vr| |xxdz cc</| dk(s5|jd|vsG| |jdxx|jdz cc<onJ|jr |jd| |jd<n|jr| |xxdz cc<n||fSt| jd} | dd| ddk\rJ||rtj||dz f}|S||r||dz tjf}|S||f}|S||r||dz tjf}|S||rtj||dz f}|S||f}|S||dz }|S)aHelper method for polytope_integrate. Currently not used in the main algorithm. Explanation =========== Returns a point on the lineseg whose vector inner product with the divergence of `expr` yields an expression with the least maximum total power. Parameters ========== a : Hyperplane parameter denoting direction. b : Hyperplane parameter denoting distance. lineseg : Line segment on which to find the origin. expr : The expression which determines the best point. Algorithm(currently works only for 2D use case) =============================================== 1 > Firstly, check for edge cases. Here that would refer to vertical or horizontal lines. 2 > If input expression is a polynomial containing more than one generator then find out the total power of each of the generators. x**2 + 3 + x*y + x**4*y**5 ---> {x: 7, y: 6} If expression is a constant value then pick the first boundary point of the line segment. 3 > First check if a point exists on the line segment where the value of the highest power generator becomes 0. If not check if the value of the next highest becomes 0. If none becomes 0 within line segment constraints then pick the first boundary point of the line segment. Actually, any point lying on the segment can be picked as best origin in the last case. Examples ======== >>> from sympy.integrals.intpoly import best_origin >>> from sympy.abc import x, y >>> from sympy import Point, Segment2D >>> l = Segment2D(Point(0, 3), Point(1, 1)) >>> expr = x**3*y**7 >>> best_origin((2, 1), 3, l, expr) (0, 3.0) rc|j\}}|jjr t|S|jjr t|S|j|jz tj kr]|j|j |j z z|j|jz z |j ztj fSy)zReturns the point where the input line segment intersects the x-axis. Parameters ========== ls : Line segment r)rqrr/r]rr0rlspqs rD x_axis_cutzbest_origin..x_axis_cutysyy1 33;;8O SS[[8O SSWqvv 33!##)$accACCi01336> >rJc|j\}}|jjr t|S|jjr t|S|j|jz tj kr]tj |j|j |j z z|j|jz z |j zfSy)zReturns the point where the input line segment intersects the y-axis. Parameters ========== ls : Line segment r)rqrr/r]rr0rrs rD y_axis_cutzbest_origin..y_axis_cutsyy1 33;;8O SS[[8O SSWqvv 66133!##),accACCi8133>> >rJrrct|dSNr)str)ks rDrzbest_origin..sc!A$irJkey)rqrrrr0r$rr is_Addargsis_Powis_Mul is_Symbolsorteditems)rcrdlinesegr3rb1rrr power_gensr7monomial univariate term_typerares rD best_originr@sgn^^A FB(( q6DJ  1  4y1} t9>tqyg&661QqT6>)r6M1g&QqT6166>)r6M dD !{{ $ >H#==+t3&x}}Q'78HMM!J(+JOO(+5??1+=!> > > > IIAD #DIII A~$$,"4(A-("aDIIaLD,@"499Q<0DIIaL@0 A +/99Q< 499Q<(4 A% r6MJ,,.4GH a= z!}Q/ /'"ffa!A$h' IG$!A$h' I"X I'"!faff% I G$ffa!f% I"X I!f IrJc i}t|tr3|js&|jr||d<n|jr|j t }|jDcgc]}tt|g||f}}|r|Dchc]}|d c}S|D]+}|\}}|j|r||xx|z cc<'|||<-n|jr|j\}}|||<nbd}|jD]F}t|j} | dk(r|jr|dz }/| dk(s5||jdz }H|||<n||d<|rt|jS|Scc}wcc}w)aXDecomposes an input polynomial into homogeneous ones of smaller or equal degree. Explanation =========== Returns a dictionary with keys as the degree of the smaller constituting polynomials. Values are the constituting polynomials. Parameters ========== expr : Expr Polynomial(SymPy expression). separate : bool If True then simply return a list of the constituent monomials If not then break up the polynomial into constituent homogeneous polynomials. Examples ======== >>> from sympy.abc import x, y >>> from sympy.integrals.intpoly import decompose >>> decompose(x**2 + x*y + x + y + x**3*y**2 + y**5) {1: x + y, 2: x**2 + x*y, 5: x**3*y**2 + y**5} >>> decompose(x**2 + x*y + x + y + x**3*y**2 + y**5, True) {x, x**2, y, y**5, x*y, x**3*y**2} rrr)rr rHrratomsr rrrrrrr$setvalues) r3r poly_dictsymbolsrAdegreesrlrar=rs rDr)r)sy<I$dnn >>IaL [[jj(G$(II/ K8895A/G/.56Ua66$1E#(LFD }}V,!&)T1),0 &) 1 [[ IAv $If F  + N >dnnaKF!^diil*F  + !%If  ! 9##%&& 9/7s #E) E.c< t|tr |jn|}t|}|dkr t |S|rt j nt j t|d}|dk(r3ttd|D|z td|D|z nFttd|D|z td|D|z td|D|z  fd} fd }t|t|dk(r | S| S) aFReturns the same polygon with points sorted in clockwise or anti-clockwise order. Note that it's necessary for input points to be sorted in some order (clockwise or anti-clockwise) for the integration algorithm to work. As a convention algorithm has been implemented keeping clockwise orientation in mind. Parameters ========== poly: 2D or 3D Polygon. normal : optional The normal of the plane which the 3-Polytope is a part of. clockwise : bool, optional Returns points sorted in clockwise order if True and anti-clockwise if False. Examples ======== >>> from sympy.integrals.intpoly import point_sort >>> from sympy import Point >>> point_sort([Point(0, 0), Point(1, 0), Point(1, 1)]) [Point2D(1, 1), Point2D(1, 0), Point2D(0, 0)] rrc34K|]}|jywrGrrrs rDrzpoint_sort..O7FHH7c34K|]}|jywrGrrs rDrzpoint_sort..P8&VXX8rc34K|]}|jywrGrrs rDrzpoint_sort..Rrrc34K|]}|jywrGrrs rDrzpoint_sort..Srrc34K|]}|jywrG)rrs rDrzpoint_sort..Trrc|jjz tjk\r-|jjz tjkr S|jjz dkr|jjz dk\rS|jjz dk(r|jjz dk(rt|jjz dk\s|jjz dk\r|j|jkDr SS|j|jkDr SS|jjz |jjz z|jjz |jjz zz }|dkr S|dkDrS|jjz |jjz z|jjz |jjz zz}|jjz |jjz z|jjz |jjz zz}||kDr SSr)rrr0r)rcrddetfirstsecondcenterorders rDcomparezpoint_sort..compareVs 33>QVV #fhh(?6M SS688^a ACC&((Na$7L SS688^q QSS688^q%8ssVXX~"accFHHn&9!"qssv55SS133YE6 1E 1ssVXX~!##.1ssVXX~!##.12 76M 1WLvxxACC&((N3vxxACC&((N34##.QSS688^4##.QSS688^45v2U2rJc~t||tfdtddD}|dkr S|dkDrSy)Nc34K|]}||zywrGr)rr7rrs rDrz0point_sort..compare3d..osB#a&6!9,Brrr)rrr%)rcrdrrrrrs @rD compare3dzpoint_sort..compare3dmsFFAq)BeAqkBB ?6M 1_LrJr) rr r r$r-rrI NegativeOnerrrr) r2rrptsndimrrrrs ` @@rDrr+s8&dG4$--$C CA1uCyAEEAMME c!f+C axs73781<8C89A=?s73781<8C89A=8C89A=?3. #:agG HHYG HHrJctj}t|ttfrt d|D|zSt|t rat|tr"|jdz|jdzz|zS|jdz|jdzz|jz|zSt|tr#t d|jD|zSy)a/Returns the Euclidean norm of a point from origin. Parameters ========== point: This denotes a point in the dimension_al spac_e. Examples ======== >>> from sympy.integrals.intpoly import norm >>> from sympy import Point >>> norm(Point(2, 7)) sqrt(53) c3&K|] }|dz ywrNr)rcoords rDrznorm..s1%5A:1rc3&K|] }|dz ywrr)rr7s rDrznorm..s0A1a40rN) rHalfrr-r]rrrrrrdictr)rhalfs rDrMrMxs" 66D%$'1511T99 E5 ! eW %GGqL577a</D8 8GGqL577a</%''9dB B E4 000D88 !rJc|dddk(r|dk(rI|jd\}}|jd\}}|jd\}}|jd\} } nQ|dk(rL|jd\}}} |jd\}}} |jd\}}} |jd\} } }z  z zz  z zz }|rP||z||zz }|| z| |zz }t||| z z|||z zz |z t||| z z|||z zz |z fS|dddk(r\|d k(rV|d\}}|d\}}|d|d}}||z||zz }|r.t||z||zz |z t||z||zz |z fSyyy) alReturns intersection between geometric objects. Explanation =========== Note that this function is meant for use in integration_reduction and at that point in the calling function the lines denoted by the segments surely intersect within segment boundaries. Coincident lines are taken to be non-intersecting. Also, the hyperplane intersection for 2D case is also implemented. Parameters ========== geom_1, geom_2: The input line segments. Examples ======== >>> from sympy.integrals.intpoly import intersection >>> from sympy import Point, Segment2D >>> l1 = Segment2D(Point(1, 1), Point(3, 5)) >>> l2 = Segment2D(Point(2, 0), Point(2, 5)) >>> intersection(l1, l2, "segment2D") (2, 3) >>> p1 = ((-1, 0), 0) >>> p2 = ((0, 1), 1) >>> intersection(p1, p2, "plane2D") (0, 1) Nsegmentrrr segment3Dplaner)rqr)geom_1geom_2intersection_typery1ry2x3y3x4y4z1z2z3z4denomt1t2a1xa1ya2xa2yrb2s rDr&r&s@"*  +]]1%FB]]1%FB]]1%FB]]1%FB + -q)JBBq)JBBq)JBBq)JBBbR"W%bR"W(== b27"Bb27"BbBGnrR"W~56>bBGnrR"W~56>@ @"(  )ayHCayHCAYq B#Ic )E"s(R#X-.6"s(R#X-.688 *)rJcdt|trt|dvryyt|tryy)auIf the input entity is a vertex return True. Parameter ========= ent : Denotes a geometric entity representing a point. Examples ======== >>> from sympy import Point >>> from sympy.integrals.intpoly import is_vertex >>> is_vertex((2, 3)) True >>> is_vertex((2, 3, 6)) True >>> is_vertex(Point(2, 3)) True )rrTF)rr]r$r)ents rDrrs4*#u s8v   C  rJcddlm}m}|jDcgc]}|j}}|jDcgc]}|j }}|j |jdj|j |jdj |||}||d}|jycc}wcc}w)zPlots the 2D polytope using the functions written in plotting module which in turn uses matplotlib backend. Parameter ========= poly: Denotes a 2-Polytope. r)Plot List2DSerieszlabel_axes=True)axesN)sympy.plotting.plotr&r'r rrr+show)r2r&r'rxlyll2dsrs rD plot_polytoper.s7!% /v&(( /B /!% /v&(( /B /IIdmmA  !IIdmmA  ! B D T)*AFFH 0 /s CCcjddlm}m}|j}t |dk(r ||y||y)zPlots the polynomial using the functions written in plotting module which in turn uses matplotlib backend. Parameter ========= expr: Denotes a polynomial(SymPy expression). r)plot3dplotrN)r)r0r1 free_symbolsr$)r3r0r1rs rDplot_polynomialr3s+1   D 4yA~t  T rJrG)NN)rr)F)NT)1__doc__ functoolsr sympy.abcrrr sympy.corerrr r sympy.core.sympifyr sympy.geometryr r rrsympy.polys.polytoolsrrrrsympy.simplify.simplifyrrEr1rVr(r.r\rurvrLrr[rrYr"rrr)rrMr&rr.r3rrJrDr<s"!,,'==AA-r3Utr3j$*TnB]J2j<*Z4*n5tJN+/],@/d+\8v+dNAHJIZ9:;8|: .rJ