📄 matrix.h
字号:
#define M 400
#include "string.h"
#include "iostream.h"
#include "stdio.h"
#include "stdlib.h"
#include "iomanip.h"
#include "fstream.h"
#include "math.h"
class matrix
{
public:
matrix::matrix(int mSize,int nSize);
matrix::matrix(int mSize,int nSize,double value[]);
matrix::matrix(int nSize);
matrix::matrix();
matrix::matrix(int nSize,double value[]);
matrix (const matrix &other);
matrix::~matrix();
matrix & operator =(const matrix &other);
matrix operator *(const matrix &other) const;
matrix operator +(const matrix &other) const;
matrix operator -(const matrix &other) const;
matrix operator *( double value) const;
bool setele(int mSize,int nSize,double value);
matrix matrix::getaccompany()const;
/*Get the rows or the columns of your matrix*/
int matrix::getrows() const
{return m;}
int matrix::getcols()const
{return n;}
double * matrix::getdata()
{return d;}
matrix matrix::T() const
//求转置矩阵
{
matrix temp(m,n);
if(n==0||m==0) return temp;
if(n==1&&m==1) return *this;
temp.n=m;
temp.m=n;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
temp.d[j*temp.n+i]=d[i*n+j];
}
}
return temp;
}
matrix matrix::operator ~()
//求逆矩阵
{
matrix tp(m,n);
//tp.d[0]=double(1.0/d[0]);
if(m==1&&n==1) return tp;
if(getrange()!=0)
{double s=1.0/getrange();
tp=getaccompany()*s;}
else cout<<" \nthe matrix's range is 0!can not be inverted!"<<endl;
return tp;
}
double matrix::getrange() const
//计算矩阵的行列式的值,矩阵是方阵
{
double sum=0;matrix z(m,n);
if(m==0||n==0) return sum=0.0;
if(m==1&&n==1) return sum=d[0];
if(m==2&&n==2) return sum=d[0]*d[3]-d[1]*d[2];
int i=0;
for(int j=0;j<n;j++)
{
matrix tp(m-1,n-1);
int num=0;
for(int g=0;g<m;g++)
{
if(g==i) continue;
for(int h=0;h<n;h++)
{
if(h==j) continue;
else
{
tp.d[num]=d[g*n+h];
num++;
}
}
}
int tip;
double fuhao;
tip=i+j+2;
fuhao=1.0;
if(tip%2==1) fuhao=-1.0;
z.d[i*n+j]=fuhao*tp.getrange();
}
for(i=0;i<m;i++) sum+=d[i]*z.getele(0,i);
return sum;
}
double matrix::getele(int mSize,int nSize) const
{
double value=0.0;
if(mSize>=0 && mSize<m && nSize>=0 && nSize<n )
if(d)
value=d[nSize+mSize*n];
else
cout<<"An error occured when get element"<<endl;
return value;
}
/* The initiment of the matrix*/
bool init(int m,int n)
{
if(d)
{ delete [] d;
d=NULL;
}
int nSize=m*n,i=0;
if(nSize<0)
return false;
d=new double[nSize];
if(d==NULL)
return false;
//memset(d,0.00,sizeof(double)*nSize);
for(i=0;i<m*n;i++)
d[i]=0.00;
return true;
}
/* set the member of the matrxi */
void setdata(double value[])
{
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
d[i*n+j]=0.0;
// memset(d,0.0,sizeof(double)*m*n);
for(i=0;i<m;i++)
for(int j=0;j<n;j++)
d[i*n+j]=value[i*n+j];
//memcpy(d,value,sizeof(double)*m*n);
}
/* output your matrix*/
void output()
{ int i=0;
if(d)
for(i=0;i<m;i++)
{ cout<<"\n"<<endl;
for(int j=0;j<n;j++)
cout<<d[i*n+j]<<" ";
}
}
private:
int m;//define the rows
int n;//define the columns
double *d;
};
matrix::matrix(int mSize,int nSize)
{
m=mSize;
n=nSize;
d=NULL;
init(m,n);
}
matrix::matrix(int nSize)
{
m=nSize;
n=nSize;
d=NULL;
init(m,n);
}
matrix::matrix()
{
m=1;
n=1;
d=NULL;
init(m,n);
}
matrix::matrix(int nSize,double value[])
{
m=nSize;
n=nSize;
d=NULL;
init(m,n);
setdata(value);
}
matrix::matrix(int mSize,int nSize,double value[])
{
m=mSize;
n=nSize;
d=NULL;
init(m,n);
setdata(value);
}
matrix::matrix (const matrix &other)
{n=other.getcols();
m=other.getrows();
d=NULL;
init(m,n);
memcpy(d,other.d,sizeof(double)*m*n);
}
matrix::~matrix()
{
if(d)
{delete d;
d=NULL;
}
}
/*set the value of fixed element*/
bool matrix::setele(int mSize,int nSize,double value)
{
if(mSize<0 || mSize>m || nSize<0 || nSize>n )
return false;
if(d==NULL)
return false;
d[nSize+n*mSize]=value;
return true;
}
/* the operator of =,==,+,-,*,!= */
matrix & matrix::operator =(const matrix &other)
{
if(&other!=this)
{m=other.getrows();
n=other.getcols();
init(m,n);
//copy the pointer
memcpy(d,other.d,sizeof(double)*m*n);
}
return *this;
}
matrix matrix::operator * ( const matrix & other) const
{ //assert(n==other.getrows());
matrix result(m,other.getcols());
if(n==other.getrows())
{
double value;
for(int i=0;i<m;i++)
for(int j=0;j<other.getcols();j++)
{ value=0.0;
for(int k=0;k<n;k++)
{
value+=getele(i,k)*(other.getele(k,j));
}
result.setele(i,j,value);
}
}
return result;
}
matrix matrix::operator +( const matrix &other) const
{ matrix result(*this);
if((m==other.getrows())&&(n==other.getcols()))
{
double value;
for(int i=0;i<m;++i)
for(int j=0;j<n;++j)
{value=getele(i,j)+other.getele(i,j);
result.setele(i,j,value);
}
return result;
}
else
{cout<<"An error occured when operator + of matrix!"<<endl;
return result;}
}
matrix matrix::operator -(const matrix &other) const
{ matrix result(*this);
double value=0.0;
if((m==other.getrows())&&(n==other.getcols()))
{
for(int i=0;i<m;++i)
for(int j=0;j<n;++j)
{
value=getele(i,j)-other.getele(i,j);
result.setele(i,j,value);
}
return result;
}
else
{cout<<"An error occured when operator + of matrix!"<<endl;
return result;
}
}
matrix matrix::operator *( double value) const
{ matrix result(*this);
for(int i=0;i<m;++i)
for(int j=0;j<n;++j)
result.setele(i,j,value*result.getele(i,j));
return result;
}
matrix matrix::getaccompany()const
/* 求伴随矩阵*/
{
matrix z(m,n);
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
matrix tp(m-1,n-1);
int num=0;
for(int g=0;g<m;g++)
{
if(g==i) continue;
for(int h=0;h<n;h++)
{
if(h==j) continue;
else
{
tp.d[num]=d[g*n+h];
num++;
}
}
}
int tip;
double fuhao;
tip=i+j+2;
fuhao=1.0;
if(tip%2==1) fuhao=-1.0;
z.d[i*n+j]=fuhao*tp.getrange();
}
}
z=z.T();
return z;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -