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

📄 matrixpolicy.hpp

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

SDL_MATHS_MATRIX_BEGIN

class	MatrixPolicy{};

template<typename _NumberType>
struct	FullMatrixPolicy
	:	public	MatrixPolicy
{
	typedef	_NumberType							NumberType;
	typedef	_NumberType							MatrixDataType;
	typedef	MatrixData<MatrixDataType>			MatrixData;
	typedef	FullMatrixPolicy					SelfType;
	typedef	FullMatrixStoredElementIterator<
				MatrixDataType, NumberType>			StoredElementIterator;
	typedef	ConstFullMatrixStoredElementIterator<
				MatrixDataType, NumberType>			ConstStoredElementIterator;

	static	Int32	AllocateSize(Int32 row, Int32 col)
	{
		return row * col;
	}

	FullMatrixPolicy(Int32 _Row, Int32 _Col)
		:	Row(_Row), Col(_Col)
	{
		RT_CONDITION(Row > 0 && Col > 0);
	}

	Int32	index(Int32 _row, Int32 _col) const
	{
		return _row * Col + _col;
	}

	Bool	good(Int32 _row, Int32 _col) const
	{
		return true;
	}

	NumberType	at(MatrixDataType * _pData, Int32 _row, Int32 _col) const
	{
		INDEX_ADJUST(_row);
		INDEX_ADJUST(_col);

#ifdef	INDEX_CHECK
		RT_CONDITION(_row >= 0 && _row < Row && _col >= 0 && _col < Col);
#endif

		return _pData[index(_row, _col)];
	}

	NumberType&	at(MatrixDataType * _pData, Int32 _row, Int32 _col)
	{
		INDEX_ADJUST(_row);
		INDEX_ADJUST(_col);

#ifdef	INDEX_CHECK
		RT_CONDITION(_row >= 0 && _row < Row && _col >= 0 && _col < Col);
#endif

		return _pData[index(_row, _col)];
	}

	void	Swap(SelfType& other)
	{
		std::swap(Row, other.Row);
		std::swap(Col, other.Col);
	}

	Int32	SecondPara() const
	{
		return	Col;
	}

protected:

	Int32	Row;
	Int32	Col;

};

template<typename _NumberType>
struct	DiagMatrixPolicy
	:	public	MatrixPolicy
{
	typedef	_NumberType							NumberType;
	typedef	_NumberType							MatrixDataType;
	typedef	MatrixData<MatrixDataType>			MatrixData;
	typedef	DiagMatrixPolicy					SelfType;
	typedef	DiagMatrixStoredElementIterator<
				MatrixDataType, NumberType>			StoredElementIterator;
	typedef	ConstDiagMatrixStoredElementIterator<
				MatrixDataType, NumberType>			ConstStoredElementIterator;

	static		NumberType							ZeroData;

	static	Int32	AllocateSize(Int32 row, Int32)
	{
		return row;
	}

	DiagMatrixPolicy(Int32 _Row, Int32)
		:	Row(_Row), Col(_Row)
	{
		RT_CONDITION(Row > 0);
	}

	Int32	index(Int32 _row, Int32) const
	{
		return	_row;
	}

	Bool	good(Int32 _row, Int32 _col) const
	{
		return _row == _col;
	}

	NumberType	at(MatrixDataType * _pData, Int32 _row, Int32 _col) const
	{
		INDEX_ADJUST(_row);
		INDEX_ADJUST(_col);

#ifdef	INDEX_CHECK
		RT_CONDITION(_row >= 0 && _row < Row && _col >= 0 && _col < Col);
#endif

		if (good(_row, _col))
		{
			return _pData[index(_row, _col)];
		}
		else
		{
			return 0;
		}
	}

	NumberType&	at(MatrixDataType * _pData, Int32 _row, Int32 _col)
	{
		INDEX_ADJUST(_row);
		INDEX_ADJUST(_col);

#ifdef	INDEX_CHECK
		RT_CONDITION(_row >= 0 && _row < Row && _col >= 0 && _col < Col);
#endif

		if (good(_row, _col))
		{
			return _pData[index(_row, _col)];
		}
		else
		{
			return ZeroData;
		}
	}

	void	Swap(SelfType& other)
	{
		std::swap(Row, other.Row);
		std::swap(Col, other.Col);
	}

	Int32	SecondPara() const
	{
		return	Col;
	}

protected:

	Int32	Row;
	Int32	Col;

};

template<typename _NumberType>
struct	KDiagMatrixPolicy
	:	public	MatrixPolicy
{
	typedef	_NumberType							NumberType;
	typedef	_NumberType							MatrixDataType;
	typedef	MatrixData<MatrixDataType>			MatrixData;
	typedef	KDiagMatrixPolicy					SelfType;
	typedef	KDiagMatrixStoredElementIterator<
				MatrixDataType, NumberType>			StoredElementIterator;
	typedef	ConstKDiagMatrixStoredElementIterator<
				MatrixDataType, NumberType>			ConstStoredElementIterator;

	static		NumberType							ZeroData;

	static	Int32	AllocateSize(Int32 row, Int32 k)
	{
		return row * k;
	}

	KDiagMatrixPolicy(Int32 _Row, Int32 _K)
		:	Row(_Row), Col(_Row),
			K(_K), HalfWidth((_K + 1) >> 1)
	{
		RT_CONDITION(Row > 0 && K > 1);
	}

	Int32	index(Int32 _row, Int32 _col) const
	{
		return	_row < HalfWidth ? _row * K + _col : _row * K + _col - (_row - HalfWidth);
	}

	Bool	good(Int32 _row, Int32 _col) const
	{
		return abs(_row-_col) < HalfWidth;
	}

	NumberType	at(MatrixDataType * _pData, Int32 _row, Int32 _col) const
	{
		INDEX_ADJUST(_row);
		INDEX_ADJUST(_col);

#ifdef	INDEX_CHECK
		RT_CONDITION(_row >= 0 && _row < Row && _col >= 0 && _col < Col);
#endif

		if (good(_row, _col))
		{
			return _pData[index(_row, _col)];
		}
		else
		{
			return 0;
		}
	}

	NumberType&	at(MatrixDataType * _pData, Int32 _row, Int32 _col)
	{
		INDEX_ADJUST(_row);
		INDEX_ADJUST(_col);

#ifdef	INDEX_CHECK
		RT_CONDITION(_row >= 0 && _row < Row && _col >= 0 && _col < Col);
#endif

		if (good(_row, _col))
		{
			return _pData[index(_row, _col)];
		}
		else
		{
			return ZeroData;
		}
	}

	void	Swap(SelfType& other)
	{
		std::swap(Row, other.Row);
		std::swap(Col, other.Col);
		std::swap(K, other.K);
		std::swap(HalfWidth, other.HalfWidth);
	}

	Int32	SecondPara() const
	{
		return	K;
	}

protected:

	Int32	Row;
	Int32	Col;
	Int32	K;
	Int32	HalfWidth;
	
};

template<typename _NumberType>
struct	UpperMatrixPolicy
	:	public	MatrixPolicy
{
	typedef	_NumberType							NumberType;
	typedef	_NumberType							MatrixDataType;
	typedef	MatrixData<MatrixDataType>			MatrixData;
	typedef	UpperMatrixPolicy					SelfType;
	typedef	UpperMatrixStoredElementIterator<
				MatrixDataType, NumberType>			StoredElementIterator;
	typedef	ConstUpperMatrixStoredElementIterator<
				MatrixDataType, NumberType>			ConstStoredElementIterator;

	static		NumberType							ZeroData;
	
	static	Int32	AllocateSize(Int32 row, Int32)
	{
		return row * (row + 1) / 2;
	}

	UpperMatrixPolicy(Int32 _Row, Int32)
		:	Row(_Row), Col(_Row)
	{
		RT_CONDITION(Row > 0);
	}

	Int32	index(Int32 _row, Int32 _col) const
	{
		return	(Row + Row - _row - 1) * _row / 2 + _col;
	}

	Bool	good(Int32 _row, Int32 _col) const
	{
		return _row <= _col;
	}

	NumberType	at(MatrixDataType * _pData, Int32 _row, Int32 _col) const
	{
		INDEX_ADJUST(_row);
		INDEX_ADJUST(_col);

#ifdef	INDEX_CHECK
		RT_CONDITION(_row >= 0 && _row < Row && _col >= 0 && _col < Col);
#endif

		if (good(_row, _col))
		{
			return _pData[index(_row, _col)];
		}
		else
		{
			return 0;
		}
	}

	NumberType&	at(MatrixDataType * _pData, Int32 _row, Int32 _col)
	{
		INDEX_ADJUST(_row);
		INDEX_ADJUST(_col);

#ifdef	INDEX_CHECK
		RT_CONDITION(_row >= 0 && _row < Row && _col >= 0 && _col < Col);
#endif

		if (good(_row, _col))
		{
			return _pData[index(_row, _col)];
		}
		else
		{
			return ZeroData;
		}
	}

	void	Swap(SelfType& other)
	{
		std::swap(Row, other.Row);
		std::swap(Col, other.Col);
	}

	Int32	SecondPara() const
	{
		return	Col;
	}

protected:

	Int32	Row;
	Int32	Col;

};

