Eurobot2012_Primary

Dependencies:   mbed Eurobot_2012_Primary

Committer:
narshu
Date:
Wed Oct 17 22:22:28 2012 +0000
Revision:
25:143b19c1fb05
Commit before publishing;

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: General.h,v 1.13 2007-06-23 15:58:59 opetzold Exp $
narshu 25:143b19c1fb05 22 */
narshu 25:143b19c1fb05 23
narshu 25:143b19c1fb05 24 #ifndef TVMET_UTIL_GENERAL_H
narshu 25:143b19c1fb05 25 #define TVMET_UTIL_GENERAL_H
narshu 25:143b19c1fb05 26
narshu 25:143b19c1fb05 27
narshu 25:143b19c1fb05 28 /** forward */
narshu 25:143b19c1fb05 29 namespace tvmet {
narshu 25:143b19c1fb05 30 template<class T, std::size_t Rows, std::size_t Cols> class Matrix;
narshu 25:143b19c1fb05 31 template<class T, std::size_t Sz> class Vector;
narshu 25:143b19c1fb05 32 }
narshu 25:143b19c1fb05 33
narshu 25:143b19c1fb05 34 namespace tvmet {
narshu 25:143b19c1fb05 35
narshu 25:143b19c1fb05 36 namespace util {
narshu 25:143b19c1fb05 37
narshu 25:143b19c1fb05 38 /*
narshu 25:143b19c1fb05 39 * \defgroup _util_function
narshu 25:143b19c1fb05 40 * \brief Usefull utility functions
narshu 25:143b19c1fb05 41 */
narshu 25:143b19c1fb05 42
narshu 25:143b19c1fb05 43 /**
narshu 25:143b19c1fb05 44 * \fn Gemm(const Matrix<T, Rows, Cols>& m1, const Matrix<T, Rows, Cols>& m2, Matrix<T, Rows, Cols>& m3)
narshu 25:143b19c1fb05 45 * \brief General matrix matrix multiplication using loops.
narshu 25:143b19c1fb05 46 * \ingroup _util_function
narshu 25:143b19c1fb05 47 */
narshu 25:143b19c1fb05 48 template<class T, std::size_t Rows, std::size_t Cols>
narshu 25:143b19c1fb05 49 inline
narshu 25:143b19c1fb05 50 void
narshu 25:143b19c1fb05 51 Gemm(const Matrix<T, Rows, Cols>& m1, const Matrix<T, Rows, Cols>& m2,
narshu 25:143b19c1fb05 52 Matrix<T, Rows, Cols>& m3)
narshu 25:143b19c1fb05 53 {
narshu 25:143b19c1fb05 54 for (std::size_t i = 0; i < Rows; ++i) {
narshu 25:143b19c1fb05 55 for (std::size_t j = 0; j < Cols; ++j) {
narshu 25:143b19c1fb05 56 T sum(0);
narshu 25:143b19c1fb05 57 for (std::size_t k = 0; k < Cols; ++k) {
narshu 25:143b19c1fb05 58 sum += m1(i,k) * m2(k,j);
narshu 25:143b19c1fb05 59 }
narshu 25:143b19c1fb05 60 m3(i,j) = sum;
narshu 25:143b19c1fb05 61 }
narshu 25:143b19c1fb05 62 }
narshu 25:143b19c1fb05 63 }
narshu 25:143b19c1fb05 64
narshu 25:143b19c1fb05 65
narshu 25:143b19c1fb05 66 /**
narshu 25:143b19c1fb05 67 * \fn Gemv(const Matrix<T, Rows, Cols>& m, const Vector<T, Cols>& v, Vector<T, Cols>& v2)
narshu 25:143b19c1fb05 68 * \brief General matrix vector multiplication using loops.
narshu 25:143b19c1fb05 69 * \ingroup _util_function
narshu 25:143b19c1fb05 70 */
narshu 25:143b19c1fb05 71 template<class T, std::size_t Rows, std::size_t Cols>
narshu 25:143b19c1fb05 72 inline
narshu 25:143b19c1fb05 73 void
narshu 25:143b19c1fb05 74 Gemv(const Matrix<T, Rows, Cols>& m, const Vector<T, Cols>& v,
narshu 25:143b19c1fb05 75 Vector<T, Cols>& v2)
narshu 25:143b19c1fb05 76 {
narshu 25:143b19c1fb05 77 for (std::size_t i = 0; i < Rows; ++i){
narshu 25:143b19c1fb05 78 v2(i) = T(0); // clean up before use
narshu 25:143b19c1fb05 79 for (std::size_t j = 0; j < Cols; ++j) {
narshu 25:143b19c1fb05 80 v2(i) += m(i,j) * v(j);
narshu 25:143b19c1fb05 81 }
narshu 25:143b19c1fb05 82 }
narshu 25:143b19c1fb05 83 }
narshu 25:143b19c1fb05 84
narshu 25:143b19c1fb05 85
narshu 25:143b19c1fb05 86 /**
narshu 25:143b19c1fb05 87 * \fn Gevvmul(const Vector<T, Sz>& v1, const Vector<T, Sz>& v2, Vector<T, Sz>& v3)
narshu 25:143b19c1fb05 88 * \brief General vector vector elementwise multiplication using loop.
narshu 25:143b19c1fb05 89 * \ingroup _util_function
narshu 25:143b19c1fb05 90 */
narshu 25:143b19c1fb05 91 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 92 inline
narshu 25:143b19c1fb05 93 void
narshu 25:143b19c1fb05 94 Gevvmul(const Vector<T, Sz>& v1, const Vector<T, Sz>& v2,
narshu 25:143b19c1fb05 95 Vector<T, Sz>& v3)
narshu 25:143b19c1fb05 96 {
narshu 25:143b19c1fb05 97 for(std::size_t i = 0; i < Sz; ++i)
narshu 25:143b19c1fb05 98 v3(i) = v1(i) * v2(i);
narshu 25:143b19c1fb05 99 }
narshu 25:143b19c1fb05 100
narshu 25:143b19c1fb05 101
narshu 25:143b19c1fb05 102 /**
narshu 25:143b19c1fb05 103 * \fn Gevvadd(const Vector<T, Sz>& v1, const Vector<T, Sz>& v2, Vector<T, Sz>& v3)
narshu 25:143b19c1fb05 104 * \brief General vector vector elementwise multiplication using loop.
narshu 25:143b19c1fb05 105 * \ingroup _util_function
narshu 25:143b19c1fb05 106 */
narshu 25:143b19c1fb05 107 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 108 inline
narshu 25:143b19c1fb05 109 void
narshu 25:143b19c1fb05 110 Gevvadd(const Vector<T, Sz>& v1, const Vector<T, Sz>& v2,
narshu 25:143b19c1fb05 111 Vector<T, Sz>& v3)
narshu 25:143b19c1fb05 112 {
narshu 25:143b19c1fb05 113 for(std::size_t i = 0; i < Sz; ++i)
narshu 25:143b19c1fb05 114 v3(i) = v1(i) + v2(i);
narshu 25:143b19c1fb05 115 }
narshu 25:143b19c1fb05 116
narshu 25:143b19c1fb05 117 } // namespace util
narshu 25:143b19c1fb05 118
narshu 25:143b19c1fb05 119 } // namespace tvmet
narshu 25:143b19c1fb05 120
narshu 25:143b19c1fb05 121 #endif // TVMET_UTIL_GENERAL_H
narshu 25:143b19c1fb05 122
narshu 25:143b19c1fb05 123 // Local Variables:
narshu 25:143b19c1fb05 124 // mode:C++
narshu 25:143b19c1fb05 125 // tab-width:8
narshu 25:143b19c1fb05 126 // End: