Ejemplo n.º 1
0
def test_vector_dist_using_inner(fn):
    xarr, yarr, x, y = _vectors(fn, 2)

    weight_vec = _pos_array(fn)
    w = FnVectorWeighting(weight_vec)

    true_dist = np.linalg.norm(np.sqrt(weight_vec) * (xarr - yarr))
    assert almost_equal(w.dist(x, y), true_dist)
    assert almost_equal(w.dist(x, x), 0)

    # Only possible for exponent=2
    with pytest.raises(ValueError):
        FnVectorWeighting(weight_vec, exponent=1, dist_using_inner=True)

    # With free function
    w_dist = weighted_dist(weight_vec, use_inner=True)
    assert almost_equal(w_dist(x, y), true_dist)
Ejemplo n.º 2
0
def test_vector_dist_using_inner(fn):
    [xarr, yarr], [x, y] = example_vectors(fn, 2)

    weight_vec = _pos_array(fn)
    w = FnVectorWeighting(weight_vec)

    true_dist = np.linalg.norm(np.sqrt(weight_vec) * (xarr - yarr))
    # Using 3 places (single precision default) since the result is always
    # double even if the underlying computation was only single precision
    assert almost_equal(w.dist(x, y), true_dist, places=3)

    # Only possible for exponent=2
    with pytest.raises(ValueError):
        FnVectorWeighting(weight_vec, exponent=1, dist_using_inner=True)

    # With free function
    w_dist = weighted_dist(weight_vec, use_inner=True)
    assert almost_equal(w_dist(x, y), true_dist, places=3)
Ejemplo n.º 3
0
def test_vector_dist(fn, exponent):
    xarr, yarr, x, y = _vectors(fn, n=2)

    weight_vec = _pos_array(fn)
    weighting_vec = FnVectorWeighting(weight_vec, exponent=exponent)

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

    assert almost_equal(weighting_vec.dist(x, y), true_dist)

    # With free function
    pdist_vec = weighted_dist(weight_vec, exponent=exponent)
    assert almost_equal(pdist_vec(x, y), true_dist)
Ejemplo n.º 4
0
def test_vector_dist(fn, exponent):
    [xarr, yarr], [x, y] = example_vectors(fn, n=2)

    weight_vec = _pos_array(fn)
    weighting_vec = FnVectorWeighting(weight_vec, exponent=exponent)

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

    assert almost_equal(weighting_vec.dist(x, y), true_dist)

    # With free function
    pdist_vec = weighted_dist(weight_vec, exponent=exponent)
    assert almost_equal(pdist_vec(x, y), true_dist)