A CMatrix class for dynamic sized matrices






2.73/5 (5 votes)
A class for manipulating dynamic matrices.
Introduction
CMatrix
is a class to manipulate a set of matrices with dynamic sizes. It can be used different matrices used for graphical display, solving equations, and for vector manipulations.
Here is the typedef
:
typedef std::vector<double> MXCol; typedef std::vector<double>::iterator MXColPos; typedef std::vector<MXCol> MXRow; typedef std::vector<MXCol>::iterator MXRowPos; typedef enum {MX_EMPTY, MX_ROWEMPTY, MX_COLEMPTY, MX_UNITY, MX_ROWVECTOR, MX_COLVECTOR, MX_SQUARE, MX_RECT} MXType; typedef struct _MX_SIZESTRUCT { int ms_row; int ms_col; }MXSize;
It is clear that I have used a std::vector<>
and is the reason why a dynamic size matrix was created.
The following are the functions and members:
//size functions void mxSetSize(int row, int col); void mxSetSize(MXSize& size); MXSize mxGetSize(); MXType mxGetType(); //matrix operations bool mxInsertRow(int nAfterRow, int nArraySize, const double *array); bool mxInsertCol(int nAfterCol, int nArraySize, const double *array); bool mxInsertRow(int nAfterRow, CMatrix& m); bool mxInsertCol(int nAfterCol, CMatrix& m); bool mxInsertMatrix(int nAfterRow, int nAfterCol, CMatrix& m); bool mxEraseRow(int nRowCount); bool mxEraseCol(int nColCount); void mxFill(double value); //testing bool mxIsOK(); //retrieve matrix CMatrix mxGetSubMatrix(int nRowCount, int nColCount, MXSize& size); //calculation double mxDet(); CMatrix mxInverse(); //operator functions CMatrix& operator=(CMatrix& m); CMatrix& operator=(double a); MXCol& operator[](int n); //protected functions void mxSetType(); void mxZeroCol(MXCol& col); void mxExtendRowZero(int pRowPos, int Num); void mxExtendColZero(int pColPos, int Num); void mxRefreshSize(); //friend functions friend ostream& operator<<(ostream& out, CMatrix& m); friend CMatrix operator +(CMatrix& m1, CMatrix& m2); friend CMatrix operator -(CMatrix& m1, CMatrix& m2); friend CMatrix operator *(CMatrix& m1, CMatrix& m2); friend CMatrix operator *(CMatrix& m, double f); friend CMatrix operator /(CMatrix& m, double f); friend bool operator ==(CMatrix& m1, CMatrix& m2); friend bool operator !=(CMatrix& m1, CMatrix& m2); friend bool mxIsSameSize(CMatrix& m1, CMatrix& m2); friend bool mxIsProducable(CMatrix& m1, CMatrix& m2); friend void swap(CMatrix& m1, CMatrix& m2); friend CMatrix mxTrans(CMatrix& m); //member MXSize mx_size; MXType mx_type; MXRow mx_row;
To declare the matrix class:
MATRIX m(2,3);
which means a 2x3 matrix:
[][][]
[][][]
is created.
Note that if the matrix is not given a size initially, we do:
MATRIX m; MATRIX m(2,0) MATRIX m(0,2)
Feel free to use the code.