preCICE v3.2.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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::math {
7
8constexpr double NUMERICAL_ZERO_DIFFERENCE = 1.0e-14;
9
11template <class DerivedA, class DerivedB>
12constexpr bool equals(const Eigen::MatrixBase<DerivedA> &A,
13 const Eigen::MatrixBase<DerivedB> &B,
14 double tolerance = NUMERICAL_ZERO_DIFFERENCE)
15{
16 return A.isApprox(B, tolerance);
17}
18
20template <class Scalar>
21typename std::enable_if<std::is_arithmetic<Scalar>::value, bool>::type equals(const Scalar a, const Scalar b, const Scalar tolerance = NUMERICAL_ZERO_DIFFERENCE)
22{
23 return std::abs(a - b) <= tolerance;
24}
25
26template <class DerivedA, class DerivedB>
27bool oneGreater(const Eigen::MatrixBase<DerivedA> &A,
28 const Eigen::MatrixBase<DerivedB> &B,
29 double tolerance = math::NUMERICAL_ZERO_DIFFERENCE)
30{
31 PRECICE_ASSERT(A.rows() == B.rows(), "Matrices with different number of rows can't be compared.");
32 PRECICE_ASSERT(A.cols() == B.cols(), "Matrices with different number of cols can't be compared.");
33
34 return ((A - B).array() > tolerance).any();
35}
36
37template <class DerivedA, class DerivedB>
38bool oneGreaterEquals(const Eigen::MatrixBase<DerivedA> &A,
39 const Eigen::MatrixBase<DerivedB> &B,
40 double tolerance = math::NUMERICAL_ZERO_DIFFERENCE)
41{
42 PRECICE_ASSERT(A.rows() == B.rows(), "Matrices with different number of rows can't be compared.");
43 PRECICE_ASSERT(A.cols() == B.cols(), "Matrices with different number of cols can't be compared.");
44
45 return ((A - B).array() >= -tolerance).any();
46}
47
48template <class DerivedA, class DerivedB>
49bool allGreater(const Eigen::MatrixBase<DerivedA> &A,
50 const Eigen::MatrixBase<DerivedB> &B,
51 double tolerance = math::NUMERICAL_ZERO_DIFFERENCE)
52{
53 PRECICE_ASSERT(A.rows() == B.rows(), "Matrices with different number of rows can't be compared.");
54 PRECICE_ASSERT(A.cols() == B.cols(), "Matrices with different number of cols can't be compared.");
55
56 return ((A - B).array() > tolerance).all();
57}
58
59template <class DerivedA, class DerivedB>
60bool allGreaterEquals(const Eigen::MatrixBase<DerivedA> &A,
61 const Eigen::MatrixBase<DerivedB> &B,
62 double tolerance = math::NUMERICAL_ZERO_DIFFERENCE)
63{
64 PRECICE_ASSERT(A.rows() == B.rows(), "Matrices with different number of rows can't be compared.");
65 PRECICE_ASSERT(A.cols() == B.cols(), "Matrices with different number of cols can't be compared.");
66
67 return ((A - B).array() >= tolerance).all();
68}
69
70template <class Scalar>
71typename std::enable_if<std::is_arithmetic<Scalar>::value, bool>::type greater(Scalar A, Scalar B, Scalar tolerance = NUMERICAL_ZERO_DIFFERENCE)
72{
73 return A - B > tolerance;
74}
75
76template <class Scalar>
77typename std::enable_if<std::is_arithmetic<Scalar>::value, bool>::type greaterEquals(Scalar A, Scalar B, Scalar tolerance = NUMERICAL_ZERO_DIFFERENCE)
78{
79 return A - B >= -tolerance;
80}
81
82template <class Scalar>
83typename std::enable_if<std::is_arithmetic<Scalar>::value, bool>::type smaller(Scalar A, Scalar B, Scalar tolerance = NUMERICAL_ZERO_DIFFERENCE)
84{
85 return A - B < -tolerance;
86}
87
88template <class Scalar>
89typename std::enable_if<std::is_arithmetic<Scalar>::value, bool>::type smallerEquals(Scalar A, Scalar B, Scalar tolerance = NUMERICAL_ZERO_DIFFERENCE)
90{
91 return A - B <= tolerance;
92}
93
94} // namespace precice::math
#define PRECICE_ASSERT(...)
Definition assertion.hpp:85
T make_unique(T... args)
provides general mathematical constants and functions.
Definition barycenter.cpp:9
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)