12#ifndef BELFEM_CL_AR_MATRIX_HPP
13#define BELFEM_CL_AR_MATRIX_HPP
27 template<
typename T >
58 const size_t aNumCols ) :
59 mMatrix( aNumRows, aNumCols ) {}
67 const size_t aNumCols,
69 mMatrix( aNumRows, aNumCols )
79 Matrix(
const std::initializer_list<std::initializer_list<T> > & aInitList )
80 : mMatrix( aInitList )
91 : mMatrix( aExpression ) {}
98 template<
typename ET,
typename OP >
99 Matrix(
const arma::Op<ET, OP> & aExpression )
100 : mMatrix( aExpression ) {}
108 mMatrix( aMatrix.mMatrix )
115 mMatrix( std::move( aMatrix.mMatrix ) )
137 return mMatrix.memptr();
148 return mMatrix.memptr();
180 mMatrix.fill( aValue );
186 set_size(
const size_t aNumRows,
const size_t aNumCols )
188 mMatrix.set_size( aNumRows, aNumCols );
194 set_size(
const size_t aNumRows,
const size_t aNumCols,
const T & aValue )
196 this->
set_size( aNumRows, aNumCols );
197 this->
fill( aValue );
207 return mMatrix.n_rows;
215 return mMatrix.n_cols;
226 return mMatrix.n_elem;
238 return mMatrix.n_rows;
251 if (
this != & aMatrix )
253 mMatrix = std::move( aMatrix.mMatrix );
264 if (
this != &aMatrix )
266 mMatrix = aMatrix.mMatrix;
281 "Row index %lu out of bounds, which must be smaller than %lu.",
282 (
long unsigned int ) aRowIndex,
283 (
long unsigned int ) this->
n_rows() );
286 "Col index %lu out of bounds, which must be smaller than %lu.",
287 (
long unsigned int ) aColIndex,
288 (
long unsigned int ) this->
n_cols() );
290 return mMatrix( aRowIndex, aColIndex );
298 virtual inline const T &
299 operator()(
const size_t aRowIndex,
const size_t aColIndex )
const
302 "Row index %lu out of bounds, which must be smaller than %lu.",
303 (
long unsigned int ) aRowIndex,
304 (
long unsigned int ) this->
n_rows() );
307 "Col index %lu out of bounds, which must be smaller than %lu.",
308 (
long unsigned int ) aColIndex,
309 (
long unsigned int ) this->
n_cols() );
311 return mMatrix( aRowIndex, aColIndex );
319 row(
const size_t aRowIndex )
320 ->decltype( mMatrix.row( aRowIndex ) )
323 "Row index %lu out of bounds, which must be less than %lu.",
324 (
long unsigned int ) aRowIndex,
325 (
long unsigned int ) this->
n_rows() );
327 return mMatrix.row( aRowIndex );
333 row(
const size_t aRowIndex )
const
334 ->decltype( mMatrix.row( aRowIndex ) )
337 "Row index %lu out of bounds, which must be less than %lu.",
338 (
long unsigned int ) aRowIndex,
339 (
long unsigned int ) this->
n_rows() );
341 return mMatrix.row( aRowIndex );
347 col(
const size_t aColIndex )
348 ->decltype( mMatrix.col( aColIndex ) )
351 "Col index %lu out of bounds, which must be less than %lu.",
352 (
long unsigned int ) aColIndex,
353 (
long unsigned int ) this->
n_cols() );
355 return mMatrix.col( aColIndex );
361 col(
const size_t aColIndex )
const
362 ->decltype( mMatrix.col( aColIndex ) )
365 "Col index %lu out of bounds, which must be less than %lu.",
366 (
long unsigned int ) aColIndex,
367 (
long unsigned int ) this->
n_cols() );
369 return mMatrix.col( aColIndex );
376 const size_t aFirstCol,
377 const size_t aLastRow,
378 const size_t aLastCol )
379 ->
decltype( mMatrix.submat( aFirstRow, aFirstCol, aLastRow, aLastCol ) )
382 "First row index %lu out of bounds, which must be less than %lu.",
383 (
long unsigned int ) aFirstRow,
384 (
long unsigned int ) this->
n_rows() );
387 "Last row index %lu out of bounds, which must be less than %lu.",
388 (
long unsigned int ) aLastRow,
389 (
long unsigned int ) this->
n_rows() );
392 "First col index %lu out of bounds, which must be less than %lu.",
393 (
long unsigned int ) aFirstCol,
394 (
long unsigned int ) this->
n_cols() );
397 "Last col index %lu out of bounds, which must be less than %lu.",
398 (
long unsigned int ) aLastCol,
399 (
long unsigned int ) this->
n_cols() );
401 return mMatrix.submat( aFirstRow, aFirstCol, aLastRow, aLastCol );
408 const size_t aFirstCol,
409 const size_t aLastRow,
410 const size_t aLastCol )
const
411 ->
decltype( mMatrix.submat( aFirstRow, aFirstCol, aLastRow, aLastCol ) )
414 "First row index %lu out of bounds, which must be less than %lu.",
415 (
long unsigned int ) aFirstRow,
416 (
long unsigned int ) this->
n_rows() );
419 "Last row index %lu out of bounds, which must be less than %lu.",
420 (
long unsigned int ) aLastRow,
421 (
long unsigned int ) this->
n_rows() );
424 "First col index %lu out of bounds, which must be less than %lu.",
425 (
long unsigned int ) aFirstCol,
426 (
long unsigned int ) this->
n_cols() );
429 "Last col index %lu out of bounds, which must be less than %lu.",
430 (
long unsigned int ) aLastCol,
431 (
long unsigned int ) this->
n_cols() );
433 return mMatrix.submat( aFirstRow, aFirstCol, aLastRow, aLastCol );
439 set_row(
const size_t aRowIndex,
const Vector <T> & aVector )
443 "Wrong length of vector : %lu ( expect %lu )",
444 (
long unsigned int ) aVector.
length(),
445 (
long unsigned int ) this->n_cols() );
450 template <
typename ET >
452 set_row(
const size_t aColIndex,
const ET & aExpression )
454 this->
row( aColIndex ) = aExpression;
460 set_col(
const size_t aColIndex,
const Vector <T> & aVector )
464 "Wrong length of vector : %lu ( expect %lu )",
465 (
long unsigned int ) aVector.
length(),
466 (
long unsigned int ) this->n_rows() );
471 template <
typename ET >
473 set_col(
const size_t aColIndex,
const ET & aExpression )
475 this->
col( aColIndex ) = aExpression;
489 this->
fill( aValue );
498 template <
typename ET >
502 mMatrix = aExpression;
529 mMatrix += aMatrix.matrix_data();
538 mMatrix += aExpression;
558 mMatrix -= aMatrix.matrix_data();
567 mMatrix -= aExpression;
587 mMatrix *= aMatrix.matrix_data();
596 mMatrix *= aExpression;
616 print(
const std::string aLabel=
"Matrix" )
const;
624#pragma GCC diagnostic push
625#pragma GCC diagnostic ignored "-Wformat"
627 template <
typename T >
void
630 FILE * tOutFile = stdout;
632 fprintf( tOutFile,
"%s = [ ... \n", aLabel.c_str() );
637 uint tRows = tThis.n_rows();
638 uint tCols = tThis.n_cols();
640 for(
uint i=0; i< tRows; ++i )
642 for(
uint j=0; j< tCols; ++j )
646 fprintf( tOutFile,
"%d, ", (
int ) tThis( i, j ) );
650 fprintf( tOutFile,
"%d; ", (
int ) tThis( i, j ) );
656 fprintf( tOutFile,
"...\n" );
660 fprintf( tOutFile,
"];\n" );
671 FILE * tOutFile = stdout;
673 fprintf( tOutFile,
"%s = [ ... \n", aLabel.c_str() );
680 for(
uint i=0; i< tRows; ++i )
682 for(
uint j=0; j< tCols; ++j )
686 fprintf( tOutFile,
"%+.15e, ", (
double ) tThis( i, j ) );
690 fprintf( tOutFile,
"%+.15e; ", (
double ) tThis( i, j ) );
696 fprintf( tOutFile,
"...\n" );
700 fprintf( tOutFile,
"];\n" );
707#pragma GCC diagnostic pop
#define BELFEM_ASSERT(aCheck,...)
Definition assert.hpp:244
Dense column-major matrix.
Definition cl_BZ_Matrix.hpp:28
Matrix< T > & operator=(Matrix< T > &&aMatrix) noexcept
move assignment operator
Definition cl_AR_Matrix.hpp:249
size_t spacing() const
inter-column stride of the data container ( the leading dimension in BLAS terms; here always n_rows )
Definition cl_AR_Matrix.hpp:236
Matrix< T > & operator+=(const T &aValue)
Definition cl_AR_Matrix.hpp:511
auto col(const size_t aColIndex) const -> decltype(mMatrix.col(aColIndex))
Definition cl_AR_Matrix.hpp:361
void set_size(const size_t aNumRows, const size_t aNumCols, const T &aValue)
Definition cl_AR_Matrix.hpp:194
virtual ~Matrix()=default
empty destructor
auto col(const size_t aColIndex) -> decltype(mMatrix.col(aColIndex))
Definition cl_AR_Matrix.hpp:347
auto submat(const size_t aFirstRow, const size_t aFirstCol, const size_t aLastRow, const size_t aLastCol) const -> decltype(mMatrix.submat(aFirstRow, aFirstCol, aLastRow, aLastCol))
Definition cl_AR_Matrix.hpp:407
const MatrixType & matrix_data() const
Definition cl_AR_Matrix.hpp:168
void fill(const T &aValue)
Definition cl_AR_Matrix.hpp:178
auto submat(const size_t aFirstRow, const size_t aFirstCol, const size_t aLastRow, const size_t aLastCol) -> decltype(mMatrix.submat(aFirstRow, aFirstCol, aLastRow, aLastCol))
Definition cl_AR_Matrix.hpp:375
auto row(const size_t aRowIndex) -> decltype(mMatrix.row(aRowIndex))
Definition cl_AR_Matrix.hpp:319
Matrix< T > & operator/=(const T &aValue)
Definition cl_AR_Matrix.hpp:605
Matrix(const size_t aNumRows, const size_t aNumCols)
Constructor without fill value.
Definition cl_AR_Matrix.hpp:57
void set_row(const size_t aRowIndex, const Vector< T > &aVector)
Definition cl_AR_Matrix.hpp:439
MatrixType & matrix_data()
Definition cl_AR_Matrix.hpp:157
size_t capacity() const
length of data container
Definition cl_AR_Matrix.hpp:224
Matrix< T > & operator-=(const T &aValue)
Definition cl_AR_Matrix.hpp:547
Matrix(const MatrixType &aExpression)
Constructor from expression.
Definition cl_AR_Matrix.hpp:90
Matrix< T > & operator-=(const MatrixType &aExpression)
Definition cl_AR_Matrix.hpp:565
Matrix(const arma::Op< ET, OP > &aExpression)
Constructor from expression.
Definition cl_AR_Matrix.hpp:99
Matrix(const size_t aNumRows, const size_t aNumCols, const real aValue)
Constructor with fill value.
Definition cl_AR_Matrix.hpp:66
void set_size(const size_t aNumRows, const size_t aNumCols)
Definition cl_AR_Matrix.hpp:186
size_t n_rows() const
Definition cl_AR_Matrix.hpp:205
void set_col(const size_t aColIndex, const Vector< T > &aVector)
Definition cl_AR_Matrix.hpp:460
Matrix(const Matrix< T > &aMatrix)
copy constructor
Definition cl_AR_Matrix.hpp:107
Matrix< T > & operator=(const Matrix< T > &aMatrix)
copy assignment operator
Definition cl_AR_Matrix.hpp:262
T & operator()(const size_t aRowIndex, const size_t aColIndex)
access operator ( writable version )
Definition cl_AR_Matrix.hpp:278
void set_row(const size_t aColIndex, const ET &aExpression)
Definition cl_AR_Matrix.hpp:452
arma::Mat< T > MatrixType
Definition cl_AR_Matrix.hpp:34
Matrix< T > & operator=(const T &aValue)
Fill with value.
Definition cl_AR_Matrix.hpp:487
Matrix< T > & operator-=(const Matrix< T > &aMatrix)
Definition cl_AR_Matrix.hpp:556
Matrix()=default
empty constructor
virtual const T & operator()(const size_t aRowIndex, const size_t aColIndex) const
access operator ( const version )
Definition cl_AR_Matrix.hpp:299
size_t n_cols() const
Definition cl_AR_Matrix.hpp:213
Matrix(Matrix< T > &&aMatrix) noexcept
move constructor
Definition cl_AR_Matrix.hpp:114
Matrix(const std::initializer_list< std::initializer_list< T > > &aInitList)
Constructor with initializer list.
Definition cl_AR_Matrix.hpp:79
Matrix< T > & operator+=(T &aValue)
Definition cl_AR_Matrix.hpp:518
Matrix< T > & operator*=(const MatrixType &aExpression)
Definition cl_AR_Matrix.hpp:594
Matrix< T > & operator+=(const MatrixType &aExpression)
Definition cl_AR_Matrix.hpp:536
Matrix< T > & operator*=(const Matrix< T > &aMatrix)
Definition cl_AR_Matrix.hpp:585
Matrix< T > & operator*=(const T &aValue)
Definition cl_AR_Matrix.hpp:576
const T * data() const
Definition cl_AR_Matrix.hpp:146
void print(const std::string aLabel="Matrix") const
Definition cl_AR_Matrix.hpp:628
T * data()
Definition cl_AR_Matrix.hpp:135
Matrix< T > & operator+=(const Matrix< T > &aMatrix)
Definition cl_AR_Matrix.hpp:527
Matrix< T > & operator=(const ET &aExpression)
Fill with expression.
Definition cl_AR_Matrix.hpp:500
void set_col(const size_t aColIndex, const ET &aExpression)
Definition cl_AR_Matrix.hpp:473
auto row(const size_t aRowIndex) const -> decltype(mMatrix.row(aRowIndex))
Definition cl_AR_Matrix.hpp:333
size_t length() const
get the length of the vector
Definition cl_AR_Vector.hpp:257
VectorType & vector_data()
expose the underlying matrix implementation ( writable version )
Definition cl_AR_Vector.hpp:204
USER GUIDES:
Definition cl_Capacitor.cpp:16
unsigned int uint
Definition typedefs.hpp:30
double real
Definition typedefs.hpp:36