Reference documentation for deal.II version 9.5.0
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
floating_point_comparator.h
Go to the documentation of this file.
1// ---------------------------------------------------------------------
2//
3// Copyright (C) 1998 - 2023 by the deal.II authors
4//
5// This file is part of the deal.II library.
6//
7// The deal.II library is free software; you can use it, redistribute
8// it, and/or modify it under the terms of the GNU Lesser General
9// Public License as published by the Free Software Foundation; either
10// version 2.1 of the License, or (at your option) any later version.
11// The full text of the license can be found in the file LICENSE.md at
12// the top level directory of deal.II.
13//
14// ---------------------------------------------------------------------
15
16#ifndef dealii_base_floating_point_copmerator_h
17#define dealii_base_floating_point_copmerator_h
18
19#include <deal.II/base/config.h>
20
21#include <deal.II/base/table.h>
22#include <deal.II/base/tensor.h>
24
25#include <bitset>
26#include <vector>
27
29
43template <typename Number>
45{
47 typename ::internal::VectorizedArrayTrait<Number>::value_type;
48 static constexpr std::size_t width =
50
57 {
58 less,
59 equal,
61 };
62
68 const bool use_absolute_tolerance = true,
69 const std::bitset<width> &mask = std::bitset<width>().flip());
70
72
74
81 template <typename T>
82 bool
83 operator()(const T &object1, const T &object2) const;
84
90 template <typename T>
92 compare(const std::vector<T> &v1, const std::vector<T> &v2) const;
93
97 template <std::size_t dim, typename T>
99 compare(const std::array<T, dim> &t1, const std::array<T, dim> &t2) const;
100
104 template <int rank, int dim, typename T>
106 compare(const Tensor<rank, dim, T> &t1, const Tensor<rank, dim, T> &t2) const;
107
111 template <typename T>
113 compare(const Table<2, T> &t1, const Table<2, T> &t2) const;
114
119 compare(const ScalarNumber s1, const ScalarNumber s2) const;
120
127
128private:
131 const std::bitset<width> mask;
132};
133
134
135/* ------------------------------------------------------------------ */
136
137
138template <typename Number>
140 const ScalarNumber tolerance,
141 const bool use_absolute_tolerance,
142 const std::bitset<width> &mask)
143 : tolerance(tolerance)
144 , use_absolute_tolerance(use_absolute_tolerance)
145 , mask(mask)
146{
147 Assert(mask.count() > 0, ExcMessage("No component selected"));
148}
149
150
151
152template <typename Number>
153template <typename T>
154bool
156 const T &object2) const
157{
158 return compare(object1, object2) == ComparisonResult::less;
159}
160
161
162
163template <typename Number>
164template <typename T>
167 const std::vector<T> &v2) const
168{
169 const unsigned int s1 = v1.size(), s2 = v2.size();
170 if (s1 < s2)
171 return ComparisonResult::less;
172 else if (s1 > s2)
173 return ComparisonResult::greater;
174 else
175 for (unsigned int i = 0; i < s1; ++i)
176 {
177 const ComparisonResult result = compare(v1[i], v2[i]);
178 if (result != ComparisonResult::equal)
179 return result;
180 }
181 return ComparisonResult::equal;
182}
183
184
185
186template <typename Number>
187template <std::size_t dim, typename T>
189FloatingPointComparator<Number>::compare(const std::array<T, dim> &t1,
190 const std::array<T, dim> &t2) const
191{
192 for (unsigned int i = 0; i < t1.size(); ++i)
193 {
194 const ComparisonResult result = compare(t1[i], t2[i]);
195 if (result != ComparisonResult::equal)
196 return result;
197 }
198 return ComparisonResult::equal;
199}
200
201
202
203template <typename Number>
204template <int rank, int dim, typename T>
207 const Tensor<rank, dim, T> &t2) const
208{
209 for (unsigned int i = 0; i < dim; ++i)
210 {
211 const ComparisonResult result = compare(t1[i], t2[i]);
212 if (result != ComparisonResult::equal)
213 return result;
214 }
215 return ComparisonResult::equal;
216}
217
218
219
220template <typename Number>
221template <typename T>
224 const Table<2, T> &t2) const
225{
226 AssertDimension(t1.size(0), t2.size(0));
227 AssertDimension(t1.size(1), t2.size(1));
228
229 for (unsigned int i = 0; i < t1.size(0); ++i)
230 for (unsigned int j = 0; j < t1.size(1); ++j)
231 {
232 const ComparisonResult result = compare(t1[i][j], t2[i][j]);
233 if (result != ComparisonResult::equal)
234 return result;
235 }
236 return ComparisonResult::equal;
237}
238
239
240
241template <typename Number>
244 const ScalarNumber s2) const
245{
246 if (width == 1 || mask[0])
247 {
248 const ScalarNumber tolerance =
249 use_absolute_tolerance ?
250 this->tolerance :
251 ((std::abs(s1) + std::abs(s2)) * this->tolerance);
252
253 if (s1 < s2 - tolerance)
254 return ComparisonResult::less;
255 else if (s1 > s2 + tolerance)
256 return ComparisonResult::greater;
257 }
258
259 return ComparisonResult::equal;
260}
261
262template <typename Number>
267{
268 for (unsigned int i = 0; i < width; ++i)
269 if (mask[i])
270 {
271 const ComparisonResult result = compare(v1[i], v2[i]);
272 if (result != ComparisonResult::equal)
273 return result;
274 }
275
276 return ComparisonResult::equal;
277}
278
279
281
282#endif
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:472
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:473
const unsigned int v1
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
static ::ExceptionBase & ExcMessage(std::string arg1)
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
bool operator()(const T &object1, const T &object2) const
FloatingPointComparator(const FloatingPointComparator &rhs)=default
const std::bitset< width > mask
FloatingPointComparator(FloatingPointComparator &&rhs) noexcept=default
static constexpr std::size_t width
typename ::internal::VectorizedArrayTrait< Number >::value_type ScalarNumber
ComparisonResult compare(const std::vector< T > &v1, const std::vector< T > &v2) const
FloatingPointComparator(const ScalarNumber tolerance, const bool use_absolute_tolerance=true, const std::bitset< width > &mask=std::bitset< width >().flip())
static constexpr std::size_t width()