<commit_before>import sympy as sp
import itertools
import operator

def derive_expr_from_counter_entry(expression, variables, counter_entry):

    """
    Derives an given expression with respect to arbitrary variables and orders.

    :param expression: the expression to be derived
    :param variables: the name of the variables (typically {y_0, y_1, ..., y_n})
    :param counter_entry: an entry of counter. That is a tuple of integers of length equal to the number of variables.
    For example, (0,2,1) means we derive with respect to the third variable (first order)
    and to the second variable (second order)

    :return: the derived expression
    """

    assert(len(variables) == len(counter_entry))

    # no derivation, we return the unchanged expression
    if sum(counter_entry) == 0:
        return expression


    expr_out = expression
    # we recursively derive the expression with respect to all variables at the degree specified in counter
    for (var, degree) in zip(variables, counter_entry):
        expr_out = sp.diff(expr_out, var, degree)
        # If the expression reaches 0, we can return 0
        if(expr_out == sp.S(0)):
            return sp.S(0)

    return expr_out

def get_factorial_term(counter_entry):

    """
    Calculates  the "1/n!" of eq. 6 (see Ale et al. 2013). Note that n! is a product of factorials.

    :param counter_entry: an entry of counter. That is a tuple of integers of length equal to the number of variables.
    For instance, (1,0,1) could exist for three variables.

    :return: the scalar result scalar as a sympy expression
    """

    # compute all factorials
    factos = [sp.factorial(c) for c in counter_entry]
    # multiply them
    prod = reduce(operator.mul, factos)
    # return the invert
    return sp.S(1)/sp.S(prod)


def taylor_expansion(variables, propensity, counter):
    """
    Creates terms used in eq. 6 (see Ale et al. 2013) to calculate dmu/dt for EACH VARIABLE combination,<commit_msg>simplify differentiation function using built in diff 'hidden' properties<commit_after>import sympy as sp
import itertools
import operator

def derive_expr_from_counter_entry(expression, variables, counter_entry):

    """
    Derives an given expression with respect to arbitrary variables and orders.

    :param expression: the expression to be derived
    :param variables: the name of the variables (typically {y_0, y_1, ..., y_n})
    :param counter_entry: an entry of counter. That is a tuple of integers of length equal to the number of variables.
    For example, (0,2,1) means we derive with respect to the third variable (first order)
    and to the second variable (second order)

    :return: the derived expression
    """

    # no derivation, we return the unchanged expression
    if sum(counter_entry) == 0:
        return expression
    # repeat a variable as many time as its value in counter
    diff_orders = reduce(operator.add, map(lambda v,c :[v] *c , variables, counter_entry))

    return sp.diff(expression, *diff_orders)

    #
    # # we recursively derive the expression with respect to all variables at the degree specified in counter
    # for (var, degree) in zip(variables, counter_entry):
    #     expr_out = sp.diff(expr_out, var, degree)
    #     # If the expression reaches 0, we can return 0
    #     if(expr_out == sp.S(0)):
    #         return sp.S(0)
    #
    # return expr_out

def get_factorial_term(counter_entry):

    """
    Calculates  the "1/n!" of eq. 6 (see Ale et al. 2013). Note that n! is a product of factorials.

    :param counter_entry: an entry of counter. That is a tuple of integers of length equal to the number of variables.
    For instance, (1,0,1) could exist for three variables.

    :return: the scalar result scalar as a sympy expression
    """

    # compute all factorials
    factos = [sp.factorial(c) for c in counter_entry]
    # multiply them
    prod = reduce(operator.mul, factos)
    # return the invert
    return sp.S(1)/sp.S(prod)


def taylor_expansion(variables, propensity, counter):
    """
    Creates terms used in eq. 6 (see Ale et al. 2013) to calculate dmu/dt for EACH VARIABLE combination,
{'fraction_non_alphanumeric': 0.07909875359539789, 'fraction_numerical': 0.011984659635666348, 'mean_word_length': 3.253822629969419, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5552665', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1165, 'n_words': 509}