-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a do-nothing linear operator. This might be further extended to allow for multiple inputs (i.e. an endomorph overload).
- Loading branch information
1 parent
f64add5
commit 567089a
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""Identity Operator.""" | ||
|
||
import torch | ||
|
||
from mrpro.operators.LinearOperator import LinearOperator | ||
|
||
|
||
class IdentityOp(LinearOperator): | ||
r"""The Identity Operator. | ||
A Linear Operator that returns a single input unchanged. | ||
""" | ||
|
||
def __init__(self) -> None: | ||
"""Initialize Identity Operator.""" | ||
super().__init__() | ||
|
||
def forward(self, x: torch.Tensor) -> tuple[torch.Tensor]: | ||
"""Identity of input. | ||
Parameters | ||
---------- | ||
x | ||
input tensor | ||
Returns | ||
------- | ||
the input tensor | ||
""" | ||
return (x,) | ||
|
||
def adjoint(self, x: torch.Tensor) -> tuple[torch.Tensor]: | ||
"""Adjoint Identity. | ||
Parameters | ||
---------- | ||
x | ||
input tensor | ||
Returns | ||
------- | ||
the input tensor | ||
""" | ||
return (x,) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""Tests for Identity Linear Operator.""" | ||
|
||
import torch | ||
from mrpro.operators import IdentityOp | ||
|
||
from tests import RandomGenerator | ||
|
||
|
||
def test_identity_op(): | ||
"""Test forward identity.""" | ||
generator = RandomGenerator(seed=0) | ||
tensor = generator.complex64_tensor(2, 3, 4) | ||
operator = IdentityOp() | ||
torch.testing.assert_close(tensor, *operator(tensor)) | ||
assert tensor is operator(tensor)[0] | ||
|
||
|
||
def test_identity_op_adjoint(): | ||
"""Test adjoint identity.""" | ||
generator = RandomGenerator(seed=0) | ||
tensor = generator.complex64_tensor(2, 3, 4) | ||
operator = IdentityOp().H | ||
torch.testing.assert_close(tensor, *operator(tensor)) | ||
assert tensor is operator(tensor)[0] | ||
|
||
|
||
def test_identity_op_operatorsyntax(): | ||
"""Test Identity@(Identity*alpha) + (beta*Identity.H).H""" | ||
generator = RandomGenerator(seed=0) | ||
tensor = generator.complex64_tensor(2, 3, 4) | ||
alpha = generator.complex64_tensor(2, 3, 4) | ||
beta = generator.complex64_tensor(2, 3, 4) | ||
composition = IdentityOp() @ (IdentityOp() * alpha) + (beta * IdentityOp().H).H | ||
expected = tensor * alpha + tensor * beta.conj() | ||
(actual,) = composition(tensor) | ||
torch.testing.assert_close(actual, expected) |