MyMatrix example 1.0
MyMatrix example
Loading...
Searching...
No Matches
MyMatrix Class Reference

#include <MyMatrix.h>

Public Member Functions

 MyMatrix ()
 Default constructor: empty invalid matrix.
 
 MyMatrix (int nrows, int ncols, dataType value=NAN)
 Constructor: creates a matrix with the size desired and filled with a value.
 
 MyMatrix (const std::string &dataTxt)
 Constructor: creates a matrix with the size desired and filled with values, all entered from a string.
 
 MyMatrix (const std::vector< dataType > &data, bool rowVector=true)
 Constructor: creates a row or column vector filled from a standard vector.
 
 MyMatrix (const MyMatrix &other)
 Copy constructor: creates a matrix with the same data as other.
 
 ~MyMatrix ()
 Default destructor: removes all memory allocations.
 
bool IsValid () const
 Checks if the matrix contents are valid

 
dataType GetAt (int iRow, int iCol) const
 Gets the value at row i, column j of the matrix

 
void SetAt (int iRow, int iCol, dataType value)
 Sets the value at row i, column j of the matrix to the specified value

 
int GetRows () const
 Gets the number of rows in the matrix

 
int GetCols () const
 Gets the number of columns in the matrix

 
dataTypeoperator() (int iRow, int iCol)
 Obtains a reference to the value at row i, column j of the matrix

 
const dataTypeoperator() (int iRow, int iCol) const
 Obtains a const reference to the value at row i, column j of the matrix

 
MyMatrixoperator= (const MyMatrix &other)
 Copy operator: modifies the calling matrix contents to equalize the argument matrix

 
MyMatrix operator+ (dataType scalar) const
 Addition operator: returns a new matrix result of the addition of the scalar to all the elements of the calling matrix.
 
MyMatrix operator+ (const MyMatrix &other) const
 Addition operator: returns a new matrix result of the addition the calling and the argument matrix.
 
MyMatrix operator- (dataType scalar) const
 Subtraction operator: returns a new matrix result of the subtraction of the scalar to all the elements of the calling matrix.
 
MyMatrix operator- (const MyMatrix &other) const
 Subtraction operator: returns a new matrix result of the subtraction the calling and the argument matrix.
 
MyMatrix operator* (dataType scalar) const
 Product operator: returns a new matrix result of the product of the scalar and all the elements of the calling matrix.
 
MyMatrix operator* (const MyMatrix &other) const
 Product operator: returns a new matrix result of the product the calling and the argument matrix.
 
MyMatrix operator/ (dataType scalar) const
 Division operator: returns a new matrix result of the division of all the elements of the calling matrix by the scalar.
 
MyMatrix ProdElByEl (const MyMatrix &other) const
 Element by element Product function: returns a new matrix result of the element by element product of the calling and the argument matrix.
 
MyMatrix DivElByEl (const MyMatrix &other) const
 Element by element Division function: returns a new matrix result of the element by element division of the calling and the argument matrix.
 
MyMatrix Transpose () const
 Transpose function: returns a new matrix result of the traspose of the calling matrix.
 
bool IsEqual (const MyMatrix &other, double tolerance=1.0e-6) const
 Compare function: returns a true boolean if the calling and the argument matrix are 'almost' equal.
 
MyMatrix Adjunct (int i_row, int i_col) const
 
dataType Determinant () const
 
MyMatrix GetRow (int i_row) const
 
MyMatrix GetCol (int i_col) const
 
MyMatrix GetSubmatrix (int i_row, int n_rows, int i_col, int n_cols) const
 

Static Public Member Functions

static MyMatrix Eye (int size)
 Create a square diagonal ones matrix (eye)
 

Related Symbols

(Note that these are not member symbols.)

std::ostream & SetPrecisionAndFormat (std::ostream &out, int nDigits, int nDecimals)
 Function for output streams: sets number of digits and decimals for matrix outputs
(as the stream is the first argument, this cannot be a function inside MyMatrix)
SetPrecisionAndFormat(std::cout,8,3); --> next output operations in the console will use 8 digits and 3 decimals for each value

 
std::ostream & operator<< (std::ostream &out, const MyMatrix &m)
 Operator for output streams: outputs the matrix to out

 
std::istream & operator>> (std::istream &in, MyMatrix &m)
 Operator for input streams: gets the matrix contents from in.
 
MyMatrix operator* (dataType scalar, const MyMatrix &m)
 Addition operator: returns a new matrix result of the product of the scalar with all the elements of the calling matrix.
 
MyMatrix operator+ (dataType scalar, const MyMatrix &m)
 Addition operator: returns a new matrix result of the addition of the scalar with all the elements of the calling matrix.
 
MyMatrix operator- (dataType scalar, const MyMatrix &m)
 Subtraction operator: returns a new matrix result of the subtraction of the scalar with all the elements of the calling matrix.
 

Detailed Description

Class MyMatrix A basic class for Matrix algebra operations Ignacio Alvarez - Jan 2024

Constructor & Destructor Documentation

◆ MyMatrix() [1/5]

MyMatrix::MyMatrix ( )

Default constructor: empty invalid matrix.

◆ MyMatrix() [2/5]

MyMatrix::MyMatrix ( int nrows,
int ncols,
dataType value = NAN )

Constructor: creates a matrix with the size desired and filled with a value.

Example:

