} }
return sqrt(sum); }
double sum(Matrix & m) {
double total=0;
for(long i=0;i return total; } Matrix multiply(Matrix & m1,Matrix & m2){ assert(m1.getHeight()==m2.getHeight() && m1.getWidth()==m2.getWidth()); Matrix m(m1.getHeight(),m1.getWidth()); for(long i=0;i //cout< Matrix Matrix::operator /(Matrix &m1) { assert(m1.width==1 && m1.height==1); assert(m1[0][0]!=0); return *this/m1[0][0]; } Matrix Matrix::operator /(double sub) { assert(sub!=0); Matrix m=*this; for(long i=0;i return m; } Matrix & Matrix::operator =(Matrix & m) { if(&m==this) return *this; height=m.height; width=m.width; delete [] p; p=new double[height*width]; for(long i=0;i *(p+width*i+j)=*(m.p+width*i+j); } } return *this; } double * Matrix::operator [](long heightPos) { assert(heightPos>=0 && heightPos<=height);//报错 return p+width*heightPos; } double operator+(double dbl,Matrix & m) { assert(m.getHeight()==1 && m.getWidth()==1); return dbl+m[0][0]; } double operator+(Matrix & m,double dbl) { return dbl+m; } double operator-(double dbl,Matrix & m) { assert(m.getHeight()==1 && m.getWidth()==1); return dbl-m[0][0]; } double operator-(Matrix & m,double dbl) { return -(dbl-m); } ostream & operator<<(ostream & os,Matrix & m) { os<<\ os<<\ for(long i=0;i os< return os; } Matrix::~Matrix(void) { delete [] p; //cout<<\} void Matrix::Test(){ double arr[4][4]={{1,1,1,1},{1,2,3,4},{1,3,6,10},{1,4,10,20}}; Matrix m1,m(&arr[0][0],4,4),m2(&arr[0][0],4,4); m.isPtv(); /* cout<<\ cout< void Matrix::info(){ cout<<\ cout<<\} NO 04 前段时间做KALMAN预测滤波算法要用到矩阵的一些运算,所以自己写了一个矩类,支持矩阵的加减、乘除法,计算矩阵的伴随矩阵、行列式值、逆矩阵,可以再对它扩充。 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // CMatrix.h 头文件 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #ifndef _CMATRIX_H #define _CMATRIX_H typedef char TCHAR; class CMatrix { public: inline int GetRowNum(){return m_Row;}; inline int GetColNum(){return m_Col;}; inline TCHAR * GetMatrixName(){return (TCHAR *)m_Name;}; inline CMatrix(); CMatrix(int Row,int Col,double * Data,TCHAR * Name = NULL); CMatrix(const CMatrix & M); BOOL Create(int Row,int Col,double * Data,TCHAR * Name = NULL); inline ~CMatrix(); CMatrix & operator =(double m); CMatrix & operator =(const CMatrix & m); CMatrix operator *(double m); //与常数相乘; CMatrix operator *(const CMatrix & m); //与矩阵相乘; CMatrix operator +(const CMatrix & m); //与矩阵相加; CMatrix operator -(const CMatrix & m); //与矩相减; double Deternimant(); //求矩阵的行列式; CMatrix Transposed(); //求转置矩阵; CMatrix GetAccompany(); //求伴随矩阵; CMatrix GetReverse(); //求逆矩阵; private: int m_Col; int m_Row; double* m_pData; TCHAR m_Name[100]; }; #endif /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // CMatrix.cpp /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include \ CMatrix::CMatrix() { strcpy(m_Name,\ m_Col = 0; m_Row = 0; m_pData = NULL; } CMatrix::~CMatrix() { strcpy(m_Name,\ m_Col = 0; m_Row = 0; if(m_pData != NULL) { delete [] m_pData; m_pData =NULL; } } CMatrix::CMatrix(const CMatrix & m) { strcpy(m_Name,m.m_Name); m_Row=m.m_Row; m_Col=m.m_Col; Create(m_Row,m_Col,NULL,\ for(int i=0;i m_pData[i]=m.m_pData[i]; } } CMatrix::CMatrix(int Row,int Col,double *Data,TCHAR *Name) { m_Row = Row; m_Col = Col; if(Name != NULL) strcpy(m_Name,Name); else strcpy(m_Name,\ Create(m_Row,m_Col,NULL,m_Name); if(Data != NULL) { for(int i=0;i m_pData[i]=Data[i]; } } } BOOL CMatrix::Create(int Row,int Col,double * Data,TCHAR * Name) { m_Row = Row; m_Col = Col; if(Name != NULL)