Ë
    täi®-  ã                   óÂ   — 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„ Zd„ Zdd„Zd„ Zd„ Zd„ Z y)zAThis module implements tools for integrating rational functions. é    )ÚLambda)ÚI)ÚS)ÚDummyÚSymbolÚsymbols)Úlog)Úatan)ÚDomainError)Úroots)Úcancel)ÚRootSum)ÚPolyÚ	resultantÚZZc                 óx  — t        | t        «      r| \  }}n| j                  «       \  }}t        ||dd¬«      t        ||dd¬«      }}|j	                  |«      \  }}}|j                  |«      \  }}|j                  |«      j                  «       }|j                  r||z  S t        |||«      \  }}	|	j                  «       \  }
}t        |
|«      }
t        ||«      }|
j                  |«      \  }}|||j                  |«      j                  «       z   z  }|j                  �s•|j                  dd«      }t        |t        «      st        |«      }n|j                  «       }t        ||||«      }|j                  d«      }|€dt        | t        «      r'| \  }}|j                  «       |j                  «       z  }n| j                  «       }||hz
  D ]  }|j                   rŒd} n d}t"        j$                  }|sS|D ]M  \  }	}|	j'                  «       \  }}	|t)        |t+        ||t-        |	j                  «       «      z  «      d¬«      z  }ŒO nh|D ]c  \  }	}|	j'                  «       \  }}	t/        |	|||«      }|�||z  }Œ/|t)        |t+        ||t-        |	j                  «       «      z  «      d¬«      z  }Œe ||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Úreal)Ú	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                         úl/Volumes/fast/ai/experiments/MLX_z-image/.venv/lib/python3.12/site-packages/sympy/integrals/rationaltools.pyÚratintr<      s“  € ôD �!”UÔØ‰ˆ‰1à×ÑÓ!‰ˆˆ1ä��1 ¨TÔ2´D¸¸AÈÐVZÔ4[€q€Aà—(‘(˜1“+�K€Eˆ1ˆaØ�e‰e�A‹h�G€Dˆ!à�^‰^˜AÓ×&Ñ&Ó(€Fà‡y‚yØ�V‰|Ðä˜!˜Q Ó"�D€A€qà×ÑÓ�D€A€qäˆQ�‹
€AÜˆQ�‹
€Aà�5‰5�‹8�D€A€qà
ˆa�!—+‘+˜a“.×(Ñ(Ó*Ñ*Ñ*€Fà�9‹9Ø—‘˜8 SÓ)ˆä˜&¤&Ô)Ü�f“‰Aà—‘Ó!ˆAä˜1˜a  AÓ&ˆà�y‰y˜Ó ˆàˆ<Ü˜!œUÔ#Ø‘��1ØŸ™›	 A§G¡G£IÑ-‘àŸ™›	�à ˜s”{�Ø×+Ó+Ø �DÙð #ð
 �ä�f‰fˆáÛ‘��1Ø—{‘{“}‘��1Ø”wØ”v˜a ¤3 q§y¡y£{Ó#3Ñ!3Ó4ÀôFñ F‘ñ ó
 ‘��1Ø—{‘{“}‘��1Ü  1 a¨Ó+�à�=Ø˜1‘H‘Càœ7Øœ6 ! Q¤s¨1¯9©9«;Ó'7Ñ%7Ó8ÀDôJñ J‘Cð ð 	�#‰ˆà�‰<Ðó    c           
      ó¤  — ddl m} t        | |«      } t        ||«      }|j                  |j	                  «       «      \  }}}|j                  «       }|j                  «       }t        d|«      D �	cg c]  }	t        dt        ||	z
  «      z   «      ‘Œ }
}	t        d|«      D �	cg c]  }	t        dt        ||	z
  «      z   «      ‘Œ }}	|
|z   }t        |
|t        |   ¬«      }t        ||t        |   ¬«      }| |j	                  «       |z  z
  ||j	                  «       |z  j                  |«      z  z   ||z  z
  } ||j                  «       |«      }|j                  «       j                  |«      }|j                  «       j                  |«      }t        ||j                  «       z  |«      }t        ||j                  «       z  |«      }||fS c c}	w c c}	w )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   )ÚsolveÚaÚb)Údomain)Úsympy.solvers.solversr?   r   Ú	cofactorsÚdiffÚdegreeÚranger   Ústrr   ÚquoÚcoeffsr   Úsubsr   )r)   r1   r*   r?   ÚuÚvr9   ÚnÚmÚiÚA_coeffsÚB_coeffsÚC_coeffsÚAÚBÚHr0   Úrat_partÚlog_parts                      r;   r    r    }   s�  € õ@ ,äˆQ�‹
€AÜˆQ�‹
€Aà�k‰k˜!Ÿ&™&›(Ó#�G€A€qˆ!à	�‰‹
€AØ	�‰‹
€Aä27¸¸1´+Ó?±+¨Q”�sœS  Q¡›ZÑ'Õ(°+€HÐ?Ü27¸¸1´+Ó?±+¨Q”�sœS  Q¡›ZÑ'Õ(°+€HÐ?à˜(Ñ"€HäˆX�q¤ H¡Ô.€AÜˆX�q¤ H¡Ô.€Aà	ˆA�F‰F‹H�Q‰J‰˜˜AŸF™F›H Q™J×+Ñ+¨AÓ.Ñ.Ñ.°°1±Ñ4€Aá�1—8‘8“:˜xÓ(€Fà	�	‰	‹×Ñ˜Ó €AØ	�	‰	‹×Ñ˜Ó €Aä�a˜Ÿ	™	›‘m QÓ'€HÜ�a˜Ÿ	™	›‘m QÓ'€Hà�XÐÐùò% @ùÚ?s   Á0!GÂ!!GNc                 óŠ  — t        | |«      t        ||«      }} |xs t        d«      }|| |j                  «       t        ||«      z  z
  }}t        ||d¬«      \  }}t        ||d¬«      }|sJ d|›d|›d�«       ‚i g }	}|D ]  }
|
||
j	                  «       <   Œ d	„ }|j                  «       \  }} |||«       |D �]‡  \  }}|j                  «       \  }}|j	                  «       |k(  r|	j                  ||f«       ŒA||   }t        |j                  «       |d¬
«      }|j                  d¬«      \  }} |||«       |D ]2  \  }}|j                  t        |j                  |«      |z  |«      «      }Œ4 |j                  |«      t        j                  g}}|j                  «       dd D ]P  }|j                  |j                   «      }||z  j#                  |«      }|j                  |j%                  «       «       ŒR t        t'        t)        t+        |j-                  «       |«      «      «      |«      }|	j                  ||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                   r7| dk  dk(  r.|d   \  }}| j                  |j                  «      }||z  |f|d<   y y y )Nr   T)r%   Úas_polyÚgens)ÚcÚsqfr2   ÚkÚc_polys        r;   Ú_include_signz%ratint_logpart.<locals>._include_signñ   sM   € Ø×Ò 1 q¡5¨T¢/Ø�q‘6‰DˆAˆqØ—Y‘Y˜qŸv™vÓ&ˆFØ�v‘X˜q�[ˆC�ŠFð #2Ðr=   )r   )Úallé   N)r   r   rE   r   rF   Úsqf_listr'   ÚappendÚLCrI   ÚgcdÚinvertr   ÚOnerJ   r\   r]   Úremr   ÚdictÚlistÚzipÚmonoms)r)   r1   r*   r   r@   rA   Úresr:   ÚR_maprV   r5   rb   ÚCÚres_sqfr-   rP   r9   r2   Úh_lcr^   Úh_lc_sqfÚjÚinvrJ   r.   ÚTs                             r;   r#   r#   ¼   s   € ôL ��1‹:”t˜A˜q“z€q€Aà	ŠŒU�3‹Z€AØˆa�!—&‘&“(œ4  1›:Ñ%Ñ%€q€Aä�q˜!¨Ô-�F€CˆÜ
ˆs�A Ô
'€CáÑ@º1ºaÐ@Ó@ˆ3à�2ˆ1€EãˆØˆˆa�h‰h‹jÒð ò!ð —‘“�J€A€wÙ�!�WÔä‰ˆˆ1Ø�{‰{‹}‰ˆˆ1à�8‰8‹:˜Š?Ø�H‰H�a˜�VÕà�a‘ˆAÜ˜Ÿ™› ¨Ô.ˆDàŸ-™-¨D˜-Ó1‰KˆAˆxÙ˜!˜XÔ&ã ‘��1Ø—E‘Eœ$˜qŸu™u Q›x¨™{¨AÓ.Ó/‘ð !ð Ÿ+™+ a›.¬1¯5©5¨'�ˆCàŸ™› A B›�ØŸ™ c§h¡hÓ/�Ø˜‘Y—O‘O AÓ&�Ø—‘˜aŸi™i›kÕ*ð (ô
 ”Tœ$œs 1§8¡8£:¨vÓ6Ó7Ó8¸!Ó<ˆAà�H‰H�a˜�VÖð1 ð4 €Hr=   c                 ó¼  — | j                  «       |j                  «       k  r| | }} | j                  «       } |j                  «       }| j                  |«      \  }}|j                  rdt	        |j                  «       «      z  S |j                  |  «      \  }}}| |z  ||z  z   j                  |«      }dt	        |j                  «       «      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
    é   )	rF   Úto_fieldr   r   r
   r   ÚgcdexrI   Úlog_to_atan)	r)   r1   r,   r-   Úsr   r2   rL   rT   s	            r;   r}   r}     s½   € ð> 	‡x�xƒz�A—H‘H“JÒØˆr�1ˆ1ˆà	�
