
    wg                        d Z ddlmZmZmZmZ ddlmZmZm	Z	m
Z
mZmZmZ ddlmZmZmZmZmZmZmZmZmZ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"m#Z# d	 Z$d
 Z%d Z&d Z'd Z(d Z)d Z*d Z+dbdZ,dcdZ-dddZ.dddZ/dedZ0d Z1dcdZ2dcdZ3dfdZ4dcdZ5dcdZ6dgdZ7dbdZ8dgdZ9dhd Z:d!Z;d"Z<d#Z=d$Z>d%Z?d&Z@d'ZAd(ZBd)ZCi e<e<fde=e=fde>e>fde?e?fde@e<fd*eAe=fd*eBe>fd*eCe?fd*e@e?fd+eAe<fd+eBe=fd+eCe>fd+e<eAfd,e=eBfd,e>eCfd,e?e@fd,e<e@fd-e=eAfd-e>eBfd-e?eCfd-e<e=fd.e=e>fd.e>e?fd.e?e<fd.e<e?fd/e=e<fd/e>e=fd/e?e>fd/iZDi e@e;e<fd0eAe;e=fd0eBe;e>fd0eCe;e?fd0e@e;e?fd1eAe;e<fd1eBe;e=fd1eCe;e>fd1e<e;eAfd2e=e;eBfd2e>e;eCfd2e?e;e@fd2e<e;e@fd3e=e;eAfd3e>e;eBfd3e?e;eCfd3e@eBfd4i eAeCfd4eBe@fd4eCeAfd4e@e;eBfd4eAe;eCfd4eBe;e@fd4eCe;eAfd4e<e>fd5e=e?fd5e>e<fd5e?e=fd5e<e;e>fd5e=e;e?fd5e>e;e<fd5e?e;e=fd5e@eAfd6eAeBfd6i eBeCfd6eCe@fd6e@e;eAfd6eAe;eBfd6eBe;eCfd6eCe;e@fd6e@eCfd7eAe@fd7eBeAfd7eCeBfd7e@e;eCfd7eAe;e@fd7eBe;eAfd7eCe;eBfd7e<eBfd8e=eCfd8e>e@fd8i e?eAfd8e<e;eBfd8e=e;eCfd8e>e;e@fd8e?e;eAfd8e<eCfd9e=e@fd9e>eAfd9e?eBfd9e<e;eCfd9e=e;e@fd9e>e;eAfd9e?e;eBfd9e@e>fd:eAe?fd:eBe<fd:eCe=fd:i e@e;e>fd:eAe;e?fd:eBe;e<fd:eCe;e=fd:e@e=fd;eAe>fd;eBe?fd;eCe<fd;e@e;e=fd;eAe;e>fd;eBe;e?fd;eCe;e<fd;e<e;e=fd<e=e;e>fd<e>e;e?fd<e?e;e<fd<e<e;e?fd=e=e;e<fd=e>e;e=fd=e?e;e>fd=e@e;e@fd>eAe;eAfd>eBe;eBfd>eCe;eCfd>e<e;e<fd?e=e;e=fd?e>e;e>fd?e?e;e?fd?iZEi dd@gd*dAgd+dBgd,dAgd-dBgd0dCdAgd1dDdBgd2dCdAgd3dDdBgd.dEgd/dFgd4dGdHgd5dGdHgd6dEdIgd7dJdFgd8dKdLgd9dMdNgdMdNgdKdLgdEdIgdJdFgdOd@gdOd@gdPZFdQ ZGdR ZHdidSZIdT ZJdU ZKdjdVZLdW ZMdX ZNdY ZOdZ ZPddd[ZQdcd\ZRddd]ZS G d^ d_      ZT G d` da      ZUy)kz;Real and complex root isolation and refinement algorithms.     )dup_neg
dup_rshiftdup_remdup_l2_norm_squared)dup_LCdup_TC
dup_degree	dup_stripdup_reversedup_convertdup_terms_gcd)
dup_clear_denoms
dup_mirror	dup_scale	dup_shiftdup_transformdup_diffdup_evaldmp_eval_indup_sign_variationsdup_real_imag)dup_discriminant)dup_factor_list)RefinementFailedDomainErrorPolynomialError)dup_sqf_partdup_sqf_listc                     |j                   st        d|z        t        | |      } | t        | d|      g}|d   r4t	        |d   |d   |      }|j                  t        ||             |d   r4|dd S )a   
    Computes the Sturm sequence of ``f`` in ``F[x]``.

    Given a univariate, square-free polynomial ``f(x)`` returns the
    associated Sturm sequence ``f_0(x), ..., f_n(x)`` defined by::

       f_0(x), f_1(x) = f(x), f'(x)
       f_n = -rem(f_{n-2}(x), f_{n-1}(x))

    Examples
    ========

    >>> from sympy.polys import ring, QQ
    >>> R, x = ring("x", QQ)

    >>> R.dup_sturm(x**3 - 2*x**2 + x - 3)
    [x**3 - 2*x**2 + x - 3, 3*x**2 - 4*x + 1, 2/9*x + 25/9, -2079/4]

    References
    ==========

    .. [1] [Davenport88]_

    z%Cannot compute Sturm sequence over %s   N)is_Fieldr   r   r   r   appendr   )fKsturmss       ^/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/polys/rootisolation.py	dup_sturmr*       s    2 ::AAEFFQAAq!"E
)E"IuRy!,WQ]# ) ":    c                 \   t        |       g }}||j                  gz  }t        | |      dk  rt        | |      } t	        t        |             } t        d|      D ]  }| |   dk\  r|j                  | |    d      g }}t        |dz   |      D ]B  }| |   dk  r||   |z   |j                  | |   d      z
  }	|j                  |	||z
  z  |g       D |s{t        |      }	||	d      dz   ||	d   <   |j                  |	d           |sy |j                         d      t        |      dz   z  S )a  Compute the LMQ upper bound for the positive roots of `f`;
       LMQ (Local Max Quadratic) was developed by Akritas-Strzebonski-Vigklas.

    References
    ==========
    .. [1] Alkiviadis G. Akritas: "Linear and Quadratic Complexity Bounds on the
        Values of the Positive Roots of Polynomials"
        Journal of Universal Computer Science, Vol. 15, No. 3, 523-537, 2009.
    r      r    N)lenoner   r   listreversedrangelogr$   min	get_fieldmax)
r%   r&   nPtiaQLjqs
             r)   dup_root_upper_boundr?   F   sD    q62qA	QUUGAa|aAqMXa[A1a[ Q419qteQ2q1ua 	)Atqy!q1551q>)AIIqQU|Q'(	) GAaD'A+!A$	1+. q{{}Q#a&1*--r+   c                 <    t        t        |       |      }|d|z  S y)a  Compute the LMQ lower bound for the positive roots of `f`;
       LMQ (Local Max Quadratic) was developed by Akritas-Strzebonski-Vigklas.

       References
       ==========
       .. [1] Alkiviadis G. Akritas: "Linear and Quadratic Complexity Bounds on the
              Values of the Positive Roots of Polynomials"
              Journal of Universal Computer Science, Vol. 15, No. 3, 523-537, 2009.
    Nr    )r?   r   )r%   r&   bounds      r)   dup_root_lower_boundrB   r   s&     !Q3Ewr+   c                    t        |       }|dk  rt        d      |j                  r |j                         }t	        | ||      |}} n7|j
                  r|j                  s|j                  rt        d|z        | dd } |j                  | d         r%| j                          |j                  | d         r%t        |       dk(  r|j                  S | d   |j                  t        fd| dd D              z   S )z
    Compute the Cauchy upper bound on the absolute value of all roots of f,
    real or complex.

    References
    ==========
    .. [1] https://en.wikipedia.org/wiki/Geometrical_properties_of_polynomial_roots#Lagrange's_and_Cauchy's_bounds
    r    zPolynomial has no roots.z"Cauchy bound not supported over %sNr!   r   c              3   :   K   | ]  }t        |z          y wN)abs).0r7   lcs     r)   	<genexpr>z)dup_cauchy_upper_bound.<locals>.<genexpr>   s     2qs1r6{2s   )r	   r   is_ZZr5   r   is_QQis_RRis_CCr   is_zeropopr.   zeror/   r6   )r%   r&   r7   LrH   s       @r)   dup_cauchy_upper_boundrR      s     	1A1u899wwKKM1a#Q1WW177 >BCCaD
))AbE
	 ))AbE

1v{vv	
1B5532AabE2222r+   c                     t        |       }t        |      dk  rt        d      |j                  r|j	                         }t        ||      }|j                  |z  S )zhCompute the Cauchy lower bound on the absolute value of all non-zero
       roots of f, real or complex.r-   z!Polynomial has no non-zero roots.)r   r.   r   rJ   r5   rR   r/   )r%   r&   gbs       r)   dup_cauchy_lower_boundrV      sP     	AA
1vzABBwwKKMq!$A5519r+   c                    t        |       }|dk  rt        d      |j                  r |j                         }t	        | ||      |}} n2|j
                  r|j                  s|j                  rt        d|z        t        | |      }t        | |      } |d      |j                  |      z   ||      |dz   z  ||dz
  z  z  z  S )a  
    Return the square of the Mignotte lower bound on separation between
    distinct roots of f. The square is returned so that the bound lies in
    K or its quotient field.

    References
    ==========

    .. [1] Mignotte, Maurice. "Some useful bounds." Computer algebra.
        Springer, Vienna, 1982. 259-263.
        https://people.dm.unipi.it/gianni/AC-EAG/Mignotte.pdf
    r-   z1Polynomials of degree < 2 have no distinct roots.z$Mignotte bound not supported over %s   r    )r	   r   rJ   r5   r   rK   rL   rM   r   r   r   rF   )r%   r&   r7   rQ   Dl2sqs         r)   dup_mignotte_sep_bound_squaredr[      s     	1A1uQRRwwKKM1a#Q1WW177 @1DEEAAq!$DQ4a=QqTAaC[4!A#;688r+   c                     | \  }}|j                  |      |j                  |      }}|j                  |      |j                  |      }}||||fS )z0Convert an open interval to a Mobius transform. )numerdenom)Ifieldr(   r9   r;   crU   ds           r)   _mobius_from_intervalrc      sK    DAq;;q>5;;q>qA;;q>5;;q>qAaA:r+   c                 N    | \  }}}} |||       |||      }}||k  r||fS ||fS )z0Convert a Mobius transform to an open interval.  )Mr`   r;   rU   ra   rb   r(   r9   s           r)   _mobius_to_intervalrg      s=    JAq!QA;aqAAv1v1vr+   c                    |\  }}}}||k(  r||k(  r| ||||ffS t        | |      }| |t        |            }n|j                  }|r(|dkD  r#t        | ||      } ||z  ||z  |j                  }}}||j                  k\  r<t        | ||      } ||z  |z   ||z  |z   }}t        | |j                  |      s| ||||ffS t        | |j                  |      | }	} |||z   |||z   f\  }
}}}t        | |j                  |      s| ||||ffS t        | |      }|dk(  r|
|||f\  }}}}nUt        t        |	      |j                  |      } t        | |j                  |      st        | d|      } |||z   |||z   f\  }}}}| ||||ffS )z5One step of positive real root refinement algorithm.    r    )
rB   intrP   r   r/   r   r   r   r   r   )r%   rf   r&   fastr;   rU   ra   rb   ArT   a1b1c1d1ks                  r)   dup_step_refine_real_rootrr      s   JAq!QAv!q&1aA,Q"A}c!fIFFBaAA#qsAEEa1AEEzaAsQw!a11661%q!Ql?"Qq!1qAAq!a%'NBBAqvvq!2r2r"""Aq!AAvR^
1ak!naeeQ/1661%1a#AAq!a%'
1aq!Ql?r+   Nc                    |j                         }t        |      dk(  rt        ||      \  }	}
}}n|\  }	}
}}|st        | |	|
||f||      \  } \  }	}
}}|s|P|Nt	        d|      D ]>  }t         ||	|       ||
|      z
        |k\  rt        | |	|
