K idZddlmZddlmZddlmZmZmZm Z m Z m Z m Z m Z mZmZmZddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZm 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-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;mZ>m?Z?m@Z@mAZAmBZBmCZCmDZDddlEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTddlUmVZVmWZWmXZXdd lYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z`dd lambZbdd lcmdZddd lemfZfmgZgmhZhmiZidd ljmkZkddllmmZnmoZpmqZredk(rddlsmtZtndZtdZudZvdZwdZxdZydZzdZ{dZ|dZ}d7dZ~dZdZdZdZd Zd!Zd"Zd#Zd$Zd%Zd&Zd8d'Zd(Zd)Zd*Zd+Zd,Zd-Zd.Zd/Zd0Zd1Zd2Zd3Zd4Zd5Zd6Zy)9z:Polynomial factorization routines in characteristic zero. ) GROUND_TYPES)_randint) gf_from_int_polygf_to_int_poly gf_lshift gf_add_mulgf_mulgf_divgf_remgf_gcdexgf_sqf_p gf_factor_sqf gf_factor)dup_LCdmp_LC dmp_ground_LCdup_TC dup_convert dmp_convert dup_degree dmp_degree dmp_degree_indmp_degree_list dmp_from_dict dmp_zero_pdmp_onedmp_nest dmp_raise dup_strip dmp_ground dup_inflate dmp_exclude dmp_include dmp_inject dmp_eject dup_terms_gcd dmp_terms_gcd)dup_negdmp_negdup_adddmp_adddup_subdmp_subdup_muldmp_muldup_sqrdmp_powdup_divdmp_divdup_quodmp_quo dmp_expand dmp_add_mul dup_sub_mul dmp_sub_mul dup_lshift dup_max_norm dmp_max_norm dup_l1_normdup_mul_grounddmp_mul_grounddup_quo_grounddmp_quo_ground)dup_clear_denomsdmp_clear_denoms dup_truncdmp_ground_trunc dup_content dup_monicdmp_ground_monic dup_primitivedmp_ground_primitive dmp_eval_tail dmp_eval_indmp_diff_eval_in dup_shift dmp_shift dup_mirror) dmp_primitive dup_inner_gcd dmp_inner_gcd) dup_sqf_p dup_sqf_norm dmp_sqf_norm dup_sqf_part dmp_sqf_part_dup_check_degrees_dmp_check_degrees) _sort_factors)query)ExtraneousFactors DomainErrorCoercionFailedEvaluationFailed)subsets)ceilloglog2flint) fmpz_polyNcg}|D]D}d} t|||\}}|s||dz}}nn|dk(r td|j||fFt|S)z Determine multiplicities of factors for a univariate polynomial using trial division. An error will be raised if any factor does not divide ``f``. rtrial division failed)r2 RuntimeErrorappendr[)ffactorsKresultfactorkqrs ]/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/polys/factortools.pydup_trial_divisionruXs|F# 1fa(DAq!a%1  667 7 vqk"#   cg}|D]O}d} t||||\}}t||r||dz}}nn'|dk(r td|j||fQt |S)z Determine multiplicities of factors for a multivariate polynomial using trial division. An error will be raised if any factor does not divide ``f``. rrhri)r3rrjrkr[) rlrmurnrorprqrrrss rtdmp_trial_divisionrytsF# 1fa+DAq!Q!a%1  667 7 vqk"#   rvcdddlm}t|}t|dz }t|dz }|j t d|D}||dz |}||dz |dz }|j t||} ||z|| zz} | t||z } t| dz dz} | S)a The Knuth-Cohen variant of Mignotte bound for univariate polynomials in ``K[x]``. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x = ring("x", ZZ) >>> f = x**3 + 14*x**2 + 56*x + 64 >>> R.dup_zz_mignotte_bound(f) 152 By checking ``factor(f)`` we can see that max coeff is 8 Also consider a case that ``f`` is irreducible for example ``f = 2*x**2 + 3*x + 4``. To avoid a bug for these cases, we return the bound plus the max coefficient of ``f`` >>> f = 2*x**2 + 3*x + 4 >>> R.dup_zz_mignotte_bound(f) 6 Lastly, to see the difference between the new and the old Mignotte bound consider the irreducible polynomial: >>> f = 87*x**7 + 4*x**6 + 80*x**5 + 17*x**4 + 9*x**3 + 12*x**2 + 49*x + 26 >>> R.dup_zz_mignotte_bound(f) 744 The new Mignotte bound is 744 whereas the old one (SymPy 1.5.1) is 1937664. References ========== ..[1] [Abbott13]_ r)binomialc3&K|] }|dz yw)r|N.0cfs rt z(dup_zz_mignotte_bound..s0rRU0rh) (sympy.functions.combinatorial.factorialsr{r_ceilsqrtsumabsrr;) rlrnr{ddeltadelta2 eucl_normt1t2lcbounds rtdup_zz_mignotte_boundrsRB1 A !a%LE 519 F0Q002I %!)V $B %!)VaZ (B va| B NR"W $E \!Q E %!) q E Lrvct|||}tt|||}tt ||}|j ||dzd|zz|z|zS)z7Mignotte bound for multivariate polynomials in `K[X]`. rhr|)r<rrrrr)rlrxrnabns rtdmp_zz_mignotte_boundrs^Q1A M!Q "#A OAq !"A 66!AE( AqD  "1 $$rvc|dz}t||||}t|||}tt|||||\} } t| ||} t| ||} t t|||t| |||} tt || |||} tt || |||} t t|| |t|| ||} tt | |j g|||}tt|||| |\}}t|||}t|||}t t|||t|| ||} tt |||||}tt || |||}| | ||fS)a  One step in Hensel lifting in `Z[x]`. Given positive integer `m` and `Z[x]` polynomials `f`, `g`, `h`, `s` and `t` such that:: f = g*h (mod m) s*g + t*h = 1 (mod m) lc(f) is not a zero divisor (mod m) lc(h) = 1 deg(f) = deg(g) + deg(h) deg(s) < deg(h) deg(t) < deg(g) returns polynomials `G`, `H`, `S` and `T`, such that:: f = G*H (mod m**2) S*G + T*H = 1 (mod m**2) References ========== .. [1] [Gathen99]_ r|)r8rDr2r.r*r,one)mrlghstrnMerrrsrxGHrcrSTs rtdup_zz_hensel_steprs8 1AAq!QA!QA 71a#Q *DAq!QA!QA1a '!Q"2A6A'!Q"Aq)A'!Q"Aq)A1a '!Q"2A6A'!aeeWa(!Q/A 71a#Q *DAq!QA!QA1a '!Q"2A6A'!Q"Aq)A'!Q"Aq)A aA:rvc t|}t||}|dk(r4t||j|||zd|}t |||z|gS|}|dz} t t t|} t|g|} |d| D]} t| t| |||} t|| |} || dzdD]} t| t| |||} t| | ||\}}}t| |} t| |} t||}t||}td| dzD]}t||| | ||||dzc\} } }}} t|| |d| ||t|| || d||zS)a Multifactor Hensel lifting in `Z[x]`. Given a prime `p`, polynomial `f` over `Z[x]` such that `lc(f)` is a unit modulo `p`, monic pair-wise coprime polynomials `f_i` over `Z[x]` satisfying:: f = lc(f) f_1 ... f_r (mod p) and a positive integer `l`, returns a list of monic polynomials `F_1,\ F_2,\ \dots,\ F_r` satisfying:: f = lc(f) F_1 ... F_r (mod p**l) F_i = f_i (mod p), i = 1..r References ========== .. [1] [Gathen99]_ rhrr|N)lenrr>gcdexrDintr_log2rr r rrangerdup_zz_hensel_lift)prlf_listlrnrsrFrrqrrf_irrr_s rtrrs. F A 1BAv 1aggb!Q$/2A 61adA&(( A QA E%(OA"q!Abqz6 1&sA.1 56 A&Aa!ef~6 1&sA.1 56q!Q"GAq!q!Aq!Aq!Aq!A 1a!e_H,Q1aAqA1a4 Aq!aH aF2AJ1 5 Q6!":q! 4 55rvc2||dzkDr||z }|sy||zdk(S)Nr|Trr~)fcrrpls rt_test_plrGs*27{ F  6Q;rvc t|}|dk(r|gSddlm}|d}t||}t ||}t t |j||dzd|zz|z|z}t |dzd|zz|d|zdz zz}t tdt|z} t d| zt| z} g} td| dzD]} || r|| zdk(r|j| } t|| } t| | |s?t| | |d}| j!| |ft#|dkst#| dkDsnt%| d \}}t ttd|zdz|}|Dcgc]}t'||}}t)|||||}tt#|}t+|}gd}}||z}d|zt#|krrt-||D]J}|dk(r'd}|D] }|||dz}||z}t/|||sG0|g}|D]}t1||||}t3|||}t5||d}|d}|r ||zdk7rv|g}t+|}||z }|dk(r'|g}|D]}t1||||}t3|||}|D]}t1||||}t3|||}t7|} t7||}!| |!z|ks|}|Dcgc] }||vs| }}t5||d}t5||d}|j!|t ||}n|dz }d|zt#|krr||gzScc}wcc}w) z4Factor primitive square-free polynomials in `Z[x]`. rhr)isprimer|ct|dS)Nrh)r)xs rtz#dup_zz_zassenhaus..ps3qt9rv)key)r sympy.ntheoryrr;rrrrrr_logrconvertrr rrkrminrrsetrarr.rDrIr=)"rlrnrrrArBCgammarrpxrfsqfxrfsqfrffmodularrsorted_TrrmrrrrrirrT_SG_normH_norms" rtdup_zz_zassenhausrNs1 AAvs % 2BQAq! A CqQx A%a') *+A QUacN1qsQw< '(A aaj! "E %U # $E AAuqy! r{a"fk  YYr] Q #2q! aQ'* "e u:?c!fqj  !,-GAt E$qsQw" #$A/34~b!$4G41a!Q/ASV}H H AQQG AB A#Q-1%4 A Av#A!A$r( A#FAr*C,A1Q4+A,aQ'!!Q'*bEa1AAAa%CAvC,A1Q4+A,aQ' (AqtQ' (!R#A A&F A&Ff}!'/>!1A:A>>!!Q'*!!Q'*q!1aLe4 h FAk A#Q-n aS=A5h?sM+? M0 M0ct||}t||}t|dd|}|r=ddlm}|t |}|j D]}||zs ||dzzsyyy)z2Test irreducibility using Eisenstein's criterion. rhNr factorintr|T)rrrFrrrkeys)rlrnrtce_fcre_ffrs rtdup_zz_irreducible_prsl 1B 1B qua D +T# AQR!Q$Y   rvc|jr! ||j}}t|||}n |jsyt ||}t ||}|dk7s |dk7r|dk7ry|s't||\}}||jk7s||dfgk7ryt|}gg} } t|ddD]} | jd|| t|dz ddD]} | jd|| tt| |} tt| |} t| t| d||} |j!t | |r t#| |} | |k(ryt%||} |j!t | |r t#| |} | | k(r t'| |ryt)| |} t| || k(r t'| |ryy#t$rYywxYw)ad Efficiently test if ``f`` is a cyclotomic polynomial. Examples ======== >>> from sympy.polys import ring, ZZ >>> R, x = ring("x", ZZ) >>> f = x**16 + x**14 - x**10 + x**8 - x**6 + x**2 + 1 >>> R.dup_cyclotomic_p(f) False >>> g = x**16 + x**14 - x**10 - x**8 - x**6 + x**2 + 1 >>> R.dup_cyclotomic_p(g) True References ========== Bradford, Russell J., and James H. Davenport. "Effective tests for cyclotomic polynomials." In International Symposium on Symbolic and Algebraic Computation, pp. 244-251. Springer, Berlin, Heidelberg, 1988. FrhrrT)is_QQget_ringrr_is_ZZrrdup_factor_listrrrinsertr0rr,r: is_negativer(rPdup_cyclotomic_prW)rlrn irreducibleK0rrcoeffrmrrrrrrs rtrrs4 ww qzz|BAr1%AWW 1B 1B Qw28a (A.w AEE>W!Q01 A rqA 1b"  AaD1q5"b ! AaD  ! a A ! a A:aA&*A}}VAq\" AqMAv1aA}}VAq\" AqMAv"1a(QAq!}.q!4 e  sG GGcddlm}|j|j g}||jD]0\}}t t |||||}t |||dz z|}2|S)z1Efficiently generate n-th cyclotomic polynomial. rrrh)rrritemsr4r!)rrnrrrrqs rtdup_zz_cyclotomic_polyrsl' A! ""$*1 K1a(!Q / 1q1u:q )* Hrvc zddlm}|j|j gg}||jD]w\}}|Dcgc]}t t |||||}}|j |td|D]-}|D cgc]} t | ||}} |j |/y|Scc}wcc} w)Nrrrh)rrrrr4r!extendr) rrnrrrrqrQrrrs rt_dup_cyclotomic_decomposer&s' %%!%%A! ""$1;< >agk!Q*Aq1 > >  q! A0131+aA&3A3 HHQK   H ?4s B3B8c@t||t||}}t|dkry|dk7s|dvrytd|ddDryt|}t ||}|j |s|Sg}t d|z|D]}||vs|j ||S)a Efficiently factor polynomials `x**n - 1` and `x**n + 1` in `Z[x]`. Given a univariate polynomial `f` in `Z[x]` returns a list of factors of `f`, provided that `f` is in the form `x**n - 1` or `x**n + 1` for `n >= 1`. Otherwise returns None. Factorization is performed using cyclotomic decomposition of `f`, which makes this method much faster that any other direct factorization approach (e.g. Zassenhaus's). References ========== .. [1] [Weisstein09]_ rNrh)rrhc32K|]}t|yw)N)boolrs rtrz+dup_zz_cyclotomic_factor..Ps &48 &srr|)rrranyris_onerk)rlrnlc_ftc_frrrrs rtdup_zz_cyclotomic_factorr6s$1va|$D!} qyD' &a"g &&1 A!!Q'A 88D> *1Q32 Az  rvc<t||\}}t|}t||dkr| t||}}|dkr|gfS|dk(r||gfSt drt ||r||gfSd}t dr t ||}| t||}|t|dfS)z:Factor square-free (non-primitive) polynomials in `Z[x]`. rrhUSE_IRREDUCIBLE_IN_FACTORNUSE_CYCLOTOMIC_FACTORF)multiple) rIrrr(r\rrrr[)rlrncontrrrms rtdup_zz_factor_sqfrbsAq!GD!1 A a|a%AaAvRx aaSy () 1 %!9 G $%*1a0#Aq) w7 77rvcLtdk(r[t|ddd}|j\}}|Dcgc]\}}|jddd|f}}}|t |fSt ||\}}t |}t||dkr| t||}}|dkr|gfS|dk(r||dfgfStdrt||r||dfgfSt||}d} tdr t||} | t||} t|| |}t||||fScc}}w)a Factor (non square-free) polynomials in `Z[x]`. Given a univariate polynomial `f` in `Z[x]` computes its complete factorization `f_1, ..., f_n` into irreducibles over integers:: f = content(f) f_1**k_1 ... f_n**k_n The factorization is computed by reducing the input polynomial into a primitive square-free polynomial and factoring it using Zassenhaus algorithm. Trial division is used to recover the multiplicities of factors. The result is returned as a tuple consisting of:: (content(f), [(f_1, k_1), ..., (f_n, k_n)) Examples ======== Consider the polynomial `f = 2*x**4 - 2`:: >>> from sympy.polys import ring, ZZ >>> R, x = ring("x", ZZ) >>> R.dup_zz_factor(2*x**4 - 2) (2, [(x - 1, 1), (x + 1, 1), (x**2 + 1, 1)]) In result we got the following factorization:: f = 2 (x - 1) (x + 1) (x**2 + 1) Note that this is a complete factorization over integers, however over Gaussian integers we can factor the last term. By default, polynomials `x**n - 1` and `x**n + 1` are factored using cyclotomic decomposition to speedup computations. To disable this behaviour set cyclotomic=False. References ========== .. [1] [Gathen99]_ reNrrrhrr)rrfrpcoeffsr[rIrrr(r\rrWrrrurY) rlrnf_flintrrmfacexprrrs rt dup_zz_factorrsL\wAddG$( g=DEcCJJL2&,EE]7+++Aq!GD!1 A a|a%AaAvRx aq!fX~ () 1 %1a&> !QA A $% $Q *y a # Aq)Gq'" =AFs"D c||zg}|D]d}t|}t|D]8}|dk7r|j||}||z}|dk7r|j|s7y|j |f|ddS)z,Wang/EEZ: Compute a set of valid divisors. rhN)rreversedgcdrrk)Ecsctrnrorrrss rtdmp_zz_wang_non_divisorsr s"uYF   F&! Aq&EE!QKFq&xx{    a  !":rvc tt||||dz |s tdt||||}t||s tdt ||\}}|j t ||r| t||}}|dz } |D cgc]\} } t| || |} } } t| |||} | ||| fStdcc} } w)z2Wang/EEZ: Test evaluation points for suitability. rhzno luck) rKrr`rTrIrrr(r )rlrr rrxrnrrrvrrrDs rtdmp_zz_wang_test_pointsrs 1q!a% 3y))aAq!A Q?y)) A DAq}}VAq\"r71a=1 AA0131-1a #3A3 Ar1-A}!Qwy)) 4sC c gdgt|z|dz } } }|D]} t| |} t| ||z} tt t|D]M}d||||c}}\}}| |zs| |z|dz}} | |zs|dk7s.t | t ||| || |dc} | |<O|j| t| stgg}}t||D]\} } t| || |} t| |}|j|r|| z}n.|j|| }| |z||z}} t| | ||| z}} t| || |} |j| |j| |j|r|||fSgg}}t||D]?\} } |jt| || ||jt| |d|At||t|dz z||}|||fS)z0Wang/EEZ: Compute correct leading coefficients. rrh)rrrrrr/r1rkallr]ziprKrrr>r?)rlrrrrrrxrnrJr rrrrrqrrrCCHHrccrCCCHHHs rtdmp_zz_wang_lead_coeffsrs+1#c!f*a!e!qA   AqM 1aLO%A-( CAadAaDLAq&1a1u!tQU11uAv!!WQ1a%8!Q?1Q4 C    q6 BAq 1 !Q1 % Aq\ 88B<QBb! AqD"a%rA"1a+RUrA 1b!Q ' !  !   xx|"by2CB 01 >!RA./ >!RA./0 q"s1vz*Aq1A c3;rvc &t|dk(r|\}}t||}t||}t||||\}} } t|||}t| ||} t ||||\} }t | | |||} t ||}t | |} || g} | S|dg} t|ddD]"}| jdt|| d|$gdgg}} t|| D]@\}}t||g|dgd|d|\} }|j| | j|Bg| |dgz} } t| |D]S\}}t||}t||}tt||||||}t ||}| j|U| S)z2Wang/EEZ: Solve univariate Diophantine equations. r|rrhr)rrr rr rrrrr.rdmp_zz_diophantinerkr )rrrrnrrrlrrrrrrrorrrss rtdup_zz_diophantiner7s 1v{1 Q " Q "1aA&1a aA  aA aAq!1 q!Q1 % 1a  1a Q2 M/rUG!Ab'" -A HHQ1Q4+ , -QC511I DAq%q!faeRAq!DDAq HHQK HHQK  QrUG 1I DAq A&A A&AyAq)1a3Aq!$A MM!   Mrvc |s|Dcgc]}g}}t|} t|D]a\} } | s t|| | z ||} tt|| D]0\} \}}t || |}t t ||||||| <2c|St|} t|||}|d|dd}}gg}}|D]=}|jt|||||jt||| ||?t||| ||}|dz }t|||||||}|Dcgc]}t|d||}}t||D]\}}t|||||}t||||}t!|j"| g| |}t%| |}t'd|D]}t)||rnt+||||}t-||dz|| ||}t)||r@t/||j1||dz||}t|||||||} t| D]"\} }t+t|d|||||| | <$tt|| D]\} \}}t3|||||| <t| |D]\}}t|||||}t||||} |Dcgc]}t||||}}|Scc}wcc}wcc}w)z4Wang/EEZ: Solve multivariate Diophantine equations. rNrhr)r enumeraterrr>rDr*rr6rkr5rLrrr9rErrrrrr/rMrA factorialr+)rrrrrrxrnrrrrrrjrrrrrrrlrr rrrrqs rtrrgs<  Qb   qM!!  9HAu"1a!eQ2A&s1ay1 9 6Aq"1eQ/ Aq!11a8! 9  9v Hc F q!Q uaf121 1A HHWQ1a( ) HH[Aq!Q/ 0 1 1aA & E q!Q1a 3-. 0i1a# 0 01I +DAqAq!Q*A + Q1a ( aeeaR[!Q ' AqMq! 1A!Q1a#A AE1aA6Aa#"1akk!A$(&;QB&q!Q1a;%aLCDAq"9Q1a#8!QBAaDC"+3q!9!5/IAv1"1aA.AaD/ 1I3DAq#Aq!Q2A3%Q1a0) 1,56 7qq!Q* 7 7 H} 8 1@ 8s KK)K c &|gt||dz } }}t|}tt|ddD]>\} } t |d| || z || z |} |j dt | || | z |@tt||dd} ttd|dz||D]\}} } t||dz }}|d|dz ||dz d}}tt||D]@\} \}}t t||| |||dz |}|gt|ddd|dz |z|| <Bt|j| g||}t||}t!| t#|||||}t%| ||}td|D]}t'||rt)||||}t+||dz| |||}t'||dz rBt-||j/||dz|dz |}t1|||| ||dz |}tt||D]7\} \}}t3|t|d|dz ||||}t |||||| <9t!| t#|||||}t ||||}t#||||k7rt4|S)z-Wang/EEZ: Parallel Hensel lifting algorithm. rhNrr|)rlistrrrLrrEmaxrrrrKrrrrr-r6rrr/rMrArrr7r])rlrLCrrrxrnrrr rrrrrrwIrrrrrrdjrqrrrs rtdmp_zz_wang_hensel_liftingr'sc3q61q5!qA QA(1QR5/*61 !aQAq 1 $Q1q5!456 OAq !!" %&AuQA1- 11aAwA1!a%y!AEF)1#C2J/ 8JAw2!-Aq!" a_2, ..., x_n -> a_n where `a_i`, for `i = 2, \dots, n`, are carefully chosen integers. The mapping is used to transform `f` into a univariate polynomial in `Z[x_1]`, which can be factored efficiently using Zassenhaus algorithm. The last step is to lift univariate factors to obtain true multivariate factors. For this purpose a parallel Hensel lifting procedure is used. The parameter ``seed`` is passed to _randint and can be used to seed randint (when an integer) or (for testing purposes) can be a sequence of numbers. References ========== .. [1] [Wang78]_ .. [2] [Geddes92]_ r) nextprimerhNr|EEZ_NUMBER_OF_CONFIGSEEZ_NUMBER_OF_TRIESEEZ_MODULUS_STEP)NrrEEZ_RESTART_IF_NEEDEDz3we need to restart algorithm with better parameters)rr)r dmp_zz_factorrrrzerorrrr`r\rtupleaddrkr;rr'r] dmp_zz_wangrJrrr)) rlrxrnmodseedr)randintr rrrhistoryconfigsrrsrrrrreez_num_configs eez_num_tries eez_mod_steprrs_norms_argr_s_normorig_fr#rmros rtr2r2s<(tnG &A,Aq 1EBaA&A )A,A { 6CC UB D8GWa  *1aQ1=Aq A&1 F 63Jr1a#$34O/0M+,L g, (}%" A16q;A!GSD#&';A;Qxw& E!H% 21aQ1EAq%Q*DAqQB}7Av%' Avs NNAr1a+ ,7|.A" D < CG g, (J"FE1  1aAq!$   F Q U^NAr1a FG*1aQ1aC1b,Q2q!QBF #Aq!,1 ==q!Q/ 01a A a  Mc    <$  \ G ( )vq!S1W5 5#EG G GsB85J.JJJ 'J, JJ J)(J),%K Kc|s t||St||r|jgfSt|||\}}t |||dkr| t |||}}t dt||Dr|gfSt|||\}}g}t||dkDr(t|||}t|||}t||||}t||dz |dD]\}}|jd|g|ft||||t!|fS)a Factor (non square-free) polynomials in `Z[X]`. Given a multivariate polynomial `f` in `Z[x]` computes its complete factorization `f_1, \dots, f_n` into irreducibles over integers:: f = content(f) f_1**k_1 ... f_n**k_n The factorization is computed by reducing the input polynomial into a primitive square-free polynomial and factoring it using Enhanced Extended Zassenhaus (EEZ) algorithm. Trial division is used to recover the multiplicities of factors. The result is returned as a tuple consisting of:: (content(f), [(f_1, k_1), ..., (f_n, k_n)) Consider polynomial `f = 2*(x**2 - y**2)`:: >>> from sympy.polys import ring, ZZ >>> R, x,y = ring("x,y", ZZ) >>> R.dmp_zz_factor(2*x**2 - 2*y**2) (2, [(x - y, 1), (x + y, 1)]) In result we got the following factorization:: f = 2 (x - y) (x + y) References ========== .. [1] [Gathen99]_ rc3&K|] }|dk ywrNr~rrs rtrz dmp_zz_factor.. 1a16 1rrh)rrr/rJrr)rrrQrrXr2ryr.rrZr[) rlrxrnrrrrmrrqs rtr.r.ms+H Q""!Qvvrz"1a+GD!Q1!%Aq)a 1?1a0 11Rx Aq !DAqG!Q! Aq ! 1a $Q1a0aQ*1-$1qA3(#$q!W% w' ''rvc |j}t|||}t||\}}|Dcgc]\}}t||||f}}}|j||}||fScc}}w)z>Factor univariate polynomials into irreducibles in `QQ_I[x]`. )as_AlgebraicFieldrrr)rlrK1rrmrrs rtdup_qq_i_factorrHsu   BAr2A$Q+NE7;BCa CR(!,CGC JJub !E '>DsA$c<|j}t|||}t||\}}g}|D]R\}}t||\}} t| ||} t | d|\} } || |zz||zz}|j | |fT|}|j ||}||fS)z>Factor univariate polynomials into irreducibles in `ZZ_I[x]`. r) get_fieldrrHrBrJrkr) rlrrGrrm new_factorsrr fac_denomfac_num fac_num_ZZ_Icontentfac_prims rtdup_zz_i_factorrQs BAr2A$Q+NE7K*Q-c26 7"7B3 0q"EA%)q.8Ha=)*G JJub !E '>rvc |j}t||||}t|||\}}|Dcgc]\}}t|||||f}}}|j||}||fScc}}w)z@Factor multivariate polynomials into irreducibles in `QQ_I[X]`. )rFrdmp_factor_listr)rlrxrrGrrmrrs rtdmp_qq_i_factorrTs{   BAq"b!A$Q2.NE7>EFFC CB+Q/FGF JJub !E '>GsA'cD|j}t||||}t|||\}}g}|D]T\}}t|||\} } t| |||} t | ||\} } || |zz| |zz}|j | |fV|}|j ||}||fS)z@Factor multivariate polynomials into irreducibles in `ZZ_I[X]`. )rJrrTrCrJrkr)rlrxrrGrrmrKrrrLrMrNrOrPs rtdmp_zz_i_factorrVs BAq"b!A$Q2.NE7K*Q-c1b9 7"7Ar26 0q"EA%)q.8Ha=)*G JJub !E '>rvct|t||}}t||}|dkr|gfS|dk(r||dfgfSt|||}}t ||\}}}t ||j }t|dk(r|||t|zfgfS||jz} t|D]B\} \} } t| |j |} t| ||\} } }t| | |} | || <Dt|||}t||||fS)aN Factor univariate polynomials over algebraic number fields. The domain `K` must be an algebraic number field `k(a)` (see :ref:`QQ(a)`). Examples ======== First define the algebraic number field `K = \mathbb{Q}(\sqrt{2})`: >>> from sympy import QQ, sqrt >>> from sympy.polys.factortools import dup_ext_factor >>> K = QQ.algebraic_field(sqrt(2)) We can now factorise the polynomial `x^2 - 2` over `K`: >>> p = [K(1), K(0), K(-2)] # x^2 - 2 >>> p1 = [K(1), -K.unit] # x - sqrt(2) >>> p2 = [K(1), +K.unit] # x + sqrt(2) >>> dup_ext_factor(p, K) == (K.one, [(p1, 1), (p2, 1)]) True Usually this would be done at a higher level: >>> from sympy import factor >>> from sympy.abc import x >>> factor(x**2 - 2, extension=sqrt(2)) (x - sqrt(2))*(x + sqrt(2)) Explanation =========== Uses Trager's algorithm. In particular this function is algorithm ``alg_factor`` from [Trager76]_. If `f` is a polynomial in `k(a)[x]` then its norm `g(x)` is a polynomial in `k[x]`. If `g(x)` is square-free and has irreducible factors `g_1(x)`, `g_2(x)`, `\cdots` then the irreducible factors of `f` in `k(a)[x]` are given by `f_i(x) = \gcd(f(x), g_i(x))` where the GCD is computed in `k(a)[x]`. The first step in Trager's algorithm is to find an integer shift `s` so that `f(x-sa)` has square-free norm. Then the norm is factorized in `k[x]` and the GCD of (shifted) `f` with each factor gives the shifted factors of `f`. At the end the shift is undone to recover the unshifted factors of `f` in `k(a)[x]`. The algorithm reduces the problem of factorization in `k(a)[x]` to factorization in `k[x]` with the main additional steps being to compute the norm (a resultant calculation in `k[x,y]`) and some polynomial GCDs in `k(a)[x]`. In practice in SymPy the base field `k` will be the rationals :ref:`QQ` and this function factorizes a polynomial with coefficients in an algebraic number field like `\mathbb{Q}(\sqrt{2})`. See Also ======== dmp_ext_factor: Analogous function for multivariate polynomials over ``k(a)``. dup_sqf_norm: Subroutine ``sqfr_norm`` also from [Trager76]_. sympy.polys.polytools.factor: The high-level function that ultimately uses this function as needed. rrh)rrrGrWrUdup_factor_list_includedomrunitrrrRrNrurY)rlrnrrrrrrsrmrrrprrs rtdup_ext_factorr[s.D qM6!Q>> from sympy import QQ, sqrt >>> from sympy.polys.factortools import dmp_ext_factor >>> K = QQ.algebraic_field(sqrt(2)) We can now factorise the polynomial `x^2 y^2 - 2` over `K`: >>> p = [[K(1),K(0),K(0)], [], [K(-2)]] # x**2*y**2 - 2 >>> p1 = [[K(1),K(0)], [-K.unit]] # x*y - sqrt(2) >>> p2 = [[K(1),K(0)], [+K.unit]] # x*y + sqrt(2) >>> dmp_ext_factor(p, 1, K) == (K.one, [(p1, 1), (p2, 1)]) True Usually this would be done at a higher level: >>> from sympy import factor >>> from sympy.abc import x, y >>> factor(x**2*y**2 - 2, extension=sqrt(2)) (x*y - sqrt(2))*(x*y + sqrt(2)) Explanation =========== This is Trager's algorithm for multivariate polynomials. In particular this function is algorithm ``alg_factor`` from [Trager76]_. See :func:`dup_ext_factor` for explanation. See Also ======== dup_ext_factor: Analogous function for univariate polynomials over ``k(a)``. dmp_sqf_norm: Multivariate version of subroutine ``sqfr_norm`` also from [Trager76]_. sympy.polys.polytools.factor: The high-level function that ultimately uses this function as needed. c3&K|] }|dk ywrBr~rCs rtrz!dmp_ext_factor..rDrrh)r[rrHrrrXrVdmp_factor_list_includerYrrrrSrZrOryrZ)rlrxrnrrrrrsrmrrprrsirros rtdmp_ext_factorr`TsA^ a## q!Q BAq!A 1?1a0 112v 1a !qA1a#GAq!%aAEE2G 7|q#'0 NA{FAquua0A#Aq!Q/GAq!%&'rAFF'A'!Q1%AGAJ   7Aq 1Fq!V$ v:(sD ct|||j}t||j|j\}}t |D]$\}\}}t||j||f||<&|j ||j|fS)z2Factor univariate polynomials over finite fields. )rrYrr3rr)rlrnrrmrrqs rt dup_gf_factorrbsAq!%% Aq!%%/NE7w'3 6Aq!!QUUA.2 3 99UAEE "G ++rvctd)z4Factor multivariate polynomials over finite fields. z+multivariate polynomials over finite fields)NotImplementedError)rlrxrns rt dmp_gf_factorres K LLrvcbt||\}}t||\}}|jrt||\}}n9|jrt ||\}}n|j rt||\}}n|jrt||\}}n|js ||j}}t|||}nd}|jr.|j}t|||\}}t|||}n|}|j rt#||\}}n|j$rot'|d|\}} t)|| |j*\}}t-|D]\} \}} t/|| || f|| <|j1||j*}nt3d|z|jrt-|D]\} \}} t|||| f|| <|j1||}|j5|}|ryt-|D]W\} \}} t7||} t9|| |}t|||}|| f|| <|j;||j=| | }Y|j1||}|}|r*|j?d|j@|jBg|f||ztE|fS);Factor univariate polynomials into irreducibles in `K[x]`. Nr#factorization not supported over %s)#r&rIis_FiniteFieldrb is_Algebraicr[is_GaussianRingrQis_GaussianFieldrHis_Exact get_exactris_FieldrrBrris_Polyr$rSrYrr%rr^quor;r@mulpowrrr/r[) rlrrrrrm K0_inexactrndenomrxrrqmax_norms rtrrs B DAqAr"GD! &q"-w '2.w  (B/w  (B/w{{JAz2.AJ ;; A'2q1HE1Ar1%AA 77*1a0NE7 YYaA&DAq,Q1559NE7&w/ 5 6Aq'1a0!4  5IIeQUU+ECbHI I ;;&w/ 8 6Aq)!Q3Q7  8JJua(EFF5%(E!*7!3?IAv1+Ar2H&q(B7A#Ar:6A"#QGAJFF5"&&1*=>E ?#**5"5qBFFBGG,a01 :}W- --rvct||\}}|st|gdfgSt|dd||}||ddfg|ddzS)rgrhrN)rrr>)rlrnrrmrs rtrXrXsa$Q*NE7 E7#Q'(( 71:a=% 3GAJqM"#gabk11rvc l|s t||St|||\}}t|||\}}|jrt |||\}}n~|j rt |||\}}n`|jrt|||\}}nB|jrt|||\}}n$|js!||j}}t||||}nd}|jr0|j}t!||||\} }t||||}n|}|j"rKt%|||\} }} t'|| |\}}t)|D]\} \}} t+|| | || f|| <n|j,rot/|||\}} t1|| |j2\}}t)|D]\} \}} t5|| || f|| <|j7||j2}nt9d|z|jrt)|D]\} \}} t||||| f|| <|j7||}|j;| }|r|t)|D]Z\} \}} t=|||}t?||||}t||||}|| f|| <|jA||jC|| }\|j7||}|}t)tE|D]D\} }|s d|| z zdzd| zz|jFi}|jIdtK||||fF||ztM|fS)=Factor multivariate polynomials into irreducibles in `K[X]`. Nrh)r)rhr)'rr'rJrirerjr`rkrVrlrTrmrnrrorrCrr"r.rr#rpr$rSrYr%rr^rqr<rArrrsrrrrr[)rlrxrrrrrmrtrnrulevelsr rrqrvrterms rtrSrSsU q"%% Ar "DAq"1a,GD! &q!R0w '1b1w  (Ar2w  (Ar2w{{JAq*b1AJ ;; A'1b!4HE1Aq"a(AA 77&q!Q/LFAq*1a3NE7&w/ ? 6Aq)!VQ:A>  ? YYaA&DAq,Q1559NE7&w/ 5 6Aq'1a0!4  5IIeQUU+ECbHI I ;;&w/ ; 6Aq)!Q26:  ;JJua(EFF5%(E!*7!3?IAv1+Aq"5H&q(Ar:A#Aq"j9A"#QGAJFF5"&&1*=>E ?#**5"5(1+&;1 a!e t#d1f,bff5q=q"5q9: ; :}W- --rvc|s t||St|||\}}|st||dfgSt|dd|||}||ddfg|ddzS)ryrhrN)rXrSr r?)rlrxrnrrmrs rtr^r^Msu &q!,,$Q1-NE7 E1%q)** 71:a=%A 6GAJqM"#gabk11rvct|d|S)z_ Returns ``True`` if a univariate polynomial ``f`` has no factors over its domain. r)dmp_irreducible_p)rlrns rtdup_irreducible_pr[s Q1 %%rvc`t|||\}}|syt|dkDry|d\}}|dk(S)za Returns ``True`` if a multivariate polynomial ``f`` has no factors over its domain. TrhFr)rSr)rlrxrnrrmrqs rtr~r~csA !Aq)JAw  W qz1Av rv)F)NN)__doc__sympy.external.gmpyrsympy.core.randomrsympy.polys.galoistoolsrrrrr r r r r rrsympy.polys.densebasicrrrrrrrrrrrrrrrrr r!r"r#r$r%r&r'sympy.polys.densearithr(r)r*r+r,r-r.r/r0r1r2r3r4r5r6r7r8r9r:r;r<r=r>r?r@rAsympy.polys.densetoolsrBrCrDrErFrGrHrIrJrKrLrMrNrOrPsympy.polys.euclidtoolsrQrRrSsympy.polys.sqfreetoolsrTrUrVrWrXrYrZsympy.polys.polyutilsr[sympy.polys.polyconfigr\sympy.polys.polyerrorsr]r^r_r`sympy.utilitiesramathrbrrcrrdrrerfruryrrrrrrrrrrrrrr rrrrr'r2r.rHrQrTrVr[r`rbrerrXrSr^rr~r~rvrtrs@,&""""""""$$$$$$$$&&&&&""0(FF$::7I!8!8:x%6r75rfR  Pf    )X8:Qh(*43l-`A H1hK\@(F,,_DK\ ,M ?.D2J.Z 2& rv