Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
P
pymor
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Requirements
Requirements
List
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Insights
Issue
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
pyMOR
pymor
Commits
b27b9ec8
Commit
b27b9ec8
authored
Apr 28, 2020
by
René Fritze
Committed by
René Fritze
Jun 26, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[tools] adds a compare func with tolerances
parent
c1b44cbe
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
2 deletions
+51
-2
src/pymor/tools/floatcmp.py
src/pymor/tools/floatcmp.py
+26
-0
src/pymortests/tools.py
src/pymortests/tools.py
+25
-2
No files found.
src/pymor/tools/floatcmp.py
View file @
b27b9ec8
...
...
@@ -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
src/pymortests/tools.py
View file @
b27b9ec8
# 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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment