ICRS Eurobot 2013

Dependencies:   mbed mbed-rtos Servo QEI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MtMProduct.h Source File

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