<commit_before>from __future__ import print_function, division

from math import log as _log

from .sympify import _sympify
from .cache import cacheit
from .core import C
from .singleton import S
from .expr import Expr

from sympy.core.function import (_coeff_isneg, expand_complex,
    expand_multinomial, expand_mul)
from sympy.core.logic import fuzzy_bool
from sympy.core.compatibility import as_int, xrange

from sympy.mpmath.libmp import sqrtrem as mpmath_sqrtrem
from sympy.utilities.iterables import sift


def integer_nthroot(y, n):
    """
    Return a tuple containing x = floor(y**(1/n))
    and a boolean indicating whether the result is exact (that is,
    whether x**n == y).

    >>> from sympy import integer_nthroot
    >>> integer_nthroot(16,2)
    (4, True)
    >>> integer_nthroot(26,2)
    (5, False)

    """
    y, n = int(y), int(n)
    if y < 0:
        raise ValueError("y must be nonnegative")
    if n < 1:
        raise ValueError("n must be positive")
    if y in (0, 1):
        return y, True
    if n == 1:
        return y, True
    if n == 2:
        x, rem = mpmath_sqrtrem(y)
        return int(x), not rem
    if n > y:
        return 1, False
    # Get initial estimate for Newton's method. Care must be taken to
    # avoid overflow
    try:
        guess = int(y**(1./n) + 0.5)
    except OverflowError:
        exp = _log(y, 2)/n
        if exp > 53:
            shift = int(exp - 53)
            guess = int(2.0**(exp - shift) + 1) << shift
        else:
            guess = int(2.0**exp)
    #print n
    if guess > 2**50:
        # Newton iteration
        xprev, x = -1, guess
        while 1:
            t = x**(n - 1)
            #xprev, x = x, x - (t*x-y)//(n*t)
            xprev, x = x, ((n - 1)*x + y//t)//n
            #print n, x-xprev, abs(x-xprev) < 2
            if abs(x - xprev) < 2:
                break
    else:
        x = guess
    # Compensate
    t = x**n
    while t < y:
        x += 1
        t = x**n
    while t > y:
        x -= 1
        t = x**n
    return x, t == y


class Pow(Expr):

    is_Pow = True

    __slots__ = ['is_commutative']

    @cacheit
    def __new__(cls, b, e, evaluate=True):
        from sympy.functions.elementary.exponential import exp_polar

        # don't optimize "if e==0; return 1" here; it's better to handle that
        # in the calling routine so this doesn't get called
        b = _sympify(b)
        e = _sympify(e)
        if evaluate:
            if e is S.Zero:
                return S.One
            elif e is S.One:
                return b
            elif b is S.One:
                if e not in ( S.NaN, S.Infinity, -S.Infinity):
                    return S.One
                else:
                    return S.NaN
            elif S.NaN in (b, e):
                return S.NaN
            else:
                # recognize base as E<commit_msg>Added documentation of singletons for Pow<commit_after>from __future__ import print_function, division

from math import log as _log

from .sympify import _sympify
from .cache import cacheit
from .core import C
from .singleton import S
from .expr import Expr

from sympy.core.function import (_coeff_isneg, expand_complex,
    expand_multinomial, expand_mul)
from sympy.core.logic import fuzzy_bool
from sympy.core.compatibility import as_int, xrange

from sympy.mpmath.libmp import sqrtrem as mpmath_sqrtrem
from sympy.utilities.iterables import sift


def integer_nthroot(y, n):
    """
    Return a tuple containing x = floor(y**(1/n))
    and a boolean indicating whether the result is exact (that is,
    whether x**n == y).

    >>> from sympy import integer_nthroot
    >>> integer_nthroot(16,2)
    (4, True)
    >>> integer_nthroot(26,2)
    (5, False)

    """
    y, n = int(y), int(n)
    if y < 0:
        raise ValueError("y must be nonnegative")
    if n < 1:
        raise ValueError("n must be positive")
    if y in (0, 1):
        return y, True
    if n == 1:
        return y, True
    if n == 2:
        x, rem = mpmath_sqrtrem(y)
        return int(x), not rem
    if n > y:
        return 1, False
    # Get initial estimate for Newton's method. Care must be taken to
    # avoid overflow
    try:
        guess = int(y**(1./n) + 0.5)
    except OverflowError:
        exp = _log(y, 2)/n
        if exp > 53:
            shift = int(exp - 53)
            guess = int(2.0**(exp - shift) + 1) << shift
        else:
            guess = int(2.0**exp)
    #print n
    if guess > 2**50:
        # Newton iteration
        xprev, x = -1, guess
        while 1:
            t = x**(n - 1)
            #xprev, x = x, x - (t*x-y)//(n*t)
            xprev, x = x, ((n - 1)*x + y//t)//n
            #print n, x-xprev, abs(x-xprev) < 2
            if abs(x - xprev) < 2:
                break
    else:
        x = guess
    # Compensate
    t = x**n
    while t < y:
        x += 1
        t = x**n
    while t > y:
        x -= 1
        t = x**n
    return x, t == y


class Pow(Expr):
    """
    Defines the expression x**y as "x raised to a power y"


    Singleton definitions involving (nan,0,1,-1,oo,-oo):
        z**nan      = nan
        z**0        = 1  although arguments over 0**0 exist
        z**1        = z
        (-oo)**(-1) = 0
        (-1)**-1    = -1
        S.Zero**-1  = oo  -- this is not strictly true,
                        as 0**-1 may be undefined, but is convenient
                        is some contexts where the base is assumed
                        to be positive.
        1**-1       = 1
        oo**-1      = 0
        nan**-1     = nan
        nan**oo     = nan
        nan**-oo    = nan
        0**oo       = 0 because for all complex numbers z near 0, z**oo -> 0
        0**-oo      = oo  -- this is strictly true, as
                        as 0**oo may be oscillating between positive and
                        negative values or rotating in the complex plane.
                        It is convenient, however, when the base
                        is positive.
        1**oo       = nan because there are various cases where
                        lim(x(t),t)=1, lim(y(t),t)=oo, but
                        lim( x(t)**y(t), t) != 1
        1**-oo      = nan for the same reason
        (-1)**oo    = nan because of oscillations in the limit
        (-1)**(-oo) = nan because of oscillations in the limit
        oo**oo      = oo
        oo**-oo     = 0
        (-oo)**oo   = nan
        (-oo)**-oo  = nan

    Because symbolic computations are more flexible that floating point
    calculations and we prefer to never return an incorrect answer,
    we choose not to conform to all IEEE 754 conventions.  This helps
    us avoid extra test-case code in the calculation of limits.

    """
    is_Pow = True

    __slots__ = ['is_commutative']

    @cacheit
    def __new__(cls, b, e, evaluate=True):
        from sympy.functions.elementary.exponential import exp_polar

        # don't optimize "if e==0; return 1" here; it's better to handle that
        # in the calling routine so this doesn't get called
        b = _sympify(b)
        e = _sympify(e)
        if evaluate:
            if e is S.Zero:
                return S.One
            elif e is S.One:
                return b
            elif b is S.One:
                if e in ( S.NaN, S.Infinity, -S.Infinity):
                    return S.NaN
                return S.One
            elif S.NaN in (b, e):
                return S.NaN
            else:
                # recognize base as E
{'fraction_non_alphanumeric': 0.10448358686949559, 'fraction_numerical': 0.016279690419001867, 'mean_word_length': 1.5597677595628416, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2443043', 'n_tokens_mistral': 2483, 'n_tokens_neox': 2332, 'n_words': 838}