Ticket #12116: trac_12116-rebase.patch

File trac_12116-rebase.patch, 9.1 KB (added by roed, 8 years ago)

Rebased against 5.8.beta0

  • sage/rings/factorint.pyx

    # HG changeset patch
    # User David Roe <roed@math.harvard.edu>
    # Date 1323005513 25200
    # Node ID 9e0dc2c19fc7be2b56005879a1b035c8a17cac16
    # Parent  cfad24271909d7b5d61417602cff6f14f670b8ca
    #12116: changes to is_power and related functions in integer.pyx.
    
    diff --git a/sage/rings/factorint.pyx b/sage/rings/factorint.pyx
    a b  
    2424from sage.rings.fast_arith import prime_range
    2525from sage.structure.factorization_integer import IntegerFactorization
    2626from math import floor
     27from sage.misc.superseded import deprecated_function_alias
    2728
    2829cdef extern from "limits.h":
    2930    long LONG_MAX
     
    103104    Fm = R(F.sqrt()*R(-1/m*tmp).exp()).round()
    104105    return [Fm, Integer(round(F//Fm))]
    105106
    106 cpdef base_exponent(n):
    107     r"""
    108     Returns base and prime exponent of `n` if `n` is power.
    109     Otherwise return `n, 1`.
    110 
    111     INPUT:
    112 
    113     - ``n`` - integer
    114 
    115     OUTPUT:
    116 
    117     - ``base, exp`` - where ``n = base^exp`` and ``exp`` is prime or 1
    118 
    119     EXAMPLES:
    120         sage: from sage.rings.factorint import base_exponent
    121         sage: base_exponent(101**29)
    122         (101, 29)
    123         sage: base_exponent(0)
    124         (0, 1)
    125         sage: base_exponent(-4)
    126         (-4, 1)
    127         sage: base_exponent(-27)
    128         (-3, 3)
    129     """
    130     if n != 0:
    131         for p in prime_range(2 if n > 0 else 3,int(abs(n).log(2)+1)):
    132             tmp = n.nth_root(p,truncate_mode=1)
    133             if tmp[1]:
    134                 return tmp[0], p
    135     return n,1
     107base_exponent = deprecated_function_alias(12116, lambda n: (n.perfect_power()[1], n.perfect_power()[0]))
    136108
    137109cpdef factor_aurifeuillian(n):
    138110    r"""
     
    173145    cdef int exp = 1
    174146    for x in [-1, 1]:
    175147        b = n + x
    176         while b.is_power():
    177             tmp = base_exponent(b)
    178             b = tmp[0]
    179             exp *= tmp[1]
     148        exp, b = b.perfect_power()
    180149        if exp > 1:
    181150            if not b.is_prime():
    182151                continue
  • sage/rings/integer.pyx

    diff --git a/sage/rings/integer.pyx b/sage/rings/integer.pyx
    a b  
    4343
    4444- Vincent Delecroix (2010-12-28): added unicode in Integer.__init__
    4545
     46- David Roe (2012-03): deprecate :meth:`~sage.rings.integer.Integer.is_power`
     47  in favour of :meth:`~sage.rings.integer.Integer.is_perfect_power` (see
     48  :trac:`12116`)
     49
    4650EXAMPLES:
    4751
    4852Add 2 integers::
     
    175179import sage.libs.pari.all
    176180
    177181from sage.structure.element import canonical_coercion
     182from sage.misc.superseded import deprecated_function_alias
    178183
    179184cdef object numpy_long_interface = {'typestr': '=i4' if sizeof(long) == 4 else '=i8' }
    180185cdef object numpy_int64_interface = {'typestr': '=i8'}
     
    40454050        """
    40464051        return mpz_perfect_square_p(self.value)
    40474052
    4048     def is_power(self):
     4053    is_power = deprecated_function_alias(12116, is_perfect_power)
     4054
     4055    def perfect_power(self):
    40494056        r"""
    4050         Returns ``True`` if self is a perfect power, i.e. if there exist
    4051         integers `a` and `b`, `b > 1` with `self = a^b`.
    4052        
     4057        Returns ``(a, b)``, where this integer is `b^a` and `a` is maximal.
     4058
     4059        If called on `-1`, `0` or `1`, `a` will be `1`, since there is no
     4060        maximal value of `a`.
     4061
     4062        .. seealso::
     4063
     4064            - :meth:`is_perfect_power`: testing whether an integer is a perfect
     4065              power is usually faster than finding `a` and `b`.
     4066            - :meth:`is_prime_power`: checks whether the base is prime.
     4067            - :meth:`is_power_of`: if you know the base already, this method is
     4068              the fastest option.
     4069
    40534070        EXAMPLES::
    4054        
    4055             sage: Integer(-27).is_power()
    4056             True
    4057             sage: Integer(12).is_power()
    4058             False
    4059         """
    4060         return mpz_perfect_power_p(self.value)
     4071
     4072            sage: 144.perfect_power()
     4073            (2, 12)
     4074            sage: 1.perfect_power()
     4075            (1, 1)
     4076            sage: 0.perfect_power()
     4077            (1, 0)
     4078            sage: (-1).perfect_power()
     4079            (1, -1)
     4080            sage: (-8).perfect_power()
     4081            (3, -2)
     4082            sage: (-4).perfect_power()
     4083            (1, -4)
     4084            sage: (101^29).perfect_power()
     4085            (29, 101)
     4086            sage: (-243).perfect_power()
     4087            (5, -3)
     4088            sage: (-64).perfect_power()
     4089            (3, -4)
     4090        """
     4091        parians = self._pari_().ispower()
     4092        return Integer(parians[0]), Integer(parians[1])
    40614093
    40624094    cdef bint _is_power_of(Integer self, Integer n):
    40634095        r"""
     
    41834215        r"""
    41844216        Returns ``True`` if there is an integer b with
    41854217        `\mathtt{self} = n^b`.
     4218
     4219        .. seealso::
     4220
     4221            - :meth:`perfect_power`: Finds the minimal base for which this
     4222              integer is a perfect power.
     4223            - :meth:`is_perfect_power`: If you don't know the base but just
     4224              want to know if this integer is a perfect power, use this
     4225              function.
     4226            - :meth:`is_prime_power`: Checks whether the base is prime.
    41864227       
    41874228        EXAMPLES::
    41884229       
     
    42174258        .. note::
    42184259
    42194260           For large integers self, is_power_of() is faster than
    4220            is_power(). The following examples gives some indication of
     4261           is_perfect_power(). The following examples gives some indication of
    42214262           how much faster.
    42224263       
    42234264        ::
     
    42264267            sage: b.exact_log(2)
    42274268            14446
    42284269            sage: t=cputime()
    4229             sage: for a in range(2, 1000): k = b.is_power()
     4270            sage: for a in range(2, 1000): k = b.is_perfect_power()
    42304271            sage: cputime(t)      # random
    42314272            0.53203299999999976
    42324273            sage: t=cputime()
     
    42444285            sage: b.exact_log(2)
    42454286            1437
    42464287            sage: t=cputime()
    4247             sage: for a in range(2, 10000): k = b.is_power() # note that we change the range from the example above
     4288            sage: for a in range(2, 10000): k = b.is_perfect_power() # note that we change the range from the example above
    42484289            sage: cputime(t)      # random
    42494290            0.17201100000000036
    42504291            sage: t=cputime(); TWO=int(2)
     
    42664307
    42674308    def is_prime_power(self, flag=0):
    42684309        r"""
    4269         Returns True if `x` is a prime power, and False otherwise.
     4310        Returns True if this integer is a prime power, and False otherwise.
    42704311       
    42714312        INPUT:
    42724313           
    4273            -  ``flag`` (for primality testing) - int
    4274            - ``0`` (default): use a combination of algorithms.
    4275            - ``1``: certify primality using the Pocklington-Lehmer test.
    4276            - ``2``: certify primality using the APRCL test.
     4314        - ``flag`` (for primality testing) - int. Values are:
     4315
     4316          - ``0`` (default): use a combination of algorithms.
     4317          - ``1``: certify primality using the Pocklington-Lehmer test.
     4318          - ``2``: certify primality using the APRCL test.
     4319
     4320        .. seealso::
     4321
     4322            - :meth:`perfect_power`: Finds the minimal base for which integer
     4323              is a perfect power.
     4324            - :meth:`is_perfect_power`: Doesn't test whether the base is prime.
     4325            - :meth:`is_power_of`: If you know the base already this method is
     4326              the fastest option.
    42774327
    42784328        EXAMPLES::
    42794329       
     
    42884338            sage: (10000).is_prime_power(flag=1)
    42894339            False
    42904340
    4291         We check that \#4777 is fixed::
     4341        We check that :trac:`4777` is fixed::
    42924342
    42934343            sage: n = 150607571^14
    42944344            sage: n.is_prime_power()
     
    43054355        if not self.is_perfect_power():
    43064356            return False
    43074357        k, g = self._pari_().ispower()
    4308         if not k:
     4358        if k == 1:
    43094359            raise RuntimeError, "Inconsistent results between GMP and PARI"
    43104360        return g.isprime(flag=flag)
    43114361
     
    43994449       
    44004450    def is_perfect_power(self):
    44014451        r"""
    4402         Returns ``True`` if self is a perfect power.
     4452        Returns ``True`` if self is a perfect power, ie if there exist integers
     4453        a and b, `b > 1` with `\texttt{self} = a^b`.
     4454
     4455        .. seealso::
     4456
     4457            - :meth:`perfect_power`: Finds the minimal base for which this
     4458              integer is a perfect power.
     4459            - :meth:`is_power_of`: If you know the base already this method is
     4460              the fastest option.
     4461            - :meth:`is_prime_power`: Checks whether the base is prime.
    44034462       
    44044463        EXAMPLES::
    44054464       
     4465            sage: Integer(-27).is_perfect_power()
     4466            True
     4467            sage: Integer(12).is_perfect_power()
     4468            False
     4469
    44064470            sage: z = 8
    44074471            sage: z.is_perfect_power()
    44084472            True
     
    44154479            sage: (-4).is_perfect_power()
    44164480            False
    44174481
    4418         This is a test to make sure we work around a bug in GMP,
    4419         see trac \#4612.
     4482            sage: (4).is_power()
     4483            doctest:...: DeprecationWarning: is_power is deprecated. Please use is_perfect_power instead.
     4484            See http://trac.sagemath.org/12116 for details.
     4485            True
     4486
     4487        TESTS:
     4488
     4489        This is a test to make sure we work around a bug in GMP, see
     4490        :trac:`4612`.
    44204491
    44214492        ::
    44224493