Skip to content

Commit

Permalink
[tests] fix VectorArray tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sdrave committed Nov 30, 2016
1 parent aadb662 commit a9c4127
Showing 1 changed file with 31 additions and 76 deletions.
107 changes: 31 additions & 76 deletions src/pymortests/vectorarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,7 @@ def test_copy(vector_array):
assert c.subtype == v.subtype
assert np.all(almost_equal(c, v, V_ind=ind))
if hasattr(v, 'data'):
dv = v.data
dc = c.data
assert np.allclose(dc, indexed(dv, ind))
assert np.allclose(c.data, indexed(v.data, ind))


def test_copy_repeated_index(vector_array):
Expand All @@ -196,23 +194,16 @@ def test_copy_repeated_index(vector_array):
assert almost_equal(c, v, U_ind=0, V_ind=ind[0])
assert almost_equal(c, v, U_ind=1, V_ind=ind[0])
if hasattr(v, 'data'):
dv = indexed(v.data, ind)
dc = c.data
assert dv.shape == dc.shape
assert indexed(v.data, ind).shape == c.data.shape
c.scal(2., ind=0)
assert almost_equal(c, v, U_ind=1, V_ind=ind[0])
assert c.l2_norm(ind=0) == 2 * v.l2_norm(ind=ind[0])
if hasattr(v, 'data'):
dv = indexed(v.data, ind)
dc = c.data
assert dv.shape == dc.shape
assert indexed(v.data, ind).shape == c.data.shape


def test_append(compatible_vector_array_pair):
v1, v2 = compatible_vector_array_pair
if hasattr(v1, 'data'):
dv1 = v1.data
dv2 = v2.data
len_v1, len_v2 = len(v1), len(v2)
for ind in valid_inds(v2):
c1, c2 = v1.copy(), v2.copy()
Expand All @@ -222,7 +213,7 @@ def test_append(compatible_vector_array_pair):
assert len(c1) == len_v1 + len_ind
assert np.all(almost_equal(c1, c2, U_ind=range(len_v1, len(c1)), V_ind=ind))
if hasattr(v1, 'data'):
assert np.allclose(c1.data, np.vstack((dv1, indexed(dv2, ind))))
assert np.allclose(c1.data, np.vstack((v1.data, indexed(v2.data, ind))))
c1.append(c2, o_ind=ind, remove_from_other=True)
assert len(c2) == len(ind_complement_)
assert c2.dim == c1.dim
Expand All @@ -231,7 +222,7 @@ def test_append(compatible_vector_array_pair):
assert np.all(almost_equal(c1, c1, U_ind=range(len_v1, len_v1 + len_ind), V_ind=range(len_v1 + len_ind, len(c1))))
assert np.all(almost_equal(c2, v2, V_ind=ind_complement_))
if hasattr(v1, 'data'):
assert np.allclose(c2.data, indexed(dv2, ind_complement_))
assert np.allclose(c2.data, indexed(v2.data, ind_complement_))


def test_append_self(vector_array):
Expand All @@ -250,8 +241,6 @@ def test_append_self(vector_array):

def test_remove(vector_array):
v = vector_array
if hasattr(v, 'data'):
dv = v.data
for ind in valid_inds(v):
ind_complement_ = ind_complement(v, ind)
c = v.copy()
Expand All @@ -261,17 +250,14 @@ def test_remove(vector_array):
assert len(c) == len(ind_complement_)
assert np.all(almost_equal(v, c, U_ind=ind_complement_))
if hasattr(v, 'data'):
assert np.allclose(c.data, indexed(dv, ind_complement_))
assert np.allclose(c.data, indexed(v.data, ind_complement_))
c.remove()
assert len(c) == 0


def test_replace(compatible_vector_array_pair):
v1, v2 = compatible_vector_array_pair
len_v1, len_v2 = len(v1), len(v2)
if hasattr(v1, 'data'):
dv1 = v1.data
dv2 = v2.data
for ind1, ind2 in valid_inds_of_same_length(v1, v2):
if v1.len_ind(ind1) != v1.len_ind_unique(ind1):
with pytest.raises(Exception):
Expand All @@ -289,11 +275,11 @@ def test_replace(compatible_vector_array_pair):
assert np.all(almost_equal(c1, v2, U_ind=ind1, V_ind=ind2))
assert np.all(almost_equal(c2, v2))
if hasattr(v1, 'data'):
x = dv1.copy()
x = v1.data.copy()
if NUMPY_INDEX_QUIRK and len(x) == 0 and hasattr(ind1, '__len__') and len(ind1) == 0:
pass
else:
x[ind1] = indexed(dv2, ind2)
x[ind1] = indexed(v2.data, ind2)
assert np.allclose(c1.data, x)

