preCICE v3.1.2
Loading...
Searching...
No Matches
differences.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <Eigen/Core>
4#include "utils/assertion.hpp"
5
6namespace precice {
7namespace math {
8
9constexpr double NUMERICAL_ZERO_DIFFERENCE = 1.0e-14;
10
12template <class DerivedA, class DerivedB>
13constexpr bool equals(const Eigen::MatrixBase<DerivedA> &A,
14 const Eigen::MatrixBase<DerivedB> &B,
15 double tolerance = NUMERICAL_ZERO_DIFFERENCE)
16{
17 return A.isApprox(B, tolerance);
18}
19
21template <class Scalar>
22typename std::enable_if<std::is_arithmetic<Scalar>::value, bool>::type equals(const Scalar a, const Scalar b, const Scalar tolerance = NUMERICAL_ZERO_DIFFERENCE)
23{
24 return std::abs(a - b) <= tolerance;
25}
26
27template <class DerivedA, class DerivedB>
28bool oneGreater(const Eigen::MatrixBase<DerivedA> &A,
29 const Eigen::MatrixBase<DerivedB> &B,
30 double tolerance = math::NUMERICAL_ZERO_DIFFERENCE)
31{
32 PRECICE_ASSERT(A.rows() == B.rows(), "Matrices with different number of rows can't be compared.");
33 PRECICE_ASSERT(A.cols() == B.cols(), "Matrices with different number of cols can't be compared.");
34
35 return ((A - B).array() > tolerance).any();
36}
37
38template <class DerivedA, class DerivedB>
39bool oneGreaterEquals(const Eigen::MatrixBase<DerivedA> &A,
40 const Eigen::MatrixBase<DerivedB> &B,
41 double tolerance = math::NUMERICAL_ZERO_DIFFERENCE)
42{
43 PRECICE_ASSERT(A.rows() == B.rows(), "Matrices with different number of rows can't be compared.");
44 PRECICE_ASSERT(A.cols() == B.cols(), "Matrices with different number of cols can't be compared.");
45
46 return ((A - B).array() >= -tolerance).any();
47}
48
49template <class DerivedA, class DerivedB>
50bool allGreater(const Eigen::MatrixBase<DerivedA> &A,
51 const Eigen::MatrixBase<DerivedB> &B,
52 double tolerance = math::NUMERICAL_ZERO_DIFFERENCE)
53{
54 PRECICE_ASSERT(A.rows() == B.rows(), "Matrices with different number of rows can't be compared.");
55 PRECICE_ASSERT(A.cols() == B.cols(), "Matrices with different number of cols can't be compared.");
56
57 return ((A - B).array() > tolerance).all();
58}
59
60template <class DerivedA, class DerivedB>
61bool allGreaterEquals(const Eigen::MatrixBase<DerivedA> &A,
62 const Eigen::MatrixBase<DerivedB> &B,
63 double tolerance = math::NUMERICAL_ZERO_DIFFERENCE)
64{
65 PRECICE_ASSERT(A.rows() == B.rows(), "Matrices with different number of rows can't be compared.");
66 PRECICE_ASSERT(A.cols() == B.cols(), "Matrices with different number of cols can't be compared.");
67
68 return ((A - B).array() >= tolerance).all();
69}
70
71template <class Scalar>
72typename std::enable_if<std::is_arithmetic<Scalar>::value, bool>::type greater(Scalar A, Scalar B, Scalar tolerance = NUMERICAL_ZERO_DIFFERENCE)
73{
74 return A - B > tolerance;
75}
76
77template <class Scalar>
78typename std::enable_if<std::is_arithmetic<Scalar>::value, bool>::type greaterEquals(Scalar A, Scalar B, Scalar tolerance = NUMERICAL_ZERO_DIFFERENCE)
79{
80 return A - B >= -tolerance;
81}
82
83template <class Scalar>
84typename std::enable_if<std::is_arithmetic<Scalar>::value, bool>::type smaller(Scalar A, Scalar B, Scalar tolerance = NUMERICAL_ZERO_DIFFERENCE)
85{
86 return A - B < -tolerance;
87}
88
89template <class Scalar>
90typename std::enable_if<std::is_arithmetic<Scalar>::value, bool>::type smallerEquals(Scalar A, Scalar B, Scalar tolerance = NUMERICAL_ZERO_DIFFERENCE)
91{
92 return A - B <= tolerance;
93}
94
95} // namespace math
96} // namespace precice
#define PRECICE_ASSERT(...)
Definition assertion.hpp:87
constexpr bool equals(const Eigen::MatrixBase< DerivedA > &A, const Eigen::MatrixBase< DerivedB > &B, double tolerance=NUMERICAL_ZERO_DIFFERENCE)
Compares two Eigen::MatrixBase for equality up to tolerance.
bool oneGreaterEquals(const Eigen::MatrixBase< DerivedA > &A, const Eigen::MatrixBase< DerivedB > &B, double tolerance=math::NUMERICAL_ZERO_DIFFERENCE)
std::enable_if< std::is_arithmetic< Scalar >::value, bool >::type smallerEquals(Scalar A, Scalar B, Scalar tolerance=NUMERICAL_ZERO_DIFFERENCE)
constexpr double NUMERICAL_ZERO_DIFFERENCE
std::enable_if< std::is_arithmetic< Scalar >::value, bool >::type greaterEquals(Scalar A, Scalar B, Scalar tolerance=NUMERICAL_ZERO_DIFFERENCE)
std::enable_if< std::is_arithmetic< Scalar >::value, bool >::type smaller(Scalar A, Scalar B, Scalar tolerance=NUMERICAL_ZERO_DIFFERENCE)
bool allGreater(const Eigen::MatrixBase< DerivedA > &A, const Eigen::MatrixBase< DerivedB > &B, double tolerance=math::NUMERICAL_ZERO_DIFFERENCE)
bool allGreaterEquals(const Eigen::MatrixBase< DerivedA > &A, const Eigen::MatrixBase< DerivedB > &B, double tolerance=math::NUMERICAL_ZERO_DIFFERENCE)
bool oneGreater(const Eigen::MatrixBase< DerivedA > &A, const Eigen::MatrixBase< DerivedB > &B, double tolerance=math::NUMERICAL_ZERO_DIFFERENCE)
std::enable_if< std::is_arithmetic< Scalar >::value, bool >::type greater(Scalar A, Scalar B, Scalar tolerance=NUMERICAL_ZERO_DIFFERENCE)
Main namespace of the precice library.