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

r""" 

Enumeration of rational points on affine schemes 

 

Naive algorithms for enumerating rational points over `\QQ` or finite fields 

over for general schemes. 

 

.. WARNING:: 

 

Incorrect results and infinite loops may occur if using a wrong function. 

 

(For instance using an affine function for a projective scheme or a finite 

field function for a scheme defined over an infinite field.) 

 

EXAMPLES: 

 

Affine, over `\QQ`:: 

 

sage: from sage.schemes.affine.affine_rational_point import enum_affine_rational_field 

sage: A.<x,y,z> = AffineSpace(3, QQ) 

sage: S = A.subscheme([2*x-3*y]) 

sage: enum_affine_rational_field(S, 2) 

[(0, 0, -2), (0, 0, -1), (0, 0, -1/2), (0, 0, 0), 

(0, 0, 1/2), (0, 0, 1), (0, 0, 2)] 

 

Affine over a finite field:: 

 

sage: from sage.schemes.affine.affine_rational_point import enum_affine_finite_field 

sage: A.<w,x,y,z> = AffineSpace(4, GF(2)) 

sage: enum_affine_finite_field(A(GF(2))) 

[(0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 0), (0, 0, 1, 1), (0, 1, 0, 0), 

(0, 1, 0, 1), (0, 1, 1, 0), (0, 1, 1, 1), (1, 0, 0, 0), (1, 0, 0, 1), 

(1, 0, 1, 0), (1, 0, 1, 1), (1, 1, 0, 0), (1, 1, 0, 1), (1, 1, 1, 0), 

(1, 1, 1, 1)] 

 

AUTHORS: 

 

- David R. Kohel <kohel@maths.usyd.edu.au>: original version. 

 

- John Cremona and Charlie Turner <charlotteturner@gmail.com> (06-2010): 

improvements to clarity and documentation. 

""" 

 

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

# Copyright (C) 2010 William Stein, David Kohel, John Cremona, Charlie Turner 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License 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 six.moves import range 

 

from sage.rings.all import ZZ 

from sage.misc.all import cartesian_product_iterator 

from sage.schemes.generic.scheme import is_Scheme 

 

 

def enum_affine_rational_field(X, B): 

""" 

Enumerates affine rational points on scheme ``X`` up to bound ``B``. 

 

INPUT: 

 

- ``X`` - a scheme or set of abstract rational points of a scheme. 

- ``B`` - a positive integer bound. 

 

OUTPUT: 

 

- a list containing the affine points of ``X`` of height up to ``B``, 

sorted. 

 

EXAMPLES:: 

 

sage: A.<x,y,z> = AffineSpace(3, QQ) 

sage: from sage.schemes.affine.affine_rational_point import enum_affine_rational_field 

sage: enum_affine_rational_field(A(QQ), 1) 

[(-1, -1, -1), (-1, -1, 0), (-1, -1, 1), (-1, 0, -1), (-1, 0, 0), (-1, 0, 1), 

(-1, 1, -1), (-1, 1, 0), (-1, 1, 1), (0, -1, -1), (0, -1, 0), (0, -1, 1), 

(0, 0, -1), (0, 0, 0), (0, 0, 1), (0, 1, -1), (0, 1, 0), (0, 1, 1), (1, -1, -1), 

(1, -1, 0), (1, -1, 1), (1, 0, -1), (1, 0, 0), (1, 0, 1), (1, 1, -1), (1, 1, 0), 

(1, 1, 1)] 

 

:: 

 

sage: A.<w,x,y,z> = AffineSpace(4, QQ) 

sage: S = A.subscheme([x^2-y*z+3, w^3+z+y^2]) 

sage: enum_affine_rational_field(S(QQ), 2) 

[] 

sage: enum_affine_rational_field(S(QQ), 3) 

[(-2, 0, -3, -1)] 

 

:: 

 

sage: A.<x,y> = AffineSpace(2, QQ) 

sage: C = Curve(x^2+y-x) 

sage: enum_affine_rational_field(C, 10) 

[(-2, -6), (-1, -2), (0, 0), (1, 0), (2, -2), (3, -6)] 

 

 

AUTHORS: 

 

- David R. Kohel <kohel@maths.usyd.edu.au>: original version. 

 

- Charlie Turner (06-2010): small adjustments. 

""" 

from sage.schemes.affine.affine_space import is_AffineSpace 

if(is_Scheme(X)): 

if (not is_AffineSpace(X.ambient_space())): 

raise TypeError("ambient space must be affine space over the rational field") 

X = X(X.base_ring()) 

else: 

if (not is_AffineSpace(X.codomain().ambient_space())): 

raise TypeError("codomain must be affine space over the rational field") 

 

n = X.codomain().ambient_space().ngens() 

if X.value_ring() is ZZ: 

Q = [1] 

else: # rational field 

Q = list(range(1, B + 1)) 

R = [ 0 ] + [ s*k for k in range(1, B+1) for s in [1, -1] ] 

pts = [] 

P = [0] * n 

m = ZZ.zero() 

try: 

pts.append(X(P)) 

except TypeError: 

pass 

iters = [ iter(R) for _ in range(n) ] 

for it in iters: 

next(it) 

i = 0 

while i < n: 

try: 

a = ZZ(next(iters[i])) 

except StopIteration: 

iters[i] = iter(R) # reset 

P[i] = next(iters[i]) # reset P[i] to 0 and increment 

i += 1 

continue 

m = m.gcd(a) 

P[i] = a 

for b in Q: 

if m.gcd(b) == 1: 

try: 

pts.append(X([ num/b for num in P ])) 

except TypeError: 

pass 

i = 0 

m = ZZ(0) 

pts.sort() 

return pts 

 

 

def enum_affine_number_field(X, B): 

""" 

Enumerates affine points on scheme ``X`` defined over a number field. Simply checks all of the 

points of absolute height up to ``B`` and adds those that are on the scheme to the list. 

 

INPUT: 

 

- ``X`` - a scheme defined over a number field. 

 

- ``B`` - a real number. 

 

OUTPUT: 

 

- a list containing the affine points of ``X`` of absolute height up to ``B``, 

sorted. 

 

EXAMPLES:: 

 

sage: from sage.schemes.affine.affine_rational_point import enum_affine_number_field 

sage: u = QQ['u'].0 

sage: K = NumberField(u^2 + 2, 'v') 

sage: A.<x,y,z> = AffineSpace(K, 3) 

sage: X = A.subscheme([y^2 - x]) 

sage: enum_affine_number_field(X(K), 4) 

[(0, 0, -1), (0, 0, -v), (0, 0, -1/2*v), (0, 0, 0), (0, 0, 1/2*v), (0, 0, v), (0, 0, 1), 

(1, -1, -1), (1, -1, -v), (1, -1, -1/2*v), (1, -1, 0), (1, -1, 1/2*v), (1, -1, v), (1, -1, 1), 

(1, 1, -1), (1, 1, -v), (1, 1, -1/2*v), (1, 1, 0), (1, 1, 1/2*v), (1, 1, v), (1, 1, 1)] 

 

:: 

 

sage: u = QQ['u'].0 

sage: K = NumberField(u^2 + 3, 'v') 

sage: A.<x,y> = AffineSpace(K, 2) 

sage: X=A.subscheme(x-y) 

sage: from sage.schemes.affine.affine_rational_point import enum_affine_number_field 

sage: enum_affine_number_field(X, 3) 

[(-1, -1), (-1/2*v - 1/2, -1/2*v - 1/2), (1/2*v - 1/2, 1/2*v - 1/2), (0, 0), (-1/2*v + 1/2, -1/2*v + 1/2), 

(1/2*v + 1/2, 1/2*v + 1/2), (1, 1)] 

""" 

from sage.schemes.affine.affine_space import is_AffineSpace 

if(is_Scheme(X)): 

if (not is_AffineSpace(X.ambient_space())): 

