NiHu  2.0
kron_identity.hpp
Go to the documentation of this file.
1 
7 #ifndef NIHU_KRON_IDENTITY_HPP_INCLUDED
8 #define NIHU_KRON_IDENTITY_HPP_INCLUDED
9 
10 #include <Eigen/Dense>
11 
12 #include <type_traits>
13 #include <utility>
14 
15 namespace NiHu
16 {
17 namespace fmm
18 {
19 
20 template <class Lhs, size_t Dim>
22 {
23 public:
24  typedef typename std::decay<Lhs>::type lhs_t;
25  static unsigned const dimension = Dim;
26  typedef typename lhs_t::Scalar scalar_t;
27  static Eigen::Index const lhs_rows_compile_time = lhs_t::RowsAtCompileTime;
28  static Eigen::Index const result_rows_compile_time =
29  lhs_rows_compile_time == Eigen::Dynamic ? Eigen::Dynamic : dimension * lhs_rows_compile_time;
30 
32  {
33  }
34 
35  explicit kron_identity(Lhs &&lhs)
36  : m_lhs(std::forward<Lhs>(lhs))
37  {
38  }
39 
40  template <class RhsDerived>
41  Eigen::Matrix<scalar_t, result_rows_compile_time, RhsDerived::ColsAtCompileTime>
42  operator*(Eigen::MatrixBase<RhsDerived> const &rhs) const
43  {
44  // allocate result
45  Eigen::Matrix<scalar_t, result_rows_compile_time, RhsDerived::ColsAtCompileTime> res(m_lhs.rows() * dimension, rhs.cols());
46  res.setZero();
47  for (Eigen::Index i = 0; i < m_lhs.rows(); ++i)
48  for (Eigen::Index j = 0; j < m_lhs.cols(); ++j)
49  res.block(i * dimension, 0, dimension, rhs.cols()) +=
50  m_lhs(i, j) * rhs.block(j * dimension, 0, dimension, rhs.cols());
51  return res;
52  }
53 
54 private:
55  Lhs m_lhs;
56 };
57 
58 template <size_t Dim, class Lhs>
59 kron_identity<Lhs, Dim> create_kron_identity(Lhs &&lhs)
60 {
61  return kron_identity<Lhs, Dim>(std::forward<Lhs>(lhs));
62 }
63 
64 } // end of namespace fmm
65 } // end of namespace NiHu
66 
67 #endif /* NIHU_KRON_IDENTITY_HPP_INCLUDED */
NiHu::fmm::kron_identity
Definition: kron_identity.hpp:21