Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/modular/arithgroup/congroup_sl2z.py
4084 views
1
r"""
2
The modular group `{\rm SL}_2(\ZZ)`
3
4
AUTHORS:
5
6
- Niles Johnson (2010-08): Trac #3893: ``random_element()`` should pass on ``*args`` and ``**kwds``.
7
8
"""
9
10
################################################################################
11
#
12
# Copyright (C) 2009, The Sage Group -- http://www.sagemath.org/
13
#
14
# Distributed under the terms of the GNU General Public License (GPL)
15
#
16
# The full text of the GPL is available at:
17
#
18
# http://www.gnu.org/licenses/
19
#
20
################################################################################
21
22
from congroup_gamma0 import Gamma0_class
23
from arithgroup_element import ArithmeticSubgroupElement
24
from sage.rings.integer_ring import ZZ
25
from sage.modular.cusps import Cusp
26
from sage.rings.arith import gcd
27
from sage.modular.modsym.p1list import lift_to_sl2z
28
29
def is_SL2Z(x):
30
r"""
31
Return True if x is the modular group `{\rm SL}_2(\ZZ)`.
32
33
EXAMPLES::
34
35
sage: from sage.modular.arithgroup.all import is_SL2Z
36
sage: is_SL2Z(SL2Z)
37
True
38
sage: is_SL2Z(Gamma0(6))
39
False
40
"""
41
return isinstance(x, SL2Z_class)
42
43
class SL2Z_class(Gamma0_class):
44
r"""
45
The full modular group `{\rm SL}_2(\ZZ)`, regarded as a congruence
46
subgroup of itself.
47
"""
48
49
def __init__(self):
50
r"""
51
The modular group ${\rm SL}_2(\Z)$.
52
53
EXAMPLES::
54
55
sage: G = SL2Z; G
56
Modular Group SL(2,Z)
57
sage: G.gens()
58
(
59
[ 0 -1] [1 1]
60
[ 1 0], [0 1]
61
)
62
sage: G.0
63
[ 0 -1]
64
[ 1 0]
65
sage: G.1
66
[1 1]
67
[0 1]
68
sage: latex(G)
69
\mbox{\rm SL}_2(\Bold{Z})
70
sage: G([1,-1,0,1])
71
[ 1 -1]
72
[ 0 1]
73
sage: loads(G.dumps()) == G
74
True
75
sage: SL2Z.0 * SL2Z.1
76
[ 0 -1]
77
[ 1 1]
78
79
sage: SL2Z == loads(dumps(SL2Z))
80
True
81
sage: SL2Z is loads(dumps(SL2Z))
82
True
83
"""
84
Gamma0_class.__init__(self, 1)
85
86
def __reduce__(self):
87
"""
88
Used for pickling self.
89
90
EXAMPLES::
91
92
sage: SL2Z.__reduce__()
93
(<function _SL2Z_ref at ...>, ())
94
"""
95
return _SL2Z_ref, ()
96
97
def __call__(self, x, check=True):
98
r"""
99
Create an element of self from x. If check=True (the default), check
100
that x really defines a 2x2 integer matrix of det 1.
101
102
EXAMPLE::
103
104
sage: SL2Z([1,0,0,1])
105
[1 0]
106
[0 1]
107
sage: SL2Z([1, QQ, False], check=False) # don't do this!
108
[1, Rational Field, False]
109
"""
110
return ArithmeticSubgroupElement(self, x, check=check)
111
112
def _repr_(self):
113
"""
114
Return the string representation of self.
115
116
EXAMPLES::
117
118
sage: SL2Z._repr_()
119
'Modular Group SL(2,Z)'
120
"""
121
return "Modular Group SL(2,Z)"
122
123
def _latex_(self):
124
r"""
125
Return the \LaTeX representation of self.
126
127
EXAMPLES::
128
129
sage: SL2Z._latex_()
130
'\\mbox{\\rm SL}_2(\\Bold{Z})'
131
sage: latex(SL2Z)
132
\mbox{\rm SL}_2(\Bold{Z})
133
"""
134
return "\\mbox{\\rm SL}_2(%s)"%(ZZ._latex_())
135
136
def is_subgroup(self, right):
137
"""
138
Return True if self is a subgroup of right.
139
140
EXAMPLES::
141
142
sage: SL2Z.is_subgroup(SL2Z)
143
True
144
sage: SL2Z.is_subgroup(Gamma1(1))
145
True
146
sage: SL2Z.is_subgroup(Gamma0(6))
147
False
148
"""
149
return right.level() == 1
150
151
def reduce_cusp(self, c):
152
r"""
153
Return the unique reduced cusp equivalent to c under the
154
action of self. Always returns Infinity, since there is only
155
one equivalence class of cusps for $SL_2(Z)$.
156
157
EXAMPLES::
158
159
sage: SL2Z.reduce_cusp(Cusps(-1/4))
160
Infinity
161
"""
162
return Cusp(1,0)
163
164
def random_element(self, bound=100, *args, **kwds):
165
r"""
166
Return a random element of `{\rm SL}_2(\ZZ)` with entries whose
167
absolute value is strictly less than bound (default 100).
168
Additional arguments and keywords are passed to the random_element
169
method of ZZ.
170
171
(Algorithm: Generate a random pair of integers at most bound. If they
172
are not coprime, throw them away and start again. If they are, find an
173
element of `{\rm SL}_2(\ZZ)` whose bottom row is that, and
174
left-multiply it by `\begin{pmatrix} 1 & w \\ 0 & 1\end{pmatrix}` for
175
an integer `w` randomly chosen from a small enough range that the
176
answer still has entries at most bound.)
177
178
It is, unfortunately, not true that all elements of SL2Z with entries <
179
bound appear with equal probability; those with larger bottom rows are
180
favoured, because there are fewer valid possibilities for w.
181
182
EXAMPLES::
183
184
sage: SL2Z.random_element()
185
[60 13]
186
[83 18]
187
sage: SL2Z.random_element(5)
188
[-1 3]
189
[ 1 -4]
190
191
Passes extra positional or keyword arguments through::
192
193
sage: SL2Z.random_element(5, distribution='1/n')
194
[ 1 -1]
195
[ 0 1]
196
197
"""
198
if bound <= 1: raise ValueError, "bound must be greater than 1"
199
c = ZZ.random_element(1-bound, bound, *args, **kwds)
200
d = ZZ.random_element(1-bound, bound, *args, **kwds)
201
if gcd(c,d) != 1: # try again
202
return self.random_element(bound)
203
else:
204
a,b,c,d = lift_to_sl2z(c,d,0)
205
whi = bound
206
wlo = bound
207
if c > 0:
208
whi = min(whi, ((bound - a)/ZZ(c)).ceil())
209
wlo = min(wlo, ((bound + a)/ZZ(c)).ceil())
210
elif c < 0:
211
whi = min(whi, ((bound + a)/ZZ(-c)).ceil())
212
wlo = min(wlo, ((bound - a)/ZZ(-c)).ceil())
213
214
if d > 0:
215
whi = min(whi, ((bound - b)/ZZ(d)).ceil())
216
wlo = min(wlo, ((bound + b)/ZZ(d)).ceil())
217
elif d < 0:
218
whi = min(whi, ((bound + b)/ZZ(-d)).ceil())
219
wlo = min(wlo, ((bound - b)/ZZ(-d)).ceil())
220
221
w = ZZ.random_element(1-wlo, whi)
222
a += c*w
223
b += d*w
224
return self([a,b,c,d])
225
226
227
SL2Z = SL2Z_class()
228
229
def _SL2Z_ref():
230
"""
231
Return SL2Z. (Used for pickling SL2Z.)
232
233
EXAMPLES::
234
235
sage: sage.modular.arithgroup.congroup_sl2z._SL2Z_ref()
236
Modular Group SL(2,Z)
237
sage: sage.modular.arithgroup.congroup_sl2z._SL2Z_ref() is SL2Z
238
True
239
"""
240
return SL2Z
241
242
243
244