OpenKalman
number_traits.hpp
Go to the documentation of this file.
1 /* This file is part of OpenKalman, a header-only C++ library for
2  * Kalman filters and other recursive filters.
3  *
4  * Copyright (c) 2022-2025 Christopher Lee Ogden <ogden@gatech.edu>
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
9  */
10 
16 #ifndef OPENKALMAN_NUMBER_TRAITS_HPP
17 #define OPENKALMAN_NUMBER_TRAITS_HPP
18 
19 #include <limits>
20 #include <complex>
21 #include "../../basics/compatibility/language-features.hpp"
22 
23 namespace OpenKalman::interface
24 {
31 #ifdef __cpp_concepts
32  template<typename T>
33 #else
34  template<typename T, typename = void>
35 #endif
37  {
42  static constexpr bool is_specialized = false;
43 
44 
48  static constexpr bool is_complex = false;
49 
50 
54  static constexpr auto real = [](T) { throw std::logic_error("Interface not implemented"); };
55 
56 
60  static constexpr auto imag = [](T) { throw std::logic_error("Interface not implemented"); };
61 
62 
68  static constexpr auto make_complex = [](T re, T im) { throw std::logic_error("Interface not implemented"); };
69 
70  };
71 
72 
73 
77  template<>
78  struct number_traits<void>
79  {
80  static constexpr bool is_specialized = false;
81  static constexpr bool is_complex = false;
82  };
83 
84 
85 
89 #ifdef __cpp_concepts
90  template<typename T> requires std::is_arithmetic_v<T>
91  struct number_traits<T>
92 #else
93  template<typename T>
94  struct number_traits<T, std::enable_if_t<std::is_arithmetic_v<T>>>
95 #endif
96  {
97  static constexpr bool is_specialized = true;
98  static constexpr bool is_complex = false;
99  static constexpr auto real = [](T t) noexcept { return std::real(std::move(t)); };
100  static constexpr auto imag = [](T t) noexcept { return std::imag(std::move(t)); };
101  static constexpr auto make_complex = [](T re, T im) noexcept { return std::complex<T> {std::move(re), std::move(im)}; };
102  };
103 
104 
108  template<typename T>
110  {
111  static constexpr bool is_specialized = true;
112  static constexpr bool is_complex = true;
113  static constexpr auto real = [](std::complex<T> t) noexcept { return std::real(std::move(t)); };
114  static constexpr auto imag = [](std::complex<T> t) noexcept { return std::imag(std::move(t)); };
115  static constexpr auto make_complex = [](T re, T im) noexcept { return std::complex<T> {std::move(re), std::move(im)}; };
116 
117  };
118 
119 } // namespace OpenKalman::interface
120 
121 #endif //OPENKALMAN_NUMBER_TRAITS_HPP
static constexpr auto imag
A callable object that returns the real part of the argument of type T.
Definition: number_traits.hpp:60
Definition: basics.hpp:41
constexpr auto imag(Arg arg)
A constexpr function to obtain the imaginary part of a (complex) number.
Definition: imag.hpp:40
constexpr bool complex
T is a values::value that reduces to std::complex or a custom complex type.
Definition: complex.hpp:46
static constexpr bool is_specialized
This value is true for all T for which there exists a specialization of numeric_traits.
Definition: number_traits.hpp:42
Definition: tuple_reverse.hpp:103
static constexpr auto real
A callable object that returns the real part of the argument of type T.
Definition: number_traits.hpp:54
constexpr auto real(Arg arg)
A constexpr function to obtain the real part of a (complex) number.
Definition: real.hpp:40
static constexpr auto make_complex
A callable object that makes a complex number consistent with T from two real arguments.
Definition: number_traits.hpp:68
static constexpr bool is_complex
Whether T is a complex number.
Definition: number_traits.hpp:48
Definition: number_traits.hpp:36