<commit_before>"""
Array manipulation routines
===========================

"""
from . import array_create
import numpy_force as numpy
from . import bhary
from .bhary import fix_returned_biclass
import itertools

@fix_returned_biclass
def flatten(ary):
    """
    Return a copy of the array collapsed into one dimension.

    Parameters
    ----------
    a : array_like
        Array from which to retrive the flattened data from.

    Returns
    -------
    y : ndarray
        A copy of the input array, flattened to one dimension.

    Notes
    -----
    The order of the data in memory is always row-major (C-style).

    Examples
    --------
    >>> a = np.array([[1,2], [3,4]])
    >>> np.flatten(a)
    array([1, 2, 3, 4])
    """
    return ary.reshape(numpy.multiply.reduce(numpy.asarray(ary.shape)))

@fix_returned_biclass
def trace(ary, offset=0, axis1=0, axis2=1, dtype=None):
    D = diagonal(ary, offset=offset, axis1=axis1, axis2=axis2)
    if bhary.check(D):
        D = D.copy2numpy()
    if dtype:
        D = D.astype(dtype)
    return numpy.add.reduce(D, axis=D.ndim-1)

@fix_returned_biclass
def diagonal(ary, offset=0, axis1=0, axis2=1):
    """
    Return specified diagonals.

    If `a` is 2-D, returns the diagonal of `a` with the given offset,
    i.e., the collection of elements of the form ``a[i, i+offset]``.
    If `a` has more than two dimensions, then the axes specified by
    `axis1` and `axis2` are used to determine the 2-D sub-array whose
    diagonal is returned. The shape of the resulting array can be
    determined by removing `axis1` and `axis2` and appending an index
    to the right equal to the size of the resulting diagonals.

    Parameters
    ----------
    ary : array_like
        Array from which the diagonals are taken.
    offset : int, optional
        Offset of the diagonal from the main diagonal.  Can be positive or
        negative.  Defaults to main diagonal (0).
    axis1 : int, optional
        Axis to be used as the first axis of the 2-D sub-arrays from which<commit_msg>Removed the Numpy copy in trace<commit_after>"""
Array manipulation routines
===========================

"""
from . import array_create
import numpy_force as numpy
from . import bhary
from .bhary import fix_returned_biclass
import itertools

@fix_returned_biclass
def flatten(ary):
    """
    Return a copy of the array collapsed into one dimension.

    Parameters
    ----------
    a : array_like
        Array from which to retrive the flattened data from.

    Returns
    -------
    y : ndarray
        A copy of the input array, flattened to one dimension.

    Notes
    -----
    The order of the data in memory is always row-major (C-style).

    Examples
    --------
    >>> a = np.array([[1,2], [3,4]])
    >>> np.flatten(a)
    array([1, 2, 3, 4])
    """
    return ary.reshape(numpy.multiply.reduce(numpy.asarray(ary.shape)))

@fix_returned_biclass
def trace(ary, offset=0, axis1=0, axis2=1, dtype=None):
    D = diagonal(ary, offset=offset, axis1=axis1, axis2=axis2)
    if dtype:
        D = D.astype(dtype)
    return numpy.add.reduce(D, axis=D.ndim-1)

@fix_returned_biclass
def diagonal(ary, offset=0, axis1=0, axis2=1):
    """
    Return specified diagonals.

    If `a` is 2-D, returns the diagonal of `a` with the given offset,
    i.e., the collection of elements of the form ``a[i, i+offset]``.
    If `a` has more than two dimensions, then the axes specified by
    `axis1` and `axis2` are used to determine the 2-D sub-array whose
    diagonal is returned. The shape of the resulting array can be
    determined by removing `axis1` and `axis2` and appending an index
    to the right equal to the size of the resulting diagonals.

    Parameters
    ----------
    ary : array_like
        Array from which the diagonals are taken.
    offset : int, optional
        Offset of the diagonal from the main diagonal.  Can be positive or
        negative.  Defaults to main diagonal (0).
    axis1 : int, optional
        Axis to be used as the first axis of the 2-D sub-arrays from which
{'fraction_non_alphanumeric': 0.11761811023622047, 'fraction_numerical': 0.015994094488188976, 'mean_word_length': 2.678733031674208, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '1442685', 'n_tokens_mistral': 1294, 'n_tokens_neox': 1239, 'n_words': 478}