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

[tools] adds a compare func with tolerances

parent c1b44cbe
......@@ -2,6 +2,8 @@
# Copyright 2013-2020 pyMOR developers and contributors. All rights reserved.
# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)
import operator
import warnings
import numpy as np
from pymor.core.defaults import defaults
......@@ -85,3 +87,27 @@ def contains_zero_vector(vector_array, rtol=None, atol=None):
if float_cmp_all(vec, zero, rtol, atol):
return True
return False
@defaults('rtol', 'atol')
def compare_with_tolerance(x, y, comparison_op, rtol=1e-14, atol=1e-14):
""" 'One-sided' Comparison x and y component-wise with given comparison op.
For scalars we define almost equality as ::
compare_with_tolerance(x,y) <=> op(x - y, atol + y*rtol)
Parameters
----------
x, y
|NumPy arrays| to be compared. Have to be broadcastable to the same shape.
comparison_op
binary operator object, see |operator| module.
rtol
The relative tolerance.
atol
The absolute tolerance.
"""
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
# This file is part of the pyMOR project (http://www.pymor.org).
# Copyright 2013-2020 pyMOR developers and contributors. All rights reserved.
# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)
import operator
from math import sin, pi, exp, factorial
import numpy as np
import pytest
......@@ -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
from pymor.tools.floatcmp import float_cmp, float_cmp_all, compare_with_tolerance
from pymor.vectorarrays.numpy import NumpyVectorSpace
from pymor.tools import timing
......@@ -103,6 +103,29 @@ def test_float_cmp():
assert not float_cmp(-inf, inf, rtol, atol), msg
def test_compare_with_tolerance():
tol_range = [0.0, 1e-8, 1]
nan = float('nan')
inf = float('inf')
for (rtol, atol) in itertools.product(tol_range, tol_range):
msg = f'rtol: {rtol} | atol {atol}'
op = operator.le
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
assert compare_with_tolerance(0., atol, op, rtol, atol), msg
assert (rtol==0.0 and not compare_with_tolerance(0., inf, op, rtol, atol), msg) or \
compare_with_tolerance(0., inf, op, rtol, atol), msg
op = operator.ge
assert compare_with_tolerance(1., 0., op, rtol, atol), msg
assert compare_with_tolerance(-0., -1., op, rtol, atol), msg
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)
@given(hy_rect_or_tria_grid)
def test_vtkio(grid):
steps = 4
......
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