def test_almost_equal_incompatible(incompatible_vector_array_pair): v1, v2 = incompatible_vector_array_pair for ind1, ind2 in valid_inds_of_same_length(v1, v2): for n in ['sup', 'l1', 'l2']: c1, c2 = v1.copy(), v2.copy() with pytest.raises(Exception): almost_equal(c1[ind1], c2[ind2], norm=n)
def test_pairwise_apply2(operator_with_arrays): op, mu, U, V = operator_with_arrays for U_ind, V_ind in valid_inds_of_same_length(U, V): M = op.pairwise_apply2(V[V_ind], U[U_ind], mu=mu) assert M.shape == (V.len_ind(V_ind), ) M2 = V[V_ind].pairwise_dot(op.apply(U[U_ind], mu=mu)) assert np.allclose(M, M2)
def test_pairwise_apply2_with_product(operator_with_arrays_and_products): op, mu, U, V, sp, rp = operator_with_arrays_and_products for U_ind, V_ind in valid_inds_of_same_length(U, V): M = op.pairwise_apply2(V, U, U_ind=U_ind, V_ind=V_ind, mu=mu, product=rp) assert M.shape == (V.len_ind(V_ind),) M2 = V.pairwise_dot(rp.apply(op.apply(U, ind=U_ind, mu=mu)), ind=V_ind) assert np.allclose(M, M2)
def test_pairwise_apply2(operator_with_arrays): op, mu, U, V = operator_with_arrays for U_ind, V_ind in valid_inds_of_same_length(U, V): M = op.pairwise_apply2(V[V_ind], U[U_ind], mu=mu) assert M.shape == (V.len_ind(V_ind),) M2 = V[V_ind].pairwise_dot(op.apply(U[U_ind], mu=mu)) assert np.allclose(M, M2)
def test_almost_equal(compatible_vector_array_pair): v1, v2 = compatible_vector_array_pair try: dv1 = v1.to_numpy() dv2 = v2.to_numpy() except NotImplementedError: dv1 = dv2 = None for ind1, ind2 in valid_inds_of_same_length(v1, v2): for rtol, atol in ((1e-5, 1e-8), (1e-10, 1e-12), (0., 1e-8), (1e-5, 1e-8)): for n, o in [('sup', np.inf), ('l1', 1), ('l2', 2)]: try: r = almost_equal(v1[ind1], v2[ind2], norm=n) except NotImplementedError as e: if n == 'l1': pytest.xfail('l1_norm not implemented') else: raise e assert isinstance(r, np.ndarray) assert r.shape == (v1.len_ind(ind1), ) if dv1 is not None: if dv2.shape[1] == 0: continue assert np.all(r == ( np.linalg.norm(indexed(dv1, ind1) - indexed(dv2, ind2), ord=o, axis=1) <= atol + rtol * np.linalg.norm(indexed(dv2, ind2), ord=o, axis=1)))
def test_apply2_pairwise(operator_with_arrays): op, mu, U, V = operator_with_arrays for U_ind, V_ind in valid_inds_of_same_length(U, V): M = op.apply2(V, U, pairwise=True, U_ind=U_ind, V_ind=V_ind, mu=mu) assert M.shape == (V.len_ind(V_ind), ) M2 = V.dot(op.apply(U, ind=U_ind, mu=mu), pairwise=True, ind=V_ind) assert np.allclose(M, M2)
def test_pairwise_apply2_with_product(operator_with_arrays_and_products): op, mu, U, V, sp, rp = operator_with_arrays_and_products for U_ind, V_ind in valid_inds_of_same_length(U, V): M = op.pairwise_apply2(V, U, U_ind=U_ind, V_ind=V_ind, mu=mu, product=rp) assert M.shape == (V.len_ind(V_ind), ) M2 = V.pairwise_dot(rp.apply(op.apply(U, ind=U_ind, mu=mu)), ind=V_ind) assert np.allclose(M, M2)
def test_almost_equal(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): for rtol, atol in ((1e-5, 1e-8), (1e-10, 1e-12), (0., 1e-8), (1e-5, 1e-8)): for n, o in [('sup', np.inf), ('l1', 1), ('l2', 2)]: r = almost_equal(v1[ind1], v2[ind2], norm=n) assert isinstance(r, np.ndarray) assert r.shape == (v1.len_ind(ind1),) if hasattr(v1, 'data'): if dv2.shape[1] == 0: continue assert np.all(r == (np.linalg.norm(indexed(dv1, ind1) - indexed(dv2, ind2), ord=o, axis=1) <= atol + rtol * np.linalg.norm(indexed(dv2, ind2), ord=o, axis=1)))
def test_almost_equal(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): for rtol, atol in ((1e-5, 1e-8), (1e-10, 1e-12), (0., 1e-8), (1e-5, 1e-8)): for n, o in [('sup', np.inf), ('l1', 1), ('l2', 2)]: r = almost_equal(v1, v2, U_ind=ind1, V_ind=ind2, norm=n) assert isinstance(r, np.ndarray) assert r.shape == (v1.len_ind(ind1),) if hasattr(v1, 'data'): if dv2.shape[1] == 0: continue assert np.all(r == (np.linalg.norm(indexed(dv1, ind1) - indexed(dv2, ind2), ord=o, axis=1) <= atol + rtol * np.linalg.norm(indexed(dv2, ind2), ord=o, axis=1)))
def test_almost_equal(compatible_vector_array_pair): v1, v2 = compatible_vector_array_pair try: dv1 = v1.to_numpy() dv2 = v2.to_numpy() except NotImplementedError: dv1 = dv2 = None for ind1, ind2 in valid_inds_of_same_length(v1, v2): for rtol, atol in ((1e-5, 1e-8), (1e-10, 1e-12), (0., 1e-8), (1e-5, 1e-8)): for n, o in [('sup', np.inf), ('l1', 1), ('l2', 2)]: r = almost_equal(v1[ind1], v2[ind2], norm=n) assert isinstance(r, np.ndarray) assert r.shape == (v1.len_ind(ind1),) if dv1 is not None: if dv2.shape[1] == 0: continue assert np.all(r == (np.linalg.norm(indexed(dv1, ind1) - indexed(dv2, ind2), ord=o, axis=1) <= atol + rtol * np.linalg.norm(indexed(dv2, ind2), ord=o, axis=1)))
def test_almost_equal_product(operator_with_arrays_and_products): _, _, v1, _, product, _ = operator_with_arrays_and_products if len(v1) < 2: return v2 = v1.empty() v2.append(v1[:len(v1) // 2]) for ind1, ind2 in valid_inds_of_same_length(v1, v2): for rtol, atol in ((1e-5, 1e-8), (1e-10, 1e-12), (0., 1e-8), (1e-5, 1e-8)): norm = induced_norm(product) r = almost_equal(v1[ind1], v2[ind2], norm=norm) assert isinstance(r, np.ndarray) assert r.shape == (v1.len_ind(ind1),) assert np.all(r == (norm(v1[ind1] - v2[ind2]) <= atol + rtol * norm(v2[ind2]))) r = almost_equal(v1[ind1], v2[ind2], product=product) assert isinstance(r, np.ndarray) assert r.shape == (v1.len_ind(ind1),) assert np.all(r == (norm(v1[ind1] - v2[ind2]) <= atol + rtol * norm(v2[ind2])))
def test_almost_equal_product(operator_with_arrays_and_products): _, _, v1, _, product, _ = operator_with_arrays_and_products if len(v1) < 2: return v2 = v1.empty() v2.append(v1, o_ind=list(range(len(v1) // 2))) for ind1, ind2 in valid_inds_of_same_length(v1, v2): for rtol, atol in ((1e-5, 1e-8), (1e-10, 1e-12), (0., 1e-8), (1e-5, 1e-8)): norm = induced_norm(product) r = almost_equal(v1, v2, U_ind=ind1, V_ind=ind2, norm=norm) assert isinstance(r, np.ndarray) assert r.shape == (v1.len_ind(ind1),) assert np.all(r == (norm(v1.copy(ind1) - v2.copy(ind2)) <= atol + rtol * norm(v2.copy(ind2)))) r = almost_equal(v1, v2, U_ind=ind1, V_ind=ind2, product=product) assert isinstance(r, np.ndarray) assert r.shape == (v1.len_ind(ind1),) assert np.all(r == (norm(v1.copy(ind1) - v2.copy(ind2)) <= atol + rtol * norm(v2.copy(ind2))))