-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add conj function and adjoint tests (#62)
* Add conj function and tests for adjoint * Minor fixes * Add test for pytree * Remove self-adjoint tag
- Loading branch information
Showing
5 changed files
with
164 additions
and
4 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
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
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,57 @@ | ||
import jax | ||
import jax.numpy as jnp | ||
import jax.random as jr | ||
import pytest | ||
|
||
import lineax as lx | ||
from lineax import FunctionLinearOperator | ||
|
||
from .helpers import ( | ||
make_diagonal_operator, | ||
make_operators, | ||
make_tridiagonal_operator, | ||
shaped_allclose, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("make_operator", make_operators) | ||
@pytest.mark.parametrize("dtype", (jnp.float32, jnp.complex64)) | ||
def test_adjoint(make_operator, dtype, getkey): | ||
if make_operator is make_diagonal_operator: | ||
matrix = jnp.eye(4, dtype=dtype) | ||
tags = lx.diagonal_tag | ||
in_size = out_size = 4 | ||
elif make_operator is make_tridiagonal_operator: | ||
matrix = jnp.eye(4, dtype=dtype) | ||
tags = lx.tridiagonal_tag | ||
in_size = out_size = 4 | ||
else: | ||
matrix = jr.normal(getkey(), (3, 5), dtype=dtype) | ||
tags = () | ||
in_size = 5 | ||
out_size = 3 | ||
operator = make_operator(matrix, tags) | ||
v1, v2 = jr.normal(getkey(), (in_size,), dtype=dtype), jr.normal( | ||
getkey(), (out_size,), dtype=dtype | ||
) | ||
|
||
inner1 = operator.mv(v1) @ v2.conj() | ||
adjoint_op1 = lx.conj(operator).transpose() | ||
ov2 = adjoint_op1.mv(v2) | ||
inner2 = v1 @ ov2.conj() | ||
assert shaped_allclose(inner1, inner2) | ||
|
||
adjoint_op2 = lx.conj(operator.transpose()) | ||
ov2 = adjoint_op2.mv(v2) | ||
inner2 = v1 @ ov2.conj() | ||
assert shaped_allclose(inner1, inner2) | ||
|
||
|
||
def test_functional_pytree_adjoint(): | ||
def fn(y): | ||
return {"b": y["a"]} | ||
|
||
y_struct = jax.eval_shape(lambda: {"a": 0.0}) | ||
operator = FunctionLinearOperator(fn, y_struct) | ||
conj_operator = lx.conj(operator) | ||
assert shaped_allclose(lx.materialise(conj_operator), lx.materialise(operator)) |