K i$dZddlmZdZdZdZy)z-Heuristic polynomial GCD algorithm (HEUGCD). )HeuristicGCDFailedc |j|jk(r |jjjsJ|j}|jd}|j}|j |\}}}|j }|j }|dt ||zdz}tt |d|j|zdt |t|jz|t|jzzdz} tdtD]} |j|| } |j|| } | r?| r<|jdk(r|j| | \} }}nt!| | \} }}t#| | |} | j%d} |j'| \}}|s.|j'| \}}|s| j)|} | ||fcSt#|| |}|j'|\} }|s.|j'| \}}|s| j)|} | ||fcSt#|| |}|j'|\} }|s.|j'| \}}|s| j)|} | ||fcSd| z|j|j| zdz} t+d ) aH 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.heuristicgcd import heugcd >>> 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 >>> h, cff, cfg = heugcd(f, g) >>> h, cff, cfg (x + y, x + y, x) >>> cff*h == f True >>> cfg*h == g True References ========== .. [1] [Liao95]_ criB iizno luck)ringdomainis_ZZgensextract_groundmax_normminmaxsqrtabsLCrange HEU_GCD_MAXevaluatengens cofactorsheugcd_gcd_interpolate primitivediv mul_groundr)fgr x0r gcdf_normg_normBxiffgghcffcfgcff_rcfg_s ^/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/polys/heuristicgcd.pyrrs` 66QVV  3 33 3 66D 1B [[F  #ICA ZZ\F ZZ\FqVV$$r)*A C2fkk!n$ % c&CI%CI%' ')* + ,A1k ",; ZZA  ZZA  "zzQ$..r26 3$Rn 3 At,A a AeeAhGD!%%(a S)AdD=("340C55:DAq%%(a S)Ac4<'"340C55:DAq%%(a S)AdC<' !Gfkk&++a.1 1U :Y,;\ Y ''cb|jd}}|jdk(r-|r}||z}||dzkDr||z}||z |z}|r|||f<|dz }|r*nR|rP|j|}||z j|}|r!|j D]\}}|||f|z<|dz }|rP|j dkr| S|S)z-Interpolate polynomial GCD from integer GCD. rrr)zeror trunc_ground quo_ground itertermsr)r+r'r r r(r!monomcoeffs r1rrxs 99aqA zzQAA16z161Q1 A1$ FAq!AQ""1%A$%KKM,LE5&+AqdUlO, FA ttaxr  r2N)__doc__ polyerrorsrrrrr2r1r=s3* o(br2