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

📄 matrixope.hpp

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

#ifndef		_SDL_MATHS_MATRIX_MATRIX_HPP_
#error		"must be included by Matrix.hpp"
#endif

SDL_MATHS_MATRIX_BEGIN

struct	MatrixOpe{};

/*
*
*	operator <<
*
*/

template<
		typename _NumberType,
		typename _MatrixPolicy
		>
inline
OutStream&	operator << (OutStream& o, const BasicMatrix<_NumberType, _MatrixPolicy>& mat)
{
	for (int i = INDEX_OFFSET; i < mat.row() + INDEX_OFFSET;o << std::endl, ++i)
		for (int j = INDEX_OFFSET; j < mat.col() + INDEX_OFFSET; ++j)
			o << mat(i, j) << ' ';
	o << std::endl;
	return o;
}

template<typename _MatType>
inline
OutStream&	operator << (OutStream& o, const ConstSubMatrixRef<_MatType>& mat)
{
	for (int i = INDEX_OFFSET; i < mat.row() + INDEX_OFFSET;o << std::endl, ++i)
		for (int j = INDEX_OFFSET; j < mat.col() + INDEX_OFFSET; ++j)
			o << mat(i, j) << ' ';
	o << std::endl;
	return o;
}

template<typename _MatType>
inline
OutStream&	operator << (OutStream& o, const SubMatrixRef<_MatType>& mat)
{
	for (int i = INDEX_OFFSET; i < mat.row() + INDEX_OFFSET;o << std::endl, ++i)
		for (int j = INDEX_OFFSET; j < mat.col() + INDEX_OFFSET; ++j)
			o << mat(i, j) << ' ';
	o << std::endl;
	return o;
}

template<typename _MatType>
inline
OutStream&	operator << (OutStream& o, const BasicMatrixExpr<_MatType>& mat)
{
	for (int i = INDEX_OFFSET; i < mat.row() + INDEX_OFFSET;o << std::endl, ++i)
		for (int j = INDEX_OFFSET; j < mat.col() + INDEX_OFFSET; ++j)
			o << mat(i, j) << ' ';
	o << std::endl;
	return o;
}

/*
*
*	Treat	1 * 1 matrix as scalar
*	If the result of a matrix mutiple a matrix is a 1 * 1 matrix, use it
*
*/

template<
		typename _MatType,
		Bool = true
		>
struct	ScalarHelper
	:	public	MatrixOpe
{
	typedef	typename _MatType::NumberType	NumberType;

	ScalarHelper(const _MatType& _Mat)
	{
		RT_CONDITION(_Mat.row() == 1 && _Mat.col() == 1);

		Value = _Mat(INDEX_OFFSET, INDEX_OFFSET);
	}
	NumberType	Value;
};

template<typename _MatType>
struct	ScalarHelper<_MatType, false>
	:	public	MatrixOpe
{
	typedef	_MatType	NumberType;

	ScalarHelper(const _MatType& _Value) :
		Value(_Value)
	{
	}
	NumberType	Value;
};

template<typename _MatType>
inline
typename
ScalarHelper<
		_MatType,
		IsMatConcept<_MatType>::Result
		>
		::NumberType
Scalar(const _MatType& _Mat)
{
	return ScalarHelper<_MatType, IsMatConcept<_MatType>::Result>(_Mat).Value;
}

/*
*
*	operator +
*	Not Evalueate the expression immediately.
*
*/

template<
		typename _LMatType,
		typename _RMatType,
		Bool = true,
		Bool = true
		>
struct	AddHelper
	:	public	MatrixOpe
{
		typedef	MatAddMatrixExpr<_LMatType, _RMatType>	ContainedMatType;
		typedef	BasicMatrixExpr<ContainedMatType >	ResultType;

		static ResultType	make(const _LMatType& _l, const _RMatType& _r)
		{
				return ResultType(ContainedMatType(_l, _r));
		}
};

template<
		typename _LMatType,
		typename _RMatType
		>
