def test_laplacian(space, padding): """Discretized spatial laplacian operator.""" # Invalid space with pytest.raises(TypeError): Laplacian(range=odl.rn(1)) if isinstance(padding, tuple): pad_mode, pad_const = padding else: pad_mode, pad_const = padding, 0 if pad_mode in ('order1', 'order2'): return # these pad modes not supported for laplacian # Operator instance lap = Laplacian(space, pad_mode=pad_mode, pad_const=pad_const) # Apply operator dom_vec = noise_element(space) div_dom_vec = lap(dom_vec) # computation of divergence with helper function expected_result = np.zeros(space.shape) for axis, dx in enumerate(space.cell_sides): diff_f = finite_diff(dom_vec.asarray(), axis=axis, dx=dx**2, method='forward', pad_mode=pad_mode, pad_const=pad_const) diff_b = finite_diff(dom_vec.asarray(), axis=axis, dx=dx**2, method='backward', pad_mode=pad_mode, pad_const=pad_const) expected_result += diff_f - diff_b assert all_almost_equal(expected_result, div_dom_vec.asarray()) # Adjoint operator derivative = lap.derivative() deriv_lap_dom_vec = derivative(dom_vec) ran_vec = noise_element(lap.range) adj_lap_ran_vec = derivative.adjoint(ran_vec) # Adjoint condition lhs = ran_vec.inner(deriv_lap_dom_vec) rhs = dom_vec.inner(adj_lap_ran_vec) # Check not to use trivial data assert lhs != 0 assert rhs != 0 assert almost_equal(lhs, rhs, places=4)
def test_laplacian(impl, padding): """Discretized spatial laplacian operator.""" # Invalid space with pytest.raises(TypeError): Divergence(range=odl.Rn(1)) if isinstance(padding, tuple): padding_method, padding_value = padding else: padding_method, padding_value = padding, None # DiscreteLp space = odl.uniform_discr([0, 0], [1, 1], DATA_2D.shape, impl=impl) # Operator instance lap = Laplacian(space, padding_method=padding_method, padding_value=padding_value) # Apply operator dom_vec = lap.domain.element(DATA_2D) div_dom_vec = lap(dom_vec) # computation of divergence with helper function dx0, dx1 = space.cell_sides expected_result = np.zeros(space.shape) for axis, dx in enumerate(space.cell_sides): diff_f = finite_diff(dom_vec.asarray(), axis=axis, dx=dx ** 2, method='forward', padding_method=padding_method, padding_value=padding_value) diff_b = finite_diff(dom_vec.asarray(), axis=axis, dx=dx ** 2, method='backward', padding_method=padding_method, padding_value=padding_value) expected_result += diff_f - diff_b assert all_almost_equal(expected_result, div_dom_vec.asarray()) # Adjoint operator derivative = lap.derivative() deriv_lap_dom_vec = derivative(dom_vec) ran_vec = lap.range.element(DATA_2D ** 2) adj_lap_ran_vec = derivative.adjoint(ran_vec) # Adjoint condition lhs = ran_vec.inner(deriv_lap_dom_vec) rhs = dom_vec.inner(adj_lap_ran_vec) # Check not to use trivial data assert lhs != 0 assert rhs != 0 assert almost_equal(lhs, rhs)
def test_laplacian(space, padding): """Discretized spatial laplacian operator.""" # Invalid space with pytest.raises(TypeError): Laplacian(range=odl.rn(1)) if isinstance(padding, tuple): pad_mode, pad_const = padding else: pad_mode, pad_const = padding, 0 if pad_mode in ('order1', 'order2'): return # these pad modes not supported for laplacian # Operator instance lap = Laplacian(space, pad_mode=pad_mode, pad_const=pad_const) # Apply operator dom_vec = noise_element(space) div_dom_vec = lap(dom_vec) # computation of divergence with helper function expected_result = np.zeros(space.shape) for axis, dx in enumerate(space.cell_sides): diff_f = finite_diff(dom_vec.asarray(), axis=axis, dx=dx ** 2, method='forward', pad_mode=pad_mode, pad_const=pad_const) diff_b = finite_diff(dom_vec.asarray(), axis=axis, dx=dx ** 2, method='backward', pad_mode=pad_mode, pad_const=pad_const) expected_result += diff_f - diff_b assert all_almost_equal(expected_result, div_dom_vec.asarray()) # Adjoint operator derivative = lap.derivative() deriv_lap_dom_vec = derivative(dom_vec) ran_vec = noise_element(lap.range) adj_lap_ran_vec = derivative.adjoint(ran_vec) # Adjoint condition lhs = ran_vec.inner(deriv_lap_dom_vec) rhs = dom_vec.inner(adj_lap_ran_vec) # Check not to use trivial data assert lhs != 0 assert rhs != 0 assert almost_equal(lhs, rhs, places=4)
def test_laplacian_init(): """Check initialization of ``Laplacian``.""" space = odl.uniform_discr([0, 0], [1, 1], (4, 5)) op = Laplacian(space) assert repr(op) != '' op = Laplacian(space, range=space.astype('float32')) assert repr(op) != '' op = Laplacian(space, pad_const=1) assert repr(op) != '' op = Laplacian(space, pad_mode='order0') assert repr(op) != '' with pytest.raises(TypeError): Laplacian(odl.rn(1))