Exemple #1
0
def test_gradient_cuda():
    """Discretized spatial gradient operator using CUDA."""

    # DiscreteLp Vector
    discr_space = odl.uniform_discr([0, 0], [6, 2.5], DATA_2D.shape,
                                    impl='cuda')
    dom_vec = discr_space.element(DATA_2D)

    # computation of gradient components with helper function
    dx0, dx1 = discr_space.cell_sides
    diff_0 = finite_diff(DATA_2D, axis=0, dx=dx0, padding_method='constant')
    diff_1 = finite_diff(DATA_2D, axis=1, dx=dx1, padding_method='constant')

    # gradient
    grad = Gradient(discr_space)
    grad_vec = grad(dom_vec)
    assert len(grad_vec) == DATA_2D.ndim
    assert all_equal(grad_vec[0].asarray(), diff_0)
    assert all_equal(grad_vec[1].asarray(), diff_1)

    # adjoint operator
    ran_vec = grad.range.element([DATA_2D, DATA_2D ** 2])
    adj_vec = grad.adjoint(ran_vec)
    lhs = ran_vec.inner(grad_vec)
    rhs = dom_vec.inner(adj_vec)
    assert lhs != 0
    assert rhs != 0
    assert lhs == rhs
Exemple #2
0
def test_gradient_cpu():
    """Discretized spatial gradient operator."""

    with pytest.raises(TypeError):
        Gradient(odl.Rn(1))

    # DiscreteLp Vector
    discr_space = odl.uniform_discr([0, 0], [6, 2.5], DATA_2D.shape)
    dom_vec = discr_space.element(DATA_2D)

    # computation of gradient components with helper function
    dx0, dx1 = discr_space.cell_sides
    diff_0 = finite_diff(DATA_2D, axis=0, dx=dx0, method='forward',
                         padding_method='constant')
    diff_1 = finite_diff(DATA_2D, axis=1, dx=dx1, method='forward',
                         padding_method='constant')

    # gradient
    grad = Gradient(discr_space)
    grad_vec = grad(dom_vec)
    assert len(grad_vec) == DATA_2D.ndim
    assert all_equal(grad_vec[0].asarray(), diff_0)
    assert all_equal(grad_vec[1].asarray(), diff_1)

    # Test adjoint operator

    ran_vec = grad.range.element([DATA_2D, DATA_2D ** 2])
    adj_vec = grad.adjoint(ran_vec)
    lhs = ran_vec.inner(grad_vec)
    rhs = dom_vec.inner(adj_vec)
    # Check not to use trivial data
    assert lhs != 0
    assert rhs != 0
    assert lhs == rhs

    # higher dimensional arrays
    lin_size = 3
    for ndim in range(1, 6):

        # DiscreteLp Vector
        discr_space = odl.uniform_discr([0.] * ndim, [lin_size] * ndim,
                                        [lin_size] * ndim)
        dom_vec = discr_space.element(ndvolume(lin_size, ndim))

        # gradient
        grad = Gradient(discr_space)
        grad(dom_vec)