Eurobot2012_Primary
Dependencies: mbed Eurobot_2012_Primary
tvmet/meta/Matrix.h@26:0995f61cb7b8, 2012-10-17 (annotated)
- 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?
User | Revision | Line number | New 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: Matrix.h,v 1.19 2007-06-23 15:58:59 opetzold Exp $ |
narshu | 25:143b19c1fb05 | 22 | */ |
narshu | 25:143b19c1fb05 | 23 | |
narshu | 25:143b19c1fb05 | 24 | #ifndef TVMET_META_MATRIX_H |
narshu | 25:143b19c1fb05 | 25 | #define TVMET_META_MATRIX_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 | namespace meta { |
narshu | 25:143b19c1fb05 | 33 | |
narshu | 25:143b19c1fb05 | 34 | |
narshu | 25:143b19c1fb05 | 35 | /** |
narshu | 25:143b19c1fb05 | 36 | * \class Matrix Matrix.h "tvmet/meta/Matrix.h" |
narshu | 25:143b19c1fb05 | 37 | * \brief Meta %Matrix class using expression and meta templates. |
narshu | 25:143b19c1fb05 | 38 | */ |
narshu | 25:143b19c1fb05 | 39 | template<std::size_t Rows, std::size_t Cols, |
narshu | 25:143b19c1fb05 | 40 | std::size_t M=0, std::size_t N=0> |
narshu | 25:143b19c1fb05 | 41 | class Matrix |
narshu | 25:143b19c1fb05 | 42 | { |
narshu | 25:143b19c1fb05 | 43 | Matrix(); |
narshu | 25:143b19c1fb05 | 44 | Matrix(const Matrix&); |
narshu | 25:143b19c1fb05 | 45 | Matrix& operator=(const Matrix&); |
narshu | 25:143b19c1fb05 | 46 | |
narshu | 25:143b19c1fb05 | 47 | private: |
narshu | 25:143b19c1fb05 | 48 | enum { |
narshu | 25:143b19c1fb05 | 49 | doRows = (M < Rows - 1) ? 1 : 0, /**< recursive counter Rows. */ |
narshu | 25:143b19c1fb05 | 50 | doCols = (N < Cols - 1) ? 1 : 0 /**< recursive counter Cols. */ |
narshu | 25:143b19c1fb05 | 51 | }; |
narshu | 25:143b19c1fb05 | 52 | |
narshu | 25:143b19c1fb05 | 53 | public: |
narshu | 25:143b19c1fb05 | 54 | /** assign an expression on columns on given row using the functional assign_fn. */ |
narshu | 25:143b19c1fb05 | 55 | template<class Dest, class Src, class Assign> |
narshu | 25:143b19c1fb05 | 56 | static inline |
narshu | 25:143b19c1fb05 | 57 | void assign2(Dest& lhs, const Src& rhs, const Assign& assign_fn) { |
narshu | 25:143b19c1fb05 | 58 | assign_fn.apply_on(lhs(M, N), rhs(M, N)); |
narshu | 25:143b19c1fb05 | 59 | Matrix<Rows * doCols, Cols * doCols, |
narshu | 25:143b19c1fb05 | 60 | M * doCols, (N+1) * doCols>::assign2(lhs, rhs, assign_fn); |
narshu | 25:143b19c1fb05 | 61 | } |
narshu | 25:143b19c1fb05 | 62 | |
narshu | 25:143b19c1fb05 | 63 | /** assign an expression on row-wise using the functional assign_fn. */ |
narshu | 25:143b19c1fb05 | 64 | template<class Dest, class Src, class Assign> |
narshu | 25:143b19c1fb05 | 65 | static inline |
narshu | 25:143b19c1fb05 | 66 | void assign(Dest& lhs, const Src& rhs, const Assign& assign_fn) { |
narshu | 25:143b19c1fb05 | 67 | Matrix<Rows, Cols, |
narshu | 25:143b19c1fb05 | 68 | M, 0>::assign2(lhs, rhs, assign_fn); |
narshu | 25:143b19c1fb05 | 69 | Matrix<Rows * doRows, Cols * doRows, |
narshu | 25:143b19c1fb05 | 70 | (M+1) * doRows, 0>::assign(lhs, rhs, assign_fn); |
narshu | 25:143b19c1fb05 | 71 | } |
narshu | 25:143b19c1fb05 | 72 | |
narshu | 25:143b19c1fb05 | 73 | /** evaluate a given matrix expression, column wise. */ |
narshu | 25:143b19c1fb05 | 74 | template<class E> |
narshu | 25:143b19c1fb05 | 75 | static inline |
narshu | 25:143b19c1fb05 | 76 | bool all_elements2(const E& e) { |
narshu | 25:143b19c1fb05 | 77 | if(!e(M, N)) return false; |
narshu | 25:143b19c1fb05 | 78 | return Matrix<Rows * doCols, Cols * doCols, |
narshu | 25:143b19c1fb05 | 79 | M * doCols, (N+1) * doCols>::all_elements2(e); |
narshu | 25:143b19c1fb05 | 80 | } |
narshu | 25:143b19c1fb05 | 81 | |
narshu | 25:143b19c1fb05 | 82 | /** evaluate a given matrix expression, row wise. */ |
narshu | 25:143b19c1fb05 | 83 | template<class E> |
narshu | 25:143b19c1fb05 | 84 | static inline |
narshu | 25:143b19c1fb05 | 85 | bool all_elements(const E& e) { |
narshu | 25:143b19c1fb05 | 86 | if(!Matrix<Rows, Cols, M, 0>::all_elements2(e) ) return false; |
narshu | 25:143b19c1fb05 | 87 | return Matrix<Rows * doRows, Cols * doRows, |
narshu | 25:143b19c1fb05 | 88 | (M+1) * doRows, 0>::all_elements(e); |
narshu | 25:143b19c1fb05 | 89 | } |
narshu | 25:143b19c1fb05 | 90 | |
narshu | 25:143b19c1fb05 | 91 | /** evaluate a given matrix expression, column wise. */ |
narshu | 25:143b19c1fb05 | 92 | template<class E> |
narshu | 25:143b19c1fb05 | 93 | static inline |
narshu | 25:143b19c1fb05 | 94 | bool any_elements2(const E& e) { |
narshu | 25:143b19c1fb05 | 95 | if(e(M, N)) return true; |
narshu | 25:143b19c1fb05 | 96 | return Matrix<Rows * doCols, Cols * doCols, |
narshu | 25:143b19c1fb05 | 97 | M * doCols, (N+1) * doCols>::any_elements2(e); |
narshu | 25:143b19c1fb05 | 98 | } |
narshu | 25:143b19c1fb05 | 99 | |
narshu | 25:143b19c1fb05 | 100 | /** evaluate a given matrix expression, row wise. */ |
narshu | 25:143b19c1fb05 | 101 | template<class E> |
narshu | 25:143b19c1fb05 | 102 | static inline |
narshu | 25:143b19c1fb05 | 103 | bool any_elements(const E& e) { |
narshu | 25:143b19c1fb05 | 104 | if(Matrix<Rows, Cols, M, 0>::any_elements2(e) ) return true; |
narshu | 25:143b19c1fb05 | 105 | return Matrix<Rows * doRows, Cols * doRows, |
narshu | 25:143b19c1fb05 | 106 | (M+1) * doRows, 0>::any_elements(e); |
narshu | 25:143b19c1fb05 | 107 | } |
narshu | 25:143b19c1fb05 | 108 | |
narshu | 25:143b19c1fb05 | 109 | /** trace a given matrix expression. */ |
narshu | 25:143b19c1fb05 | 110 | template<class E> |
narshu | 25:143b19c1fb05 | 111 | static inline |
narshu | 25:143b19c1fb05 | 112 | typename E::value_type |
narshu | 25:143b19c1fb05 | 113 | trace(const E& e) { |
narshu | 25:143b19c1fb05 | 114 | return e(M, N) |
narshu | 25:143b19c1fb05 | 115 | + Matrix<Rows * doCols, Cols * doCols, |
narshu | 25:143b19c1fb05 | 116 | (M+1) * doCols, (N+1) * doCols>::trace(e); |
narshu | 25:143b19c1fb05 | 117 | } |
narshu | 25:143b19c1fb05 | 118 | |
narshu | 25:143b19c1fb05 | 119 | }; |
narshu | 25:143b19c1fb05 | 120 | |
narshu | 25:143b19c1fb05 | 121 | |
narshu | 25:143b19c1fb05 | 122 | /** |
narshu | 25:143b19c1fb05 | 123 | * \class Matrix<0, 0, 0, 0> Matrix.h "tvmet/meta/Matrix.h" |
narshu | 25:143b19c1fb05 | 124 | * \brief Meta %Matrix specialized for recursion. |
narshu | 25:143b19c1fb05 | 125 | */ |
narshu | 25:143b19c1fb05 | 126 | template<> |
narshu | 25:143b19c1fb05 | 127 | class Matrix<0, 0, 0, 0> |
narshu | 25:143b19c1fb05 | 128 | { |
narshu | 25:143b19c1fb05 | 129 | Matrix(); |
narshu | 25:143b19c1fb05 | 130 | Matrix(const Matrix&); |
narshu | 25:143b19c1fb05 | 131 | Matrix& operator=(const Matrix&); |
narshu | 25:143b19c1fb05 | 132 | |
narshu | 25:143b19c1fb05 | 133 | public: |
narshu | 25:143b19c1fb05 | 134 | template<class Dest, class Src, class Assign> |
narshu | 25:143b19c1fb05 | 135 | static inline void assign2(Dest&, const Src&, const Assign&) { } |
narshu | 25:143b19c1fb05 | 136 | |
narshu | 25:143b19c1fb05 | 137 | template<class Dest, class Src, class Assign> |
narshu | 25:143b19c1fb05 | 138 | static inline void assign(Dest&, const Src&, const Assign&) { } |
narshu | 25:143b19c1fb05 | 139 | |
narshu | 25:143b19c1fb05 | 140 | template<class E> |
narshu | 25:143b19c1fb05 | 141 | static inline bool all_elements2(const E&) { return true; } |
narshu | 25:143b19c1fb05 | 142 | |
narshu | 25:143b19c1fb05 | 143 | template<class E> |
narshu | 25:143b19c1fb05 | 144 | static inline bool all_elements(const E&) { return true; } |
narshu | 25:143b19c1fb05 | 145 | |
narshu | 25:143b19c1fb05 | 146 | template<class E> |
narshu | 25:143b19c1fb05 | 147 | static inline bool any_elements2(const E&) { return false; } |
narshu | 25:143b19c1fb05 | 148 | |
narshu | 25:143b19c1fb05 | 149 | template<class E> |
narshu | 25:143b19c1fb05 | 150 | static inline bool any_elements(const E&) { return false; } |
narshu | 25:143b19c1fb05 | 151 | |
narshu | 25:143b19c1fb05 | 152 | template<class E> |
narshu | 25:143b19c1fb05 | 153 | static inline XprNull trace(const E&) { return XprNull(); } |
narshu | 25:143b19c1fb05 | 154 | }; |
narshu | 25:143b19c1fb05 | 155 | |
narshu | 25:143b19c1fb05 | 156 | |
narshu | 25:143b19c1fb05 | 157 | } // namespace meta |
narshu | 25:143b19c1fb05 | 158 | |
narshu | 25:143b19c1fb05 | 159 | } // namespace tvmet |
narshu | 25:143b19c1fb05 | 160 | |
narshu | 25:143b19c1fb05 | 161 | #endif /* TVMET_META_MATRIX_H */ |
narshu | 25:143b19c1fb05 | 162 | |
narshu | 25:143b19c1fb05 | 163 | // Local Variables: |
narshu | 25:143b19c1fb05 | 164 | // mode:C++ |
narshu | 25:143b19c1fb05 | 165 | // tab-width:8 |
narshu | 25:143b19c1fb05 | 166 | // End: |