K i dZddlZddlmZddlmZmZmZddlm Z ddl m Z ddl m Z ddlmZmZdd lmZdd lmZmZdd lmZdd lmZdd lmZddlmZmZmZddl m!Z!ddl"m#Z#m$Z$m%Z%m&Z&m'Z'ddl(m)Z)m*Z*ddl+m,Z,m-Z-m.Z.ddl/m0Z0ddl1m2Z2m3Z3m4Z4m5Z5ddl6m7Z7ddl8m9Z9m:Z:m;Z;mm?Z?ddl@mAZAddlBmCZCddlDmEZEedZFdZGdZHd.dZId ZJd!ZKd"ZLd#ZMd.d$ZNd%ZOd&ZPd'ZQd(ZReCd)d)dd)dddddd* d+ZSdd,d-ZTy)/z8Algorithms for computing symbolic roots of polynomials. N)reduce)SIpi) factor_terms)_mexpand) fuzzy_not) expand_2argMul)igcd)Rationalcomp)Pow)Eq)ordered)DummySymbolsymbols)sympify)expimcosacos Piecewise)rootsqrt)divisorsisprime nextprime)EX)PolynomialErrorGeneratorsNeeded DomainErrorUnsolvableFactorError) PolyQuintic)Polycancelfactorgcd_list discriminant)together)cyclotomic_poly)public) filldedentzc|jd |jdz }|j}|js(|jrt |}|gSddlm}||}|gS)z/Returns a list of roots of a linear polynomial.rsimplify)nth get_domain is_Numerical is_Compositer(sympy.simplify.simplifyr3)frdomr3s [/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/polys/polyroots.py roots_linearr=%sb q !%%(A ,,.C      q A 3J 9 A 3Jc|j\}}}|jd}fd}|tjurBtj| |z }}js ||}||gS|j r||}}||gS|tjur+| |z }js||}||} | }| }||gS|dzd|z|zz } d|z} | | z } js|| } || } t || | z } | | z }| | z}|j r||}}||gSjs||fDcgc] }t|c}\}}||gScc}w)aPReturns a list of roots of a quadratic polynomial. If the domain is ZZ then the roots will be sorted with negatives coming before positives. The ordering will be the same for any numerical coefficients as long as the assumptions tested are correct, otherwise the ordering will not be sorted (but will be canonical). cg}g}tj|D]z}|jr[|jjrE|jdzdk(r3|j t |j|jdzj|j |||rt|}t|}|t|zSt|S)Nr) r make_argsis_Powr is_Integerappendrbaser)dcootherdis r<_sqrtzroots_quadratic.._sqrt?s--" !ByyRVV..266A:? #bggrvvqy12 R  ! U AbBd1g: Awr>cNjr t|Sddlm}||S)Nrr2)r7r(r8r3)exprr3r;s r< _simplifyz"roots_quadratic.._simplifyPs#   $<  8D> !r>rA) all_coeffsr5rZeror6 is_negativerr )r9abcrKrNr0r1r:RrGABDir;s @r<roots_quadraticr]4sllnGAq! ,,.C"" AFF{!AB2B8 8O7^^B4 8O3 aff BqD! A !HR $ 8O! qD1Q3q5L aC BqD! A! A q! $ U U ==B 8O!!/12h7k!n7FB 8O8s8EFc |r8|j\}}}}d|z|z|dzz d|dzzz }d|dzzd|z|z|zz d|dzz|zzd|dzzz }d|z|z|z|zd|dzz|zz |dz|dzzzd|z|dzzz d|dzz|dzzz }|dkDdk(rg} tdD]q} | jdt| dz zt t ||z td |z zt ddzdz | tzt ddzz zs| D cgc] } | |dz |z z c} S|jj\} }}}|tjur&td ||gd \} }| tj|gS||dzdz z }|||zdz z d|dzzdz z}|dz }|dz }d }|tjur@|tjur| gdzS|jr t|d n t| d}n|tjur7td d|gd \}}|tj|fDcgc]}||z  c}S|jr5|jr)t| dz t|dzdz |dzzzd }t tdzdz }|tj"}t d d|z}t d d|z }|||}}}|dzd|zz }d|dzzd|z|zz d|zz}t|t|dzd|dzzz zdz d}|||fDcgc]}|||zz||z |z z dz c}S|t d d|zz}|t d d|z z}|tjur||z ||z ||z gS| ||z z|z | ||z z|z | ||z z|z g}|Scc} wcc}wcc}w)zReturns a list of roots of a cubic polynomial. References ========== [1] https://en.wikipedia.org/wiki/Cubic_function, General formula for roots, (accessed November 17, 2014). rA rOrTr1multipleN)rPrangerErrrr rmonicrrQroots is_positiveris_realrRrOne)r9trigrSrTrUrGpqr[rvkr\_x1x2pon3aon3u1y1y2tmpcoeffu2u3D0D1Cuksolns r< roots_cubicrzsY \\^ 1a qSUQT\AadF # q!tVac!eAg 1a4 )Bq!tG 4 qDF1HQJ1a4 !AqDAI -!AqD 82ad71a4< G Ed?B1X i !D!AJ,s4!DAJx1~0M+Nq+PSTUWSWX`abdeXfSf+f'ggh i')*!A!AI* *%%'JAq!QAFF{1ay40BAFFB AqDF A AaCE AadF2IA Q3D Q3D BAFF{ ;E719 MMd1aj[tQB{ aff1ay40B')1662&67sd 77 q}}A2a4$q!tAva/00!4 4 d1gIaKE z UU b!_u $ b!_u $Qa1 TAaCZ q!tVac!e^bd " "tBEAb!eGO,,a/ 335r2,?B!bd(RT"W$%a'?? Xb!_u $ %B Xb!_u $ %BAFF{T 29b4i00 d2g  d2g  d2g  D Ke+.8@s'O O <Octd}d|dzzd|z|dzzzd|dzzd|zz |zz|dzz }ttt||d j }|Dcgc]}|j s|j s| }}|sy t|}t|} | | zd|zz } | |dz z } t| | z} t| | z } | | z |z | | z |z | | z|z | | z|z gScc}w) al Descartes-Euler solution of the quartic equation Parameters ========== p, q, r: coefficients of ``x**4 + p*x**2 + q*x + r`` a: shift of the roots Notes ===== This is a helper function for ``roots_quartic``. Look for solutions of the form :: ``x1 = sqrt(R) - sqrt(A + B*sqrt(R))`` ``x2 = -sqrt(R) - sqrt(A - B*sqrt(R))`` ``x3 = -sqrt(R) + sqrt(A - B*sqrt(R))`` ``x4 = sqrt(R) + sqrt(A + B*sqrt(R))`` To satisfy the quartic equation one must have ``p = -2*(R + A); q = -4*B*R; r = (R - A)**2 - B**2*R`` so that ``R`` must satisfy the Descartes-Euler resolvent equation ``64*R**3 + 32*p*R**2 + (4*p**2 - 16*r)*R - q**2 = 0`` If the resolvent does not have a rational solution, return None; in that case it is likely that the Ferrari method gives a simpler solution. Examples ======== >>> from sympy import S >>> from sympy.polys.polyroots import _roots_quartic_euler >>> p, q, r = -S(64)/5, -S(512)/125, -S(1024)/3125 >>> _roots_quartic_euler(p, q, r, S(0))[0] -sqrt(32*sqrt(5)/125 + 16/5) + 4*sqrt(5)/5 x@r_ rArOF)cubicsN) rlistrir&keys is_rational is_nonzeromaxr)rnror:rSreqxsolssolrXc1rZrYc2c3s r<_roots_quartic_eulerrsR c A AqD2a419 !Q$A q0 01a4 7B tB{51668 9E! HSS__S HE H  E A aB 2qs A QqSA a!eB a!eB GaK"rAsRx!|R"Wq[ AA Is*C9< C9 C9c jj\}}}}}|s#tjgt d|||gdzS||z dz|k(rj ||z }}t |dz||zz|zd|zz |}t|\} } t |dz| |zz |z|} t |dz| |zz |z|} t| } t| }| |zS|dz}|d|zdz z t|||dz |dz z zz|dz t||d|zdz |dz z z|zzz }jrCt d|gdDcgc] }t|c}\}}| | ||fDcgc]}|z  c}S|jr6tjgt dd gdz}|Dcgc]}|z  c}St|}|r|Sdz d z |z }dz d z |zdz zdzdz z }tdd}fd }t|}td dz||zz }|jr||St|dzdz |dzdz z}| dz |z}||z}td dz|z||z dz z }t|jr||St||||Dcgc]\}}t|t!|d f|df!c}}Scc}wcc}wcc}wcc}}w)a Returns a list of roots of a quartic polynomial. There are many references for solving quartic expressions available [1-5]. This reviewer has found that many of them require one to select from among 2 or more possible sets of solutions and that some solutions work when one is searching for real roots but do not work when searching for complex roots (though this is not always stated clearly). The following routine has been tested and found to be correct for 0, 2 or 4 complex roots. The quasisymmetric case solution [6] looks for quartics that have the form `x**4 + A*x**3 + B*x**2 + C*x + D = 0` where `(C/A)**2 = D`. Although no general solution that is always applicable for all coefficients is known to this reviewer, certain conditions are tested to determine the simplest 4 expressions that can be returned: 1) `f = c + a*(a**2/8 - b/2) == 0` 2) `g = d - a*(a*(3*a**2/256 - b/16) + c/4) = 0` 3) if `f != 0` and `g != 0` and `p = -d + a*c/4 - b**2/12` then a) `p == 0` b) `p != 0` Examples ======== >>> from sympy import Poly >>> from sympy.polys.polyroots import roots_quartic >>> r = roots_quartic(Poly('x**4-6*x**3+17*x**2-26*x+20')) >>> # 4 complex roots: 1+-I*sqrt(3), 2+-I >>> sorted(str(tmp.evalf(n=2)) for tmp in r) ['1.0 + 1.7*I', '1.0 - 1.7*I', '2.0 + 1.0*I', '2.0 - 1.0*I'] References ========== 1. http://mathforum.org/dr.math/faq/faq.cubic.equations.html 2. https://en.wikipedia.org/wiki/Quartic_function#Summary_of_Ferrari.27s_method 3. https://planetmath.org/encyclopedia/GaloisTheoreticDerivationOfTheQuarticFormula.html 4. https://people.bath.ac.uk/masjhd/JHD-CA.pdf 5. http://www.albmath.org/files/Math_5713.pdf 6. https://web.archive.org/web/20171002081448/http://www.statemaster.com/encyclopedia/Quartic-equation 7. https://eqworld.ipmnet.ru/en/solutions/ae/ae0108.pdf r1TrdrAr_rOrr lct d|zz}d zd|zz}d z|z }g}dD];}t|||zz }dD]"}|j||z||zz dz z $=|S)NrAr_rfr1)rrE) ywarg1arg2anssrtaon4er9s r<_anszroots_quartic.._ansPsQqSMsQqSys1u ra)rhrPrrQrigenr&r]ris_zerorrr r ziprr)r9rrrSrTrUrGrmgz1z2h1h2rWr2a2rzrxryrsolsrnroTHrrr:ua1rrs` @@r< roots_quarticrsQ^GGI((*MAq!Q x%Aq! t<<< A#Quuac1 A!a!A#%q ) #B !Q$A+/1 % !Q$A+/1 % R  R Bw T "QJ QBqD1Q3J' (s Qq!B$r'AaC-01455 6 99Q1I57Cd3i7FB,.3R*<=3C$J= = 995!Q1==A*+,3C$J, ,(1a6D Ab1 AAc AaCE!AqDF*A!QB  A8B?"QU*ByyBx1QAb()D1t A2A8B?"Q&1Q.B#Bx#&d2hR"9;Br2a8nr4j9; ;_7=-T;sK&2 K+4 K0>$K5cl|j}|j||jd}}t||z  }t||}|jr|j d}|j }|dzdk(}|r|dk(r|dzjrd}nd}g} |dz} |r| j| | dz} |s| jdt| ddD]-} |r| j| | g| j| | g/|rJ| jdr7tdt| dD]} | | | dz} tt| } gdtztz|z }} | D]D}t!||zj d}| j||zj dF| S) aOReturns a list of roots of a binomial polynomial. If the domain is ZZ then the roots will be sorted with negatives coming before positives. The ordering will be the same for any numerical coefficients as long as the assumptions tested are correct, otherwise the ordering will not be sorted (but will be canonical). rTcomplexrAr1Frf) power_base)degreer4r'r is_numberexpandrRrjrErgextendlenrreversedrrr)r9nrSrTrFalphanegevenbigksimaxr\pairrirGrqzetas r<roots_binomialrss  A 558QUU1XqA 1Q3Kct|Dcgc]}t|dzs|dz}}d\}}|D]}||z}||dz z}|}ttj|t ||z z}dx}}g}||kr't |}|j|||z}||kr'||z}d}|ddD] }||dz z} ttj|t ||z z}||fScc}w)z Find ``(L, U)`` such that ``L <= phi^-1(m) <= U``. Examples ======== >>> from sympy.polys.polyroots import _inv_totient_estimate >>> _inv_totient_estimate(192) (192, 840) >>> _inv_totient_estimate(400) (400, 1750) r1)r1r1rANrf)rrintmathceilfloatrrE) rrGprimesrSrTrnLUPs r<_inv_totient_estimaters'qk =WQU^q1u =F = DAq  Q QU  A DIIaq!n %&A IA F q& aL a Q q& !GA A CR[ QU  DIIaq!n %&A a4K7>s C/C/c t|j\}}t||dzD]5 t |jd}|j |j k(s5n t dg}|s dz td dzDcgc]}t| dk(s|}}|j fddtztz z }t|D]/} |jt| |zjd1|St|t!d  }t#|j%dD]%\ } |j j' '|Scc}w) z)Compute roots of cyclotomic polynomials. r1T)polysz/failed to find index of a cyclotomic polynomialrAc6|kr|dfSt|z dfS)Nrfr1)abs)rhrs r<z"roots_cyclotomic..s!aq"gc!a%j!_r>)keyrrf) extension)rrrgr,rrM RuntimeErrorr sortrrrrErrr&rr factor_listTC) r9r(rrrrir\rrGrqrrrrs @@r<roots_cyclotomicrsQ  ,DAq 1a!e_N AquuD 1 66QVV   N LMM E  qDq!a% |<|=|ztdzz j%}?;j%}@d x}A}Bt3dD]}7|-d|7}Ct3dD]w}8|-d|8}Dt'|:Cdzz@|Ddzzz|>z j%d |s9t'D|:dzzC@dzzz|?z j%d |sg|,d|7}A|,d|8}BnAngS|9AzBz|;zdz }/|9|z|A|zz|B|zz|;|zzdz }0|9|z|A|zz|B|zz|;|zzdz }1|9|z|A|zz|B|zz|;|zzdz }2|9|z|A|zz|B|zz|;|zzdz }3|/|0|1|2|3g}t9}E|D],} | j%d} | EvrgcSEj;| .|r|D cgc] } | |dz z  }} |Scc} w) z Calculate exact roots of a solvable irreducible quintic with rational coefficients. Return an empty list if the quintic is reducible or not solvable. c34K|]}|jywN is_Rational).0r{s r< z roots_quintic..sQUu  Qr1rAr_rO}i5 rg|=N)rPallr&rr%is_irreduciblef20r is_linearrr*rrTrl0_quintic_simplifyorderrrr/r as_real_imag enumeratexreplacergruvsetadd)Fr9resultcoeff_5coeff_4p_q_r_s_rrrnror:rrquinticr_factorthetarGdeltazeta1zeta2zeta3zeta4rtolr alpha_barbetabeta_bardiscdisc_barrStwol1l4l2l3rtestR1R2R3R4ResRes_nx0rsrtx3x4x5x6x7rr\jrWr1_nr4rvtestplus testminusr4_nrr3r2temp_nr3temp_nsawsF r< roots_quinticr.s F'(||~$GWb"b" QwRR.PQ Q !| W %&\\^"7BB 77"1$ $ 72a !GQJ,r/ 1 72a !GQJ,r/""4 4q!|C7G G A  2 b 0 07A:b=3D DqRS|TXGX X EE A!Q$1a4'!A#-1 2R^ 1a!nG   ++C  ??$Q' 1:  AJOOA&E  QA GE!(E5% %A E(C aD1Q4: E!qtEz!I Q4!A$u* Dtad5j H !8af D!|aj(H E B Q4D UFT$Z/47 8B UFT$Z/47 8B YJh74? @B YJh74? @B MM% #E %'')O"$$&2446/BDDFRTTVO!D FD a RB bhE !BuH ,r%x 7B bhE !BuH ,r%x 7B bhE !BuH ,r%x 7B bhE !BuH ,r%x 7B $D6!8dVAXvax 8C D6!8dVAXvax$ :E 2 B 2 B 2 B 2 B QqT!VB aB aB a"fB 2B aB 2aB DaLB sBrEBJRURZ2#rBw{2CR"rTUEV WC  B  B  B  B#H1%ajj!RUQr!uW_1E&FGAq %ajj!RUQr!uW_1E&FGAq %ajj!RUQr!uW_1E&FGAq %ajj!RUQr!uW_1E&FGAq H 1a[5q 5Aa&)++-E!HQK)#a&)4CF1I 55 QB 8A;D 1X 4a #$a -QB  ::eQ DAqAeGDGO#&&(HQuWT!W_$'')I 446DNB 1X8A;q AQx{Hd8Q;&hk)99HDGGI1cRhtQw&$')99IEHHJAsSVAYVAY  >   r'B, Q B U(RX 5 (2e8 3Q 6B U(RX 5 (2e8 3Q 6B U(RX 5 (2e8 3Q 6B U(RX 5 (2e8 3Q 6B"b"b !F %C  CCF 8I  +12a!gk/22 M3s`.cJddlm}||}t|}t|S)Nr)powsimp)r8r0r'r+)rMr0s r<rrs!/ 4=D $r>ctt|j\}}tt|\}ttt|}|d|dkr5tt |}|d}t |Dcgc]}||z  }}ny|dd}|dd}t |dk(r;t|dtj|dz }|jr t|Syt tt|dd} t|} t||D]\}} | ||zzdk7s t|}n|S0cc}w#t$rYywxYw#t$rYywxYw)a~Compute coefficient basis for a polynomial over integers. Returns the integer ``div`` such that substituting ``x = div*y`` ``p(x) = m*q(y)`` where the coefficients of ``q`` are smaller than those of ``p``. For example ``x**5 + 512*x + 1024 = 0`` with ``div = 4`` becomes ``y**5 + 2*y + 1 = 0`` Returns the integer ``div`` or ``None`` if there is no possible scaling. Examples ======== >>> from sympy.polys import Poly >>> from sympy.abc import x >>> from sympy.polys.polyroots import _integer_basis >>> p = Poly(x**5 + 512*x + 1024, x, domain='ZZ') >>> _integer_basis(p) 4 rrfNr1)rrtermsmaprrrrrrlrDrrr)next StopIteration) polymonomscoeffsrr\r:divsdivmonomr{s r<_integer_basisr<s|,#tzz|,-NFF3< GF #c6" #F ay6":hv&' 1I!)&!12A!a%22 CR[F CR[F 6{a q 155? + <<q6M HXf-.qr2 3D4j / LE5sEz!Q&t*C J -3& %  s* E E/ E EE E! E!c\tj}|j} |jd\}}|j d}|j }|jjr1td|jjDr|j}tt|j}t|j dd}|d|dd}}tt||D]\}}d} |d|dkr t#|}d} d} t||D]+\} } | s| s | r| s?| | zdk7rI| | z} | | } %| | k7s+[| r| } |j%|d}||| zz}|j'||r|j(|}|j*rS|jj,r9t/|,|j1fd }|j3|}|z}t5||s||}||fS#t$r||fcYSwxYw) z7Try to get rid of symbolic coefficients from ``poly``. T)convertr1c34K|]}|jywr)is_term)rrUs r<rz#preprocess_roots..s(Nq(NrNrFrfc ||dz zzS)Nr)rqr{basisrs r<funczpreprocess_roots..func*sea!A$h///r>)rrlrD clear_denomsr# primitiveretractr5is_Polyrrepr8injectrrr7gensrevalremoveeject is_univariateis_ZZr<rtermwise isinstance)r6r{ poly_funcrrstripsrKrFrstripreverseratiorSrT_ratiorDrCrs @@r<preprocess_rootsrYs2 EEE I##D#14 >> A D <<>D   S(NDHHOO 4::t$D doo/55t$   A 0==&D UNE dI & $; d{sH H+*H+T) autorrmquarticsquinticsrefilter predicatestrictc L0ddlm} t| } t|trS| r t dt d} it|dz }}|D]}t||dz c||<}t|| d}n t|g| i| }t|ts!|jjs td|}|j}|jd k(r`|d kDrZ|jj |j"\}}| j% }||k7r|}g}t'j(|D]Y}|j*s|j-\}}|j.s0|j0s=|j3|t d f[|rt5t||zj7t|g|j"g|j"||||d | }|j9Dcic]4\}}t;|j7|Dcic]\}}|| c}}6c}}S|j>r td d }0fd}fd0tAdt|j"zt }|jC|jD|}|jG\\}}|si}ntHjJ|i}tM|\}}|r*|jOjPr|jS}|jOjTr.|jC|jDjWtX}d} d}!i}"|jZs|jO}#|#j\s.|#j^r"|jaD] }$||"||$dn|jdk(r||"|tc|ddn|jd k(r9|jd k(rtdntf}%|%|D] }$||"||$dnLt|jji\}&}'t|'dk(r/|jd k(rte|D] }$||"||$dnt|'dk(r|'dddk(r|jOjjra| |}(|(r>|(d |(d d\}!}n |(d|(d}} t5|}"|"s||D] })||"||)dnj0|D] }$||"||$dnQ||D] })||"||)dn8|'D]3\}*}0t|*|jdD] }$||"||$|5|tHjlur$|"i}"}+|+j9D] \})}||"||)z<|dvr;ddddd}, |,|}-t|"jqD]}.|-|.r |"|.=|*t|"jqD]}.||.r |"|.=| r$i}/|"j9D] \}}||/|| z<|/}"|!r$i}/|"j9D] \}}||/||!z<|/}"|"js|| r>tu|"jw|jkrtyt{d|s|"Sg}t}|"D]}.|j|.g|"|.z|Scc}}wcc}}w#t<$r |rgcYSicYSwxYw#tn$rt d|zwxYw)a Computes symbolic roots of a univariate polynomial. Given a univariate polynomial f with symbolic coefficients (or a list of the polynomial's coefficients), returns a dictionary with its roots and their multiplicities. Only roots expressible via radicals will be returned. To get a complete set of roots use RootOf class or numerical methods instead. By default cubic and quartic formulas are used in the algorithm. To disable them because of unreadable output set ``cubics=False`` or ``quartics=False`` respectively. If cubic roots are real but are expressed in terms of complex numbers (casus irreducibilis [1]) the ``trig`` flag can be set to True to have the solutions returned in terms of cosine and inverse cosine functions. To get roots from a specific domain set the ``filter`` flag with one of the following specifiers: Z, Q, R, I, C. By default all roots are returned (this is equivalent to setting ``filter='C'``). By default a dictionary is returned giving a compact result in case of multiple roots. However to get a list containing all those roots set the ``multiple`` flag to True; the list will have identical roots appearing next to each other in the result. (For a given Poly, the all_roots method will give the roots in sorted numerical order.) If the ``strict`` flag is True, ``UnsolvableFactorError`` will be raised if the roots found are known to be incomplete (because some roots are not expressible in radicals). Examples ======== >>> from sympy import Poly, roots, degree >>> from sympy.abc import x, y >>> roots(x**2 - 1, x) {-1: 1, 1: 1} >>> p = Poly(x**2-1, x) >>> roots(p) {-1: 1, 1: 1} >>> p = Poly(x**2-y, x, y) >>> roots(Poly(p, x)) {-sqrt(y): 1, sqrt(y): 1} >>> roots(x**2 - y, x) {-sqrt(y): 1, sqrt(y): 1} >>> roots([1, 0, -1]) {-1: 1, 1: 1} ``roots`` will only return roots expressible in radicals. If the given polynomial has some or all of its roots inexpressible in radicals, the result of ``roots`` will be incomplete or empty respectively. Example where result is incomplete: >>> roots((x-1)*(x**5-x+1), x) {1: 1} In this case, the polynomial has an unsolvable quintic factor whose roots cannot be expressed by radicals. The polynomial has a rational root (due to the factor `(x-1)`), which is returned since ``roots`` always finds all rational roots. Example where result is empty: >>> roots(x**7-3*x**2+1, x) {} Here, the polynomial has no roots expressible in radicals, so ``roots`` returns an empty dictionary. The result produced by ``roots`` is complete if and only if the sum of the multiplicity of each root is equal to the degree of the polynomial. If strict=True, UnsolvableFactorError will be raised if the result is incomplete. The result can be be checked for completeness as follows: >>> f = x**3-2*x**2+1 >>> sum(roots(f, x).values()) == degree(f, x) True >>> f = (x-1)*(x**5-x+1) >>> sum(roots(f, x).values()) == degree(f, x) False References ========== .. [1] https://en.wikipedia.org/wiki/Cubic_equation#Trigonometric_and_hyperbolic_solutions r)to_rational_coeffszredundant generators givenrr1T)fieldzgenerator must be a SymbolrA)positive)rZrrmr[r\rer]r^*multivariate polynomials are not supportedc|tjk(rAtj|vr|tjxx|z cc<n||tj<||vr||xx|z cc<y|||<yr)rrQ)rzeros currentrootrqs r< _update_dictzroots.._update_dictsZ !&& vvaff " !aff & ; 1 $ "#F; r>c|jg}}|dD]}|j||ddD]M}t|g}}|D]9}|t||jz }|D]}|j|;O|S)z+Find roots using functional decomposition. rr1N) decomposerErr&r)r9factorsrirg currentfactorpreviousr_try_heuristicss r<_try_decomposezroots.._try_decomposes*71:6 &K LL % &%QR[ .M"5k2eH' . !Daee$<<#21#5.KLL-. . . r>c v|jrgS|jr"tjg|j zS|j dk(r@|j dk(r"t ttt|St|Sg}dD]X}|j|r|jt|j|z |j}|j|n|j }|dk(r'|t ttt|z }|S|dk(r'|t ttt!|z }|S|j"r|t%|z }|S|dk(rr|t'|z }|S|dk(rr|t)|z }|S|dk(rr|t+|z }|S)z+Find roots using formulas and some tricks. rAr1rr_)rmrOr) is_ground is_monomialrrQrlengthrr3r'r=rrLquor&rrEr] is_cyclotomicrrrr.)r9rr\rrr[r\rms r<rnzroots.._try_heuristicss ;;I ==FF8AHHJ& & 88:?xxzQC Q899%a(( A66!9EE$quuqy!%%01 a    HHJ 6 d3v|A78 8F !V d3vq'9:; ;F __ &q) )F !V k!$/ /F  !V mA& &F !V mA& &F r>zx:%d)clsNrf)Nrc|jSr)rDr:s r<rzroots..ls 1<<r>c|jSrrrxs r<rzroots..ms 1==r>cBtd|jDS)Nc34K|]}|jywr)rk)rrSs r<rz*roots....nsEQqyyEr)ras_numer_denomrxs r<rzroots..ns3E!2B2B2DEEr>c|jSr) is_imaginaryrxs r<rzroots..os 1>>r>)ZQrXrzInvalid filter: %sa Strict mode: some factors cannot be solved in radicals, so a complete list of solutions cannot be returned. Call roots with strict=False to get solutions expressible in radicals (if there are any). )@sympy.polys.polytoolsradictrRr ValueErrorrrrr&r is_Symbolr!rrsas_expras_independentrKr(r rBrC as_base_exprDis_AddrEriritemsrr"is_multivariaterperrI terms_gcdrrQrYr5is_Ringto_fieldis_QQ_Ir>r rqis_Exactr6nrootsr=r]rris_EXrlKeyErrorrupdatesumvaluesr$r.rr)1r9rZrrmr[r\rer]r^r_rKflagsrarr6r\r{FrcondepfconbasesrTrrprqr&rhrodumgensrf rescale_x translate_xrr;r: roots_funrrrkresrgrl_resulthandlersqueryzeroresult1rns1 ```` @r<riri5s3`9 KE!T 9: : #Jc!fqja /E QJDGQ / q %& 5Q'''Aa&quu%&BCCA AxxzQ1q55199;55qvv>S '3;CE ]]3/H88#$==?DAq || % a1E-F G H "4s(<( XXZ1_+,88:?Iq\ 2VUAq1 2aiik*668JAw7|q QXXZ1_(+6A 156w<1$A!);||~++03"1v~14QR Q/21vs2w1 %*1XF#)3A!3D!PK$0 Q$O!P&5Q%7B ,VUAq AB,:!+<HK( QGH-4>( q!0mQUURV1W!X>A(1=>> AEE!2%mmo *NK()F5$ % *[ '(E)   <V$EL%%' !D;4L !L%%' !DT?4L !LLN %DAq#$GAiK  %LLN )DAq'(GA O $ ) MM% #fmmo&3#J0%   FO .D LL$t , - . G5 5?     t <1F:; ; >> from sympy.abc import x, y >>> from sympy.polys.polyroots import root_factors >>> root_factors(x**2 - y, x) [x - sqrt(y), x + sqrt(y)] rdrrc ||zSrrB)rnros r<rzroot_factors..s AaCr>)rr&rHrrrKrirrrrrErtrRr) r9r]rKargsrrrfrkNr:rGs r< root_factorsrs :D QA 99s EFF q A !F #E #EKKM* =DAq DQN#3A#55q1uQG = qxxz>'1A NN1558 $ a )02AAIIK22 N3s7D)F)U__doc__r functoolsr sympy.corerrrsympy.core.exprtoolsrsympy.core.functionrsympy.core.logicr sympy.core.mulr r sympy.core.intfuncr sympy.core.numbersr rsympy.core.powerrsympy.core.relationalrsympy.core.sortingrsympy.core.symbolrrrsympy.core.sympifyrsympy.functionsrrrrr(sympy.functions.elementary.miscellaneousrr sympy.ntheoryrrrsympy.polys.domainsr sympy.polys.polyerrorsr!r"r#r$sympy.polys.polyquinticconstr%rr&r'r(r)r*sympy.polys.rationaltoolsr+sympy.polys.specialpolysr,sympy.utilitiesr-sympy.utilities.miscr.r/r=r]rrrrrrr.rr<rYrirrBr>r<rs> -(&+#- $&44&99?66"((4NN.4"+ 3K CLCJ5Bpy;x7t*Z@gT>BFR  ggT #'+r>