o
    Th-                     @   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 rt | tr| \}}| | B }n|  }||h D ]	}|jsd} nqd}tj}|s|D ]\}	}|	 \}}	|t|t||t|	  dd7 }qn/|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   ^/home/air/segue/gemini/back/venv/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}|| |}||  qt 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rqt|||||i|}t|||||i|}|d
 |d
   }||t| |t||  7 }qqut||}|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