Example #1
0
def test_scal(vector_array):
    v = vector_array
    for ind in valid_inds(v):
        if v.len_ind(ind) != v.len_ind_unique(ind):
            with pytest.raises(Exception):
                c = v.copy()
                c[ind].scal(1.)
            continue
        ind_complement_ = ind_complement(v, ind)
        c = v.copy()
        c[ind].scal(1.)
        assert len(c) == len(v)
        assert np.all(almost_equal(c, v))

        c = v.copy()
        c[ind].scal(0.)
        assert np.all(almost_equal(c[ind], v.zeros(v.len_ind(ind))))
        assert np.all(almost_equal(c[ind_complement_], v[ind_complement_]))

        for x in (1., 1.4, np.random.random(v.len_ind(ind))):
            c = v.copy()
            c[ind].scal(x)
            assert np.all(almost_equal(c[ind_complement_], v[ind_complement_]))
            assert np.allclose(c[ind].sup_norm(), v[ind].sup_norm() * abs(x))
            assert np.allclose(c[ind].l2_norm(), v[ind].l2_norm() * abs(x))
            try:
                y = v.to_numpy(True)
                if isinstance(x, np.ndarray) and not isinstance(ind, Number):
                    x = x[:, np.newaxis]
                y[ind] *= x
                assert np.allclose(c.to_numpy(), y)
            except NotImplementedError:
                pass
Example #2
0
def test_InverseAdjointOperator(operator_with_arrays):
    op, mu, U, V = operator_with_arrays
    if not op.linear:
        return
    inv = InverseAdjointOperator(op)
    rtol = atol = 1e-12
    try:
        assert np.all(almost_equal(inv.apply(U, mu=mu), op.apply_inverse_adjoint(U, mu=mu),
                                   rtol=rtol, atol=atol))
    except (InversionError, LinAlgError, NotImplementedError):
        pass
    try:
        assert np.all(almost_equal(inv.apply_inverse(V, mu=mu), op.apply_adjoint(V, mu=mu),
                                   rtol=rtol, atol=atol))
    except (InversionError, LinAlgError, NotImplementedError):
        pass
    try:
        assert np.all(almost_equal(inv.apply_adjoint(V, mu=mu), op.apply_inverse(V, mu=mu),
                                   rtol=rtol, atol=atol))
    except (InversionError, LinAlgError, NotImplementedError):
        pass
    try:
        assert np.all(almost_equal(inv.apply_inverse_adjoint(U, mu=mu), op.apply(U, mu=mu),
                                   rtol=rtol, atol=atol))
    except (InversionError, LinAlgError, NotImplementedError):
        pass
Example #3
0
def test_append(compatible_vector_array_pair):
    v1, v2 = compatible_vector_array_pair
    len_v1, len_v2 = len(v1), len(v2)
    for ind in valid_inds(v2):
        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
Example #4
0
def test_sub(vector_arrays):
    v1, v2 = vector_arrays
    c1 = v1.copy()
    cc1 = v1.copy()
    c1.axpy(-1, v2)
    assert np.all(almost_equal((v1 - v2), c1))
    assert np.all(almost_equal(v1, cc1))
Example #5
0
def test_assemble(operator_with_arrays):
    op, mu, U, V = operator_with_arrays
    aop = op.assemble(mu=mu)
    assert op.source == aop.source
    assert op.range == aop.range
    assert np.all(almost_equal(aop.apply(U), op.apply(U, mu=mu)))

    try:
        ATV = op.apply_adjoint(V, mu=mu)
    except (NotImplementedError, LinAlgError):
        ATV = None
    if ATV is not None:
        assert np.all(almost_equal(aop.apply_adjoint(V), ATV))

    try:
        AIV = op.apply_inverse(V, mu=mu)
    except InversionError:
        AIV = None
    if AIV is not None:
        assert np.all(almost_equal(aop.apply_inverse(V), AIV))

    try:
        AITU = op.apply_inverse_adjoint(U, mu=mu)
    except (InversionError, LinAlgError):
        AITU = None
    if AITU is not None:
        assert np.all(almost_equal(aop.apply_inverse_adjoint(U), AITU))
Example #6
0
def test_lincomb_op():
    p1 = MonomOperator(1)
    p2 = MonomOperator(2)
    p12 = p1 + p2
    p0 = p1 - p1
    x = np.linspace(-1., 1., num=3)
    vx = p1.source.make_array((x[:, np.newaxis]))
    one = p1.source.make_array([1])
    assert np.allclose(p0.apply(vx).to_numpy(), [0.])
    assert np.allclose(p12.apply(vx).to_numpy(), (x * x + x)[:, np.newaxis])
    assert np.allclose((p1 * 2.).apply(vx).to_numpy(), (x * 2.)[:, np.newaxis])
    with pytest.raises(AssertionError):
        p2.jacobian(vx)
    for i in range(len(vx)):
        assert almost_equal(
            p2.jacobian(vx[i]).apply(one),
            p1.apply(vx[i]) * 2.)
        assert almost_equal(p0.jacobian(vx[i]).apply(one), vx[i] * 0.)
    with pytest.raises(TypeError):
        p2.as_vector()
    p1.as_vector()
    assert almost_equal(p1.as_vector(), p1.apply(p1.source.make_array([1.])))

    basis = p1.source.make_array([1.])
    for p in (p1, p2, p12):
        projected = project(p, basis, basis)
        pa = projected.apply(vx)
        assert almost_equal(pa, p.apply(vx)).all()
Example #7
0
def test_neg(vector_array):
    v = vector_array
    c = v.copy()
    cc = v.copy()
    c.scal(-1)
    assert np.all(almost_equal(c, -v))
    assert np.all(almost_equal(v, cc))
Example #8
0
def test_lincomb_op_with_zero_coefficients():
    p1 = MonomOperator(1)
    p2 = MonomOperator(2)
    p10 = p1 + 0 * p2
    p0 = 0 * p1 + 0 * p1
    x = np.linspace(-1., 1., num=3)
    vx = p1.source.make_array((x[:, np.newaxis]))

    pc1 = NumpyMatrixOperator(np.eye(p1.source.dim))
    pc2 = NumpyMatrixOperator(2 * np.eye(p1.source.dim))
    pc10 = pc1 + 0 * pc2
    pc0 = 0 * pc1 + 0 * pc2

    assert np.allclose(p0.apply(vx).to_numpy(), [0.])
    assert len(p0.apply(vx)) == len(vx)
    assert almost_equal(p10.apply(vx), p1.apply(vx)).all()

    assert np.allclose(p0.apply2(vx, vx), [0.])
    assert len(p0.apply2(vx, vx)) == len(vx)
    assert np.allclose(p10.apply2(vx, vx), p1.apply2(vx, vx))

    assert np.allclose(p0.pairwise_apply2(vx, vx), [0.])
    assert len(p0.pairwise_apply2(vx, vx)) == len(vx)
    assert np.allclose(p10.pairwise_apply2(vx, vx), p1.pairwise_apply2(vx, vx))

    assert np.allclose(pc0.apply_adjoint(vx).to_numpy(), [0.])
    assert len(pc0.apply_adjoint(vx)) == len(vx)
    assert almost_equal(pc10.apply_adjoint(vx), pc1.apply_adjoint(vx)).all()
Example #9
0
def test_scal(vector_array):
    v = vector_array
    for ind in valid_inds(v):
        if v.len_ind(ind) != v.len_ind_unique(ind):
            with pytest.raises(Exception):
                c = v.copy()
                c[ind].scal(1.)
            continue
        ind_complement_ = ind_complement(v, ind)
        c = v.copy()
        c[ind].scal(1.)
        assert len(c) == len(v)
        assert np.all(almost_equal(c, v))

        c = v.copy()
        c[ind].scal(0.)
        assert np.all(almost_equal(c[ind], v.zeros(v.len_ind(ind))))
        assert np.all(almost_equal(c[ind_complement_], v[ind_complement_]))

        for x in (1., 1.4, np.random.random(v.len_ind(ind))):
            c = v.copy()
            c[ind].scal(x)
            assert np.all(almost_equal(c[ind_complement_], v[ind_complement_]))
            assert np.allclose(c[ind].sup_norm(), v[ind].sup_norm() * abs(x))
            assert np.allclose(c[ind].l2_norm(), v[ind].l2_norm() * abs(x))
            if hasattr(v, 'data'):
                y = v.data.copy()
                if NUMPY_INDEX_QUIRK and len(y) == 0:
                    pass
                else:
                    if isinstance(x,
                                  np.ndarray) and not isinstance(ind, Number):
                        x = x[:, np.newaxis]
                    y[ind] *= x
                assert np.allclose(c.data, y)
Example #10
0
def test_almost_equal_incompatible(vector_arrays):
    v1, v2 = vector_arrays
    for ind1, ind2 in valid_inds_of_same_length(v1, v2):
        for sup_norm in (False, True):
            c1, c2 = v1.copy(), v2.copy()
            with pytest.raises(Exception):
                almost_equal(c1[ind1], c2[ind2], sup_norm=sup_norm)
Example #11
0
def test_assemble(operator_with_arrays):
    op, mu, U, V = operator_with_arrays
    aop = op.assemble(mu=mu)
    assert op.source == aop.source
    assert op.range == aop.range
    assert np.all(almost_equal(aop.apply(U), op.apply(U, mu=mu)))

    try:
        ATV = op.apply_adjoint(V, mu=mu)
    except (NotImplementedError, LinAlgError):
        ATV = None
    if ATV is not None:
        assert np.all(almost_equal(aop.apply_adjoint(V), ATV))

    try:
        AIV = op.apply_inverse(V, mu=mu)
    except InversionError:
        AIV = None
    if AIV is not None:
        assert np.all(almost_equal(aop.apply_inverse(V), AIV))

    try:
        AITU = op.apply_inverse_adjoint(U, mu=mu)
    except (InversionError, LinAlgError):
        AITU = None
    if AITU is not None:
        assert np.all(almost_equal(aop.apply_inverse_adjoint(U), AITU))
Example #12
0
def test_scal(vector_array):
    v = vector_array
    for ind in valid_inds(v):
        if v.len_ind(ind) != v.len_ind_unique(ind):
            with pytest.raises(Exception):
                c = v.copy()
                c[ind].scal(1.)
            continue
        ind_complement_ = ind_complement(v, ind)
        c = v.copy()
        c[ind].scal(1.)
        assert len(c) == len(v)
        assert np.all(almost_equal(c, v))

        c = v.copy()
        c[ind].scal(0.)
        assert np.all(almost_equal(c[ind], v.zeros(v.len_ind(ind))))
        assert np.all(almost_equal(c[ind_complement_], v[ind_complement_]))

        for x in (1., 1.4, np.random.random(v.len_ind(ind))):
            c = v.copy()
            c[ind].scal(x)
            assert np.all(almost_equal(c[ind_complement_], v[ind_complement_]))
            assert np.allclose(c[ind].sup_norm(), v[ind].sup_norm() * abs(x))
            assert np.allclose(c[ind].l2_norm(), v[ind].l2_norm() * abs(x))
            if hasattr(v, 'data'):
                y = v.data.copy()
                if NUMPY_INDEX_QUIRK and len(y) == 0:
                    pass
                else:
                    if isinstance(x, np.ndarray) and not isinstance(ind, Number):
                        x = x[:, np.newaxis]
                    y[ind] *= x
                assert np.allclose(c.data, y)
Example #13
0
def test_neg(vector_array):
    v = vector_array
    c = v.copy()
    cc = v.copy()
    c.scal(-1)
    assert np.all(almost_equal(c, -v))
    assert np.all(almost_equal(v, cc))
Example #14
0
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()
        c1.append(c2, o_ind=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, c2, U_ind=list(range(len_v1, len(c1))),
                         V_ind=ind))
        if hasattr(v1, 'data'):
            assert np.allclose(c1.data, np.vstack((dv1, indexed(dv2, ind))))
        c1.append(c2, o_ind=ind, remove_from_other=True)
        assert len(c2) == len(ind_complement_)
        assert c2.dim == c1.dim
        assert c2.subtype == c1.subtype
        assert len(c1) == len_v1 + 2 * len_ind
        assert np.all(
            almost_equal(c1,
                         c1,
                         U_ind=list(range(len_v1, len_v1 + len_ind)),
                         V_ind=list(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_))
Example #15
0
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)
Example #16
0
def test_scal(vector_array):
    v = vector_array
    for ind in valid_inds(v):
        if v.len_ind(ind) != v.len_ind_unique(ind):
            with pytest.raises(Exception):
                c = v.copy()
                c[ind].scal(1.)
            continue
        ind_complement_ = ind_complement(v, ind)
        c = v.copy()
        c[ind].scal(1.)
        assert len(c) == len(v)
        assert np.all(almost_equal(c, v))

        c = v.copy()
        c[ind].scal(0.)
        assert np.all(almost_equal(c[ind], v.zeros(v.len_ind(ind))))
        assert np.all(almost_equal(c[ind_complement_], v[ind_complement_]))

        for x in (1., 1.4, np.random.random(v.len_ind(ind))):
            c = v.copy()
            c[ind].scal(x)
            assert np.all(almost_equal(c[ind_complement_], v[ind_complement_]))
            assert np.allclose(c[ind].sup_norm(), v[ind].sup_norm() * abs(x))
            assert np.allclose(c[ind].l2_norm(), v[ind].l2_norm() * abs(x))
            try:
                y = v.to_numpy(True)
                if isinstance(x, np.ndarray) and not isinstance(ind, Number):
                    x = x[:, np.newaxis]
                y[ind] *= x
                assert np.allclose(c.to_numpy(), y)
            except NotImplementedError:
                pass
Example #17
0
def test_InverseAdjointOperator(operator_with_arrays):
    op, mu, U, V = operator_with_arrays
    if not op.linear:
        return
    inv = InverseAdjointOperator(op)
    rtol = atol = 1e-12
    try:
        assert np.all(almost_equal(inv.apply(U, mu=mu), op.apply_inverse_adjoint(U, mu=mu),
                                   rtol=rtol, atol=atol))
    except (InversionError, LinAlgError, NotImplementedError):
        pass
    try:
        assert np.all(almost_equal(inv.apply_inverse(V, mu=mu), op.apply_adjoint(V, mu=mu),
                                   rtol=rtol, atol=atol))
    except (InversionError, LinAlgError, NotImplementedError):
        pass
    try:
        assert np.all(almost_equal(inv.apply_adjoint(V, mu=mu), op.apply_inverse(V, mu=mu),
                                   rtol=rtol, atol=atol))
    except (InversionError, LinAlgError, NotImplementedError):
        pass
    try:
        assert np.all(almost_equal(inv.apply_inverse_adjoint(U, mu=mu), op.apply(U, mu=mu),
                                   rtol=rtol, atol=atol))
    except (InversionError, LinAlgError, NotImplementedError):
        pass
Example #18
0
def test_add(vector_arrays):
    v1, v2 = vector_arrays
    c1 = v1.copy()
    cc1 = v1.copy()
    c1.axpy(1, v2)
    assert np.all(almost_equal(v1 + v2, c1))
    assert np.all(almost_equal(v1, cc1))
Example #19
0
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)
Example #20
0
def test_rmul(vector_array):
    c = vector_array.copy()
    for a in (-1, -3, 0, 1, 23):
        cc = vector_array.copy()
        cc.scal(a)
        assert np.all(almost_equal((a * vector_array), cc))
        assert np.all(almost_equal(vector_array, c))
Example #21
0
def test_mul(vector_array):
    c = vector_array.copy()
    for a in (-1, -3, 0, 1, 23, np.arange(len(vector_array))):
        cc = vector_array.copy()
        cc.scal(a)
        assert np.all(almost_equal((vector_array * a), cc))
        assert np.all(almost_equal(vector_array, c))
Example #22
0
def test_mul(vector_array):
    v = vector_array
    c = v.copy()
    for a in (-1, -3, 0, 1, 23):
        cc = v.copy()
        cc.scal(a)
        assert np.all(almost_equal((v * a), cc))
        assert np.all(almost_equal(v, c))
Example #23
0
def test_rmul(vector_array):
    v = vector_array
    c = v.copy()
    for a in (-1, -3, 0, 1, 23):
        cc = v.copy()
        cc.scal(a)
        assert np.all(almost_equal((a * v), cc))
        assert np.all(almost_equal(v, c))
Example #24
0
def test_identity_lincomb():
    space = NumpyVectorSpace(10)
    identity = IdentityOperator(space)
    ones = space.ones()
    idid = (identity + identity)
    assert almost_equal(ones * 2, idid.apply(ones))
    assert almost_equal(ones * 2, idid.apply_adjoint(ones))
    assert almost_equal(ones * 0.5, idid.apply_inverse(ones))
    assert almost_equal(ones * 0.5, idid.apply_inverse_adjoint(ones))
Example #25
0
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)))
Example #26
0
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)))
Example #27
0
def test_identity_lincomb():
    space = NumpyVectorSpace(10)
    identity = IdentityOperator(space)
    ones = space.ones()
    idid = (identity + identity)
    assert almost_equal(ones * 2, idid.apply(ones))
    assert almost_equal(ones * 2, idid.apply_adjoint(ones))
    assert almost_equal(ones * 0.5, idid.apply_inverse(ones))
    assert almost_equal(ones * 0.5, idid.apply_inverse_adjoint(ones))
Example #28
0
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):
                c1, c2 = v1.copy(), v2.copy()
                c1.axpy(0., c2, ind=ind1, x_ind=ind2)
            continue

        ind1_complement = ind_complement(v1, ind1)
        c1, c2 = v1.copy(), v2.copy()
        c1.axpy(0., c2, ind=ind1, x_ind=ind2)
        assert len(c1) == len(v1)
        assert np.all(almost_equal(c1, v1))
        assert np.all(almost_equal(c2, v2))

        np.random.seed(len(v1) + 39)
        for a in (1., 1.4, np.random.random(v1.len_ind(ind1))):
            c1, c2 = v1.copy(), v2.copy()
            c1.axpy(a, c2, ind=ind1, x_ind=ind2)
            assert len(c1) == len(v1)
            assert np.all(
                almost_equal(c1,
                             v1,
                             U_ind=ind1_complement,
                             V_ind=ind1_complement))
            assert np.all(almost_equal(c2, v2))
            assert np.all(
                c1.sup_norm(ind1) <= v1.sup_norm(ind1) +
                abs(a) * v2.sup_norm(ind2) * (1. + 1e-10))
            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()
                if isinstance(ind1, Number):
                    x[[ind1]] += indexed(dv2, ind2) * a
                else:
                    if NUMPY_INDEX_QUIRK and len(x) == 0:
                        pass
                    else:
                        if isinstance(a, np.ndarray):
                            aa = a[:, np.newaxis]
                        else:
                            aa = a
                        x[ind1] += indexed(dv2, ind2) * aa
                assert np.allclose(c1.data, x)
            c1.axpy(-a, c2, ind=ind1, x_ind=ind2)
            assert len(c1) == len(v1)
            assert np.all(almost_equal(c1, v1))
Example #29
0
def test_almost_equal_wrong_ind(compatible_vector_array_pair):
    v1, v2 = compatible_vector_array_pair
    for ind1, ind2 in invalid_ind_pairs(v1, v2):
        for n in ['sup', 'l1', 'l2']:
            if (ind1 is None and len(v1) == 1 or isinstance(ind1, Number) or hasattr(ind1, '__len__') and len(ind1) == 1 or  # NOQA
                ind2 is None and len(v2) == 1 or isinstance(ind2, Number) or hasattr(ind2, '__len__') and len(ind2) == 1):   # NOQA
                continue
            c1, c2 = v1.copy(), v2.copy()
            with pytest.raises(Exception):
                almost_equal(c1[ind], c2[ind], norm=n)
Example #30
0
def test_lincomb_adjoint():
    op = LincombOperator([NumpyMatrixOperator(np.eye(10)), NumpyMatrixOperator(np.eye(10))],
                         [1+3j, ExpressionParameterFunctional('c[0] + 3', {'c': 1})])
    mu = op.parameters.parse(1j)
    U = op.range.random()
    V = op.apply_adjoint(U, mu=mu)
    VV = op.H.apply(U, mu=mu)
    assert np.all(almost_equal(V, VV))
    VVV = op.apply(U, mu=mu).conj()
    assert np.all(almost_equal(V, VVV))
Example #31
0
def test_almost_equal_wrong_ind(compatible_vector_array_pair):
    v1, v2 = compatible_vector_array_pair
    for ind1, ind2 in invalid_ind_pairs(v1, v2):
        for n in ['sup', 'l1', 'l2']:
            if (ind1 is None and len(v1) == 1 or isinstance(ind1, Number) or hasattr(ind1, '__len__') and len(ind1) == 1 or
                ind2 is None and len(v2) == 1 or isinstance(ind2, Number) or hasattr(ind2, '__len__') and len(ind2) == 1):  # NOQA
                continue
            c1, c2 = v1.copy(), v2.copy()
            with pytest.raises(Exception):
                almost_equal(c1, c2, U_ind=ind1, V_ind=ind2, norm=n)
Example #32
0
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))
Example #33
0
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):
                c, = v.copy()
                c.axpy(0., c, ind=ind1, x_ind=ind2)
            continue

        ind1_complement = ind_complement(v, ind1)
        c = v.copy()
        c.axpy(0., c, ind=ind1, x_ind=ind2)
        assert len(c) == len(v)
        assert np.all(almost_equal(c, v))
        assert np.all(almost_equal(c, v))

        np.random.seed(len(v) + 8)
        for a in (1., 1.4, np.random.random(v.len_ind(ind1))):
            c = v.copy()
            c.axpy(a, c, ind=ind1, x_ind=ind2)
            assert len(c) == len(v)
            assert np.all(almost_equal(c, v, U_ind=ind1_complement, V_ind=ind1_complement))
            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()
                if isinstance(ind1, Number):
                    x[[ind1]] += indexed(dv, ind2) * a
                else:
                    if NUMPY_INDEX_QUIRK and len(x) == 0:
                        pass
                    else:
                        if isinstance(a, np.ndarray):
                            aa = a[:, np.newaxis]
                        else:
                            aa = a
                        x[ind1] += indexed(dv, ind2) * aa
                assert np.allclose(c.data, x)
            c.axpy(-a, v, ind=ind1, x_ind=ind2)
            assert len(c) == len(v)
            assert np.all(almost_equal(c, v))

    for ind in valid_inds(v):
        if v.len_ind(ind) != v.len_ind_unique(ind):
            continue

        for x in (1., 23., -4):
            c = v.copy()
            cc = v.copy()
            c.axpy(x, c, ind=ind, x_ind=ind)
            cc.scal(1 + x, ind=ind)
            assert np.all(almost_equal(c, cc))
Example #34
0
def test_axpy_self(vector_array):
    v = vector_array

    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):
                c, = v.copy()
                c[ind1].axpy(0., c[ind2])
            continue

        ind1_complement = ind_complement(v, ind1)
        c = v.copy()
        c[ind1].axpy(0., c[ind2])
        assert len(c) == len(v)
        assert np.all(almost_equal(c, v))

        np.random.seed(len(v) + 8)
        for a in (1., 1.4, np.random.random(v.len_ind(ind1))):
            c = v.copy()
            c[ind1].axpy(a, c[ind2])
            assert len(c) == len(v)
            assert np.all(almost_equal(c[ind1_complement], v[ind1_complement]))
            assert np.all(c[ind1].sup_norm() <= v[ind1].sup_norm() +
                          abs(a) * v[ind2].sup_norm() * (1. + 1e-10))
            assert np.all(c[ind1].l1_norm() <=
                          (v[ind1].l1_norm() + abs(a) * v[ind2].l1_norm()) *
                          (1. + 1e-10))
            if hasattr(v, 'data'):
                x = v.data.copy()
                if isinstance(ind1, Number):
                    x[[ind1]] += indexed(v.data, ind2) * a
                else:
                    if NUMPY_INDEX_QUIRK and len(x) == 0:
                        pass
                    else:
                        if isinstance(a, np.ndarray):
                            aa = a[:, np.newaxis]
                        else:
                            aa = a
                        x[ind1] += indexed(v.data, ind2) * aa
                assert np.allclose(c.data, x)
            c[ind1].axpy(-a, v[ind2])
            assert len(c) == len(v)
            assert np.all(almost_equal(c, v))

    for ind in valid_inds(v):
        if v.len_ind(ind) != v.len_ind_unique(ind):
            continue

        for x in (1., 23., -4):
            c = v.copy()
            cc = v.copy()
            c[ind].axpy(x, c[ind])
            cc[ind].scal(1 + x)
            assert np.all(almost_equal(c, cc))
Example #35
0
def test_sub(compatible_vector_array_pair):
    v1, v2 = compatible_vector_array_pair
    if len(v2) < len(v1):
        v2.append(v2[np.zeros(len(v1) - len(v2), dtype=np.int)])
    elif len(v2) > len(v1):
        del v2[list(range(len(v2) - len(v1)))]
    c1 = v1.copy()
    cc1 = v1.copy()
    c1.axpy(-1, v2)
    assert np.all(almost_equal((v1 - v2), c1))
    assert np.all(almost_equal(v1, cc1))
Example #36
0
def test_add(compatible_vector_array_pair):
    v1, v2 = compatible_vector_array_pair
    if len(v2) < len(v1):
        v2.append(v2, o_ind=np.zeros(len(v1) - len(v2), dtype=np.int))
    elif len(v2) > len(v1):
        v2.remove(range(len(v2)-len(v1)))
    c1 = v1.copy()
    cc1 = v1.copy()
    c1.axpy(1, v2)
    assert np.all(almost_equal(v1 + v2, c1))
    assert np.all(almost_equal(v1, cc1))
Example #37
0
def test_add(compatible_vector_array_pair):
    v1, v2 = compatible_vector_array_pair
    if len(v2) < len(v1):
        v2.append(v2, o_ind=np.zeros(len(v1) - len(v2), dtype=np.int))
    elif len(v2) > len(v1):
        v2.remove(list(range(len(v2) - len(v1))))
    c1 = v1.copy()
    cc1 = v1.copy()
    c1.axpy(1, v2)
    assert np.all(almost_equal(v1 + v2, c1))
    assert np.all(almost_equal(v1, cc1))
Example #38
0
def test_add(compatible_vector_array_pair):
    v1, v2 = compatible_vector_array_pair
    if len(v2) < len(v1):
        v2.append(v2[np.zeros(len(v1) - len(v2), dtype=np.int)])
    elif len(v2) > len(v1):
        del v2[:len(v2) - len(v1)]
    c1 = v1.copy()
    cc1 = v1.copy()
    c1.axpy(1, v2)
    assert np.all(almost_equal(v1 + v2, c1))
    assert np.all(almost_equal(v1, cc1))
Example #39
0
def test_add(compatible_vector_array_pair):
    v1, v2 = compatible_vector_array_pair
    if len(v2) < len(v1):
        v2.append(v2[np.zeros(len(v1) - len(v2), dtype=np.int)])
    elif len(v2) > len(v1):
        del v2[:len(v2)-len(v1)]
    c1 = v1.copy()
    cc1 = v1.copy()
    c1.axpy(1, v2)
    assert np.all(almost_equal(v1 + v2, c1))
    assert np.all(almost_equal(v1, cc1))
Example #40
0
def test_sub(compatible_vector_array_pair):
    v1, v2 = compatible_vector_array_pair
    if len(v2) < len(v1):
        v2.append(v2[np.zeros(len(v1) - len(v2), dtype=np.int)])
    elif len(v2) > len(v1):
        del v2[list(range(len(v2)-len(v1)))]
    c1 = v1.copy()
    cc1 = v1.copy()
    c1.axpy(-1, v2)
    assert np.all(almost_equal((v1 - v2), c1))
    assert np.all(almost_equal(v1, cc1))
Example #41
0
def test_gram_schmidt(vector_array):
    U = vector_array

    V = U.copy()
    onb = gram_schmidt(U, copy=True)
    assert np.all(almost_equal(U, V))
    assert np.allclose(onb.dot(onb), np.eye(len(onb)))
    assert np.all(almost_equal(U, onb.lincomb(U.dot(onb)), rtol=1e-13))

    onb2 = gram_schmidt(U, copy=False)
    assert np.all(almost_equal(onb, onb2))
    assert np.all(almost_equal(onb, U))
Example #42
0
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(almost_equal(Uind, U, V_ind=ind))
        assert np.all(almost_equal(Uind, op.apply_adjoint(V.copy(ind=ind), mu=mu)))
Example #43
0
def test_gram_schmidt_with_product(operator_with_arrays_and_products):
    _, _, U, _, p, _ = operator_with_arrays_and_products

    V = U.copy()
    onb = gram_schmidt(U, product=p, copy=True)
    assert np.all(almost_equal(U, V))
    assert np.allclose(p.apply2(onb, onb), np.eye(len(onb)))
    assert np.all(almost_equal(U, onb.lincomb(p.apply2(onb, U).T), rtol=1e-13))

    onb2 = gram_schmidt(U, product=p, copy=False)
    assert np.all(almost_equal(onb, onb2))
    assert np.all(almost_equal(onb, U))
Example #44
0
def test_block_identity_lincomb():
    space = NumpyVectorSpace(10)
    space2 = BlockVectorSpace([space, space])
    identity = BlockDiagonalOperator([IdentityOperator(space), IdentityOperator(space)])
    identity2 = IdentityOperator(space2)
    ones = space.ones()
    ones2 = space2.make_array([ones, ones])
    idid = identity + identity2
    assert almost_equal(ones2 * 2, idid.apply(ones2))
    assert almost_equal(ones2 * 2, idid.apply_adjoint(ones2))
    assert almost_equal(ones2 * 0.5, idid.apply_inverse(ones2))
    assert almost_equal(ones2 * 0.5, idid.apply_inverse_adjoint(ones2))
Example #45
0
def test_gram_schmidt_with_product(operator_with_arrays_and_products):
    _, _, U, _, p, _ = operator_with_arrays_and_products

    V = U.copy()
    onb = gram_schmidt(U, product=p, copy=True)
    assert np.all(almost_equal(U, V))
    assert np.allclose(p.apply2(onb, onb), np.eye(len(onb)))
    assert np.all(almost_equal(U, onb.lincomb(p.apply2(U, onb)), rtol=1e-13))

    onb2 = gram_schmidt(U, product=p, copy=False)
    assert np.all(almost_equal(onb, onb2))
    assert np.all(almost_equal(onb, U))
Example #46
0
def test_gram_schmidt(vector_array):
    U = vector_array

    V = U.copy()
    onb = gram_schmidt(U, copy=True)
    assert np.all(almost_equal(U, V))
    assert np.allclose(onb.dot(onb), np.eye(len(onb)))
    assert np.all(almost_equal(U, onb.lincomb(U.dot(onb)), rtol=1e-13))

    onb2 = gram_schmidt(U, copy=False)
    assert np.all(almost_equal(onb, onb2))
    assert np.all(almost_equal(onb, U))
Example #47
0
def test_axpy_self(vector_array):
    v = vector_array

    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):
                c, = v.copy()
                c[ind1].axpy(0., c[ind2])
            continue

        ind1_complement = ind_complement(v, ind1)
        c = v.copy()
        c[ind1].axpy(0., c[ind2])
        assert len(c) == len(v)
        assert np.all(almost_equal(c, v))

        np.random.seed(len(v) + 8)
        for a in (1., 1.4, np.random.random(v.len_ind(ind1))):
            c = v.copy()
            c[ind1].axpy(a, c[ind2])
            assert len(c) == len(v)
            assert np.all(almost_equal(c[ind1_complement], v[ind1_complement]))
            assert np.all(c[ind1].sup_norm() <= v[ind1].sup_norm() +
                          abs(a) * v[ind2].sup_norm() * (1. + 1e-10))
            try:
                x = v.to_numpy(True).astype(
                    complex)  # ensure that inplace addition works
                if isinstance(ind1, Number):
                    x[[ind1]] += indexed(v.to_numpy(), ind2) * a
                else:
                    if isinstance(a, np.ndarray):
                        aa = a[:, np.newaxis]
                    else:
                        aa = a
                    x[ind1] += indexed(v.to_numpy(), ind2) * aa
                assert np.allclose(c.to_numpy(), x)
            except NotImplementedError:
                pass
            c[ind1].axpy(-a, v[ind2])
            assert len(c) == len(v)
            assert np.all(almost_equal(c, v))

    for ind in valid_inds(v):
        if v.len_ind(ind) != v.len_ind_unique(ind):
            continue

        for x in (1., 23., -4):
            c = v.copy()
            cc = v.copy()
            c[ind].axpy(x, c[ind])
            cc[ind].scal(1 + x)
            assert np.all(almost_equal(c, cc))
Example #48
0
def test_axpy_self(vector_array):
    v = vector_array

    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):
                c, = v.copy()
                c[ind1].axpy(0., c[ind2])
            continue

        ind1_complement = ind_complement(v, ind1)
        c = v.copy()
        c[ind1].axpy(0., c[ind2])
        assert len(c) == len(v)
        assert np.all(almost_equal(c, v))

        np.random.seed(len(v) + 8)
        for a in (1., 1.4, np.random.random(v.len_ind(ind1))):
            c = v.copy()
            c[ind1].axpy(a, c[ind2])
            assert len(c) == len(v)
            assert np.all(almost_equal(c[ind1_complement], v[ind1_complement]))
            assert np.all(c[ind1].sup_norm() <= v[ind1].sup_norm() + abs(a) * v[ind2].sup_norm() * (1. + 1e-10))
            assert np.all(c[ind1].l1_norm() <= (v[ind1].l1_norm() + abs(a) * v[ind2].l1_norm()) * (1. + 1e-10))
            try:
                x = v.to_numpy(True)
                if isinstance(ind1, Number):
                    x[[ind1]] += indexed(v.to_numpy(), ind2) * a
                else:
                    if isinstance(a, np.ndarray):
                        aa = a[:, np.newaxis]
                    else:
                        aa = a
                    x[ind1] += indexed(v.to_numpy(), ind2) * aa
                assert np.allclose(c.to_numpy(), x)
            except NotImplementedError:
                pass
            c[ind1].axpy(-a, v[ind2])
            assert len(c) == len(v)
            assert np.all(almost_equal(c, v))

    for ind in valid_inds(v):
        if v.len_ind(ind) != v.len_ind_unique(ind):
            continue

        for x in (1., 23., -4):
            c = v.copy()
            cc = v.copy()
            c[ind].axpy(x, c[ind])
            cc[ind].scal(1 + x)
            assert np.all(almost_equal(c, cc))
Example #49
0
def test_gram_schmidt_with_R(vector_array):
    U = vector_array

    V = U.copy()
    onb, R = gram_schmidt(U, return_R=True, copy=True)
    assert np.all(almost_equal(U, V))
    assert np.allclose(onb.dot(onb), np.eye(len(onb)))
    assert np.all(almost_equal(U, onb.lincomb(U.dot(onb)), rtol=1e-13))
    assert np.all(almost_equal(V, onb.lincomb(R.T)))

    onb2, R2 = gram_schmidt(U, return_R=True, copy=False)
    assert np.all(almost_equal(onb, onb2))
    assert np.all(R == R2)
    assert np.all(almost_equal(onb, U))
Example #50
0
def test_projected_2(operator_with_arrays):
    op, mu, U, V = operator_with_arrays
    op_U = op.projected(None, U)
    op_V = op.projected(V, None)
    op_U_V = op_U.projected(V, None)
    op_V_U = op_V.projected(None, U)
    op_UV = op.projected(V, U)
    np.random.seed(4711 + U.dim + len(V))
    W = NumpyVectorArray(np.random.random(len(U)), copy=False)
    Y0 = op_UV.apply(W, mu=mu)
    Y1 = op_U_V.apply(W, mu=mu)
    Y2 = op_V_U.apply(W, mu=mu)
    assert np.all(almost_equal(Y0, Y1))
    assert np.all(almost_equal(Y0, Y2))
Example #51
0
def test_project_2(operator_with_arrays):
    op, mu, U, V = operator_with_arrays
    op_U = project(op, None, U)
    op_V = project(op, V, None)
    op_U_V = project(op_U, V, None)
    op_V_U = project(op_V, None, U)
    op_UV = project(op, V, U)
    np.random.seed(4711 + U.dim + len(V))
    W = op_UV.source.make_array(np.random.random(len(U)))
    Y0 = op_UV.apply(W, mu=mu)
    Y1 = op_U_V.apply(W, mu=mu)
    Y2 = op_V_U.apply(W, mu=mu)
    assert np.all(almost_equal(Y0, Y1))
    assert np.all(almost_equal(Y0, Y2))
Example #52
0
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)))
Example #53
0
def test_projected_with_product_2(operator_with_arrays_and_products):
    op, mu, U, V, sp, rp = operator_with_arrays_and_products
    op_U = op.projected(None, U)
    op_V = op.projected(V, None, product=rp)
    op_U_V = op_U.projected(V, None, product=rp)
    op_V_U = op_V.projected(None, U)
    op_UV = op.projected(V, U, product=rp)
    np.random.seed(4711 + U.dim + len(V))
    W = op_UV.source.make_array(np.random.random(len(U)))
    Y0 = op_UV.apply(W, mu=mu)
    Y1 = op_U_V.apply(W, mu=mu)
    Y2 = op_V_U.apply(W, mu=mu)
    assert np.all(almost_equal(Y0, Y1))
    assert np.all(almost_equal(Y0, Y2))
Example #54
0
def test_gram_schmidt_biorth(vector_array):
    U = vector_array
    if U.dim < 2:
        return
    l = len(U) // 2
    l = min((l, U.dim - 1))
    if l < 1:
        return
    U1 = U[:l].copy()
    U2 = U[l:2 * l].copy()

    V1 = U1.copy()
    V2 = U2.copy()
    A1, A2 = gram_schmidt_biorth(U1, U2, copy=True)
    assert np.all(almost_equal(U1, V1))
    assert np.all(almost_equal(U2, V2))
    assert np.allclose(A2.dot(A1), np.eye(len(A1)))
    c = np.linalg.cond(A1.to_numpy()) * np.linalg.cond(A2.to_numpy())
    assert np.all(almost_equal(U1, A1.lincomb(U1.dot(A2)), rtol=c * 1e-14))
    assert np.all(almost_equal(U2, A2.lincomb(U2.dot(A1)), rtol=c * 1e-14))

    B1, B2 = gram_schmidt_biorth(U1, U2, copy=False)
    assert np.all(almost_equal(A1, B1))
    assert np.all(almost_equal(A2, B2))
    assert np.all(almost_equal(A1, U1))
    assert np.all(almost_equal(A2, U2))
Example #55
0
def test_gram_schmidt_biorth_with_product(operator_with_arrays_and_products):
    _, _, U, _, p, _ = operator_with_arrays_and_products
    if U.dim < 2:
        return
    l = len(U) // 2
    l = min((l, U.dim - 1))
    if l < 1:
        return
    U1 = U[:l].copy()
    U2 = U[l:2 * l].copy()

    V1 = U1.copy()
    V2 = U2.copy()
    A1, A2 = gram_schmidt_biorth(U1, U2, product=p, copy=True)
    assert np.all(almost_equal(U1, V1))
    assert np.all(almost_equal(U2, V2))
    assert np.allclose(p.apply2(A2, A1), np.eye(len(A1)))
    c = np.linalg.cond(A1.to_numpy()) * np.linalg.cond(p.apply(A2).to_numpy())
    assert np.all(almost_equal(U1, A1.lincomb(p.apply2(U1, A2)), rtol=c * 1e-14))
    assert np.all(almost_equal(U2, A2.lincomb(p.apply2(U2, A1)), rtol=c * 1e-14))

    B1, B2 = gram_schmidt_biorth(U1, U2, product=p, copy=False)
    assert np.all(almost_equal(A1, B1))
    assert np.all(almost_equal(A2, B2))
    assert np.all(almost_equal(A1, U1))
    assert np.all(almost_equal(A2, U2))
Example #56
0
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()
            with pytest.raises(Exception):
                c.replace(c, ind=ind1, o_ind=ind2, remove_from_other=False)
            c = v.copy()
            with pytest.raises(Exception):
                c.replace(c, ind=ind1, o_ind=ind2, remove_from_other=True)
            continue

        c = v.copy()
        with pytest.raises(Exception):
            c.replace(c, ind=ind1, o_ind=ind2, remove_from_other=True)

        c = v.copy()
        c.replace(c, ind=ind1, o_ind=ind2, remove_from_other=False)
        assert len(c) == len(v)
        assert c.dim == v.dim
        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()
            if NUMPY_INDEX_QUIRK and len(x) == 0 and hasattr(ind1, '__len__') and len(ind1) == 0:
                pass
            else:
                x[ind1] = indexed(dv, ind2)
            assert np.allclose(c.data, x)
Example #57
0
def test_copy_repeated_index(vector_array):
    v = vector_array
    if len(v) == 0:
        return
    ind = [int(len(vector_array) * 3 / 4)] * 2
    for deep in (True, False):
        c = v[ind].copy(deep)
        assert almost_equal(c[0], v[ind[0]])
        assert almost_equal(c[1], v[ind[0]])
        if hasattr(v, 'data'):
            assert indexed(v.data, ind).shape == c.data.shape
        c[0].scal(2.)
        assert almost_equal(c[1], v[ind[0]])
        assert c[0].l2_norm() == 2 * v[ind[0]].l2_norm()
        if hasattr(v, 'data'):
            assert indexed(v.data, ind).shape == c.data.shape
Example #58
0
def test_pickle_by_solving(discretization):
    d = discretization
    d2 = loads(dumps(d))
    d.disable_caching()
    d2.disable_caching()
    for mu in d.parameter_space.sample_randomly(3, seed=234):
        assert np.all(almost_equal(d.solve(mu), d2.solve(mu)))
Example #59
0
File: model.py Project: pymor/pymor
def test_pickle_by_solving(model):
    m = model
    m2 = loads(dumps(m))
    m.disable_caching()
    m2.disable_caching()
    for mu in m.parameter_space.sample_randomly(3, seed=234):
        assert np.all(almost_equal(m.solve(mu), m2.solve(mu)))
Example #60
0
File: basic.py Project: pymor/pymor
def extend_basis(U, basis, product=None, method='gram_schmidt', pod_modes=1, pod_orthonormalize=True, copy_U=True):
    assert method in ('trivial', 'gram_schmidt', 'pod')

    basis_length = len(basis)

    if method == 'trivial':
        remove = set()
        for i in range(len(U)):
            if np.any(almost_equal(U[i], basis)):
                remove.add(i)
        basis.append(U[[i for i in range(len(U)) if i not in remove]],
                     remove_from_other=(not copy_U))
    elif method == 'gram_schmidt':
        basis.append(U, remove_from_other=(not copy_U))
        gram_schmidt(basis, offset=basis_length, product=product, copy=False, check=False)
    elif method == 'pod':
        U_proj_err = U - basis.lincomb(U.inner(basis, product))

        basis.append(pod(U_proj_err, modes=pod_modes, product=product, orthonormalize=False)[0])

        if pod_orthonormalize:
            gram_schmidt(basis, offset=basis_length, product=product, copy=False, check=False)

    if len(basis) <= basis_length:
        raise ExtensionError