MyMatrix example 1.0
MyMatrix example
|
#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 | |
dataType & | operator() (int iRow, int iCol) |
Obtains a reference to the value at row i, column j of the matrix | |
const dataType & | operator() (int iRow, int iCol) const |
Obtains a const reference to the value at row i, column j of the matrix | |
MyMatrix & | operator= (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. | |
Class MyMatrix A basic class for Matrix algebra operations Ignacio Alvarez - Jan 2024
MyMatrix::MyMatrix | ( | ) |
Default constructor: empty invalid matrix.
MyMatrix::MyMatrix | ( | int | nrows, |
int | ncols, | ||
dataType | value = NAN ) |
Constructor: creates a matrix with the size desired and filled with a value.
Example:
nrows | number of rows of the matrix |
ncols | number of columns of the matrix |
value | value to fill all the elements of the matrix (don't use if no fill is required) |
MyMatrix::MyMatrix | ( | const std::string & | dataTxt | ) |
Constructor: creates a matrix with the size desired and filled with values, all entered from a string.
Example:
dataTxt | string 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::MyMatrix | ( | const std::vector< dataType > & | data, |
bool | rowVector = true ) |
Constructor: creates a row or column vector filled from a standard vector.
data | vector with the data (a const reference is passed to avoid construction/destruction) |
rowVector | the result must be a row vector (true, default) or a column vector (false) |
MyMatrix::MyMatrix | ( | const MyMatrix & | other | ) |
Copy constructor: creates a matrix with the same data as other.
other | matrix to copy from (a const reference is passed to avoid construction/destruction) |
MyMatrix::~MyMatrix | ( | ) |
Default destructor: removes all memory allocations.
MyMatrix MyMatrix::Adjunct | ( | int | i_row, |
int | i_col ) const |
dataType MyMatrix::Determinant | ( | ) | 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
other | matrix to divide (el. by el.) by the calling matrix (a const reference is passed to avoid construction/destruction) |
|
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]
|
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)
iRow | row number (0 to nRows-1) |
iCol | column number (0 to nCols-1) |
MyMatrix MyMatrix::GetCol | ( | int | i_col | ) | 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)
MyMatrix MyMatrix::GetRow | ( | int | i_row | ) | 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)
MyMatrix MyMatrix::GetSubmatrix | ( | int | i_row, |
int | n_rows, | ||
int | i_col, | ||
int | n_cols ) const |
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
|
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)
|
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)
iRow | row number (0 to nRows-1) |
iCol | column number (0 to nCols-1) |
|
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)
iRow | row number (0 to nRows-1) |
iCol | column number (0 to nCols-1) |
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)
other | matrix to multiply to calling matrix (a const reference is passed to avoid construction/destruction) |
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
scalar | value to multiply to each element |
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
other | matrix to add to calling matrix (a const reference is passed to avoid construction/destruction) |
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
scalar | value to add to each element |
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
other | matrix to subtract from calling matrix (a const reference is passed to avoid construction/destruction) |
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
scalar | value to subtract from each element |
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
scalar | value to divide each element |
Copy operator: modifies the calling matrix contents to equalize the argument matrix
m2 = m1{4x6} → m2{4x6}, where m2i,j = m1i,j
other | matrix to copy from (a const reference is passed to avoid construction/destruction) |
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
other | matrix to multiply (el. by el.) with the calling matrix (a const reference is passed to avoid construction/destruction) |
|
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)
iRow | row number (0 to nRows-1) |
iCol | column number (0 to nCols-1) |
value | value to give to element in position iRow,iCol |
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
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
scalar | value before the * sign, to multiply by each element |
m | value after the * sign, with the matrix elements (a const reference is passed to avoid construction/destruction) |
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
scalar | value before the + sign, to add to each element |
m | value after the + sign, with the matrix elements (a const reference is passed to avoid construction/destruction) |
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
scalar | value before the - sign |
m | value after the - sign, with the matrix elements (a const reference is passed to avoid construction/destruction) |
|
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
out | output stream |
m | value after the << sign, with the matrix elements (a const reference is passed to avoid construction/destruction) |
|
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
in | in stream |
m | value after the >> sign, with the matrix that will be filled (matrix must be initialized previously) |
|
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
out | output stream |
nDigits | number of total digits for each element |
nDecimals | number of decimal digits for each element |