CPP Linear Algebra ToolBox  0.1.0
Matrix Class Reference

A class describing a Matrix object. Matrices are array of size (n,m) whre n and m are integer greater or equal than 1. Notice that in general a matrix is not square. Non specific structure is assumed. Square, Diagonal, Triangular, etc, have dedicated classes. More...

#include <matrix.h>

Inheritance diagram for Matrix:

Public Member Functions

 Matrix ()
 Construct a Matrix object. Default constructor. Set number of rows and number of columns to 0. Set data pointer to NULL. More...
 
 Matrix (uint16_t, uint16_t)
 Construct a Matrix object. Construct a matrix of size n rows times m columns. More...
 
 Matrix (const Matrix &)
 Construct a Matrix object. Copy contructor. More...
 
virtual ~Matrix ()
 Destroy the Matrix object. More...
 
uint16_t get_n_rows () const
 Accessor to the number of rows. More...
 
uint16_t get_n_cols () const
 Accessor to the number of columns. More...
 
uint32_t get_n_elements () const
 Accessor to the number of elements. More...
 
bool is_square () const
 Test if a matrix has a square shape. More...
 
Vector row (uint16_t) const
 Extract row from Matrix. More...
 
Vector col (uint16_t) const
 Extract column from Matrix. More...
 
double norm1 () const
 Compute the norm-1 of a Matrix object. For a Matrix M of dimension \(n\times m\), its norm-1 is given by the maximum colum sum (in absolute value) More...
 
double normInf () const
 Compute the norm-Infinity of a Matrix object. For a Matrix M of dimension \(n\times m\), its norm-Infinity is given by the maximum row sum (in absolute value) More...
 
double normFrob () const
 Compute the Frobenius norm of a Matrix object. For a Matrix M of dimension \(n\times m\), its Frobenius norm is given by the sum of its coefficient squared. More...
 
Matrix transpose () const
 Compute the transpose Matrix of a Matrix object. More...
 
double & operator() (uint32_t) const
 Overload operator (i) More...
 
double & operator() (uint16_t, uint16_t) const
 Overload operator (i,j). More...
 
Matrixoperator= (const Matrix &)
 Overload assignement operator. More...
 
Matrixoperator+= (const Matrix &)
 Overload operator +=. More...
 
Matrixoperator-= (const Matrix &)
 Overload operator -=. More...
 
Matrix operator- () const
 Overload unary operator -. More...
 

Static Public Member Functions

static Matrix zeros (uint16_t, uint16_t)
 Construct a matrix object of dimension n times m filled with zeros. More...
 
static Matrix ones (uint16_t, uint16_t)
 Construct a matrix object of dimension n times m filled with ones. More...
 
static Matrix rand (uint16_t, uint16_t)
 Construct a matrix object of dimension n times m filled with random values sampled from 0. to 1. More...
 
static Matrix hilbert (uint16_t, uint16_t)
 Construct an Hilbert matrix. More...
 
static Matrix vandermonde (const Vector &, uint16_t)
 Construct a Vandermonde matrix. See https://en.wikipedia.org/wiki/Vandermonde_matrix. More...
 
static Matrix outer (const Vector &, const Vector &)
 Overload operator * between two vectors to compute their outer product. More...
 
static Matrix matmul (const Matrix &, MATRIX_TRANSPOSE, const Matrix &, MATRIX_TRANSPOSE)
 Compute the product of two matrices combined with their transposition. More...
 

Protected Member Functions

double & at (uint32_t) const
 Helper to access the n-th stored value of a Matrix object. More...
 
double & at (uint16_t, uint16_t) const
 Access given element of a matrix. More...
 
void show () const
 Helper to display a Matrix object to the standart output stream. More...
 
void default_data ()
 Fill the data array with zeros.
More...
 

Protected Attributes

uint16_t n_rows
 
uint16_t n_cols
 
uint32_t n_elements
 
double * data
 

Friends

bool operator== (const Matrix &, const Matrix &)
 Overload of operator == Two matrices are equal if and only they have the same dimensions and the same elements. More...
 
Matrix operator* (const Matrix &, const Matrix &)
 Overload operator * for multiplication of Matrices. More...
 
Vector operator* (const Vector &, const Matrix &)
 Overload operator * for left multiplication of covector with Matrix. More...
 
Vector operator* (const Matrix &, const Vector &)
 Overload operator * for right multiplication of Matrix with Vector. More...
 
Matrix operator* (const double, const Matrix &)
 Overload operator * for multiplication of a scalar with Matrix. More...
 
Matrix operator* (const Matrix &, const double)
 Overload operator * for multiplication of a Matrix with scalar. More...
 
Matrix operator+ (const Matrix &, const Matrix &)
 Overload operator + for addition of Matrices. More...
 
