📄 zz_pxfactoring.c
字号:
{ long n = deg(f); if (n <= 0) return 0; if (n == 1) return 1; const ZZ& p = ZZ_p::modulus(); ZZ_pXModulus F; build(F, f); ZZ_pX b, r, s; PowerXMod(b, p, F); long i; for (i = 0; i < iter; i++) { random(r, n); TraceMap(s, r, n, F, b); if (deg(s) > 0) return 0; } if (p >= n) return 1; long pp; conv(pp, p); if (n % pp != 0) return 1; PowerCompose(s, b, n/pp, F); return !IsX(s);}long ZZ_pX_BlockingFactor = 10;void DDF(vec_pair_ZZ_pX_long& factors, const ZZ_pX& ff, const ZZ_pX& hh, long verbose){ ZZ_pX f = ff; ZZ_pX h = hh; if (!IsOne(LeadCoeff(f))) Error("DDF: bad args"); factors.SetLength(0); if (deg(f) == 0) return; if (deg(f) == 1) { AddFactor(factors, f, 1, verbose); return; } long CompTableSize = 2*SqrRoot(deg(f)); long GCDTableSize = ZZ_pX_BlockingFactor; ZZ_pXModulus F; build(F, f); ZZ_pXArgument H; build(H, h, F, min(CompTableSize, deg(f))); long i, d, limit, old_n; ZZ_pX g, X; vec_ZZ_pX tbl(INIT_SIZE, GCDTableSize); SetX(X); i = 0; g = h; d = 1; limit = GCDTableSize; while (2*d <= deg(f)) { old_n = deg(f); sub(tbl[i], g, X); i++; if (i == limit) { ProcessTable(f, factors, F, i, tbl, d, verbose); i = 0; } d = d + 1; if (2*d <= deg(f)) { // we need to go further if (deg(f) < old_n) { // f has changed build(F, f); rem(h, h, f); rem(g, g, f); build(H, h, F, min(CompTableSize, deg(f))); } CompMod(g, g, H, F); } } ProcessTable(f, factors, F, i, tbl, d-1, verbose); if (!IsOne(f)) AddFactor(factors, f, deg(f), verbose);}void RootEDF(vec_ZZ_pX& factors, const ZZ_pX& f, long verbose){ vec_ZZ_p roots; double t; if (verbose) { cerr << "finding roots..."; t = GetTime(); } FindRoots(roots, f); if (verbose) { cerr << (GetTime()-t) << "\n"; } long r = roots.length(); factors.SetLength(r); for (long j = 0; j < r; j++) { SetX(factors[j]); sub(factors[j], factors[j], roots[j]); }}staticvoid EDFSplit(vec_ZZ_pX& v, const ZZ_pX& f, const ZZ_pX& b, long d){ ZZ_pX a, g, h; ZZ_pXModulus F; vec_ZZ_p roots; build(F, f); long n = F.n; long r = n/d; random(a, n); TraceMap(g, a, d, F, b); MinPolyMod(h, g, F, r); FindRoots(roots, h); FindFactors(v, f, g, roots);}staticvoid RecEDF(vec_ZZ_pX& factors, const ZZ_pX& f, const ZZ_pX& b, long d, long verbose){ vec_ZZ_pX v; long i; ZZ_pX bb; if (verbose) cerr << "+"; EDFSplit(v, f, b, d); for (i = 0; i < v.length(); i++) { if (deg(v[i]) == d) { append(factors, v[i]); } else { ZZ_pX bb; rem(bb, b, v[i]); RecEDF(factors, v[i], bb, d, verbose); } }} void EDF(vec_ZZ_pX& factors, const ZZ_pX& ff, const ZZ_pX& bb, long d, long verbose){ ZZ_pX f = ff; ZZ_pX b = bb; if (!IsOne(LeadCoeff(f))) Error("EDF: bad args"); long n = deg(f); long r = n/d; if (r == 0) { factors.SetLength(0); return; } if (r == 1) { factors.SetLength(1); factors[0] = f; return; } if (d == 1) { RootEDF(factors, f, verbose); return; } double t; if (verbose) { cerr << "computing EDF(" << d << "," << r << ")..."; t = GetTime(); } factors.SetLength(0); RecEDF(factors, f, b, d, verbose); if (verbose) cerr << (GetTime()-t) << "\n";}void SFCanZass(vec_ZZ_pX& factors, const ZZ_pX& ff, long verbose){ ZZ_pX f = ff; if (!IsOne(LeadCoeff(f))) Error("SFCanZass: bad args"); if (deg(f) == 0) { factors.SetLength(0); return; } if (deg(f) == 1) { factors.SetLength(1); factors[0] = f; return; } factors.SetLength(0); double t; const ZZ& p = ZZ_p::modulus(); ZZ_pXModulus F; build(F, f); ZZ_pX h; if (verbose) { cerr << "computing X^p..."; t = GetTime(); } PowerXMod(h, p, F); if (verbose) { cerr << (GetTime()-t) << "\n"; } vec_pair_ZZ_pX_long u; if (verbose) { cerr << "computing DDF..."; t = GetTime(); } NewDDF(u, f, h, verbose); if (verbose) { t = GetTime()-t; cerr << "DDF time: " << t << "\n"; } ZZ_pX hh; vec_ZZ_pX v; long i; for (i = 0; i < u.length(); i++) { const ZZ_pX& g = u[i].a; long d = u[i].b; long r = deg(g)/d; if (r == 1) { // g is already irreducible append(factors, g); } else { // must perform EDF if (d == 1) { // root finding RootEDF(v, g, verbose); append(factors, v); } else { // general case rem(hh, h, g); EDF(v, g, hh, d, verbose); append(factors, v); } } }} void CanZass(vec_pair_ZZ_pX_long& factors, const ZZ_pX& f, long verbose){ if (!IsOne(LeadCoeff(f))) Error("CanZass: bad args"); double t; vec_pair_ZZ_pX_long sfd; vec_ZZ_pX x; if (verbose) { cerr << "square-free decomposition..."; t = GetTime(); } SquareFreeDecomp(sfd, f); if (verbose) cerr << (GetTime()-t) << "\n"; factors.SetLength(0); long i, j; for (i = 0; i < sfd.length(); i++) { if (verbose) { cerr << "factoring multiplicity " << sfd[i].b << ", deg = " << deg(sfd[i].a) << "\n"; } SFCanZass(x, sfd[i].a, verbose); for (j = 0; j < x.length(); j++) append(factors, cons(x[j], sfd[i].b)); }}void mul(ZZ_pX& f, const vec_pair_ZZ_pX_long& v){ long i, j, n; n = 0; for (i = 0; i < v.length(); i++) n += v[i].b*deg(v[i].a); ZZ_pX g(INIT_SIZE, n+1); set(g); for (i = 0; i < v.length(); i++) for (j = 0; j < v[i].b; j++) { mul(g, g, v[i].a); } f = g;}staticlong BaseCase(const ZZ_pX& h, long q, long a, const ZZ_pXModulus& F){ long b, e; ZZ_pX lh(INIT_SIZE, F.n); lh = h; b = 1; e = 0; while (e < a-1 && !IsX(lh)) { e++; b *= q; PowerCompose(lh, lh, q, F); } if (!IsX(lh)) b *= q; return b;}staticvoid TandemPowerCompose(ZZ_pX& y1, ZZ_pX& y2, const ZZ_pX& h, long q1, long q2, const ZZ_pXModulus& F){ ZZ_pX z(INIT_SIZE, F.n); long sw; z = h; SetX(y1); SetX(y2); while (q1 || q2) { sw = 0; if (q1 > 1 || q2 > 1) sw = 4; if (q1 & 1) { if (IsX(y1)) y1 = z; else sw = sw | 2; } if (q2 & 1) { if (IsX(y2)) y2 = z; else sw = sw | 1; } switch (sw) { case 0: break; case 1: CompMod(y2, y2, z, F); break; case 2: CompMod(y1, y1, z, F); break; case 3: Comp2Mod(y1, y2, y1, y2, z, F); break; case 4: CompMod(z, z, z, F); break; case 5: Comp2Mod(z, y2, z, y2, z, F); break; case 6: Comp2Mod(z, y1, z, y1, z, F); break; case 7: Comp3Mod(z, y1, y2, z, y1, y2, z, F); break; } q1 = q1 >> 1; q2 = q2 >> 1; }}staticlong RecComputeDegree(long u, const ZZ_pX& h, const ZZ_pXModulus& F, FacVec& fvec){ if (IsX(h)) return 1; if (fvec[u].link == -1) return BaseCase(h, fvec[u].q, fvec[u].a, F); ZZ_pX h1, h2; long q1, q2, r1, r2; q1 = fvec[fvec[u].link].val; q2 = fvec[fvec[u].link+1].val; TandemPowerCompose(h1, h2, h, q1, q2, F); r1 = RecComputeDegree(fvec[u].link, h2, F, fvec); r2 = RecComputeDegree(fvec[u].link+1, h1, F, fvec); return r1*r2;} long ComputeDegree(const ZZ_pX& h, const ZZ_pXModulus& F) // f = F.f is assumed to be an "equal degree" polynomial // h = X^p mod f // the common degree of the irreducible factors of f is computed{ if (F.n == 1 || IsX(h)) return 1; FacVec fvec; FactorInt(fvec, F.n); return RecComputeDegree(fvec.length()-1, h, F, fvec);}long ProbComputeDegree(const ZZ_pX& h, const ZZ_pXModulus& F){ if (F.n == 1 || IsX(h)) return 1; long n = F.n; ZZ_pX P1, P2, P3; random(P1, n); TraceMap(P2, P1, n, F, h); ProbMinPolyMod(P3, P2, F, n/2); long r = deg(P3); if (r <= 0 || n % r != 0) return 0; else return n/r;}void FindRoot(ZZ_p& root, const ZZ_pX& ff)// finds a root of ff.// assumes that ff is monic and splits into distinct linear factors{ ZZ_pXModulus F; ZZ_pX h, h1, f; ZZ_p r; ZZ p1; f = ff; if (!IsOne(LeadCoeff(f))) Error("FindRoot: bad args"); if (deg(f) == 0) Error("FindRoot: bad args"); RightShift(p1, ZZ_p::modulus(), 1); h1 = 1; while (deg(f) > 1) { build(F, f); random(r); PowerXPlusAMod(h, r, p1, F); sub(h, h, h1); GCD(h, h, f); if (deg(h) > 0 && deg(h) < deg(f)) { if (deg(h) > deg(f)/2) div(f, f, h); else f = h; } } negate(root, ConstTerm(f));}staticlong power(long a, long e){ long i, res; res = 1; for (i = 1; i <= e; i++) res = res * a; return res;}staticlong IrredBaseCase(const ZZ_pX& h, long q, long a, const ZZ_pXModulus& F){ long e; ZZ_pX X, s, d; e = power(q, a-1); PowerCompose(s, h, e, F); SetX(X); sub(s, s, X); GCD(d, F.f, s); return IsOne(d);}staticlong RecIrredTest(long u, const ZZ_pX& h, const ZZ_pXModulus& F, const FacVec& fvec){ long q1, q2; ZZ_pX h1, h2; if (IsX(h)) return 0; if (fvec[u].link == -1) { return IrredBaseCase(h, fvec[u].q, fvec[u].a, F); } q1 = fvec[fvec[u].link].val; q2 = fvec[fvec[u].link+1].val; TandemPowerCompose(h1, h2, h, q1, q2, F); return RecIrredTest(fvec[u].link, h2, F, fvec) && RecIrredTest(fvec[u].link+1, h1, F, fvec);}long DetIrredTest(const ZZ_pX& f){ if (deg(f) <= 0) return 0; if (deg(f) == 1) return 1; ZZ_pXModulus F; build(F, f); ZZ_pX h; PowerXMod(h, ZZ_p::modulus(), F); ZZ_pX s; PowerCompose(s, h, F.n, F); if (!IsX(s)) return 0; FacVec fvec; FactorInt(fvec, F.n); return RecIrredTest(fvec.length()-1, h, F, fvec);}long IterIrredTest(const ZZ_pX& f){ if (deg(f) <= 0) return 0; if (deg(f) == 1) return 1; ZZ_pXModulus F; build(F, f); ZZ_pX h; PowerXMod(h, ZZ_p::modulus(), F); long CompTableSize = 2*SqrRoot(deg(f)); ZZ_pXArgument H; build(H, h, F, CompTableSize); long i, d, limit, limit_sqr; ZZ_pX g, X, t, prod; SetX(X); i = 0; g = h; d = 1; limit = 2; limit_sqr = limit*limit; set(prod); while (2*d <= deg(f)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -