Beispiel #1
0
def test_constant_equals():
    n = 10
    constant = 1.5

    w_const = FnConstWeighting(constant)
    w_const2 = FnConstWeighting(constant)
    w_other_const = FnConstWeighting(constant + 1)
    w_other_exp = FnConstWeighting(constant, exponent=1)

    const_sparse_mat = sp.sparse.dia_matrix(([constant] * n, [0]),
                                            shape=(n, n))
    const_dense_mat = constant * np.eye(n)
    w_matrix_sp = FnMatrixWeighting(const_sparse_mat)
    w_matrix_de = FnMatrixWeighting(const_dense_mat)

    assert w_const == w_const
    assert w_const == w_const2
    assert w_const2 == w_const
    # Equivalent but not equal -> False
    assert w_const != w_matrix_sp
    assert w_const != w_matrix_de

    # Different
    assert w_const != w_other_const
    assert w_const != w_other_exp
Beispiel #2
0
def test_constant_init(exponent):
    constant = 1.5

    # Just test if the code runs
    FnConstWeighting(constant, exponent=exponent)

    with pytest.raises(ValueError):
        FnConstWeighting(0)
    with pytest.raises(ValueError):
        FnConstWeighting(-1)
    with pytest.raises(ValueError):
        FnConstWeighting(float('inf'))
Beispiel #3
0
def test_constant_inner(fn):
    xarr, yarr, x, y = _vectors(fn, 2)

    constant = 1.5
    true_result_const = constant * np.vdot(yarr, xarr)

    w_const = FnConstWeighting(constant)
    assert almost_equal(w_const.inner(x, y), true_result_const)

    # Exponent != 2 -> no inner
    w_const = FnConstWeighting(constant, exponent=1)
    with pytest.raises(NotImplementedError):
        w_const.inner(x, y)
Beispiel #4
0
def test_constant_equiv():
    n = 10
    constant = 1.5

    w_const = FnConstWeighting(constant)
    w_const2 = FnConstWeighting(constant)

    const_sparse_mat = sp.sparse.dia_matrix(([constant] * n, [0]),
                                            shape=(n, n))
    const_dense_mat = constant * np.eye(n)
    w_matrix_sp = FnMatrixWeighting(const_sparse_mat)
    w_matrix_de = FnMatrixWeighting(const_dense_mat)

    # Equal -> True
    assert w_const.equiv(w_const)
    assert w_const.equiv(w_const2)
    # Equivalent matrix representation -> True
    assert w_const.equiv(w_matrix_sp)
    assert w_const.equiv(w_matrix_de)

    w_different_const = FnConstWeighting(2.5)
    assert not w_const.equiv(w_different_const)

    # Bogus input
    assert not w_const.equiv(True)
    assert not w_const.equiv(object)
    assert not w_const.equiv(None)
Beispiel #5
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)
Beispiel #6
0
def test_constant_norm(fn, exponent):
    xarr, x = _vectors(fn)

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

    w_const = FnConstWeighting(constant, exponent=exponent)
    assert almost_equal(w_const.norm(x), true_norm)

    # With free function
    w_const_norm = weighted_norm(constant, exponent=exponent)
    assert almost_equal(w_const_norm(x), true_norm)
Beispiel #7
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)
Beispiel #8
0
def test_constant_norm(fn, exponent):
    xarr, x = example_vectors(fn)

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

    w_const = FnConstWeighting(constant, exponent=exponent)
    assert almost_equal(w_const.norm(x), true_norm)

    # With free function
    w_const_norm = weighted_norm(constant, exponent=exponent)
    assert almost_equal(w_const_norm(x), true_norm)
Beispiel #9
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)
Beispiel #10
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)
Beispiel #11
0
def test_const_equals(exponent):
    constant = 1.5

    weighting = CudaFnConstWeighting(constant, exponent=exponent)
    weighting2 = CudaFnConstWeighting(constant, exponent=exponent)
    other_weighting = CudaFnConstWeighting(2.5, exponent=exponent)
    wrong_exp = FnConstWeighting(constant, exponent=exponent + 1)

    assert weighting == weighting
    assert weighting == weighting2
    assert weighting2 == weighting

    assert weighting != other_weighting
    assert weighting != wrong_exp
Beispiel #12
0
def test_constant_inner(fn):
    [xarr, yarr], [x, y] = example_vectors(fn, 2)

    constant = 1.5
    true_result_const = constant * np.vdot(yarr, xarr)

    w_const = FnConstWeighting(constant)
    assert almost_equal(w_const.inner(x, y), true_result_const)

    # Exponent != 2 -> no inner
    w_const = FnConstWeighting(constant, exponent=1)
    with pytest.raises(NotImplementedError):
        w_const.inner(x, y)
Beispiel #13
0
def test_vector_equiv():
    rn = Rn(5)
    weight_vec = _pos_array(rn)
    weight_elem = rn.element(weight_vec)
    diag_mat = weight_vec * np.eye(5)
    different_vec = weight_vec - 1

    w_vec = FnVectorWeighting(weight_vec)
    w_elem = FnVectorWeighting(weight_elem)
    w_diag_mat = FnMatrixWeighting(diag_mat)
    w_different_vec = FnVectorWeighting(different_vec)

    # Equal -> True
    assert w_vec.equiv(w_vec)
    assert w_vec.equiv(w_elem)
    # Equivalent matrix -> True
    assert w_vec.equiv(w_diag_mat)
    # Different vector -> False
    assert not w_vec.equiv(w_different_vec)

    # Test shortcuts
    const_vec = np.ones(5) * 1.5

    w_vec = FnVectorWeighting(const_vec)
    w_const = FnConstWeighting(1.5)
    w_wrong_const = FnConstWeighting(1)
    w_wrong_exp = FnConstWeighting(1.5, exponent=1)

    assert w_vec.equiv(w_const)
    assert not w_vec.equiv(w_wrong_const)
    assert not w_vec.equiv(w_wrong_exp)

    # Bogus input
    assert not w_vec.equiv(True)
    assert not w_vec.equiv(object)
    assert not w_vec.equiv(None)
Beispiel #14
0
def test_init_weighting(exponent):
    const = 1.5
    weight_vec = _pos_array(Rn(3, float))
    weight_mat = _dense_matrix(Rn(3, float))

    spaces = [
        Fn(3, complex, exponent=exponent, weight=const),
        Fn(3, complex, exponent=exponent, weight=weight_vec),
        Fn(3, complex, exponent=exponent, weight=weight_mat)
    ]
    weightings = [
        FnConstWeighting(const, exponent=exponent),
        FnVectorWeighting(weight_vec, exponent=exponent),
        FnMatrixWeighting(weight_mat, exponent=exponent)
    ]

    for spc, weight in zip(spaces, weightings):
        assert spc.weighting == weight
Beispiel #15
0
def test_constant_equiv():
    n = 10
    constant = 1.5

    w_const = FnConstWeighting(constant)
    w_const2 = FnConstWeighting(constant)

    const_sparse_mat = sp.sparse.dia_matrix(([constant] * n, [0]),
                                            shape=(n, n))
    const_dense_mat = constant * np.eye(n)
    w_matrix_sp = FnMatrixWeighting(const_sparse_mat)
    w_matrix_de = FnMatrixWeighting(const_dense_mat)

    # Equal -> True
    assert w_const.equiv(w_const)
    assert w_const.equiv(w_const2)
    # Equivalent matrix representation -> True
    assert w_const.equiv(w_matrix_sp)
    assert w_const.equiv(w_matrix_de)

    w_different_const = FnConstWeighting(2.5)
    assert not w_const.equiv(w_different_const)

    # Bogus input
    assert not w_const.equiv(True)
    assert not w_const.equiv(object)
    assert not w_const.equiv(None)