
    \hT                     >   S r SSKJr  SSKJrJrJrJrJrJ	r	J
r
JrJrJrJrJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJrJrJrJr  SS	KJrJr  SS
K J!r!J"r"J#r#J$r$J%r%  SSK&J'r'J(r(J)r)  SSK*J+r+  SSK,J-r-J.r.  SSK/J0r0J1r1J2r2J3r3  SSK4J5r5J6r6J7r7J8r8  SSK9J:r:J;r;  SSK<J=r=  SSK>J?r?  S/r@ " S S5      rA\A" 5       qB\A" 5       qCS rDS rE\1S"S j5       rF\1 " S S\5      5       rG\1 " S S\G5      5       rH\HrI\=" \H\H5      S 5       rJ\=" \H\5      S 5       rJ\1 " S  S!\5      5       rKg)#z2Implementation of RootOf class and related tools.     )Basic)SExprIntegerFloatIooAddLambdasymbolssympifyRationalDummy)cacheit)is_le)ordered)QQ)MultivariatePolynomialErrorGeneratorsNeededPolynomialErrorDomainError)
symmetrizeviete)roots_linearroots_quadraticroots_binomialpreprocess_rootsroots)PolyPurePolyfactor)together)dup_isolate_complex_roots_sqfdup_isolate_real_roots_sqf)lambdifypublicsiftnumbered_symbols)mpfmpcfindrootworkprec)dps_to_precprec_to_dps)dispatch)chainCRootOfc                   0    \ rS rSrSrS rS rS rS rSr	g)	_pure_key_dict&   a  A minimal dictionary that makes sure that the key is a
univariate PurePoly instance.

Examples
========

Only the following actions are guaranteed:

>>> from sympy.polys.rootoftools import _pure_key_dict
>>> from sympy import PurePoly
>>> from sympy.abc import x, y

1) creation

>>> P = _pure_key_dict()

2) assignment for a PurePoly or univariate polynomial

>>> P[x] = 1
>>> P[PurePoly(x - y, x)] = 2

3) retrieval based on PurePoly key comparison (use this
   instead of the get method)

>>> P[y]
1

4) KeyError when trying to retrieve a nonexisting key

>>> P[y + 1]
Traceback (most recent call last):
...
KeyError: PurePoly(y + 1, y, domain='ZZ')

5) ability to query with ``in``

>>> x + 1 in P
False

