def test_apply2(operator_with_arrays): op, mu, U, V = operator_with_arrays for U_ind in valid_inds(U): for V_ind in valid_inds(V): M = op.apply2(V[V_ind], U[U_ind], mu=mu) assert M.shape == (V.len_ind(V_ind), U.len_ind(U_ind)) M2 = V[V_ind].inner(op.apply(U[U_ind], mu=mu)) assert np.allclose(M, M2)
def test_axpy_one_x(vector_arrays, random): v1, v2 = vector_arrays for ind1, ind2 in product(pyst.valid_inds(v1, random_module=False), pyst.valid_inds(v2, 1, random_module=False)): assert v1.check_ind(ind1) assert v2.check_ind(ind2) if v1.len_ind(ind1) != v1.len_ind_unique(ind1): with pytest.raises(Exception): c1, c2 = v1.copy(), v2.copy() c1[ind1].axpy(0., c2[ind2]) continue ind1_complement = ind_complement(v1, ind1) c1, c2 = v1.copy(), v2.copy() gc = c1[ind1] gv = c2[ind2] # TODO this was somehow hardcoded in the old fixture, makes the test extremely slow assume(len(gc) == 0 or len(gv) > 0) gc.axpy(0., gv) assert len(c1) == len(v1) assert np.all(almost_equal(c1, v1)) assert np.all(almost_equal(c2, v2)) for a in (1., 1.4, np.random.random(v1.len_ind(ind1))): c1, c2 = v1.copy(), v2.copy() c1[ind1].axpy(a, c2[ind2]) assert len(c1) == len(v1) assert np.all( almost_equal(c1[ind1_complement], v1[ind1_complement])) assert np.all(almost_equal(c2, v2)) # for the openstack CI machines this could be 1 + 1e-10 rtol_factor = 1. + 147e-9 assert np.all(c1[ind1].sup_norm() <= v1[ind1].sup_norm() + abs(a) * v2[ind2].sup_norm() * rtol_factor) assert np.all(c1[ind1].norm() <= (v1[ind1].norm() + abs(a) * v2[ind2].norm()) * (1. + 1e-10)) try: x = v1.to_numpy(True).astype( complex) # ensure that inplace addition works if isinstance(ind1, Number): x[[ind1]] += indexed(v2.to_numpy(), ind2) * a else: if isinstance(a, np.ndarray): aa = a[:, np.newaxis] else: aa = a x[ind1] += indexed(v2.to_numpy(), ind2) * aa assert np.allclose(c1.to_numpy(), x) except NotImplementedError: pass c1[ind1].axpy(-a, c2[ind2]) assert len(c1) == len(v1) assert np.all(almost_equal(c1, v1))
def test_append(vector_arrays): v1, v2 = vector_arrays len_v1, len_v2 = len(v1), len(v2) for ind in pyst.valid_inds(v2, random_module=False): c1, c2 = v1.copy(), v2.copy() c1.append(c2[ind]) len_ind = v2.len_ind(ind) ind_complement_ = ind_complement(v2, ind) assert len(c1) == len_v1 + len_ind assert np.all(almost_equal(c1[len_v1:len(c1)], c2[ind])) try: assert np.allclose( c1.to_numpy(), np.vstack((v1.to_numpy(), indexed(v2.to_numpy(), ind)))) except NotImplementedError: pass c1.append(c2[ind], remove_from_other=True) assert len(c2) == len(ind_complement_) assert c2.space == c1.space assert len(c1) == len_v1 + 2 * len_ind assert np.all( almost_equal(c1[len_v1:len_v1 + len_ind], c1[len_v1 + len_ind:len(c1)])) assert np.all(almost_equal(c2, v2[ind_complement_])) try: assert np.allclose(c2.to_numpy(), indexed(v2.to_numpy(), ind_complement_)) except NotImplementedError: pass
def test_getitem_repeated(vectors_and_indices): v, ind = vectors_and_indices v_ind = v[ind] v_ind_copy = v_ind.copy() assert not v_ind_copy.is_view for ind_ind in pyst.valid_inds(v_ind, random_module=False): v_ind_ind = v_ind[ind_ind] assert np.all(almost_equal(v_ind_ind, v_ind_copy[ind_ind]))
def test_apply(operator_with_arrays): op, mu, U, _ = operator_with_arrays V = op.apply(U, mu=mu) assert V in op.range assert len(V) == len(U) for ind in valid_inds(U): Vind = op.apply(U[ind], mu=mu) assert np.all(almost_equal(Vind, V[ind])) assert np.all(almost_equal(Vind, op.apply(U[ind], mu=mu)))
def test_apply_inverse(operator_with_arrays): op, mu, _, V = operator_with_arrays for ind in valid_inds(V): try: U = op.apply_inverse(V[ind], mu=mu) except InversionError: return assert U in op.source assert len(U) == V.len_ind(ind) VV = op.apply(U, mu=mu) assert np.all(almost_equal(VV, V[ind], atol=1e-10, rtol=1e-3))
def test_almost_equal_self_product(operator_with_arrays_and_products): _, _, v, _, product, _ = operator_with_arrays_and_products norm = induced_norm(product) for ind in valid_inds(v): for rtol, atol in ((1e-5, 1e-8), (1e-10, 1e-12), (0., 1e-8), (1e-5, 1e-8), (1e-12, 0.)): r = almost_equal(v[ind], v[ind], norm=norm) assert isinstance(r, np.ndarray) assert r.shape == (v.len_ind(ind),) assert np.all(r) if v.len_ind(ind) == 0 or np.max(v[ind].sup_norm() == 0): continue r = almost_equal(v[ind], v[ind], product=product) assert isinstance(r, np.ndarray) assert r.shape == (v.len_ind(ind),) assert np.all(r) if v.len_ind(ind) == 0 or np.max(v[ind].sup_norm() == 0): continue c = v.copy() c.scal(atol * (1 - 1e-10) / (np.max(norm(v[ind])))) assert np.all(almost_equal(c[ind], c.zeros(v.len_ind(ind)), atol=atol, rtol=rtol, norm=norm)) assert np.all(almost_equal(c[ind], c.zeros(v.len_ind(ind)), atol=atol, rtol=rtol, product=product)) if atol > 0: c = v.copy() c.scal(2. * atol / (np.max(norm(v[ind])))) assert not np.all(almost_equal(c[ind], c.zeros(v.len_ind(ind)), atol=atol, rtol=rtol, norm=norm)) assert not np.all(almost_equal(c[ind], c.zeros(v.len_ind(ind)), atol=atol, rtol=rtol, product=product)) c = v.copy() c.scal(1. + rtol * 0.9) assert np.all(almost_equal(c[ind], v[ind], atol=atol, rtol=rtol, norm=norm)) assert np.all(almost_equal(c[ind], v[ind], atol=atol, rtol=rtol, product=product)) if rtol > 0: c = v.copy() c.scal(2. + rtol * 1.1) assert not np.all(almost_equal(c[ind], v[ind], atol=atol, rtol=rtol, norm=norm)) assert not np.all(almost_equal(c[ind], v[ind], atol=atol, rtol=rtol, product=product)) c = v.copy() c.scal(1. + atol * 0.9 / np.max(np.max(norm(v[ind])))) assert np.all(almost_equal(c[ind], v[ind], atol=atol, rtol=rtol, norm=norm)) assert np.all(almost_equal(c[ind], v[ind], atol=atol, rtol=rtol, product=product)) if atol > 0 or rtol > 0: c = v.copy() c.scal(1 + rtol * 1.1 + atol * 1.1 / np.max(np.max(norm(v[ind])))) assert not np.all(almost_equal(c[ind], v[ind], atol=atol, rtol=rtol, norm=norm)) assert not np.all(almost_equal(c[ind], v[ind], atol=atol, rtol=rtol, product=product))
def test_apply_adjoint(operator_with_arrays): op, mu, _, V = operator_with_arrays if not op.linear: return try: U = op.apply_adjoint(V, mu=mu) except (NotImplementedError, LinAlgError): return assert U in op.source assert len(V) == len(U) for ind in list(valid_inds(V, 3)) + [[]]: Uind = op.apply_adjoint(V[ind], mu=mu) assert np.all(almost_equal(Uind, U[ind])) assert np.all(almost_equal(Uind, op.apply_adjoint(V[ind], mu=mu)))
def test_apply_inverse_adjoint(operator_with_arrays): op, mu, U, _ = operator_with_arrays if not op.linear: return for ind in valid_inds(U): if len(U[ind]) == 0: continue try: V = op.apply_inverse_adjoint(U[ind], mu=mu) except (InversionError, LinAlgError): return assert V in op.range assert len(V) == U.len_ind(ind) UU = op.apply_adjoint(V, mu=mu) assert np.all(almost_equal(UU, U[ind], atol=1e-10, rtol=1e-3))