NiHu  2.0
mex_matrix_separate.hpp
1 // This file is a part of NiHu, a C++ BEM template library.
2 //
3 // Copyright (C) 2012-2014 Peter Fiala <fiala@hit.bme.hu>
4 // Copyright (C) 2012-2014 Peter Rucz <rucz@hit.bme.hu>
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
30 #ifndef MEX_MATRIX_SEPARATE_HPP_INCLUDED
31 #define MEX_MATRIX_SEPARATE_HPP_INCLUDED
32 
33 #include "eigen_utils.hpp"
34 #include "../core/result_matrix.hpp"
35 
36 #include <complex>
37 #include <cstddef>
38 #include <type_traits>
39 
40 namespace NiHu
41 {
42 
48 namespace mex {
49 
52 {
53 protected:
59  matrix_base(size_t rows, size_t cols)
60  : m_rows(rows), m_cols(cols)
61  {
62  }
63 
68  matrix_base(mxArray const *input)
69  : m_rows(mxGetM(input))
70  , m_cols(mxGetN(input))
71  {
72  }
73 
74 public:
78  size_t rows(void) const
79  {
80  return m_rows;
81  }
82 
86  size_t cols(void) const
87  {
88  return m_cols;
89  }
90 
91 protected:
93  size_t m_rows;
95  size_t m_cols;
96 };
97 
98 
100 template <class T>
101 struct classID;
102 
104 template <>
105 struct classID<int>
106 {
108  static mxClassID const value = mxINT32_CLASS;
109 };
110 
112 template <>
113 struct classID<double>
114 {
116  static mxClassID const value = mxDOUBLE_CLASS;
117 };
118 
120 template <>
121 struct classID<float>
122 {
124  static mxClassID const value = mxSINGLE_CLASS;
125 };
126 
127 
132 template <class T>
133 class real_matrix :
134  public Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> >
135 {
136 public:
137  typedef Eigen::Map< Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> > base_t;
138 
140  typedef T scalar_t;
141 
148  real_matrix(size_t rows, size_t cols, mxArray *&output)
149  : base_t(
150  static_cast<scalar_t *>(mxGetData(output = mxCreateNumericMatrix(rows, cols, classID<scalar_t>::value, mxREAL))),
151  rows, cols)
152  {
153  }
154 
159  real_matrix(mxArray const *input)
160  : base_t(static_cast<scalar_t *>(mxGetData(input)), mxGetM(input), mxGetN(input))
161  {
162  }
163 };
164 
166 template <class Parent>
168 {
169 public:
171  typedef typename Parent::scalar_t scalar_t;
172 
178  index_proxy(Parent &matrix, size_t row, size_t col)
179  : m_parent(matrix), m_row(row), m_col(col)
180  {
181  }
182 
186  operator std::complex<scalar_t>() const
187  {
188  return std::complex<scalar_t>(real(), imag());
189  }
190 
194  scalar_t &real(void) const
195  {
196  return m_parent.m_real[m_row + m_parent.rows()*m_col];
197  }
198 
202  scalar_t &imag(void) const
203  {
204  return m_parent.m_imag[m_row + m_parent.rows()*m_col];
205  }
206 
210  template <class complex_rhs_t>
211  void operator =(complex_rhs_t const &data) const
212  {
213  real() = data.real();
214  imag() = data.imag();
215  }
216 
220  template <class complex_rhs_t>
221  void operator +=(complex_rhs_t const &data) const
222  {
223  real() += data.real();
224  imag() += data.imag();
225  }
226 
230  void operator =(scalar_t const &data)
231  {
232  real() = data;
233  imag() = 0.0;
234  }
235 
239  void operator +=(scalar_t const &data)
240  {
241  real() += data;
242  }
243 
244 private:
246  Parent &m_parent;
248  size_t const m_row;
250  size_t const m_col;
251 };
252 
253 
255 template <class T>
257  public matrix_base
258 {
259 public:
260  friend class index_proxy<complex_matrix<T> >;
262  typedef T scalar_t;
263 
269  complex_matrix(size_t rows, size_t cols, mxArray *&output)
270  : matrix_base(rows, cols)
271  {
272  output = mxCreateNumericMatrix(
273  m_rows, m_cols, classID<scalar_t>::value, mxCOMPLEX);
274  m_real = static_cast<scalar_t *>(mxGetData(output));
275  m_imag = static_cast<scalar_t *>(mxGetImagData(output));
276  }
277 
281  complex_matrix(mxArray const *input)
282  : matrix_base(input),
283  m_real(static_cast<scalar_t *>(mxGetData(input))),
284  m_imag(static_cast<scalar_t *>(mxGetImagData(input)))
285  {
286  }
287 
293  std::complex<scalar_t> operator() (size_t row, size_t col) const
294  {
295  return std::complex<scalar_t>(
296  m_real[row+m_rows*col], m_imag[row+m_rows*col]);
297  }
298 
304  index_proxy<complex_matrix> operator() (size_t row, size_t col)
305  {
306  return index_proxy<complex_matrix>(*this, row, col);
307  }
308 
309 protected:
314 };
315 
316 template <class Scalar>
317 Scalar const &get_scalar(mxArray const *pa)
318 {
319  return *static_cast<Scalar *>(mxGetPr(pa));
320 }
321 
322 
323 } // end of namespace mex
324 
325 
327 template <class T>
328 struct is_result_matrix_impl<mex::real_matrix<T> > : std::true_type {};
329 
331 template <class T>
332 struct is_result_matrix_impl<mex::complex_matrix<T> > : std::true_type {};
333 
334 } // end of namespace NiHu
335 
336 #endif /* MEX_MATRIX_SEPARATE_HPP_INCLUDED */
337 
NiHu::mex::real_matrix::scalar_t
T scalar_t
the scalar type
Definition: mex_matrix_separate.hpp:140
NiHu::mex::complex_matrix::complex_matrix
complex_matrix(size_t rows, size_t cols, mxArray *&output)
output matrix (allocating) constructor
Definition: mex_matrix_separate.hpp:269
NiHu::mex::real_matrix::real_matrix
real_matrix(size_t rows, size_t cols, mxArray *&output)
output matrix (allocating) constructor
Definition: mex_matrix_separate.hpp:148
NiHu::mex::matrix_base::m_rows
size_t m_rows
number of rows
Definition: mex_matrix_separate.hpp:93
NiHu::mex::complex_matrix::scalar_t
T scalar_t
the real scalar type
Definition: mex_matrix_separate.hpp:262
NiHu::mex::classID
metafunction assigning a Matlab class ID to a C type
Definition: mex_matrix_interleaved.hpp:54
NiHu::mex::index_proxy::scalar_t
Parent::scalar_t scalar_t
the scalar type of the parent class
Definition: mex_matrix_separate.hpp:171
NiHu::is_result_matrix_impl
Definition: result_matrix.hpp:35
NiHu::mex::matrix_base::m_cols
size_t m_cols
number of columns
Definition: mex_matrix_separate.hpp:95
eigen_utils.hpp
Implementation of Eigen related utility classes.
NiHu::mex::matrix_base::cols
size_t cols(void) const
return number of columns
Definition: mex_matrix_separate.hpp:86
NiHu::mex::complex_matrix::m_real
scalar_t * m_real
array of real data
Definition: mex_matrix_separate.hpp:311
NiHu::mex::index_proxy::operator+=
void operator+=(complex_rhs_t const &data) const
increment operator
Definition: mex_matrix_separate.hpp:221
NiHu::mex::index_proxy::index_proxy
index_proxy(Parent &matrix, size_t row, size_t col)
constructor
Definition: mex_matrix_separate.hpp:178
NiHu::mex::complex_matrix
Container class of a complex matrix stored in Matlab format.
Definition: mex_matrix_separate.hpp:256
NiHu::mex::index_proxy::operator=
void operator=(complex_rhs_t const &data) const
assignment operator
Definition: mex_matrix_separate.hpp:211
NiHu::mex::real_matrix
Definition: mex_matrix_separate.hpp:133
NiHu::mex::matrix
Matlab mex matrix.
Definition: mex_matrix_interleaved.hpp:151
NiHu::mex::index_proxy
index proxy class of a complex matrix
Definition: mex_matrix_separate.hpp:167
NiHu::mex::index_proxy::imag
scalar_t & imag(void) const
return reference to the imaginary part
Definition: mex_matrix_separate.hpp:202
NiHu::mex::matrix_base
base class of a Matlab mex matrix
Definition: mex_matrix_separate.hpp:51
NiHu::mex::complex_matrix::complex_matrix
complex_matrix(mxArray const *input)
input matrix constructor
Definition: mex_matrix_separate.hpp:281
NiHu::mex::complex_matrix::operator()
std::complex< scalar_t > operator()(size_t row, size_t col) const
index operator that returns a complex number
Definition: mex_matrix_separate.hpp:293
NiHu::mex::index_proxy::real
scalar_t & real(void) const
return reference to the real part
Definition: mex_matrix_separate.hpp:194
NiHu::mex::matrix_base::rows
size_t rows(void) const
return number of rows
Definition: mex_matrix_separate.hpp:78
NiHu::mex::real_matrix::real_matrix
real_matrix(mxArray const *input)
input matrix constructor
Definition: mex_matrix_separate.hpp:159
NiHu::mex::matrix_base::matrix_base
matrix_base(size_t rows, size_t cols)
output matrix constructor
Definition: mex_matrix_separate.hpp:59
NiHu::mex::matrix_base::matrix_base
matrix_base(mxArray const *input)
input matrix constructor
Definition: mex_matrix_separate.hpp:68
NiHu::mex::complex_matrix::m_imag
scalar_t * m_imag
array of imaginary data
Definition: mex_matrix_separate.hpp:313