NOTE: this is a *not* a dictionary. It is a very basic object
for internal use that makes sure to always address its cache
via PurePoly instances. It does not, for example, implement
``get`` or ``setdefault``.
c                     0 U l         g N_dictselfs    O/var/www/auris/envauris/lib/python3.13/site-packages/sympy/polys/rootoftools.py__init___pure_key_dict.__init__S   s	    
    c                     [        U[        5      (       d>  [        U[        5      (       a  [        UR                  5      S:X  d  [
        e[        USS9nU R                  U   $ )N   Fexpand)
isinstancer    r   lenfree_symbolsKeyErrorr8   r:   ks     r;   __getitem___pure_key_dict.__getitem__V   sK    !X&&q$''C,?1,D5)Azz!}r>   c                     [        U[        5      (       dC  [        U[        5      (       a  [        UR                  5      S:X  d  [        S5      e[        USS9nX R                  U'   g )Nr@   zexpecting univariate expressionFrA   )rC   r    r   rD   rE   
ValueErrorr8   )r:   rH   vs      r;   __setitem___pure_key_dict.__setitem__]   sO    !X&&q$''C,?1,D !BCC5)A

1r>   c                 .     X     g! [          a     gf = f)NTF)rF   rG   s     r;   __contains___pure_key_dict.__contains__d   s!    	G 		s    
r7   N)
__name__
__module____qualname____firstlineno____doc__r<   rI   rN   rQ   __static_attributes__ r>   r;   r3   r3   &   s    +Xr>   r3   c           	      t    U R                  5       u  pU VVs/ s H  u  p4[        USS9U4PM     snn$ s  snnf )NFrA   )factor_listr    )poly_factorsfms        r;   _pure_factorsra   o   s8    !!#JA7>?wtqXa&*w???s   4c                 v   U R                  5        VVs/ s H
  u  u  pX4PM     nnn[        S U 5       5      (       a  gU VVs/ s H  u  pU[        U-  U-  4PM     nnn[        R                  " [        U5      [        S5      5      n[        UR                  [        * [        5      5      $ s  snnf s  snnf )zRReturn the number of imaginary roots for irreducible
univariate polynomial ``f``.
c              3   0   #    U  H  u  pUS -  v   M     g7f)   NrY   ).0ijs      r;   	<genexpr>(_imag_count_of_factor.<locals>.<genexpr>y   s     
#UTQ1q5Us   r   x)
termsanyr   r   	from_dictdictr   intcount_rootsr	   )r_   rf   rg   rk   evens        r;   _imag_count_of_factorrr   t   s     $%779-9aV9E-

#U
###$)*EDAQ1QKED*>>$t*eCj1DtR()) . +s   B/B5Nc                     [        XX#US9$ )ar  An indexed root of a univariate polynomial.

Returns either a :obj:`ComplexRootOf` object or an explicit
expression involving radicals.

Parameters
==========

f : Expr
    Univariate polynomial.
x : Symbol, optional
    Generator for ``f``.
index : int or Integer
radicals : bool
           Return a radical expression if possible.
expand : bool
         Expand ``f``.
indexradicalsrB   )r1   )r_   rj   ru   rv   rB   s        r;   rootofrw      s    ( 1uGGr>   c                   &    \ rS rSrSrSrSS jrSrg)RootOf   zRepresents a root of a univariate polynomial.

Base class for roots of different kinds of polynomials.
Only complex roots are currently supported.
)r\   Nc                     [        XX4US9$ )z>Construct a new ``CRootOf`` object for ``k``-th root of ``f``.rt   )rw   )clsr_   rj   ru   rv   rB   s         r;   __new__RootOf.__new__   s    a%6JJr>   rY   NTT)rS   rT   rU   rV   rW   	__slots__r}   rX   rY   r>   r;   ry   ry      s     IKr>   ry   c                   f   \ rS rSrSrSrSrSrSrSr	S0S jr
\S 5       rS r\S	 5       r\S
 5       r\S 5       rS rS r\S1S j5       r\S1S j5       r\S1S j5       r\S1S j5       r\S1S j5       r\S1S j5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r \S2S j5       r!S r"S r#\S 5       r$S r%\S1S  j5       r&\\'S! 5       5       r(\S" 5       r)\S# 5       r*\S$ 5       r+\S% 5       r,\S& 5       r-\S' 5       r.S( r/S) r0S* r1S+ r2S2S, jr3S- r4S3S. jr5S/r6g)4ComplexRootOf   aI  Represents an indexed complex root of a polynomial.

Roots of a univariate polynomial separated into disjoint
real or complex intervals and indexed in a fixed order:

* real roots come first and are sorted in increasing order;
* complex roots come next and are sorted primarily by increasing
  real part, secondarily by increasing imaginary part.

Currently only rational coefficients are allowed.
Can be imported as ``CRootOf``. To avoid confusion, the
generator must be a Symbol.


Examples
========

>>> from sympy import CRootOf, rootof
>>> from sympy.abc import x

CRootOf is a way to reference a particular root of a
polynomial. If there is a rational root, it will be returned:

>>> CRootOf.clear_cache()  # for doctest reproducibility
>>> CRootOf(x**2 - 4, 0)
-2

Whether roots involving radicals are returned or not
depends on whether the ``radicals`` flag is true (which is
set to True with rootof):

>>> CRootOf(x**2 - 3, 0)
CRootOf(x**2 - 3, 0)
>>> CRootOf(x**2 - 3, 0, radicals=True)
-sqrt(3)
>>> rootof(x**2 - 3, 0)
-sqrt(3)

The following cannot be expressed in terms of radicals:

>>> r = rootof(4*x**5 + 16*x**3 + 12*x**2 + 7, 0); r
CRootOf(4*x**5 + 16*x**3 + 12*x**2 + 7, 0)

The root bounds can be seen, however, and they are used by the
evaluation methods to get numerical approximations for the root.

>>> interval = r._get_interval(); interval
(-1, 0)
>>> r.evalf(2)
-0.98

The evalf method refines the width of the root bounds until it
guarantees that any decimal approximation within those bounds
will satisfy the desired precision. It then stores the refined
interval so subsequent requests at or below the requested
precision will not have to recompute the root bounds and will
return very quickly.

Before evaluation above, the interval was

>>> interval
(-1, 0)

After evaluation it is now

>>> r._get_interval() # doctest: +SKIP
(-165/169, -206/211)

To reset all intervals for a given polynomial, the :meth:`_reset` method
can be called from any CRootOf instance of the polynomial:

>>> r._reset()
>>> r._get_interval()
(-1, 0)

The :meth:`eval_approx` method will also find the root to a given
precision but the interval is not modified unless the search
for the root fails to converge within the root bounds. And
the secant method is used to find the root. (The ``evalf``
method uses bisection and will always update the interval.)

>>> r.eval_approx(2)
-0.98

The interval needed to be slightly updated to find that root:

>>> r._get_interval()
(-1, -1/2)

The ``evalf_rational`` will compute a rational approximation
of the root to the desired accuracy or precision.

>>> r.eval_rational(n=2)
-69629/71318

>>> t = CRootOf(x**3 + 10*x + 1, 1)
>>> t.eval_rational(1e-1)
15/256 - 805*I/256
>>> t.eval_rational(1e-1, 1e-4)
3275/65536 - 414645*I/131072
>>> t.eval_rational(1e-4, 1e-4)
6545/131072 - 414645*I/131072
>>> t.eval_rational(n=2)
104755/2097152 - 6634255*I/2097152

Notes
=====

Although a PurePoly can be constructed from a non-symbol generator
RootOf instances of non-symbols are disallowed to avoid confusion
over what root is being represented.

>>> from sympy import exp, PurePoly
>>> PurePoly(x) == PurePoly(exp(x))
True
>>> CRootOf(x - 1, 0)
1
>>> CRootOf(exp(x) - 1, 0)  # would correspond to x == 0
Traceback (most recent call last):
...
sympy.polys.polyerrors.PolynomialError: generator must be a Symbol

See Also
========

eval_approx
eval_rational

)ru   TNc                 @   [        U5      nUc  UR                  (       a  SUp2O[        U5      nUb  UR                  (       a  [        U5      nO[        SU-  5      e[	        XSUS9nUR
                  (       d  [        S5      eUR                  R                  (       d  [        S5      eUR                  5       nUS::  a  [        SU-  5      eX7* :  d  X7:  a  [        S	U* US
-
  U4-  5      eUS:  a  X7-  nUR                  5       nUR                  (       d  UR                  5       nU R                  Xd5      n	U	b  X   $ [        U5      u  pUR                  5       nUR                   (       d  [#        SU-  5      eU R%                  XcSS9nXR'                  X5      -  $ )zConstruct an indexed complex root of a polynomial.

See ``rootof`` for the parameters.

The default value of ``radicals`` is ``False`` to satisfy
``eval(srepr(expr) == expr``.
Nz&expected an integer root index, got %sF)greedyrB   'only univariate polynomials are allowedzgenerator must be a Symbolr   z&Cannot construct CRootOf object for %sz(root index out of [%d, %d] range, got %dr@   z CRootOf is not supported over %sT)lazy)r   
is_Integerro   rL   r    is_univariater   gen	is_Symboldegree
IndexError
get_domainis_Exactto_exact_roots_trivialr   is_ZZNotImplementedError_indexed_root_postprocess_root)r|   r_   rj   ru   rv   rB   r\   r   domr   coeffroots               r;   r}   ComplexRootOf.__new__0  s    AJ=Q\\QuENE!1!1JEEMNNU6:!!!"KLLxx!! "">??Q;!"JQ"NOO7?eoG%gvz59: ; ;QYOEoo||==?D""42<&t,ooyy%&H3&NOO  4 8,,T<<<r>   c                     [         R                  " U 5      n[        U5      Ul        X#l         [
        U   [
        UR                  '   [        U   [        UR                  '   U$ ! [         a     U$ f = f)z0Construct new ``CRootOf`` object from raw data. )r   r}   r    r\   ru   _reals_cache_complexes_cacherF   )r|   r\   ru   objs       r;   _newComplexRootOf._newl  sn     ll3D>		%1$%7L")9$)?SXX& 
  	
	s   4A$ $
A21A2c                 2    U R                   U R                  4$ r6   )r\   ru   r9   s    r;   _hashable_contentComplexRootOf._hashable_content|  s    		4::&&r>   c                 6    U R                   R                  5       $ r6   r\   as_exprr9   s    r;   exprComplexRootOf.expr      yy  ""r>   c                 D    U R                   [        U R                  5      4$ r6   )r   r   ru   r9   s    r;   argsComplexRootOf.args  s    		74::.//r>   c                     [        5       $ r6   )setr9   s    r;   rE   ComplexRootOf.free_symbols  s    
 ur>   c                 t    U R                  5         U R                  [        [        U R                     5      :  $ )z%Return ``True`` if the root is real. )_ensure_reals_initru   rD   r   r\   r9   s    r;   _eval_is_realComplexRootOf._eval_is_real  s+    !zzCTYY 7888r>   c                     U R                  5         U R                  [        [        U R                     5      :  a,  U R                  5       nUR                  UR                  -  S:*  $ g)z*Return ``True`` if the root is imaginary. r   F)r   ru   rD   r   r\   _get_intervalaxbx)r:   ivls     r;   _eval_is_imaginary ComplexRootOf._eval_is_imaginary  sP    !::\$))455$$&C66#&&=A%%r>   c                 &    U R                  SX5      $ )z Get real roots of a polynomial. _real_roots
_get_rootsr|   r\   rv   s      r;   
real_rootsComplexRootOf.real_roots  s     ~~mT<<r>   c                 &    U R                  SX5      $ )z,Get real and complex roots of a polynomial. 
_all_rootsr   r   s      r;   	all_rootsComplexRootOf.all_roots  s     ~~lD;;r>   c                     U(       a  U[         ;   a  [         U   nU$ [        UR                  R                  5       UR                  R                  SS9=[         U'   nU$ )z;Get real root isolating intervals for a square-free factor.Tblackbox)r   r$   repto_listr   )r|   currentfactor	use_cache	real_parts       r;   _get_reals_sqfComplexRootOf._get_reals_sqf  se     ,6$]3I  +!%%--/1B1B1F1FQUWWL') r>   c                     U(       a  U[         ;   a  [         U   nU$ [        UR                  R                  5       UR                  R                  SS9=[         U'   nU$ )z>Get complex root isolating intervals for a square-free factor.Tr   )r   r#   r   r   r   )r|   r   r   complex_parts       r;   _get_complexes_sqf ComplexRootOf._get_complexes_sqf  sf     *::+M:L
  .!!))+]->->-B-BTSS]+l r>   c           	      X   / nU H?  u  pE U(       d  [         e[        U   nUR                  U Vs/ s H  owXE4PM     sn5        MA     U R	                  U5      nU$ s  snf ! [          a?    U R                  XB5      nU V	s/ s H  oXE4PM     Os  sn	f n
n	UR                  U
5         M  f = f)z=Compute real root isolating intervals for a list of factors. )rF   r   extendr   _reals_sorted)r|   r^   r   realsr   rH   rrf   r   r   news              r;   
_get_realsComplexRootOf._get_reals  s      'M" "N /Q?Q-3Q?@ !( !!%( @ "..}H	<EFIDm/IFFS!"s-   %A A
A A  B)?BB)(B)c           	      j   / n[        U5       H?  u  pE U(       d  [        e[        U   nUR                  U Vs/ s H  owXE4PM     sn5        MA     U R                  U5      nU$ s  snf ! [         a?    U R	                  XB5      nU V	s/ s H  oXE4PM     Os  sn	f n
n	UR                  U
5         M  f = f)z@Compute complex root isolating intervals for a list of factors. )r   rF   r   r   r   _complexes_sorted)r|   r^   r   	complexesr   rH   crf   r   r   r   s              r;   _get_complexesComplexRootOf._get_complexes  s     	 ' 0M& "N$]3  !CAm"7!CD !1 )))4	 "D &"55mO<HILDm/LII  %&s.   %A)A$
A)$A))B2BB21B2c                 n   0 n[        U5       HK  u  nu  pEn[        XS-   S 5       H(  u  nu  pn
UR                  U5      u  pHXU
4XU-   S-   '   M*     XEU4X'   MM     [        US S9nU H%  u  pnX;   a  X,   R                  U5        M   U/X,'   M'     UR	                  5        H  u  pU[
        U'   M     U$ )z7Make real isolating intervals disjoint and sort roots. r@   Nc                      U S   R                   $ )Nr   )a)r   s    r;   <lambda>-ComplexRootOf._reals_sorted.<locals>.<lambda>  s    AaDFFr>   )key)	enumeraterefine_disjointsortedappenditemsr   )r|   r   cacherf   ur_   rH   rg   rM   gr`   r   r   r]   s                 r;   r   ComplexRootOf._reals_sorted  s     %e,LAya )%A- 89A!((+$%!9!eai  !9 ayEH - u"23&+"D%$++D1(,v$	 ', $);;=M*.L' $1 r>   c           	         [        US 5      n/ n[        U5       GH[  n[        U5      nUS:X  ao  X#    He  u  pSnUR                  UR                  -  S::  a/  UR                  5       nUR                  UR                  -  S::  a  M/  UR                  XSU45        Mg     M  [        [        [        X#   5      5      5      n [        U5      S:  d   e[        U5       Hr  nX#   U   u  pSnUR                  UR                  -  S:  a  UR                  U5        M=  UR                  UR                  :w  d  MY  UR                  5       nXSU4X#   U'   Mt     [        U5      U:X  a  OM  UR                  X#   5        GM^     U$ )Nc                     U S   $ )Nr@   rY   )r   s    r;   r   1ComplexRootOf._refine_imaginary.<locals>.<lambda>  s    1Q4r>   r   r@   )r'   r   rr   r   r   _inner_refiner   listrangerD   remover   )	r|   r   siftedr_   nimagr   rH   potential_imagrf   s	            r;   _refine_imaginaryComplexRootOf._refine_imaginary  sO   i0	A)!,Ez%yGA!$$qtt)q.OO- $$qtt)q.$$aAY/  ) "&eC	N&;!<~.222!.1"()A,a449q=*11!4TTQTT\ ! 1A+,7FIaL 2 >*e3    +- !. r>   c                    [        U5       HK  u  nu  p4n[        XS-   S 5       H(  u  nu  pxn	UR                  U5      u  p7XxU	4XU-   S-   '   M*     X4U4X'   MM     U R                  U5      n[        U5       H[  u  nu  p4nUR                  UR                  -  S::  a/  UR                  5       nUR                  UR                  -  S::  a  M/  X4U4X'   M]     U$ )zreturn complexes such that no bounding rectangles of non-conjugate
roots would intersect. In addition, assure that neither ay nor by is
0 to guarantee that non-real roots are distinct from real roots in
terms of the y-bounds.
r@   Nr   )r   r   r   aybyrefine)
r|   r   rf   r   r_   rH   rg   rM   r   r`   s
             r;   _refine_complexesComplexRootOf._refine_complexes  s     &i0LAya ))EF*; <9A!((+()ay	a%!)$ != !9IL 1 )))4	 &i0LAya$$qtt)q.HHJ $$qtt)q.7IL 1 r>   c                    U R                  U5      nSu  p#U Vs1 s H  oDU   iM	     nn[        S[        U5      5       H1  nX   U   XS-
     U   :w  d  M  UR                  XS-
     U   5        M3     [	        U5       H  u  pFXb   R
                  US-  S:H  L a  M   e   0 nU H'  u  pn
UR                  U	/ 5      R                  U5        M)     UR                  5        H  u  pU[        U	'   M     U$ s  snf )z:Make complex isolating intervals disjoint and sort roots. )r   r@   r@   rd   r   )
r   r   rD   r   r   conj
setdefaultr   r   r   )r|   r   CFrf   fscmplxr   r   r   r]   s              r;   r   ComplexRootOf._complexes_sorted5  s    )))4	 %&IqdI&q#i.)A|A)E"21"55 		)E*1-. * "),HA 8==QUaZ000 - &/"D]B/66t< '0 $);;=M.2]+ $1 + 's   C4c                     Sn[        U5       H8  u  nu  pVnX#U-   :  a#  USp(USU  H  u  pVnXh:X  d  M  US-  nM     X4s  $ X7-  nM:     g)zM
Map initial real root index to an index in a factor where
the root belongs.
r   Nr@   )r   )	r|   r   ru   rf   rg   r]   r   rH   r\   s	            r;   _reals_indexComplexRootOf._reals_indexT  sg     (1%(8$A$!1u}+Qe+0!9'Aa$,
 ,5 {" )9r>   c                     Sn[        U5       HM  u  nu  pVnX#U-   :  a8  USp(USU  H  u  pVnXh:X  d  M  US-  nM     U[        [        U   5      -  nX4s  $ X7-  nMO     g)zP
Map initial complex root index to an index in a factor where
the root belongs.
r   Nr@   )r   rD   r   )	r|   r   ru   rf   rg   r]   r   rH   r\   s	            r;   _complexes_indexComplexRootOf._complexes_indexh  s{     (1)(<$A$!1u}+Qe+4Ra='Aa$,
 ,9 \$/00{" )=r>   c                 &    [        S U 5       5      $ )z>Count the number of real or complex roots with multiplicities.c              3   ,   #    U  H
  u    pUv   M     g 7fr6   rY   )re   r]   rH   s      r;   rh   -ComplexRootOf._count_roots.<locals>.<genexpr>  s     *EA1Es   )sum)r|   r   s     r;   _count_rootsComplexRootOf._count_roots}  s     *E***r>   c                 *   [        U5      nU(       a%  [        U5      S:X  a  US   S   S:X  a
  US   S   U4$ U R                  U5      nU R                  U5      nX&:  a  U R	                  XR5      $ U R                  U5      nU R                  XrU-
  5      $ )z/Get a root of a composite polynomial by index. r@   r   )ra   rD   r   r  r  r   r  )r|   r\   ru   r   r^   r   reals_countr   s           r;   r   ComplexRootOf._indexed_root  s      % CLA%'!*Q-1*<1:a=%''w'&&u-##E11**73I''	;3FGGr>   c                 z    U R                   [        ;  a'  U R                  U R                   U R                  5        gg)z5Ensure that our poly has entries in the reals cache. N)r\   r   r   ru   r9   s    r;   r    ComplexRootOf._ensure_reals_init  s+    99L(tyy$**5 )r>   c                 z    U R                   [        ;  a'  U R                  U R                   U R                  5        gg)z9Ensure that our poly has entries in the complexes cache. N)r\   r   r   ru   r9   s    r;   _ensure_complexes_init$ComplexRootOf._ensure_complexes_init  s,    99,,tyy$**5 -r>   c                     [        U5      nU R                  U5      nU R                  U5      n/ n[        SU5       H$  nUR                  " U R                  X65      5        M&     U$ )z*Get real roots of a composite polynomial. r   )ra   r   r  r   r   r  )r|   r\   r^   r   r  r   ru   s          r;   r   ComplexRootOf._real_roots  s`      %w'&&u-1k*ELL))%78 + r>   c                 8    U R                  U R                  SS9  g)z
Reset all intervals
Fr   N)r   r\   r9   s    r;   _resetComplexRootOf._reset  s     			U3r>   c                 p   [        U5      nU R                  X2S9nU R                  U5      n/ n[        SU5       H$  nUR                  " U R                  XG5      5        M&     U R                  X2S9nU R                  U5      n	[        SU	5       H$  nUR                  " U R                  X5      5        M&     U$ )z6Get real and complex roots of a composite polynomial. r!  r   )ra   r   r  r   r   r  r   r  )
r|   r\   r   r^   r   r  r   ru   r   complexes_counts
             r;   r   ComplexRootOf._all_roots  s      %w<&&u-1k*ELL))%78 + &&w&D	**951o.ELL--i?@ / r>   c                     UR                  5       S:X  a  [        U5      $ U(       d  gUR                  5       S:X  a  [        U5      $ UR                  5       S:X  a   UR	                  5       (       a  [        U5      $ g)z7Compute roots in linear, quadratic and binomial cases. r@   Nrd   )r   r   r   lengthTCr   r   s      r;   r   ComplexRootOf._roots_trivial  sb     ;;=A%%;;=A"4(([[]aDGGII!$''r>   c                     UR                  5       nUR                  (       d  UR                  5       n[        U5      u  p1UR                  5       nUR                  (       d  [        SU-  5      eX14$ )zBTake heroic measures to make ``poly`` compatible with ``CRootOf``.z"sorted roots not supported over %s)r   r   r   r   r   r   )r|   r\   r   r   s       r;   _preprocess_rootsComplexRootOf._preprocess_roots  s`     oo||==?D&t,ooyy%4s:< < {r>   c                 \    Uu  p4U R                  X25      nUb  XT   $ U R                  X45      $ )z:Return the root if it is trivial or a ``CRootOf`` object. )r   r   )r|   r   rv   r\   ru   r   s         r;   r   ComplexRootOf._postprocess_root  s8     ""42<88D((r>   c                    UR                   (       d  [        S5      eUR                  5       n[        5       nUR	                  UR
                  U5      n[        S5      nUR                   Vs1 s H  n[        U5      iM     nn[        [        S5      4[        S5      5       H&  nUR                  U;  d  M  UR                  XV5      n  O   UR                  (       d  UR                  (       a  U R                  XU5      $ UR                   (       d"  UR"                  (       d  UR$                  (       a  U R'                  XU5      $ U R                  XU5      $ s  snf )z.Return postprocessed roots of specified kind. r   rj   )r   r   r   r   subsr   r   rE   strr0   r(   namereplaceis_QQr   _get_roots_qqis_AlgebraicFieldis_ZZ_Iis_QQ_I_get_roots_alg)	r|   methodr\   rv   r   drj   rf   
free_namess	            r;   r   ComplexRootOf._get_roots  s    !!!"KLLoo Gyy1%CL '+&7&78&7c!f&7
8(8(=>AvvZ'||A) ?
 99		$$V8<<""ckkS[[%%fH== $$V8<< 9s   ,Ec                     U R                  U5      u  pB/ n[        X5      " U5       H&  nUR                  " X@R                  Xc5      -  5        M(     U$ )zYReturn postprocessed roots of specified kind
for polynomials with rational coefficients. )r,  getattrr   r   )r|   r;  r\   rv   r   r   r   s          r;   r6  ComplexRootOf._get_roots_qq  sP     ++D1C(.DLL44TDDE / r>   c                    U R                  XR                  5       U5      n0 nUR                  5       S    HA  u  pgUS:X  a  UR                  U5      nOUS:X  a  UR	                  U5      nW H  n	XuU	'   M	     MC     [        5       n
/ nU H;  n	X;   d  M
  X;  d  M  XY   nUR                  U	/U-  5        U
R                  U	5        M=     U$ )a  Return postprocessed roots of specified kind
for polynomials with algebraic coefficients. It assumes
the domain is already an algebraic field. First it
finds the roots using _get_roots_qq, then uses the
square-free factors to filter roots and get the correct
multiplicity.
r@   r   r   )r6  liftsqf_listwhich_real_rootswhich_all_rootsr   r   add)r|   r;  r\   rv   r   subrootsr_   r`   
roots_filtr   
roots_seen
roots_flats               r;   r:  ComplexRootOf._get_roots_alg$  s     !!&))+x@MMOA&DA&//6
<'..u5
   ' U

A}!4K!!1#'*q!	  r>   c                 ,    [        5       q[        5       qg)a'  Reset cache for reals and complexes.

The intervals used to approximate a root instance are updated
as needed. When a request is made to see the intervals, the
most current values are shown. `clear_cache` will reset all
CRootOf instances back to their original state.

See Also
========

_reset
N)r3   r   r   )r|   s    r;   clear_cacheComplexRootOf.clear_cacheD  s     &')+r>   c                 "   U R                  5         U R                  (       a   [        U R                     U R                     $ [        [        U R                     5      nU R                  5         [        U R                     U R                  U-
     $ )z@Internal function for retrieving isolation interval from cache. r   is_realr   r\   ru   rD   r  r   )r:   r  s     r;   r   ComplexRootOf._get_intervalV  si    !<<		*4::66l49956K'')#DII.tzzK/GHHr>   c                 &   U R                  5         U R                  (       a!  U[        U R                     U R                  '   g[        [        U R                     5      nU R                  5         U[        U R                     U R                  U-
  '   g)z<Internal function for updating isolation interval in cache. NrQ  )r:   intervalr  s      r;   _set_intervalComplexRootOf._set_interval`  sh    !<<2:L#DJJ/l49956K'')DLTYY'

[(@Ar>   c                     U $ r6   rY   )r:   oldr   s      r;   
_eval_subsComplexRootOf._eval_subsj  s    r>   c                     U R                   (       a  U $ U R                  u  pU R                  XU R                  5       R                  (       a  S-   5      $ S-   5      $ )Nr@   )rR  r   funcr   r  )r:   r   rf   s      r;   _eval_conjugateComplexRootOf._eval_conjugaten  sI    <<K))yy););)=)B)BAKLLKLLr>   c           	      *	   [        U5      n[        U5         U R                  R                  nUR                  (       dJ  [        S5      nU R                  (       a	  U[        -  n[        XPR                  R                  XE5      5      nOKU R                  nU R                  (       a#  U R                  R                  U[        U-  5      n[        XG5      nU R                  5       n U R                  (       a  [        [        UR                  5      5      n	[        [        UR                   5      5      n
X:X  a  U	nGO[        [        UR"                  5      5      nU[        [        UR$                  5      5      S-  -   nGOU R                  (       a  [        [        UR&                  5      5      n	[        [        UR(                  5      5      n
X:X  a  [+        [        S5      U	5      nGO[        [        UR"                  S   5      5      nU[        [        UR,                  5      5      S-  -   nO[        [        UR.                  5      5      n[        [        UR0                  5      5      n[        [        UR&                  5      5      n[        [        UR(                  5      5      nX:X  a  UU:X  a  [+        UU5      nGO/[+        [3        [        UR"                  5      6 nU[+        [3        [        UR$                  UR,                  45      6 S-  -   n [5        XlU45      nU R                  (       d  U R                  (       ae  [7        UR8                  5      U R                  :X  dA  W	Us=::  a  W
::  a4  O  OgU R                  (       a  [+        [        S5      UR:                  5      nOIO5WUR:                  s=::  a  W::  a  O  OWUR8                  s=::  a  W::  a  O  OOURA                  5       nGM	  SSS5        U RC                  W5        U(       a  W$ [D        RF                  " WR:                  RH                  U5      [        [D        RF                  " UR8                  RH                  U5      -  -   $ ! [<        [>        4 a     Nf = f! , (       d  f       N= f)a  Evaluate this complex root to the given precision.

This uses secant method and root bounds are used to both
generate an initial guess and to check that the root
returned is valid. If ever the method converges outside the
root bounds, the bounds will be made smaller and updated.
rj      0r@   N)%r-   r,   r\   r   r   r   is_imaginaryr   r%   r   r1  r   rR  r)   r2  r   bcenterdxr   r   r*   dyr   r   mapr+   boolimagrealUnboundLocalErrorrL   r   rV  r   r   _mpf_)r:   nreturn_mpmathprecr   r<  r^  r   rU  r   re  r   x0x1r   r   r   r   s                     r;   eval_approxComplexRootOf.eval_approxt  s>    1~d^		A;;#J$$FA99>>!#78yy$$99>>!QqS1D())+H<<C

O,AC

O,Av S12Bc#hkk"23A55B&&C,-AC,-Av"3s8Q/S!345Bc#hkk"23A55BS-.BS-.BS-.BS-.BxB"H"2r{c#x78Bc3sX[[(++,F#GHJJB $Dr(3D ||t'8'8#DII$,,> !TQ#00'*3s8TYY'?!		/R/B$))4Ir4I $??,o  R 	8$K

499??D1ejj$//0 	1 *:6 G ^sU   K:RA!Q.3R40Q.$R&Q.?R Q.R.R>R RR
Rc                 P    U R                  [        U5      S9R                  U5      $ )z2Evaluate this complex root to the given precision.ro  )eval_rationalr.   _evalf)r:   rq  kwargss      r;   _eval_evalfComplexRootOf._eval_evalf  s(     !!K$5!6==dCCr>   c                    U=(       d    UnU(       aY  Sn[        U[        5      (       a  UO[        [        U5      5      n[        U[        5      (       a  UO[        [        U5      5      nO[        S5      US-   * -  nU R	                  5       n U R
                  (       a{  U(       a  [        UR                  U-  5      nUR                  US9nUR                  n[        U5      n[        R                  nU(       a  UR                  [        Xd-  5      :  a  GOIGODU R                  (       a  U(       a  [        UR                  S   U-  5      nSnUR                  XS9nUR                  S   n[        U5      n[        R                  nU(       a  UR                  [        Xd-  5      :  a  OOU(       a6  [        UR                  S   U-  5      n[        UR                  S   U-  5      nUR                  X5      nUR                  n[        [        U5      u  pxU(       a>  UR                  [        US   U-  5      :  a   UR                  [        US   U-  5      :  a  OGM  U R                  U5        U[        U-  -   $ )a-  
Return a Rational approximation of ``self`` that has real
and imaginary component approximations that are within ``dx``
and ``dy`` of the true values, respectively. Alternatively,
``n`` digits of precision can be specified.

The interval is refined with bisection and is sure to
converge. The root bounds are updated when the refinement
is complete so recalculation at the same or lesser precision
will not have to repeat the refinement and should be much
faster.

The following example first obtains Rational approximation to
1e-8 accuracy for all roots of the 4-th order Legendre
polynomial. Since the roots are all less than 1, this will
ensure the decimal representation of the approximation will be
correct (including rounding) to 6 digits:

>>> from sympy import legendre_poly, Symbol
>>> x = Symbol("x")
>>> p = legendre_poly(4, x, polys=True)
>>> r = p.real_roots()[-1]
>>> r.eval_rational(10**-8).n(6)
0.861136

It is not necessary to a two-step calculation, however: the
decimal representation can be computed directly:

>>> r.evalf(17)
0.86113631159405258

N
   rd   )rg  r@   )rg  rh  r   )rC   r   r2  r   r   rR  absrf  refine_sizeZerorg  rd  rh  ri  rV  r   )	r:   rg  rh  ro  rtolrU  r   rl  rk  s	            r;   rx  ComplexRootOf.eval_rational  s	   B X2D!"h//Xc"g5FB!"h//Xc"g5FB R5AE(?D%%'||X__T12B#//2/6OO{vvx{{S[8  9""X__Q/45BB#//2/=OOA&{vvx{{S[8  9 X__Q/45BX__Q/45B#//7OO 1-
 c!A$t)n4 c!A$t)n4= D 	8$af}r>   rY   )NFTT)F)NN   )7rS   rT   rU   rV   rW   r   
is_complex	is_number	is_finiteis_algebraicr}   classmethodr   r   propertyr   r   rE   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r   r   r  r   r"  r   r   r   r,  r   r   r6  r:  rN  r   rV  rZ  r_  rt  r{  rx  rX   rY   r>   r;   r   r      s   @D IJIIL:=x  ' # # 0 0  9
 = = < < 	 	    $  $  0  8  8  <  &  ( + + H H,6
6
  4  (       ) ) = =: 	 	  > , ,"IMMV1pD
Pr>   r   c                 
    X:H  $ r6   rY   )lhsrhss     r;   _eval_is_eqr  '  s     :r>   c                    UR                   (       d  g UR                  (       d  gU R                  R                  U R                  R                  R                  5       U5      R                  nUSL a  gUR                  UR                  4nU R                  U R                  4nS U;  d   eX4:w  a  S U;  a  gUR                  5       u  pVU R                  (       am  U(       a  gU R                  5       nUR                  UR                  4 Vs/ s H  n[        [        U5      5      PM     snu  p[        X:*  =(       a    X:*  5      $ U R                  5       nUR                   UR"                  UR$                  UR&                  4 Vs/ s H  n[        [        U5      5      PM     snu  pp[)        X5      =(       a/    [)        X]5      =(       a    [)        X5      =(       a    [)        Xo5      $ s  snf s  snf )NF)r  r  r   r1  rE   popis_zerorR  rd  as_real_imagr   r   re  r   r2  r   r   r   r   r   r   )r  r  zosreimrf   r]   r   re  rg   r1r2i1i2s                   r;   r  r  -  s~    ====chh++//137??AEzS%%%AS%%%Aq==v$a-FB
{{+,33*5*QQ *5qx,CH--A	addADD!$$1  ! 1 1hs1v& 1  !NBB=KU2\KeBlKuR|K 6!s   G,G1c                       \ rS rSrSrSrSS jr\SS j5       r\SS j5       r	\S 5       r
\S	 5       r\S
 5       rS r\S 5       r\S 5       r\S 5       r\S 5       rS rS rS rSrg)RootSumiM  z:Represents a sum of all roots of a univariate polynomial. )r\   funautoNc           	         U R                  X5      u  pgUR                  (       d  [        S5      eUc!  [        UR                  UR                  5      nOn[        USS5      nU(       aL  SUR                  ;   a<  [        U[        5      (       d&  [        UR                  U" UR                  5      5      nO[        SU-  5      eUR                  S   UR                  pU[        R                  La  UR                  XU	-  5      nUR                  5       n
UR                  U	5      (       d  X-  $ UR                   (       a  UR#                  U	5      u  pO[        R$                  nUR&                  (       a  UR#                  U	5      u  pO[        R                  n[        X5      nU R)                  Xr5      n[+        U5      / pU H  u  nnUR,                  (       a  U" [/        U5      S   5      nOiU(       a0  UR0                  (       a  [3        [5        U[7        U5      5      5      nO2U(       a  U(       d  U R9                  XrU5      nOU R;                  Xr5      nUR=                  UU-  5        M     U[?        U6 -  X-  -   $ )z>Construct a new ``RootSum`` instance of roots of a polynomial.r   is_FunctionFr@   z&expected a univariate function, got %sr   ) 
_transformr   r   r   r   r@  nargsrC   rL   	variablesr   r   Oner1  r   hasis_Addas_independentr  is_Mul_is_func_rationalra   	is_linearr   is_quadraticr  ri  r   r   _rational_caser   r
   )r|   r   r^  rj   r  	quadraticr   r\   is_funcvardeg	add_const	mul_constrationalr^   rk   rH   terms                     r;   r}   RootSum.__new__S  s   nnT-!!-9; ; <$((DHH-DdM59G1

?!$//!$((DN;D <tCE E NN1%tyyT99S),Dkkmxx}}8O;;"11#6OItI;;"11#6OItIc ((4&t,bGD!~~L.q12t003t_T%:;<t88D5D--d9DLL4   e$s}44r>   c                 V    [         R                  " U 5      nXl        X$l        X4l        U$ )z(Construct new raw ``RootSum`` instance. )r   r}   r\   r  r  )r|   r\   r^  r  r   s        r;   r   RootSum._new  s&     ll3
r>   c                     UR                   R                  " UR                  6 (       d  UR                   $ U R                  X5      nU(       a  U(       d  U R	                  XU5      $ U R                  X5      $ )z$Construct new ``RootSum`` instance. )r   r  r  r  r   r  )r|   r\   r^  r  r  s        r;   r   RootSum.new  sY     yy}}dnn-99((4t88D--%%d11r>   c                 ,    [        XSS9n[        U5      $ )z)Transform an expression to a polynomial. F)r   )r    r   )r|   r   rj   r\   s       r;   r  RootSum._transform  s     .%%r>   c                 X    UR                   S   UR                  pCUR                  U5      $ )z*Check if a lambda is a rational function. r   )r  r   is_rational_function)r|   r\   r^  r  r   s        r;   r  RootSum._is_func_rational  s)     NN1%tyyT((--r>   c                 2  ^^ [        SUR                  5       -  5      nUR                  S   UR                  smm[	        UU4S jU 5       5      n[        U5      R                  5       u  pV[        U   nUR                  5       nUR                  5       n [        XWSS9n[        UR                  5       6 u  p [        XgSS9n[        UR                  5       6 u  p[        X-   SS9u  p[        X5      / p[        X5       H  u  u  nnu  nnUR                  UU45        M!     [!        U5       H  u  nu  nnUR#                  U5      UU'   M     [%        U	5      nUSU n	UUS nUb8  [        ['        [        WU	5      5      /UR(                  Q76 R+                  5       nOU	u  nUb8  [        ['        [        W
U5      5      /UR(                  Q76 R+                  5       nOUu  n[-        XV-  5      $ ! [         a    SU4p GNHf = f! [         a    SU4p GN:f = f)	z#Handle the rational function case. zr:%dr   c              3   H   >#    U  H  nTR                  TU5      v   M     g 7fr6   )r1  )re   r   r   r  s     r;   rh   )RootSum._rational_case.<locals>.<genexpr>  s     15a		#q!!5s   "F)domainrB   NT)formal)r   r   r  r   r  r"   as_numer_denomr   rB   r   ziprk   r   r   r   r   r   r1  rD   rn   gensr   r!   )r|   r\   r^  r   r_   pqr  p_monomp_coeffq_monomq_coeffcoeffsmappingformulasvaluessymr]   valrf   r   ro  r   r  s                         @@r;   r  RootSum._rational_case  s    ./NN1%tyy	T1511{))+EHHJHHJ	/Qe4A  #AGGIG	/Qe4A  #AGGIG$W%6tD -r&"%g"8HS!hq#MM3*% #9 'v.MAzq

6*F1I / L!**=T#gw/0:166:BBDADQ=T#gw/0:166:BBDADQac{I   	$tw	$   	$tw	$s$   
G/ 9
H /H HHHc                 2    U R                   U R                  4$ r6   )r\   r  r9   s    r;   r   RootSum._hashable_content  s    		488$$r>   c                 6    U R                   R                  5       $ r6   r   r9   s    r;   r   RootSum.expr  r   r>   c                 \    U R                   U R                  U R                  R                  4$ r6   )r   r  r\   r   r9   s    r;   r   RootSum.args  s    		488TYY]]33r>   c                 \    U R                   R                  U R                  R                  -  $ r6   )r\   rE   r  r9   s    r;   rE   RootSum.free_symbols  s!    yy%%(=(===r>   c                     g)NTrY   r9   s    r;   is_commutativeRootSum.is_commutative  s    r>   c                    UR                  SS5      (       d  U $ [        U R                  SS9n[        U5      U R                  R	                  5       :  a  U $ [        U Vs/ s H  o0R                  U5      PM     sn6 $ s  snf )Nr   T)multiple)getr   r\   rD   r   r
   r  )r:   hints_rootsr   s       r;   doitRootSum.doit  si    yy$''Ktyy40v;))++Kf5f!f5665s    A>c                      U R                   R                  [        U5      S9n[        U Vs/ s H  o0R	                  U5      PM     sn6 $ s  snf ! [
        [        4 a    U s $ f = f)Nrw  )r\   nrootsr.   r
   r  r   r   )r:   rq  r  r   s       r;   r{  RootSum._eval_evalf  sc    	7YY%%D(9%:F f5f!f5665 _- 	K	s   "A AA%$A%c                     U R                   R                  u  p#[        X#R                  U5      5      nU R	                  U R
                  X@R                  5      $ r6   )r  r   r   diffr   r\   r  )r:   rj   r  r   r^  s        r;   _eval_derivativeRootSum._eval_derivative  s<    HHMM	c99Q<(xx		433r>   rY   )NNTFr  )rS   rT   rU   rV   rW   r   r}   r  r   r   r  r  r  r   r  r   r   rE   r  r  r{  r  rX   rY   r>   r;   r  r  M  s    D'I:5x   
2 
2 & &
 . .
 3 3j% # # 4 4 > >  	774r>   r  r   )LrW   sympy.core.basicr   
sympy.corer   r   r   r   r   r	   r
   r   r   r   r   r   sympy.core.cacher   sympy.core.relationalr   sympy.core.sortingr   sympy.polys.domainsr   sympy.polys.polyerrorsr   r   r   r   sympy.polys.polyfuncsr   r   sympy.polys.polyrootsr   r   r   r   r   sympy.polys.polytoolsr   r    r!   sympy.polys.rationaltoolsr"   sympy.polys.rootisolationr#   r$   sympy.utilitiesr%   r&   r'   r(   mpmathr)   r*   r+   r,   mpmath.libmp.libmpfr-   r.   sympy.multipledispatchr/   	itertoolsr0   __all__r3   r   r   ra   rr   rw   ry   r   r1   r  r  rY   r>   r;   <module>r     sR   8 #' ' ' ' $ ' & " 
 4  9 8 .  E D / / 8 +  +C CJ !# @

* H H, KT K K zF z zz  
-' (
 
-L  L> D4d D4 D4r>   