Beispiel #1
0
def test_vector_dist(exponent):
    rn = CudaRn(5)
    [xarr, yarr], [x, y] = example_vectors(rn, n=2)

    weight = _pos_vector(CudaRn(5))

    weighting = CudaFnVectorWeighting(weight, exponent=exponent)

    if exponent in (1.0, float('inf')):
        true_dist = np.linalg.norm(weight.asarray() * (xarr - yarr),
                                   ord=exponent)
    else:
        true_dist = np.linalg.norm(
            weight.asarray() ** (1 / exponent) * (xarr - yarr), ord=exponent)

    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            weighting.dist(x, y)
    else:
        assert almost_equal(weighting.dist(x, y), true_dist)

    # Same with free function
    pdist = odl.cu_weighted_dist(weight, exponent=exponent)

    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            pdist(x, y)
    else:
        assert almost_equal(pdist(x, y), true_dist)
Beispiel #2
0
def test_vector_dist(exponent):
    rn = odl.CudaRn(5)
    xarr, yarr, x, y = _vectors(rn, n=2)

    weight = _pos_vector(odl.CudaRn(5))

    weighting = CudaFnVectorWeighting(weight, exponent=exponent)

    if exponent in (1.0, float('inf')):
        true_dist = np.linalg.norm(weight.asarray() * (xarr - yarr),
                                   ord=exponent)
    else:
        true_dist = np.linalg.norm(
            weight.asarray() ** (1 / exponent) * (xarr - yarr), ord=exponent)

    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            weighting.dist(x, y)
    else:
        assert almost_equal(weighting.dist(x, y), true_dist)

    # Same with free function
    pdist = odl.cu_weighted_dist(weight, exponent=exponent)

    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            pdist(x, y)
    else:
        assert almost_equal(pdist(x, y), true_dist)
Beispiel #3
0
def test_vector_dist(exponent):
    rn = odl.CudaRn(5)
    xarr, yarr, x, y = _vectors(rn, n=2)

    weight_vec = _pos_array(odl.Rn(5))
    weight_elem = rn.element(weight_vec)

    weighting_vec = CudaFnVectorWeighting(weight_vec, exponent=exponent)
    weighting_elem = CudaFnVectorWeighting(weight_elem, exponent=exponent)

    if exponent in (1.0, float('inf')):
        true_dist = np.linalg.norm(weight_vec * (xarr - yarr), ord=exponent)
    else:
        true_dist = np.linalg.norm(
            weight_vec ** (1 / exponent) * (xarr - yarr), ord=exponent)

    if exponent == float('inf') or int(exponent) != exponent:
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            weighting_vec.dist(x, y)
        with pytest.raises(NotImplementedError):
            weighting_elem.dist(x, y)
    else:
        assert almost_equal(weighting_vec.dist(x, y), true_dist)
        assert almost_equal(weighting_elem.dist(x, y), true_dist)

    # Same with free function
    pdist_vec = odl.cu_weighted_dist(weight_vec, exponent=exponent)
    pdist_elem = odl.cu_weighted_dist(weight_elem, exponent=exponent)

    if exponent == float('inf') or int(exponent) != exponent:
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            pdist_vec(x, y)
        with pytest.raises(NotImplementedError):
            pdist_elem(x, y)
    else:
        assert almost_equal(pdist_vec(x, y), true_dist)
        assert almost_equal(pdist_elem(x, y), true_dist)