/* * 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.59 2004/11/04 16:21:17 opetzold Exp $ */ #ifndef TVMET_MATRIX_FUNCTIONS_H #define TVMET_MATRIX_FUNCTIONS_H #include namespace tvmet { /* forwards */ template class Vector; template class VectorConstReference; /********************************************************* * PART I: DECLARATION *********************************************************/ /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Vector arithmetic functions add, sub, mul and div *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /* * function(Matrix, Matrix) * function(XprMatrix, Matrix) * function(Matrix, XprMatrix) */ #define TVMET_DECLARE_MACRO(NAME) \ template \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME, \ MatrixConstReference, \ MatrixConstReference \ >, \ Rows, Cols \ > \ NAME (const Matrix& lhs, \ const Matrix& rhs) TVMET_CXX_ALWAYS_INLINE; \ \ template \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME, \ XprMatrix, \ MatrixConstReference \ >, \ Rows, Cols \ > \ NAME (const XprMatrix& lhs, \ const Matrix& rhs) TVMET_CXX_ALWAYS_INLINE; \ \ template \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME, \ MatrixConstReference, \ XprMatrix \ >, \ Rows, Cols \ > \ NAME (const Matrix& lhs, \ const XprMatrix& rhs) TVMET_CXX_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(Matrix, POD) * function(POD, Matrix) * Note: - operations +,-,*,/ are per se element wise */ #define TVMET_DECLARE_MACRO(NAME, POD) \ template \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME, \ MatrixConstReference, \ XprLiteral \ >, \ Rows, Cols \ > \ NAME (const Matrix& lhs, \ POD rhs) TVMET_CXX_ALWAYS_INLINE; \ \ template \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME< POD, T>, \ XprLiteral< POD >, \ MatrixConstReference \ >, \ Rows, Cols \ > \ NAME (POD lhs, \ const Matrix& 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) #endif 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) #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(Matrix, complex) * function(complex, Matrix) * Note: - operations +,-,*,/ are per se element wise * \todo type promotion */ #define TVMET_DECLARE_MACRO(NAME) \ template \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME< std::complex, std::complex >, \ MatrixConstReference< std::complex, Rows, Cols>, \ XprLiteral > \ >, \ Rows, Cols \ > \ NAME (const Matrix< std::complex, Rows, Cols>& lhs, \ const std::complex& rhs) TVMET_CXX_ALWAYS_INLINE; \ \ template \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME< std::complex, std::complex >, \ XprLiteral< std::complex >, \ MatrixConstReference< std::complex, Rows, Cols> \ >, \ Rows, Cols \ > \ NAME (const std::complex& lhs, \ const Matrix< std::complex, 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 specific prod( ... ) functions *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ template XprMatrix< XprMMProduct< MatrixConstReference, Rows1, Cols1, // M1(Rows1, Cols1) MatrixConstReference, Cols2 // M2(Cols1, Cols2) >, Rows1, Cols2 // return Dim > prod(const Matrix& lhs, const Matrix& rhs) TVMET_CXX_ALWAYS_INLINE; template XprMatrix< XprMMProduct< XprMatrix, Rows1, Cols1, // M1(Rows1, Cols1) MatrixConstReference, Cols2 // M2(Cols1, Cols2) >, Rows1, Cols2 // return Dim > prod(const XprMatrix& lhs, const Matrix& rhs) TVMET_CXX_ALWAYS_INLINE; template XprMatrix< XprMMProduct< MatrixConstReference, Rows1, Cols1, // M1(Rows1, Cols1) XprMatrix, Cols2 // M2(Cols1, Cols2) >, Rows1, Cols2 // return Dim > prod(const Matrix& lhs, const XprMatrix& rhs) TVMET_CXX_ALWAYS_INLINE; template XprMatrix< XprMMProductTransposed< MatrixConstReference, Rows1, Cols1, // M1(Rows1, Cols1) MatrixConstReference, Cols2 // M2(Cols1, Cols2) >, Cols2, Rows1 // return Dim > trans_prod(const Matrix& lhs, const Matrix& rhs) TVMET_CXX_ALWAYS_INLINE; template // Rows2 = Rows1 XprMatrix< XprMtMProduct< MatrixConstReference, Rows1, Cols1, // M1(Rows1, Cols1) MatrixConstReference, Cols2 // M2(Rows1, Cols2) >, Cols1, Cols2 // return Dim > MtM_prod(const Matrix& lhs, const Matrix& rhs) TVMET_CXX_ALWAYS_INLINE; template XprMatrix< XprMMtProduct< MatrixConstReference, Rows1, Cols1, // M1(Rows1, Cols1) MatrixConstReference, Cols1 // M2(Rows2, Cols1) >, Rows1, Rows2 // return Dim > MMt_prod(const Matrix& lhs, const Matrix& rhs) TVMET_CXX_ALWAYS_INLINE; /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * matrix-vector specific prod( ... ) functions *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ template XprVector< XprMVProduct< MatrixConstReference, Rows, Cols, // M(Rows, Cols) VectorConstReference // V >, Rows > prod(const Matrix& lhs, const Vector& rhs) TVMET_CXX_ALWAYS_INLINE; template XprVector< XprMVProduct< MatrixConstReference, Rows, Cols, XprVector >, Rows > prod(const Matrix& lhs, const XprVector& rhs) TVMET_CXX_ALWAYS_INLINE; template XprVector< XprMVProduct< XprMatrix, Rows, Cols, // M(Rows, Cols) VectorConstReference // V >, Rows > prod(const XprMatrix& lhs, const Vector& rhs) TVMET_CXX_ALWAYS_INLINE; template XprVector< XprMtVProduct< MatrixConstReference, Rows, Cols, // M(Rows, Cols) VectorConstReference // V >, Cols > Mtx_prod(const Matrix& lhs, const Vector& rhs) TVMET_CXX_ALWAYS_INLINE; /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * matrix specific functions *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ template XprMatrix< XprMatrixTranspose< MatrixConstReference >, Cols, Rows > trans(const Matrix& rhs) TVMET_CXX_ALWAYS_INLINE; template typename NumericTraits::sum_type trace(const Matrix& m) TVMET_CXX_ALWAYS_INLINE; template XprVector< XprMatrixRow< MatrixConstReference, Rows, Cols >, Cols > row(const Matrix& m, std::size_t no) TVMET_CXX_ALWAYS_INLINE; template XprVector< XprMatrixCol< MatrixConstReference, Rows, Cols >, Rows > col(const Matrix& m, std::size_t no) TVMET_CXX_ALWAYS_INLINE; template XprVector< XprMatrixDiag< MatrixConstReference, Sz >, Sz > diag(const Matrix& m) TVMET_CXX_ALWAYS_INLINE; /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * min/max unary functions *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ template Extremum maximum(const XprMatrix& e); // NOT TVMET_CXX_ALWAYS_INLINE; template Extremum maximum(const Matrix& m) TVMET_CXX_ALWAYS_INLINE; template Extremum minimum(const XprMatrix& e); // NOT TVMET_CXX_ALWAYS_INLINE; template Extremum minimum(const Matrix& m) TVMET_CXX_ALWAYS_INLINE; template typename E::value_type max(const XprMatrix& e); // NOT TVMET_CXX_ALWAYS_INLINE; template T max(const Matrix& m) TVMET_CXX_ALWAYS_INLINE; template typename E::value_type min(const XprMatrix& e); // NOT TVMET_CXX_ALWAYS_INLINE; template T min(const Matrix& m) TVMET_CXX_ALWAYS_INLINE; /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * other unary functions *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ template XprMatrix< XprIdentity, Rows, Cols > identity() TVMET_CXX_ALWAYS_INLINE; template XprMatrix< XprIdentity< typename M::value_type, M::Rows, M::Cols>, M::Rows, M::Cols > identity() TVMET_CXX_ALWAYS_INLINE; template XprMatrix< MatrixConstReference, Rows, Cols > cmatrix_ref(const T* mem) TVMET_CXX_ALWAYS_INLINE; /********************************************************* * PART II: IMPLEMENTATION *********************************************************/ /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Vector arithmetic functions add, sub, mul and div *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /* * function(Matrix, Matrix) * function(XprMatrix, Matrix) * function(Matrix, XprMatrix) */ #define TVMET_IMPLEMENT_MACRO(NAME) \ template \ inline \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME, \ MatrixConstReference, \ MatrixConstReference \ >, \ Rows, Cols \ > \ NAME (const Matrix& lhs, const Matrix& rhs) { \ typedef XprBinOp < \ Fcnl_##NAME, \ MatrixConstReference, \ MatrixConstReference \ > expr_type; \ return XprMatrix( \ expr_type(lhs.const_ref(), rhs.const_ref())); \ } \ \ template \ inline \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME, \ XprMatrix, \ MatrixConstReference \ >, \ Rows, Cols \ > \ NAME (const XprMatrix& lhs, const Matrix& rhs) { \ typedef XprBinOp< \ Fcnl_##NAME, \ XprMatrix, \ MatrixConstReference \ > expr_type; \ return XprMatrix( \ expr_type(lhs, rhs.const_ref())); \ } \ \ template \ inline \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME, \ MatrixConstReference, \ XprMatrix \ >, \ Rows, Cols \ > \ NAME (const Matrix& lhs, const XprMatrix& rhs) { \ typedef XprBinOp< \ Fcnl_##NAME, \ MatrixConstReference, \ XprMatrix \ > expr_type; \ return XprMatrix( \ expr_type(lhs.const_ref(), 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(Matrix, POD) * function(POD, Matrix) * Note: - operations +,-,*,/ are per se element wise */ #define TVMET_IMPLEMENT_MACRO(NAME, POD) \ template \ inline \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME, \ MatrixConstReference, \ XprLiteral \ >, \ Rows, Cols \ > \ NAME (const Matrix& lhs, POD rhs) { \ typedef XprBinOp< \ Fcnl_##NAME, \ MatrixConstReference, \ XprLiteral< POD > \ > expr_type; \ return XprMatrix( \ expr_type(lhs.const_ref(), XprLiteral< POD >(rhs))); \ } \ \ template \ inline \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME< POD, T>, \ XprLiteral< POD >, \ MatrixConstReference \ >, \ Rows, Cols \ > \ NAME (POD lhs, const Matrix& rhs) { \ typedef XprBinOp< \ Fcnl_##NAME< POD, T>, \ XprLiteral< POD >, \ MatrixConstReference \ > expr_type; \ return XprMatrix( \ expr_type(XprLiteral< POD >(lhs), rhs.const_ref())); \ } TVMET_IMPLEMENT_MACRO(add, int) TVMET_IMPLEMENT_MACRO(sub, int) TVMET_IMPLEMENT_MACRO(mul, int) TVMET_IMPLEMENT_MACRO(div, int) #if defined(TVMET_HAVE_LONG_LONG) TVMET_IMPLEMENT_MACRO(add, long long int) TVMET_IMPLEMENT_MACRO(sub, long long int) TVMET_IMPLEMENT_MACRO(mul, long long int) TVMET_IMPLEMENT_MACRO(div, long long int) #endif 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) #if defined(TVMET_HAVE_LONG_DOUBLE) TVMET_IMPLEMENT_MACRO(add, long double) TVMET_IMPLEMENT_MACRO(sub, long double) TVMET_IMPLEMENT_MACRO(mul, long double) TVMET_IMPLEMENT_MACRO(div, long double) #endif #undef TVMET_IMPLEMENT_MACRO #if defined(TVMET_HAVE_COMPLEX) /* * function(Matrix, complex) * function(complex, Matrix) * Note: - operations +,-,*,/ are per se element wise * \todo type promotion */ #define TVMET_IMPLEMENT_MACRO(NAME) \ template \ inline \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME< std::complex, std::complex >, \ MatrixConstReference< std::complex, Rows, Cols>, \ XprLiteral > \ >, \ Rows, Cols \ > \ NAME (const Matrix< std::complex, Rows, Cols>& lhs, \ const std::complex& rhs) { \ typedef XprBinOp< \ Fcnl_##NAME< std::complex, std::complex >, \ MatrixConstReference< std::complex, Rows, Cols>, \ XprLiteral< std::complex > \ > expr_type; \ return XprMatrix( \ expr_type(lhs.const_ref(), XprLiteral< std::complex >(rhs))); \ } \ \ template \ inline \ XprMatrix< \ XprBinOp< \ Fcnl_##NAME< std::complex, std::complex >, \ XprLiteral< std::complex >, \ MatrixConstReference< std::complex, Rows, Cols> \ >, \ Rows, Cols \ > \ NAME (const std::complex& lhs, \ const Matrix< std::complex, Rows, Cols>& rhs) { \ typedef XprBinOp< \ Fcnl_##NAME< std::complex, std::complex >, \ XprLiteral< std::complex >, \ MatrixConstReference \ > expr_type; \ return XprMatrix( \ expr_type(XprLiteral< std::complex >(lhs), rhs.const_ref())); \ } TVMET_IMPLEMENT_MACRO(add) TVMET_IMPLEMENT_MACRO(sub) TVMET_IMPLEMENT_MACRO(mul) TVMET_IMPLEMENT_MACRO(div) #undef TVMET_IMPLEMENT_MACRO #endif // defined(TVMET_HAVE_COMPLEX) /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * matrix specific prod( ... ) functions *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /** * \fn prod(const Matrix& lhs, const Matrix& rhs) * \brief Function for the matrix-matrix-product. * \ingroup _binary_function * \note The rows2 has to be equal to cols1. */ template inline XprMatrix< XprMMProduct< MatrixConstReference, Rows1, Cols1, // M1(Rows1, Cols1) MatrixConstReference, Cols2 // M2(Cols1, Cols2) >, Rows1, Cols2 // return Dim > prod(const Matrix& lhs, const Matrix& rhs) { typedef XprMMProduct< MatrixConstReference, Rows1, Cols1, MatrixConstReference, Cols2 > expr_type; return XprMatrix( expr_type(lhs.const_ref(), rhs.const_ref())); } /** * \fn prod(const XprMatrix& lhs, const Matrix& rhs) * \brief Evaluate the product of XprMatrix and Matrix. * \ingroup _binary_function */ template inline XprMatrix< XprMMProduct< XprMatrix, Rows1, Cols1, // M1(Rows1, Cols1) MatrixConstReference, Cols2 // M2(Cols1, Cols2) >, Rows1, Cols2 // return Dim > prod(const XprMatrix& lhs, const Matrix& rhs) { typedef XprMMProduct< XprMatrix, Rows1, Cols1, MatrixConstReference, Cols2 > expr_type; return XprMatrix( expr_type(lhs, rhs.const_ref())); } /** * \fn prod(const Matrix& lhs, const XprMatrix& rhs) * \brief Evaluate the product of Matrix and XprMatrix. * \ingroup _binary_function */ template inline XprMatrix< XprMMProduct< MatrixConstReference, Rows1, Cols1, // M1(Rows1, Cols1) XprMatrix, Cols2 // M2(Cols1, Cols2) >, Rows1, Cols2 // return Dim > prod(const Matrix& lhs, const XprMatrix& rhs) { typedef XprMMProduct< MatrixConstReference, Rows1, Cols1, XprMatrix, Cols2 > expr_type; return XprMatrix( expr_type(lhs.const_ref(), rhs)); } /** * \fn trans_prod(const Matrix& lhs, const Matrix& rhs) * \brief Function for the trans(matrix-matrix-product) * \ingroup _binary_function * Perform on given Matrix M1 and M2: * \f[ * (M_1\,M_2)^T * \f] */ template inline XprMatrix< XprMMProductTransposed< MatrixConstReference, Rows1, Cols1, // M1(Rows1, Cols1) MatrixConstReference, Cols2 // M2(Cols1, Cols2) >, Cols2, Rows1 // return Dim > trans_prod(const Matrix& lhs, const Matrix& rhs) { typedef XprMMProductTransposed< MatrixConstReference, Rows1, Cols1, MatrixConstReference, Cols2 > expr_type; return XprMatrix( expr_type(lhs.const_ref(), rhs.const_ref())); } /** * \fn MtM_prod(const Matrix& lhs, const Matrix& rhs) * \brief Function for the trans(matrix)-matrix-product. * \ingroup _binary_function * 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. */ template // Rows2 = Rows1 inline XprMatrix< XprMtMProduct< MatrixConstReference, Rows1, Cols1, // M1(Rows1, Cols1) MatrixConstReference, Cols2 // M2(Rows1, Cols2) >, Cols1, Cols2 // return Dim > MtM_prod(const Matrix& lhs, const Matrix& rhs) { typedef XprMtMProduct< MatrixConstReference, Rows1, Cols1, MatrixConstReference, Cols2 > expr_type; return XprMatrix( expr_type(lhs.const_ref(), rhs.const_ref())); } /** * \fn MMt_prod(const Matrix& lhs, const Matrix& rhs) * \brief Function for the matrix-trans(matrix)-product. * \ingroup _binary_function * \note The Cols2 has to be equal to Cols1. */ template inline XprMatrix< XprMMtProduct< MatrixConstReference, Rows1, Cols1, // M1(Rows1, Cols1) MatrixConstReference, Cols1 // M2(Rows2, Cols1) >, Rows1, Rows2 // return Dim > MMt_prod(const Matrix& lhs, const Matrix& rhs) { typedef XprMMtProduct< MatrixConstReference, Rows1, Cols1, MatrixConstReference, Cols1 > expr_type; return XprMatrix( expr_type(lhs.const_ref(), rhs.const_ref())); } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * matrix-vector specific prod( ... ) functions *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /** * \fn prod(const Matrix& lhs, const Vector& rhs) * \brief Function for the matrix-vector-product * \ingroup _binary_function */ template inline XprVector< XprMVProduct< MatrixConstReference, Rows, Cols, // M(Rows, Cols) VectorConstReference // V >, Rows > prod(const Matrix& lhs, const Vector& rhs) { typedef XprMVProduct< MatrixConstReference, Rows, Cols, VectorConstReference > expr_type; return XprVector( expr_type(lhs.const_ref(), rhs.const_ref())); } /** * \fn prod(const Matrix& lhs, const XprVector& rhs) * \brief Function for the matrix-vector-product * \ingroup _binary_function */ template inline XprVector< XprMVProduct< MatrixConstReference, Rows, Cols, XprVector >, Rows > prod(const Matrix& lhs, const XprVector& rhs) { typedef XprMVProduct< MatrixConstReference, Rows, Cols, XprVector > expr_type; return XprVector( expr_type(lhs.const_ref(), rhs)); } /* * \fn prod(const XprMatrix& lhs, const Vector& rhs) * \brief Compute the product of an XprMatrix with a Vector. * \ingroup _binary_function */ template inline XprVector< XprMVProduct< XprMatrix, Rows, Cols, // M(Rows, Cols) VectorConstReference // V >, Rows > prod(const XprMatrix& lhs, const Vector& rhs) { typedef XprMVProduct< XprMatrix, Rows, Cols, VectorConstReference > expr_type; return XprVector( expr_type(lhs, rhs.const_ref())); } /** * \fn Mtx_prod(const Matrix& matrix, const Vector& vector) * \brief Function for the trans(matrix)-vector-product * \ingroup _binary_function * Perform on given Matrix M and vector x: * \f[ * M^T\, x * \f] */ template inline XprVector< XprMtVProduct< MatrixConstReference, Rows, Cols, // M(Rows, Cols) VectorConstReference // V >, Cols > Mtx_prod(const Matrix& lhs, const Vector& rhs) { typedef XprMtVProduct< MatrixConstReference, Rows, Cols, VectorConstReference > expr_type; return XprVector( expr_type(lhs.const_ref(), rhs.const_ref())); } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * matrix specific functions *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /** * \fn trans(const Matrix& rhs) * \brief Transpose the matrix * \ingroup _unary_function */ template inline XprMatrix< XprMatrixTranspose< MatrixConstReference >, Cols, Rows > trans(const Matrix& rhs) { typedef XprMatrixTranspose< MatrixConstReference > expr_type; return XprMatrix( expr_type(rhs.const_ref())); } /* * \fn trace(const Matrix& 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 Matrix& m) { return meta::Matrix::trace(m); } /** * \fn row(const Matrix& m, std::size_t no) * \brief Returns a row vector of the given matrix. * \ingroup _binary_function */ template inline XprVector< XprMatrixRow< MatrixConstReference, Rows, Cols >, Cols > row(const Matrix& m, std::size_t no) { typedef XprMatrixRow< MatrixConstReference, Rows, Cols > expr_type; return XprVector(expr_type(m.const_ref(), no)); } /** * \fn col(const Matrix& m, std::size_t no) * \brief Returns a column vector of the given matrix. * \ingroup _binary_function */ template inline XprVector< XprMatrixCol< MatrixConstReference, Rows, Cols >, Rows > col(const Matrix& m, std::size_t no) { typedef XprMatrixCol< MatrixConstReference, Rows, Cols > expr_type; return XprVector(expr_type(m.const_ref(), no)); } /** * \fn diag(const Matrix& m) * \brief Returns the diagonal vector of the given square matrix. * \ingroup _unary_function */ template inline XprVector< XprMatrixDiag< MatrixConstReference, Sz >, Sz > diag(const Matrix& m) { typedef XprMatrixDiag< MatrixConstReference, Sz > expr_type; return XprVector(expr_type(m.const_ref())); } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * min/max unary functions *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /** * \fn maximum(const XprMatrix& e) * \brief Find the maximum of a matrix expression * \ingroup _unary_function */ template inline Extremum maximum(const XprMatrix& e) { typedef typename E::value_type value_type; value_type temp(e(0, 0)); std::size_t row_no(0), col_no(0); for(std::size_t i = 0; i != Rows; ++i) { for(std::size_t j = 0; j != Cols; ++j) { if(e(i, j) > temp) { temp = e(i, j); row_no = i; col_no = j; } } } return Extremum(temp, row_no, col_no); } /** * \fn maximum(const Matrix& m) * \brief Find the maximum of a matrix * \ingroup _unary_function */ template inline Extremum maximum(const Matrix& m) { return maximum(m.as_expr()); } /** * \fn minimum(const XprMatrix& e) * \brief Find the minimum of a matrix expression * \ingroup _unary_function */ template inline Extremum minimum(const XprMatrix& e) { typedef typename E::value_type value_type; value_type temp(e(0, 0)); std::size_t row_no(0), col_no(0); for(std::size_t i = 0; i != Rows; ++i) { for(std::size_t j = 0; j != Cols; ++j) { if(e(i, j) < temp) { temp = e(i, j); row_no = i; col_no = j; } } } return Extremum(temp, row_no, col_no); } /** * \fn minimum(const Matrix& m) * \brief Find the minimum of a matrix * \ingroup _unary_function */ template inline Extremum minimum(const Matrix& m) { return minimum(m.as_expr()); } /** * \fn max(const XprMatrix& e) * \brief Find the maximum of a matrix expression * \ingroup _unary_function */ template inline typename E::value_type max(const XprMatrix& e) { typedef typename E::value_type value_type; value_type temp(e(0, 0)); for(std::size_t i = 0; i != Rows; ++i) for(std::size_t j = 0; j != Cols; ++j) if(e(i, j) > temp) temp = e(i, j); return temp; } /** * \fn max(const Matrix& m) * \brief Find the maximum of a matrix * \ingroup _unary_function */ template inline T max(const Matrix& m) { typedef T value_type; typedef typename Matrix< T, Rows, Cols >::const_iterator const_iterator; const_iterator iter(m.begin()); const_iterator last(m.end()); value_type temp(*iter); for( ; iter != last; ++iter) if(*iter > temp) temp = *iter; return temp; } /** * \fn min(const XprMatrix& e) * \brief Find the minimum of a matrix expression * \ingroup _unary_function */ template inline typename E::value_type min(const XprMatrix& e) { typedef typename E::value_type value_type; value_type temp(e(0, 0)); for(std::size_t i = 0; i != Rows; ++i) for(std::size_t j = 0; j != Cols; ++j) if(e(i, j) < temp) temp = e(i, j); return temp; } /** * \fn min(const Matrix& m) * \brief Find the minimum of a matrix * \ingroup _unary_function */ template inline T min(const Matrix& m) { typedef T value_type; typedef typename Matrix< T, Rows, Cols >::const_iterator const_iterator; const_iterator iter(m.begin()); const_iterator last(m.end()); value_type temp(*iter); for( ; iter != last; ++iter) if(*iter < temp) temp = *iter; return temp; } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * other unary functions *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /** * \fn XprMatrix, M::Rows, M::Cols>identity() * \brief Fill a matrix to an identity matrix. * \ingroup _unary_function * * \note The matrix doesn't need to be square. Only the elements * where the current number of rows are equal to columns * will be set to 1, else to 0. * * \par Usage: * \code * typedef Matrix matrix_type; * ... * matrix_type E( identity() ); * \endcode * * Note, we have to specify the type, number of rows and columns * since ADL can't work here. * * * * \since release 1.6.0 */ template inline XprMatrix< XprIdentity, Rows, Cols > identity() { typedef XprIdentity expr_type; return XprMatrix(expr_type()); } /** * \fn XprMatrix, M::Rows, M::Cols>identity() * \brief Fill a matrix to an identity matrix (convenience wrapper * for matrix typedefs). * \ingroup _unary_function * * \note The matrix doesn't need to be square. Only the elements * where the current number of rows are equal to columns * will be set to 1, else to 0. * * \par Usage: * \code * typedef Matrix matrix_type; * ... * matrix_type E( identity() ); * \endcode * * Note, we have to specify the matrix type, since ADL can't work here. * * \since release 1.6.0 */ template inline XprMatrix< XprIdentity< typename M::value_type, M::Rows, M::Cols>, M::Rows, M::Cols > identity() { return identity(); } /** * \fn cmatrix_ref(const T* mem) * \brief Creates an expression wrapper for a C like matrices. * \ingroup _unary_function * * This is like creating a matrix of external data, as described * at \ref construct. With this function you wrap an expression * around a C style matrix and you can operate directly with it * as usual. * * \par Example: * \code * static float lhs[3][3] = { * {-1, 0, 1}, { 1, 0, 1}, {-1, 0, -1} * }; * static float rhs[3][3] = { * { 0, 1, 1}, { 0, 1, -1}, { 0, -1, 1} * }; * ... * * typedef Matrix matrix_type; * * matrix_type M( cmatrix_ref(&lhs[0][0]) * * cmatrix_ref(&rhs[0][0]) ); * \endcode * * \since release 1.6.0 */ template inline XprMatrix< MatrixConstReference, Rows, Cols > cmatrix_ref(const T* mem) { typedef MatrixConstReference expr_type; return XprMatrix(expr_type(mem)); }; } // namespace tvmet #endif // TVMET_MATRIX_FUNCTIONS_H // Local Variables: // mode:C++ // End: