VCCC  2024.05
VisualCamp Common C++ library
matrix_sub.hpp
Go to the documentation of this file.
1 # /*
2 # * Created by YongGyu Lee on 2020/02/04.
3 # */
4 #
5 # ifndef VCCC_MATH_MATRIX_MATRIX_SUB_HPP
6 # define VCCC_MATH_MATRIX_MATRIX_SUB_HPP
7 #
10 
11 namespace vccc {
12 
13 namespace internal {
14 namespace math {
15 
16 template<typename LhsType, typename RhsType>
17 struct traits<MatrixSub<LhsType, RhsType>> {
18  enum : int {
19  rows = LhsType::rows,
20  cols = RhsType::cols,
21  size = rows * cols,
22  };
23 
24  enum : int {
25  option = traits<LhsType>::option | traits<RhsType>::option | Flag::kReferenceUnsafe
26  };
27  using value_type = typename LhsType::value_type;
28 };
29 
30 } // namespace math
31 } // namespace internal
32 
35 
36 template<typename LhsType, typename RhsType>
37 class MatrixSub : public MatrixBase<MatrixSub<LhsType, RhsType>> {
38  public:
39  using lhs_type = internal::math::hold_type_selector_t<LhsType>;
40  using rhs_type = internal::math::hold_type_selector_t<RhsType>;
41 
42  using value_type = typename LhsType::value_type;
43 
44  constexpr MatrixSub(const LhsType& lhs, const RhsType& rhs) noexcept : lhs(lhs), rhs(rhs) {}
45 
46  constexpr value_type operator() (std::size_t i) const { return lhs(i) - rhs(i); }
47  constexpr value_type operator() (std::size_t i, std::size_t j) const { return lhs(i, j) - rhs(i, j); }
48  constexpr value_type operator[] (std::size_t i) const { return lhs[i] - rhs[i]; }
49 
50  private:
51  lhs_type lhs;
52  rhs_type rhs;
53 };
54 
56 MatrixSub<E1, E2>
57 operator-(const MatrixBase<E1>& lhs, const MatrixBase<E2>& rhs) noexcept {
58  return MatrixSub<E1, E2>{*static_cast<const E1*>(&lhs), *static_cast<const E2*>(&rhs)};
59 }
60 
62 
63 } // namespace vccc
64 
65 # endif // VCCC_MATH_MATRIX_MATRIX_SUB_HPP
Definition: matrix_base.hpp:20
Definition: matrix_sub.hpp:37
internal::math::hold_type_selector_t< RhsType > rhs_type
Definition: matrix_sub.hpp:40
constexpr MatrixSub(const LhsType &lhs, const RhsType &rhs) noexcept
Definition: matrix_sub.hpp:44
constexpr value_type operator[](std::size_t i) const
Definition: matrix_sub.hpp:48
internal::math::hold_type_selector_t< LhsType > lhs_type
Definition: matrix_sub.hpp:39
typename LhsType::value_type value_type
Definition: matrix_sub.hpp:42
constexpr value_type operator()(std::size_t i) const
Definition: matrix_sub.hpp:46
constexpr MatrixMinus< E > operator-(const MatrixBase< E > &lhs)
Definition: matrix_minus.hpp:52
Definition: directory.h:12
constexpr auto size(const C &c) -> decltype(c.size())
Definition: size.hpp:16
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35