iterative-solver 0.0
 
Loading...
Searching...
No Matches
scalar_traits.h
1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_SCALAR_TRAITS_H_
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_SCALAR_TRAITS_H_
3
12#include <complex>
13#include <limits>
14#include <type_traits>
15
16namespace molpro::linalg {
17
18template <typename T>
19struct is_complex : std::false_type {};
20
21template <typename T>
22struct is_complex<std::complex<T>> : std::true_type {};
23
25template <typename T>
26struct real_type {
27 using type = T;
28};
29
30template <typename T>
31struct real_type<std::complex<T>> {
32 using type = T;
33};
34
42template <typename T>
44
46template <typename T>
47T conjugate(const T& x) {
48 if constexpr (is_complex<T>{}) {
49 using std::conj;
50 return conj(x);
51 } else {
52 return x;
53 }
54}
55
57template <typename T>
59 if constexpr (is_complex<T>{}) {
60 return x.real();
61 } else {
62 return x;
63 }
64}
65
67template <typename T>
69 if constexpr (is_complex<T>{}) {
70 return x.imag();
71 } else {
72 return real_type_t<T>{};
73 }
74}
75
84template <typename value_type>
85real_type_t<value_type> precision_scaled(double tolerance_for_double) {
86 using real_t = real_type_t<value_type>;
87 if constexpr (std::is_same_v<real_t, double>) {
88 return tolerance_for_double;
89 } else {
90 return real_t(tolerance_for_double) *
91 (std::numeric_limits<real_t>::epsilon() / real_t(std::numeric_limits<double>::epsilon()));
92 }
93}
94
95} // namespace molpro::linalg
96
97#endif // LINEARALGEBRA_SRC_MOLPRO_LINALG_SCALAR_TRAITS_H_
Definition helper.h:14
real_type_t< T > real_part(const T &x)
The real part of a scalar; the value itself for a real type.
Definition scalar_traits.h:58
T conjugate(const T &x)
Complex conjugate, staying within the scalar type; the identity for a real type.
Definition scalar_traits.h:47
typename real_type< T >::type real_type_t
The real type underlying T, i.e. T itself for a real type and U for std::complex<U>.
Definition scalar_traits.h:43
real_type_t< T > imaginary_part(const T &x)
The imaginary part of a scalar; zero for a real type.
Definition scalar_traits.h:68
real_type_t< value_type > precision_scaled(double tolerance_for_double)
Rescale a tolerance that was calibrated for IEEE double precision to the working precision.
Definition scalar_traits.h:85
Definition scalar_traits.h:19
T type
Definition scalar_traits.h:32
The real type underlying a (possibly complex) scalar type.
Definition scalar_traits.h:26
T type
Definition scalar_traits.h:27