def test_vector_vector(): rn = Rn(5) weight_vec = _pos_array(rn) weight_elem = rn.element(weight_vec) weighting_vec = FnVectorWeighting(weight_vec) weighting_elem = FnVectorWeighting(weight_elem) assert isinstance(weighting_vec.vector, np.ndarray) assert isinstance(weighting_elem.vector, FnVector)
def test_vector_isvalid(): rn = Rn(5) weight_vec = _pos_array(rn) weighting_vec = FnVectorWeighting(weight_vec) assert weighting_vec.vector_is_valid() # Invalid weight_vec[0] = 0 weighting_vec = FnVectorWeighting(weight_vec) assert not weighting_vec.vector_is_valid()
def test_vector_norm(fn, exponent): xarr, x = example_vectors(fn) weight_vec = _pos_array(fn) weighting_vec = FnVectorWeighting(weight_vec, exponent=exponent) if exponent == float('inf'): true_norm = np.linalg.norm(weight_vec * xarr, ord=float('inf')) else: true_norm = np.linalg.norm(weight_vec**(1 / exponent) * xarr, ord=exponent) assert almost_equal(weighting_vec.norm(x), true_norm) # With free function pnorm_vec = weighted_norm(weight_vec, exponent=exponent) assert almost_equal(pnorm_vec(x), true_norm)
def test_vector_norm(fn, exponent): xarr, x = _vectors(fn) weight_vec = _pos_array(fn) weighting_vec = FnVectorWeighting(weight_vec, exponent=exponent) if exponent == float('inf'): true_norm = np.linalg.norm(weight_vec * xarr, ord=float('inf')) else: true_norm = np.linalg.norm(weight_vec ** (1 / exponent) * xarr, ord=exponent) assert almost_equal(weighting_vec.norm(x), true_norm) # With free function pnorm_vec = weighted_norm(weight_vec, exponent=exponent) assert almost_equal(pnorm_vec(x), true_norm)
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)
def test_vector_inner(fn): [xarr, yarr], [x, y] = example_vectors(fn, 2) weight_vec = _pos_array(fn) weighting_vec = FnVectorWeighting(weight_vec) true_inner = np.vdot(yarr, xarr * weight_vec) assert almost_equal(weighting_vec.inner(x, y), true_inner) # With free function inner_vec = weighted_inner(weight_vec) assert almost_equal(inner_vec(x, y), true_inner) # Exponent != 2 -> no inner product, should raise with pytest.raises(NotImplementedError): FnVectorWeighting(weight_vec, exponent=1.0).inner(x, y)
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)
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)
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)
def test_vector_inner(fn): xarr, yarr, x, y = _vectors(fn, 2) weight_vec = _pos_array(fn) weighting_vec = FnVectorWeighting(weight_vec) true_inner = np.vdot(yarr, xarr * weight_vec) assert almost_equal(weighting_vec.inner(x, y), true_inner) # With free function inner_vec = weighted_inner(weight_vec) assert almost_equal(inner_vec(x, y), true_inner) # Exponent != 2 -> no inner product, should raise with pytest.raises(NotImplementedError): FnVectorWeighting(weight_vec, exponent=1.0).inner(x, y)
def test_matrix_equiv(): fn = Rn(5) sparse_mat = _sparse_matrix(fn) sparse_mat_as_dense = sparse_mat.todense() dense_mat = _dense_matrix(fn) different_dense_mat = dense_mat.copy() different_dense_mat[0, 0] = -10 w_sparse = FnMatrixWeighting(sparse_mat) w_sparse2 = FnMatrixWeighting(sparse_mat) w_sparse_as_dense = FnMatrixWeighting(sparse_mat_as_dense) w_dense = FnMatrixWeighting(dense_mat) w_dense_copy = FnMatrixWeighting(dense_mat.copy()) w_different_dense = FnMatrixWeighting(different_dense_mat) # Equal -> True assert w_sparse.equiv(w_sparse) assert w_sparse.equiv(w_sparse2) # Equivalent matrices -> True assert w_sparse.equiv(w_sparse_as_dense) assert w_dense.equiv(w_dense_copy) # Different matrices -> False assert not w_dense.equiv(w_different_dense) # Test shortcuts sparse_eye = sp.sparse.eye(5) w_eye = FnMatrixWeighting(sparse_eye) w_dense_eye = FnMatrixWeighting(sparse_eye.todense()) w_eye_vec = FnVectorWeighting(np.ones(5)) w_eye_wrong_exp = FnMatrixWeighting(sparse_eye, exponent=1) sparse_smaller_eye = sp.sparse.eye(4) w_smaller_eye = FnMatrixWeighting(sparse_smaller_eye) sparse_shifted_eye = sp.sparse.eye(5, k=1) w_shifted_eye = FnMatrixWeighting(sparse_shifted_eye) sparse_almost_eye = sp.sparse.dia_matrix((np.ones(4), [0]), (5, 5)) w_almost_eye = FnMatrixWeighting(sparse_almost_eye) assert w_eye.equiv(w_dense_eye) assert w_dense_eye.equiv(w_eye) assert w_eye.equiv(w_eye_vec) assert not w_eye.equiv(w_eye_wrong_exp) assert not w_eye.equiv(w_smaller_eye) assert not w_eye.equiv(w_shifted_eye) assert not w_smaller_eye.equiv(w_shifted_eye) assert not w_eye.equiv(w_almost_eye) # Bogus input assert not w_eye.equiv(True) assert not w_eye.equiv(object) assert not w_eye.equiv(None)
def test_vector_is_valid(): rn = Rn(5) weight_vec = _pos_array(rn) weighting_vec = FnVectorWeighting(weight_vec) assert weighting_vec.is_valid() # Invalid weight_vec[0] = 0 weighting_vec = FnVectorWeighting(weight_vec) assert not weighting_vec.is_valid()
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
def test_vector_equals(): rn = Rn(5) weight_vec = _pos_array(rn) weight_elem = rn.element(weight_vec) weighting_vec = FnVectorWeighting(weight_vec) weighting_vec2 = FnVectorWeighting(weight_vec) weighting_elem = FnVectorWeighting(weight_elem) weighting_elem2 = FnVectorWeighting(weight_elem) weighting_other_vec = FnVectorWeighting(weight_vec - 1) weighting_other_exp = FnVectorWeighting(weight_vec - 1, exponent=1) assert weighting_vec == weighting_vec2 assert weighting_vec != weighting_elem assert weighting_elem == weighting_elem2 assert weighting_vec != weighting_other_vec assert weighting_vec != weighting_other_exp
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)
def test_vector_init(exponent): rn = Rn(5) weight_vec = _pos_array(rn) FnVectorWeighting(weight_vec, exponent=exponent) FnVectorWeighting(rn.element(weight_vec), exponent=exponent)