From 40a941900c691bea86ef8a24923217bff11714b0 Mon Sep 17 00:00:00 2001 From: "Kiran S. Kedlaya" Date: Tue, 30 Apr 2024 18:22:55 -0700 Subject: [PATCH 1/2] Fix overflow in hypergeometric trace --- src/sage/modular/hypergeometric_misc.pyx | 31 +++++++++++++++-------- src/sage/modular/hypergeometric_motive.py | 6 +++++ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/sage/modular/hypergeometric_misc.pyx b/src/sage/modular/hypergeometric_misc.pyx index b8c39aeefa7..3be8e4dd545 100644 --- a/src/sage/modular/hypergeometric_misc.pyx +++ b/src/sage/modular/hypergeometric_misc.pyx @@ -4,7 +4,7 @@ significantly from Cythonization. """ from cpython cimport array from cysignals.signals cimport sig_check - +from sage.rings.integer cimport Integer cpdef hgm_coeffs(long long p, int f, int prec, gamma, m, int D, gtable, int gtable_prec, bint use_longs): @@ -37,7 +37,7 @@ cpdef hgm_coeffs(long long p, int f, int prec, gamma, m, int D, cdef int gl, j, k, l, v, gv cdef long long i, q1, w, w1, w2, q2, r, r1 - cdef bint flip + cdef bint flip, use_longlongs q1 = p ** f - 1 gl = len(gamma) @@ -55,6 +55,7 @@ cpdef hgm_coeffs(long long p, int f, int prec, gamma, m, int D, # for efficiency. flip = (f == 1 and prec == 1 and gtable_prec == 1) ans = [] + Rz = R.zero() if use_longs: q2 = p ** prec try: @@ -64,13 +65,15 @@ cpdef hgm_coeffs(long long p, int f, int prec, gamma, m, int D, for r in range(q1): gtab2[r] = gtable[r].lift() % q2 else: - gtab2 = array.array('q', [0]) * q1 - try: - for r in range(q1): - gtab2[r] = gtable[r] - except TypeError: - for r in range(q1): - gtab2[r] = gtable[r].lift() + use_longlongs = (Integer(p) ** prec < 2 ** 63) + if use_longlongs: + gtab2 = array.array('q', [0]) * q1 + try: + for r in range(q1): + gtab2[r] = gtable[r] + except TypeError: + for r in range(q1): + gtab2[r] = gtable[r].lift() if f == 1: for r in range(q1): digit_count[r] = r @@ -82,7 +85,6 @@ cpdef hgm_coeffs(long long p, int f, int prec, gamma, m, int D, w += r1 % p r1 //= p digit_count[r] = w - Rz = R.zero() ans = [None] * q1 for r in range(q1): sig_check() @@ -123,7 +125,7 @@ cpdef hgm_coeffs(long long p, int f, int prec, gamma, m, int D, else: for j in range(-gv): w1 = w1 * w2 % q2 - else: + elif use_longlongs: w2 = gtab2[r1] if gv > 0: for j in range(gv): @@ -131,6 +133,13 @@ cpdef hgm_coeffs(long long p, int f, int prec, gamma, m, int D, else: for j in range(-gv): u1 *= w2 + else: + if gv > 0: + for j in range(gv): + u *= gtable[r1] + else: + for j in range(-gv): + u1 *= gtable[r1] if use_longs: u = R(w) u1 = R(w1) diff --git a/src/sage/modular/hypergeometric_motive.py b/src/sage/modular/hypergeometric_motive.py index bcb719fef9b..c44e31d8adb 100644 --- a/src/sage/modular/hypergeometric_motive.py +++ b/src/sage/modular/hypergeometric_motive.py @@ -1389,6 +1389,12 @@ def padic_H_value(self, p, f, t, prec=None, cache_p=False): ....: print(s) p is tame + Check that :issue:`37910` is resolved:: + + sage: H = Hyp(alpha_beta=[[1/2,1/3,2/3,1/6,5/6], [0,0,0,0,0]]) + sage: H.padic_H_value(6211, 2, -512000) # long time + 2791959433670741 + REFERENCES: - [MagmaHGM]_ From e3426d7003e55f6f6e8175888a9c9f6cab056fd9 Mon Sep 17 00:00:00 2001 From: "Kiran S. Kedlaya" Date: Tue, 30 Apr 2024 18:28:07 -0700 Subject: [PATCH 2/2] Replace doctest with a faster example --- src/sage/modular/hypergeometric_motive.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sage/modular/hypergeometric_motive.py b/src/sage/modular/hypergeometric_motive.py index c44e31d8adb..347b1b4d44c 100644 --- a/src/sage/modular/hypergeometric_motive.py +++ b/src/sage/modular/hypergeometric_motive.py @@ -1391,9 +1391,9 @@ def padic_H_value(self, p, f, t, prec=None, cache_p=False): Check that :issue:`37910` is resolved:: - sage: H = Hyp(alpha_beta=[[1/2,1/3,2/3,1/6,5/6], [0,0,0,0,0]]) - sage: H.padic_H_value(6211, 2, -512000) # long time - 2791959433670741 + sage: H = Hyp(alpha_beta=[[1/2,1/2,1/2,1/2,1/2,1/3,2/3,1/6,5/6], [0,0,0,0,0,0,0,0,0]]) + sage: H.padic_H_value(151, 2, -512000) + 50178940126155881 REFERENCES: