-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
220f60b
commit 25a1686
Showing
2 changed files
with
46 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"""Tests for linear operator arithmetics fallbacks.""" | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
from probnum.linops._arithmetic_fallbacks import ( | ||
NegatedLinearOperator, | ||
ProductLinearOperator, | ||
ScaledLinearOperator, | ||
SumLinearOperator, | ||
) | ||
from probnum.linops._linear_operator import Matrix | ||
from probnum.problems.zoo.linalg import random_spd_matrix | ||
|
||
|
||
@pytest.fixture | ||
def rng(): | ||
return np.random.default_rng(123) | ||
|
||
|
||
@pytest.fixture | ||
def scalar(): | ||
return 3.14 | ||
|
||
|
||
@pytest.fixture | ||
def rand_spd_mat(rng): | ||
return Matrix(random_spd_matrix(rng, dim=4)) | ||
|
||
|
||
def test_scaled_linop(rand_spd_mat, scalar): | ||
with pytest.raises(TypeError): | ||
ScaledLinearOperator(np.random.rand(4, 4), scalar=scalar) | ||
with pytest.raises(TypeError): | ||
ScaledLinearOperator(rand_spd_mat, scalar=np.ones(4)) | ||
|
||
scaled1 = ScaledLinearOperator(rand_spd_mat, scalar=0.0) | ||
scaled2 = ScaledLinearOperator(rand_spd_mat, scalar=scalar) | ||
|
||
with pytest.raises(np.linalg.LinAlgError): | ||
scaled1.inv() | ||
|
||
assert np.allclose( | ||
scaled2.inv().todense(), (1.0 / scalar) * scaled2._linop.inv().todense() | ||
) |