Ticket #10546: trac_10546-gamma_cusps.patch

File trac_10546-gamma_cusps.patch, 6.4 KB (added by davidloeffler, 11 years ago)

Patch against 4.7.1.alpha4 + #11601 and its prerequisites

  • sage/modular/arithgroup/arithgroup_generic.py

    # HG changeset patch
    # User David Loeffler <d.loeffler.01@cantab.net>
    # Date 1310896246 -3600
    # Node ID e86f985644f38e49ef4bc789a9fd062d14fb24e8
    # Parent  ebd8ce7df572f0eaa64324dcdd1fe64751326a4f
    #10546: implement a custom cusps() method for principal congruence subgroup
    
    diff -r ebd8ce7df572 -r e86f985644f3 sage/modular/arithgroup/arithgroup_generic.py
    a b  
    543543
    544544    def reduce_cusp(self, c):
    545545        r"""
    546         Given a cusp `c \in \mathbb{P}^1(\QQ)`, return the
    547         unique reduced cusp equivalent to c under the action of self,
    548         where a reduced cusp is an element `\tfrac{r}{s}` with r,s coprime
    549         integers, s as small as possible, and r as small as possible
    550         for that s.
     546        Given a cusp `c \in \mathbb{P}^1(\QQ)`, return the unique reduced cusp
     547        equivalent to c under the action of self, where a reduced cusp is an
     548        element `\tfrac{r}{s}` with r,s coprime non-negative integers, s as
     549        small as possible, and r as small as possible for that s.
    551550       
    552551        NOTE: This function should be overridden by all subclasses.
    553552
  • sage/modular/arithgroup/congroup_gamma.py

    diff -r ebd8ce7df572 -r e86f985644f3 sage/modular/arithgroup/congroup_gamma.py
    a b  
    1717from congroup_generic import CongruenceSubgroup
    1818from arithgroup_element import ArithmeticSubgroupElement
    1919from sage.misc.misc import prod
    20 from sage.rings.all import ZZ, Zmod
     20from sage.rings.all import ZZ, Zmod, gcd, QQ
     21from sage.rings.integer import GCD_list
    2122from sage.groups.matrix_gps.matrix_group import MatrixGroup
    2223from sage.matrix.constructor import matrix
     24from sage.modular.cusps import Cusp
    2325
    2426_gamma_cache = {}
    2527def Gamma_constructor(N):
     
    168170        if n==2:
    169171            return ZZ(3)
    170172        return prod([p**(2*e) - p**(2*e-2) for (p,e) in n.factor()])//2
    171        
     173     
     174    def nirregcusps(self):
     175        r"""
     176        Return the number of irregular cusps of self. For principal congruence subgroups this is always 0.
     177
     178        EXAMPLE::
     179
     180            sage: Gamma(17).nirregcusps()
     181            0
     182        """
     183        return 0
     184
     185    def _find_cusps(self):
     186        r"""
     187        Calculate the reduced representatives of the equivalence classes of
     188        cusps for this group. Adapted from code by Ron Evans.
     189
     190        EXAMPLE::
     191
     192            sage: Gamma(8).cusps() # indirect doctest
     193            [0, 1/4, 1/3, 3/8, 1/2, 2/3, 3/4, 1, 4/3, 3/2, 5/3, 2, 7/3, 5/2, 8/3, 3, 7/2, 11/3, 4, 14/3, 5, 6, 7, Infinity]
     194        """
     195        n = self.level()
     196        C=[QQ(x) for x in xrange(n)]
     197
     198        n0=n//2
     199        n1=(n+1)//2
     200
     201        for r in xrange(1, n1):
     202            if r > 1 and gcd(r,n)==1:
     203                C.append(ZZ(r)/ZZ(n))
     204            if n0==n/2 and gcd(r,n0)==1:
     205                C.append(ZZ(r)/ZZ(n0))
     206
     207        for s in xrange(2,n1):
     208            for r in xrange(1, 1+n):
     209                if GCD_list([s,r,n])==1:
     210                    # GCD_list is ~40x faster than gcd, since gcd wastes loads
     211                    # of time initialising a Sequence type.
     212                    u,v = _lift_pair(r,s,n)
     213                    C.append(ZZ(u)/ZZ(v))
     214
     215        return [Cusp(x) for x in sorted(C)] + [Cusp(1,0)]
     216
     217    def reduce_cusp(self, c):
     218        r"""
     219        Calculate the unique reduced representative of the equivalence of the
     220        cusp `c` modulo this group. The reduced representative of an
     221        equivalence class is the unique cusp in the class of the form `u/v`
     222        with `u, v \ge 0` coprime, `v` minimal, and `u` minimal for that `v`.
     223
     224        EXAMPLES::
     225
     226            sage: Gamma(5).reduce_cusp(1/5)
     227            Infinity
     228            sage: Gamma(5).reduce_cusp(7/8)
     229            3/2
     230            sage: Gamma(6).reduce_cusp(4/3)
     231            2/3
     232
     233        TESTS::
     234
     235            sage: G = Gamma(50); all([c == G.reduce_cusp(c) for c in G.cusps()])
     236            True
     237        """
     238        N = self.level()
     239        c = Cusp(c)
     240        u,v = c.numerator() % N, c.denominator() % N
     241        if (v > N//2) or (2*v == N and u > N//2):
     242            u,v = -u,-v
     243        u,v = _lift_pair(u,v,N)
     244        return Cusp(u,v)
     245
     246    def are_equivalent(self, x, y, trans=False):
     247        r"""
     248        Check if the cusps `x` and `y` are equivalent under the action of this group.
     249
     250        ALGORITHM: The cusps `u_1 / v_1` and `u_2 / v_2` are equivalent modulo
     251        `\Gamma(N)` if and only if `(u_1, v_1) = \pm (u_2, v_2) \bmod N`.
     252       
     253        EXAMPLE::
     254
     255            sage: Gamma(7).are_equivalent(Cusp(2/3), Cusp(5/4))
     256            True
     257        """
     258        if trans:
     259            return CongruenceSubgroup.are_equivalent(self, x,y,trans=trans)
     260        N = self.level()
     261        u1,v1 = (x.numerator() % N, x.denominator() % N)
     262        u2,v2 = (y.numerator(), y.denominator())
     263
     264        return ((u1,v1) == (u2 % N, v2 % N)) or ((u1,v1) == (-u2 % N, -v2 % N))
     265
    172266    def nu3(self):
    173267        r"""
    174268        Return the number of elliptic points of order 3 for this arithmetic
     
    214308
    215309    return isinstance(x, Gamma_class)
    216310
     311def _lift_pair(U,V,N):
     312    r"""
     313    Utility function. Given integers ``U, V, N``, with `N \ge 1` and `{\rm
     314    gcd}(U, V, N) = 1`, return a pair `(u, v)` congruent to `(U, V) \bmod N`,
     315    such that `{\rm gcd}(u,v) = 1`, `u, v \ge 0`, `v` is as small as possible,
     316    and `u` is as small as possible for that `v`.
     317
     318    *Warning*: As this function is for internal use, it does not do a
     319    preliminary sanity check on its input, for efficiency. It will recover
     320    reasonably gracefully if ``(U, V, N)`` are not coprime, but only after
     321    wasting quite a lot of cycles!
     322
     323    EXAMPLE::
     324
     325        sage: from sage.modular.arithgroup.congroup_gamma import _lift_pair
     326        sage: _lift_pair(2,4,7)
     327        (9, 4)
     328        sage: _lift_pair(2,4,8) # don't do this
     329        Traceback (most recent call last):
     330        ...
     331        ValueError: (U, V, N) must be coprime
     332    """
     333    u = U % N
     334    v = V % N
     335    if v == 0:
     336        if u == 1:
     337            return (1,0)
     338        else:
     339            v = N
     340    while gcd(u, v) > 1:
     341        u = u+N
     342        if u > N*v: raise ValueError, "(U, V, N) must be coprime"
     343    return (u, v)