Core 1.0
MatrixMN Class Reference

A simple, non-optimized implementation of rectangular matrixes. More...

#include <matrix-mn.h>

Public Member Functions

 MatrixMN (int, int, double=0.0)
 Create a rectangular matrix.
 MatrixMN (int, double=0.0)
 Create a square matrix.
 MatrixMN (int, int, const std::vector< double > &)
 Create a rectangular matrix.
 MatrixMN (const std::vector< std::vector< double > > &)
 Creates a rectangular matrix from a 2D std::vector.
double & operator() (int, int)
 Return the matrix entry at row i, column j.
double operator() (int, int) const
 Return the matrix entry at row i, column j.
void SwapRows (int, int)
 Swap rows.
MatrixMN Transpose () const
 Transpose.
MatrixMN Inverse () const
 Compute the Gauss Jordan inverse.
double LogDet () const
 Compute the log of the (assumed positive) determinant, via Gaussian elimination with partial pivoting.
MatrixMN operator- () const
 Returns the negation of a matrix -A.
MatrixMN & operator+= (const MatrixMN &)
 Self sum.
MatrixMN & operator-= (const MatrixMN &)
 Self difference.
MatrixMN & operator*= (const MatrixMN &)
 Self product.
MatrixMN & operator*= (const double &)
 Self multiply by a scalar.
MatrixMN & operator/= (const double &)
 Self division by a scalar.

Static Public Member Functions

static MatrixMN Identity (int)
 Create the identity matrix.
static MatrixMN Covariance (const std::vector< std::vector< double > > &vectors)
 Create the covariance matrix of a given set of vectors.

Protected Member Functions

int Index (int, int) const
 Compute the entry of a term.
bool SameSize (const MatrixMN &) const
 Test if the matrix has the same size as the argument matrix.

Protected Attributes

int m = 0
 Rows.
int n = 0
 Columns.
std::vector< double > v
 Array of data.

Friends

MatrixMN operator+ (const MatrixMN &a, const MatrixMN &b)
 Sum.
MatrixMN operator- (const MatrixMN &a, const MatrixMN &b)
 Difference.
MatrixMN operator* (const MatrixMN &a, const MatrixMN &b)
 Product.
MatrixMN operator* (const MatrixMN &a, double b)
 Right multiply by a scalar.
MatrixMN operator* (double b, const MatrixMN &a)
 Left multiply by a scalar.
MatrixMN operator/ (const MatrixMN &a, const double &d)
 Divide all entries by a scalar.
std::ostream & operator<< (std::ostream &s, const MatrixMN &mat)
 Overloaded.

Detailed Description

A simple, non-optimized implementation of rectangular matrixes.

Author
Hugo Schott

Constructor & Destructor Documentation

◆ MatrixMN() [1/4]

MatrixMN::MatrixMN ( int m,
int n,
double d = 0.0 )

Create a rectangular matrix.

Parameters
m,nSize in rows, columns.
dDefault scalar value.

◆ MatrixMN() [2/4]

MatrixMN::MatrixMN ( int n,
double d = 0.0 )

Create a square matrix.

Parameters
nSize.
dDefault scalar value.

◆ MatrixMN() [3/4]

MatrixMN::MatrixMN ( int m,
int n,
const std::vector< double > & data )

Create a rectangular matrix.

Parameters
m,nNumber of rows and columns.
dataLinearized array of values in row-major order (m*n elements).

◆ MatrixMN() [4/4]

MatrixMN::MatrixMN ( const std::vector< std::vector< double > > & data)

Creates a rectangular matrix from a 2D std::vector.

Parameters
data2D std::vector, each row must have same number of elements.

Member Function Documentation

◆ Covariance()

MatrixMN MatrixMN::Covariance ( const std::vector< std::vector< double > > & vectors)
static

Create the covariance matrix of a given set of vectors.

Parameters
vectorsSet of vectors, they must have the same size.

◆ Identity()

MatrixMN MatrixMN::Identity ( int n)
static

Create the identity matrix.

Parameters
nSize.

◆ Index()

int MatrixMN::Index ( int i,
int j ) const
inlineprotected

Compute the entry of a term.

Parameters
i,jIndexes.

◆ Inverse()

MatrixMN MatrixMN::Inverse ( ) const

Compute the Gauss Jordan inverse.

The function only works if the matrix is square.

The pivot of every column is the coefficient of largest absolute value among the remaining rows (partial pivoting), which limits the amplification of the rounding errors. The matrix is considered as singular if this pivot is smaller than 1e-12 times the largest absolute value of the coefficients of the matrix: in that case the identity matrix is returned.

◆ LogDet()

double MatrixMN::LogDet ( ) const

Compute the log of the (assumed positive) determinant, via Gaussian elimination with partial pivoting.

The function only works if the matrix is square. Returns minus infinity if the matrix is singular.

The matrix is considered as singular if a pivot is smaller than 1e-12 times the largest absolute value of its coefficients: the test does not depend on the scale of the coefficients, so that the determinant of a well conditioned matrix with small (or large) coefficients is computed.

◆ operator()() [1/2]

double & MatrixMN::operator() ( int i,
int j )
inline

Return the matrix entry at row i, column j.

Parameters
i,jInteger coordinates of the matrix entry.

◆ operator()() [2/2]

double MatrixMN::operator() ( int i,
int j ) const
inline

Return the matrix entry at row i, column j.

Parameters
i,jInteger coordinates of the matrix entry.

◆ operator*=() [1/2]

MatrixMN & MatrixMN::operator*= ( const double & d)

Self multiply by a scalar.

Parameters
dReal.

◆ operator*=() [2/2]

MatrixMN & MatrixMN::operator*= ( const MatrixMN & a)

Self product.

Parameters
aMatrix.

◆ operator+=()

MatrixMN & MatrixMN::operator+= ( const MatrixMN & a)

Self sum.

Parameters
aMatrix.

◆ operator-=()

MatrixMN & MatrixMN::operator-= ( const MatrixMN & a)

Self difference.

Parameters
aMatrix.

◆ operator/=()

MatrixMN & MatrixMN::operator/= ( const double & d)

Self division by a scalar.

Parameters
dReal.

◆ SameSize()

bool MatrixMN::SameSize ( const MatrixMN & A) const
inlineprotected

Test if the matrix has the same size as the argument matrix.

Parameters
AMatrix

◆ SwapRows()

void MatrixMN::SwapRows ( int i,
int j )

Swap rows.

Parameters
i,jIndexes.

◆ operator* [1/3]

MatrixMN operator* ( const MatrixMN & a,
const MatrixMN & b )
friend

Product.

Parameters
a,bMatrixes.

◆ operator* [2/3]

MatrixMN operator* ( const MatrixMN & a,
double b )
friend

Right multiply by a scalar.

Parameters
aMatrix.
bReal.

◆ operator* [3/3]

MatrixMN operator* ( double b,
const MatrixMN & a )
friend

Left multiply by a scalar.

Parameters
aMatrix.
bReal.

◆ operator+

MatrixMN operator+ ( const MatrixMN & a,
const MatrixMN & b )
friend

Sum.

Parameters
a,bMatrixes.

◆ operator-

MatrixMN operator- ( const MatrixMN & a,
const MatrixMN & b )
friend

Difference.

Parameters
a,bMatrixes.

◆ operator/

MatrixMN operator/ ( const MatrixMN & a,
const double & d )
friend

Divide all entries by a scalar.

Parameters
aMatrix.
dScalar divisor.

◆ operator<<

std::ostream & operator<< ( std::ostream & s,
const MatrixMN & mat )
friend

Overloaded.

Parameters
sStream.
matMatrix.