예제 #1
0
def test_const_norm(exponent):
    rn = CudaFn(5)
    xarr, x = noise_elements(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 = 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)
예제 #2
0
def test_const_dist(exponent):
    rn = CudaFn(5)
    [xarr, yarr], [x, y] = noise_elements(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 = 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)
예제 #3
0
def test_const_dist(exponent):
    rn = CudaFn(5)
    [xarr, yarr], [x, y] = noise_elements(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 = 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)
예제 #4
0
def test_const_norm(exponent):
    rn = CudaFn(5)
    xarr, x = noise_elements(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 = 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)
예제 #5
0
def test_const_inner():
    rn = CudaFn(5)
    [xarr, yarr], [x, y] = noise_elements(rn, 2)

    constant = 1.5
    weighting = CudaFnConstWeighting(constant)

    true_inner = constant * np.vdot(yarr, xarr)
    assert almost_equal(weighting.inner(x, y), true_inner)
예제 #6
0
def test_const_inner():
    rn = CudaFn(5)
    [xarr, yarr], [x, y] = noise_elements(rn, 2)

    constant = 1.5
    weighting = CudaFnConstWeighting(constant)

    true_inner = constant * np.vdot(yarr, xarr)
    assert almost_equal(weighting.inner(x, y), true_inner)
예제 #7
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 = CudaFnConstWeighting(constant, exponent=exponent + 1)

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

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

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

    weighting_none = CudaFnNoWeighting(exponent=exponent)
    weighting_const = CudaFnConstWeighting(const, exponent=exponent)
    weighting_vec = CudaFnArrayWeighting(weight_vec, exponent=exponent)
    weighting_elem = CudaFnArrayWeighting(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
예제 #9
0
def test_const_init(exponent):
    const = 1.5
    CudaFnConstWeighting(const, exponent=exponent)