||f||      \  } \  }	}
}}> n n|Zt         ||	|       ||
|      z
        |k\  r;t        | |	|
||f||      \  } \  }	}
}}t         ||	|       ||
|      z
        |k\  r;|,t	        d|      D ]  }t        | |	|
||f||      \  } \  }	}
}} |;	 t        |	|
||f|      \  }}||k  s||k  rnt        | |	|
||f||      \  } \  }	}
}}:|st        |	|
||f|      S | |	|
||ffS )zGRefine a positive root of `f` given a Mobius transform or an interval. r-   rk   r   )r5   r.   rc   rr   r2   rF   rg   )r%   rf   r&   epsstepsdisjointrk   mobiusFr;   rU   ra   rb   r:   uvs                   r)   dup_inner_refine_real_rootr|     s
   	A
1v{*1a0
1a
1a3A1a8<Aq!Q  5,q% 	A1Q7Qq!W$%,";A1a|QUY"Z<Aq!Q		 ?a1g!Q'(C/";A1a|QUY"Z<Aq!Q a1g!Q'(C/ 1e_ [";A1a|QUY"Z<Aq!Q[ &1a|Q7DAqH}A";A1a|QUY"Z<Aq!Q  "Aq!Q<331aA,r+   c           	          t        ||f|j                               \  }}	}
}t        | t        ||	g      t        |
|g      |      } t	        | |      dk7  rt        d|d|d      t        | ||	|
|f|||||      S )z:Refine a positive root of `f` given an interval `(s, t)`. r    z%there should be exactly one root in (, z
) intervalru   rv   rw   rk   )rc   r5   r   r
   r   r   r|   )r%   r(   r9   r&   ru   rv   rw   rk   r;   rU   ra   rb   s               r)   dup_outer_refine_real_rootr   7  s    &1vq{{}=JAq!QaAq6*"Aq6*A	/A 1a A%Z[]^_``%a!Q1qcYahlmmr+   c           
         |j                   r#t        | |d      |j                         c\  }} }n|j                  st	        d|z        ||k(  r||fS ||kD  r||}}d}	|dk  r/|dk  rt        | |      | | df\  } }}}	nt        d|d|d      |	r||dk  r| }nd	}t        | |||||||
      \  }}|	r| | fS ||fS )zBRefine real root's approximating interval to the given precision. Tconvertz*real root refinement not supported over %sFr   Cannot refine a real root in (r~   )Nr   )rK   r   get_ringrJ   r   r   
ValueErrorr   )
r%   r(   r9   r&   ru   rv   rw   rk   _negatives
             r)   dup_refine_real_rootr   C  s    ww$Q48!**,	AWWFJKKAv1v1u!1H1u6 *1a 01"qb$ >Aq!X1MNNH(a< yHH%	1a58$HDAq QBxAwr+   c           
         |j                   |j                  |j                  |j                   f\  }}}}t        | |      }|dk(  rg S |dk(  rt        | ||||f|||d      g}	|	S g ||||| |fg}
}	|
r|
j	                         \  }}}}} }t        | |      }| |t        |            }n|j                  }|r(|dkD  r#t        | ||      } ||z  ||z  |j                   }}}||j                   k\  rt        | ||      } ||z  |z   ||z  |z   }}t        | |      s$|	j                  | ||||ff       t        | d|      } t        | |      }|dk(  r|dk(  r&|	j                  t        | ||||f|||d             	t        | |j                   |      }|||z   |||z   df\  }}}}}t        ||      s&|	j                  |||||ff       t        |d|      d}}t        ||      }||z
  |z
  }|||z   |||z   f\  }}}}|dkD  rFt        t        |       |j                   |      }t        ||      st        |d|      }t        ||      }nd}||k  r!||||f\  }}}}||||f\  }}}}||||f\  }}}}|s|9t        t        |       |j                   |      }t        ||      st        |d|      }|dk(  r%|	j                  t        |||||f|||d             n|
j                  ||||||f       |s|9t        t        |       |j                   |      }t        ||      st        |d|      }|dk(  r%|	j                  t        |||||f|||d             n|
j                  ||||||f       |
r|	S )aN  Internal function for isolation positive roots up to given precision.

       References
       ==========
           1. Alkiviadis G. Akritas and Adam W. Strzebonski: A Comparative Study of Two Real Root
           Isolation Methods . Nonlinear Analysis: Modelling and Control, Vol. 10, No. 4, 297-304, 2005.
           2. Alkiviadis G. Akritas, Adam W. Strzebonski and Panagiotis S. Vigklas: Improving the
           Performance of the Continued Fractions Method Using new Bounds of Positive Roots. Nonlinear
           Analysis: Modelling and Control, Vol. 13, No. 3, 265-279, 2008.
    r   r    T)ru   rk   rx   Nri   )r/   rP   r   r|   rO   rB   rj   r   r   r   r$   r   r   )r%   r&   ru   rk   r;   rU   ra   rb   rq   rootsstackrl   f1rm   rn   ro   rp   rk1k2a2b2c2d2f2s                            r)   dup_inner_isolate_real_rootsr   f  s    -JAq!QAq!AAv	Av+1a|QCd4A B| Lw Q1aA./u$yy{Aq!Q1$Q*A}c!fIFFBaA&A#qsAEEa1AEEzaA&sQw!a1a|LL!aAq\!23"1a+A'1-66LL!;Aq!Q<$t"M N1aeeQ'B !1q5!QUA 5BBA"a=b2r2r"234"2q!,aA$R+BR!BAq!a%/NBBAv{1~quua8b!}#B1-B(Q/Bw!#RRBB!#RRBB!#RRBBz{1~quua8b!}#B1-BQw7RR(!4N O b"b"b"56z{1~quua8b!}#B1-BQw7RR(!4N O b"b"b"56o r Lr+   c                     |j                         }	 t        ||      \  }	}
|r|
 |	 }
}	||	|k\  r||
|k  r
|s|	|
fS | |fS ||	|kD  s||
|k  ryt        | |||      \  } }Q)z9Discard an isolating interval if outside ``(inf, sup)``. Nrt   )r5   rg   rr   )r%   rf   infsupr&   r   rk   rx   ry   rz   r{   s              r)   _discard_if_outside_intervalr     s    	A
"1a(12rqAK18#+c!t!to!c's1s7,Q14@DAq r+   c                 *   ||dk  rg S t        | |||      }|j                         g }	}||2|D ]+  \  } }
t        | |
|||d||      }||	j                  |       - |	S |s.|D ]'  \  } }
t	        |
|      \  }}|	j                  ||f       ) |	S |}	|	S )z@Iteratively compute disjoint positive root isolation intervals. r   ru   rk   F)r   r5   r   r$   rg   )r%   r&   ru   r   r   rk   rx   r   ry   resultsrf   resultrz   r{   s                 r)    dup_inner_isolate_positive_rootsr     s    
37	(A3TBEwA
#/ 	'DAq1!QS!UDRXYF!v&		' N  	#DAq&q!,DAqNNAq6"	# N Nr+   c                 B   ||dk\  rg S t        t        | |      |||      }|j                         g }	}||2|D ]+  \  } }
t        | |
|||d||      }||	j	                  |       - |	S |s0|D ])  \  } }
t        |
|      \  }}|	j	                  | | f       + |	S |}	|	S )z@Iteratively compute disjoint negative root isolation intervals. r   r   T)r   r   r5   r   r$   rg   )r%   r&   r   r   ru   rk   rx   r   ry   r   rf   r   rz   r{   s                 r)    dup_inner_isolate_negative_rootsr     s    
3!8	(Aq)91#DQEwA
#/ 	'DAq1!QS!T4QWXF!v&		' N  	%DAq&q!,DAqNNQB8$	% N Nr+   c                 N   t        | |      \  }} |dkD  r|j                         }||dk  rw|d|k  rp|sS|s|j                  |j                  f|fg| fS |j                  |j                  f||j                  |j                  gfg| fS |j                  |j                  fg| fS g | fS )z?Handle special case of CF algorithm when ``f`` is homogeneous. r   )r   r5   rP   r/   )r%   r&   r   r   basissqfr=   ry   s           r)   _isolate_zeror     s    ADAq1uKKMK3!8#+cffaff-q12A55ffaff-q155!&&/BCQFF()1,,q5Lr+   c           	         |j                   r#t        | |d      |j                         c\  }} }n|j                  st	        d|z        t        |       dk  rg S t        | |||dd      \  }} t        | |||||      }	t        | |||||      }
t        |	|z   |
z         }|s|S |D cg c]  \  }}t        ||f| |       c}}S c c}}w )a  Isolate real roots of a square-free polynomial using the Vincent-Akritas-Strzebonski (VAS) CF approach.

       References
       ==========
       .. [1] Alkiviadis G. Akritas and Adam W. Strzebonski: A Comparative
              Study of Two Real Root Isolation Methods. Nonlinear Analysis:
              Modelling and Control, Vol. 10, No. 4, 297-304, 2005.
       .. [2] Alkiviadis G. Akritas, Adam W. Strzebonski and Panagiotis S.
              Vigklas: Improving the Performance of the Continued Fractions
              Method Using New Bounds of Positive Roots. Nonlinear Analysis:
              Modelling and Control, Vol. 13, No. 3, 265-279, 2008.

    Tr   -isolation of real roots not supported over %sr   Fr   r   ru   r   r   rk   )rK   r   r   rJ   r   r	   r   r   r   sortedRealInterval)r%   r&   ru   r   r   rk   blackboxr   I_zeroI_negI_posr   r;   rU   s                 r)   dup_isolate_real_roots_sqfr   .  s     	ww$Q48!**,	AWWIAMNN!}	aCEtDIFA,QsSWXE,QsSWXE56>E)*E:?AAq!fa+AAAs   *Cc           	      .   |j                   r#t        | |d      |j                         c\  }} }n|j                  st	        d|z        t        |       dk  rg S t        | ||||d      \  }} t        | |      \  }}	t        |	      dk(  rZ|	\  \  } }
t        | |||||      }t        | |||||      }|D cg c]  \  }}||f|
f }}}|D cg c]  \  }}||f|
f }}}nt        |	||||||	      \  }}t        ||z   |z         S c c}}w c c}}w )
a  Isolate real roots using Vincent-Akritas-Strzebonski (VAS) continued fractions approach.

       References
       ==========

       .. [1] Alkiviadis G. Akritas and Adam W. Strzebonski: A Comparative
              Study of Two Real Root Isolation Methods. Nonlinear Analysis:
              Modelling and Control, Vol. 10, No. 4, 297-304, 2005.
       .. [2] Alkiviadis G. Akritas, Adam W. Strzebonski and Panagiotis S.
              Vigklas: Improving the Performance of the Continued Fractions
              Method Using New Bounds of Positive Roots.
              Nonlinear Analysis: Modelling and Control, Vol. 13, No. 3, 265-279, 2008.

    Tr   r   r   Fr   r    r   )ru   r   r   r   rk   )rK   r   r   rJ   r   r	   r   r   r.   r   r   _real_isolate_and_disjoinr   )r%   r&   ru   r   r   r   rk   r   r   factorsrq   r   r   rz   r{   s                  r)   dup_isolate_real_rootsr   P  s4    	ww$Q48!**,	AWWIAMNN!}	aCEuEIFAa#JAw
7|q	!Q0A3CSW[\0A3CSW[\*/1$!QAq61+11*/1$!QAq61+110!#U?u %&.5()) 21s   9DDc           
      J   |j                   r@|j                         || dd } }}t        |       D ]  \  }	}
t        |
||d      d   | |	<    n|j                  st        d|z        di }}||dk  r|d|k  rdi }}t        |       D ]V  \  }	}
t        |
|      \  }}
|r
|dkD  r||	<   t        |
|      d   D ]$  \  }}t        |      }||vr|	|i||<   |||   |	<   & X g }|j                         D ]!  \  }}|j                  t        |      |f       # t        ||||||||      \  }}|j                         }|rsg }nP|s|j                  |j                  f|fg}n2|j                  |j                  f||j                  |j                  gfg}t!        ||z   |z         S )	a  Isolate real roots of a list of square-free polynomial using Vincent-Akritas-Strzebonski (VAS) CF approach.

       References
       ==========

       .. [1] Alkiviadis G. Akritas and Adam W. Strzebonski: A Comparative
              Study of Two Real Root Isolation Methods. Nonlinear Analysis:
              Modelling and Control, Vol. 10, No. 4, 297-304, 2005.
       .. [2] Alkiviadis G. Akritas, Adam W. Strzebonski and Panagiotis S.
              Vigklas: Improving the Performance of the Continued Fractions
              Method Using New Bounds of Positive Roots.
              Nonlinear Analysis: Modelling and Control, Vol. 13, No. 3, 265-279, 2008.

    NTr   r    r   Fr   )ru   r   r   strictr   rk   )rK   r   	enumerater   rJ   r   r   r   tupleitemsr$   r0   r   r5   rP   r/   r   )polysr&   ru   r   r   r   r   rk   ry   r:   pzerosfactors_dictzero_indicesr=   r%   rq   factors_listindicesr   r   r   s                         r)   dup_isolate_real_roots_listr   y  s    	wwjjlAuQxe1e$ 	BDAq'1a>qAE!H	BWWIAMNN<EsaxckQ#X"B|%  '1Q"1QULO#Aq)!, 	'DAqaA$#$a&Q%&Q"	'' L"((* 0
7T!Wg./0 -\1#Su4ALE5 	
A'67F'qvvGHF%&.5())r+   c                     | \  }}}}|\  }}}	}
||z  ||z  }}||
z  ||	z  }}||k(  r||k(  ry||kD  r||||f\  }}}}||kD  r||
||	f\  }}	}}
|s||z  |	|z  k\  xs ||z  |
|z  k  S ||z  |	|z  kD  xs ||z  |
|z  k  S )z6Check if Mobius transforms define disjoint intervals. Tre   )rf   Nr   rm   rn   ro   rp   r   r   r   r   a1d1b1c1a2d2b2c2s                  r)   _disjoint_pr     s    NBBNBBB2$DB2$Dt|d{RRBBd{RRBB"u2~/B"R%/"ur"u}-22-r+   c           
      <   g g }	}t        |       D ]f  \  }
