VCCC  2024.05
VisualCamp Common C++ library
matrix_base.hpp
Go to the documentation of this file.
1 # /*
2 # * Created by YongGyu Lee on 2020/02/04.
3 # */
4 #
5 # ifndef VCCC_MATH_MAT_EXPRESSION_HPP
6 # define VCCC_MATH_MAT_EXPRESSION_HPP
7 #
8 # include <cstddef>
9 # include <tuple>
10 # include <type_traits>
11 #
13 
14 namespace vccc {
15 
18 
19 template<typename Derived>
20 class MatrixBase {
21  public:
22  using derived_type = Derived;
23  using derived_traits = internal::math::traits<derived_type>;
24  using value_type = typename derived_traits::value_type;
25 
26  enum : int {
27  rows = derived_traits::rows,
28  cols = derived_traits::cols,
29  size = rows * cols,
30  shortdim = rows < cols ? rows : cols
31  };
32 
33  static_assert((rows > 0 && cols > 0), "matrix size must be greater than 0");
34 
36  constexpr value_type operator() (std::size_t i) const {
37  return derived()(i);
38  }
39  constexpr value_type operator() (std::size_t i, std::size_t j) const {
40  return derived()(i, j);
41  }
42  constexpr value_type operator[] (std::size_t i) const {
43  return derived()[i];
44  }
45 
46  constexpr inline const derived_type& derived() const {
47  return static_cast<const derived_type&>(*this);
48  }
49  constexpr inline derived_type& derived() {
50  return static_cast<derived_type&>(*this);
51  }
52 };
53 
55 
56 namespace internal {
57 namespace math {
58 
59 template<typename Derived>
60 std::true_type is_matrix_impl(const vccc::MatrixBase<Derived>&);
61 std::false_type is_matrix_impl(...);
62 
63 } // namespace math
64 } // namespace internal
65 
66 template<typename T>
67 struct is_matrix : decltype(::vccc::internal::math::is_matrix_impl(std::declval<T>())) {};
68 
69 } // namespace vccc
70 
71 template<typename Derived>
72 struct std::tuple_size<vccc::MatrixBase<Derived>>
73  : std::integral_constant<std::size_t, vccc::MatrixBase<Derived>::size> {};
74 
75 namespace std {
76 
77 template<std::size_t I, typename Derived>
78 constexpr inline std::enable_if_t<(I < vccc::MatrixBase<Derived>::size), const typename vccc::MatrixBase<Derived>::value_type&>
80  return m[I];
81 }
82 
83 } // namespace std
84 
85 #endif // VCCC_MATH_MAT_EXPRESSION_HPP
Definition: matrix_base.hpp:20
constexpr value_type operator[](std::size_t i) const
Definition: matrix_base.hpp:42
constexpr derived_type & derived()
Definition: matrix_base.hpp:49
Derived derived_type
Definition: matrix_base.hpp:22
constexpr const derived_type & derived() const
Definition: matrix_base.hpp:46
constexpr value_type operator()(std::size_t i) const
static polymorphic virtual-like member functions
Definition: matrix_base.hpp:36
typename derived_traits::value_type value_type
Definition: matrix_base.hpp:24
internal::math::traits< derived_type > derived_traits
Definition: matrix_base.hpp:23
@ size
Definition: matrix_base.hpp:29
@ cols
Definition: matrix_base.hpp:28
@ rows
Definition: matrix_base.hpp:27
constexpr VCCC_INLINE_OR_STATIC detail::size_niebloid size
returns the size of a container or array
Definition: size.hpp:145
Definition: matrix.hpp:495
constexpr std::enable_if_t<(I< m *n), typename vccc::Matrix< T, m, n >::value_type & > get(vccc::Matrix< T, m, n > &matrix)
Definition: matrix.hpp:499
Definition: directory.h:12
Definition: matrix_base.hpp:67