Skip to content

Commit

Permalink
Deprecate PARI nth_prime and prime_list
Browse files Browse the repository at this point in the history
  • Loading branch information
jdemeyer committed Mar 16, 2016
1 parent 5fb408d commit d5c934c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 53 deletions.
7 changes: 5 additions & 2 deletions src/sage/arith/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3784,6 +3784,8 @@ def nth_prime(n):
5
sage: nth_prime(10)
29
sage: nth_prime(10^7)
179424673
::
Expand All @@ -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"""
Expand Down
2 changes: 1 addition & 1 deletion src/sage/functions/prime_pi.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ cpdef Integer legendre_phi(x, a):
# Deal with the general case
if (<PrimePi>prime_pi).__smallPi == NULL:
(<PrimePi>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)
(<PrimePi>prime_pi)._init_primes(z)
if not sig_on_no_except():
Expand Down
107 changes: 57 additions & 50 deletions src/sage/libs/pari/pari_instance.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
Expand Down
22 changes: 22 additions & 0 deletions src/sage/libs/pari/tests.py
Original file line number Diff line number Diff line change
@@ -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]
"""

0 comments on commit d5c934c

Please sign in to comment.