Esempio n. 1
0
def test_constant_dist(fn, exponent):
    xarr, yarr, x, y = _vectors(fn, 2)

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

    w_const = FnConstWeighting(constant, exponent=exponent)
    assert almost_equal(w_const.dist(x, y), true_dist)

    # With free function
    w_const_dist = weighted_dist(constant, exponent=exponent)
    assert almost_equal(w_const_dist(x, y), true_dist)
Esempio n. 2
0
def test_constant_dist(fn, exponent):
    [xarr, yarr], [x, y] = example_vectors(fn, 2)

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

    w_const = FnConstWeighting(constant, exponent=exponent)
    assert almost_equal(w_const.dist(x, y), true_dist)

    # With free function
    w_const_dist = weighted_dist(constant, exponent=exponent)
    assert almost_equal(w_const_dist(x, y), true_dist)
Esempio n. 3
0
def test_const_dist_using_inner(fn):
    xarr, yarr, x, y = _vectors(fn, 2)

    constant = 1.5
    w = FnConstWeighting(constant)

    true_dist = np.sqrt(constant) * np.linalg.norm(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):
        FnConstWeighting(constant, exponent=1, dist_using_inner=True)

    # With free function
    w_dist = weighted_dist(constant, use_inner=True)
    assert almost_equal(w_dist(x, y), true_dist)
Esempio n. 4
0
def test_const_dist_using_inner(fn):
    [xarr, yarr], [x, y] = example_vectors(fn, 2)

    constant = 1.5
    w = FnConstWeighting(constant)

    true_dist = np.sqrt(constant) * np.linalg.norm(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):
        FnConstWeighting(constant, exponent=1, dist_using_inner=True)

    # With free function
    w_dist = weighted_dist(constant, use_inner=True)
    assert almost_equal(w_dist(x, y), true_dist, places=3)