C++矩阵类

矩阵类的要求:

  1. 设计一个二维矩阵类
  2. 成员变量为double*类型,用来存储矩阵中的元素
  3. 写出赋值运算符重载(包括+=,-=,*=),拷贝构造函数和构造函数
  4. 实现矩阵的转置操作
  5. 实现矩阵的逆求解
  6. 实现矩阵的行列式运算
  7. 矩阵的加减乘操作
  8. 利用()运算符重载可以改变某个矩阵元素的值
  9. 实现AX=b求解操作
  10. 求矩阵的秩
  11. 求矩阵的特征值和特征向量
#include
#include
#include
#include
#include
#include 
using namespace std;
const double EPS = 1e-10;
class Matrix{
	
	public:
		int row;
		int col;
		int quantity;
		double *matrix;
		Matrix(int r,int c):row(r),col(c)//构造函数
	    {
	        quantity = r*c;
	        if (quantity>0)
	        {
	            matrix= new double[quantity];
	        }
	        else
	            matrix= NULL;
	    };
		Matrix(const Matrix &rhs)//拷贝构造
	    {
	        row = rhs.row;
	        col = rhs.col;
	        quantity = rhs.quantity;
	        matrix = new double[quantity];
	        for (int i = 0; i>(istream&, Matrix&);
		friend ostream &operator<<(ostream&, Matrix&);
   		friend Matrix  operator+(const Matrix&, const Matrix&);
		friend Matrix operator-(const Matrix&, const Matrix&);
		friend Matrix operator*(const Matrix&, const Matrix&); 
		friend Matrix  operator*(double, const Matrix&);  //数乘矩阵
    	friend Matrix  operator*(const Matrix&, double);  //矩阵乘数 
		Matrix operator=(double *);
		Matrix& operator+=(const Matrix &m1);
		Matrix& operator-=(const Matrix &);
		Matrix& operator*=(const Matrix &m1);
		double*operator[](int i){ return matrix + i*col; }
		Matrix  transpose()const;//矩阵转置 
		double determinant();//矩阵行列式
		Matrix Adjugate();
		void swapRows(int, int);
 		Matrix gaussianEliminate();//高斯消元法 const Matrix &m
 		
 		Matrix diag();
 		void QR(Matrix&, Matrix&)const;
   		Matrix eig_val(int _iters = 1000);
    	Matrix eig_vect(int _iters = 1000);
		friend void menu();
}; 

Matrix::~Matrix()//析构函数 
{
	for(int i=0;i>(istream &is, Matrix &obj)
{
    for(int i=0;i>obj.matrix[i];
    }
    return is;
}
ostream& operator<<(ostream &out, Matrix &obj)
{
    for (int i=0;i=0?Ab[k][j]:-1*Ab[k][j];
					if (cur_abs>max_val)
					{
						max_row=k;
						max_val=cur_abs;
					}
				}
				if (max_row!=i){
					Ab.swapRows(max_row, i);
					flag=true;
				}
				else {
					j++;
				}
			}
		}
		if (flag)
		{
			for (int t=i+1;t=0;--m)
        {
            sum=0;
            for(int j=m+1;j>i;
	Matrix a(n,n);
	//cin>>a;
	
	Matrix c(n,n);
	switch(i) {
		case 1:{
			cout<<"请输入矩阵:"<>a;
			Matrix b(n,n);
			cin>>b;
				c=a+b;cout<<"a+b:\n"<>a;
			Matrix b(n,n);
			cin>>b;
				c=a-b;
				cout<<"a-b:\n"<>a;
			Matrix b(n,n);
			cin>>b;
			c=a*b;cout<<"a*b:\n"<>a;
				c=a.inverse();
				cout<<"矩阵a的逆\n"<>a;
			int det;
			det=a.determinant();
			cout<<"a的行列式:\n"<>a;
			c=a.transpose();
			cout<<"实现矩阵的转置操作\n"<>a;
			Matrix b(n,n);
			cin>>b;
			c=solveAb(a,b);
			cout<<"实现AX=b求解操作\n"<>a;
			int i,j,n;
			cout<<"利用()运算符重载可以改变某个矩阵元素的值"<>i;
			cout<<"请输入col:"<>j;
			cout<<"请输入n:"<>n;
			a(i,j)=n; //利用()运算符重载可以改变某个矩阵元素的值
			break;
		}
		case 9:{
			
			int r;
			cout<<"请输入矩阵:"<>a;
			Matrix temp(n,n);
			temp=a.gaussianEliminate();
			cout<<"temp\n"<>choose;
		if(choose==1)
			menu();
		else
			break;
	}
	 
	return 0;
 } 

你可能感兴趣的:(矩阵,c++,线性代数)