Debian Codegraph @

include/deal.II/base/numbers.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2006 - 2025 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_numbers_h
16 #define dealii_numbers_h
17
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/base/types.h>
22
23 #include <cmath>
24 #include <complex>
25 #include <cstddef>
26 #include <type_traits>
27
28 // Forward-declare the automatic differentiation types so we can add prototypes
29 // for our own wrappers.
30 #ifdef DEAL_II_WITH_ADOLC
31 class adouble;
32 namespace adtl
33 {
34 class adouble;
35 }
36 #endif
37
38 DEAL_II_NAMESPACE_OPEN
39
40 namespace internal
41 {
42 /**
43 * A helper class specifying the maximal vector length of VectorizedArray
44 * for a specified data type Number for the given processor architecture and
45 * optimization level.
46 *
47 * The value of the maximal vector length is used as default template
48 * argument in VectorizedArray, such that VectorizedArray<Number> is
49 * equivalent to VectorizedArray<Number,
50 * VectorizedArrayWidthSpecifier<Number>::max_width>.
51 *
52 * @note This class is the default implementation for data types for which
53 * no vectorization is supported.
54 *
55 * @tparam Number The underlying data type for which one wants to find out
56 * the maximal length of hardware supported vectors.
57 */
58 template <typename Number>
59 struct VectorizedArrayWidthSpecifier
60 {
61 /**
62 * Maximal vector length of VectorizedArray for an arbitrary type.
63 */
64 constexpr static unsigned int max_width = 1;
65 };
66
67 /**
68 * A helper class specifying the maximal vector length of VectorizedArray
69 * for the data type `double` for the given processor architecture and
70 * optimization level. For a detailed description of supported maximal vector
71 * lengths, see the documentation of VectorizedArray.
72 */
73 template <>
74 struct VectorizedArrayWidthSpecifier<double>
75 {
76 /**
77 * Maximal vector length of VectorizedArray for double.
78 */
79 constexpr static unsigned int max_width =
80 #if DEAL_II_VECTORIZATION_WIDTH_IN_BITS >= 512
81 8;
82 #elif DEAL_II_VECTORIZATION_WIDTH_IN_BITS >= 256
83 4;
84 #elif DEAL_II_VECTORIZATION_WIDTH_IN_BITS >= 128
85 2;
86 #else
87 1;
88 #endif
89 };
90
91 /**
92 * A helper class specifying the maximal vector length of VectorizedArray
93 * for the data type `float` for the given processor architecture and
94 * optimization level. For a detailed description of supported maximal vector
95 * lengths, see the documentation of VectorizedArray.
96 */
97 template <>
98 struct VectorizedArrayWidthSpecifier<float>
99 {
100 /**
101 * Maximal vector length of VectorizedArray for float.
102 */
103 constexpr static unsigned int max_width =
104 #if DEAL_II_VECTORIZATION_WIDTH_IN_BITS >= 128 && defined(__ALTIVEC__)
105 4;
106 #elif DEAL_II_VECTORIZATION_WIDTH_IN_BITS >= 512 && defined(__AVX512F__)
107 16;
108 #elif DEAL_II_VECTORIZATION_WIDTH_IN_BITS >= 256 && defined(__AVX__)
109 8;
110 #elif DEAL_II_VECTORIZATION_WIDTH_IN_BITS >= 128 && defined(__SSE2__)
111 4;
112 #elif DEAL_II_VECTORIZATION_WIDTH_IN_BITS >= 128 && defined(__ARM_NEON)
113 4;
114 #else
115 1;
116 #endif
117 };
118
119
120 } // namespace internal
121
122 // forward declarations to support abs or sqrt operations on VectorizedArray
123 #ifndef DOXYGEN
124 template <typename Number,
125 std::size_t width =
126 internal::VectorizedArrayWidthSpecifier<Number>::max_width>
127 class VectorizedArray;
128 template <typename T>
129 struct EnableIfScalar;
130 #endif
131
132 #ifdef DEAL_II_WITH_ADOLC
133 # ifndef DOXYGEN
134 // Prototype some inline functions present in adolc_math.h for use in
135 // NumberTraits.
136 //
137 // ADOL-C uses fabs(), but for genericity we want to use abs(). Simultaneously,
138 // though, we don't want to include ADOL-C headers in this header since
139 // numbers.h is in everything. To get around this: use C++ rules which permit
140 // the use of forward-declared classes in function prototypes to declare some
141 // functions which are defined in adolc_math.h. This permits us to write "using
142 // dealii::abs;" in NumberTraits which will allow us to select the correct
143 // overload (the one in dealii::) when instantiating NumberTraits for ADOL-C
144 // types.
145
146 adouble
147 abs(const adouble &x);
148
149 adtl::adouble
150 abs(const adtl::adouble &x);
151 # endif
152 #endif
153
154 DEAL_II_NAMESPACE_CLOSE // Do not convert for module purposes
155
156 namespace std
157 {
158 template <typename Number, std::size_t width>
159 DEAL_II_ALWAYS_INLINE ::dealii::VectorizedArray<Number, width> sqrt(
160 const ::dealii::VectorizedArray<Number, width> &);
161 template <typename Number, std::size_t width>
162 DEAL_II_ALWAYS_INLINE ::dealii::VectorizedArray<Number, width> abs(
163 const ::dealii::VectorizedArray<Number, width> &);
164 template <typename Number, std::size_t width>
165 DEAL_II_ALWAYS_INLINE ::dealii::VectorizedArray<Number, width> max(
166 const ::dealii::VectorizedArray<Number, width> &,
167 const ::dealii::VectorizedArray<Number, width> &);
168 template <typename Number, std::size_t width>
169 DEAL_II_ALWAYS_INLINE ::dealii::VectorizedArray<Number, width> min(
170 const ::dealii::VectorizedArray<Number, width> &,
171 const ::dealii::VectorizedArray<Number, width> &);
172 template <typename Number, size_t width>
173 ::dealii::VectorizedArray<Number, width> pow(
174 const ::dealii::VectorizedArray<Number, width> &, const Number p);
175 template <typename Number, size_t width>
176 ::dealii::VectorizedArray<Number, width> sin(
177 const ::dealii::VectorizedArray<Number, width> &);
178 template <typename Number, size_t width>
179 ::dealii::VectorizedArray<Number, width> cos(
180 const ::dealii::VectorizedArray<Number, width> &);
181 template <typename Number, size_t width>
182 ::dealii::VectorizedArray<Number, width> tan(
183 const ::dealii::VectorizedArray<Number, width> &);
184 template <typename Number, size_t width>
185 ::dealii::VectorizedArray<Number, width> exp(
186 const ::dealii::VectorizedArray<Number, width> &);
187 template <typename Number, size_t width>
188 ::dealii::VectorizedArray<Number, width> log(
189 const ::dealii::VectorizedArray<Number, width> &);
190 } // namespace std
191
192 DEAL_II_NAMESPACE_OPEN // Do not convert for module purposes
193
194 /**
195 * Namespace for the declaration of universal constants. Since the
196 * availability in <tt>math.h</tt> is not always guaranteed, we put them here.
197 * Since this file is included by <tt>base/config.h</tt>, they are available
198 * to the whole library.
199 *
200 * The constants defined here are a subset of the <tt>M_XXX</tt> constants
201 * sometimes declared in the system include file <tt>math.h</tt>, but without
202 * the prefix <tt>M_</tt>.
203 *
204 * In addition to that, we declare <tt>invalid_unsigned_int</tt> to be the
205 * largest unsigned integer representable; this value is widely used in the
206 * library as a marker for an invalid index, an invalid size of an array, and
207 * similar purposes.
208 */
209 namespace numbers
210 {
211 /**
212 * e
213 */
214 constexpr double E = 2.7182818284590452354;
215
216 /**
217 * log_2 e
218 */
219 constexpr double LOG2E = 1.4426950408889634074;
220
221 /**
222 * log_10 e
223 */
224 constexpr double LOG10E = 0.43429448190325182765;
225
226 /**
227 * log_e 2
228 */
229 constexpr double LN2 = 0.69314718055994530942;
230
231 /**
232 * log_e 10
233 */
234 constexpr double LN10 = 2.30258509299404568402;
235
236 /**
237 * pi
238 */
239 constexpr double PI = 3.14159265358979323846;
240
241 /**
242 * pi/2
243 */
244 constexpr double PI_2 = 1.57079632679489661923;
245
246 /**
247 * pi/4
248 */
249 constexpr double PI_4 = 0.78539816339744830962;
250
251 /**
252 * sqrt(2)
253 */
254 constexpr double SQRT2 = 1.41421356237309504880;
255
256 /**
257 * 1/sqrt(2)
258 */
259 constexpr double SQRT1_2 = 0.70710678118654752440;
260
261 /**
262 * Return @p true if the given value is a finite floating point number, i.e.
263 * is neither plus or minus infinity nor NaN (not a number).
264 *
265 * Note that the argument type of this function is <code>double</code>. In
266 * other words, if you give a very large number of type <code>long
267 * double</code>, this function may return <code>false</code> even if the
268 * number is finite with respect to type <code>long double</code>.
269 */
270 bool is_finite(const double x);
271
272 /**
273 * Return @p true if real and imaginary parts of the given complex number
274 * are finite.
275 */
276 bool is_finite(const std::complex<double> &x);
277
278 /**
279 * Return @p true if real and imaginary parts of the given complex number
280 * are finite.
281 */
282 bool is_finite(const std::complex<float> &x);
283
284 /**
285 * Return @p true if real and imaginary parts of the given complex number
286 * are finite.
287 *
288 * Again may not work correctly if real or imaginary parts are very large
289 * numbers that are infinite in terms of <code>double</code>, but finite
290 * with respect to <code>long double</code>.
291 */
292 bool is_finite(const std::complex<long double> &x);
293
294 /**
295 * Return whether two numbers are equal to one another.
296 *
297 * For intricate data types (e.g. some automatically differentiable numbers),
298 * this function returns the result of the comparison of scalar values stored
299 * by the input arguments.
300 *
301 * @note This function expects that @p value_2 is castable to the type
302 * of @p value_1.
303 */
304 template <typename Number1, typename Number2>
305 constexpr DEAL_II_HOST_DEVICE bool values_are_equal(const Number1 &value_1,
306 const Number2 &value_2);
307
308 /**
309 * Return whether two numbers are not equal to one another.
310 *
311 * For intricate data types (e.g. some automatically differentiable numbers),
312 * this function returns the result of the comparison of scalar values stored
313 * by the input arguments.
314 *
315 * @note This function expects that @p value_2 is castable to the type
316 * of @p value_1.
317 */
318 template <typename Number1, typename Number2>
319 constexpr bool values_are_not_equal(const Number1 &value_1,
320 const Number2 &value_2);
321
322 /**
323 * Return whether or not a value is equal to zero.
324 *
325 * For intricate data types (e.g. some automatically differentiable numbers),
326 * this function returns the result of the comparison of scalar values stored
327 * by the input arguments.
328 */
329 template <typename Number>
330 constexpr DEAL_II_HOST_DEVICE bool value_is_zero(const Number &value);
331
332 /**
333 * Return whether @p value_1 is less than that of @p value_2.
334 *
335 * For intricate data types (e.g. some automatically differentiable numbers),
336 * this function returns the result of the comparison of scalar values stored
337 * by the input arguments.
338 *
339 * @note This function expects that @p value_2 is castable to the type
340 * of @p value_1.
341 */
342 template <typename Number1, typename Number2>
343 bool value_is_less_than(const Number1 &value_1, const Number2 &value_2);
344
345 /**
346 * Return whether @p value_1 is less than or equal to that of @p value_2.
347 *
348 * For intricate data types (e.g. some automatically differentiable numbers),
349 * this function returns the result of the comparison of scalar values stored
350 * by the input arguments.
351 *
352 * @note This function expects that @p value_2 is castable to the type
353 * of @p value_1.
354 */
355 template <typename Number1, typename Number2>
356 bool value_is_less_than_or_equal_to(const Number1 &value_1,
357 const Number2 &value_2);
358
359
360
361 /**
362 * Return whether @p value_1 is greater than that of @p value_2.
363 *
364 * For intricate data types (e.g. some automatically differentiable numbers),
365 * this function returns the result of the comparison of scalar values stored
366 * by the input arguments.
367 *
368 * @note This function expects that @p value_2 is castable to the type
369 * of @p value_1.
370 */
371 template <typename Number1, typename Number2>
372 bool value_is_greater_than(const Number1 &value_1, const Number2 &value_2);
373
374 /**
375 * Return whether @p value_1 is greater than or equal to that of @p value_2.
376 *
377 * For intricate data types (e.g. some automatically differentiable numbers),
378 * this function returns the result of the comparison of scalar values stored
379 * by the input arguments.
380 *
381 * @note This function expects that @p value_2 is castable to the type
382 * of @p value_1.
383 */
384 template <typename Number1, typename Number2>
385 bool value_is_greater_than_or_equal_to(const Number1 &value_1,
386 const Number2 &value_2);
387
388 /**
389 * A structure that, together with its partial specializations
390 * NumberTraits<std::complex<number> >, provides traits and member functions
391 * that make it possible to write templates that work on both real number
392 * types and complex number types. This template is mostly used to implement
393 * linear algebra classes such as vectors and matrices that work for both
394 * real and complex numbers.
395 */
396 template <typename number>
397 struct NumberTraits
398 {
399 /**
400 * A flag that specifies whether the template type given to this class is
401 * complex or real. Since the general template is selected for non-complex
402 * types, the answer is <code>false</code>.
403 */
404 static constexpr bool is_complex = false;
405
406 /**
407 * For this data type, alias the corresponding real type. Since the
408 * general template is selected for all data types that are not
409 * specializations of std::complex<T>, the underlying type must be
410 * real-values, so the real_type is equal to the underlying type.
411 */
412 using real_type = number;
413
414 /**
415 * For this data type, alias the corresponding double type.
416 */
417 using double_type = double;
418
419 /**
420 * Return the complex-conjugate of the given number. Since the general
421 * template is selected if number is not a complex data type, this
422 * function simply returns the given number.
423 *
424 * @note This function can also be used in @ref GlossDevice "device" code.
425 */
426 static constexpr DEAL_II_HOST_DEVICE const number &
427 conjugate(const number &x);
428
429 /**
430 * Return the square of the absolute value of the given number. Since the
431 * general template is chosen for types not equal to std::complex, this
432 * function simply returns the square of the given number.
433 *
434 * @note If the template type can be used in @ref GlossDevice "device" code, the same holds true
435 * for this function.
436 */
437 static constexpr DEAL_II_HOST_DEVICE real_type
438 abs_square(const number &x);
439
440 /**
441 * Return the absolute value of a number.
442 */
443 static real_type
444 abs(const number &x);
445 };
446
447
448 /**
449 * Specialization of the general NumberTraits class that provides the
450 * relevant information if the underlying data type is std::complex<T>.
451 */
452 template <typename number>
453 struct NumberTraits<std::complex<number>>
454 {
455 /**
456 * A flag that specifies whether the template type given to this class is
457 * complex or real. Since this specialization of the general template is
458 * selected for complex types, the answer is <code>true</code>.
459 */
460 static constexpr bool is_complex = true;
461
462 /**
463 * For this data type, alias the corresponding real type. Since this
464 * specialization of the template is selected for number types
465 * std::complex<T>, the real type is equal to the type used to store the
466 * two components of the complex number.
467 */
468 using real_type = number;
469
470 /**
471 * For this data type, alias the corresponding double type.
472 */
473 using double_type = std::complex<double>;
474
475 /**
476 * Return the complex-conjugate of the given number.
477 */
478 static constexpr std::complex<number>
479 conjugate(const std::complex<number> &x);
480
481 /**
482 * Return the square of the absolute value of the given number. Since this
483 * specialization of the general template is chosen for types equal to
484 * std::complex, this function returns the product of a number and its
485 * complex conjugate.
486 */
487 static constexpr real_type
488 abs_square(const std::complex<number> &x);
489
490
491 /**
492 * Return the absolute value of a complex number.
493 */
494 static real_type
495 abs(const std::complex<number> &x);
496 };
497
498 // --------------- inline and template functions ---------------- //
499
500 inline bool is_nan(const double x)
501 {
502 return std::isnan(x);
503 }
504
505
506
507 inline bool is_finite(const double x)
508 {
509 return std::isfinite(x);
510 }
511
512
513
514 inline bool is_finite(const std::complex<double> &x)
515 {
516 // Check complex numbers for infinity
517 // by testing real and imaginary part
518 return (is_finite(x.real()) && is_finite(x.imag()));
519 }
520
521
522
523 inline bool is_finite(const std::complex<float> &x)
524 {
525 // Check complex numbers for infinity
526 // by testing real and imaginary part
527 return (is_finite(x.real()) && is_finite(x.imag()));
528 }
529
530
531
532 inline bool is_finite(const std::complex<long double> &x)
533 {
534 // Same for std::complex<long double>
535 return (is_finite(x.real()) && is_finite(x.imag()));
536 }
537
538
539 template <typename number>
540 constexpr DEAL_II_HOST_DEVICE const number &NumberTraits<number>::conjugate(
541 const number &x)
542 {
543 return x;
544 }
545
546
547
548 template <typename number>
549 constexpr DEAL_II_HOST_DEVICE typename NumberTraits<number>::real_type
550 NumberTraits<number>::abs_square(const number &x)
551 {
552 return x * x;
553 }
554
555
556
557 template <typename number>
558 typename NumberTraits<number>::real_type NumberTraits<number>::abs(
559 const number &x)
560 {
561 // Make things work with AD types
562 using std::abs;
563 #ifdef DEAL_II_WITH_ADOLC
564 // This one is a little tricky - we have our own abs function in dealii::,
565 // prototyped with forward-declared types in this file, but it only exists
566 // if we have ADOL-C: hence we only add this using statement in that
567 // situation
568 using dealii::abs;
569 #endif
570 return abs(x);
571 }
572
573
574
575 template <typename number>
576 constexpr std::complex<number> NumberTraits<std::complex<number>>::conjugate(
577 const std::complex<number> &x)
578 {
579 return std::conj(x);
580 }
581
582
583
584 template <typename number>
585 typename NumberTraits<std::complex<number>>::real_type
586 NumberTraits<std::complex<number>>::abs(const std::complex<number> &x)
587 {
588 // Make things work with AD types
589 using std::abs;
590 #ifdef DEAL_II_WITH_ADOLC
591 // Same comment as the non-complex case holds here
592 using dealii::abs;
593 #endif
594 return abs(x);
595 }
596
597
598
599 template <typename number>
600 constexpr typename NumberTraits<std::complex<number>>::real_type
601 NumberTraits<std::complex<number>>::abs_square(const std::complex<number> &x)
602 {
603 return std::norm(x);
604 }
605
606 } // namespace numbers
607
608
609 // Forward declarations
610 namespace Differentiation
611 {
612 namespace AD
613 {
614 namespace internal
615 {
616 // Defined in differentiation/ad/ad_number_traits.h
617 template <typename T>
618 struct NumberType;
619 } // namespace internal
620
621 // Defined in differentiation/ad/ad_number_traits.h
622 template <typename NumberType>
623 struct is_ad_number;
624 } // namespace AD
625 } // namespace Differentiation
626
627
628 namespace internal
629 {
630 /**
631 * A test to see if it is possible to convert one number
632 * type to the other.
633 */
634 template <typename From, typename To>
635 struct is_explicitly_convertible
636 {
637 // Source: https://stackoverflow.com/a/16944130
638 private:
639 template <typename T>
640 static void f(T);
641
642 template <typename F, typename T>
643 static constexpr auto
644 test(int) -> decltype(f(static_cast<T>(std::declval<F>())), true)
645 {
646 return true;
647 }
648
649 template <typename F, typename T>
650 static constexpr auto
651 test(...) -> bool
652 {
653 return false;
654 }
655
656 public:
657 static const bool value = test<From, To>(0);
658 };
659
660 /*
661 * The structs below are needed to convert between some special number types.
662 * Also see tensor.h for another specialization.
663 */
664 template <typename T>
665 struct NumberType
666 {
667 static constexpr DEAL_II_HOST_DEVICE_ALWAYS_INLINE const T &
668 value(const T &t)
669 {
670 return t;
671 }
672
673 // Below are generic functions that allows an overload for any
674 // type U that is transformable to type T. This is particularly
675 // useful when needing to cast exotic number types
676 // (e.g. auto-differentiable or symbolic numbers) to a floating
677 // point one, such as might happen when converting between tensor
678 // types.
679
680 // Type T is constructible from F.
681 template <typename F>
682 static constexpr DEAL_II_HOST_DEVICE_ALWAYS_INLINE T
683 value(const F &f,
684 std::enable_if_t<!std::is_same_v<std::decay_t<T>, std::decay_t<F>> &&
685 std::is_constructible_v<T, F>> * = nullptr)
686 {
687 return T(f);
688 }
689
690 // Type T is explicitly convertible (but not constructible) from F.
691 template <typename F>
692 static constexpr DEAL_II_HOST_DEVICE_ALWAYS_INLINE T
693 value(const F &f,
694 std::enable_if_t<!std::is_same_v<std::decay_t<T>, std::decay_t<F>> &&
695 !std::is_constructible_v<T, F> &&
696 is_explicitly_convertible<const F, T>::value> * =
697 nullptr)
698 {
699 return static_cast<T>(f);
700 }
701
702 // Sacado doesn't provide any conversion operators, so we have
703 // to extract the value and perform further conversions from there.
704 // To be safe, we extend this to other possible AD numbers that
705 // might fall into the same category.
706 template <typename F>
707 static T
708 value(
709 const F &f,
710 std::enable_if_t<!std::is_same_v<std::decay_t<T>, std::decay_t<F>> &&
711 !std::is_constructible_v<T, F> &&
712 !is_explicitly_convertible<const F, T>::value &&
713 Differentiation::AD::is_ad_number<F>::value> * = nullptr)
714 {
715 return Differentiation::AD::internal::NumberType<T>::value(f);
716 }
717 };
718
719 template <typename T>
720 struct NumberType<std::complex<T>>
721 {
722 static constexpr const std::complex<T> &
723 value(const std::complex<T> &t)
724 {
725 return t;
726 }
727
728 static constexpr std::complex<T>
729 value(const T &t)
730 {
731 return std::complex<T>(t);
732 }
733
734 // Facilitate cast from complex<double> to complex<float>
735 template <typename U>
736 static constexpr std::complex<T>
737 value(const std::complex<U> &t)
738 {
739 return std::complex<T>(NumberType<T>::value(t.real()),
740 NumberType<T>::value(t.imag()));
741 }
742 };
743
744 } // namespace internal
745
746 namespace numbers
747 {
748 #ifdef DEAL_II_ADOLC_WITH_ADVANCED_BRANCHING
749
750 /**
751 * Return whether two numbers are equal to one another. For intricate data
752 * types (e.g. some automatically differentiable numbers), this function
753 * returns only whether the scalar values stored by the input values are
754 * equal.
755 *
756 * @note When ADOL-C is compiled with the "advanced branching" feature, then
757 * this specialization is only intended for use in assertions and
758 * other code paths that do not affect the end result of a computation.
759 */
760 // Defined in differentiation/ad/adolc_number_types.cc
761 bool
762 values_are_equal(const adouble &value_1, const adouble &value_2);
763
764
765 /**
766 * Return whether two numbers are equal to one another. For intricate data
767 * types (e.g. some automatically differentiable numbers), this function
768 * returns only whether the scalar values stored by the input values are
769 * equal.
770 *
771 * @note When ADOL-C is compiled with the "advanced branching" feature, then
772 * this specialization is only intended for use in assertions and
773 * other code paths that do not affect the end result of a computation.
774 */
775 template <typename Number>
776 bool
777 values_are_equal(const adouble &value_1, const Number &value_2)
778 {
779 // Use the specialized definition for two ADOL-C taped types
780 return values_are_equal(
781 value_1, dealii::internal::NumberType<adouble>::value(value_2));
782 }
783
784
785 /**
786 * Return whether two numbers are equal to one another. For intricate data
787 * types (e.g. some automatically differentiable numbers), this function
788 * returns only whether the scalar values stored by the input values are
789 * equal.
790 *
791 * @note When ADOL-C is compiled with the "advanced branching" feature, then
792 * this specialization is only intended for use in assertions and
793 * other code paths that do not affect the end result of a computation.
794 */
795 template <typename Number>
796 bool
797 values_are_equal(const Number &value_1, const adouble &value_2)
798 {
799 // Use the above definition
800 return values_are_equal(value_2, value_1);
801 }
802
803 /**
804 * Return whether @p value_1 is less than that of @p value_2.
805 *
806 * For intricate data types (e.g. some automatically differentiable numbers),
807 * this function returns the result of the comparison of scalar values stored
808 * by the input arguments.
809 *
810 * @note When ADOL-C is compiled with the "advanced branching" feature, then
811 * this specialization is only intended for use in assertions and
812 * other code paths that do not affect the end result of a computation.
813 */
814 // Defined in differentiation/ad/adolc_number_types.cc
815 bool
816 value_is_less_than(const adouble &value_1, const adouble &value_2);
817
818
819 /**
820 * Return whether @p value_1 is less than that of @p value_2.
821 *
822 * For intricate data types (e.g. some automatically differentiable numbers),
823 * this function returns the result of the comparison of scalar values stored
824 * by the input arguments.
825 *
826 * @note When ADOL-C is compiled with the "advanced branching" feature, then
827 * this specialization is only intended for use in assertions and
828 * other code paths that do not affect the end result of a computation.
829 */
830 template <typename Number>
831 bool
832 value_is_less_than(const adouble &value_1, const Number &value_2)
833 {
834 // Use the specialized definition for two ADOL-C taped types
835 return value_is_less_than(
836 value_1, dealii::internal::NumberType<adouble>::value(value_2));
837 }
838
839
840 /**
841 * Return whether @p value_1 is less than that of @p value_2.
842 *
843 * For intricate data types (e.g. some automatically differentiable numbers),
844 * this function returns the result of the comparison of scalar values stored
845 * by the input arguments.
846 *
847 * @note When ADOL-C is compiled with the "advanced branching" feature, then
848 * this specialization is only intended for use in assertions and
849 * other code paths that do not affect the end result of a computation.
850 */
851 template <typename Number>
852 bool
853 value_is_less_than(const Number &value_1, const adouble &value_2)
854 {
855 // Use the specialized definition for two ADOL-C taped types
856 return value_is_less_than(
857 dealii::internal::NumberType<adouble>::value(value_1), value_2);
858 }
859
860 #endif
861
862
863 template <typename Number1, typename Number2>
864 constexpr DEAL_II_HOST_DEVICE bool
865 values_are_equal(const Number1 &value_1, const Number2 &value_2)
866 {
867 return (value_1 == dealii::internal::NumberType<Number1>::value(value_2));
868 }
869
870
871 template <typename Number1, typename Number2>
872 inline constexpr bool
873 values_are_not_equal(const Number1 &value_1, const Number2 &value_2)
874 {
875 return !(values_are_equal(value_1, value_2));
876 }
877
878
879 template <typename Number>
880 constexpr DEAL_II_HOST_DEVICE bool
881 value_is_zero(const Number &value)
882 {
883 return values_are_equal(value, 0.0);
884 }
885
886
887 template <typename Number1, typename Number2>
888 inline bool
889 value_is_less_than(const Number1 &value_1, const Number2 &value_2)
890 {
891 return (value_1 < dealii::internal::NumberType<Number1>::value(value_2));
892 }
893
894
895 template <typename Number1, typename Number2>
896 inline bool
897 value_is_less_than_or_equal_to(const Number1 &value_1, const Number2 &value_2)
898 {
899 return (value_is_less_than(value_1, value_2) ||
900 values_are_equal(value_1, value_2));
901 }
902
903
904 template <typename Number1, typename Number2>
905 bool
906 value_is_greater_than(const Number1 &value_1, const Number2 &value_2)
907 {
908 return !(value_is_less_than_or_equal_to(value_1, value_2));
909 }
910
911
912 template <typename Number1, typename Number2>
913 inline bool
914 value_is_greater_than_or_equal_to(const Number1 &value_1,
915 const Number2 &value_2)
916 {
917 return !(value_is_less_than(value_1, value_2));
918 }
919 } // namespace numbers
920
921 DEAL_II_NAMESPACE_CLOSE
922
923 #endif