SourceForge Logo Tiny Vector Matrix library using Expression Templates Sourceforge Project Page

include/tvmet/xpr/MatrixFunctions.h

Go to the documentation of this file.
00001 /*
00002  * Tiny Vector Matrix Library
00003  * Dense Vector Matrix Libary of Tiny size using Expression Templates
00004  *
00005  * Copyright (C) 2001 - 2007 Olaf Petzold <opetzold@users.sourceforge.net>
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  * $Id: MatrixFunctions.h,v 1.44 2007-06-23 15:59:00 opetzold Exp $
00022  */
00023 
00024 #ifndef TVMET_XPR_MATRIX_FUNCTIONS_H
00025 #define TVMET_XPR_MATRIX_FUNCTIONS_H
00026 
00027 namespace tvmet {
00028 
00029 
00030 /* forwards */
00031 template<class T, std::size_t Rows, std::size_t Cols> class Matrix;
00032 template<class T, std::size_t Sz> class Vector;
00033 template<class E, std::size_t Sz> class XprVector;
00034 template<class E> class XprMatrixTranspose;
00035 template<class E, std::size_t Sz> class XprMatrixDiag;
00036 template<class E, std::size_t Rows, std::size_t Cols> class XprMatrixRow;
00037 template<class E, std::size_t Rows, std::size_t Cols> class XprMatrixCol;
00038 
00039 
00040 /*********************************************************
00041  * PART I: DECLARATION
00042  *********************************************************/
00043 
00044 
00045 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00046  * Matrix arithmetic functions add, sub, mul and div
00047  *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
00048 
00049 
00050 /*
00051  * function(XprMatrix<E1, Rows, Cols>, XprMatrix<E2, Rows, Cols>)
00052  */
00053 #define TVMET_DECLARE_MACRO(NAME)         \
00054 template<class E1, class E2, std::size_t Rows, std::size_t Cols>  \
00055 XprMatrix<                \
00056   XprBinOp<               \
00057     Fcnl_##NAME<typename E1::value_type, typename E2::value_type>,  \
00058     XprMatrix<E1, Rows, Cols>,            \
00059     XprMatrix<E2, Rows, Cols>           \
00060   >,                  \
00061   Rows, Cols                \
00062 >                 \
00063 NAME (const XprMatrix<E1, Rows, Cols>& lhs,       \
00064       const XprMatrix<E2, Rows, Cols>& rhs) TVMET_CXX_ALWAYS_INLINE;
00065 
00066 TVMET_DECLARE_MACRO(add)      // per se element wise
00067 TVMET_DECLARE_MACRO(sub)      // per se element wise
00068 namespace element_wise {
00069   TVMET_DECLARE_MACRO(mul)      // not defined for matrizes
00070   TVMET_DECLARE_MACRO(div)      // not defined for matrizes
00071 }
00072 
00073 #undef TVMET_DECLARE_MACRO
00074 
00075 
00076 /*
00077  * function(XprMatrix<E, Rows, Cols>, POD)
00078  * function(POD, XprMatrix<E, Rows, Cols>)
00079  * Note: - operations +,-,*,/ are per se element wise
00080  */
00081 #define TVMET_DECLARE_MACRO(NAME, POD)          \
00082 template<class E, std::size_t Rows, std::size_t Cols>     \
00083 XprMatrix<                \
00084   XprBinOp<               \
00085     Fcnl_##NAME<typename E::value_type, POD >,        \
00086     XprMatrix<E, Rows, Cols>,           \
00087     XprLiteral< POD >             \
00088   >,                  \
00089   Rows, Cols                \
00090 >                 \
00091 NAME (const XprMatrix<E, Rows, Cols>& lhs,        \
00092       POD rhs) TVMET_CXX_ALWAYS_INLINE;         \
00093                   \
00094 template<class E, std::size_t Rows, std::size_t Cols>     \
00095 XprMatrix<                \
00096   XprBinOp<               \
00097     Fcnl_##NAME< POD, typename E::value_type>,        \
00098     XprLiteral< POD >,              \
00099     XprMatrix<E, Rows, Cols>            \
00100   >,                  \
00101   Rows, Cols                \
00102 >                 \
00103 NAME (POD lhs,                \
00104       const XprMatrix<E, Rows, Cols>& rhs) TVMET_CXX_ALWAYS_INLINE;
00105 
00106 TVMET_DECLARE_MACRO(add, int)
00107 TVMET_DECLARE_MACRO(sub, int)
00108 TVMET_DECLARE_MACRO(mul, int)
00109 TVMET_DECLARE_MACRO(div, int)
00110 
00111 #if defined(TVMET_HAVE_LONG_LONG)
00112 TVMET_DECLARE_MACRO(add, long long int)
00113 TVMET_DECLARE_MACRO(sub, long long int)
00114 TVMET_DECLARE_MACRO(mul, long long int)
00115 TVMET_DECLARE_MACRO(div, long long int)
00116 #endif
00117 
00118 TVMET_DECLARE_MACRO(add, float)
00119 TVMET_DECLARE_MACRO(sub, float)
00120 TVMET_DECLARE_MACRO(mul, float)
00121 TVMET_DECLARE_MACRO(div, float)
00122 
00123 TVMET_DECLARE_MACRO(add, double)
00124 TVMET_DECLARE_MACRO(sub, double)
00125 TVMET_DECLARE_MACRO(mul, double)
00126 TVMET_DECLARE_MACRO(div, double)
00127 
00128 #if defined(TVMET_HAVE_LONG_DOUBLE)
00129 TVMET_DECLARE_MACRO(add, long double)
00130 TVMET_DECLARE_MACRO(sub, long double)
00131 TVMET_DECLARE_MACRO(mul, long double)
00132 TVMET_DECLARE_MACRO(div, long double)
00133 #endif
00134 
00135 #undef TVMET_DECLARE_MACRO
00136 
00137 
00138 #if defined(TVMET_HAVE_COMPLEX)
00139 /*
00140  * function(XprMatrix<E, Rows, Cols>, complex<T>)
00141  * function(complex<T>, XprMatrix<E, Rows, Cols>)
00142  * Note: - operations +,-,*,/ are per se element wise
00143  * \todo type promotion
00144  */
00145 #define TVMET_DECLARE_MACRO(NAME)         \
00146 template<class E, class T, std::size_t Rows, std::size_t Cols>    \
00147 XprMatrix<                \
00148   XprBinOp<               \
00149     Fcnl_##NAME<typename E::value_type, std::complex<T> >,    \
00150     XprMatrix<E, Rows, Cols>,           \
00151     XprLiteral< std::complex<T> >         \
00152   >,                  \
00153   Rows, Cols                \
00154 >                 \
00155 NAME (const XprMatrix<E, Rows, Cols>& lhs,        \
00156       const std::complex<T>& rhs) TVMET_CXX_ALWAYS_INLINE;    \
00157                   \
00158 template<class T, class E, std::size_t Rows, std::size_t Cols>    \
00159 XprMatrix<                \
00160   XprBinOp<               \
00161     Fcnl_##NAME< std::complex<T>, typename E::value_type>,    \
00162     XprLiteral< std::complex<T> >,          \
00163     XprMatrix<E, Rows, Cols>            \
00164   >,                  \
00165   Rows, Cols                \
00166 >                 \
00167 NAME (const std::complex<T>& lhs,         \
00168       const XprMatrix<E, Rows, Cols>& rhs) TVMET_CXX_ALWAYS_INLINE;
00169 
00170 TVMET_DECLARE_MACRO(add)
00171 TVMET_DECLARE_MACRO(sub)
00172 TVMET_DECLARE_MACRO(mul)
00173 TVMET_DECLARE_MACRO(div)
00174 
00175 #undef TVMET_DECLARE_MACRO
00176 
00177 #endif // defined(TVMET_HAVE_COMPLEX)
00178 
00179 
00180 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00181  * matrix prod( ... ) functions
00182  *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
00183 
00184 
00185 template<class E1, std::size_t Rows1, std::size_t Cols1,
00186    class E2, std::size_t Cols2>
00187 XprMatrix<
00188   XprMMProduct<
00189     XprMatrix<E1, Rows1, Cols1>, Rows1, Cols1,  // M1(Rows1, Cols1)
00190     XprMatrix<E2, Cols1, Cols2>, Cols2
00191   >,
00192   Rows1, Cols2          // return Dim
00193 >
00194 prod(const XprMatrix<E1, Rows1, Cols1>& lhs,
00195      const XprMatrix<E2, Cols1, Cols2>& rhs) TVMET_CXX_ALWAYS_INLINE;
00196 
00197 
00198 template<class E1, std::size_t Rows1, std::size_t Cols1,
00199    class E2, std::size_t Cols2>
00200 XprMatrix<
00201   XprMMProductTransposed<
00202     XprMatrix<E1, Rows1, Cols1>, Rows1, Cols1,  // M1(Rows1, Cols1)
00203     XprMatrix<E2, Cols1, Cols2>, Cols2    // M2(Cols1, Cols2)
00204   >,
00205   Cols2, Rows1          // return Dim
00206 >
00207 trans_prod(const XprMatrix<E1, Rows1, Cols1>& lhs,
00208      const XprMatrix<E2, Cols1, Cols2>& rhs) TVMET_CXX_ALWAYS_INLINE;
00209 
00210 
00211 template<class E1, std::size_t Rows1, std::size_t Cols1,
00212    class E2, std::size_t Cols2> // Rows2 = Rows1
00213 XprMatrix<
00214   XprMtMProduct<
00215     XprMatrix<E1, Rows1, Cols1>, Rows1, Cols1,  // M1(Rows1, Cols1)
00216     XprMatrix<E2, Rows1, Cols2>, Cols2    // M2(Rows1, Cols2)
00217   >,
00218   Cols1, Cols2          // return Dim
00219 >
00220 MtM_prod(const XprMatrix<E1, Rows1, Cols1>& lhs,
00221    const XprMatrix<E2, Rows1, Cols2>& rhs) TVMET_CXX_ALWAYS_INLINE;
00222 
00223 
00224 template<class E1, std::size_t Rows1, std::size_t Cols1,
00225    class E2, std::size_t Rows2>     // Cols2 = Cols1
00226 XprMatrix<
00227   XprMMtProduct<
00228     XprMatrix<E1, Rows1, Cols1>, Rows1, Cols1,  // M1(Rows1, Cols1)
00229     XprMatrix<E2, Rows2, Cols1>, Cols1    // M2(Rows2, Cols1)
00230   >,
00231   Rows1, Rows2          // return Dim
00232 >
00233 MMt_prod(const XprMatrix<E1, Rows1, Cols1>& lhs,
00234    const XprMatrix<E2, Rows2, Cols1>& rhs) TVMET_CXX_ALWAYS_INLINE;
00235 
00236 
00237 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00238  * matrix-vector specific prod( ... ) functions
00239  *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
00240 
00241 
00242 template<class E1, std::size_t Rows, std::size_t Cols,
00243    class E2>
00244 XprVector<
00245   XprMVProduct<
00246     XprMatrix<E1, Rows, Cols>, Rows, Cols,
00247     XprVector<E2, Cols>
00248   >,
00249   Rows
00250 >
00251 prod(const XprMatrix<E1, Rows, Cols>& lhs,
00252      const XprVector<E2, Cols>& rhs) TVMET_CXX_ALWAYS_INLINE;
00253 
00254 
00255 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00256  * matrix specific functions
00257  *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
00258 
00259 
00260 template<class E, std::size_t Rows, std::size_t Cols>
00261 XprMatrix<
00262   XprMatrixTranspose<
00263     XprMatrix<E, Rows, Cols>
00264   >,
00265   Cols, Rows
00266 >
00267 trans(const XprMatrix<E, Rows, Cols>& rhs) TVMET_CXX_ALWAYS_INLINE;
00268 
00269 
00270 template<class E, std::size_t Sz>
00271 typename NumericTraits<typename E::value_type>::sum_type
00272 trace(const XprMatrix<E, Sz, Sz>& m) TVMET_CXX_ALWAYS_INLINE;
00273 
00274 
00275 template<class E, std::size_t Rows, std::size_t Cols>
00276 XprVector<
00277   XprMatrixRow<
00278     XprMatrix<E, Rows, Cols>,
00279     Rows, Cols
00280   >,
00281   Cols
00282 >
00283 row(const XprMatrix<E, Rows, Cols>& m,
00284     std::size_t no) TVMET_CXX_ALWAYS_INLINE;
00285 
00286 
00287 template<class E, std::size_t Rows, std::size_t Cols>
00288 XprVector<
00289   XprMatrixCol<
00290     XprMatrix<E, Rows, Cols>,
00291     Rows, Cols
00292   >,
00293   Rows
00294 >
00295 col(const XprMatrix<E, Rows, Cols>& m, std::size_t no) TVMET_CXX_ALWAYS_INLINE;
00296 
00297 
00298 template<class E, std::size_t Sz>
00299 XprVector<
00300   XprMatrixDiag<
00301     XprMatrix<E, Sz, Sz>,
00302     Sz
00303   >,
00304   Sz
00305 >
00306 diag(const XprMatrix<E, Sz, Sz>& m) TVMET_CXX_ALWAYS_INLINE;
00307 
00308 
00309 /*********************************************************
00310  * PART II: IMPLEMENTATION
00311  *********************************************************/
00312 
00313 
00314 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00315  * Matrix arithmetic functions add, sub, mul and div
00316  *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
00317 
00318 
00319 /*
00320  * function(XprMatrix<E1, Rows, Cols>, XprMatrix<E2, Rows, Cols>)
00321  */
00322 #define TVMET_IMPLEMENT_MACRO(NAME)         \
00323 template<class E1, class E2, std::size_t Rows, std::size_t Cols>  \
00324 inline                  \
00325 XprMatrix<                \
00326   XprBinOp<               \
00327     Fcnl_##NAME<typename E1::value_type, typename E2::value_type>,  \
00328     XprMatrix<E1, Rows, Cols>,            \
00329     XprMatrix<E2, Rows, Cols>           \
00330   >,                  \
00331   Rows, Cols                \
00332 >                 \
00333 NAME (const XprMatrix<E1, Rows, Cols>& lhs,         \
00334       const XprMatrix<E2, Rows, Cols>& rhs) {       \
00335   typedef XprBinOp<             \
00336     Fcnl_##NAME<typename E1::value_type, typename E2::value_type>,  \
00337     XprMatrix<E1, Rows, Cols>,            \
00338     XprMatrix<E2, Rows, Cols>           \
00339   >                expr_type; \
00340   return XprMatrix<expr_type, Rows, Cols>(expr_type(lhs, rhs));   \
00341 }
00342 
00343 TVMET_IMPLEMENT_MACRO(add)      // per se element wise
00344 TVMET_IMPLEMENT_MACRO(sub)      // per se element wise
00345 namespace element_wise {
00346   TVMET_IMPLEMENT_MACRO(mul)      // not defined for matrizes
00347   TVMET_IMPLEMENT_MACRO(div)      // not defined for matrizes
00348 }
00349 
00350 #undef TVMET_IMPLEMENT_MACRO
00351 
00352 
00353 /*
00354  * function(XprMatrix<E, Rows, Cols>, POD)
00355  * function(POD, XprMatrix<E, Rows, Cols>)
00356  * Note: - operations +,-,*,/ are per se element wise
00357  */
00358 #define TVMET_IMPLEMENT_MACRO(NAME, POD)        \
00359 template<class E, std::size_t Rows, std::size_t Cols>     \
00360 inline                  \
00361 XprMatrix<                \
00362   XprBinOp<               \
00363     Fcnl_##NAME<typename E::value_type, POD >,        \
00364     XprMatrix<E, Rows, Cols>,           \
00365     XprLiteral< POD >             \
00366   >,                  \
00367   Rows, Cols                \
00368 >                 \
00369 NAME (const XprMatrix<E, Rows, Cols>& lhs, POD rhs) {     \
00370   typedef XprBinOp<             \
00371     Fcnl_##NAME<typename E::value_type, POD >,        \
00372     XprMatrix<E, Rows, Cols>,           \
00373     XprLiteral< POD >             \
00374   >             expr_type;  \
00375   return XprMatrix<expr_type, Rows, Cols>(        \
00376     expr_type(lhs, XprLiteral< POD >(rhs)));        \
00377 }                 \
00378                   \
00379 template<class E, std::size_t Rows, std::size_t Cols>     \
00380 inline                  \
00381 XprMatrix<                \
00382   XprBinOp<               \
00383     Fcnl_##NAME< POD, typename E::value_type>,        \
00384     XprLiteral< POD >,              \
00385     XprMatrix<E, Rows, Cols>            \
00386   >,                  \
00387   Rows, Cols                \
00388 >                 \
00389 NAME (POD lhs, const XprMatrix<E, Rows, Cols>& rhs) {     \
00390   typedef XprBinOp<             \
00391     Fcnl_##NAME< POD, typename E::value_type>,        \
00392     XprLiteral< POD >,              \
00393     XprMatrix<E, Rows, Cols>            \
00394   >             expr_type;  \
00395   return XprMatrix<expr_type, Rows, Cols>(        \
00396     expr_type(XprLiteral< POD >(lhs), rhs));        \
00397 }
00398 
00399 TVMET_IMPLEMENT_MACRO(add, int)
00400 TVMET_IMPLEMENT_MACRO(sub, int)
00401 TVMET_IMPLEMENT_MACRO(mul, int)
00402 TVMET_IMPLEMENT_MACRO(div, int)
00403 
00404 #if defined(TVMET_HAVE_LONG_LONG)
00405 TVMET_IMPLEMENT_MACRO(add, long long int)
00406 TVMET_IMPLEMENT_MACRO(sub, long long int)
00407 TVMET_IMPLEMENT_MACRO(mul, long long int)
00408 TVMET_IMPLEMENT_MACRO(div, long long int)
00409 #endif
00410 
00411 TVMET_IMPLEMENT_MACRO(add, float)
00412 TVMET_IMPLEMENT_MACRO(sub, float)
00413 TVMET_IMPLEMENT_MACRO(mul, float)
00414 TVMET_IMPLEMENT_MACRO(div, float)
00415 
00416 TVMET_IMPLEMENT_MACRO(add, double)
00417 TVMET_IMPLEMENT_MACRO(sub, double)
00418 TVMET_IMPLEMENT_MACRO(mul, double)
00419 TVMET_IMPLEMENT_MACRO(div, double)
00420 
00421 #if defined(TVMET_HAVE_LONG_DOUBLE)
00422 TVMET_IMPLEMENT_MACRO(add, long double)
00423 TVMET_IMPLEMENT_MACRO(sub, long double)
00424 TVMET_IMPLEMENT_MACRO(mul, long double)
00425 TVMET_IMPLEMENT_MACRO(div, long double)
00426 #endif
00427 
00428 #undef TVMET_IMPLEMENT_MACRO
00429 
00430 
00431 #if defined(TVMET_HAVE_COMPLEX)
00432 /*
00433  * function(XprMatrix<E, Rows, Cols>, complex<T>)
00434  * function(complex<T>, XprMatrix<E, Rows, Cols>)
00435  * Note: - operations +,-,*,/ are per se element wise
00436  * \todo type promotion
00437  */
00438 #define TVMET_IMPLEMENT_MACRO(NAME)         \
00439 template<class E, class T, std::size_t Rows, std::size_t Cols>    \
00440 inline                  \
00441 XprMatrix<                \
00442   XprBinOp<               \
00443     Fcnl_##NAME<typename E::value_type, std::complex<T> >,    \
00444     XprMatrix<E, Rows, Cols>,           \
00445     XprLiteral< std::complex<T> >         \
00446   >,                  \
00447   Rows, Cols                \
00448 >                 \
00449 NAME (const XprMatrix<E, Rows, Cols>& lhs,        \
00450       const std::complex<T>& rhs) {         \
00451   typedef XprBinOp<             \
00452     Fcnl_##NAME<typename E::value_type, std::complex<T> >,    \
00453     XprMatrix<E, Rows, Cols>,           \
00454     XprLiteral< std::complex<T> >         \
00455   >             expr_type;  \
00456   return XprMatrix<expr_type, Rows, Cols>(        \
00457     expr_type(lhs, XprLiteral< std::complex<T> >(rhs)));    \
00458 }                 \
00459                   \
00460 template<class T, class E, std::size_t Rows, std::size_t Cols>    \
00461 inline                  \
00462 XprMatrix<                \
00463   XprBinOp<               \
00464     Fcnl_##NAME< std::complex<T>, typename E::value_type>,    \
00465     XprLiteral< std::complex<T> >,          \
00466     XprMatrix<E, Rows, Cols>            \
00467   >,                  \
00468   Rows, Cols                \
00469 >                 \
00470 NAME (const std::complex<T>& lhs,           \
00471       const XprMatrix<E, Rows, Cols>& rhs) {        \
00472   typedef XprBinOp<             \
00473     Fcnl_##NAME< std::complex<T>, typename E::value_type>,    \
00474     XprLiteral< std::complex<T> >,          \
00475     XprMatrix<E, Rows, Cols>            \
00476   >             expr_type;  \
00477   return XprMatrix<expr_type, Rows, Cols>(        \
00478     expr_type(XprLiteral< std::complex<T> >(lhs), rhs));    \
00479 }
00480 
00481 TVMET_IMPLEMENT_MACRO(add)
00482 TVMET_IMPLEMENT_MACRO(sub)
00483 TVMET_IMPLEMENT_MACRO(mul)
00484 TVMET_IMPLEMENT_MACRO(div)
00485 
00486 #undef TVMET_IMPLEMENT_MACRO
00487 
00488 #endif // defined(TVMET_HAVE_COMPLEX)
00489 
00490 
00491 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00492  * matrix prod( ... ) functions
00493  *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
00494 
00495 
00506 template<class E1, std::size_t Rows1, std::size_t Cols1,
00507    class E2, std::size_t Cols2>
00508 inline
00509 XprMatrix<
00510   XprMMProduct<
00511     XprMatrix<E1, Rows1, Cols1>, Rows1, Cols1,  // M1(Rows1, Cols1)
00512     XprMatrix<E2, Cols1, Cols2>, Cols2
00513   >,
00514   Rows1, Cols2          // return Dim
00515 >
00516 prod(const XprMatrix<E1, Rows1, Cols1>& lhs, const XprMatrix<E2, Cols1, Cols2>& rhs) {
00517   typedef XprMMProduct<
00518     XprMatrix<E1, Rows1, Cols1>, Rows1, Cols1,
00519     XprMatrix<E2, Cols1, Cols2>, Cols2
00520   >             expr_type;
00521   return XprMatrix<expr_type, Rows1, Cols2>(expr_type(lhs, rhs));
00522 }
00523 
00524 
00535 template<class E1, std::size_t Rows1, std::size_t Cols1,
00536    class E2, std::size_t Cols2>
00537 inline
00538 XprMatrix<
00539   XprMMProductTransposed<
00540     XprMatrix<E1, Rows1, Cols1>, Rows1, Cols1,  // M1(Rows1, Cols1)
00541     XprMatrix<E2, Cols1, Cols2>, Cols2    // M2(Cols1, Cols2)
00542   >,
00543   Cols2, Rows1          // return Dim
00544 >
00545 trans_prod(const XprMatrix<E1, Rows1, Cols1>& lhs, const XprMatrix<E2, Cols1, Cols2>& rhs) {
00546   typedef XprMMProductTransposed<
00547     XprMatrix<E1, Rows1, Cols1>, Rows1, Cols1,
00548     XprMatrix<E2, Cols1, Cols2>, Cols2
00549   >             expr_type;
00550   return XprMatrix<expr_type, Cols2, Rows1>(expr_type(lhs, rhs));
00551 }
00552 
00553 
00566 template<class E1, std::size_t Rows1, std::size_t Cols1,
00567    class E2, std::size_t Cols2> // Rows2 = Rows1
00568 inline
00569 XprMatrix<
00570   XprMtMProduct<
00571     XprMatrix<E1, Rows1, Cols1>, Rows1, Cols1,  // M1(Rows1, Cols1)
00572     XprMatrix<E2, Rows1, Cols2>, Cols2    // M2(Rows1, Cols2)
00573   >,
00574   Cols1, Cols2          // return Dim
00575 >
00576 MtM_prod(const XprMatrix<E1, Rows1, Cols1>& lhs, const XprMatrix<E2, Rows1, Cols2>& rhs) {
00577   typedef XprMtMProduct<
00578     XprMatrix<E1, Rows1, Cols1>, Rows1, Cols1,
00579     XprMatrix<E2, Rows1, Cols2>, Cols2
00580   >             expr_type;
00581   return XprMatrix<expr_type, Cols1, Cols2>(expr_type(lhs, rhs));
00582 }
00583 
00584 
00591 template<class E1, std::size_t Rows1, std::size_t Cols1,
00592    class E2, std::size_t Rows2> // Cols2 = Cols1
00593 inline
00594 XprMatrix<
00595   XprMMtProduct<
00596     XprMatrix<E1, Rows1, Cols1>, Rows1, Cols1,  // M1(Rows1, Cols1)
00597     XprMatrix<E2, Rows2, Cols1>, Cols1    // M2(Rows2, Cols1)
00598   >,
00599   Rows1, Rows2          // return Dim
00600 >
00601 MMt_prod(const XprMatrix<E1, Rows1, Cols1>& lhs, const XprMatrix<E2, Rows2, Cols1>& rhs) {
00602   typedef XprMMtProduct<
00603     XprMatrix<E1, Rows1, Cols1>, Rows1, Cols1,
00604     XprMatrix<E2, Rows2, Cols1>, Cols1
00605   >             expr_type;
00606   return XprMatrix<expr_type, Rows1, Rows2>(expr_type(lhs, rhs));
00607 }
00608 
00609 
00610 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00611  * matrix-vector specific prod( ... ) functions
00612  *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
00613 
00614 
00620 template<class E1, std::size_t Rows, std::size_t Cols,
00621    class E2>
00622 inline
00623 XprVector<
00624   XprMVProduct<
00625     XprMatrix<E1, Rows, Cols>, Rows, Cols,
00626     XprVector<E2, Cols>
00627   >,
00628   Rows
00629 >
00630 prod(const XprMatrix<E1, Rows, Cols>& lhs, const XprVector<E2, Cols>& rhs) {
00631   typedef XprMVProduct<
00632     XprMatrix<E1, Rows, Cols>, Rows, Cols,
00633     XprVector<E2, Cols>
00634   >             expr_type;
00635   return XprVector<expr_type, Rows>(expr_type(lhs, rhs));
00636 }
00637 
00638 
00639 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00640  * matrix specific functions
00641  *+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
00642 
00643 
00649 template<class E, std::size_t Rows, std::size_t Cols>
00650 inline
00651 XprMatrix<
00652   XprMatrixTranspose<
00653     XprMatrix<E, Rows, Cols>
00654   >,
00655   Cols, Rows
00656 >
00657 trans(const XprMatrix<E, Rows, Cols>& rhs) {
00658   typedef XprMatrixTranspose<
00659     XprMatrix<E, Rows, Cols>
00660   >             expr_type;
00661   return XprMatrix<expr_type, Cols, Rows>(expr_type(rhs));
00662 }
00663 
00664 
00665 /*
00666  * \fn trace(const XprMatrix<E, Sz, Sz>& m)
00667  * \brief Compute the trace of a square matrix.
00668  * \ingroup _unary_function
00669  *
00670  * Simply compute the trace of the given matrix expression as:
00671  * \f[
00672  *  \sum_{k = 0}^{Sz-1} m(k, k)
00673  * \f]
00674  */
00675 template<class E, std::size_t Sz>
00676 inline
00677 typename NumericTraits<typename E::value_type>::sum_type
00678 trace(const XprMatrix<E, Sz, Sz>& m) {
00679   return meta::Matrix<Sz, Sz, 0, 0>::trace(m);
00680 }
00681 
00682 
00688 template<class E, std::size_t Rows, std::size_t Cols>
00689 inline
00690 XprVector<
00691   XprMatrixRow<
00692     XprMatrix<E, Rows, Cols>,
00693     Rows, Cols
00694   >,
00695   Cols
00696 >
00697 row(const XprMatrix<E, Rows, Cols>& m, std::size_t no) {
00698   typedef XprMatrixRow<
00699     XprMatrix<E, Rows, Cols>,
00700     Rows, Cols
00701   >             expr_type;
00702 
00703   return XprVector<expr_type, Cols>(expr_type(m, no));
00704 }
00705 
00706 
00712 template<class E, std::size_t Rows, std::size_t Cols>
00713 inline
00714 XprVector<
00715   XprMatrixCol<
00716     XprMatrix<E, Rows, Cols>,
00717     Rows, Cols
00718   >,
00719   Rows
00720 >
00721 col(const XprMatrix<E, Rows, Cols>& m, std::size_t no) {
00722   typedef XprMatrixCol<
00723     XprMatrix<E, Rows, Cols>,
00724     Rows, Cols
00725   >             expr_type;
00726 
00727   return XprVector<expr_type, Cols>(expr_type(m, no));
00728 }
00729 
00730 
00736 template<class E, std::size_t Sz>
00737 inline
00738 XprVector<
00739   XprMatrixDiag<
00740     XprMatrix<E, Sz, Sz>,
00741     Sz
00742   >,
00743   Sz
00744 >
00745 diag(const XprMatrix<E, Sz, Sz>& m) {
00746   typedef XprMatrixDiag<
00747     XprMatrix<E, Sz, Sz>,
00748   Sz>             expr_type;
00749 
00750   return XprVector<expr_type, Sz>(expr_type(m));
00751 }
00752 
00753 
00754 } // namespace tvmet
00755 
00756 #endif // TVMET_XPR_MATRIX_FUNCTIONS_H
00757 
00758 // Local Variables:
00759 // mode:C++
00760 // tab-width:8
00761 // End:

Author: