ó
¡¼™\c           @  s£  d  Z  d d l m Z m Z d d l m Z d d l m Z m Z m	 Z	 m
 Z
 d d l m Z d d l m Z m Z m Z m Z d d l m Z m Z m Z d d l m Z m Z m Z d! d	 „ Z d
 „  Z d! d „ Z d! d „ Z d „  Z d „  Z  d „  Z! d „  Z" d „  Z# d! d! d „ Z$ d „  Z% d d d „ Z& d! d „ Z' d „  Z( d „  Z) e* d „ Z+ d! e, d „ Z- d „  Z. d „  Z/ d „  Z0 d „  Z1 d  „  Z2 d! S("   sø  
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
iÿÿÿÿ(   t   print_functiont   division(   t
   cmp_to_key(   t   St   difft   Exprt   Symbol(   t	   nsimplify(   t	   Segment2Dt   Polygont   Pointt   Point2D(   t   xt   yt   z(   t   LCt   gcd_listt   degree_listc         K  s4  | j  d t ƒ } | j  d d ƒ } | rf t |  t ƒ rW t d t t |  j ƒ Œ }  qf t d ƒ ‚ n  t |  t ƒ r t |  ƒ } |  j	 } n6t
 |  d ƒ d k rft
 |  ƒ } t
 |  d d ƒ d k rWg  t d | ƒ D]( } t |  | d | |  | d ƒ ^ qÙ }	 |  } t
 |	 ƒ }
 g  t d |
 ƒ D]% } t |	 | |	 | d |
 ƒ ^ q)} qÃt d	 ƒ ‚ n] |  d } |  d } t | | ƒ } | d k rÃ| d k r°t d
 ƒ ‚ n  t | | | | ƒ S| d k	 r	i  } t | t ƒ r | d k	 r t d ƒ ‚ n  t
 | d d ƒ d k r5t d | | | | ƒ } n t d | | | ƒ } | d k rZ| Sx¨ | D]  }  |  | k ra|  t j k r˜t j | t j <qan  t j } t |  d t ƒ} x> | D]6 } t | ƒ } t | ƒ \ } } | | | | 7} qºW| | |  <qaqaW| S| d k r$t d
 ƒ ‚ n  t | | | ƒ S(   s  Integrates polynomials over 2/3-Polytopes.

    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.geometry.polygon import Polygon
    >>> from sympy.geometry.point import Point
    >>> 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}
    t	   clockwiset
   max_degreet   evaluates>   clockwise=True works for only 2-PolytopeV-representation inputi    i   i   t   plane2Ds<   Integration for H-representation 3Dcase not implemented yet.s3   Input expression be mustbe a valid SymPy expressions-   Input polynomials must be list of expressionsi   t   separateN(   t   gett   Falset   Nonet
   isinstanceR	   t
   point_sortt   verticest	   TypeErrort   hyperplane_parameterst   sidest   lent   ranget   intersectionR   t   NotImplementedErrort   main_integrate3dt   listt   main_integrateR   t   Zerot	   decomposet   TrueR   t   strip(   t   polyt   exprt   kwargsR   R   t	   hp_paramst   facetst   plent   it   intersectionst   lintsR   t   resultt   result_dictt   integral_valuet   monomst   monomt   coefft   m(    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyt   polytope_integrate   sh    !;;

	c         C  sJ   |  t  j k r d S|  j r& |  d f St |  ƒ } | t  |  ƒ | f Sd  S(   Ni    i   (   i    i    (   R   R'   t	   is_numberR   (   R8   R9   (    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyR*   Š   s    	
c   #      C  s  i  } t  t t f } t | ƒ } | r¢t | d ƒ } g  | D]& }	 |	 D] }
 |
 D] } | ^ qQ qG q= } x | D] } d | | d <qp Wxt | ƒ D]\ } } | d | d } } | | | d } xÏ t | ƒ D]Á \ } } | \ }  } } } } } } } | | | } | t j k r(t j } n6 t | | | | |  | | | | | | | | | | ƒ } | | d <| |  c | | t	 | ƒ | | | | 7<qÕ Wq• W| St j } t
 |  ƒ } xÃ | D]» } t j }  d } x‹ t | ƒ D]} \ } }! | | } | d t j k rqàn  t |! | | | | |  | ƒ }" |  |" | d t	 t | d ƒ ƒ 7}  | d 7} qàW|  | | }  | |  7} q¾W| S(   sw  Function to translate the problem of integrating uni/bi/tri-variate
    polynomials over a 3-Polytope to integrating over its faces.
    This is done using Generalized Stokes' Theorem and Euler's Theorem.

    Parameters
    ===========

    expr : The input polynomial
    facets : Faces of the 3-Polytope(expressed as indices of `vertices`)
    vertices : Vertices that constitute the Polytope
    hp_params : Hyperplane Parameters of the facets

    Optional Parameters
    -------------------
    max_degree : Max degree of constituent monomial in given list of polynomial

    Examples
    ========

    >>> from sympy.abc import x, y
    >>> 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
    i   i    i   i   (   R   R   R   R    t   gradient_termst	   enumerateR   R'   t   integration_reduction_dynamict   normR(   t   polygon_integratet   tuple(#   R,   R/   R   R.   R   R4   t   dimst
   dim_lengtht
   grad_termst   z_termst   x_termt   termt	   flat_listt   facet_countt   hpt   at   bt   x0R1   R8   t   x_dt   y_dt   z_dt   z_indext   y_indext   x_indext   _t   degreet   value_over_faceR6   t   polynomialst   degt   poly_contributet   facett   pi(    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyR$   ”   sV    !
	

,		
 c         C  s>  t  t f } t | ƒ } i  } t j } | r‘d d d d g g t | ƒ } x>t | ƒ D]0\ }	 }
 |
 d |
 d } } | |	 j d } xû t | ƒ D]í \ } } | \ } } } } | j | d ƒ } t j } | t j k rì t j } n: | | } t
 | |	 | | | | | | | | | | | ƒ } | | d <| d k	 re| | c | | t | ƒ | | 7<q˜ | | t | ƒ | | | | <q˜ WqY W| St |  ƒ } xš | D]’ } t j } d }	 xb | D]Z }
 t | |	 |
 d |
 d | | | | ƒ } | | |
 d t |
 d ƒ 7} |	 d 7}	 qÀW| | | } | | 7} q¤W| S(   s¹  Function 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

    Optional Parameters:
    --------------------
    max_degree : 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.geometry.polygon import Polygon
    >>> from sympy.geometry.point import Point
    >>> 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
    i    i   i   N(   R   R   R    R   R'   R=   R>   t   pointsR   R   R?   R@   R(   t   integration_reduction(   R,   R/   R.   R   RC   RD   R4   R6   RE   RJ   RK   RL   RM   RN   R1   R8   R:   RO   RP   RU   t   valueRV   t   value_over_boundaryRX   RY   RZ   (    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyR&   ê   sR    		
	

(	 c      
   C  s5  t  | ƒ } | t  j k r" t  j St  j } | |  d } xt t t |  ƒ ƒ D]` }	 | |  |	 | |  |	 d t |  ƒ f }
 | t | |
 | d ƒ t |  |	 |
 | | ƒ 7} qL W| j s#t | t ƒ | d t | t	 ƒ | d t | t
 ƒ | d } | t |  | | | | | | d ƒ 7} n  | | d } | S(   sÔ  Helper 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.abc import x, y
    >>> 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
    i    i   i   (   R   R'   R!   R    t   distance_to_sidet   lineseg_integrateR<   R   R   R   R   RA   (   R[   t   hp_paramt   indexR/   R   R,   RV   R4   RN   R1   t   side(    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyRA   9  s     	*	)c         C  s  | \ } } g  | D]  } d t  | ƒ t | ƒ ^ q } g  t d d ƒ D] } | | | | ^ qI } g  t d d ƒ D] } | | t | ƒ ^ qw } t d | | ƒ } g  t d d ƒ D] } | d | |  | ^ q¹ }	 t g  t d d ƒ D] } |	 | | | ^ qî ƒ }
 |
 S(   sy  Helper 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
    iÿÿÿÿi    i   (   i    i    i    (   R   R@   R!   t   cross_productt   sum(   t   pointt   line_segt   At   x1t   x2R1   t
   rev_normalt   vectort   n_sidet   vectorx0t   dot_product(    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyRa   h  s    -.024c   
      C  sE  | t  j k r t  j St  j } | d } t t g  t d ƒ D]  } | d | | d | ^ q< ƒ ƒ } t | t ƒ rÄ i | d d t 6| d d t 6| d d t	 6}	 | | | j
 |	 ƒ 7} n | | | 7} t | t ƒ | d t | t ƒ | d t | t	 ƒ | d } | t |  | | | | d ƒ 7} | | d } | S(   sÓ  Helper 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
    i    i   i   i   (   R   R'   R@   RB   R!   R   R   R   R   R   t   subsR   Rb   (
   t   polygonRd   Ri   R,   RV   R4   RN   R1   t   distancet	   expr_dict(    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyRb   †  s"    	
	6) c      
   C  sã   | t  j k r | St  j } |  | j d } t |  ƒ }	 t t f }
 t | |
 d ƒ | d t | |
 d ƒ | d } | d k r® | t |  | | | | | | d ƒ 7} n  | t |	 | |  | | |
 ƒ 7} | t | ƒ | d S(   sô  Helper 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.geometry.point import Point
    >>> from sympy.geometry.polygon import 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
    i    i   (	   R   R'   R]   R    R   R   R   R^   t   left_integral2D(   R/   Rd   RL   RM   R,   RC   RV   R_   RN   R:   t   genst   inner_product(    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyR^   ¯  s    	4c         C  sN  t  j } x>t d |  ƒ D]-} d } | | d |  k sM | | d |  k rj t | | | | d ƒ } n  | r t t t d „  | | ƒ ƒ ƒ }	 t | ƒ rFt | t	 ƒ r2t
 | ƒ d k rô i | d | d 6| d | d 6| d | d 6}
 n$ i | d | d 6| d | d 6}
 | |	 | j |
 ƒ 7} qC| |	 | 7} qFq q W| S(   sÀ  Computes 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.geometry.point import Point
    >>> from sympy.geometry.polygon import 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
    i    i   t	   segment2Dc         S  s   |  | S(   N(    (   R   R   (    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyt   <lambda>  t    i   i   (    (   R   R'   R!   R"   R@   RB   t   mapt	   is_vertexR   R   R    Rr   (   R:   Rd   R/   RN   R,   Rw   R_   t   jt	   intersectt   distance_originRu   (    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyRv   ß  s&    	(c         C  s&  t  j } t |  ƒ } | t  j k r( | St | ƒ d k r| j sß | | \ } } } } | d k rw | |	 | d n d } | d k r“ | d n d } | | d | | d } } | | | |
 d | | |
 d 7} n  | t | | |  |
 | | ƒ 7} n|	 } | j sñ| | | | | } } } | d k rV| | d | d | d n d } | d k r‚| | d | | d n d } | d k r²| | d | | d d n d } | | | |
 d | | |
 d | | |
 d 7} n  | t |  | | | | | ƒ 7} | t | ƒ | d S(   sœ  The 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.

    Optional Parameters
    -------------------
    vertices : Coordinates of vertices constituting the 3-Polytope
    hp_param : 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, gradient_terms
    >>> from sympy.geometry.point import Point
    >>> from sympy.geometry.polygon import 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
    i   i    i   i   i   (   R   R'   R    R<   Rv   t   left_integral3D(   R/   Rd   RL   RM   R,   RV   RC   RT   RS   t	   max_indexRN   t   monomial_valuest   monom_indexR   Rc   R_   R:   RU   t   x_degreet   y_degreet   x_valuet   y_valueRR   t   z_degreet   z_value(    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyR?     s0    /		$)"	0,06c      	   C  sœ   t  j } |  | } | | d } xt t t | ƒ ƒ D]` }	 | | |	 | | |	 d t | ƒ f }
 | t | |
 | d ƒ t | |	 |
 | | ƒ 7} q4 W| S(   s0  Computes the left integral of Eq 10 in Chin et al.
    For the 3D case, this is the sum of the integral values over constituting
    line segments of the face (which is accessed by facets[index]) multiplied
    by the distance between the first point of facet and that line segment.

    Parameters
    ===========
    facets : List of faces of the 3-Polytope.
    index : Index of face over which integral is to be calculated.
    expr : Input polynomial
    vertices : List of vertices that constitute the 3-Polytope
    hp_param : The hyperplane parameters of the face
    degree : Degree of the expr

    >>> from sympy.abc import x, y
    >>> 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
    i    i   (   R   R'   R!   R    Ra   Rb   (   R/   Rd   R,   R   Rc   RV   R_   R[   RN   R1   Re   (    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyR   h  s    	
*i    i   c         C  sY  | d k r¨ d } d g t |  d d |  d d ƒ } xt d |  d ƒ D]T } xK t d |  | d ƒ D]2 } t | t | | | d g | | <| d 7} qk WqM Wn­ g  t d |  d ƒ D]“ } g  t d | d ƒ D]s } g  t | | d d ƒ D]P } t | t | t | | | | | | | | | | | | | d g ^ qó ^ qÖ ^ q¼ } | S(   s‚  Returns 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.abc import x, y
    >>> 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]]]]
    i   i    i   i   iÿÿÿÿN(   R   t   intR!   R   R   R   (   t   binomial_powert
   no_of_genst   countt   termst   x_countt   y_countt   z_count(    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyR=   Œ  s    'ªc         C  s  t  |  t ƒ rt |  j ƒ |  j d g } d g t | ƒ d } xÏt t | ƒ d ƒ D]¹ } | | } | | d } | d | d } | d | d } | d | d | d | d } t | | | g ƒ }	 t | ƒ |	 } t | ƒ |	 t | ƒ |	 f }
 |
 | f | | <qZ Wnû d g t |  ƒ } xå t	 |  ƒ D]× \ } } g  | d  D] } | | ^ qQ\ } } } t
 | | | ƒ } t g  t d d ƒ D] } | | | | ^ q•ƒ } t | ƒ } | t j k rÚd } n  g  | D] } | | ^ qá} | | } | | f | | <q:W| S(   s©  A 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.geometry.point import Point
    >>> from sympy.geometry.polygon import 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)]
    i    i   i   N(   R   R	   R%   R   R   R    R!   R   R   R>   Rf   Rg   R'   (   R+   R   t   paramsR1   t   v1t   v2t   a1t   a2RM   t   factorRL   Rs   t   vertext   v3t   normalR~   t   fac(    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyR   ¹  s2    
" *4	
c         C  s½   g  t  d d ƒ D] } | | |  | ^ q } g  t  d d ƒ D] } | | |  | ^ q> } | d | d | d | d | d | d | d | d | d | d | d | d g S(   se   Returns the cross-product of vectors (v2 - v1) and (v3 - v1)
    That is : (v2 - v1) X (v3 - v1)
    i    i   i   i   (   R!   (   R”   R•   Rš   R~   (    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyRf   ò  s
    ..c         C  sá  | j  d \ } } d „  } d „  } t t f } i  }	 x | D] }
 t j |	 |
 <q> Wt | ƒ d k rÏt | ƒ d k rþ |  d t j k rº | | ƒ r­ t j | |  d f S| | f Sqþ |  d t j k rþ | | ƒ rî | |  d t j f S| | f Sqþ n  t | t ƒ rØ| j rûx¼| j	 D]Ô } | j
 rf| j	 d | k rô|	 | j	 d c | j	 d 7<qôq x‹ | j	 D]€ } t | j	 ƒ } | d k r°| | k r°|	 | c d 7<qp| d k rp| j	 d | k rp|	 | j	 d c | j	 d 7<qpqpWq Wqâ| j r•xÎ | j	 D]€ } t | j	 ƒ } | d k rN| | k rN|	 | c d 7<q| d k r| j	 d | k r|	 | j	 d c | j	 d 7<qqWqâ| j
 r¹| j	 d |	 | j	 d <qâ| j râ|	 | c d 7<qân
 | | f St |	 j ƒ  d d „  ƒ}	 |	 d d |	 d d k rt| | ƒ r?t j | |  d f } qÌ| | ƒ re| |  d t j f } qÌ| | f } qÝ| | ƒ rš| |  d t j f } qÝ| | ƒ rÀt j | |  d f } qÝ| | f } n | |  d } | S(   sÙ  Helper method for polytope_integrate. Currently not used in the main
    algorithm.
    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.geometry.line import Segment2D
    >>> from sympy.geometry.point import Point
    >>> l = Segment2D(Point(0, 3), Point(1, 1))
    >>> expr = x**3*y**7
    >>> best_origin((2, 1), 3, l, expr)
    (0, 3.0)
    i    c         S  s›   |  j  \ } } | j t j k r+ t | ƒ S| j t j k rG t | ƒ S| j | j t j k  r“ | j | j | j | j | j | j t j f Sd Sd S(   s–   Returns the point where the input line segment
        intersects the x-axis.
        Parameters
        ==========
        ls : Line segment
        N(    (   R]   R   R   R'   RB   R   (   t   lst   pt   q(    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyt
   x_axis_cut/  s    

3c         S  s›   |  j  \ } } | j t j k r+ t | ƒ S| j t j k rG t | ƒ S| j | j t j k  r“ t j | j | j | j | j | j | j f Sd Sd S(   s–   Returns the point where the input line segment
        intersects the y-axis.
        Parameters
        ==========
        ls : Line segment
        N(    (   R]   R   R   R'   RB   R   (   R   Rž   RŸ   (    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyt
   y_axis_cut@  s    

3i   i   t   keyc         S  s   t  |  d ƒ S(   Ni    (   t   str(   t   k(    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyRz   ƒ  R{   (   R]   R   R   R   R'   R    R   R   t   is_Addt   argst   is_Powt   is_Mult	   is_Symbolt   sortedt   items(   RL   RM   t   linesegR,   R–   t   b1R    R¡   Rw   t
   power_gensR1   t   monomialt
   univariatet	   term_typeRH   RN   (    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyt   best_originý  sp    0				$	(		
c   
      C  sŸ  i  } t  |  t ƒ r{|  j r{|  j r5 |  | d <q…|  j rÞ |  j t ƒ } g  |  j D]! } t t	 | | Œ ƒ | f ^ qW } | r’ d „  | Dƒ Sxã | D]> } | \ } } | j
 | ƒ rÍ | | c | 7<q™ | | | <q™ Wq…|  j r|  j \ } } |  | | <q…d } xb |  j D]W } t | j ƒ }	 |	 d k rJ| j rJ| d 7} q|	 d k r| | j d 7} qqW|  | | <n
 |  | d <| r›t | j ƒ  ƒ S| S(   s[  Decomposes an input polynomial into homogeneous ones of
    smaller or equal degree.
    Returns a dictionary with keys as the degree of the smaller
    constituting polynomials. Values are the constituting polynomials.

    Parameters
    ==========
    expr : Polynomial(SymPy expression)

    Optional Parameters:
    --------------------
    separate : 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}
    i   c         S  s   h  |  ] } | d  ’ q S(   i   (    (   t   .0R8   (    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pys	   <setcomp>»  s   	 i    i   (   R   R   R<   R©   R¥   t   atomsR   R¦   Rg   R   R   R§   R    t   sett   values(
   R,   R   t	   poly_dictt   symbolsR8   t   degreesRV   RH   RU   R±   (    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyR(   —  s<    		.	
c           s[  t  |  t ƒ r |  j n |  } t | ƒ } | d k  r@ t | ƒ S| rR t d ƒ n	 t d ƒ ‰ t | d ƒ } | d k r¸ t t t d „  | ƒ ƒ | t t d „  | ƒ ƒ | ƒ ‰  nT t t t d „  | ƒ ƒ | t t d „  | ƒ ƒ | t t d	 „  | ƒ ƒ | ƒ ‰  ‡  ‡ f d
 †  } ‡  ‡ ‡ f d †  } t	 | d t
 | d k rQ| n | ƒ ƒS(   sU  Returns 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

    Optional Parameters:
    ---------------------

    normal : The normal of the plane which the 3-Polytope is a part of.
    clockwise : Returns points sorted in clockwise order if True and
    anti-clockwise if False.

    Examples
    ========

    >>> from sympy.integrals.intpoly import point_sort
    >>> from sympy.geometry.point import Point
    >>> point_sort([Point(0, 0), Point(1, 0), Point(1, 1)])
    [Point2D(1, 1), Point2D(1, 0), Point2D(0, 0)]
    i   i   iÿÿÿÿi    c         S  s   |  j  S(   N(   R   (   R™   (    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyRz   ü  R{   c         S  s   |  j  S(   N(   R   (   R™   (    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyRz   ý  R{   c         S  s   |  j  S(   N(   R   (   R™   (    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyRz   ÿ  R{   c         S  s   |  j  S(   N(   R   (   R™   (    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyRz      R{   c         S  s   |  j  S(   N(   R   (   R™   (    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyRz     R{   c           sñ  |  j  ˆ  j  t j k r7 | j  ˆ  j  t j k  r7 ˆ S|  j  ˆ  j  t j k  rm | j  ˆ  j  t j k rm ˆ S|  j  ˆ  j  t j k r| j  ˆ  j  t j k r|  j ˆ  j t j k sÑ | j ˆ  j t j k rì |  j | j k rè ˆ Sˆ S| j |  j k rˆ Sˆ S|  j  ˆ  j  | j ˆ  j | j  ˆ  j  |  j ˆ  j } | t j k  rUˆ S| t j k rhˆ S|  j  ˆ  j  |  j  ˆ  j  |  j ˆ  j |  j ˆ  j } | j  ˆ  j  | j  ˆ  j  | j ˆ  j | j ˆ  j } | | k ríˆ Sˆ S(   N(   R   R   R'   R   (   RL   RM   t   dett   firstt   second(   t   centert   order(    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyt   compare  s&    2222c           sq   t  ˆ  |  | ƒ } t g  t d d ƒ D] } | | ˆ | ^ q% ƒ } | t j k  rZ ˆ S| t j k rm ˆ Sd  S(   Ni    i   (   Rf   Rg   R!   R   R'   (   RL   RM   Rº   R1   Rq   (   R½   R›   R¾   (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyt	   compare3d  s    4R¢   (   R   R	   R   R    R%   R   R
   Rg   R|   Rª   R   (   R+   R›   R   t   ptst   nt   dimR¿   RÀ   (    (   R½   R›   R¾   s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyR   ×  s    
"c         C  s×   t  d ƒ d } t |  t t f ƒ rJ t g  |  D] } | d ^ q/ ƒ | St |  t ƒ r¦ t |  t ƒ r‚ |  j d |  j d | S|  j d |  j d |  j	 | Sn- t |  t
 ƒ rÓ t d „  |  j ƒ  Dƒ ƒ | Sd S(   s6  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.geometry.point import Point
    >>> norm(Point(2, 7))
    sqrt(53)
    i   i   c         s  s   |  ] } | d  Vq d S(   i   N(    (   R³   R1   (    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pys	   <genexpr>>  s    N(   R   R   R%   RB   Rg   R
   R   R   R   R   t   dictR¶   (   Rh   t   halft   coord(    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyR@   %  s    %$c         C  s  | d  d k rg| d k rk |  j  d \ } } |  j  d \ } } | j  d \ } } | j  d \ }	 }
 ng | d k rÒ |  j  d \ } } } |  j  d \ } } } | j  d \ } } } | j  d \ }	 }
 } n  | | | |
 | | | |	 } | rg| | | | } | |
 |	 | } t | | |	 | | | ƒ | t | | |
 | | | ƒ | f Sn  | d  d k r| d k r|  d \ } } | d \ } } |  d | d } } | | | | } | r	t | | | | ƒ | t | | | | ƒ | f Sqn  d	 S(
   sx  Returns intersection between geometric objects.

    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.geometry.point import Point
    >>> from sympy.geometry.line import 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)
    iþÿÿÿt   segmentRy   i    i   t	   segment3Dt   planeR   N(   R]   R   (   t   geom_1t   geom_2t   intersection_typeRk   t   y1Rl   t   y2t   x3t   y3t   x4t   y4t   z1t   z2t   z3t   z4t   denomt   t1t   t2t   a1xt   a1yt   a2xt   a2yR­   t   b2(    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyR"   A  s4    "!(c         C  s?   t  |  t ƒ r( t |  ƒ d k r; t Sn t  |  t ƒ r; t St S(   sy  If the input entity is a vertex return True
    Parameter
    =========

    ent : Denotes a geometric entity representing a point

    Examples
    ========

    >>> from sympy.geometry.point 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
    i   i   (   i   i   (   R   RB   R    R)   R
   R   (   t   ent(    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyR}   |  s    c         C  s©   d d l  m } m } t t d „  |  j ƒ ƒ } t t d „  |  j ƒ ƒ } | j |  j d j ƒ | j |  j d j ƒ | | | ƒ } | | d d ƒ} | j	 ƒ  d S(	   s±   Plots the 2D polytope using the functions written in plotting
    module which in turn uses matplotlib backend.

    Parameter
    =========

    poly: Denotes a 2-Polytope
    iÿÿÿÿ(   t   Plott   List2DSeriesc         S  s   |  j  S(   N(   R   (   R™   (    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyRz   ¢  R{   c         S  s   |  j  S(   N(   R   (   R™   (    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyRz   £  R{   i    t   axess   label_axes=TrueN(
   t   sympy.plotting.plotRà   Rá   R%   R|   R   t   appendR   R   t   show(   R+   Rà   Rá   t   xlt   ylt   l2dsRž   (    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyt   plot_polytope—  s    	c         C  sL   d d l  m } m } |  j } t | ƒ d k r> | |  ƒ n
 | |  ƒ d S(   sÂ   Plots the polynomial using the functions written in
    plotting module which in turn uses matplotlib backend.

    Parameter
    =========

    expr: Denotes a polynomial(SymPy expression)
    iÿÿÿÿ(   t   plot3dt   ploti   N(   Rã   Rê   Rë   t   free_symbolsR    (   R,   Rê   Rë   Rw   (    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyt   plot_polynomial­  s
    		N(3   t   __doc__t
   __future__R    R   t	   functoolsR   t
   sympy.coreR   R   R   R   t   sympy.simplify.simplifyR   t   sympy.geometryR   R	   R
   R   t	   sympy.abcR   R   R   t   sympy.polys.polytoolsR   R   R   R   R;   R*   R$   R&   RA   Ra   Rb   R^   Rv   R?   R   R=   R   Rf   R²   R   R(   R)   R   R@   R"   R}   Ré   Rí   (    (    (    s6   lib/python2.7/site-packages/sympy/integrals/intpoly.pyt   <module>   s<   ""m	
VO	/		)	0	5S	$-9		š@N		;		