K iUdZddlmZmZmZmZddlmZmZm Z m Z m Z m Z m Z ddlmZmZmZmZmZmZmZmZmZmZddlmZddlmZddlmZmZm Z ddl!m"Z"m#Z#d Z$d Z%d Z&d Z'd Z(dZ)dZ*dZ+dbdZ,dcdZ-dddZ.dddZ/dedZ0dZ1dcdZ2dcdZ3dfdZ4dcdZ5dcdZ6dgdZ7dbdZ8dgdZ9dhd Z:d!Z;d"Zd%Z?d&Z@d'ZAd(ZBd)ZCiee>fde?e?fde@efd*eCe?fd*e@e?fd+eAefd+eeCfd,e?e@fd,eeBfd-e?eCfd-efd.e>e?fd.e?ee=fd/e?e>fd/i ZDie@e;efd0eCe;e?fd0e@e;e?fd1eAe;efd1ee;eCfd2e?e;e@fd2ee;eBfd3e?e;eCfd3e@eBfd4ieAeCfd4eBe@fd4eCeAfd4e@e;eBfd4eAe;eCfd4eBe;e@fd4eCe;eAfd4efd5e=e?fd5e>efd5e=e;e?fd5e>e;ee@fd8ie?eAfd8ee;e@fd8e?e;eAfd8eeAfd9e?eBfd9ee;eAfd9e?e;eBfd9e@e>fd:eAe?fd:eBefd:eAe;e?fd:eBe;efd;eBe?fd;eCefd;eBe;e?fd;eCe;efd<e>e;e?fd<e?e;ee;e=fd=e?e;e>fd=e@e;e@fd>eAe;eAfd>eBe;eBfd>eCe;eCfd>ee;e>fd?e?e;e?fd?i ZEidd@gd*dAgd+dBgd,dAgd-dBgd0dCdAgd1dDdBgd2dCdAgd3dDdBgd.dEgd/dFgd4dGdHgd5dGdHgd6dEdIgd7dJdFgd8dKdLgd9dMdNgdMdNgdKdLgdEdIgdJdFgdOd@gdOd@gdPZFdQZGdRZHdidSZIdTZJdUZKdjdVZLdWZMdXZNdYZOdZZPddd[ZQdcd\ZRddd]ZSGd^d_ZTGd`daZUy)kz;Real and complex root isolation and refinement algorithms. )dup_neg dup_rshiftdup_remdup_l2_norm_squared)dup_LCdup_TC dup_degree dup_strip dup_reverse dup_convert dup_terms_gcd) dup_clear_denoms dup_mirror dup_scale dup_shift dup_transformdup_diffdup_eval dmp_eval_indup_sign_variations dup_real_imag)dup_discriminant)dup_factor_list)RefinementFailed DomainErrorPolynomialError) dup_sqf_part dup_sqf_listc|jstd|zt||}|t|d|g}|dr4t |d|d|}|j t |||dr4|ddS)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 %sN)is_Fieldrrrrappendr)fKsturmss _/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/polys/rootisolation.py dup_sturmr* s2 ::AAEFFQA Aq! "E ) E"IuRy! , WQ]# ) ":c\t|g}}||jgz}t||dkr t||}t t |}t d|D]}||dk\r |j|| dg}}t |dz|D]B}||dkr |||z|j||dz } |j| ||z z|gD|s{t|} || ddz|| d<|j| d|sy|jdt|dzzS)aCompute 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. rr N) lenonerrlistreversedrangelogr$min get_fieldmax) r%r&nPtiaQLjqs r)dup_root_upper_boundr?FsD q62qA QUUG A a|a AqM Xa[A 1a[ Q419 qteQ2q1ua )Atqy!q1551q>)A IIqQU|Q' (  )  GAaD'A+!A$ 1+. q{{}Q#a&1*--r+c<tt||}|d|z Sy)aCompute 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_boundrBrs& !Q 3E wr+ct|}|dkr td|jr |j}t ||||}}n7|j r|j s |jrtd|z|dd}|j|dr%|j|j|dr%t|dk(r |jS|d|jtfd|ddDzS)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 zPolynomial has no roots.z"Cauchy bound not supported over %sNr!rc3:K|]}t|z ywN)abs).0r7lcs r) z)dup_cauchy_upper_bound..s2qs1r6{2s)r ris_ZZr5r is_QQis_RRis_CCris_zeropopr.zeror/r6)r%r&r7LrHs @r)dup_cauchy_upper_boundrRs 1 A1u899ww KKM1a#Q1 WW177>BCC aD ))AbE   ))AbE  1v{vv 1B 5532AabE22 22r+ct|}t|dkr td|jr|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.rrJr5rRr/)r%r&gbs r)dup_cauchy_lower_boundrVsP AA 1vzABBww KKMq!$A 5519r+ct|}|dkr td|jr |j}t ||||}}n2|j r|j s |jrtd|zt||}t||}|d|j|z|||dzz||dz zzz 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 %sr ) r rrJr5r rKrLrMrrrrF)r%r&r7rQDl2sqs r)dup_mignotte_sep_bound_squaredr[s 1 A1uQRRww KKM1a#Q1 WW177@1DEEAA q! $D Q4a=QqTAaC[4!A#;6 88r+c|\}}|j||j|}}|j||j|}}||||fS)z0Convert an open interval to a Mobius transform. )numerdenom)Ifieldr(r9r;crUds r)_mobius_from_intervalrcsK DAq ;;q>5;;q>qA ;;q>5;;q>qA aA:r+cN|\}}}}||||||}}||kr||fS||fS)z0Convert a Mobius transform to an open interval. )Mr`r;rUrarbr(r9s r)_mobius_to_intervalrgs=JAq!Q A;a qAAv1v 1v r+c|\}}}}||k(r ||k(r|||||ffSt||}||t|}n |j}|r(|dkDr#t|||}||z||z|j}}}||jk\r} t || | || | z |k\rt|| | | | f||\}\} } } } >nn|Zt || | || | z |k\r;t|| | | | f||\}\} } } } t || | || | z |k\r;|,t d|D]} t|| | | | f||\}\} } } } |; t | | | | f|\}}||ks||krnt|| | | | f||\}\} } } } :|st | | | | f|S|| | | | ffS)zGRefine a positive root of `f` given a Mobius transform or an interval. r-rkr)r5r.rcrrr2rFrg)r%rfr&epsstepsdisjointrkmobiusFr;rUrarbr:uvs r)dup_inner_refine_real_rootr| s  A 1v{*1a0 1a 1a3A1a 8 Aq!X1MN NH( a< yHH % 1a58$ HDAqQBxAwr+c |j|j|j|jf\}}}}t||}|dk(rgS|dk(rt|||||f|||dg} | Sg||||||fg} } | r| j \}}}}}}t ||} | |t | } n |j} |r(| dkDr#t|| |}| |z| |z|j} }}| |jk\rt|| |}| |z|z| |z|z}}t||s$| j|||||fft|d|}t||}|dk(r|dk(r&| jt|||||f|||d t||j|} |||z|||zdf\} }}}}t| |s&| j| ||||fft| d|d}} t| |}||z |z }|||z|||zf\}}}}|dkDrFtt||j|}t||s t|d|}t||}nd}||kr!|| ||f\} }}}||||f\}}}}|| ||f\} }}}|s| 9tt||j|} t| |s t| d|} |dk(r%| jt| | |||f|||dn| j| |||| |f|s|9tt||j|}t||s t|d|}|dk(r%| jt|||||f|||dn| j||||||f| r| S)aNInternal 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. rr T)rurkrxNri) r/rPrr|rOrBrjrrrr$rr )r%r&rurkr;rUrarbrqrootsstackrlf1rmrnrorprk1k2a2b2c2d2f2s r)dup_inner_isolate_real_rootsrfs-JAq!QAq!AAv Av+ 1a|QCd4AB| LwQ1aA./u$yy{ Aq!Q1$Q*A}c!fIFFBaA&A#qsAEEa1AEEzaA&sQw!a1a|LL!aAq\!23"1a+A'1-66LL!;Aq!Q<$t"MN1aeeQ'B !1q5!QUA 5 BBA"a= b2r2r"234"2q!,aA$R+BR!BAq!a%/NBBAv{1~quua8b!}#B1-B(Q/Bw!#RRBB!#RRBB!#RRBBz{1~quua8b!}#B1-BQw 7RR(!4NO b"b"b"56z{1~quua8b!}#B1-BQw 7RR(!4NO b"b"b"56or Lr+c|j} t||\} } |r| | } } || |k\r|| |kr |s| | fS||fS|| |kDs|| |kryt||||\}}Q)z9Discard an isolating interval if outside ``(inf, sup)``. Nrt)r5rgrr) r%rfinfsupr&rrkrxryrzr{s r)_discard_if_outside_intervalrs A "1a(1 2rqA K18#+c!t !t o!c's1s7,Q14@DAq r+c   ||dkrgSt||||}|jgc }||2|D]+\}} t|| |||d||} | |j| -|S|s|j fd|D|S|}|S)z@Iteratively compute disjoint positive root isolation intervals. rrurkFc3<K|]\}}t|ywrE)rg)rGrrfrys r)rIz3dup_inner_isolate_positive_roots..sCTQ*1a0Cs)rr5rr$extend) r%r&rurrrkrxrresultsrfresultrys @r) dup_inner_isolate_positive_rootsrs 37 (A3T BEJAw #/ 'DAq1!QS!UDRXYF!v&  ' N CUCC N Nr+c B||dk\rgStt|||||}|jg} }||2|D]+\}} t|| |||d||} | | j | -| S|s0|D])\}} t | |\} } | j | | f+| S|} | S)z@Iteratively compute disjoint negative root isolation intervals. rrT)rrr5rr$rg)r%r&rrrurkrxrryrrfrrzr{s r) dup_inner_isolate_negative_rootsrs 3!8 (Aq)91#D QEwA #/ 'DAq1!QS!T4QWXF!v&  ' N %DAq&q!,DAq NNQB8 $ % N Nr+cNt||\}}|dkDr|j}||dkrw|d|krp|sS|s|j|jf|fg|fS|j|jf||j|jgfg|fS|j|jfg|fSg|fS)z?Handle special case of CF algorithm when ``f`` is homogeneous. r)r r5rPr/)r%r&rrbasissqfr=rys r) _isolate_zerors A DAq1u KKM K3!8#+cffaff-q12A55ffaff-q155!&&/BCQFF()1,, q5Lr+c |jr#t||d|jc\}}}n|jst d|zt |dkrgSt ||||dd\}}t||||||} t||||||} t| |z| z} |s| S| D cgc]\} } t| | f||c} } Scc} } w)aIsolate 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 %srFrrrurrrk) rKrrrJrr rrrsorted RealInterval)r%r&rurrrkblackboxrI_zeroI_negI_posrr;rUs r)dup_isolate_real_roots_sqfr,s ww$Q48!**, A WWIAMNN!} aCEtDIFA ,QsSW XE ,QsSW XE 56>E) *E  :?AAq!fa+AAAs*Cc .|jr#t||d|jc\}}}n|jst d|zt |dkrgSt |||||d\}}t||\}} t| dk(rZ| \\}} t||||||} t||||||} | D cgc] \} }| |f| f } } }| D cgc] \} }| |f| f } } }nt| |||||| \} } t| |z| zScc}} wcc}} w) aIsolate 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. TrrrFrr r)rurrrrk) rKrrrJrr rrr.rr_real_isolate_and_disjoinr)r%r&rurrrrkrrfactorsrqrrrzr{s r)dup_isolate_real_rootsrNs4 ww$Q48!**, A WWIAMNN!} aCEuEIFAa#JAw 7|q !Q0A3CSW[\0A3CSW[\*/1$!QAq61+11*/1$!QAq61+110!#U? u %&.5( )) 21s 9D Dc D|jr@|j||dd}}}t|D]\} } t| ||dd|| <n|jst d|zdi} } ||dkr |d|krdi} } t|D]V\} } t | |\}} | r |dkDr| | <t| |dD]$\}}t|}|| vr| |i| |<|| || <&X| jDcgc]\}}t||f}}}t||||||||\}}|j}| r sg}nP|s|j|jf| fg}n2|j|jf| |j|jgfg}t||z|zScc}}w) aIsolate real roots of a list of 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. NTrr rFr)rurrstrictrrk)rKr enumeraterrJrr rtupleitemsr0rr5rPr/r)polysr&rurrrrrkryr:pzeros factors_dict zero_indicesr=r%rqindices factors_listrrrs r)dup_isolate_real_roots_listrws wwjjlAuQxe1e$ BDAq'1a>qAE!H B WWIAMNN L 9L L c Lt|dkry|js||j}}t|||}t ||}|4t |Dcgc]}t ||dt|zzc}|}n%t |Dcgc]}t|||c}|}|%t |Dcgc]}t ||c}|}n%t |Dcgc]}t|||c}|}t||z } |t|||s| dz } | Scc}wcc}wcc}wcc}w)zFReturns the number of distinct real roots of ``f`` in ``[inf, sup]``. rr!r ) r r#r5r r*rrrrF) r%r&rrRr'r( signs_inf signs_supcounts r)dup_count_real_rootsrs!} ::!++-1 1a  aOE {'TY([q&A,Z]7J*J([]^_ 'u(N!(1c1*=(NPQR  {'(G1&A,(GK 'u(N!(1c1*=(NPQR  I% &E x32   L)\(N)H(Ns"DD-DD!OOQ1Q2Q3Q4A1A2A3A4r r-rXr!r" rirr )r r)r!r)rr)rrr r-)r!r-r r r!r )rr-)rXr-)rXr)rr)rr)rr)r-r )rrrrirrcd|s|stS|s|dkDrtStS|s|dkDrtStSy)zEReturn the half-axis (or origin) on which (re, im) point is located. rN)rrrrr)reims r)_classify_pointrs9 b 6II  6II r+c`|sgSg}|s[|d\\}}} } ||cxk(r|k(rnnt|dk(r(t|||dkDr ttgSttgS|d\\}} } } t|||zdz |dkDr|j ttgd} n|j ttgd} |dd}n?t|||dkDr|j td} n|j td} |D]b\\}} } } |j t| ddzdk(r| } ||k7s3| dkDr|j tN|j td|S|s[|d\\}}} } ||cxk(r|k(rnnt|dk(r(t|||dkDr ttgSttgS|d\\}} } } t|||zdz |dkDr|j ttgd} n|j ttgd} |dd}n?t|||dkDr|j td} n|j td} |D]b\\}} } } |j t| ddzdk(r| } ||k7s3| dkDr|j tN|j td|St|||} t|||}| r|s{|j t| |t|dk(rt|||} t|||}n2|d\\}} } } t|||zdz |} t|||zdz |}|dd}| dkDrd} nd} |dkDrd} nd} ttttd}|j || | f|D]\\}}} } ||k(r9t|||} t|||}t| |}||j |d| vr| ddzdk(r| } d| vr| ddzdk(r| } ||k(r||k(rw|j || | f|S)zJGenerate a sequence of extended quadrants from a list of critical points. rr r-r!N)rr)r!r!)r r!)r.rrrrrr$rrrrrrr) intervalsrrr(r9ryQr;rUrf2_sgnrf1_sgnrrsgnclss r)_intervals_to_quadrantsr sU   A  | A1 ;Q;9~"B1%)8O8O(| A1BQ 1-1HHb"X&FHHb"X&F%abM Aq!A%  "+ ! FQGQ HHRLqzA~" AvA:HHRLHHRL !  | A1 ;Q;9~"B1%)8O8O(| A1BQ 1-1HHb"X&FHHb"X&F%abM Aq!A%  "+ ! FQGQ HHRLqzA~" AvA:HHRLHHRL ! "a B "a B R R() y>Q "a#B"a#B$Q.s"7TQ5'!*Q-(7sr-)rjsum)Tr`s `r)_winding_numberr!s" s7Q77%(B CCr+c F+,|js|jstd|z|jr||jc}+n|j |c}+t ||+}||9t |tt|+c},dt+,fd|Dz}| | } }n|\}} ||} } n|\} } t|+\} } t| | dd+}t| | dd+}t|+|d\}}t|+|d\}}t| | dd+}t| | dd+}t|+|d\}}t|+|d\}}t| | dd+}t| | dd+}t|+|d\}}t|+|d\}}t| |dd+}t| |dd+}t|+|d\}}t|+|d\}}||g}||g}||g} ||g}!t|||| ddd}"t||| | ddd}#t| ||| ddd}$t|!|| | ddd}%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-c3TK|]}jt|!ywrEquorFrGraryrHs r)rIz*dup_count_complex_roots..s /!%%A#/%(r Trr)rrrkrrr)rJrKrr5rr r rFrr6rrrrrrrr!)-r%r&rrrrrBrzr{r(r9rrf1L1Ff2L1Ff1L1Rf2L1Rf1L2Ff2L2Ff1L2Rf2L2Rf1L3Ff2L3Ff1L3Rf2L3Rf1L4Ff2L4Ff1L4Rf2L4RS_L1S_L2S_L3S_L4I_L1I_L2I_L3I_L4r r r rr ryrHs- @@r)dup_count_complex_rootsrBs  77177JQNOOww!++-1zz|Q1Aq!A {ck1 s6!Q<02 c/Q// / {"qbAA {"qbAA 1a FB Aq!Q 'E Aq!Q 'Eq!T:HAuq!T:HAu Aq!Q 'E Aq!Q 'Eq!T:HAuq!T:HAu Aq!Q 'E Aq!Q 'Eq!T:HAuq!T:HAu Aq!Q 'E Aq!Q 'Eq!T:HAuq!T:HAu 5>D 5>D 5>D 5>D &tQA14t\` aD &tQA14t\` aD &tQA14t\` aD &tQA14t\` aD d #D d #D "4q!Q ?D "4q!Q ?D "4q!Q ?D "4q!Q ?DD$dGDA 1a  r+c ||c\} } \} } |\}}}}|\}}}}|\}}}}|\}}}}| | zdz }t||dd| }t||dd| } t|| g| | | ddd}!gg}#}"|!|}%}$gg}'}&|t|!})}(|D]}|\\}}}*}+||k(rQ||k(r#|"j||#j|9||kr|"j|P|#j|b||kr|"j|y||k\r|#j|t |+||| j |d\}}||kr|"j||f|*|+f||k\s|#j||f|*|+f|D]}|\\}}}*}+||k(rQ||k(r#|&j||'j|9||kr|&j|P|'j|b||kr|&j|y||k\r|'j|t |+||| j |d\}}||kr|&j||f|*|+f||k\s|'j||f|*|+ft |"||| || },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|CfS)zFVertical bisection step in Collins-Krandick root isolation algorithm. r-rr Trrrkrrrwrkr( rrrr$rrrrr!)Drr;rUr_rF1F2rrryrzr{r(r9r>r?r@rAr r r rr*r.r2r6r+r/r3r7xf1Vf2VI_VI_L1_LI_L1_RI_L2_LI_L2_RI_L3_LI_L3_RI_L4_LI_L4_RrhQ_L1_LQ_L2_LQ_L3_LQ_L4_LQ_L1_RQ_L2_RQ_L3_RQ_L4_RT_LT_RN_LN_RI_LQ_LI_RQ_RF1_LF2_LF1_RF2_RrarbD_LD_RsD r)_vertical_bisectionrl@sNFQFQD$dD$d!#E5%!#E5% Q! A b!Q1 %C b!Q1 %C %sCj!UYae fCFF$FFFF-c2FF 8A 6Av a  a Q a  a Av a a a +Aq!QZZ\ATXY16MMAq67A"676MMAq67A"67-808A 6Av a  a Q a  a Av a a a +Aq!QZZ\ATXY16MMAq67A"676MMAq67A"67-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 3u %D 3u %D 5% %D 5% %D q6Aq6qA q6Aq6qA 1c3d +C 1c3d +C 8Or+c ||c\} } \} } |\}}}}|\}}}}|\}}}}|\}}}}| | zdz }t||dd| }t||dd| } t|| g| | | ddd}!||!}#}"gg}%}$t|!|}'}&gg})}(|D]}|\\}}}*}+||k(rQ||k(r#|$j||%j|9||kr|$j|P|%j|b||kr|$j|y||k\r|%j|t |+||| j |d\}}||kr|$j||f|*|+f||k\s|%j||f|*|+f|D]}|\\}}}*}+||k(rQ||k(r#|(j||)j|9||kr|(j|P|)j|b||kr|(j|y||k\r|)j|t |+||| j |d\}}||kr|(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|CfS)zHHorizontal bisection step in Collins-Krandick root isolation algorithm. r-r TrDrEr(rF)Drr;rUr_rrGrHrrryrzr{r(r9r>r?r@rAr r r rr*r.r2r6r+r/r3r7yf1Hf2HI_HI_L1_BI_L1_UI_L2_BI_L2_UI_L3_BI_L3_UI_L4_BI_L4_UrrUQ_L1_BQ_L2_BQ_L3_BQ_L4_BQ_L1_UQ_L2_UQ_L3_UQ_L4_UT_BT_UN_BN_UI_BQ_BI_UQ_UF1_BF2_BF1_UF2_UrarbD_BD_UsD r)_horizontal_bisectionrsNFQFQD$dD$d!#E5%!#E5% Q! A b!Q1 %C b!Q1 %C %sCj!UYae fC3FFFF',dFFFF 8A 6Av a  a Q a  a Av a a a +Aq!QZZ\ATXY16MMAq67A"676MMAq67A"67-808A 6Av a  a Q a  a Av a a a +Aq!QZZ\ATXY16MMAq67A"676MMAq67A"67-80F $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 1c3d +C 1c3d +C 8Or+cd\}}t|D]*\}\}\}}\}}}}}}||z ||z z} || |ks'| |}},|j|S)z0Find a rectangle of minimum area for bisection. NN)rrO) rectanglesmin_arear=r:rrzr{r(r9areas r)_depth_first_selectrsrKHa.7 .C"* *Av1v1q!QAA  thaH " >>! r+cB||c\}}\}}|||z |kxr||z |kSy)z8Return ``True`` if the given rectangle is small enough. Tre)r;rUrurzr{r(r9s r)_rectangle_small_pr s8NFQFQ 1us{*q1us{*r+c HI|js|jstd|zt|dkrgS|jr|j Hn|Ht ||H}t t|HIdtHIfd|Dz}| Hjf||fc\}}\} } ||}||} |dks | |ks| |kr tdt|H\} } t| |ddH} t| |ddH}t| | ddH}t| | ddH}t| | ddH}t| | ddH}t| |ddH}t| |ddH}| |g}||g}||g}||g}t|H|| ddd}t|H|| ddd}t|H|| ddd}t|H|| ddd}t|}t|}t|| ||| H}t||||| H}t|||| |H}t|||| |H} t!|||| }!t#|!H}"|"sgS||||f}#|||| f}$| |||f}%||||f}&|"||f| | f|#|$|%|&fgg}(}'|'rt%|'\}"\}}\} } }#}$}%}&| |z | |z kDrt'|"||f| | f|#|$|%|&| | H \})}*|)\}+},}-}.}/}0}1|*\}2}3}4}5}6}7}8|+dk\rF|+dk(r0t)|,|-|r#|(j+t-|,|-|.|/|0|1| | H n|'j+|)|2dk\r |2dk(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|;dk\rF|;dk(r0t)|,|-|r#|(j+t-|,|-|<|=|>|?| | H n|'j+|9@dk\rF@dk(r0t)|3|4|r#|(j+t-|3|4ABCD| | H n|'j+|:|'rt1|(d g}(}E|ED]#}F|(j3|Fj5|Fg%|r|(S|(DGcgc]}G|Gj7c}GScc}Gw) zTIsolate complex roots of a square-free polynomial using Collins-Krandick algorithm. z3isolation of complex roots is not supported over %srr-c3TK|]}jt|!ywrEr$r&s r)rIz0dup_isolate_complex_roots_sqf..9s +AaeeCFB +r'z'not a valid complex isolation rectangler TrDc2|j|jfSrE)axay)rs r)z/dup_isolate_complex_roots_sqf..sqtt r+)key)rJrKrr r5r rFrr6rPrrrrrrrr!rrlrr$ComplexIntervalrrr conjugateas_tuple)Jr%r&rurrrr)rzr{r(r9rrf1L1f2L1f1L2f2L2f1L3f2L3f1L4f2L4r:r;r<r=r>r?r@rAr r r rr rr_rrGrHrrrjrkr`r;rUrbrcrfrgrararbrdrerhrirrrrrrrrrrrr_rootsrootrryrHsJ @@r)dup_isolate_complex_roots_sqfr)sL 77177ORSSTT!} ww KKM Aq!A VAq\ B # + + ++Ab!&&\Aq6NFQFQ    1uQ!q&BCC 1a FB r1aA &D r1aA &D r1aA &D r1aA &D r1aA &D r1aA &D r1aA &D r1aA &D $DAqDH1u6(21c(:QBD(H%Aq!TX$PQST%UVV.1vs}}GJAq!QaAq6!2!*Aq6!2C9AQ1*DKs)DKBxDHcr+ctSrE)rrs r)funczRealInterval.funcsr+ch|}|j|jfz|j|jfSrE)rxrr%rrr:s r)argszRealInterval.argss+ AEE8#QSS!%%00r+cbt|t|ury|j|jk(SNFtyperrothers r)__eq__zRealInterval.__eq__( ;d4j (yyEJJ&&r+c|jj}|j\}}}}|js||z||zkr |||S|||S||z||zkDr ||| S||| S)z%Return the position of the left end. )rr5rxr)rr`r;rUrarbs r)r;zRealInterval.as""$[[ 1axxsQqSyQ{"A; sQqSya |#!QK< r+cV|j}| |_|j }||_|S)z&Return the position of the right end. )rr;)rwasrvs r)rUzRealInterval.bs-hh7ffW r+c4|j|jz S)z-Return width of the real isolating interval. rUr;rs r)dxzRealInterval.dxsvvr+c:|j|jzdz S)z2Return the center of the real isolating interval. r-r;rUrs r)centerzRealInterval.centers""r+cjt|jj|jjSz=Return the largest denominator occurring in either endpoint. )r6r; denominatorrUrs r) max_denomzRealInterval.max_denom s%466%%tvv'9'9::r+c2|j|jfS)z8Return tuple representation of real isolating interval. rrs r)rzRealInterval.as_tuplesr+c<d|jd|jdS)N(r~rrrs r)__repr__zRealInterval.__repr__s!VVTVV,,r+ct|tr|\}}n|d}}|dk(xr$|j|cxkxr|jkScS)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) isinstancerr;rUritemrrs r) __contains__zRealInterval.__contains__sG dE "FB1BQw1466R14661111r+cRt|tr4|j|jkxs|j|jkSt|tsJ|j|j kxs7|j |jkxs|j|jzdkDS)9Return ``True`` if two isolation intervals are disjoint. r) rrrUr;rrbxrbyrs r) is_disjointzRealInterval.is_disjoint*s e\ *FFUWW$8$&&(8 9%111!%UXX%6%xx 1$ &r+c|j|St|j|j|jdd\}}t ||j fz||jS)z2Internal one step real root refinement procedure. r T)rvrx)rxr|r%rrr)rr%rxs r) _inner_refinezRealInterval._inner_refine2sW ;; K. FFDKK4A 6Fdhh[0!TXX>>r+c|}|j|s2|j|j}}|j|s2||fSzDRefine an isolating interval until it is disjoint with another one. rrrrexprs r)refine_disjointzRealInterval.refine_disjoint<L""5),,.0C0C0E%D""5)U{r+ch|}|j|ks |j}|j|ks |SzERefine an isolating interval until it is of sufficiently small size. )rr)rrrs r) refine_sizezRealInterval.refine_sizeDs477R<%%'D77R< r+cJ|}t|D]}|j}|S)z9Perform several steps of real root refinement algorithm. r2rrrvrrs r) refine_stepzRealInterval.refine_stepL/u (A%%'D ( r+c"|jS)z4Perform one step of real root refinement algorithm. rrs r)refinezRealInterval.refineT!!##r+Nr )__name__ __module__ __qualname____doc__rpropertyrrrr;rUrrrrrrrrrrrrrer+r)rrsI"211'     ##;; -2$&?$r+rceZdZdZddZedZedZdZedZ edZ edZ ed Z ed Z ed Zed Zed ZdZdZdZdZdZdZdZddZddZdZy)raA 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;rUr_rrrGrrHrconj) rr;rUr_rrGrHrrrrs r)rzComplexInterval.__init__sPAArr r+ctSrE)rrs r)rzComplexInterval.funcsr+c |}|j|j|j|j|j|j |j |j|j|jf SrE) r;rUr_rrGrHrrrrrs r)rzComplexInterval.argssJ QSS!##qssADD!$$addAEE166JJr+cbt|t|ury|j|jk(Srrrs r)rzComplexInterval.__eq__rr+c |jdS)z1Return ``x`` coordinate of south-western corner. r)r;rs r)rzComplexInterval.axvvayr+cX|js|jdS|jd S)z1Return ``y`` coordinate of south-western corner. r )rr;rUrs r)rzComplexInterval.ay)yy66!9 FF1I: r+c |jdS)z1Return ``x`` coordinate of north-eastern corner. r)rUrs r)rzComplexInterval.bxrr+cX|js|jdS|jd S)z1Return ``y`` coordinate of north-eastern corner. r )rrUr;rs r)rzComplexInterval.by r r+c@|jd|jdz S)z0Return width of the complex isolating interval. rrrs r)rzComplexInterval.dxvvay466!9$$r+c@|jd|jdz S)z1Return height of the complex isolating interval. r rrs r)dyzComplexInterval.dyr r+cr|j|jzdz |j|jzdz fS)z5Return the center of the complex isolating interval. r-rrrrrs r)rzComplexInterval.centers3477"A%$''(91'<==r+ct|jj|jj|jj|j jSr)r6rrrrrrs r)rzComplexInterval.max_denom$sB477&&(;(;77&&(;(;= =r+cb|j|jf|j|jffS)ziReturn tuple representation of the complex isolating interval's SW and NE corners, respectively. )rrrrrs r)rzComplexInterval.as_tuple*s)$''"TWWdgg$677r+c pd|jd|jd|jd|jd S)Nrr~z) x (rrrs r)rzComplexInterval.__repr__/s!(,$''477KKr+c t|j|j|j|j|j |j |j|j|jd S)z=This complex interval really is located in lower half-plane. T)r) rr;rUr_rrGrHrrrrs r)rzComplexInterval.conjugate2sJtvvtvvtvvtvv GGTWWdggtwwtE Er+ct|tr|\}}n|d}}|j|cxkxr|jkncxr$|j|cxkxr|j kScS)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)rrrrrrrs r)rzComplexInterval.__contains__7sU dE "FB1Bww"''DDGGr,DTWW,DD,DDr+cRt|tr|j|S|j|jk7ry|j|j kxs|j|j k}|ry|j |jkxs|j |jk}|S)rT)rrrrrrrr)rr re_distinct im_distincts r)rzComplexInterval.is_disjointJs e\ *$$T* * 99 "ww)?UXX-? ww)?UXX-? r+c |j|jc\}}\}}|j|j}}|j|j }}|j |j} } |j} ||z ||z kDr9td||f||f|||| || | \} } | ddk(r | \}}}}}}} nC| \}}}}}}} n8td||f||f|||| || | \}}|ddk(r |\}}}}}}} n |\}}}}}}} t|||||| || | |j S)z5Internal one step complex root refinement procedure. r r) r;rUr_rrrGrrHrrlrrr)rrzr{r(r9r_rrrGrrHrrjrkrr;rUrrs r)rzComplexInterval._inner_refineVsCAAvvtvv1$''B$''Bhh q51q5=*1q!fq!faBBPRTWXHC1v{(+%1aAr2(+%1aAr2,QAA1b"bRTVYZHC1v{(+%1aAr2(+%1aAr2q!Q2r2r3 JJr+c|}|j|s2|j|j}}|j|s2||fSrrrs r)rzComplexInterval.refine_disjointrrr+Nc||}|}|j|kr|j|ks0|j}|j|ks |j|ks0|Sr)rrr)rrrrs r)rzComplexInterval.refine_sizezsR :B77R>== 8 LE E& K8$r+rr)NNNFF)NNNFr)FF)NNNFFFrrE)NNN)Vrsympy.polys.densearithrrrrsympy.polys.densebasicrrr r r r r sympy.polys.densetoolsrrrrrrrrrrsympy.polys.euclidtoolsrsympy.polys.factortoolsrsympy.polys.polyerrorsrrrsympy.polys.sqfreetoolsrrr*r?rBrRrVr[rcrgrrr|rrrrrrrrrrrrrrrrrrrrrrr rrrrrrr!rBrlrrrrrrrrrer+r)r'sM A  $L*.X"3@ 9: ,\(T n!FpdA(,0$ BD'*R8*t.,;z8         *Ha*Ha*Ha * Ha *Ha*Ha*Ha*Ha*Ha*Ha* Ha!*"Ha#*(Ha)**Ha+*,Ha-*.Ha/*4Ha5*6HaHaHaHbHbHbHbHbHbHbHbS* XHRL"HRL"HRL" H RL" HRL"HRL"HRL"HRL"HRL"HRL"H RL"!H"RL"#H(RL")H*RL"+H,RL"-H.RL"/H4Ha5H6Ha7H8Ha9H:Ha;H>RL!?H@RL!AHBRL!CHDRL!EHJHaKHLHaMHNHaOHPHaQHTRL!UHVRL!WHXRL!YHZRL![H`HaaHbHacHdHaeHfHagHjRL!kHlRL!mHnRL!oHpRL!qHvHbwHxHbyHzHb{H|Hb}H@RL"AHBRL"CHDRL"EHFRL"GHLHbMHNHbOHPHbQHRHbSHVRL"WHXRL"YHZRL"[H\RL"]HbHbcHdHbeHfHbgHhHbiHlRL"mHnRL"oHpRL"qHrRL"sHxHbyHzHb{H|Hb}H~HbHBRL"CHDRL"EHFRL"GHHRL"IHNHbOHPHbQHRHbSHTHbUHXRL"YHZRL"[H\RL"]H^RL"_HdRL"eHfRL"gHhRL"iHjRL"kHpRL"qHrRL"RL"RL"RL"RL"RL"RL"RL"RL"RL"RL"OHT y y y y   y   '  ' ' '        '  '! "'# $ ' ' ' ' ' '/ 4 T lJXRDH!ThThT {/z[ Y(V$V$ru$u$r+