‰
‹€AØ	�
‰
‹€Aà�5‰5�‹8�D€A€qà‡y‚yØ”�a—i‘i“kÓ"Ñ"Ð"à—'‘'˜1˜"“+‰ˆˆ1ˆaØˆq‰S�1�Q‘3‰Y�O‰O˜AÓˆØŒd�1—9‘9“;ÓÑˆà”;˜q !Ó$Ñ$Ð$r=   c                 ó‚   — t        | d¬«      }	 | j                  «       }t        |«      |k(  r|S y# t        $ r |cY S w xY w)zget real roots of f if possibler:   )ÚfilterN)r   Úcount_rootsÚlenr   )r)   r*   ÚrsÚ	num_rootss       r;   Ú_get_real_rootsr…   H  sJ   € ä	ˆq˜Ô	€BðØ—M‘M“Oˆ	ô ˆr‹7�iÒØˆIàøô ò ØŠ	ðús   �0 °>½>c           
      ó°  — ddl m} t        dt        ¬«      \  }}| j	                  «       j                  ||t        |z  z   i«      j                  «       }|j	                  «       j                  ||t        |z  z   i«      j                  «       } ||t        d¬«      }	 ||t        d¬«      }
|	j                  t        j                  t        j                  «      |	j                  t        t        j                  «      }}|
j                  t        j                  t        j                  «      |
j                  t        t        j                  «      }}t        t        |||«      |«      }t        ||«      }|€yt        j                  }|j                  «       D �]j  }t        |j                  ||i«      |«      }|s-t        |j                  ||i«      |«      }t        j                  }t        ||«      }|€ yg }|D ]Z  }||vsŒ| |vsŒ|j                   s|j#                  «       r|j%                  | «       Œ=|j&                  rŒJ|j%                  |«       Œ\ |D ]¥  }|j                  ||||i«      }|j)                  d¬	«      dk7  rŒ.t        |j                  ||||i«      |«      }t        |j                  ||||i«      |«      }|d
z  |d
z  z   j	                  «       }||t+        |«      z  |t-        ||«      z  z   z  }Œ§ �Œm t        ||«      }|€y|j                  «       D ]1  }||t+        | j	                  «       j/                  ||«      «      z  z  }Œ3 |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   rj   r&   r   r   r…   ÚkeysÚis_negativeÚcould_extract_minus_signrf   r   Úevalfr	   r}   rK   )r2   r-   r*   r   r‡   rL   rM   rV   r4   ÚH_mapÚQ_mapr@   rA   r^   Údr:   ÚR_ur0   Úr_urr   ÚR_vÚ
R_v_pairedÚr_vÚDrT   rU   ÚABÚR_qr5   s                                r;   r(   r(   W  sÖ  € õB /Ü�5œeÔ$�D€A€qà	�	‰	‹×Ñ˜a ¤Q q¡S¡˜\Ó*×1Ñ1Ó3€AØ	�	‰	‹×Ñ˜a ¤Q q¡S¡˜\Ó*×1Ñ1Ó3€Aá�A”q 5Ô)€EÙ�A”q 5Ô)€Eà�9‰9”Q—U‘UœAŸF™FÓ# U§Y¡Y¬q´!·&±&Ó%9€q€AØ�9‰9”Q—U‘UœAŸF™FÓ# U§Y¡Y¬q´!·&±&Ó%9€q€AäŒY�q˜!˜QÓ Ó#€Aä
˜!˜QÓ
€Cà
€{Øä�V‰V€Fà�x‰x�zˆÜ�—‘˜Q ˜HÓ% qÓ)ˆÙô �Q—Z‘Z  C Ó)¨1Ó-ˆAô
 —‘ˆAä˜a Ó#ˆàˆ;Ùàˆ
ÛˆCØ˜*Ò$¨#¨°ZÒ)?Ø—?’? c×&BÑ&BÔ&DØ×%Ñ% s dÕ+ØŸ›Ø×%Ñ% cÕ*ð ó ˆCà—
‘
˜A˜s A sÐ+Ó,ˆAà�w‰w˜DˆwÓ! QÒ&Øä�Q—Z‘Z  C¨¨CÐ 0Ó1°1Ó5ˆAÜ�Q—Z‘Z  C¨¨CÐ 0Ó1°1Ó5ˆAà�Q‘$˜˜A™‘+×&Ñ&Ó(ˆBà�cœ#˜b›'‘k C¬°A°qÓ(9Ñ$9Ñ9Ñ9‰Fò ð5 ôP ˜!˜QÓ
€Cà
€{Øà�X‰XŽZˆØ�!”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.polyerrorsr   Úsympy.polys.polyrootsr   Úsympy.polys.polytoolsr   Úsympy.polys.rootoftoolsr   Úsympy.polysr   r   r   r<   r    r#   r}   r…   r(   © r=   r;   Ú<module>rª      sT   ðÙ Gå &Ý  Ý "ß 6Ñ 6Ý 6Ý 9Ý .Ý 'Ý (Ý +ß +Ñ +òjòZ<ó~Xòv.%òbófr=   