📄 matrixfunctions.h
字号:
/* * Tiny Vector Matrix Library * Dense Vector Matrix Libary of Tiny size using Expression Templates * * Copyright (C) 2001 - 2007 Olaf Petzold <opetzold@users.sourceforge.net> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * lesser General Public License for more details. * * You should have received a copy of the GNU lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: MatrixFunctions.h,v 1.44 2007-06-23 15:59:00 opetzold Exp $ */#ifndef TVMET_XPR_MATRIX_FUNCTIONS_H#define TVMET_XPR_MATRIX_FUNCTIONS_Hnamespace tvmet {/* forwards */template<class T, std::size_t Rows, std::size_t Cols> class Matrix;template<class T, std::size_t Sz> class Vector;template<class E, std::size_t Sz> class XprVector;template<class E> class XprMatrixTranspose;template<class E, std::size_t Sz> class XprMatrixDiag;template<class E, std::size_t Rows, std::size_t Cols> class XprMatrixRow;template<class E, std::size_t Rows, std::size_t Cols> class XprMatrixCol;/********************************************************* * PART I: DECLARATION *********************************************************//*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Matrix arithmetic functions add, sub, mul and div *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*//* * function(XprMatrix<E1, Rows, Cols>, XprMatrix<E2, Rows, Cols>) */#define TVMET_DECLARE_MACRO(NAME) \template<class E1, class E2, std::size_t Rows, std::size_t Cols> \XprMatrix< \ XprBinOp< \ Fcnl_##NAME<typename E1::value_type, typename E2::value_type>, \ XprMatrix<E1, Rows, Cols>, \ XprMatrix<E2, Rows, Cols> \ >, \ Rows, Cols \> \NAME (const XprMatrix<E1, Rows, Cols>& lhs, \ const XprMatrix<E2, Rows, Cols>& rhs) TVMET_CXX_ALWAYS_INLINE;TVMET_DECLARE_MACRO(add) // per se element wiseTVMET_DECLARE_MACRO(sub) // per se element wisenamespace element_wise { TVMET_DECLARE_MACRO(mul) // not defined for matrizes TVMET_DECLARE_MACRO(div) // not defined for matrizes}#undef TVMET_DECLARE_MACRO/* * function(XprMatrix<E, Rows, Cols>, POD) * function(POD, XprMatrix<E, Rows, Cols>) * Note: - operations +,-,*,/ are per se element wise */#define TVMET_DECLARE_MACRO(NAME, POD) \template<class E, std::size_t Rows, std::size_t Cols> \XprMatrix< \ XprBinOp< \ Fcnl_##NAME<typename E::value_type, POD >, \ XprMatrix<E, Rows, Cols>, \ XprLiteral< POD > \ >, \ Rows, Cols \> \NAME (const XprMatrix<E, Rows, Cols>& lhs, \ POD rhs) TVMET_CXX_ALWAYS_INLINE; \ \template<class E, std::size_t Rows, std::size_t Cols> \XprMatrix< \ XprBinOp< \ Fcnl_##NAME< POD, typename E::value_type>, \ XprLiteral< POD >, \ XprMatrix<E, Rows, Cols> \ >, \ Rows, Cols \> \NAME (POD lhs, \ const XprMatrix<E, Rows, Cols>& rhs) TVMET_CXX_ALWAYS_INLINE;TVMET_DECLARE_MACRO(add, int)TVMET_DECLARE_MACRO(sub, int)TVMET_DECLARE_MACRO(mul, int)TVMET_DECLARE_MACRO(div, int)#if defined(TVMET_HAVE_LONG_LONG)TVMET_DECLARE_MACRO(add, long long int)TVMET_DECLARE_MACRO(sub, long long int)TVMET_DECLARE_MACRO(mul, long long int)TVMET_DECLARE_MACRO(div, long long int)#endifTVMET_DECLARE_MACRO(add, float)TVMET_DECLARE_MACRO(sub, float)TVMET_DECLARE_MACRO(mul, float)TVMET_DECLARE_MACRO(div, float)TVMET_DECLARE_MACRO(add, double)TVMET_DECLARE_MACRO(sub, double)TVMET_DECLARE_MACRO(mul, double)TVMET_DECLARE_MACRO(div, double)#if defined(TVMET_HAVE_LONG_DOUBLE)TVMET_DECLARE_MACRO(add, long double)TVMET_DECLARE_MACRO(sub, long double)TVMET_DECLARE_MACRO(mul, long double)TVMET_DECLARE_MACRO(div, long double)#endif#undef TVMET_DECLARE_MACRO#if defined(TVMET_HAVE_COMPLEX)/* * function(XprMatrix<E, Rows, Cols>, complex<T>) * function(complex<T>, XprMatrix<E, Rows, Cols>) * Note: - operations +,-,*,/ are per se element wise * \todo type promotion */#define TVMET_DECLARE_MACRO(NAME) \template<class E, class T, std::size_t Rows, std::size_t Cols> \XprMatrix< \ XprBinOp< \ Fcnl_##NAME<typename E::value_type, std::complex<T> >, \ XprMatrix<E, Rows, Cols>, \ XprLiteral< std::complex<T> > \ >, \ Rows, Cols \> \NAME (const XprMatrix<E, Rows, Cols>& lhs, \ const std::complex<T>& rhs) TVMET_CXX_ALWAYS_INLINE; \ \template<class T, class E, std::size_t Rows, std::size_t Cols> \XprMatrix< \ XprBinOp< \ Fcnl_##NAME< std::complex<T>, typename E::value_type>, \ XprLiteral< std::complex<T> >, \ XprMatrix<E, Rows, Cols> \ >, \ Rows, Cols \> \NAME (const std::complex<T>& lhs, \ const XprMatrix<E, Rows, Cols>& rhs) TVMET_CXX_ALWAYS_INLINE;TVMET_DECLARE_MACRO(add)TVMET_DECLARE_MACRO(sub)TVMET_DECLARE_MACRO(mul)TVMET_DECLARE_MACRO(div)#undef TVMET_DECLARE_MACRO#endif // defined(TVMET_HAVE_COMPLEX)/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * matrix prod( ... ) functions *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/template<class E1, std::size_t Rows1, std::size_t Cols1, class E2, std::size_t Cols2>XprMatrix< XprMMProduct< XprMatrix<E1, Rows1, Cols1>, Rows1, Cols1, // M1(Rows1, Cols1) XprMatrix<E2, Cols1, Cols2>, Cols2 >, Rows1, Cols2 // return Dim>prod(const XprMatrix<E1, Rows1, Cols1>& lhs, const XprMatrix<E2, Cols1, Cols2>& rhs) TVMET_CXX_ALWAYS_INLINE;template<class E1, std::size_t Rows1, std::size_t Cols1, class E2, std::size_t Cols2>XprMatrix< XprMMProductTransposed< XprMatrix<E1, Rows1, Cols1>, Rows1, Cols1, // M1(Rows1, Cols1) XprMatrix<E2, Cols1, Cols2>, Cols2 // M2(Cols1, Cols2) >, Cols2, Rows1 // return Dim>trans_prod(const XprMatrix<E1, Rows1, Cols1>& lhs, const XprMatrix<E2, Cols1, Cols2>& rhs) TVMET_CXX_ALWAYS_INLINE;template<class E1, std::size_t Rows1, std::size_t Cols1, class E2, std::size_t Cols2> // Rows2 = Rows1XprMatrix< XprMtMProduct< XprMatrix<E1, Rows1, Cols1>, Rows1, Cols1, // M1(Rows1, Cols1) XprMatrix<E2, Rows1, Cols2>, Cols2 // M2(Rows1, Cols2) >, Cols1, Cols2 // return Dim>MtM_prod(const XprMatrix<E1, Rows1, Cols1>& lhs, const XprMatrix<E2, Rows1, Cols2>& rhs) TVMET_CXX_ALWAYS_INLINE;template<class E1, std::size_t Rows1, std::size_t Cols1, class E2, std::size_t Rows2> // Cols2 = Cols1XprMatrix< XprMMtProduct< XprMatrix<E1, Rows1, Cols1>, Rows1, Cols1, // M1(Rows1, Cols1) XprMatrix<E2, Rows2, Cols1>, Cols1 // M2(Rows2, Cols1) >, Rows1, Rows2 // return Dim>MMt_prod(const XprMatrix<E1, Rows1, Cols1>& lhs, const XprMatrix<E2, Rows2, Cols1>& rhs) TVMET_CXX_ALWAYS_INLINE;/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * matrix-vector specific prod( ... ) functions *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/template<class E1, std::size_t Rows, std::size_t Cols, class E2>XprVector< XprMVProduct< XprMatrix<E1, Rows, Cols>, Rows, Cols, XprVector<E2, Cols> >, Rows>prod(const XprMatrix<E1, Rows, Cols>& lhs, const XprVector<E2, Cols>& rhs) TVMET_CXX_ALWAYS_INLINE;/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * matrix specific functions *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/template<class E, std::size_t Rows, std::size_t Cols>XprMatrix< XprMatrixTranspose< XprMatrix<E, Rows, Cols> >, Cols, Rows>trans(const XprMatrix<E, Rows, Cols>& rhs) TVMET_CXX_ALWAYS_INLINE;template<class E, std::size_t Sz>typename NumericTraits<typename E::value_type>::sum_typetrace(const XprMatrix<E, Sz, Sz>& m) TVMET_CXX_ALWAYS_INLINE;template<class E, std::size_t Rows, std::size_t Cols>XprVector< XprMatrixRow< XprMatrix<E, Rows, Cols>, Rows, Cols >, Cols>row(const XprMatrix<E, Rows, Cols>& m, std::size_t no) TVMET_CXX_ALWAYS_INLINE;template<class E, std::size_t Rows, std::size_t Cols>XprVector< XprMatrixCol< XprMatrix<E, Rows, Cols>, Rows, Cols >, Rows>col(const XprMatrix<E, Rows, Cols>& m, std::size_t no) TVMET_CXX_ALWAYS_INLINE;template<class E, std::size_t Sz>XprVector< XprMatrixDiag< XprMatrix<E, Sz, Sz>, Sz >, Sz>diag(const XprMatrix<E, Sz, Sz>& m) TVMET_CXX_ALWAYS_INLINE;/********************************************************* * PART II: IMPLEMENTATION *********************************************************//*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Matrix arithmetic functions add, sub, mul and div *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*//* * function(XprMatrix<E1, Rows, Cols>, XprMatrix<E2, Rows, Cols>) */#define TVMET_IMPLEMENT_MACRO(NAME) \template<class E1, class E2, std::size_t Rows, std::size_t Cols> \inline \XprMatrix< \ XprBinOp< \ Fcnl_##NAME<typename E1::value_type, typename E2::value_type>, \ XprMatrix<E1, Rows, Cols>, \ XprMatrix<E2, Rows, Cols> \ >, \ Rows, Cols \> \NAME (const XprMatrix<E1, Rows, Cols>& lhs, \ const XprMatrix<E2, Rows, Cols>& rhs) { \ typedef XprBinOp< \ Fcnl_##NAME<typename E1::value_type, typename E2::value_type>, \ XprMatrix<E1, Rows, Cols>, \ XprMatrix<E2, Rows, Cols> \ > expr_type; \ return XprMatrix<expr_type, Rows, Cols>(expr_type(lhs, rhs)); \}TVMET_IMPLEMENT_MACRO(add) // per se element wiseTVMET_IMPLEMENT_MACRO(sub) // per se element wisenamespace element_wise { TVMET_IMPLEMENT_MACRO(mul) // not defined for matrizes TVMET_IMPLEMENT_MACRO(div) // not defined for matrizes}#undef TVMET_IMPLEMENT_MACRO/* * function(XprMatrix<E, Rows, Cols>, POD) * function(POD, XprMatrix<E, Rows, Cols>) * Note: - operations +,-,*,/ are per se element wise */#define TVMET_IMPLEMENT_MACRO(NAME, POD) \template<class E, std::size_t Rows, std::size_t Cols> \inline \XprMatrix< \ XprBinOp< \ Fcnl_##NAME<typename E::value_type, POD >, \ XprMatrix<E, Rows, Cols>, \ XprLiteral< POD > \ >, \ Rows, Cols \> \NAME (const XprMatrix<E, Rows, Cols>& lhs, POD rhs) { \ typedef XprBinOp< \ Fcnl_##NAME<typename E::value_type, POD >, \ XprMatrix<E, Rows, Cols>, \ XprLiteral< POD > \ > expr_type; \ return XprMatrix<expr_type, Rows, Cols>( \ expr_type(lhs, XprLiteral< POD >(rhs))); \} \ \template<class E, std::size_t Rows, std::size_t Cols> \inline \XprMatrix< \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -