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].dot(op.apply(U[U_ind], mu=mu)) assert np.allclose(M, M2)
def test_apply2_with_product(operator_with_arrays_and_products): op, mu, U, V, sp, rp = operator_with_arrays_and_products for U_ind in valid_inds(U): for V_ind in valid_inds(V): M = op.apply2(V, U, U_ind=U_ind, V_ind=V_ind, mu=mu, product=rp) assert M.shape == (V.len_ind(V_ind), U.len_ind(U_ind)) M2 = V.dot(rp.apply(op.apply(U, ind=U_ind, mu=mu)), ind=V_ind) assert np.allclose(M, M2)
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, U, pairwise=False, U_ind=U_ind, V_ind=V_ind, mu=mu) assert M.shape == (V.len_ind(V_ind), U.len_ind(U_ind)) M2 = V.dot(op.apply(U, ind=U_ind, mu=mu), pairwise=False, ind=V_ind) assert np.allclose(M, M2)
def test_almost_equal_self(vector_array): v = vector_array 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.)): for n in ['sup', 'l1', 'l2']: r = almost_equal(v[ind], v[ind], norm=n) 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(getattr(v[ind], n + '_norm')()))) assert np.all( almost_equal(c[ind], c.zeros(v.len_ind(ind)), atol=atol, rtol=rtol, norm=n)) if atol > 0: c = v.copy() c.scal(2. * atol / (np.max(getattr(v[ind], n + '_norm')()))) assert not np.all( almost_equal(c[ind], c.zeros(v.len_ind(ind)), atol=atol, rtol=rtol, norm=n)) c = v.copy() c.scal(1. + rtol * 0.9) assert np.all( almost_equal(c[ind], v[ind], atol=atol, rtol=rtol, norm=n)) 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=n)) c = v.copy() c.scal(1. + atol * 0.9 / np.max(getattr(v[ind], n + '_norm')())) assert np.all( almost_equal(c[ind], v[ind], atol=atol, rtol=rtol, norm=n)) if atol > 0 or rtol > 0: c = v.copy() c.scal(1 + rtol * 1.1 + atol * 1.1 / np.max(getattr(v[ind], n + '_norm')())) assert not np.all( almost_equal( c[ind], v[ind], atol=atol, rtol=rtol, norm=n))
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(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, mu=mu, ind=ind) assert np.all(Vind.almost_equal(V, o_ind=ind)) assert np.all(Vind.almost_equal(op.apply(U.copy(ind=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_apply_adjoint(operator_with_arrays): op, mu, _, V = operator_with_arrays try: U = op.apply_adjoint(V, mu=mu) except NotImplementedError: return assert U in op.source assert len(V) == len(U) for ind in list(valid_inds(V, 3)) + [[]]: Uind = op.apply_adjoint(V, mu=mu, ind=ind) assert np.all(Uind.almost_equal(U, o_ind=ind)) assert np.all(Uind.almost_equal(op.apply_adjoint(V.copy(ind=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): try: V = op.apply_inverse_adjoint(U, mu=mu, ind=ind) except InversionError: 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, V_ind=ind, atol=1e-10, rtol=1e-3))
def test_apply_adjoint(operator_with_arrays): op, mu, _, V = operator_with_arrays try: U = op.apply_adjoint(V, mu=mu) except NotImplementedError: return assert U in op.source assert len(V) == len(U) for ind in list(valid_inds(V, 3)) + [[]]: Uind = op.apply_adjoint(V, mu=mu, ind=ind) assert np.all(Uind.almost_equal(U, o_ind=ind)) assert np.all( Uind.almost_equal(op.apply_adjoint(V.copy(ind=ind), mu=mu)))
def test_apply_inverse_adjoint_with_products(operator_with_arrays_and_products): op, mu, U, _, sp, rp = operator_with_arrays_and_products if not op.linear: return for ind in valid_inds(U): try: V = op.apply_inverse_adjoint(U, mu=mu, ind=ind, source_product=sp, range_product=rp) except InversionError: return assert V in op.range assert len(V) == U.len_ind(ind) UU = op.apply_adjoint(V, mu=mu, source_product=sp, range_product=rp) assert np.all(almost_equal(UU, U, V_ind=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, v, U_ind=ind, V_ind=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.sup_norm(ind) == 0): continue r = almost_equal(v, v, U_ind=ind, V_ind=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.sup_norm(ind) == 0): continue c = v.copy() c.scal(atol * (1 - 1e-10) / (np.max(norm(v, ind=ind)))) assert np.all(almost_equal(c, c.zeros(v.len_ind(ind)), U_ind=ind, atol=atol, rtol=rtol, norm=norm)) assert np.all(almost_equal(c, c.zeros(v.len_ind(ind)), U_ind=ind, atol=atol, rtol=rtol, product=product)) if atol > 0: c = v.copy() c.scal(2. * atol / (np.max(norm(v, ind=ind)))) assert not np.all(almost_equal(c, c.zeros(v.len_ind(ind)), U_ind=ind, atol=atol, rtol=rtol, norm=norm)) assert not np.all(almost_equal(c, c.zeros(v.len_ind(ind)), U_ind=ind, atol=atol, rtol=rtol, product=product)) c = v.copy() c.scal(1. + rtol * 0.9) assert np.all(almost_equal(c, v, U_ind=ind, V_ind=ind, atol=atol, rtol=rtol, norm=norm)) assert np.all(almost_equal(c, v, U_ind=ind, V_ind=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, v, U_ind=ind, V_ind=ind, atol=atol, rtol=rtol, norm=norm)) assert not np.all(almost_equal(c, v, U_ind=ind, V_ind=ind, atol=atol, rtol=rtol, product=product)) c = v.copy() c.scal(1. + atol * 0.9 / np.max(np.max(norm(v, ind=ind)))) assert np.all(almost_equal(c, v, U_ind=ind, V_ind=ind, atol=atol, rtol=rtol, norm=norm)) assert np.all(almost_equal(c, v, U_ind=ind, V_ind=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=ind)))) assert not np.all(almost_equal(c, v, U_ind=ind, V_ind=ind, atol=atol, rtol=rtol, norm=norm)) assert not np.all(almost_equal(c, v, U_ind=ind, V_ind=ind, atol=atol, rtol=rtol, product=product))
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_transpose(operator_with_arrays): op, mu, _, V = operator_with_arrays if not op.linear: return try: U = op.apply_transpose(V, mu=mu) except NotImplementedError: return assert U in op.source assert len(V) == len(U) for ind in list(valid_inds(V, 3)) + [[]]: Uind = op.apply_transpose(V[ind], mu=mu) assert np.all(almost_equal(Uind, U[ind])) assert np.all(almost_equal(Uind, op.apply_transpose(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))
def test_apply_inverse(operator_with_arrays): op, mu, _, V = operator_with_arrays for options in chain([None], op.invert_options, op.invert_options.itervalues()): for ind in valid_inds(V): try: U = op.apply_inverse(V, mu=mu, ind=ind, options=options) except InversionError: return assert U in op.source assert len(U) == V.len_ind(ind) VV = op.apply(U, mu=mu) if (isinstance(options, str) and options.startswith('least_squares') or not isinstance(options, (str, type(None))) and options['type'].startswith('least_squares')): continue assert float_cmp_all(VV.l2_norm(), V.l2_norm(ind=ind), atol=1e-10, rtol=0.5)
def test_apply_inverse_transpose(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_transpose(U[ind], mu=mu) except InversionError: return assert V in op.range assert len(V) == U.len_ind(ind) UU = op.apply_transpose(V, mu=mu) assert np.all(almost_equal(UU, U[ind], atol=1e-10, rtol=1e-3))
def test_almost_equal_self(vector_array): v = vector_array 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.)): for n in ['sup', 'l1', 'l2']: r = almost_equal(v[ind], v[ind], norm=n) 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(getattr(v[ind], n + '_norm')()))) assert np.all(almost_equal(c[ind], c.zeros(v.len_ind(ind)), atol=atol, rtol=rtol, norm=n)) if atol > 0: c = v.copy() c.scal(2. * atol / (np.max(getattr(v[ind], n + '_norm')()))) assert not np.all(almost_equal(c[ind], c.zeros(v.len_ind(ind)), atol=atol, rtol=rtol, norm=n)) c = v.copy() c.scal(1. + rtol * 0.9) assert np.all(almost_equal(c[ind], v[ind], atol=atol, rtol=rtol, norm=n)) 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=n)) c = v.copy() c.scal(1. + atol * 0.9 / np.max(getattr(v[ind], n + '_norm')())) assert np.all(almost_equal(c[ind], v[ind], atol=atol, rtol=rtol, norm=n)) if atol > 0 or rtol > 0: c = v.copy() c.scal(1 + rtol * 1.1 + atol * 1.1 / np.max(getattr(v[ind], n + '_norm')())) assert not np.all(almost_equal(c[ind], v[ind], atol=atol, rtol=rtol, norm=n))