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: Vector.h,v 1.24 2007-06-23 15:58:59 opetzold Exp $
narshu 25:143b19c1fb05 22 */
narshu 25:143b19c1fb05 23
narshu 25:143b19c1fb05 24 #ifndef TVMET_META_VECTOR_H
narshu 25:143b19c1fb05 25 #define TVMET_META_VECTOR_H
narshu 25:143b19c1fb05 26
narshu 25:143b19c1fb05 27 #include <tvmet/NumericTraits.h>
narshu 25:143b19c1fb05 28 #include <tvmet/xpr/Null.h>
narshu 25:143b19c1fb05 29
narshu 25:143b19c1fb05 30 namespace tvmet {
narshu 25:143b19c1fb05 31
narshu 25:143b19c1fb05 32 /* forwards */
narshu 25:143b19c1fb05 33 template<class T, std::size_t Sz> class Vector;
narshu 25:143b19c1fb05 34
narshu 25:143b19c1fb05 35
narshu 25:143b19c1fb05 36 namespace meta {
narshu 25:143b19c1fb05 37
narshu 25:143b19c1fb05 38
narshu 25:143b19c1fb05 39 /**
narshu 25:143b19c1fb05 40 * \class Vector Vector.h "tvmet/meta/Vector.h"
narshu 25:143b19c1fb05 41 * \brief Meta %Vector class using expression templates
narshu 25:143b19c1fb05 42 */
narshu 25:143b19c1fb05 43 template<std::size_t Sz, std::size_t K=0>
narshu 25:143b19c1fb05 44 class Vector
narshu 25:143b19c1fb05 45 {
narshu 25:143b19c1fb05 46 Vector();
narshu 25:143b19c1fb05 47 Vector(const Vector&);
narshu 25:143b19c1fb05 48 Vector& operator=(const Vector&);
narshu 25:143b19c1fb05 49
narshu 25:143b19c1fb05 50 private:
narshu 25:143b19c1fb05 51 enum {
narshu 25:143b19c1fb05 52 doIt = (K < (Sz-1)) ? 1 : 0 /**< recursive counter */
narshu 25:143b19c1fb05 53 };
narshu 25:143b19c1fb05 54
narshu 25:143b19c1fb05 55 public:
narshu 25:143b19c1fb05 56 /** assign an expression expr using the functional assign_fn. */
narshu 25:143b19c1fb05 57 template <class Dest, class Src, class Assign>
narshu 25:143b19c1fb05 58 static inline
narshu 25:143b19c1fb05 59 void assign(Dest& lhs, const Src& rhs, const Assign& assign_fn) {
narshu 25:143b19c1fb05 60 assign_fn.apply_on(lhs(K), rhs(K));
narshu 25:143b19c1fb05 61 meta::Vector<Sz * doIt, (K+1) * doIt>::assign(lhs, rhs, assign_fn);
narshu 25:143b19c1fb05 62 }
narshu 25:143b19c1fb05 63
narshu 25:143b19c1fb05 64 /** build the sum of the vector. */
narshu 25:143b19c1fb05 65 template<class E>
narshu 25:143b19c1fb05 66 static inline
narshu 25:143b19c1fb05 67 typename E::value_type
narshu 25:143b19c1fb05 68 sum(const E& e) {
narshu 25:143b19c1fb05 69 return e(K) + meta::Vector<Sz * doIt, (K+1) * doIt>::sum(e);
narshu 25:143b19c1fb05 70 }
narshu 25:143b19c1fb05 71
narshu 25:143b19c1fb05 72 /** build the product of the vector. */
narshu 25:143b19c1fb05 73 template<class E>
narshu 25:143b19c1fb05 74 static inline
narshu 25:143b19c1fb05 75 typename NumericTraits<
narshu 25:143b19c1fb05 76 typename E::value_type
narshu 25:143b19c1fb05 77 >::sum_type
narshu 25:143b19c1fb05 78 product(const E& e) {
narshu 25:143b19c1fb05 79 return e(K) * meta::Vector<Sz * doIt, (K+1) * doIt>::product(e);
narshu 25:143b19c1fb05 80 }
narshu 25:143b19c1fb05 81
narshu 25:143b19c1fb05 82 /** build the dot product of the vector. */
narshu 25:143b19c1fb05 83 template<class Dest, class Src>
narshu 25:143b19c1fb05 84 static inline
narshu 25:143b19c1fb05 85 typename PromoteTraits<
narshu 25:143b19c1fb05 86 typename Dest::value_type,
narshu 25:143b19c1fb05 87 typename Src::value_type
narshu 25:143b19c1fb05 88 >::value_type
narshu 25:143b19c1fb05 89 dot(const Dest& lhs, const Src& rhs) {
narshu 25:143b19c1fb05 90 return lhs(K) * rhs(K)
narshu 25:143b19c1fb05 91 + meta::Vector<Sz * doIt, (K+1) * doIt>::dot(lhs, rhs);
narshu 25:143b19c1fb05 92 }
narshu 25:143b19c1fb05 93
narshu 25:143b19c1fb05 94 /** check for all elements */
narshu 25:143b19c1fb05 95 template<class E>
narshu 25:143b19c1fb05 96 static inline
narshu 25:143b19c1fb05 97 bool
narshu 25:143b19c1fb05 98 all_elements(const E& e) {
narshu 25:143b19c1fb05 99 if(!e(K)) return false;
narshu 25:143b19c1fb05 100 return meta::Vector<Sz * doIt, (K+1) * doIt>::all_elements(e);
narshu 25:143b19c1fb05 101 }
narshu 25:143b19c1fb05 102
narshu 25:143b19c1fb05 103 /** check for any elements */
narshu 25:143b19c1fb05 104 template<class E>
narshu 25:143b19c1fb05 105 static inline
narshu 25:143b19c1fb05 106 bool
narshu 25:143b19c1fb05 107 any_elements(const E& e) {
narshu 25:143b19c1fb05 108 if(e(K)) return true;
narshu 25:143b19c1fb05 109 return meta::Vector<Sz * doIt, (K+1) * doIt>::any_elements(e);
narshu 25:143b19c1fb05 110 }
narshu 25:143b19c1fb05 111 };
narshu 25:143b19c1fb05 112
narshu 25:143b19c1fb05 113
narshu 25:143b19c1fb05 114 /**
narshu 25:143b19c1fb05 115 * \class Vector<0,0> Vector.h "tvmet/meta/Vector.h"
narshu 25:143b19c1fb05 116 * \brief Meta %Vector Specialized for recursion
narshu 25:143b19c1fb05 117 */
narshu 25:143b19c1fb05 118 template<>
narshu 25:143b19c1fb05 119 class Vector<0,0>
narshu 25:143b19c1fb05 120 {
narshu 25:143b19c1fb05 121 Vector();
narshu 25:143b19c1fb05 122 Vector(const Vector&);
narshu 25:143b19c1fb05 123 Vector& operator=(const Vector&);
narshu 25:143b19c1fb05 124
narshu 25:143b19c1fb05 125 public:
narshu 25:143b19c1fb05 126 template <class Dest, class Src, class Assign>
narshu 25:143b19c1fb05 127 static inline void assign(Dest&, const Src&, const Assign&) { }
narshu 25:143b19c1fb05 128
narshu 25:143b19c1fb05 129 template<class E>
narshu 25:143b19c1fb05 130 static inline XprNull sum(const E&) { return XprNull(); }
narshu 25:143b19c1fb05 131
narshu 25:143b19c1fb05 132 template<class E>
narshu 25:143b19c1fb05 133 static inline XprNull product(const E&) { return XprNull(); }
narshu 25:143b19c1fb05 134
narshu 25:143b19c1fb05 135 template<class Dest, class Src>
narshu 25:143b19c1fb05 136 static inline XprNull dot(const Dest&, const Src&) { return XprNull(); }
narshu 25:143b19c1fb05 137
narshu 25:143b19c1fb05 138 template<class E>
narshu 25:143b19c1fb05 139 static inline bool all_elements(const E&) { return true; }
narshu 25:143b19c1fb05 140
narshu 25:143b19c1fb05 141 template<class E>
narshu 25:143b19c1fb05 142 static inline bool any_elements(const E&) { return false; }
narshu 25:143b19c1fb05 143 };
narshu 25:143b19c1fb05 144
narshu 25:143b19c1fb05 145
narshu 25:143b19c1fb05 146 } // namespace meta
narshu 25:143b19c1fb05 147
narshu 25:143b19c1fb05 148 } // namespace tvmet
narshu 25:143b19c1fb05 149
narshu 25:143b19c1fb05 150 #endif /* TVMET_META_VECTOR_H */
narshu 25:143b19c1fb05 151
narshu 25:143b19c1fb05 152 // Local Variables:
narshu 25:143b19c1fb05 153 // mode:C++
narshu 25:143b19c1fb05 154 // tab-width:8
narshu 25:143b19c1fb05 155 // End: