-
Notifications
You must be signed in to change notification settings - Fork 0
/
MollerSig_weak.m
560 lines (477 loc) · 18 KB
/
MollerSig_weak.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
// Created: Wed Dec 13 17:47:42 2017
// Last modified: Tue May 14 11:57:58 2019
// Hash: 1a4c44eccaa97014ef2ceea4036895ee
Attach("general.m");
load "Signatures_old.m";
function SatSet_of_mon(LMs,m,XS)
/* Returns the largest saturated set of {1..m} wrt LMs[1]...LMs[m]
whose lcm divides XS.
The output is the set of all j in {1..m} such that LMs[j] divides
XS.
*/
S := [];
while exists(k){k : k in [1..m] | (not k in S)
and IsDivisibleBy(XS,LMs[k])} do
Append(~S,k);
end while;
return S;
end function;
function SatSet_lcm(S,LMs)
/* Given a saturated set S, returns its lcm x^S */
return Lcm([LMs[j] : j in S]);
end function;
function SatSets_Generate(LMs,ss)
/* Generate all saturated sets of [1..ss] containing ss */
ExistingLcms := {};
SS := [];
m := ss-1;
/* subsets := Subsets({1..m}); */
/* subsets := [Include(S,ss) : S in subsets]; */
for size_S in [0..m] do
subsets := Subsets({1..m},size_S);
subsets := [Sort(Append(Setseq(S),ss)) : S in subsets];
for S in subsets do
XS := SatSet_lcm(S,LMs);
if XS in ExistingLcms then
continue;
else
Include(~ExistingLcms,XS);
end if;
SExt := SatSet_of_mon(LMs,ss,XS);
Include(~SS,SExt);
end for;
end for;
return SS;
end function;
function SatSets_Generate_with_grouping(LMs,ss)
/* Generate all saturated sets of [1..ss] containing ss.
This function implements the following optimization: it first
groups elements with the same LM together, and then make a call to
SatSets_Generate with a list of distinct LMs. The result is then
expanded again.
The complexity is thus exponential in the number of distinct
elements in LMs[1..ss], instead of exponential in ss.
*/
ExistingLMs := [LMs[ss]];
Indices := [[ss]];
for i in [1..ss-1] do
j := Index(ExistingLMs,LMs[i]);
/* print i,LMs[i],ExistingLMs,j; */
if j gt 0 then
Indices[j] := Append(Indices[j],i);
else
Append(~ExistingLMs,LMs[i]);
Append(~Indices,[i]);
end if;
end for;
ExistingLMs := ExistingLMs[[2..#ExistingLMs]] cat [ExistingLMs[1]];
printf "npols=%o\tnLMs=%o\n", ss, #ExistingLMs;
Indices := Indices[[2..#Indices]] cat [Indices[1]];
/* print ExistingLMs; */
/* print Indices; */
SS := SatSets_Generate(ExistingLMs,#ExistingLMs);
SS := [Sort(&cat[Indices[i] : i in S]) : S in SS];
return SS;
end function;
function Max_sigs(sigs)
/* Given a set of signatures, returns the greatest one, as well as
the list of indices realizing this maximum. */
ires := [1]; res :=
sigs[1]; for i in [2..#sigs] do
if Sig_Lt(res,sigs[i]) then
res := sigs[i];
ires := [i];
elif Sig_Simeq(res,sigs[i]) then
Append(~ires,i);
end if;
end for;
return res,ires;
end function;
function Sig_OfSatSet(S,LMs,sigs)
/* Computes the signature of a saturated set, together with the
list of indices realizing that signature. */
XS := SatSet_lcm(S,LMs);
sigsS := [Sig_Multiply(sigs[i],1,XS div LMs[i]) : i in S];
sig_ss,ii := Max_sigs(sigsS);
ss := [S[i] : i in ii];
return sig_ss,ss;
end function;
function Sig_RegularizeSatSet(S,LMs,sigs)
/* Given a saturated set, computes all regular saturated sets which
have the same lcm */
sig_ss,SS := Sig_OfSatSet(S,LMs,sigs);
XS := SatSet_lcm(S,LMs);
Srest := [s : s in S | not s in SS];
res := [];
for s in SS do
Scand := Append(Srest,s);
if XS eq SatSet_lcm(Scand,LMs) then
Append(~res,Scand);
end if;
end for;
return res;
end function;
function Moller_Reduce(gg,sigG,ss,pols,sigs,LMs,LCs,funs :
Criterion := true,
Tail := true)
/* Implements weak reduction of gg, with signature sigG, wrt
module elements alpha_1..alpha_ss. The alpha_i's are given with
their signature sigs[i] and their polynomial pols[i].
For all i in [1..ss], LMs[i] = LeadingMonomial(pols[i]) and LCs[i] = LeadingCoefficient(pols[i]).
Optional parameters:
- Criterion: True iff we only do regular reductions (if false, the contents of sigG and sigs are ignored)
- Tail: True iff we want to reduce beyond the leading coefficient of gg (it makes the algorithm more efficient and the output more readable)
*/
SatIdeal,CosetRep,LinDecomp := Explode(funs);
reducible := true;
m := #pols;
sing_red := false;
g := gg;
res := 0;
first := true;
main_term := 0;
while first or (Tail and g ne 0) do
first := false;
reducible := true;
while reducible do
/* print(g); */
if g eq 0 then
reducible := false;
break;
end if;
lm := LeadingMonomial(g);
S := SatSet_of_mon(LMs,m,lm);
if Criterion then
done := false;
while not IsEmpty(S) and not done do
XS := LeadingMonomial(g);
sigsS := [Sig_Multiply(sigs[i],1,XS div LMs[i]) : i in S];
if exists(iii){i : i in [1..#S]
| Sig_Simeq(sigsS[i],sigG)
or Sig_Lt(sigG,sigsS[i])} then
Remove(~S,iii);
else
done := true;
end if;
end while;
end if;
if IsEmpty(S) then
/* Then we cannot reduce any more */
reducible := false;
else
LC_S := [LCs[j] : j in S];
lc := LeadingCoefficient(g);
lc_red := CosetRep(LC_S,lc);
/* printf "S=%o, LC_S=%o, lc=%o, lc_red=%o\n", S, LC_S,lc,lc_red; */
if lc_red ne 0 then
/* We cannot eliminate the leading coefficient, so we
mark the polynomial as irreducible and we perform one
last reduction step to bring the LC to coset
representative form */
reducible := false;
lc := lc-lc_red;
end if;
bb := LinDecomp(LC_S,lc);
for j in [1..#LC_S] do
ii := S[j];
g -:= bb[j]*(lm div LMs[ii])*pols[ii];
end for;
end if;
end while;
if Tail then
res +:= LeadingTerm(g);
g -:= LeadingTerm(g);
else
res := g;
end if;
end while;
return res;
end function;
function Sig_F5Criterion(sig_ss,ss,LMs,LCs,sigs,interm_ideals,funs)
/* True iff the signature sig_ss satisfies the F5 criterion (and
should be kept)
That is, if sig_ss = k*m*e_i, it is True iff k*m is not reducible
modulo a basis of the initial ideal of <f_1...f_(i-1)>. This basis
is recovered from LMs and LCs (respectively the LMs and LCs of
already computed elements) and interm_ideals[i-1] (which holds the
index of the last polynomial in the basis with signature
#*#*e_(i-1)).
*/
if sig_ss`i eq 1 then
return true;
end if;
lpols := [LCs[i]*LMs[i] : i in [1..interm_ideals[sig_ss`i]-1]];
mon := sig_ss`k * sig_ss`mu;
mon_red := Moller_Reduce(mon,0,ss,lpols,sigs,LMs,LCs,funs : Criterion := false);
return mon_red ne 0;
end function;
function SatSets_Generate_maybe_regular(ss,LMs,sigs
: Criterion := true)
/* Generates (regular) saturated sets of [1..ss]
Optional parameters:
- Criterion: if false, just compute all saturated sets,
disregarding the signatures
*/
new_satsets := SatSets_Generate_with_grouping(LMs,ss);
/* print "New candidate satsets:", new_satsets; */
if Criterion then
/* If we are computing with signatures, we need to extend the
set of saturated sets whenever we add a polynomial to the
basis */
res := [];
for S in new_satsets do
res cat:= Sig_RegularizeSatSet(S,LMs,sigs);
end for;
new_satsets := res;
end if;
return new_satsets;
end function;
function OneSingularReducible(g,SigG,sigs,LMs)
/* Test if a candidate basis element is 1-singular reducible.
Implementation of 1-SingularReducible (Algorithm 4)
*/
LMg := LeadingMonomial(g);
test := exists{j : j in [1..#sigs] | IsDivisibleBy(LMg,LMs[j])
and ((LMg div LMs[j])*(sigs[j]`mu))
eq SigG`mu
and sigs[j]`i eq SigG`i
and IsDivisibleBy(SigG`k,sigs[j]`k) };
return test;
end function;
function MollerSig_weak (F,funs :
Signature := true,
F5_Criterion := true,
Sing_Criterion := true)
/*
Compute a Gröbner basis of F.
funs are the required algorithms for the base ring: SatIdeal,
CosetRep and LinDecomp. CosetRep is not described in the paper,
for applicable rings it gives the possibility to perform "total
reductions", giving to each Leading Coefficient a unique
representation modulo the leading coefficient of candidate
reducers.
For example over ZZ, the Euclidean remainder can be used, and over
a multivariate polynomial ring, the Normal Form modulo a Gröbner basis.
When not applicable, a generic "do-nothing" CosetRep function is provided.
Optional parameters:
- Signature: True iff we want to compute a signature Gröbner basis (if false, the algorithm is just the classical version of Möller's algorithm)
- F5_Criterion: True iff we want to exclude S-vectorsets using the F5 criterion
- Sing_Criterion: True iff we want to exclude S-vectorsets using the singular criterion
*/
SatIdeal,CosetRep,LinDecomp := Explode(funs);
pols := [];
sigs := [];
LMs := [];
LCs := [];
interm_ideals := [];
m := #F;
ss := 1;
cnt_vectsets := 0;
cnt_satsets := 0;
cnt_red0 := 0;
cnt_sing_pairs := 0;
cnt_1sing_red := 0;
cnt_F5_pairs := 0;
SS := [];
prev_sig := Sig_Create(1,1,1);
for i in [1..m] do
sigi := Sig_Create(1,1,i);
fred := Moller_Reduce(F[i],sigi,ss,pols,sigs,LMs,LCs,funs :
Criterion := Signature);
if fred eq 0 then
printf "F[%o] reduces to 0\n", i;
Append(~interm_ideals,#pols);
printf "i=%o, interm_ideals=%o\n",i,interm_ideals;
continue;
// In this case we completely skip this polynomial
end if;
Append(~pols,fred);
Append(~sigs,sigi);
Append(~LMs, LeadingMonomial(fred));
Append(~LCs, LeadingCoefficient(fred));
Append(~interm_ideals,#pols);
printf "i=%o, interm_ideals=%o\n",i,interm_ideals;
new_satsets :=
SatSets_Generate_maybe_regular(#pols,LMs,sigs
:Criterion := Signature);
printf "Adding %o new saturated sets\n", #new_satsets;
SS cat:= new_satsets;
SS := Setseq(Seqset(SS)); /* Eliminate duplicates */
while not IsEmpty(SS) do
printf "#SS=%o\n", #SS;
if Signature then
Sort(~SS,func<S1,S2 | Sig_Compare(Sig_OfSatSet(S1,LMs,sigs),
Sig_OfSatSet(S2,LMs,sigs))>);
end if;
/* If we are using the signature algorithm, SS is sorted by increasing signatures */
S := SS[1];
Remove(~SS,1);
print "S=", S;
if Signature then
sigS,smax := Sig_OfSatSet(S,LMs,sigs);
ss := smax[1];
else
ss := Max(S);
end if;
if #S eq 1 then
continue;
end if;
cnt_satsets +:= 1;
XS := SatSet_lcm(S,LMs);
/* ss := Max(S); */
f_current := pols[ss];
lm_current := LeadingMonomial(f_current);
Srest := [j : j in S | j ne ss];
LC_S := [LCs[j] : j in Srest];
gens := SatIdeal(LC_S,LCs[ss]);
for b_S in gens do
if Signature then
SigG := Sig_Multiply(sigS,b_S,1);
printf "Signature of saturated set: %o\n",
Sig_ToString(SigG);
if Sing_Criterion and exists{s : s in sigs | Sig_Eq(s,SigG)} then
printf "Polynomial excluded by singular criterion\n";
cnt_sing_pairs +:= 1;
continue;
elif F5_Criterion
and not Sig_F5Criterion(SigG,ss,LMs,LCs,sigs,
interm_ideals,funs) then
printf "Polynomial excluded by F5 criterion\n";
cnt_F5_pairs +:= 1;
continue;
end if;
else
SigG := 0; // Arbitrary, for the call to reduce
end if;
cnt_vectsets +:= 1;
bb := LinDecomp(LC_S,b_S*LCs[ss]);
g := b_S*(XS div lm_current) * f_current;
/* print LC_S,S; */
for j in [1..#LC_S] do
ii := Srest[j];
g -:= bb[j]
*(XS div LeadingMonomial(pols[ii]))
*pols[ii];
end for;
gg := Moller_Reduce(g,SigG,ss,pols,sigs,LMs,LCs,funs
: Criterion := Signature);
if gg eq 0 then
/* Regular syzygy */
printf "Reduction to 0\n";
cnt_red0 +:= 1;
//error "";
elif Signature and OneSingularReducible(gg,SigG,sigs,LMs)
then
/* 1-Singular reducible */
printf "1-singular reducible: %o\n", gg;
cnt_1sing_red +:= 1;
else
/* Otherwise: added to the basis */
if Signature then
printf "Adding %o with signature (%o,%o,%o)\n",
gg, SigG`k,SigG`mu,SigG`i;
end if;
Append(~pols,gg);
if Signature then
Append(~sigs,SigG);
end if;
Append(~LMs, LeadingMonomial(gg));
Append(~LCs, LeadingCoefficient(gg));
new_satsets :=
SatSets_Generate_maybe_regular(#pols,LMs,sigs
:Criterion := Signature);
printf "Adding %o new saturated sets\n", #new_satsets;
SS cat:= new_satsets;
SS := Setseq(Seqset(SS)); /* Eliminate duplicates */
end if;
end for;
end while;
end for;
printf "Total # of vectorsets: %o\n", cnt_vectsets;
printf "Total # of saturated sets: %o\n", cnt_satsets;
printf "Total # of reductions to 0: %o\n", cnt_red0;
printf "Total # of skipped singular S-pairs: %o\n", cnt_sing_pairs;
printf "Total # of skipped F5 S-pairs: %o\n", cnt_F5_pairs;
printf "Total # of skipped 1-singular-reducible pols: %o\n", cnt_1sing_red;
return pols,sigs;
end function;
function Moller_ReduceGB(G,funs)
/* Given a Gröbner basis, compute a reduced Gröbner basis */
finished := false;
while not finished do
while 0 in G do
Exclude(~G,0);
end while;
done_anything := false;
for i in [1..#G] do
rest := [j : j in [1..#G] | j ne i];
pols := [G[j] : j in rest];
LMs := [LeadingMonomial(G[j]) : j in rest];
LCs := [LeadingCoefficient(G[j]) : j in rest];
gg := Moller_Reduce(G[i],0,#G,pols,[],LMs,LCs,funs
: Criterion := false);
if gg ne G[i] then
done_anything := true;
G[i] := gg;
if gg eq 0 then
break;
end if;
end if;
end for;
if not done_anything then
finished := true;
end if;
end while;
return G;
end function;
/* Implementation of the ring procedures */
// Functions for euclidian rings
function Euclid_SatIdeal(F,g)
d := Gcd(F);
m := Lcm(d,g);
return [m div g];
end function;
function Euclid_CosetRep(F,g)
res := g mod (Gcd(F));
return res;
end function;
function Euclid_LinDecomp(F,g)
d,bb := ExtendedGreatestCommonDivisor(F);
if g mod d ne 0 then
error "Not divisible";
else
dd := g div d;
return [b*dd : b in bb];
end if;
end function;
// Functions for fields
function Field_SatIdeal(F,g)
return [1];
end function;
function Field_CosetRep(F,g)
return 0;
end function;
function Field_LinDecomp(F,g)
return [0 : i in [1..#F-1]] cat [g/F[#F]];
end function;
// Functions for multivariate polynomial rings
function Pol_SatIdeal(F,g)
return GroebnerBasis(ColonIdeal(Ideal(F),Ideal(g)));
end function;
/*
Behind the scenes:
I : (f) = 1/f (I cap (f))
I cap f = elim(t, t I + (1-t)f)
*/
function Pol_CosetRep(F,g)
return NormalForm(g,Ideal(F));
end function;
function Pol_LinDecomp(F,g)
I := IdealWithFixedBasis(F);
return Coordinates(I,g);
end function;
// Generic CosetRep function for rings without definite coset
// representatives
function Generic_CosetRep(F,g)
return g;
end function;