o
    ˜…›i®-  ã                   @   sÎ   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 ddlmZmZmZ dd„ Zdd„ Zddd„Zdd„ Zdd„ Zdd„ Z dS )zAThis module implements tools for integrating rational functions. é    )ÚLambda)ÚI)ÚS)ÚDummyÚSymbolÚsymbols)Úlog)Úatan)ÚDomainError)Úroots)Úcancel)ÚRootSum)ÚPolyÚ	resultantÚZZc              
   K   s&  t | tƒr
| \}}n|  ¡ \}}t||dddt||ddd}}| |¡\}}}| |¡\}}| |¡ ¡ }|jr>|| S t	|||ƒ\}}	|	 ¡ \}
}t|
|ƒ}
t||ƒ}|
 |¡\}}||| |¡ ¡  7 }|js| 
dd¡}t |tƒs|t|ƒ}n| ¡ }t||||ƒ}| 
d¡}|du r·t | tƒr¢| \}}| ¡ | ¡ B }n|  ¡ }||h D ]	}|js´d} nq«d}tj}|sÜ|D ]\}	}|	 ¡ \}}	|t|t||t|	 ¡ ƒ ƒdd7 }q¾n/|D ],\}	}|	 ¡ \}}	t|	|||ƒ}|durø||7 }qÞ|t|t||t|	 ¡ ƒ ƒdd7 }qÞ||7 }|| 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Ú_ÚR© r9   ú_/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/sympy/integrals/rationaltools.pyÚratint   sf   
"
"





þ
ÿþ

ÿr;   c                    s$  ddl m} t| |ƒ} t||ƒ}| | ¡ ¡\}}}| ¡ ‰| ¡ ‰ ‡fdd„tdˆƒD ƒ}‡ fdd„tdˆ ƒD ƒ}|| }	t||t|	 d}
t||t|	 d}| |
 ¡ |  |
| ¡ |  |¡  ||  }|| 	¡ |	ƒ}|
 
¡  |¡}
| 
¡  |¡}t|
| 
¡  |ƒ}t|| 
¡  |ƒ}||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                    ó    g | ]}t d tˆ | ƒ ƒ‘qS )Úa©r   Ústr©Ú.0Úi)Únr9   r:   Ú
<listcomp>§   ó     z"ratint_ratpart.<locals>.<listcomp>c                    r=   )Úbr?   rA   )Úmr9   r:   rE   ¨   rF   )Údomain)Úsympy.solvers.solversr<   r   Ú	cofactorsÚdiffÚdegreeÚranger   ÚquoÚcoeffsr   Úsubsr   )r'   r/   r(   r<   ÚuÚvr7   ÚA_coeffsÚB_coeffsÚC_coeffsÚAÚBÚHr.   Úrat_partÚlog_partr9   )rH   rD   r:   r   }   s$    

.r   Nc                 C   sÂ  t | |ƒt ||ƒ} }|ptdƒ}|| | ¡ t ||ƒ  }}t||dd\}}t ||dd}|s9J d||f ƒ‚i g }}	|D ]}
|
||
 ¡ < q@dd„ }| ¡ \}}|||ƒ |D ]„\}}| ¡ \}}| ¡ |krr|	 ||f¡ qZ|| }t | ¡ |dd	}|jdd
\}}|||ƒ |D ]\}}| 	t | 
|¡| |ƒ¡}qŽ| |¡tjg}}| ¡ dd… D ]}| |j¡}||  |¡}| | ¡ ¡ q²t ttt| ¡ |ƒƒƒ|ƒ}|	 ||f¡ qZ|	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(%s, %s) cannot be zeroc                 S   sF   | j r| dk dkr!|d \}}|  |j¡}|| |f|d< d S d S d S )Nr   T)r#   Úas_polyÚgens)ÚcÚsqfr0   ÚkÚc_polyr9   r9   r:   Ú_include_signñ   s
   ýz%ratint_logpart.<locals>._include_sign)r   )Úallé   N)r   r   rL   r   rM   Úsqf_listr%   ÚappendÚLCrO   ÚgcdÚinvertr   ÚOnerP   r]   r^   Úremr   ÚdictÚlistÚzipÚmonoms)r'   r/   r(   r   r>   rG   Úresr8   ÚR_maprY   r3   rc   ÚCÚres_sqfr+   rC   r7   r0   Úh_lcr_   Úh_lc_sqfÚjÚinvrP   r,   ÚTr9   r9   r:   r!   ¼   s<   &


r!   c           	      C   s–   |   ¡ |  ¡ k r| | } }|  ¡ } | ¡ }|  |¡\}}|jr(dt| ¡ ƒ S | |  ¡\}}}| | ||   |¡}dt| ¡ ƒ }|t||ƒ 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
    é   )	rM   Úto_fieldr   r   r	   r   ÚgcdexrO   Úlog_to_atan)	r'   r/   r*   r+   Úsr   r0   rR   rW   r9   r9   r:   r}     s   r}   c                 C   sD   t | dd}z|  ¡ }W n ty   | Y S w t|ƒ|kr |S dS )zget real roots of f if possibler8   )ÚfilterN)r   Úcount_rootsr
   Úlen)r'   r(   ÚrsÚ	num_rootsr9   r9   r:   Ú_get_real_rootsH  s   ÿr„   c              	   C   s^  ddl m} tdtd\}}|  ¡  ||t|  i¡ ¡ }| ¡  ||t|  i¡ ¡ }||tdd}	||tdd}
|	 t	j
t	j¡|	 tt	j¡}}|
 t	j
t	j¡|
 tt	j¡}}tt|||ƒ|ƒ}t||ƒ}|du rndS t	j}| ¡ D ]•}t| ||i¡|ƒ}|st| ||i¡|ƒ}t	j}t||ƒ}|du rœ dS g }|D ]!}||vrÁ| |vrÁ|js²| ¡ r¹| | ¡ q |jsÁ| |¡ q |D ]E}| ||||i¡}|jdd	dkrØqÄt| ||||i¡|ƒ}t| ||||i¡|ƒ}|d
 |d
   ¡ }||t|ƒ |t||ƒ  7 }qÄqut||ƒ}|du rdS | ¡ D ]}||t|  ¡  ||¡ƒ 7 }q|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)ÚevaluateNT)Úchoprz   )Úsympy.simplify.radsimpr…   r   r   r   Úxreplacer   Úexpandr   r   rk   r$   r   r   r„   ÚkeysÚis_negativeÚcould_extract_minus_signrg   r   Úevalfr   r}   rQ   )r0   r+   r(   r   r…   rR   rS   rY   r2   ÚH_mapÚQ_mapr>   rG   r_   Údr8   ÚR_ur.   Úr_urs   ÚR_vÚ
R_v_pairedÚr_vÚDrW   rX   ÚABÚR_qr3   r9   r9   r:   r&   W  sX   !  


€ ô

 r&   )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.polyerrorsr
   Úsympy.polys.polyrootsr   Úsympy.polys.polytoolsr   Úsympy.polys.rootoftoolsr   Úsympy.polysr   r   r   r;   r   r!   r}   r„   r&   r9   r9   r9   r:   Ú<module>   s$    m
?[1