deal.II version GIT relicensing-6834-g5b78e6bcdf 2026-10-01 11:20:01+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// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2022 - 2024 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_base_floating_point_copmerator_h
14#define dealii_base_floating_point_copmerator_h
15
16#include <deal.II/base/config.h>
17
19#include <deal.II/base/table.h>
20#include <deal.II/base/tensor.h>
22
23#include <bitset>
24#include <vector>
25
27
41template <typename Number>
43{
45 typename ::internal::VectorizedArrayTrait<Number>::value_type;
46 static constexpr std::size_t width =
48
55 {
56 less,
57 equal,
59 };
60
66 const bool use_absolute_tolerance = true,
67 const std::bitset<width> &mask = std::bitset<width>().flip());
68
70
72
79 template <typename T>
80 bool
81 operator()(const T &object1, const T &object2) const;
82
88 template <typename T>
90 compare(const std::vector<T> &v1, const std::vector<T> &v2) const;
91
95 template <std::size_t dim, typename T>
97 compare(const std::array<T, dim> &t1, const std::array<T, dim> &t2) const;
98
102 template <int rank, int dim, typename T>
104 compare(const Tensor<rank, dim, T> &t1, const Tensor<rank, dim, T> &t2) const;
105
109 template <int rank, int dim, int spacedim, typename T>
113
117 template <typename T>
119 compare(const Table<2, T> &t1, const Table<2, T> &t2) const;
120
125 compare(const ScalarNumber s1, const ScalarNumber s2) const;
126
133
134private:
137 const std::bitset<width> mask;
138};
139
140
141/* ------------------------------------------------------------------ */
142
143
144template <typename Number>
146 const ScalarNumber tolerance,
147 const bool use_absolute_tolerance,
148 const std::bitset<width> &mask)
149 : tolerance(tolerance)
150 , use_absolute_tolerance(use_absolute_tolerance)
151 , mask(mask)
152{
153 Assert(mask.count() > 0, ExcMessage("No component selected"));
154}
155
156
157
158template <typename Number>
159template <typename T>
160bool
162 const T &object2) const
163{
164 return compare(object1, object2) == ComparisonResult::less;
165}
166
167
168
169template <typename Number>
170template <typename T>
173 const std::vector<T> &v2) const
174{
175 const unsigned int s1 = v1.size(), s2 = v2.size();
176 if (s1 < s2)
177 return ComparisonResult::less;
178 else if (s1 > s2)
179 return ComparisonResult::greater;
180 else
181 for (unsigned int i = 0; i < s1; ++i)
182 {
183 const ComparisonResult result = compare(v1[i], v2[i]);
184 if (result != ComparisonResult::equal)
185 return result;
186 }
187 return ComparisonResult::equal;
188}
189
190
191
192template <typename Number>
193template <std::size_t dim, typename T>
195FloatingPointComparator<Number>::compare(const std::array<T, dim> &t1,
196 const std::array<T, dim> &t2) const
197{
198 for (unsigned int i = 0; i < t1.size(); ++i)
199 {
200 const ComparisonResult result = compare(t1[i], t2[i]);
201 if (result != ComparisonResult::equal)
202 return result;
203 }
204 return ComparisonResult::equal;
205}
206
207
208
209template <typename Number>
210template <int rank, int dim, typename T>
213 const Tensor<rank, dim, T> &t2) const
214{
215 for (unsigned int i = 0; i < dim; ++i)
216 {
217 const ComparisonResult result = compare(t1[i], t2[i]);
218 if (result != ComparisonResult::equal)
219 return result;
220 }
221 return ComparisonResult::equal;
222}
223
224
225
226template <typename Number>
227template <int rank, int dim, int spacedim, typename T>
232{
233 for (unsigned int i = 0; i < spacedim; ++i)
234 {
235 const ComparisonResult result = compare(t1[i], t2[i]);
236 if (result != ComparisonResult::equal)
237 return result;
238 }
239 return ComparisonResult::equal;
240}
241
242
243
244template <typename Number>
245template <typename T>
248 const Table<2, T> &t2) const
249{
250 AssertDimension(t1.size(0), t2.size(0));
251 AssertDimension(t1.size(1), t2.size(1));
252
253 for (unsigned int i = 0; i < t1.size(0); ++i)
254 for (unsigned int j = 0; j < t1.size(1); ++j)
255 {
256 const ComparisonResult result = compare(t1[i][j], t2[i][j]);
257 if (result != ComparisonResult::equal)
258 return result;
259 }
260 return ComparisonResult::equal;
261}
262
263
264
265template <typename Number>
268 const ScalarNumber s2) const
269{
270 if (width == 1 || mask[0])
271 {
272 const ScalarNumber tolerance =
273 use_absolute_tolerance ?
274 this->tolerance :
275 ((std::abs(s1) + std::abs(s2)) * this->tolerance);
276
277 if (s1 < s2 - tolerance)
278 return ComparisonResult::less;
279 else if (s1 > s2 + tolerance)
280 return ComparisonResult::greater;
281 }
282
283 return ComparisonResult::equal;
284}
285
286template <typename Number>
291{
292 for (unsigned int i = 0; i < width; ++i)
293 if (mask[i])
294 {
295 const ComparisonResult result = compare(v1[i], v2[i]);
296 if (result != ComparisonResult::equal)
297 return result;
298 }
299
300 return ComparisonResult::equal;
301}
302
303
305
306#endif
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
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()