template<typename _NumberType>
struct	LowerMatrixPolicy
	:	public	MatrixPolicy
{
	typedef	_NumberType							NumberType;
	typedef	_NumberType							MatrixDataType;
	typedef	MatrixData<MatrixDataType>			MatrixData;
	typedef	LowerMatrixPolicy					SelfType;
	typedef	LowerMatrixStoredElementIterator<
				MatrixDataType, NumberType>			StoredElementIterator;
	typedef	ConstLowerMatrixStoredElementIterator<
				MatrixDataType, NumberType>			ConstStoredElementIterator;

	static		NumberType							ZeroData;

	static	Int32	AllocateSize(Int32 row, Int32)
	{
		return row * (row + 1) / 2;
	}

	LowerMatrixPolicy(Int32 _Row, Int32)
		:	Row(_Row), Col(_Row)
	{
		RT_CONDITION(Row > 0);
	}

	Int32	index(Int32 _row, Int32 _col) const
	{
		return	(1 + _row) * _row / 2 + _col;
	}

	Bool	good(Int32 _row, Int32 _col) const
	{
		return (_row >= _col);
	}

	NumberType	at(MatrixDataType * _pData, Int32 _row, Int32 _col) const
	{
		INDEX_ADJUST(_row);
		INDEX_ADJUST(_col);

#ifdef	INDEX_CHECK
		RT_CONDITION(_row >= 0 && _row < Row && _col >= 0 && _col < Col);
#endif

		if (good(_row, _col))
		{
			return _pData[index(_row, _col)];
		}
		else
		{
			return 0;
		}
	}

	NumberType&	at(MatrixDataType * _pData, Int32 _row, Int32 _col)
	{
		INDEX_ADJUST(_row);
		INDEX_ADJUST(_col);

#ifdef	INDEX_CHECK
		RT_CONDITION(_row >= 0 && _row < Row && _col >= 0 && _col < Col);
#endif

		if (good(_row, _col))
		{
			return _pData[index(_row, _col)];
		}
		else
		{
			return ZeroData;
		}
	}

	void	Swap(SelfType& other)
	{
		std::swap(Row, other.Row);
		std::swap(Col, other.Col);
	}

	Int32	SecondPara() const
	{
		return	Col;
	}

protected:

	Int32	Row;
	Int32	Col;

};

template<typename _NumberType>
struct	SymMatrixPolicy
	:	public	MatrixPolicy
{
	typedef	_NumberType							NumberType;
	typedef	_NumberType							MatrixDataType;
	typedef	MatrixData<MatrixDataType>			MatrixData;
	typedef	SymMatrixPolicy						SelfType;
	typedef	SymMatrixStoredElementIterator<
				MatrixDataType, NumberType>			StoredElementIterator;
	typedef	ConstSymMatrixStoredElementIterator<
				MatrixDataType, NumberType>			ConstStoredElementIterator;

	static		NumberType							ZeroData;
	
	static	Int32	AllocateSize(Int32 row, Int32)
	{
		return row * (row + 1) / 2;
	}

	SymMatrixPolicy(Int32 _Row, Int32)
		:	Row(_Row), Col(_Row)
	{
		RT_CONDITION(Row > 0);
	}

	Int32	index(Int32 _row, Int32 _col) const
	{
		
		return	_row >= _col ? (1 + _row) * _row / 2 + _col : (1 + _col) * _col / 2 + _row;
	}

	Bool	good(Int32 _row, Int32 _col) const
	{
		return true;
	}

	NumberType	at(MatrixDataType * _pData, Int32 _row, Int32 _col) const
	{
		INDEX_ADJUST(_row);
		INDEX_ADJUST(_col);

#ifdef	INDEX_CHECK
		RT_CONDITION(_row >= 0 && _row < Row && _col >= 0 && _col < Col);
#endif

		if (good(_row, _col))
		{
			return _pData[index(_row, _col)];
		}
		else
		{
			return 0;
		}
	}

	NumberType&	at(MatrixDataType * _pData, Int32 _row, Int32 _col)
	{
		INDEX_ADJUST(_row);
		INDEX_ADJUST(_col);

#ifdef	INDEX_CHECK
		RT_CONDITION(_row >= 0 && _row < Row && _col >= 0 && _col < Col);
#endif

		if (good(_row, _col))
		{
			return _pData[index(_row, _col)];
		}
		else
		{
			return ZeroData;
		}
	}

	void	Swap(SelfType& other)
	{
		std::swap(Row, other.Row);
		std::swap(Col, other.Col);
	}

	Int32	SecondPara() const
	{
		return	Col;
	}

protected:

	Int32	Row;
	Int32	Col;

};

template<typename _NumberType>
struct	SparseMatrixPolicy
	:	public	MatrixPolicy
{
	typedef	_NumberType							NumberType;
	typedef	std::map<Int32, _NumberType>			MatrixDataType;
	typedef	MatrixData<MatrixDataType>			MatrixData;
	typedef	SparseMatrixPolicy					SelfType;
	typedef	SparseMatrixStoredElementIterator<
				MatrixDataType, NumberType>			StoredElementIterator;
	typedef	ConstSparseMatrixStoredElementIterator<
				MatrixDataType, NumberType>			ConstStoredElementIterator;

	static	NumberType							ZeroData;

	static	Int32	AllocateSize(Int32, Int32)
	{
		return 1;
	}

	SparseMatrixPolicy(Int32 _Row, Int32 _Col)
		:	Row(_Row), Col(_Col)
	{
		RT_CONDITION(Row >= 0 && Col >= 0);
	}

	Int32	index(Int32 row, Int32 col) const
	{
		return	(row<<16)+col;
	}

	Bool	good(Int32 row, Int32 col) const
	{
		return true;
	}

	NumberType	at(MatrixDataType * _pData, Int32 _row, Int32 _col) const
	{
		INDEX_ADJUST(_row);
		INDEX_ADJUST(_col);

#ifdef	INDEX_CHECK
		RT_CONDITION(_row >= 0 && _row < Row && _col >= 0 && _col < Col);
#endif

		typename MatrixDataType::iterator pos = _pData->find(index(_row, _col));
		if (pos == _pData->end())
		{
			return 0;
		}
		else
		{
			return pos->second;
		}
	}

	NumberType&	at(MatrixDataType * _pData, Int32 _row, Int32 _col)
	{
		INDEX_ADJUST(_row);
		INDEX_ADJUST(_col);

#ifdef	INDEX_CHECK
		RT_CONDITION(_row >= 0 && _row < Row && _col >= 0 && _col < Col);
#endif

		return (*_pData)[index(_row, _col)];
	}

	void	Swap(SelfType& other)
	{
		std::swap(Row, other.Row);
		std::swap(Col, other.Col);
	}

	Int32	SecondPara() const
	{
		return	Col;
	}

protected:

	Int32	Row;
	Int32	Col;

};

template<typename _NumberType>
typename DiagMatrixPolicy<_NumberType>::NumberType 
DiagMatrixPolicy<_NumberType>:: ZeroData = 0;

template<typename _NumberType>
typename KDiagMatrixPolicy<_NumberType>::NumberType 
KDiagMatrixPolicy<_NumberType>:: ZeroData = 0;

template<typename _NumberType>
typename UpperMatrixPolicy< _NumberType>::NumberType 
UpperMatrixPolicy<_NumberType>:: ZeroData = 0;

template<typename _NumberType>
typename LowerMatrixPolicy<_NumberType>::NumberType 
LowerMatrixPolicy<_NumberType>:: ZeroData = 0;

template<typename _NumberType>
typename SymMatrixPolicy<_NumberType>::NumberType 
SymMatrixPolicy<_NumberType>:: ZeroData = 0;

template<typename _NumberType>
typename SparseMatrixPolicy<_NumberType>::NumberType 
SparseMatrixPolicy<_NumberType>:: ZeroData = 0;

SDL_MATHS_MATRIX_END

#endif

⌨️ 快捷键说明

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