\  }}t        ||||||d      D ]  \  }}|j                  ||||f        t        ||||||d      D ]  \  }}|	j                  ||||f        h t        |      D ]  \  }
\  }}}}t        ||
dz   d       D ]^  \  }\  }}}}t	        |||      s7t        |||d|d      \  }}t        |||d|d      \  }}t	        |||      s7||||f||
|z   dz   <   ` ||||f||
<    t        |	      D ]  \  }
\  }}}}t        |	|
dz   d       D ]^  \  }\  }}}}t	        |||      s7t        |||d|d      \  }}t        |||d|d      \  }}t	        |||      s7||||f|	|
|z   dz   <   ` ||||f|	|
<    |rt        |	      D ]9  \  }
\  }}}}|d   r|d   st        |||d|d      \  }}|d   s||||f|	|
<    n t        |      D ]9  \  }\  }}}}|d   r|d   st        |||d|d      \  }}|d   s||||f||<    n |j                         }|	D cg c]  \  }}}}t        ||      ||f }	}}}}|D cg c]  \  }}}}t        ||      ||f }}}}}|	D cg c]  \  \  }}}}| | f||f }	}}}}|s@|	D cg c]  \  \  }}}}||f|f }	}}}}|D cg c]  \  \  }}}}||f|f }}}}}|	|fS c c}}}}w c c}}}}w c c}}}}w c c}}}}w c c}}}}w )zCIsolate real roots of a list of polynomials and disjoin intervals. T)ru   r   r   rk   rx   r    N)r   )rv   rk   rx   r   )r   r   r$   r   r   r|   r5   rg   )r   r&   ru   r   r   r   r   rk   r   r   r:   r%   rq   ry   rf   Gr   r=   rT   mr`   r   rz   r{   s                           r)   r   r     s   r5Ew' '	6Aq4QsQT[_hlm 	'DAqLL!Q1&	' 5QsQT[_hlm 	'DAqLL!Q1&	'	' %U+  <Aq!Q(q1uv7 	,OA|1a!!Qv61!QVZ[11!QVZ[1 "!Qv6 !"1a|E!a%!)	, q!Q<a  %U+  <Aq!Q(q1uv7 	,OA|1a!!Qv61!QVZ[11!QVZ[1 "!Qv6 !"1a|E!a%!)	, q!Q<a  (/ 	OA|1aQ4A$5aAQTZ^_DAq A$ q!Q<a	  )/ 	OA|1aQ4A$5aAQTZ^_DAq A$ q!Q<a	 KKMEHMOOAq!"1e,a3OEOHMOOAq!"1e,a3OEO49::.6Aq1arA2h1:E:3899&1a!Q1a&!993899&1a!Q1a&!99%< PO: :9s   K6
-K>
L
9L
L
c           
      L   t        |       dk  ry|j                  s||j                         }}t        | ||      } t	        | |      }|4t        |D cg c]  }t        ||      dt        |      z  z   c}|      }n%t        |D cg c]  }t        |||       c}|      }|%t        |D cg c]  }t        ||       c}|      }n%t        |D cg c]  }t        |||       c}|      }t        ||z
        }	|t        | ||      s|	dz  }	|	S c c}w c c}w c c}w c c}w )zFReturns the number of distinct real roots of ``f`` in ``[inf, sup]``. r   r!   r    )	r	   r#   r5   r   r*   r   r   r   rF   )
r%   r&   r   r   Rr'   r(   	signs_inf	signs_supcounts
             r)   dup_count_real_rootsr   
  s   !}::!++-11a aOE
{'TY([q&A,Z]7J*J([]^_	'u(N!(1c1*=(NPQR	
{'(G1&A,(GK	'u(N!(1c1*=(NPQR		I%&E
x32
L )\(N )H(Ns   "DD-DD!OOQ1Q2Q3Q4A1A2A3A4r    r-   rX         r!   r"         	   
                  ri         r   r    )r    r   )r!   r   )r   r   )r   r   r    r-   )r!   r-   r    r    r!   r    )r   r-   )rX   r-   )rX   r   )r   r   )r   r   )r   r   )r-   r    )r   r   r   ri   r   r   c                 d    | s|st         S | s|dkD  rt        S t        S |s| dkD  rt        S t        S y)zEReturn the half-axis (or origin) on which (re, im) point is located. r   N)r   r   r   r   r   )reims     r)   _classify_pointr     s9    b	6II6II	 r+   c                 `   | sg S g }|s[| d   \  \  }}}	}	||cxk(  r|k(  rn nt        |       dk(  r(t        |||      dkD  rt        t        gS t        t        gS | d   \  \  }}	}	}	t        |||z   dz  |      dkD  r|j                  t        t        g       d}
n|j                  t        t        g       d}
| dd } n?t        |||      dkD  r|j                  t               d}
n|j                  t               d}
| D ]b  \  \  }}	}}	|j                  t               |d   dz  dk(  r|
 }
||k7  s3|
dkD  r|j                  t               N|j                  t               d |S |s[| d   \  \  }}}	}	||cxk(  r|k(  rn nt        |       dk(  r(t        |||      dkD  rt        t        gS t        t        gS | d   \  \  }}	}	}	t        |||z   dz  |      dkD  r|j                  t        t        g       d}n|j                  t        t        g       d}| dd } n?t        |||      dkD  r|j                  t               d}n|j                  t               d}| D ]b  \  \  }}	}}	|j                  t               |d   dz  dk(  r| }||k7  s3|dkD  r|j                  t               N|j                  t               d |S t        |||      }t        |||      }|r|s{|j                  t        ||             t        |       dk(  rt        |||      }t        |||      }n2| d   \  \  }}	}	}	t        |||z   dz  |      }t        |||z   dz  |      }| dd } |dkD  rd}nd}|dkD  rd}
nd}
t        t        t        t        d}|j                  |||
f          | D ]  \  \  }}}}	||k(  r9t        |||      }t        |||      }t        ||      }||j                  |       d|v r|d   dz  dk(  r| }d|v r|d   dz  dk(  r|
 }
||k(  r||k(  rw|j                  |||
f           |S )zJGenerate a sequence of extended quadrants from a list of critical points. r   r    r-   r!   N)r   r   )r!   r!   )r    r!   )r.   r   r   r   r   extendr$   r   r   r   r   r   r   r   )	intervalsr   r   r(   r9   ry   Qr;   rU   r   f2_sgnr   f1_sgnr   r   sgnclss                    r)   _intervals_to_quadrantsr     sU   	
A |A1;Q;9~"B1%)8O8O(|A1BQ	1-1HHb"X&FHHb"X&F%abM	Aq!A%"+ 
	!FQGQHHRLqzA~" AvA:HHRLHHRL
	!  |A1;Q;9~"B1%)8O8O(|A1BQ	1-1HHb"X&FHHb"X&F%abM	Aq!A%"+ 
	!FQGQHHRLqzA~" AvA:HHRLHHRL
	! 	"a	B	"a	BR	R()y>Q"a#B"a#B$Q<LFQAq"q1uai+B"q1uai+BabM		Av	Av 	C HHS&&!"#' ,A6"a#B"a#B!"b)C<qzA~" <qzA~" Q16HHS&&)*+',* Hr+   c                    |du rg d}ddddd}ng d}ddddd}|U|durQt        |      }t        g d      D ]  \  }}||v sd||<    t        g d	      D ]  \  }}	|	|v sd||dz
  d
z  |f<    | |||gg }}
t        |
      D ]o  \  }}|s
|d   t        k(  r|dd }|d   t        k(  r^|dz
  d
z  |dd }}|
|   d   t        |d   f}|t        v r |j	                  t        |   |||f   f       nt        dt        |      z         |d   d}}|t        |      k  s||   |dz   }}|t        k7  rd||f}|t        v r|j	                  t        |   df       n|t        v r|j	                  t        |   ||   f       nat        dt        |      z         ||||   f|dz   }}|t        v r|j	                  t        |   ||   f       nt        dt        |      z         |d   }|t        |      k  rՐr |S )z9Transform sequences of quadrants to a sequence of rules. T)r    r    r   r   r    r   )r   r   )r-   rX   )rX   r   )r   r   r   r   N)SEr   W)SWSENENWr   r!   r"   z3 element rule (corner): z2 element rule (inside): z3 element rule (edge): )	setr   r   _rules_ambiguousr$   NotImplementedErrorstrr.   _rules_simple)Q_L1Q_L2Q_L3Q_L4excludeedgescornersr:   edgecornerQQrulesr   r=   qqq1rq   q2s                     r)   _traverse_quadrantsr    sw   $ 	
  	
 wd2g, !56 	GAtwa	 ##;< 	.IAv ,-!a%1a()	. tT4("B" &1R5B;#2AQ42:EQ;!"qAQ%)R1&B%%.r2GQFODE)*EB*OPP!aA#a&jaD!a%BRx"X&LL-"3Q!78++LL"22"6a!AB-.ICPRG.STTR1AA))LL"22"6a!AB-.G#b'.QRRBB) #a&j%&P Lr+   c           
      h    t        |       D cg c]  \  \  }}}}||f||f c}}}}S c c}}}}w )zKReverse intervals for traversal from right to left and from top to bottom. )r1   )r   r;   rU   r   r%   s        r)   _reverse_intervalsr    s4    <DY<OQQ&8fq!gqq!fgq!QQQs   ,
c                 P    t        t        fd| D               d      z        S )zNCompute the winding number of the input polynomial, i.e. the number of roots. c              3   B   K   | ]  \  }} t         |   |      y wrE   )_values)rG   r9   r:   r`   s      r)   rI   z"_winding_number.<locals>.<genexpr>  s"     7TQ5'!*Q-(7s   r-   )rj   sum)Tr`   s    `r)   _winding_numberr     s"    s7Q77%(BCCr+   c           	      F  +, |j                   s|j                  st        d|z        |j                   r||j                         c}+n|j	                         |c}+t        | |+      } ||9t        |       t        t        | +            c},dt        +,fd| D              z  }| | }	}n|\  }}	||}}
n|\  }
}t        | +      \  }}t        ||	dd+      }t        ||	dd+      }t        |+|d      \  }}t        |+|d      \  }}t        ||
dd+      }t        ||
dd+      }t        |+|d      \  }}t        |+|d      \  }}t        ||dd+      }t        ||dd+      }t        |+|d      \  }}t        |+|d      \  }}t        ||dd+      }t        ||dd+      }t        |+|d      \  }}t        |+|d      \  }}||g}||g}||g} ||g}!t        ||||
ddd      }"t        |||	|ddd      }#t        | |||
ddd      }$t        |!||	|ddd      }%t        |$      }$t        |%      }%t        |"||||
+      }&t        |#|||	|+      }'t        |$|||
|+      }(t        |%||||	+      })t!        |&|'|(|)|	      }*t#        |*+      S )
zRCount all roots in [u + v*I, s + t*I] rectangle using Collins-Krandick algorithm. z.complex root counting is not supported over %sr-   c              3   T   K   | ]  }j                  t        |             ! y wrE   quorF   rG   ra   ry   rH   s     r)   rI   z*dup_count_complex_roots.<locals>.<genexpr>
  s      /!%%A#/   %(r    Tr   r   )r   r   rk   r   r   r  )rJ   rK   r   r5   r   r   r	   rF   r   r6   r   r   r   r   r  r   r  r   )-r%   r&   r   r   r  r   r   Brz   r{   r(   r9   r   r   f1L1Ff2L1Ff1L1Rf2L1Rf1L2Ff2L2Ff1L2Rf2L2Rf1L3Ff2L3Ff1L3Rf2L3Rf1L4Ff2L4Ff1L4Rf2L4RS_L1S_L2S_L3S_L4I_L1I_L2I_L3I_L4r
  r  r  r  r  ry   rH   s-                                              @@r)   dup_count_complex_rootsrA    s	   77177JQNOOww!++-1zz|Q1Aq!A
{ck1s6!Q<02c/Q///
{"qbAA
{"qbAA1a FBAq!Q'EAq!Q'Eq!T:HAuq!T:HAuAq!Q'EAq!Q'Eq!T:HAuq!T:HAuAq!Q'EAq!Q'Eq!T:HAuq!T:HAuAq!Q'EAq!Q'Eq!T:HAuq!T:HAu5>D5>D5>D5>D&tQA14t\`aD&tQA14t\`aD&tQA14t\`aD&tQA14t\`aDd#Dd#D"4q!Q?D"4q!Q?D"4q!Q?D"4q!Q?DD$dGDA1a  r+   c
           	         ||c\  }
}\  }}|\  }}}}|\  }}}}|\  }}}}|\  }}}}|
|z   dz  }t        ||dd|	      }t        ||dd|	      } t        || g|	||ddd      }!g g }#}"|!|}%}$g g }'}&|t        |!      })}(|D ]  }|\  \  }}}*}+||k(  rQ||k(  r#|"j                  |       |#j                  |       9||k  r|"j                  |       P|#j                  |       b||k  r|"j                  |       y||k\  r|#j                  |       t	        |+|||	j                         |d      \  }}||k  r|"j                  ||f|*|+f       ||k\  s|#j                  ||f|*|+f        |D ]  }|\  \  }}}*}+||k(  rQ||k(  r#|&j                  |       |'j                  |       9||k  r|&j                  |       P|'j                  |       b||k  r|&j                  |       y||k\  r|'j                  |       t	        |+|||	j                         |d      \  }}||k  r|&j                  ||f|*|+f       ||k\  s|'j                  ||f|*|+f        t        |"|||
||	      },t        |$|| |||	      }-t        |&||||
|	      }.|}/t        |#|||||	      }0|}1t        |'|||||	      }2t        |)|| |||	      }3t        |,|-|.|/d      }4t        |0|1|2|3d      }5t        |4|	      }6t        |5|	      }7|"|$|&|(f}8|,|-|.|/f}9|#|%|'|)f}:|0|1|2|3f};||||f}<|| ||f}=||||f}>|||| f}?|
|f||f}}||f||f}A}@|6|||8|9|<|=f}B|7|@|A|:|;|>|?f}C|B|CfS )zFVertical bisection step in Collins-Krandick root isolation algorithm. r-   r   r    Tr   r   rk   r   r   rw   rk   r'  	r   r   r  r$   r   r   r   r  r   )Dr   r;   rU   r_   r   F1F2r   r   ry   rz   r{   r(   r9   r=  r>  r?  r@  r
  r  r  r  r)  r-  r1  r5  r*  r.  r2  r6  xf1Vf2VI_VI_L1_LI_L1_RI_L2_LI_L2_RI_L3_LI_L3_RI_L4_LI_L4_Rr   hQ_L1_LQ_L2_LQ_L3_LQ_L4_LQ_L1_RQ_L2_RQ_L3_RQ_L4_RT_LT_RN_LN_RI_LQ_LI_RQ_RF1_LF2_LF1_RF2_Rra   rb   D_LD_RsD                                                                       r)   _vertical_bisectionrk  F  s   NFQFQD$dD$d!#E5%!#E5%	
Q!A
b!Q1
%C
b!Q1
%C
%sCj!UYae
fCFF$FFFF-c2FF 8A6Ava a Qa a Ava aa +Aq!QZZ\ATXY16MMAq67A"676MMAq67A"67-80  8A6Ava a Qa a Ava aa +Aq!QZZ\ATXY16MMAq67A"676MMAq67A"67-80 %VUE1aCF$VS#q!Q?F$VUE1aCFF$VUE1aCFF$VUE1aCF$VS#q!Q?F
ffffd
KC
ffffd
KC
#q
!C
#q
!C666
*C666
*C666
*C666
*C3u%D3u%D5%%D5%%Dq6Aq6qAq6Aq6qA1c3d
+C1c3d
+C8Or+   c
           	         ||c\  }
}\  }}|\  }}}}|\  }}}}|\  }}}}|\  }}}}||z   dz  }t        ||dd|	      }t        ||dd|	      } t        || g|	|
|ddd      }!||!}#}"g g }%}$t        |!      |}'}&g g })}(|D ]  }|\  \  }}}*}+||k(  rQ||k(  r#|$j                  |       |%j                  |       9||k  r|$j                  |       P|%j                  |       b||k  r|$j                  |       y||k\  r|%j                  |       t	        |+|||	j                         |d      \  }}||k  r|$j                  ||f|*|+f       ||k\  s|%j                  ||f|*|+f        |D ]  }|\  \  }}}*}+||k(  rQ||k(  r#|(j                  |       |)j                  |       9||k  r|(j                  |       P|)j                  |       b||k  r|(j                  |       y||k\  r|)j                  |       t	        |+|||	j                         |d      \  }}||k  r|(j                  ||f|*|+f       ||k\  s|)j                  ||f|*|+f        |},t        |$|||||	      }-t        |&|| ||
|	      }.t        |(|||||	      }/t        |#|| |
||	      }0t        |%|||||	      }1|}2t        |)|||||	      }3t        |,|-|.|/d      }4t        |0|1|2|3d      }5t        |4|	      }6t        |5|	      }7|"|$|&|(f}8|,|-|.|/f}9|#|%|'|)f}:|0|1|2|3f};||||f}<||| |f}=||||f}>| |||f}?|
|f||f}}|
|f||f}A}@|6|||8|9|<|=f}B|7|@|A|:|;|>|?f}C|B|CfS )zHHorizontal bisection step in Collins-Krandick root isolation algorithm. r-   r    TrC  rD  r'  rE  )Dr   r;   rU   r_   r   rF  rG  r   r   ry   rz   r{   r(   r9   r=  r>  r?  r@  r
  r  r  r  r)  r-  r1  r5  r*  r.  r2  r6  yf1Hf2HI_HI_L1_BI_L1_UI_L2_BI_L2_UI_L3_BI_L3_UI_L4_BI_L4_Ur   rT  Q_L1_BQ_L2_BQ_L3_BQ_L4_BQ_L1_UQ_L2_UQ_L3_UQ_L4_UT_BT_UN_BN_UI_BQ_BI_UQ_UF1_BF2_BF1_UF2_Ura   rb   D_BD_UsD                                                                       r)   _horizontal_bisectionr    s   NFQFQD$dD$d!#E5%!#E5%	
Q!A
b!Q1
%C
b!Q1
%C
%sCj!UYae
fC3FFFF',dFFFF 8A6Ava a Qa a Ava aa +Aq!QZZ\ATXY16MMAq67A"676MMAq67A"67-80  8A6Ava a Qa a Ava aa +Aq!QZZ\ATXY16MMAq67A"676MMAq67A"67-80 F$VUE1aCF$VS#q!Q?F$VUE1aCF$VS#q!Q?F$VUE1aCFF$VUE1aCF
ffffd
KC
ffffd
KC
#q
!C
#q
!C666
*C666
*C666
*C666
*C5#u%D5#u%Du%Du%Dq6Aq6qAq6Aq6qA1c3d
+C1c3d
+C8Or+   c                     d\  }}t        |       D ]*  \  }\  }\  }}\  }}}}}}||z
  ||z
  z  }	||	|k  s'|	|}}, | j                  |      S )z0Find a rectangle of minimum area for bisection. NN)r   rO   )

rectanglesmin_arear=   r:   r   rz   r{   r(   r9   areas
             r)   _depth_first_selectr    sr    KHa.7
.C "**Av1v1q!QAAthaH	" >>!r+   c                 B    | |c\  }}\  }}|||z
  |k  xr ||z
  |k  S y)z8Return ``True`` if the given rectangle is small enough. Tre   )r;   rU   ru   rz   r{   r(   r9   s          r)   _rectangle_small_pr  &  s8    NFQFQ
1us{*q1us{*r+   c                   HI |j                   s|j                  st        d|z        t        |       dk  rg S |j                   r|j	                         Hn|Ht        | |H      } t        t        | H            Idt        HIfd| D              z  }| Hj                  f||fc\  }}\  }	}
||}||}	|dk  s
|
|k  s|	|k  rt        d      t        | H      \  }}t        ||ddH      }t        ||ddH      }t        ||	ddH      }t        ||	ddH      }t        ||
ddH      }t        ||
ddH      }t        ||ddH      }t        ||ddH      }||g}||g}||g}||g}t        |H||	ddd      }t        |H||
ddd      }t        |H||	ddd      }t        |H||
ddd      }t        |      }t        |      }t        |||||	H      }t        |||||
H      }t        ||||	|H      }t        ||||
|H      } t!        ||||       }!t#        |!H      }"|"sg S ||||f}#|||| f}$||||f}%||||f}&|"||f|	|
f|#|$|%|&fgg }(}'|'rt%        |'      \  }"\  }}\  }	}
}#}$}%}&|	|z
  |
|z
  kD  rt'        |"||f|	|
f|#|$|%|&||H
      \  })}*|)\  }+},}-}.}/}0}1|*\  }2}3}4}5}6}7}8|+dk\  rF|+dk(  r0t)        |,|-|      r#|(j+                  t-        |,|-|.|/|0|1||H	             n|'j+                  |)       |2dk\  r|2dk(  r0t)        |3|4|      r#|(j+                  t-        |3|4|5|6|7|8||H	             n|'j+                  |*       nt/        |"||f|	|
f|#|$|%|&||H
      \  }9}:|9\  };},}-}<}=}>}?|:\  }@}3}4}A}B}C}D|;dk\  rF|;dk(  r0t)        |,|-|      r#|(j+                  t-        |,|-|<|=|>|?||H	             n|'j+                  |9       @dk\  rF@dk(  r0t)        |3|4|      r#|(j+                  t-        |3|4ABCD||H	             n|'j+                  |:       |'rt1        |(d	 
      g }(}E|ED ]#  }F|(j3                  |Fj5                         |Fg       % |r|(S |(D Gcg c]  }G|Gj7                          c}GS c c}Gw )zTIsolate complex roots of a square-free polynomial using Collins-Krandick algorithm. z3isolation of complex roots is not supported over %sr   r-   c              3   T   K   | ]  }j                  t        |             ! y wrE   r#  r%  s     r)   rI   z0dup_isolate_complex_roots_sqf.<locals>.<genexpr>?  s      +AaeeCFB+r&  z'not a valid complex isolation rectangler    TrC  c                 2    | j                   | j                  fS rE   )axay)r   s    r)   <lambda>z/dup_isolate_complex_roots_sqf.<locals>.<lambda>  s    qtt r+   )key)rJ   rK   r   r	   r5   r   rF   r   r6   rP   r   r   r   r   r  r   r  r   r  rk  r  r$   ComplexIntervalr  r   r   	conjugateas_tuple)Jr%   r&   ru   r   r   r   r(  rz   r{   r(   r9   r   r   f1L1f2L1f1L2f2L2f1L3f2L3f1L4f2L4r9  r:  r;  r<  r=  r>  r?  r@  r
  r  r  r  r  r   r_   r   rF  rG  r  r   ri  rj  r_  r;   rU   ra  rb  re  rf  r`  ra   rb   rc  rd  rg  rh  r  r  r  r  r  r  r  r  r  r  r  r  _rootsrootr   ry   rH   sJ                                                                           @@r)   dup_isolate_complex_roots_sqfr  /  sL   77177ORSSTT!}	wwKKMAq!A	VAq\	B	#++
++Ab!&&\Aq6NFQFQ

1uQ!q&BCC1a FBr1aA&Dr1aA&Dr1aA&Dr1aA&Dr1aA&Dr1aA&Dr1aA&Dr1aA&D$<D$<D$<D$<D&tQA14PT\`aD&tQA14PT\`aD&tQA14PT\`aD&tQA14PT\`aDd#Dd#D"4tQ1=D"4tQ1=D"4tQ1=D"4tQ1=DD$d3A1A		tT4 A	tT4 A
dD	!B
dD	!BaVaVQ2r:;RJ
*=j*I'6Aq6Aq1aRq51q5=*1q!fq!faBBPRTUVHC.1+CAsCt.1+CAsCtax!8 21a =LLAsCtRQSUV!WX%%c*ax!8 21a =LLAsCtRQSUV!WX%%c*,QAA1b"bRTVWXHC.1+CAsCt.1+CAsCtax!8 21a =LL1c3dBA"? @ %%c*ax!8 21a =LL1c3dBA"? @ %%c*M P 5&<=rEF /dnn&-./ ',.!...s    Qc           	      H    t        | ||||||      t        | |||||      fS )zBIsolate real and complex roots of a square-free polynomial ``f``. )ru   r   r   rk   r   )ru   r   r   r   )r   r  )r%   r&   ru   r   r   rk   r   s          r)   dup_isolate_all_roots_sqfr    s8     	#Aqcs$Yab%a#PXY[ [r+   c                 T   |j                   s|j                  st        d|z        t        | |      \  }}t	        |      dk(  rO|\  \  } }t        | |||||      \  }	}
|	D cg c]  \  }}||f|f }	}}|
D cg c]  \  }}||f|f }
}}|	|
fS t        d      c c}}w c c}}w )zFIsolate real and complex roots of a non-square-free polynomial ``f``. z<isolation of real and complex roots is not supported over %sr    r   z2only trivial square-free polynomials are supported)rJ   rK   r   r   r.   r  r  )r%   r&   ru   r   r   rk   r   r   rq   	real_partcomplex_partr;   rU   s                r)   dup_isolate_all_rootsr    s    77177X[\\]]a#JAw
7|q	!Q";qcs$#8	< 1:;fq!1vqk;	;3?A!Q1a&!AA,&&!#WXX <As   $B<B$c                       e Zd ZdZd Zed        Zed        Zd Zed        Z	ed        Z
ed        Zed	        Zed
        Zd Zd Zd Zd Zd Zd Zd ZddZd Zy)r   z?A fully qualified representation of a real isolation interval. c                    t        |      dk(  r|\  }}d| _        |dk  r4|dk  rt        ||      | | df\  }}}| _        nt        d|d|d      t	        ||f|j                               \  }}}}	t        |t        ||g      t        ||	g      |      }||||	f| _        n|dd	 | _        |d	   | _        ||c| _	        | _
        y)
z8Initialize new real interval with complete information. r-   Fr   Tr   r~   r   Nr!   )r.   negr   r   rc   r5   r   r
   rx   r%   dom)
selfdatar%   r  r(   r9   r;   rU   ra   rb   s
             r)   __init__zRealInterval.__init__  s    t9>DAqDH1u6(21c(:QBD(H%Aq!TX$PQST%UVV.1vs}}GJAq!QaAq6!2!*Aq6!2C9A Q1*DKs)DKBxDHcr+   c                     t         S rE   )r   r  s    r)   funczRealInterval.func  s    r+   c                 h    | }|j                   |j                  fz   |j                  |j                  fS rE   )rx   r  r%   r  r  r:   s     r)   argszRealInterval.args  s+    AEE8#QSS!%%00r+   c                 b    t        |      t        |       ury| j                  |j                  k(  S NFtyper  r  others     r)   __eq__zRealInterval.__eq__  (    ;d4j(yyEJJ&&r+   c                     | j                   j                         }| j                  \  }}}}| j                  s||z  ||z  k  r	 |||      S  |||      S ||z  ||z  kD  r
 |||       S  |||       S )z%Return the position of the left end. )r  r5   rx   r  )r  r`   r;   rU   ra   rb   s         r)   r;   zRealInterval.a  s     ""$[[
1axxsQqSyQ{"A;sQqSya|#!QK<r+   c                 V    | j                   }| | _         | j                   }|| _         |S )z&Return the position of the right end. )r  r;   )r  wasrvs      r)   rU   zRealInterval.b  s-     hh7ffW	r+   c                 4    | j                   | j                  z
  S )z-Return width of the real isolating interval. rU   r;   r  s    r)   dxzRealInterval.dx  s     vvr+   c                 :    | j                   | j                  z   dz  S )z2Return the center of the real isolating interval. r-   r;   rU   r  s    r)   centerzRealInterval.center  s     ""r+   c                 j    t        | j                  j                  | j                  j                        S z=Return the largest denominator occurring in either endpoint. )r6   r;   denominatorrU   r  s    r)   	max_denomzRealInterval.max_denom  s%     466%%tvv'9'9::r+   c                 2    | j                   | j                  fS )z8Return tuple representation of real isolating interval. r  r  s    r)   r  zRealInterval.as_tuple  s    r+   c                 <    d| j                   d| j                  dS )N(r~   r   r  r  s    r)   __repr__zRealInterval.__repr__  s    !VVTVV,,r+   c                     t        |t              r|\  }}n|d}}|dk(  xr$ | j                  |cxk  xr | j                  k  S c S )a  
        Say whether a complex number belongs to this real interval.

        Parameters
        ==========

        item : pair (re, im) or number re
            Either a pair giving the real and imaginary parts of the number,
            or else a real number.

        r   )
