Skip to content

vectorized_trace

vectorized_trace(n, d)

Given a flattened stack of n dxd matrices, computes the trace operator that, when applied to this stack, returns another flattened stack of n values containing the traces of each of these matrices.

Parameters:

Name Type Description Default
n int

Number of matrices

required
d int

Dimension of matrices

required

Returns:

Name Type Description
T (n, n*d*d) scipy sparse matrix
Source code in src\fast_cody\vectorized_trace.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def vectorized_trace(n, d):
    """
    Given a flattened stack of n  dxd matrices, computes the trace operator
    that, when applied to this stack, returns another flattened stack of n values containing the traces
    of each of these matrices.

    Parameters
    ----------
    n : int
        Number of matrices
    d : int
        Dimension of matrices

    Returns
    -------
    T : (n, n*d*d) scipy sparse matrix

    """
    # trace of matrix i will have elements i, then n+i + 1, 2n+i+2

    ii = np.arange(n * d * d)
    Mi = np.reshape(ii, (n * d, d))

    Mii = np.reshape(Mi, (n, d, d))
    Mii = np.reshape(Mii, (d * n, d))

    mask = np.identity(d, dtype=bool)
    maskii = np.tile(mask, (n, 1))
    j = Mii[maskii]
    i = np.repeat(np.arange(n), d)

    v = np.ones((i.shape[0]))
    T = sp.sparse.csc_matrix((v, (i, j)), (n, n * d * d))


    return T