Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
scalar_traits.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "numeric_limits.hpp"
4#include "types.hpp"
5#include <complex>
6#include <type_traits>
7
8namespace uni20
9{
10
16template <typename T>
17struct is_integer_t : std::bool_constant<std::integral<T> && !std::same_as<T, bool> && !std::same_as<T, char> &&
18 !std::same_as<T, signed char> && !std::same_as<T, unsigned char>>
19{};
20
24template <typename T> inline constexpr bool is_integer_v = is_integer_t<std::remove_cvref_t<T>>::value;
25
30template <typename T> struct is_real_t : std::false_type
31{};
32
33template <> struct is_real_t<float> : std::true_type
34{};
35
36template <> struct is_real_t<double> : std::true_type
37{};
38
39template <> struct is_real_t<long double> : std::true_type
40{};
41
42#if UNI20_HAS_MPREAL
43template <> struct is_real_t<mpreal> : std::true_type
44{};
45#endif
46
47#if UNI20_HAS_FLOAT128
48template <> struct is_real_t<uni20::float128> : std::true_type
49{};
50#endif
51
55template <typename T> inline constexpr bool is_real_v = is_real_t<std::remove_cvref_t<T>>::value;
56
61template <typename T> struct is_complex_t : std::false_type
62{};
63
64template <typename T> struct is_complex_t<detail::standard_complex<T>> : std::true_type
65{};
66#if UNI20_HAS_MPREAL
67template <> struct is_complex_t<mpcomplex> : std::true_type
68{};
69#endif
70
74template <typename T> inline constexpr bool is_complex_v = is_complex_t<std::remove_cvref_t<T>>::value;
75
79template <typename T> inline constexpr bool is_scalar_v = is_real_v<T> || is_complex_v<T> || is_integer_v<T>;
80
84template <typename T> inline constexpr bool is_real_or_complex_v = is_real_v<T> || is_complex_v<T>;
85
92template <typename T> struct scalar_type
93{
94 using type = void;
95};
96
97template <typename T>
98 requires is_scalar_v<T>
99struct scalar_type<T>
100{
101 using type = T;
102};
103
104template <typename T>
105 requires requires { typename T::value_type; } && (!is_scalar_v<T>) &&
106 requires { typename scalar_type<typename T::value_type>::type; }
107struct scalar_type<T> : scalar_type<typename T::value_type>
108{};
109
110template <typename T> using scalar_t = typename scalar_type<std::remove_cvref_t<T>>::type;
111
117template <typename T> inline constexpr bool is_scalar_valued_v = !std::same_as<scalar_t<T>, void>;
118
122template <typename T>
123inline constexpr bool is_integer_scalar_valued_v = is_scalar_valued_v<T> && is_integer_v<scalar_t<T>>;
124
128template <typename T> inline constexpr bool is_real_scalar_valued_v = is_scalar_valued_v<T> && is_real_v<scalar_t<T>>;
129
133template <typename T>
134inline constexpr bool is_complex_scalar_valued_v = is_scalar_valued_v<T> && is_complex_v<scalar_t<T>>;
135
139template <typename T>
140inline constexpr bool is_real_or_complex_scalar_valued_v = is_scalar_valued_v<T> && is_real_or_complex_v<scalar_t<T>>;
141
149template <typename T> struct make_real;
150
151template <typename T>
152 requires is_real_scalar_valued_v<T>
153struct make_real<T>
154{
155 using type = T;
156};
157
158template <typename T>
159 requires is_complex_v<T>
160struct make_real<T>
161{
162 using type = typename T::value_type;
163};
164
168template <typename T> using make_real_t = typename make_real<T>::type;
169
177template <typename T> struct make_complex;
178
183template <typename T>
184 requires is_real_v<T>
186{
188};
189
193template <typename T>
194 requires is_complex_scalar_valued_v<T>
195struct make_complex<T>
196{
197 using type = T;
198};
199
203template <typename T> using make_complex_t = typename make_complex<T>::type;
204
205} // namespace uni20
Owning exact rational or finite-precision MPFR real, with eager arithmetic.
Definition mpreal.hpp:74
locale_t value
Definition data_table.cpp:49
constexpr bool is_scalar_v
Trait to detect whether a type is a numeric scalar (real, complex, or integer).
Definition scalar_traits.hpp:79
constexpr bool is_real_or_complex_v
Trait to detect whether a type is a real or complex scalar.
Definition scalar_traits.hpp:84
constexpr bool is_complex_scalar_valued_v
Trait to detect whether T is scalar-valued with a complex scalar.
Definition scalar_traits.hpp:134
constexpr bool is_real_v
Convenience alias that exposes the value of is_real_t.
Definition scalar_traits.hpp:55
typename detail::complex_type< Real >::type complex
Project-level complex scalar spelling.
Definition types.hpp:66
constexpr bool is_integer_scalar_valued_v
Trait to detect whether T is scalar-valued with an integer scalar.
Definition scalar_traits.hpp:123
constexpr bool is_integer_v
Convenience alias that exposes the value of is_integer_t.
Definition scalar_traits.hpp:24
typename make_real< T >::type make_real_t
Alias for the underlying real-valued type of T.
Definition scalar_traits.hpp:168
constexpr bool is_complex_v
Convenience alias that exposes the value of is_complex_t.
Definition scalar_traits.hpp:74
typename make_complex< T >::type make_complex_t
Alias for the complexified version of T.
Definition scalar_traits.hpp:203
constexpr bool is_real_scalar_valued_v
Trait to detect whether T is scalar-valued with a real scalar.
Definition scalar_traits.hpp:128
constexpr bool is_real_or_complex_scalar_valued_v
Trait to detect whether T is scalar-valued with a real or complex scalar.
Definition scalar_traits.hpp:140
constexpr bool is_scalar_valued_v
Trait to detect whether T is scalar-valued.
Definition scalar_traits.hpp:117
Definition async/async.hpp:26
typename scalar_type< std::remove_cvref_t< T > >::type scalar_t
Definition scalar_traits.hpp:110
Trait to detect whether a type is a complex scalar type.
Definition scalar_traits.hpp:62
Trait to detect whether a type is an integer scalar (excluding bool).
Definition scalar_traits.hpp:19
Trait to detect whether a type is a real-valued scalar.
Definition scalar_traits.hpp:31
uni20::complex< T > type
Definition scalar_traits.hpp:187
Metafunction to convert a type to its complexified analog.
Definition scalar_traits.hpp:177
T type
Definition scalar_traits.hpp:155
Metafunction to convert a type to its real-valued analog.
Definition scalar_traits.hpp:149
T type
Definition scalar_traits.hpp:101
Trait to extract the scalar type from a type T.
Definition scalar_traits.hpp:93
void type
Definition scalar_traits.hpp:94