Eurobot2012_Secondary

Fork of Eurobot_2012_Secondary by Shuto Naruse

Committer:
narshu
Date:
Wed Oct 17 22:25:31 2012 +0000
Revision:
1:cc2a9eb0bd55
Commit before publishing

Who changed what in which revision?

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