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

📄 matrixref.hpp

📁 简单的C++计算库.其中实现了矩阵运算,大数运算.矩阵处理中使用lazy calculate,并且封装常见算法,可以将普通matlab程序用SDL改写.
💻 HPP
字号:
#ifndef		_SDL_MATHS_MATRIX_MATRIX_REF_H_
#define		_SDL_MATHS_MATRIX_MATRIX_REF_H_

SDL_MATHS_MATRIX_BEGIN

class	MatrixRef{};

template<typename _MatType>
class	RowRef;

template<typename _MatType>
struct	BasicMatrixExpr;

template<typename _MatType>
class	ConstRowRef
	:	public	MatrixRef
{
public:

	typedef	typename _MatType::NumberType	NumberType;

	ConstRowRef(const _MatType& _Mat, Int32 _Row)
		:	Mat(_Mat), Row(_Row)
	{
	}

	ConstRowRef(const ConstRowRef& other) :
		Mat(other.Mat), Row(other.Row)
	{
	}

	ConstRowRef(const RowRef<_MatType>& other);

	NumberType	operator [] (Int32 _Col) const
	{
		return Mat(Row, _Col);
	}

private:

	const	_MatType&	Mat;
	Int32				Row;

};

template<typename _MatType>
class	RowRef
	:	public	MatrixRef
{
	friend class ConstRowRef<_MatType>;

public:

	typedef	typename _MatType::NumberType	NumberType;

	RowRef(_MatType& _Mat, Int32 _Row)
		:	Mat(_Mat), Row(_Row)
	{
	}

	RowRef(const RowRef& other)
		:	Mat(other.Mat), Row(other.Row)
	{
	}

	NumberType	operator [] (Int32 _Col) const
	{
		return Mat(Row, _Col);
	}

	NumberType&	operator [] (Int32 _Col)
	{
		return Mat(Row, _Col);
	}

private:

	_MatType&	Mat;
	Int32		Row;

};

template<typename _MatType>
ConstRowRef<_MatType>::ConstRowRef(const RowRef<_MatType>& other)
	:	Mat(other.Mat), Row(other.Row)
{
}

/*
*template<typename _MatType>
*class		ConstSubMatrixRef
*template<typename _MatType>
*class		SubMatrixRef
*/

template<typename _MatType>
class	SubMatrixRef;

template<typename _MatType>
class	ConstSubMatrixRef
	:	public	MatrixRef
{
public:

	typedef	typename _MatType::NumberType	NumberType;
	typedef	typename NonConstType<ConstSubMatrixRef>::Type	SelfType;

	ConstSubMatrixRef(const _MatType& _Mat, Int32 _RowBegin, Int32 _RowEnd,
								 Int32 _ColBegin, Int32 _ColEnd)
		:	Mat(_Mat),
			RowBegin(_RowBegin), RowEnd(_RowEnd),
			ColBegin(_ColBegin), ColEnd(_ColEnd)
	{
	}

	ConstSubMatrixRef(const ConstSubMatrixRef& other) 
		:	Mat(other.Mat),
			RowBegin(other.RowBegin), RowEnd(other.RowEnd),
			ColBegin(other.ColBegin), ColEnd(other.ColEnd)
	{
	}

	ConstSubMatrixRef(const SubMatrixRef<_MatType>& other);

	Int32	row() const
	{
		return RowEnd - RowBegin + 1;
	}

	Int32	col() const
	{
		return ColEnd - ColBegin + 1;
	}

	ConstRowRef<SelfType>	operator [] (Int32 _row) const
	{
		return ConstRowRef<SelfType>(*this, _row);
	}

	NumberType	operator () (Int32 _row, Int32 _col) const
	{
		return Mat(RowBegin + _row - INDEX_OFFSET, ColBegin + _col - INDEX_OFFSET);
	}

	ConstSubMatrixRef<_MatType> operator () (Int32 _RowBegin, Int32 _RowEnd,
											 Int32 _ColBegin, Int32 _ColEnd) const
	{
		return ConstSubMatrixRef<_MatType>(Mat, 
			_RowBegin + RowBegin - INDEX_OFFSET, _RowEnd + RowBegin - INDEX_OFFSET,
			_ColBegin + ColBegin - INDEX_OFFSET, _ColEnd + ColBegin - INDEX_OFFSET);
	}

	template<typename _OtherType>
	Bool	operator == (const _OtherType& other) const
	{
		if (row() != other.row() || col() != other.col())
		{
			return false;
		}

		for (int i = INDEX_OFFSET; i < row() + INDEX_OFFSET; ++i)
		for (int j = INDEX_OFFSET; j < col() + INDEX_OFFSET; ++j)
			if (operator () (i, j) != other(i, j))
			{
				return false;
			}

		return true;
	}

	Bool	operator == (const SelfType& other) const
	{
		if (&other == this)
		{
			return true;
		}

		if (row() != other.row() || col() != other.col())
		{
			return false;
		}

		for (int i = INDEX_OFFSET; i < row() + INDEX_OFFSET; ++i)
		for (int j = INDEX_OFFSET; j < col() + INDEX_OFFSET; ++j)
			if (operator () (i, j) != other(i, j))
			{
				return false;
			}

		return true;
	}

	template<typename _OtherType>
	Bool	operator != (const _OtherType& other) const
	{
		return ! operator == (other);
	}

	Bool	operator != (const SelfType& other) const
	{
		return ! operator == (other);;
	}

private:

	ConstSubMatrixRef operator = (ConstSubMatrixRef&);
	const _MatType& Mat;
	Int32	RowBegin, RowEnd;
	Int32	ColBegin, ColEnd;

};

template<typename _MatType>
class	SubMatrixRef
	:	public	MatrixRef
{
	friend class ConstSubMatrixRef<_MatType>;

public:

	typedef	typename _MatType::NumberType	NumberType;
	typedef	typename NonConstType<SubMatrixRef>::Type	SelfType;

	SubMatrixRef(_MatType& _Mat, Int32 _RowBegin, Int32 _RowEnd,
								 Int32 _ColBegin, Int32 _ColEnd) 
		:	Mat(_Mat),
			RowBegin(_RowBegin), RowEnd(_RowEnd),
			ColBegin(_ColBegin), ColEnd(_ColEnd)
	{
	}

	SubMatrixRef(const SubMatrixRef& other) 
		:	Mat(other.Mat),
			RowBegin(other.RowBegin), RowEnd(other.RowEnd),
			ColBegin(other.ColBegin), ColEnd(other.ColEnd)
	{
	}

	BasicMatrixExpr<SelfType> expr()
	{
		return BasicMatrixExpr<SelfType>(*this);
	}

	Int32	row() const
	{
		return RowEnd - RowBegin + 1;
	}

	Int32	col() const
	{
		return ColEnd - ColBegin + 1;
	}

	ConstRowRef<SelfType>	operator [] (Int32 _row) const
	{
		return ConstRowRef<SelfType>(*this, _row);
	}

	RowRef<SelfType>	operator [] (Int32 _row)
	{
		return RowRef<SelfType>(*this, _row);
	}

	NumberType	operator () (Int32 _row, Int32 _col) const
	{
		return Mat(RowBegin + _row - INDEX_OFFSET, ColBegin + _col - INDEX_OFFSET);
	}

	NumberType&	operator () (Int32 _row, Int32 _col)
	{
		return Mat(RowBegin + _row - INDEX_OFFSET, ColBegin + _col - INDEX_OFFSET);
	}

	ConstSubMatrixRef<_MatType>	operator () (Int32 _RowBegin, Int32 _RowEnd,
											Int32 _ColBegin, Int32 _ColEnd) const
	{
		return ConstSubMatrixRef<_MatType>(Mat, 
			_RowBegin + RowBegin - INDEX_OFFSET, _RowEnd + RowBegin - INDEX_OFFSET,
			_ColBegin  +ColBegin - INDEX_OFFSET, _ColEnd + ColBegin - INDEX_OFFSET);
	}

	SubMatrixRef	operator () (Int32 _RowBegin, Int32 _RowEnd,
							  Int32 _ColBegin, Int32 _ColEnd)
	{
		return SubMatrixRef(Mat, 
			_RowBegin + RowBegin - INDEX_OFFSET, _RowEnd + RowBegin - INDEX_OFFSET,
			_ColBegin + ColBegin - INDEX_OFFSET, _ColEnd + ColBegin - INDEX_OFFSET);
	}

	template<typename _OtherType>
	SubMatrixRef	operator = (const _OtherType& other)
	{
		RT_CONDITION(row() == other.row() && col() == other.col());
		for (int i = INDEX_OFFSET; i < row() + INDEX_OFFSET; ++i)
		for (int j = INDEX_OFFSET; j < col() + INDEX_OFFSET; ++j)
			operator () (i, j) = other(i, j);

		return *this;
	}

	SubMatrixRef	operator = (const SubMatrixRef& other)
	{
		RT_CONDITION(row() == other.row() && col() == other.col());
		for (int i = INDEX_OFFSET; i < row() + INDEX_OFFSET; ++i)
		for (int j = INDEX_OFFSET; j < col() + INDEX_OFFSET; ++j)
			operator () (i, j) = other(i, j);

		return *this;
	}

	template<typename _OtherType>
	Bool	operator == (const _OtherType& other) const
	{
		if (row() != other.row() || col() != other.col())
		{
			return false;
		}

		for (int i = INDEX_OFFSET; i < row() + INDEX_OFFSET; ++i)
		for (int j = INDEX_OFFSET; j < col() + INDEX_OFFSET; ++j)
			if (operator () (i, j) != other(i, j))
			{
				return false;
			}

		return true;
	}

	Bool	operator == (const SelfType& other) const
	{
		if (&other == this)
		{
			return true;
		}

		if (row() != other.row() || col() != other.col())
		{
			return false;
		}

		for (int i = INDEX_OFFSET; i < row() + INDEX_OFFSET; ++i)
		for (int j = INDEX_OFFSET; j < col() + INDEX_OFFSET; ++j)
			if (operator () (i, j) != other(i, j))
			{
				return false;
			}

		return true;
	}

	template<typename _OtherType>
	Bool	operator != (const _OtherType& other) const
	{
		return ! operator == (other);
	}

	Bool	operator != (const SelfType& other) const
	{
		return ! operator == (other);;
	}

private:

	_MatType& Mat;
	Int32	RowBegin, RowEnd;
	Int32	ColBegin, ColEnd;

};

template<typename _MatType>
ConstSubMatrixRef<_MatType>::ConstSubMatrixRef(const SubMatrixRef<_MatType>& other) 
	:	Mat(other.Mat),
		RowBegin(other.RowBegin), RowEnd(other.RowEnd),
		ColBegin(other.ColBegin), ColEnd(other.ColEnd)
	{
	}

#ifdef		MATH_INDEX_STYLE

	#define		FULL_CREF(x, t) ConstSubMatrixRef<t>(x, 1, x.row(), 1, x.col())
	#define		FULL_REF(x, t) SubMatrixRef<t>(x, 1, x.row(), 1, x.col())

#else

	#define		FULL_CREF(x, t) ConstSubMatrixRef<t>(x, 0, x.row()-1, 0, x.col()-1)
	#define		FULL_REF(x, t) SubMatrixRef<t>(x, 0, x.row()-1, 0, x.col()-1)

#endif

SDL_MATHS_MATRIX_END

#endif

⌨️ 快捷键说明

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