K il&dZddlmZddlmZddlmZddlmZm Z m Z ddl m Z ddl mZmZmZmZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZmZddlm Z m!Z!m"Z"ddl#m$Z$ddl%m&Z&ddl'm(Z(m)Z)m*Z*ddl+m,Z,ddl-m.Z.m/Z/ddl0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:ddl;mm?Z?ddl@mAZAddlBmCZCddlDmEZEddlFmGZGmHZHmIZIe)ddfdZJdZKdZLd ZMd1d"ZNd#ZOd$ZPd2d%ZQd&ZRd'ZSd(ZTd)ZUd*ZVd+ZWd,ZXd-ZYeHd3d.ZZd/Z[eHd3d0Z\y!)4z*Minimal polynomials for algebraic numbers.)reduce)Add)Factors) expand_mulexpand_multinomial_mexpand)Mul)IRationalpi_illegal)S)Dummy)sympify)preorder_traversal)exp)sqrtcbrt)cossintan)divisors)subsets)ZZQQ FractionField)dup_chebyshevt) NotAlgebraicGeneratorsError) PolyPurePolyinvert factor_listgroebner resultantdegreepoly_from_exprparallel_poly_from_exprlcm)dict_from_exprexpr_from_dict)rs_compose_add)ring)CRootOf)cyclotomic_poly)numbered_symbolspublicsiftc Ft|dtr|Dcgc]}|d }}t|dk(r|dSd}i}t|dr |jng} ||kr#|Dcgc]#}|j j ||i%} }|jr| Dcgc]}|j|} }tt|t| dD]} t| | D] \} } | || < t| D cgc]0\} }t|j|j|| f2}} }td|Drut!|}|dd \\}}\}}||d zkDs||cS|d z}||kr#t#d |zcc}wcc}wcc}wcc}} w) ze Return a factor having root ``v`` It is assumed that one of the factors has root ``v``. r symbolsT)k repetitionc32K|]\}}|tvywN)r ).0i_s f/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/polys/numberfields/minpoly.py z!_choose_factor..Hs8TQ1=8sNi@Bz4multiple candidates for the minimal polynomial of %s) isinstancetuplelenhasattrr8as_exprxreplace is_numbernrrangezip enumerateabssubsanysortedNotImplementedError)factorsxvdomprecboundfprec1pointsr8ferJsr> candidatescanaixbr?s r@_choose_factorrc(s '!*e$!()A1Q4)) 7|qqz E F$S)4ckk"G 4-4; ;aaiik""Aa5) ; ; ;;%'(!##d)(B(uW$G #AGQ 1q   %R=*Aaqvvf~//67;*J* 8Z88 $C!"1gOGQVa1u9}r{"' #*  ; 4-> TWXX YYM*<(*s F!(FF?5FcLtdtj|DS)Nc3K|]y}tj|D]_}|jxsM|jxr?|jjxr'd|j zj xr |ja{yw)rBN)r make_args is_Rationalis_Powbaser is_Integeris_extended_real)r=trYs r@rAz _is_sum_surds..Ys= 3==+;=&'}}K!K !K !!%%33!K898J8JK=K=sA?B)allrrf)ps r@ _is_sum_surdsroXs% =q!= ==cd}g}|jD]}|js||r%|jtj|dzf<|j r"|j|tjfj|j r8|jjr"|j|tjftt|j|d\}}|jt|t|dzf|jd|ddtjur|S|Dcgc]\}}| }}}tt|D] }||dk7s nd d lm} | |d \} } } g} g}|D]T\}}|| vr&| j||tj"zz0|j||tj"zzVt%| }t%|}t'|dzt'|dzz }|Scc}}w) a? helper function for ``_minimal_polynomial_sq`` It selects a rational ``g`` such that the polynomial ``p`` consists of a sum of terms whose surds squared have gcd equal to ``g`` and a sum of terms with surds squared prime with ``g``; then it takes the field norm to eliminate ``sqrt(g)`` See simplify.simplify.split_surds and polytools.sqf_norm. Examples ======== >>> from sympy import sqrt >>> from sympy.abc import x >>> from sympy.polys.numberfields.minpoly import _separate_sq >>> p= -x + sqrt(2) + sqrt(3) + sqrt(7) >>> p = _separate_sq(p); p -x**2 + 2*sqrt(3)*x + 2*sqrt(7)*x - 2*sqrt(21) - 8 >>> p = _separate_sq(p); p -x**4 + 4*sqrt(7)*x**3 - 32*x**2 + 8*sqrt(7)*x + 20 >>> p = _separate_sq(p); p -x**8 + 48*x**6 - 536*x**4 + 1728*x**2 - 400 cV|jxr|jtjuSr<)rhrrHalf)exprs r@is_sqrtz_separate_sq..is_sqrtxs{{1txx16611rprBT)binaryc |dS)Nr6)zs r@z_separate_sq..s 1rp)keyr6r) _split_gcdN)argsis_MulappendrOneis_Atomrhr is_integerrRr2r sortrKrEsympy.simplify.radsimpr}rsrr)rnrur`yTFrysurdsr>r}gb1b2a1a2p1p2s r@ _separate_sqr^s42 A VV ,xxqz!%%A'!QUU$aee..!QUU$))5DAq HHc1gsAwz* + ,FF~FuQx155 41aQ E  3u:  8q= 2E!"I&IAr2 B B#1 7 IIa166 k " IIa166 k " # bB bBQ(2q5/)A H! s7 Hct|}t|}|jr|dkDr t|sy|td|z}||z} t |}||ur|j |||zi}n|})|dk(rIt |}|j||j|zdkr| }|jd}|St|d}t|||}|S)a Returns the minimal polynomial for the ``nth-root`` of a sum of surds or ``None`` if it fails. Parameters ========== p : sum of surds n : positive integer x : variable of the returned polynomial Examples ======== >>> from sympy.polys.numberfields.minpoly import _minimal_polynomial_sq >>> from sympy import sqrt >>> from sympy.abc import x >>> q = 1 + sqrt(2) + sqrt(3) >>> _minimal_polynomial_sq(q, 3, x) x**12 - 4*x**9 - 4*x**6 + 16*x**3 - 8 rNr6) rrjror rrOr coeffr& primitiver#rc)rnrJrTpnrrSresults r@_minimal_polynomial_sqrs.  A A <>> from sympy import sqrt, Add, Mul, QQ >>> from sympy.polys.numberfields.minpoly import _minpoly_op_algebraic_element >>> from sympy.abc import x, y >>> p1 = sqrt(sqrt(2) + 1) >>> p2 = sqrt(sqrt(2) - 1) >>> _minpoly_op_algebraic_element(Mul, p1, p2, x, QQ) x - 1 >>> q1 = sqrt(y) >>> q2 = 1 / y >>> _minpoly_op_algebraic_element(Add, q1, q2, x, QQ.frac_field(y)) x**2*y**2 - 2*x*y - y**3 + 1 References ========== .. [1] https://en.wikipedia.org/wiki/Resultant .. [2] I.M. Isaacs, Proc. Amer. Math. Soc. 25 (1970), 638 "Degrees of sums in a separable field extension". Xrzoption not availablegensr6domain)rstr_minpoly_composerOrrr-r*r(composerGr _mulyrRr%r,r+ as_expr_dictr&r r#rc)opex1ex2rTrVmp1mp2rRrrrr?rmp1adeg1deg2rSress r@_minpoly_op_algebraic_elementrsH c!f A {sAs+ {sAs+hh1v Sy "9R=DAq>#&q)*B>#&q)*B13A,1EKHRa 2A99;D sS!Q!"899 SyC2I dCq!f - 2r " 1>>+Q / #q>D #q>D SyTQY$!) Q#AJAw !RS\3 7C ;;=rpct||d}t|}|jDcgc]\\}}||||z zz}}}t|Scc}}w)z@ Returns ``expand_mul(x**degree(p, x)*p.subs(x, 1/x))`` rr'r&termsr)rnrTrrJr>cr`s r@_invertxr$sW 1 a Br A')xxz2GDQ!QQZ2A2 7N 3sAct||d}t|}|jDcgc]\\}}|||zz|||z zz}}}t|Scc}}w)z8 Returns ``_mexpand(y**deg*p.subs({x:x / y}))`` rr)rnrTrrrJr>rr`s r@rr/s` 1 a Br A.0hhj974AQTAAJ 9A9 7N :sAct|}|s t|||}|jstd|z|dkr.||k(rt d|zt ||}|dk(r|S| }d|z }t t|}|j||i}|j\}}tt|||z||zz |g||}|j\} } t| |||z|}|jS)a Returns ``minpoly(ex**pw, x)`` Parameters ========== ex : algebraic element pw : rational number x : indeterminate of the polynomial dom: ground domain mp : minimal polynomial of ``p`` Examples ======== >>> from sympy import sqrt, QQ, Rational >>> from sympy.polys.numberfields.minpoly import _minpoly_pow, minpoly >>> from sympy.abc import x, y >>> p = sqrt(1 + sqrt(2)) >>> _minpoly_pow(p, 2, x, QQ) x**2 - 2*x - 1 >>> minpoly(p**2, x) x**2 - 2*x - 1 >>> _minpoly_pow(y, Rational(1, 3), x, QQ.frac_field(y)) x**3 - y >>> minpoly(y**Rational(1, 3), x) x**3 - y +%s does not seem to be an algebraic elementrz %s is zeror|r6rr)rr is_rationalrZeroDivisionErrorrrrrOas_numer_denomr r%r#rcrG) expwrTrVmprrJdrr?rSs r@ _minpoly_powr:s< B b!S ) >>H2MNN Av 7#L2$56 6 b!_ 8IS rT c!f A !QB   DAq yQTAqD[s3Qs CC"JAw !RVS 1C ;;=rpc tt|d|d||}|d|dz}|ddD]}tt|||||}||z}|S)z. returns ``minpoly(Add(*a), dom, x)`` rr6rBNr)rrrTrVr`rrnpxs r@ _minpoly_addroh 'sAaD!A$3 ?B !qt Ae *32q#2 F F Irpc tt|d|d||}|d|dz}|ddD]}tt|||||}||z}|S)z. returns ``minpoly(Mul(*a), dom, x)`` rr6rBNr)rr rs r@ _minpoly_mulr{rrpc|jdj\}}|turB|jr5|j}t |}|j r>t|t}tt|Dcgc]}|||z dz z||zc}S|jdk(r"|dk(rd|dzzd|dzzz d|d zzzd z S|d zdk(r\t|t}t|dzDcgc]}|||z z||z}}t|}t|\}} t| ||} | Sdtd |ztzz d z tj z} t#| |t$} | St'd |zcc}wcc}w) zu Returns the minimal polynomial of ``sin(ex)`` see https://mathworld.wolfram.com/TrigonometryAngles.html rr6 @`$rBr)r~ as_coeff_Mulr rqris_primerrrrKrnr#rcrrrsrrr) rrTrr`rJrr>rr?rSrrts r@ _minpoly_sinrs 771: " " $DAqBw ==A Azz#1b)%(CQQQ^AaD0CDDssax6ad7R1W,r!Q$w6::1uz#1b).3AEl;QQZ!_;;G(^ 7$Wa4 QqSV_a'!&&0D"4B/CJ DrI JJ)Drr?rSrs r@ _minpoly_cosrsr 771: " " $DAqBw ==ssax33!8QT6AadF?QqS014433!8QT6AaC$?@@ACCAq"%A*/A,7QQUAaD7A7Q2)#A$QJAw !R0CJ DrI JJ 8s+Fc|jdj\}}|tur|jr|dz}t |j }|j dzdk(r|nd}g}t|j dzdz|dzdD]7}|j|||zz|||z dz z||z z |dz|dzzz}9t|}t|\}} t| ||} | Std|z)zk Returns the minimal polynomial of ``tan(ex)`` see https://github.com/sympy/sympy/issues/21430 rrBr6r) r~rr rrrrnrKrrr#rcr) rrTrr`rJrr9rr?rSrs r@ _minpoly_tanrs 771: " " $DAqBw ==AAACCASS1W\qAEACCE19ac1- 8 Qq!tV$1Qi1o&AaC!A#;7 8U A$QJAw !R0CJ DrI JJrpc|jdj\}}|ttzk(r|jrt |j }|jdk(s|jdk(r|dk(r |dz|z dzS|dk(r|dzdzS|dk(r|dz|dzz dzS|dk(r|dzdzS|d k(r|dz|dzz dzS|d k(r|dz|dzz |dzz|dzz dzS|jrd}t|D] }|| |zz } |Std|zDcgc]}t||}}t|||}|Std |ztd |zcc}w) z7 Returns the minimal polynomial of ``exp(ex)`` rr6r|rrBrrrrr7r)r~rr r rrrrnrrKrr/rcr) rrTrr`rr]r>rSrs r@ _minpoly_exprs 771: " " $DAqAbDy == Assax133"96a4!8a<'6a4!8O6a4!Q$;?*6a4!8O6a4!Q$;?*7a4!Q$;A-14q88::A"1X%qb1W %H7?qsmDq!,DGDB/BILrQR R DrI JJ EsEc|j}|j|jjd|i}t ||\}}t |||}|S)zA Returns the minimal polynomial of a ``CRootOf`` object. r)rtrOpolyrr#rc)rrTrnr?rSrs r@_minpoly_rootofrsO A  Q"#AQ"JAw GQ +F Mrpc  |jr|j|z|jz S|tur6t |dzdz||\}}t |dk(r|dzdzS|tz S|t jurQt |dz|z dz ||\}}t |dk(r |dz|z dz St||dtdzdz |S|t jurt |dz|dzz |z dz ||\}}t |dk(r|dz|dzz |z dz Sdtddtdzz ztddtdzzzdz }t||||St|d r||jvr||z S|jr>t|r3|}||z} t!|}||urtt |d||S|}+|j"rt%||g|j&}|S|j(rt+|j,} t/| j1d } | d rO|t2k(rEt5| d | d zD cgc] \} }| |z c}} }t7| d } | j9D cgc]} | j}} t;t<|d}t j>}| jA|t jB}| j1D cgc]$\}} || j|z| jzz&}}} t5|}tE||}|j||zz|j|||zzzz }||z|tGd|zz}tIt4||||||}|StK||g|j&}|S|jLr$tO|jP|jR||}|S|jTtVurtY||}|S|jTtZurt]||}|S|jTt^urta||}|S|jTtRurtc||}|S|jTtdurtg||}|Stid|zcc}} wcc} wcc} }w)a Computes the minimal polynomial of an algebraic element using operations on minimal polynomials Examples ======== >>> from sympy import minimal_polynomial, sqrt, Rational >>> from sympy.abc import x, y >>> minimal_polynomial(sqrt(2) + 3*Rational(1, 3), x, compose=True) x**2 - 2*x - 1 >>> minimal_polynomial(sqrt(y) + 1/y, x, compose=True) x**2*y**2 - 2*x*y - y**3 + 1 rBr6rr4)rVr!r8cB|djxr|djS)Nrr6)rg)itxs r@rzz"_minpoly_compose..JsA(:(:(Qs1v?Q?QrpTFN)rrr)5rgrrnr r#rEr GoldenRatiorcrTribonacciConstantrrFr8is_QQroris_Addrr~rrrSr2itemsrr dictvaluesrr) NegativeOnepopZerominimal_polynomialr rrrhrrir __class__rrrrrrrr.rr)rrTrVr?rSfacrUrrrYrbxr1rdenslcmdensneg1expn1rinumsrrrs r@rrss  ~~ttAv} Qw A1S9 7w<1,q!tax7!a%7 Q]] AAq= 7 w<1 a4!8a< !'1q47{Ao3G G Q ! !! A1q1!4aD 7 w<1 a4!Q$;?Q& &tB48O,,tB48O/DDIC!'1cs; ;sI2#42v  yy]2&  ar"Cby%k"oa&8!Q??  yy1c,BGG,L JK  BK   Q R T7sbyQuX$-?@62rB@ACagB!#-AACC-D-S$*G==DFF4(E>@hhjI74D133w;!##-.IDIt*C$S!,C %%7 "SUU4%-+@%@@C+Xa%9 99C/S#q#3TWXC" Jq#00C J 277BFFAs3 J  2q! J  2q! J  2q! J  2q! J  b!$ JH2MNNAA-JsQ< R0)Rc t|}|jr t|d}t|D]}|jsd}n|t|t }}nt dt}}|s6|jr$ttt|j}nt}t|dr||jvrtd|d||rtt|||}|j!d}|j#|t%||z}|j&r t)| }|r |||d S|j+|S|j,s t/d t1|||}|r |||d S|j+|S) a- Computes the minimal polynomial of an algebraic element. Parameters ========== ex : Expr Element or expression whose minimal polynomial is to be calculated. x : Symbol, optional Independent variable of the minimal polynomial compose : boolean, optional (default=True) Method to use for computing minimal polynomial. If ``compose=True`` (default) then ``_minpoly_compose`` is used, if ``compose=False`` then groebner bases are used. polys : boolean, optional (default=False) If ``True`` returns a ``Poly`` object else an ``Expr`` object. domain : Domain, optional Ground domain Notes ===== By default ``compose=True``, the minimal polynomial of the subexpressions of ``ex`` are computed, then the arithmetic operations on them are performed using the resultant and factorization. If ``compose=False``, a bottom-up algorithm is used with ``groebner``. The default algorithm stalls less frequently. If no ground domain is given, it will be generated automatically from the expression. Examples ======== >>> from sympy import minimal_polynomial, sqrt, solve, QQ >>> from sympy.abc import x, y >>> minimal_polynomial(sqrt(2), x) x**2 - 2 >>> minimal_polynomial(sqrt(2), x, domain=QQ.algebraic_field(sqrt(2))) x - sqrt(2) >>> minimal_polynomial(sqrt(2) + sqrt(3), x) x**4 - 10*x**2 + 1 >>> minimal_polynomial(solve(x**3 + x + 3)[0], x) x**3 + x + 3 >>> minimal_polynomial(sqrt(y), x) x**2 - y T) recursiveFrTr8z the variable z$ is an element of the ground domain r6)fieldz!groebner method only works for QQ)rrIrris_AlgebraicNumberr rr! free_symbolsrrlistrFr8rrrrr& is_negativercollectrrR_minpoly_groebner) rrTrpolysrrtclsrrs r@rrpsin B || bD )"2&  " "G   }T3sX3  ??"2tBOO'<=FFvy!a6>>&9-.89 9!"a0!!#A& LLF61-- . ==(F-2s61D)Iq8II <<!"EFF r1c *F).3vq %EFNN14EErpc tdtiicd fd  fd d}d}t|}|jr|j j S|j r|jz|jz }n||}|r|dz}d}|jr@d |jz jr'd |jz }t|j|}n&t|rt|tj }||}|o |}|z gt#j%z} t'| t#j%gzd } t)| d\} } t+| |}|r9t-}|j/t1|zd kr t3| }S)a/ Computes the minimal polynomial of an algebraic number using Groebner bases Examples ======== >>> from sympy import minimal_polynomial, sqrt, Rational >>> from sympy.abc import x >>> minimal_polynomial(sqrt(2) + 3*Rational(1, 3), x, compose=False) x**2 - 2*x - 1 r`)rNcpt}||<| ||z|z|<|S|j||<|Sr<)nextrG)rrrir` generatormappingr8s r@update_mappingz)_minpoly_groebner..update_mappingsM O  S&4-GBK&#++a.GBKrpc|jr4|tjur| vr  |ddS |S|jr|S|jr&t |j Dcgc] }| c}S|jr&t|j Dcgc] }| c}S|jrh|jjr}|jdkrt|j }t |j}|j |jj!}|jdk(r|S||j z}|jj"sR|j|jj$zj!t'd|jj(}}n|j|j}}|}||z}| vr*|j"r|j!S |d|z | S |S|j*r | vr ||j-S |St/d|zcc}wcc}w)a Transform a given algebraic expression *ex* into a multivariate polynomial, by introducing fresh variables with defining equations. Explanation =========== The critical elements of the algebraic expression *ex* are root extractions, instances of :py:class:`~.AlgebraicNumber`, and negative powers. When we encounter a root extraction or an :py:class:`~.AlgebraicNumber` we replace this expression with a fresh variable ``a_i``, and record the defining polynomial for ``a_i``. For example, if ``a_0**(1/3)`` occurs, we will replace it with ``a_1``, and record the new defining polynomial ``a_1**3 - a_0``. When we encounter a negative power we transform it into a positive power by algebraically inverting the base. This means computing the minimal polynomial in ``x`` for the base, inverting ``x`` modulo this poly (which generates a new polynomial) and then substituting the original base expression for ``x`` in this last polynomial. We return the transformed expression, and we record the defining equations for new symbols using the ``update_mapping()`` function. rBr6rr|z*%s does not seem to be an algebraic number)rr ImaginaryUnitrgrrr~rr rhrrrir"rGrOexpandrjrnr rrminpoly_of_elementr)rr minpoly_baseinversebase_invrirrtbottom_up_scanrrr8rrTs r@rz)_minpoly_groebner..bottom_up_scans8 ::Q__$W$)"a33"2;& YYRWW>.+>? ? YYRWW>.+>? ? YYvv!!66A:#4RWWa#EL$Q 5==?G&||Arww7>>@Hvv|-h77%0vv(()668Xa5JD!##D%d+Syw&~~#{{},-dAGdUCC"4=(  " " %b"*?*?*ABBr{"G"LMMG?>s !IIcd|jr?d|jz jr&|jdkr|jjry|j rYd}|j D]E}|jry|js|jjs5|jdkDsEy|ryy)z Returns True if it is more likely that the minimal polynomial algorithm works better with the inverse r6rTF)rhrrrirrr~)rhitrns r@simpler_inversez*_minpoly_groebner..simpler_inverse4s 99"&&$$!77>> 99CWW %88 88vv}}$  %rpFr|r6lex)orderrr<)r0rrrr rGrgrrnrhrrjrrirorrrrr$r#rcrrr&r)rrTrrinvertedrrrJbusrGr?rSrrrr8rs `` @@@@@r@rrs!%0I2GW HNHNT,H B B $$&..q11 a"$$"2& RB 99!BFF(.."&&A(!Q7C 2 (QUUA6C ?F ; $CS D!122AD!12aS8FA$QrU+JAw#GQ3F&!$ <<6&!,, - 1(F Mrpc"t|||||S)z6This is a synonym for :py:func:`~.minimal_polynomial`.)rTrrr)r)rrTrrrs r@minpolyros bAweF SSrp)NNr<)NTFN)]__doc__ functoolsrsympy.core.addrsympy.core.exprtoolsrsympy.core.functionrrrsympy.core.mulr sympy.core.numbersr r r r sympy.core.singletonrsympy.core.symbolrsympy.core.sympifyrsympy.core.traversalr&sympy.functions.elementary.exponentialr(sympy.functions.elementary.miscellaneousrr(sympy.functions.elementary.trigonometricrrrsympy.ntheory.factor_rsympy.utilities.iterablesrsympy.polys.domainsrrrsympy.polys.orthopolysrsympy.polys.polyerrorsrrsympy.polys.polytoolsr r!r"r#r$r%r&r'r(r)sympy.polys.polyutilsr*r+sympy.polys.ring_seriesr,sympy.polys.ringsr-sympy.polys.rootoftoolsr.sympy.polys.specialpolysr/sympy.utilitiesr0r1r2rcrorrrrrrrrrrrrrrrrrrxrpr@r5s0(HH::"#&36?BB*-551A2"+4 ')s!-Z`= ? B4lL^2j  #KLK>K0!KHZzYFYFx_DTTrp