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:
void mxSetSize(int row, int col);
void mxSetSize(MXSize& size);
MXSize mxGetSize();
MXType mxGetType();
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);
bool mxIsOK();
CMatrix mxGetSubMatrix(int nRowCount, int nColCount, MXSize& size);
double mxDet();
CMatrix mxInverse();
CMatrix& operator=(CMatrix& m);
CMatrix& operator=(double a);
MXCol& operator[](int n);
void mxSetType();
void mxZeroCol(MXCol& col);
void mxExtendRowZero(int pRowPos, int Num);
void mxExtendColZero(int pColPos, int Num);
void mxRefreshSize();
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);
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.