Path: blob/master/src/sage/schemes/affine/affine_rational_point.py
8820 views
r"""1Enumeration of rational points on affine schemes23Naive algorithms for enumerating rational points over `\QQ` or finite fields4over for general schemes.56.. WARNING::78Incorrect results and infinite loops may occur if using a wrong function.9(For instance using an affine function for a projective scheme or a finite10field function for a scheme defined over an infinite field.)1112EXAMPLES:1314Affine, over `\QQ`::1516sage: from sage.schemes.affine.affine_rational_point import enum_affine_rational_field17sage: A.<x,y,z> = AffineSpace(3,QQ)18sage: S = A.subscheme([2*x-3*y])19sage: enum_affine_rational_field(S,2)20[(0, 0, -2), (0, 0, -1), (0, 0, -1/2), (0, 0, 0),21(0, 0, 1/2), (0, 0, 1), (0, 0, 2)]2223Affine over a finite field::2425sage: from sage.schemes.affine.affine_rational_point import enum_affine_finite_field26sage: A.<w,x,y,z> = AffineSpace(4,GF(2))27sage: enum_affine_finite_field(A(GF(2)))28[(0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 0), (0, 0, 1, 1), (0, 1, 0, 0),29(0, 1, 0, 1), (0, 1, 1, 0), (0, 1, 1, 1), (1, 0, 0, 0), (1, 0, 0, 1),30(1, 0, 1, 0), (1, 0, 1, 1), (1, 1, 0, 0), (1, 1, 0, 1), (1, 1, 1, 0),31(1, 1, 1, 1)]3233AUTHORS:3435- David R. Kohel <[email protected]>: original version.3637- John Cremona and Charlie Turner <[email protected]> (06-2010):38improvements to clarity and documentation.39"""404142#*******************************************************************************43# Copyright (C) 2010 William Stein, David Kohel, John Cremona, Charlie Turner44# Distributed under the terms of the GNU General Public License (GPL)45# http://www.gnu.org/licenses/46#*******************************************************************************474849from sage.rings.all import ZZ50from sage.misc.all import srange, cartesian_product_iterator51from sage.schemes.generic.scheme import is_Scheme525354def enum_affine_rational_field(X,B):55"""56Enumerates affine rational points on scheme ``X`` (defined over `\QQ`) up57to bound ``B``.5859INPUT:6061- ``X`` - a scheme or set of abstract rational points of a scheme;62- ``B`` - a positive integer bound.6364OUTPUT:6566- a list containing the affine points of ``X`` of height up to ``B``,67sorted.6869EXAMPLES::7071sage: A.<x,y,z> = AffineSpace(3,QQ)72sage: from sage.schemes.affine.affine_rational_point import enum_affine_rational_field73sage: enum_affine_rational_field(A(QQ),1)74[(-1, -1, -1), (-1, -1, 0), (-1, -1, 1), (-1, 0, -1), (-1, 0, 0), (-1, 0, 1),75(-1, 1, -1), (-1, 1, 0), (-1, 1, 1), (0, -1, -1), (0, -1, 0), (0, -1, 1),76(0, 0, -1), (0, 0, 0), (0, 0, 1), (0, 1, -1), (0, 1, 0), (0, 1, 1), (1, -1, -1),77(1, -1, 0), (1, -1, 1), (1, 0, -1), (1, 0, 0), (1, 0, 1), (1, 1, -1), (1, 1, 0),78(1, 1, 1)]7980::8182sage: A.<w,x,y,z> = AffineSpace(4,QQ)83sage: S = A.subscheme([x^2-y*z+3,w^3+z+y^2])84sage: enum_affine_rational_field(S(QQ),2)85[]86sage: enum_affine_rational_field(S(QQ),3)87[(-2, 0, -3, -1)]8889::9091sage: A.<x,y> = AffineSpace(2,QQ)92sage: C = Curve(x^2+y-x)93sage: enum_affine_rational_field(C,10)94[(-2, -6), (-1, -2), (0, 0), (1, 0), (2, -2), (3, -6)]959697AUTHORS:9899- David R. Kohel <[email protected]>: original version.100101- Charlie Turner (06-2010): small adjustments.102"""103if is_Scheme(X):104X = X(X.base_ring())105n = X.codomain().ambient_space().ngens()106if X.value_ring() is ZZ:107Q = [ 1 ]108else: # rational field109Q = range(1, B + 1)110R = [ 0 ] + [ s*k for k in range(1, B+1) for s in [1, -1] ]111pts = []112P = [0] * n113m = ZZ(0)114try:115pts.append(X(P))116except TypeError:117pass118iters = [ iter(R) for _ in range(n) ]119for it in iters:120it.next()121i = 0122while i < n:123try:124a = ZZ(iters[i].next())125except StopIteration:126iters[i] = iter(R) # reset127P[i] = iters[i].next() # reset P[i] to 0 and increment128i += 1129continue130m = m.gcd(a)131P[i] = a132for b in Q:133if m.gcd(b) == 1:134try:135pts.append(X([ num/b for num in P ]))136except TypeError:137pass138i = 0139m = ZZ(0)140pts.sort()141return pts142143144145def enum_affine_finite_field(X):146r"""147Enumerates affine points on scheme ``X`` defined over a finite field.148149INPUT:150151- ``X`` - a scheme defined over a finite field or a set of abstract152rational points of such a scheme.153154OUTPUT:155156- a list containing the affine points of ``X`` over the finite field,157sorted.158159EXAMPLES::160161sage: F = GF(7)162sage: A.<w,x,y,z> = AffineSpace(4,F)163sage: C = A.subscheme([w^2+x+4,y*z*x-6,z*y+w*x])164sage: from sage.schemes.affine.affine_rational_point import enum_affine_finite_field165sage: enum_affine_finite_field(C(F))166[]167sage: C = A.subscheme([w^2+x+4,y*z*x-6])168sage: enum_affine_finite_field(C(F))169[(0, 3, 1, 2), (0, 3, 2, 1), (0, 3, 3, 3), (0, 3, 4, 4), (0, 3, 5, 6),170(0, 3, 6, 5), (1, 2, 1, 3), (1, 2, 2, 5), (1, 2, 3, 1), (1, 2, 4, 6),171(1, 2, 5, 2), (1, 2, 6, 4), (2, 6, 1, 1), (2, 6, 2, 4), (2, 6, 3, 5),172(2, 6, 4, 2), (2, 6, 5, 3), (2, 6, 6, 6), (3, 1, 1, 6), (3, 1, 2, 3),173(3, 1, 3, 2), (3, 1, 4, 5), (3, 1, 5, 4), (3, 1, 6, 1), (4, 1, 1, 6),174(4, 1, 2, 3), (4, 1, 3, 2), (4, 1, 4, 5), (4, 1, 5, 4), (4, 1, 6, 1),175(5, 6, 1, 1), (5, 6, 2, 4), (5, 6, 3, 5), (5, 6, 4, 2), (5, 6, 5, 3),176(5, 6, 6, 6), (6, 2, 1, 3), (6, 2, 2, 5), (6, 2, 3, 1), (6, 2, 4, 6),177(6, 2, 5, 2), (6, 2, 6, 4)]178179::180181sage: A.<x,y,z> = AffineSpace(3,GF(3))182sage: S = A.subscheme(x+y)183sage: enum_affine_finite_field(S)184[(0, 0, 0), (0, 0, 1), (0, 0, 2), (1, 2, 0), (1, 2, 1), (1, 2, 2),185(2, 1, 0), (2, 1, 1), (2, 1, 2)]186187ALGORITHM:188189Checks all points in affine space to see if they lie on X.190191.. WARNING::192193If ``X`` is defined over an infinite field, this code will not finish!194195AUTHORS:196197- John Cremona and Charlie Turner (06-2010)198"""199if is_Scheme(X):200X = X(X.base_ring())201n = X.codomain().ambient_space().ngens()202F = X.value_ring()203pts = []204for c in cartesian_product_iterator([F]*n):205try:206pts.append(X(c))207except Exception:208pass209pts.sort()210return pts211212213