struct	AddHelper<_LMatType, _RMatType, true, false>
	:	public	MatrixOpe
{
		typedef	ScalarAddMatrixExpr<_LMatType, _RMatType>	ContainedMatType;
		typedef	BasicMatrixExpr<ContainedMatType >		ResultType;
		
		static ResultType	make(const _LMatType& _l, _RMatType _r)
		{
				return ResultType(ContainedMatType(_l, _r));
		}
};

template<
		typename _LMatType,
		typename _RMatType
		>
struct	AddHelper<_LMatType, _RMatType, false, true>
	:	public	MatrixOpe
{
		typedef	ScalarAddMatrixExpr<_RMatType, _LMatType>	ContainedMatType;
		typedef	BasicMatrixExpr<ContainedMatType>	ResultType;

		static ResultType	make(_LMatType _l, const _RMatType& _r)
		{
				return ResultType(ContainedMatType(_r, _l));
		}
};

template<
		typename _LMatType,
		typename _RMatType
		>
inline
typename
AddHelper<
		_LMatType, _RMatType, 
		IsMatConcept<_LMatType>::Result,
		IsMatConcept<_RMatType>::Result
		>
		::ResultType
operator + (const _LMatType& _l, const _RMatType& _r)
{
	return AddHelper<_LMatType, _RMatType,
		IsMatConcept<_LMatType>::Result,
		IsMatConcept<_RMatType>::Result >
		::make(_l, _r);
}

/*
*
*	operator -
*	Not Evalueate the expression immediately.
*
*/

template<
		typename _LMatType,
		typename _RMatType,
		Bool = true,
		Bool = true
		>
struct	SubHelper
	:	public	MatrixOpe
{
		typedef	MatSubMatrixExpr<_LMatType, _RMatType>	ContainedMatType;
		typedef	BasicMatrixExpr<ContainedMatType >	ResultType;

		static ResultType	make(const _LMatType& _l, const _RMatType& _r)
		{
				return ResultType(ContainedMatType(_l, _r));
		}
};

template<
		typename _LMatType,
		typename _RMatType
		>
struct	SubHelper<_LMatType, _RMatType, true, false>
	:	public	MatrixOpe
{
		typedef	ScalarSubMatrixExprR<_LMatType, _RMatType>	ContainedMatType;
		typedef	BasicMatrixExpr<ContainedMatType >	ResultType;

		static ResultType	make(const _LMatType& _l, _RMatType _r)
		{
				return ResultType(ContainedMatType(_l, _r));
		}
};

template<
		typename _LMatType,
		typename _RMatType
		>
struct	SubHelper<_LMatType, _RMatType, false, true>
	:	public	MatrixOpe
{
		typedef	ScalarSubMatrixExprL<_LMatType, _RMatType>	ContainedMatType;
		typedef	BasicMatrixExpr<ContainedMatType>	ResultType;

		static ResultType	make(_LMatType _l, const _RMatType& _r)
		{
				return ResultType(ContainedMatType(_l, _r));
		}
};

template<
		typename _LMatType,
		typename _RMatType
		>
inline
typename
SubHelper<
		_LMatType, _RMatType, 
		IsMatConcept<_LMatType>::Result,
		IsMatConcept<_RMatType>::Result
		>
		::ResultType
operator - (const _LMatType& _l, const _RMatType& _r)
{
	return SubHelper<_LMatType, _RMatType,
		IsMatConcept<_LMatType>::Result,
		IsMatConcept<_RMatType>::Result >
		::make(_l, _r);
}

template<typename _MatType>
inline
BasicMatrixExpr<NegMatrixExpr<_MatType> >
operator - (const _MatType& _Mat)
{
	typedef	NegMatrixExpr<_MatType> ContainedMatType;
	return BasicMatrixExpr<ContainedMatType>(ContainedMatType(_Mat));
}

/*
*
*	operator *
*	Not Evalueate the expression immediately.
*	Assign	dependence.
*
*/

template<
		typename _LMatType,
		typename _RMatType,
		Bool = true,
		Bool = true
		>
struct	MulHelper
	:	public	MatrixOpe
{
		typedef	MatMulMatrixExpr<_LMatType, _RMatType>	ContainedMatType;
		typedef	BasicMatrixExpr<ContainedMatType >		ResultType;

		static ResultType	make(const _LMatType& _l, const _RMatType& _r)
		{
				return ResultType(ContainedMatType(_l, _r));
		}
};

template<
		typename _LMatType,
		typename _RMatType
		>
struct	MulHelper<_LMatType, _RMatType, true, false>
	:	public	MatrixOpe
{
		typedef	ScalarMulMatrixExpr<_LMatType, _RMatType>	ContainedMatType;
		typedef	BasicMatrixExpr<ContainedMatType >			ResultType;

		static ResultType	make(const _LMatType& _l, _RMatType _r)
		{
				return ResultType(ContainedMatType(_l, _r));
		}
};

template<
		typename _LMatType,
		typename _RMatType
		>
struct	MulHelper<_LMatType, _RMatType, false, true>
	:	public	MatrixOpe
{
		typedef	ScalarMulMatrixExpr<_RMatType, _LMatType>	ContainedMatType;
		typedef	BasicMatrixExpr<ContainedMatType>			ResultType;

		static ResultType	make(_LMatType _l, const _RMatType& _r)
		{
				return ResultType(ContainedMatType(_r, _l));
		}
};

template<
		typename _LMatType,
		typename _RMatType
		>
inline
typename
MulHelper<
		_LMatType, _RMatType, 
		IsMatConcept<_LMatType>::Result,
		IsMatConcept<_RMatType>::Result
		>
		::ResultType
operator * (const _LMatType& _l, const _RMatType& _r)
{
	return MulHelper<_LMatType, _RMatType,
		IsMatConcept<_LMatType>::Result,
		IsMatConcept<_RMatType>::Result >
		::make(_l, _r);
}

/*
*
*	operator /
*	Evalueate the expression immediately expect a matrix divide a scalar
*
*/

template<
		typename _LMatType,
		typename _RMatType,
		Bool = true,
		Bool = true
		>
struct	DivHelper
	:	public	MatrixOpe
{
		typedef	MatDivMatrixExpr<_LMatType, _RMatType>	ContainedMatType;
		typedef	BasicMatrixExpr<ContainedMatType >		ResultType;

		static ResultType	make(const _LMatType& _l, const _RMatType& _r)
		{
				return ResultType(ContainedMatType(_l, _r));
		}
};

template<
		typename _LMatType,
		typename _RMatType
		>
struct	DivHelper<_LMatType, _RMatType, true, false>
	:	public	MatrixOpe
{
		typedef	ScalarDivMatrixExprR<_LMatType, _RMatType>	ContainedMatType;
		typedef	BasicMatrixExpr<ContainedMatType >			ResultType;

		static ResultType	make(const _LMatType& _l, _RMatType _r)
		{
				return ResultType(ContainedMatType(_l, _r));
		}
};

template<
		typename _LMatType,
		typename _RMatType
		>
struct	DivHelper<_LMatType, _RMatType, false, true>
	:	public	MatrixOpe
{
		typedef	ScalarDivMatrixExprL<_LMatType, _RMatType>	ContainedMatType;
		typedef	BasicMatrixExpr<ContainedMatType>			ResultType;

		static ResultType	make(_LMatType _l, const _RMatType& _r)
		{
				return ResultType(ContainedMatType(_l, _r));
		}
};

template<
		typename _LMatType,
		typename _RMatType
		>
inline
typename
DivHelper<
		_LMatType, _RMatType, 
		IsMatConcept<_LMatType>::Result,
		IsMatConcept<_RMatType>::Result
		>
		::ResultType
operator / (const _LMatType& _l, const _RMatType& _r)
{
	return DivHelper<_LMatType, _RMatType,
		IsMatConcept<_LMatType>::Result,
		IsMatConcept<_RMatType>::Result >
		::make(_l, _r);
}

/*
*
*	operator ^
*
*/

template<typename _MatType>
inline
BasicMatrixExpr<PowMatrixExpr<_MatType> >
operator ^ (const _MatType& _l, Int32 n)
{
	typedef	PowMatrixExpr<_MatType> ContainedMatType;

	return BasicMatrixExpr<ContainedMatType>(ContainedMatType(_l, n));
}

SDL_MATHS_MATRIX_END

#endif

⌨️ 快捷键说明

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