当前位置: 首页>>代码示例>>Python>>正文


Python basic.almost_equal函数代码示例

本文整理汇总了Python中pymor.algorithms.basic.almost_equal函数的典型用法代码示例。如果您正苦于以下问题:Python almost_equal函数的具体用法?Python almost_equal怎么用?Python almost_equal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了almost_equal函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_scal

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)
开发者ID:renemilk,项目名称:pyMor,代码行数:34,代码来源:vectorarray.py

示例2: test_InverseAdjointOperator

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
开发者ID:pymor,项目名称:pymor,代码行数:26,代码来源:operators.py

示例3: test_assemble

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))
开发者ID:pymor,项目名称:pymor,代码行数:27,代码来源:operators.py

示例4: test_neg

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))
开发者ID:renemilk,项目名称:pyMor,代码行数:7,代码来源:vectorarray.py

示例5: test_scal

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
开发者ID:pymor,项目名称:pymor,代码行数:33,代码来源:vectorarray.py

示例6: test_almost_equal_incompatible

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)
开发者ID:tobiasleibner,项目名称:pymor,代码行数:7,代码来源:basic.py

示例7: test_rmul

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))
开发者ID:renemilk,项目名称:pyMor,代码行数:8,代码来源:vectorarray.py

示例8: test_apply

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)))
开发者ID:pymor,项目名称:pymor,代码行数:9,代码来源:operators.py

示例9: test_identity_lincomb

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))
开发者ID:pymor,项目名称:pymor,代码行数:9,代码来源:operators.py

示例10: test_almost_equal_wrong_ind

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)
开发者ID:tobiasleibner,项目名称:pymor,代码行数:10,代码来源:basic.py

示例11: test_axpy_self

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))
开发者ID:nsrishankar,项目名称:pymor,代码行数:55,代码来源:vectorarray.py

示例12: test_add

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))
开发者ID:renemilk,项目名称:pyMor,代码行数:11,代码来源:vectorarray.py

示例13: test_sub

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))
开发者ID:renemilk,项目名称:pyMor,代码行数:11,代码来源:vectorarray.py

示例14: test_add

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))
开发者ID:nsrishankar,项目名称:pymor,代码行数:11,代码来源:vectorarray.py

示例15: test_apply_adjoint

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)))
开发者ID:sdrave,项目名称:pymor,代码行数:12,代码来源:operators.py


注:本文中的pymor.algorithms.basic.almost_equal函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。