Mathlib  1.0.0.0
A hobbyist C++ library for numerical algorithms
MatrixX< scalarType > Class Template Reference

MatrixX is a templated class that implements dynamic matrices. More...

#include <MatrixX.h>

Public Member Functions

 MatrixX ()
 Default constructor. No memory allocations are performed here. More...
 
 MatrixX (int n)
 Construct a column vector \(v \in \mathbf{R}^{n}\). More...
 
 MatrixX (int m, int n)
 Construct a matrix \(A \in \mathbf{R}^{m \times n}\). More...
 
 MatrixX (const MatrixX &m)
 Copy constructor. Creates a deep copy of the MatrixX object passed. More...
 
 MatrixX (std::initializer_list< std::initializer_list< scalarType >>)
 Allocates and initializes a matrix using the curly brace initializer list. More...
 
std::vector< scalarType > getRawData () const
 
int rows () const
 Getter method for the number of rows of a matrix. More...
 
int cols () const
 Getter method for the number of columns of a 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 MatrixX 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 MatrixX objects. More...
 
MatrixX operator+ (const MatrixX &m) const
 Matrix addition. This routine overloads the binary addition operator + and is used to add two matrices. More...
 
MatrixX operator- (const MatrixX &m) const
 Matrix subtraction. This routine overloads the binary subtraction operator - and is used to subtract two matrices. More...
 
MatrixXoperator<< (const scalarType x)
 Overload the << operator to initialize a matrix. More...
 
MatrixXoperator, (const scalarType x)
 Overload the , operator to initialize a matrix. More...
 
MatrixXoperator= (const MatrixX &right_hand_side)
 Copy assignment operator. More...
 
MatrixXoperator= (const MatrixRowSlice< scalarType > &rhs)
 
MatrixXoperator= (const MatrixColSlice< scalarType > &rhs)
 
bool operator== (const MatrixX &right_hand_side)
 Boolean comparision operator. Compares if \(A = B\). More...
 
MatrixXoperator+= (const MatrixX &m)
 Addition assignment operator More...
 
MatrixXoperator-= (const MatrixX &m)
 Subtraction assignment operator More...
 
MatrixRowSlice< scalarType > MatrixX (int i)
 
MatrixColSlice< scalarType > MatrixX (int j)
 
MatrixX< scalarType > transpose () const
 Transpose the matrix. More...
 

Private Attributes

std::vector< scalarType > A
 
int _rows
 
int _cols
 
int _size
 
std::vector< scalarType >::iterator currentPosition
 

Detailed Description

template<typename scalarType>
class MatrixX< scalarType >

MatrixX is a templated class that implements dynamic matrices.

Template Parameters
scalarType

Constructor & Destructor Documentation

◆ MatrixX() [1/7]

template<typename scalarType >
MatrixX< scalarType >::MatrixX

Default constructor. No memory allocations are performed here.

Template Parameters
scalarType
302  :_rows{ 0 }, _cols{ 0 }, _size{ 0 }
303 {
304  currentPosition = A.begin();
305 }

◆ MatrixX() [2/7]

template<typename scalarType >
MatrixX< scalarType >::MatrixX ( int  n)

Construct a column vector \(v \in \mathbf{R}^{n}\).

Template Parameters
scalarType
Parameters
m
n
336  : MatrixX(n, 1)
337 {
338  currentPosition = A.begin();
339 }

◆ MatrixX() [3/7]

template<typename scalarType >
MatrixX< scalarType >::MatrixX ( int  m,
int  n 
)

Construct a matrix \(A \in \mathbf{R}^{m \times n}\).

Template Parameters
scalarType
Parameters
m
n
324  : _rows{ m }, _cols{ n }, _size{ m * n }, A(m * n)
325 {
326  currentPosition = A.begin();
327 }

◆ MatrixX() [4/7]

template<typename scalarType >
MatrixX< scalarType >::MatrixX< typename scalarType > ( const MatrixX< scalarType > &  m)

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

Template Parameters
scalarType
313  : A{ m.A }, _rows{ m.rows() }, _cols{ m.cols() }, currentPosition{ m.currentPosition }, _size{ m.size() }
314 {
315 }

◆ MatrixX() [5/7]

template<typename scalarType >
MatrixX< scalarType >::MatrixX ( std::initializer_list< std::initializer_list< scalarType >>  list)

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

Template Parameters
scalarType
Parameters
list
348  :MatrixX<scalarType>{} //Delegate to the default constructor to set up the initial array
349 {
350  typename std::initializer_list<std::initializer_list<scalarType>>::iterator i{};
351  _rows = list.size();
352  for (i = list.begin(); i < list.end(); ++i)
353  {
354  typename std::initializer_list<scalarType>::iterator j{};
355  for (j = (*i).begin(); j < (*i).end(); ++j)
356  {
357  if (j == (*i).begin())
358  _cols = (*i).size();
359  A.push_back(*j);
360  }
361  }
362  _size = _rows * _cols;
363  currentPosition = A.begin();
364 }

◆ MatrixX() [6/7]

template<typename scalarType >
MatrixRowSlice<scalarType> MatrixX< scalarType >::MatrixX ( int  i)

◆ MatrixX() [7/7]

template<typename scalarType >
MatrixColSlice<scalarType> MatrixX< scalarType >::MatrixX ( int  j)

Member Function Documentation

◆ cols()

template<typename scalarType >
int MatrixX< scalarType >::cols

Getter method for the number of columns of a matrix.

Template Parameters
scalarType
Returns
384 {
385  return _cols;
386 }

◆ getRawData()

template<typename scalarType >
std::vector< scalarType > MatrixX< scalarType >::getRawData
401 {
402  return A;
403 }

◆ operator()() [1/2]

template<typename scalarType >
scalarType & MatrixX< 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 MatrixX objects.

Template Parameters
scalarType
Parameters
i
j
Returns
431 {
432  if (i * cols() + j < A.size())
433  return A[i * cols() + j];
434  else
435  throw std::out_of_range("\nError accessing an element beyond matrix bounds");
436 }

◆ operator()() [2/2]

template<typename scalarType >
scalarType MatrixX< 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 MatrixX objects.

413 {
414  if (i * cols() + j < A.size())
415  return A[i * cols() + j];
416  else
417  throw std::out_of_range("\nError accessing an element beyond matrix bounds");
418 }

◆ operator+()

template<typename scalarType >
MatrixX< typename scalarType > MatrixX< scalarType >::operator+ ( const MatrixX< scalarType > &  m) const

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

Template Parameters
scalarType
Parameters
m
Returns
447 {
448  if (this->rows() == m.rows() && this->cols() == m.cols())
449  {
450  MatrixX<scalarType> result{ m.rows(),m.cols() };
451  for (int i{}; i < A.size(); ++i)
452  {
453  result.A[i] = A[i] + m.A[i];
454  }
455  return result;
456  }
457  else
458  {
459  throw std::logic_error("Matrices have different dimensions; therefore cannot be added!");
460  }
461 }

◆ operator+=()

template<class scalarType >
MatrixX< scalarType > & MatrixX< scalarType >::operator+= ( const MatrixX< scalarType > &  m)

Addition assignment operator

Template Parameters
scalarType
Parameters
m
Returns
709 {
710  (*this) = (*this) + m;
711  return (*this);
712 }

◆ operator,()

template<typename scalarType >
MatrixX< typename scalarType > & MatrixX< scalarType >::operator, ( const scalarType  x)

Overload the , operator to initialize a matrix.

Template Parameters
scalarType
Parameters
x
Returns
517 {
518  if (currentPosition < A.end())
519  {
520  *currentPosition = x;
521  ++currentPosition;
522  }
523  else
524  {
525  throw std::logic_error("Error: Attempting to set values beyond matrix bounds!");
526  }
527  return *this;
528 }

◆ operator-()

template<typename scalarType >
MatrixX< typename scalarType > MatrixX< scalarType >::operator- ( const MatrixX< scalarType > &  m) const

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

Template Parameters
scalarType
Parameters
m
Returns
472 {
473  if (this->rows() == m.rows() && this->cols() == m.cols())
474  {
475  MatrixX<scalarType> result{ m.rows(),m.cols() };
476  for (int i{}; i < A.size(); ++i)
477  {
478  result.A[i] = A[i] - m.A[i];
479  }
480  return result;
481  }
482  else
483  {
484  throw std::logic_error("Matrices have different dimensions; therefore cannot be added!");
485  }
486 }

◆ operator-=()

template<class scalarType >
MatrixX< scalarType > & MatrixX< scalarType >::operator-= ( const MatrixX< scalarType > &  m)

Subtraction assignment operator

Template Parameters
scalarType
Parameters
m
Returns
722 {
723  (*this) = (*this) - m;
724  return (*this);
725 }

◆ operator<<()

template<typename scalarType >
MatrixX< typename scalarType > & MatrixX< scalarType >::operator<< ( const scalarType  x)

Overload the << operator to initialize a matrix.

Template Parameters
scalarType
Parameters
x
Returns
496 {
497  if (currentPosition < A.end())
498  {
499  *currentPosition = x;
500  ++currentPosition;
501  }
502  else
503  {
504  throw std::logic_error("Error: Attempting to set values beyond matrix bounds!");
505  }
506  return *this;
507 }

◆ operator=() [1/3]

template<typename scalarType >
MatrixX< scalarType > & MatrixX< scalarType >::operator= ( const MatrixColSlice< scalarType > &  rhs)
571 {
572  slice s{ rhs.getMatrixSlice() };
573  MatrixX<scalarType>& mat = rhs.getMatrixRef();
574  this->_rows = s.getLength();
575  this->_size = this->rows();
576  this->_cols = 1;
577  this->A = std::vector<scalarType>(_rows);
578  for (int i{}; i < s.getLength(); ++i)
579  {
580  A[i] = mat(s(i), rhs.getCol());
581  }
582  this->currentPosition = mat.currentPosition;
583  return *this;
584 }

◆ operator=() [2/3]

template<typename scalarType >
MatrixX< scalarType > & MatrixX< scalarType >::operator= ( const MatrixRowSlice< scalarType > &  rhs)
554 {
555  slice s{ rhs.getMatrixSlice() };
556  MatrixX<scalarType>& mat = rhs.getMatrixRef();
557  this->_rows = 1;
558  this->_cols = s.getLength();
559  this->_size = this->_cols;
560  this->A = std::vector<scalarType>(_cols);
561  for (int j{}; j < s.getLength(); ++j)
562  {
563  A[j] = mat(rhs.getRow(), s(j));
564  }
565  this->currentPosition = mat.currentPosition;
566  return *this;
567 }

◆ operator=() [3/3]

template<typename scalarType >
MatrixX< typename scalarType > & MatrixX< scalarType >::operator= ( const MatrixX< scalarType > &  rhs)

Copy assignment operator.

Template Parameters
scalarType
Parameters
right_hand_side
Returns
538 {
539  if (this->rows() != rhs.rows() || this->cols() != rhs.cols())
540  throw std::logic_error("Assignment failed, matrices have different dimensions");
541 
542  if (this == &rhs)
543  return *this;
544 
545  this->A = rhs.A;
546  this->_rows = rhs._rows;
547  this->_cols = rhs._cols;
548  this->currentPosition = rhs.currentPosition;
549  return *this;
550 }

◆ operator==()

template<typename scalarType >
bool MatrixX< scalarType >::operator== ( const MatrixX< scalarType > &  right_hand_side)

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

Template Parameters
scalarType
Parameters
right_hand_side
Returns
673 {
674  return (this->A == right_hand_side.A);
675 }

◆ rows()

template<typename scalarType >
int MatrixX< scalarType >::rows

Getter method for the number of rows of a matrix.

Template Parameters
scalarType
Returns
373 {
374  return _rows;
375 }

◆ size()

template<typename scalarType >
int MatrixX< scalarType >::size

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

Template Parameters
scalarType
Returns
395 {
396  return A.size();
397 }

◆ transpose()

template<class scalarType >
MatrixX< scalarType > MatrixX< scalarType >::transpose

Transpose the matrix.

Template Parameters
scalarType
Returns
788 {
789  MatrixX<scalarType> result{ cols(),rows() };
790  for (int i{}; i < rows(); ++i)
791  for (int j{}; j < cols(); ++j)
792  result(j, i) = (*this) (i, j);
793 
794  result._rows = cols();
795  result._cols = rows();
796  return result;
797 }

Member Data Documentation

◆ _cols

template<typename scalarType >
int MatrixX< scalarType >::_cols
private

◆ _rows

template<typename scalarType >
int MatrixX< scalarType >::_rows
private

◆ _size

template<typename scalarType >
int MatrixX< scalarType >::_size
private

◆ A

template<typename scalarType >
std::vector<scalarType> MatrixX< scalarType >::A
private

◆ currentPosition

template<typename scalarType >
std::vector<scalarType>::iterator MatrixX< scalarType >::currentPosition
private

The documentation for this class was generated from the following file:
MatrixX::rows
int rows() const
Getter method for the number of rows of a matrix.
Definition: MatrixX.h:372
MatrixX::_rows
int _rows
Definition: MatrixX.h:242
MatrixX::currentPosition
std::vector< scalarType >::iterator currentPosition
Definition: MatrixX.h:245
slice
The design of slice is owed to the "The C++ Programming language", Bjarne Stroustrup,...
Definition: slice.h:7
MatrixX::_size
int _size
Definition: MatrixX.h:244
MatrixColSlice::getMatrixRef
MatrixX< scalarType > & getMatrixRef() const
Return a ref to the matrix object from which this col vector was created.
Definition: MatrixX.h:91
MatrixRowSlice::getMatrixRef
MatrixX< scalarType > & getMatrixRef() const
Return a ref to the matrix object from which this row vector was created.
Definition: MatrixX.h:80
MatrixX::MatrixX
MatrixX()
Default constructor. No memory allocations are performed here.
Definition: MatrixX.h:302
MatrixX::cols
int cols() const
Getter method for the number of columns of a matrix.
Definition: MatrixX.h:383
MatrixRowSlice::getRow
int getRow() const
Definition: MatrixX.h:110
MatrixColSlice::getCol
int getCol() const
Definition: MatrixX.h:116
MatrixX::_cols
int _cols
Definition: MatrixX.h:243
MatrixRowSlice::getMatrixSlice
slice getMatrixSlice() const
Definition: MatrixX.h:98
MatrixX::A
std::vector< scalarType > A
Definition: MatrixX.h:241
MatrixX
MatrixX is a templated class that implements dynamic matrices.
Definition: MatrixX.h:239
MatrixColSlice::getMatrixSlice
slice getMatrixSlice() const
Definition: MatrixX.h:104
MatrixX::size
int size() const
Getter method for the size (the total number of elements) of the matrix.
Definition: MatrixX.h:394