Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

r""" 

This module implements `\Q/n\Z` for `n \in \Q`. 

 

When `n \in \Z`, you can construct these groups as follows:: 

 

sage: G = QQ/ZZ; G 

Q/Z 

sage: QQ/(2*ZZ) 

Q/2Z 

 

You can create random elements:: 

 

sage: [G.random_element() for _ in range(4)] 

[15/16, 0, 1/2, 139/190] 

 

There is an iterator over the (infinitely many) elements:: 

 

sage: import itertools 

sage: list(itertools.islice(G, 10)) 

[0, 1/2, 1/3, 2/3, 1/4, 3/4, 1/5, 2/5, 3/5, 4/5] 

""" 

 

#***************************************************************************** 

# Copyright (C) 2017 David Roe <roed.math@gmail.com> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# as published by the Free Software Foundation; either version 2 of 

# the License, or (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

 

from sage.structure.parent import Parent 

from sage.structure.unique_representation import UniqueRepresentation 

from sage.rings.all import ZZ, QQ 

from sage.categories.commutative_additive_groups import CommutativeAdditiveGroups 

from .qmodnz_element import QmodnZ_Element 

 

class QmodnZ(Parent, UniqueRepresentation): 

r""" 

The ``QmodnZ`` class represents the abelian group `\Q/n\Z`. 

 

INPUT: 

 

The constructor may be called in any of the following ways. 

 

#. ``QmodnZ(n)``, where 

 

- `n` -- a rational number (including 0 or negative rational numbers). 

 

#. ``QQ/(n*ZZ)``, where 

 

- `n` -- an integer (including 0 or negative integers). 

 

 

OUTPUT: 

 

The abelian group `\Q/n\Z`. 

 

EXAMPLES:: 

 

sage: from sage.groups.additive_abelian.qmodnz import QmodnZ 

sage: QQ/(19*ZZ) 

Q/19Z 

 

sage: QmodnZ(19) 

Q/19Z 

 

sage: QmodnZ(2/3) 

Q/(2/3)Z 

""" 

 

Element = QmodnZ_Element 

def __init__(self, n=1): 

r""" 

Initialization. 

 

EXAMPLES:: 

 

sage: from sage.groups.additive_abelian.qmodnz import QmodnZ 

sage: G = QmodnZ(2) 

sage: G 

Q/2Z 

 

TESTS:: 

 

sage: G = QQ/(19*ZZ) 

sage: TestSuite(G).run() 

""" 

self.n = QQ(n).abs() 

category = CommutativeAdditiveGroups().Infinite() 

Parent.__init__(self, base=ZZ, category=category) 

self._populate_coercion_lists_(coerce_list=[QQ]) 

 

def _repr_(self): 

r""" 

Display the group. 

 

EXAMPLES:: 

 

sage: from sage.groups.additive_abelian.qmodnz import QmodnZ 

sage: G = QmodnZ(1); G 

Q/Z 

 

sage: G = QQ/(3*ZZ); G 

Q/3Z 

 

sage: G = QmodnZ(1/5); G 

Q/(1/5)Z 

""" 

if self.n == 1: 

return "Q/Z" 

elif self.n in ZZ: 

return "Q/%sZ"%(self.n) 

else: 

return "Q/(%s)Z"%(self.n) 

 

def _coerce_map_from_(self, S): 

r""" 

Coercion from a parent ``S``. 

 

There is a coercion from ``S`` if ``S`` has a coerce map to `\Q` 

or if `S = \Q/m\Z` for `m` a multiple of `n`. 

 

TESTS:: 

 

sage: G2 = QQ/(2*ZZ) 

sage: G3 = QQ/(3*ZZ) 

sage: G4 = QQ/(4*ZZ) 

sage: G2.has_coerce_map_from(QQ) 

True 

sage: G2.has_coerce_map_from(ZZ) 

True 

sage: G2.has_coerce_map_from(ZZ['x']) 

False 

sage: G2.has_coerce_map_from(G3) 

False 

sage: G2.has_coerce_map_from(G4) 

True 

sage: G4.has_coerce_map_from(G2) 

False 

""" 

if QQ.has_coerce_map_from(S): 

return True 

if isinstance(S, QmodnZ) and (S.n / self.n in ZZ): 

return True 

 

#TODO: Disallow order comparisons between different Q/nZ's 

# e.g., sage: QmodnZ(10/3) > QmodnZ(5/3) 

# returns False. 

 

def _element_constructor_(self, x): 

r""" 

Construct an element in `\Q/n\Z`. 

 

EXAMPLES:: 

 

sage: from sage.groups.additive_abelian.qmodnz import QmodnZ 

sage: G = QmodnZ(2/3) 

sage: G(5/6) 

1/6 

""" 

return self.element_class(self, QQ(x)) 

 

def an_element(self): 

""" 

Return an element, for use in coercion system. 

 

TESTS:: 

 

sage: (QQ/ZZ).an_element() 

0 

""" 

return self(0) 

 

def some_elements(self): 

""" 

Return some elements, for use in testing. 

 

TESTS:: 

 

sage: L = (QQ/ZZ).some_elements() 

sage: len(L) 

92 

""" 

return sorted(list(set([self(x) for x in QQ.some_elements()]))) 

 

def random_element(self): 

r""" 

Return a random element of `\Q/n\Z`. The denominator is selected 

using the ``1/n`` distribution on integers, modified to return 

a positive value. The numerator is then selected uniformly. 

 

EXAMPLES:: 

 

sage: G = QQ/(6*ZZ) 

sage: G.random_element() 

47/16 

sage: G.random_element() 

1 

sage: G.random_element() 

3/5 

""" 

if self.n == 0: 

return self(QQ.random_element()) 

d = ZZ.random_element() 

if d >= 0: 

d = 2*d + 1 

else: 

d = -2*d 

n = ZZ.random_element((self.n * d).ceil()) 

return self(n/d) 

 

def __iter__(self): 

r""" 

Creates an iterator that generates the elements of `\Q/n\Z` without 

repetition, organized by increasing denominator; for a fixed denominator 

elements are listed by increasing numerator. 

 

EXAMPLES: 

 

The first 19 elements of `\Q/5\Z`:: 

 

sage: import itertools 

sage: list(itertools.islice(QQ/(5*ZZ),19)) 

[0, 1, 2, 3, 4, 1/2, 3/2, 5/2, 7/2, 9/2, 1/3, 2/3, 4/3, 5/3, 7/3, 8/3, 10/3, 11/3, 13/3] 

""" 

 

if self.n == 0: 

for x in QQ: 

yield self(x) 

else: 

yield self(0) 

d = ZZ(1) 

while True: 

for a in d.coprime_integers((d*self.n).floor()): 

yield self(a/d) 

d += 1