def test_matrix_representation_not_linear_op(): """Verify error when operator is non-linear""" class MyNonLinOp(odl.Operator): """Small nonlinear test operator.""" def _call(self, x): return x**2 nonlin_op = MyNonLinOp(domain=odl.rn(3), range=odl.rn(3), linear=False) with pytest.raises(ValueError): matrix_representation(nonlin_op)
def test_matrix_representation_not_linear_op(): # Verify that the matrix representation function gives correct error class small_nonlin_op(odl.Operator): """Small nonlinear test operator""" def __init__(self): super().__init__(domain=odl.Rn(3), range=odl.Rn(4), linear=False) def _apply(self, x, out): return odl.Rn(np.random.rand(4)) nonlin_op = small_nonlin_op() with pytest.raises(ValueError): matrix_representation(nonlin_op)
def test_matrix_representation_not_linear_op(): # Verify that the matrix representation function gives correct error class small_nonlin_op(odl.Operator): """Small nonlinear test operator""" def __init__(self): super().__init__(domain=odl.Rn(3), range=odl.Rn(4), linear=False) def _call(self, x, out): return odl.Rn(np.random.rand(4)) nonlin_op = small_nonlin_op() with pytest.raises(ValueError): matrix_representation(nonlin_op)
def test_matrix_representation_wrong_range(): """Verify that the matrix representation function gives correct error""" class MyOp(odl.Operator): """Small test operator.""" def __init__(self): super(MyOp, self).__init__(domain=odl.rn(3), range=odl.rn(3) * odl.rn(3)**2, linear=True) def _call(self, x, out): return odl.rn(np.random.rand(4)) nonlin_op = MyOp() with pytest.raises(TypeError): matrix_representation(nonlin_op)
def test_matrix_representation_not_linear_op(): # Verify that the matrix representation function gives correct error class MyNonLinOp(odl.Operator): """Small nonlinear test operator.""" def __init__(self): super(MyNonLinOp, self).__init__(domain=odl.rn(3), range=odl.rn(4), linear=False) def _call(self, x): return x**2 nonlin_op = MyNonLinOp() with pytest.raises(ValueError): matrix_representation(nonlin_op)
def test_matrix_representation_wrong_domain(): # Verify that the matrix representation function gives correct error class small_op(odl.Operator): """Small nonlinear test operator""" def __init__(self): super().__init__(domain=ProductSpace(odl.Rn(3), ProductSpace(odl.Rn(3), odl.Rn(3))), range=odl.Rn(4), linear=True) def _apply(self, x, out): return odl.Rn(np.random.rand(4)) nonlin_op = small_op() with pytest.raises(TypeError): matrix_representation(nonlin_op)
def test_matrix_representation_wrong_domain(): # Verify that the matrix representation function gives correct error class small_op(odl.Operator): """Small nonlinear test operator""" def __init__(self): super().__init__(domain=ProductSpace(odl.Rn(3), ProductSpace(odl.Rn(3), odl.Rn(3))), range=odl.Rn(4), linear=True) def _call(self, x, out): return odl.Rn(np.random.rand(4)) nonlin_op = small_op() with pytest.raises(TypeError): matrix_representation(nonlin_op)
def test_matrix_representation(): """Verify that the matrix repr returns the correct matrix""" n = 3 A = np.random.rand(n, n) Aop = odl.MatrixOperator(A) matrix_repr = matrix_representation(Aop) assert all_almost_equal(A, matrix_repr)
def test_matrix_representation(): # Verify that the matrix representation function returns the correct matrix n = 3 A = np.random.rand(n, n) Aop = odl.MatrixOperator(A) matrix_repr = matrix_representation(Aop) assert almost_equal(np.sum(np.abs(A - matrix_repr)), 1e-6)
def test_matrix_representation(): # Verify that the matrix representation function returns the correct matrix n = 3 A = np.random.rand(n, n) Aop = MatVecOperator(A) the_matrix = matrix_representation(Aop) assert almost_equal(np.sum(np.abs(A - the_matrix)), 1e-6)
def test_matrix_representation_product_to_product(): """Verify that the matrix repr works for product spaces. Here, since the domain and range has shape ``(2, 3)``, the shape of the matrix representation will be ``(2, 3, 2, 3)``. """ n = 3 A = np.random.rand(n, n) Aop = odl.MatrixOperator(A) B = np.random.rand(n, n) Bop = odl.MatrixOperator(B) ABop = ProductSpaceOperator([[Aop, 0], [0, Bop]]) matrix_repr = matrix_representation(ABop) assert matrix_repr.shape == (2, n, 2, n) assert np.linalg.norm(A - matrix_repr[0, :, 0, :]) == pytest.approx(0) assert np.linalg.norm(B - matrix_repr[1, :, 1, :]) == pytest.approx(0)
def test_matrix_representation_product_to_product_two(): # Verify that the matrix representation function returns the correct matrix n = 3 rn = odl.rn(n) A = np.random.rand(n, n) Aop = odl.MatrixOperator(A) B = np.random.rand(n, n) Bop = odl.MatrixOperator(B) ran_and_dom = ProductSpace(rn, 2) AB_matrix = np.vstack( [np.hstack([A, np.zeros((n, n))]), np.hstack([np.zeros((n, n)), B])]) ABop = ProductSpaceOperator([[Aop, 0], [0, Bop]], ran_and_dom, ran_and_dom) the_matrix = matrix_representation(ABop) assert almost_equal(np.sum(np.abs(AB_matrix - the_matrix)), 1e-6)
def test_matrix_representation_product_to_product_two(): # Verify that the matrix representation function returns the correct matrix n = 3 rn = odl.Rn(n) A = np.random.rand(n, n) Aop = MatVecOperator(A) B = np.random.rand(n, n) Bop = MatVecOperator(B) ran_and_dom = ProductSpace(rn, 2) AB_matrix = np.vstack([np.hstack([A, np.zeros((n, n))]), np.hstack([np.zeros((n, n)), B])]) ABop = ProductSpaceOperator([[Aop, 0], [0, Bop]], ran_and_dom, ran_and_dom) the_matrix = matrix_representation(ABop) assert almost_equal(np.sum(np.abs(AB_matrix - the_matrix)), 1e-6)
def test_matrix_representation_lin_space_to_product(): # Verify that the matrix representation function returns the correct matrix n = 3 rn = odl.Rn(n) A = np.random.rand(n, n) Aop = MatVecOperator(A) m = 2 rm = odl.Rn(m) B = np.random.rand(m, n) Bop = MatVecOperator(B) dom = ProductSpace(rn, 1) ran = ProductSpace(rn, rm) AB_matrix = np.vstack([A, B]) ABop = ProductSpaceOperator([[Aop], [Bop]], dom, ran) the_matrix = matrix_representation(ABop) assert almost_equal(np.sum(np.abs(AB_matrix - the_matrix)), 1e-6)
def test_matrix_representation_lin_space_to_product(): # Verify that the matrix representation function returns the correct matrix n = 3 rn = odl.rn(n) A = np.random.rand(n, n) Aop = odl.MatrixOperator(A) m = 2 rm = odl.rn(m) B = np.random.rand(m, n) Bop = odl.MatrixOperator(B) dom = ProductSpace(rn, 1) ran = ProductSpace(rn, rm) AB_matrix = np.vstack([A, B]) ABop = ProductSpaceOperator([[Aop], [Bop]], dom, ran) the_matrix = matrix_representation(ABop) assert almost_equal(np.sum(np.abs(AB_matrix - the_matrix)), 1e-6)