Matrix operator+ (const double, const Matrix &)
 Overload operator + for addition of scalar with Matrix. More...
 
Matrix operator+ (const Matrix &, const double)
 Overload operator + for addition of Matrix with scalar. More...
 
Matrix operator- (const Matrix &, const Matrix &)
 Overload operator - for soustraction of Matrices. More...
 
Matrix operator- (const Matrix &, const double)
 Overload operator - for soustraction of Matrix with scalar. More...
 
Matrix operator- (const double, const Matrix &)
 Overload operator - for soustraction of scalar with Matrix. More...
 
Matrix operator/ (const Matrix &, const double)
 Overload operator / for division of Matrix by scalar. More...
 
std::ostream & operator<< (std::ostream &, const Matrix &)
 Overload output stream operator for Matrix object. More...
 

Detailed Description

A class describing a Matrix object. Matrices are array of size (n,m) whre n and m are integer greater or equal than 1. Notice that in general a matrix is not square. Non specific structure is assumed. Square, Diagonal, Triangular, etc, have dedicated classes.

Warning
Indices are 0-based (Math Formulae must be adapted consequently)
Maximum dimension of a Matrix is 65 535 times 65 535

Constructor & Destructor Documentation

◆ Matrix() [1/3]

Matrix::Matrix ( )

Construct a Matrix object. Default constructor. Set number of rows and number of columns to 0. Set data pointer to NULL.

◆ Matrix() [2/3]

Matrix::Matrix ( uint16_t  m,
uint16_t  n 
)

Construct a Matrix object. Construct a matrix of size n rows times m columns.

Parameters
mnumber of rows (must be greater or equal than 1)
nnumber of colums (must be greater or equal than 1)

◆ Matrix() [3/3]

Matrix::Matrix ( const Matrix in)

Construct a Matrix object. Copy contructor.

Parameters
inMatrix to be copied

◆ ~Matrix()

Matrix::~Matrix ( )
virtual

Destroy the Matrix object.

Member Function Documentation

◆ at() [1/2]

double & Matrix::at ( uint16_t  i,
uint16_t  j 
) const
protected

Access given element of a matrix.

Parameters
iRow number (must be greater or equal than 0)
jColumn number (must be greater or equal than 0)
Returns
double & Reference to the (i,j)-th element of the matrix
Warning
Indices are 0-based

◆ at() [2/2]

double & Matrix::at ( uint32_t  n) const
protected

Helper to access the n-th stored value of a Matrix object.

Parameters
nIndex of the value (must be between 0 and n_elements-1)
Returns
double& Reference to the n-th stored value of Matrix object.

◆ col()

Vector Matrix::col ( uint16_t  c) const

Extract column from Matrix.

Parameters
cColumn number must be between 0 and n_cols-1
Returns
Vector Vector of length n_rows containing the c-th column of the current Matrix object

◆ default_data()

void Matrix::default_data ( )
protected

Fill the data array with zeros.

◆ get_n_cols()

uint16_t Matrix::get_n_cols ( ) const

Accessor to the number of columns.

Returns
uint16_t Number of columns of the matrix (is greater or equal than 1)

◆ get_n_elements()

uint32_t Matrix::get_n_elements ( ) const

Accessor to the number of elements.

Returns
uint32_t Number of elements strored in the matrix

◆ get_n_rows()

uint16_t Matrix::get_n_rows ( ) const

Accessor to the number of rows.

Returns
uint16_t Number of rows of the matrix (is greater or equal than 1)

◆ hilbert()

Matrix Matrix::hilbert ( uint16_t  m,
uint16_t  n 
)
static

Construct an Hilbert matrix.

Parameters
mNumber of rows of the Matrix (must be greater or equal than 1).
nNumber of cols of the Matrix (must be greater or equal than 1).
Returns
Matrix A Matrix object whose coefficients are \(M_{ij} = \frac{1}{i+j-1}\)

◆ is_square()

bool Matrix::is_square ( ) const

Test if a matrix has a square shape.

Returns
true If the Matrix object has teh same number of row and columns.
false Otherwise.

◆ matmul()

Matrix Matrix::matmul ( const Matrix A,
MATRIX_TRANSPOSE  TRANSA,
const Matrix B,
MATRIX_TRANSPOSE  TRANSB 
)
static

Compute the product of two matrices combined with their transposition.

Parameters
AMatrix object of dimension \((m_A,n_A)\)
TRANSA
BMatrix object of dimension \((m_B,n_B)\)
TRANSB
Returns
Matrix

◆ norm1()

double Matrix::norm1 ( ) const

Compute the norm-1 of a Matrix object. For a Matrix M of dimension \(n\times m\), its norm-1 is given by the maximum colum sum (in absolute value)

\[ \|M\|_1 = \max_{1\leq j\leq m} \sum_{i=1}^n |M_{ij}| \]

Returns
double Norm-1 of Matrix object

◆ normFrob()

double Matrix::normFrob ( ) const

Compute the Frobenius norm of a Matrix object. For a Matrix M of dimension \(n\times m\), its Frobenius norm is given by the sum of its coefficient squared.

\[ \|M\|_{F} = \sum_{i=1}^n \sum_{j=1}^m M_{ij}^2 \]

Returns
double Frobenius norm of Matrix object

◆ normInf()

double Matrix::normInf ( ) const

Compute the norm-Infinity of a Matrix object. For a Matrix M of dimension \(n\times m\), its norm-Infinity is given by the maximum row sum (in absolute value)

\[ \|M\|_{\infty} = \max_{1\leq i\leq n} \sum_{j=1}^m |M_{ij}| \]

Returns
double Norm-Infinity of Matrix object

◆ ones()

Matrix Matrix::ones ( uint16_t  m,
uint16_t  n 
)
static

Construct a matrix object of dimension n times m filled with ones.

Parameters
mNumber of rows of the Matrix (must be greater or equal than 1).
nNumber of cols of the Matrix (must be greater or equal than 1).
Returns
Matrix A Matrix object of size nxm filled with ones

◆ operator()() [1/2]

double & Matrix::operator() ( uint16_t  i,
uint16_t  j 
) const

Overload operator (i,j).

Parameters
iRow number (must be greater or equal than 0)
jColumn number (must be gretaer or equal than 0)
Returns
double & Reference to the (i,j)-th element of the matrix
Warning
Indices are 0-based

◆ operator()() [2/2]

double & Matrix::operator() ( uint32_t  n) const

Overload operator (i)

Parameters
nElement number (must be between 0 and n_elements-1)
Returns
double& Reference to the n-th stored element of the matrix

◆ operator+=()

Matrix & Matrix::operator+= ( const Matrix B)

Overload operator +=.

Parameters
BMatrix to add to current Matrix object.
Returns
Matrix & Reference to Matrix object equal to this->data + B.data
Warning
B must have the same dimensions as the current Matrix object

◆ operator-()

Matrix Matrix::operator- ( ) const

Overload unary operator -.

Returns
Matrix Object whose coefficients are the opposite of those of the current Matrix object.

◆ operator-=()

Matrix & Matrix::operator-= ( const Matrix B)

Overload operator -=.

Parameters
BMatrix to substract to current Matrix object.
Returns
Matrix & Reference to Matrix object equal to this->data - B.data
Warning
B must have the same dimensions as the current Matrix object

◆ operator=()

Matrix & Matrix::operator= ( const Matrix B)

Overload assignement operator.

Parameters
BRight Hand Side, Matrix to be copied
Returns
Matrix & A Matrix object copy of B.

◆ outer()

Matrix Matrix::outer ( const Vector u,
const Vector v 
)
static

Overload operator * between two vectors to compute their outer product.

Parameters
uVector object of dimension \(m\) (>0)
vVector object of dimension \(n\) (>0)
Returns
Matrix Matrix object M of dimension \(m\times n\) equal to M = u*v^T, i.e. \(M_{ij} = u_iv_j\).

◆ rand()

Matrix Matrix::rand ( uint16_t  m,
uint16_t  n 
)
static

Construct a matrix object of dimension n times m filled with random values sampled from 0. to 1.

Parameters
mNumber of rows of the Matrix (must be greater or equal than 1).
nNumber of cols of the Matrix (must be greater or equal than 1).
Returns
Matrix A Matrix object of size nxm filled with random values

◆ row()

Vector Matrix::row ( uint16_t  r) const

Extract row from Matrix.

Parameters
rRow number must be between 0 and n_rows-1
Returns
Vector Vector of length n_cols containing the r-th row of the current Matrix object

◆ show()

void Matrix::show ( ) const
protected

Helper to display a Matrix object to the standart output stream.

◆ transpose()

Matrix Matrix::transpose ( ) const

Compute the transpose Matrix of a Matrix object.

Returns
Matrix Transpose of current Matrix object.

◆ vandermonde()

Matrix Matrix::vandermonde ( const Vector v,
uint16_t  n 
)
static

Construct a Vandermonde matrix. See https://en.wikipedia.org/wiki/Vandermonde_matrix.

Parameters
vVector of data sampled
nPower order of the vandermonde matrix
Returns
Matrix A Matrix object of dimension v.dim x n whose coefficients are \(M_{ij}=v_i^{j-1}\)

◆ zeros()

Matrix Matrix::zeros ( uint16_t  m,
uint16_t  n 
)
static

Construct a matrix object of dimension n times m filled with zeros.

Parameters
mNumber of rows of the Matrix (must be greater or equal than 1).
nNumber of cols of the Matrix (must be greater or equal than 1).
Returns
Matrix A Matrix object of size nxm filled with zeros

Friends And Related Function Documentation

◆ operator* [1/5]

Matrix operator* ( const double  k,
const Matrix B 
)
friend

Overload operator * for multiplication of a scalar with Matrix.

Parameters
kA scalar.
BA Matrix object.
Returns
Matrix Matrix whose coefficients are those of B multiplied by k

◆ operator* [2/5]

Matrix operator* ( const Matrix B,
const double  k 
)
friend

Overload operator * for multiplication of a Matrix with scalar.

Parameters
BA Matrix object.
kA scalar.
Returns
Matrix Matrix whose coefficients are those of B multiplied by k

◆ operator* [3/5]

Matrix operator* ( const Matrix lhs,
const Matrix rhs 
)
friend

Overload operator * for multiplication of Matrices.

Parameters
lhsLeft Hand Side
rhsRight Hand Side
Returns
Matrix Matrix equal to lhs*rhs
Warning
lhs must have the same number of cols as the number of cols of rhs

◆ operator* [4/5]

Vector operator* ( const Matrix A,
const Vector v 
)
friend

Overload operator * for right multiplication of Matrix with Vector.

Parameters
vA Vector object of dimension n
AA Matrix object of dimension (m,n)
Returns
Vector A Vector object of dimension m
Warning
Vector v must have the length equla to the number of columns of A

◆ operator* [5/5]

Vector operator* ( const Vector v,
const Matrix A 
)
friend

Overload operator * for left multiplication of covector with Matrix.

Parameters
vA Vector object of dimension n (actually a covector)
AA Matrix object of dimension (n,m)
Returns
Vector A Vector object of dimension m (actually a covector)
Warning
Vector v must have the length equal to the number of rows of A

◆ operator+ [1/3]

Matrix operator+ ( const double  k,
const Matrix B 
)
friend

Overload operator + for addition of scalar with Matrix.

Parameters
kA scalar.
BA Matrix.
Returns
Matrix Matrix whose coefficients are those of B plus k

◆ operator+ [2/3]

Matrix operator+ ( const Matrix B,
const double  k 
)
friend

Overload operator + for addition of Matrix with scalar.

Parameters
BA Matrix.
kA scalar.
Returns
Matrix Matrix whose coefficients are those of B plus k

◆ operator+ [3/3]

Matrix operator+ ( const Matrix lhs,
const Matrix rhs 
)
friend

Overload operator + for addition of Matrices.

Parameters
lhsLeft Hand Side
rhsRight Hand Side
Returns
Matrix Sum of lhs and rhs, i.e. lhs+rhs
Warning
lhs and rhs must have the same dimensions

◆ operator- [1/3]

Matrix operator- ( const double  k,
const Matrix B 
)
friend

Overload operator - for soustraction of scalar with Matrix.

Parameters
kA scalar.
BA Matrix.
Returns
Matrix Matrix whose coefficients are k minus B

◆ operator- [2/3]

Matrix operator- ( const Matrix B,
const double  k 
)
friend

Overload operator - for soustraction of Matrix with scalar.

Parameters
BA Matrix.
kA scalar.
Returns
Matrix Matrix whose coefficients are those of B minus k

◆ operator- [3/3]

Matrix operator- ( const Matrix lhs,
const Matrix rhs 
)
friend

Overload operator - for soustraction of Matrices.

Parameters
lhsLeft Hand Side
rhsRight Hand Side
Returns
Matrix Sum of lhs and rhs, i.e. lhs-rhs
Warning
lhs and rhs must have the same dimensions

◆ operator/

Matrix operator/ ( const Matrix B,
const double  k 
)
friend

Overload operator / for division of Matrix by scalar.

Parameters
BA Matrix.
kA scalar (non-zero).
Returns
Matrix Matrix whose coefficients are those of B divided by k
Warning
k must be non-zero.

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const Matrix M 
)
friend

Overload output stream operator for Matrix object.

Parameters
osOutput stream.
MMatrix object.
Returns
std::ostream & updated output stream

◆ operator==

bool operator== ( const Matrix lhs,
const Matrix rhs 
)
friend

Overload of operator == Two matrices are equal if and only they have the same dimensions and the same elements.

Parameters
lhsLeft Hand Side
rhsRight Hand Side
Returns
true if this matrix is equal to B
false if this matrix is different than B

Member Data Documentation

◆ data

double* Matrix::data
protected

Matrix data

◆ n_cols

uint16_t Matrix::n_cols
protected

Number of columns.

Warning
Must be greater than 1

◆ n_elements

uint32_t Matrix::n_elements
protected

Number of elements

◆ n_rows

uint16_t Matrix::n_rows
protected

Number of rows.

Warning
Must be greater than 1

The documentation for this class was generated from the following files: