Skip to content

util

sparse_inverse_nonzero(a)

Invert nonzero elements of a scipy.sparse.csr_matrix.

:param a: Matrix to invert. :type a: csr_matrix :return: Matrix with nonzero elements inverted. :rtype: csr_matrix

Source code in src/recnexteval/metrics/core/util.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def sparse_inverse_nonzero(a: csr_matrix) -> csr_matrix:
    """Invert nonzero elements of a `scipy.sparse.csr_matrix`.

    :param a: Matrix to invert.
    :type a: csr_matrix
    :return: Matrix with nonzero elements inverted.
    :rtype: csr_matrix
    """
    inv_a = a.copy()
    inv_a.data = 1 / inv_a.data
    return inv_a

sparse_divide_nonzero(a, b)

Elementwise divide of nonzero elements of a by nonzero elements of b.

Elements that were zero in either a or b are zero in the resulting matrix.

:param a: Numerator. :type a: csr_matrix :param b: Denominator. :type b: csr_matrix :return: Result of the elementwise division of matrix a by matrix b. :rtype: csr_matrix

Source code in src/recnexteval/metrics/core/util.py
17
18
19
20
21
22
23
24
25
26
27
28
29
def sparse_divide_nonzero(a: csr_matrix, b: csr_matrix) -> csr_matrix:
    """Elementwise divide of nonzero elements of a by nonzero elements of b.

    Elements that were zero in either a or b are zero in the resulting matrix.

    :param a: Numerator.
    :type a: csr_matrix
    :param b: Denominator.
    :type b: csr_matrix
    :return: Result of the elementwise division of matrix a by matrix b.
    :rtype: csr_matrix
    """
    return a.multiply(sparse_inverse_nonzero(b))