Skip to content

brahmap.base.ReducedLinearOperator

Restricts a non-symmetric linear operator to a subset of its rows and columns.

This operation can be useful in masking out unobserved or unwanted dimensions by projecting them out of the functional space.

Parameters:

Name Type Description Default
op LinearOperator

The original linear operator to be reduced

required
row_indices Sequence[int]

Indices to restrict the output vector

required
col_indices Sequence[int]

Indices to restrict the input vector

required

Returns:

Type Description
LinearOperator

A new reduced linear operator of shape (len(row_indices), len(col_indices))

Source code in brahmap/base/linop.py
def ReducedLinearOperator(
    op: LinearOperator, row_indices, col_indices
) -> LinearOperator:
    """
    Restricts a non-symmetric linear operator to a subset of its rows and columns.

    This operation can be useful in masking out unobserved or unwanted
    dimensions by projecting them out of the functional space.

    Parameters
    ----------
    op : LinearOperator
        The original linear operator to be reduced
    row_indices : Sequence[int]
        Indices to restrict the output vector
    col_indices : Sequence[int]
        Indices to restrict the input vector

    Returns
    -------
    LinearOperator
        A new reduced linear operator of shape `(len(row_indices), len(col_indices))`
    """

    nargin, nargout = len(col_indices), len(row_indices)
    m, n = op.shape  # Shape of non-reduced operator.

    def matvec(x):
        z = np.zeros(n, dtype=x.dtype)
        z[col_indices] = x[:]
        y = op * z
        return y[row_indices]  # type: ignore

    def rmatvec(x):
        z = np.zeros(m, dtype=x.dtype)
        z[row_indices] = x[:]
        y = op.H * z
        return y[col_indices]  # type: ignore

    return LinearOperator(
        nargin, nargout, matvec=matvec, symmetric=False, rmatvec=rmatvec
    )