<commit_before>from .numbers import Integer, Rational, Undefined, Number
from .symbol import Symbol
from .operations import Pow, Mul, Add, base, exponent, term, const
from .subs import _map
from .evaluate import *
from .order import isordered

from fractions import gcd


def simplify_rational(u):
    """ Simplifies a rational down to its canonical form.

        Args:
            u: a Rational or Integer instance
        Returns:
            Rational (in canonical form) or Integer
        Raises:
            ValueError: if u is neither Rational or Integer
    """

    if not isinstance(u, (Integer, Rational)):
        raise ValueError('u must be an Integer or Rational instance')

    if isinstance(u, Integer):
        return u
    elif isinstance(u, Rational):
        n = u.numerator
        d = u.denominator

        integer_quotent, integer_remainder = divmod(n, d)

        if integer_remainder == 0:
            return integer_quotent
        else:
            g = gcd(n, d)
            if d > 0:
                new_numerator, _ = divmod(n, g)
                new_denominator, _ = divmod(d, g)
                return Rational(new_numerator, new_denominator)
            elif d < 0:
                new_numerator, _ = divmod(-n, g)
                new_denominator, _ = divmod(-d, g)
                return Rational(new_numerator, new_denominator)

    raise NotImplementedError('rational {} has fallen through'.format(u))

def simplify_rne(u):
    """ Takes a RNE (expression comprised of Numbers) and returns it in its canonical form.
        Args:
            u: A Integer or Rational or a binary expression containing them
        Returns:
            A Integer or Fraction in cononical form, or Undefined()
    """
    def simp_rec(u):
        if isinstance(u, Integer):
            return u

        elif isinstance(u, Rational):
            if u.denominator == 0:
                return Undefined()
            else:
                return u

        elif len(u.args) == 1:
            v = simp_rec(u.args[0])
            if isinstance(v, Undefined):
                return Undefined()
            elif isinstance(v, Add):
                return v

        elif len(u.args) == 2:
            if isinstance(u, (Add, Mul)):
                v = simp_rec(u.args[0])
                w = simp_rec(u.args[1])

                if isinstance(v, Undefined) or isinstance(v, Undefined):
                    return Undefined()

                else:
                    if isinstance(u, Add):
                        return evaluate_add(v, w)
                    if isinstance(u, Mul):
                        return evaluate_mul(v, w)
            elif isinstance(u, Pow):
                v = simp_rec(u.args[0])
                if isinstance(v, Undefined):
                    return Undefined()
                else:
                    return evaluate_power(v, u.args[1])
    v = simp_rec(u)
    if isinstance(v, Undefined):
        return Undefined
    else:
        return simplify_rational(v)


def simplify_integer_power(v, w):
    """ Simplifies Pow(v, w) with an Integer exponent to its canonical form.

        SINTPOW-1: Both v and w are Numbers => can be evalated
        SINTPOW-2: x^0 => 1<commit_msg>simplify rational was returning int not Integer<commit_after>from .numbers import Integer, Rational, Undefined, Number
from .symbol import Symbol
from .operations import Pow, Mul, Add, base, exponent, term, const
from .subs import _map
from .evaluate import *
from .order import isordered

from fractions import gcd


def simplify_rational(u):
    """ Simplifies a rational down to its canonical form.

        Args:
            u: a Rational or Integer instance
        Returns:
            Rational (in canonical form) or Integer
        Raises:
            ValueError: if u is neither Rational or Integer
    """

    if not isinstance(u, (Integer, Rational)):
        raise ValueError('u must be an Integer or Rational instance')

    if isinstance(u, Integer):
        return u
    elif isinstance(u, Rational):
        n = u.numerator
        d = u.denominator

        integer_quotent, integer_remainder = divmod(n, d)

        if integer_remainder == 0:
            return Number(integer_quotent)
        else:
            g = gcd(n, d)
            if d > 0:
                new_numerator, _ = divmod(n, g)
                new_denominator, _ = divmod(d, g)
                return Rational(new_numerator, new_denominator)
            elif d < 0:
                new_numerator, _ = divmod(-n, g)
                new_denominator, _ = divmod(-d, g)
                return Rational(new_numerator, new_denominator)

    raise NotImplementedError('rational {} has fallen through'.format(u))

def simplify_rne(u):
    """ Takes a RNE (expression comprised of Numbers) and returns it in its canonical form.
        Args:
            u: A Integer or Rational or a binary expression containing them
        Returns:
            A Integer or Fraction in cononical form, or Undefined()
    """
    def simp_rec(u):
        if isinstance(u, Integer):
            return u

        elif isinstance(u, Rational):
            if u.denominator == 0:
                return Undefined()
            else:
                return u

        elif len(u.args) == 1:
            v = simp_rec(u.args[0])
            if isinstance(v, Undefined):
                return Undefined()
            elif isinstance(v, Add):
                return v

        elif len(u.args) == 2:
            if isinstance(u, (Add, Mul)):
                v = simp_rec(u.args[0])
                w = simp_rec(u.args[1])

                if isinstance(v, Undefined) or isinstance(v, Undefined):
                    return Undefined()

                else:
                    if isinstance(u, Add):
                        return evaluate_add(v, w)
                    if isinstance(u, Mul):
                        return evaluate_mul(v, w)
            elif isinstance(u, Pow):
                v = simp_rec(u.args[0])
                if isinstance(v, Undefined):
                    return Undefined()
                else:
                    return evaluate_power(v, u.args[1])
    v = simp_rec(u)
    if isinstance(v, Undefined):
        return Undefined()
    else:
        return simplify_rational(v)


def simplify_integer_power(v, w):
    """ Simplifies Pow(v, w) with an Integer exponent to its canonical form.

        SINTPOW-1: Both v and w are Numbers => can be evalated
        SINTPOW-2: x^0 => 1
{'fraction_non_alphanumeric': 0.08469356328919002, 'fraction_numerical': 0.004619648906683092, 'mean_word_length': 1.6983797257997508, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '6055239', 'n_tokens_mistral': 1879, 'n_tokens_neox': 1765, 'n_words': 587}