Reference documentation for deal.II version GIT acb749f4f5 2024-02-21 16:10:02+00:00
\(\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
22#include <deal.II/base/table.h>
23#include <deal.II/base/tensor.h>
25
26#include <bitset>
27#include <vector>
28
30
44template <typename Number>
46{
48 typename ::internal::VectorizedArrayTrait<Number>::value_type;
49 static constexpr std::size_t width =
51
58 {
59 less,
60 equal,
62 };
63
69 const bool use_absolute_tolerance = true,
70 const std::bitset<width> &mask = std::bitset<width>().flip());
71
73
75
82 template <typename T>
83 bool
84 operator()(const T &object1, const T &object2) const;
85
91 template <typename T>
93 compare(const std::vector<T> &v1, const std::vector<T> &v2) const;
94
98 template <std::size_t dim, typename T>
100 compare(const std::array<T, dim> &t1, const std::array<T, dim> &t2) const;
101
105 template <int rank, int dim, typename T>
107 compare(const Tensor<rank, dim, T> &t1, const Tensor<rank, dim, T> &t2) const;
108
112 template <int rank, int dim, int spacedim, typename T>
116
120 template <typename T>
122 compare(const Table<2, T> &t1, const Table<2, T> &t2) const;
123
128 compare(const ScalarNumber s1, const ScalarNumber s2) const;
129
136
137private:
140 const std::bitset<width> mask;
141};
142
143
144/* ------------------------------------------------------------------ */
145
146
147template <typename Number>
149 const ScalarNumber tolerance,
150 const bool use_absolute_tolerance,
151 const std::bitset<width> &mask)
152 : tolerance(tolerance)
153 , use_absolute_tolerance(use_absolute_tolerance)
154 , mask(mask)
155{
156 Assert(mask.count() > 0, ExcMessage("No component selected"));
157}
158
159
160
161template <typename Number>
162template <typename T>
163bool
165 const T &object2) const
166{
167 return compare(object1, object2) == ComparisonResult::less;
168}
169
170
171
172template <typename Number>
173template <typename T>
176 const std::vector<T> &v2) const
177{
178 const unsigned int s1 = v1.size(), s2 = v2.size();
179 if (s1 < s2)
180 return ComparisonResult::less;
181 else if (s1 > s2)
182 return ComparisonResult::greater;
183 else
184 for (unsigned int i = 0; i < s1; ++i)
185 {
186 const ComparisonResult result = compare(v1[i], v2[i]);
187 if (result != ComparisonResult::equal)
188 return result;
189 }
190 return ComparisonResult::equal;
191}
192
193
194
195template <typename Number>
196template <std::size_t dim, typename T>
198FloatingPointComparator<Number>::compare(const std::array<T, dim> &t1,
199 const std::array<T, dim> &t2) const
200{
201 for (unsigned int i = 0; i < t1.size(); ++i)
202 {
203 const ComparisonResult result = compare(t1[i], t2[i]);
204 if (result != ComparisonResult::equal)
205 return result;
206 }
207 return ComparisonResult::equal;
208}
209
210
211
212template <typename Number>
213template <int rank, int dim, typename T>
216 const Tensor<rank, dim, T> &t2) const
217{
218 for (unsigned int i = 0; i < dim; ++i)
219 {
220 const ComparisonResult result = compare(t1[i], t2[i]);
221 if (result != ComparisonResult::equal)
222 return result;
223 }
224 return ComparisonResult::equal;
225}
226
227
228
229template <typename Number>
230template <int rank, int dim, int spacedim, typename T>
235{
236 for (unsigned int i = 0; i < dim; ++i)
237 {
238 const ComparisonResult result = compare(t1[i], t2[i]);
239 if (result != ComparisonResult::equal)
240 return result;
241 }
242 return ComparisonResult::equal;
243}
244
245
246
247template <typename Number>
248template <typename T>
251 const Table<2, T> &t2) const
252{
253 AssertDimension(t1.size(0), t2.size(0));
254 AssertDimension(t1.size(1), t2.size(1));
255
256 for (unsigned int i = 0; i < t1.size(0); ++i)
257 for (unsigned int j = 0; j < t1.size(1); ++j)
258 {
259 const ComparisonResult result = compare(t1[i][j], t2[i][j]);
260 if (result != ComparisonResult::equal)
261 return result;
262 }
263 return ComparisonResult::equal;
264}
265
266
267
268template <typename Number>
271 const ScalarNumber s2) const
272{
273 if (width == 1 || mask[0])
274 {
275 const ScalarNumber tolerance =
276 use_absolute_tolerance ?
277 this->tolerance :
278 ((std::abs(s1) + std::abs(s2)) * this->tolerance);
279
280 if (s1 < s2 - tolerance)
281 return ComparisonResult::less;
282 else if (s1 > s2 + tolerance)
283 return ComparisonResult::greater;
284 }
285
286 return ComparisonResult::equal;
287}
288
289template <typename Number>
294{
295 for (unsigned int i = 0; i < width; ++i)
296 if (mask[i])
297 {
298 const ComparisonResult result = compare(v1[i], v2[i]);
299 if (result != ComparisonResult::equal)
300 return result;
301 }
302
303 return ComparisonResult::equal;
304}
305
306
308
309#endif
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:503
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:504
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()