def test_linear_addition(): A = np.random.rand(4, 3) B = np.random.rand(4, 3) x = np.random.rand(3) y = np.random.rand(4) Aop = MatVecOperator(A) Bop = MatVecOperator(B) xvec = Aop.domain.element(x) yvec = Aop.range.element(y) # Explicit instantiation C = OperatorSum(Aop, Bop) assert C.is_linear assert C.adjoint.is_linear assert all_almost_equal(C(xvec), np.dot(A, x) + np.dot(B, x)) assert all_almost_equal(C.adjoint(yvec), np.dot(A.T, y) + np.dot(B.T, y)) # Using operator overloading assert all_almost_equal((Aop + Bop)(xvec), np.dot(A, x) + np.dot(B, x)) assert all_almost_equal((Aop + Bop).adjoint(yvec), np.dot(A.T, y) + np.dot(B.T, y))
def test_functional_addition(): r3 = odl.Rn(3) Aop = SumFunctional(r3) Bop = SumFunctional(r3) x = r3.element([1, 2, 3]) y = 1 # Explicit instantiation C = OperatorSum(Aop, Bop) assert C.is_linear assert C.adjoint.is_linear assert C(x) == 2 * np.sum(x) # Test adjoint assert all_almost_equal(C.adjoint(y), y * 2 * np.ones(3)) assert all_almost_equal(C.adjoint.adjoint(x), C(x)) # Using operator overloading assert (Aop + Bop)(x) == 2 * np.sum(x) assert all_almost_equal((Aop + Bop).adjoint(y), y * 2 * np.ones(3))