<commit_before>import torch
import gpytorch.utils.fft as fft
import gpytorch.utils as utils


def index_coef_to_sparse(index_matrix, value_matrix, row_length):
    """
    Converts a sparse matrix stored densely in an index matrix and a value matrix
    to a torch sparse matrix.
    Args:
        - index_matrix (Matrix n-by-nz) -- A matrix that describes, for each row of the
                       sparse matrix W, which elements are nonzero.
        - value_matrix (Matrix n-by-nz) -- A matrix that describes, for each row of the
                       sparse matrix W, the values of the nonzero elements. Each value is
                       placed in the position described by the index matrix (see the
                       definition of W given below).
        - row_length (scalar) -- The size of the second dimension of W
    Returns:
        - W (Sparse matrix n-by-row_length) - A torch sparse matrix W so that
            W[i, index_matrix[i, j]] = value_matrix[i, j].
    """
    num_target_points, num_coefficients = value_matrix.size()
    index_list = [[], []]
    value_list = []
    for i in range(num_target_points):
        for j in range(num_coefficients):
            if value_matrix[i, j] == 0:
                continue
            index_list[0].append(i)
            index_list[1].append(index_matrix[i, j])
            value_list.append(value_matrix[i, j])

    index_tensor = torch.LongTensor(index_list)
    value_tensor = torch.FloatTensor(value_list)
    W = torch.sparse.FloatTensor(index_tensor, value_tensor, torch.Size([num_target_points, row_length]))
    return W


def toeplitz(toeplitz_column, toeplitz_row):
    """
    Constructs tensor version of toeplitz matrix from column vector
    Args:
        - toeplitz_column (vector n) - column of toeplitz matrix
        - toeplitz_row (vector n-1) - row of toeplitz matrix
    Returns:
        - Matrix (n x n) - matrix representation
    """
    assert toeplitz_column.ndimension() == 1
    assert toeplitz_row.ndimension() == 1
    assert toeplitz_column[0] == toeplitz_row[0]
    assert len(toeplitz_column) == len(toeplitz_row)
    assert type(toeplitz_column) == type(toeplitz_row)

    res = torch.Tensor(len(toeplitz_column), len(toeplitz_column)).type_as(toeplitz_column)
    for i, val in enumerate(toeplitz_column):
        for j in range(len(toeplitz_column) - i):
            res[j + i, j] = val
    for i, val in list(enumerate(toeplitz_row))[1:]:
        for j in range(len(toeplitz_row) - i):
            res[j, j + i] = val
    return res


def sym_toeplitz(toeplitz_column):
    """
    Constructs tensor version of symmetric toeplitz matrix from column vector
    Args:
        - toeplitz_column (vector n) - column of Toeplitz matrix
    Returns:
        - Matrix (n x n) - matrix representation
    """
    return toeplitz(toeplitz_column, toeplitz_column)


def toeplitz_getitem(toeplitz_column, toeplitz_row, i, j):
    """
    Gets the (i,j)th entry of a Toeplitz matrix T.
    Args:
        - toeplitz_column (vector n) - column of Toeplitz matrix
        - toeplitz_row (vector n) - row of Toeplitz matrix
        - i (scalar) - row of entry to get
        - j (scalar) - column of entry to get
    Returns:<commit_msg>Change asserts to RuntimeErrors<commit_after>import torch
import gpytorch.utils.fft as fft
import gpytorch.utils as utils


def index_coef_to_sparse(index_matrix, value_matrix, row_length):
    """
    Converts a sparse matrix stored densely in an index matrix and a value matrix
    to a torch sparse matrix.
    Args:
        - index_matrix (Matrix n-by-nz) -- A matrix that describes, for each row of the
                       sparse matrix W, which elements are nonzero.
        - value_matrix (Matrix n-by-nz) -- A matrix that describes, for each row of the
                       sparse matrix W, the values of the nonzero elements. Each value is
                       placed in the position described by the index matrix (see the
                       definition of W given below).
        - row_length (scalar) -- The size of the second dimension of W
    Returns:
        - W (Sparse matrix n-by-row_length) - A torch sparse matrix W so that
            W[i, index_matrix[i, j]] = value_matrix[i, j].
    """
    num_target_points, num_coefficients = value_matrix.size()
    index_list = [[], []]
    value_list = []
    for i in range(num_target_points):
        for j in range(num_coefficients):
            if value_matrix[i, j] == 0:
                continue
            index_list[0].append(i)
            index_list[1].append(index_matrix[i, j])
            value_list.append(value_matrix[i, j])

    index_tensor = torch.LongTensor(index_list)
    value_tensor = torch.FloatTensor(value_list)
    W = torch.sparse.FloatTensor(index_tensor, value_tensor, torch.Size([num_target_points, row_length]))
    return W


def toeplitz(toeplitz_column, toeplitz_row):
    """
    Constructs tensor version of toeplitz matrix from column vector
    Args:
        - toeplitz_column (vector n) - column of toeplitz matrix
        - toeplitz_row (vector n-1) - row of toeplitz matrix
    Returns:
        - Matrix (n x n) - matrix representation
    """
    if toeplitz_column.ndimension() != 1:
        raise RuntimeError('toeplitz_column must be a vector.')

    if toeplitz_row.ndimension() != 1:
        raise RuntimeError('toeplitz_row must be a vector.')

    if toeplitz_column[0] != toeplitz_row[0]:
        raise RuntimeError('The first column and first row of the Toeplitz matrix should have the same first \
                            otherwise the value of T[0,0] is ambiguous. \
                            Got: c[0]={} and r[0]={}'.format(toeplitz_column[0], toeplitz_row[0]))

    if len(toeplitz_column) != len(toeplitz_row):
        raise RuntimeError('c and r should have the same length (Toeplitz matrices are necessarily square).')

    if type(toeplitz_column) != type(toeplitz_row):
        raise RuntimeError('toeplitz_column and toeplitz_row should be the same type.')

    res = torch.Tensor(len(toeplitz_column), len(toeplitz_column)).type_as(toeplitz_column)
    for i, val in enumerate(toeplitz_column):
        for j in range(len(toeplitz_column) - i):
            res[j + i, j] = val
    for i, val in list(enumerate(toeplitz_row))[1:]:
        for j in range(len(toeplitz_row) - i):
            res[j, j + i] = val
    return res


def sym_toeplitz(toeplitz_column):
    """
    Constructs tensor version of symmetric toeplitz matrix from column vector
    Args:
        - toeplitz_column (vector n) - column of Toeplitz matrix
    Returns:
        - Matrix (n x n) - matrix representation
    """
    return toeplitz(toeplitz_column, toeplitz_column)


def toeplitz_getitem(toeplitz_column, toeplitz_row, i, j):
    """
    Gets the (i,j)th entry of a Toeplitz matrix T.
    Args:
        - toeplitz_column (vector n) - column of Toeplitz matrix
        - toeplitz_row (vector n) - row of Toeplitz matrix
        - i (scalar) - row of entry to get
        - j (scalar) - column of entry to get
    Returns:
{'fraction_non_alphanumeric': 0.08875070661390616, 'fraction_numerical': 0.0033917467495760316, 'mean_word_length': 2.5544952285283777, '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': '3189558', 'n_tokens_mistral': 2251, 'n_tokens_neox': 2200, 'n_words': 685}