From 4ed5382215b14eff132106177fb098f54c40a4e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Tue, 12 Apr 2022 13:40:35 +0200 Subject: [PATCH 1/2] some care for pep8 in pyx files in modular/ --- .../modular/arithgroup/arithgroup_element.pyx | 2 +- src/sage/modular/arithgroup/congroup.pyx | 26 +++--- src/sage/modular/arithgroup/farey_symbol.pyx | 4 +- src/sage/modular/modsym/heilbronn.pyx | 35 ++++--- src/sage/modular/modsym/manin_symbol.pyx | 4 +- src/sage/modular/modsym/p1list.pyx | 91 ++++++++++++------- 6 files changed, 101 insertions(+), 61 deletions(-) diff --git a/src/sage/modular/arithgroup/arithgroup_element.pyx b/src/sage/modular/arithgroup/arithgroup_element.pyx index 847252780d5..86ef0e933aa 100644 --- a/src/sage/modular/arithgroup/arithgroup_element.pyx +++ b/src/sage/modular/arithgroup/arithgroup_element.pyx @@ -194,7 +194,7 @@ cdef class ArithmeticSubgroupElement(MultiplicativeGroupElement): return richcmp(self.__x, right.__x, op) def __bool__(self): - """ + r""" Return ``True``, since the ``self`` lives in SL(2,\Z), which does not contain the zero matrix. diff --git a/src/sage/modular/arithgroup/congroup.pyx b/src/sage/modular/arithgroup/congroup.pyx index 3cb2a84b334..9554ebbc11d 100644 --- a/src/sage/modular/arithgroup/congroup.pyx +++ b/src/sage/modular/arithgroup/congroup.pyx @@ -1,4 +1,4 @@ -""" +r""" Cython helper functions for congruence subgroups This file contains optimized Cython implementations of a few functions related @@ -6,13 +6,13 @@ to the standard congruence subgroups `\Gamma_0, \Gamma_1, \Gamma_H`. These functions are for internal use by routines elsewhere in the Sage library. """ -#***************************************************************************** +# **************************************************************************** # 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/ -#***************************************************************************** +# https://www.gnu.org/licenses/ +# **************************************************************************** from cysignals.memory cimport check_allocarray, sig_free @@ -28,7 +28,7 @@ from sage.matrix.matrix_integer_dense cimport Matrix_integer_dense from sage.modular.modsym.p1list import lift_to_sl2z from sage.matrix.matrix_space import MatrixSpace from sage.rings.integer_ring import ZZ -Mat2Z = MatrixSpace(ZZ,2) +Mat2Z = MatrixSpace(ZZ, 2) cdef Matrix_integer_dense genS, genT, genI genS = Matrix_integer_dense(Mat2Z, [0,-1, 1, 0], True, True) @@ -124,9 +124,11 @@ def degeneracy_coset_representatives_gamma0(int N, int M, int t): cc = M*random.randrange(-halfmax, halfmax+1) dd = random.randrange(-halfmax, halfmax+1) g = arith_int.c_xgcd_int(-cc,dd,&bb,&aa) - if g == 0: continue + if g == 0: + continue cc = cc / g - if cc % M != 0: continue + if cc % M != 0: + continue dd = dd / g # Test if we've found a new coset representative. is_new = 1 @@ -212,7 +214,6 @@ def degeneracy_coset_representatives_gamma1(int N, int M, int t): cdef int d, g, i, j, k, n, aa, bb, cc, dd, Ndivt, halfmax, is_new cdef int* R - # total number of coset representatives that we'll find n = Gamma1(N).index() / Gamma1(M).index() d = arith_int.c_gcd_int(t, N/t) @@ -226,11 +227,14 @@ def degeneracy_coset_representatives_gamma1(int N, int M, int t): cc = M*random.randrange(-halfmax, halfmax+1) dd = 1 + M*random.randrange(-halfmax, halfmax+1) g = arith_int.c_xgcd_int(-cc,dd,&bb,&aa) - if g == 0: continue + if g == 0: + continue cc = cc / g - if cc % M != 0: continue + if cc % M != 0: + continue dd = dd / g - if M != 1 and dd % M != 1: continue + if M != 1 and dd % M != 1: + continue # Test if we've found a new coset representative. is_new = 1 for i from 0 <= i < k: diff --git a/src/sage/modular/arithgroup/farey_symbol.pyx b/src/sage/modular/arithgroup/farey_symbol.pyx index 18ba31ae5dc..01b3db0b019 100644 --- a/src/sage/modular/arithgroup/farey_symbol.pyx +++ b/src/sage/modular/arithgroup/farey_symbol.pyx @@ -610,7 +610,7 @@ cdef class Farey: if forced_format == 'plain': # output not using xymatrix s = r'\left( -\infty' - a = [x._latex_() for x in self.fractions()] + ['\infty'] + a = [x._latex_() for x in self.fractions()] + [r'\infty'] b = self.pairings() for i in xrange(len(a)): u = b[i] @@ -623,7 +623,7 @@ cdef class Farey: else: # output using xymatrix s = r'\begin{xy}\xymatrix{& -\infty ' - f = [x._latex_() for x in self.fractions()]+[r'\infty'] + f = [x._latex_() for x in self.fractions()] + [r'\infty'] f.reverse() for p in self.pairings(): if p >= 0: diff --git a/src/sage/modular/modsym/heilbronn.pyx b/src/sage/modular/modsym/heilbronn.pyx index 3bfd5ffafe1..2ba337dddc4 100644 --- a/src/sage/modular/modsym/heilbronn.pyx +++ b/src/sage/modular/modsym/heilbronn.pyx @@ -211,7 +211,7 @@ cdef class Heilbronn: sig_off() cdef apply_to_polypart(self, fmpz_poly_t* ans, int i, int k): - """ + r""" INPUT: - ``ans`` - fmpz_poly_t\*; pre-allocated an @@ -402,7 +402,11 @@ cdef class HeilbronnCremona(Heilbronn): a = -b b = c x3 = q*x2 - x1 - x1 = x2; x2 = x3; y3 = q*y2 - y1; y1 = y2; y2 = y3 + x1 = x2 + x2 = x3 + y3 = q*y2 - y1 + y1 = y2 + y2 = y3 list_append4(L, x1,x2, y1,y2) self.length = L.i/4 sig_off() @@ -573,9 +577,11 @@ def hecke_images_gamma0_weight2(int u, int v, int N, indices, R): # Allocate memory to hold images of (u,v) under all Heilbronn matrices a = sig_malloc(sizeof(int)*H.length) - if not a: raise MemoryError + if not a: + raise MemoryError b = sig_malloc(sizeof(int)*H.length) - if not b: raise MemoryError + if not b: + raise MemoryError # Compute images of (u,v) under all Heilbronn matrices H.apply_only(u, v, N, a, b) @@ -708,9 +714,11 @@ def hecke_images_nonquad_character_weight2(int u, int v, int N, indices, chi, R) # Allocate memory to hold images of (u,v) under all Heilbronn matrices a = sig_malloc(sizeof(int)*H.length) - if not a: raise MemoryError + if not a: + raise MemoryError b = sig_malloc(sizeof(int)*H.length) - if not b: raise MemoryError + if not b: + raise MemoryError # Compute images of (u,v) under all Heilbronn matrices H.apply_only(u, v, N, a, b) @@ -798,16 +806,19 @@ def hecke_images_quad_character_weight2(int u, int v, int N, indices, chi, R): # are the values of the character chi. _chivals = chi.values() cdef int *chi_vals = sig_malloc(sizeof(int)*len(_chivals)) - if not chi_vals: raise MemoryError + if not chi_vals: + raise MemoryError for i in range(len(_chivals)): chi_vals[i] = _chivals[i] for i, n in enumerate(indices): H = HeilbronnCremona(n) if is_prime(n) else HeilbronnMerel(n) a = sig_malloc(sizeof(int)*H.length) - if not a: raise MemoryError + if not a: + raise MemoryError b = sig_malloc(sizeof(int)*H.length) - if not b: raise MemoryError + if not b: + raise MemoryError H.apply_only(u, v, N, a, b) for j in range(H.length): @@ -893,9 +904,11 @@ def hecke_images_gamma0_weight_k(int u, int v, int i, int N, int k, indices, R): # Allocate memory to hold images of (u,v) under all Heilbronn matrices a = sig_malloc(sizeof(int)*H.length) - if not a: raise MemoryError + if not a: + raise MemoryError b = sig_malloc(sizeof(int)*H.length) - if not b: raise MemoryError + if not b: + raise MemoryError # Compute images of (u,v) under all Heilbronn matrices H.apply_only(u, v, N, a, b) diff --git a/src/sage/modular/modsym/manin_symbol.pyx b/src/sage/modular/modsym/manin_symbol.pyx index 0a4944b4ad8..86927b91959 100644 --- a/src/sage/modular/modsym/manin_symbol.pyx +++ b/src/sage/modular/modsym/manin_symbol.pyx @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -""" +r""" Manin symbols This module defines the class ManinSymbol. A Manin symbol of @@ -19,9 +19,7 @@ monomial Manin symbols to monomial Manin symbols, up to a scalar factor. For general matrices (such as `T=[0,1,-1,-1]` and `T^2=[-1,-1;0,1]`) the image of a monomial Manin symbol is expressed as a formal sum of monomial Manin symbols, with integer coefficients. - """ - from sage.modular.cusps import Cusp from sage.rings.all import Infinity, ZZ from sage.rings.integer cimport Integer diff --git a/src/sage/modular/modsym/p1list.pyx b/src/sage/modular/modsym/p1list.pyx index fbe461c269d..aaa8aa609d7 100644 --- a/src/sage/modular/modsym/p1list.pyx +++ b/src/sage/modular/modsym/p1list.pyx @@ -68,8 +68,10 @@ cdef int c_p1_normalize_int(int N, int u, int v, u = u % N v = v % N - if u<0: u = u + N - if v<0: v = v + N + if u < 0: + u = u + N + if v < 0: + v = v + N if u == 0: uu[0] = 0 if arith_int.c_gcd_int(v,N) == 1: @@ -81,7 +83,8 @@ cdef int c_p1_normalize_int(int N, int u, int v, g = arith_int.c_xgcd_int(u, N, &s, &t) s = s % N - if s<0: s = s + N + if s < 0: + s = s + N if arith_int.c_gcd_int(g, v) != 1: uu[0] = 0 vv[0] = 0 @@ -99,7 +102,8 @@ cdef int c_p1_normalize_int(int N, int u, int v, u = g v = (s*v) % N - min_v = v; min_t = 1 + min_v = v + min_t = 1 if g!=1: Ng = N/g vNg = (v*Ng) % N @@ -108,10 +112,13 @@ cdef int c_p1_normalize_int(int N, int u, int v, v = (v + vNg) % N t = (t + Ng) % N if v(ll_s % ll_N) t = (ll_t % ll_N) s = s % N - if s<0: s = s + N + if s < 0: + s = s + N if arith_int.c_gcd_int(g, v) != 1: uu[0] = 0 vv[0] = 0 @@ -355,7 +366,8 @@ cdef int c_p1_normalize_llong(int N, int u, int v, # v = (s*v) % N v = ( ((s) * (v)) % ll_N ) - min_v = v; min_t = 1 + min_v = v + min_t = 1 if g!=1: Ng = N/g vNg = ((v * Ng) % ll_N) @@ -364,16 +376,20 @@ cdef int c_p1_normalize_llong(int N, int u, int v, v = (v + vNg) % N t = (t + Ng) % N if v (arith_llong.c_inverse_mod_longlong(( s)*( min_t), N) % ll_N) return 0 + def p1_normalize_llong(N, u, v): r""" Computes the canonical representative of @@ -449,7 +465,8 @@ def p1list_llong(int N): [(0, 1), (34603, 1), (34603, 2), (34603, 3)] """ cdef int g, u, v, s, c, d, h, d1, cmax - if N==1: return [(0,0)] + if N == 1: + return [(0, 0)] lst = [(0,1)] c = 1 @@ -478,8 +495,9 @@ def p1list_llong(int N): lst.sort() return lst + def p1list(N): - """ + r""" Return the elements of the projective line modulo `N`, `\mathbb{P}^1(\ZZ/N\ZZ)`, as a plain list of 2-tuples. @@ -600,8 +618,10 @@ cdef int p1_normalize_xgcdtable(int N, int u, int v, u = u % N v = v % N - if u<0: u = u + N - if v<0: v = v + N + if u < 0: + u = u + N + if v < 0: + v = v + N if u == 0: uu[0] = 0 if t_g[v] == 1: # "if arith_int.c_gcd_int(v,N) == 1" @@ -616,7 +636,8 @@ cdef int p1_normalize_xgcdtable(int N, int u, int v, s = t_a[u] t = t_b[u] s = s % N - if s<0: s = s + N + if s < 0: + s = s + N if g != 1 and arith_int.c_gcd_int(g, v) != 1: uu[0] = 0 vv[0] = 0 @@ -634,7 +655,8 @@ cdef int p1_normalize_xgcdtable(int N, int u, int v, u = g v = (s*v) % N - min_v = v; min_t = 1 + min_v = v + min_t = 1 if g!=1: Ng = N/g vNg = (v*Ng) % N @@ -643,10 +665,13 @@ cdef int p1_normalize_xgcdtable(int N, int u, int v, v = (v + vNg) % N t = (t + Ng) % N if vcheck_allocarray(N, sizeof(int)) self.s = check_allocarray(N, sizeof(int)) self.t = check_allocarray(N, sizeof(int)) @@ -822,10 +847,10 @@ cdef class P1List(object): 'The projective line over the integers modulo 8' """ - return "The projective line over the integers modulo %s"%self.__N + return "The projective line over the integers modulo %s" % self.__N def lift_to_sl2z(self, int i): - """ + r""" Lift the `i`'th element of this P1list to an element of `SL(2,\ZZ)`. @@ -836,7 +861,6 @@ cdef class P1List(object): INPUT: - - ``i`` - integer (the index of the element to lift). EXAMPLES:: @@ -1187,8 +1211,9 @@ cdef class export: int compute_s) except -1: return c_p1_normalize_llong(N, u, v, uu, vv, ss, compute_s) + def lift_to_sl2z_int(int c, int d, int N): - """ + r""" Lift a pair `(c, d)` to an element of `SL(2, \ZZ)`. `(c,d)` is assumed to be an element of From 5bfe5e127f0f3371d146c363f64da045f1d5438f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Wed, 13 Apr 2022 08:38:55 +0200 Subject: [PATCH 2/2] using += where possible in p1list --- src/sage/modular/modsym/p1list.pyx | 34 +++++++++++++++--------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/sage/modular/modsym/p1list.pyx b/src/sage/modular/modsym/p1list.pyx index aaa8aa609d7..df75484734e 100644 --- a/src/sage/modular/modsym/p1list.pyx +++ b/src/sage/modular/modsym/p1list.pyx @@ -69,9 +69,9 @@ cdef int c_p1_normalize_int(int N, int u, int v, u = u % N v = v % N if u < 0: - u = u + N + u += N if v < 0: - v = v + N + v += N if u == 0: uu[0] = 0 if arith_int.c_gcd_int(v,N) == 1: @@ -84,7 +84,7 @@ cdef int c_p1_normalize_int(int N, int u, int v, g = arith_int.c_xgcd_int(u, N, &s, &t) s = s % N if s < 0: - s = s + N + s += N if arith_int.c_gcd_int(g, v) != 1: uu[0] = 0 vv[0] = 0 @@ -116,9 +116,9 @@ cdef int c_p1_normalize_int(int N, int u, int v, min_t = t v = min_v if u < 0: - u = u+N + u += N if v < 0: - v = v+N + v += N uu[0] = u vv[0] = v if compute_s: @@ -329,9 +329,9 @@ cdef int c_p1_normalize_llong(int N, int u, int v, u = u % N v = v % N if u < 0: - u = u + N + u += N if v < 0: - v = v + N + v += N if u == 0: uu[0] = 0 if arith_int.c_gcd_int(v,N) == 1: @@ -347,7 +347,7 @@ cdef int c_p1_normalize_llong(int N, int u, int v, t = (ll_t % ll_N) s = s % N if s < 0: - s = s + N + s += N if arith_int.c_gcd_int(g, v) != 1: uu[0] = 0 vv[0] = 0 @@ -380,9 +380,9 @@ cdef int c_p1_normalize_llong(int N, int u, int v, min_t = t v = min_v if u < 0: - u = u+N + u += N if v < 0: - v = v+N + v += N uu[0] = u vv[0] = v if compute_s: @@ -619,9 +619,9 @@ cdef int p1_normalize_xgcdtable(int N, int u, int v, u = u % N v = v % N if u < 0: - u = u + N + u += N if v < 0: - v = v + N + v += N if u == 0: uu[0] = 0 if t_g[v] == 1: # "if arith_int.c_gcd_int(v,N) == 1" @@ -637,7 +637,7 @@ cdef int p1_normalize_xgcdtable(int N, int u, int v, t = t_b[u] s = s % N if s < 0: - s = s + N + s += N if g != 1 and arith_int.c_gcd_int(g, v) != 1: uu[0] = 0 vv[0] = 0 @@ -669,9 +669,9 @@ cdef int p1_normalize_xgcdtable(int N, int u, int v, min_t = t v = min_v if u < 0: - u = u+N + u += N if v < 0: - v = v+N + v += N uu[0] = u vv[0] = v if compute_s: @@ -1274,7 +1274,7 @@ def lift_to_sl2z_int(int c, int d, int N): if g == 1: break m = m / g - d = d + N*m + d += N * m g = arith_int.c_xgcd_int(c, d, &z1, &z2) if g != 1: @@ -1344,7 +1344,7 @@ def lift_to_sl2z_llong(llong c, llong d, int N): if g == 1: break m = m / g - d = d + N*m + d += N * m g = arith_llong.c_xgcd_longlong(c, d, &z1, &z2) if g != 1: