Ejemplo n.º 1
0
def test_array_dist_using_inner(fn):
    [xarr, yarr], [x, y] = noise_elements(fn, 2)

    weight_arr = _pos_array(fn)
    w = NumpyFnArrayWeighting(weight_arr)

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

    # With free function
    w_dist = npy_weighted_dist(weight_arr, use_inner=True)
    assert almost_equal(w_dist(x, y), true_dist, places=3)
Ejemplo n.º 2
0
def test_array_dist(fn, exponent):
    [xarr, yarr], [x, y] = noise_elements(fn, n=2)

    weight_arr = _pos_array(fn)
    weighting_arr = NumpyFnArrayWeighting(weight_arr, exponent=exponent)

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

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

    # With free function
    pdist_vec = npy_weighted_dist(weight_arr, exponent=exponent)
    assert almost_equal(pdist_vec(x, y), true_dist)