forked from oscar-system/Oscar.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatGrp.jl
1121 lines (920 loc) · 32 KB
/
MatGrp.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
matrix_group_type(::Type{T}) where T<:RingElement = MatrixGroup{T, dense_matrix_type(T)}
matrix_group_type(::Type{S}) where S<:Ring = matrix_group_type(elem_type(S))
matrix_group_type(x) = matrix_group_type(typeof(x)) # to stop this method from eternally recursing on itself, we better add ...
matrix_group_type(::Type{T}) where T = throw(ArgumentError("Type `$T` must be subtype of `RingElement`."))
const ZZMatrixGroup = matrix_group_type(ZZRing)
const QQMatrixGroup = matrix_group_type(QQField)
matrix_group(F::Ring, m::Int) = matrix_group_type(F)(F, m)
# build a MatrixGroup given a list of generators, given as array of either MatrixGroupElem or AbstractAlgebra matrices
"""
matrix_group(R::Ring, m::Int, V::T...) where T<:Union{MatElem,MatrixGroupElem}
matrix_group(R::Ring, m::Int, V::AbstractVector{T}) where T<:Union{MatElem,MatrixGroupElem}
matrix_group(V::T...) where T<:Union{MatElem,MatrixGroupElem}
matrix_group(V::AbstractVector{T}) where T<:Union{MatElem,MatrixGroupElem}
Return the matrix group generated by matrices in `V`. If the degree `m` and
coefficient ring `R` are not given, then `V` must be non-empty
"""
function matrix_group(F::Ring, m::Int, V::AbstractVector{T}; check::Bool=true) where T<:Union{MatElem,AbstractMatrixGroupElem}
@req all(v -> size(v) == (m,m), V) "Matrix group generators must all be square and of equal degree"
@req all(v -> base_ring(v) == F, V) "Matrix group generators must have the same base ring"
# if T <: MatrixGroupElem, we can already assume that det(V[i]) != 0
if T<:MatElem && check
@req all(v -> is_unit(det(v)), V) "Matrix group generators must be invertible over their base ring"
end
G = matrix_group(F, m)
L = Vector{elem_type(G)}(undef, length(V))
for i in 1:length(V)
if T<:MatElem
L[i] = MatrixGroupElem(G,V[i])
else
# TODO: this part of code from here
if isdefined(V[i],:elm)
if isdefined(V[i],:X)
L[i] = MatrixGroupElem(G,matrix(V[i]),V[i].X)
else
L[i] = MatrixGroupElem(G,matrix(V[i]))
end
else
L[i] = MatrixGroupElem(G,V[i].X)
end
# to here
# can be replaced by the following line once deepcopy works for GAP objects
# > L[i] = deepcopy(V[i]); L[i].parent = G;
end
end
G.gens = L
return G
end
matrix_group(R::Ring, m::Int, V::Union{MatElem,MatrixGroupElem}...; check::Bool=true) = matrix_group(R, m, collect(V); check)
matrix_group(V::AbstractVector{T}; check::Bool=true) where T<:Union{MatElem,MatrixGroupElem} = matrix_group(base_ring(V[1]), nrows(V[1]), V; check)
matrix_group(V::Union{MatElem,MatrixGroupElem}...; check::Bool=true) = matrix_group(collect(V); check)
# For `general_linear_group` etc. the degree comes first, so we should also provide that option
matrix_group(m::Int, R::Ring) = matrix_group(R, m)
matrix_group(m::Int, R::Ring, V; check::Bool = true) = matrix_group(R, m, V, check = check)
# `MatrixGroup`: compare types, dimensions, and coefficient rings
function check_parent(G::T, g::GAPGroupElem) where T <: MatrixGroup
P = parent(g)
return T === typeof(P) && degree(G) == degree(P) && base_ring(G) == base_ring(P)
end
function _as_subgroup_bare(G::MatrixGroup, H::GapObj)
H1 = typeof(G)(base_ring(G), degree(G))
H1.ring_iso = _ring_iso(G)
H1.X = H
return H1
end
MatrixGroupElem(G::MatrixGroup{RE,T}, x::T, x_gap::GapObj) where {RE,T} = MatrixGroupElem{RE,T}(G, x, x_gap)
MatrixGroupElem(G::MatrixGroup{RE,T}, x::T) where {RE, T} = MatrixGroupElem{RE,T}(G,x)
MatrixGroupElem(G::MatrixGroup{RE,T}, x_gap::GapObj) where {RE, T} = MatrixGroupElem{RE,T}(G,x_gap)
"""
ring_elem_type(G::MatrixGroup{S,T}) where {S,T}
ring_elem_type(::Type{MatrixGroup{S,T}}) where {S,T}
Return the type `S` of the entries of the elements of `G`.
One can enter the type of `G` instead of `G`.
# Examples
```jldoctest
julia> g = GL(2, 3);
julia> ring_elem_type(typeof(g)) == elem_type(typeof(base_ring(g)))
true
```
"""
ring_elem_type(::Type{MatrixGroup{S,T}}) where {S,T} = S
ring_elem_type(::MatrixGroup{S,T}) where {S,T} = S
"""
mat_elem_type(G::MatrixGroup{S,T}) where {S,T}
mat_elem_type(::Type{MatrixGroup{S,T}}) where {S,T}
Return the type `T` of `matrix(x)`, for elements `x` of `G`.
One can enter the type of `G` instead of `G`.
# Examples
```jldoctest
julia> g = GL(2, 3);
julia> mat_elem_type(typeof(g)) == typeof(matrix(one(g)))
true
```
"""
mat_elem_type(::Type{MatrixGroup{S,T}}) where {S,T} = T
mat_elem_type(::MatrixGroup{S,T}) where {S,T} = T
_gap_filter(::Type{<:MatrixGroup}) = GAP.Globals.IsMatrixGroup
elem_type(::Type{MatrixGroup{S,T}}) where {S,T} = MatrixGroupElem{S,T}
Base.eltype(::Type{MatrixGroup{S,T}}) where {S,T} = MatrixGroupElem{S,T}
# `parent_type` is defined and documented in AbstractAlgebra.
parent_type(::Type{MatrixGroupElem{S,T}}) where {S,T} = MatrixGroup{S,T}
function Base.deepcopy_internal(x::MatrixGroupElem, dict::IdDict)
if isdefined(x, :X)
X = Base.deepcopy_internal(x.X, dict)
if isdefined(x, :elm)
elm = Base.deepcopy_internal(matrix(x), dict)
return MatrixGroupElem(parent(x), elm, X)
else
return MatrixGroupElem(parent(x), X)
end
elseif isdefined(x, :elm)
elm = Base.deepcopy_internal(matrix(x), dict)
return MatrixGroupElem(parent(x), elm)
end
error("$x has neither :X nor :elm")
end
change_base_ring(R::Ring, G::MatrixGroup) = map_entries(R, G)
########################################################################
#
# Basic
#
########################################################################
function _print_matrix_group_desc(io::IO, x::MatrixGroup)
io = pretty(io)
R = base_ring(x)
print(io, LowercaseOff(), string(x.descr), "(", degree(x) ,",")
if x.descr==:GU || x.descr==:SU
print(io, characteristic(R)^(div(degree(R),2)),")")
elseif R isa Field && is_finite(R)
print(io, order(R),")")
else
print(terse(io), R)
print(io ,")")
end
end
function Base.show(io::IO, ::MIME"text/plain", x::MatrixGroup)
isdefined(x, :descr) && return _print_matrix_group_desc(io, x)
println(io, "Matrix group of degree ", degree(x))
io = pretty(io)
print(io, Indent())
print(io, "over ", Lowercase(), base_ring(x))
print(io, Dedent())
end
function Base.show(io::IO, x::MatrixGroup)
@show_name(io, x)
@show_special(io, x)
isdefined(x, :descr) && return _print_matrix_group_desc(io, x)
print(io, "Matrix group")
if !is_terse(io)
print(io, " of degree ", degree(x))
io = pretty(io)
print(terse(io), " over ", Lowercase(), base_ring(x))
end
end
Base.show(io::IO, x::MatrixGroupElem) = show(io, matrix(x))
Base.show(io::IO, mi::MIME"text/plain", x::MatrixGroupElem) = show(io, mi, matrix(x))
group_element(G::MatrixGroup, x::GapObj) = MatrixGroupElem(G,x)
# Compute and store the component `G.X` if this is possible.
function assign_from_description(G::MatrixGroup)
F = codomain(_ring_iso(G))
GAP.Globals.IsBaseRingSupportedForClassicalMatrixGroup(F, GapObj(G.descr)) || error("no generators are known for the matrix group of type $(G.descr) over $(base_ring(G))")
if G.descr==:GL G.X=GAP.Globals.GL(degree(G), F)
elseif G.descr==:SL G.X=GAP.Globals.SL(degree(G), F)
elseif G.descr==:Sp G.X=GAP.Globals.Sp(degree(G), F)
elseif G.descr==Symbol("GO+") G.X=GAP.Globals.GO(1, degree(G), F)
elseif G.descr==Symbol("SO+") G.X=GAP.Globals.SO(1, degree(G), F)
elseif G.descr==Symbol("Omega+")
# FIXME/TODO: Work around GAP issue <https://github.com/gap-system/gap/issues/500>
# using the following inefficient code. In the future, we should use appropriate
# generators for Omega (e.g. by applying a form change matrix to the Omega
# generators returned by GAP).
L = GAP.Globals.SubgroupsOfIndexTwo(GAP.Globals.SO(1, degree(G), F))
if degree(G) == 4 && order(base_ring(G)) == 2 # this is the only case SO(n,q) has more than one subgroup of index 2
for y in L
_ranks = [GAP.Globals.Rank(u) for u in GAPWrap.GeneratorsOfGroup(y)]
if all(is_even, _ranks)
G.X=y
break
end
end
else
@assert length(L) == 1
G.X=L[1]
end
elseif G.descr==Symbol("GO-") G.X=GAP.Globals.GO(-1, degree(G), F)
elseif G.descr==Symbol("SO-") G.X=GAP.Globals.SO(-1, degree(G), F)
elseif G.descr==Symbol("Omega-") G.X=GAP.Globals.SubgroupsOfIndexTwo(GAP.Globals.SO(-1, degree(G), F))[1]
elseif G.descr==:GO G.X=GAP.Globals.GO(0, degree(G), F)
elseif G.descr==:SO G.X=GAP.Globals.SO(0, degree(G), F)
elseif G.descr==:Omega
# For even q or d = 1, \Omega(d,q) is equal to SO(d,q).
# Otherwise, \Omega(d,q) has index 2 in SO(d,q).
# Here d is odd, and we do not get here if d == 1 holds
# because `omega_group` delegates to `SO` in this case.
@assert degree(G) > 1
if iseven(GAPWrap.Size(F))
G.X = GAP.Globals.SO(0, degree(G), F)
else
L = GAP.Globals.SubgroupsOfIndexTwo(GAP.Globals.SO(0, degree(G), F))
@assert length(L) == 1
G.X = L[1]
end
elseif G.descr==:GU G.X=GAP.Globals.GU(degree(G),Int(characteristic(base_ring(G))^(div(degree(base_ring(G)),2) ) ))
elseif G.descr==:SU G.X=GAP.Globals.SU(degree(G),Int(characteristic(base_ring(G))^(div(degree(base_ring(G)),2) ) ))
else error("unsupported description")
end
end
function _ring_iso(G::MatrixGroup{T}) where T
if !isdefined(G, :ring_iso)
if T === QQBarFieldElem
# get all matrix entries into one vector
entries = reduce(vcat, vec(collect(matrix(g))) for g in gens(G))
# construct a number field over which all matrices are already defined
nf, nf_to_QQBar = number_field(QQ, entries)
iso = iso_oscar_gap(nf)
G.ring_iso = MapFromFunc(base_ring(G), codomain(iso),
x -> iso(preimage(nf_to_QQBar, x)),
y -> nf_to_QQBar(preimage(iso, y))
)
else
G.ring_iso = iso_oscar_gap(base_ring(G))::MapFromFunc{parent_type(T), GapObj}
end
end
return G.ring_iso::MapFromFunc{parent_type(T), GapObj}
end
GAP.@install function GapObj(G::MatrixGroup)
if !isdefined(G, :X)
if isdefined(G, :descr)
assign_from_description(G)
elseif isdefined(G, :gens)
V = GapObj(gens(G); recursive = true)
G.X = isempty(V) ? GAP.Globals.Group(V, GapObj(one(G))) : GAP.Globals.Group(V)
else
error("Cannot determine underlying GAP object")
end
end
return G.X
end
GAP.@install function GapObj(x::MatrixGroupElem)
if !isdefined(x, :X)
x.X = map_entries(_ring_iso(parent(x)), x.elm)
end
return x.X
end
Base.IteratorSize(::Type{<:MatrixGroup}) = Base.SizeUnknown()
Base.iterate(G::MatrixGroup) = iterate(G, GAPWrap.Iterator(GapObj(G)))
function Base.iterate(G::MatrixGroup, state::GapObj)
GAPWrap.IsDoneIterator(state) && return nothing
i = GAPWrap.NextIterator(state)::GapObj
return MatrixGroupElem(G, i), state
end
########################################################################
#
# Membership
#
########################################################################
function ==(G::MatrixGroup,H::MatrixGroup)
degree(G) == degree(H) || return false
base_ring(G) == base_ring(H) || return false
if isdefined(G, :descr) && isdefined(H, :descr)
return G.descr == H.descr
end
if isdefined(G, :gens) && isdefined(H, :gens)
gens(G)==gens(H) && return true
end
return GapObj(G)==GapObj(H)
end
# this saves the value of x.X
# x_gap = x.X if this is already known, x_gap = nothing otherwise
function lies_in(x::MatElem, G::MatrixGroup, x_gap)
if base_ring(x) != base_ring(G) || nrows(x) != degree(G) return false, x_gap end
if isone(x) return true, x_gap end
if isdefined(G,:gens)
for g in gens(G)
if x==matrix(g)
return true, x_gap
end
end
end
if isdefined(G,:descr) && G.descr === :GL
return det(x)!=0, x_gap
elseif isdefined(G,:descr) && G.descr === :SL
return det(x)==1, x_gap
elseif x_gap === nothing
x_gap = map_entries(_ring_iso(G), x)
end
return (x_gap in GapObj(G)), x_gap
end
Base.in(x::MatElem, G::MatrixGroup) = lies_in(x,G,nothing)[1]
function Base.in(x::MatrixGroupElem, G::MatrixGroup)
isdefined(x,:X) && return lies_in(matrix(x),G,GapObj(x))[1]
_is_true, x_gap = lies_in(matrix(x),G,nothing)
if x_gap !== nothing
x.X = x_gap
end
return _is_true
end
# embedding an element of type MatElem into a group G
# if check=false, there are no checks on the condition `x in G`
function (G::MatrixGroup)(x::MatElem; check::Bool=true)
if check
_is_true, x_gap = lies_in(x,G,nothing)
@req _is_true "Element not in the group"
x_gap !== nothing && return MatrixGroupElem(G,x,x_gap)
end
return MatrixGroupElem(G,x)
end
# embedding an element of type MatrixGroupElem into a group G
# if check=false, there are no checks on the condition `x in G`
function (G::MatrixGroup)(x::MatrixGroupElem; check::Bool=true)
if !check
z = x
z.parent = G
return z
end
if isdefined(x,:X)
if isdefined(x,:elm)
_is_true = lies_in(matrix(x),G,GapObj(x))[1]
@req _is_true "Element not in the group"
return MatrixGroupElem(G,matrix(x),GapObj(x))
else
@req GapObj(x) in GapObj(G) "Element not in the group"
return MatrixGroupElem(G,GapObj(x))
end
else
_is_true, x_gap = lies_in(matrix(x),G,nothing)
@req _is_true "Element not in the group"
if x_gap === nothing
return MatrixGroupElem(G,matrix(x))
end
return MatrixGroupElem(G,matrix(x),x_gap)
end
end
# embedding a n x n array into a group G
function (G::MatrixGroup)(L::AbstractVecOrMat; check::Bool=true)
x = matrix(base_ring(G), degree(G), degree(G), L)
return G(x; check=check)
end
########################################################################
#
# Methods on elements
#
########################################################################
# we are not currently keeping track of the parent; two elements coincide iff their matrices coincide
function ==(x::MatrixGroupElem{S,T},y::MatrixGroupElem{S,T}) where {S,T}
if isdefined(x,:X) && isdefined(y,:X) return x.X==y.X
else return matrix(x)==matrix(y)
end
end
function _common_parent_group(x::T, y::T) where T <: MatrixGroup
x === y && return x
@assert degree(x) == degree(y)
@assert base_ring(x) === base_ring(y)
return GL(degree(x), base_ring(x))::T
end
# Base.:* is defined in src/Groups/GAPGroups.jl,
# and it calls the function _prod below.
# If the parents are different,
# the parent of the product is set as GL(n, R)
function _prod(x::T,y::T) where {T <: MatrixGroupElem}
G = _common_parent_group(parent(x), parent(y))
# if the underlying GAP matrices are both defined, but not both Oscar matrices,
# then use the GAP matrices.
# Otherwise, use the Oscar matrices, which if necessary are implicitly computed
# by the matrix(::MatrixGroupElem) method .
if isdefined(x,:X) && isdefined(y,:X) && !(isdefined(x,:elm) && isdefined(y,:elm))
return T(G, x.X*y.X)
else
return T(G, matrix(x)*matrix(y))
end
end
Base.:*(x::MatrixGroupElem, y::MatElem) = matrix(x)*y
Base.:*(x::MatElem, y::MatrixGroupElem) = x*matrix(y)
Base.:^(x::MatrixGroupElem, n::Int) = MatrixGroupElem(parent(x), matrix(x)^n)
Base.isone(x::MatrixGroupElem) = isone(matrix(x))
Base.inv(x::MatrixGroupElem) = MatrixGroupElem(parent(x), inv(matrix(x)))
# if the parents are different, the parent of the output is set as GL(n,q)
function Base.:^(x::MatrixGroupElem, y::MatrixGroupElem)
G = parent(x) == parent(y) ? parent(x) : GL(degree(parent(x)), base_ring(parent(x)))
if isdefined(x,:X) && isdefined(y,:X) && !(isdefined(x,:elm) && isdefined(y,:elm))
return MatrixGroupElem(G, inv(y.X)*x.X*y.X)
else
return MatrixGroupElem(G,inv(matrix(y))*matrix(x)*matrix(y))
end
end
comm(x::MatrixGroupElem, y::MatrixGroupElem) = inv(x)*conj(x,y)
#T why needed? GAPGroupElem has x^-1*x^y
"""
det(x::MatrixGroupElem)
Return the determinant of the underlying matrix of `x`.
"""
det(x::MatrixGroupElem) = det(matrix(x))
"""
base_ring(x::MatrixGroupElem)
Return the base ring of the matrix group to which `x` belongs.
This is also the base ring of the underlying matrix of `x`.
# Examples
```jldoctest
julia> F = GF(4); g = general_linear_group(2, F);
julia> x = gen(g, 1)
[o 0]
[0 1]
julia> base_ring(x) == F
true
julia> base_ring(x) == base_ring(matrix(x))
true
```
"""
base_ring(x::MatrixGroupElem) = base_ring(parent(x))
base_ring_type(::Type{<:MatrixGroupElem{RE}}) where {RE} = parent_type(RE)
parent(x::MatrixGroupElem) = x.parent
"""
matrix(x::MatrixGroupElem)
Return the underlying matrix of `x`.
# Examples
```jldoctest
julia> F = GF(4); g = general_linear_group(2, F);
julia> x = gen(g, 1)
[o 0]
[0 1]
julia> m = matrix(x)
[o 0]
[0 1]
julia> x == m
false
julia> x == g(m)
true
```
"""
function matrix(x::MatrixGroupElem)
if !isdefined(x, :elm)
x.elm = preimage_matrix(_ring_iso(parent(x)), GapObj(x))
end
return x.elm
end
Base.getindex(x::MatrixGroupElem, i::Int, j::Int) = matrix(x)[i,j]
"""
number_of_rows(x::MatrixGroupElem)
Return the number of rows of the underlying matrix of `x`.
"""
number_of_rows(x::MatrixGroupElem) = number_of_rows(matrix(x))
"""
number_of_columns(x::MatrixGroupElem)
Return the number of columns of the underlying matrix of `x`.
"""
number_of_columns(x::MatrixGroupElem) = number_of_columns(matrix(x))
#
size(x::MatrixGroupElem) = size(matrix(x))
"""
tr(x::MatrixGroupElem)
Return the trace of the underlying matrix of `x`.
# Examples
```jldoctest
julia> F = GF(4); g = general_linear_group(2, F);
julia> x = gen(g, 1)
[o 0]
[0 1]
julia> t = tr(x)
o + 1
julia> t in F
true
```
"""
tr(x::MatrixGroupElem) = tr(matrix(x))
#FIXME for the following functions, the output may not belong to the parent group of x
#=
frobenius(x::MatrixGroupElem, n::Int) = MatrixGroupElem(parent(x), matrix(base_ring(x), degree(parent(x)), degree(parent(x)), [frobenius(y,n) for y in matrix(x)]))
frobenius(x::MatrixGroupElem) = frobenius(x,1)
transpose(x::MatrixGroupElem) = MatrixGroupElem(parent(x), transpose(matrix(x)))
=#
########################################################################
#
# Methods on groups
#
########################################################################
"""
base_ring(G::MatrixGroup)
Return the base ring of the matrix group `G`.
# Examples
```jldoctest
julia> F = GF(4); g = general_linear_group(2, F);
julia> base_ring(g) == F
true
```
"""
base_ring(G::MatrixGroup{RE}) where RE <: RingElem = G.ring::parent_type(RE)
base_ring_type(::Type{<:MatrixGroup{RE}}) where {RE} = parent_type(RE)
"""
degree(G::MatrixGroup)
Return the degree of `G`, i.e., the number of rows of its matrices.
# Examples
```jldoctest
julia> degree(GL(4, 2))
4
```
"""
degree(G::MatrixGroup) = G.deg
Base.one(G::MatrixGroup) = MatrixGroupElem(G, identity_matrix(base_ring(G), degree(G)))
function Base.rand(rng::Random.AbstractRNG, G::MatrixGroup)
x_gap = GAP.Globals.Random(GAP.wrap_rng(rng), GapObj(G))::GapObj
return MatrixGroupElem(G, x_gap)
end
function gens(G::MatrixGroup)
if !isdefined(G,:gens)
L = GAPWrap.GeneratorsOfGroup(GapObj(G))::GapObj
G.gens = [MatrixGroupElem(G, a::GapObj) for a in L]
end
return G.gens::Vector{elem_type(G)}
end
# Note that the `gen(G::GAPGroup, i::Int)` method cannot be used
# for `MatrixGroup` because of the `:gens` attribute.
function gen(G::MatrixGroup, i::Int)
i == 0 && return one(G)
L = gens(G)
0 < i && i <= length(L) && return L[i]
i < 0 && -i <= length(L) && return inv(L[-i])
@req false "i must be in the range -$(length(L)):$(length(L))"
end
number_of_generators(G::MatrixGroup) = length(gens(G))
compute_order(G::GAPGroup) = ZZRingElem(GAPWrap.Size(GapObj(G)))
function compute_order(G::MatrixGroup{T}) where {T <: Union{AbsSimpleNumFieldElem, QQFieldElem}}
#=
- For a matrix group G over the rationals or over a number field,
the GAP group GapObj(G) does usually not store the flag `IsHandledByNiceMonomorphism`.
- If we know a reasonable ("nice") faithful permutation action of `G` in advance,
we can set this flag in `GapObj(G)` to true and store the action homomorphism in `GapObj(G)`,
and then this information should be used in the computation of the order.
- If the flag is not known to be true then the Oscar code from
`isomorphic_group_over_finite_field` shall be preferred.
=#
if GAP.Globals.HasIsHandledByNiceMonomorphism(GapObj(G)) && GAPWrap.IsHandledByNiceMonomorphism(GapObj(G))
# The call to `IsHandledByNiceMonomorphism` triggers an expensive
# computation of `IsFinite` which we avoid by checking
# `HasIsHandledByNiceMonomorphism` first.
return ZZRingElem(GAPWrap.Size(GapObj(G)))
else
n = order(isomorphic_group_over_finite_field(G)[1])
set_order(G, n)
return n
end
end
function order(::Type{T}, G::MatrixGroup) where T <: IntegerUnion
res = get_attribute!(G, :order) do
return compute_order(G)
end::ZZRingElem
return T(res)::T
end
"""
map_entries(f, G::MatrixGroup)
Return the matrix group obtained by applying `f` element-wise to
each generator of `G`.
`f` can be a ring or a field, a suitable map, or a Julia function.
# Examples
```jldoctest
julia> mat = matrix(ZZ, 2, 2, [1, 1, 0, 1]);
julia> G = matrix_group(mat);
julia> G2 = map_entries(x -> -x, G)
Matrix group of degree 2
over integer ring
julia> is_finite(G2)
false
julia> order(map_entries(GF(3), G))
3
```
"""
function map_entries(f, G::MatrixGroup)
Ggens = gens(G)
if length(Ggens) == 0
z = f(zero(base_ring(G)))
return matrix_group(parent(z), degree(G), MatrixGroupElem[])
else
imgs = [map_entries(f, matrix(x)) for x in gens(G)]
return matrix_group(imgs)
end
end
function map_entries(R::Ring, G::MatrixGroup)
imgs = dense_matrix_type(R)[map_entries(R, matrix(x)) for x in gens(G)]
return matrix_group(R, degree(G), imgs)
end
function map_entries(mp::Map, G::MatrixGroup)
imgs = dense_matrix_type(codomain(mp))[map_entries(mp, matrix(x)) for x in gens(G)]
return matrix_group(codomain(mp), degree(G), imgs)
end
########################################################################
#
# Constructors
#
########################################################################
function _field_from_q(q::Int)
flag, n, p = is_prime_power_with_data(q)
@req flag "The field size must be a prime power"
return GF(p, n)
end
"""
general_linear_group(n::Int, q::Int)
general_linear_group(n::Int, R::Ring)
GL = general_linear_group
Return the general linear group of dimension `n` over the ring `R` respectively the field `GF(q)`.
Currently `R` must be either a finite field or a residue ring or `ZZ`.
# Examples
```jldoctest
julia> F = GF(7)
Prime field of characteristic 7
julia> H = general_linear_group(2, F)
GL(2,7)
julia> gens(H)
2-element Vector{MatrixGroupElem{FqFieldElem, FqMatrix}}:
[3 0; 0 1]
[6 1; 6 0]
julia> order(general_linear_group(2, residue_ring(ZZ, 6)[1]))
288
```
"""
function general_linear_group(n::Int, R::Ring)
G = matrix_group(R, n)
G.descr = :GL
return G
end
function general_linear_group(n::Int, q::Int)
return general_linear_group(n, _field_from_q(q))
end
"""
special_linear_group(n::Int, q::Int)
special_linear_group(n::Int, R::Ring)
SL = special_linear_group
Return the special linear group of dimension `n` over the ring `R` respectively the field `GF(q)`.
Currently `R` must be either a finite field or a residue ring or `ZZ`.
# Examples
```jldoctest
julia> F = GF(7)
Prime field of characteristic 7
julia> H = special_linear_group(2, F)
SL(2,7)
julia> gens(H)
2-element Vector{MatrixGroupElem{FqFieldElem, FqMatrix}}:
[3 0; 0 5]
[6 1; 6 0]
julia> order(special_linear_group(2, residue_ring(ZZ, 6)[1]))
144
```
"""
function special_linear_group(n::Int, R::Ring)
G = matrix_group(R, n)
G.descr = :SL
return G
end
function special_linear_group(n::Int, q::Int)
return special_linear_group(n, _field_from_q(q))
end
"""
symplectic_group(n::Int, q::Int)
symplectic_group(n::Int, R::Ring)
Sp = symplectic_group
Return the symplectic group of dimension `n` over the ring `R` respectively the
field `GF(q)`. The dimension `n` must be even.
Currently `R` must be either a finite field
or a residue ring of prime power order.
# Examples
```jldoctest
julia> F = GF(7)
Prime field of characteristic 7
julia> H = symplectic_group(2, F)
Sp(2,7)
julia> gens(H)
2-element Vector{MatrixGroupElem{FqFieldElem, FqMatrix}}:
[3 0; 0 5]
[6 1; 6 0]
julia> order(symplectic_group(2, residue_ring(ZZ, 4)[1]))
48
```
"""
function symplectic_group(n::Int, R::Ring)
@req iseven(n) "The dimension must be even"
G = matrix_group(R, n)
G.descr = :Sp
return G
end
function symplectic_group(n::Int, q::Int)
return symplectic_group(n, _field_from_q(q))
end
"""
orthogonal_group(e::Int, n::Int, R::Ring)
orthogonal_group(e::Int, n::Int, q::Int)
GO = orthogonal_group
Return the orthogonal group of dimension `n` over the ring `R` respectively the
field `GF(q)`, and of type `e`, where `e` in {`+1`,`-1`} for `n` even and `e`=`0`
for `n` odd. If `n` is odd, `e` can be omitted.
Currently `R` must be either a finite field
or a residue ring of odd prime power order.
# Examples
```jldoctest
julia> F = GF(7)
Prime field of characteristic 7
julia> H = orthogonal_group(1, 2, F)
GO+(2,7)
julia> gens(H)
2-element Vector{MatrixGroupElem{FqFieldElem, FqMatrix}}:
[3 0; 0 5]
[0 1; 1 0]
julia> order(orthogonal_group(-1, 2, residue_ring(ZZ, 9)[1]))
24
```
"""
function orthogonal_group(e::Int, n::Int, R::Ring)
if e==1
@req iseven(n) "The dimension must be even"
G = matrix_group(R, n)
G.descr = Symbol("GO+")
elseif e==-1
@req iseven(n) "The dimension must be even"
G = matrix_group(R, n)
G.descr = Symbol("GO-")
elseif e==0
@req isodd(n) "The dimension must be odd"
G = matrix_group(R, n)
G.descr = :GO
else
throw(ArgumentError("Invalid description of orthogonal group"))
end
return G
end
function orthogonal_group(e::Int, n::Int, q::Int)
return orthogonal_group(e, n, _field_from_q(q))
end
orthogonal_group(n::Int, R::Ring) = orthogonal_group(0,n,R)
orthogonal_group(n::Int, q::Int) = orthogonal_group(0,n,q)
"""
special_orthogonal_group(e::Int, n::Int, R::Ring)
special_orthogonal_group(e::Int, n::Int, q::Int)
SO = special_orthogonal_group
Return the special orthogonal group of dimension `n` over the ring `R` respectively
the field `GF(q)`, and of type `e`, where `e` in {`+1`,`-1`} for `n` even and
`e`=`0` for `n` odd. If `n` is odd, `e` can be omitted.
Currently `R` must be either a finite field
or a residue ring of odd prime power order.
# Examples
```jldoctest
julia> F = GF(7)
Prime field of characteristic 7
julia> H = special_orthogonal_group(1, 2, F)
SO+(2,7)
julia> gens(H)
3-element Vector{MatrixGroupElem{FqFieldElem, FqMatrix}}:
[3 0; 0 5]
[5 0; 0 3]
[1 0; 0 1]
julia> order(special_orthogonal_group(-1, 2, residue_ring(ZZ, 9)[1]))
12
```
"""
function special_orthogonal_group(e::Int, n::Int, R::Ring)
characteristic(R) == 2 && return GO(e,n,R)
if e==1
@req iseven(n) "The dimension must be even"
G = matrix_group(R, n)
G.descr = Symbol("SO+")
elseif e==-1
@req iseven(n) "The dimension must be even"
G = matrix_group(R, n)
G.descr = Symbol("SO-")
elseif e==0
@req isodd(n) "The dimension must be odd"
G = matrix_group(R, n)
G.descr = :SO
else
throw(ArgumentError("Invalid description of orthogonal group"))
end
return G
end
function special_orthogonal_group(e::Int, n::Int, q::Int)
return special_orthogonal_group(e, n, _field_from_q(q))
end
special_orthogonal_group(n::Int, R::Ring) = special_orthogonal_group(0,n,R)
special_orthogonal_group(n::Int, q::Int) = special_orthogonal_group(0,n,q)
"""
omega_group(e::Int, n::Int, R::Ring)
omega_group(e::Int, n::Int, q::Int)
Return the Omega group of dimension `n` over the field `GF(q)` of type `e`,
where `e` in {`+1`,`-1`} for `n` even and `e`=`0` for `n` odd. If `n` is odd,
`e` can be omitted.
Currently `R` must be either a finite field
or a residue ring of odd prime power order.
# Examples
```jldoctest
julia> F = GF(7)
Prime field of characteristic 7
julia> H = omega_group(1, 2, F)
Omega+(2,7)
julia> gens(H)
1-element Vector{MatrixGroupElem{FqFieldElem, FqMatrix}}:
[2 0; 0 4]
julia> order(omega_group(0, 3, residue_ring(ZZ, 9)[1]))
324
```
"""
function omega_group(e::Int, n::Int, R::Ring)
n==1 && return SO(e,n,R)
if e==1
@req iseven(n) "The dimension must be even"
G = matrix_group(R, n)
G.descr = Symbol("Omega+")
elseif e==-1
@req iseven(n) "The dimension must be even"
G = matrix_group(R, n)
G.descr = Symbol("Omega-")
elseif e==0
@req isodd(n) "The dimension must be odd"
G = matrix_group(R, n)
G.descr = :Omega
else
throw(ArgumentError("Invalid description of orthogonal group"))
end
return G
end
function omega_group(e::Int, n::Int, q::Int)
return omega_group(e, n, _field_from_q(q))
end
omega_group(n::Int, q::Int) = omega_group(0,n,q)
omega_group(n::Int, R::Ring) = omega_group(0,n,R)