Ticket #9055: trac_9055_reviewer.patch
File trac_9055_reviewer.patch, 14.0 KB (added by , 11 years ago) |
---|
-
sage/schemes/generic/rational_point.py
# HG changeset patch # User Andrey Novoseltsev <novoselt@gmail.com> # Date 1291224631 25200 # Node ID 3eb2946ea7722bbfaecaa92d5d6993758a97ef38 # Parent 36c0cfe4548562627590fe6feef769a57fa51c30 Trac 9055: Reviewer patch: format documentation and do not catch ALL exceptions. diff -r 36c0cfe45485 -r 3eb2946ea772 sage/schemes/generic/rational_point.py
a b 1 1 r""" 2 2 Enumeration of rational points on affine and projective schemes 3 3 4 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 the wrong function.(For instance using an affine function for a projective scheme or finite field function for a scheme defined over an infinite field). 4 Naive algorithms for enumerating rational points over `\QQ` or finite fields 5 over for general schemes. 6 7 .. WARNING:: 8 9 Incorrect results and infinite loops may occur if using a wrong function. 10 (For instance using an affine function for a projective scheme or a finite 11 field function for a scheme defined over an infinite field.) 5 12 6 13 EXAMPLES: 7 14 … … 11 18 sage: P.<X,Y,Z> = ProjectiveSpace(2,QQ) 12 19 sage: C = P.subscheme([X+Y-Z]) 13 20 sage: enum_projective_rational_field(C,3) 14 [(-2 : 3 : 1), (-1 : 1 : 0), (-1 : 2 : 1), (-1/2 : 3/2 : 1), (0 : 1 : 1),15 (1/3 : 2/3 : 1), (1/2 : 1/2 : 1), (2/3 : 1/3 : 1), (1 : 0: 1),16 (3/2 : -1/2 : 1), (2 : -1 : 1), (3 : -2 : 1)]21 [(-2 : 3 : 1), (-1 : 1 : 0), (-1 : 2 : 1), (-1/2 : 3/2 : 1), 22 (0 : 1 : 1), (1/3 : 2/3 : 1), (1/2 : 1/2 : 1), (2/3 : 1/3 : 1), 23 (1 : 0 : 1), (3/2 : -1/2 : 1), (2 : -1 : 1), (3 : -2 : 1)] 17 24 18 25 Affine, over `\QQ`:: 19 26 … … 21 28 sage: A.<x,y,z> = AffineSpace(3,QQ) 22 29 sage: S = A.subscheme([2*x-3*y]) 23 30 sage: enum_affine_rational_field(S,2) 24 [(0, 0, -2), (0, 0, -1), (0, 0, -1/2), (0, 0, 0), (0, 0, 1/2), (0, 0, 1),25 (0, 0, 2)]31 [(0, 0, -2), (0, 0, -1), (0, 0, -1/2), (0, 0, 0), 32 (0, 0, 1/2), (0, 0, 1), (0, 0, 2)] 26 33 27 34 Projective over a finite field:: 28 35 29 36 sage: from sage.schemes.generic.rational_point import enum_projective_finite_field 30 37 sage: E = EllipticCurve('72').change_ring(GF(19)) 31 38 sage: enum_projective_finite_field(E) 32 [(0 : 1 : 0), (1 : 0 : 1), (3 : 0 : 1), (4 : 9 : 1), (4 : 10 : 1), (6 : 6 : 1),33 (6 : 13 : 1), (7 : 6 : 1), (7 : 13 : 1), (9 : 4 : 1), (9 : 15 : 1), (12 : 8: 1),34 (12 : 11 : 1), (13 : 8 : 1), (13 : 11 : 1), (14 : 3 : 1), (14 : 16 : 1),35 (15 : 0 : 1), (16 : 9 : 1), (16 : 10 : 1), (17 : 7 : 1), (17 : 12 : 1), (18 : 9: 1),36 (18 : 10 : 1)]39 [(0 : 1 : 0), (1 : 0 : 1), (3 : 0 : 1), (4 : 9 : 1), (4 : 10 : 1), 40 (6 : 6 : 1), (6 : 13 : 1), (7 : 6 : 1), (7 : 13 : 1), (9 : 4 : 1), 41 (9 : 15 : 1), (12 : 8 : 1), (12 : 11 : 1), (13 : 8 : 1), (13 : 11 : 1), 42 (14 : 3 : 1), (14 : 16 : 1), (15 : 0 : 1), (16 : 9 : 1), (16 : 10 : 1), 43 (17 : 7 : 1), (17 : 12 : 1), (18 : 9 : 1), (18 : 10 : 1)] 37 44 38 45 Affine over a finite field:: 39 46 40 47 sage: from sage.schemes.generic.rational_point import enum_affine_finite_field 41 48 sage: A.<w,x,y,z> = AffineSpace(4,GF(2)) 42 49 sage: enum_affine_finite_field(A(GF(2))) 43 [(0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 0), (0, 0, 1, 1), (0, 1, 0, 0), (0, 1, 0, 1), 44 (0, 1, 1, 0), (0, 1, 1, 1), (1, 0, 0, 0), (1, 0, 0, 1), (1, 0, 1, 0), (1, 0, 1, 1), 45 (1, 1, 0, 0), (1, 1, 0, 1), (1, 1, 1, 0), (1, 1, 1, 1)] 50 [(0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 0), (0, 0, 1, 1), (0, 1, 0, 0), 51 (0, 1, 0, 1), (0, 1, 1, 0), (0, 1, 1, 1), (1, 0, 0, 0), (1, 0, 0, 1), 52 (1, 0, 1, 0), (1, 0, 1, 1), (1, 1, 0, 0), (1, 1, 0, 1), (1, 1, 1, 0), 53 (1, 1, 1, 1)] 46 54 47 55 AUTHORS: 48 56 49 Originally by David R. Kohel <kohel@maths.usyd.edu.au> 57 - David R. Kohel <kohel@maths.usyd.edu.au>: original version. 50 58 51 Improvements to clarity and documentation made by John Cremona and Charlie Turner <charlotteturner@gmail.com> (06-2010) 59 - John Cremona and Charlie Turner <charlotteturner@gmail.com> (06-2010): 60 improvements to clarity and documentation. 52 61 """ 62 63 53 64 #******************************************************************************* 54 65 # Copyright (C) 2010 William Stein, David Kohel, John Cremona, Charlie Turner 55 66 # Distributed under the terms of the GNU General Public License (GPL) 56 67 # http://www.gnu.org/licenses/ 57 68 #******************************************************************************* 69 70 58 71 from sage.rings.arith import gcd 59 72 from sage.rings.all import QQ, ZZ 60 73 from sage.misc.all import srange, cartesian_product_iterator 61 74 from sage.schemes.all import is_Scheme 62 75 63 76 def enum_projective_rational_field(X,B): 64 """ 65 Enumerates projective, rational points on scheme X of height up to bound B. 77 r""" 78 Enumerates projective, rational points on scheme ``X`` of height up to 79 bound ``B``. 66 80 67 81 INPUT: 68 82 69 - ``X`` - a scheme or set of abstract rational points of a scheme .70 - ``B`` - a positive integer bound 83 - ``X`` - a scheme or set of abstract rational points of a scheme; 84 - ``B`` - a positive integer bound. 71 85 72 86 OUTPUT: 73 87 74 - a list containing the projective points of X of height up to B, sorted. 88 - a list containing the projective points of ``X`` of height up to ``B``, 89 sorted. 75 90 76 91 EXAMPLES:: 77 92 … … 79 94 sage: C = P.subscheme([X+Y-Z]) 80 95 sage: from sage.schemes.generic.rational_point import enum_projective_rational_field 81 96 sage: enum_projective_rational_field(C(QQ),6) 82 [(-5 : 6 : 1), (-4 : 5 : 1), (-3 : 4 : 1), (-2 : 3 : 1), (-3/2 : 5/2 : 1), 83 (-1 : 1 : 0), (-1 : 2 : 1), (-2/3 : 5/3 : 1), (-1/2 : 3/2 : 1), (-1/3 : 4/3 : 1), 84 (-1/4 : 5/4 : 1), (-1/5 : 6/5 : 1), (0 : 1 : 1), (1/6 : 5/6 : 1), (1/5 : 4/5 : 1), 85 (1/4 : 3/4 : 1), (1/3 : 2/3 : 1), (2/5 : 3/5 : 1), (1/2 : 1/2 : 1), (3/5 : 2/5 : 1), 86 (2/3 : 1/3 : 1), (3/4 : 1/4 : 1), (4/5 : 1/5 : 1), (5/6 : 1/6 : 1), (1 : 0 : 1), 87 (6/5 : -1/5 : 1), (5/4 : -1/4 : 1), (4/3 : -1/3 : 1), (3/2 : -1/2 : 1), 88 (5/3 : -2/3 : 1), (2 : -1 : 1), (5/2 : -3/2 : 1), (3 : -2 : 1), (4 : -3 : 1), 89 (5 : -4 : 1), (6 : -5 : 1)] 97 [(-5 : 6 : 1), (-4 : 5 : 1), (-3 : 4 : 1), (-2 : 3 : 1), 98 (-3/2 : 5/2 : 1), (-1 : 1 : 0), (-1 : 2 : 1), (-2/3 : 5/3 : 1), 99 (-1/2 : 3/2 : 1), (-1/3 : 4/3 : 1), (-1/4 : 5/4 : 1), 100 (-1/5 : 6/5 : 1), (0 : 1 : 1), (1/6 : 5/6 : 1), (1/5 : 4/5 : 1), 101 (1/4 : 3/4 : 1), (1/3 : 2/3 : 1), (2/5 : 3/5 : 1), (1/2 : 1/2 : 1), 102 (3/5 : 2/5 : 1), (2/3 : 1/3 : 1), (3/4 : 1/4 : 1), (4/5 : 1/5 : 1), 103 (5/6 : 1/6 : 1), (1 : 0 : 1), (6/5 : -1/5 : 1), (5/4 : -1/4 : 1), 104 (4/3 : -1/3 : 1), (3/2 : -1/2 : 1), (5/3 : -2/3 : 1), (2 : -1 : 1), 105 (5/2 : -3/2 : 1), (3 : -2 : 1), (4 : -3 : 1), (5 : -4 : 1), 106 (6 : -5 : 1)] 90 107 sage: enum_projective_rational_field(C,6) == enum_projective_rational_field(C(QQ),6) 91 108 True 92 109 … … 108 125 ALGORITHM: 109 126 110 127 We just check all possible projective points in correct dimension 111 of projective space to see if they lie on X.128 of projective space to see if they lie on ``X``. 112 129 113 130 AUTHORS: 114 131 115 John Cremona and Charlie Turner (06-2010)132 - John Cremona and Charlie Turner (06-2010) 116 133 """ 117 134 if is_Scheme(X): 118 135 X = X(X.base_ring()) 119 n = X.codomain().ambient_space().ngens()-1 120 zero=tuple([0 for _ in range(n+1)]) 121 # pts = [X(c) for c in cartesian_product_iterator([srange(-B,B+1) for _ in range(n+1)]) if gcd(c)==1 and c>zero] 122 pts =[] 123 for c in cartesian_product_iterator([srange(-B,B+1) for _ in range(n+1)]): 124 if gcd(c)==1 and c>zero: 136 n = X.codomain().ambient_space().ngens() 137 zero = (0,) * n 138 pts = [] 139 for c in cartesian_product_iterator([srange(-B,B+1) for _ in range(n)]): 140 if gcd(c) == 1 and c > zero: 125 141 try: 126 142 pts.append(X(c)) 127 except :143 except TypeError: 128 144 pass 129 145 pts.sort() 130 146 return pts 131 147 132 148 def enum_affine_rational_field(X,B): 133 149 """ 134 Enumerates affine rational points on scheme X (defined over `\QQ`) up to bound B. 150 Enumerates affine rational points on scheme ``X`` (defined over `\QQ`) up 151 to bound ``B``. 135 152 136 153 INPUT: 137 154 138 - ``X`` - a scheme or set of abstract rational points of a scheme 139 - ``B`` - a positive integer bound 155 - ``X`` - a scheme or set of abstract rational points of a scheme; 156 - ``B`` - a positive integer bound. 140 157 141 158 OUTPUT: 142 159 143 - a list containing the affine points of X of height up to B, sorted. 160 - a list containing the affine points of ``X`` of height up to ``B``, 161 sorted. 144 162 145 163 EXAMPLES:: 146 164 147 sage: A.<x,y,z> = AffineSpace(3,QQ) 165 sage: A.<x,y,z> = AffineSpace(3,QQ) 148 166 sage: from sage.schemes.generic.rational_point import enum_affine_rational_field 149 167 sage: enum_affine_rational_field(A(QQ),1) 150 168 [(-1, -1, -1), (-1, -1, 0), (-1, -1, 1), (-1, 0, -1), (-1, 0, 0), (-1, 0, 1), … … 153 171 (1, -1, 0), (1, -1, 1), (1, 0, -1), (1, 0, 0), (1, 0, 1), (1, 1, -1), (1, 1, 0), 154 172 (1, 1, 1)] 155 173 156 157 174 :: 158 175 159 176 sage: A.<w,x,y,z> = AffineSpace(4,QQ) … … 171 188 [(-2, -6), (-1, -2), (0, 0), (1, 0), (2, -2), (3, -6)] 172 189 173 190 174 AUTHOR :191 AUTHORS: 175 192 176 David R. Kohel <kohel@maths.usyd.edu.au> 177 (small adjustments by Charlie Turner 06-2010) 193 - David R. Kohel <kohel@maths.usyd.edu.au>: original version. 194 195 - Charlie Turner (06-2010): small adjustments. 178 196 """ 179 197 if is_Scheme(X): 180 198 X = X(X.base_ring()) … … 182 200 if X.value_ring() is ZZ: 183 201 Q = [ 1 ] 184 202 else: # rational field 185 Q = [ k+1 for k in range(B) ]186 R = [ 0 ] + [ s*k for k in range(1, B+1) for s in [1,-1] ]203 Q = range(1, B + 1) 204 R = [ 0 ] + [ s*k for k in range(1, B+1) for s in [1, -1] ] 187 205 pts = [] 188 P = [ 0 for _ in range(n) ]206 P = [0] * n 189 207 m = ZZ(0) 190 208 try: 191 209 pts.append(X(P)) 192 except :210 except TypeError: 193 211 pass 194 212 iters = [ iter(R) for _ in range(n) ] 195 [ iters[j].next() for j in range(n) ] 213 for it in iters: 214 it.next() 196 215 i = 0 197 216 while i < n: 198 217 try: 199 218 a = ZZ(iters[i].next()) 200 m = m.gcd(a)201 P[i] = a202 for b in Q:203 if m.gcd(b) == 1:204 try:205 pts.append(X([ num/b for num in P ]))206 except:207 pass208 i = 0209 m = ZZ(0)210 219 except StopIteration: 211 220 iters[i] = iter(R) # reset 212 221 P[i] = iters[i].next() # reset P[i] to 0 and increment 213 222 i += 1 223 continue 224 m = m.gcd(a) 225 P[i] = a 226 for b in Q: 227 if m.gcd(b) == 1: 228 try: 229 pts.append(X([ num/b for num in P ])) 230 except TypeError: 231 pass 232 i = 0 233 m = ZZ(0) 214 234 pts.sort() 215 235 return pts 216 236 217 237 def enum_projective_finite_field(X): 218 238 """ 219 Enumerates projective points on scheme X defined over a finite field239 Enumerates projective points on scheme ``X`` defined over a finite field. 220 240 221 241 INPUT: 222 242 223 - ``X`` - a scheme defined over a finite field or set of abstract rational points of such a scheme 243 - ``X`` - a scheme defined over a finite field or a set of abstract 244 rational points of such a scheme. 224 245 225 246 OUTPUT: 226 247 227 - a list containing the projective points of X over the finite field, sorted 248 - a list containing the projective points of ``X`` over the finite field, 249 sorted. 228 250 229 251 EXAMPLES:: 230 252 … … 242 264 sage: P.<X,Y,Z> = ProjectiveSpace(2,F) 243 265 sage: C = Curve(X^3-Y^3+Z^2*Y) 244 266 sage: enum_projective_finite_field(C(F)) 245 [(0 : 0 : 1), (0 : 1 : 1), (0 : 2 : 1), (1 : 1 : 0), (a + 1 : 2*a : 1), 267 [(0 : 0 : 1), (0 : 1 : 1), (0 : 2 : 1), (1 : 1 : 0), (a + 1 : 2*a : 1), 246 268 (a + 1 : 2*a + 1 : 1), (a + 1 : 2*a + 2 : 1), (2*a + 2 : a : 1), 247 269 (2*a + 2 : a + 1 : 1), (2*a + 2 : a + 2 : 1)] 248 270 … … 262 284 263 285 Checks all points in projective space to see if they lie on X. 264 286 265 NOTE: 287 .. WARNING:: 288 289 If ``X`` is defined over an infinite field, this code will not finish! 266 290 267 Warning:if X given as input is defined over an infinite field then this code will not finish!268 269 AUTHORS: John Cremona and Charlie Turner (06-2010)291 AUTHORS: 292 293 - John Cremona and Charlie Turner (06-2010). 270 294 """ 271 295 if is_Scheme(X): 272 296 X = X(X.base_ring()) … … 277 301 for c in cartesian_product_iterator([F for _ in range(k)]): 278 302 try: 279 303 pts.append(X(list(c)+[1]+[0]*(n-k))) 280 except :304 except TypeError: 281 305 pass 282 306 pts.sort() 283 307 return pts 284 308 285 309 def enum_affine_finite_field(X): 286 """287 Enumerates affine points on scheme X defined over a finite field310 r""" 311 Enumerates affine points on scheme ``X`` defined over a finite field. 288 312 289 313 INPUT: 290 314 291 - ``X`` - a scheme defined over a finite field or set of abstract rational points of such a scheme 315 - ``X`` - a scheme defined over a finite field or a set of abstract 316 rational points of such a scheme. 292 317 293 318 OUTPUT: 294 319 295 - a list containing the affine points of X over the finite field, sorted 320 - a list containing the affine points of ``X`` over the finite field, 321 sorted. 296 322 297 323 EXAMPLES:: 298 324 … … 326 352 327 353 Checks all points in affine space to see if they lie on X. 328 354 329 NOTE:330 331 Warning:if X given as input is defined over an infinite field thenthis code will not finish!355 .. WARNING:: 356 357 If ``X`` is defined over an infinite field, this code will not finish! 332 358 333 359 AUTHORS: 334 360 335 John Cremona and Charlie Turner (06-2010) 336 361 - John Cremona and Charlie Turner (06-2010) 337 362 """ 338 363 if is_Scheme(X): 339 364 X = X(X.base_ring())