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

📄 cmat.cpp

📁 关于求矩阵秩的程序
💻 CPP
字号:
#include "CMat.h"
#include "iostream.h"
#include "math.h"

/*******************************************/
//constructor and destructor
/*******************************************/

CMat::CMat(int rows,int cols)
{
	RowDim=rows;
	ColDim=cols;
	Mat=new float *[RowDim];
	GaussElimd=new float *[RowDim];
	int Cnt;
	for(Cnt=0;Cnt<RowDim;Cnt++)
	{
		Mat[Cnt]=new float[ColDim];
		GaussElimd[Cnt]=new float[ColDim];
	}
	int RowCnt,ColCnt;

	//intialize Mat
	for(RowCnt=0;RowCnt<RowDim;RowCnt++)
	{
		for(ColCnt=0;ColCnt<ColDim;ColCnt++)
		{
			cin>>Mat[RowCnt][ColCnt];
			GaussElimd[RowCnt][ColCnt]=Mat[RowCnt][ColCnt];
		}
		cout<<endl;
	}
}

CMat::~CMat()
{
	int Cnt;
	for(Cnt=0;Cnt<RowDim;Cnt++)
	{
		delete []Mat[Cnt];
		delete []GaussElimd[Cnt];
	}
	delete []Mat;
	delete []GaussElimd;
}

/*******************************************/
//matrix calculations
/*******************************************/

//determinant
//recursive algorithm
//float CMat::Det()
//{

//}

//matrix order
//Gauss elimination algorithm
int CMat::Order()
{
	int order=0;//to be returned
	int RowIndex=0,ColIndex=0;
	int RowCnt,ColCnt;
	int SwapRow;
	float factor;
	bool flag;

	while((RowIndex<RowDim)&&(ColIndex<ColDim))
	{
		while(ColIndex<ColDim)
		{
			float *temp;
			//intialize temp
			temp=new float[RowDim-RowIndex];
			for(ColCnt=0;ColCnt<RowDim-RowIndex;ColCnt++)
				temp[ColCnt]=GaussElimd[RowIndex+ColCnt][ColIndex];
			flag=IsZeroVec(temp,RowDim-RowIndex);
			delete []temp;
			if(flag)			
				ColIndex++;
			else
				break;
		}

		if(flag)
			break;
		else
		{
			if(GaussElimd[RowIndex][ColIndex]==0.0)
			{
				SwapRow=RowIndex;
				for(RowCnt=RowIndex;RowCnt<RowDim;RowCnt++)					
					if(GaussElimd[RowCnt][ColIndex]!=0.0)
					{
						SwapRow=RowCnt;
						break;
					}
				SwapRows(GaussElimd,RowIndex,SwapRow);
			}

			for(RowCnt=RowIndex+1;RowCnt<RowDim;RowCnt++)
			{
				factor=GaussElimd[RowCnt][ColIndex]/GaussElimd[RowIndex][ColIndex];
				for(ColCnt=ColIndex;ColCnt<ColDim;ColCnt++)
					GaussElimd[RowCnt][ColCnt]-=(factor*GaussElimd[RowIndex][ColCnt]);
			}
		}
		RowIndex++;
		ColIndex++;
	}
	for(RowCnt=0;RowCnt<RowDim;RowCnt++)
		if(!IsZeroVec(GaussElimd[RowCnt],ColDim))
			order++;
	return order;
}

//swap two rows in Mat
void CMat::SwapRows(float **Matrix,int Row1,int Row2)
{
	float *temp;
	temp=new float[ColDim];
	int Cnt;
	for(Cnt=0;Cnt<ColDim;Cnt++)
	{
		temp[Cnt]=Matrix[Row1][Cnt];
		Matrix[Row1][Cnt]=Matrix[Row2][Cnt];
		Matrix[Row2][Cnt]=temp[Cnt];
	}
	delete []temp;
}

//judge whether Vec is a zero-vector or not
//Yes: return true
//No: return false
bool CMat::IsZeroVec(float *Vec,int VecDim)
{
	bool flag=true;
	int Cnt;
	for(Cnt=0;Cnt<VecDim;Cnt++)
	{
		if(fabs(Vec[Cnt])>=1.0e-3)
		{
			flag=false;
			break;
		}
	}
	return flag;
}

//show a matrix
void CMat::show(float **mat)
{
	int RowCnt,ColCnt;
	for(RowCnt=0;RowCnt<RowDim;RowCnt++)
	{
		for(ColCnt=0;ColCnt<ColDim;ColCnt++)
		{
			if(fabs(mat[RowCnt][ColCnt])<1.0e-3)
				mat[RowCnt][ColCnt]=0.0;
			cout<<mat[RowCnt][ColCnt]<<"    ";
		}
		cout<<endl;
	}
}

⌨️ 快捷键说明

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