/
ModulesGraded.jl
3124 lines (2529 loc) · 89.6 KB
/
ModulesGraded.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
###############################################################################
# Graded Modules constructors
###############################################################################
@doc raw"""
graded_free_module(R::Ring, p::Int, W::Vector{FinGenAbGroupElem}=[grading_group(R)[0] for i in 1:p], name::String="e")
Given a graded ring `R` with grading group `G`, say,
and given a vector `W` with `p` elements of `G`, create the free module $R^p$
equipped with its basis of standard unit vectors, and assign weights to these
vectors according to the entries of `W`. Return the resulting graded free module.
graded_free_module(R::Ring, W::Vector{FinGenAbGroupElem}, name::String="e")
As above, with `p = length(W)`.
!!! note
The function applies to graded multivariate polynomial rings and their quotients.
The string `name` specifies how the basis vectors are printed.
# Examples
```jldoctest
julia> R, (x,y) = graded_polynomial_ring(QQ, ["x", "y"])
(Graded multivariate polynomial ring in 2 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x, y])
julia> graded_free_module(R,3)
Graded free module R^3([0]) of rank 3 over R
julia> G = grading_group(R)
Z
julia> graded_free_module(R, [G[1], 2*G[1]])
Graded free module R^1([-1]) + R^1([-2]) of rank 2 over R
```
"""
function graded_free_module(R::Ring, p::Int, W::Vector{FinGenAbGroupElem}=[grading_group(R)[0] for i in 1:p], name::String="e")
@assert length(W) == p
@assert is_graded(R)
all(x -> parent(x) == grading_group(R), W) || error("entries of W must be elements of the grading group of the base ring")
M = FreeMod(R, p, name)
M.d = W
return M
end
function graded_free_module(R::Ring, p::Int, W::Vector{Any}, name::String="e")
@assert length(W) == p
@assert is_graded(R)
p == 0 || error("W should be either an empty array or a Vector{FinGenAbGroupElem}")
W = FinGenAbGroupElem[]
return graded_free_module(R, p, W, name)
end
function graded_free_module(R::Ring, W::Vector{FinGenAbGroupElem}, name::String="e")
p = length(W)
return graded_free_module(R, p, W, name)
end
function graded_free_module(R::Ring, W::Vector{Any}, name::String="e")
p = length(W)
@assert is_graded(R)
p == 0 || error("W should be either an empty array or a Vector{FinGenAbGroupElem}")
W = FinGenAbGroupElem[]
return graded_free_module(R, p, W, name)
end
@doc raw"""
graded_free_module(R::Ring, W::Vector{<:Vector{<:IntegerUnion}}, name::String="e")
Given a graded ring `R` with grading group $G = \mathbb Z^m$,
and given a vector `W` of integer vectors of the same size `p`, say, create the free
module $R^p$ equipped with its basis of standard unit vectors, and assign weights to these
vectors according to the entries of `W`, converted to elements of `G`. Return the
resulting graded free module.
graded_free_module(R::Ring, W::Union{ZZMatrix, Matrix{<:IntegerUnion}}, name::String="e")
As above, converting the columns of `W`.
graded_free_module(R::Ring, W::Vector{<:IntegerUnion}, name::String="e")
Given a graded ring `R` with grading group $G = \mathbb Z$,
and given a vector `W` of integers, set `p = length(W)`, create the free module $R^p$
equipped with its basis of standard unit vectors, and assign weights to these
vectors according to the entries of `W`, converted to elements of `G`. Return
the resulting graded free module.
The string `name` specifies how the basis vectors are printed.
!!! note
The function applies to graded multivariate polynomial rings and their quotients.
# Examples
```jldoctest
julia> R, (x,y) = graded_polynomial_ring(QQ, ["x", "y"]);
julia> F = graded_free_module(R, [1, 2])
Graded free module R^1([-1]) + R^1([-2]) of rank 2 over R
```
```jldoctest
julia> S, (x, y, z) = graded_polynomial_ring(QQ, ["x", "y", "z"], [1 0 1; 0 1 1]);
julia> FF = graded_free_module(S, [[1, 2], [-1, 3]])
Graded free module S^1([-1 -2]) + S^1([1 -3]) of rank 2 over S
julia> FFF = graded_free_module(S, [1 -1; 2 3])
Graded free module S^1([-1 -2]) + S^1([1 -3]) of rank 2 over S
julia> FF == FFF
true
```
"""
function graded_free_module(R::Ring, W::Vector{<:Vector{<:IntegerUnion}}, name::String="e")
@assert is_zm_graded(R)
n = length(W[1])
@assert all(x->length(x) == n, W)
A = grading_group(R)
d = [A(w) for w = W]
return graded_free_module(R, length(W), d, name)
end
function graded_free_module(R::Ring, W::Union{ZZMatrix, Matrix{<:IntegerUnion}}, name::String="e")
@assert is_zm_graded(R)
A = grading_group(R)
d = [A(W[:, i]) for i = 1:size(W, 2)]
return graded_free_module(R, size(W, 2), d, name)
end
function graded_free_module(R::Ring, W::Vector{<:IntegerUnion}, name::String="e")
@assert is_graded(R)
A = grading_group(R)
d = [W[i] * A[1] for i in 1:length(W)]
return graded_free_module(R, length(W), d, name)
end
@doc raw"""
grade(F::FreeMod, W::Vector{FinGenAbGroupElem})
Given a free module `F` over a graded ring with grading group `G`, say, and given
a vector `W` of `ngens(F)` elements of `G`, create a `G`-graded free module
by assigning the entries of `W` as weights to the generators of `F`. Return
the new module.
grade(F::FreeMod)
As above, with all weights set to `zero(G)`.
!!! note
The function applies to free modules over both graded multivariate polynomial rings and their quotients.
# Examples
```jldoctest
julia> R, x, y = polynomial_ring(QQ, "x" => 1:2, "y" => 1: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> F = free_module(S, 3)
Free module of rank 3 over S
julia> FF = grade(F)
Graded free module S^3([0, 0]) of rank 3 over S
julia> F
Free module of rank 3 over S
```
"""
function grade(F::FreeMod, W::Vector{FinGenAbGroupElem})
@assert length(W) == ngens(F)
@assert is_graded(base_ring(F))
R = base_ring(F)
all(x -> parent(x) == grading_group(R), W) || error("entries of W must be elements of the grading group of the base ring")
N = free_module(R, length(W))
N.d = W
N.S = F.S
return N
end
function grade(F::FreeMod)
@assert is_graded(base_ring(F))
R = base_ring(F)
G = grading_group(R)
W = [zero(G) for i = 1: ngens(F)]
return grade(F, W)
end
@doc raw"""
grade(F::FreeMod, W::Vector{<:Vector{<:IntegerUnion}})
Given a free module `F` over a graded ring with grading group $G = \mathbb Z^m$, and given
a vector `W` of `ngens(F)` integer vectors of the same size `m`, say, define a $G$-grading on `F`
by converting the vectors in `W` to elements of $G$, and assigning these elements as weights to
the variables. Return the new module.
grade(F::FreeMod, W::Union{ZZMatrix, Matrix{<:IntegerUnion}})
As above, converting the columns of `W`.
grade(F::FreeMod, W::Vector{<:IntegerUnion})
Given a free module `F` over a graded ring with grading group $G = \mathbb Z$, and given
a vector `W` of `ngens(F)` integers, define a $G$-grading on `F` converting the entries
of `W` to elements of `G`, and assigning these elements as weights to the variables.
Return the new module.
!!! note
The function applies to free modules over both graded multivariate polynomial
rings and their quotients.
# Examples
```jldoctest
julia> R, (x, y, z) = graded_polynomial_ring(QQ, ["x", "y", "z"], [1 0 1; 0 1 1])
(Graded multivariate polynomial ring in 3 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x, y, z])
julia> F = free_module(R, 2)
Free module of rank 2 over R
julia> FF = grade(F, [[1, 0], [0, 1]])
Graded free module R^1([-1 0]) + R^1([0 -1]) of rank 2 over R
julia> FFF = grade(F, [1 0; 0 1])
Graded free module R^1([-1 0]) + R^1([0 -1]) of rank 2 over R
```
```jldoctest
julia> R, (x, y) = graded_polynomial_ring(QQ, ["x", "y"])
(Graded multivariate polynomial ring in 2 variables over QQ, MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}[x, y])
julia> S, _ = quo(R, [x*y])
(Quotient of multivariate polynomial ring by ideal (x*y), Map: R -> S)
julia> F = free_module(S, 2)
Free module of rank 2 over S
julia> FF = grade(F, [1, 2])
Graded free module S^1([-1]) + S^1([-2]) of rank 2 over S
```
"""
function grade(F::FreeMod, W::Vector{<:Vector{<:IntegerUnion}})
@assert length(W) == ngens(F)
R = base_ring(F)
@assert is_zm_graded(R)
n = length(W[1])
@assert all(x->length(x) == n, W)
A = grading_group(R)
return grade(F, [A(w) for w = W])
end
function grade(F::FreeMod, W::Union{ZZMatrix, Matrix{<:IntegerUnion}})
@assert size(W, 2) == ngens(F)
R = base_ring(F)
@assert is_zm_graded(R)
A = grading_group(R)
return grade(F, [A(W[:, i]) for i = 1:size(W, 2)])
end
function grade(F::FreeMod, W::Vector{<:IntegerUnion})
@assert length(W) == ngens(F)
R = base_ring(F)
@assert is_z_graded(R)
A = grading_group(R)
N = free_module(R, length(W))
N.d = [W[i] * A[1] for i in 1:length(W)]
N.S = F.S
return N
end
@doc raw"""
grading_group(F::FreeMod)
Return the grading group of `base_ring(F)`.
# Examples
```jldoctest
julia> R, (x,y) = graded_polynomial_ring(QQ, ["x", "y"]);
julia> F = graded_free_module(R, 3)
Graded free module R^3([0]) of rank 3 over R
julia> grading_group(F)
Z
```
"""
function grading_group(M::FreeMod)
return grading_group(base_ring(M))
end
# Forgetful functor for gradings
function forget_grading(F::FreeMod)
@assert is_graded(F) "module must be graded"
R = base_ring(F)
result = FreeMod(R, ngens(F))
phi = hom(F, result, gens(result); check=false)
psi = hom(result, F, gens(F); check=false)
set_attribute!(phi, :inverse=>psi)
set_attribute!(psi, :inverse=>phi)
return result, phi
end
function forget_grading(I::SubModuleOfFreeModule;
ambient_forgetful_map::FreeModuleHom=begin
R = base_ring(I)
F = ambient_free_module(I)
_, iso_F = forget_grading(F)
iso_F
end
)
g = gens(I)
gg = ambient_forgetful_map.(g)
FF = codomain(ambient_forgetful_map)
result = SubModuleOfFreeModule(FF, gg)
return result
end
function forget_grading(M::SubquoModule;
ambient_forgetful_map::FreeModuleHom=begin
R = base_ring(M)
F = ambient_free_module(M)
_, iso_F = forget_grading(F)
iso_F
end
)
@assert is_graded(M) "module must be graded"
FF = codomain(ambient_forgetful_map)
if isdefined(M, :sub) && isdefined(M, :quo)
new_sub = forget_grading(M.sub; ambient_forgetful_map)
new_quo = forget_grading(M.quo; ambient_forgetful_map)
result = SubquoModule(new_sub, new_quo)
phi = hom(M, result, gens(result); check=false)
psi = hom(result, M, gens(M); check=false)
set_attribute!(phi, :inverse=>psi)
set_attribute!(psi, :inverse=>phi)
return result, phi
elseif isdefined(M, :sub)
new_sub = forget_grading(M.sub; ambient_forgetful_map)
result = SubquoModule(new_sub)
phi = hom(M, result, gens(result); check=false)
psi = hom(result, M, gens(M); check=false)
set_attribute!(phi, :inverse=>psi)
set_attribute!(psi, :inverse=>phi)
return result, phi
elseif isdefined(M, :quo)
new_quo = forget_grading(M.quo; ambient_forgetful_map)
pre_result = SubquoModule(new_quo)
result, _ = quo(FF, pre_result)
phi = hom(M, result, gens(result); check=false)
psi = hom(result, M, gens(M); check=false)
set_attribute!(phi, :inverse=>psi)
set_attribute!(psi, :inverse=>phi)
return result, phi
end
end
# Dangerous: Only for internal use with care!!!
@doc raw"""
set_grading!(F::FreeMod, W::Vector{FinGenAbGroupElem})
set_grading!(F::FreeMod, W::Vector{<:Vector{<:IntegerUnion}})
set_grading!(F::FreeMod, W::Union{ZZMatrix, Matrix{<:IntegerUnion}})
set_grading!(F::FreeMod, W::Vector{<:IntegerUnion})
Assign weights to the generators of `F` according to the entries of `W`.
See the `grade` and `graded_free_module` functions.
```
"""
function set_grading!(M::FreeMod, W::Vector{FinGenAbGroupElem})
@assert length(W) == ngens(M)
@assert is_graded(base_ring(M))
R = base_ring(F)
all(x -> parent(x) == grading_group(R), W) || error("entries of W must be elements of the grading group of the base ring")
M.d = W
end
function set_grading!(M::FreeMod, W::Vector{<:Vector{<:IntegerUnion}})
@assert length(W) == ngens(M)
R = base_ring(M)
@assert is_zm_graded(R)
n = length(W[1])
@assert all(x->length(x) == n, W)
A = grading_group(R)
M.d = [A(w) for w = W]
end
function set_grading!(M::FreeMod, W::Union{ZZMatrix, Matrix{<:IntegerUnion}})
@assert size(W, 2) == ngens(M)
R = base_ring(M)
@assert is_zm_graded(R)
A = grading_group(R)
M.d = [A(W[:, i]) for i = 1:size(W, 2)]
end
function set_grading!(M::FreeMod, W::Vector{<:IntegerUnion})
@assert length(W) == ngens(M)
R = base_ring(M)
@assert is_z_graded(R)
A = grading_group(R)
M.d = [W[i] * A[1] for i in 1:length(W)]
end
function degrees(M::FreeMod)
@assert is_graded(M)
return M.d::Vector{FinGenAbGroupElem}
end
@doc raw"""
degrees_of_generators(F::FreeMod)
Return the degrees of the generators of `F`.
# Examples
```jldoctest
julia> R, (x, y, z) = graded_polynomial_ring(QQ, ["x", "y", "z"]);
julia> F = graded_free_module(R, 2)
Graded free module R^2([0]) of rank 2 over R
julia> degrees_of_generators(F)
2-element Vector{FinGenAbGroupElem}:
[0]
[0]
```
"""
function degrees_of_generators(F::FreeMod)
return degrees(F)
end
###############################################################################
# Graded Free Modules functions
###############################################################################
function swap!(A::Vector{T}, i::Int, j::Int) where T
A[i], A[j] = A[j], A[i]
end
function generate(k::Int, A::Vector{T}) where T
if k == 1
return [copy(A)]
else
perms = generate(k - 1, A)
for i in 0:(k - 2)
if k % 2 == 0
swap!(A, i + 1, k)
else
swap!(A, 1, k)
end
perms = vcat(perms, generate(k - 1, A))
end
return perms
end
end
function permute(v::Vector{T}) where T
return generate(length(v), v)
end
function find_bijections(v_dict::Dict{T,Vector{Int}}, w_dict::Dict{T,Vector{Int}}, v_key::Int, bijections::Vector{Dict{Int,Int}}, current_bijection::Dict{Int,Int}) where T
if v_key > length(keys(v_dict))
push!(bijections, deepcopy(current_bijection))
return nothing
end
element = collect(keys(v_dict))[v_key]
v_indices = v_dict[element]
w_indices = w_dict[element]
if length(v_indices) == length(w_indices)
for w_perm in permute(w_indices)
next_bijection = deepcopy(current_bijection)
for (i, j) in zip(v_indices, w_perm)
next_bijection[i] = j
end
find_bijections(v_dict, w_dict, v_key + 1, bijections, next_bijection)
end
end
end
function get_multiset_bijection(
v::Vector{T},
w::Vector{T},
all_bijections::Bool=false
) where {T<:Any}
v_dict = Dict{T,Vector{Int}}()
w_dict = Dict{T,Vector{Int}}()
for (i, x) in enumerate(v)
push!(get!(v_dict, x, []), i)
end
for (i, x) in enumerate(w)
push!(get!(w_dict, x, []), i)
end
bijections = Vector{Dict{Int,Int}}()
find_bijections(v_dict, w_dict, 1, bijections, Dict{Int,Int}())
return all_bijections ? bijections : (isempty(bijections) ? nothing : bijections[1])
end
###############################################################################
# Graded Free Module elements functions
###############################################################################
@doc raw"""
is_homogeneous(f::FreeModElem)
Given an element `f` of a graded free module, 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]);
julia> F = free_module(R, 2)
Free module of rank 2 over R
julia> FF = grade(F, [1,4])
Graded free module R^1([-1]) + R^1([-4]) of rank 2 over R
julia> f = y^2*2*FF[1]-x*FF[2]
2*y^2*e[1] - x*e[2]
julia> is_homogeneous(f)
true
```
"""
function is_homogeneous(el::FreeModElem)
!isnothing(el.d) && return true
!is_graded(parent(el)) && error("the parent module is not graded")
iszero(el) && return true
el.d = isa(el.d, FinGenAbGroupElem) ? el.d : determine_degree_from_SR(coordinates(el), degrees(parent(el)))
return isa(el.d, FinGenAbGroupElem)
end
AnyGradedRingElem = Union{<:MPolyDecRingElem, <:MPolyQuoRingElem{<:MPolyDecRingElem},
<:MPolyLocRingElem{<:Ring, <:RingElem, <:MPolyDecRing},
<:MPolyQuoLocRingElem{<:Ring, <:RingElem, <:MPolyDecRing}
}
@doc raw"""
degree(f::FreeModElem{T}; check::Bool=true) where {T<:AnyGradedRingElem}
Given a homogeneous element `f` of a graded free module, return the degree of `f`.
degree(::Type{Vector{Int}}, f::FreeModElem)
Given a homogeneous element `f` of a $\mathbb Z^m$-graded free module, return the degree of `f`, converted to a vector of integer numbers.
degree(::Type{Int}, f::FreeModElem)
Given a homogeneous element `f` of a $\mathbb Z$-graded free module, return the degree of `f`, converted to an integer number.
If `check` is set to `false`, then there is no check for homegeneity. This should be called
internally on provably sane input, as it speeds up computation significantly.
# Examples
```jldoctest
julia> R, (w, x, y, z) = graded_polynomial_ring(QQ, ["w", "x", "y", "z"]);
julia> f = y^2*z − x^2*w
-w*x^2 + y^2*z
julia> degree(f)
[3]
julia> typeof(degree(f))
FinGenAbGroupElem
julia> degree(Int, f)
3
julia> typeof(degree(Int, f))
Int64
```
"""
function degree(f::FreeModElem{T}; check::Bool=true) where {T<:AnyGradedRingElem}
!isnothing(f.d) && return f.d::FinGenAbGroupElem
@check is_graded(parent(f)) "the parent module is not graded"
@check is_homogeneous(f) "the element is not homogeneous"
f.d = _degree_fast(f)
return f.d::FinGenAbGroupElem
end
function _degree_of_parent_generator(f::FreeModElem, i::Int)
return f.parent.d[i]::FinGenAbGroupElem
end
# TODO: This has the potential to be a "hot" function.
# Should we store the information in the parent of `f` directly?
# Or is it enough that things are cached in the generators
# of the `sub`?
function _degree_of_parent_generator(f::SubquoModuleElem, i::Int)
return _degree_fast(gen(parent(f), i))::FinGenAbGroupElem
end
# Fast method only to be used on sane input; returns a `GrbAbFinGenElem`.
# This is exposed as an extra internal function so that `check=false` can be avoided.
function _degree_fast(f::FreeModElem)
iszero(f) && return zero(grading_group(base_ring(f)))
for (i, c) in coordinates(f)
!iszero(c) && return (_degree_fast(c) + _degree_of_parent_generator(f, i))::FinGenAbGroupElem
end
error("this line should never be reached")
end
function degree(::Type{Vector{Int}}, f::FreeModElem; check::Bool=true)
@assert is_zm_graded(parent(f))
d = degree(f; check)
return Int[d[i] for i=1:ngens(parent(d))]
end
function degree(::Type{Int}, f::FreeModElem; check::Bool=true)
@assert is_z_graded(parent(f))
return Int(degree(f; check)[1])
end
# Checks for homogeneity and computes the degree.
# If the input is not homogeneous, this returns nothing.
function determine_degree_from_SR(coords::SRow, unit_vector_degrees::Vector{FinGenAbGroupElem})
element_degree = nothing
for (position, coordval) in coords
if !is_homogeneous(coordval)
return nothing
end
current_degree = degree(coordval) + unit_vector_degrees[position]
if element_degree === nothing
element_degree = current_degree
elseif element_degree != current_degree
return nothing
end
end
return element_degree
end
###############################################################################
# Graded Free Module homomorphisms constructors
###############################################################################
function graded_map(A::MatElem)
R = base_ring(A)
G = grading_group(R)
Fcdm = graded_free_module(R, [G[0] for _ in 1:ncols(A)])
return graded_map(Fcdm, A)
end
function graded_map(F::FreeMod{T}, A::MatrixElem{T}; check::Bool=true) where {T <: RingElement}
R = base_ring(F)
G = grading_group(R)
source_degrees = Vector{eltype(G)}()
for i in 1:nrows(A)
for j in 1:ncols(A)
if !is_zero(A[i, j])
push!(source_degrees, degree(A[i, j]; check) + degree(F[j]; check))
break
end
end
end
Fcdm = graded_free_module(R, source_degrees)
phi = hom(Fcdm, F, A; check)
return phi
end
function graded_map(F::FreeMod{T}, V::Vector{<:AbstractFreeModElem{T}}; check::Bool=true) where {T <: RingElement}
R = base_ring(F)
G = grading_group(R)
nrows = length(V)
ncols = rank(F)
@check true # Trigger an error if checks are supposed to be disabled.
source_degrees = Vector{eltype(G)}()
for (i, v) in enumerate(V)
if is_zero(v)
push!(source_degrees, zero(G))
continue
end
for (j, c) in coordinates(v)
if !iszero(c)
push!(source_degrees, degree(coordinates(V[i])[j]; check) + degree(F[j]; check))
break
end
end
end
@assert length(source_degrees) == nrows
Fcdm = graded_free_module(R, source_degrees)
@assert ngens(Fcdm) == length(V) "number of generators must be equal to the number of images"
phi = hom(Fcdm, F, V; check)
return phi
end
function graded_map(F::SubquoModule{T}, V::Vector{<:ModuleFPElem{T}}; check::Bool=true) where {T <: RingElement}
R = base_ring(F)
G = grading_group(R)
nrows = length(V)
source_degrees = Vector{eltype(G)}()
for (i, v) in enumerate(V)
if is_zero(v)
push!(source_degrees, zero(G))
continue
end
for (j, c) in coordinates(v)
if !iszero(c)
push!(source_degrees, degree(coordinates(V[i])[j]; check) + degree(F[j]; check))
break
end
end
end
Fcdm = graded_free_module(R, source_degrees)
phi = hom(Fcdm, F, V; check)
return phi
end
###############################################################################
# Graded Free Module homomorphisms functions
###############################################################################
function set_grading(f::FreeModuleHom{T1, T2}; check::Bool=true) where {T1 <: FreeMod, T2 <: Union{FreeMod, SubquoModule, Oscar.SubModuleOfFreeModule}}
if !is_graded(domain(f)) || !is_graded(codomain(f))
return f
end
f.d = degree(f; check)
return f
end
function set_grading(f::FreeModuleHom{T1, T2}; check::Bool=true) where {T1 <: FreeMod_dec, T2 <: FreeMod_dec}
return f
end
# for decorations: add SubquoModule_dec for codomain once it exists
@doc raw"""
degree(a::FreeModuleHom; check::Bool=true)
If `a` is graded, return the degree of `a`.
# Examples
```jldoctest
julia> R, (x, y, z) = graded_polynomial_ring(QQ, ["x", "y", "z"]);
julia> F = graded_free_module(R, 3)
Graded free module R^3([0]) of rank 3 over R
julia> G = graded_free_module(R, 2)
Graded free module R^2([0]) of rank 2 over R
julia> V = [y*G[1], x*G[1]+y*G[2], z*G[2]]
3-element Vector{FreeModElem{MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}}}:
y*e[1]
x*e[1] + y*e[2]
z*e[2]
julia> a = hom(F, G, V)
F -> G
e[1] -> y*e[1]
e[2] -> x*e[1] + y*e[2]
e[3] -> z*e[2]
Graded module homomorphism of degree [1]
julia> degree(a)
[1]
```
"""
function degree(f::FreeModuleHom; check::Bool=true)
# TODO: isdefined should not be necessary here. Can it be kicked?
isdefined(f, :d) && isnothing(f.d) && return nothing # This stands for the map being not homogeneous
isdefined(f, :d) && return f.d::FinGenAbGroupElem
@check (is_graded(domain(f)) && is_graded(codomain(f))) "both domain and codomain must be graded"
@check is_graded(f) "map is not graded"
for i in 1:ngens(domain(f))
if iszero(domain(f)[i]) || iszero(image_of_generator(f, i))
continue
end
f.d = degree(image_of_generator(f, i); check) - degree(domain(f)[i]; check)
return f.d::FinGenAbGroupElem
end
# If we got here, the map is the zero map. Return degree zero in this case
return zero(grading_group(domain(f)))::FinGenAbGroupElem
# Old code left for debugging
return degree(image_of_generator(f, 1))
domain_degrees = degrees(T1)
df = nothing
for i in 1:length(domain_degrees)
image_vector = f(T1[i])
if isempty(coordinates(image_vector)) || is_zero(image_vector)
continue
end
current_df = degree(image_vector) - domain_degrees[i]
if df === nothing
df = current_df
elseif df != current_df
error("The homomorphism is not graded")
end
end
if df === nothing
R = base_ring(T1)
G = grading_group(R)
return G[0]
end
return df
end
@doc raw"""
is_graded(a::ModuleFPHom)
Return `true` if `a` is graded, `false` otherwise.
# Examples
```jldoctest
julia> R, (x, y, z) = graded_polynomial_ring(QQ, ["x", "y", "z"]);
julia> F = graded_free_module(R, 3)
Graded free module R^3([0]) of rank 3 over R
julia> G = graded_free_module(R, 2)
Graded free module R^2([0]) of rank 2 over R
julia> V = [y*G[1], x*G[1]+y*G[2], z*G[2]]
3-element Vector{FreeModElem{MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}}}:
y*e[1]
x*e[1] + y*e[2]
z*e[2]
julia> a = hom(F, G, V)
F -> G
e[1] -> y*e[1]
e[2] -> x*e[1] + y*e[2]
e[3] -> z*e[2]
Graded module homomorphism of degree [1]
julia> is_graded(a)
true
```
"""
function is_graded(f::ModuleFPHom)
isdefined(f, :d) && return true
(is_graded(domain(f)) && is_graded(codomain(f))) || return false
T1 = domain(f)
T2 = codomain(f)
domain_degrees = degrees_of_generators(T1)
df = nothing
for i in 1:length(domain_degrees)
image_vector = f(T1[i])
if isempty(coordinates(image_vector)) || is_zero(image_vector)
continue
end
current_df = degree(image_vector) - domain_degrees[i]
if df === nothing
df = current_df
elseif df != current_df
return false
end
end
if df === nothing
R = base_ring(T1)
G = grading_group(R)
f.d = zero(G)
return true
end
f.d = df
return true
end
@doc raw"""
grading_group(a::FreeModuleHom)
If `a` is graded, return the grading group of `a`.
# Examples
```jldoctest
julia> R, (x, y, z) = graded_polynomial_ring(QQ, ["x", "y", "z"]);
julia> F = graded_free_module(R, 3)
Graded free module R^3([0]) of rank 3 over R
julia> G = graded_free_module(R, 2)
Graded free module R^2([0]) of rank 2 over R
julia> V = [y*G[1], x*G[1]+y*G[2], z*G[2]]
3-element Vector{FreeModElem{MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}}}:
y*e[1]
x*e[1] + y*e[2]
z*e[2]
julia> a = hom(F, G, V)
F -> G
e[1] -> y*e[1]
e[2] -> x*e[1] + y*e[2]
e[3] -> z*e[2]
Graded module homomorphism of degree [1]
julia> is_graded(a)
true
julia> grading_group(a)
Z
```
"""
function grading_group(f::FreeModuleHom)
return grading_group(base_ring(domain(f)))
end
@doc raw"""
is_homogeneous(a::FreeModuleHom)
Return `true` if `a` is homogeneous, `false` otherwise
Here, if `G` is the grading group of `a`, `a` is homogeneous if `a`
is graded of degree `zero(G)`.
# Examples
```jldoctest
julia> R, (x, y, z) = graded_polynomial_ring(QQ, ["x", "y", "z"]);
julia> F = graded_free_module(R, 3)
Graded free module R^3([0]) of rank 3 over R
julia> G = graded_free_module(R, 2)
Graded free module R^2([0]) of rank 2 over R
julia> V = [y*G[1], x*G[1]+y*G[2], z*G[2]]
3-element Vector{FreeModElem{MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}}}:
y*e[1]
x*e[1] + y*e[2]
z*e[2]
julia> a = hom(F, G, V)
F -> G
e[1] -> y*e[1]
e[2] -> x*e[1] + y*e[2]
e[3] -> z*e[2]
Graded module homomorphism of degree [1]
julia> is_homogeneous(a)
false
```
"""
function is_homogeneous(f::FreeModuleHom)
A = grading_group(f)
return isdefined(f, :d) && degree(f)==A[0]
end
###############################################################################
# Graded submodules
###############################################################################
function is_graded(M::SubModuleOfFreeModule)
is_graded(M.F) && all(is_homogeneous, M.gens)
end
function degrees_of_generators(M::SubModuleOfFreeModule{T}; check::Bool=true) where T
return map(gen -> degree(gen; check), gens(M))
end
###############################################################################
# Graded subquotient constructors
###############################################################################
# mostly automatic, just needed for matrices
function graded_cokernel(A::MatElem)
return cokernel(graded_map(A))
end
function graded_cokernel(F::FreeMod{R}, A::MatElem{R}) where R
@assert is_graded(F)
cokernel(graded_map(F,A))
end
function graded_image(F::FreeMod{R}, A::MatElem{R}) where R
@assert is_graded(F)
image(graded_map(F,A))[1]
end
function graded_image(A::MatElem)
return image(graded_map(A))[1]
end
###############################################################################
# Graded subquotients
###############################################################################
@doc raw"""
grading_group(M::SubquoModule)
Return the grading group of `base_ring(M)`.
# Examples
```jldoctest