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

📄 matrix.hpp

📁 随机数类,产生随机数
💻 HPP
📖 第 1 页 / 共 5 页
字号:
		return (*this);
	}
	template <class T> Matrix<T>& Matrix<T>::Asin()
	{
		unsigned long row,col;
		for(row=1;row<=m_row;row++)
			for(col=1;col<=m_col;col++)
			{
				this->SetValue(row,col,(T)asin((double)this->GetValue(row,col)));
			}
		return *this;
	}
	template <class T> Matrix<T>& Matrix<T>::Acos()
	{
		unsigned long row,col;
		for(row=1;row<=m_row;row++)
			for(col=1;col<=m_col;col++)
			{
				this->SetValue(row,col,(T)acos((double)this->GetValue(row,col)));
			}
		return *this;
	}
	template <class T> Matrix<T>& Matrix<T>::Atan()
	{
		unsigned long row,col;
		for(row=1;row<=m_row;row++)
			for(col=1;col<=m_col;col++)
			{
				this->SetValue(row,col,(T)atan((double)this->GetValue(row,col)));
			}
		return *this;
	}
	template <class T> void Matrix<T>::Save(const char* FileName)
	{
		unsigned long row, col;
		ofstream out;
		out.open(FileName);
		if(!out)
			throw Exception("Matrix","Save","Can not create data file");
		out<<m_row<<"    "<<m_col<<endl;
		for(row=1;row<=m_row;row++)
		{
			for(col=1;col<=m_col;col++)
			{
				//out.setf(ios_base::showpoint); 
				out.width(15);
				out<<GetValue(row,col)<<"  ";
			}
			out<<endl;
		}
	}
	template <class T> Matrix<T>::~Matrix()
	{
		delete[] this->m_buf;//释放内存空间 
	}
	template <class T> T Matrix<T>::Adjust(T a)//对小数点后五位四舍五入
	{
		T b=floor(a*10000.0+0.5)/10000.0;
		if(ValueAbs(b-a)<m_Tolerance)//如果舍入误差很小,小于容许误差,则抛弃
			a=b;
		return a;
	}
	template <class T> void Matrix<T>::DoAdjust()
	{
		unsigned long row,col;
		for(row=1;row<=m_row;row++)
			for(col=1;col<=m_col;col++)
			{
				SetValue(row,col,Adjust((*this)(row,col)));
			}
	}
	template <class T> unsigned long Matrix<T>::RowNum() const//返回矩阵的行数
	{
		return m_row; 
	}
	template <class T> unsigned long Matrix<T>::ColNum() const//返回矩阵的列数
	{
		return m_col;
	}
	template <class T> bool Matrix<T>::IsEqual(Matrix<T>& matrix)//判断两个矩阵是否相等
	{// is this matrix equal to another matrix
		if( (m_col==matrix.Cols()) && (m_row=matrix.Rows()) )//如果两个矩阵的行数列数相等,比较各个元素
		{
			long i,j;
			for(i=1;i<=m_row;i++)
				for(j=1;j<=m_col;j++)
				{
					if ((this->GetValue(i,j)-matrix.GetValue(i,j)) > this->m_Tolerance) 
						return false;
				}
			return true;
		}
		else
			return false;
	}
	template <class T> bool Matrix<T>::IsSquare()//判断当前矩阵是否为方阵
	{//is a square matrix?
		if(m_row==m_col)
			return true;
		else 
			return false;
	}
	
	template <class T>
	bool Matrix<T>::IsSym()//判断当前矩阵是否为对称矩阵, 只对方阵 
	{
		if(this->m_col!=this->m_row)//如果当前矩阵不是方阵,则抛出异常,只有方阵才满足这项计算的要求
		{
			throw Exception("Matrix","IsSym","The matrix is not square.");
			return false;
		}
		
		unsigned long row, col;
		for(row=1;row<=this->m_row; row++ )
		{
			for(col=(row+1);col<=this->m_col;col++)
			{
				if(ValueAbs(this->GetValue(row,col)-this->GetValue(col,row))>this->m_Tolerance)
					return false;
			}
		}
		return true;
	}

	
	template <class T> bool Matrix<T>::IsI()
	{
		if(!this->IsSquare())//如果当前矩阵不是方阵,抛出异常
			throw Exception("Matrix","IsI","The matrix is not square.");
		else{
			unsigned long row,col;
			for(row=1;row<=this->m_row;row++)
				for(col=1;col<=this->m_col;col++)
				{
					if(row==col)
					{
						T temp, unit;
						unit=1;
						temp=*this(row,col)-unit;
						if(temp<0)// less than zero
						{
							if(temp<(-this->m_Tolerance)) 
								return false;
						}
						else{// more than zero
							if(temp>this->m_Tolerance)
								return false;
						}
					}
					else{
						if(*this(row,col)>0)//more than zero
						{
							if(*this(row,col)>this->m_Tolerance)
								return false;
						}
						else{//less than zero
							if(*this(row,col)<(-this->m_Tolerance))
								return false;
						}
					}
				}
			return true;
		}
		
	}

	template <class T>
		Matrix<T> Matrix<T>::ExtractRowV(unsigned long row) const
	{
		Matrix<T> matrix(1, this->m_col);	// row vector
		
		if(row>this->m_row)	// exception manipulation
			throw Exception("Matrix","ExtractRowV","Subscripts are over matrix size");
		else
		{
			for(int i=1; i<=matrix.ColNum(); i++)
			{
				matrix.SetValue(1, i, this->GetValue(row, i));
			}
			return matrix;
		}
	}

	template <class T>
		Matrix<T> Matrix<T>::ExtractColV(unsigned long col) const
	{
		Matrix<T> matrix(this->m_row, 1);	// column vector
		
		if(col>this->m_col)	// exception manipulation
			throw Exception("Matrix","ExtractColV","Subscripts are over matrix size");
		else
		{
			for(int i=1; i<=(int)matrix.RowNum(); i++)
			{
				matrix.SetValue(i, 1, this->GetValue(i, col));
			}
			return matrix;
		}
	}

	template <class T>
		void Matrix<T>::SetRowV(const unsigned long row, const Matrix<T>& rowElement)
	{
		if(row>m_row)	// exception manipulation
			throw Exception("Matrix","SetRowV","Subscripts are over matrix size");
		else
		{
			unsigned long i;
			for(i=1; i<=m_col; i++)
			{
				SetValue(row, i, rowElement.GetValue(1, i));
			}
		}
	}
	
	template <class T>
		void Matrix<T>::SetColV(const unsigned long col, const Matrix<T>& colElement)
	{
		if(col>m_col)	// exception manipulation
			throw Exception("Matrix","SetColV","Subscripts are over matrix size");
		else
		{
			unsigned long i;
			for(i=1; i<=m_row; i++)
			{
				SetValue(i, col, colElement.GetValue(i, 1));
			}
		}
	}

	template <class T> T Matrix<T>::GetValue(unsigned long row, unsigned long col) const
	{
		//这里要注意异常处理,可能存在要求返回的矩阵元素的下标超过矩阵的规模
		if(row>this->m_row || col>this->m_col)//如果下标超出矩阵的范围,抛出异常
			throw Exception("Matrix","GetValue","Subscripts are over matrix size");
		else{
			return m_buf[(row-1)*m_col+col-1];//从缓冲区读出数值 (row-1)*m_col+col-1
		}
	}

	template <class T> void Matrix<T>::SetValue(unsigned long row,unsigned long col,T value)
	{
		if(row>this->m_row|| col > this->m_col)//如果下标超过矩阵的规模
			throw Exception("Matrix","SetValue","Subscripts are over matrix size");
		else{
			this->m_buf[(row-1)*m_col+col-1]=value; 
		}
	}

	template <class T> Matrix<T>& Matrix<T>::Tran()
	{
		Matrix<T> matrix(this->m_col,this->m_row);// 一个和当前矩阵规模相同的矩阵
		unsigned long row, col;//矩阵的行数和列数
		for(row=1;row<=matrix.RowNum();row++)
			for(col=1;col<=matrix.ColNum();col++)
			{
				matrix.SetValue(row,col,GetValue(col,row));
			}
		*this=matrix;
		return *this;
	}

	template <class T>
	Matrix<T> Matrix<T>::t()
	{
		Matrix<T> matrix(this->m_col,this->m_row);// 一个和当前矩阵规模相同的矩阵
		unsigned long row, col;//矩阵的行数和列数
		for(row=1;row<=matrix.RowNum();row++)
			for(col=1;col<=matrix.ColNum();col++)
			{
				matrix.SetValue(row,col,GetValue(col,row));
			}
		return matrix;
	}

	template <class T> Matrix<T>& Matrix<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)(row,col))); 
			}
		return *this;
	}
	template <class T> void Matrix<T>::SwapCols(unsigned long col1,unsigned long col2)
	{
		if(col1>this->m_col||col2>this->m_col)//如果列数超过了矩阵的规模,抛出异常。
			throw Exception("Matrix","SwapCols","At lease one column number is over the matrix size");
		else{
			T temp;
			unsigned long row;
			for(row=1;row<=this->m_row;row++)
			{
				temp=GetValue(row,col1);
				SetValue(row,col1,GetValue(row,col2));
				SetValue(row,col2,temp);
			}
		}
	}
	template <class T> void Matrix<T>::SwapRows(unsigned long row1,unsigned long row2)
	{
		if(row1>m_row||row2>m_col)//如果行数超过了矩阵的规模,抛出异常
			throw Exception("Matrix","SwapRows","At least one row number is over the matrix size");
		else{
			unsigned long col;
			T temp;
			for(col=1;col<=this->m_col;col++)
			{
				temp=GetValue(row1,col);
				SetValue(row1,col,GetValue(row2,col));
				SetValue(row2,col,temp);
			}
		}
	}
	template <class T> T Matrix<T>::operator()(unsigned long row, unsigned long col)
	{
		if(row>this->m_row|| col>this->m_col)//如果下标超过矩阵规模
			throw Exception("Matrix","Operator()","Subscripts are over the matrix size");
		else{
			return this->m_buf[(row-1)*m_col+col-1];
		}
	}

	template <class T> Matrix<T>& Matrix<T>::operator =(const Matrix<T>& rmatrix)
	{
		m_col=rmatrix.ColNum();
		m_row=rmatrix.RowNum();
		unsigned long row, col;
		delete[] m_buf;
		m_buf = new T[m_col*m_row];
		for(row=1;row<=m_row;row++)
			for(col=1;col<=m_col;col++)
			{
				SetValue(row,col,(rmatrix.GetValue(row,col)));
			}
		return(*this);
	}



	template <class T>
	Matrix<T>& Matrix<T>::operator =(const T& scalar)
	{
		unsigned long row, col;
		for(row=1;row<=this->m_row;row++)
			for(col=1;col<=this->m_col;col++)
			{
				SetValue(row,col,scalar);
			}
		return(*this);
	}

	template <class T> T Matrix<T>::ValueAbs(T value)//计算绝对值,私有函数
	{
		if(value>=0)
			return value;
		else
			return -value;
	}

	template <class T> void Matrix<T>::GenI()//产生单位矩阵
	{
		unsigned long row, col;
		if(!this->IsSquare())//如果不是方阵,不能生成单位矩阵,抛出异常
			throw Exception("Matrix","GenI","The current matrix is not square, can't generate identity matrix.");
		else{
			for(row=1;row<=this->m_row; row++)
				for(col=1;col<=this->m_col; col++)
				{
					if(row==col)
						this->SetValue(row,col,1.0);
					else
						this->SetValue(row,col,0.0);
				}
		}
	}
	template <class T> void Matrix<T>::GenOnes()//产生所有元素都为1的矩阵
	{
		unsigned long row, col;

⌨️ 快捷键说明

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