MyMatrix example 1.0
MyMatrix example
Loading...
Searching...
No Matches
D:/User/Nacho/Mecatronica/C2-CSSD/ProgramacionCpp/Ej1Matrix/MyMatrix.h
Go to the documentation of this file.
1#ifndef _INC_MYMATRIX_H
2#define _INC_MYMATRIX_H
3
4#include <vector>
5#include <string>
6#include <stdexcept>
7#include <math.h>
8
9typedef double dataType;
10
15class MyMatrix {
16public:
19 MyMatrix();
20
29 MyMatrix( int nrows, int ncols,dataType value=NAN );
30
41 MyMatrix(const std::string& dataTxt );
42
48 MyMatrix( const std::vector<dataType>& data, bool rowVector=true );
49
54 MyMatrix( const MyMatrix& other );
55
59 ~MyMatrix();
60
67 inline bool IsValid() const {
68 return nRows>0 && nCols>0 && data!=nullptr;
69 }
70
80 inline dataType GetAt( int iRow, int iCol ) const {
81 return data[ GetOffset(iRow, iCol ) ];
82 }
83
93 inline void SetAt( int iRow, int iCol, dataType value ) {
94 data[ GetOffset(iRow, iCol) ] = value;
95 }
96
103 inline int GetRows() const {
104 return nRows;
105 }
106
113 inline int GetCols() const {
114 return nCols;
115 }
116
128 inline dataType& operator()(int iRow, int iCol ) {
129 return data[ GetOffset(iRow,iCol) ];
130 }
131
144 inline const dataType& operator()(int iRow, int iCol ) const {
145 return data[ GetOffset(iRow,iCol) ];
146 }
147
155 MyMatrix& operator=( const MyMatrix& other );
156
166 MyMatrix operator+(dataType scalar ) const;
167
179 MyMatrix operator+(const MyMatrix& other ) const;
180
190 MyMatrix operator-(dataType scalar ) const;
191
203 MyMatrix operator-(const MyMatrix& other ) const;
204
214 MyMatrix operator*(dataType scalar ) const;
215
216
228 MyMatrix operator*(const MyMatrix& other ) const;
229
239 MyMatrix operator/(dataType scalar ) const;
240
251 MyMatrix ProdElByEl(const MyMatrix& other) const;
252
263 MyMatrix DivElByEl(const MyMatrix& other) const;
264
272 MyMatrix Transpose() const;
273
274
282 bool IsEqual(const MyMatrix& other, double tolerance=1.0e-6 ) const;
283
295 static MyMatrix Eye(int size);
296
297
298 MyMatrix Adjunct(int i_row,int i_col) const;
299// TODO int Rank() const;
300 dataType Determinant() const;
301
302 MyMatrix GetRow(int i_row) const;
303 MyMatrix GetCol(int i_col) const;
304 MyMatrix GetSubmatrix(int i_row,int n_rows,int i_col,int n_cols) const;
305
306private:
307 // Documentation for these functions and data should not be in doxygen format, so it doesn't appear in the output
308 int nRows=-1,nCols=-1;
309 dataType* data=nullptr;
310
311 void AssignMemo(int i_rows,int i_cols);
312 void FreeMemo();
313 inline int GetOffset(int i_row,int i_col) const
314 {
315 if (i_row>=0 && i_row<nRows && i_col>=0 && i_col<=nCols)
316 return i_row*nCols+i_col;
317 throw std::runtime_error("Matrix error access");
318 }
319
320 void CopyFrom(const MyMatrix& other);
321 bool ReadFromString(const char *pt, int *ptRows = nullptr, int *ptCols = nullptr);
322};
323
324
335std::ostream& SetPrecisionAndFormat(std::ostream& out,int nDigits,int nDecimals);
336
347std::ostream& operator<<(std::ostream& out,const MyMatrix& m );
348
360std::istream& operator>>(std::istream& in,MyMatrix& m );
361
373inline MyMatrix operator*(dataType scalar, const MyMatrix& m ) {
374 return m*scalar;
375}
376
388inline MyMatrix operator+(dataType scalar, const MyMatrix& m ) {
389 return m+scalar;
390}
391
402inline MyMatrix operator-(dataType scalar, const MyMatrix& m ) {
403 return m*(-1)+scalar;
404}
405
406#endif // _INC_MYMATRIX_H
407
408
409
std::istream & operator>>(std::istream &in, MyMatrix &m)
Definition MyMatrix.cpp:372
std::ostream & SetPrecisionAndFormat(std::ostream &out, int nDigits, int nDecimals)
Definition MyMatrix.cpp:363
std::ostream & operator<<(std::ostream &out, const MyMatrix &m)
Definition MyMatrix.cpp:346
double dataType
Datatype to be used with matrixes (.
Definition MyMatrix.h:9
Definition MyMatrix.h:15
MyMatrix GetSubmatrix(int i_row, int n_rows, int i_col, int n_cols) const
dataType & operator()(int iRow, int iCol)
Obtains a reference to the value at row i, column j of the matrix
Definition MyMatrix.h:128
int GetCols() const
Gets the number of columns in the matrix
Definition MyMatrix.h:113
static MyMatrix Eye(int size)
Create a square diagonal ones matrix (eye)
Definition MyMatrix.cpp:207
MyMatrix & operator=(const MyMatrix &other)
Copy operator: modifies the calling matrix contents to equalize the argument matrix
Definition MyMatrix.cpp:116
dataType Determinant() const
Definition MyMatrix.cpp:168
MyMatrix operator-(dataType scalar) const
Subtraction operator: returns a new matrix result of the subtraction of the scalar to all the element...
Definition MyMatrix.cpp:254
bool IsValid() const
Checks if the matrix contents are valid
Definition MyMatrix.h:67
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 ...
Definition MyMatrix.h:373
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...
Definition MyMatrix.h:388
MyMatrix operator/(dataType scalar) const
Division operator: returns a new matrix result of the division of all the elements of the calling mat...
Definition MyMatrix.cpp:270
MyMatrix Adjunct(int i_row, int i_col) const
Definition MyMatrix.cpp:147
MyMatrix DivElByEl(const MyMatrix &other) const
Element by element Division function: returns a new matrix result of the element by element division ...
Definition MyMatrix.cpp:286
MyMatrix operator+(dataType scalar) const
Addition operator: returns a new matrix result of the addition of the scalar to all the elements of t...
Definition MyMatrix.cpp:246
int GetRows() const
Gets the number of rows in the matrix
Definition MyMatrix.h:103
MyMatrix GetCol(int i_col) const
Definition MyMatrix.cpp:194
MyMatrix ProdElByEl(const MyMatrix &other) const
Element by element Product function: returns a new matrix result of the element by element product of...
Definition MyMatrix.cpp:275
~MyMatrix()
Default destructor: removes all memory allocations.
Definition MyMatrix.cpp:107
const dataType & operator()(int iRow, int iCol) const
Obtains a const reference to the value at row i, column j of the matrix
Definition MyMatrix.h:144
MyMatrix Transpose() const
Transpose function: returns a new matrix result of the traspose of the calling matrix.
Definition MyMatrix.cpp:122
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.
Definition MyMatrix.cpp:135
MyMatrix operator*(dataType scalar) const
Product operator: returns a new matrix result of the product of the scalar and all the elements of th...
Definition MyMatrix.cpp:262
MyMatrix operator-(dataType scalar, const MyMatrix &m)
Subtraction operator: returns a new matrix result of the subtraction of the scalar with all the eleme...
Definition MyMatrix.h:402
dataType GetAt(int iRow, int iCol) const
Gets the value at row i, column j of the matrix
Definition MyMatrix.h:80
void SetAt(int iRow, int iCol, dataType value)
Sets the value at row i, column j of the matrix to the specified value
Definition MyMatrix.h:93
MyMatrix()
Default constructor: empty invalid matrix.
Definition MyMatrix.cpp:13
MyMatrix GetRow(int i_row) const
Definition MyMatrix.cpp:182