Skip to content

brahmap.base.MatrixLinearOperator

Bases: LinearOperator

A linear operator for a numpy matrix

A linear operator wrapping the multiplication with a matrix and its transpose (real) or conjugate transpose (complex). The operator's dtype is the same as the specified matrix argument.

Parameters:

Name Type Description Default
matrix ndarray

description

required
**kwargs Any

description

{}
Source code in brahmap/base/linop.py
class MatrixLinearOperator(LinearOperator):
    """A linear operator for a numpy matrix

    A linear operator wrapping the multiplication with a matrix and its
    transpose (real) or conjugate transpose (complex). The operator's dtype
    is the same as the specified `matrix` argument.

    Parameters
    ----------
    matrix : np.ndarray
        _description_
    **kwargs: Any
        _description_
    """

    def __init__(self, matrix: np.ndarray, **kwargs: Any):
        if "symmetric" in kwargs:
            kwargs.pop("symmetric")
        if "matvec" in kwargs:
            kwargs.pop("matvec")
        if "dtype" in kwargs:
            kwargs.pop("dtype")

        if not hasattr(matrix, "shape"):
            matrix = np.asanyarray(matrix)

        if matrix.ndim != 2:
            msg = "matrix must be 2-d (shape can be [M, N], [M, 1] or [1, N])"
            raise ValueError(msg)

        matvec = matrix.dot
        iscomplex = np.iscomplexobj(matrix)

        if matrix.shape[0] == matrix.shape[1]:
            symmetric = np.all(matrix == matrix.conj().T)
        else:
            symmetric = False

        if not symmetric:
            rmatvec = matrix.conj().T.dot if iscomplex else matrix.T.dot
        else:
            rmatvec = None

        super(MatrixLinearOperator, self).__init__(
            matrix.shape[1],
            matrix.shape[0],
            symmetric=symmetric,
            matvec=matvec,
            rmatvec=rmatvec,
            dtype=matrix.dtype,
            **kwargs,
        )