Ticket #9055: trac_9055_reviewer.patch

File trac_9055_reviewer.patch, 14.0 KB (added by novoselt, 11 years ago)

Apply last

  • 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  
    11r"""
    22Enumeration of rational points on affine and projective schemes
    33
    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).
     4Naive algorithms for enumerating rational points over `\QQ` or finite fields
     5over 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.)
    512
    613EXAMPLES:
    714
     
    1118    sage: P.<X,Y,Z> = ProjectiveSpace(2,QQ)
    1219    sage: C = P.subscheme([X+Y-Z])
    1320    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)]
    1724
    1825Affine, over `\QQ`::
    1926   
     
    2128    sage: A.<x,y,z> = AffineSpace(3,QQ)
    2229    sage: S = A.subscheme([2*x-3*y])
    2330    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)]
    2633
    2734Projective over a finite field::
    2835
    2936    sage: from sage.schemes.generic.rational_point import enum_projective_finite_field
    3037    sage: E = EllipticCurve('72').change_ring(GF(19))
    3138    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)]
    3744
    3845Affine over a finite field::
    3946
    4047    sage: from sage.schemes.generic.rational_point import enum_affine_finite_field
    4148    sage: A.<w,x,y,z> = AffineSpace(4,GF(2))
    4249    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)]
    4654
    4755AUTHORS:
    4856
    49 Originally by David R. Kohel <kohel@maths.usyd.edu.au>
     57- David R. Kohel <kohel@maths.usyd.edu.au>: original version.
    5058
    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.
    5261"""
     62
     63
    5364#*******************************************************************************
    5465#  Copyright (C) 2010 William Stein, David Kohel, John Cremona, Charlie Turner
    5566#  Distributed under the terms of the GNU General Public License (GPL)
    5667#                  http://www.gnu.org/licenses/
    5768#*******************************************************************************
     69
     70
    5871from sage.rings.arith import gcd
    5972from sage.rings.all import QQ, ZZ
    6073from sage.misc.all import srange, cartesian_product_iterator
    6174from sage.schemes.all import is_Scheme
    6275
    6376def 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``.
    6680   
    6781    INPUT:
    6882   
    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.
    7185
    7286    OUTPUT:
    7387
    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.
    7590
    7691    EXAMPLES::
    7792
     
    7994        sage: C = P.subscheme([X+Y-Z])
    8095        sage: from sage.schemes.generic.rational_point import enum_projective_rational_field
    8196        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)]
    90107        sage: enum_projective_rational_field(C,6) == enum_projective_rational_field(C(QQ),6)
    91108        True
    92109
     
    108125    ALGORITHM:
    109126
    110127    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``.
    112129
    113130    AUTHORS:
    114131
    115     John Cremona and Charlie Turner (06-2010)
     132    - John Cremona and Charlie Turner (06-2010)
    116133    """
    117134    if is_Scheme(X):
    118135        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:
    125141            try:
    126142                pts.append(X(c))
    127             except:
     143            except TypeError:
    128144                pass
    129145    pts.sort()
    130146    return pts
    131147
    132148def enum_affine_rational_field(X,B):
    133149    """
    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``.
    135152
    136153    INPUT:
    137154   
    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.
    140157
    141158    OUTPUT:
    142159
    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.
    144162
    145163    EXAMPLES::
    146164
    147         sage: A.<x,y,z> = AffineSpace(3,QQ)                                         
     165        sage: A.<x,y,z> = AffineSpace(3,QQ)
    148166        sage: from sage.schemes.generic.rational_point import enum_affine_rational_field
    149167        sage: enum_affine_rational_field(A(QQ),1)
    150168        [(-1, -1, -1), (-1, -1, 0), (-1, -1, 1), (-1, 0, -1), (-1, 0, 0), (-1, 0, 1),
     
    153171        (1, -1, 0), (1, -1, 1), (1, 0, -1), (1, 0, 0), (1, 0, 1), (1, 1, -1), (1, 1, 0),
    154172        (1, 1, 1)]
    155173
    156 
    157174    ::
    158175
    159176        sage: A.<w,x,y,z> = AffineSpace(4,QQ)
     
    171188        [(-2, -6), (-1, -2), (0, 0), (1, 0), (2, -2), (3, -6)]
    172189
    173190
    174     AUTHOR:
     191    AUTHORS:
    175192
    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.
    178196    """
    179197    if is_Scheme(X):
    180198        X = X(X.base_ring())
     
    182200    if X.value_ring() is ZZ:
    183201        Q = [ 1 ]
    184202    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] ]
    187205    pts = []
    188     P = [ 0 for _ in range(n) ]
     206    P = [0] * n
    189207    m = ZZ(0)
    190208    try:
    191209        pts.append(X(P))
    192     except:
     210    except TypeError:
    193211        pass
    194212    iters = [ iter(R) for _ in range(n) ]
    195     [ iters[j].next() for j in range(n) ]
     213    for it in iters:
     214        it.next()
    196215    i = 0
    197216    while i < n:
    198217        try:
    199218            a = ZZ(iters[i].next())
    200             m = m.gcd(a)
    201             P[i] = a
    202             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                         pass
    208             i = 0
    209             m = ZZ(0)
    210219        except StopIteration:
    211220            iters[i] = iter(R) # reset
    212221            P[i] = iters[i].next() # reset P[i] to 0 and increment
    213222            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)
    214234    pts.sort()
    215235    return pts
    216236
    217237def enum_projective_finite_field(X):
    218238    """
    219     Enumerates projective points on scheme X defined over a finite field
     239    Enumerates projective points on scheme ``X`` defined over a finite field.
    220240   
    221241    INPUT:
    222242   
    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.
    224245
    225246    OUTPUT:
    226247
    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.
    228250
    229251    EXAMPLES::
    230252
     
    242264        sage: P.<X,Y,Z> = ProjectiveSpace(2,F)
    243265        sage: C = Curve(X^3-Y^3+Z^2*Y)
    244266        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),
    246268        (a + 1 : 2*a + 1 : 1), (a + 1 : 2*a + 2 : 1), (2*a + 2 : a : 1),
    247269        (2*a + 2 : a + 1 : 1), (2*a + 2 : a + 2 : 1)]
    248270       
     
    262284
    263285    Checks all points in projective space to see if they lie on X.
    264286
    265     NOTE:
     287    .. WARNING::
     288   
     289        If ``X`` is defined over an infinite field, this code will not finish!
    266290
    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).
    270294    """
    271295    if is_Scheme(X):
    272296        X = X(X.base_ring())
     
    277301        for c in cartesian_product_iterator([F for _ in range(k)]):
    278302            try:
    279303                pts.append(X(list(c)+[1]+[0]*(n-k)))
    280             except:
     304            except TypeError:
    281305                pass
    282306    pts.sort()
    283307    return pts
    284308
    285309def enum_affine_finite_field(X):
    286     """
    287     Enumerates affine points on scheme X defined over a finite field
     310    r"""
     311    Enumerates affine points on scheme ``X`` defined over a finite field.
    288312   
    289313    INPUT:
    290314   
    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.
    292317
    293318    OUTPUT:
    294319
    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.
    296322
    297323    EXAMPLES::
    298324
     
    326352
    327353    Checks all points in affine space to see if they lie on X.
    328354
    329     NOTE:
    330 
    331     Warning:if X given as input is defined over an infinite field then this code will not finish!
     355    .. WARNING::
     356   
     357        If ``X`` is defined over an infinite field, this code will not finish!
    332358
    333359    AUTHORS:
    334360
    335     John Cremona and Charlie Turner (06-2010)
    336 
     361    - John Cremona and Charlie Turner (06-2010)
    337362    """
    338363    if is_Scheme(X):
    339364        X = X(X.base_ring())