<commit_before>from __future__ import division
import numpy as np
from scipy.sparse import issparse
from .linalg import dot_inplace_right


def cca(X, Y, inplace=False):
    """
    Calculate Canonical Correlation Analysis (CCA) on the two given matrices.
    That is, find the linear subspaces that maximally correlate X and Y.
    In the case where the ``X`` or ``Y`` matrices is very large, it is advisable
    to set ``inplace = True``. However, note this destructively edits the
    matrices by subtracting their means inplace.

    The resulting linear projection matrices will have a number of dimensions
    corresponding to the minimum rank of the correlations e.g.

        d = min(rank(X), rank(Y))

    Parameters
    ----------
    X : ``(n_samples, n_dims1)`` `ndarray`
        Data matrix one.
    Y : ``(n_samples, n_dims2)`` `ndarray`
        Data matrix two. May have a different number of columns than ``X``.
    inplace : `bool`, optional
        Whether to do the mean subtracting inplace or not.

    Returns
    -------
    Wx : ``(n_dims1, d)`` `ndarray`
        Linear subspace for the ``X`` matrix that maximally correlates
        ``X`` with ``Y``.
    Wy : ``(n_dims2, d)`` `ndarray`
        Linear subspace for the ``Y`` matrix that maximally correlates
        ``Y`` with ``X``.
    mx : ``(n_dims1,)`` `ndarray`
        The mean of ``X``.
    my : ``(n_dims2,)`` `ndarray`
        The mean of ``Y``.
    C : ``(d,)`` `ndarray`
        The canonical correlations (the eigenvalues of the cross-correlation).
    """
    # compute means
    mx = np.mean(X, axis=0)
    my = np.mean(Y, axis=0)

    # mean center views
    if inplace:
        X -= mx
        Y -= my
    else:
        X = X - mx
        Y = Y - my

    Ux, Sx, Vx = np.linalg.svd(X, full_matrices=False)
    Uy, Sy, Vy = np.linalg.svd(Y, full_matrices=False)

    # Compute cross-correlation in U-bases
    Cxy = Ux.T.dot(Uy)

    Fx, C, Fy = np.linalg.svd(Cxy, full_matrices=False)

    Wx = Vx.T.dot(1 / Sx[..., None] * Fx)
    Wy = Vy.T.dot(1 / Sy[..., None] * Fy.T)

    return Wx, Wy, mx, my, C


def eigenvalue_decomposition(C, eps=1e-10):
    r"""
    Eigenvalue decomposition of a given covariance (or scatter) matrix.

    Parameters
    ----------
    C : ``(N, N)`` `ndarray` or `scipy.sparse`
        The Covariance/Scatter matrix. If it is a `numpy.array`, then
        `numpy.linalg.eigh` is used. If it is an instance of `scipy.sparse`,
        then `scipy.sparse.linalg.eigsh` is used.
    eps : `float`, optional
        Tolerance value for positive eigenvalue. Those eigenvalues smaller
        than the specified eps value, together with their corresponding
        eigenvectors, will be automatically discarded. The final
        limit is computed as ::

            limit = np.max(np.abs(eigenvalues)) * eps

    Returns
    -------
    pos_eigenvectors : ``(N, p)`` `ndarray`
        The matrix with the eigenvectors corresponding to positive eigenvalues.
    pos_eigenvalues : ``(p,)`` `ndarray`
        The array of positive eigenvalues.
    """
    # compute eigenvalue decomposition
    if issparse(C):<commit_msg>Remove cca<commit_after>from __future__ import division
import numpy as np
from scipy.sparse import issparse
from .linalg import dot_inplace_right


def eigenvalue_decomposition(C, eps=1e-10):
    r"""
    Eigenvalue decomposition of a given covariance (or scatter) matrix.

    Parameters
    ----------
    C : ``(N, N)`` `ndarray` or `scipy.sparse`
        The Covariance/Scatter matrix. If it is a `numpy.array`, then
        `numpy.linalg.eigh` is used. If it is an instance of `scipy.sparse`,
        then `scipy.sparse.linalg.eigsh` is used.
    eps : `float`, optional
        Tolerance value for positive eigenvalue. Those eigenvalues smaller
        than the specified eps value, together with their corresponding
        eigenvectors, will be automatically discarded. The final
        limit is computed as ::

            limit = np.max(np.abs(eigenvalues)) * eps

    Returns
    -------
    pos_eigenvectors : ``(N, p)`` `ndarray`
        The matrix with the eigenvectors corresponding to positive eigenvalues.
    pos_eigenvalues : ``(p,)`` `ndarray`
        The array of positive eigenvalues.
    """
    # compute eigenvalue decomposition
    if issparse(C):
{'fraction_non_alphanumeric': 0.11673242051520075, 'fraction_numerical': 0.0037131585054537017, 'mean_word_length': 2.5154975530179446, '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': '3487273', 'n_tokens_mistral': 1412, 'n_tokens_neox': 1344, 'n_words': 475}