Beispiel #1
0
def test_const_dist(exponent):
    rn = odl.CudaRn(5)
    xarr, yarr, x, y = _vectors(rn, n=2)

    constant = 1.5
    weighting = CudaFnConstWeighting(constant, exponent=exponent)

    factor = 1 if exponent == float('inf') else constant ** (1 / exponent)
    true_dist = factor * np.linalg.norm(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(constant, 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_const_dist(exponent):
    rn = CudaRn(5)
    [xarr, yarr], [x, y] = example_vectors(rn, n=2)

    constant = 1.5
    weighting = CudaFnConstWeighting(constant, exponent=exponent)

    factor = 1 if exponent == float('inf') else constant ** (1 / exponent)
    true_dist = factor * np.linalg.norm(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(constant, 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_const_norm(exponent):
    rn = CudaRn(5)
    xarr, x = example_vectors(rn)

    constant = 1.5
    weighting = CudaFnConstWeighting(constant, exponent=exponent)

    factor = 1 if exponent == float('inf') else constant ** (1 / exponent)
    true_norm = factor * np.linalg.norm(xarr, ord=exponent)

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

    # Same with free function
    pnorm = odl.cu_weighted_norm(constant, exponent=exponent)
    if exponent == float('inf'):
        # Not yet implemented, should raise
        with pytest.raises(NotImplementedError):
            pnorm(x)
    else:
        assert almost_equal(pnorm(x), true_norm)
Beispiel #4
0
def test_const_inner():
    rn = odl.CudaRn(5)
    xarr, yarr, x, y = _vectors(rn, 2)

    constant = 1.5
    weighting = CudaFnConstWeighting(constant)

    true_inner = constant * np.vdot(yarr, xarr)
    assert almost_equal(weighting.inner(x, y), true_inner)
Beispiel #5
0
def test_const_inner():
    rn = CudaRn(5)
    [xarr, yarr], [x, y] = example_vectors(rn, 2)

    constant = 1.5
    weighting = CudaFnConstWeighting(constant)

    true_inner = constant * np.vdot(yarr, xarr)
    assert almost_equal(weighting.inner(x, y), true_inner)
Beispiel #6
0
def test_const_equals(exponent):
    constant = 1.5

    weighting = CudaFnConstWeighting(constant, exponent=exponent)
    weighting2 = CudaFnConstWeighting(constant, exponent=exponent)
    other_weighting = CudaFnConstWeighting(2.5, exponent=exponent)
    wrong_exp = FnConstWeighting(constant, exponent=exponent + 1)

    assert weighting == weighting
    assert weighting == weighting2
    assert weighting2 == weighting

    assert weighting != other_weighting
    assert weighting != wrong_exp
Beispiel #7
0
def test_init_weighting(exponent):
    const = 1.5
    weight_vec = _pos_vector(CudaRn(3))
    weight_elem = CudaFn(3, dtype='float32').element(weight_vec)

    f3_none = CudaFn(3, dtype='float32', exponent=exponent)
    f3_const = CudaFn(3, dtype='float32', weight=const, exponent=exponent)
    f3_vec = CudaFn(3, dtype='float32', weight=weight_vec, exponent=exponent)
    f3_elem = CudaFn(3, dtype='float32', weight=weight_elem, exponent=exponent)

    weighting_none = CudaFnNoWeighting(exponent=exponent)
    weighting_const = CudaFnConstWeighting(const, exponent=exponent)
    weighting_vec = CudaFnVectorWeighting(weight_vec, exponent=exponent)
    weighting_elem = CudaFnVectorWeighting(weight_elem, exponent=exponent)

    assert f3_none.weighting == weighting_none
    assert f3_const.weighting == weighting_const
    assert f3_vec.weighting == weighting_vec
    assert f3_elem.weighting == weighting_elem
Beispiel #8
0
def test_const_init(exponent):
    const = 1.5
    CudaFnConstWeighting(const, exponent=exponent)