isinstancer   r;   rU   r  itemr   r   s       r)   __contains__zRealInterval.__contains__  sG     dE"FB1BQw1466R14661111r+   c                 R   t        |t              r4| j                  |j                  k  xs |j                  | j                  k  S t        |t              sJ | j                  |j
                  k  xs7 |j                  | j                  k  xs |j                  |j                  z  dkD  S )9Return ``True`` if two isolation intervals are disjoint. r   )	r  r   rU   r;   r  r  bxr  byr  s     r)   is_disjointzRealInterval.is_disjoint0  s    e\*FFUWW$8$&&(89%111! %UXX%6 %xx 1$	&r+   c                     | j                   | S t        | j                  | j                   | j                  dd      \  }}t	        || j
                  fz   || j                        S )z2Internal one step real root refinement procedure. r    T)rv   rx   )rx   r|   r%   r  r   r  )r  r%   rx   s      r)   _inner_refinezRealInterval._inner_refine8  sW    ;;K.FFDKK4A	6 Fdhh[0!TXX>>r+   c                     | }|j                  |      s2|j                         |j                         }}|j                  |      s2||fS zDRefine an isolating interval until it is disjoint with another one. r  r  r  r  exprs      r)   refine_disjointzRealInterval.refine_disjointB  L    ""5),,.0C0C0E%D ""5) U{r+   c                 h    | }|j                   |k  s |j                         }|j                   |k  s |S zERefine an isolating interval until it is of sufficiently small size. )r  r  )r  r  r  s      r)   refine_sizezRealInterval.refine_sizeJ  s4    77R<%%'D 77R< r+   c                 J    | }t        |      D ]  }|j                         } |S )z9Perform several steps of real root refinement algorithm. r2   r  r  rv   r  r   s       r)   refine_stepzRealInterval.refine_stepR  /    u 	(A%%'D	( r+   c                 "    | j                         S )z4Perform one step of real root refinement algorithm. r  r  s    r)   refinezRealInterval.refineZ      !!##r+   Nr    )__name__
__module____qualname____doc__r  propertyr  r  r  r;   rU   r  r  r  r  r  r  r  r  r  r  r  r  re   r+   r)   r   r     s    I"2   1 1'
         # # ; ; -2$&?$r+   r   c                       e Zd ZdZddZed        Zed        Zd Zed        Z	ed        Z
ed        Zed	        Zed
        Zed        Zed        Zed        Zd Zd Zd Zd Zd Zd Zd ZddZddZd Zy)r  a  A fully qualified representation of a complex isolation interval.
    The printed form is shown as (ax, bx) x (ay, by) where (ax, ay)
    and (bx, by) are the coordinates of the southwest and northeast
    corners of the interval's rectangle, respectively.

    Examples
    ========

    >>> from sympy import CRootOf, S
    >>> from sympy.abc import x
    >>> CRootOf.clear_cache()  # for doctest reproducibility
    >>> root = CRootOf(x**10 - 2*x + 3, 9)
    >>> i = root._get_interval(); i
    (3/64, 3/32) x (9/8, 75/64)

    The real part of the root lies within the range [0, 3/4] while
    the imaginary part lies within the range [9/8, 3/2]:

    >>> root.n(3)
    0.0766 + 1.14*I

    The width of the ranges in the x and y directions on the complex
    plane are:

    >>> i.dx, i.dy
    (3/64, 3/64)

    The center of the range is

    >>> i.center
    (9/128, 147/128)

    The northeast coordinate of the rectangle bounding the root in the
    complex plane is given by attribute b and the x and y components
    are accessed by bx and by:

    >>> i.b, i.bx, i.by
    ((3/32, 75/64), 3/32, 75/64)

    The southwest coordinate is similarly given by i.a

    >>> i.a, i.ax, i.ay
    ((3/64, 9/8), 3/64, 9/8)

    Although the interval prints to show only the real and imaginary
    range of the root, all the information of the underlying root
    is contained as properties of the interval.

    For example, an interval with a nonpositive imaginary range is
    considered to be the conjugate. Since the y values of y are in the
    range [0, 1/4] it is not the conjugate:

    >>> i.conj
    False

    The conjugate's interval is

    >>> ic = i.conjugate(); ic
    (3/64, 3/32) x (-75/64, -9/8)

        NOTE: the values printed still represent the x and y range
        in which the root -- conjugate, in this case -- is located,
        but the underlying a and b values of a root and its conjugate
        are the same:

        >>> assert i.a == ic.a and i.b == ic.b

        What changes are the reported coordinates of the bounding rectangle:

        >>> (i.ax, i.ay), (i.bx, i.by)
        ((3/64, 9/8), (3/32, 75/64))
        >>> (ic.ax, ic.ay), (ic.bx, ic.by)
        ((3/64, -75/64), (3/32, -9/8))

    The interval can be refined once:

    >>> i  # for reference, this is the current interval
    (3/64, 3/32) x (9/8, 75/64)

    >>> i.refine()
    (3/64, 3/32) x (9/8, 147/128)

    Several refinement steps can be taken:

    >>> i.refine_step(2)  # 2 steps
    (9/128, 3/32) x (9/8, 147/128)

    It is also possible to refine to a given tolerance:

    >>> tol = min(i.dx, i.dy)/2
    >>> i.refine_size(tol)
    (9/128, 21/256) x (9/8, 291/256)

    A disjoint interval is one whose bounding rectangle does not
    overlap with another. An interval, necessarily, is not disjoint with
    itself, but any interval is disjoint with a conjugate since the
    conjugate rectangle will always be in the lower half of the complex
    plane and the non-conjugate in the upper half:

    >>> i.is_disjoint(i), i.is_disjoint(i.conjugate())
    (False, True)

    The following interval j is not disjoint from i:

    >>> close = CRootOf(x**10 - 2*x + 300/S(101), 9)
    >>> j = close._get_interval(); j
    (75/1616, 75/808) x (225/202, 1875/1616)
    >>> i.is_disjoint(j)
    False

    The two can be made disjoint, however:

    >>> newi, newj = i.refine_disjoint(j)
    >>> newi
    (39/512, 159/2048) x (2325/2048, 4653/4096)
    >>> newj
    (3975/51712, 2025/25856) x (29325/25856, 117375/103424)

    Even though the real ranges overlap, the imaginary do not, so
    the roots have been resolved as distinct. Intervals are disjoint
    when either the real or imaginary component of the intervals is
    distinct. In the case above, the real components have not been
    resolved (so we do not know, yet, which root has the smaller real
    part) but the imaginary part of ``close`` is larger than ``root``:

    >>> close.n(3)
    0.0771 + 1.13*I
    >>> root.n(3)
    0.0766 + 1.14*I
    c                     ||c| _         | _        ||c| _        | _        ||c| _        | _        ||c| _        | _        |	| _        |
