tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
concepts.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_LINALG_CONCEPTS_HPP
4#define TETL_LINALG_CONCEPTS_HPP
5
10#include <etl/_complex/real.hpp>
13#include <etl/_math/abs.hpp>
18#include <etl/_numeric/abs.hpp>
26
27namespace etl::linalg::detail {
28
29template <typename T>
30struct is_mdspan : false_type { };
31
32template <typename T, typename Extents, typename Layout, typename Accessor>
33struct is_mdspan<mdspan<T, Extents, Layout, Accessor>> : true_type { };
34
35template <typename... Ts>
36using common_size_type_t = common_type_t<typename Ts::size_type...>;
37
38namespace linalg_adl_checks {
39
40using etl::abs;
41using etl::conj;
42using etl::imag;
43using etl::real;
44
45template <typename T>
46auto abs(T const&) -> T = delete;
47template <typename T>
48auto conj(T const&) -> T = delete;
49template <typename T>
50auto real(T const&) -> T = delete;
51template <typename T>
52auto imag(T const&) -> T = delete;
53
54template <typename T>
55concept has_abs = requires { abs(declval<T>()); };
56
57template <typename T>
58concept has_conj = requires { conj(declval<T>()); };
59
60template <typename T>
61concept has_real = requires { real(declval<T>()); };
62
63template <typename T>
64concept has_imag = requires { imag(declval<T>()); };
65
66} // namespace linalg_adl_checks
67
68template <typename T>
69concept has_adl_abs = linalg_adl_checks::has_abs<T>;
70
71template <typename T>
72concept has_adl_conj = linalg_adl_checks::has_conj<T>;
73
74template <typename T>
75concept has_adl_real = linalg_adl_checks::has_real<T>;
76
77template <typename T>
78concept has_adl_imag = linalg_adl_checks::has_imag<T>;
79
80inline constexpr auto abs_if_needed = []<typename T>(T const& val) {
81 if constexpr (unsigned_integral<T>) {
82 return val;
83 } else if constexpr (is_arithmetic_v<T>) {
84 return etl::abs(val);
85 } else if constexpr (has_adl_abs<T>) {
86 return abs(val);
87 } else {
88 static_assert(always_false<T>);
89 }
90};
91
92inline constexpr auto conj_if_needed = []<typename T>(T const& val) {
93 if constexpr (not is_arithmetic_v<T> and has_adl_conj<T>) {
94 return conj(val);
95 } else {
96 return val;
97 }
98};
99
100inline constexpr auto real_if_needed = []<typename T>(T const& val) {
101 if constexpr (not is_arithmetic_v<T> and has_adl_real<T>) {
102 return real(val);
103 } else {
104 return val;
105 }
106};
107
108inline constexpr auto imag_if_needed = []<typename T>(T const& val) {
109 if constexpr (not is_arithmetic_v<T> and has_adl_imag<T>) {
110 return imag(val);
111 } else {
112 return T{};
113 }
114};
115
116} // namespace etl::linalg::detail
117
118namespace etl::linalg {
119
121template <typename T>
122concept in_vector = detail::is_mdspan<T>::value && T::rank() == 1;
123
125template <typename T>
127 = detail::is_mdspan<T>::value && T::rank() == 1
128 && same_as<remove_const_t<typename T::element_type>, typename T::element_type> && T::is_always_unique();
129
131template <typename T>
133 = detail::is_mdspan<T>::value && T::rank() == 1
134 && same_as<remove_const_t<typename T::element_type>, typename T::element_type> && T::is_always_unique();
135
137template <typename T>
138concept in_matrix = detail::is_mdspan<T>::value && T::rank() == 2;
139
141template <typename T>
143 = detail::is_mdspan<T>::value && T::rank() == 2
144 && is_same_v<remove_const_t<typename T::element_type>, typename T::element_type> && T::is_always_unique();
145
147template <typename T>
149 = detail::is_mdspan<T>::value && T::rank() == 2
150 && is_same_v<remove_const_t<typename T::element_type>, typename T::element_type> && T::is_always_unique();
151
152// template <typename T>
153// concept possibly_packed_inout_matrix =
154// detail::is_mdspan<T>::value && T::rank() == 2 &&
155// is_same_v<remove_const_t<typename T::element_type>,
156// typename T::element_type> &&
157// (T::is_always_unique() ||
158// is_same_v<typename T::layout_type, layout_blas_packed>);
159
161template <typename T>
162concept in_object = detail::is_mdspan<T>::value && (T::rank() == 1 || T::rank() == 2);
163
165template <typename T>
167 = detail::is_mdspan<T>::value && (T::rank() == 1 || T::rank() == 2)
168 && is_same_v<remove_const_t<typename T::element_type>, typename T::element_type> && T::is_always_unique();
169
171template <typename T>
173 = detail::is_mdspan<T>::value && (T::rank() == 1 || T::rank() == 2)
174 && is_same_v<remove_const_t<typename T::element_type>, typename T::element_type> && T::is_always_unique();
175
176} // namespace etl::linalg
177
178#endif // TETL_LINALG_CONCEPTS_HPP
Definition concepts.hpp:138
Definition concepts.hpp:162
Definition concepts.hpp:122
Definition concepts.hpp:149
Definition concepts.hpp:173
Definition concepts.hpp:133
Definition concepts.hpp:143
Definition concepts.hpp:167
Definition concepts.hpp:127
The concept same_as<T, U> is satisfied if and only if T and U denote the same type....
Definition same_as.hpp:19
constexpr auto real(complex< T > const &z) noexcept(noexcept(z.real())) -> T
Definition real.hpp:14
constexpr auto imag(complex< T > const &z) noexcept(noexcept(z.imag())) -> T
Definition imag.hpp:14
constexpr auto abs(complex< T > const &z) -> T
Definition abs.hpp:13
constexpr auto conj(complex< T > const &z) noexcept -> complex< T >
Definition conj.hpp:14
auto val(pin_number pin) -> etl::uint16_t
Definition gpio.hpp:37
Definition accessor_conjugate.hpp:12
mdspan(CArray &) -> mdspan< remove_all_extents_t< CArray >, extents< size_t, extent_v< CArray, 0 > > >
auto declval() noexcept -> add_rvalue_reference_t< T >
typename remove_const< T >::type remove_const_t
Definition remove_const.hpp:23
constexpr bool always_false
Definition always_false.hpp:9
bool_constant< true > true_type
Definition bool_constant.hpp:13
constexpr bool is_same_v
Definition is_same.hpp:11
constexpr bool is_arithmetic_v
Definition is_arithmetic.hpp:21
typename common_type< T... >::type common_type_t
Definition common_type.hpp:50
bool_constant< false > false_type
Definition bool_constant.hpp:14
static constexpr bool value
Definition integral_constant.hpp:10