ó
~9­\c           @  sj  d  Z  d d l m Z m 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 m Z m Z d d l m Z d d l m Z m Z d d	 l m Z e d
 d „ ƒ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z  d „  Z! d „  Z" d „  Z# d „  Z$ d „  Z% d e& f d „  ƒ  YZ' e d e f d „  ƒ  Yƒ Z( d S(   s@   Tools and arithmetics for monomials of distributed polynomials. iÿÿÿÿ(   t   print_functiont   division(   t   combinations_with_replacementt   product(   t   dedent(   t   Mult   St   Tuplet   sympify(   t   exec_t   iterablet   range(   t   ExactQuotientFailed(   t   PicklableWithSlotst   dict_from_expr(   t   publici    c         C  sÍ  | d k  s | | k r t  ƒ  S|  s2 | d k r? t d ƒ h St |  ƒ t d ƒ g }  t d „  |  Dƒ ƒ rg  } x™ t |  | ƒ D]ˆ } t ƒ  } x |  D] } d | | <qš Wx- | D]% } | d k rµ | | c d 7<qµ qµ Wt | j ƒ  ƒ | k r„ | j t	 | Œ  ƒ q„ q„ Wt  | ƒ Sg  } xœ t
 |  d | ƒD]ˆ } t ƒ  } x |  D] } d | | <qIWx- | D]% } | d k rd| | c d 7<qdqdWt | j ƒ  ƒ | k r3| j t	 | Œ  ƒ q3q3Wt  | ƒ Sd S(   s¦  
    Generate a set of monomials of the degree greater than or equal
    to `min_degree` and less than or equal to `max_degree`.

    Given a set of variables `V` and a min_degree `N` and a max_degree `M`
    generate a set of monomials of degree less than or equal to `N` and greater
    than or equal to `M`. The total number of monomials in commutative
    variables is huge and is given by the following formula if `M = 0`:

    .. math::

        \frac{(\#V + N)!}{\#V! N!}

    For example if we would like to generate a dense polynomial of
    a total degree `N = 50` and `M = 0`, which is the worst case, in 5
    variables, assuming that exponents and all of coefficients are 32-bit long
    and stored in an array we would need almost 80 GiB of memory! Fortunately
    most polynomials, that we will encounter, are sparse.

    Examples
    ========

    Consider monomials in commutative variables `x` and `y`
    and non-commutative variables `a` and `b`::

        >>> from sympy import symbols
        >>> from sympy.polys.monomials import itermonomials
        >>> from sympy.polys.orderings import monomial_key
        >>> from sympy.abc import x, y

        >>> sorted(itermonomials([x, y], 2), key=monomial_key('grlex', [y, x]))
        [1, x, y, x**2, x*y, y**2]

        >>> sorted(itermonomials([x, y], 3), key=monomial_key('grlex', [y, x]))
        [1, x, y, x**2, x*y, y**2, x**3, x**2*y, x*y**2, y**3]

        >>> a, b = symbols('a, b', commutative=False)
        >>> itermonomials([a, b, x], 2)
        {1, a, a**2, b, b**2, x, x**2, a*b, b*a, x*a, x*b}

        >>> sorted(itermonomials([x, y], 2, 1), key=monomial_key('grlex', [y, x]))
        [x, y, x**2, x*y, y**2]


    i    i   c         s  s   |  ] } | j  Vq d  S(   N(   t   is_commutative(   t   .0t   variable(    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pys	   <genexpr>C   s    t   repeatN(   t   setR   t   listt   allR   t   dictt   maxt   valuest   appendR   R   (   t	   variablest
   max_degreet
   min_degreet   monomials_list_commt   itemt   powersR   t   monomials_list_non_comm(    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   itermonomials   s8    /	
	c         C  s2   d d l  m } | |  | ƒ | |  ƒ | | ƒ S(   sQ  
    Computes the number of monomials.

    The number of monomials is given by the following formula:

    .. math::

        \frac{(\#V + N)!}{\#V! N!}

    where `N` is a total degree and `V` is a set of variables.

    Examples
    ========

    >>> from sympy.polys.monomials import itermonomials, monomial_count
    >>> from sympy.polys.orderings import monomial_key
    >>> from sympy.abc import x, y

    >>> monomial_count(2, 2)
    6

    >>> M = itermonomials([x, y], 2)

    >>> sorted(M, key=monomial_key('grlex', [y, x]))
    [1, x, y, x**2, x*y, y**2]
    >>> len(M)
    6

    iÿÿÿÿ(   t	   factorial(   t   sympyR#   (   t   Vt   NR#   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   monomial_count\   s    c         C  s0   t  g  t |  | ƒ D] \ } } | | ^ q ƒ S(   s%  
    Multiplication of tuples representing monomials.

    Examples
    ========

    Lets multiply `x**3*y**4*z` with `x*y**2`::

        >>> from sympy.polys.monomials import monomial_mul

        >>> monomial_mul((3, 4, 1), (1, 2, 0))
        (4, 6, 1)

    which gives `x**4*y**5*z`.

    (   t   tuplet   zip(   t   At   Bt   at   b(    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   monomial_mul}   s    c         C  s7   t  |  | ƒ } t d „  | Dƒ ƒ r/ t | ƒ Sd Sd S(   sœ  
    Division of tuples representing monomials.

    Examples
    ========

    Lets divide `x**3*y**4*z` by `x*y**2`::

        >>> from sympy.polys.monomials import monomial_div

        >>> monomial_div((3, 4, 1), (1, 2, 0))
        (2, 2, 1)

    which gives `x**2*y**2*z`. However::

        >>> monomial_div((3, 4, 1), (1, 2, 2)) is None
        True

    `x*y**2*z**2` does not divide `x**3*y**4*z`.

    c         s  s   |  ] } | d  k Vq d S(   i    N(    (   R   t   c(    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pys	   <genexpr>¨   s    N(   t   monomial_ldivR   R(   t   None(   R*   R+   t   C(    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   monomial_div   s    
c         C  s0   t  g  t |  | ƒ D] \ } } | | ^ q ƒ S(   s…  
    Division of tuples representing monomials.

    Examples
    ========

    Lets divide `x**3*y**4*z` by `x*y**2`::

        >>> from sympy.polys.monomials import monomial_ldiv

        >>> monomial_ldiv((3, 4, 1), (1, 2, 0))
        (2, 2, 1)

    which gives `x**2*y**2*z`.

        >>> monomial_ldiv((3, 4, 1), (1, 2, 2))
        (2, 2, -1)

    which gives `x**2*y**2*z**-1`.

    (   R(   R)   (   R*   R+   R,   R-   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyR0   ­   s    c         C  s!   t  g  |  D] } | | ^ q
 ƒ S(   s%   Return the n-th pow of the monomial. (   R(   (   R*   t   nR,   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   monomial_powÅ   s    c         C  s5   t  g  t |  | ƒ D] \ } } t | | ƒ ^ q ƒ S(   s.  
    Greatest common divisor of tuples representing monomials.

    Examples
    ========

    Lets compute GCD of `x*y**4*z` and `x**3*y**2`::

        >>> from sympy.polys.monomials import monomial_gcd

        >>> monomial_gcd((1, 4, 1), (3, 2, 0))
        (1, 2, 0)

    which gives `x*y**2`.

    (   R(   R)   t   min(   R*   R+   R,   R-   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   monomial_gcdÉ   s    c         C  s5   t  g  t |  | ƒ D] \ } } t | | ƒ ^ q ƒ S(   s1  
    Least common multiple of tuples representing monomials.

    Examples
    ========

    Lets compute LCM of `x*y**4*z` and `x**3*y**2`::

        >>> from sympy.polys.monomials import monomial_lcm

        >>> monomial_lcm((1, 4, 1), (3, 2, 0))
        (3, 4, 1)

    which gives `x**3*y**4*z`.

    (   R(   R)   R   (   R*   R+   R,   R-   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   monomial_lcmÜ   s    c         C  s   t  d „  t |  | ƒ Dƒ ƒ S(   sö   
    Does there exist a monomial X such that XA == B?

    Examples
    ========

    >>> from sympy.polys.monomials import monomial_divides
    >>> monomial_divides((1, 2), (3, 4))
    True
    >>> monomial_divides((1, 2), (0, 2))
    False
    c         s  s!   |  ] \ } } | | k Vq d  S(   N(    (   R   R,   R-   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pys	   <genexpr>ü   s    (   R   R)   (   R*   R+   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   monomial_dividesï   s    c          G  sc   t  |  d ƒ } xF |  d D]: } x1 t | ƒ D]# \ } } t | | | ƒ | | <q. Wq Wt | ƒ S(   s‘  
    Returns maximal degree for each variable in a set of monomials.

    Examples
    ========

    Consider monomials `x**3*y**4*z**5`, `y**5*z` and `x**6*y**3*z**9`.
    We wish to find out what is the maximal degree for each of `x`, `y`
    and `z` variables::

        >>> from sympy.polys.monomials import monomial_max

        >>> monomial_max((3,4,5), (0,5,1), (6,3,9))
        (6, 5, 9)

    i    i   (   R   t	   enumerateR   R(   (   t   monomst   MR&   t   iR4   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   monomial_maxþ   s
    c          G  sc   t  |  d ƒ } xF |  d D]: } x1 t | ƒ D]# \ } } t | | | ƒ | | <q. Wq Wt | ƒ S(   s‘  
    Returns minimal degree for each variable in a set of monomials.

    Examples
    ========

    Consider monomials `x**3*y**4*z**5`, `y**5*z` and `x**6*y**3*z**9`.
    We wish to find out what is the minimal degree for each of `x`, `y`
    and `z` variables::

        >>> from sympy.polys.monomials import monomial_min

        >>> monomial_min((3,4,5), (0,5,1), (6,3,9))
        (0, 3, 1)

    i    i   (   R   R:   R6   R(   (   R;   R<   R&   R=   R4   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   monomial_min  s
    c         C  s
   t  |  ƒ S(   sÍ   
    Returns the total degree of a monomial.

    Examples
    ========

    The total degree of `xy^2` is 3:

    >>> from sympy.polys.monomials import monomial_deg
    >>> monomial_deg((1, 2))
    3
    (   t   sum(   R<   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   monomial_deg0  s    c         C  s   |  \ } } | \ } } t  | | ƒ } | j rY | d k	 rR | | j | | ƒ f Sd Sn0 | d k pl | | s… | | j | | ƒ f Sd Sd S(   s,   Division of two terms in over a ring/field. N(   R3   t   is_FieldR1   t   quo(   R,   R-   t   domaint   a_lmt   a_lct   b_lmt   b_lct   monom(    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   term_div?  s    	t   MonomialOpsc           B  sh   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z RS(   s6   Code generator of fast monomial arithmetic functions. c         C  s   | |  _  d  S(   N(   t   ngens(   t   selfRL   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   __init__T  s    c         C  s   i  } t  | | ƒ | | S(   N(   R	   (   RM   t   codet   namet   ns(    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   _buildW  s    c         C  s*   g  t  |  j ƒ D] } d | | f ^ q S(   Ns   %s%s(   R   RL   (   RM   RP   R=   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   _vars\  s    c   	      C  s²   d } t  d ƒ } |  j d ƒ } |  j d ƒ } g  t | | ƒ D] \ } } d | | f ^ q@ } | t d | d d j | ƒ d	 d j | ƒ d
 d j | ƒ ƒ } |  j | | ƒ S(   NR.   ss           def %(name)s(A, B):
            (%(A)s,) = A
            (%(B)s,) = B
            return (%(AB)s,)
        R,   R-   s   %s + %sRP   R*   s   , R+   t   AB(   R   RS   R)   R   t   joinRR   (	   RM   RP   t   templateR*   R+   R,   R-   RT   RO   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   mul_  s    	2@c      	   C  s   d } t  d ƒ } |  j d ƒ } g  | D] } d | ^ q( } | t d | d d j | ƒ d d j | ƒ ƒ } |  j | | ƒ S(	   NR5   sZ           def %(name)s(A, k):
            (%(A)s,) = A
            return (%(Ak)s,)
        R,   s   %s*kRP   R*   s   , t   Ak(   R   RS   R   RU   RR   (   RM   RP   RV   R*   R,   RX   RO   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   powm  s    	1c   	      C  s²   d } t  d ƒ } |  j d ƒ } |  j d ƒ } g  t | | ƒ D] \ } } d | | f ^ q@ } | t d | d d j | ƒ d	 d j | ƒ d
 d j | ƒ ƒ } |  j | | ƒ S(   Nt   monomial_mulpowsw           def %(name)s(A, B, k):
            (%(A)s,) = A
            (%(B)s,) = B
            return (%(ABk)s,)
        R,   R-   s	   %s + %s*kRP   R*   s   , R+   t   ABk(   R   RS   R)   R   RU   RR   (	   RM   RP   RV   R*   R+   R,   R-   R[   RO   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   mulpowy  s    	2@c   	      C  s²   d } t  d ƒ } |  j d ƒ } |  j d ƒ } g  t | | ƒ D] \ } } d | | f ^ q@ } | t d | d d j | ƒ d	 d j | ƒ d
 d j | ƒ ƒ } |  j | | ƒ S(   NR0   ss           def %(name)s(A, B):
            (%(A)s,) = A
            (%(B)s,) = B
            return (%(AB)s,)
        R,   R-   s   %s - %sRP   R*   s   , R+   RT   (   R   RS   R)   R   RU   RR   (	   RM   RP   RV   R*   R+   R,   R-   RT   RO   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   ldiv‡  s    	2@c   	      C  sÍ   d } t  d ƒ } |  j d ƒ } |  j d ƒ } g  t |  j ƒ D] } d t d | ƒ ^ q@ } |  j d ƒ } | t d | d	 d
 j | ƒ d d
 j | ƒ d d j | ƒ d d
 j | ƒ ƒ } |  j | | ƒ S(   NR3   s†           def %(name)s(A, B):
            (%(A)s,) = A
            (%(B)s,) = B
            %(RAB)s
            return (%(R)s,)
        R,   R-   s7   r%(i)s = a%(i)s - b%(i)s
    if r%(i)s < 0: return NoneR=   t   rRP   R*   s   , R+   t   RABs   
    t   R(   R   RS   R   RL   R   RU   RR   (	   RM   RP   RV   R*   R+   R=   R_   R`   RO   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   div•  s    	/Oc   	      C  s¸   d } t  d ƒ } |  j d ƒ } |  j d ƒ } g  t | | ƒ D]" \ } } d | | | | f ^ q@ } | t d | d d j | ƒ d	 d j | ƒ d
 d j | ƒ ƒ } |  j | | ƒ S(   NR8   ss           def %(name)s(A, B):
            (%(A)s,) = A
            (%(B)s,) = B
            return (%(AB)s,)
        R,   R-   s   %s if %s >= %s else %sRP   R*   s   , R+   RT   (   R   RS   R)   R   RU   RR   (	   RM   RP   RV   R*   R+   R,   R-   RT   RO   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   lcm¥  s    	8@c   	      C  s¸   d } t  d ƒ } |  j d ƒ } |  j d ƒ } g  t | | ƒ D]" \ } } d | | | | f ^ q@ } | t d | d d j | ƒ d	 d j | ƒ d
 d j | ƒ ƒ } |  j | | ƒ S(   NR7   ss           def %(name)s(A, B):
            (%(A)s,) = A
            (%(B)s,) = B
            return (%(AB)s,)
        R,   R-   s   %s if %s <= %s else %sRP   R*   s   , R+   RT   (   R   RS   R)   R   RU   RR   (	   RM   RP   RV   R*   R+   R,   R-   RT   RO   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   gcd³  s    	8@(   t   __name__t
   __module__t   __doc__RN   RR   RS   RW   RY   R\   R]   Ra   Rb   Rc   (    (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyRK   Q  s   									t   Monomialc           B  s±   e  Z d  Z d d g Z d d „ Z d d „ Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z e Z Z d „  Z d „  Z d „  Z RS(   s9   Class representing a monomial, i.e. a product of powers. t	   exponentst   gensc         C  s©   t  | ƒ s„ t t | ƒ d | ƒ\ } } t | ƒ d k rq t | j ƒ  ƒ d d k rq t | j ƒ  ƒ d } q„ t d | ƒ ‚ n  t t	 t
 | ƒ ƒ |  _ | |  _ d  S(   NRi   i   i    s   Expected a monomial got %s(   R
   R   R   t   lenR   R   t   keyst
   ValueErrorR(   t   mapt   intRh   Ri   (   RM   RI   Ri   t   rep(    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyRN   Ç  s    .c         C  s   |  j  | | p |  j ƒ S(   N(   t	   __class__Ri   (   RM   Rh   Ri   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   rebuildÒ  s    c         C  s   t  |  j ƒ S(   N(   Rj   Rh   (   RM   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   __len__Õ  s    c         C  s   t  |  j ƒ S(   N(   t   iterRh   (   RM   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   __iter__Ø  s    c         C  s   |  j  | S(   N(   Rh   (   RM   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   __getitem__Û  s    c         C  s   t  |  j j |  j |  j f ƒ S(   N(   t   hashRp   Rd   Rh   Ri   (   RM   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   __hash__Þ  s    c         C  sc   |  j  rH d j g  t |  j  |  j ƒ D] \ } } d | | f ^ q% ƒ Sd |  j j |  j f Sd  S(   Nt   *s   %s**%ss   %s(%s)(   Ri   RU   R)   Rh   Rp   Rd   (   RM   t   gent   exp(    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   __str__á  s    	?c         G  s[   | p |  j  } | s( t d |  ƒ ‚ n  t g  t | |  j ƒ D] \ } } | | ^ q> Œ  S(   s3   Convert a monomial instance to a SymPy expression. s4   can't convert %s to an expression without generators(   Ri   Rl   R   R)   Rh   (   RM   Ri   Ry   Rz   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   as_exprç  s
    c         C  sJ   t  | t ƒ r | j } n" t  | t t f ƒ r9 | } n t S|  j | k S(   N(   t
   isinstanceRg   Rh   R(   R   t   False(   RM   t   otherRh   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   __eq__ñ  s    	c         C  s   |  | k S(   N(    (   RM   R   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   __ne__û  s    c         C  sV   t  | t ƒ r | j } n" t  | t t f ƒ r9 | } n t S|  j t |  j | ƒ ƒ S(   N(   R}   Rg   Rh   R(   R   t   NotImplementedErrorRq   R.   (   RM   R   Rh   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   __mul__þ  s    	c         C  s   t  | t ƒ r | j } n" t  | t t f ƒ r9 | } n t St |  j | ƒ } | d  k	 rh |  j | ƒ St	 |  t | ƒ ƒ ‚ d  S(   N(
   R}   Rg   Rh   R(   R   R‚   R3   R1   Rq   R   (   RM   R   Rh   t   result(    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   __div__  s    	c         C  sŽ   t  | ƒ } | s, |  j d g t |  ƒ ƒ S| d k rz |  j } x) t d | ƒ D] } t | |  j ƒ } qQ W|  j | ƒ St d | ƒ ‚ d  S(   Ni    i   s'   a non-negative integer expected, got %s(   Rn   Rq   Rj   Rh   R   R.   Rl   (   RM   R   R4   Rh   R=   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   __pow__  s    	c         C  sb   t  | t ƒ r | j } n. t  | t t f ƒ r9 | } n t d | ƒ ‚ |  j t |  j | ƒ ƒ S(   s&   Greatest common divisor of monomials. s.   an instance of Monomial class expected, got %s(   R}   Rg   Rh   R(   R   t	   TypeErrorRq   R7   (   RM   R   Rh   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyRc   (  s    	c         C  sb   t  | t ƒ r | j } n. t  | t t f ƒ r9 | } n t d | ƒ ‚ |  j t |  j | ƒ ƒ S(   s$   Least common multiple of monomials. s.   an instance of Monomial class expected, got %s(   R}   Rg   Rh   R(   R   R‡   Rq   R8   (   RM   R   Rh   (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyRb   4  s    	N(   Rd   Re   Rf   t	   __slots__R1   RN   Rq   Rr   Rt   Ru   Rw   R{   R|   R€   R   Rƒ   R…   t   __floordiv__t   __truediv__R†   Rc   Rb   (    (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyRg   Á  s$   						
	
		
	
		N()   Rf   t
   __future__R    R   t	   itertoolsR   R   t   textwrapR   t
   sympy.coreR   R   R   R   t   sympy.core.compatibilityR	   R
   R   t   sympy.polys.polyerrorsR   t   sympy.polys.polyutilsR   R   t   sympy.utilitiesR   R"   R'   R.   R3   R0   R5   R7   R8   R9   R>   R?   RA   RJ   t   objectRK   Rg   (    (    (    s4   lib/python2.7/site-packages/sympy/polys/monomials.pyt   <module>   s2   "M	!											p