raise TypeError("ambient space must be affine space over a number field") 

X = X(X.base_ring()) 

else: 

if (not is_AffineSpace(X.codomain().ambient_space())): 

raise TypeError("codomain must be affine space over a number field") 

 

R = X.codomain().ambient_space() 

 

pts = [] 

for P in R.points_of_bounded_height(B): 

try: 

pts.append(X(P)) 

except TypeError: 

pass 

pts.sort() 

return pts 

 

 

def enum_affine_finite_field(X): 

r""" 

Enumerates affine points on scheme ``X`` defined over a finite field. 

 

INPUT: 

 

- ``X`` - a scheme defined over a finite field or a set of abstract 

rational points of such a scheme. 

 

OUTPUT: 

 

- a list containing the affine points of ``X`` over the finite field, 

sorted. 

 

EXAMPLES:: 

 

sage: F = GF(7) 

sage: A.<w,x,y,z> = AffineSpace(4, F) 

sage: C = A.subscheme([w^2+x+4, y*z*x-6, z*y+w*x]) 

sage: from sage.schemes.affine.affine_rational_point import enum_affine_finite_field 

sage: enum_affine_finite_field(C(F)) 

[] 

sage: C = A.subscheme([w^2+x+4, y*z*x-6]) 

sage: enum_affine_finite_field(C(F)) 

[(0, 3, 1, 2), (0, 3, 2, 1), (0, 3, 3, 3), (0, 3, 4, 4), (0, 3, 5, 6), 

(0, 3, 6, 5), (1, 2, 1, 3), (1, 2, 2, 5), (1, 2, 3, 1), (1, 2, 4, 6), 

(1, 2, 5, 2), (1, 2, 6, 4), (2, 6, 1, 1), (2, 6, 2, 4), (2, 6, 3, 5), 

(2, 6, 4, 2), (2, 6, 5, 3), (2, 6, 6, 6), (3, 1, 1, 6), (3, 1, 2, 3), 

(3, 1, 3, 2), (3, 1, 4, 5), (3, 1, 5, 4), (3, 1, 6, 1), (4, 1, 1, 6), 

(4, 1, 2, 3), (4, 1, 3, 2), (4, 1, 4, 5), (4, 1, 5, 4), (4, 1, 6, 1), 

(5, 6, 1, 1), (5, 6, 2, 4), (5, 6, 3, 5), (5, 6, 4, 2), (5, 6, 5, 3), 

(5, 6, 6, 6), (6, 2, 1, 3), (6, 2, 2, 5), (6, 2, 3, 1), (6, 2, 4, 6), 

(6, 2, 5, 2), (6, 2, 6, 4)] 

 

:: 

 

sage: A.<x,y,z> = AffineSpace(3, GF(3)) 

sage: S = A.subscheme(x+y) 

sage: enum_affine_finite_field(S) 

[(0, 0, 0), (0, 0, 1), (0, 0, 2), (1, 2, 0), (1, 2, 1), (1, 2, 2), 

(2, 1, 0), (2, 1, 1), (2, 1, 2)] 

 

ALGORITHM: 

 

Checks all points in affine space to see if they lie on X. 

 

.. WARNING:: 

 

If ``X`` is defined over an infinite field, this code will not finish! 

 

AUTHORS: 

 

- John Cremona and Charlie Turner (06-2010) 

""" 

from sage.schemes.affine.affine_space import is_AffineSpace 

if(is_Scheme(X)): 

if (not is_AffineSpace(X.ambient_space())): 

raise TypeError("ambient space must be affine space over a finite field") 

X = X(X.base_ring()) 

else: 

if (not is_AffineSpace(X.codomain().ambient_space())): 

raise TypeError("codomain must be affine space over a finite field") 

 

n = X.codomain().ambient_space().ngens() 

F = X.value_ring() 

pts = [] 

for c in cartesian_product_iterator([F]*n): 

try: 

pts.append(X(c)) 

except Exception: 

pass 

pts.sort() 

return pts