K iPdZddlmZmZmZmZmZmZmZm Z m Z m Z m Z m Z mZmZmZmZmZmZmZmZmZmZddlmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+ddl,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;ddlZ>ddl?m@Z@ddlAmBZBmCZCmDZDmEZEmFZFdZGd ZHd ZId ZJd ZKd ZLdZMdZNdZOdZPdZQdZRdZSd@dZTdZUdZVdZWdZXdZYdZZdZ[d@dZ\dZ]dZ^d Z_d!Z`d"Zad#Zbd$Zcd%Zdd&Zed'Zfd(Zgd)Zhd*Zid+Zjd,Zkd-Zld.Zmd/Znd0Zod1Zpd2Zqd3Zrd4Zsd5Ztd6Zud7Zvd8Zwd9Zxd:Zyd;Zzd<Z{dAd=Z|dAd>Z}y?)BzEEuclidean algorithms, GCDs, LCMs and polynomial remainder sequences. ) dup_sub_muldup_negdmp_negdmp_adddmp_subdup_muldmp_muldmp_powdup_divdmp_divdup_remdup_quodmp_quodup_premdmp_premdup_mul_grounddmp_mul_ground dmp_mul_termdup_quo_grounddmp_quo_ground dup_max_norm dmp_max_norm) dup_strip dmp_raisedmp_zerodmp_one dmp_ground dmp_one_p dmp_zero_p dmp_zeros dup_degree dmp_degree dmp_degree_indup_LCdmp_LC dmp_ground_LCdmp_multi_deflate dmp_inflate dup_convert dmp_convertdmp_apply_pairs)dup_clear_denomsdmp_clear_denomsdup_diffdmp_diffdup_evaldmp_eval dmp_eval_in dup_truncdmp_ground_trunc dup_monicdmp_ground_monic dup_primitivedmp_ground_primitive dup_extractdmp_ground_extractgf_intgf_crt)query)MultivariatePolynomialErrorHeuristicGCDFailedHomomorphismFailed NotInvertible DomainErrorc|jstd|z|jgg}}|r't|||\}}||}}|t ||||}}|r't |t |||}t||}||fS)ar Half extended Euclidean algorithm in `F[x]`. Returns ``(s, h)`` such that ``h = gcd(f, g)`` and ``s*f = h (mod g)``. Examples ======== >>> from sympy.polys import ring, QQ >>> R, x = ring("x", QQ) >>> f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15 >>> g = x**3 + x**2 - 4*x - 4 >>> R.dup_half_gcdex(f, g) (-1/5*x + 3/5, x + 1) z(Cannot compute half extended GCD over %s)is_FieldrConer rrr$r5)fgKabqrs ]/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/polys/euclidtools.pydup_half_gcdexrO2s& ::DqHII EE7BqA q!Q1!1+aAq)1 q&A,*A!QA a4Kc8|s t|||St||)z Half extended Euclidean algorithm in `F[X]`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x,y = ring("x,y", ZZ) )rOr?rGrHurIs rNdmp_half_gcdexrTUs# aA&&)!Q//rPcbt|||\}}t||||}t|||}|||fS)a Extended Euclidean algorithm in `F[x]`. Returns ``(s, t, h)`` such that ``h = gcd(f, g)`` and ``s*f + t*g = h``. Examples ======== >>> from sympy.polys import ring, QQ >>> R, x = ring("x", QQ) >>> f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15 >>> g = x**3 + x**2 - 4*x - 4 >>> R.dup_gcdex(f, g) (-1/5*x + 3/5, 1/5*x**2 - 6/5*x + 2, x + 1) )rOrr)rGrHrIshFts rN dup_gcdexrZfs?& !Q "DAqAq!QA1aA a7NrPc8|s t|||St||)z Extended Euclidean algorithm in `F[X]`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x,y = ring("x,y", ZZ) )rZr?rRs rN dmp_gcdexr\s# Aq!!)!Q//rPcrt|||\}}||jgk(r t|||Std)at Compute multiplicative inverse of `f` modulo `g` in `F[x]`. Examples ======== >>> from sympy.polys import ring, QQ >>> R, x = ring("x", QQ) >>> f = x**2 - 1 >>> g = 2*x - 1 >>> h = x - 1 >>> R.dup_invert(f, g) -4/3 >>> R.dup_invert(f, h) Traceback (most recent call last): ... NotInvertible: zero divisor z zero divisor)rOrFr rB)rGrHrIrVrWs rN dup_invertr^s=. !Q "DAqQUUG|q!QN++rPc8|s t|||St||)z Compute multiplicative inverse of `f` modulo `g` in `F[X]`. Examples ======== >>> from sympy.polys import ring, QQ >>> R, x = ring("x", QQ) )r^r?rRs rN dmp_invertr`s# !Q"")!Q//rPcv||g}t|||}|r%|j|||}}t|||}|r%|S)an Euclidean polynomial remainder sequence (PRS) in `K[x]`. Examples ======== >>> from sympy.polys import ring, QQ >>> R, x = ring("x", QQ) >>> f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5 >>> g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21 >>> prs = R.dup_euclidean_prs(f, g) >>> prs[0] x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5 >>> prs[1] 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21 >>> prs[2] -5/9*x**4 + 1/9*x**2 - 1/3 >>> prs[3] -117/25*x**2 - 9*x + 441/25 >>> prs[4] 233150/19773*x - 102500/6591 >>> prs[5] -1288744821/543589225 )r append)rGrHrIprsrWs rNdup_euclidean_prsrdsL: a&C1aA  1 !1 Aq!  JrPc8|s t|||St||)z Euclidean polynomial remainder sequence (PRS) in `K[X]`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x,y = ring("x,y", ZZ) )rdr?rRs rNdmp_euclidean_prsrf#  Aq)))!Q//rPc||g}tt||||\}}|r2|j|||}}tt||||\}}|r2|S)a; Primitive polynomial remainder sequence (PRS) in `K[x]`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x = ring("x", ZZ) >>> f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5 >>> g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21 >>> prs = R.dup_primitive_prs(f, g) >>> prs[0] x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5 >>> prs[1] 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21 >>> prs[2] -5*x**4 + x**2 - 3 >>> prs[3] 13*x**2 + 25*x - 49 >>> prs[4] 4663*x - 6150 >>> prs[5] 1 )r7rrb)rGrHrIrc_rWs rNdup_primitive_prsrjsb: a&C !Q*A .DAq  1 !1XaA.21 JrPc8|s t|||St||)z Primitive polynomial remainder sequence (PRS) in `K[X]`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x,y = ring("x,y", ZZ) )rjr?rRs rNdmp_primitive_prsrl#rgrPc^t|}t|}||kr||}}||}}|sggfS|s|g|jgfS||g}||z }|j |dzz}t|||}t|||}t ||} | |z} |j| g} | } |rt|} |j |||| || z f\}}}}| | |zz}t|||}t |||}t ||} |dkDr| |dz z} |j| |z| } n| } | j | |r|| fS)a Subresultant PRS algorithm in `K[x]`. Computes the subresultant polynomial remainder sequence (PRS) and the non-zero scalar subresultants of `f` and `g`. By [1] Thm. 3, these are the constants '-c' (- to optimize computation of sign). The first subdeterminant is set to 1 by convention to match the polynomial and the scalar subdeterminants. If 'deg(f) < deg(g)', the subresultants of '(g,f)' are computed. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x = ring("x", ZZ) >>> R.dup_inner_subresultants(x**2 + 1, x**2 - 1) ([x**2 + 1, x**2 - 1, -2], [1, 1, 4]) References ========== .. [1] W.S. Brown, The Subresultant PRS Algorithm. ACM Transaction of Mathematical Software 4 (1978) 237-249 )r!rFrrr$rbrquo)rGrHrInmRdrKrWlccSkrLs rNdup_inner_subresultantsrx4sp8 1 A1 A1u!1!1 2v sQUUG| AA AA %%1q5AAqAq!QA 1B AA  A A qM  1a!e^ 1a C!Q$J Q1  1a # Aq\ q5AE AsQh"AA ! ' * a4KrPc"t|||dS)z Computes subresultant PRS of two polynomials in `K[x]`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x = ring("x", ZZ) >>> R.dup_subresultants(x**2 + 1, x**2 - 1) [x**2 + 1, x**2 - 1, -2] r)rxrGrHrIs rNdup_subresultantsr{s #1a +A ..rPc|r|s|jgfSt|||\}}t|ddkDr|j|fS|d|fS)z Resultant algorithm in `K[x]` using subresultant PRS. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x = ring("x", ZZ) >>> R.dup_prs_resultant(x**2 + 1, x**2 - 1) (4, [x**2 + 1, x**2 - 1, -2]) r)zerorxr!)rGrHrIrrrvs rNdup_prs_resultantrsU A| "1a +DAq!B%1{ R5!8OrPc@|r t|||St|||dS)z Computes resultant of two polynomials in `K[x]`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x = ring("x", ZZ) >>> R.dup_resultant(x**2 + 1, x**2 - 1) 4 r)r)rGrHrI includePRSs rN dup_resultantrs* Aq)) Q1 %a ((rPc |s t|||St||}t||}||kr||}}||}}t||rggfS|dz }t||r|gt|j|gfS||g}||z }t t|j ||dz||} t ||||} t| | d||} t||} t | |||} t|j|| g} t| ||} t| |st| |}|j| || |||z f\}}}}tt| ||t | |||||} t ||||} | Dcgc]}t|| ||} }t||} |dkDr9t t| |||||}t | |dz ||}t||||} n t| ||} | jt| ||t| |s|| fScc}w)a Subresultant PRS algorithm in `K[X]`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x,y = ring("x,y", ZZ) >>> f = 3*x**2*y - y**3 - 4 >>> g = x**2 + x*y**3 - 9 >>> a = 3*x*y**4 + y**3 - 27*y + 4 >>> b = -3*y**10 - 12*y**7 + y**6 - 54*y**4 + 8*y**3 + 729*y**2 - 216*y + 16 >>> prs = [f, g, a, b] >>> sres = [[1], [1], [3, 0, 0, 0, 0], [-3, 0, 0, -12, 1, 0, -54, 8, 729, -216, 16]] >>> R.dmp_inner_subresultants(f, g) == (prs, sres) True rnr) rxr"rrrFr rrr%rrbr r)rGrHrSrIrprqvrrrsrKrWrtrurvrwchprLs rNdmp_inner_subresultantsrs:. &q!Q//1aA1aA1u!1!1!Q2v  AA!QsZq)*** AA AA AEE61%q1ua3AAq!AQ1a#A 1BAq!A AEE1 q!A1aAA q!   1a!e^ 1a GB1%Aq!Q'A / Q1a -. 0rgb!Q" 0 0 Aq\ q5Aq)1a3A1q5!Q'A1a#AAq!A Aq!"+A. a4K 1s)Hc$t||||dS)a Computes subresultant PRS of two polynomials in `K[X]`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x,y = ring("x,y", ZZ) >>> f = 3*x**2*y - y**3 - 4 >>> g = x**2 + x*y**3 - 9 >>> a = 3*x*y**4 + y**3 - 27*y + 4 >>> b = -3*y**10 - 12*y**7 + y**6 - 54*y**4 + 8*y**3 + 729*y**2 - 216*y + 16 >>> R.dmp_subresultants(f, g) == [f, g, a, b] True r)rrRs rNdmp_subresultantsrs( #1aA .q 11rPc|s t|||St||s t||rt|dz gfSt||||\}}t |d|dkDrt|dz |fS|d|fS)a Resultant algorithm in `K[X]` using subresultant PRS. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x,y = ring("x,y", ZZ) >>> f = 3*x**2*y - y**3 - 4 >>> g = x**2 + x*y**3 - 9 >>> a = 3*x*y**4 + y**3 - 27*y + 4 >>> b = -3*y**10 - 12*y**7 + y**6 - 54*y**4 + 8*y**3 + 729*y**2 - 216*y + 16 >>> res, prs = R.dmp_prs_resultant(f, g) >>> res == b # resultant has n-1 variables False >>> res == b.drop(x) True >>> prs == [f, g, a, b] True rnr}r)rrrrr")rGrHrSrIrrrvs rNdmp_prs_resultantr(s4  Aq))!Q:a+Q$$ "1aA .DAq!B%aQ## R5!8OrPc |stt|||d|z|S|dz }t||}t||}t|d|}t|d|} || z||zz} |jg|j } } t |} t | | krU | |jz } | |k(r tdt|t| |d||}t|||k(r)t|t| |d||}t|||k(rnqt|||||}t| | ||}|st|g}t|g}n|g}|g}|jt| | ||}t| ||}t||d|}t!|t#||||||}t%| |||} t'| |||} t)| |j| g|} t+| ||} t | | krU| S)a Compute resultant of `f` and `g` modulo a prime `p`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x,y = ring("x,y", ZZ) >>> f = x + y + 2 >>> g = 2*x*y + x + 3 >>> R.dmp_zz_modular_resultant(f, g, 5) -2*y**2 + 1 rrnno luck)r<rr"r#rFrr!rAr2dmp_zz_modular_resultantr1rinvertr0rrr rrr4rr3)rGrHrrSrIrrprqNMBDrJrMrXGrrersrus rNrrPs" '1a03a7;; AA1aA1aAaAAaAA !ac A EE7QUUFqA A Q-1  JAAv(33Ava|Q15A!Q1$6!Q<Aq9a#q( %Q1a 3 Q1a 1#A1#AAA HHXaA& * 1a # aAq ! Awq!Q*Aq 1 Aq!Q  Q1a ( Ar{A & aA G Q-1 J HrPc>tt||g||g|||zS)z2Wrapper of CRT for Collins's resultant algorithm. r;)rMrrPrrIs rN _collins_crtrs$ &!Q!Q+QqS 11rPct||}t||}|dks|dkrt|dz St|||}t|||}t|||}t|||} |dz } |d|j |||zz||zz||zz}t| |j |j } } } ddlm}| |kr||| } || zr| | zs||| } || zs| | zst|| ||}t|| ||} t||| ||}|j| r|} nt| |t| | |f| |} | | z} | |kr| S#t$rYwxYw)a Collins's modular resultant algorithm in `Z[X]`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x,y = ring("x,y", ZZ) >>> f = x + y + 2 >>> g = 2*x*y + x + 3 >>> R.dmp_zz_collins_resultant(f, g) -2*y**2 - 5*y + 1 rrn) nextprime)r"rrr& factorialrF sympy.ntheoryrr4rrAis_oner+r)rGrHrSrIrprqArrJrKrrMrrrrXrrrs rNdmp_zz_collins_resultantrs$ 1aA1aA1uAAQ1AQ1AaAAaAA AA !Q[[1q5 ""1a4'1,Aqk155!%%!qA' q& ilOq5!a%)A,Aq5!a% Q1a ( Q1a ( (Aq!Q7A 88A;A1lQ1Iq!DA Q' q&* H"   sE E)(E)ct||}t||}|dks|dkrt|dz S|j}t||||\}}t||||\}}t ||||}t ||||}t ||||} t | |dz ||} |j ||z||zz|} t| | |dz |S)a$ Collins's modular resultant algorithm in `Q[X]`. Examples ======== >>> from sympy.polys import ring, QQ >>> R, x,y = ring("x,y", QQ) >>> f = QQ(1,2)*x + y + QQ(2,3) >>> g = 2*x*y + x + 3 >>> R.dmp_qq_collins_resultant(f, g) -2*y**2 - 7/3*y + 5/6 rrn)r"rget_ringr-r*rconvertr) rGrHrSK0rprqK1cfcgrMrus rNdmp_qq_collins_resultantrs" 1aA1aA1uAA B Q2r *EB Q2r *EBAq"b!AAq"b!A Aq"-AAq1ub"%A 2q52q5="%A !QAr **rPc|st||||S|rt||||S|jr%|jr>t dr3t ||||S|j rt drt||||St||||dS)aH Computes resultant of two polynomials in `K[X]`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x,y = ring("x,y", ZZ) >>> f = 3*x**2*y - y**3 - 4 >>> g = x**2 + x*y**3 - 9 >>> R.dmp_resultant(f, g) -3*y**10 - 12*y**7 + y**6 - 54*y**4 + 8*y**3 + 729*y**2 - 216*y + 16 )rUSE_COLLINS_RESULTANTr)rrrEis_QQr>ris_ZZr)rGrHrSrIrs rN dmp_resultantrs" Q1<< Aq!,,zz 77u45+Aq!Q7 7 77u45+Aq!Q7 7 Q1a ( ++rPct|}|dkr |jSd||dz zdzz}t||}t|t |d||}|j ||||zS)z Computes discriminant of a polynomial in `K[x]`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x = ring("x", ZZ) >>> R.dup_discriminant(x**2 + 2*x + 3) -8 rr}rnr)r!r~r$rr.ro)rGrIrsrVrurMs rNdup_discriminantr#so 1 AAvvv Aq1uI!# $ 1aL !XaA. 2uuQ!A$rPc  |s t||St|||dz }}|dkr t|Sd||dz zdzz}t||}t |t |d||||}t |||||}t||||S)z Computes discriminant of a polynomial in `K[X]`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x,y,z,t = ring("x,y,z,t", ZZ) >>> R.dmp_discriminant(x**2*y + x*z + t) -4*y*t + z**2 rnrr}r)rr"rr%rr/rr)rGrSrIrsrrVrurMs rNdmp_discriminantr>s 1%% a QUqAAv{ Aq1uI!# $ 1aL !XaAq11a 8 1adAq )q!Q""rPc6|s|sgggfS|sF|jt||r|g|jgfSt||g|j gfS|sF|jt||r||jggfSt|||j ggfSy)3Handle trivial cases in GCD algorithm over a ring. N)is_nonnegativer$rFrrzs rN_dup_rr_trivial_gcdr]s 2rz   F1aL )b155'> !1a="vh. .   F1aL )quugr> !1a=AEE6(B. . rPc|s|sgggfS|st||gt||gfS|st||t||ggfSy)4Handle trivial cases in GCD algorithm over a field. N)r5r$rzs rN_dup_ff_trivial_gcdrosR 2rz AVAq\N22 A122rPct||}t||}t|||xs t|||}|r|rttd||S|rb|j t |||r|t |t||fSt|||t |t|j |fS|rb|j t |||r|t||t |fSt|||t|j |t |fS|rt||||fStdrt||||Sy)rUSE_SIMPLIFY_GCDN) rrtupler rr&rrrrrFr>_dmp_simplify_gcd)rGrHrSrIzero_fzero_gif_contain_ones rN_dmp_rr_trivial_gcdr{s2 1 F 1 Fq!Q'=9Q1+=N &Yq!Q'((   M!Q2 3hqk71a=0 01a#Xa[*aeeVQ2GG G   M!Q2 3gamXa[0 01a#Z%:HQKG G q!}a"" ! " Aq!,,rPc Zt||}t||}|r|rttd||S|r.t|||t |t t ||||fS|r.t|||t t ||||t |fStdrt||||Sy)rrrN) rrr r6rrr&r>r)rGrHrSrIrrs rN_dmp_ff_trivial_gcdrs 1 F 1 F &Yq!Q'((  Aq) =Aq1157 7  Aq)=Aq115   ! " Aq!,,rPc t||}t||}|dkDr|dkDry|s|st||}t||}n5|st||}t|||}nt|||}t||}|dz }t||||} |D cgc]} t | | ||} } |D cgc]} t | | ||} } | g| | fScc} wcc} w)z7Try to eliminate `x_0` from GCD computation in `K[X]`. rNrn)r"r% dmp_contentdmp_gcdr)rGrHrSrIdfdgrXrrrWrcffrcfgs rNrrs Aq B Aq B Av"q& " 1aL 1aLq! AAq!$AAq!$Aq! A AA1aA+, .RGB1a .C .+, .RGB1a .C . 3S= / .s C(C cVt|||}||St||\}}t||\}}|j||}t|||d} t| |\} } ||j t | |z}t | ||} t|| |} t|| |} | | | fS)aa Computes polynomial GCD using subresultants over a ring. Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``, ``cff = quo(f, h)``, and ``cfg = quo(g, h)``. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x = ring("x", ZZ) >>> R.dup_rr_prs_gcd(x**2 - 1, x**2 - 3*x + 2) (x - 1, x + 1, x - 2) r})rr7gcdr{canonical_unitr$rr) rGrHrIresultfcrXgcrrurWrirrs rNdup_rr_prs_gcdrs"!Aq )F  !Q EB !Q EB b" A!Q"2&A A DAq  &A, ''Aq!QA !Q C !Q C c3;rPct|||}||St|||d}t||}t|||}t|||}|||fS)ab Computes polynomial GCD using subresultants over a field. Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``, ``cff = quo(f, h)``, and ``cfg = quo(g, h)``. Examples ======== >>> from sympy.polys import ring, QQ >>> R, x = ring("x", QQ) >>> R.dup_ff_prs_gcd(x**2 - 1, x**2 - 3*x + 2) (x - 1, x + 1, x - 2) r})rr{r5r)rGrHrIrrWrrs rNdup_ff_prs_gcdrsc"!Aq )F  !Q"2&A!QA !Q C !Q C c3;rPc|s t|||St||||}||St|||\}}t|||\}}t||||d} t |||dz |\} } } t| ||\} } t | | d||} |j t| ||} | |jk7rt| | ||} t|| ||} t|| ||}| | |fS)a Computes polynomial GCD using subresultants over a ring. Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``, ``cff = quo(f, h)``, and ``cfg = quo(g, h)``. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x,y, = ring("x,y", ZZ) >>> f = x**2 + 2*x*y + y**2 >>> g = x**2 + x*y >>> R.dmp_rr_prs_gcd(f, g) (x + y, x + y, x) r}rnr) rr dmp_primitiverdmp_rr_prs_gcdrrr&rFrr)rGrHrSrIrrrXrrrWruriunitrrs rNrr s( aA&& Aq! ,F  !Q "EB !Q "EB!Q1%b)ARQUA.GAq! Aq !DAqQ1a#A  M!Q2 3D quu} 1dAq ) !Q1 C !Q1 C c3;rPcj|s t|||St||||}||St|||\}}t|||\}}t||||d} t |||dz |\} } } t| ||\} } t | | d||} t | ||} t|| ||} t|| ||} | | | fS)a Computes polynomial GCD using subresultants over a field. Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``, ``cff = quo(f, h)``, and ``cfg = quo(g, h)``. Examples ======== >>> from sympy.polys import ring, QQ >>> R, x,y, = ring("x,y", QQ) >>> f = QQ(1,2)*x**2 + x*y + QQ(1,2)*y**2 >>> g = x**2 + x*y >>> R.dmp_ff_prs_gcd(f, g) (x + y, 1/2*x + 1/2*y, x) r}rnr)rrrrdmp_ff_prs_gcdrr6r)rGrHrSrIrrrXrrrWrurirrs rNrr=s( aA&& Aq! ,F  !Q "EB !Q "EB!Q1%b)ARQUA.GAq! Aq !DAqQ1a#AAq!A !Q1 C !Q1 C c3;rPclg}|r/||z}||dzkDr||z}|jd|||z |z}|r/|S)-Interpolate polynomial GCD from integer GCD. rr)insert)rWxrIrGrHs rN_dup_zz_gcd_interpolaterksM A E qAv: FA A UqL  HrPct|||}||St|}t|}t|||\}}}|dk(s|dk(r|g||fSt||}t||}|dt ||zdz} t t | d|j | zdt |tt||z|tt||zzdz} tdtD]D} t|| |} t|| |} | r| r|j| | }| |z}| |z}t|| |}t||d}t|||\}}|s&t|||\}}|st!|||}|||fcSt|| |}t|||\}}|s&t|||\}}|st!|||}|||fcSt|| |}t|||\}}|s&t|||\}}|st!|||}|||fcSd| z|j |j | zdz} Gt#d ) a  Heuristic polynomial GCD in `Z[x]`. Given univariate polynomials `f` and `g` in `Z[x]`, returns their GCD and cofactors, i.e. polynomials ``h``, ``cff`` and ``cfg`` such that:: h = gcd(f, g), cff = quo(f, h) and cfg = quo(g, h) The algorithm is purely heuristic which means it may fail to compute the GCD. This will be signaled by raising an exception. In this case you will need to switch to another GCD method. The algorithm computes the polynomial GCD by evaluating polynomials f and g at certain points and computing (fast) integer GCD of those evaluations. The polynomial GCD is recovered from the integer image by interpolation. The final step is to verify if the result is the correct GCD. This gives cofactors as a side effect. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x = ring("x", ZZ) >>> R.dup_zz_heu_gcd(x**2 - 1, x**2 - 3*x + 2) (x - 1, x + 1, x - 2) References ========== .. [1] [Liao95]_ rrcrnB ir)rr!r9rminmaxsqrtabsr$range HEU_GCD_MAXr0rrr7r rr@)rGrHrIrrrrf_normg_normrriffggrWrrcff_rMcfg_s rNdup_zz_heu_gcdr{sF!Aq )F  AB ABAq!$ICA Qw"'ua{ !Q F !Q F !C  " $%A C2affQi< c&Cq! --Cq! --/ /12 3 4A1k ",1 aA  aA  "b" A'C'C'1a0Aa#A&AaA&GD!!!Q*a&q#q1AdD=()#q!4C1c1%DAq!!Q*a&q#q1Ac4<')#q!4C1c1%DAq!!Q*a&q#q1AdC<' !GaffQVVAY' '5 0Y,1\ Y ''rPcg}t||sIt||||}|jd|t||||}t ||||}t||sI|j t ||dz|rt||dz|S|S)rrrn)rr4rrr is_negativer&r)rWrrrIrGrHs rN_dmp_zz_gcd_interpolaters AA Q1a ( A Aq!Q  1aA & A }}]1a!eQ/0q!a%##rPc|s t|||St||||}||St||||\}}}t|||}t|||}|dt ||zdz}t t |d|j |zdt |tt|||z|tt|||zzdz} tdtD]} t|| ||} t|| ||} |dz } t| | sFt| | s9t| | | |\}}}t|| | |}t|||d}t!||||\}}t||r2t!||||\}}t||rt#||||}|||fcSt|| | |}t!||||\}}t||r2t!||||\}}t||rt#||||}|||fcSt|| | |}t!||||\}}t||r2t!||||\}}t||rt#||||}|||fcSd| z|j |j | zdz} t%d ) a Heuristic polynomial GCD in `Z[X]`. Given univariate polynomials `f` and `g` in `Z[X]`, returns their GCD and cofactors, i.e. polynomials ``h``, ``cff`` and ``cfg`` such that:: h = gcd(f, g), cff = quo(f, h) and cfg = quo(g, h) The algorithm is purely heuristic which means it may fail to compute the GCD. This will be signaled by raising an exception. In this case you will need to switch to another GCD method. The algorithm computes the polynomial GCD by evaluating polynomials f and g at certain points and computing (fast) integer GCD of those evaluations. The polynomial GCD is recovered from the integer image by interpolation. The evaluation process reduces f and g variable by variable into a large integer. The final step is to verify if the interpolated polynomial is the correct GCD. This gives cofactors of the input polynomials as a side effect. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x,y, = ring("x,y", ZZ) >>> f = x**2 + 2*x*y + y**2 >>> g = x**2 + x*y >>> R.dmp_zz_heu_gcd(f, g) (x + y, x + y, x) References ========== .. [1] [Liao95]_ rrrrrrnrrr)rrr:rrrrrr&rrr1rdmp_zz_heu_gcdrr8r rr@)rGrHrSrIrrrrrrrrrrrWrrrrMrs rNrrsP aA&& Aq! ,F  "1aA.ICA !Q "F !Q "F !C  " $%A C2affQi< c&C aA 677C aA 6779 9;< = >A1k "+1 aAq ! aAq ! E2q!ZA%6(RA6KAsC'1a3A$Q1-a0AaAq)GD!!Q!!Q1-aa#&q#q!4AdD=()#q!Q7C1c1a(DAq!Q!!Q1-aa#&q#q!4Ac4<')#q!Q7C1c1a(DAq!Q!!Q1-aa#&q#q!4AdC<' !GaffQVVAY' '5 0W+1Z Y ''rPct|||}||S|j}t|||\}}t|||\}}t|||}t|||}t |||\}}} t|||}t ||} t ||}t|||}t| ||} t||j| ||}t| |j| ||} ||| fS)a Heuristic polynomial GCD in `Q[x]`. Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``, ``cff = quo(f, h)``, and ``cfg = quo(g, h)``. Examples ======== >>> from sympy.polys import ring, QQ >>> R, x = ring("x", QQ) >>> f = QQ(1,2)*x**2 + QQ(7,4)*x + QQ(3,2) >>> g = QQ(1,2)*x**2 + x >>> R.dup_qq_heu_gcd(f, g) (x + 2, 1/2*x + 3/4, 1/2*x) ) rrr,r)rr$r5rro) rGrHrrrrrrWrrrus rNdup_qq_heu_gcdras(!Ar *F  B QB 'EB QB 'EBAr2AAr2A Ar*KAsCAr2Aq" A!RA c2r "C c2r "C bffQmR 0C bffQmR 0C c3;rPct||||}||S|j}t||||\}}t||||\}}t||||}t||||}t ||||\}} } t||||}t |||} t |||}t| |||} t| |||} t| |j| |||} t| |j| |||} || | fS)a Heuristic polynomial GCD in `Q[X]`. Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``, ``cff = quo(f, h)``, and ``cfg = quo(g, h)``. Examples ======== >>> from sympy.polys import ring, QQ >>> R, x,y, = ring("x,y", QQ) >>> f = QQ(1,4)*x**2 + x*y + y**2 >>> g = QQ(1,2)*x**2 + x*y >>> R.dmp_qq_heu_gcd(f, g) (x + 2*y, 1/4*x + 1/2*y, 1/2*x) ) rrr-r*rr&r6rro) rGrHrSrrrrrrWrrrus rNdmp_qq_heu_gcdrs(!Aq" -F  B Q2r *EB Q2r *EBAq"b!AAq"b!A Aq"-KAsCAq"b!AaBAAr"A c1b" %C c1b" %C bffQmQ 3C bffQmQ 3C c3;rPc\|js |jrh |j}t |||}t |||}t |||\}}}t |||}t |||}t |||}|||fS|jr2|jrtdr t|||St|||S|jrtdr t|||St|||S#t$r|jg||fcYSwxYw#t$rYiwxYw#t$rYFwxYw)ag Computes polynomial GCD and cofactors of `f` and `g` in `K[x]`. Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``, ``cff = quo(f, h)``, and ``cfg = quo(g, h)``. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x = ring("x", ZZ) >>> R.dup_inner_gcd(x**2 - 1, x**2 - 3*x + 2) (x - 1, x + 1, x - 2) USE_HEU_GCD)is_RRis_CC get_exactrCrFr) dup_inner_gcdrErr>rr@rrrr)rGrHrIexactrWrrs rNrrs?: ww!'' !KKME 1e $ 1e $#Aq%0 3 5! $#ua(#ua(#s{  77u]+ %aA..aA&& 77u]+ %aA..aA&&9 !EE7Aq=  !"&  &  s5C1% D D1D  D  DD D+*D+cV|jsn |j}t ||||}t ||||}t ||||\}}}t ||||}t ||||}t ||||}|||fS|j r4|jrtdr t||||St||||S|jrtdr t||||St||||S#t$rt||||fcYSwxYw#t$rYkwxYw#t$rYFwxYw)z'Helper function for `dmp_inner_gcd()`. r)is_ExactrrCrr*_dmp_inner_gcdrErr>rr@rrrr)rGrHrSrIrrWrrs rNrrsL :: 'KKME 1a ' 1a '$Q1e4 3 1eQ '#q%+#q%+#s{  77u]+ %aAq11aAq)) 77u]+ %aAq11aAq))9 '1a=!Q& & '"&  &  s5C/ D  D/D  D  DD D('D(c|s t|||St||f||\}\}}t||||\}}}t||||t||||t||||fS)a Computes polynomial GCD and cofactors of `f` and `g` in `K[X]`. Returns ``(h, cff, cfg)`` such that ``a = gcd(f, g)``, ``cff = quo(f, h)``, and ``cfg = quo(g, h)``. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x,y, = ring("x,y", ZZ) >>> f = x**2 + 2*x*y + y**2 >>> g = x**2 + x*y >>> R.dmp_inner_gcd(f, g) (x + y, x + y, x) )rr'rr()rGrHrSrIJrWrrs rN dmp_inner_gcdr &sz( Q1%%!1a&!Q/IAv1 Aq!,KAsC 1a # Q1 % Q1 % ''rPc"t|||dS)z Computes polynomial GCD of `f` and `g` in `K[x]`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x = ring("x", ZZ) >>> R.dup_gcd(x**2 - 1, x**2 - 3*x + 2) x - 1 r)rrzs rNdup_gcdr Es Aq !! $$rPc$t||||dS)z Computes polynomial GCD of `f` and `g` in `K[X]`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x,y, = ring("x,y", ZZ) >>> f = x**2 + 2*x*y + y**2 >>> g = x**2 + x*y >>> R.dmp_gcd(f, g) x + y r)r rRs rNrrVs" Aq! $Q ''rPc|r|s tdSt||\}}t||\}}|j||}tt |||t ||||}|j t||}t|||z|S)z Computes polynomial LCM over a ring in `K[x]`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x = ring("x", ZZ) >>> R.dup_rr_lcm(x**2 - 1, x**2 - 3*x + 2) x**3 - 2*x**2 - x + 2 r) rr7lcmrrr rr$r)rGrHrIrrrurWrSs rN dup_rr_lcmrjs A{ !Q EB !Q EB b" A1a 1a ! %A 1&A !QqS! $$rPc`tt|||t||||}t||S)a Computes polynomial LCM over a field in `K[x]`. Examples ======== >>> from sympy.polys import ring, QQ >>> R, x = ring("x", QQ) >>> f = QQ(1,2)*x**2 + QQ(7,4)*x + QQ(3,2) >>> g = QQ(1,2)*x**2 + x >>> R.dup_ff_lcm(f, g) x**3 + 7/2*x**2 + 3*x )rrr r5)rGrHrIrWs rN dup_ff_lcmrs5" 1a 1a ! %A Q?rPcN|jr t|||St|||S)z Computes polynomial LCM of `f` and `g` in `K[x]`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x = ring("x", ZZ) >>> R.dup_lcm(x**2 - 1, x**2 - 3*x + 2) x**3 - 2*x**2 - x + 2 )rErrrzs rNdup_lcmrs) zz!Q""!Q""rPc t|||\}}t|||\}}|j||}tt||||t ||||||}t ||||S)a Computes polynomial LCM over a ring in `K[X]`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x,y, = ring("x,y", ZZ) >>> f = x**2 + 2*x*y + y**2 >>> g = x**2 + x*y >>> R.dmp_rr_lcm(f, g) x**3 + 2*x**2*y + x*y**2 )r8rrr rr)rGrHrSrIrrrurWs rN dmp_rr_lcmrss" !Aq )EB Aq )EB b" A1a#1a#Q +A !Q1 %%rPc htt||||t||||||}t|||S)a" Computes polynomial LCM over a field in `K[X]`. Examples ======== >>> from sympy.polys import ring, QQ >>> R, x,y, = ring("x,y", QQ) >>> f = QQ(1,4)*x**2 + x*y + y**2 >>> g = QQ(1,2)*x**2 + x*y >>> R.dmp_ff_lcm(f, g) x**3 + 4*x**2*y + 4*x*y**2 )rr rr6)rGrHrSrIrWs rN dmp_ff_lcmrs>" 1a#1a#Q +A Aq! $$rPcp|s t|||S|jrt||||St||||S)a Computes polynomial LCM of `f` and `g` in `K[X]`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x,y, = ring("x,y", ZZ) >>> f = x**2 + 2*x*y + y**2 >>> g = x**2 + x*y >>> R.dmp_lcm(f, g) x**3 + 2*x**2*y + x*y**2 )rrErrrRs rNdmp_lcmrs?" q!Qzz!Q1%%!Q1%%rPct|||dz }}t||r|S|ddD]}t||||}t|||sn|j t |||r t |||S|S)z Returns GCD of multivariate coefficients. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x,y, = ring("x,y", ZZ) >>> R.dmp_content(2*x*y + 6*x + 4*y + 12) 2*y + 6 rnN)r%rrrrr&rrGrSrIcontrrus rNrrsQlAE!D!Q qrUtQ1% T1a    }}]4A./tQ"" rPc t||||dz }}t||s t|||r||fS||Dcgc]}t||||c}fScc}w)z Returns multivariate content and a primitive polynomial. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x,y, = ring("x,y", ZZ) >>> R.dmp_primitive(2*x*y + 6*x + 4*y + 12) (2*y + 6, x + 2) rn)rrrrrs rNrrs\!Q"AE!D!Q9T1a0Qwq:!wq$1-::::sAc"t||d||S)z Cancel common factors in a rational function `f/g`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x = ring("x", ZZ) >>> R.dup_cancel(2*x**2 - 2, x**2 - 2*x + 1) (2*x + 2, x - 1) r)include) dmp_cancel)rGrHrIrs rN dup_cancelr!5s aAq' 22rPcd}|jrE|jr9||j}}t||||d\}}t||||d\}}n|j|j}}t ||||\}} } |4|j ||\}}}t| |||} t| |||} |}|jt| ||} |jt| ||} | r| rt| ||t| ||} } n%| r| t| ||} }n| r| t| ||} }|s||| | fSt| |||} t| |||} | | fS)z Cancel common factors in a rational function `f/g`. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x,y = ring("x,y", ZZ) >>> R.dmp_cancel(2*x**2 - 2, x**2 - 2*x + 1) (2*x + 2, x - 1) NT)r) rEhas_assoc_Ringrr-rFr  cofactorsr*rr&rr) rGrHrSrIrrcqcprirrLp_negq_negs rNr r Fsp Bzza&&1::sympy.polys.polyerrorsr?r@rArBrCrOrTrZr\r^r`rdrfrjrlrxr{rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr r rrrrrrrrrr!r rPrNr1sK            %%%%%( F0"60",>0"%P0"%P0"M`/"2)&M`2.%PF R2 : z$+N,B 6#>$ 4*8%P>-`)V   g(T"h(V.b.b<'~!*H'>%"((%<.#(&8%.&4>;,3"2rP