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

📄 matrix.hpp

📁 随机数类,产生随机数
💻 HPP
📖 第 1 页 / 共 5 页
字号:
			for(col=1;col<=m_col;col++)
			{
				this->SetValue(row,col,cmatrix(row,col)); 
			}
	}
	template <class T> CMatrix<T>::CMatrix(const char *FileName)
	{ 
		ifstream in;
		in.open(FileName);
		if(!in)//不能打开文件
			throw Exception("CMatrix","CMatrix(const char *FileName)","Can not open data file");
		T real,imag;
		unsigned long row,col;
		in>>this->m_row;
		in>>this->m_col;
		this->m_RealBuf=new T[m_row*m_col];
		this->m_ImagBuf=new T[m_row*m_col];
		this->m_Tolerance =(T)1e-8; 
		cout<<m_row;
		for(row=1;row<=m_row;row++)
			for(col=1;col<=m_col;col++)
			{
				in>>real;
				in>>imag;
				this->SetValue(row,col,real,imag);
			}
	}
	template <class T> CMatrix<T>::CMatrix(unsigned long row,unsigned long col,Matrix<T> real,Matrix<T> imag)
	{
		if((row==imag.RowNum())&&(row==real.RowNum())&&(col==imag.ColNum())&&(col==real.ColNum()))
		{
			m_row=row;
			m_col=col;
			m_Tolerance=(T)(0.000001);
			m_RealBuf= new T[m_row*m_col];
			m_ImagBuf= new T[m_row*m_col];
			unsigned long i,j;
			for(i=1;i<=m_row;i++)
				for(j=1;j<=m_col;j++)
				{
					this->SetValue(i,j,real.GetValue(i,j),imag.GetValue(i,j));
				}
		}
		else
		{
			throw Exception("CMatrix","CMatrix(unsigned long row,unsigned long col,Matrix<T> real,Matrix<T> imag)","Dimension mismatch");
		}
	}
	template <class T> void CMatrix<T>::SetDim(unsigned long rowNum,unsigned long colNum)
	{
		m_row=row;
		m_col=col;
		m_Tolerance=(T)(0.000001);
		m_RealBuf= new T[m_row*m_col];
		m_ImagBuf= new T[m_row*m_col];
		unsigned long k;
		for(k=0;k<m_row*m_col;k++)
		{
			m_RealBuf[k]=(T)0.0;
			m_ImagBuf[k]=(T)0.0;
		}
	}
	template <class T> void CMatrix<T>::SaveData(const char *FileName)
	{
		ofstream out;
		out.open(FileName);
		if(!out)
			throw Exception("CMatrix","Save","Can not create data file");
		unsigned long row,col;
		out<<this->m_row<<"	"<<this->m_col<<endl;
		for(row=1;row<=m_row;row++)
		{
			for(col=1;col<=m_col;col++)
			{
				out.setf(ios_base::showpoint); 
				out<<(*this)(row,col).real()<<"	"<<(*this)(row,col).imag()<<"	";
			}
			out<<endl;
		}	
	}
	template <class T> void CMatrix<T>::Save(const char *FileName)
	{
		ofstream out;
		out.open(FileName);
		if(!out)
			throw Exception("CMatrix","Save","Can not create data file");
		unsigned long row,col;
		out<<this->m_row<<"	"<<this->m_col<<endl;
		for(row=1;row<=m_row;row++)
		{
			for(col=1;col<=m_col;col++)
			{
				out.setf(ios_base::showpoint); 
				out<<this->GetValue(row,col)<<"	";
			}
			out<<endl;
		}	
	}
	template <class T> CMatrix<T>::~CMatrix()
	{
		delete[] m_RealBuf;
		delete[] m_ImagBuf;
	}
	template <class T> unsigned long CMatrix<T>::RowNum()
	{
		return m_row;
	}
	template <class T> unsigned long CMatrix<T>::ColNum()
	{
		return m_col;
	}
	template <class T> CMatrix<T>& CMatrix<T>::Neg()
	{
		unsigned long row, col;
		for(row=1;row<=m_row;row++)
			for(col=1;col<=m_col;col++)
			{
				this->SetValue(row,col,(-this->GetValue(row,col))) ;
			}
		return *this;
	}
	template <class T> T CMatrix<T>::ValueAbs(T value) const
	{
		if(value>=0)
			return value;
		else
			return -value;
	}
	template <class T> T CMatrix<T>::MaxAbs(unsigned long& row, unsigned long& col, unsigned long k)
	{
		T a=(T)0.0;
		T br,bi,b;
		unsigned long i, j;
		for(i=1;i<=m_row;i++)
			for(j=1;j<=m_col;j++)
			{
				br=this->GetValue(i,j).real();
				bi=this->GetValue(i,j).imag();
				b=br*br+bi*bi;
				if(a<b)
				{
					a=b;
					row=i;
					col=j;
				}
			}
		return a;
	}
	template <class T> T CMatrix<T>::Adjust(T a)
	{
		T b=floor(a*10000.0+0.5)/10000.0;
		if(ValueAbs(b-a)<this->m_Tolerance)//如果舍入误差很小,小于容许误差,则抛弃
			a=b;
		return a;
	}
	template <class T> void CMatrix<T>::DoAdjust()
	{
		unsigned long k;
		for(k=0;k<=(m_row*m_col);k++)
		{
			this->m_ImagBuf[k]=this->Adjust(this->m_ImagBuf[k]);
			this->m_RealBuf[k]=this->Adjust(this->m_RealBuf[k]);   
		}
	}
	template <class T> CMatrix<T> CMatrix<T>::operator -()
	{
		CMatrix<T> temp(*this);
		return temp.Neg(); 
	}
	template <class T> CMatrix<T>& CMatrix<T>::Tran()
	{
		unsigned long row, col;
		CMatrix<T> cm(*this);
		row=this->m_row; 
		this->m_row =this->m_col;
		this->m_col =row;
		for(row=1;row<=m_row;row++)
			for(col=1;col<=m_col;col++)
			{
				this->SetValue(row,col,cm.GetValue(col,row));
			}
		return *this;
	}
	template <class T> CMatrix<T> CMatrix<T>::t()
	{
		CMatrix<T> temp(*this);
		return temp.Tran(); 
	}
	template <class T> CMatrix<T> CMatrix<T>::conj_tran()
	{
		CMatrix<T> temp(*this);
		temp.Conj();
		return temp.Tran();
	}
	template <class T> void CMatrix<T>::SetValue(unsigned long row, unsigned long col, T real, T image)
	{
		this->m_RealBuf[(row-1)*m_col+col-1]=real;
		this->m_ImagBuf[(row-1)*m_col+col-1]=image; 
	}
	template <class T> void CMatrix<T>::SetValue(unsigned long row, unsigned long col,complex<T>& c)
	{
		this->m_RealBuf[(row-1)*m_col+col-1]=c.real();
		this->m_ImagBuf[(row-1)*m_col+col-1]=c.imag();  
	}
	template <class T> complex<T> CMatrix<T>::GetValue(unsigned long row, unsigned long col)
	{
		if(row>m_row||col>m_col)//如果下标超过矩阵的规模,抛出异常
			throw Exception("CMatrix","GetValue","row or col exceed thd size of matrix");
		T real,image;
		real=this->m_RealBuf[(row-1)*m_col+col-1];
		image=this->m_ImagBuf[(row-1)*m_col+col-1]; 
		return complex<T>(real,image);
	}
	template <class T> CMatrix<T>& CMatrix<T>::Conj()
	{
		unsigned long k;
		for(k=0;k<(m_row*m_col);k++)
			m_ImagBuf[k]*=(T)(-1);
		return *this;
	}
	template <class T> Matrix<T> CMatrix<T>::Real()
	{
		Matrix<T> m(this->RowNum(),this->ColNum());
		unsigned long row, col;
		for(row=1;row<=this->m_row;row++)
			for(col=1;col<=this->m_col;col++)
			{
				m.SetValue(row,col,this->GetValue(row,col).real());
			}
		return m;
	}
	template <class T> Matrix<T> CMatrix<T>::Imag()
	{
		Matrix<T> m(this->RowNum(),this->ColNum());
		unsigned long row, col;
		for(row=1;row<=this->m_row;row++)
			for(col=1;col<=this->m_col;col++)
			{
				m.SetValue(row,col,this->GetValue(row,col).imag());
			}
		return m;
	}
	template <class T> Matrix<T> CMatrix<T>::Arg()
	{
		Matrix<T> m(this->RowNum(),this->ColNum());
		unsigned long row, col;
		for(row=1;row<=this->m_row;row++)
			for(col=1;col<=this->m_col;col++)
			{
				m.SetValue(row,col,arg(this->GetValue(row,col)));
			}
		return m;
	}
	template <class T> complex<T> CMatrix<T>::operator()(unsigned long row, unsigned long col)//根据下标,返回一个复数
	{
		T real, image;
		real=this->m_RealBuf[(row-1)*m_col+col-1];
		image=this->m_ImagBuf[(row-1)*m_col+col-1]; 
		return complex<T>(real,image);//返回一个complex<T>类型,应用real和image初始化的值
	}
	template <class T> CMatrix<T>& CMatrix<T>::operator =(CMatrix& rcmatrix)
	{
		m_col=rcmatrix.ColNum();
		m_row=rcmatrix.RowNum();
		delete[] m_ImagBuf;
		delete[] m_RealBuf; 
		m_ImagBuf=new T[m_row*m_col]; 
		m_RealBuf=new T[m_row*m_col];
		unsigned long row, col;
		for(row=1;row<=m_row;row++)
			for(col=1;col<=m_col;col++)
			{
				this->SetValue(row,col,rcmatrix(row,col)); 
			}
		return *this;
	}
	template <class T> ostream& operator<<(ostream& os,const CMatrix<T>& cmatrix)
	{
		unsigned long row, col;
		for(row=1;row<=cmatrix.m_row;row++)
		{
			for(col=1;col<=cmatrix.m_col;col++)
			{
				os.setf(ios::right);//设置输出为左对齐 
				os<<"(";
				os.width(13); 
				os<<cmatrix.m_RealBuf[(row-1)*cmatrix.m_col+col-1];//(row,col).real();
				if(cmatrix.m_ImagBuf[(row-1)*cmatrix.m_col+col-1]>=0)
				{
					os<<" + ";
					os.setf(ios::left);
					os.width(13);
					os<<cmatrix.ValueAbs(cmatrix.m_ImagBuf[(row-1)*cmatrix.m_col+col-1])<<"i"<<")  ";
				}
				else
				{
					os<<" - ";
					os.setf(ios::left);
					os.width(13);
					os<<cmatrix.ValueAbs(cmatrix.m_ImagBuf[(row-1)*cmatrix.m_col+col-1])<<"i"<<")  ";
				}
			}
			os<<endl;
		}
		return os;
	}
	template <class T> CMatrix<T>& CMatrix<T>::operator +=(CMatrix<T>& rcmatrix)
	{//实部与实部相加,虚部与虚部相加
		unsigned long row, col;
		for(row=1;row<=m_row;row++)
			for(col=1;col<=m_col;col++)
			{
				this->SetValue(row,col, (rcmatrix(row,col)+this->GetValue(row,col)));
			}
		return *this;
	}
	template <class T> CMatrix<T> CMatrix<T>::operator +(CMatrix<T>& rcmatrix)
	{
		if(this->m_row!=rcmatrix.RowNum()||this->m_col!=rcmatrix.ColNum())
			throw Exception("CMatrix","operator +","the two matrice are not in same size, can not perform addition operation.");
		CMatrix<T> temp(*this);
		return temp+=rcmatrix;
	}
	template <class T> CMatrix<T>& CMatrix<T>::operator -=(CMatrix<T>& rcmatrix)
	{
		if(this->m_row!=rcmatrix.RowNum||this->m_col!=rcmatrix.ColNum())
			throw Exception("CMatrix","operator -=","the two matrice are not in same size, can not perform minus operation.");
		return ((*this)+=(-rcmatrix));
	}
	template <class T> CMatrix<T> CMatrix<T>::operator -(CMatrix<T>& rcmatrix)
	{
		if(this->m_row!=rcmatrix.RowNum||this->m_col!=rcmatrix.ColNum())
			throw Exception("CMatrix","operator -","the two matrice are not in same size, can not perform minus operation.");
		CMatrix<T> temp(*this);
		return (temp-=rcmatrix);
	}
	template <class T> CMatrix<T>& CMatrix<T>::operator *=(T k)
	{
		unsigned long row;
		unsigned long col;
		for(row=1;row<=m_row;row++)
			for(col=1;col<=m_col;col++)
			{
				this->SetValue(row,col,(k*this->GetValue(row,col)));  
			}
		return *this; 
	}
	template <class T> CMatrix<T> CMatrix<T>::operator *(T k)
	{
		CMatrix<T> temp(*this);
		return temp*=k;
	}
	template <class T> CMatrix<T>& CMatrix<T>::operator /=(T k)
	{
		if(this->ValueAbs(k)<=this->m_Tolerance)
			throw Exception("CMatrix","operator /=","divided by zero");
		(*this)*=(1/k);
		return *this;
	}
	template <class T> CMatrix<T> CMatrix<T>::operator /(T k)
	{
		if(this->ValueAbs(k)<=this->m_Tolerance)
			throw Exception("CMatrix","operator /","divided by zero");
		CMatrix<T> temp(*this);
		return temp/=k;
	}
	template <class T> CMatrix<T>& CMatrix<T>::operator *=(CMatrix<T>& rcmatrix)
	{
		if(this->m_col!=rcmatrix.RowNum())//如果不满足矩阵相乘地条件
			throw Exception("CMatrix","operator *=","The sizes of two matrices are mismatch");
		unsigned long row, col,k;
		CMatrix<T> temp(*this);
		delete[] m_RealBuf;
		delete[] m_ImagBuf;
		m_RealBuf=new T[m_row*rcmatrix.ColNum()];
		m_ImagBuf= new T[m_row*rcmatrix.ColNum()];
		this->m_col=rcmatrix.ColNum();
		for(row=1;row<=m_row;row++)
			for(col

⌨️ 快捷键说明

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