From d5c934c5164255e352cee9ddbc780d0759b6e185 Mon Sep 17 00:00:00 2001 From: Jeroen Demeyer Date: Tue, 15 Mar 2016 18:08:24 +0100 Subject: [PATCH] Deprecate PARI nth_prime and prime_list --- src/sage/arith/misc.py | 7 +- src/sage/functions/prime_pi.pyx | 2 +- src/sage/libs/pari/pari_instance.pyx | 107 ++++++++++++++------------- src/sage/libs/pari/tests.py | 22 ++++++ 4 files changed, 85 insertions(+), 53 deletions(-) create mode 100644 src/sage/libs/pari/tests.py diff --git a/src/sage/arith/misc.py b/src/sage/arith/misc.py index b705bc52940..08597be6266 100644 --- a/src/sage/arith/misc.py +++ b/src/sage/arith/misc.py @@ -3784,6 +3784,8 @@ def nth_prime(n): 5 sage: nth_prime(10) 29 + sage: nth_prime(10^7) + 179424673 :: @@ -3796,9 +3798,10 @@ def nth_prime(n): sage: all(prime_pi(nth_prime(j)) == j for j in range(1, 1000, 10)) True - """ - return ZZ(pari.nth_prime(n)) + if n <= 0: + raise ValueError("nth prime meaningless for non-positive n (=%s)" % n) + return ZZ(pari.prime(n)) def quadratic_residues(n): r""" diff --git a/src/sage/functions/prime_pi.pyx b/src/sage/functions/prime_pi.pyx index 72a0140b707..28a485e72e9 100644 --- a/src/sage/functions/prime_pi.pyx +++ b/src/sage/functions/prime_pi.pyx @@ -543,7 +543,7 @@ cpdef Integer legendre_phi(x, a): # Deal with the general case if (prime_pi).__smallPi == NULL: (prime_pi)._init_tables() - cdef uint32_t z = pari.nth_prime(a) + cdef uint32_t z = pari.prime(a) if z >= y: return Integer(1) (prime_pi)._init_primes(z) if not sig_on_no_except(): diff --git a/src/sage/libs/pari/pari_instance.pyx b/src/sage/libs/pari/pari_instance.pyx index e54ac5c632d..14a5804ba97 100644 --- a/src/sage/libs/pari/pari_instance.pyx +++ b/src/sage/libs/pari/pari_instance.pyx @@ -192,7 +192,7 @@ from sage.libs.flint.fmpz_mat cimport * from sage.libs.pari.gen cimport gen, objtogen from sage.libs.pari.handle_error cimport _pari_init_error_handling -from sage.misc.superseded import deprecated_function_alias +from sage.misc.superseded import deprecation, deprecated_function_alias # real precision in decimal digits: see documentation for # get_real_precision() and set_real_precision(). This variable is used @@ -1338,78 +1338,83 @@ cdef class PariInstance(PariInstance_auto): sig_on() return self.new_gen(gp_read_file(filename)) - def prime_list(self, long n): + def primes(self, n=None, end=None): """ - prime_list(n): returns list of the first n primes - - To extend the table of primes use pari.init_primes(M). + Return the vector of the first `n` primes, the primes in the + interval `[n, end]` or the list of primes up to `end`. INPUT: + Either - - ``n`` - C long + - ``n`` -- integer + or - OUTPUT: + - ``n`` -- list or tuple `[a, b]` defining an interval of primes + + or + + - ``n, end`` -- start and end point of an interval of primes + or - - ``gen`` - PARI list of first n primes + - ``end`` -- end point for the list of primes + OUTPUT: a PARI list of prime numbers EXAMPLES:: - sage: pari.prime_list(0) - [] - sage: pari.prime_list(-1) - [] - sage: pari.prime_list(3) + sage: pari.primes(3) [2, 3, 5] - sage: pari.prime_list(10) + sage: pari.primes(10) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] - sage: pari.prime_list(20) + sage: pari.primes(20) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71] - sage: len(pari.prime_list(1000)) + sage: len(pari.primes(1000)) 1000 - """ - if n >= 2: - self.nth_prime(n) - sig_on() - return self.new_gen(primes(n)) - - def primes_up_to_n(self, long n): - """ - Return the primes <= n as a pari list. + sage: pari.primes(11,29) + [11, 13, 17, 19, 23, 29] + sage: pari.primes((11,29)) + [11, 13, 17, 19, 23, 29] + sage: pari.primes(end=29) + [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] + sage: pari.primes(10^30, 10^30 + 100) + [1000000000000000000000000000057, 1000000000000000000000000000099] - EXAMPLES:: + TESTS:: - sage: pari.primes_up_to_n(1) + sage: pari.primes(0) + [] + sage: pari.primes(-1) + [] + sage: pari.primes(end=1) + [] + sage: pari.primes(end=-1) + [] + sage: pari.primes(3,2) [] - sage: pari.primes_up_to_n(20) - [2, 3, 5, 7, 11, 13, 17, 19] - """ - if n <= 1: - return pari([]) - self.init_primes(n+1) - return self.prime_list(pari(n).primepi()) - - def __nth_prime(self, long n): - """ - nth_prime(n): returns the n-th prime, where n is a C-int """ - if n <= 0: - raise ValueError("nth prime meaningless for non-positive n (=%s)" % n) - cdef GEN g + cdef gen t0, t1 + if end is None: + t0 = objtogen(n) + sig_on() + return self.new_gen(primes0(t0.g)) + elif n is None: + t0 = self.PARI_TWO # First prime + else: + t0 = objtogen(n) + t1 = objtogen(end) sig_on() - g = prime(n) - return self.new_gen(g) + return self.new_gen(primes_interval(t0.g, t1.g)) - def nth_prime(self, long n): - from sage.libs.pari.all import PariError - try: - return self.__nth_prime(n) - except PariError: - self.init_primes(max(2*maxprime(), 20*n)) - return self.nth_prime(n) + def primes_up_to_n(self, n): + deprecation(20216, "pari.primes_up_to_n(n) is deprecated, use pari.primes(end=n) instead") + return self.primes(end=n) + + prime_list = deprecated_function_alias(20216, primes) + + nth_prime = deprecated_function_alias(20216, PariInstance_auto.prime) def euler(self, unsigned long precision=0): """ @@ -1474,6 +1479,8 @@ cdef class PariInstance(PariInstance_auto): sig_on() return self.new_gen(polchebyshev1(n, self.get_var(v))) + # Deprecated by upstream PARI: do not remove this deprecated alias + # as long as it exists in PARI. poltchebi = deprecated_function_alias(18203, polchebyshev) def factorial(self, long n): diff --git a/src/sage/libs/pari/tests.py b/src/sage/libs/pari/tests.py new file mode 100644 index 00000000000..d76a513c42d --- /dev/null +++ b/src/sage/libs/pari/tests.py @@ -0,0 +1,22 @@ +r""" +Tests for the Sage <-> PARI interface + +Deprecation checks:: + + sage: pari.poltchebi(10) + doctest:...: DeprecationWarning: poltchebi is deprecated. Please use polchebyshev instead. + See http://trac.sagemath.org/18203 for details. + 512*x^10 - 1280*x^8 + 1120*x^6 - 400*x^4 + 50*x^2 - 1 + sage: pari.nth_prime(10) + doctest:...: DeprecationWarning: nth_prime is deprecated. Please use prime instead. + See http://trac.sagemath.org/20216 for details. + 29 + sage: pari.prime_list(10) + doctest:...: DeprecationWarning: prime_list is deprecated. Please use primes instead. + See http://trac.sagemath.org/20216 for details. + [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] + sage: pari.primes_up_to_n(20) + doctest:...: DeprecationWarning: pari.primes_up_to_n(n) is deprecated, use pari.primes(end=n) instead + See http://trac.sagemath.org/20216 for details. + [2, 3, 5, 7, 11, 13, 17, 19] +"""