/* * Tiny Vector Matrix Library * Dense Vector Matrix Libary of Tiny size using Expression Templates * * Copyright (C) 2001 - 2003 Olaf Petzold * * 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.39 2004/07/06 05:49:22 opetzold Exp $ */ #ifndef TVMET_XPR_MATRIX_FUNCTIONS_H #define TVMET_XPR_MATRIX_FUNCTIONS_H namespace tvmet { /* forwards */ template class Matrix; template class Vector; template class XprVector; template class XprMatrixTranspose; template class XprMatrixDiag; template class XprMatrixRow; template class XprMatrixCol; /********************************************************* * PART I: DECLARATION *********************************************************/ /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Matrix arithmetic functions add, sub, mul and div *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /* * function(XprMatrix, XprMatrix) */ #define TVMET_DECLARE_MACRO(NAME) \ template \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME, \ XprMatrix, \ XprMatrix \ >, \ Rows, Cols \ > \ NAME (const XprMatrix& lhs, \ const XprMatrix& rhs) _tvmet_always_inline; TVMET_DECLARE_MACRO(add) // per se element wise TVMET_DECLARE_MACRO(sub) // per se element wise namespace element_wise { TVMET_DECLARE_MACRO(mul) // not defined for matrizes TVMET_DECLARE_MACRO(div) // not defined for matrizes } #undef TVMET_DECLARE_MACRO /* * function(XprMatrix, POD) * function(POD, XprMatrix) * Note: - operations +,-,*,/ are per se element wise */ #define TVMET_DECLARE_MACRO(NAME, POD) \ template \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME, \ XprMatrix, \ XprLiteral< POD > \ >, \ Rows, Cols \ > \ NAME (const XprMatrix& lhs, \ POD rhs) _tvmet_always_inline; \ \ template \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME< POD, typename E::value_type>, \ XprLiteral< POD >, \ XprMatrix \ >, \ Rows, Cols \ > \ NAME (POD lhs, \ const XprMatrix& rhs) _tvmet_always_inline; TVMET_DECLARE_MACRO(add, int) TVMET_DECLARE_MACRO(sub, int) TVMET_DECLARE_MACRO(mul, int) TVMET_DECLARE_MACRO(div, int) TVMET_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) #undef TVMET_DECLARE_MACRO #if defined(EIGEN_USE_COMPLEX) /* * function(XprMatrix, complex) * function(complex, XprMatrix) * Note: - operations +,-,*,/ are per se element wise * \todo type promotion */ #define TVMET_DECLARE_MACRO(NAME) \ template \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME >, \ XprMatrix, \ XprLiteral< std::complex > \ >, \ Rows, Cols \ > \ NAME (const XprMatrix& lhs, \ const std::complex& rhs) _tvmet_always_inline; \ \ template \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME< std::complex, typename E::value_type>, \ XprLiteral< std::complex >, \ XprMatrix \ >, \ Rows, Cols \ > \ NAME (const std::complex& lhs, \ const XprMatrix& rhs) _tvmet_always_inline; TVMET_DECLARE_MACRO(add) TVMET_DECLARE_MACRO(sub) TVMET_DECLARE_MACRO(mul) TVMET_DECLARE_MACRO(div) #undef TVMET_DECLARE_MACRO #endif // defined(EIGEN_USE_COMPLEX) /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * matrix prod( ... ) functions *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ template XprMatrix< XprMMProduct< XprMatrix, Rows1, Cols1, // M1(Rows1, Cols1) XprMatrix, Cols2 >, Rows1, Cols2 // return Dim > prod(const XprMatrix& lhs, const XprMatrix& rhs) _tvmet_always_inline; template XprMatrix< XprMMProductTransposed< XprMatrix, Rows1, Cols1, // M1(Rows1, Cols1) XprMatrix, Cols2 // M2(Cols1, Cols2) >, Cols2, Rows1 // return Dim > trans_prod(const XprMatrix& lhs, const XprMatrix& rhs) _tvmet_always_inline; template // Rows2 = Rows1 XprMatrix< XprMtMProduct< XprMatrix, Rows1, Cols1, // M1(Rows1, Cols1) XprMatrix, Cols2 // M2(Rows1, Cols2) >, Cols1, Cols2 // return Dim > MtM_prod(const XprMatrix& lhs, const XprMatrix& rhs) _tvmet_always_inline; template // Cols2 = Cols1 XprMatrix< XprMMtProduct< XprMatrix, Rows1, Cols1, // M1(Rows1, Cols1) XprMatrix, Cols1 // M2(Rows2, Cols1) >, Rows1, Rows2 // return Dim > MMt_prod(const XprMatrix& lhs, const XprMatrix& rhs) _tvmet_always_inline; /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * matrix-vector specific prod( ... ) functions *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ template XprVector< XprMVProduct< XprMatrix, Rows, Cols, XprVector >, Rows > prod(const XprMatrix& lhs, const XprVector& rhs) _tvmet_always_inline; /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * matrix specific functions *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ template XprMatrix< XprMatrixTranspose< XprMatrix >, Cols, Rows > trans(const XprMatrix& rhs) _tvmet_always_inline; #if 0 // XXX needs declaration of meta::Matrix::trace template typename NumericTraits::sum_type trace(const XprMatrix& m)_tvmet_always_inline; #endif template XprVector< XprMatrixRow< XprMatrix, Rows, Cols >, Cols > row(const XprMatrix& m, int no) _tvmet_always_inline; template XprVector< XprMatrixCol< XprMatrix, Rows, Cols >, Rows > col(const XprMatrix& m, int no) _tvmet_always_inline; template XprVector< XprMatrixDiag< XprMatrix, Sz >, Sz > diag(const XprMatrix& m) _tvmet_always_inline; /********************************************************* * PART II: IMPLEMENTATION *********************************************************/ /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Matrix arithmetic functions add, sub, mul and div *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /* * function(XprMatrix, XprMatrix) */ #define TVMET_IMPLEMENT_MACRO(NAME) \ template \ inline \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME, \ XprMatrix, \ XprMatrix \ >, \ Rows, Cols \ > \ NAME (const XprMatrix& lhs, \ const XprMatrix& rhs) { \ typedef XprBinOp< \ Fcnl_##NAME, \ XprMatrix, \ XprMatrix \ > expr_type; \ return XprMatrix(expr_type(lhs, rhs)); \ } TVMET_IMPLEMENT_MACRO(add) // per se element wise TVMET_IMPLEMENT_MACRO(sub) // per se element wise namespace element_wise { TVMET_IMPLEMENT_MACRO(mul) // not defined for matrizes TVMET_IMPLEMENT_MACRO(div) // not defined for matrizes } #undef TVMET_IMPLEMENT_MACRO /* * function(XprMatrix, POD) * function(POD, XprMatrix) * Note: - operations +,-,*,/ are per se element wise */ #define TVMET_IMPLEMENT_MACRO(NAME, POD) \ template \ inline \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME, \ XprMatrix, \ XprLiteral< POD > \ >, \ Rows, Cols \ > \ NAME (const XprMatrix& lhs, POD rhs) { \ typedef XprBinOp< \ Fcnl_##NAME, \ XprMatrix, \ XprLiteral< POD > \ > expr_type; \ return XprMatrix( \ expr_type(lhs, XprLiteral< POD >(rhs))); \ } \ \ template \ inline \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME< POD, typename E::value_type>, \ XprLiteral< POD >, \ XprMatrix \ >, \ Rows, Cols \ > \ NAME (POD lhs, const XprMatrix& rhs) { \ typedef XprBinOp< \ Fcnl_##NAME< POD, typename E::value_type>, \ XprLiteral< POD >, \ XprMatrix \ > expr_type; \ return XprMatrix( \ expr_type(XprLiteral< POD >(lhs), rhs)); \ } TVMET_IMPLEMENT_MACRO(add, int) TVMET_IMPLEMENT_MACRO(sub, int) TVMET_IMPLEMENT_MACRO(mul, int) TVMET_IMPLEMENT_MACRO(div, int) TVMET_IMPLEMENT_MACRO(add, float) TVMET_IMPLEMENT_MACRO(sub, float) TVMET_IMPLEMENT_MACRO(mul, float) TVMET_IMPLEMENT_MACRO(div, float) TVMET_IMPLEMENT_MACRO(add, double) TVMET_IMPLEMENT_MACRO(sub, double) TVMET_IMPLEMENT_MACRO(mul, double) TVMET_IMPLEMENT_MACRO(div, double) #undef TVMET_IMPLEMENT_MACRO #if defined(EIGEN_USE_COMPLEX) /* * function(XprMatrix, complex) * function(complex, XprMatrix) * Note: - operations +,-,*,/ are per se element wise * \todo type promotion */ #define TVMET_IMPLEMENT_MACRO(NAME) \ template \ inline \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME >, \ XprMatrix, \ XprLiteral< std::complex > \ >, \ Rows, Cols \ > \ NAME (const XprMatrix& lhs, \ const std::complex& rhs) { \ typedef XprBinOp< \ Fcnl_##NAME >, \ XprMatrix, \ XprLiteral< std::complex > \ > expr_type; \ return XprMatrix( \ expr_type(lhs, XprLiteral< std::complex >(rhs))); \ } \ \ template \ inline \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME< std::complex, typename E::value_type>, \ XprLiteral< std::complex >, \ XprMatrix \ >, \ Rows, Cols \ > \ NAME (const std::complex& lhs, \ const XprMatrix& rhs) { \ typedef XprBinOp< \ Fcnl_##NAME< std::complex, typename E::value_type>, \ XprLiteral< std::complex >, \ XprMatrix \ > expr_type; \ return XprMatrix( \ expr_type(XprLiteral< std::complex >(lhs), rhs)); \ } TVMET_IMPLEMENT_MACRO(add) TVMET_IMPLEMENT_MACRO(sub) TVMET_IMPLEMENT_MACRO(mul) TVMET_IMPLEMENT_MACRO(div) #undef TVMET_IMPLEMENT_MACRO #endif // defined(EIGEN_USE_COMPLEX) /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * matrix prod( ... ) functions *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /** * \fn prod(const XprMatrix& lhs, const XprMatrix& rhs) * \brief Evaluate the product of two XprMatrix. * Perform on given Matrix M1 and M2: * \f[ * M_1\,M_2 * \f] * \note The numer of Rows2 has to be equal to Cols1. * \ingroup _binary_function */ template inline XprMatrix< XprMMProduct< XprMatrix, Rows1, Cols1, // M1(Rows1, Cols1) XprMatrix, Cols2 >, Rows1, Cols2 // return Dim > prod(const XprMatrix& lhs, const XprMatrix& rhs) { typedef XprMMProduct< XprMatrix, Rows1, Cols1, XprMatrix, Cols2 > expr_type; return XprMatrix(expr_type(lhs, rhs)); } /** * \fn trans_prod(const XprMatrix& lhs, const XprMatrix& rhs) * \brief Function for the trans(matrix-matrix-product) * Perform on given Matrix M1 and M2: * \f[ * (M_1\,M_2)^T * \f] * \note The numer of Rows2 has to be equal to Cols1. * \ingroup _binary_function */ template inline XprMatrix< XprMMProductTransposed< XprMatrix, Rows1, Cols1, // M1(Rows1, Cols1) XprMatrix, Cols2 // M2(Cols1, Cols2) >, Cols2, Rows1 // return Dim > trans_prod(const XprMatrix& lhs, const XprMatrix& rhs) { typedef XprMMProductTransposed< XprMatrix, Rows1, Cols1, XprMatrix, Cols2 > expr_type; return XprMatrix(expr_type(lhs, rhs)); } /** * \fn MtM_prod(const XprMatrix& lhs, const XprMatrix& rhs) * \brief Function for the trans(matrix)-matrix-product. * using formula * \f[ * M_1^{T}\,M_2 * \f] * \note The number of cols of matrix 2 have to be equal to number of rows of * matrix 1, since matrix 1 is trans - the result is a (Cols1 x Cols2) * matrix. * \ingroup _binary_function */ template // Rows2 = Rows1 inline XprMatrix< XprMtMProduct< XprMatrix, Rows1, Cols1, // M1(Rows1, Cols1) XprMatrix, Cols2 // M2(Rows1, Cols2) >, Cols1, Cols2 // return Dim > MtM_prod(const XprMatrix& lhs, const XprMatrix& rhs) { typedef XprMtMProduct< XprMatrix, Rows1, Cols1, XprMatrix, Cols2 > expr_type; return XprMatrix(expr_type(lhs, rhs)); } /** * \fn MMt_prod(const XprMatrix& lhs, const XprMatrix& rhs) * \brief Function for the matrix-trans(matrix)-product. * \ingroup _binary_function * \note The cols2 has to be equal to cols1. */ template // Cols2 = Cols1 inline XprMatrix< XprMMtProduct< XprMatrix, Rows1, Cols1, // M1(Rows1, Cols1) XprMatrix, Cols1 // M2(Rows2, Cols1) >, Rows1, Rows2 // return Dim > MMt_prod(const XprMatrix& lhs, const XprMatrix& rhs) { typedef XprMMtProduct< XprMatrix, Rows1, Cols1, XprMatrix, Cols1 > expr_type; return XprMatrix(expr_type(lhs, rhs)); } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * matrix-vector specific prod( ... ) functions *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /** * \fn prod(const XprMatrix& lhs, const XprVector& rhs) * \brief Evaluate the product of XprMatrix and XprVector. * \ingroup _binary_function */ template inline XprVector< XprMVProduct< XprMatrix, Rows, Cols, XprVector >, Rows > prod(const XprMatrix& lhs, const XprVector& rhs) { typedef XprMVProduct< XprMatrix, Rows, Cols, XprVector > expr_type; return XprVector(expr_type(lhs, rhs)); } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * matrix specific functions *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /** * \fn trans(const XprMatrix& rhs) * \brief Transpose an expression matrix. * \ingroup _unary_function */ template inline XprMatrix< XprMatrixTranspose< XprMatrix >, Cols, Rows > trans(const XprMatrix& rhs) { typedef XprMatrixTranspose< XprMatrix > expr_type; return XprMatrix(expr_type(rhs)); } #if 0 // XXX needs declaration of meta::Matrix::trace /* * \fn trace(const XprMatrix& m) * \brief Compute the trace of a square matrix. * \ingroup _unary_function * * Simply compute the trace of the given matrix as: * \f[ * \sum_{k = 0}^{Sz-1} m(k, k) * \f] */ template inline typename NumericTraits::sum_type trace(const XprMatrix& m) { return meta::Matrix::trace(m); } #endif /** * \fn row(const XprMatrix& m, int no) * \brief Returns a row vector of the given matrix. * \ingroup _binary_function */ template inline XprVector< XprMatrixRow< XprMatrix, Rows, Cols >, Cols > row(const XprMatrix& m, int no) { typedef XprMatrixRow< XprMatrix, Rows, Cols > expr_type; return XprVector(expr_type(m, no)); } /** * \fn col(const XprMatrix& m, int no) * \brief Returns a column vector of the given matrix. * \ingroup _binary_function */ template inline XprVector< XprMatrixCol< XprMatrix, Rows, Cols >, Rows > col(const XprMatrix& m, int no) { typedef XprMatrixCol< XprMatrix, Rows, Cols > expr_type; return XprVector(expr_type(m, no)); } /** * \fn diag(const XprMatrix& m) * \brief Returns the diagonal vector of the given square matrix. * \ingroup _unary_function */ template inline XprVector< XprMatrixDiag< XprMatrix, Sz >, Sz > diag(const XprMatrix& m) { typedef XprMatrixDiag< XprMatrix, Sz> expr_type; return XprVector(expr_type(m)); } } // namespace tvmet #endif // TVMET_XPR_MATRIX_FUNCTIONS_H // Local Variables: // mode:C++ // End: