LinearOperator

class xitorch.LinearOperator(*args, **kwargs)[source]

LinearOperator is a base class designed to behave as a linear operator without explicitly determining the matrix. This LinearOperator should be able to operate as batched linear operators where its shape is (B1,B2,...,Bb,p,q) with B* as the (optional) batch dimensions.

For a user-defined class to behave as LinearOperator, it must use LinearOperator as one of the parent and it has to have ._mv() method implemented and ._getparamnames() if used in xitorch’s functionals with torch grad enabled.

classmethod m(mat: torch.Tensor, is_hermitian: Optional[bool] = None)[source]

Class method to wrap a matrix into LinearOperator.

Parameters
  • mat (torch.Tensor) – Matrix to be wrapped in the LinearOperator.

  • is_hermitian (bool or None) – Indicating if the matrix is Hermitian. If None, the symmetry will be checked. If supplied as a bool, there is no check performed.

Returns

Linear operator object that represents the matrix.

Return type

LinearOperator

Example

>>> mat = torch.rand(1,3,1,2)  # 1x2 matrix with (1,3) batch dimensions
>>> linop = xitorch.LinearOperator.m(mat)
>>> print(linop)
MatrixLinearOperator with shape (1, 3, 1, 2):
   tensor([[[[0.1117, 0.8158]],

            [[0.2626, 0.4839]],

            [[0.6765, 0.7539]]]])
abstract _getparamnames(prefix: str = '') → List[str][source]

List the self’s parameters that affecting the LinearOperator. This is for the derivative purpose.

Parameters

prefix (str) – The prefix to be appended in front of the parameters name. This usually contains the dots.

Returns

List of parameter names (including the prefix) that affecting the LinearOperator.

Return type

list of str

abstract _mv(x: torch.Tensor) → torch.Tensor[source]

Abstract method to be implemented for matrix-vector multiplication. Required for all LinearOperator objects.

_rmv(x: torch.Tensor) → torch.Tensor[source]

Abstract method to be implemented for transposed matrix-vector multiplication. Optional. If not implemented, it will use the adjoint trick to compute .rmv(). Usually implemented for efficiency reasons.

_mm(x: torch.Tensor) → torch.Tensor[source]

Abstract method to be implemented for matrix-matrix multiplication. If not implemented, then it uses batched version of matrix-vector multiplication. Usually this is implemented for efficiency reasons.

_rmm(x: torch.Tensor) → torch.Tensor[source]

Abstract method to be implemented for transposed matrix-matrix multiplication. If not implemented, then it uses batched version of transposed matrix-vector multiplication. Usually this is implemented for efficiency reasons.

mv(x: torch.Tensor) → torch.Tensor[source]

Apply the matrix-vector operation to vector x with shape (...,q). The batch dimensions of x need not be the same as the batch dimensions of the LinearOperator, but it must be broadcastable.

Parameters

x (torch.tensor) – The vector with shape (...,q) where the linear operation is operated on

Returns

y – The result of the linear operation with shape (...,p)

Return type

torch.tensor

mm(x: torch.Tensor) → torch.Tensor[source]

Apply the matrix-matrix operation to matrix x with shape (...,q,r). The batch dimensions of x need not be the same as the batch dimensions of the LinearOperator, but it must be broadcastable.

Parameters

x (torch.tensor) – The matrix with shape (...,q,r) where the linear operation is operated on.

Returns

y – The result of the linear operation with shape (...,p,r)

Return type

torch.tensor

rmv(x: torch.Tensor) → torch.Tensor[source]

Apply the matrix-vector adjoint operation to vector x with shape (...,p), i.e. A^H x. The batch dimensions of x need not be the same as the batch dimensions of the LinearOperator, but it must be broadcastable.

Parameters

x (torch.tensor) – The vector of shape (...,p) where the adjoint linear operation is operated at.

Returns

y – The result of the adjoint linear operation with shape (...,q)

Return type

torch.tensor

rmm(x: torch.Tensor) → torch.Tensor[source]

Apply the matrix-matrix adjoint operation to matrix x with shape (...,p,r), i.e. A^H X. The batch dimensions of x need not be the same as the batch dimensions of the LinearOperator, but it must be broadcastable.

Parameters

x (torch.Tensor) – The matrix of shape (...,p,r) where the adjoint linear operation is operated on.

Returns

y – The result of the adjoint linear operation with shape (...,q,r).

Return type

torch.Tensor

property H

Returns a LinearOperator representing the Hermite / transposed of the self LinearOperator.

Returns

The Hermite / transposed LinearOperator

Return type

LinearOperator

matmul(b: xitorch._core.linop.LinearOperator, is_hermitian: bool = False)[source]

Returns a LinearOperator representing self @ b.

Parameters
  • b (LinearOperator) – Other linear operator

  • is_hermitian (bool) – Flag to indicate if the resulting LinearOperator is Hermitian.

Returns

LinearOperator representing self @ b

Return type

LinearOperator

check(warn: Optional[bool] = None) → None[source]

Perform checks to make sure the LinearOperator behaves as a proper linear operator.

Parameters

warn (bool or None) – If True, then raises a warning to the user that the check might slow down the program. This is to remind the user to turn off the check when not in a debugging mode. If None, it will raise a warning if it runs not in a debug mode, but will be silent if it runs in a debug mode.

Raises
  • RuntimeError – Raised if an error is raised when performing linear operations of the object (e.g. calling .mv(), .mm(), etc)

  • AssertionError – Raised if the linear operations do not behave as proper linear operations. (e.g. not scaling linearly)