Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some cython-linting in modular/ folder #35516

Merged
merged 3 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/sage/modular/arithgroup/arithgroup_element.pyx
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
"""
Elements of arithmetic subgroups
"""

################################################################################
# ##############################################################################
#
# Copyright (C) 2009, The Sage Group -- http://www.sagemath.org/
# Copyright (C) 2009, The Sage Group -- https://www.sagemath.org/
#
# Distributed under the terms of the GNU General Public License (GPL)
#
# The full text of the GPL is available at:
#
# https://www.gnu.org/licenses/
#
################################################################################
# ##############################################################################

from sage.structure.element cimport MultiplicativeGroupElement, MonoidElement, Element
from sage.structure.element cimport MultiplicativeGroupElement
from sage.structure.richcmp cimport richcmp
from sage.rings.integer_ring import ZZ
from sage.modular.cusps import Cusp

from sage.matrix.matrix_space import MatrixSpace
from sage.matrix.matrix_integer_dense cimport Matrix_integer_dense
Expand Down Expand Up @@ -109,7 +107,7 @@ cdef class ArithmeticSubgroupElement(MultiplicativeGroupElement):
sage: unpickle_build(si, (Gamma0(13), {'_ArithmeticSubgroupElement__x': x}))
"""
from .congroup_sl2z import SL2Z
oldparent, kwdict = state
_, kwdict = state
self._parent = SL2Z
if '_ArithmeticSubgroupElement__x' in kwdict:
self.__x = M2Z(kwdict['_ArithmeticSubgroupElement__x'])
Expand Down
41 changes: 21 additions & 20 deletions src/sage/modular/arithgroup/congroup.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ def degeneracy_coset_representatives_gamma0(int N, int M, int t):
14
"""
if N % M != 0:
raise ArithmeticError("M (=%s) must be a divisor of N (=%s)" % (M,N))
raise ArithmeticError(f"M (={M}) must be a divisor of N (={N})")

if (N/M) % t != 0:
raise ArithmeticError("t (=%s) must be a divisor of N/M (=%s)"%(t,N/M))
if (N // M) % t != 0:
raise ArithmeticError(f"t (={t}) must be a divisor of N/M (={N//M})")

cdef int n, i, j, k, aa, bb, cc, dd, g, Ndivt, halfmax, is_new
cdef int* R
Expand All @@ -122,7 +122,7 @@ def degeneracy_coset_representatives_gamma0(int N, int M, int t):
while k < n:
# try to find another coset representative.
cc = M*random.randrange(-halfmax, halfmax+1)
dd = 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
Expand All @@ -134,8 +134,8 @@ def degeneracy_coset_representatives_gamma0(int N, int M, int t):
is_new = 1
for i from 0 <= i < k:
j = 4*i
if (R[j+1]*aa - R[j]*bb)%t == 0 and \
(R[j+3]*cc - R[j+2]*dd)%Ndivt == 0:
if (R[j+1]*aa - R[j]*bb) % t == 0 and \
(R[j+3]*cc - R[j+2]*dd) % Ndivt == 0:
is_new = 0
break
# If our matrix is new add it to the list.
Expand All @@ -148,12 +148,13 @@ def degeneracy_coset_representatives_gamma0(int N, int M, int t):

# Return the list left multiplied by T.
S = []
for i from 0 <= i < k:
for i in range(k):
j = 4*i
S.append([R[j], R[j+1], R[j+2]*t, R[j+3]*t])
sig_free(R)
return S


def degeneracy_coset_representatives_gamma1(int N, int M, int t):
r"""
Let `N` be a positive integer and `M` a divisor of `N`. Let `t` be a
Expand Down Expand Up @@ -204,29 +205,28 @@ def degeneracy_coset_representatives_gamma1(int N, int M, int t):
sage: len(degeneracy_coset_representatives_gamma1(13, 1, 13))
168
"""

if N % M != 0:
raise ArithmeticError("M (=%s) must be a divisor of N (=%s)" % (M,N))
raise ArithmeticError(f"M (={M}) must be a divisor of N (={N})")

if (N/M) % t != 0:
raise ArithmeticError("t (=%s) must be a divisor of N/M (=%s)"%(t,N/M))
if (N // M) % t != 0:
raise ArithmeticError(f"t (={t}) must be a divisor of N/M (={N//M})")

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)
d = arith_int.c_gcd_int(t, N // t)
n = n / d
k = 0 # number found so far
Ndivt = N / t
Ndivt = N // t
R = <int*>check_allocarray(4 * n, sizeof(int))
halfmax = 2*(n+10)
while k < n:
# try to find another coset representative.
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)
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
cc = cc / g
Expand All @@ -239,10 +239,10 @@ def degeneracy_coset_representatives_gamma1(int N, int M, int t):
is_new = 1
for i from 0 <= i < k:
j = 4*i
if (R[j] - aa)%t == 0 and \
(R[j+1] - bb)%t == 0 and \
(R[j+2] - cc)%(Ndivt) == 0 and \
(R[j+3] - dd)%(Ndivt) == 0:
if (R[j] - aa) % t == 0 and \
(R[j+1] - bb) % t == 0 and \
(R[j+2] - cc) % Ndivt == 0 and \
(R[j+3] - dd) % Ndivt == 0:
is_new = 0
break
# If our matrix is new add it to the list.
Expand All @@ -264,6 +264,7 @@ def degeneracy_coset_representatives_gamma1(int N, int M, int t):
sig_free(R)
return S


def generators_helper(coset_reps, level):
r"""
Helper function for generators of Gamma0, Gamma1 and GammaH.
Expand Down
30 changes: 13 additions & 17 deletions src/sage/modular/arithgroup/farey_symbol.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ from .congroup_sl2z import SL2Z
from sage.modular.cusps import Cusp

from sage.misc.decorators import options, rename_keyword
from sage.misc.latex import latex
from sage.misc.lazy_attribute import lazy_attribute
from sage.misc.cachefunc import cached_method
from sage.structure.richcmp cimport richcmp_not_equal

Expand Down Expand Up @@ -338,12 +336,12 @@ cdef class Farey:
for i,g in enumerate(self.generators()):
m = g.matrix()
if (-m).is_one():
return [i+1]
return [i + 1]
t = m.trace()
if t == 0: # order = 4
return 2 * [i+1]
elif t == 1: # order = 6
return 3 * [i+1]
if t == 0: # order = 4
return 2 * [i + 1]
elif t == 1: # order = 6
return 3 * [i + 1]
return []

def word_problem(self, M, output = 'standard', check = True):
Expand Down Expand Up @@ -443,13 +441,12 @@ cdef class Farey:
raise ValueError('Unrecognized output format')
if check:
if M not in self.group:
raise ValueError("Matrix ( %s ) is not in group ( %s )"%(M,self.group))
raise ValueError("Matrix ( %s ) is not in group ( %s )" % (M, self.group))
cdef Integer a = M.d()
cdef Integer b = -M.b()
cdef Integer c = -M.c()
cdef Integer d = M.a()
cdef cpp_SL2Z *cpp_beta = new cpp_SL2Z(1,0,0,1)
E = SL2Z([-1,0,0,-1])
cdef cpp_SL2Z *cpp_beta = new cpp_SL2Z(1, 0, 0, 1)
sig_on()
result = self.this_ptr.word_problem(a.value, b.value, c.value, d.value, cpp_beta)
sig_off()
Expand Down Expand Up @@ -505,13 +502,13 @@ cdef class Farey:
for i in range(len(tietze)):
t = tietze[i]
tmp = tmp * gens[t-1] if t > 0 else tmp * gens[-t-1]**-1
assert tmp.matrix() == M.matrix(),'%s %s %s'%(tietze, tmp.matrix(),M.matrix())
assert tmp.matrix() == M.matrix(),'%s %s %s' % (tietze, tmp.matrix(),M.matrix())
if output == 'standard':
return tuple(tietze)
if output == 'syllables':
return tuple((a-1,len(list(g))) if a > 0 else (-a-1,-len(list(g))) for a,g in groupby(tietze))
else: # output == 'gens'
return tuple((gens[a-1],len(list(g))) if a > 0 else (gens[-a-1],-len(list(g))) for a,g in groupby(tietze))
else: # output == 'gens'
return tuple((gens[a-1],len(list(g))) if a > 0 else (gens[-a-1],-len(list(g))) for a, g in groupby(tietze))

def __contains__(self, M):
r"""
Expand Down Expand Up @@ -957,17 +954,16 @@ cdef class Farey:

sage: FareySymbol(Gamma0(23)).fundamental_domain(tesselation='coset', linestyle=':', thickness='2')
Graphics object consisting of 58 graphics primitives

"""
from sage.plot.all import Graphics
from sage.plot.colors import rainbow, to_mpl_color
from sage.plot.all import hyperbolic_arc, hyperbolic_triangle, text
from sage.plot.colors import rainbow
from sage.plot.all import hyperbolic_arc, hyperbolic_triangle

I = CC(0, 1)
w = RR(3).sqrt()
L = 1000
g = Graphics()
## show coset
# show coset
for x in self.coset_reps():
a, b, c, d = x[1, 1], -x[0, 1], -x[1, 0], x[0, 0]
A, B = CC(0, L), CC(0, L)
Expand Down
1 change: 0 additions & 1 deletion src/sage/modular/hypergeometric_misc.pxd
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
cpdef hgm_coeffs(long long p, int f, int prec, gamma, m, int D,
gtable, int gtable_prec, bint use_longs)

7 changes: 3 additions & 4 deletions src/sage/modular/hypergeometric_misc.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ cpdef hgm_coeffs(long long p, int f, int prec, gamma, m, int D,
sage: H = Hyp(cyclotomic=[[10,2],[1,1,1,1,1]])
sage: u = H.euler_factor(2,79) # indirect doctest
sage: u.reverse().is_weil_polynomial()
True

True
"""
from sage.rings.padics.factory import Zp

cdef int gl, j, k, l, v, gv
cdef long long i, q1, w, w1, w2, q2, r, r1
cdef bint flip, need_lift
cdef bint flip

q1 = p ** f - 1
gl = len(gamma)
Expand Down Expand Up @@ -118,7 +117,7 @@ cpdef hgm_coeffs(long long p, int f, int prec, gamma, m, int D,
gv = -gv
if use_longs:
w2 = gtab2[r1] # cast to long long to avoid overflow
if gv > 0:
if gv > 0:
for j in range(gv):
w = w * w2 % q2
else:
Expand Down
4 changes: 1 addition & 3 deletions src/sage/modular/modform/eis_series_cython.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ from cysignals.memory cimport check_allocarray, sig_free
from cysignals.signals cimport sig_check

from sage.arith.misc import primes, bernoulli
from sage.rings.rational_field import QQ
from sage.rings.power_series_ring import PowerSeriesRing
from sage.rings.integer cimport Integer
from sage.rings.fast_arith cimport prime_range

Expand Down Expand Up @@ -165,7 +163,7 @@ cpdef eisenstein_series_poly(int k, int prec = 10) :
cdef int i
cdef Fmpz_poly res = Fmpz_poly.__new__(Fmpz_poly)

if k%2 or k < 2:
if k % 2 or k < 2:
raise ValueError("k (=%s) must be an even positive integer" % k)
if prec < 0:
raise ValueError("prec (=%s) must be an even nonnegative integer" % prec)
Expand Down
1 change: 0 additions & 1 deletion src/sage/modular/modform/l_series_gross_zagier_coeffs.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def bqf_theta_series(Q, long bound, var=None):


cdef long* bqf_theta_series_c(long* terms, long bound, long a, long b, long c) except NULL:
cdef long i
cdef long x, y, yD
cdef long xmax, ymin, ymax
cdef double sqrt_yD
Expand Down
3 changes: 2 additions & 1 deletion src/sage/modular/modsym/apply.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ cdef class Apply:
cdef int apply_to_monomial_flint(self, fmpz_poly_t ans, int i, int j,
int a, int b, int c, int d) except -1:
if i < 0 or j - i < 0:
raise ValueError("i (=%s) and j-i (=%s) must both be nonnegative."%(i,j-i))
raise ValueError("i (=%s) and j-i (=%s) must both be nonnegative." % (i, j - i))

# f = b+a*x, g = d+c*x
fmpz_poly_set_coeff_si(self.f, 0, b)
Expand All @@ -61,6 +61,7 @@ cdef class Apply:

cdef Apply A = Apply()


def apply_to_monomial(int i, int j, int a, int b, int c, int d):
r"""
Return a list of the coefficients of
Expand Down
Loading