HEU_GCD_MAX=6def_dup_zz_gcd_interpolate(h,x,K):"""Interpolate polynomial GCD from integer GCD. """f=[]whileh:g=h%xifg>x//2:g-=xf.insert(0,g)h=(h-g)//xreturnfdefdup_zz_heu_gcd(f,g,K):""" 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]_ """result=_dup_rr_trivial_gcd(f,g,K)ifresultisnotNone:returnresultdf=dup_degree(f)dg=dup_degree(g)gcd,f,g=dup_extract(f,g,K)ifdf==0ordg==0:return[gcd],f,gf_norm=dup_max_norm(f,K)g_norm=dup_max_norm(g,K)B=K(2*min(f_norm,g_norm)+29)x=max(min(B,99*K.sqrt(B)),2*min(f_norm//abs(dup_LC(f,K)),g_norm//abs(dup_LC(g,K)))+2)foriinrange(0,HEU_GCD_MAX):ff=dup_eval(f,x,K)gg=dup_eval(g,x,K)ifffandgg:h=K.gcd(ff,gg)cff=ff//hcfg=gg//hh=_dup_zz_gcd_interpolate(h,x,K)h=dup_primitive(h,K)[1]cff_,r=dup_div(f,h,K)ifnotr:cfg_,r=dup_div(g,h,K)ifnotr:h=dup_mul_ground(h,gcd,K)returnh,cff_,cfg_cff=_dup_zz_gcd_interpolate(cff,x,K)h,r=dup_div(f,cff,K)ifnotr:cfg_,r=dup_div(g,h,K)ifnotr:h=dup_mul_ground(h,gcd,K)returnh,cff,cfg_cfg=_dup_zz_gcd_interpolate(cfg,x,K)h,r=dup_div(g,cfg,K)ifnotr:cff_,r=dup_div(f,h,K)ifnotr:h=dup_mul_ground(h,gcd,K)returnh,cff_,cfgx=73794*x*K.sqrt(K.sqrt(x))//27011raiseHeuristicGCDFailed('no luck')def_dmp_zz_gcd_interpolate(h,x,v,K):"""Interpolate polynomial GCD from integer GCD. """f=[]whilenotdmp_zero_p(h,v):g=dmp_ground_trunc(h,x,v,K)f.insert(0,g)h=dmp_sub(h,g,v,K)h=dmp_quo_ground(h,x,v,K)ifK.is_negative(dmp_ground_LC(f,v+1,K)):returndmp_neg(f,v+1,K)else:returnf

[docs]defdmp_zz_heu_gcd(f,g,u,K):""" 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 proces 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]_ """ifnotu:returndup_zz_heu_gcd(f,g,K)result=_dmp_rr_trivial_gcd(f,g,u,K)ifresultisnotNone:returnresultgcd,f,g=dmp_ground_extract(f,g,u,K)f_norm=dmp_max_norm(f,u,K)g_norm=dmp_max_norm(g,u,K)B=K(2*min(f_norm,g_norm)+29)x=max(min(B,99*K.sqrt(B)),2*min(f_norm//abs(dmp_ground_LC(f,u,K)),g_norm//abs(dmp_ground_LC(g,u,K)))+2)foriinrange(0,HEU_GCD_MAX):ff=dmp_eval(f,x,u,K)gg=dmp_eval(g,x,u,K)v=u-1ifnot(dmp_zero_p(ff,v)ordmp_zero_p(gg,v)):h,cff,cfg=dmp_zz_heu_gcd(ff,gg,v,K)h=_dmp_zz_gcd_interpolate(h,x,v,K)h=dmp_ground_primitive(h,u,K)[1]cff_,r=dmp_div(f,h,u,K)ifdmp_zero_p(r,u):cfg_,r=dmp_div(g,h,u,K)ifdmp_zero_p(r,u):h=dmp_mul_ground(h,gcd,u,K)returnh,cff_,cfg_cff=_dmp_zz_gcd_interpolate(cff,x,v,K)h,r=dmp_div(f,cff,u,K)ifdmp_zero_p(r,u):cfg_,r=dmp_div(g,h,u,K)ifdmp_zero_p(r,u):h=dmp_mul_ground(h,gcd,u,K)returnh,cff,cfg_cfg=_dmp_zz_gcd_interpolate(cfg,x,v,K)h,r=dmp_div(g,cfg,u,K)ifdmp_zero_p(r,u):cff_,r=dmp_div(f,h,u,K)ifdmp_zero_p(r,u):h=dmp_mul_ground(h,gcd,u,K)returnh,cff_,cfgx=73794*x*K.sqrt(K.sqrt(x))//27011raiseHeuristicGCDFailed('no luck')