Skip to content

brahmap.base.BaseLinearOperator

Bases: object

Base class for defining the common interface shared by all linear operators.

A linear operator is a linear mapping \(x \mapsto A(x)\) such that the size of the input vector \(x\) is nargin and the size of the output vector is nargout. The linear operator \(A\) can be visualized as a matrix of shape (nargout, nargin).

Parameters:

Name Type Description Default
nargin int

Size of the input vector \(x\)

required
nargout int

Size of the output vector \(A(x)\)

required
symmetric bool

A parameter to specify whether the linear operator is symmetric, by default False

False
dtype DTypeLike

Data type of the linear operator, by default np.float64

float64
**kwargs Any

Extra keywords arguments

{}
Source code in brahmap/base/linop.py
class BaseLinearOperator(object):
    """Base class for defining the common interface shared by all linear
    operators.

    A linear operator is a linear mapping $x \\mapsto A(x)$ such that the size
    of the input vector $x$ is `nargin` and the size of the output vector is
    `nargout`. The linear operator $A$ can be visualized as a matrix of shape
    `(nargout, nargin)`.

    Parameters
    ----------
    nargin : int
        Size of the input vector $x$
    nargout : int
        Size of the output vector $A(x)$
    symmetric : bool, optional
        A parameter to specify whether the linear operator is symmetric, by
        default `False`
    dtype : npt.DTypeLike, optional
        Data type of the linear operator, by default `np.float64`
    **kwargs : Any
        Extra keywords arguments
    """

    # A logger may be attached to the linear operator via the `logger` keyword
    # argument.

    def __init__(
        self,
        nargin: int,
        nargout: int,
        symmetric: bool = False,
        dtype: npt.DTypeLike = np.float64,
        **kwargs,
    ):
        self.__nargin = nargin
        self.__nargout = nargout
        self.__symmetric = symmetric
        self.__shape = (nargout, nargin)
        self.dtype = dtype
        self._nMatvec = 0

        # Log activity.
        self.logger = kwargs.get("logger", null_log)
        self.logger.info("New linear operator with shape " + str(self.shape))
        return

    @property
    def nargin(self) -> int:
        """The size of the input vector."""
        return self.__nargin

    @property
    def nargout(self) -> int:
        """The size of the output vector."""
        return self.__nargout

    @property
    def symmetric(self) -> bool:
        """Indicate whether the operator is symmetric or not."""
        return self.__symmetric

    @property
    def shape(self) -> Tuple[int, int]:
        """The shape of the operator."""
        return self.__shape

    @property
    def dtype(self) -> npt.DTypeLike:
        """The data type of the operator."""
        return self.__dtype

    @dtype.setter
    def dtype(self, dtype):
        self.__dtype = dtype

    @property
    def nMatvec(self) -> int:
        """The number of products with vectors computed so far."""
        return self._nMatvec

    def reset_counters(self):
        """Reset operator/vector product counter to zero."""
        self._nMatvec = 0

    def dot(self, x):
        """Numpy-like dot() method."""
        return self.__mul__(x)

    def __call__(self, *args, **kwargs):
        # An alias for __mul__.
        return self.__mul__(*args, **kwargs)

    def __mul__(self, x):
        raise NotImplementedError("Please subclass to implement __mul__.")

    def __repr__(self) -> str:
        if self.symmetric:
            s = "Symmetric"
        else:
            s = "Unsymmetric"
        s += " <" + self.__class__.__name__ + ">"
        s += " of type %s" % self.dtype
        s += " with shape (%d,%d)" % (self.nargout, self.nargin)
        return s

dtype: npt.DTypeLike property writable

The data type of the operator.

nMatvec: int property

The number of products with vectors computed so far.

nargin: int property

The size of the input vector.

nargout: int property

The size of the output vector.

shape: Tuple[int, int] property

The shape of the operator.

symmetric: bool property

Indicate whether the operator is symmetric or not.

dot(x)

Numpy-like dot() method.

Source code in brahmap/base/linop.py
def dot(self, x):
    """Numpy-like dot() method."""
    return self.__mul__(x)

reset_counters()

Reset operator/vector product counter to zero.

Source code in brahmap/base/linop.py
def reset_counters(self):
    """Reset operator/vector product counter to zero."""
    self._nMatvec = 0