-
Notifications
You must be signed in to change notification settings - Fork 63
/
Ideal.jl
2325 lines (2207 loc) · 72.6 KB
/
Ideal.jl
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###############################################################################
#
# Ideal.jl : Generic ideals over Euclidean domains
#
###############################################################################
###############################################################################
#
# Type and parent functions
#
###############################################################################
base_ring(S::IdealSet) = S.base_ring::base_ring_type(S)
base_ring(I::Ideal) = I.base_ring::base_ring_type(I)
function parent(I::Ideal)
R = base_ring(I)
return IdealSet{elem_type(R)}(R)
end
elem_type(::Type{IdealSet{S}}) where S <: RingElement = Ideal{S}
parent_type(::Type{Ideal{S}}) where S <: RingElement = IdealSet{S}
###############################################################################
#
# Basic manipulation
#
###############################################################################
@doc raw"""
gens(I::Ideal{T}) where T <: RingElement
Return a list of generators of the ideal `I` in reduced form and canonicalised.
"""
gens(I::Ideal{T}) where T <: RingElement = I.gens
###############################################################################
#
# Heap and nodes
#
###############################################################################
const IDEAL_MULTIV_DEBUG = false # whether to print debugging information
const poly_level = 1 # 1 = display leading terms only, 2 = display entire polynomials
const print_inactive = false # whether to print polynomials and node nums of inactive nodes
const print_prompt = false # whether to wait for a keypress between debug output
print_node_level = [0] # 0 = print only polynomials in nodes, 1 = print lattice of nodes
node_num = [0] # for enumerating nodes when printing
# The main node structure used in the lattice of leading monomials
# Each node contains a polynomial and a lattice of nodes shows which
# leading monomials are divisible by which others
# The nodes directly above a node (i.e. ones it divides) are given
# by the field `up` and then by `next.up`, `next.next.up`, etc.
# Nodes with polynomials having equal leading monomials are chained
# together via the `equal` field in a closed loop that eventually
# links back to the present node
# Only the nodes on the backbone of the lattice show divisibility
# via the `up` and `next` fields; `equal` nodes have the same
# divisibility relations with other nodes and so the information
# is not duplicated for these
# When reduction occurs, the best reducer node is first attached
# to the node at the `reducer` field
# Nodes are marked as inactive by setting the `active` field to
# `false` if they are no removed from the basis/lattice
# The inactive nodes are retained as potential reducers in the
# lattice, and possibly purged at regular intervals
# One does not have to check if a node can be reduced by a node
# that was already in the lattice before the last lot of reductions
# if the node could not be reduced by anything at the time of the
# last round of reductions, but if a node is new since the last
# round then it is marked as such by setting `new_node` to true
# since it could potentially be a reducer or reduced by anything
# There are various kinds of reduction possible, including having
# the leading coefficient removed and having the leading coefficient
# reduced and nodes that had no reducers of a given kind at a given
# round are marked as such via the `settled` field so that they are
# not checked for this against old nodes again
# The leading monomial of a node is stored for easy access in the
# `lm` field and the least common multiple of all leading monomials
# in the subtree rooted at a given node are also stored in that node
# The `path` flag is used to mark visited nodes when traversing the
# lattice
# Each node is given a unique number so they can be printed easily
# The size of a node with respect the heuristic used to sort
# potential reducers by `size` is cached on the node in the `size`
# field
mutable struct lmnode{U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N}
poly::Union{U, Nothing}
up::Union{lmnode{U, V, N}, Nothing} # out (divisible leading monomials)
next::Union{lmnode{U, V, N}, Nothing} # extend current node so out-degree can be > 1
equal::Union{lmnode{U, V, N}, Nothing} # to chain nodes with equal lm
reducer::Union{lmnode{U, V, N}, Nothing} # best reducer found so far for node
active::Bool # whether polynomial is still actively being reduced
new_node::Bool # whether the node was added to the lattice since last time
settled::Int # 0 = newly reduced, 1 = no reducers remove lc, 2 = none reduce lc, 3 = no reducers
new_settled::Int # used for temporary when computing settled
lm::NTuple{N, Int} # leading monomial as exponent vector
lcm::NTuple{N, Int} # lcm of lm's in tree rooted here, as exponent vector
path::Bool # used for marking paths to divisible nodes
num::Int # for counting nodes (used in printing)
size::Float64 # caches the size of the polynomial
function lmnode{U, V, N}(p::Union{U, Nothing}) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N}
node = new{U, V, N}(p, nothing, nothing, nothing, nothing, true, true, 0, 0)
node.size = 0.0
if p != nothing && !iszero(p)
if leading_coefficient(p) < 0
node.poly = -node.poly
end
node.lm = Tuple(exponent_vector(node.poly, 1))
node.lcm = node.lm
node.size = reducer_size(node)
end
node.equal = node
node.path = false
node_num[] += 1
node.num = node_num[]
return node::lmnode{U, V, N}
end
end
# compute the lcm of the leading monomials of the given nodes as a tuple
function lm_lcm(node1::lmnode{U, V, N}, node2::lmnode{U, V, N}) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N}
return Tuple(max(node1.lcm[i], node2.lcm[i]) for i in 1:N)
end
# return `true` if the leading monomial of `node2` divides the lcm of the
# leading monomials of the tree rooted at `node1`, i.e. return `true` if
# `node2` could divide some node in the tree rooted at `node1`
function lm_divides_lcm(node1::lmnode{U, V, N}, node2::lmnode{U, V, N}) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N}
for i = 1:N
if node2.lm[i] > node1.lcm[i]
return false
end
end
return true
end
# print "nothing" at up, next nodes which lead nowhere
function show_inner(io::IO, n::Nothing)
print(io, "nothing")
end
# print a polynomial, either just the leading monomial if the
# `poly_level` debug flag above is 1 and the whole thing
# otherwise
function print_poly(io::IO, p::MPolyRingElem)
if poly_level == 1
print(io, leading_term(p))
if length(p) > 1
print(io, "+...")
end
else
print(io, p)
end
end
# print a node and all its fields recursively, excluding
# inactive nodes if the debug flag `print_inactive` above
# is set to `false`
function show_inner(io::IO, n::lmnode)
if !n.path
print(io, "Node(")
if print_inactive || n.active
print(io, "p", n.num)
if print_inactive
if n.active
print(io, "(Y)")
else
print(io, "(N)")
end
end
print(io, "=")
print_poly(io, n.poly)
else
print(io, "_")
end
print(io, ", ")
show_inner(io, n.up)
print(io, ", ")
if n.next == nothing
print(io, "nothing")
else
if n.next.next != nothing
print(io, "n", n.num, ":[")
end
n2 = n.next
while n2 != nothing
show_inner(io, n2.up)
n2 = n2.next
if n2 != nothing
print(io, ", ")
end
end
if n.next.next != nothing
print(io, "]")
end
end
print(io, ", ")
if n.equal == n
print(io, "nothing")
else
if n.equal.equal != n
print(io, "e", n.num, ":[")
end
n2 = n.equal
while n2 != n
print(io, "e", n2.num)
if n2.active
print(io, "(Y)")
else
print(io, "(N)")
end
print(io, "=")
print_poly(io, n2.poly)
n2 = n2.equal
if n2 != n
print(io, ", ")
end
end
if n.equal.equal != n
print(io, "]")
end
end
print(io, ")")
else
print(io, "N", n.num)
end
n.path = true
end
# print a vector of nodes
function show(io::IO, B::Vector{lmnode{U, V, N}}) where {U, V, N}
if print_node_level[] == 0
BB = extract_gens(B)
print(io, "[")
for i = 1:length(BB) - 1
print_poly(io, BB[i])
print(io, ", ")
end
if !isempty(BB)
print_poly(io, BB[end])
end
print(io, "]")
else
print(io, "[")
for i in 1:length(B) - 1
show_inner(io, B[i])
print(io, ", ")
end
if !isempty(B)
show_inner(io, B[end])
end
print(io, "]")
clear_path(B)
end
end
# heap implementation for sorting polys by lm, head = smallest
# Defined in MPoly, repeated here for reference
# heapleft(i::Int) = 2i
# heapright(i::Int) = 2i + 1
# heapparent(i::Int) = div(i, 2)
function lm_precedes(node1::lmnode{U, :lex, N}, node2::lmnode{U, :lex, N}) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, N}
for i = 1:N
if node2.lm[i] < node1.lm[i]
return true
end
if node2.lm[i] > node1.lm[i]
return false
end
end
return true
end
function lm_precedes(node1::lmnode{U, :deglex, N}, node2::lmnode{U, :deglex, N}) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, N}
s1 = sum(node1.lm)
s2 = sum(node2.lm)
if s2 < s1
return true
end
if s2 > s1
return false
end
for i = 1:N
if node2.lm[i] < node1.lm[i]
return true
end
if node2.lm[i] > node1.lm[i]
return false
end
end
return true
end
function lm_precedes(node1::lmnode{U, :degrevlex, N}, node2::lmnode{U, :degrevlex, N}) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, N}
s1 = sum(node1.lm)
s2 = sum(node2.lm)
if s2 < s1
return true
end
if s2 > s1
return false
end
for i = N:-1:1
if node2.lm[i] > node1.lm[i]
return true
end
if node2.lm[i] < node1.lm[i]
return false
end
end
return true
end
function lm_divides(node1::lmnode{U, V, N}, node2::lmnode{U, V, N}) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N}
for i = 1:N
if node2.lm[i] > node1.lm[i]
return false
end
end
return true
end
function heapinsert!(heap::Vector{lmnode{U, V, N}}, node::lmnode{U, V, N}) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N}
@assert !iszero(node.poly)
i = n = length(heap) + 1
if i != 1 && lm_precedes(heap[1], node)
i = 1
else
@inbounds while (j = heapparent(i)) >= 1
if lm_precedes(heap[j], node)
i = j
else
break
end
end
end
push!(heap, node)
while n > i
heap[n] = heap[heapparent(n)]
n >>= 1
end
heap[i] = node
return nothing
end
function heappop!(heap::Vector{lmnode{U, V, N}}) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N}
s = length(heap)
x = heap[1]
i = 1
j = 2
@inbounds while j < s
if !lm_precedes(heap[j + 1], heap[j])
j += 1
end
heap[i] = heap[j]
i = j
j <<= 1
end
n = heap[s]
j = i >> 1
@inbounds while i > 1 && lm_precedes(heap[j], n)
heap[i] = heap[j]
i = j
j >>= 1
end
heap[i] = heap[s]
pop!(heap)
x.up = nothing
x.next = nothing
x.lm = Tuple(exponent_vector(x.poly, 1))
x.lcm = x.lm
return x
end
###############################################################################
#
# Ideal reduction for multivariates over Euclidean domain
#
###############################################################################
# Reduce coefficients of polynomial p starting from term with index `start`
# (numbered from 1). Used for `normal_form` and `tail_reduce`.
function reduce_coefficients(p::U, V::Vector{U}, start::Int) where U <: AbstractAlgebra.MPolyRingElem
n = start
len = length(p)
infl = [1 for i in 1:nvars(parent(p))]
while n <= len
for i = 1:length(V)
c = coeff(p, n)
mv = exponent_vector(V[i], 1)
mp = exponent_vector(p, n)
if max.(mv, mp) == mp # leading monomial divides
h = leading_coefficient(V[i]) # should be positive
q, r = AbstractAlgebra.divrem(c, h)
if !iszero(q)
shift = mp .- mv
u = inflate(V[i], shift, infl)
p -= q*u
if iszero(r)
n -= 1
break
end
end
end
end
n += 1
len = length(p)
end
return p
end
# return the polynomial `p` tail reduced by the given basis `V`
function tail_reduce(p::U, V::Vector{U}) where U <: AbstractAlgebra.MPolyRingElem
len = length(p)
if len <= 1
return p
end
return reduce_coefficients(p, V, 2)
end
# return the normal form of the polynomial `p` after reduction by
# the given basis `V`
# this is internal, the user facing version being given below
function normal_form(p::U, V::Vector{U}) where U <: AbstractAlgebra.MPolyRingElem
if iszero(p)
return zero(parent(p))
end
return reduce_coefficients(p, V, 1)
end
# given two nodes, return a node giving the spoly of the two nodes
function compute_spoly(f::T, g::T) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N, T <: lmnode{U, V, N}}
fc = leading_coefficient(f.poly)
gc = leading_coefficient(g.poly)
c = lcm(fc, gc)
llcm = max.(f.lm, g.lm)
infl = [1 for i in 1:N]
shiftf = llcm .- exponent_vector(f.poly, 1)
shiftg = llcm .- exponent_vector(g.poly, 1)
s = divexact(c, fc)*inflate(f.poly, shiftf, infl) - divexact(c, gc)*inflate(g.poly, shiftg, infl)
return lmnode{U, V, N}(s)
end
# given two nodes, return a node giving the gpoly of the two nodes
function compute_gpoly(f::T, g::T) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N, T <: lmnode{U, V, N}}
fc = leading_coefficient(f.poly)
gc = leading_coefficient(g.poly)
_, s, t = gcdx(fc, gc)
llcm = max.(f.lm, g.lm)
infl = [1 for i in 1:N]
shiftf = llcm .- exponent_vector(f.poly, 1)
shiftg = llcm .- exponent_vector(g.poly, 1)
g = s*inflate(f.poly, shiftf, infl) + t*inflate(g.poly, shiftg, infl)
return lmnode{U, V, N}(g)
end
# given two polynomials, return the spoly of the two
function spoly(f::T, g::T) where T <: MPolyRingElem
n = nvars(parent(f))
fc = leading_coefficient(f)
gc = leading_coefficient(g)
mf = exponent_vector(f, 1)
mg = exponent_vector(g, 1)
c = lcm(fc, gc)
llcm = max.(mf, mg)
infl = [1 for i in 1:n]
shiftf = llcm .- mf
shiftg = llcm .- mg
s = divexact(c, fc)*inflate(f, shiftf, infl) - divexact(c, gc)*inflate(g, shiftg, infl)
end
# given two polynomials, return the gpoly of the two
function gpoly(f::T, g::T) where T <: MPolyRingElem
n = nvars(parent(f))
fc = leading_coefficient(f)
gc = leading_coefficient(g)
mf = exponent_vector(f, 1)
mg = exponent_vector(g, 1)
_, s, t = gcdx(fc, gc)
llcm = max.(mf, mg)
infl = [1 for i in 1:n]
shiftf = llcm .- mf
shiftg = llcm .- mg
g = s*inflate(f, shiftf, infl) + t*inflate(g, shiftg, infl)
end
# used for sorting polynomials in final basis, first by leading monomial and
# then by leading coefficient
function isless_monomial_lc(p::U, q::U) where U <: AbstractAlgebra.MPolyRingElem{<:RingElement}
plm = leading_monomial(p)
qlm = leading_monomial(q)
if plm < qlm
return true
end
if plm == qlm && leading_coefficient(p) < leading_coefficient(q)
return true
end
return false
end
# heuristic for size of reducer polynomials (smaller is better), used to sort
# potential reducers
function reducer_size(f::T) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N, T <: lmnode{U, V, N}}
if f.size != 0.0
return f.size
end
s = 0.0
# heuristic really punishes polys with lots of terms
len = length(f.poly)
j = len
for i = 1:len
c = coeff(f.poly, i)
# larger coefficients are also somewhat punished
s += Base.log(ndigits(c; base=2))*j*j
j -= 1
end
return s
end
# returns the leading coefficient of a node for use in sorting
function leading_coefficient_size(f::T) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N, T <: lmnode{U, V, N}}
return leading_coefficient(f.poly)
end
# reduce the node `b` by the reducer attached to it, inactivating
# `b` if its leading term is removed, and putting any fragments in
# `H` and marking `b` for recomputation of spolys (by placing it in
# `S`) if it has been merely modified
function reduce_by_reducer(S::Vector{T}, H::Vector{T}, b::T) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N, T <: lmnode{U, V, N}}
reduced = false
if b.reducer != nothing
c = leading_coefficient(b.poly)
h = leading_coefficient(b.reducer.poly)
q, r = AbstractAlgebra.divrem(c, h)
coeff_divides = false
if iszero(r)
coeff_divides = true
end
if iszero(q) # need gcd polynomial
sp = compute_spoly(b, b.reducer)
g, s, t = gcdx(c, h)
infl = [1 for in in 1:N]
shift = exponent_vector(b.poly, 1) .- exponent_vector(b.reducer.poly, 1)
d = s*b.poly + t*inflate(b.reducer.poly, shift, infl)
q = divexact(c, g)
p = b.poly - q*d # reduce b by d (the gcd poly)
if leading_coefficient(d) < 0
d = -d
end
b.poly = d # replace b with gcd poly
b.size = 0.0 # update size
b.size = reducer_size(b)
b.new_node = true
# must recompute s-polys
push!(S, b)
if !iszero(p)
push!(H, lmnode{U, V, N}(p))
end
else
infl = [1 for in in 1:N]
shift = exponent_vector(b.poly, 1) .- exponent_vector(b.reducer.poly, 1)
d = b.poly - q*inflate(b.reducer.poly, shift, infl)
if coeff_divides
# leading coeff of reducer divides leading coeff of b.poly
if !iszero(d) # we have a fragment
push!(H, lmnode{U, V, N}(d))
end
b.active = false
else
# case 2: leading coeff is reduced
if leading_coefficient(d) < 0
d = -d
end
b.poly = d
b.size = 0.0 # update size
b.size = reducer_size(b)
b.new_node = true
# must recompute s-polys
push!(S, b)
end
end
reduced = true
end
b.reducer = nothing
return reduced
end
# reduce nodes in a tree depth first
# X is used as temporary space to sort equal nodes
# H is used for fragments that must be inserted into the basis
# S is used to mark nodes for computation of spolys when they cease
# being reduced
# returns true if some reduction actually occurred
function reduce_nodes(S::Vector{T}, H::Vector{T}, b::T, X::Vector{T}) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N, T <: lmnode{U, V, N}}
b.path = true
b.settled = b.new_settled
b.new_settled = 0
# depth first
reduced = false
if b.up != nothing
if !b.up.path
reduced |= reduce_nodes(S, H, b.up, X)
end
b2 = b
while b2.next != nothing
b2 = b2.next
if !b2.up.path
reduced |= reduce_nodes(S, H, b2.up, X)
end
end
end
# reduce nodes equal to b
# first sort nodes by leading coefficient
push!(X, b)
b2 = b
while b2.equal != b
b2 = b2.equal
b2.settled = b2.new_settled
b2.new_settled = 0
index = searchsortedfirst(X, b2, by=leading_coefficient_size, rev=true) # find index at which to insert x
insert!(X, index, b2)
end
# of the active nodes, remove ones which are the same up to units
filter!(x->x.active, X)
i = 1
len = length(X)
while i < len
c1 = leading_coefficient(X[i].poly)
j = i + 1
while j <= len && leading_coefficient(X[j].poly) == c1
if X[j].poly == X[i].poly || X[j] == -X[i].poly
X[j].active = false
deleteat!(X, j)
reduced = true
j -= 1
len -= 1
end
j += 1
end
i += 1
end
# do reductions
for b2 in X
reduced |= reduce_by_reducer(S, H, b2)
end
# clear nodes from temporary space X
while !isempty(X)
pop!(X)
end
return reduced
end
# actually performs the reductions which have been attached to nodes
# by best_reducer
# X is used to sort equal nodes
# H is used for fragments
# S is used to mark nodes for generation of spolys when they stop
# being reduced
# returns true if some reduction actually occurred
function reduce_nodes(S::Vector{T}, H::Vector{T}, B::Vector{T}, X::Vector{T}) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N, T <: lmnode{U, V, N}}
reduced = false
for b in B
if !b.path
reduced |= reduce_nodes(S, H, b, X)
end
end
return reduced
end
# given a node b and a list of potential reducers, find the
# best one which will remove the leading coefficient of `b`
# if it exists
# the current best reducer and whether it would remove
# the leading coefficient are also passed in
function find_best_divides(b::T, X::Vector{T}, best::Union{T, Nothing}, best_divides::Bool) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N, T <: lmnode{U, V, N}}
c = leading_coefficient(b.poly)
best_size = best == nothing ? 0.0 : reducer_size(best)
for i = length(X):-1:1
# poly can't be reduced by itself or by a node that will be reduced
# in this round
if X[i] != b && X[i].reducer == nothing
h = leading_coefficient(X[i].poly)
if divides(c, h)[1]
usable = true
# make sure a circular chain of reduction can't occur
if X[i].lm == b.lm
x2 = X[i]
while x2 != nothing
if x2 == b
usable = false
end
x2 = x2.reducer
end
end
# if there's currently no reducer or this one is better
# attach the reducer that has been found
# only reduce by nodes with same lm if active, else it
# could be removed by an equal node or fragments thereof
if usable && (c != h || X[i].active)
if best == nothing || !best_divides
best = X[i]
best_size = reducer_size(X[i])
best_divides = true
else
red_size = reducer_size(X[i])
if red_size < best_size
best = X[i]
best_size = red_size
best_divides = true
end
end
end
end
end
end
return best, best_divides
end
# given a node b and a list of potential reducers, find the
# best one which will reduce the leading coefficient of `b`
# if it exists
# the current best reducer and whether it would reduce
# the leading coefficient are also passed in
function find_best_reduces(b::T, X::Vector{T}, best::Union{T, Nothing}, best_reduces::Bool) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N, T <: lmnode{U, V, N}}
c = leading_coefficient(b.poly)
best_size = best == nothing ? 0.0 : reducer_size(best)
for i = length(X):-1:1
# poly can't be reduced by itself or a node that will be reduced
# in this round
if X[i] != b && X[i].active && X[i].reducer == nothing
h = AbstractAlgebra.mod(c, leading_coefficient(X[i].poly))
if h != c && h != 0 && !divides(leading_coefficient(X[i].poly), c)[1]
usable = true
# check there are no cycles of reducers
if X[i].lm == b.lm
x2 = X[i]
while x2 != nothing
if x2 == b
usable = false
end
x2 = x2.reducer
end
end
# if node currently doesn't have a reducer or this one is
# better, attach the reducer just found
if usable
if best == nothing || !best_reduces
best = X[i]
best_size = reducer_size(X[i])
best_reduces = true
else
red_size = reducer_size(X[i])
if red_size < best_size
best = X[i]
best_size = red_size
best_reduces = true
end
end
end
end
end
end
return best, best_reduces
end
# given a node b and a list of potential reducers, find the
# best one which will give a smaller leading coefficient if
# we gcd with the leading coefficient of `b` if such exists
# the current best reducer and whether it would gcd to reduce
# the leading coefficient are also passed in
function find_best_gcd(b::T, X::Vector{T}, best::Union{T, Nothing}, best_is_gcd::Bool) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N, T <: lmnode{U, V, N}}
c = leading_coefficient(b.poly)
best_size = best == nothing ? 0.0 : reducer_size(best)
for i = length(X):-1:1
# poly can't be reduced by itself or by node that will be reduced
# in this round
if X[i] != b && X[i].active && X[i].reducer == nothing
if !divides(leading_coefficient(X[i].poly), c)[1] &&
!divides(c, leading_coefficient(X[i].poly))[1]
usable = true
# check there are no cycles of reducers
if X[i].lm == b.lm
x2 = X[i]
while x2 != nothing
if x2 == b
usable = false
end
x2 = x2.reducer
end
end
# if node currently doesn't have a reducer or this one is
# better, attach the reducer just found
if usable
if best == nothing || !best_is_gcd
best = X[i]
best_size = reducer_size(X[i])
best_is_gcd = true
else
red_size = reducer_size(X[i])
if red_size < best_size
best = X[i]
best_size = red_size
best_is_gcd = true
end
end
end
end
end
end
return best, best_is_gcd
end
# used for heuristics, to select one kind of reducer over another preferentially
# current strategy is to prefer reducer which gives degree reduction over
# everything else and reduction of leading coefficient after that
# the function takes a list of potential reducers (i.e. nodes from further down
# the tree), with the ones that are new since last round being in `Xnew`
function find_best_reducer(b::T, X::Vector{T}, Xnew::Vector{T}) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N, T <: lmnode{U, V, N}}
# prevent nodes that are not active from being reduced
if !b.active
return nothing
end
c = leading_coefficient(b.poly)
best = b.reducer
# 1. check for leading coefficient that divides c
best_divides = best == nothing ? false : divides(c, leading_coefficient(best.poly))[1]
if b.settled == 0
best, best_divides = find_best_divides(b, X, best, best_divides)
end
best, best_divides = find_best_divides(b, Xnew, best, best_divides)
if best_divides
b.new_settled = 0
return best
else
b.new_settled = 1
end
# 2. check for leading coefficient that reduces c
best_reduces = best == nothing ? false : AbstractAlgebra.mod(c, leading_coefficient(best.poly)) != c
if b.settled <= 1
best, best_reduces = find_best_reduces(b, X, best, best_reduces)
end
best, best_reduces = find_best_reduces(b, Xnew, best, best_reduces)
if best_reduces
return best
else
b.new_settled = 2
end
# 3. check for leading coefficient that is not divisible by c (gcd poly possible)
best_is_gcd = best == nothing ? false : !divides(leading_coefficient(best.poly), c)[1]
if b.settled <= 2
best, best_is_gcd = find_best_gcd(b, X, best, best_is_gcd)
end
best, best_is_gcd = find_best_gcd(b, Xnew, best, best_is_gcd)
if best_is_gcd
return best
else
b.new_settled = 3 # no reducer found
end
return best
end
# attach best reducer to each node in tree
# `X` and `Xnew` will be used to make a list of potential
# reducers (i.e. nodes further down in tree), with `Xnew`
# being those that are new since last round
function best_reducer(b::T, X::Vector{T}, Xnew::Vector{T}) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N, T <: lmnode{U, V, N}}
# insert nodes equal to b in X
b2 = b
oldlen = length(X)
newlen = length(Xnew)
bend = nothing
while b2 != bend
if b2.new_node
push!(Xnew, b2)
else
push!(X, b2)
end
b2 = b2.equal
bend = b
end
# set best reducer of B for each node equal to b
b.reducer = find_best_reducer(b, X, Xnew)
b2 = b.equal
while b2 != b
b2.reducer = find_best_reducer(b2, X, Xnew)
b2 = b2.equal
end
# recurse to b.up, b.next etc
if b.up != nothing
best_reducer(b.up, X, Xnew)
b2 = b
while b2.next != nothing
b2 = b2.next
best_reducer(b2.up, X, Xnew)
end
end
# remove nodes equal to b from X and Xnew
while length(X) != oldlen
pop!(X)
end
while length(Xnew) != newlen
d = pop!(Xnew)
d.new_node = false
end
end
# Setup for one round of reduction
# Each node will be reduced mod the leading coeff of the best node
# whose leading term divides it and if there are none, by the
# best node whose leading monomial divides it and if none exists
# gcd polynomials will be created if possible
# The reductions are not actually done here; the best reducer
# is just attached to the node
# The potential reducers along the current path so far are given by
# the array `X` unless they are for new nodes since last time, in
# which case they will go in `Xnew`
function best_reducer(B::Vector{T}, X::Vector{T}, Xnew::Vector{T}) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N, T <: lmnode{U, V, N}}
for b in B
best_reducer(b, X, Xnew)
end
end
# insert fragments from `H` into basis `B`
# they are sorted by `size` of leading monomial and only
# those below a current bound are inserted
# nodes are marked for generation of spolys when they stop
# being reduced, by placing them in `S`
function insert_fragments(S::Vector{T}, B::Vector{T}, H::Vector{T}, bound::NTuple{N, Int}) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N, T <: lmnode{U, V, N}}
sort!(H, by=x->sum(max.(x.lm, bound) .- bound), rev=true)
while !isempty(H)
if max.(H[end].lm, bound) != bound
break
end
d = pop!(H)
basis_insert(S, B, d)
end
end
# insert a node into the given branch of basis
function basis_insert(b::T, d::T) where {U <: AbstractAlgebra.MPolyRingElem{<:RingElement}, V, N, T <: lmnode{U, V, N}}
if lm_divides(b, d) # divides in both directions, so equal
if d.active # avoid inserting polynomials that are a constant multiple of existing ones
found = false
c = leading_coefficient(d.poly)
if length(d.poly) == length(b.poly)
h = leading_coefficient(b.poly)
flag, q = divides(c, h)
if flag
found = d.poly == q*b.poly
end
end
d2 = b.equal
while !found && d2 != b
if length(d.poly) == length(d2.poly)
h = leading_coefficient(d2.poly)
flag, q = divides(c, h)
if flag
found = d.poly == q*d2.poly
end
end
d2 = d2.equal
end
if !found
d.equal = b.equal
b.equal = d
else
d.active = false
end
end
else # not equal to current node
if b.up != nothing
flag = lm_divides(d, b.up)
if flag && !b.up.path
basis_insert(b.up, d)
end
b2 = b
while b2.next != nothing
b2 = b2.next
if lm_divides(d, b2.up)
flag = true
if !b2.up.path
basis_insert(b2.up, d)
end
end
end
if !flag # not inserted yet
if b != d && d.active && d.settled != -1
n = lmnode{U, V, N}(nothing)
n.up = d
n.next = b.next