mrpro.operators.EinsumOp
- class mrpro.operators.EinsumOp[source]
Bases:
LinearOperator
A Linear Operator that implements sum products in Einstein notation.
Implements \(A_{\mathrm{indices}_A}*x^{\mathrm{indices}_x} = y_{\mathrm{indices}_y}\) with Einstein summation rules over the \(indices\), see
torch.einsum
oreinops.einsum
for more information. Note, that the indices must be space separated (einops convention).It can be used to implement tensor contractions, such as for example, different versions of matrix-vector or matrix-matrix products of the form
A @ x
, depending on the chosen einsum rules and shapes ofA
andx
.Examples are:
matrix-vector multiplication of \(A\) and the batched vector \(x = [x1, ..., xN]\) consisting of \(N\) vectors \(x1, x2, ..., xN\). Then, the operation defined by \(A @ x := \mathrm{diag}(A, A, ..., A) * [x1, x2, ..., xN]^T\) = \([A*x1, A*x2, ..., A*xN]^T\) can be implemented by the einsum rule
"i j, ... j -> ... i"
.matrix-vector multiplication of a matrix \(A\) consisting of \(N\) different matrices \(A1, A2, ... AN\) with one vector \(x\). Then, the operation defined by \(A @ x: = \mathrm{diag}(A1, A2,..., AN) * [x, x, ..., x]^T\) can be implemented by the einsum rule
"... i j, j -> ... i"
.matrix-vector multiplication of a matrix \(A\) consisting of \(N\) different matrices \(A1, A2, ... AN\) with a vector \(x = [x1,...,xN]\) consisting of \(N\) vectors \(x1, x2, ..., xN\). Then, the operation defined by \(A @ x: = \mathrm{diag}(A1, A2,..., AN) * [x1, x2, ..., xN]^T\) can be implemented by the einsum rule
"... i j, ... j -> ... i"
. This is the default behavior of the operator.
- __init__(matrix: Tensor, einsum_rule: str = '... i j, ... j -> ... i') None [source]
Initialize Einsum Operator.
- property H: LinearOperator[source]
Adjoint operator.
Obtains the adjoint of an instance of this operator as an
AdjointLinearOperator
, which itself is a anLinearOperator
that can be applied to tensors.Note:
linear_operator.H.H == linear_operator
- property gram: LinearOperator[source]
Gram operator.
For a LinearOperator \(A\), the self-adjoint Gram operator is defined as \(A^H A\).
Note
This is the inherited default implementation.
- __call__(*args: Unpack) Tout [source]
Apply the forward operator.
For more information, see
forward
.
- adjoint(y: Tensor) tuple[Tensor] [source]
Multiplication of input with the adjoint of \(A\).
- Parameters:
y (
Tensor
) – tensor to be multiplied with hermitian/adjoint ‘matrix’ \(A\)- Returns:
result of adjoint sum product
- forward(x: Tensor) tuple[Tensor] [source]
Sum-Multiplication of input \(x\) with \(A\).
\(A\) and the rule used to perform the sum-product is set at initialization.
- Parameters:
x (
Tensor
) – input tensor to be multiplied with the ‘matrix’ \(A\).- Returns:
result of matrix-vector multiplication
- operator_norm(initial_value: Tensor, dim: Sequence[int] | None, max_iterations: int = 20, relative_tolerance: float = 1e-4, absolute_tolerance: float = 1e-5, callback: Callable[[Tensor], None] | None = None) Tensor [source]
Power iteration for computing the operator norm of the operator.
- Parameters:
initial_value (
Tensor
) – initial value to start the iteration; must be element of the domain. if the initial value contains a zero-vector for one of the considered problems, the function throws anValueError
.The dimensions of the tensors on which the operator operates. The choice of
dim
determines how the operator norm is inperpreted. For example, for a matrix-vector multiplication with a batched matrix tensor of shape(batch1, batch2, row, column)
and a batched input tensor of shape(batch1, batch2, row)
:If
dim=None
, the operator is considered as a block diagonal matrix with batch1*batch2 blocks and the result is a tensor containing a single norm value (shape(1, 1, 1)
).If
dim=(-1)
,batch1*batch2
matrices are considered, and for each a separate operator norm is computed.If
dim=(-2,-1)
,batch1
matrices withbatch2
blocks are considered, and for each matrix a separate operator norm is computed.
Thus, the choice of
dim
determines implicitly determines the domain of the operator.max_iterations (
int
, default:20
) – maximum number of iterationsrelative_tolerance (
float
, default:1e-4
) – absolute tolerance for the change of the operator-norm at each iteration; if set to zero, the maximal number of iterations is the only stopping criterion used to stop the power iteration.absolute_tolerance (
float
, default:1e-5
) – absolute tolerance for the change of the operator-norm at each iteration; if set to zero, the maximal number of iterations is the only stopping criterion used to stop the power iteration.callback (
Callable
[[Tensor
],None
] |None
, default:None
) – user-provided function to be called at each iteration
- Returns:
An estimaton of the operator norm. Shape corresponds to the shape of the input tensor
initial_value
with the dimensions specified indim
reduced to a single value. The pointwise multiplication ofinitial_value
with the result of the operator norm will always be well-defined.
- __add__(other: LinearOperator | Tensor) LinearOperator [source]
- __add__(other: Operator[Tensor, tuple[Tensor]]) Operator[Tensor, tuple[Tensor]]
Operator addition.
Returns
lambda x: self(x) + other(x)
if other is a operator,lambda x: self(x) + other
if other is a tensor
- __and__(other: LinearOperator) LinearOperatorMatrix [source]
Vertical stacking of two LinearOperators.
A&B
is aLinearOperatorMatrix
with two rows, with(A&B)(x) == (A(x), B(x))
. Seemrpro.operators.LinearOperatorMatrix
for more information.
- __matmul__(other: LinearOperator) LinearOperator [source]
- __matmul__(other: Operator[Unpack, tuple[Tensor]]) Operator[Unpack, tuple[Tensor]]
Operator composition.
Returns
lambda x: self(other(x))
- __mul__(other: Tensor | complex) LinearOperator [source]
Operator elementwise left multiplication with tensor/scalar.
Returns
lambda x: self(x*other)
- __or__(other: LinearOperator) LinearOperatorMatrix [source]
Horizontal stacking of two LinearOperators.
A|B
is aLinearOperatorMatrix
with two columns, with(A|B)(x1,x2) == A(x1)+B(x2)
. Seemrpro.operators.LinearOperatorMatrix
for more information.
- __radd__(other: Tensor) LinearOperator [source]
Operator addition.
Returns
lambda x: self(x) + other*x
- __rmul__(other: Tensor | complex) LinearOperator [source]
Operator elementwise right multiplication with tensor/scalar.
Returns
lambda x: other*self(x)