NiHu  2.0
matrix_free.hpp
Go to the documentation of this file.
1 
7 #ifndef NIHU_MATRIX_FREE_HPP_INCLUDED
8 #define NIHU_MATRIX_FREE_HPP_INCLUDED
9 
10 #include "fmm_matrix.hpp"
11 #include "util/real_part_type.hpp"
12 
13 #include <Eigen/SparseCore>
14 
15 #include <complex>
16 #include <type_traits>
17 
18 
19 namespace NiHu
20 {
21 namespace fmm
22 {
23 
24 // Forward declaration
25 template <class FmmMatrix>
27 
28 } // end of namespace fmm
29 } // end of namespace NiHu
30 
31 namespace Eigen {
32 namespace internal {
33 // matrix_free looks-like a SparseMatrix, so let's inherits its traits:
34 template<class FmmMatrix>
35 struct traits<NiHu::fmm::matrix_free<FmmMatrix> >
36  : public Eigen::internal::traits<Eigen::SparseMatrix<typename FmmMatrix::scalar_t> >
37 {};
38 }
39 }
40 
41 namespace NiHu
42 {
43 namespace fmm
44 {
45 
53 template <class FmmMatrix>
54 class matrix_free
55  : public Eigen::EigenBase<matrix_free<FmmMatrix> >
56 {
57 public:
58  typedef FmmMatrix fmm_matrix_t;
59  typedef typename fmm_matrix_t::scalar_t scalar_t;
60 
61  typedef scalar_t Scalar;
62  typedef typename NiHu::real_part_type<Scalar>::type RealScalar;
63  typedef int StorageIndex;
64  enum {
65  ColsAtCompileTime = Eigen::Dynamic,
66  MaxColsAtCompileTime = Eigen::Dynamic,
67  IsRowMajor = false
68  };
69 
70  typedef Eigen::Matrix<scalar_t, Eigen::Dynamic, 1> vector_t;
71 
74  matrix_free(FmmMatrix &mat)
75  : m_mat(mat)
76  {
77  }
78 
81  Eigen::Index rows() const
82  {
83  return m_mat.rows();
84  }
85 
88  Eigen::Index cols() const
89  {
90  return m_mat.cols();
91  }
92 
97  template <class RHS>
98  typename FmmMatrix::response_t
99  operator*(Eigen::MatrixBase<RHS> const &rhs) const
100  {
101  return m_mat * rhs;
102  }
103 
106  vector_t get_diagonal() const
107  {
108  return m_mat.get_diagonal();
109  }
110 
111 private:
112  fmm_matrix_t &m_mat;
113 };
114 
115 
122 template <class FmmMatrix>
123 matrix_free<FmmMatrix>
124 create_matrix_free(FmmMatrix &fmm_mtx)
125 {
126  return matrix_free<FmmMatrix>(fmm_mtx);
127 }
128 
129 } // end of namespace fmm
130 } // end of namespace NiHu
131 
132 #endif /* NIHU_MATRIX_FREE_HPP_INCLUDED */
NiHu::fmm::create_matrix_free
matrix_free< FmmMatrix > create_matrix_free(FmmMatrix &fmm_mtx)
Create function for the matrix_free class.
Definition: matrix_free.hpp:124
NiHu::fmm::matrix_free::cols
Eigen::Index cols() const
return the number of columns
Definition: matrix_free.hpp:88
NiHu::fmm::matrix_free::operator*
FmmMatrix::response_t operator*(Eigen::MatrixBase< RHS > const &rhs) const
compute MVP
Definition: matrix_free.hpp:99
NiHu::fmm::matrix_free
An Eigen::Matrix adaptor for the fmm_matrix class.
Definition: matrix_free.hpp:26
NiHu::fmm::matrix_free::get_diagonal
vector_t get_diagonal() const
return the diagonal of the matrix
Definition: matrix_free.hpp:106
NiHu::fmm::matrix_free::matrix_free
matrix_free(FmmMatrix &mat)
construct a new matrix_free instance
Definition: matrix_free.hpp:74
fmm_matrix.hpp
Class NiHu::fmm::fmm_matrix.
NiHu::fmm::matrix_free::rows
Eigen::Index rows() const
return the number of rows
Definition: matrix_free.hpp:81