?? lzz_pxfactoring.cpp
字號:
long p;
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;
if (n % p != 0) return 1;
PowerCompose(s, b, n/p, 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]);
}
}
static
void 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);
}
static
void 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 SFCanZass1(vec_pair_zz_pX_long& u, zz_pX& h, const zz_pX& f, long verbose)
{
if (!IsOne(LeadCoeff(f)) || deg(f) == 0)
Error("SFCanZass1: bad args");
double t;
long p = zz_p::modulus();
zz_pXModulus F;
build(F, f);
if (verbose) { cerr << "computing X^p..."; t = GetTime(); }
PowerXMod(h, p, F);
if (verbose) { cerr << (GetTime()-t) << "\n"; }
if (verbose) { cerr << "computing DDF..."; t = GetTime(); }
NewDDF(u, f, h, verbose);
if (verbose) {
t = GetTime()-t;
cerr << "DDF time: " << t << "\n";
}
}
void SFCanZass2(vec_zz_pX& factors, const vec_pair_zz_pX_long& u,
const zz_pX& h, long verbose)
{
zz_pX hh;
vec_zz_pX v;
factors.SetLength(0);
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 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;
long 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;
}
static
long 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;
}
void 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;
}
}
long 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;
long p1;
f = ff;
if (!IsOne(LeadCoeff(f)))
Error("FindRoot: bad args");
if (deg(f) == 0)
Error("FindRoot: bad args");
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));
}
static
long power(long a, long e)
{
long i, res;
res = 1;
for (i = 1; i <= e; i++)
res = res * a;
return res;
}
static
long 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);
}
static
long 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;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -