Commit c969345a authored by René Fritze's avatar René Fritze Committed by René Fritze

[tools] adds a short_hand for `compare_with_tolerance(x, y, operator.le)`

parent df167106
......@@ -4,6 +4,8 @@
import operator
import warnings
from functools import partial
import numpy as np
from pymor.core.defaults import defaults
......@@ -89,4 +91,7 @@ def compare_with_tolerance(x, y, comparison_op, rtol=1e-14, atol=1e-14):
"""
if comparison_op is operator.eq:
warnings.warn('Use float_cmp for float equality tests')
return comparison_op(x-y, atol + y * rtol)
\ No newline at end of file
return comparison_op(x-y, atol + y * rtol)
almost_less = partial(compare_with_tolerance, comparison_op=operator.le)
\ No newline at end of file
......@@ -16,7 +16,7 @@ from pymortests.fixtures.grid import hy_rect_or_tria_grid
from pymor.discretizers.builtin.grids.vtkio import write_vtk
from pymor.discretizers.builtin.quadratures import GaussQuadratures
from pymor.tools.deprecated import Deprecated
from pymor.tools.floatcmp import float_cmp, float_cmp_all, compare_with_tolerance
from pymor.tools.floatcmp import float_cmp, float_cmp_all, compare_with_tolerance, almost_less
from pymor.vectorarrays.numpy import NumpyVectorSpace
from pymor.tools import timing
......@@ -110,6 +110,8 @@ def test_compare_with_tolerance():
for (rtol, atol) in itertools.product(tol_range, tol_range):
msg = f'rtol: {rtol} | atol {atol}'
op = operator.le
assert almost_less(0., 1, rtol, atol), msg
assert almost_less(-1., -0., rtol, atol), msg
assert compare_with_tolerance(0., 1, op, rtol, atol), msg
assert compare_with_tolerance(-1., -0., op, rtol, atol), msg
assert compare_with_tolerance(-1., 1., op, rtol, atol), msg
......@@ -122,6 +124,7 @@ def test_compare_with_tolerance():
assert compare_with_tolerance(1., -1., op, rtol, atol), msg
assert compare_with_tolerance(atol, 0, op, rtol, atol), msg
assert not compare_with_tolerance(-inf, 0., op, rtol, atol), msg
with pytest.warns(Warning, match='Use float_cmp'):
compare_with_tolerance(0.0, 0.0, operator.eq)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment