2014 Eurobot fork
Dependencies: mbed-rtos mbed QEI
tvmet/xpr/Matrix.h@92:4a1225fbb146, 2013-10-15 (annotated)
- Committer:
- rsavitski
- Date:
- Tue Oct 15 12:19:32 2013 +0000
- Revision:
- 92:4a1225fbb146
- Parent:
- 15:9c5aaeda36dc
touch: ripped out 2013-specific bits. Need to address "2014" comments. Rewrite AI layer and other deleted parts.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
madcowswe | 15:9c5aaeda36dc | 1 | /* |
madcowswe | 15:9c5aaeda36dc | 2 | * Tiny Vector Matrix Library |
madcowswe | 15:9c5aaeda36dc | 3 | * Dense Vector Matrix Libary of Tiny size using Expression Templates |
madcowswe | 15:9c5aaeda36dc | 4 | * |
madcowswe | 15:9c5aaeda36dc | 5 | * Copyright (C) 2001 - 2007 Olaf Petzold <opetzold@users.sourceforge.net> |
madcowswe | 15:9c5aaeda36dc | 6 | * |
madcowswe | 15:9c5aaeda36dc | 7 | * This library is free software; you can redistribute it and/or |
madcowswe | 15:9c5aaeda36dc | 8 | * modify it under the terms of the GNU lesser General Public |
madcowswe | 15:9c5aaeda36dc | 9 | * License as published by the Free Software Foundation; either |
madcowswe | 15:9c5aaeda36dc | 10 | * version 2.1 of the License, or (at your option) any later version. |
madcowswe | 15:9c5aaeda36dc | 11 | * |
madcowswe | 15:9c5aaeda36dc | 12 | * This library is distributed in the hope that it will be useful, |
madcowswe | 15:9c5aaeda36dc | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
madcowswe | 15:9c5aaeda36dc | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
madcowswe | 15:9c5aaeda36dc | 15 | * lesser General Public License for more details. |
madcowswe | 15:9c5aaeda36dc | 16 | * |
madcowswe | 15:9c5aaeda36dc | 17 | * You should have received a copy of the GNU lesser General Public |
madcowswe | 15:9c5aaeda36dc | 18 | * License along with this library; if not, write to the Free Software |
madcowswe | 15:9c5aaeda36dc | 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
madcowswe | 15:9c5aaeda36dc | 20 | * |
madcowswe | 15:9c5aaeda36dc | 21 | * $Id: Matrix.h,v 1.26 2007-06-23 15:59:00 opetzold Exp $ |
madcowswe | 15:9c5aaeda36dc | 22 | */ |
madcowswe | 15:9c5aaeda36dc | 23 | |
madcowswe | 15:9c5aaeda36dc | 24 | #ifndef TVMET_XPR_MATRIX_H |
madcowswe | 15:9c5aaeda36dc | 25 | #define TVMET_XPR_MATRIX_H |
madcowswe | 15:9c5aaeda36dc | 26 | |
madcowswe | 15:9c5aaeda36dc | 27 | #include <tvmet/meta/Matrix.h> |
madcowswe | 15:9c5aaeda36dc | 28 | #include <tvmet/loop/Matrix.h> |
madcowswe | 15:9c5aaeda36dc | 29 | |
madcowswe | 15:9c5aaeda36dc | 30 | namespace tvmet { |
madcowswe | 15:9c5aaeda36dc | 31 | |
madcowswe | 15:9c5aaeda36dc | 32 | |
madcowswe | 15:9c5aaeda36dc | 33 | /* forwards */ |
madcowswe | 15:9c5aaeda36dc | 34 | template <class T, std::size_t Rows, std::size_t Cols> class Matrix; |
madcowswe | 15:9c5aaeda36dc | 35 | |
madcowswe | 15:9c5aaeda36dc | 36 | /** |
madcowswe | 15:9c5aaeda36dc | 37 | * \class XprMatrix Matrix.h "tvmet/xpr/Matrix.h" |
madcowswe | 15:9c5aaeda36dc | 38 | * \brief Represents the expression for vectors at any node in the parse tree. |
madcowswe | 15:9c5aaeda36dc | 39 | * |
madcowswe | 15:9c5aaeda36dc | 40 | * Specifically, XprMatrix is the class that wraps the expression, and the |
madcowswe | 15:9c5aaeda36dc | 41 | * expression itself is represented by the template parameter E. The |
madcowswe | 15:9c5aaeda36dc | 42 | * class XprMatrix is known as an anonymizing expression wrapper because |
madcowswe | 15:9c5aaeda36dc | 43 | * it can hold any subexpression of arbitrary complexity, allowing |
madcowswe | 15:9c5aaeda36dc | 44 | * clients to work with any expression by holding on to it via the |
madcowswe | 15:9c5aaeda36dc | 45 | * wrapper, without having to know the name of the type object that |
madcowswe | 15:9c5aaeda36dc | 46 | * actually implements the expression. |
madcowswe | 15:9c5aaeda36dc | 47 | * \note leave the CCtors non-explicit to allow implicit type conversation. |
madcowswe | 15:9c5aaeda36dc | 48 | */ |
madcowswe | 15:9c5aaeda36dc | 49 | template<class E, std::size_t NRows, std::size_t NCols> |
madcowswe | 15:9c5aaeda36dc | 50 | class XprMatrix |
madcowswe | 15:9c5aaeda36dc | 51 | : public TvmetBase< XprMatrix<E, NRows, NCols> > |
madcowswe | 15:9c5aaeda36dc | 52 | { |
madcowswe | 15:9c5aaeda36dc | 53 | XprMatrix(); |
madcowswe | 15:9c5aaeda36dc | 54 | XprMatrix& operator=(const XprMatrix&); |
madcowswe | 15:9c5aaeda36dc | 55 | |
madcowswe | 15:9c5aaeda36dc | 56 | public: |
madcowswe | 15:9c5aaeda36dc | 57 | /** Dimensions. */ |
madcowswe | 15:9c5aaeda36dc | 58 | enum { |
madcowswe | 15:9c5aaeda36dc | 59 | Rows = NRows, /**< Number of rows. */ |
madcowswe | 15:9c5aaeda36dc | 60 | Cols = NCols, /**< Number of cols. */ |
madcowswe | 15:9c5aaeda36dc | 61 | Size = Rows * Cols /**< Complete Size of Matrix. */ |
madcowswe | 15:9c5aaeda36dc | 62 | }; |
madcowswe | 15:9c5aaeda36dc | 63 | |
madcowswe | 15:9c5aaeda36dc | 64 | public: |
madcowswe | 15:9c5aaeda36dc | 65 | /** Complexity counter. */ |
madcowswe | 15:9c5aaeda36dc | 66 | enum { |
madcowswe | 15:9c5aaeda36dc | 67 | ops_assign = Rows * Cols, |
madcowswe | 15:9c5aaeda36dc | 68 | ops = E::ops, |
madcowswe | 15:9c5aaeda36dc | 69 | use_meta = ops_assign < TVMET_COMPLEXITY_M_ASSIGN_TRIGGER ? true : false |
madcowswe | 15:9c5aaeda36dc | 70 | }; |
madcowswe | 15:9c5aaeda36dc | 71 | |
madcowswe | 15:9c5aaeda36dc | 72 | public: |
madcowswe | 15:9c5aaeda36dc | 73 | typedef typename E::value_type value_type; |
madcowswe | 15:9c5aaeda36dc | 74 | |
madcowswe | 15:9c5aaeda36dc | 75 | public: |
madcowswe | 15:9c5aaeda36dc | 76 | /** Constructor. */ |
madcowswe | 15:9c5aaeda36dc | 77 | explicit XprMatrix(const E& e) |
madcowswe | 15:9c5aaeda36dc | 78 | : m_expr(e) |
madcowswe | 15:9c5aaeda36dc | 79 | { } |
madcowswe | 15:9c5aaeda36dc | 80 | |
madcowswe | 15:9c5aaeda36dc | 81 | /** Copy Constructor. Not explicit! */ |
madcowswe | 15:9c5aaeda36dc | 82 | #if defined(TVMET_OPTIMIZE_XPR_MANUAL_CCTOR) |
madcowswe | 15:9c5aaeda36dc | 83 | XprMatrix(const XprMatrix& rhs) |
madcowswe | 15:9c5aaeda36dc | 84 | : m_expr(rhs.m_expr) |
madcowswe | 15:9c5aaeda36dc | 85 | { } |
madcowswe | 15:9c5aaeda36dc | 86 | #endif |
madcowswe | 15:9c5aaeda36dc | 87 | |
madcowswe | 15:9c5aaeda36dc | 88 | /** access by index. */ |
madcowswe | 15:9c5aaeda36dc | 89 | value_type operator()(std::size_t i, std::size_t j) const { |
madcowswe | 15:9c5aaeda36dc | 90 | TVMET_RT_CONDITION((i < Rows) && (j < Cols), "XprMatrix Bounce Violation") |
madcowswe | 15:9c5aaeda36dc | 91 | return m_expr(i, j); |
madcowswe | 15:9c5aaeda36dc | 92 | } |
madcowswe | 15:9c5aaeda36dc | 93 | |
madcowswe | 15:9c5aaeda36dc | 94 | private: |
madcowswe | 15:9c5aaeda36dc | 95 | /** Wrapper for meta assign. */ |
madcowswe | 15:9c5aaeda36dc | 96 | template<class Dest, class Src, class Assign> |
madcowswe | 15:9c5aaeda36dc | 97 | static inline |
madcowswe | 15:9c5aaeda36dc | 98 | void do_assign(dispatch<true>, Dest& dest, const Src& src, const Assign& assign_fn) { |
madcowswe | 15:9c5aaeda36dc | 99 | meta::Matrix<Rows, Cols, 0, 0>::assign(dest, src, assign_fn); |
madcowswe | 15:9c5aaeda36dc | 100 | } |
madcowswe | 15:9c5aaeda36dc | 101 | |
madcowswe | 15:9c5aaeda36dc | 102 | /** Wrapper for loop assign. */ |
madcowswe | 15:9c5aaeda36dc | 103 | template<class Dest, class Src, class Assign> |
madcowswe | 15:9c5aaeda36dc | 104 | static inline |
madcowswe | 15:9c5aaeda36dc | 105 | void do_assign(dispatch<false>, Dest& dest, const Src& src, const Assign& assign_fn) { |
madcowswe | 15:9c5aaeda36dc | 106 | loop::Matrix<Rows, Cols>::assign(dest, src, assign_fn); |
madcowswe | 15:9c5aaeda36dc | 107 | } |
madcowswe | 15:9c5aaeda36dc | 108 | |
madcowswe | 15:9c5aaeda36dc | 109 | public: |
madcowswe | 15:9c5aaeda36dc | 110 | /** assign this expression to Matrix dest. */ |
madcowswe | 15:9c5aaeda36dc | 111 | template<class Dest, class Assign> |
madcowswe | 15:9c5aaeda36dc | 112 | void assign_to(Dest& dest, const Assign& assign_fn) const { |
madcowswe | 15:9c5aaeda36dc | 113 | /* here is a way for caching, since each complex 'Node' |
madcowswe | 15:9c5aaeda36dc | 114 | is of type XprMatrix. */ |
madcowswe | 15:9c5aaeda36dc | 115 | do_assign(dispatch<use_meta>(), dest, *this, assign_fn); |
madcowswe | 15:9c5aaeda36dc | 116 | } |
madcowswe | 15:9c5aaeda36dc | 117 | |
madcowswe | 15:9c5aaeda36dc | 118 | public: // debugging Xpr parse tree |
madcowswe | 15:9c5aaeda36dc | 119 | void print_xpr(std::ostream& os, std::size_t l=0) const { |
madcowswe | 15:9c5aaeda36dc | 120 | os << IndentLevel(l++) |
madcowswe | 15:9c5aaeda36dc | 121 | << "XprMatrix[" |
madcowswe | 15:9c5aaeda36dc | 122 | << (use_meta ? "M" : "L") << ", O=" << ops << "]<" |
madcowswe | 15:9c5aaeda36dc | 123 | << std::endl; |
madcowswe | 15:9c5aaeda36dc | 124 | m_expr.print_xpr(os, l); |
madcowswe | 15:9c5aaeda36dc | 125 | os << IndentLevel(l) |
madcowswe | 15:9c5aaeda36dc | 126 | << "R=" << Rows << ", C=" << Cols << std::endl; |
madcowswe | 15:9c5aaeda36dc | 127 | os << IndentLevel(--l) << ">" |
madcowswe | 15:9c5aaeda36dc | 128 | << ((l != 0) ? "," : "") << std::endl; |
madcowswe | 15:9c5aaeda36dc | 129 | } |
madcowswe | 15:9c5aaeda36dc | 130 | |
madcowswe | 15:9c5aaeda36dc | 131 | private: |
madcowswe | 15:9c5aaeda36dc | 132 | const E m_expr; |
madcowswe | 15:9c5aaeda36dc | 133 | }; |
madcowswe | 15:9c5aaeda36dc | 134 | |
madcowswe | 15:9c5aaeda36dc | 135 | |
madcowswe | 15:9c5aaeda36dc | 136 | } // namespace tvmet |
madcowswe | 15:9c5aaeda36dc | 137 | |
madcowswe | 15:9c5aaeda36dc | 138 | #include <tvmet/Functional.h> |
madcowswe | 15:9c5aaeda36dc | 139 | |
madcowswe | 15:9c5aaeda36dc | 140 | #include <tvmet/xpr/BinOperator.h> |
madcowswe | 15:9c5aaeda36dc | 141 | #include <tvmet/xpr/UnOperator.h> |
madcowswe | 15:9c5aaeda36dc | 142 | #include <tvmet/xpr/Literal.h> |
madcowswe | 15:9c5aaeda36dc | 143 | |
madcowswe | 15:9c5aaeda36dc | 144 | #include <tvmet/xpr/Identity.h> |
madcowswe | 15:9c5aaeda36dc | 145 | |
madcowswe | 15:9c5aaeda36dc | 146 | #include <tvmet/xpr/MMProduct.h> |
madcowswe | 15:9c5aaeda36dc | 147 | #include <tvmet/xpr/MMProductTransposed.h> |
madcowswe | 15:9c5aaeda36dc | 148 | #include <tvmet/xpr/MMtProduct.h> |
madcowswe | 15:9c5aaeda36dc | 149 | #include <tvmet/xpr/MtMProduct.h> |
madcowswe | 15:9c5aaeda36dc | 150 | #include <tvmet/xpr/MVProduct.h> |
madcowswe | 15:9c5aaeda36dc | 151 | #include <tvmet/xpr/MtVProduct.h> |
madcowswe | 15:9c5aaeda36dc | 152 | #include <tvmet/xpr/MatrixTranspose.h> |
madcowswe | 15:9c5aaeda36dc | 153 | |
madcowswe | 15:9c5aaeda36dc | 154 | #include <tvmet/xpr/MatrixFunctions.h> |
madcowswe | 15:9c5aaeda36dc | 155 | #include <tvmet/xpr/MatrixBinaryFunctions.h> |
madcowswe | 15:9c5aaeda36dc | 156 | #include <tvmet/xpr/MatrixUnaryFunctions.h> |
madcowswe | 15:9c5aaeda36dc | 157 | #include <tvmet/xpr/MatrixOperators.h> |
madcowswe | 15:9c5aaeda36dc | 158 | #include <tvmet/xpr/Eval.h> |
madcowswe | 15:9c5aaeda36dc | 159 | |
madcowswe | 15:9c5aaeda36dc | 160 | #endif // TVMET_XPR_MATRIX_H |
madcowswe | 15:9c5aaeda36dc | 161 | |
madcowswe | 15:9c5aaeda36dc | 162 | // Local Variables: |
madcowswe | 15:9c5aaeda36dc | 163 | // mode:C++ |
madcowswe | 15:9c5aaeda36dc | 164 | // tab-width:8 |
madcowswe | 15:9c5aaeda36dc | 165 | // End: |