VCCC  2024.05
VisualCamp Common C++ library
matrix_sum.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_SUM_HPP
6 # define VCCC_MATH_MATRIX_MATRIX_SUM_HPP
7 #
10 
11 namespace vccc {
12 
13 namespace internal {
14 namespace math {
15 
16 template<typename LhsType, typename RhsType>
17 struct traits<MatrixSum<LhsType, RhsType>> {
18  enum : int {
19  rows = traits<LhsType>::rows,
20  cols = traits<LhsType>::cols,
21  size = rows * cols,
22  };
23 
24  enum : int {
25  option = static_cast<int>(traits<LhsType>::option) | static_cast<int>(traits<RhsType>::option) | static_cast<int>(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 MatrixSum : public MatrixBase<MatrixSum<LhsType, RhsType>> {
38  public:
39 
40  using lhs_type = internal::math::hold_type_selector_t<LhsType>;
41  using rhs_type = internal::math::hold_type_selector_t<RhsType>;
42 
43  using value_type = typename LhsType::value_type;
44 
45  constexpr MatrixSum(const LhsType& lhs, const RhsType& rhs) : lhs(lhs), rhs(rhs) {};
46 
47  constexpr value_type operator() (std::size_t i) const { return lhs(i) + rhs(i); }
48  constexpr value_type operator() (std::size_t i, std::size_t j) const { return lhs(i, j) + rhs(i, j); }
49  constexpr value_type operator[] (std::size_t i) const { return lhs[i] + rhs[i]; }
50 
51  private:
52  lhs_type lhs;
53  rhs_type rhs;
54 };
55 
57 constexpr inline
58 MatrixSum<E1, E2>
59 operator+(const MatrixBase<E1>& lhs, const MatrixBase<E2>& rhs) {
60  return MatrixSum<E1, E2>(*static_cast<const E1*>(&lhs), *static_cast<const E2*>(&rhs));
61 }
62 
64 
65 } // namespace vccc
66 
67 # endif // VCCC_MATH_MATRIX_MATRIX_SUM_HPP
Definition: matrix_base.hpp:20
Definition: matrix_sum.hpp:37
internal::math::hold_type_selector_t< RhsType > rhs_type
Definition: matrix_sum.hpp:41
constexpr value_type operator[](std::size_t i) const
Definition: matrix_sum.hpp:49
internal::math::hold_type_selector_t< LhsType > lhs_type
Definition: matrix_sum.hpp:40
typename LhsType::value_type value_type
Definition: matrix_sum.hpp:43
constexpr value_type operator()(std::size_t i) const
Definition: matrix_sum.hpp:47
constexpr MatrixSum(const LhsType &lhs, const RhsType &rhs)
Definition: matrix_sum.hpp:45
constexpr MatrixSum< E1, E2 > operator+(const MatrixBase< E1 > &lhs, const MatrixBase< E2 > &rhs)
Definition: matrix_sum.hpp:59
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