-
Notifications
You must be signed in to change notification settings - Fork 126
/
groebner.jl
1613 lines (1301 loc) · 52.8 KB
/
groebner.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
# groebner stuff #######################################################
@doc raw"""
groebner_assure(I::MPolyIdeal, complete_reduction::Bool = false, need_global::Bool = false)
groebner_assure(I::MPolyIdeal, ordering::MonomialOrdering, complete_reduction::Bool = false)
**Note**: Internal function, subject to change, do not use.
Given an ideal `I` in a multivariate polynomial ring this function assures that a
Gröbner basis w.r.t. the given monomial ordering is attached to `I` in `I.gb`.
It *currently* also ensures that the basis is defined on the Singular side in
`I.gb.S`, but this should not be relied upon: use `singular_assure(I.gb)` before
accessing `I.gb.S`.
# Examples
```jldoctest
julia> R,(x,y) = polynomial_ring(QQ, ["x","y"])
(Multivariate polynomial ring in 2 variables over QQ, QQMPolyRingElem[x, y])
julia> I = ideal([x*y-3*x,y^3-2*x^2*y])
ideal(x*y - 3*x, -2*x^2*y + y^3)
julia> Oscar.groebner_assure(I, degrevlex(R));
julia> I.gb[degrevlex(R)]
Gröbner basis with elements
1 -> x*y - 3*x
2 -> y^3 - 6*x^2
3 -> 2*x^3 - 9*x
with respect to the ordering
degrevlex([x, y])
```
"""
function groebner_assure(I::MPolyIdeal, complete_reduction::Bool = false, need_global::Bool = false)
if !isempty(I.gb)
for G in values(I.gb)
need_global || return G
is_global(G.ord) || continue
complete_reduction || return G
if !G.isReduced
I.gb[G.ord] = _compute_standard_basis(G, G.ord, true)
return I.gb[G.ord]
end
end
end
ord = default_ordering(base_ring(I))
(need_global <= is_global(ord)) || error("Monomial ordering must be global.")
I.gb[ord] = groebner_assure(I, ord, complete_reduction)
return I.gb[ord]
end
function groebner_assure(I::MPolyIdeal, ordering::MonomialOrdering, complete_reduction::Bool = false)
return get!(I.gb, ordering) do
_compute_standard_basis(I.gens, ordering, complete_reduction)
end
end
@doc raw"""
_compute_standard_basis(B::IdealGens; ordering::MonomialOrdering,
complete_reduction::Bool = false)
**Note**: Internal function, subject to change, do not use.
Given an `IdealGens` `B` and optional parameters `ordering` for a monomial ordering and `complete_reduction`
this function computes a Gröbner basis (if `complete_reduction = true` the reduced Gröbner basis) of the
ideal spanned by the elements in `B` w.r.t. the given monomial ordering `ordering`. The Gröbner basis is then
returned in `B.S`.
# Examples
```jldoctest
julia> R,(x,y) = polynomial_ring(QQ, ["x","y"])
(Multivariate polynomial ring in 2 variables over QQ, QQMPolyRingElem[x, y])
julia> A = Oscar.IdealGens([x*y-3*x,y^3-2*x^2*y])
Ideal generating system with elements
1 -> x*y - 3*x
2 -> -2*x^2*y + y^3
julia> B = Oscar._compute_standard_basis(A, degrevlex(R))
Gröbner basis with elements
1 -> x*y - 3*x
2 -> y^3 - 6*x^2
3 -> 2*x^3 - 9*x
with respect to the ordering
degrevlex([x, y])
```
"""
function _compute_standard_basis(B::IdealGens, ordering::MonomialOrdering, complete_reduction::Bool = false)
# incorrect one
singular_assure(B, ordering)
R = B.Sx
I = Singular.Ideal(R, gens(B.S)...)
i = Singular.std(I, complete_reduction = complete_reduction)
BA = IdealGens(B.Ox, i, complete_reduction)
# correct one (segfaults)
#gensSord = singular_generators(B, ordering)
#i = Singular.std(gensSord, complete_reduction = complete_reduction)
#BA = IdealGens(B.Ox, i, complete_reduction)
BA.isGB = true
BA.ord = ordering
if isdefined(BA, :S)
BA.S.isGB = true
end
return BA
end
# standard basis for non-global orderings #############################
@doc raw"""
standard_basis(I::MPolyIdeal; ordering::MonomialOrdering = default_ordering(base_ring(I)),
complete_reduction::Bool = false, algorithm::Symbol = :buchberger)
Return a standard basis of `I` with respect to `ordering`.
The keyword `algorithm` can be set to
- `:buchberger` (implementation of Buchberger's algorithm in *Singular*),
- `:hilbert` (implementation of a Hilbert driven Gröbner basis computation in *Singular*),
- `:fglm` (implementation of the FGLM algorithm in *Singular*), and
- `:f4` (implementation of Faugère's F4 algorithm in the *msolve* package).
!!! note
See the description of the functions `groebner_basis_hilbert_driven`, `fglm`,
and `f4` in the OSCAR documentation for some more details and for restrictions
on the input data when using these versions of the standard basis algorithm.
!!! note
The returned standard basis is reduced if `ordering` is `global` and `complete_reduction = true`.
# Examples
```jldoctest
julia> R,(x,y) = polynomial_ring(QQ, ["x","y"]);
julia> I = ideal([x*(x+1), x^2-y^2+(x-2)*y]);
julia> standard_basis(I, ordering = negdegrevlex(R))
Standard basis with elements
1 -> x
2 -> y
with respect to the ordering
negdegrevlex([x, y])
```
"""
function standard_basis(I::MPolyIdeal; ordering::MonomialOrdering = default_ordering(base_ring(I)),
complete_reduction::Bool = false, algorithm::Symbol = :buchberger)
complete_reduction && @assert is_global(ordering)
if haskey(I.gb, ordering) && (complete_reduction == false || I.gb[ordering].isReduced == true)
return I.gb[ordering]
end
if algorithm == :buchberger
if !haskey(I.gb, ordering)
I.gb[ordering] = _compute_standard_basis(I.gens, ordering, complete_reduction)
elseif complete_reduction == true
I.gb[ordering] = _compute_standard_basis(I.gb[ordering], ordering, complete_reduction)
end
elseif algorithm == :fglm
_compute_groebner_basis_using_fglm(I, ordering)
elseif algorithm == :hilbert
weights = _find_weights(gens(I))
if !any(iszero, weights)
J, target_ordering, hn = I, ordering, nothing
else
R = base_ring(I)
K = iszero(characteristic(R)) && !haskey(I.gb, degrevlex(R)) ? _mod_rand_prime(I) : I
S = base_ring(K)
gb = groebner_assure(K, degrevlex(S))
K_hom = homogenization(K, "w")
gb_hom = IdealGens((p -> homogenization(p, base_ring(K_hom))).(gens(gb)))
gb_hom.isGB = true
K_hom.gb[degrevlex(S)] = gb_hom
singular_assure(K_hom.gb[degrevlex(S)])
hn = hilbert_series(quo(base_ring(K_hom), K_hom)[1])[1]
J = homogenization(I, "w")
weights = ones(Int, ngens(base_ring(J)))
target_ordering = _extend_mon_order(ordering, base_ring(J))
end
GB = groebner_basis_hilbert_driven(J, destination_ordering=target_ordering,
complete_reduction=complete_reduction,
weights=weights,
hilbert_numerator=hn)
if base_ring(I) == base_ring(J)
I.gb[ordering] = GB
else
GB_dehom_gens = [dehomogenization(p, base_ring(I), 1) for p in gens(GB)]
I.gb[ordering] = IdealGens(GB_dehom_gens, ordering, isGB = true)
end
elseif algorithm == :f4
groebner_basis_f4(I, complete_reduction=complete_reduction)
end
return I.gb[ordering]
end
@doc raw"""
groebner_basis(I::MPolyIdeal;
ordering::MonomialOrdering = default_ordering(base_ring(I)),
complete_reduction::Bool = false, algorithm::Symbol = :buchberger)
If `ordering` is global, return a Gröbner basis of `I` with respect to `ordering`.
The keyword `algorithm` can be set to
- `:buchberger` (implementation of Buchberger's algorithm in *Singular*),
- `:hilbert` (implementation of a Hilbert driven Gröbner basis computation in *Singular*),
- `:fglm` (implementation of the FGLM algorithm in *Singular*), and
- `:f4` (implementation of Faugère's F4 algorithm in the *msolve* package).
!!! note
See the description of the functions `groebner_basis_hilbert_driven`, `fglm`,
and `f4` in the OSCAR documentation for some more details and for restrictions
on the input data when using these versions of the standard basis algorithm.
!!! note
The returned Gröbner basis is reduced if `complete_reduction = true`.
# Examples
```jldoctest
julia> R, (x, y, z) = polynomial_ring(QQ, ["x", "y", "z"]);
julia> I = ideal(R, [y-x^2, z-x^3]);
julia> G = groebner_basis(I)
Gröbner basis with elements
1 -> y^2 - x*z
2 -> x*y - z
3 -> x^2 - y
with respect to the ordering
degrevlex([x, y, z])
julia> elements(G)
3-element Vector{QQMPolyRingElem}:
-x*z + y^2
x*y - z
x^2 - y
julia> elements(G) == gens(G)
true
julia> groebner_basis(I, ordering = lex(R))
Gröbner basis with elements
1 -> y^3 - z^2
2 -> x*z - y^2
3 -> x*y - z
4 -> x^2 - y
with respect to the ordering
lex([x, y, z])
```
```jldoctest
julia> R, (x, y) = graded_polynomial_ring(QQ, ["x", "y"], [1, 3]);
julia> I = ideal(R, [x*y-3*x^4,y^3-2*x^6*y]);
julia> groebner_basis(I)
Gröbner basis with elements
1 -> 3*x^4 - x*y
2 -> 2*x^3*y^2 - 3*y^3
3 -> x*y^3
4 -> y^4
with respect to the ordering
wdegrevlex([x, y], [1, 3])
```
```jldoctest
julia> R, (x, y, z) = polynomial_ring(QQ, ["x", "y", "z"]);
julia> V = [3*x^3*y+x^3+x*y^3+y^2*z^2, 2*x^3*z-x*y-x*z^3-y^4-z^2,
2*x^2*y*z-2*x*y^2+x*z^2-y^4];
julia> I = ideal(R, V);
julia> G = groebner_basis(I, ordering = lex(R), algorithm = :fglm);
julia> length(G)
8
julia> total_degree(G[8])
34
julia> leading_coefficient(G[8])
-91230304237130414552564280286681870842473427917231798336639893796481988733936505735341479640589040146625319419037353645834346047404145021391726185993823650399589880820226804328750
```
"""
function groebner_basis(I::MPolyIdeal; ordering::MonomialOrdering = default_ordering(base_ring(I)), complete_reduction::Bool=false,
algorithm::Symbol = :buchberger)
is_global(ordering) || error("Ordering must be global")
return standard_basis(I, ordering=ordering, complete_reduction=complete_reduction, algorithm=algorithm)
end
@doc raw"""
groebner_basis_f4(I::MPolyIdeal, <keyword arguments>)
Compute a Gröbner basis of `I` with respect to `degrevlex` using Faugère's F4 algorithm.
See [Fau99](@cite) for more information.
!!! note
At current state only prime fields of characteristic `0 < p < 2^{31}` are supported.
# Possible keyword arguments
- `initial_hts::Int=17`: initial hash table size `log_2`.
- `nr_thrds::Int=1`: number of threads for parallel linear algebra.
- `max_nr_pairs::Int=0`: maximal number of pairs per matrix, only bounded by minimal degree if `0`.
- `la_option::Int=2`: linear algebra option: exact sparse-dense (`1`), exact sparse (`2`, default), probabilistic sparse-dense (`42`), probabilistic sparse(`44`).
- `eliminate::Int=0`: size of first block of variables to be eliminated.
- `complete_reduction::Bool=true`: compute a reduced Gröbner basis for `I`
- `info_level::Int=0`: info level printout: off (`0`, default), summary (`1`), detailed (`2`).
# Examples
```jldoctest
julia> R,(x,y,z) = polynomial_ring(GF(101), ["x","y","z"], ordering=:degrevlex)
(Multivariate polynomial ring in 3 variables over GF(101), fpMPolyRingElem[x, y, z])
julia> I = ideal(R, [x+2*y+2*z-1, x^2+2*y^2+2*z^2-x, 2*x*y+2*y*z-y])
ideal(x + 2*y + 2*z + 100, x^2 + 2*y^2 + 2*z^2 + 100*x, 2*x*y + 2*y*z + 100*y)
julia> groebner_basis_f4(I)
Gröbner basis with elements
1 -> x + 2*y + 2*z + 100
2 -> y*z + 82*z^2 + 10*y + 40*z
3 -> y^2 + 60*z^2 + 20*y + 81*z
4 -> z^3 + 28*z^2 + 64*y + 13*z
with respect to the ordering
degrevlex([x, y, z])
```
"""
function groebner_basis_f4(
I::MPolyIdeal;
initial_hts::Int=17,
nr_thrds::Int=1,
max_nr_pairs::Int=0,
la_option::Int=2,
eliminate::Int=0,
complete_reduction::Bool=true,
info_level::Int=0
)
AI = AlgebraicSolving.Ideal(I.gens.O)
AlgebraicSolving.groebner_basis(AI,
initial_hts = initial_hts,
nr_thrds = nr_thrds,
max_nr_pairs = max_nr_pairs,
la_option = la_option,
eliminate = eliminate,
complete_reduction = complete_reduction,
info_level = info_level)
vars = gens(base_ring(I))[eliminate+1:end]
ord = degrevlex(vars)
I.gb[ord] =
IdealGens(AI.gb[eliminate], ord, keep_ordering = false, isGB = true)
I.gb[ord].isReduced = complete_reduction
return I.gb[ord]
end
@doc raw"""
_compute_standard_basis_with_transform(B::IdealGens, ordering::MonomialOrdering, complete_reduction::Bool = false)
**Note**: Internal function, subject to change, do not use.
Given an `IdealGens` `B` and optional parameters `ordering` for a monomial ordering and `complete_reduction`
this function computes a standard basis (if `ordering` is a global monomial ordering and `complete_reduction = true`
the reduced Gröbner basis) of the ideal spanned by the elements in `B` w.r.t. the given monomial ordering `ordering`
and the transformation matrix from the ideal to the standard basis. Return value is a IdealGens together with a map.
# Examples
```jldoctest
julia> R, (x, y) = polynomial_ring(QQ, ["x", "y"])
(Multivariate polynomial ring in 2 variables over QQ, QQMPolyRingElem[x, y])
julia> A = Oscar.IdealGens([x*y-3*x,y^3-2*x^2*y])
Ideal generating system with elements
1 -> x*y - 3*x
2 -> -2*x^2*y + y^3
julia> B,m = Oscar._compute_standard_basis_with_transform(A, degrevlex(R))
(Ideal generating system with elements
1 -> x*y - 3*x
2 -> -6*x^2 + y^3
3 -> 6*x^3 - 27*x
with associated ordering
degrevlex([x, y]), [1 2*x -2*x^2+y^2+3*y+9; 0 1 -x])
```
"""
function _compute_standard_basis_with_transform(B::IdealGens, ordering::MonomialOrdering, complete_reduction::Bool = false)
istd, m = Singular.lift_std(singular_generators(B, ordering), complete_reduction = complete_reduction)
return IdealGens(B.Ox, istd), map_entries(x -> B.Ox(x), m)
end
@doc raw"""
standard_basis_with_transformation_matrix(I::MPolyIdeal;
ordering::MonomialOrdering = default_ordering(base_ring(I)),
complete_reduction::Bool=false)
Return a pair `G`, `T`, say, where `G` is a standard basis of `I` with respect to `ordering`, and `T`
is a transformation matrix from `gens(I)` to `G`. That is, `gens(I)*T == G`.
!!! note
The returned Gröbner basis is reduced if `ordering` is a global monomial odering and `complete_reduction = true`.
# Examples
```jldoctest
julia> R,(x,y) = polynomial_ring(QQ,["x","y"]);
julia> I = ideal([x*y^2-1,x^3+y^2+x*y]);
julia> G, T = standard_basis_with_transformation_matrix(I, ordering=neglex(R))
(Standard basis with elements
1 -> 1 - x*y^2
with respect to the ordering
neglex([x, y]), [-1; 0])
julia> gens(I)*T == gens(G)
true
```
"""
function standard_basis_with_transformation_matrix(I::MPolyIdeal; ordering::MonomialOrdering = default_ordering(base_ring(I)), complete_reduction::Bool = false)
complete_reduction && @assert is_global(ordering)
G, m = _compute_standard_basis_with_transform(I.gens, ordering, complete_reduction)
G.isGB = true
I.gb[ordering] = G
return G, m
end
@doc raw"""
groebner_basis_with_transformation_matrix(I::MPolyIdeal;
ordering::MonomialOrdering = default_ordering(base_ring(I)),
complete_reduction::Bool=false)
Return a pair `G`, `T`, say, where `G` is a Gröbner basis of `I` with respect to `ordering`, and `T`
is a transformation matrix from `gens(I)` to `G`. That is, `gens(I)*T == G`.
!!! note
The returned Gröbner basis is reduced if `complete_reduction = true`.
# Examples
```jldoctest
julia> R,(x,y) = polynomial_ring(QQ,["x","y"]);
julia> I = ideal([x*y^2-1,x^3+y^2+x*y]);
julia> G, T = groebner_basis_with_transformation_matrix(I)
(Gröbner basis with elements
1 -> x*y^2 - 1
2 -> x^3 + x*y + y^2
3 -> y^4 + x^2 + y
with respect to the ordering
degrevlex([x, y]), [1 0 -x^2-y; 0 1 y^2])
julia> gens(I)*T == gens(G)
true
```
"""
function groebner_basis_with_transformation_matrix(I::MPolyIdeal; ordering::MonomialOrdering = default_ordering(base_ring(I)), complete_reduction::Bool = false)
is_global(ordering) || error("Ordering must be global")
return standard_basis_with_transformation_matrix(I, ordering=ordering, complete_reduction=complete_reduction)
end
# syzygies #######################################################
@doc raw"""
syzygy_generators(G::Vector{<:MPolyRingElem})
Return generators for the syzygies on the polynomials given as elements of `G`.
# Examples
```jldoctest
julia> R, (x, y) = polynomial_ring(QQ, ["x", "y"])
(Multivariate polynomial ring in 2 variables over QQ, QQMPolyRingElem[x, y])
julia> S = syzygy_generators([x^3+y+2,x*y^2-13*x^2,y-14])
3-element Vector{FreeModElem{QQMPolyRingElem}}:
(-y + 14)*e[2] + (-13*x^2 + x*y^2)*e[3]
(-169*y + 2366)*e[1] + (-13*x*y + 182*x - 196*y + 2744)*e[2] + (13*x^2*y^2 - 2548*x^2 + 196*x*y^2 + 169*y + 338)*e[3]
(-13*x^2 + 196*x)*e[1] + (-x^3 - 16)*e[2] + (x^4*y + 14*x^4 + 13*x^2 + 16*x*y + 28*x)*e[3]
```
"""
function syzygy_generators(a::Vector{<:MPolyRingElem})
I = ideal(a)
singular_assure(I)
s = Singular.syz(I.gens.S)
F = free_module(parent(a[1]), length(a))
@assert rank(s) == length(a)
return [F(s[i]) for i=1:Singular.ngens(s)]
end
# leading ideal #######################################################
@doc raw"""
leading_ideal(G::Vector{T}; ordering::MonomialOrdering = default_ordering(parent(G[1])))
where T <: MPolyRingElem
Return the leading ideal of `G` with respect to `ordering`.
# Examples
```jldoctest
julia> R, (x, y) = polynomial_ring(QQ, ["x", "y"])
(Multivariate polynomial ring in 2 variables over QQ, QQMPolyRingElem[x, y])
julia> L = leading_ideal([x*y^2-3*x, x^3-14*y^5], ordering=degrevlex(R))
ideal(x*y^2, y^5)
julia> L = leading_ideal([x*y^2-3*x, x^3-14*y^5], ordering=lex(R))
ideal(x*y^2, x^3)
```
"""
function leading_ideal(G::Vector{T}; ordering::MonomialOrdering = default_ordering(parent(G[1]))) where { T <: MPolyRingElem }
return ideal(parent(G[1]), [leading_monomial(f; ordering = ordering) for f in G])
end
function leading_ideal(I::IdealGens{T}) where { T <: MPolyRingElem }
return ideal(base_ring(I), [leading_monomial(f; ordering = I.ord) for f in I])
end
function leading_ideal(I::IdealGens{T}, ordering::MonomialOrdering) where T <: MPolyRingElem
return ideal(base_ring(I), [leading_monomial(f; ordering = ordering) for f in I])
end
@doc raw"""
leading_ideal(I::MPolyIdeal; ordering::MonomialOrdering = default_ordering(base_ring(I)))
Return the leading ideal of `I` with respect to `ordering`.
# Examples
```jldoctest
julia> R, (x, y) = polynomial_ring(QQ, ["x", "y"])
(Multivariate polynomial ring in 2 variables over QQ, QQMPolyRingElem[x, y])
julia> I = ideal(R,[x*y^2-3*x, x^3-14*y^5])
ideal(x*y^2 - 3*x, x^3 - 14*y^5)
julia> L = leading_ideal(I, ordering=degrevlex(R))
ideal(x*y^2, x^4, y^5)
julia> L = leading_ideal(I, ordering=lex(R))
ideal(y^7, x*y^2, x^3)
```
"""
function leading_ideal(I::MPolyIdeal; ordering::MonomialOrdering = default_ordering(base_ring(I)))
G = standard_basis(I, ordering=ordering)
return ideal(base_ring(I), [leading_monomial(g; ordering = ordering) for g in G])
end
@doc raw"""
normal_form_internal(I::Singular.sideal, J::MPolyIdeal, o::MonomialOrdering)
**Note**: Internal function, subject to change, do not use.
Compute the normal form of the generators `gens(I)` of the ideal `I` w.r.t. a
Gröbner basis of `J` and the monomial ordering `o`.
CAVEAT: This computation needs a Gröbner basis of `J` and the monomial ordering
`o`. If this Gröbner basis is not available, one is computed automatically.
This may take some time.
# Examples
```jldoctest
julia> R,(a,b,c) = polynomial_ring(QQ,["a","b","c"])
(Multivariate polynomial ring in 3 variables over QQ, QQMPolyRingElem[a, b, c])
julia> J = ideal(R,[-1+c+b,-1+b+c*a+2*a*b])
ideal(b + c - 1, 2*a*b + a*c + b - 1)
julia> gens(groebner_basis(J))
2-element Vector{QQMPolyRingElem}:
b + c - 1
a*c - 2*a + c
julia> SR = singular_poly_ring(base_ring(J))
Singular Polynomial Ring (QQ),(x,y,z),(dp(3),C)
julia> I = Singular.Ideal(SR,[SR(-1+c+b+a^3),SR(-1+b+c*a+2*a^3),SR(5+c*b+c^2*a)])
Singular ideal over Singular Polynomial Ring (QQ),(x,y,z),(dp(3),C) with generators (x^3 + y + z - 1, 2*x^3 + x*z + y - 1, x*z^2 + y*z + 5)
julia> Oscar.normal_form_internal(I,J,default_ordering(base_ring(J)))
3-element Vector{QQMPolyRingElem}:
a^3
2*a^3 + 2*a - 2*c
4*a - 2*c^2 - c + 5
```
"""
function normal_form_internal(I::Singular.sideal, J::MPolyIdeal, o::MonomialOrdering)
groebner_assure(J, o)
G = J.gb[o]
R = base_ring(J)
SR = singular_poly_ring(R, o)
f = Singular.AlgebraHomomorphism(base_ring(I), SR, gens(SR))
IS = Singular.map_ideal(f, I)
GS = singular_generators(G, o)
K = ideal(base_ring(J), reduce(IS, GS))
return [J.gens.Ox(x) for x = gens(K.gens.S)]
end
@doc raw"""
reduce(I::IdealGens, J::IdealGens;
ordering::MonomialOrdering = default_ordering(base_ring(J)))
Return a `Vector` whose elements are the underlying elements of `I`
reduced by the underlying generators of `J` w.r.t. the monomial
ordering `ordering`. `J` need not be a Gröbner basis. The returned
`Vector` will have the same number of elements as `I`, even if they
are zero.
# Examples
```jldoctest
julia> R, (x, y, z) = polynomial_ring(GF(11), ["x", "y", "z"]);
julia> I = ideal(R, [x^2, x*y - y^2]);
julia> J = ideal(R, [y^3])
ideal(y^3)
julia> reduce(J.gens, I.gens)
1-element Vector{fpMPolyRingElem}:
y^3
julia> reduce(J.gens, groebner_basis(I))
1-element Vector{fpMPolyRingElem}:
0
julia> reduce(y^3, [x^2, x*y-y^3])
x*y
julia> reduce(y^3, [x^2, x*y-y^3], ordering=lex(R))
y^3
julia> reduce([y^3], [x^2, x*y-y^3], ordering=lex(R))
1-element Vector{fpMPolyRingElem}:
y^3
```
"""
function reduce(I::IdealGens, J::IdealGens; ordering::MonomialOrdering = default_ordering(base_ring(J)))
@assert base_ring(J) == base_ring(I)
Is = singular_generators(I, ordering)
Js = singular_generators(J, ordering)
res = reduce(Is, Js)
return [J.gens.Ox(x) for x = gens(res)]
end
@doc raw"""
reduce(g::T, F::Vector{T};
ordering::MonomialOrdering = default_ordering(parent(F[1]))) where T <: MPolyRingElem
If `ordering` is global, return the remainder in a standard representation for `g` on division by the polynomials in `F` with respect to `ordering`.
Otherwise, return the remainder in a *weak* standard representation for `g` on division by the polynomials in `F` with respect to `ordering`.
reduce(G::Vector{T}, F::Vector{T};
ordering::MonomialOrdering = default_ordering(parent(F[1]))) where T <: MPolyRingElem
Return a `Vector` which contains, for each element `g` of `G`, a remainder as above.
!!! note
In the global case, the returned remainders are fully reduced.
# Examples
```jldoctest
julia> R, (x, y) = polynomial_ring(QQ, ["x", "y"]);
julia> reduce(y^3, [x^2, x*y-y^3])
x*y
julia> reduce(y^3, [x^2, x*y-y^3], ordering = lex(R))
y^3
```
```jldoctest
julia> R, (z, y, x) = polynomial_ring(QQ, ["z", "y", "x"]);
julia> f1 = y-x^2; f2 = z-x^3;
julia> g = x^3*y-3*y^2*z^2+x*y*z;
julia> reduce(g, [f1, f2], ordering = lex(R))
-3*x^10 + x^6 + x^5
```
"""
function reduce(f::T, F::Vector{T}; ordering::MonomialOrdering = default_ordering(parent(f))) where {T <: MPolyRingElem}
@assert parent(f) == parent(F[1])
R = parent(f)
I = IdealGens(R, [f], ordering)
J = IdealGens(R, F, ordering)
redv = reduce(I, J, ordering=ordering)
return redv[1]
end
function reduce(F::Vector{T}, G::Vector{T}; ordering::MonomialOrdering = default_ordering(parent(F[1]))) where {T <: MPolyRingElem}
@assert parent(F[1]) == parent(G[1])
R = parent(F[1])
I = IdealGens(R, F, ordering)
J = IdealGens(R, G, ordering)
return reduce(I, J, ordering=ordering)
end
@doc raw"""
reduce_with_quotients_and_unit(g::T, F::Vector{T};
ordering::MonomialOrdering = default_ordering(parent(F[1]))) where T <: MPolyRingElem
Return the unit, the quotients and the remainder in a weak standard representation for `g` on division by the polynomials in `F` with respect to `ordering`.
reduce_with_quotients_and_unit(G::Vector{T}, F::Vector{T};
ordering::MonomialOrdering = default_ordering(parent(F[1]))) where T <: MPolyRingElem
Return a `Vector` which contains, for each element `g` of `G`, a unit, quotients, and a remainder as above.
!!! note
In the global case, a standard representation with a fully reduced remainder is computed.
# Examples
```jldoctest
julia> R, (x, y, z) = polynomial_ring(QQ, ["x", "y", "z"]);
julia> f1 = x^2+x^2*y; f2 = y^3+x*y*z; f3 = x^3*y^2+z^4;
julia> g = x^3*y+x^5+x^2*y^2*z^2+z^6;
julia> u, Q, h = reduce_with_quotients_and_unit(g, [f1,f2, f3], ordering = negdegrevlex(R))
([y+1], [x^3-x*y^2*z^2+x*y+y^2*z^2 0 y*z^2+z^2], 0)
julia> u*g == Q[1]*f1+Q[2]*f2+Q[3]*f3+h
true
julia> G = [g, x*y^3-3*x^2*y^2*z^2];
julia> U, Q, H = reduce_with_quotients_and_unit(G, [f1, f2, f3], ordering = lex(R));
julia> U
[1 0]
[0 1]
julia> H
2-element Vector{QQMPolyRingElem}:
-z^9 + z^7 + z^6 + z^4
-3*z^7 + z^6
julia> U*G == Q*[f1, f2, f3]+H
true
```
"""
function reduce_with_quotients_and_unit(f::T, F::Vector{T}; ordering::MonomialOrdering = default_ordering(parent(F[1]))) where {T <: MPolyRingElem}
@assert parent(f) == parent(F[1])
R = parent(f)
I = IdealGens(R, [f], ordering)
J = IdealGens(R, F, ordering)
u, q, r = _reduce_with_quotients_and_unit(I, J, ordering)
return u, q, r[1]
end
function reduce_with_quotients_and_unit(F::Vector{T}, G::Vector{T}; ordering::MonomialOrdering = default_ordering(parent(F[1]))) where {T <: MPolyRingElem}
@assert parent(F[1]) == parent(G[1])
R = parent(F[1])
I = IdealGens(R, F, ordering)
J = IdealGens(R, G, ordering)
return _reduce_with_quotients_and_unit(I, J, ordering)
end
@doc raw"""
reduce_with_quotients_and_unit(I::IdealGens, J::IdealGens;
ordering::MonomialOrdering = default_ordering(base_ring(J)))
Return a `Tuple` consisting of a `Generic.MatSpaceElem` `M`, a
`Vector` `res` whose elements are the underlying elements of `I`
reduced by the underlying generators of `J` w.r.t. the monomial
ordering `ordering` and a diagonal matrix `units` such that `M *
gens(J) + res == units * gens(I)`. If `ordering` is global then
`units` will always be the identity matrix, see also
`reduce_with_quotients`. `J` need not be a Gröbner basis. `res` will
have the same number of elements as `I`, even if they are zero.
# Examples
```jldoctest
julia> R, (x, y) = polynomial_ring(GF(11), ["x", "y"]);
julia> I = ideal(R, [x]);
julia> R, (x, y) = polynomial_ring(GF(11), ["x", "y"]);
julia> I = ideal(R, [x]);
julia> J = ideal(R, [x+1]);
julia> unit, M, res = reduce_with_quotients_and_unit(I.gens, J.gens, ordering = neglex(R))
([x+1], [x], fpMPolyRingElem[0])
julia> M * gens(J) + res == unit * gens(I)
true
julia> f = x^3*y^2-y^4-10
x^3*y^2 + 10*y^4 + 1
julia> F = [x^2*y-y^3, x^3-y^4]
2-element Vector{fpMPolyRingElem}:
x^2*y + 10*y^3
x^3 + 10*y^4
julia> reduce_with_quotients_and_unit(f, F)
([1], [x*y 10*x+1], x^4 + 10*x^3 + 1)
julia> unit, M, res = reduce_with_quotients_and_unit(f, F, ordering=lex(R))
([1], [0 y^2], y^6 + 10*y^4 + 1)
julia> M * F + [res] == unit * [f]
true
```
"""
function reduce_with_quotients_and_unit(I::IdealGens, J::IdealGens; ordering::MonomialOrdering = default_ordering(base_ring(J)))
return _reduce_with_quotients_and_unit(I, J, ordering)
end
@doc raw"""
reduce_with_quotients(I::IdealGens, J::IdealGens; ordering::MonomialOrdering = default_ordering(base_ring(J)))
Return a `Tuple` consisting of a `Generic.MatSpaceElem` `M` and a
`Vector` `res` whose elements are the underlying elements of `I`
reduced by the underlying generators of `J` w.r.t. the monomial
ordering `ordering` such that `M * gens(J) + res == gens(I)` if `ordering` is global.
If `ordering` is local then this equality holds after `gens(I)` has been multiplied
with an unknown diagonal matrix of units, see reduce_with_quotients_and_unit` to
obtain this matrix. `J` need not be a Gröbner basis. `res` will have the same number
of elements as `I`, even if they are zero.
# Examples
```jldoctest
julia> R, (x, y, z) = polynomial_ring(GF(11), ["x", "y", "z"]);
julia> J = ideal(R, [x^2, x*y - y^2]);
julia> I = ideal(R, [x*y, y^3]);
julia> gb = groebner_basis(J)
Gröbner basis with elements
1 -> x*y + 10*y^2
2 -> x^2
3 -> y^3
with respect to the ordering
degrevlex([x, y, z])
julia> M, res = reduce_with_quotients(I.gens, gb)
([1 0 0; 0 0 1], fpMPolyRingElem[y^2, 0])
julia> M * gens(gb) + res == gens(I)
true
julia> f = x^3*y^2-y^4-10
x^3*y^2 + 10*y^4 + 1
julia> F = [x^2*y-y^3, x^3-y^4]
2-element Vector{fpMPolyRingElem}:
x^2*y + 10*y^3
x^3 + 10*y^4
julia> reduce_with_quotients_and_unit(f, F)
([1], [x*y 10*x+1], x^4 + 10*x^3 + 1)
julia> unit, M, res = reduce_with_quotients_and_unit(f, F, ordering=lex(R))
([1], [0 y^2], y^6 + 10*y^4 + 1)
julia> M * F + [res] == unit * [f]
true
```
"""
function reduce_with_quotients(I::IdealGens, J::IdealGens; ordering::MonomialOrdering = default_ordering(base_ring(J)))
_, q, r = _reduce_with_quotients_and_unit(I, J, ordering)
return q, r
end
@doc raw"""
reduce_with_quotients(g::T, F::Vector{T};
ordering::MonomialOrdering = default_ordering(parent(F[1]))) where T <: MPolyRingElem
If `ordering` is global, return the quotients and the remainder in a standard representation for `g` on division by the polynomials in `F` with respect to `ordering`.
Otherwise, return the quotients and the remainder in a *weak* standard representation for `g` on division by the polynomials in `F` with respect to `ordering`.
reduce_with_quotients(G::Vector{T}, F::Vector{T};
ordering::MonomialOrdering = default_ordering(parent(F[1]))) where T <: MPolyRingElem
Return a `Vector` which contains, for each element `g` of `G`, quotients and a remainder as above.
!!! note
In the global case, the returned remainders are fully reduced.
# Examples
```jldoctest
julia> R, (z, y, x) = polynomial_ring(QQ, ["z", "y", "x"]);
julia> f1 = y-x^2; f2 = z-x^3;
julia> g = x^3*y-3*y^2*z^2+x*y*z;
julia> Q, h = reduce_with_quotients(g, [f1, f2], ordering = lex(R));
julia> Q
[-3*y*x^6 - 3*x^8 + x^4 + x^3 -3*z*y^2 - 3*y^2*x^3 + y*x]
julia> h
-3*x^10 + x^6 + x^5
julia> g == Q[1]*f1+Q[2]*f2+h
true
julia> G = [g, x*y^3-3*x^2*y^2*z^2];
julia> Q, H = reduce_with_quotients(G, [f1, f2], ordering = lex(R));
julia> Q
[ -3*y*x^6 - 3*x^8 + x^4 + x^3 -3*z*y^2 - 3*y^2*x^3 + y*x]
[y^2*x - 3*y*x^8 + y*x^3 - 3*x^10 + x^5 -3*z*y^2*x^2 - 3*y^2*x^5]
julia> H
2-element Vector{QQMPolyRingElem}:
-3*x^10 + x^6 + x^5
-3*x^12 + x^7
julia> G == Q*[f1, f2]+H
true
```
"""
function reduce_with_quotients(f::T, F::Vector{T}; ordering::MonomialOrdering = default_ordering(parent(F[1]))) where {T <: MPolyRingElem}
@assert parent(f) == parent(F[1])
R = parent(f)
I = IdealGens(R, [f], ordering)
J = IdealGens(R, F, ordering)
_, q, r = _reduce_with_quotients_and_unit(I, J, ordering)
return q, r[1]
end
function reduce_with_quotients(F::Vector{T}, G::Vector{T}; ordering::MonomialOrdering = default_ordering(parent(F[1]))) where {T <: MPolyRingElem}
@assert parent(F[1]) == parent(G[1])
R = parent(F[1])
I = IdealGens(R, F, ordering)
J = IdealGens(R, G, ordering)
_, q, r = _reduce_with_quotients_and_unit(I, J, ordering)
return q, r
end
function _reduce_with_quotients_and_unit(I::IdealGens, J::IdealGens, ordering::MonomialOrdering = default_ordering(base_ring(J)))
@assert base_ring(J) == base_ring(I)
sI = singular_generators(I, ordering)
sJ = singular_generators(J, ordering)
res = Singular.division(sI, sJ)
return matrix(base_ring(I), res[3]), matrix(base_ring(I), res[1]), [J.gens.Ox(x) for x = gens(res[2])]
end
@doc raw"""
normal_form(g::T, I::MPolyIdeal;
ordering::MonomialOrdering = default_ordering(base_ring(I))) where T <: MPolyRingElem
Compute the normal form of `g` mod `I` with respect to `ordering`.
normal_form(G::Vector{T}, I::MPolyIdeal;
ordering::MonomialOrdering = default_ordering(base_ring(I))) where T <: MPolyRingElem
Return a `Vector` which contains for each element `g` of `G` a normal form as above.
# Examples
```jldoctest
julia> R,(a,b,c) = polynomial_ring(QQ,["a","b","c"])
(Multivariate polynomial ring in 3 variables over QQ, QQMPolyRingElem[a, b, c])
julia> J = ideal(R,[-1+c+b,-1+b+c*a+2*a*b])
ideal(b + c - 1, 2*a*b + a*c + b - 1)
julia> gens(groebner_basis(J))
2-element Vector{QQMPolyRingElem}:
b + c - 1
a*c - 2*a + c
julia> normal_form(-1+c+b+a^3, J)
a^3
julia> R,(a,b,c) = polynomial_ring(QQ,["a","b","c"])
(Multivariate polynomial ring in 3 variables over QQ, QQMPolyRingElem[a, b, c])
julia> A = [-1+c+b+a^3,-1+b+c*a+2*a^3,5+c*b+c^2*a]
3-element Vector{QQMPolyRingElem}:
a^3 + b + c - 1
2*a^3 + a*c + b - 1
a*c^2 + b*c + 5
julia> J = ideal(R,[-1+c+b,-1+b+c*a+2*a*b])
ideal(b + c - 1, 2*a*b + a*c + b - 1)
julia> gens(groebner_basis(J))
2-element Vector{QQMPolyRingElem}:
b + c - 1
a*c - 2*a + c
julia> normal_form(A, J)
3-element Vector{QQMPolyRingElem}:
a^3
2*a^3 + 2*a - 2*c
4*a - 2*c^2 - c + 5
```
"""
function normal_form(f::T, J::MPolyIdeal; ordering::MonomialOrdering = default_ordering(base_ring(J))) where { T <: MPolyRingElem }
singular_assure(J)
SR = J.gens.Sx
I = Singular.Ideal(SR, SR(f))
N = normal_form_internal(I, J, ordering)
return N[1]
end
function normal_form(A::Vector{T}, J::MPolyIdeal; ordering::MonomialOrdering=default_ordering(base_ring(J))) where { T <: MPolyRingElem }
singular_assure(J)
SR = J.gens.Sx
I = Singular.Ideal(SR, [SR(x) for x in A])
normal_form_internal(I, J, ordering)