ó
¡¼™\c           @  sÌ   d  d l  m Z m Z d  d l m Z d  d l m Z m Z m Z d  d l	 m
 Z
 d  d l m Z m Z m Z m Z m Z d  d l m Z m Z d  d l m Z m Z d  d l m Z d	 „  Z d
 „  Z d S(   iÿÿÿÿ(   t   print_functiont   division(   t   combinations_with_replacement(   t   symbolst   Addt   Dummy(   t   Rational(   t   cancelt   ComputationFailedt   parallel_poly_from_exprt   reducedt   Poly(   t   Monomialt   monomial_div(   t   DomainErrort   PolificationFailed(   t   debugc         C  sq   t  |  ƒ j ƒ  \ } } y( t | | g d t d t ƒ\ } } Wn t k
 rX | | SXt | Œ  t  | | ƒ S(   s×   
    Put an expression over a common denominator, cancel and reduce.

    Examples
    ========

    >>> from sympy import ratsimp
    >>> from sympy.abc import x, y
    >>> ratsimp(1/x + 1/y)
    (x + y)/(x*y)
    t   fieldt   expand(   R   t   as_numer_denomR
   t   Truet   FalseR   R   (   t   exprt   ft   gt   Qt   r(    (    s5   lib/python2.7/site-packages/sympy/simplify/ratsimp.pyt   ratsimp   s    (	c      	     sâ  d d l  m ‰ | j d t ƒ ‰ | j d t ƒ } t d |  ƒ t |  ƒ j ƒ  \ } } y& t | | g ˆ  | | Ž \ } ‰ Wn t	 k
 r“ |  SXˆ j
 } | j r¸ | j ƒ  ˆ _
 n t d | ƒ ‚ g  | d D] }	 |	 j ˆ j ƒ ^ qÓ ‰ t ƒ  ‰ ‡ ‡ ‡ f d †  ‰ d	 d	 ‡  ‡ ‡ ‡ ‡ ‡ ‡ f d
 † ‰ t | ˆ  ˆ j d ˆ j ƒd } t | ˆ  ˆ j d ˆ j ƒd } | rŽ| | j ƒ  Sˆ t | ˆ j d ˆ j
 ƒt | ˆ j d ˆ j
 ƒg  ƒ \ }
 } } ˆ ru| rut d t | ƒ ƒ g  } xZ | D]R \ } } } } ˆ | | d t d t ƒ} | j | j | ƒ | j | ƒ f ƒ qWt | d d „  ƒ\ }
 } n  | j sÀ|
 j d t ƒ \ } }
 | j d t ƒ \ } } t | | ƒ } n t d ƒ } |
 | j | | j S(   s¾  
    Simplifies a rational expression ``expr`` modulo the prime ideal
    generated by ``G``.  ``G`` should be a Groebner basis of the
    ideal.

    >>> from sympy.simplify.ratsimp import ratsimpmodprime
    >>> from sympy.abc import x, y
    >>> eq = (x + y**5 + y)/(x - y)
    >>> ratsimpmodprime(eq, [x*y**5 - x - y], x, y, order='lex')
    (x**2 + x*y + x + y)/(x**2 - x*y)

    If ``polynomial`` is False, the algorithm computes a rational
    simplification which minimizes the sum of the total degrees of
    the numerator and the denominator.

    If ``polynomial`` is True, this function just brings numerator and
    denominator into a canonical form. This is much faster, but has
    potentially worse results.

    References
    ==========

    .. [1] M. Monagan, R. Pearce, Rational Simplification Modulo a Polynomial
    Ideal,
    http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.163.6984
    (specifically, the second algorithm)
    iÿÿÿÿ(   t   solvet   quickt
   polynomialt   ratsimpmodprimes-   can't compute rational simplification over %si   c           sî   |  d k r d g Sg  } x› t  t t ˆ j ƒ ƒ |  ƒ D]{ } d g t ˆ j ƒ } x | D] } | | c d 7<q[ Wt g  ˆ  D] } t | | ƒ d k ^ q ƒ r8 | j | ƒ q8 q8 Wg  | D] } t | ƒ j	 ˆ j Œ  ^ q¾ ˆ |  d ƒ S(   s‹   
        Compute all monomials with degree less than ``n`` that are
        not divisible by any element of ``leading_monomials``.
        i    i   N(
   R   t   ranget   lent   genst   allR   t   Nonet   appendR   t   as_expr(   t   nt   St   mit   mt   it   lmgt   s(   t   leading_monomialst   optt	   staircase(    s5   lib/python2.7/site-packages/sympy/simplify/ratsimp.pyR0   X   s    %(i    c      	     sÈ  |  | } } d } |  j  ƒ  | j  ƒ  } ˆ r< | d }	 n | }	 x| | |	 k ra| | f ˆ k rk Pn  ˆ j | | f ƒ ˆ | ƒ }
 ˆ | ƒ } t d | | |
 | f ƒ t d t |
 ƒ d t ƒ} t d t | ƒ d t ƒ} | | } t t g  t t |
 ƒ ƒ D] } | | |
 | ^ qƒ ˆ j	 | ƒ } t t g  t t | ƒ ƒ D] } | | | | ^ qRƒ ˆ j	 | ƒ } t
 |  | | | ˆ  ˆ j	 | d ˆ j d t ƒd } t | d	 ˆ j	 ƒj ƒ  } ˆ | | | d
 t d t ƒ} | r@t g  | j ƒ  D] } | d k ^ qƒ r@| j | ƒ } | j | ƒ } | j t t t | | d g t | ƒ t | ƒ ƒ ƒ ƒ ƒ } | j t t t | | d g t | ƒ t | ƒ ƒ ƒ ƒ ƒ } t | ˆ j	 ƒ } t | ˆ j	 ƒ } | d k rÿt d ƒ ‚ n  | j | | | | | f ƒ | | | k r<| d g } n  Pn  | d 7} | d 7} | d 7} qE W| d k r»ˆ | | | | | | ƒ \ } } } ˆ | | | | | | ƒ \ } } } n  | | | f S(   sB  
        Computes a rational simplification of ``a/b`` which minimizes
        the sum of the total degrees of the numerator and the denominator.

        The algorithm proceeds by looking at ``a * d - b * c`` modulo
        the ideal generated by ``G`` for some ``c`` and ``d`` with degree
        less than ``a`` and ``b`` respectively.
        The coefficients of ``c`` and ``d`` are indeterminates and thus
        the coefficients of the normalform of ``a * d - b * c`` are
        linear polynomials in these indeterminates.
        If these linear polynomials, considered as system of
        equations, have a nontrivial solution, then `\frac{a}{b}
        \equiv \frac{c}{d}` modulo the ideal generated by ``G``. So,
        by construction, the degree of ``c`` and ``d`` is less than
        the degree of ``a`` and ``b``, so a simpler representation
        has been found.
        After a simpler representation has been found, the algorithm
        tries to reduce the degree of the numerator and denominator
        and returns the result afterwards.

        As an extension, if quick=False, we look at all possible degrees such
        that the total degree is less than *or equal to* the best current
        solution. We retain a list of all solutions of minimal degree, and try
        to find the best one at the end.
        i    i   s   %s / %s: %s, %ss   c:%dt   clss   d:%dt   ordert   polysR"   t
   particularR   s   Ideal not prime?iÿÿÿÿ(   t   total_degreet   addR   R   R!   R   R   t   sumR    R"   R
   R2   R   t   coeffsR#   t   valuest   subst   dictt   listt   zipt
   ValueErrorR%   (   t   at   bt   allsolt   Nt   Dt   ct   dt   stepst   maxdegt   boundt   M1t   M2t   Cst   Dst   ngR+   t   c_hatt   d_hatR   R(   t   solR-   (   t   Gt   _ratsimpmodprimeR/   R   R   R0   t   tested(    s5   lib/python2.7/site-packages/sympy/simplify/ratsimp.pyRR   j   sX    
DD"2??

%(R2   i   t   domains*   Looking for best minimal solution. Got: %sR4   t   keyc         S  s(   t  |  d j ƒ  ƒ t  |  d j ƒ  ƒ S(   Ni    i   (   R!   t   terms(   t   x(    (    s5   lib/python2.7/site-packages/sympy/simplify/ratsimp.pyt   <lambda>Ó   t    t   convert(   t   sympyR   t   popR   R   R   R   R   R	   R   RT   t   has_assoc_Fieldt	   get_fieldR   t   LMR2   t   setR
   R"   R   R!   R%   R:   t   mint   is_Fieldt   clear_denomsR   t   qt   p(   R   RQ   R"   t   argsR   t   numt   denomR3   RT   R   RD   RE   RA   t   newsolRN   RO   R(   RM   RP   t   cnt   dnR   (    (   RQ   RR   R.   R/   R   R   R0   RS   s5   lib/python2.7/site-packages/sympy/simplify/ratsimp.pyR   !   sJ    &		)	'[""B)	N(   t
   __future__R    R   t	   itertoolsR   t
   sympy.coreR   R   R   t   sympy.core.numbersR   t   sympy.polysR   R   R	   R
   R   t   sympy.polys.monomialsR   R   t   sympy.polys.polyerrorsR   R   t   sympy.utilities.miscR   R   R   (    (    (    s5   lib/python2.7/site-packages/sympy/simplify/ratsimp.pyt   <module>   s   (	