c1, c2 = v1.copy(), v2.copy()
Expand All @@ -306,19 +292,17 @@ def test_replace(compatible_vector_array_pair):
assert len(c2) == len(ind2_complement)
assert np.all(almost_equal(c2, v2, V_ind=ind2_complement))
if hasattr(v1, 'data'):
x = dv1.copy()
x = v1.data.copy()
if NUMPY_INDEX_QUIRK and len(x) == 0 and hasattr(ind1, '__len__') and len(ind1) == 0:
pass
else:
x[ind1] = indexed(dv2, ind2)
x[ind1] = indexed(v2.data, ind2)
assert np.allclose(c1.data, x)
assert np.allclose(c2.data, indexed(dv2, ind2_complement))
assert np.allclose(c2.data, indexed(v2.data, ind2_complement))


def test_replace_self(vector_array):
v = vector_array
if hasattr(v, 'data'):
dv = v.data
for ind1, ind2 in valid_inds_of_same_length(v, v):
if v.len_ind(ind1) != v.len_ind_unique(ind1):
c = v.copy()
Expand All @@ -340,18 +324,16 @@ def test_replace_self(vector_array):
assert c.subtype == v.subtype
assert np.all(almost_equal(c, v, U_ind=ind1, V_ind=ind2))
if hasattr(v, 'data'):
x = dv.copy()
x = v.data.copy()
if NUMPY_INDEX_QUIRK and len(x) == 0 and hasattr(ind1, '__len__') and len(ind1) == 0:
pass
else:
x[ind1] = indexed(dv, ind2)
x[ind1] = indexed(v.data, ind2)
assert np.allclose(c.data, x)


def test_scal(vector_array):
v = vector_array
if hasattr(v, 'data'):
dv = v.data
for ind in valid_inds(v):
if v.len_ind(ind) != v.len_ind_unique(ind):
with pytest.raises(Exception):
Expand All @@ -376,7 +358,7 @@ def test_scal(vector_array):
assert np.allclose(c.sup_norm(ind), v.sup_norm(ind) * abs(x))
assert np.allclose(c.l2_norm(ind), v.l2_norm(ind) * abs(x))
if hasattr(v, 'data'):
y = dv.copy()
y = v.data.copy()
if NUMPY_INDEX_QUIRK and len(y) == 0:
pass
else:
Expand All @@ -388,10 +370,6 @@ def test_scal(vector_array):

def test_axpy(compatible_vector_array_pair):
v1, v2 = compatible_vector_array_pair
if hasattr(v1, 'data'):
dv1 = v1.data
dv2 = v2.data

for ind1, ind2 in valid_inds_of_same_length(v1, v2):
if v1.len_ind(ind1) != v1.len_ind_unique(ind1):
with pytest.raises(Exception):
Expand All @@ -417,9 +395,9 @@ def test_axpy(compatible_vector_array_pair):
assert np.all(c1.l1_norm(ind1) <= (v1.l1_norm(ind1) + abs(a) * v2.l1_norm(ind2)) * (1. + 1e-10))
assert np.all(c1.l2_norm(ind1) <= (v1.l2_norm(ind1) + abs(a) * v2.l2_norm(ind2)) * (1. + 1e-10))
if hasattr(v1, 'data'):
x = dv1.copy()
x = v1.data.copy()
if isinstance(ind1, Number):
x[[ind1]] += indexed(dv2, ind2) * a
x[[ind1]] += indexed(v2.data, ind2) * a
else:
if NUMPY_INDEX_QUIRK and len(x) == 0:
pass
Expand All @@ -428,7 +406,7 @@ def test_axpy(compatible_vector_array_pair):
aa = a[:, np.newaxis]
else:
aa = a
x[ind1] += indexed(dv2, ind2) * aa
x[ind1] += indexed(v2.data, ind2) * aa
assert np.allclose(c1.data, x)
c1.axpy(-a, c2, ind=ind1, x_ind=ind2)
assert len(c1) == len(v1)
Expand All @@ -437,10 +415,6 @@ def test_axpy(compatible_vector_array_pair):

def test_axpy_one_x(compatible_vector_array_pair):
v1, v2 = compatible_vector_array_pair
if hasattr(v1, 'data'):
dv1 = v1.data
dv2 = v2.data

for ind1, ind2 in product(valid_inds(v1), valid_inds(v2, 1)):
if v1.len_ind(ind1) != v1.len_ind_unique(ind1):
with pytest.raises(Exception):
Expand All @@ -466,9 +440,9 @@ def test_axpy_one_x(compatible_vector_array_pair):
assert np.all(c1.l1_norm(ind1) <= (v1.l1_norm(ind1) + abs(a) * v2.l1_norm(ind2)) * (1. + 1e-10))
assert np.all(c1.l2_norm(ind1) <= (v1.l2_norm(ind1) + abs(a) * v2.l2_norm(ind2)) * (1. + 1e-10))
if hasattr(v1, 'data'):
x = dv1.copy()
x = v1.data.copy()
if isinstance(ind1, Number):
x[[ind1]] += indexed(dv2, ind2) * a
x[[ind1]] += indexed(v2.data, ind2) * a
else:
if NUMPY_INDEX_QUIRK and len(x) == 0:
pass
Expand All @@ -477,7 +451,7 @@ def test_axpy_one_x(compatible_vector_array_pair):
aa = a[:, np.newaxis]
else:
aa = a
x[ind1] += indexed(dv2, ind2) * aa
x[ind1] += indexed(v2.data, ind2) * aa
assert np.allclose(c1.data, x)
c1.axpy(-a, c2, ind=ind1, x_ind=ind2)
assert len(c1) == len(v1)
Expand All @@ -486,9 +460,6 @@ def test_axpy_one_x(compatible_vector_array_pair):

def test_axpy_self(vector_array):
v = vector_array
if hasattr(v, 'data'):
dv = v.data

for ind1, ind2 in valid_inds_of_same_length(v, v):
if v.len_ind(ind1) != v.len_ind_unique(ind1):
with pytest.raises(Exception):
Expand All @@ -512,9 +483,9 @@ def test_axpy_self(vector_array):
assert np.all(c.sup_norm(ind1) <= v.sup_norm(ind1) + abs(a) * v.sup_norm(ind2) * (1. + 1e-10))
assert np.all(c.l1_norm(ind1) <= (v.l1_norm(ind1) + abs(a) * v.l1_norm(ind2)) * (1. + 1e-10))
if hasattr(v, 'data'):
x = dv.copy()
x = v.data.copy()
if isinstance(ind1, Number):
x[[ind1]] += indexed(dv, ind2) * a
x[[ind1]] += indexed(v.data, ind2) * a
else:
if NUMPY_INDEX_QUIRK and len(x) == 0:
pass
Expand All @@ -523,7 +494,7 @@ def test_axpy_self(vector_array):
aa = a[:, np.newaxis]
else:
aa = a
x[ind1] += indexed(dv, ind2) * aa
x[ind1] += indexed(v.data, ind2) * aa
assert np.allclose(c.data, x)
c.axpy(-a, v, ind=ind1, x_ind=ind2)
assert len(c) == len(v)
Expand All @@ -543,8 +514,6 @@ def test_axpy_self(vector_array):

def test_pairwise_dot(compatible_vector_array_pair):
v1, v2 = compatible_vector_array_pair
if hasattr(v1, 'data'):
dv1, dv2 = v1.data, v2.data
for ind1, ind2 in valid_inds_of_same_length(v1, v2):
r = v1.pairwise_dot(v2, ind=ind1, o_ind=ind2)
assert isinstance(r, np.ndarray)
Expand All @@ -553,13 +522,11 @@ def test_pairwise_dot(compatible_vector_array_pair):
assert np.all(r == r2)
assert np.all(r <= v1.l2_norm(ind1) * v2.l2_norm(ind2) * (1. + 1e-10))
if hasattr(v1, 'data'):
assert np.allclose(r, np.sum(indexed(dv1, ind1) * indexed(dv2, ind2), axis=1))
assert np.allclose(r, np.sum(indexed(v1.data, ind1) * indexed(v2.data, ind2), axis=1))


def test_pairwise_dot_self(vector_array):
v = vector_array
if hasattr(v, 'data'):
dv = v.data
for ind1, ind2 in valid_inds_of_same_length(v, v):
r = v.pairwise_dot(v, ind=ind1, o_ind=ind2)
assert isinstance(r, np.ndarray)
Expand All @@ -568,16 +535,14 @@ def test_pairwise_dot_self(vector_array):
assert np.all(r == r2)
assert np.all(r <= v.l2_norm(ind1) * v.l2_norm(ind2) * (1. + 1e-10))
if hasattr(v, 'data'):
assert np.allclose(r, np.sum(indexed(dv, ind1) * indexed(dv, ind2), axis=1))
assert np.allclose(r, np.sum(indexed(v.data, ind1) * indexed(v.data, ind2), axis=1))
for ind in valid_inds(v):
r = v.pairwise_dot(v, ind=ind, o_ind=ind)
assert np.allclose(r, v.l2_norm(ind) ** 2)


def test_dot(compatible_vector_array_pair):
v1, v2 = compatible_vector_array_pair
if hasattr(v1, 'data'):
dv1, dv2 = v1.data, v2.data
for ind1, ind2 in chain(valid_inds_of_different_length(v1, v2), valid_inds_of_same_length(v1, v2)):
r = v1.dot(v2, ind=ind1, o_ind=ind2)
assert isinstance(r, np.ndarray)
Expand All @@ -586,13 +551,11 @@ def test_dot(compatible_vector_array_pair):
assert np.all(r == r2.T)
assert np.all(r <= v1.l2_norm(ind1)[:, np.newaxis] * v2.l2_norm(ind2)[np.newaxis, :] * (1. + 1e-10))
if hasattr(v1, 'data'):
assert np.allclose(r, indexed(dv1, ind1).dot(indexed(dv2, ind2).T))
assert np.allclose(r, indexed(v1.data, ind1).dot(indexed(v2.data, ind2).T))


def test_dot_self(vector_array):
v = vector_array
if hasattr(v, 'data'):
dv = v.data
for ind1, ind2 in chain(valid_inds_of_different_length(v, v), valid_inds_of_same_length(v, v)):
r = v.dot(v, ind=ind1, o_ind=ind2)
assert isinstance(r, np.ndarray)
Expand All @@ -601,7 +564,7 @@ def test_dot_self(vector_array):
assert np.all(r == r2.T)
assert np.all(r <= v.l2_norm(ind1)[:, np.newaxis] * v.l2_norm(ind2)[np.newaxis, :] * (1. + 1e-10))
if hasattr(v, 'data'):
assert np.allclose(r, indexed(dv, ind1).dot(indexed(dv, ind2).T))
assert np.allclose(r, indexed(v.data, ind1).dot(indexed(v.data, ind2).T))
for ind in valid_inds(v):
r = v.dot(v, ind=ind, o_ind=ind)
assert np.all(r == r.T)
Expand Down Expand Up @@ -660,8 +623,6 @@ def test_lincomb_wrong_coefficients(vector_array):

def test_l1_norm(vector_array):
v = vector_array
if hasattr(v, 'data'):
dv = v.data
for ind in valid_inds(v):
c = v.copy()
norm = c.l1_norm(ind)
Expand All @@ -671,7 +632,7 @@ def test_l1_norm(vector_array):
if v.dim == 0:
assert np.all(norm == 0)
if hasattr(v, 'data'):
assert np.allclose(norm, np.sum(np.abs(indexed(dv, ind)), axis=1))
assert np.allclose(norm, np.sum(np.abs(indexed(v.data, ind)), axis=1))
c.scal(4.)
assert np.allclose(c.l1_norm(ind), norm * 4)
c.scal(-4.)
Expand All @@ -682,8 +643,6 @@ def test_l1_norm(vector_array):

def test_l2_norm(vector_array):
v = vector_array
if hasattr(v, 'data'):
dv = v.data
for ind in valid_inds(v):
c = v.copy()
norm = c.l2_norm(ind)
Expand All @@ -693,7 +652,7 @@ def test_l2_norm(vector_array):
if v.dim == 0:
assert np.all(norm == 0)
if hasattr(v, 'data'):
assert np.allclose(norm, np.sqrt(np.sum(np.power(indexed(dv, ind), 2), axis=1)))
assert np.allclose(norm, np.sqrt(np.sum(np.power(indexed(v.data, ind), 2), axis=1)))
c.scal(4.)
assert np.allclose(c.l2_norm(ind), norm * 4)
c.scal(-4.)
Expand All @@ -704,8 +663,6 @@ def test_l2_norm(vector_array):

def test_sup_norm(vector_array):
v = vector_array
if hasattr(v, 'data'):
dv = v.data
for ind in valid_inds(v):
c = v.copy()
norm = c.sup_norm(ind)
Expand All @@ -715,7 +672,7 @@ def test_sup_norm(vector_array):
if v.dim == 0:
assert np.all(norm == 0)
if hasattr(v, 'data') and v.dim > 0:
assert np.allclose(norm, np.max(np.abs(indexed(dv, ind)), axis=1))
assert np.allclose(norm, np.max(np.abs(indexed(v.data, ind)), axis=1))
c.scal(4.)
assert np.allclose(c.sup_norm(ind), norm * 4)
c.scal(-4.)
Expand All @@ -727,8 +684,6 @@ def test_sup_norm(vector_array):
def test_components(vector_array):
v = vector_array
np.random.seed(len(v) + 24 + v.dim)
if hasattr(v, 'data'):
dv = v.data
for ind in valid_inds(v):
c = v.copy()
comp = c.components(np.array([], dtype=np.int), ind=ind)
Expand Down Expand Up @@ -757,7 +712,7 @@ def test_components(vector_array):
comp2 = c.components(np.hstack((c_ind, c_ind)), ind=ind)
assert np.all(comp2 == np.hstack((comp, comp)))
if hasattr(v, 'data'):
assert np.all(comp == indexed(dv, ind)[:, c_ind])
assert np.all(comp == indexed(v.data, ind)[:, c_ind])


def test_components_wrong_component_indices(vector_array):
Expand Down

0 comments on commit a9c4127

Please sign in to comment.