65,176
社区成员




#include "myMatrix.h"
#include <iostream>
#include <math.h>
using namespace std;
//start 默认构造函数
Matrix::Matrix()
{
this->row=0; this->col=0;
this->n=0; this->p=0;
}//end 默认构造函数
//start 带参数的构造函数
Matrix::Matrix(const int& r,const int& c)
{
this->row=r; this->col=c; this->n=r*c;
this->p=new double[n];
for(int i=0;i<n;++i)
{
this->p[i]=0.0;//默认值设为0
}
}
Matrix::Matrix(const int& r,const int& c,const double* p)
{
this->row=r; this->col=c; this->n=r*c;
this->p=new double[n];
for(int i=0;i<n;++i)
{
this->p[i]=p[i];//给p赋值
}
this->modify();
}//end 带参数的构造函数
//start 复制构造函数
Matrix::Matrix(const Matrix& other)
{
this->row=other.row; this->col=other.col;
this->n=row*col;
this->p=new double[n];
for(int i=0;i<n;++i)
{
this->p[i]=other.p[i];
}
this->modify();
}//end 复制构造函数
//start 赋值操作符
Matrix& Matrix::operator=(const Matrix& other)
{
if(*this==other) return *this;
this->row=other.row;
this->col=other.col;
this->n=row*col;
delete[] this->p;
this->p=new double[n];
for(int i=0;i<n;++i)
{
this->p[i]=other.p[i];
}
this->modify();
return *this;
}//end 赋值操作符
//start +=重载
Matrix& Matrix::operator+=(const Matrix& other)
{
if(this->row!=other.row
|| this->col!=other.col)
{
cout<<"行列相等的矩阵才能进行\"+=\"操作!"<<endl;
return *this;
}
for(int i=0;i<this->n;++i)
{
this->p[i]+=other.p[i];
}
this->modify();
return *this;
}//end +=重载
//start -=重载
Matrix& Matrix::operator-=(const Matrix& other)
{
if(this->row!=other.row
|| this->col!=other.col)
{
cout<<"行列相等的矩阵才能进行\"-=\"操作!"<<endl;
return *this;
}
for(int i=0;i<this->n;++i)
{
this->p[i]-=other.p[i];
}
this->modify();
return *this;
}//end -=重载
//start +重载
Matrix operator+(const Matrix& lhs,const Matrix& rhs)
{
Matrix lhsTemp=lhs,rhsTemp=rhs;
return lhsTemp+=rhsTemp;
}//end +重载
//start -重载
Matrix operator-(const Matrix& lhs,const Matrix& rhs)
{
Matrix lhsTemp=lhs,rhsTemp=rhs;
return lhsTemp-=rhsTemp;
}//end -重载
//start *重载(矩阵相乘)
Matrix operator*(const Matrix& lhs,const Matrix& rhs)
{
if(lhs.row!=lhs.col)
{
cout<<"相乘矩阵不符合条件!"<<endl;
return Matrix();
}
Matrix temp(lhs.row,rhs.col);
for(int r=0;r<temp.row;++r)
for(int c=0;c<temp.col;++c)
{
for(int k=0;k<lhs.col;++k)
temp[r][c]+=lhs[r][k]*rhs[k][c];
}
return temp;
}//end *重载(矩阵相乘)
// start *重载(实数与矩阵相乘)
Matrix operator*(const double& c,const Matrix& M)
{
Matrix temp(M.row,M.col);
for(int i=0;i<temp.row*temp.col;++i)
{
temp.p[i]=M.p[i]*c;
}
return temp;
}// end *重载(实数与矩阵相乘)
//start 转置
Matrix trv(const Matrix& M)
{
Matrix temp(M.col,M.row);
for(int r=0;r<M.row;++r)
for(int c=0;c<M.col;++c)
temp[c][r]=M[r][c];
return temp;
}//end 转置
//start 打印输出
void Matrix::print()const
{
for(int i=0;i<row;++i)
{
for(int j=0;j<col;++j)
cout<<(*this)[i][j]<<" ";
cout<<endl;
}
}//end 打印输出
//start 相等操作符(友元函数)
bool operator==(const Matrix& lhs,const Matrix& rhs)
{
if(lhs.row!=rhs.row)
return false;
if(lhs.row!=rhs.col)
return false;
for(int i=0;i<lhs.row*lhs.col;++i)
{
if(lhs.p[i]!=rhs.p[i])
return false;
}
return true;
}//end 相等操作符(友元函数)
//start 下标操作符
double* Matrix::operator[](int indexRow)const
{
return this->p+indexRow*col;
}//end 下标操作符
//start 下标操作符
double* Matrix::operator[](int indexRow)
{
return this->p+indexRow*col;
}//end 下标操作符
//start 求行列式的值(方阵)
double det(Matrix M)
{
if(M.row!=M.col)
{
cout<<"det()只能对方阵操作!"<<endl;
return 0;
}
double detValue=1;
//start 将行列式化为阶梯型
for(int c=0,r=0;c<M.col&&r<M.row;++c,++r)
{
int ind=r;//主元行号
//start 寻找主元
for(int i=c;i<M.row;++i)
{
if(M[i][c]!=0)
{
ind=i;
break;
}
//某子列全为0,行列式为0
if(i==M.row)
return 0;
}//end 寻找主元
//start 交换两行
if(ind!=r)
{
double temp=0;
for(int j=c;j<M.col;++j)
{
temp=M[ind][j];
M[ind][j]=M[r][j];
M[r][j]=temp;
}
detValue*=-1;//行列式取反
}//end 交换两行
M.print();
//start 主元下元素化0
for(int i=r+1;i<M.row;++i)
{
double temp=M[i][c]/M[r][c];
for(int j=c;j<M.col;++j)
{
M[i][j]-=M[r][j]*temp;
}
}//end 主元下元素化0
}//end 将行列式化为阶梯型
//start 计算行列式的值
for(int i=0;i<M.col;++i)
detValue*=M[i][i];
//end 计算行列式的值
return detValue;
}//end 求行列式的值(方阵)
//start 求逆阵
Matrix inv(Matrix M)
{
//start 判断矩阵是否可逆
if(M.row!=M.col)
{
cout<<"只有方阵才能求逆!"<<endl;
return Matrix();
}
//end 判断矩阵是否可逆
//start 构造单位阵
Matrix E(M.row,M.col);
for(int i=0;i<M.row;++i)
E[i][i]=1.0;
//end 构造单位阵
//start 将矩阵化为阶梯型
for(int c=0,r=0;c<M.col&&r<M.row;++c,++r)
{
int ind;//记录主元行号
//start 寻找主元
for(int i=r;i<M.row;++i)
{
if(M[i][c]!=0)
{
ind=i;
break;
}
if(i==M.row-1)
{
cout<<"矩阵不可逆!"<<endl;
return Matrix();
}
}
//end 寻找主元
//start 交换两行
for(int j=0;j<M.col;++j)
{
double temp;
temp=M[ind][j];
M[ind][j]=M[r][j];
M[r][j]=temp;
temp=E[ind][j];
E[ind][j]=E[r][j];
E[r][j]=temp;
}
//end 交换两行
//start 主元下元素化0
for(int i=r+1;i<M.row;++i)
{
double temp;
temp=M[i][c]/M[r][c];
for(int j=0;j<M.col;++j)
{
M[i][j]-=M[r][j]*temp;
E[i][j]-=E[r][j]*temp;
}
}
//end 主元下元素化0
}//end 将矩阵化为阶梯型
//start 主元化1
for(int r=0;r<M.row;++r)
{
double temp=M[r][r];
for(int c=r;c<M.col;++c)
{
M[r][c]/=temp;
}
for(int c=0;c<M.col;++c)
{
E[r][c]/=temp;
}
}
//end 主元化1
//start 化对角型
for(int r=M.row-2;r>=0;--r)
{
for(int i=M.row-1;i>r;--i)
{
for(int j=0;j<M.col;++j)
{
E[r][j]-=E[i][j]*M[r][i];
}
for(int j=0;j<M.col;++j)
{
M[r][j]-=M[i][j]*M[r][i];
}
}
}//end 化对角型
return E;
}//end 求逆阵
//start 求矩阵的秩
int rnk(Matrix A)
{
// 用于保存矩阵的秩
int rankValue = 0;
//start 按列循环,将矩阵化为行阶梯型
for(int c=0,r=0; c<A.col && r<A.row; c++)
{
double mainElement;//记录主元
int ind=-1;//记录主元所在行
// start 选主元
for(int i = c;i < A.row;i++)
{
if( fabs(A[i][c])>=1.0e-10 )//当前子列中存在非0元素
{ //则将该非0元素设为主元
ind = i;//记下主元所在行
mainElement = A[i][c];//记下主元
break;
}
}//end 选主元
// start 有主元,程序继续执行
if(ind != -1)
{
// start 将主元化1
for(int j = c; j <A.col; j++)
{
A[ind][j]=A[ind][j]/mainElement;
}//end 将主元化1
// start 将主元行与右下方矩阵首行互换
for(int j = 0; j <A.col; j++)
{
double temp = A[c][j];
A[c][j]=A[ind][j];
A[ind][j]=temp;
}//end 将主元行与右下方矩阵首行互换
// start 将主元所在列化0
for(int i = r+1; i <A.row; i++)
{
double temp = A[i][c]/A[r][c];
for(int j = c; j<A.col;j++)
{
A[i][j]=A[i][j]-A[r][j]*temp;
}
}//end 将主元所在列化0
++r;//行+1(没有主元时不增加)
}// end 有主元,程序继续执行
}
//start 判断A中有多少非0行
for(int i = 0;i < A.row; ++i)
{
int j;
for( j = 0; j < A.col; ++j)
if(A[i][j]!=0) break;
if(j!=A.col) ++rankValue;
}
//end 判断A中有多少非0行
return rankValue;
}//end 求矩阵的秩
//start 对矩阵修整
void Matrix::modify()
{
for(int k=0;k<this->n;++k)
if(fabs(this->p[k])<1.0e-10)
this->p[k]=0;
}
//start 析构函数
Matrix::~Matrix()
{
delete[] this->p;
}//end 析构函数
#ifndef _MYMATRIX_H
#define _MYMATRIX_H
class Matrix
{
public:
Matrix();//默认构造函数
//Matrix();
//带参数的构造函数
Matrix(const int&,const int&);
Matrix(const int&,const int&,const double*);
//复制构造函数
Matrix(const Matrix&);
//赋值操作符
Matrix& operator=(const Matrix&);
Matrix& operator+=(const Matrix&);
Matrix& operator-=(const Matrix&);
//析构函数
~Matrix();
//相等操作符
friend bool operator==(const Matrix&,const Matrix&);
//不等操作符
friend bool operator!=(const Matrix& lhs,const Matrix& rhs)
{return !(lhs==rhs);}
//下标操作符
double* operator[] (int index);
//下标操作符
double* operator[] (int index)const;
//打印输出
void print()const;
//算数运算符重载
// *(矩阵相乘)
friend Matrix operator*(const Matrix&,const Matrix&);
// *(实数*矩阵)
friend Matrix operator*(const double&,const Matrix&);
// *(矩阵*实数)
friend Matrix operator*(const Matrix& M,const double& c)
{
return c*M;
}
//转置
friend Matrix trv(const Matrix&);
//求行列式的值(方阵)
friend double det(Matrix);
//求逆阵
friend Matrix inv(Matrix);
//求矩阵的秩
friend int rnk(Matrix A);
//对矩阵修整
void modify();
private:
double* p;//存放数组
int row;//行数
int col;//列数
int n;//元素个数
};
// +
Matrix operator+(const Matrix&,const Matrix&);
// -
Matrix operator-(const Matrix&,const Matrix&);
#endif