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

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

r""" 

Elements of `\Q/n\Z`. 

 

EXAMPLES:: 

 

sage: A = QQ / (3*ZZ) 

sage: x = A(11/3); x 

2/3 

sage: x*14 

1/3 

sage: x.additive_order() 

9 

sage: x / 3 

2/9 

""" 

 

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

# 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.element import AdditiveGroupElement 

from sage.rings.integer_ring import ZZ 

from sage.rings.infinity import infinity 

from sage.structure.richcmp import richcmp, op_EQ, op_NE 

 

class QmodnZ_Element(AdditiveGroupElement): 

r""" 

The ``QmodnZ_Element`` class represents an element of the abelian group `\Q/n\Z`. 

 

INPUT: 

 

- ``q`` -- a rational number. 

 

- ``parent`` -- the parent abelian group `\Q/n\Z`. 

 

OUTPUT: 

 

The element `q` of abelian group `\Q/n\Z`, in standard form. 

 

EXAMPLES:: 

 

sage: G = QQ/(19*ZZ) 

sage: G(400/19) 

39/19 

""" 

def __init__(self, parent, x, construct=False): 

r""" 

Create an element of `\Q/n\Z`. 

 

EXAMPLES:: 

 

sage: G = QQ/(3*ZZ) 

sage: G.random_element() 

47/16 

""" 

 

AdditiveGroupElement.__init__(self, parent) 

# x = (a/b) = q(n/m) + r/mb 

# am = q(nb) + r 

# r < nb so r/mb < n/m 

if construct: 

self._x = x 

return 

n = parent.n.numerator() 

if n == 0: 

self._x = x 

else: 

m = parent.n.denominator() 

a = x.numerator() 

b = x.denominator() 

q, r = (a*m).quo_rem(n*b) 

self._x = r/(m*b) 

 

def lift(self): 

r""" 

Return the smallest non-negative rational number reducing to this element. 

 

EXAMPLES:: 

 

sage: G = QQ/(5*ZZ) 

sage: g = G(2/4); g 

1/2 

sage: q = lift(g); q 

1/2 

 

TESTS:: 

 

sage: q.parent() is QQ 

True 

""" 

return self._x 

 

def _rational_(self): 

r""" 

Lift to `\Q`. 

 

TESTS:: 

 

sage: QQ((QQ/ZZ)(4/3)) # indirect doctest 

1/3 

""" 

return self._x 

 

def _integer_(self, Z): 

r""" 

Lift to `\Z`. 

 

This is the smallest non-negative integer reducing to this element, 

or a ``ValueError`` if none exists. 

 

TESTS:: 

 

sage: G = QQ/(2*ZZ) 

sage: ZZ(G(3)) 

1 

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

sage: G = QmodnZ(8/3) 

sage: ZZ(G(1/3)) 

3 

 

sage: all(ZZ(G(i)) == i for i in range(8)) 

True 

 

sage: G = QmodnZ(101/34) 

sage: all(ZZ(G(i)) == i for i in range(101)) 

True 

""" 

QZ = self.parent() 

b = self._x.denominator() 

n = QZ.n.numerator() 

m = QZ.n.denominator() 

if not b.divides(m): 

raise ValueError("No integral lift") 

a = self._x.numerator() * (m // b) 

# a/m + q*(n/m) = (a + qn)/m = km/m so a + qn = km, 

# k = a*m^(-1) mod n. 

return (a * m.inverse_mod(n)) % n 

 

def __neg__(self): 

r""" 

Return the additive inverse of this element in `\Q/n\Z`. 

 

EXAMPLES:: 

 

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

sage: G = QmodnZ(5/7) 

sage: g = G(13/21) 

sage: -g 

2/21 

 

TESTS:: 

 

sage: G = QmodnZ(19/23) 

sage: g = G(15/23) 

sage: -g 

4/23 

sage: g + -g == G(0) 

True 

""" 

if self._x == 0: 

return self 

else: 

QZ = self.parent() 

return QZ.element_class(QZ, QZ.n - self._x, True) 

 

def _add_(self, other): 

r""" 

Return the sum of two elements in `\Q/n\Z`. 

 

EXAMPLES:: 

 

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

sage: G = QmodnZ(9/10) 

sage: g = G(5) 

sage: h = G(1/2) 

sage: g + h 

1/10 

sage: g + h == G(1/10) 

True 

 

TESTS:: 

 

sage: h + g == G(1/10) 

True 

""" 

QZ = self.parent() 

ans = self._x + other._x 

if ans >= QZ.n: 

ans -= QZ.n 

return QZ.element_class(QZ, ans, True) 

 

def _sub_(self, other): 

r""" 

Returns the difference of two elements in `\Q/n\Z`. 

 

EXAMPLES:: 

 

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

sage: G = QmodnZ(9/10) 

sage: g = G(4) 

sage: h = G(1/2) 

sage: g - h 

4/5 

sage: h - g 

1/10 

sage: g - h == G(4/5) 

True 

sage: h - g == G(1/10) 

True 

""" 

QZ = self.parent() 

ans = self._x - other._x 

if ans < 0: 

ans += QZ.n 

return QZ.element_class(QZ, ans, True) 

 

def _rmul_(self, c): 

r""" 

Returns the (right) scalar product of this element by ``c`` in `\Q/n\Z`. 

 

EXAMPLES:: 

 

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

sage: G = QmodnZ(5/7) 

sage: g = G(13/21) 

sage: g*6 

1/7 

""" 

QZ = self.parent() 

return QZ.element_class(QZ, self._x * c) 

 

def _lmul_(self, c): 

r""" 

Returns the (left) scalar product of this element by ``c`` in `\Q/n\Z`. 

 

EXAMPLES:: 

 

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

sage: G = QmodnZ(5/7) 

sage: g = G(13/21) 

sage: 6*g 

1/7 

 

TESTS:: 

 

sage: 6*g == g*6 

True 

sage: 6*g == 5*g 

False 

""" 

return self._rmul_(c) 

 

def __div__(self, other): 

r""" 

Division. 

 

.. WARNING:: 

 

Division of `x` by `m` does not yield a well defined 

result, since there are `m` elements `y` of `\Q/n\Z` 

with the property that `x = my`. We return the one 

with the smallest non-negative lift. 

 

EXAMPLES:: 

 

sage: G = QQ/(4*ZZ) 

sage: x = G(3/8) 

sage: x / 4 

3/32 

""" 

QZ = self.parent() 

other = ZZ(other) 

return QZ.element_class(QZ, self._x / other, True) 

 

def _repr_(self): 

r""" 

Display the element. 

 

EXAMPLES:: 

 

sage: G = QQ/(8*ZZ) 

sage: g = G(25/7); g 

25/7 

""" 

return repr(self._x) 

 

def __hash__(self): 

r""" 

Hashing. 

 

TESTS:: 

 

sage: G = QQ/(4*ZZ) 

sage: g = G(4/5) 

sage: hash(g) 

2135587864 # 32-bit 

-7046029254386353128 # 64-bit 

sage: hash(G(3/4)) 

527949074 # 32-bit 

3938850096065010962 # 64-bit 

sage: hash(G(1)) 

1 

""" 

return hash(self._x) 

 

def _richcmp_(self, right, op): 

r""" 

Compare two elements. 

 

EXAMPLES:: 

 

sage: G = QQ/(4*ZZ) 

sage: g = G(4/5) 

sage: h = G(6/7) 

sage: g == h 

False 

sage: g == g 

True 

""" 

if op == op_EQ or op == op_NE: 

return richcmp(self._x, right._x, op) 

else: 

return NotImplemented 

 

def additive_order(self): 

r""" 

Returns the order of this element in the abelian group `\Q/n\Z`. 

 

EXAMPLES:: 

 

sage: G = QQ/(12*ZZ) 

sage: g = G(5/3) 

sage: g.additive_order() 

36 

sage: (-g).additive_order() 

36 

""" 

# a/b * k = n/m * r 

QZ = self.parent() 

if QZ.n == 0: 

if self._x == 0: 

return ZZ(1) 

return infinity 

return (self._x / QZ.n).denominator()