Mathlib  1.0.0.0
A hobbyist C++ library for numerical algorithms
Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime > Class Template Reference

Class template for fixed-size matrices. More...

#include <Matrix.h>

Public Member Functions

 Matrix ()
 The default constructor. More...
 
 Matrix (int m, int n)
 For fixed-size matrices, the arguments m and n are ignored, and instead the dimensions of the matrix are determined from the template parameters rowsAtCompileTime and colsAtCompileTime. More...
 
 Matrix (const Matrix &m)
 Copy constructor. Creates a deep copy of the Matrix object passed. More...
 
 Matrix (std::initializer_list< std::initializer_list< scalarType >>)
 Allocates and initializes a matrix using the curly brace initializer list. More...
 
int rows () const
 Getter method for the number of rows of the matrix. More...
 
int cols () const
 Getter method for the number of columns of the matrix. More...
 
int size () const
 Getter method for the size (the total number of elements) of the matrix. More...
 
scalarType operator() (const int i, const int j) const
 Coefficient accessor. This routine overloads the parentheses operator (). A(i,j) is used the retrieve the element \(a_{ij}\) belonging to the matrix \(A\). This is const-version of the method, that works on const Matrix objects. More...
 
scalarType & operator() (const int i, const int j)
 Coefficient accessor. This routine retrieves the element in the (i,j) place of the matrix. This works on non-const Matrix objects. More...
 
Matrix operator+ (const Matrix &m) const
 Matrix addition. This routine overloads the binary addition operator + and is used to add two matrices. More...
 
Matrix operator- (const Matrix &m) const
 Matrix subtraction. This routine overloads the binary subtraction operator - and is used to subtract two matrices. More...
 
Matrixoperator<< (const scalarType x)
 Overload the << operator to initialize a matrix. More...
 
Matrixoperator, (const scalarType x)
 Overload the , operator to initialize a matrix. More...
 
Matrixoperator= (const Matrix &right_hand_side)
 Copy assignment operator. More...
 
bool operator== (const Matrix &right_hand_side)
 Boolean comparision operator. Compares if \(A = B\). More...
 
Matrixoperator+= (const Matrix &m)
 
Matrixoperator-= (const Matrix &m)
 

Private Attributes

std::array< scalarType, rowsAtCompileTime *colsAtCompileTime > A
 
int _rows
 
int _cols
 
int _size
 
std::array< scalarType, rowsAtCompileTime *colsAtCompileTime >::iterator currentPosition
 

Detailed Description

template<typename scalarType, int rowsAtCompileTime, int colsAtCompileTime>
class Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >

Class template for fixed-size matrices.

Template Parameters
scalarTypeScalar type of the elements in the matrix
rowsAtCompileTimeNumber of rows
colsAtCompileTimeNumber of columns

Constructor & Destructor Documentation

◆ Matrix() [1/4]

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::Matrix
inline

The default constructor.

Template Parameters
scalarType
109  :_rows{ rowsAtCompileTime }, _cols{ colsAtCompileTime }, _size{ rowsAtCompileTime * colsAtCompileTime }
110 {
111  currentPosition = A.begin();
112 }

◆ Matrix() [2/4]

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::Matrix ( int  m,
int  n 
)
inline

For fixed-size matrices, the arguments m and n are ignored, and instead the dimensions of the matrix are determined from the template parameters rowsAtCompileTime and colsAtCompileTime.

Template Parameters
scalarType
Parameters
m
n
124 {
125  //Do nothing.
126 }

◆ Matrix() [3/4]

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::Matrix ( const Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime > &  m)
inline

Copy constructor. Creates a deep copy of the Matrix object passed.

Template Parameters
scalarType
Parameters
m
135  : A{ m.A }, _rows{ m.rows() }, _cols{ m.cols() }, currentPosition{ m.currentPosition }
136 {
137 }

◆ Matrix() [4/4]

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::Matrix ( std::initializer_list< std::initializer_list< scalarType >>  list)

Allocates and initializes a matrix using the curly brace initializer list.

Template Parameters
scalarType
Parameters
list
145  :Matrix<scalarType, rowsAtCompileTime, colsAtCompileTime>{} //Delegate to the default constructor to set up the initial array
146 {
147  // Initialize the array from our list.
148  typename std::array<scalarType, rowsAtCompileTime* colsAtCompileTime>::iterator A_it{ A.begin() };
149  typename std::initializer_list<std::initializer_list<scalarType>>::iterator i{};
150 
151  for (i = list.begin(); i < list.end(); ++i)
152  {
153  typename std::initializer_list<scalarType>::iterator j{};
154  for (j = (*i).begin(); j < (*i).end(); ++j)
155  {
156  *A_it = *j;
157  ++A_it;
158  }
159  }
160 }

Member Function Documentation

◆ cols()

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
int Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::cols
inline

Getter method for the number of columns of the matrix.

Template Parameters
scalarType
Returns
180 {
181  return _cols;
182 }

◆ operator()() [1/2]

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
scalarType & Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::operator() ( const int  i,
const int  j 
)
inline

Coefficient accessor. This routine retrieves the element in the (i,j) place of the matrix. This works on non-const Matrix objects.

Template Parameters
scalarType
Parameters
i
j
Returns
227 {
228  typename std::array<scalarType, rowsAtCompileTime* colsAtCompileTime>::iterator it{ A.begin() };
229  it = it + (i * _cols) + j;
230  if (it < A.end())
231  return *it;
232  else
233  throw std::out_of_range("\nError accessing an element beyond matrix bounds");
234 }

◆ operator()() [2/2]

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
scalarType Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::operator() ( const int  i,
const int  j 
) const
inline

Coefficient accessor. This routine overloads the parentheses operator (). A(i,j) is used the retrieve the element \(a_{ij}\) belonging to the matrix \(A\). This is const-version of the method, that works on const Matrix objects.

Template Parameters
scalarType
Parameters
i
j
Returns
207 {
208  typename std::array<scalarType, rowsAtCompileTime* colsAtCompileTime>::const_iterator it{ A.begin() };
209  it = it + (i * _cols) + j;
210  if (it < A.end())
211  return *it;
212  else
213  throw std::out_of_range("\nError accessing an element beyond matrix bounds");
214 }

◆ operator+()

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
Matrix< typename scalarType, rowsAtCompileTime, colsAtCompileTime > Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::operator+ ( const Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime > &  m) const
inline

Matrix addition. This routine overloads the binary addition operator + and is used to add two matrices.

Template Parameters
scalarType
Parameters
m
Returns
245 {
247 
248  if (this->rows() == m.rows() && this->cols() == m.cols())
249  {
250  for (int i{}; i < A.size(); ++i)
251  result.A[i] = A[i] + m.A[i];
252  return result;
253  }
254  else
255  {
256  throw std::logic_error("Matrices have different dimensions; therefore cannot be added!");
257  }
258 }

◆ operator+=()

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
Matrix& Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::operator+= ( const Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime > &  m)

◆ operator,()

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
Matrix< typename scalarType, rowsAtCompileTime, colsAtCompileTime > & Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::operator, ( const scalarType  x)
inline

Overload the , operator to initialize a matrix.

Template Parameters
scalarType
Parameters
x
Returns
313 {
314  if (currentPosition < A.end())
315  {
316  *currentPosition = x;
317  ++currentPosition;
318  }
319  else
320  {
321  throw std::logic_error("Error: Attempting to set values beyond matrix bounds!");
322  }
323  return *this;
324 }

◆ operator-()

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
Matrix< typename scalarType, rowsAtCompileTime, colsAtCompileTime > Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::operator- ( const Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime > &  m) const
inline

Matrix subtraction. This routine overloads the binary subtraction operator - and is used to subtract two matrices.

Template Parameters
scalarType
Parameters
m
Returns
269 {
271 
272  if (this->rows() == m.rows() && this->cols() == m.cols())
273  {
274  for (int i{}; i < A.size(); ++i)
275  result.A[i] = A[i] - m.A[i];
276  return result;
277  }
278  else
279  {
280  throw std::logic_error("Matrices have different dimensions; therefore cannot be added!");
281  }
282 }

◆ operator-=()

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
Matrix& Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::operator-= ( const Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime > &  m)

◆ operator<<()

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
Matrix< typename scalarType, rowsAtCompileTime, colsAtCompileTime > & Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::operator<< ( const scalarType  x)
inline

Overload the << operator to initialize a matrix.

Template Parameters
scalarType
Parameters
x
Returns
292 {
293  if (currentPosition < A.end())
294  {
295  *currentPosition = x;
296  ++currentPosition;
297  }
298  else
299  {
300  throw std::logic_error("Error: Attempting to set values beyond matrix bounds!");
301  }
302  return *this;
303 }

◆ operator=()

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
Matrix< typename scalarType, rowsAtCompileTime, colsAtCompileTime > & Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::operator= ( const Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime > &  right_hand_side)
inline

Copy assignment operator.

Template Parameters
scalarType
Parameters
right_hand_side
Returns
334 {
335  if (this->rows() != right_hand_side.rows() || this->cols() != right_hand_side.cols())
336  throw std::logic_error("Assignment failed, matrices have different dimensions");
337 
338  if (this == &right_hand_side)
339  return *this;
340 
341  this->A = right_hand_side.A;
342  this->_rows = right_hand_side._rows;
343  this->_cols = right_hand_side._cols;
344  this->currentPosition = right_hand_side.currentPosition;
345  return *this;
346 }

◆ operator==()

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
bool Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::operator== ( const Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime > &  right_hand_side)
inline

Boolean comparision operator. Compares if \(A = B\).

Template Parameters
scalarType
Parameters
right_hand_side
Returns
436 {
437  return (this->A == right_hand_side.A);
438 }

◆ rows()

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
int Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::rows
inline

Getter method for the number of rows of the matrix.

Template Parameters
scalarType
Returns
169 {
170  return _rows;
171 }

◆ size()

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
int Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::size
inline

Getter method for the size (the total number of elements) of the matrix.

Template Parameters
scalarType
Returns
191 {
192  return A.size();
193 }

Member Data Documentation

◆ _cols

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
int Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::_cols
private

◆ _rows

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
int Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::_rows
private

◆ _size

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
int Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::_size
private

◆ A

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
std::array<scalarType, rowsAtCompileTime* colsAtCompileTime> Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::A
private

◆ currentPosition

template<typename scalarType , int rowsAtCompileTime, int colsAtCompileTime>
std::array<scalarType, rowsAtCompileTime* colsAtCompileTime>::iterator Matrix< scalarType, rowsAtCompileTime, colsAtCompileTime >::currentPosition
private

The documentation for this class was generated from the following file:
Matrix::_size
int _size
Definition: Matrix.h:60
Matrix::cols
int cols() const
Getter method for the number of columns of the matrix.
Definition: Matrix.h:179
Matrix::_rows
int _rows
Definition: Matrix.h:58
Matrix::currentPosition
std::array< scalarType, rowsAtCompileTime *colsAtCompileTime >::iterator currentPosition
Definition: Matrix.h:61
Matrix::A
std::array< scalarType, rowsAtCompileTime *colsAtCompileTime > A
Definition: Matrix.h:57
Matrix
Class template for fixed-size matrices.
Definition: Matrix.h:55
Matrix::rows
int rows() const
Getter method for the number of rows of the matrix.
Definition: Matrix.h:168
Matrix::_cols
int _cols
Definition: Matrix.h:59