Exemple #1
0
def test_matrix_dist(fn, exponent):
    [xarr, yarr], [x, y] = example_vectors(fn, n=2)
    sparse_mat = _sparse_matrix(fn)
    sparse_mat_as_dense = np.asarray(sparse_mat.todense())
    dense_mat = _dense_matrix(fn)

    if exponent == 1.0:  # d(x, y)_{A,1} = ||A(x-y)||_1
        true_dist_sparse = np.linalg.norm(np.dot(sparse_mat_as_dense,
                                                 xarr - yarr),
                                          ord=exponent)
        true_dist_dense = np.linalg.norm(np.dot(dense_mat, xarr - yarr),
                                         ord=exponent)
    elif exponent == 2.0:  # d(x, y)_{A,2} = sqrt(<x-y, A(x-y)>)
        true_dist_sparse = np.sqrt(
            np.vdot(xarr - yarr, np.dot(sparse_mat_as_dense, xarr - yarr)))
        true_dist_dense = np.sqrt(
            np.vdot(xarr - yarr, np.dot(dense_mat, xarr - yarr)))
    elif exponent == float('inf'):  # d(x, y)_{A,inf} = ||A(x-y)||_inf
        true_dist_sparse = np.linalg.norm(sparse_mat_as_dense.dot(xarr - yarr),
                                          ord=exponent)
        true_dist_dense = np.linalg.norm(dense_mat.dot(xarr - yarr),
                                         ord=exponent)
    else:  # d(x, y)_{A,p} = ||A^{1/p} (x-y)||_p
        # Calculate matrix power
        eigval, eigvec = sp.linalg.eigh(dense_mat)
        eigval **= 1.0 / exponent
        mat_pow = (eigval * eigvec).dot(eigvec.conj().T)
        true_dist_dense = np.linalg.norm(np.dot(mat_pow, xarr - yarr),
                                         ord=exponent)

    if exponent in (1.0, 2.0, float('inf')):
        w_sparse = FnMatrixWeighting(sparse_mat, exponent=exponent)
        assert almost_equal(w_sparse.dist(x, y), true_dist_sparse)

    w_dense = FnMatrixWeighting(dense_mat, exponent=exponent)
    assert almost_equal(w_dense.dist(x, y), true_dist_dense)

    # With free functions
    if exponent in (1.0, 2.0, float('inf')):
        w_sparse_dist = weighted_dist(sparse_mat, exponent=exponent)
        assert almost_equal(w_sparse_dist(x, y), true_dist_sparse)

    w_dense_dist = weighted_dist(dense_mat, exponent=exponent)
    assert almost_equal(w_dense_dist(x, y), true_dist_dense)
Exemple #2
0
def test_matrix_dist(fn, exponent):
    xarr, yarr, x, y = _vectors(fn, n=2)
    sparse_mat = _sparse_matrix(fn)
    sparse_mat_as_dense = np.asarray(sparse_mat.todense())
    dense_mat = _dense_matrix(fn)

    if exponent == 1.0:  # d(x, y)_{A,1} = ||A(x-y)||_1
        true_dist_sparse = np.linalg.norm(
            np.dot(sparse_mat_as_dense, xarr - yarr), ord=exponent)
        true_dist_dense = np.linalg.norm(
            np.dot(dense_mat, xarr - yarr), ord=exponent)
    elif exponent == 2.0:  # d(x, y)_{A,2} = sqrt(<x-y, A(x-y)>)
        true_dist_sparse = np.sqrt(
            np.vdot(xarr - yarr, np.dot(sparse_mat_as_dense, xarr - yarr)))
        true_dist_dense = np.sqrt(
            np.vdot(xarr - yarr, np.dot(dense_mat, xarr - yarr)))
    elif exponent == float('inf'):  # d(x, y)_{A,inf} = ||A(x-y)||_inf
        true_dist_sparse = np.linalg.norm(sparse_mat_as_dense.dot(xarr - yarr),
                                          ord=exponent)
        true_dist_dense = np.linalg.norm(dense_mat.dot(xarr - yarr),
                                         ord=exponent)
    else:  # d(x, y)_{A,p} = ||A^{1/p} (x-y)||_p
        # Calculate matrix power
        eigval, eigvec = sp.linalg.eigh(dense_mat)
        eigval **= 1.0 / exponent
        mat_pow = (eigval * eigvec).dot(eigvec.conj().T)
        true_dist_dense = np.linalg.norm(np.dot(mat_pow, xarr - yarr),
                                         ord=exponent)

    if exponent in (1.0, 2.0, float('inf')):
        w_sparse = FnMatrixWeighting(sparse_mat, exponent=exponent)
        assert almost_equal(w_sparse.dist(x, y), true_dist_sparse)

    w_dense = FnMatrixWeighting(dense_mat, exponent=exponent)
    assert almost_equal(w_dense.dist(x, y), true_dist_dense)

    # With free functions
    if exponent in (1.0, 2.0, float('inf')):
        w_sparse_dist = weighted_dist(sparse_mat, exponent=exponent)
        assert almost_equal(w_sparse_dist(x, y), true_dist_sparse)

    w_dense_dist = weighted_dist(dense_mat, exponent=exponent)
    assert almost_equal(w_dense_dist(x, y), true_dist_dense)
Exemple #3
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)
Exemple #4
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)
Exemple #5
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)
Exemple #6
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)
Exemple #7
0
def test_matrix_dist_using_inner(fn):
    xarr, yarr, x, y = _vectors(fn, 2)
    mat = _dense_matrix(fn)

    w = FnMatrixWeighting(mat, dist_using_inner=True)

    true_dist = np.sqrt(np.vdot(xarr - yarr, np.dot(mat, xarr - yarr)))
    assert almost_equal(w.dist(x, y), true_dist)

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

    # With free function
    w_dist = weighted_dist(mat, use_inner=True)
    assert almost_equal(w_dist(x, y), true_dist)
    assert almost_equal(w.dist(x, x), 0)
Exemple #8
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)
Exemple #9
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)
Exemple #10
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)
Exemple #11
0
def test_matrix_dist_using_inner(fn):
    [xarr, yarr], [x, y] = example_vectors(fn, 2)
    mat = _dense_matrix(fn)

    w = FnMatrixWeighting(mat, dist_using_inner=True)

    true_dist = np.sqrt(np.vdot(xarr - yarr, np.dot(mat, 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):
        FnMatrixWeighting(mat, exponent=1, dist_using_inner=True)

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