def test_linear_composition(): A = np.random.rand(5, 4) B = np.random.rand(4, 3) x = np.random.rand(3) y = np.random.rand(5) Aop = MatVecOperator(A) Bop = MatVecOperator(B) xvec = Bop.domain.element(x) yvec = Aop.range.element(y) C = OperatorComp(Aop, Bop) assert C.is_linear assert C.adjoint.is_linear assert all_almost_equal(C(xvec), np.dot(A, np.dot(B, x))) assert all_almost_equal(C.adjoint(yvec), np.dot(B.T, np.dot(A.T, y)))
def test_nonlinear_composition(): A = np.random.rand(5, 4) B = np.random.rand(4, 3) x = np.random.rand(3) Aop = MultiplyAndSquareOp(A) Bop = MultiplyAndSquareOp(B) xvec = Bop.domain.element(x) C = OperatorComp(Aop, Bop) assert not C.is_linear assert all_almost_equal(C(xvec), mult_sq_np(A, mult_sq_np(B, x))) # Verify that incorrect order fails with pytest.raises(TypeError): C = OperatorComp(Bop, Aop)
def test_operator_composition(dom_eq_ran): """Check operator composition against NumPy reference.""" if dom_eq_ran: mat1 = np.random.rand(3, 3) mat2 = np.random.rand(3, 3) else: mat1 = np.random.rand(5, 4) mat2 = np.random.rand(4, 3) op1 = MultiplyAndSquareOp(mat1) op2 = MultiplyAndSquareOp(mat2) xarr, x = noise_elements(op2.domain) comp_op = OperatorComp(op1, op2) assert not comp_op.is_linear check_call(comp_op, x, mult_sq_np(mat1, mult_sq_np(mat2, xarr))) # Verify that incorrect order fails if not dom_eq_ran: with pytest.raises(OpTypeError): OperatorComp(op2, op1)
def test_functional_composition(): r3 = odl.Rn(3) Aop = SumFunctional(r3) Bop = ConstantVector(r3) x = r3.element([1, 2, 3]) y = 1 C = OperatorComp(Bop, Aop) assert C.is_linear assert C.adjoint.is_linear assert all_almost_equal(C(x), np.sum(x) * np.ones(3)) assert all_almost_equal(C.adjoint(x), np.sum(x) * np.ones(3)) assert all_almost_equal(C.adjoint.adjoint(x), C(x)) # Using operator overloading assert (Aop * Bop)(y) == y * 3 assert (Aop * Bop).adjoint(y) == y * 3 assert all_almost_equal((Bop * Aop)(x), np.sum(x) * np.ones(3)) assert all_almost_equal((Bop * Aop).adjoint(x), np.sum(x) * np.ones(3))
def test_linear_operator_composition(dom_eq_ran): """Check call and adjoint of linear operator composition.""" if dom_eq_ran: mat1 = np.random.rand(3, 3) mat2 = np.random.rand(3, 3) else: mat1 = np.random.rand(4, 3) mat2 = np.random.rand(3, 4) op1 = MatrixOperator(mat1) op2 = MatrixOperator(mat2) xarr, x = noise_elements(op2.domain) yarr, y = noise_elements(op1.range) # Explicit instantiation comp_op = OperatorComp(op1, op2) assert comp_op.is_linear assert comp_op.adjoint.is_linear check_call(comp_op, x, np.dot(mat1, np.dot(mat2, xarr))) check_call(comp_op.adjoint, y, np.dot(mat2.T, np.dot(mat1.T, yarr))) # Using operator overloading check_call(op1 * op2, x, np.dot(mat1, np.dot(mat2, xarr))) check_call((op1 * op2).adjoint, y, np.dot(mat2.T, np.dot(mat1.T, yarr)))