12#ifndef BELFEM_CL_AR_MATRIX_HPP
13#define BELFEM_CL_AR_MATRIX_HPP
27 template<
typename T >
52 const size_t aNumCols ) :
53 mMatrix( aNumRows, aNumCols ) {}
58 const size_t aNumCols,
60 mMatrix( aNumRows, aNumCols )
67 Matrix(
const std::initializer_list<std::initializer_list<T> > & aInitList )
68 : mMatrix( aInitList )
76 : mMatrix( aExpression ) {}
84 template<
typename ET >
85 Matrix(
const arma::Base< T, ET > & aExpression )
86 : mMatrix( aExpression.get_ref() ) {}
94 mMatrix( aMatrix.mMatrix )
101 mMatrix( std::move( aMatrix.mMatrix ) )
122 return mMatrix.memptr();
130 return mMatrix.memptr();
162 mMatrix.fill( aValue );
168 set_size(
const size_t aNumRows,
const size_t aNumCols )
170 mMatrix.set_size( aNumRows, aNumCols );
176 set_size(
const size_t aNumRows,
const size_t aNumCols,
const T & aValue )
178 this->
set_size( aNumRows, aNumCols );
179 this->
fill( aValue );
189 return mMatrix.n_rows;
197 return mMatrix.n_cols;
208 return mMatrix.n_elem;
220 return mMatrix.n_rows;
233 if (
this != & aMatrix )
235 mMatrix = std::move( aMatrix.mMatrix );
246 if (
this != &aMatrix )
248 mMatrix = aMatrix.mMatrix;
263 "Row index %lu out of bounds, which must be smaller than %lu.",
264 (
long unsigned int ) aRowIndex,
265 (
long unsigned int ) this->
n_rows() );
268 "Col index %lu out of bounds, which must be smaller than %lu.",
269 (
long unsigned int ) aColIndex,
270 (
long unsigned int ) this->
n_cols() );
272 return mMatrix( aRowIndex, aColIndex );
281 operator()(
const size_t aRowIndex,
const size_t aColIndex )
const
284 "Row index %lu out of bounds, which must be smaller than %lu.",
285 (
long unsigned int ) aRowIndex,
286 (
long unsigned int ) this->
n_rows() );
289 "Col index %lu out of bounds, which must be smaller than %lu.",
290 (
long unsigned int ) aColIndex,
291 (
long unsigned int ) this->
n_cols() );
293 return mMatrix( aRowIndex, aColIndex );
301 row(
const size_t aRowIndex )
302 ->decltype( mMatrix.row( aRowIndex ) )
305 "Row index %lu out of bounds, which must be less than %lu.",
306 (
long unsigned int ) aRowIndex,
307 (
long unsigned int ) this->
n_rows() );
309 return mMatrix.row( aRowIndex );
315 row(
const size_t aRowIndex )
const
316 ->decltype( mMatrix.row( aRowIndex ) )
319 "Row index %lu out of bounds, which must be less than %lu.",
320 (
long unsigned int ) aRowIndex,
321 (
long unsigned int ) this->
n_rows() );
323 return mMatrix.row( aRowIndex );
329 col(
const size_t aColIndex )
330 ->decltype( mMatrix.col( aColIndex ) )
333 "Col index %lu out of bounds, which must be less than %lu.",
334 (
long unsigned int ) aColIndex,
335 (
long unsigned int ) this->
n_cols() );
337 return mMatrix.col( aColIndex );
343 col(
const size_t aColIndex )
const
344 ->decltype( mMatrix.col( aColIndex ) )
347 "Col index %lu out of bounds, which must be less than %lu.",
348 (
long unsigned int ) aColIndex,
349 (
long unsigned int ) this->
n_cols() );
351 return mMatrix.col( aColIndex );
358 const size_t aFirstCol,
359 const size_t aLastRow,
360 const size_t aLastCol )
361 ->
decltype( mMatrix.submat( aFirstRow, aFirstCol, aLastRow, aLastCol ) )
364 "First row index %lu out of bounds, which must be less than %lu.",
365 (
long unsigned int ) aFirstRow,
366 (
long unsigned int ) this->
n_rows() );
369 "Last row index %lu out of bounds, which must be less than %lu.",
370 (
long unsigned int ) aLastRow,
371 (
long unsigned int ) this->
n_rows() );
374 "First col index %lu out of bounds, which must be less than %lu.",
375 (
long unsigned int ) aFirstCol,
376 (
long unsigned int ) this->
n_cols() );
379 "Last col index %lu out of bounds, which must be less than %lu.",
380 (
long unsigned int ) aLastCol,
381 (
long unsigned int ) this->
n_cols() );
383 return mMatrix.submat( aFirstRow, aFirstCol, aLastRow, aLastCol );
390 const size_t aFirstCol,
391 const size_t aLastRow,
392 const size_t aLastCol )
const
393 ->
decltype( mMatrix.submat( aFirstRow, aFirstCol, aLastRow, aLastCol ) )
396 "First row index %lu out of bounds, which must be less than %lu.",
397 (
long unsigned int ) aFirstRow,
398 (
long unsigned int ) this->
n_rows() );
401 "Last row index %lu out of bounds, which must be less than %lu.",
402 (
long unsigned int ) aLastRow,
403 (
long unsigned int ) this->
n_rows() );
406 "First col index %lu out of bounds, which must be less than %lu.",
407 (
long unsigned int ) aFirstCol,
408 (
long unsigned int ) this->
n_cols() );
411 "Last col index %lu out of bounds, which must be less than %lu.",
412 (
long unsigned int ) aLastCol,
413 (
long unsigned int ) this->
n_cols() );
415 return mMatrix.submat( aFirstRow, aFirstCol, aLastRow, aLastCol );
421 set_row(
const size_t aRowIndex,
const Vector <T> & aVector )
425 "Wrong length of vector : %lu ( expect %lu )",
426 (
long unsigned int ) aVector.
length(),
427 (
long unsigned int ) this->n_cols() );
432 template <
typename ET >
434 set_row(
const size_t aColIndex,
const ET & aExpression )
436 this->
row( aColIndex ) = aExpression;
442 set_col(
const size_t aColIndex,
const Vector <T> & aVector )
446 "Wrong length of vector : %lu ( expect %lu )",
447 (
long unsigned int ) aVector.
length(),
448 (
long unsigned int ) this->n_rows() );
453 template <
typename ET >
455 set_col(
const size_t aColIndex,
const ET & aExpression )
457 this->
col( aColIndex ) = aExpression;
471 this->
fill( aValue );
480 template <
typename ET >
484 mMatrix = aExpression;
511 mMatrix += aMatrix.matrix_data();
520 mMatrix += aExpression;
531 template<
typename ET >
535 mMatrix += aExpression.get_ref();
555 mMatrix -= aMatrix.matrix_data();
564 mMatrix -= aExpression;
570 template<
typename ET >
574 mMatrix -= aExpression.get_ref();
594 mMatrix *= aMatrix.matrix_data();
603 mMatrix *= aExpression;
609 template<
typename ET >
613 mMatrix *= aExpression.get_ref();
633 print(
const std::string aLabel=
"Matrix" )
const;
641#pragma GCC diagnostic push
642#pragma GCC diagnostic ignored "-Wformat"
644 template <
typename T >
void
647 FILE * tOutFile = stdout;
649 fprintf( tOutFile,
"%s = [ ... \n", aLabel.c_str() );
654 uint tRows = tThis.n_rows();
655 uint tCols = tThis.n_cols();
657 for(
uint i=0; i< tRows; ++i )
659 for(
uint j=0; j< tCols; ++j )
663 fprintf( tOutFile,
"%d, ", (
int ) tThis( i, j ) );
667 fprintf( tOutFile,
"%d; ", (
int ) tThis( i, j ) );
673 fprintf( tOutFile,
"...\n" );
677 fprintf( tOutFile,
"];\n" );
688 FILE * tOutFile = stdout;
690 fprintf( tOutFile,
"%s = [ ... \n", aLabel.c_str() );
697 for(
uint i=0; i< tRows; ++i )
699 for(
uint j=0; j< tCols; ++j )
703 fprintf( tOutFile,
"%+.15e, ", (
double ) tThis( i, j ) );
707 fprintf( tOutFile,
"%+.15e; ", (
double ) tThis( i, j ) );
713 fprintf( tOutFile,
"...\n" );
717 fprintf( tOutFile,
"];\n" );
724#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:231
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:218
Matrix< T > & operator+=(const T &aValue)
Definition cl_AR_Matrix.hpp:493
auto col(const size_t aColIndex) const -> decltype(mMatrix.col(aColIndex))
Definition cl_AR_Matrix.hpp:343
void set_size(const size_t aNumRows, const size_t aNumCols, const T &aValue)
Definition cl_AR_Matrix.hpp:176
auto col(const size_t aColIndex) -> decltype(mMatrix.col(aColIndex))
Definition cl_AR_Matrix.hpp:329
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:389
const MatrixType & matrix_data() const
Definition cl_AR_Matrix.hpp:150
const T & operator()(const size_t aRowIndex, const size_t aColIndex) const
access operator ( const version )
Definition cl_AR_Matrix.hpp:281
void fill(const T &aValue)
Definition cl_AR_Matrix.hpp:160
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:357
auto row(const size_t aRowIndex) -> decltype(mMatrix.row(aRowIndex))
Definition cl_AR_Matrix.hpp:301
Matrix< T > & operator/=(const T &aValue)
Definition cl_AR_Matrix.hpp:622
Matrix(const size_t aNumRows, const size_t aNumCols)
Definition cl_AR_Matrix.hpp:51
Matrix< T > & operator+=(const arma::Base< T, ET > &aExpression)
compound assignment from any Armadillo expression, reaching the backend's in-place path; without this...
Definition cl_AR_Matrix.hpp:533
Matrix< T > & operator*=(const arma::Base< T, ET > &aExpression)
Definition cl_AR_Matrix.hpp:611
void set_row(const size_t aRowIndex, const Vector< T > &aVector)
Definition cl_AR_Matrix.hpp:421
MatrixType & matrix_data()
Definition cl_AR_Matrix.hpp:139
size_t capacity() const
The logical element count n_rows() * n_cols() ( Armadillo's n_elem ).
Definition cl_AR_Matrix.hpp:206
Matrix< T > & operator-=(const T &aValue)
Definition cl_AR_Matrix.hpp:544
Matrix(const MatrixType &aExpression)
Definition cl_AR_Matrix.hpp:75
Matrix< T > & operator-=(const MatrixType &aExpression)
Definition cl_AR_Matrix.hpp:562
void set_size(const size_t aNumRows, const size_t aNumCols)
Definition cl_AR_Matrix.hpp:168
size_t n_rows() const
Definition cl_AR_Matrix.hpp:187
void set_col(const size_t aColIndex, const Vector< T > &aVector)
Definition cl_AR_Matrix.hpp:442
Matrix(const Matrix< T > &aMatrix)
copy constructor
Definition cl_AR_Matrix.hpp:93
Matrix< T > & operator-=(const arma::Base< T, ET > &aExpression)
Definition cl_AR_Matrix.hpp:572
Matrix< T > & operator=(const Matrix< T > &aMatrix)
copy assignment operator
Definition cl_AR_Matrix.hpp:244
T & operator()(const size_t aRowIndex, const size_t aColIndex)
access operator ( writable version )
Definition cl_AR_Matrix.hpp:260
void set_row(const size_t aColIndex, const ET &aExpression)
Definition cl_AR_Matrix.hpp:434
arma::Mat< T > MatrixType
Definition cl_AR_Matrix.hpp:34
Matrix< T > & operator=(const T &aValue)
Fill with value.
Definition cl_AR_Matrix.hpp:469
Matrix< T > & operator-=(const Matrix< T > &aMatrix)
Definition cl_AR_Matrix.hpp:553
size_t n_cols() const
Definition cl_AR_Matrix.hpp:195
Matrix(Matrix< T > &&aMatrix) noexcept
move constructor
Definition cl_AR_Matrix.hpp:100
Matrix(const std::initializer_list< std::initializer_list< T > > &aInitList)
Definition cl_AR_Matrix.hpp:67
Matrix< T > & operator+=(T &aValue)
Definition cl_AR_Matrix.hpp:500
Matrix< T > & operator*=(const MatrixType &aExpression)
Definition cl_AR_Matrix.hpp:601
Matrix< T > & operator+=(const MatrixType &aExpression)
Definition cl_AR_Matrix.hpp:518
Matrix< T > & operator*=(const Matrix< T > &aMatrix)
Definition cl_AR_Matrix.hpp:592
~Matrix()=default
empty destructor
Matrix< T > & operator*=(const T &aValue)
Definition cl_AR_Matrix.hpp:583
const T * data() const
Definition cl_AR_Matrix.hpp:128
Matrix(const arma::Base< T, ET > &aExpression)
constructor from any Armadillo expression ( eOp, eGlue, Glue, mtOp, views ): the backend evaluates it...
Definition cl_AR_Matrix.hpp:85
void print(const std::string aLabel="Matrix") const
Definition cl_AR_Matrix.hpp:645
Matrix(const size_t aNumRows, const size_t aNumCols, const T aValue)
Definition cl_AR_Matrix.hpp:57
T * data()
Returns a borrowed raw buffer under the layout contract stated in cl_Matrix.hpp; may be nullptr when ...
Definition cl_AR_Matrix.hpp:120
Matrix< T > & operator+=(const Matrix< T > &aMatrix)
Definition cl_AR_Matrix.hpp:509
Matrix< T > & operator=(const ET &aExpression)
Fill with expression.
Definition cl_AR_Matrix.hpp:482
void set_col(const size_t aColIndex, const ET &aExpression)
Definition cl_AR_Matrix.hpp:455
auto row(const size_t aRowIndex) const -> decltype(mMatrix.row(aRowIndex))
Definition cl_AR_Matrix.hpp:315
size_t length() const
get the length of the vector
Definition cl_AR_Vector.hpp:333
VectorType & vector_data()
expose the underlying matrix implementation ( writable version )
Definition cl_AR_Vector.hpp:280
USER GUIDES:
Definition cl_Capacitor.cpp:16
unsigned int uint
Definition typedefs.hpp:30