| _	        y)z;Initialize new complex interval with complete information. N)
r;   rU   r_   r   r   rF  r   rG  r  conj)r  r;   rU   r_   r   rF  rG  r   r   r  r  s              r)   r  zComplexInterval.__init__  sP     AArr	r+   c                     t         S rE   )r  r  s    r)   r  zComplexInterval.func  s    r+   c           
          | }|j                   |j                  |j                  |j                  |j                  |j
                  |j                  |j                  |j                  |j                  f
S rE   )
r;   rU   r_   r   rF  rG  r   r   r  r  r  s     r)   r  zComplexInterval.args  sJ    QSS!##qssADD!$$addAEE166JJr+   c                 b    t        |      t        |       ury| j                  |j                  k(  S r  r  r  s     r)   r  zComplexInterval.__eq__  r  r+   c                      | j                   d   S )z1Return ``x`` coordinate of south-western corner. r   )r;   r  s    r)   r  zComplexInterval.ax       vvayr+   c                 X    | j                   s| j                  d   S | j                  d    S )z1Return ``y`` coordinate of south-western corner. r    )r  r;   rU   r  s    r)   r  zComplexInterval.ay  )     yy66!9FF1I:r+   c                      | j                   d   S )z1Return ``x`` coordinate of north-eastern corner. r   )rU   r  s    r)   r  zComplexInterval.bx  r  r+   c                 X    | j                   s| j                  d   S | j                  d    S )z1Return ``y`` coordinate of north-eastern corner. r    )r  rU   r;   r  s    r)   r  zComplexInterval.by  r  r+   c                 @    | j                   d   | j                  d   z
  S )z0Return width of the complex isolating interval. r   r  r  s    r)   r  zComplexInterval.dx       vvay466!9$$r+   c                 @    | j                   d   | j                  d   z
  S )z1Return height of the complex isolating interval. r    r  r  s    r)   dyzComplexInterval.dy   r  r+   c                 r    | j                   | j                  z   dz  | j                  | j                  z   dz  fS )z5Return the center of the complex isolating interval. r-   r  r  r  r  r  s    r)   r  zComplexInterval.center%  s3     477"A%$''(91'<==r+   c                     t        | j                  j                  | j                  j                  | j                  j                  | j
                  j                        S r  )r6   r  r  r  r  r  r  s    r)   r  zComplexInterval.max_denom*  sB     477&&(;(;77&&(;(;= 	=r+   c                 b    | j                   | j                  f| j                  | j                  ffS )ziReturn tuple representation of the complex isolating
        interval's SW and NE corners, respectively. )r  r  r  r  r  s    r)   r  zComplexInterval.as_tuple0  s)     $''"TWWdgg$677r+   c           	      p    d| j                   d| j                  d| j                  d| j                  d	S )Nr  r~   z) x (r   r  r  s    r)   r  zComplexInterval.__repr__5  s!    (,$''477KKr+   c                     t        | j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  d
      S )z=This complex interval really is located in lower half-plane. T)r  )
r  r;   rU   r_   r   rF  rG  r   r   r  r  s    r)   r  zComplexInterval.conjugate8  sJ    tvvtvvtvvtvvGGTWWdggtwwtE 	Er+   c                     t        |t              r|\  }}n|d}}| j                  |cxk  xr | j                  k  nc xr$ | j                  |cxk  xr | j
                  k  S c S )a%  
        Say whether a complex number belongs to this complex rectangular
        region.

        Parameters
        ==========

        item : pair (re, im) or number re
            Either a pair giving the real and imaginary parts of the number,
            or else a real number.

        r   )r  r   r  r  r  r  r  s       r)   r  zComplexInterval.__contains__=  sU     dE"FB1Bww"''DDGGr,DTWW,DD,DDr+   c                 R   t        |t              r|j                  |       S | j                  |j                  k7  ry| j                  |j
                  k  xs |j                  | j
                  k  }|ry| j                  |j                  k  xs |j                  | j                  k  }|S )r  T)r  r   r  r  r  r  r  r  )r  r  re_distinctim_distincts       r)   r  zComplexInterval.is_disjointP  s    e\*$$T**99

"ww)?UXX-?ww)?UXX-?r+   c                    | j                   | j                  c\  }}\  }}| j                  | j                  }}| j                  | j
                  }}| j                  | j                  }
}	| j                  }||z
  ||z
  kD  r9t        d||f||f||||
||	|
      \  }}|d   dk(  r|\  }}}}}}}
nC|\  }}}}}}}
n8t        d||f||f||||
||	|
      \  }}|d   dk(  r|\  }}}}}}}
n
|\  }}}}}}}
t        ||||||
||	|| j                  
      S )z5Internal one step complex root refinement procedure. r    r   )r;   rU   r_   r   r   rF  r   rG  r  rk  r  r  r  )r  rz   r{   r(   r9   r_   r   r   rF  r   rG  r  ri  rj  r   r;   rU   r  r  s                      r)   r  zComplexInterval._inner_refine\  sC   AAvvtvv1$''B$''Bhhq51q5=*1q!fq!faBBPRTWXHC1v{(+%1aAr2(+%1aAr2,QAA1b"bRTVYZHC1v{(+%1aAr2(+%1aAr2q!Q2r2r3		JJr+   c                     | }|j                  |      s2|j                         |j                         }}|j                  |      s2||fS r  r  r  s      r)   r  zComplexInterval.refine_disjointx  r  r+   Nc                     ||}| }|j                   |k  r|j                  |k  s0|j                         }|j                   |k  s |j                  |k  s0|S r  )r  r  r  )r  r  r  r  s       r)   r  zComplexInterval.refine_size  sR    :B77R<DGGbL%%'D 77R<DGGbL r+   c                 J    | }t        |      D ]  }|j                         } |S )z<Perform several steps of complex root refinement algorithm. r  r  s       r)   r  zComplexInterval.refine_step  r  r+   c                 "    | j                         S )z7Perform one step of complex root refinement algorithm. r  r  s    r)   r  zComplexInterval.refine  r  r+   FrE   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  r  r  re   r+   r)   r  r  _  s   AF    K K'
         % % % % > > = =
8
LE
E&
K8$r+   r  r  )NNNFF)NNNFr  )FF)NNNFFFr  rE   )NNN)Vr  sympy.polys.densearithr   r   r   r   sympy.polys.densebasicr   r   r	   r
   r   r   r   sympy.polys.densetoolsr   r   r   r   r   r   r   r   r   r   sympy.polys.euclidtoolsr   sympy.polys.factortoolsr   sympy.polys.polyerrorsr   r   r   sympy.polys.sqfreetoolsr   r   r*   r?   rB   rR   rV   r[   rc   rg   rr   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  r   r   r  r  r   rA  rk  r  r  r  r  r  r  r   r  re   r+   r)   <module>r&     sM   A   
    $L*.X"3@	9:	,\(T
n!FpdA(00$ BD'*R<*|.,;z8 
								*Ha* Ha* Ha	*
 Ha* Ha* Ha* Ha* Ha* Ha* Ha*  Ha!*" Ha#*( Ha)** Ha+*, Ha-*. Ha/*4 Ha5*6 HaHaHa HbHbHbHb HbHbHbHbS*XHRL"H RL"H RL"	H
 RL"H RL"H RL"H RL"H RL"H RL"H RL"H  RL"!H" RL"#H( RL")H* RL"+H, RL"-H. RL"/H4 Ha5H6 Ha7H8 Ha9H: Ha;H> RL!?H@ RL!AHB RL!CHD RL!EHJ HaKHL HaMHN HaOHP HaQHT RL!UHV RL!WHX RL!YHZ RL![H` HaaHb HacHd HaeHf HagHj RL!kHl RL!mHn RL!oHp RL!qHv HbwHx HbyHz Hb{H| Hb}H@ RL"AHB RL"CHD RL"EHF RL"GHL HbMHN HbOHP HbQHR HbSHV RL"WHX RL"YHZ RL"[H\ RL"]Hb HbcHd HbeHf HbgHh HbiHl RL"mHn RL"oHp RL"qHr RL"sHx HbyHz Hb{H| Hb}H~ HbHB RL"CHD RL"EHF RL"GHH RL"IHN HbOHP HbQHR HbSHT HbUHX RL"YHZ RL"[H\ RL"]H^ RL"_Hd RL"eHf RL"gHh RL"iHj RL"kHp RL"qHr RL"RL"RL" RL"RL"RL"RL" RL"RL"RL"RL"OH Tyy y y	
 y ' ' ' ' 	 	    '  '!" '#$ 
'	'	'	'	'	'/4 TlJXRDH!ThThT
{/z[Y(V$ V$ru$ u$r+   