ó
¡¼™\c           @  sÈ   d  Z  d d l m Z m Z d d l m Z m Z m Z m Z m	 Z	 m
 Z
 m Z m Z m Z m Z m Z d d l m Z m Z m Z d d l m Z d „  Z d „  Z d d „ Z d	 „  Z d
 „  Z d S(   sA   This module implements tools for integrating rational functions. iÿÿÿÿ(   t   print_functiont   division(   t   St   Symbolt   symbolst   It   logt   atant   rootst   RootSumt   Lambdat   cancelt   Dummy(   t   Polyt	   resultantt   ZZ(   t   rangec      	   K  s1  t  |  ƒ t k	 r' |  j ƒ  \ } } n |  \ } } t | | d t d t ƒt | | d t d t ƒ} } | j | ƒ \ } } } | j | ƒ \ } } | j | ƒ j	 ƒ  } | j
 r½ | | St | | | ƒ \ } }	 |	 j ƒ  \ }
 } t |
 | ƒ }
 t | | ƒ } |
 j | ƒ \ } } | | | j | ƒ j	 ƒ  7} | j
 s)| j d d ƒ } t | t ƒ spt | ƒ } n | j ƒ  } t | | | | ƒ } | j d ƒ } | d k r#t  |  ƒ t k	 rÍ|  j ƒ  } n" |  \ } } | j ƒ  | j ƒ  B} x1 | | h D] } | j sýt } PqýqýWt } n  t d ƒ } | s“xä | D]P \ }	 } |	 j ƒ  \ } }	 | t | t | | t |	 j	 ƒ  ƒ ƒ d t ƒ7} q<Wn‰ x† | D]~ \ }	 } |	 j ƒ  \ } }	 t |	 | | | ƒ } | d k	 ræ| | 7} qš| t | t | | t |	 j	 ƒ  ƒ ƒ d t ƒ7} qšW| | 7} n  | | S(	   s  Performs indefinite integration of rational functions.

       Given a field :math:`K` and a rational function :math:`f = p/q`,
       where :math:`p` and :math:`q` are polynomials in :math:`K[x]`,
       returns a function :math:`g` such that :math:`f = g'`.

       >>> from sympy.integrals.rationaltools import ratint
       >>> from sympy.abc import x

       >>> ratint(36/(x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2), x)
       (12*x + 6)/(x**2 - 1) + 4*log(x - 2) - 4*log(x + 1)

       References
       ==========

       .. [Bro05] M. Bronstein, Symbolic Integration I: Transcendental
          Functions, Second Edition, Springer-Verlag, 2005, pp. 35-70

       See Also
       ========

       sympy.integrals.integrals.Integral.doit
       ratint_logpart, ratint_ratpart
    t	   compositet   fieldt   symbolt   tt   reali    t	   quadraticN(   t   typet   tuplet   as_numer_denomR   t   Falset   TrueR   t   divt	   integratet   as_exprt   is_zerot   ratint_ratpartt   gett
   isinstanceR   R   t   as_dummyt   ratint_logpartt   Nonet   atomst   is_realR   t	   primitiveR	   R
   R   t   log_to_real(   t   ft   xt   flagst   pt   qt   coefft   polyt   resultt   gt   ht   Pt   Qt   rR   R   t   LR   R&   t   eltt   epst   _t   R(    (    s<   lib/python2.7/site-packages/sympy/integrals/rationaltools.pyt   ratint   s^    7				30c         C  s¾  d d l  m } t |  | ƒ }  t | | ƒ } | j | j ƒ  ƒ \ } } } | j ƒ  } | j ƒ  } g  t d | ƒ D]  }	 t d t | |	 ƒ ƒ ^ qt }
 g  t d | ƒ D]  }	 t d t | |	 ƒ ƒ ^ qª } |
 | } t |
 | d t	 | ƒ} t | | d t	 | ƒ} |  | j ƒ  | | | j ƒ  | j
 | ƒ | | } | | j ƒ  | ƒ } | j ƒ  j | ƒ } | j ƒ  j | ƒ } t | | j ƒ  | ƒ } t | | j ƒ  | ƒ } | | f S(   sŠ  
    Horowitz-Ostrogradsky algorithm.

    Given a field K and polynomials f and g in K[x], such that f and g
    are coprime and deg(f) < deg(g), returns fractions A and B in K(x),
    such that f/g = A' + B and B has square-free denominator.

    Examples
    ========

        >>> from sympy.integrals.rationaltools import ratint_ratpart
        >>> from sympy.abc import x, y
        >>> from sympy import Poly
        >>> ratint_ratpart(Poly(1, x, domain='ZZ'),
        ... Poly(x + 1, x, domain='ZZ'), x)
        (0, 1/(x + 1))
        >>> ratint_ratpart(Poly(1, x, domain='EX'),
        ... Poly(x**2 + y**2, x, domain='EX'), x)
        (0, 1/(x**2 + y**2))
        >>> ratint_ratpart(Poly(36, x, domain='ZZ'),
        ... Poly(x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2, x, domain='ZZ'), x)
        ((12*x + 6)/(x**2 - 1), 12/(x**2 - x - 2))

    See Also
    ========

    ratint, ratint_logpart
    iÿÿÿÿ(   t   solvei    t   at   bt   domain(   t   sympyR=   R   t	   cofactorst   difft   degreeR   R   t   strR   t   quot   coeffsR   t   subsR   (   R*   R2   R+   R=   t   ut   vR:   t   nt   mt   it   A_coeffst   B_coeffst   C_coeffst   At   Bt   HR1   t   rat_partt   log_part(    (    s<   lib/python2.7/site-packages/sympy/integrals/rationaltools.pyR    p   s$    66
7c         C  so  t  |  | ƒ t  | | ƒ }  } | p. t d ƒ } | |  | j ƒ  t  | | ƒ } } t | | d t ƒ\ } } t  | | d t ƒ} | s¡ t d | | f ƒ ‚ i  g  } }	 x | D] }
 |
 | |
 j ƒ  <qµ Wd „  } | j ƒ  \ } } | | | ƒ xq| D]i\ } } | j	 ƒ  \ } } | j ƒ  | k rD|	 j
 | | f ƒ qþ | | } t  | j ƒ  | d t ƒ} | j d t ƒ \ } } | | | ƒ x9 | D]1 \ } } | j t  | j | ƒ | | ƒ ƒ } q•W| j | ƒ t d ƒ g } } x> | j ƒ  d D], } | | j | ƒ } | j
 | j ƒ  ƒ qúWt  t t t | j ƒ  | ƒ ƒ ƒ | ƒ } |	 j
 | | f ƒ qþ W|	 S(	   sw  
    Lazard-Rioboo-Trager algorithm.

    Given a field K and polynomials f and g in K[x], such that f and g
    are coprime, deg(f) < deg(g) and g is square-free, returns a list
    of tuples (s_i, q_i) of polynomials, for i = 1..n, such that s_i
    in K[t, x] and q_i in K[t], and:
                           ___    ___
                 d  f   d  \  `   \  `
                 -- - = --  )      )   a log(s_i(a, x))
                 dx g   dx /__,   /__,
                          i=1..n a | q_i(a) = 0

    Examples
    ========

        >>> from sympy.integrals.rationaltools import ratint_logpart
        >>> from sympy.abc import x
        >>> from sympy import Poly
        >>> ratint_logpart(Poly(1, x, domain='ZZ'),
        ... Poly(x**2 + x + 1, x, domain='ZZ'), x)
        [(Poly(x + 3*_t/2 + 1/2, x, domain='QQ[_t]'),
        ...Poly(3*_t**2 + 1, _t, domain='ZZ'))]
        >>> ratint_logpart(Poly(12, x, domain='ZZ'),
        ... Poly(x**2 - x - 2, x, domain='ZZ'), x)
        [(Poly(x - 3*_t/8 - 1/2, x, domain='QQ[_t]'),
        ...Poly(-_t**2 + 16, _t, domain='ZZ'))]

    See Also
    ========

    ratint, ratint_ratpart
    R   t
   includePRSR   s$   BUG: resultant(%s, %s) can't be zeroc         S  s=   |  d k  t  k r9 | d \ } } | |  | f | d <n  d  S(   Ni    (   R   (   t   ct   sqfR3   t   k(    (    s<   lib/python2.7/site-packages/sympy/integrals/rationaltools.pyt   _include_signÝ   s    R   t   alli   (   R   R   RC   R   R   R   t   AssertionErrorRD   t   sqf_listR(   t   appendt   LCRF   t   gcdt   invertR   RG   t   remR   t   dictt   listt   zipt   monoms(   R*   R2   R+   R   R>   R?   t   resR;   t   R_mapRS   R6   RZ   t   Ct   res_sqfR.   RM   R:   R3   t   h_lcRW   t   h_lc_sqft   jt   invRG   R/   t   T(    (    s<   lib/python2.7/site-packages/sympy/integrals/rationaltools.pyR$   ¬   s:    "$	
)*c   	      C  sÒ   |  j  ƒ  | j  ƒ  k  r) | |  }  } n  |  j ƒ  }  | j ƒ  } |  j | ƒ \ } } | j rs d t | j ƒ  ƒ S| j |  ƒ \ } } } |  | | | j | ƒ } d t | j ƒ  ƒ } | t | | ƒ Sd S(   s  
    Convert complex logarithms to real arctangents.

    Given a real field K and polynomials f and g in K[x], with g != 0,
    returns a sum h of arctangents of polynomials in K[x], such that:

                   dh   d         f + I g
                   -- = -- I log( ------- )
                   dx   dx        f - I g

    Examples
    ========

        >>> from sympy.integrals.rationaltools import log_to_atan
        >>> from sympy.abc import x
        >>> from sympy import Poly, sqrt, S
        >>> log_to_atan(Poly(x, x, domain='ZZ'), Poly(1, x, domain='ZZ'))
        2*atan(x)
        >>> log_to_atan(Poly(x + S(1)/2, x, domain='QQ'),
        ... Poly(sqrt(3)/2, x, domain='EX'))
        2*atan(2*sqrt(3)*x/3 + sqrt(3)/3)

    See Also
    ========

    log_to_real
    i   N(	   RD   t   to_fieldR   R   R   R   t   gcdexRF   t   log_to_atan(	   R*   R2   R-   R.   t   sR   R3   RI   RQ   (    (    s<   lib/python2.7/site-packages/sympy/integrals/rationaltools.pyRr     s    	c         C  sƒ  d d l  m } t d d t ƒ\ } } |  j ƒ  j i | t | | 6ƒ j ƒ  } | j ƒ  j i | t | | 6ƒ j ƒ  } | | t d t ƒ}	 | | t d t ƒ}
 |	 j	 t
 d ƒ t
 d ƒ ƒ |	 j	 t t
 d ƒ ƒ } } |
 j	 t
 d ƒ t
 d ƒ ƒ |
 j	 t t
 d ƒ ƒ } } t t | | | ƒ | ƒ } t | d d	 ƒ} t | ƒ | j ƒ  k r]d St
 d ƒ } x¨| j ƒ  D]š} t | j i | | 6ƒ | ƒ } t | d d	 ƒ} t | ƒ | j ƒ  k rÉd Sg  } xi | D]a } | | k rÖ| | k rÖ| j s
| j ƒ  r| j | ƒ q7| j s7| j | ƒ q7qÖqÖWxÒ | D]Ê } | j i | | 6| | 6ƒ } | j d
 t ƒ d k rƒqBn  t | j i | | 6| | 6ƒ | ƒ } t | j i | | 6| | 6ƒ | ƒ } | d | d j ƒ  } | | t | ƒ | t | | ƒ 7} qBWqvWt | d d	 ƒ} t | ƒ | j ƒ  k rBd Sx: | j ƒ  D], } | | t |  j ƒ  j | | ƒ ƒ 7} qOW| S(   s\  
    Convert complex logarithms to real functions.

    Given real field K and polynomials h in K[t,x] and q in K[t],
    returns real function f such that:
                          ___
                  df   d  \  `
                  -- = --  )  a log(h(a, x))
                  dx   dx /__,
                         a | q(a) = 0

    Examples
    ========

        >>> from sympy.integrals.rationaltools import log_to_real
        >>> from sympy.abc import x, y
        >>> from sympy import Poly, sqrt, S
        >>> log_to_real(Poly(x + 3*y/2 + S(1)/2, x, domain='QQ[y]'),
        ... Poly(3*y**2 + 1, y, domain='ZZ'), x, y)
        2*sqrt(3)*atan(2*sqrt(3)*x/3 + sqrt(3)/3)/3
        >>> log_to_real(Poly(x**2 - 1, x, domain='ZZ'),
        ... Poly(-2*y + 1, y, domain='ZZ'), x, y)
        log(x**2 - 1)/2

    See Also
    ========

    log_to_atan
    iÿÿÿÿ(   t   collects   u,vt   clst   evaluatei   i    t   filterR;   t   chopi   N(   RA   Rt   R   R   R   RH   R   t   expandR   R!   R   R   R   R   t   lent   count_rootsR%   t   keyst   is_negativet   could_extract_minus_signR^   R   t   evalfR   R   Rr   (   R3   R.   R+   R   Rt   RI   RJ   RS   R5   t   H_mapt   Q_mapR>   R?   RW   t   dR;   t   R_uR1   t   r_uRi   t   R_vt
   R_v_pairedt   r_vt   DRQ   RR   t   ABt   R_qR6   (    (    s<   lib/python2.7/site-packages/sympy/integrals/rationaltools.pyR)   /  sN    **77	&&-*N(   t   __doc__t
   __future__R    R   RA   R   R   R   R   R   R   R   R	   R
   R   R   t   sympy.polysR   R   R   t   sympy.core.compatibilityR   R<   R    R%   R$   Rr   R)   (    (    (    s<   lib/python2.7/site-packages/sympy/integrals/rationaltools.pyt   <module>   s   L	e	<U	.