# Ticket #864: trac_864-pari_int_long_conversion.patch

File trac_864-pari_int_long_conversion.patch, 3.6 KB (added by pbruin, 8 years ago)
• ## sage/libs/pari/gen.pyx

```# HG changeset patch
# User Peter Bruin <P.Bruin@warwick.ac.uk>
# Date 1383048543 0
# Node ID c2ec2f5c1b6d54a1288b517a7a8e8b1ffbfd6095
# Parent  909180b6bbccd52134ebdcd60618a9cb6d10c926
Trac 864: speed up conversion from PARI to Python int/long

diff --git a/sage/libs/pari/gen.pyx b/sage/libs/pari/gen.pyx```
 a def __int__(gen self): """ Return Python int. Very fast, and if the number is too large to fit into a C int, a Python long is returned instead. Convert ``self`` to a Python integer. If the number is too large to fit into a Pyhon ``int``, a Python ``long`` is returned instead. EXAMPLES:: -2147483648 sage: int(pari("Pol(10)")) 10 """ cdef GEN x cdef long lx, *xp if  typ(self.g)==t_POL and self.poldegree()<=0: # Change a constant polynomial to its constant term x = constant_term(self.g) else: x = self.g if typ(x) != t_INT: raise TypeError, "gen must be of PARI type t_INT or t_POL of degree 0" if not signe(x): return 0 lx = lgefint(x)-3   # take 1 to account for the MSW xp = int_MSW(x) # special case 1 word so we return int if it fits if not lx: if   signe(x) |  xp[0] > 0:     # both positive return xp[0] elif signe(x) & -xp[0] < 0:     # both negative return -xp[0] i = xp[0] while lx: xp = int_precW(xp) i = i << BITS_IN_LONG | xp[0] lx = lx-1 if signe(x) > 0: return i else: return -i # NOTE: Could use int_unsafe below, which would be much faster, but # the default PARI prints annoying stuff to the screen when # the number is large. sage: int(pari("Mod(2, 7)")) 2 """ return int(Integer(self)) def int_unsafe(gen self): """ def __long__(gen self): """ Return Python long. """ return long(int(self)) Convert ``self`` to a Python ``long``. EXAMPLES:: sage: long(pari(0)) 0L sage: long(pari(10)) 10L sage: long(pari(-10)) -10L sage: long(pari(123456789012345678901234567890)) 123456789012345678901234567890L sage: long(pari(-123456789012345678901234567890)) -123456789012345678901234567890L sage: long(pari(2^31-1)) 2147483647L sage: long(pari(-2^31)) -2147483648L sage: long(pari("Pol(10)")) 10L sage: long(pari("Mod(2, 7)")) 2L """ return long(Integer(self)) def __float__(gen self): """
• ## sage/rings/finite_rings/element_ext_pari.py

`diff --git a/sage/rings/finite_rings/element_ext_pari.py b/sage/rings/finite_rings/element_ext_pari.py`
 a sage: int(a) Traceback (most recent call last): ... TypeError: gen must be of PARI type t_INT or t_POL of degree 0 TypeError: Unable to coerce PARI a to an Integer """ try: return int(self.__value.lift().lift())