//矩阵高度 long height; protected:
//矩阵的首地址 double * p; public:
static Matrix One(long n); void info();//显示信息 long getHeight(); long getWidth();
bool isVector();//如果是false,那就是一个数 double Deter();//求行列式determinant bool isPtv();//是否正定 Matrix Eigen();//特征值 Matrix Adjoint();//伴随矩阵 Matrix T();//转置 Matrix Inv();//逆矩阵 void Unity();//归一化
void Store(const char * filename);//把矩阵存储到文件里 Matrix BiCubic(long n);//构造如下类似矩阵 /*{{2,0.5,0,0,0,0}, {0.5,2,0.5,0,0,0}, {0,0.5,2,0.5,0,0}, {0,0,0.5,2,0.5,0}, {0,0,0,0.5,2,0.5,}, {0,0,0,0,0.5,2,} };*/
Matrix subMatrix(long offset) ;//throw (Exception &);
Matrix subMatrix(long x,long y,long WidthOffset,long HeightOffset);//取整数位置子区
//Matrix subMatrix(double x,double y,long WidthOffset,long HeightOffset);//取小数位置子区 void Test();//测试函数
/***********重载部分-Overloaded Part*****************/ Matrix operator+(Matrix &); Matrix operator-(Matrix &); Matrix operator*(Matrix &);
friend Matrix operator*(double alpha,Matrix &);//实数与矩阵相乘 Matrix operator*(double alpha);//矩阵与实数相乘
Matrix operator/(Matrix &);//实际是实数相除或矩阵和实数相除 Matrix operator/(double sub); Matrix operator+=(Matrix &); Matrix operator-=(Matrix &);
Matrix operator*=(Matrix &);//矩阵与实数相乘 Matrix operator*=(double alpha);//矩阵与实数相乘 Matrix &operator=(Matrix &);//赋值
double * operator[](long heightPos);//用于实现用[][]操作矩阵元素 friend Matrix sqrt(Matrix m);//开方
friend double abs(Matrix &);//取绝对值(泛数) friend double sum(Matrix &);//求和
friend Matrix multiply(Matrix &m1,Matrix & m2);//按元素相乘 friend double operator+(double dbl,Matrix &); friend double operator+(Matrix &,double dbl); friend double operator-(double dbl,Matrix &); friend double operator-(Matrix &,double dbl);
friend ostream & operator<<(ostream &,Matrix &);//输出 Matrix(void);//默认构造函数 Matrix(long n);//单位矩阵
Matrix(DibImage & dibImage);//类型转换(把图像文件转换成矩阵,用于图像计算处理) Matrix(double * arrAddress,long arrWidth);//构造一维矩阵(一行,arrWidth列) Matrix(double * arrAddress,long arrHeight,long arrWidth);//构造二维矩阵 Matrix(long Height,long Width);//构造空矩阵 Matrix(const Matrix &);//复制构造函数 virtual ~Matrix(void);//默认析构函数
/***********重载部分-Overloaded Part*****************/ };
#include \#include \//矩阵类-实现
#define SIGN(a,b) ((b) > 0 ? fabs(a) : -fabs(a)) Matrix::Matrix(void):width(1),height(1) {
p=new double[1]; //width=1; //height=1;
//cout<<\}
Matrix::Matrix(long n) {
height=width=n; p=new double[n*n]; for(long i=0;i Matrix::Matrix(DibImage & dibImage) { width=dibImage.getWidth();height=dibImage.getHeight(); p=new double[width*height]; if(p==NULL) assert(0);//内存分配失败 long lLineBytes = WIDTHBYTES(width * 8); LPSTR Src = ::FindDIBBits((LPSTR)::GlobalLock((HGLOBAL) dibImage.GetHDIB())); for(long i=0;i *(p+width*i+j)=double(*((unsigned char *)Src + lLineBytes * (height-1-i) + j)); // *(p+width*i+j)=double(*((unsigned char *)Src + lLineBytes * (i) + j)); } } ::GlobalUnlock((HGLOBAL) dibImage.GetHDIB()); } Matrix::Matrix(double * arrAddress,long arrWidth) { long arrHeight=1; p=new double[arrWidth*arrHeight]; for(long i=0;i *(p+arrWidth*i+j)=*(arrAddress+arrWidth*i+j); } } width=arrWidth; height=arrHeight; } Matrix::Matrix(double * arrAddress,long arrHeight,long arrWidth) { p=new double[arrWidth*arrHeight]; for(long i=0;i *(p+arrWidth*i+j)=*(arrAddress+arrWidth*i+j); } } width=arrWidth; height=arrHeight; } Matrix::Matrix(long Height,long Width) { p=new double[Height*Width]; width=Width; height=Height; } Matrix::Matrix(const Matrix & m)//copy constructor { height=m.height; width=m.width; p=new double[height*width]; for(long i=0;i *(p+width*i+j)=*(m.p+width*i+j); } } } long Matrix::getWidth(){ return width; } long Matrix::getHeight(){ return height; } Matrix Matrix::One(long n) { Matrix m(1,n); for(long i=0;i bool Matrix::isVector() { return !(width==1 && height==1); } Matrix Matrix::subMatrix(long offset) //throw (Exception &) { if(!(height==width && offset<=width && offset>=0)) throw Exception(\at Matrix Matrix::subMatrix(long offset).\\n NOT height==width && offset<=width && offset>=0\ double * t=new double[offset*offset]; for(long i=0;i *(t+offset*i+j)=*(p+width*i+j); } } Matrix m(t,offset,offset); delete [] t; return m; } Matrix Matrix::subMatrix(long x,long y,long WidthOffset,long HeightOffset) { //cout<<\y=\w=\ h=\ in subMatrix 1\\n\ bool log=true; if(x<0 || y<0){ cout<<\ log=false; }else if(WidthOffset<0 || HeightOffset<0){ cout<<\ log=false; }else if(WidthOffset*2+1>width || HeightOffset*2+1>height){ cout<<\ log=false; }else if((x-WidthOffset)<0 || (x+WidthOffset)>width){ cout<<\ log=false; }else if((y-HeightOffset)<0 || (y+HeightOffset)>height){ cout<<\ log=false; } assert(log); Matrix m(HeightOffset*2+1,WidthOffset*2+1); for(long i=0;i m[i][j]=(*this)[i+y-HeightOffset][j+x-WidthOffset]; } } return m; } double Matrix::Deter()//矩阵的行列式 { assert(width==height); double result=1; double k; Matrix m=*this; for(long i=0;i for(long n=i+1;n