
    Ed*                         d Z ddlmZ ddl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 ddlmZ dd	lmZ dd
lmZ ddlmZmZmZ d Zd ZddZd Zd ZdS )zAThis module implements tools for integrating rational functions.     )Lambda)I)S)DummySymbolsymbols)log)atan)roots)cancel)RootSum)Poly	resultantZZc                 l   t          | t                    r| \  }}n|                                 \  }}t          ||dd          t          ||dd          }}|                    |          \  }}}|                    |          \  }}|                    |                                          }|j        r||z  S t          |||          \  }}	|	                                \  }
}t          |
|          }
t          ||          }|
                    |          \  }}|||                    |                                          z   z  }|j        s|
                    dd          }t          |t                    st          |          }n|                                }t          ||||          }|
                    d          }|nt          | t                    r/| \  }}|                                |                                z  }n|                                 }||hz
  D ]}|j        sd} nd}t"          j        }|se|D ]a\  }	}|	                                \  }}	|t)          |t+          ||t-          |	                                          z            d          z  }bn~|D ]{\  }	}|	                                \  }}	t/          |	|||          }|||z  }6|t)          |t+          ||t-          |	                                          z            d          z  }|||z  }||z  S )	aa  
    Performs indefinite integration of rational functions.

    Explanation
    ===========

    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'`.

    Examples
    ========

    >>> 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
    ==========

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

    See Also
    ========

    sympy.integrals.integrals.Integral.doit
    sympy.integrals.rationaltools.ratint_logpart
    sympy.integrals.rationaltools.ratint_ratpart

    FT)	compositefieldsymboltrealN)	quadratic)
isinstancetupleas_numer_denomr   r   div	integrateas_expris_zeroratint_ratpartgetr   r   as_dummyratint_logpartatomsis_extended_realr   Zero	primitiver   r   r	   log_to_real)fxflagspqcoeffpolyresultghPQrr   r   Lr   r#   elteps_Rs                         =lib/python3.11/site-packages/sympy/integrals/rationaltools.pyratintr;      sW   D !U "11!!11T222DAVZ4[4[4[qA((1++KE1aeeAhhGD!^^A&&((Fy V|!Q""DAqDAqQ

AQ

A5588DAq
a!++a..((****F9 ,8S))&&)) 	"fAA!!A1aA&&yy   	!U## "1		AGGII-		s{  +  DE f 	J F F1{{}}1wva3qyy{{#3#3!344F F F FF
  J J1{{}}11a++ J1HCC76!Qs199;;'7'7%788DJ J J JCC 	#<    c                    ddl m} t          | |          } t          ||          }|                    |                                          \  }}}|                                |                                fdt          d          D             }fdt          d          D             }||z   }	t          ||t          |	                   }
t          ||t          |	                   }| |
                                |z  z
  |
|                                |z                      |          z  z   ||z  z
  } ||	                                |	          }|

                                                    |          }
|
                                                    |          }t          |
|
                                z  |          }t          ||
                                z  |          }||fS )a  
    Horowitz-Ostrogradsky algorithm.

    Explanation
    ===========

    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
    r   )solvec           	      T    g | ]$}t          d t          |z
            z             %S )ar   str).0ins     r:   
<listcomp>z"ratint_ratpart.<locals>.<listcomp>   0    ???QsSQZZ'((???r<   c           	      T    g | ]$}t          d t          |z
            z             %S )brA   )rC   rD   ms     r:   rF   z"ratint_ratpart.<locals>.<listcomp>   rG   r<   )domain)sympy.solvers.solversr>   r   	cofactorsdiffdegreeranger   quocoeffsr   subsr   )r(   r0   r)   r>   uvr8   A_coeffsB_coeffsC_coeffsABHr/   rat_partlog_partrJ   rE   s                   @@r:   r   r   |   s   @ ,+++++Q

AQ

Akk!&&((##GAq!	

A	

A????%1++???H????%1++???H("HXqH...AXqH...A	AFFHHQJAFFHHQJ++A....14AU188::x((F			  A			  Aa		mQ''Ha		mQ''HXr<   Nc                 x   t          | |          t          ||          }} |pt          d          }|| |                                t          ||          z  z
  }}t          ||d          \  }}t          ||d          }|sJ d|d|d            i g }	}|D ]}
|
||
                                <   d	 }|                                \  }} |||           |D ]\  }}|                                \  }}|                                |k    r|	                    ||f           M||         }t          |                                |d
          }|                    d          \  }} |||           |D ]>\  }}|	                    t          |
                    |          |z  |                    }?|                    |          t          j        g}}|                                dd         D ][}|                    |j                  }||z                      |          }|                    |                                           \t          t'          t)          t+          |                                |                              |          }|	                    ||f           |	S )an  
    Lazard-Rioboo-Trager algorithm.

    Explanation
    ===========

    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)
includePRSF)r   zBUG: resultant(z, z) cannot be zeroc                     | j         r;| dk     dk    r3|d         \  }}|                     |j                  }||z  |f|d<   d S d S d S )Nr   T)r$   as_polygens)csqfr1   kc_polys        r:   _include_signz%ratint_logpart.<locals>._include_sign   se     	!1q5T/ 	!q6DAqYYqv&&FvXq[CFFF	! 	! 	! 	!r<   )r   )all   N)r   r   rN   r   rO   sqf_listr&   appendLCrQ   gcdinvertr   OnerR   ra   rb   remr   dictlistzipmonoms)r(   r0   r)   r   r@   rI   resr9   R_mapr[   r4   rg   Cres_sqfr,   rD   r8   r1   h_lcrc   h_lc_sqfjinvrR   r-   Ts                             r:   r"   r"      s   L 1::tAqzzqA	U3ZZAa!&&((41::%%qAq!---FC
sA
'
'
'C@@@111aaa@@@@21E  ahhjj! ! ! JAwM!W  1{{}}188::? 	HHaVaA...D--D-11KAxM!X&&&  0 01EE$quuQxx{A..//++a..15'CABB + +ch//YOOA&&aiikk****T$s188::v667788!<<AHHaVHr<   c                    |                                  |                                 k     r| | }} |                                 } |                                }|                     |          \  }}|j        r$dt	          |                                          z  S |                    |            \  }}}| |z  ||z  z                       |          }dt	          |                                          z  }|t          ||          z   S )a0  
    Convert complex logarithms to real arctangents.

    Explanation
    ===========

    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
       )	rO   to_fieldr   r   r
   r   gcdexrQ   log_to_atan)	r(   r0   r+   r,   sr   r1   rT   rY   s	            r:   r   r     s    > 	xxzzAHHJJ r11	

A	

A5588DAqy %aiikk""""''1"++1aqS1Q3YOOAd199;;;q!$$$$r<   c           
         ddl m} t          dt                    \  }}|                                                     ||t          |z  z   i                                          }|                                                    ||t          |z  z   i                                          } ||t          d          }	 ||t          d          }
|	                    t          j
        t          j                  |	                    t          t          j                  }}|
                    t          j
        t          j                  |
                    t          t          j                  }}t          t          |||          |          }t          |d          }t          |          |                                k    rd	S t          j        }|                                D ]}t          |                    ||i          |          }t          |d          }t          |          |                                k    r d	S g }|D ]Y}||vrS| |vrN|j        s|                                r|                    |            =|j        s|                    |           Z|D ]}|                    ||||i          }|                    d
          dk    r6t          |                    ||||i          |          }t          |                    ||||i          |          }|dz  |dz  z                                   }||t/          |          z  |t1          ||          z  z   z  }ːt          |d          }t          |          |                                k    rd	S |                                D ]=}||t/          |                                                     ||                    z  z  }>|S )aw  
    Convert complex logarithms to real functions.

    Explanation
    ===========

    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, 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
    r   )collectzu,v)clsF)evaluater9   )filterNT)chopr   )sympy.simplify.radsimpr   r   r   r   rS   r   expandr    r   ro   r%   r   r   r   lencount_rootskeysis_negativecould_extract_minus_signrk   r   evalfr	   r   )r1   r,   r)   r   r   rT   rU   r[   r3   H_mapQ_mapr@   rI   rc   dr9   R_ur/   r_urw   R_v
R_v_pairedr_vDrY   rZ   ABR_qr4   s                                r:   r'   r'   G  s   B /.....5e$$$DAq			!Q1W&&--//A			!Q1W&&--//AGAq5)))EGAq5)))E99QUAF##UYYq!&%9%9qA99QUAF##UYYq!&%9%9qAYq!Q##A
#


C
3xx1==??" tVFxxzz : :C!!1%%Ac"""s88q}}& 	44
 	+ 	+C*$ +#Z)? +? +c&B&B&D&D +%%sd++++ +%%c*** 	: 	:C33'((AwwDw!!Q& QVVQQ,--q11AQVVQQ,--q11AQ$A+&&((Bc#b''kCAq(9(9$999FF	: #


C
3xx1==??" tXXZZ 0 0!C		((A..////Mr<   )N)__doc__sympy.core.functionr   sympy.core.numbersr   sympy.core.singletonr   sympy.core.symbolr   r   r   &sympy.functions.elementary.exponentialr	   (sympy.functions.elementary.trigonometricr
   sympy.polys.polyrootsr   sympy.polys.polytoolsr   sympy.polys.rootoftoolsr   sympy.polysr   r   r   r;   r   r"   r   r'    r<   r:   <module>r      sA   G G & & & & & &             " " " " " " 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 9 9 9 9 9 9 ' ' ' ' ' ' ( ( ( ( ( ( + + + + + + + + + + + + + + + +j j jZ< < <~X X X Xv.% .% .%b[ [ [ [ [r<   