MyMatrix m1(2,3);
Parameters
nrowsnumber of rows of the matrix
ncolsnumber of columns of the matrix
valuevalue to fill all the elements of the matrix (don't use if no fill is required)

◆ MyMatrix() [3/5]

MyMatrix::MyMatrix ( const std::string & dataTxt)

Constructor: creates a matrix with the size desired and filled with values, all entered from a string.

Example:

MyMatrix mx("[1,4,7;2,8,9;3,3,5.5]");
Parameters
dataTxtstring with Matlab style format (a const reference is passed to avoid construction/destruction):
[d0,0 , d0,1 , d0,2 , ... , d0,n-1 ;
d1,0 , d1,1 , d1,2 , ... , d1,n-1 ;
...
dm-1,0 , dm-1,1 , ... , dm-1,n-1 ]

◆ MyMatrix() [4/5]

MyMatrix::MyMatrix ( const std::vector< dataType > & data,
bool rowVector = true )

Constructor: creates a row or column vector filled from a standard vector.

Parameters
datavector with the data (a const reference is passed to avoid construction/destruction)
rowVectorthe result must be a row vector (true, default) or a column vector (false)

◆ MyMatrix() [5/5]

MyMatrix::MyMatrix ( const MyMatrix & other)

Copy constructor: creates a matrix with the same data as other.

Parameters
othermatrix to copy from (a const reference is passed to avoid construction/destruction)

◆ ~MyMatrix()

MyMatrix::~MyMatrix ( )

Default destructor: removes all memory allocations.

Member Function Documentation

◆ Adjunct()

MyMatrix MyMatrix::Adjunct ( int i_row,
int i_col ) const

◆ Determinant()

dataType MyMatrix::Determinant ( ) const

◆ DivElByEl()

MyMatrix MyMatrix::DivElByEl ( const MyMatrix & other) const

Element by element Division function: returns a new matrix result of the element by element division of the calling and the argument matrix.

(function is const, it will not modify the calling matrix contents)
The division is not the matrix division, that is:
mr=m1{3x4}.DivElByEl(m2{3x4}) --> mr{3x4}, where mri,j = m1i,j / m2k,j

MyMatrix m2=m1+mx*3-2*m1+m1; // After all operations, m2 will be equal to 3 times mx
Parameters
othermatrix to divide (el. by el.) by the calling matrix (a const reference is passed to avoid construction/destruction)
Returns
new matrix with the division (el. by el.) result
Warning
Throws exception if both matrixes are not the same size

◆ Eye()

MyMatrix MyMatrix::Eye ( int size)
static

Create a square diagonal ones matrix (eye)

(function is static, it doesn't need a matrix objet to be called)
mr=MyMatrix::Eye(3) --> mr{3x3}
[1,0,0;
0,1,0;
0,0,1]

Returns
new square matrix with all 0s except 1s in the main diagonal

◆ GetAt()

dataType MyMatrix::GetAt ( int iRow,
int iCol ) const
inline

Gets the value at row i, column j of the matrix

(function is const, it will not modify the matrix contents)
(function is inline, it will execute faster as it's code will be inlined with the caller)

Parameters
iRowrow number (0 to nRows-1)
iColcolumn number (0 to nCols-1)
Returns
value at the position iRow,iCol
Warning
Throws exception if iRow,iCol is outside the matrix

◆ GetCol()

MyMatrix MyMatrix::GetCol ( int i_col) const

◆ GetCols()

int MyMatrix::GetCols ( ) const
inline

Gets the number of columns in the matrix

(function is const, it will not modify the matrix contents)
(function is inline, it will execute faster as it's code will be inlined with the caller)

Returns
number of columns in the matrix

◆ GetRow()

MyMatrix MyMatrix::GetRow ( int i_row) const

◆ GetRows()

int MyMatrix::GetRows ( ) const
inline

Gets the number of rows in the matrix

(function is const, it will not modify the matrix contents)
(function is inline, it will execute faster as it's code will be inlined with the caller)

Returns
number of rows in the matrix

◆ GetSubmatrix()

MyMatrix MyMatrix::GetSubmatrix ( int i_row,
int n_rows,
int i_col,
int n_cols ) const

◆ IsEqual()

bool MyMatrix::IsEqual ( const MyMatrix & other,
double tolerance = 1.0e-6 ) const

Compare function: returns a true boolean if the calling and the argument matrix are 'almost' equal.

(function is const, it will not modify the calling matrix contents)
mr=m1{3x4}.IsEqual(m2{4x3},tole) --> true if for all i,j: |m1i,j-m2j,i|<tole

MyMatrix m2=m1+mx*3-2*m1+m1; // After all operations, m2 will be equal to 3 times mx
Returns
boolean with the comparison result

◆ IsValid()

bool MyMatrix::IsValid ( ) const
inline

Checks if the matrix contents are valid

(function is const, it will not modify the matrix contents)
(function is inline, it will execute faster as it's code will be inlined with the caller)

Returns
true if the matrix has valid contents, false if not

◆ operator()() [1/2]

dataType & MyMatrix::operator() ( int iRow,
int iCol )
inline

Obtains a reference to the value at row i, column j of the matrix

(alternate to GetAt() or SetAt())
(reference obtained can be used to modify matrix contents)
(function is inline, it will execute faster as it's code will be inlined with the caller)

// The following code will obtain m1 = mx flipped
for (int iRow=0;iRow<m1.GetRows();iRow++) {
for (int iCol=0;iCol<m1.GetCols();iCol++) {
m1(iRow,iCol)=mx(iRow,m1.GetCols()-iCol-1);
}
}
Parameters
iRowrow number (0 to nRows-1)
iColcolumn number (0 to nCols-1)
Returns
reference to the element at iRow,iCol
Warning
Throws exception if iRow,iCol is outside the matrix

◆ operator()() [2/2]

const dataType & MyMatrix::operator() ( int iRow,
int iCol ) const
inline

Obtains a const reference to the value at row i, column j of the matrix

(alternate to GetAt())
(reference obtained can not be used to modify matrix contents)
(function is const, it will not modify the matrix contents)
(function is inline, it will execute faster as it's code will be inlined with the caller)

// The following code will obtain m1 = mx flipped
for (int iRow=0;iRow<m1.GetRows();iRow++) {
for (int iCol=0;iCol<m1.GetCols();iCol++) {
m1(iRow,iCol)=mx(iRow,m1.GetCols()-iCol-1);
}
}
Parameters
iRowrow number (0 to nRows-1)
iColcolumn number (0 to nCols-1)
Returns
constant reference to the element at iRow,iCol
Warning
Throws exception if iRow,iCol is outside the matrix

◆ operator*() [1/2]

MyMatrix MyMatrix::operator* ( const MyMatrix & other) const

Product operator: returns a new matrix result of the product the calling and the argument matrix.

(function is const, it will not modify the calling matrix contents)
(function is polymorfic, there are several functions with the same name, the one executed depends on the arguments)
The product is the matrix product, that is:
m1{3x4} * m2{4x6} --> mr{3x6}, where mri,j = sumk(m1i,k * m2k,j)

MyMatrix m2=m1+mx*3-2*m1+m1; // After all operations, m2 will be equal to 3 times mx
Parameters
othermatrix to multiply to calling matrix (a const reference is passed to avoid construction/destruction)
Returns
new matrix with the product result
Warning
Throws exception if number of columns of calling matrix is not the same as the number of rows of argument matrix

◆ operator*() [2/2]

MyMatrix MyMatrix::operator* ( dataType scalar) const

Product operator: returns a new matrix result of the product of the scalar and all the elements of the calling matrix.

(function is const, it will not modify the calling matrix contents)
(function is polymorfic, there are several functions with the same name, the one executed depends on the arguments)
m1{3x4} * scalar --> mr{3x4}, where mri,j = m1i,j * scalar

MyMatrix m2=m1+mx*3-2*m1+m1; // After all operations, m2 will be equal to 3 times mx
Parameters
scalarvalue to multiply to each element
Returns
new matrix with the product result

◆ operator+() [1/2]

MyMatrix MyMatrix::operator+ ( const MyMatrix & other) const

Addition operator: returns a new matrix result of the addition the calling and the argument matrix.

(function is const, it will not modify the calling matrix contents)
(function is polymorfic, there are several functions with the same name, the one executed depends on the arguments)
The operation is the matrix addition, that is:
m1{3x4} + m2{3x4} --> mr{3x4}, where mri,j = m1i,j + m2k,j

MyMatrix m2=m1+mx*3-2*m1+m1; // After all operations, m2 will be equal to 3 times mx
Parameters
othermatrix to add to calling matrix (a const reference is passed to avoid construction/destruction)
Returns
a new matrix with the sum result
Warning
Throws exception if both matrixes are not the same size

◆ operator+() [2/2]

MyMatrix MyMatrix::operator+ ( dataType scalar) const

Addition operator: returns a new matrix result of the addition of the scalar to all the elements of the calling matrix.

(function is const, it will not modify the calling matrix contents)
(function is polymorfic, there are several functions with the same name, the one executed depends on the arguments)
m1{3x4} + scalar --> mr{3x4}, where mri,j = m1i,j + scalar

MyMatrix m2=m1+mx*3-2*m1+m1; // After all operations, m2 will be equal to 3 times mx
Parameters
scalarvalue to add to each element
Returns
a new matrix with the sum result

◆ operator-() [1/2]

MyMatrix MyMatrix::operator- ( const MyMatrix & other) const

Subtraction operator: returns a new matrix result of the subtraction the calling and the argument matrix.

(function is const, it will not modify the calling matrix contents)
(function is polymorfic, there are several functions with the same name, the one executed depends on the arguments)
The operation is the matrix subtraction, that is:
m1{3x4} - m2{3x4} --> mr{3x4}, where mri,j = m1i,j - m2k,j

MyMatrix m2=m1+mx*3-2*m1+m1; // After all operations, m2 will be equal to 3 times mx
Parameters
othermatrix to subtract from calling matrix (a const reference is passed to avoid construction/destruction)
Returns
new matrix with the subtraction result
Warning
Throws exception if both matrixes are not the same size

◆ operator-() [2/2]

MyMatrix MyMatrix::operator- ( dataType scalar) const

Subtraction operator: returns a new matrix result of the subtraction of the scalar to all the elements of the calling matrix.

(function is const, it will not modify the calling matrix contents)
(function is polymorfic, there are several functions with the same name, the one executed depends on the arguments)
m1{3x4} - scalar --> mr{3x4}, where mri,j = m1i,j - scalar

MyMatrix m2=m1+mx*3-2*m1+m1; // After all operations, m2 will be equal to 3 times mx
Parameters
scalarvalue to subtract from each element
Returns
a new matrix with the subtraction result

◆ operator/()

MyMatrix MyMatrix::operator/ ( dataType scalar) const

Division operator: returns a new matrix result of the division of all the elements of the calling matrix by the scalar.

(function is const, it will not modify the calling matrix contents)
(function is polymorfic, there are several functions with the same name, the one executed depends on the arguments)
m1{3x4} / scalar --> mr{3x4}, where mri,j = m1i,j / scalar

MyMatrix m2=m1+mx*3-2*m1+m1; // After all operations, m2 will be equal to 3 times mx
Parameters
scalarvalue to divide each element
Returns
new matrix with the division result

◆ operator=()

MyMatrix & MyMatrix::operator= ( const MyMatrix & other)

Copy operator: modifies the calling matrix contents to equalize the argument matrix

m2 = m1{4x6} → m2{4x6}, where m2i,j = m1i,j

MyMatrix m2=m1+mx*3-2*m1+m1; // After all operations, m2 will be equal to 3 times mx
Parameters
othermatrix to copy from (a const reference is passed to avoid construction/destruction)
Returns
reference to the matrix, as any other copy operator does, so the result can be chained in an expression

◆ ProdElByEl()

MyMatrix MyMatrix::ProdElByEl ( const MyMatrix & other) const

Element by element Product function: returns a new matrix result of the element by element product of the calling and the argument matrix.

(function is const, it will not modify the calling matrix contents)
The product is not the matrix product, that is:
mr=m1{3x4}.ProdElByEl(m2{3x4}) --> mr{3x4}, where mri,j = m1i,j * m2k,j

MyMatrix m2=m1+mx*3-2*m1+m1; // After all operations, m2 will be equal to 3 times mx
Parameters
othermatrix to multiply (el. by el.) with the calling matrix (a const reference is passed to avoid construction/destruction)
Returns
new matrix with the product (el. by el.) result
Warning
Throws exception if both matrixes are not the same size

◆ SetAt()

void MyMatrix::SetAt ( int iRow,
int iCol,
dataType value )
inline

Sets the value at row i, column j of the matrix to the specified value

(function modifies the matrix contents)
(function is inline, it will execute faster as it's code will be inlined with the caller)

Parameters
iRowrow number (0 to nRows-1)
iColcolumn number (0 to nCols-1)
valuevalue to give to element in position iRow,iCol
Warning
Throws exception if iRow,iCol is outside the matrix

◆ Transpose()

MyMatrix MyMatrix::Transpose ( ) const

Transpose function: returns a new matrix result of the traspose of the calling matrix.

(function is const, it will not modify the calling matrix contents)
mr=m1{3x4}.Transpose() --> mr{4x3}, where mri,j = m1j,i

Returns
new matrix with the transpose result

Friends And Related Symbol Documentation

◆ operator*()

MyMatrix operator* ( dataType scalar,
const MyMatrix & m )
related

Addition operator: returns a new matrix result of the product of the scalar with all the elements of the calling matrix.

(as the scalar is the first argument, this cannot be a function inside MyMatrix)
(function is polymorfic, there are several functions with the same name, the one executed depends on the arguments)
scalar * m1{3x4} --> mr{3x4}, where mri,j = scalar * m1i,j

MyMatrix m2=m1+mx*3-2*m1+m1; // After all operations, m2 will be equal to 3 times mx
Parameters
scalarvalue before the * sign, to multiply by each element
mvalue after the * sign, with the matrix elements (a const reference is passed to avoid construction/destruction)
Returns
new matrix with the product result

◆ operator+()

MyMatrix operator+ ( dataType scalar,
const MyMatrix & m )
related

Addition operator: returns a new matrix result of the addition of the scalar with all the elements of the calling matrix.

(as the scalar is the first argument, this cannot be a function inside MyMatrix)
(function is polymorfic, there are several functions with the same name, the one executed depends on the arguments)
scalar + m1{3x4} --> mr{3x4}, where mri,j = scalar + m1i,j

MyMatrix m2=m1+mx*3-2*m1+m1; // After all operations, m2 will be equal to 3 times mx
Parameters
scalarvalue before the + sign, to add to each element
mvalue after the + sign, with the matrix elements (a const reference is passed to avoid construction/destruction)
Returns
new matrix with the addition result

◆ operator-()

MyMatrix operator- ( dataType scalar,
const MyMatrix & m )
related

Subtraction operator: returns a new matrix result of the subtraction of the scalar with all the elements of the calling matrix.

(as the scalar is the first argument, this cannot be a function inside MyMatrix)
(function is polymorfic, there are several functions with the same name, the one executed depends on the arguments)
scalar - m1{3x4} --> mr{3x4}, where mri,j = scalar - m1i,j

MyMatrix m2=m1+mx*3-2*m1+m1; // After all operations, m2 will be equal to 3 times mx
Parameters
scalarvalue before the - sign
mvalue after the - sign, with the matrix elements (a const reference is passed to avoid construction/destruction)
Returns
new matrix with the subtraction result

◆ operator<<()

std::ostream & operator<< ( std::ostream & out,
const MyMatrix & m )
related

Operator for output streams: outputs the matrix to out

(as the stream is the first argument, this cannot be a function inside MyMatrix)
std::cout << m1{3x4} --> outputs the matrix contents to the console

SetPrecisionAndFormat(std::cout,10,3);
std::cout << "m1+m1-2*m1:\n" << m1+m1+2*m1 << std::endl;
Parameters
outoutput stream
mvalue after the << sign, with the matrix elements (a const reference is passed to avoid construction/destruction)
Returns
a reference to the stream is returned so other stream operators and functions can be concatenated

◆ operator>>()

std::istream & operator>> ( std::istream & in,
MyMatrix & m )
related

Operator for input streams: gets the matrix contents from in.

(as the stream is the first argument, this cannot be a function inside MyMatrix)
std::cin >> m1{3x4} --> inputs the matrix contents from the console

MyMatrix m2=m1+mx*3-2*m1+m1; // After all operations, m2 will be equal to 3 times mx
Parameters
inin stream
mvalue after the >> sign, with the matrix that will be filled (matrix must be initialized previously)
Returns
a reference to the stream is returned so other stream operators and functions can be concatenated
Warning
Throws exception if matrix is not initialized previously

◆ SetPrecisionAndFormat()

std::ostream & SetPrecisionAndFormat ( std::ostream & out,
int nDigits,
int nDecimals )
related

Function for output streams: sets number of digits and decimals for matrix outputs
(as the stream is the first argument, this cannot be a function inside MyMatrix)
SetPrecisionAndFormat(std::cout,8,3); --> next output operations in the console will use 8 digits and 3 decimals for each value

SetPrecisionAndFormat(std::cout,10,3);
std::cout << "m1+m1-2*m1:\n" << m1+m1+2*m1 << std::endl;
Parameters
outoutput stream
nDigitsnumber of total digits for each element
nDecimalsnumber of decimal digits for each element
Returns
a reference to the stream is returned so other stream operators and functions can be concatenated

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