<commit_before>'''Sympy numerical code generation utilities.'''


import itertools
import re

import numpy as np
import sympy


def ew_diff(ndexpr, *wrt, **kwargs):
    '''Element-wise symbolic derivative of n-dimensional array-like expression.
    
    >>> import sympy
    >>> x = sympy.symbols('x')
    >>> ew_diff([[x**2, sympy.cos(x)], [5/x + 3, x**3 +2*x]], x)
    array([[2*x, -sin(x)],
           [-5/x**2, 3*x**2 + 2]], dtype=object)
    
    '''
    out = np.empty_like(ndexpr, object)
    for ind, expr in np.ndenumerate(ndexpr):
        out[ind] = sympy.diff(expr, *wrt, **kwargs)
    
    return out


def ndexpr_diff(ndexpr, wrt):
    '''Calculates the derivatives of an array expression w.r.t. to an ndarray.
    
    >>> from sympy import var, sin; from numpy import array
    >>> tup = var('x,y,z')
    >>> ndexpr_diff(tup, [x,y])
    array([[1, 0],
           [0, 1],
           [0, 0]], dtype=object)
    
    >>> ndexpr_diff([x**2+2*y/z, sin(x)], (x,y))
    array([[2*x, 2/z],
           [cos(x), 0]], dtype=object)
    
    '''
    ndexpr = np.asarray(ndexpr)
    wrt = np.asarray(wrt)
    jac = np.empty(ndexpr.shape + wrt.shape, dtype=object)
    for i, elem in np.ndenumerate(wrt):
        jac[(...,) + i] = ew_diff(ndexpr, elem)
    
    return jac


def flat_cat(*args, **kwargs):
    '''Concatenate flattened arrays.'''
    chain = list(itertools.chain(args, kwargs.values()))
    if not chain:
        return np.array([])
    else:
        return np.concatenate([np.asanyarray(a).flatten() for a in chain])

<commit_msg>utils: changed derivative order to agree with numpy broadcasting rules.<commit_after>'''Sympy numerical code generation utilities.'''


import itertools
import re

import numpy as np
import sympy


def ew_diff(ndexpr, *wrt, **kwargs):
    '''Element-wise symbolic derivative of n-dimensional array-like expression.
    
    >>> import sympy
    >>> x = sympy.symbols('x')
    >>> ew_diff([[x**2, sympy.cos(x)], [5/x + 3, x**3 +2*x]], x)
    array([[2*x, -sin(x)],
           [-5/x**2, 3*x**2 + 2]], dtype=object)
    
    '''
    out = np.empty_like(ndexpr, object)
    for ind, expr in np.ndenumerate(ndexpr):
        out[ind] = sympy.diff(expr, *wrt, **kwargs)
    return out


def ndexpr_diff(ndexpr, wrt):
    '''Calculates the derivatives of an array expression w.r.t. to an ndarray.
    
    >>> from sympy import var, sin; from numpy import array
    >>> tup = var('x,y,z')
    >>> ndexpr_diff(tup, [x,y])
    array([[1, 0, 0],
           [0, 1, 0]], dtype=object)
    
    >>> ndexpr_diff([x**2+2*y/z, sin(x)], (x,y))
    array([[2*x, cos(x)],
           [2/z, 0]], dtype=object)
    
    '''
    ndexpr = np.asarray(ndexpr)
    wrt = np.asarray(wrt)
    jac = np.empty(wrt.shape + ndexpr.shape, dtype=object)
    for i, elem in np.ndenumerate(wrt):
        jac[i] = ew_diff(ndexpr, elem)
    return jac


def flat_cat(*args, **kwargs):
    '''Concatenate flattened arrays.'''
    chain = list(itertools.chain(args, kwargs.values()))
    if not chain:
        return np.array([])
    else:
        return np.concatenate([np.asanyarray(a).flatten() for a in chain])


{'fraction_non_alphanumeric': 0.17713004484304934, 'fraction_numerical': 0.014093529788597053, 'mean_word_length': 2.767189384800965, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, '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': '301403', 'n_tokens_mistral': 1189, 'n_tokens_neox': 1170, 'n_words': 286}