本文整理汇总了Python中pymor.algorithms.basic.almost_equal函数的典型用法代码示例。如果您正苦于以下问题:Python almost_equal函数的具体用法?Python almost_equal怎么用?Python almost_equal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了almost_equal函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
示例16: test_gram_schmidt_with_product
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))
开发者ID:pymor,项目名称:pymor,代码行数:12,代码来源:gram_schmidt.py
示例17: test_gram_schmidt
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))
开发者ID:pymor,项目名称:pymor,代码行数:12,代码来源:gram_schmidt.py
示例18: test_axpy_self
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))
开发者ID:pymor,项目名称:pymor,代码行数:51,代码来源:vectorarray.py
示例19: test_projected_2
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))
开发者ID:nsrishankar,项目名称:pymor,代码行数:14,代码来源:operators.py
示例20: test_project_2
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))
开发者ID:pymor,项目名称:pymor,代码行数:14,代码来源:operators.py
注:本文中的pymor.algorithms.basic.almost_equal函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论