Beispiel #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)
def test_vector_dist(exponent):
    rn = CudaFn(5)
    [xarr, yarr], [x, y] = noise_elements(rn, n=2)

    weight = _pos_vector(CudaFn(5))

    weighting = CudaFnArrayWeighting(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)
Beispiel #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)
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)