# Ticket #12116: trac_12116-rebase3.patch

File trac_12116-rebase3.patch, 9.1 KB (added by roed, 8 years ago)
• ## sage/rings/factorint.pyx

```# HG changeset patch
# User David Roe <roed@math.harvard.edu>
# Date 1323005513 25200
# Node ID 92b26d29f4028403b4071d655d7497c92812cb58
#12116: changes to is_power and related functions in integer.pyx.

diff --git a/sage/rings/factorint.pyx b/sage/rings/factorint.pyx```
 a from sage.rings.fast_arith import prime_range from sage.structure.factorization_integer import IntegerFactorization from math import floor from sage.misc.superseded import deprecated_function_alias cdef extern from "limits.h": long LONG_MAX Fm = R(F.sqrt()*R(-1/m*tmp).exp()).round() return [Fm, Integer(round(F//Fm))] cpdef base_exponent(n): r""" Returns base and prime exponent of `n` if `n` is power. Otherwise return `n, 1`. INPUT: - ``n`` - integer OUTPUT: - ``base, exp`` - where ``n = base^exp`` and ``exp`` is prime or 1 EXAMPLES: sage: from sage.rings.factorint import base_exponent sage: base_exponent(101**29) (101, 29) sage: base_exponent(0) (0, 1) sage: base_exponent(-4) (-4, 1) sage: base_exponent(-27) (-3, 3) """ if n != 0: for p in prime_range(2 if n > 0 else 3,int(abs(n).log(2)+1)): tmp = n.nth_root(p,truncate_mode=1) if tmp[1]: return tmp[0], p return n,1 base_exponent = deprecated_function_alias(12116, lambda n: n.perfect_power()) cpdef factor_aurifeuillian(n): r""" cdef int exp = 1 for x in [-1, 1]: b = n + x while b.is_power(): tmp = base_exponent(b) b = tmp[0] exp *= tmp[1] exp, b = b.perfect_power() if exp > 1: if not b.is_prime(): continue
• ## sage/rings/integer.pyx

`diff --git a/sage/rings/integer.pyx b/sage/rings/integer.pyx`
 a - Vincent Delecroix (2010-12-28): added unicode in Integer.__init__ - David Roe (2012-03): deprecate :meth:`~sage.rings.integer.Integer.is_power` in favour of :meth:`~sage.rings.integer.Integer.is_perfect_power` (see :trac:`12116`) EXAMPLES: Add 2 integers:: import sage.libs.pari.all from sage.structure.element import canonical_coercion from sage.misc.superseded import deprecated_function_alias cdef object numpy_long_interface = {'typestr': '=i4' if sizeof(long) == 4 else '=i8' } cdef object numpy_int64_interface = {'typestr': '=i8'} """ return mpz_perfect_square_p(self.value) def is_power(self): is_power = deprecated_function_alias(12116, is_perfect_power) def perfect_power(self): r""" Returns ``True`` if self is a perfect power, i.e. if there exist integers `a` and `b`, `b > 1` with `self = a^b`. Returns ``(a, b)``, where this integer is `a^b` and `b` is maximal. If called on `-1`, `0` or `1`, `b` will be `1`, since there is no maximal value of `b`. .. seealso:: - :meth:`is_perfect_power`: testing whether an integer is a perfect power is usually faster than finding `a` and `b`. - :meth:`is_prime_power`: checks whether the base is prime. - :meth:`is_power_of`: if you know the base already, this method is the fastest option. EXAMPLES:: sage: Integer(-27).is_power() True sage: Integer(12).is_power() False """ return mpz_perfect_power_p(self.value) sage: 144.perfect_power() (12, 2) sage: 1.perfect_power() (1, 1) sage: 0.perfect_power() (0, 1) sage: (-1).perfect_power() (-1, 1) sage: (-8).perfect_power() (-2, 3) sage: (-4).perfect_power() (-4, 1) sage: (101^29).perfect_power() (101, 29) sage: (-243).perfect_power() (-3, 5) sage: (-64).perfect_power() (-4, 3) """ parians = self._pari_().ispower() return Integer(parians[1]), Integer(parians[0]) cdef bint _is_power_of(Integer self, Integer n): r""" r""" Returns ``True`` if there is an integer b with `\mathtt{self} = n^b`. .. seealso:: - :meth:`perfect_power`: Finds the minimal base for which this integer is a perfect power. - :meth:`is_perfect_power`: If you don't know the base but just want to know if this integer is a perfect power, use this function. - :meth:`is_prime_power`: Checks whether the base is prime. EXAMPLES:: .. note:: For large integers self, is_power_of() is faster than is_power(). The following examples gives some indication of is_perfect_power(). The following examples gives some indication of how much faster. :: sage: b.exact_log(2) 14446 sage: t=cputime() sage: for a in range(2, 1000): k = b.is_power() sage: for a in range(2, 1000): k = b.is_perfect_power() sage: cputime(t)      # random 0.53203299999999976 sage: t=cputime() sage: b.exact_log(2) 1437 sage: t=cputime() sage: for a in range(2, 10000): k = b.is_power() # note that we change the range from the example above sage: for a in range(2, 10000): k = b.is_perfect_power() # note that we change the range from the example above sage: cputime(t)      # random 0.17201100000000036 sage: t=cputime(); TWO=int(2) def is_prime_power(self, flag=0): r""" Returns True if `x` is a prime power, and False otherwise. Returns True if this integer is a prime power, and False otherwise. INPUT: -  ``flag`` (for primality testing) - int - ``0`` (default): use a combination of algorithms. - ``1``: certify primality using the Pocklington-Lehmer test. - ``2``: certify primality using the APRCL test. - ``flag`` (for primality testing) - int. Values are: - ``0`` (default): use a combination of algorithms. - ``1``: certify primality using the Pocklington-Lehmer test. - ``2``: certify primality using the APRCL test. .. seealso:: - :meth:`perfect_power`: Finds the minimal base for which integer is a perfect power. - :meth:`is_perfect_power`: Doesn't test whether the base is prime. - :meth:`is_power_of`: If you know the base already this method is the fastest option. EXAMPLES:: sage: (10000).is_prime_power(flag=1) False We check that \#4777 is fixed:: We check that :trac:`4777` is fixed:: sage: n = 150607571^14 sage: n.is_prime_power() if not self.is_perfect_power(): return False k, g = self._pari_().ispower() if not k: if k == 1: raise RuntimeError, "Inconsistent results between GMP and PARI" return g.isprime(flag=flag) def is_perfect_power(self): r""" Returns ``True`` if self is a perfect power. Returns ``True`` if ``self`` is a perfect power, ie if there exist integers `a` and `b`, `b > 1` with ``self`` `= a^b`. .. seealso:: - :meth:`perfect_power`: Finds the minimal base for which this integer is a perfect power. - :meth:`is_power_of`: If you know the base already this method is the fastest option. - :meth:`is_prime_power`: Checks whether the base is prime. EXAMPLES:: sage: Integer(-27).is_perfect_power() True sage: Integer(12).is_perfect_power() False sage: z = 8 sage: z.is_perfect_power() True sage: (-4).is_perfect_power() False This is a test to make sure we work around a bug in GMP, see trac \#4612. sage: (4).is_power() doctest:...: DeprecationWarning: is_power is deprecated. Please use is_perfect_power instead. See http://trac.sagemath.org/12116 for details. True TESTS: This is a test to make sure we work around a bug in GMP, see :trac:`4612`. ::