Eurobot2012_Primary

Dependencies:   mbed Eurobot_2012_Primary

Committer:
narshu
Date:
Wed Oct 17 22:22:47 2012 +0000
Revision:
26:0995f61cb7b8
Parent:
25:143b19c1fb05
Eurobot 2012 Primary;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
narshu 25:143b19c1fb05 1 /*
narshu 25:143b19c1fb05 2 * Tiny Vector Matrix Library
narshu 25:143b19c1fb05 3 * Dense Vector Matrix Libary of Tiny size using Expression Templates
narshu 25:143b19c1fb05 4 *
narshu 25:143b19c1fb05 5 * Copyright (C) 2001 - 2007 Olaf Petzold <opetzold@users.sourceforge.net>
narshu 25:143b19c1fb05 6 *
narshu 25:143b19c1fb05 7 * This library is free software; you can redistribute it and/or
narshu 25:143b19c1fb05 8 * modify it under the terms of the GNU Lesser General Public
narshu 25:143b19c1fb05 9 * License as published by the Free Software Foundation; either
narshu 25:143b19c1fb05 10 * version 2.1 of the License, or (at your option) any later version.
narshu 25:143b19c1fb05 11 *
narshu 25:143b19c1fb05 12 * This library is distributed in the hope that it will be useful,
narshu 25:143b19c1fb05 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
narshu 25:143b19c1fb05 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
narshu 25:143b19c1fb05 15 * Lesser General Public License for more details.
narshu 25:143b19c1fb05 16 *
narshu 25:143b19c1fb05 17 * You should have received a copy of the GNU Lesser General Public
narshu 25:143b19c1fb05 18 * License along with this library; if not, write to the Free Software
narshu 25:143b19c1fb05 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
narshu 25:143b19c1fb05 20 *
narshu 25:143b19c1fb05 21 * $Id: MMtProduct.h,v 1.20 2007-06-23 15:58:59 opetzold Exp $
narshu 25:143b19c1fb05 22 */
narshu 25:143b19c1fb05 23
narshu 25:143b19c1fb05 24 #ifndef TVMET_XPR_MMTPRODUCT_H
narshu 25:143b19c1fb05 25 #define TVMET_XPR_MMTPRODUCT_H
narshu 25:143b19c1fb05 26
narshu 25:143b19c1fb05 27 #include <tvmet/meta/Gemmt.h>
narshu 25:143b19c1fb05 28 #include <tvmet/loop/Gemmt.h>
narshu 25:143b19c1fb05 29
narshu 25:143b19c1fb05 30 namespace tvmet {
narshu 25:143b19c1fb05 31
narshu 25:143b19c1fb05 32
narshu 25:143b19c1fb05 33 /**
narshu 25:143b19c1fb05 34 * \class XprMMtProduct MMtProduct.h "tvmet/xpr/MMtProduct.h"
narshu 25:143b19c1fb05 35 * \brief Expression for matrix-matrix product.
narshu 25:143b19c1fb05 36 * Using formula:
narshu 25:143b19c1fb05 37 * \f[
narshu 25:143b19c1fb05 38 * M_1\,M_2^T
narshu 25:143b19c1fb05 39 * \f]
narshu 25:143b19c1fb05 40 * \note The number of cols of rhs matrix have to be equal to cols of rhs matrix.
narshu 25:143b19c1fb05 41 * The result is a (Rows1 x Rows2) matrix.
narshu 25:143b19c1fb05 42 */
narshu 25:143b19c1fb05 43 template<class E1, std::size_t Rows1, std::size_t Cols1,
narshu 25:143b19c1fb05 44 class E2, std::size_t Cols2>
narshu 25:143b19c1fb05 45 class XprMMtProduct
narshu 25:143b19c1fb05 46 : public TvmetBase< XprMMtProduct<E1, Rows1, Cols1, E2, Cols2> >
narshu 25:143b19c1fb05 47 {
narshu 25:143b19c1fb05 48 private:
narshu 25:143b19c1fb05 49 XprMMtProduct();
narshu 25:143b19c1fb05 50 XprMMtProduct& operator=(const XprMMtProduct&);
narshu 25:143b19c1fb05 51
narshu 25:143b19c1fb05 52 public:
narshu 25:143b19c1fb05 53 typedef typename PromoteTraits<
narshu 25:143b19c1fb05 54 typename E1::value_type,
narshu 25:143b19c1fb05 55 typename E2::value_type
narshu 25:143b19c1fb05 56 >::value_type value_type;
narshu 25:143b19c1fb05 57
narshu 25:143b19c1fb05 58 public:
narshu 25:143b19c1fb05 59 /** Complexity counter. */
narshu 25:143b19c1fb05 60 enum {
narshu 25:143b19c1fb05 61 ops_lhs = E1::ops,
narshu 25:143b19c1fb05 62 ops_rhs = E2::ops,
narshu 25:143b19c1fb05 63 Rows2 = Cols1,
narshu 25:143b19c1fb05 64 M = Rows1 * Cols1 * Rows1,
narshu 25:143b19c1fb05 65 N = Rows1 * (Cols1 - 1) * Rows2,
narshu 25:143b19c1fb05 66 ops_plus = M * NumericTraits<value_type>::ops_plus,
narshu 25:143b19c1fb05 67 ops_muls = N * NumericTraits<value_type>::ops_muls,
narshu 25:143b19c1fb05 68 ops = ops_plus + ops_muls,
narshu 25:143b19c1fb05 69 use_meta = Rows1*Rows2 < TVMET_COMPLEXITY_MM_TRIGGER ? true : false
narshu 25:143b19c1fb05 70 };
narshu 25:143b19c1fb05 71
narshu 25:143b19c1fb05 72 public:
narshu 25:143b19c1fb05 73 /** Constructor. */
narshu 25:143b19c1fb05 74 explicit XprMMtProduct(const E1& lhs, const E2& rhs)
narshu 25:143b19c1fb05 75 : m_lhs(lhs), m_rhs(rhs)
narshu 25:143b19c1fb05 76 { }
narshu 25:143b19c1fb05 77
narshu 25:143b19c1fb05 78 /** Copy Constructor. Not explicit! */
narshu 25:143b19c1fb05 79 #if defined(TVMET_OPTIMIZE_XPR_MANUAL_CCTOR)
narshu 25:143b19c1fb05 80 XprMMtProduct(const XprMMtProduct& e)
narshu 25:143b19c1fb05 81 : m_lhs(e.m_lhs), m_rhs(e.m_rhs)
narshu 25:143b19c1fb05 82 { }
narshu 25:143b19c1fb05 83 #endif
narshu 25:143b19c1fb05 84
narshu 25:143b19c1fb05 85 private:
narshu 25:143b19c1fb05 86 /** Wrapper for meta gemm. */
narshu 25:143b19c1fb05 87 static inline
narshu 25:143b19c1fb05 88 value_type do_gemmt(dispatch<true>, const E1& lhs, const E2& rhs, std::size_t i, std::size_t j) {
narshu 25:143b19c1fb05 89 return meta::gemmt<Rows1, Cols1,
narshu 25:143b19c1fb05 90 Cols2,
narshu 25:143b19c1fb05 91 0>::prod(lhs, rhs, i, j);
narshu 25:143b19c1fb05 92 }
narshu 25:143b19c1fb05 93
narshu 25:143b19c1fb05 94 /** Wrapper for loop gemm. */
narshu 25:143b19c1fb05 95 static inline
narshu 25:143b19c1fb05 96 value_type do_gemmt(dispatch<false>, const E1& lhs, const E2& rhs, std::size_t i, std::size_t j) {
narshu 25:143b19c1fb05 97 return loop::gemmt<Rows1, Cols1, Cols1>::prod(lhs, rhs, i, j);
narshu 25:143b19c1fb05 98 }
narshu 25:143b19c1fb05 99
narshu 25:143b19c1fb05 100 public:
narshu 25:143b19c1fb05 101 /** index operator for arrays/matrices */
narshu 25:143b19c1fb05 102 value_type operator()(std::size_t i, std::size_t j) const {
narshu 25:143b19c1fb05 103 TVMET_RT_CONDITION((i < Rows1) && (j < Rows2), "XprMMtProduct Bounce Violation")
narshu 25:143b19c1fb05 104 return do_gemmt(dispatch<use_meta>(), m_lhs, m_rhs, i, j);
narshu 25:143b19c1fb05 105 }
narshu 25:143b19c1fb05 106
narshu 25:143b19c1fb05 107 public: // debugging Xpr parse tree
narshu 25:143b19c1fb05 108 void print_xpr(std::ostream& os, std::size_t l=0) const {
narshu 25:143b19c1fb05 109 os << IndentLevel(l++)
narshu 25:143b19c1fb05 110 << "XprMMtProduct["
narshu 25:143b19c1fb05 111 << (use_meta ? "M" : "L") << ", O=" << ops
narshu 25:143b19c1fb05 112 << ", (O1=" << ops_lhs << ", O2=" << ops_rhs << ")]<"
narshu 25:143b19c1fb05 113 << std::endl;
narshu 25:143b19c1fb05 114 m_lhs.print_xpr(os, l);
narshu 25:143b19c1fb05 115 os << IndentLevel(l)
narshu 25:143b19c1fb05 116 << "R1=" << Rows1 << ", C1=" << Cols1 << ",\n";
narshu 25:143b19c1fb05 117 m_rhs.print_xpr(os, l);
narshu 25:143b19c1fb05 118 os << IndentLevel(l)
narshu 25:143b19c1fb05 119 << "C2=" << Cols2 << ",\n"
narshu 25:143b19c1fb05 120 << "\n"
narshu 25:143b19c1fb05 121 << IndentLevel(--l)
narshu 25:143b19c1fb05 122 << ">," << std::endl;
narshu 25:143b19c1fb05 123 }
narshu 25:143b19c1fb05 124
narshu 25:143b19c1fb05 125 private:
narshu 25:143b19c1fb05 126 const E1 m_lhs;
narshu 25:143b19c1fb05 127 const E2 m_rhs;
narshu 25:143b19c1fb05 128 };
narshu 25:143b19c1fb05 129
narshu 25:143b19c1fb05 130
narshu 25:143b19c1fb05 131 } // namespace tvmet
narshu 25:143b19c1fb05 132
narshu 25:143b19c1fb05 133 #endif // TVMET_XPR_MMTPRODUCT_H
narshu 25:143b19c1fb05 134
narshu 25:143b19c1fb05 135 // Local Variables:
narshu 25:143b19c1fb05 136 // mode:C++
narshu 25:143b19c1fb05 137 // tab-width:8
narshu 25:143b19c1fb05 138 // End: