-
Notifications
You must be signed in to change notification settings - Fork 113
/
mpoly-graded.jl
2438 lines (1990 loc) · 72.7 KB
/
mpoly-graded.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
@attributes mutable struct MPolyDecRing{T, S} <: AbstractAlgebra.MPolyRing{T}
R::S
D::FinGenAbGroup
d::Vector{FinGenAbGroupElem}
lt::Any
function MPolyDecRing(R::S, d::Vector{FinGenAbGroupElem}) where {S}
@assert length(d) == ngens(R)
r = new{elem_type(base_ring(R)), S}()
r.R = R
r.D = parent(d[1])
r.d = d
return r
end
function MPolyDecRing(R::S, d::Vector{FinGenAbGroupElem}, lt) where {S}
@assert length(d) == ngens(R)
r = new{elem_type(base_ring(R)), S}()
r.R = R
r.D = parent(d[1])
r.d = d
r.lt = lt
return r
end
end
generator_degrees(S::MPolyDecRing) = S.d
@doc raw"""
grading_group(R::MPolyDecRing)
If `R` is, say, `G`-graded, then return `G`.
# Examples
```jldoctest
julia> R, (x, y, z) = graded_polynomial_ring(QQ, ["x", "y", "z"], [1, 2, 3])
(Graded multivariate polynomial ring in 3 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x, y, z])
julia> grading_group(R)
Z
```
"""
grading_group(R::MPolyDecRing) = R.D
function is_graded(R::MPolyDecRing)
return !isdefined(R, :lt)
end
_grading(R::MPolyDecRing) = R.d
@doc raw"""
is_graded(R::MPolyRing)
Return `true` if `R` is graded, `false` otherwise.
"""
is_graded(R::MPolyRing) = false
is_filtered(R::MPolyDecRing) = isdefined(R, :lt)
is_filtered(::MPolyRing) = false
function show(io::IO, ::MIME"text/plain", W::MPolyDecRing)
AbstractAlgebra.@show_name(io, W)
AbstractAlgebra.@show_special(io, W)
io = pretty(io)
R = forget_decoration(W)
print(io, R)
if is_filtered(W)
println(io, " filtrated by")
else
println(io, " graded by")
end
g = gens(R)
print(io, Indent())
for i = 1:ngens(R)
if i == ngens(R)
print(io, "$(g[i]) -> $(W.d[i].coeff)")
else
println(io, "$(g[i]) -> $(W.d[i].coeff)")
end
end
print(io, Dedent())
# println(IOContext(io, :compact => true, ), W.d)
end
function Base.show(io::IO, W::MPolyDecRing)
Hecke.@show_name(io, W)
Hecke.@show_special(io, W)
io = pretty(io)
if is_terse(io)
if is_filtered(W)
print(io, "Filtered multivariate polynomial ring")
else
print(io, "Graded multivariate polynomial ring")
end
else
R = forget_decoration(W)
if is_filtered(W)
print(io, "Filtered ", Lowercase(), R)
else
print(io, "Graded ", Lowercase(), R)
end
end
end
function decorate(R::MPolyRing)
A = abelian_group([0])
S = MPolyDecRing(R, [1*A[1] for i = 1: ngens(R)], (x,y) -> x[1] < y[1])
return S, map(R, gens(R))
end
@doc raw"""
grade(R::MPolyRing, W::AbstractVector{<:IntegerUnion})
Given a vector `W` of `ngens(R)` integers, create a free abelian group of type `FinGenAbGroup`
given by one free generator, and convert the entries of `W` to elements of that group. Then
create a $\mathbb Z$-graded ring by assigning the group elements as weights to the variables
of `R`, and return the new ring, together with the vector of variables.
grade(R::MPolyRing)
As above, where the grading is the standard $\mathbb Z$-grading on `R`.
# Examples
```jldoctest
julia> R, (x, y, z) = polynomial_ring(QQ, ["x", "y", "z"])
(Multivariate polynomial ring in 3 variables over QQ, QQMPolyRingElem[x, y, z])
julia> W = [1, 2, 3];
julia> S, (x, y, z) = grade(R, W)
(Graded multivariate polynomial ring in 3 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x, y, z])
julia> T, (x, y, z) = grade(R)
(Graded multivariate polynomial ring in 3 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x, y, z])
```
"""
function grade(R::MPolyRing, W::AbstractVector{<:IntegerUnion})
@assert length(W) == ngens(R)
A = abelian_group([0])
set_attribute!(A, :show_elem => show_special_elem_grad)
S = MPolyDecRing(R, [i*A[1] for i = W])
return S, map(S, gens(R))
end
function grade(R::MPolyRing)
A = abelian_group([0])
S = MPolyDecRing(R, [1*A[1] for i = 1: ngens(R)])
return S, map(S, gens(R))
end
@doc raw"""
grade(R::MPolyRing, W::AbstractVector{<:AbstractVector{<:IntegerUnion}})
Given a vector `W` of `ngens(R)` integer vectors of the same size `m`, say, create a free
abelian group of type `FinGenAbGroup` given by `m` free generators, and convert the vectors in
`W` to elements of that group. Then create a $\mathbb Z^m$-graded ring by assigning the group
elements as weights to the variables of `R`, and return the new ring, together with the vector
of variables.
grade(R::MPolyRing, W::Union{ZZMatrix, AbstractMatrix{<:IntegerUnion}})
As above, converting the columns of `W`.
# Examples
```jldoctest
julia> R, x, y = polynomial_ring(QQ, "x" => 1:2, "y" => 1:3)
(Multivariate polynomial ring in 5 variables over QQ, QQMPolyRingElem[x[1], x[2]], QQMPolyRingElem[y[1], y[2], y[3]])
julia> W = [1 1 0 0 0; 0 0 1 1 1]
2×5 Matrix{Int64}:
1 1 0 0 0
0 0 1 1 1
julia> grade(R, W)
(Graded multivariate polynomial ring in 5 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x[1], x[2], y[1], y[2], y[3]])
```
"""
function grade(R::MPolyRing, W::AbstractVector{<:AbstractVector{<:IntegerUnion}})
@assert length(W) == ngens(R)
n = length(W[1])
@assert all(x->length(x) == n, W)
A = abelian_group(zeros(Int, n))
set_attribute!(A, :show_elem => show_special_elem_grad)
S = MPolyDecRing(R, [A(w) for w = W])
return S, map(S, gens(R))
end
function grade(R::MPolyRing, W::Union{ZZMatrix, AbstractMatrix{<:IntegerUnion}})
@assert size(W, 2) == ngens(R)
A = abelian_group(zeros(Int, size(W, 1)))
set_attribute!(A, :show_elem => show_special_elem_grad)
###S = MPolyDecRing(R, [A(view(W, :, i)) for i = 1:size(W, 2)])
S = MPolyDecRing(R, [A(W[:, i]) for i = 1:size(W, 2)])
return S, map(S, gens(R))
end
@doc raw"""
is_standard_graded(R::MPolyDecRing)
Return `true` if `R` is standard $\mathbb Z$-graded, `false` otherwise.
# Examples
```jldoctest
julia> S, (x, y, z) = graded_polynomial_ring(QQ, ["x", "y", "z"]; weights = [1, 2, 3])
(Graded multivariate polynomial ring in 3 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x, y, z])
julia> is_standard_graded(S)
false
```
"""
function is_standard_graded(R::MPolyDecRing)
is_z_graded(R) || return false
A = grading_group(R)
W = R.d
for i = 1:length(W)
if W[i] != A[1]
return false
end
end
return true
end
is_standard_graded(::MPolyRing) = false
@doc raw"""
is_z_graded(R::MPolyDecRing)
Return `true` if `R` is $\mathbb Z$-graded, `false` otherwise.
!!! note
Writing `G = grading_group(R)`, we say that `R` is $\mathbb Z$-graded if
`G` is free abelian of rank `1`, and `ngens(G) == 1`.
# Examples
```jldoctest
julia> S, (x, y, z) = graded_polynomial_ring(QQ, ["x", "y", "z"]; weights = [1, 2, 3])
(Graded multivariate polynomial ring in 3 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x, y, z])
julia> is_z_graded(S)
true
```
"""
function is_z_graded(R::MPolyDecRing)
is_graded(R) || return false
A = grading_group(R)
return ngens(A) == 1 && torsion_free_rank(A) == 1 && is_free(A)
end
is_z_graded(::MPolyRing) = false
@doc raw"""
is_zm_graded(R::MPolyDecRing)
Return `true` if `R` is $\mathbb Z^m$-graded for some $m$, `false` otherwise.
!!! note
Writing `G = grading_group(R)`, we say that `R` is $\mathbb Z^m$-graded
`G` is free abelian of rank `m`, and `ngens(G) == m`.
# Examples
```jldoctest
julia> G = abelian_group([0, 0, 2, 2])
Finitely generated abelian group
with 4 generators and 4 relations and relation matrix
[0 0 0 0]
[0 0 0 0]
[0 0 2 0]
[0 0 0 2]
julia> W = [G[1]+G[3]+G[4], G[2]+G[4], G[1]+G[3], G[2], G[1]+G[2]];
julia> S, x = graded_polynomial_ring(QQ, "x" => 1:5; weights=W)
(Graded multivariate polynomial ring in 5 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x[1], x[2], x[3], x[4], x[5]])
julia> is_zm_graded(S)
false
julia> G = abelian_group(ZZMatrix([1 -1]));
julia> g = gen(G, 1)
Abelian group element [0, 1]
julia> W = [g, g, g, g];
julia> R, (w, x, y, z) = graded_polynomial_ring(QQ, ["w", "x", "y", "z"], W);
julia> is_free(G)
true
julia> is_zm_graded(R)
false
```
"""
function is_zm_graded(R::MPolyDecRing)
is_graded(R) || return false
A = grading_group(R)
return is_free(A) && ngens(A) == torsion_free_rank(A)
end
is_zm_graded(::MPolyRing) = false
@doc raw"""
is_positively_graded(R::MPolyDecRing)
Return `true` if `R` is positively graded, `false` otherwise.
!!! note
We say that `R` is *positively graded* by a finitely generated abelian group $G$ if the coefficient ring of `R` is a field,
$G$ is free, and each graded part $R_g$, $g\in G$, has finite dimension.
# Examples
```jldoctest
julia> S, (t, x, y) = graded_polynomial_ring(QQ, ["t", "x", "y"]; weights = [-1, 1, 1])
(Graded multivariate polynomial ring in 3 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[t, x, y])
julia> is_positively_graded(S)
false
julia> G = abelian_group([0, 2])
Finitely generated abelian group
with 2 generators and 2 relations and relation matrix
[0 0]
[0 2]
julia> W = [gen(G, 1)+gen(G, 2), gen(G, 1)]
2-element Vector{FinGenAbGroupElem}:
[1, 1]
[1, 0]
julia> S, (x, y) = graded_polynomial_ring(QQ, ["x", "y"]; weights = W)
(Graded multivariate polynomial ring in 2 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x, y])
julia> is_positively_graded(S)
false
```
"""
@attr Bool function is_positively_graded(R::MPolyDecRing)
@req coefficient_ring(R) isa AbstractAlgebra.Field "The coefficient ring must be a field"
is_graded(R) || return false
G = grading_group(R)
is_free(G) || return false
if ngens(G) == torsion_free_rank(G)
W = reduce(vcat, [x.coeff for x = R.d])
if is_positive_grading_matrix(transpose(W))
return true
end
end
try
homogeneous_component(R, zero(G))
catch e
if e isa ArgumentError && e.msg == "Polyhedron not bounded"
return false
else
rethrow(e)
end
end
return true
end
is_positively_graded(::MPolyRing) = false
@doc raw"""
graded_polynomial_ring(C::Ring, args...; weights, kwargs...)
Create a multivariate [`polynomial_ring`](@ref polynomial_ring(R, [:x])) with
coefficient ring `C` and variables as described by `args...` (using the exact
same syntax as `polynomial_ring`), and [`grade`](@ref) this ring
according to the data provided by the keyword argument `weights`.
Return the graded ring as an object of type `MPolyDecRing`, together with the variables.
If `weights` is omitted the grading is the standard $\mathbb Z$-grading, i.e. all variables are graded with weight `1`.
# Examples
```jldoctest
julia> W = [[1, 0], [0, 1], [1, 0], [4, 1]]
4-element Vector{Vector{Int64}}:
[1, 0]
[0, 1]
[1, 0]
[4, 1]
julia> R, x = graded_polynomial_ring(QQ, 4, :x; weights = W)
(Graded multivariate polynomial ring in 4 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x1, x2, x3, x4])
julia> S, (x, y, z) = graded_polynomial_ring(QQ, [:x, :y, :z]; weights = [1, 2, 3])
(Graded multivariate polynomial ring in 3 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x, y, z])
julia> T, x = graded_polynomial_ring(QQ, :x => 1:3)
(Graded multivariate polynomial ring in 3 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x[1], x[2], x[3]])
julia> T, x, y = graded_polynomial_ring(QQ, :x => 1:3, :y => (1:2, 1:2); weights=1:7)
(Graded multivariate polynomial ring in 7 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x[1], x[2], x[3]], MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[y[1, 1] y[1, 2]; y[2, 1] y[2, 2]])
```
"""
function graded_polynomial_ring(C::Ring, args...; weights=nothing, kwargs...)
if weights === nothing
# no weights kwarg given: for backwards compatibility also check if
# the last regular argument might be a weight vector and if so, use it.
if args[end] isa Union{Vector{<:IntegerUnion}, Vector{<:Vector{<:IntegerUnion}}, Matrix{<:IntegerUnion}, ZZMatrix, Vector{FinGenAbGroupElem}}
weights = args[end]
args = args[1:end-1]
end
end
# pass all arguments (except possibly the last one if it contains weights)
# on to polynomial_ring
R, v... = polynomial_ring(C, args...; kwargs...)
# if no weights were given as last argument or via the `weights` kwarg,
# then we now use a default value where we assign weight 1 to each variable
if weights === nothing
weights = ones(Int, nvars(R))
end
# TODO: MPolyDecRing resp. `grade` should support `cached` kwarg
S, = grade(R, weights)
# return a result of the same "shape" as that returned by polynomial_ring
return S, _map_recursive(S, v)...
end
# helper for graded_polynomial_ring
_map_recursive(S::NCRing, x::NCRingElem) = S(x)
_map_recursive(S::NCRing, a::AbstractArray) = map(x -> _map_recursive(S, x), a)
_map_recursive(S::NCRing, a::Tuple) = map(x -> _map_recursive(S, x), a)
filtrate(R::MPolyRing) = decorate(R)
function show_special_elem_grad(io::IO, a::FinGenAbGroupElem)
if get(io, :compact, false)
print(io, a.coeff)
else
print(io, "$(a.coeff)")
end
end
function filtrate(R::MPolyRing, v::Vector{Int})
A = abelian_group([0])
set_attribute!(A, :show_elem => show_special_elem_grad)
S = MPolyDecRing(R, [i*A[1] for i = v], (x,y) -> x[1] < y[1])
return S, map(S, gens(R))
end
function filtrate(R::MPolyRing, v::Vector{FinGenAbGroupElem}, lt)
S = MPolyDecRing(R, v, lt)
return S, map(S, gens(R))
end
@doc raw"""
grade(R::MPolyRing, W::Vector{FinGenAbGroupElem})
Given a vector `W` of `ngens(R)` elements of a finitely presented group `G`, say, create a
`G`-graded ring by assigning the entries of `W` as weights to the variables of `R`. Return
the new ring as an object of type `MPolyDecRing`, together with the vector of variables.
# Examples
```jldoctest
julia> R, (t, x, y) = polynomial_ring(QQ, ["t", "x", "y"])
(Multivariate polynomial ring in 3 variables over QQ, QQMPolyRingElem[t, x, y])
julia> typeof(R)
QQMPolyRing
julia> typeof(x)
QQMPolyRingElem
julia> G = abelian_group([0])
Z
julia> g = gen(G, 1)
Abelian group element [1]
julia> S, (t, x, y) = grade(R, [-g, g, g])
(Graded multivariate polynomial ring in 3 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[t, x, y])
julia> typeof(S)
MPolyDecRing{QQFieldElem, QQMPolyRing}
julia> S isa MPolyRing
true
julia> typeof(x)
MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}
julia> R, x, y = polynomial_ring(QQ, "x" => 1:2, "y" => 1:3)
(Multivariate polynomial ring in 5 variables over QQ, QQMPolyRingElem[x[1], x[2]], QQMPolyRingElem[y[1], y[2], y[3]])
julia> G = abelian_group([0, 0])
Z^2
julia> g = gens(G)
2-element Vector{FinGenAbGroupElem}:
[1, 0]
[0, 1]
julia> W = [g[1], g[1], g[2], g[2], g[2]];
julia> S, _ = grade(R, W)
(Graded multivariate polynomial ring in 5 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x[1], x[2], y[1], y[2], y[3]])
julia> typeof(x[1])
QQMPolyRingElem
julia> x = map(S, x)
2-element Vector{MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}}:
x[1]
x[2]
julia> y = map(S, y)
3-element Vector{MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}}:
y[1]
y[2]
y[3]
julia> typeof(x[1])
MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}
julia> R, x = polynomial_ring(QQ, "x" => 1:5)
(Multivariate polynomial ring in 5 variables over QQ, QQMPolyRingElem[x[1], x[2], x[3], x[4], x[5]])
julia> G = abelian_group([0, 0, 2, 2])
Finitely generated abelian group
with 4 generators and 4 relations and relation matrix
[0 0 0 0]
[0 0 0 0]
[0 0 2 0]
[0 0 0 2]
julia> g = gens(G);
julia> W = [g[1]+g[3]+g[4], g[2]+g[4], g[1]+g[3], g[2], g[1]+g[2]]
5-element Vector{FinGenAbGroupElem}:
[1, 0, 1, 1]
[0, 1, 0, 1]
[1, 0, 1, 0]
[0, 1, 0, 0]
[1, 1, 0, 0]
julia> S, x = grade(R, W)
(Graded multivariate polynomial ring in 5 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x[1], x[2], x[3], x[4], x[5]])
```
"""
function grade(R::MPolyRing, v::AbstractVector{FinGenAbGroupElem})
S = MPolyDecRing(R, v)
return S, map(S, gens(R))
end
mutable struct MPolyDecRingElem{T, S} <: MPolyRingElem{T}
f::S
parent
function MPolyDecRingElem(f::S, p) where {S}
r = new{elem_type(base_ring(f)), S}(f, p)
# if is_graded(p) && length(r) > 1
# if !is_homogeneous(r)
# error("element not homogeneous")
# end
#both wrong and undesired.
# end
return r
end
end
function Base.deepcopy_internal(f::MPolyDecRingElem{T, S}, dict::IdDict) where {T, S}
return MPolyDecRingElem(Base.deepcopy_internal(forget_decoration(f), dict), f.parent)
end
function show(io::IO, w::MPolyDecRingElem)
show(io, forget_decoration(w))
end
parent(a::MPolyDecRingElem{T, S}) where {T, S} = a.parent::MPolyDecRing{T, parent_type(S)}
symbols(R::MPolyDecRing) = symbols(forget_decoration(R))
number_of_variables(R::MPolyDecRing) = number_of_variables(forget_decoration(R))
elem_type(::Type{MPolyDecRing{T, S}}) where {T, S} = MPolyDecRingElem{T, elem_type(S)}
parent_type(::Type{MPolyDecRingElem{T, S}}) where {T, S} = MPolyDecRing{T, parent_type(S)}
(W::MPolyDecRing)() = MPolyDecRingElem(forget_decoration(W)(), W)
(W::MPolyDecRing)(i::Int) = MPolyDecRingElem(forget_decoration(W)(i), W)
(W::MPolyDecRing)(f::Singular.spoly) = MPolyDecRingElem(forget_decoration(W)(f), W)
### Coercion of elements of the underlying polynomial ring
# into the graded ring.
function (W::MPolyDecRing{S, T})(f::U) where {S, T, U<:MPolyRingElem}
if parent_type(U) === T
@assert forget_decoration(W) === parent(f)
return MPolyDecRingElem(f, W)
else
return W(forget_decoration(W)(f))
end
end
function (W::MPolyDecRing)(f)
return W(forget_decoration(W)(f))
end
function (W::MPolyDecRing{T})(c::Vector{T}, e::Vector{Vector{Int}}) where T
return W(forget_decoration(W)(c, e))
end
(W::MPolyDecRing)(g::MPolyDecRingElem; check::Bool=true) = MPolyDecRingElem(forget_decoration(W)(forget_decoration(g)), W)
one(W::MPolyDecRing) = MPolyDecRingElem(one(forget_decoration(W)), W)
zero(W::MPolyDecRing) = MPolyDecRingElem(zero(forget_decoration(W)), W)
################################################################################
#
# Binary operations
#
################################################################################
for T in [:(+), :(-), :(*)]
@eval ($T)(a::MPolyDecRingElem,
b::MPolyDecRingElem) = MPolyDecRingElem($T(forget_decoration(a), forget_decoration(b)), parent(a))
end
divexact(a::MPolyDecRingElem, b::MPolyDecRingElem; check::Bool=true) = MPolyDecRingElem(divexact(forget_decoration(a), forget_decoration(b); check=check), parent(a))
################################################################################
#
# Unitary operations
#
################################################################################
-(a::MPolyDecRingElem) = MPolyDecRingElem(-forget_decoration(a), parent(a))
################################################################################
#
# Binary ad hoc operations
#
################################################################################
divexact(a::MPolyDecRingElem, b::RingElem; check::Bool=true) = MPolyDecRingElem(divexact(forget_decoration(a), b; check=check), parent(a))
divexact(a::MPolyDecRingElem, b::Integer; check::Bool=true) = MPolyDecRingElem(divexact(forget_decoration(a), b; check=check), parent(a))
divexact(a::MPolyDecRingElem, b::Rational; check::Bool=true) = MPolyDecRingElem(divexact(forget_decoration(a), b; check=check), parent(a))
for T in [:(-), :(+)]
@eval ($T)(a::MPolyDecRingElem,
b::RingElem) = MPolyDecRingElem($(T)(forget_decoration(a), b), parent(a))
@eval ($T)(a::MPolyDecRingElem,
b::Integer) = MPolyDecRingElem($(T)(forget_decoration(a), b), parent(a))
@eval ($T)(a::MPolyDecRingElem,
b::Rational) = MPolyDecRingElem($(T)(forget_decoration(a), b), parent(a))
@eval ($T)(a::RingElem,
b::MPolyDecRingElem) = MPolyDecRingElem($(T)(a, forget_decoration(b)), b.parent)
@eval ($T)(a::Integer,
b::MPolyDecRingElem) = MPolyDecRingElem($(T)(a, forget_decoration(b)), b.parent)
@eval ($T)(a::Rational,
b::MPolyDecRingElem) = MPolyDecRingElem($(T)(a, forget_decoration(b)), b.parent)
end
function *(a::MPolyDecRingElem{T, S}, b::T) where {T <: RingElem, S}
return MPolyDecRingElem(forget_decoration(a) * b, parent(a))
end
function *(a::T, b::MPolyDecRingElem{T, S}) where {T <: RingElem, S}
return b * a
end
################################################################################
#
# Factoring, division, ...
#
################################################################################
function factor(x::MPolyDecRingElem)
R = parent(x)
D = Dict{elem_type(R), Int64}()
F = factor(forget_decoration(x))
n=length(F.fac)
#if n == 1
# return Fac(R(F.unit), D)
#else
for i in keys(F.fac)
push!(D, R(i) => Int64(F[i]))
end
return Fac(R(F.unit), D)
#end
end
function gcd(x::MPolyDecRingElem, y::MPolyDecRingElem)
R = parent(x)
return R(gcd(forget_decoration(x), forget_decoration(y)))
end
function div(x::MPolyDecRingElem, y::MPolyDecRingElem)
R = parent(x)
return R(div(forget_decoration(x), forget_decoration(y)))
end
function divrem(x::MPolyDecRingElem, y::MPolyDecRingElem)
R = parent(x)
q, r = divrem(forget_decoration(x), forget_decoration(y))
return R(q), R(r)
end
################################################################################
#
# Equality
#
################################################################################
==(a::MPolyDecRingElem, b::MPolyDecRingElem) = forget_decoration(a) == forget_decoration(b)
^(a::MPolyDecRingElem, i::Int) = MPolyDecRingElem(forget_decoration(a)^i, parent(a))
function mul!(a::MPolyDecRingElem, b::MPolyDecRingElem, c::MPolyDecRingElem)
return b*c
end
function addeq!(a::MPolyDecRingElem, b::MPolyDecRingElem)
return a+b
end
length(a::MPolyDecRingElem) = length(forget_decoration(a))
@doc raw"""
total_degree(f::MPolyDecRingElem)
Return the total degree of `f`.
Given a set of variables ``x = \{x_1, \ldots, x_n\}``, the *total degree* of a monomial ``x^\alpha=x_1^{\alpha_1}\cdots x_n^{\alpha_n}\in\text{Mon}_n(x)`` is the sum of the ``\alpha_i``. The *total degree* of a polynomial `f` is the maximum of the total degrees of its monomials.
!!! note
The notion of total degree does not dependent on weights given to the variables.
"""
total_degree(a::MPolyDecRingElem) = total_degree(forget_decoration(a))
AbstractAlgebra.monomial(a::MPolyDecRingElem, i::Int) = parent(a)(AbstractAlgebra.monomial(forget_decoration(a), i))
AbstractAlgebra.coeff(a::MPolyDecRingElem, i::Int) = AbstractAlgebra.coeff(forget_decoration(a), i)
AbstractAlgebra.term(a::MPolyDecRingElem, i::Int) = parent(a)(AbstractAlgebra.term(forget_decoration(a), i))
AbstractAlgebra.exponent_vector(a::MPolyDecRingElem, i::Int) = AbstractAlgebra.exponent_vector(forget_decoration(a), i)
AbstractAlgebra.exponent_vector(a::MPolyDecRingElem, i::Int, ::Type{T}) where T = AbstractAlgebra.exponent_vector(forget_decoration(a), i, T)
AbstractAlgebra.exponent(a::MPolyDecRingElem, i::Int, j::Int) = AbstractAlgebra.exponent(forget_decoration(a), i, j)
AbstractAlgebra.exponent(a::MPolyDecRingElem, i::Int, j::Int, ::Type{T}) where T = AbstractAlgebra.exponent(forget_decoration(a), i, j, T)
function has_weighted_ordering(R::MPolyDecRing)
grading_to_ordering = false
w_ord = degrevlex(gens(R)) # dummy, not used
# This is not meant to be exhaustive, there a probably more gradings which one
# can meaningfully translate into a monomial ordering
# However, we want to stick to global orderings.
if is_z_graded(R)
w = Int[ R.d[i].coeff[1] for i = 1:ngens(R) ]
if all(isone, w)
w_ord = degrevlex(gens(R))
grading_to_ordering = true
elseif all(x -> x > 0, w)
w_ord = wdegrevlex(gens(R), w)
grading_to_ordering = true
end
end
return grading_to_ordering, w_ord
end
@attr MonomialOrdering{T} function default_ordering(R::T) where {T<:MPolyDecRing}
fl, w_ord = has_weighted_ordering(R)
fl && return w_ord
return degrevlex(R)
end
function singular_poly_ring(R::MPolyDecRing; keep_ordering::Bool = false)
if !keep_ordering
return singular_poly_ring(forget_decoration(R), default_ordering(R))
end
return singular_poly_ring(forget_decoration(R); keep_ordering)
end
MPolyCoeffs(f::MPolyDecRingElem) = MPolyCoeffs(forget_decoration(f))
MPolyExponentVectors(f::MPolyDecRingElem) = MPolyExponentVectors(forget_decoration(f))
function push_term!(M::MPolyBuildCtx{<:MPolyDecRingElem{T, S}}, c::T, expv::Vector{Int}) where {T <: RingElement, S}
if iszero(c)
return M
end
len = length(M.poly.f) + 1
set_exponent_vector!(M.poly.f, len, expv)
setcoeff!(M.poly.f, len, c)
return M
end
function set_exponent_vector!(f::MPolyDecRingElem, i::Int, exps::Vector{Int})
f.f = set_exponent_vector!(forget_decoration(f), i, exps)
return f
end
function finish(M::MPolyBuildCtx{<:MPolyDecRingElem})
f = sort_terms!(M.poly.f)
f = combine_like_terms!(M.poly.f)
return parent(M.poly)(f)
end
function jacobian_matrix(f::MPolyDecRingElem)
R = parent(f)
n = nvars(R)
return matrix(R, n, 1, [derivative(f, i) for i=1:n])
end
function jacobian_ideal(f::MPolyDecRingElem)
R = parent(f)
n = nvars(R)
return ideal(R, [derivative(f, i) for i=1:n])
end
function jacobian_matrix(g::Vector{<:MPolyDecRingElem})
R = parent(g[1])
n = nvars(R)
@assert all(x->parent(x) === R, g)
return matrix(R, n, length(g), [derivative(x, i) for i=1:n for x = g])
end
@doc raw"""
degree(f::MPolyDecRingElem)
Given a homogeneous element `f` of a graded multivariate ring, return the degree of `f`.
degree(::Type{Vector{Int}}, f::MPolyDecRingElem)
Given a homogeneous element `f` of a $\mathbb Z^m$-graded multivariate ring, return the degree of `f`, converted to a vector of integer numbers.
degree(::Type{Int}, f::MPolyDecRingElem)
Given a homogeneous element `f` of a $\mathbb Z$-graded multivariate ring, return the degree of `f`, converted to an integer number.
# Examples
```jldoctest
julia> G = abelian_group([0, 0, 2, 2])
Finitely generated abelian group
with 4 generators and 4 relations and relation matrix
[0 0 0 0]
[0 0 0 0]
[0 0 2 0]
[0 0 0 2]
julia> W = [G[1]+G[3]+G[4], G[2]+G[4], G[1]+G[3], G[2], G[1]+G[2]];
julia> S, x = graded_polynomial_ring(QQ, "x" => 1:5; weights=W)
(Graded multivariate polynomial ring in 5 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x[1], x[2], x[3], x[4], x[5]])
julia> f = x[2]^2+2*x[4]^2
x[2]^2 + 2*x[4]^2
julia> degree(f)
Abelian group element [0, 2, 0, 0]
julia> W = [[1, 0], [0, 1], [1, 0], [4, 1]]
4-element Vector{Vector{Int64}}:
[1, 0]
[0, 1]
[1, 0]
[4, 1]
julia> R, x = graded_polynomial_ring(QQ, ["x[1]", "x[2]", "x[3]", "x[4]"], W)
(Graded multivariate polynomial ring in 4 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x[1], x[2], x[3], x[4]])
julia> f = x[1]^4*x[2]+x[4]
x[1]^4*x[2] + x[4]
julia> degree(f)
[4 1]
julia> degree(Vector{Int}, f)
2-element Vector{Int64}:
4
1
julia> R, (x, y, z) = graded_polynomial_ring(QQ, ["x", "y", "z"], [1, 2, 3])
(Graded multivariate polynomial ring in 3 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x, y, z])
julia> f = x^6+y^3+z^2
x^6 + y^3 + z^2
julia> degree(f)
[6]
julia> typeof(degree(f))
FinGenAbGroupElem
julia> degree(Int, f)
6
julia> typeof(degree(Int, f))
Int64
```
"""
function degree(a::MPolyDecRingElem; check::Bool=true)
!check && !is_filtered(parent(a)) && return _degree_fast(a)
# TODO: Also provide a fast track for the filtered case.
@req !iszero(a) "Element must be non-zero"
W = parent(a)
w = W.D[0]
first = true
d = W.d
for c = MPolyExponentVectors(forget_decoration(a))
u = W.D[0]
for i=1:length(c)
u += c[i]*d[i]
end
if first
first = false
w = u
elseif is_filtered(W)
w = W.lt(w, u) ? u : w
else
w == u || error("element not homogeneous")
end
end
return w
end
function _degree_fast(a::MPolyDecRingElem)
f = forget_grading(a)
w = parent(a).d
z = zero(grading_group(parent(a)))
is_zero(f) && return z
for (c, e) in zip(coefficients(f), exponents(f))
!iszero(c) && return sum(b*w[i] for (i, b) in enumerate(e); init=z)
end
end
function degree(::Type{Int}, a::MPolyDecRingElem; check::Bool=true)
@assert is_z_graded(parent(a))
return Int(degree(a; check)[1])
end
function degree(::Type{Vector{Int}}, a::MPolyDecRingElem); check::Bool=true
@assert is_zm_graded(parent(a))
d = degree(a; check)
return Int[d[i] for i=1:ngens(parent(d))]
end
@doc raw"""
is_homogeneous(f::MPolyDecRingElem)
Given an element `f` of a graded multivariate ring, return `true` if `f` is homogeneous, `false` otherwise.
# Examples
```jldoctest
julia> R, (x, y, z) = graded_polynomial_ring(QQ, ["x", "y", "z"], [1, 2, 3])
(Graded multivariate polynomial ring in 3 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x, y, z])
julia> f = x^2+y*z
x^2 + y*z
julia> is_homogeneous(f)
false
julia> W = [1 2 1 0; 3 4 0 1]
2×4 Matrix{Int64}:
1 2 1 0
3 4 0 1
julia> S, (w, x, y, z) = graded_polynomial_ring(QQ, ["w", "x", "y", "z"], W)
(Graded multivariate polynomial ring in 4 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[w, x, y, z])
julia> F = w^3*y^3*z^3 + w^2*x*y^2*z^2 + w*x^2*y*z + x^3
w^3*y^3*z^3 + w^2*x*y^2*z^2 + w*x^2*y*z + x^3
julia> is_homogeneous(F)
true
```
"""
function is_homogeneous(F::MPolyDecRingElem)
D = parent(F).D
d = parent(F).d
S = Set{elem_type(D)}()
for c = MPolyExponentVectors(forget_decoration(F))
u = parent(F).D[0]
for i=1:length(c)
u += c[i]*d[i]
end
push!(S, u)
if length(S) > 1
return false
end
end
return true
end
@doc raw"""
homogeneous_components(f::MPolyDecRingElem{T, S}) where {T, S}
Given an element `f` of a graded multivariate ring, return the homogeneous components of `f`.
# Examples
```jldoctest
julia> R, (x, y, z) = graded_polynomial_ring(QQ, ["x", "y", "z"], [1, 2, 3])
(Graded multivariate polynomial ring in 3 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x, y, z])