ICRS Eurobot 2013

Dependencies:   mbed mbed-rtos Servo QEI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MVProduct.h Source File

MVProduct.h

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: MVProduct.h,v 1.21 2007-06-23 15:59:00 opetzold Exp $
00022  */
00023 
00024 #ifndef TVMET_XPR_MVPRODUCT_H
00025 #define TVMET_XPR_MVPRODUCT_H
00026 
00027 #include <tvmet/meta/Gemv.h>
00028 #include <tvmet/loop/Gemv.h>
00029 
00030 namespace tvmet {
00031 
00032 
00033 /**
00034  * \class XprMVProduct MVProduct.h "tvmet/xpr/MVProduct.h"
00035  * \brief Expression for matrix-vector product
00036  *        using formula
00037  *        \f[
00038  *        M\,v
00039  *        \f]
00040  */
00041 template<class E1, std::size_t Rows, std::size_t Cols,
00042      class E2>
00043 class XprMVProduct
00044   : public TvmetBase< XprMVProduct<E1, Rows, Cols, E2> >
00045 {
00046   XprMVProduct();
00047   XprMVProduct& operator=(const XprMVProduct&);
00048 
00049 public:
00050   typedef typename PromoteTraits<
00051     typename E1::value_type,
00052     typename E2::value_type
00053   >::value_type                            value_type;
00054 
00055 public:
00056   /** Complexity counter. */
00057   enum {
00058     ops_lhs   = E1::ops,
00059     ops_rhs   = E2::ops,
00060     M         = Rows * Cols,
00061     N         = Rows * (Cols - 1),
00062     ops_plus  = M * NumericTraits<value_type>::ops_plus,
00063     ops_muls  = N * NumericTraits<value_type>::ops_muls,
00064     ops       = ops_plus + ops_muls,
00065     use_meta  = Rows*Cols < TVMET_COMPLEXITY_MV_TRIGGER ? true : false
00066   };
00067 
00068 public:
00069   /** Constructor. */
00070   explicit XprMVProduct(const E1& lhs, const E2& rhs)
00071     : m_lhs(lhs), m_rhs(rhs)
00072   { }
00073 
00074   /** Copy Constructor. Not explicit! */
00075 #if defined(TVMET_OPTIMIZE_XPR_MANUAL_CCTOR)
00076   XprMVProduct(const XprMVProduct& e)
00077     : m_lhs(e.m_lhs), m_rhs(e.m_rhs)
00078   { }
00079 #endif
00080 
00081 private:
00082   /** Wrapper for meta gemm. */
00083   static inline
00084   value_type do_gemv(dispatch<true>, const E1& lhs, const E2& rhs, std::size_t j) {
00085     return meta::gemv<Rows, Cols,
00086                       0>::prod(lhs, rhs, j);
00087   }
00088 
00089   /** Wrapper for loop gemm. */
00090   static inline
00091   value_type do_gemv(dispatch<false>, const E1& lhs, const E2& rhs, std::size_t j) {
00092     return loop::gemv<Rows, Cols>::prod(lhs, rhs, j);
00093   }
00094 
00095 public:
00096   /** index operator, returns the expression by index. This is the vector
00097       style since a matrix*vector gives a vector. */
00098   value_type operator()(std::size_t j) const {
00099     TVMET_RT_CONDITION(j < Rows , "XprMVProduct Bounce Violation")
00100     return do_gemv(dispatch<use_meta>(), m_lhs, m_rhs, j);
00101   }
00102 
00103 public: // debugging Xpr parse tree
00104   void print_xpr(std::ostream& os, std::size_t l=0) const {
00105     os << IndentLevel(l++)
00106        << "XprMVProduct["
00107        << (use_meta ? "M" :  "L") << ", O=" << ops
00108        << ", (O1=" << ops_lhs << ", O2=" << ops_rhs << ")]<"
00109        << std::endl;
00110     m_lhs.print_xpr(os, l);
00111     os << IndentLevel(l)
00112        << "R=" << Rows << ", C=" << Cols << ",\n";
00113     m_rhs.print_xpr(os, l);
00114     os << IndentLevel(--l)
00115        << ">," << std::endl;
00116   }
00117 
00118 private:
00119   const E1                            m_lhs;
00120   const E2                            m_rhs;
00121 };
00122 
00123 
00124 } // namespace tvmet
00125 
00126 #endif // TVMET_XPR_MVPRODUCT_H
00127 
00128 // Local Variables:
00129 // mode:C++
00130 // tab-width:8
00131 // End: