Example #1
0
def test_vector_dist(exponent):
    rn = CudaFn(5)
    [xarr, yarr], [x, y] = noise_elements(rn, n=2)

    weight = _pos_vector(CudaFn(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 = 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)
Example #2
0
def test_vector_norm(exponent):
    rn = CudaFn(5)
    xarr, x = noise_elements(rn)

    weight = _pos_vector(CudaFn(5))

    weighting = CudaFnVectorWeighting(weight, exponent=exponent)

    if exponent in (1.0, float('inf')):
        true_norm = np.linalg.norm(weight.asarray() * xarr, ord=exponent)
    else:
        true_norm = np.linalg.norm(weight.asarray() ** (1 / exponent) * 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(weight, 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)
Example #3
0
def test_vector_is_valid():
    rn = CudaFn(5)
    weight = _pos_vector(rn)

    weighting = CudaFnVectorWeighting(weight)

    assert weighting.is_valid()

    # Invalid
    weight[0] = 0

    weighting = CudaFnVectorWeighting(weight)

    assert not weighting.is_valid()
Example #4
0
def test_vector_inner():
    rn = CudaFn(5)
    [xarr, yarr], [x, y] = noise_elements(rn, 2)

    weight = _pos_vector(CudaFn(5))

    weighting = CudaFnVectorWeighting(weight)

    true_inner = np.vdot(yarr, xarr * weight.asarray())

    assert almost_equal(weighting.inner(x, y), true_inner)

    # Same with free function
    inner_vec = cu_weighted_inner(weight)

    assert almost_equal(inner_vec(x, y), true_inner)

    # Exponent != 2 -> no inner product, should raise
    with pytest.raises(NotImplementedError):
        CudaFnVectorWeighting(weight, exponent=1.0).inner(x, y)