ICRS Eurobot 2013

Dependencies:   mbed mbed-rtos Servo QEI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Gemmt.h Source File

Gemmt.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: Gemmt.h,v 1.9 2007-06-23 15:58:59 opetzold Exp $
00022  */
00023 
00024 #ifndef TVMET_LOOP_GEMMT_H
00025 #define TVMET_LOOP_GEMMT_H
00026 
00027 namespace tvmet {
00028 
00029 namespace loop {
00030 
00031 
00032 /**
00033  * \class gemmt Gemmt.h "tvmet/loop/Gemmt.h"
00034  * \brief class for for product matrix-transpose(matrix) operations.
00035  *        using formula
00036  *        \f[
00037  *        M_1\,M_2^{T}
00038  *        \f]
00039  * \par Example:
00040  * \code
00041  * template<class T, std::size_t Rows1, std::size_t Cols1, std::size_t Cols2>
00042  * inline
00043  * void
00044  * prod(const Matrix<T, Rows1, Cols1>& lhs, const Matrix<T, Rows2, Cols1>& rhs,
00045  *     Matrix<T, Rows1, Rows2>& dest)
00046  * {
00047  *   for (std::size_t i = 0; i != Rows1; ++i) {
00048  *     for (std::size_t j = 0; j != Rows2; ++j) {
00049  *       dest(i, j) = tvmet::loop::gemmt<Rows1, Cols1, Cols1>().prod(lhs, rhs, i, j);
00050  *     }
00051  *   }
00052  * }
00053  * \endcode
00054  * \note The number of cols of rhs matrix have to be equal to cols of rhs matrix.
00055  *       The result is a (Rows1 x Rows2) matrix.
00056  */
00057 template<std::size_t Rows1, std::size_t Cols1,
00058      std::size_t Cols2 /* unused */>
00059 class gemmt
00060 {
00061   gemmt(const gemmt&);
00062   gemmt& operator=(const gemmt&);
00063 
00064 private:
00065   enum {
00066     count     = Cols1,
00067     N         = (count+7)/8
00068   };
00069 
00070 public:
00071   gemmt() { }
00072 
00073 public:
00074   template<class E1, class E2>
00075   static inline
00076   typename PromoteTraits<
00077     typename E1::value_type,
00078     typename E2::value_type
00079     >::value_type
00080   prod(const E1& lhs, const E2& rhs, std::size_t i, std::size_t j) {
00081     typename PromoteTraits<
00082       typename E1::value_type,
00083       typename E2::value_type
00084     >::value_type                  sum(0);
00085     std::size_t                 k(0);
00086     std::size_t                 n(N);
00087 
00088     // Duff's device
00089     switch(count % 8) {
00090     case 0: do { sum += lhs(i, k) * rhs(j, k); ++k;
00091     case 7:      sum += lhs(i, k) * rhs(j, k); ++k;
00092     case 6:      sum += lhs(i, k) * rhs(j, k); ++k;
00093     case 5:      sum += lhs(i, k) * rhs(j, k); ++k;
00094     case 4:      sum += lhs(i, k) * rhs(j, k); ++k;
00095     case 3:      sum += lhs(i, k) * rhs(j, k); ++k;
00096     case 2:      sum += lhs(i, k) * rhs(j, k); ++k;
00097     case 1:      sum += lhs(i, k) * rhs(j, k); ++k;
00098             } while(--n != 0);
00099     }
00100 
00101     return sum;
00102   }
00103 };
00104 
00105 
00106 } // namespace loop
00107 
00108 } // namespace tvmet
00109 
00110 #endif /* TVMET_LOOP_GEMMT_H */
00111 
00112 // Local Variables:
00113 // mode:C++
00114 // tab-width:8
00115 // End: