⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matrix.cpp

📁 c++写的矩阵类
💻 CPP
字号:
#include <iostream>
#include "Matrix.h"
#include <cassert>
#include <iomanip>
#include <ctime>
#include <cmath>
using namespace std;


CMatrix::CMatrix(unsigned int irow ,unsigned int icol )
{

    assert(irow > 0 || icol >0);
    row = irow;
    col = icol;
    matrix = new double* [irow];
    for(int i=0 ; i<irow; i++)
	{
        matrix[i]=new double[icol];
	}
	for( i=0 ; i<irow; i++)
	{
		for(int j=0;j<icol;j++)
		{
			matrix[i][j] = 0;
		}
	}

}
CMatrix::CMatrix(const CMatrix& rhs)
{
    row = rhs.GetRow();
    col = rhs.GetCol();
    matrix = new double* [rhs.GetRow()];
    for(int i=0 ; i<rhs.GetRow(); i++)
	{
        matrix[i]=new double[rhs.GetCol()];
	}
	for( i=0 ; i<rhs.GetRow(); i++)
	{
		for(int j=0;j<rhs.GetCol();j++)
		{
			matrix[i][j] = rhs.matrix[i][j];
		}
	}
}

void CMatrix::display() const
{
    cout << '\n';
        for(int i = 0; i< row; i++)
        {
            for(int j = 0; j < col; j++)
			{
                cout <<matrix[i][j] <<'\t';
			}
			 cout <<"\n";
        }
}
void CMatrix::initMatrix()
{
    cout <<"Input data:\n";
    for(int i = 0; i < row; i++)
    {
        for(int  j = 0; j<col ; j++)
		{
            cin >> matrix[i][j];
		}
    }
}
CMatrix& CMatrix::operator +(const CMatrix& rhs)
{
    if(col == rhs.GetCol() || row == rhs.GetRow())
    {
        for(int i = 0; i< row; i++)
		{
            for(int j = 0; j<col; j++)
			{
                matrix[i][j] = matrix[i][j] + rhs.matrix[i][j];
			}
		}
    }
        return *this;
}
CMatrix& CMatrix::operator=(const CMatrix& rhs)
{
    if(this != &rhs)
    {
        if(row != rhs.GetRow() || col !=rhs.GetCol())
        {
            CMatrix::~CMatrix();
			row = rhs.GetRow();
			col = rhs.GetCol();
			matrix = new double* [rhs.GetRow()];
			for(int i=0 ; i<rhs.GetRow(); i++)
			{
				matrix[i]=new double[rhs.GetCol()];
			}
         }
		 
		for(int i=0 ; i<rhs.GetRow(); i++)
		{
			for(int j=0;j<rhs.GetCol();j++)
			{
				matrix[i][j] = rhs.matrix[i][j];
			}
		}
       
    }
	return *this;
}

CMatrix& CMatrix::operator-(const CMatrix& rhs)
{
    for(int i = 0; i< row; i++)
	{
        for(int j = 0; j<col; j++)
		{
            matrix[i][j] = matrix[i][j] - rhs.matrix[i][j];
		}
	}
	return *this;
}
CMatrix& CMatrix::operator*(const CMatrix& rhs)
{

    if(col == rhs.GetRow())
    {
        int i,j,k;
        CMatrix temp(row,rhs.GetCol());

        for( i = 0; i <temp.row; i++)
		{
            for( j = 0; j<temp.col; j++)
            {   
                
                for( k = 0; k<col; k++)
                    temp.matrix[i][j] += matrix[i][k] * rhs.matrix[k][j];
            }     
		}
		col = temp.col;
		row = temp.row;
		for(i = 0; i < row; i++)
		{
			for(j = 0; j< col ;j++)
			{
				matrix[i][j] = temp.matrix[i][j];
			}
		}
             
    }
    else
    {
        cout << "第一个矩阵的列与第二个矩阵的行不等。";
        for(int i = 0; i<row; i++)
		{
            for(int j = 0; j<col; j++)
			{
                matrix[i][j] = 0;
			}
		}
    }
    return *this;
}
CMatrix& CMatrix::zeros()
{
    for(int i = 0; i<row; i++)
	{
        for(int j = 0; j<col ; j++)
		{
            matrix[i][j] = 0;
		}
	}
	return *this;
}
CMatrix& CMatrix::onesmat(int size)
{
    int i,j;
    if(row != size || col != size)
    {
        CMatrix::~CMatrix();
        row = size;
        col = size;
        matrix = new double* [row];
        for(i=0 ; i<row; i++)
		{
            matrix[i]=new double[col];
		}
    }

    for( i = 0; i<row; i++)
	{
        for( j = 0; j<col; j++)
		{
            if(i == j)
                matrix[i][j] = 1;
            else
                matrix[i][j] = 0;
		}
	}
    return *this;
}

CMatrix& CMatrix::randmat(int irrow,int ircol)
{
    int i,j;
    if(row != irrow || col != ircol)
    {
        CMatrix::~CMatrix();
        row = irrow;
        col = ircol;
        matrix = new double* [row];
        for(i=0 ; i<row; i++)
		{
            matrix[i]=new double[col];
		}
    }
	srand(time(NULL));
	for( i = 0; i<row ;i++)
	{
		for( j = 0; j<col; j++)
		{
			int temp = rand(); 
			temp %= 10;
			matrix[i][j] = temp/10.0;
		}
	}
	return *this;
}

CMatrix& CMatrix::randmat(int size)
{
    int i,j;
    if(row != size || col != size)
    {

        CMatrix::~CMatrix();
        row = size;
        col = size;
        matrix = new double* [row];
        for(i=0 ; i<row; i++)
		{
            matrix[i]=new double[col];
		}
    }
	srand(time(NULL));
	for( i = 0; i<row ;i++)
	{
		for( j = 0; j<col; j++)
		{
			int tem = rand();     
			tem %= 10;
			matrix[i][j] = tem/10.0;
		}
    }
	return *this;
}

CMatrix& CMatrix::inverse()
{
	if(row == col)
	{
		int i,j,k;
		int *is = new int[row];
		int *js = new int[col];
		double tempmax,temp;
		for(k = 0; k<row  ; k++)
		{
			tempmax = 0.0;
			for(i = k; i<row ; i++)
			{
				for(j = k; j<row; j++)
				{
					temp = fabs(matrix[i][j]);
					if(temp > tempmax)
					{
						tempmax = temp;
						is[k] = i;
						js[k] = j;
					}
					
				}
			}
			cout <<"\nmax:"<<tempmax<<endl;
			if(tempmax + 1.0 == 1.0)
			{
				delete []is;
				delete []js;
				cout <<"\nmax" << tempmax <<endl;
				cout<<"err ** not inv\n"<<endl;
				return *this;
			}
			if(is[k] != k)
			{
				for(j = 0; j<row ; j++)
				{
					temp = matrix[k][j];
					matrix[k][j] = matrix[is[k]][j];
					matrix[is[k]][j] = temp;
				}
			}
			if(js[k] != k)
			{
				for(i = 0; i<row ; i++)
				{
					temp = matrix[i][k];
					matrix[i][k] = matrix[i][js[k]];
					matrix[i][js[k]] = temp;
				}
			}
			matrix[k][k] = 1.0/matrix[k][k];     
			for(j = 0; j< row; j++)
			{
				if(j !=k)
				{
					matrix[k][j]*=matrix[k][k];
				}
			}
			for(i = 0; i<row; i++)
			{
				if(i != k)
				{
					for(j = 0 ; j <row; j++)
					{
						if(j !=k)
							matrix[i][j] -=matrix[i][k]*matrix[k][j];
					}
				}
			}
			for(i = 0; i< row; i++)
			{
				if(i !=k)
				{
					matrix[i][k] *= -matrix[k][k];
				}
			}
		}
		for(k = row-1; k>-1; k--)
		{
			if(is[k] != k)
			{
				for(j = 0; j <row; j++)
				{
					temp = matrix[k][j];
					matrix[k][j] = matrix[is[k]][j];
					matrix[is[k]][j] = temp;
				}
			}
			if(js[k] != k)
			{
				for( i = 0; i < row; i++)
				{
					temp = matrix[i][k];
					matrix[i][k] = matrix[i][js[k]];
					matrix[i][js[k]] = temp;
				}
			}
		}
		delete []is;
		delete []js;
	}
    return *this;
}


CMatrix& CMatrix::turnmat()
{
    int i,j;
    CMatrix temp(col,row);
    for(  i = 0 ; i<row; i++)
	{
        for(j = 0; j < col; j++)
		{
            temp.matrix[j][i] = matrix[i][j];
		}
	}
    CMatrix::~CMatrix();
    col = temp.col;
    row = temp.row;
    matrix = new double* [row];
    for(i=0 ; i<row; i++)
	{
        matrix[i]=new double[col];
	}
    for(i = 0; i < row; i++)
	{
       for(j = 0; j< col ;j++)
	   {
           matrix[i][j] = temp.matrix[i][j];
	   }
	}
    return *this;
}
CMatrix& CMatrix::rowsort( int therow, bool direct)
{
    if(0< therow || therow < row)
    {
        int i,j;
        int temp;
        
        for(i =0 ; i< col-1; i++)
        {
            for(j = i+1; j<col; j++)
			{
                if(direct)
                {
                    if(matrix[therow][i]>matrix[therow][j])
                    {
                        temp = matrix[therow][i];
                        matrix[therow][i] = matrix[therow][j];
                        matrix[therow][j] = temp; 
                    }
                }
                else
                {
                    if(matrix[therow][i] < matrix[therow][j])
                    {
                        temp = matrix[therow][i];
                        matrix[therow][i] = matrix[therow][j];
                        matrix[therow][j] = temp; 

                    }
                }
			}

        }
    }
	return *this;
}

CMatrix& CMatrix::colsort(int thecol, bool direct1)
{
    if(0< thecol || thecol < col)
    {
        int i,j;
        double temp;
        
        for(i =0 ; i< row-1; i++)
        {
            for(j = i+1; j< row; j++)
			{
                if(direct1)
                {
                    if(matrix[i][thecol]>matrix[j][thecol])
                    {
                        temp = matrix[i][thecol];
                        matrix[i][thecol] = matrix[j][thecol];
                        matrix[j][thecol]= temp; 
                    }
                }
                else
                {
                    if(matrix[i][thecol] < matrix[j][thecol])
                    {
                        temp = matrix[i][thecol];
                        matrix[i][thecol] = matrix[j][thecol];
                        matrix[j][thecol]= temp; 
                    }

                }
			}
        }
    }
    return *this;
}
bool CMatrix::operator==(const CMatrix& rhs)
{
	if(row == rhs.GetRow() || col == rhs.GetCol())
	{
		for(int i = 0; i < row; i ++)
		{
			for(int j = 0; j < col; j ++)
			{
				if(matrix[i][j] != rhs.matrix[i][j])
					return false;
				
			}
		}
		return true;
	}
	return false;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -