2014 Eurobot fork
Dependencies: mbed-rtos mbed QEI
tvmet/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.58 2007-06-23 15:58:58 opetzold Exp $ |
madcowswe | 15:9c5aaeda36dc | 22 | */ |
madcowswe | 15:9c5aaeda36dc | 23 | |
madcowswe | 15:9c5aaeda36dc | 24 | #ifndef TVMET_MATRIX_H |
madcowswe | 15:9c5aaeda36dc | 25 | #define TVMET_MATRIX_H |
madcowswe | 15:9c5aaeda36dc | 26 | |
madcowswe | 15:9c5aaeda36dc | 27 | #include <iterator> // reverse_iterator |
madcowswe | 15:9c5aaeda36dc | 28 | |
madcowswe | 15:9c5aaeda36dc | 29 | #include <tvmet/tvmet.h> |
madcowswe | 15:9c5aaeda36dc | 30 | #include <tvmet/TypePromotion.h> |
madcowswe | 15:9c5aaeda36dc | 31 | #include <tvmet/CommaInitializer.h> |
madcowswe | 15:9c5aaeda36dc | 32 | #include <tvmet/RunTimeError.h> |
madcowswe | 15:9c5aaeda36dc | 33 | |
madcowswe | 15:9c5aaeda36dc | 34 | #include <tvmet/xpr/Matrix.h> |
madcowswe | 15:9c5aaeda36dc | 35 | #include <tvmet/xpr/MatrixRow.h> |
madcowswe | 15:9c5aaeda36dc | 36 | #include <tvmet/xpr/MatrixCol.h> |
madcowswe | 15:9c5aaeda36dc | 37 | #include <tvmet/xpr/MatrixDiag.h> |
madcowswe | 15:9c5aaeda36dc | 38 | |
madcowswe | 15:9c5aaeda36dc | 39 | namespace tvmet { |
madcowswe | 15:9c5aaeda36dc | 40 | |
madcowswe | 15:9c5aaeda36dc | 41 | |
madcowswe | 15:9c5aaeda36dc | 42 | /* forwards */ |
madcowswe | 15:9c5aaeda36dc | 43 | template<class T, std::size_t Rows, std::size_t Cols> class Matrix; |
madcowswe | 15:9c5aaeda36dc | 44 | template<class T, |
madcowswe | 15:9c5aaeda36dc | 45 | std::size_t RowsBgn, std::size_t RowsEnd, |
madcowswe | 15:9c5aaeda36dc | 46 | std::size_t ColsBgn, std::size_t ColsEnd, |
madcowswe | 15:9c5aaeda36dc | 47 | std::size_t RowStride, std::size_t ColStride /*=1*/> |
madcowswe | 15:9c5aaeda36dc | 48 | class MatrixSliceConstReference; // unused here; for me only |
madcowswe | 15:9c5aaeda36dc | 49 | |
madcowswe | 15:9c5aaeda36dc | 50 | |
madcowswe | 15:9c5aaeda36dc | 51 | /** |
madcowswe | 15:9c5aaeda36dc | 52 | * \class MatrixConstReference Matrix.h "tvmet/Matrix.h" |
madcowswe | 15:9c5aaeda36dc | 53 | * \brief value iterator for ET |
madcowswe | 15:9c5aaeda36dc | 54 | */ |
madcowswe | 15:9c5aaeda36dc | 55 | template<class T, std::size_t NRows, std::size_t NCols> |
madcowswe | 15:9c5aaeda36dc | 56 | class MatrixConstReference |
madcowswe | 15:9c5aaeda36dc | 57 | : public TvmetBase < MatrixConstReference<T, NRows, NCols> > |
madcowswe | 15:9c5aaeda36dc | 58 | { |
madcowswe | 15:9c5aaeda36dc | 59 | public: |
madcowswe | 15:9c5aaeda36dc | 60 | typedef T value_type; |
madcowswe | 15:9c5aaeda36dc | 61 | typedef T* pointer; |
madcowswe | 15:9c5aaeda36dc | 62 | typedef const T* const_pointer; |
madcowswe | 15:9c5aaeda36dc | 63 | |
madcowswe | 15:9c5aaeda36dc | 64 | /** Dimensions. */ |
madcowswe | 15:9c5aaeda36dc | 65 | enum { |
madcowswe | 15:9c5aaeda36dc | 66 | Rows = NRows, /**< Number of rows. */ |
madcowswe | 15:9c5aaeda36dc | 67 | Cols = NCols, /**< Number of cols. */ |
madcowswe | 15:9c5aaeda36dc | 68 | Size = Rows * Cols /**< Complete Size of Matrix. */ |
madcowswe | 15:9c5aaeda36dc | 69 | }; |
madcowswe | 15:9c5aaeda36dc | 70 | |
madcowswe | 15:9c5aaeda36dc | 71 | public: |
madcowswe | 15:9c5aaeda36dc | 72 | /** Complexity counter. */ |
madcowswe | 15:9c5aaeda36dc | 73 | enum { |
madcowswe | 15:9c5aaeda36dc | 74 | ops = Rows * Cols |
madcowswe | 15:9c5aaeda36dc | 75 | }; |
madcowswe | 15:9c5aaeda36dc | 76 | |
madcowswe | 15:9c5aaeda36dc | 77 | private: |
madcowswe | 15:9c5aaeda36dc | 78 | MatrixConstReference(); |
madcowswe | 15:9c5aaeda36dc | 79 | MatrixConstReference& operator=(const MatrixConstReference&); |
madcowswe | 15:9c5aaeda36dc | 80 | |
madcowswe | 15:9c5aaeda36dc | 81 | public: |
madcowswe | 15:9c5aaeda36dc | 82 | /** Constructor. */ |
madcowswe | 15:9c5aaeda36dc | 83 | explicit MatrixConstReference(const Matrix<T, Rows, Cols>& rhs) |
madcowswe | 15:9c5aaeda36dc | 84 | : m_data(rhs.data()) |
madcowswe | 15:9c5aaeda36dc | 85 | { } |
madcowswe | 15:9c5aaeda36dc | 86 | |
madcowswe | 15:9c5aaeda36dc | 87 | /** Constructor by a given memory pointer. */ |
madcowswe | 15:9c5aaeda36dc | 88 | explicit MatrixConstReference(const_pointer data) |
madcowswe | 15:9c5aaeda36dc | 89 | : m_data(data) |
madcowswe | 15:9c5aaeda36dc | 90 | { } |
madcowswe | 15:9c5aaeda36dc | 91 | |
madcowswe | 15:9c5aaeda36dc | 92 | public: // access operators |
madcowswe | 15:9c5aaeda36dc | 93 | /** access by index. */ |
madcowswe | 15:9c5aaeda36dc | 94 | value_type operator()(std::size_t i, std::size_t j) const { |
madcowswe | 15:9c5aaeda36dc | 95 | TVMET_RT_CONDITION((i < Rows) && (j < Cols), "MatrixConstReference Bounce Violation") |
madcowswe | 15:9c5aaeda36dc | 96 | return m_data[i * Cols + j]; |
madcowswe | 15:9c5aaeda36dc | 97 | } |
madcowswe | 15:9c5aaeda36dc | 98 | |
madcowswe | 15:9c5aaeda36dc | 99 | public: // debugging Xpr parse tree |
madcowswe | 15:9c5aaeda36dc | 100 | void print_xpr(std::ostream& os, std::size_t l=0) const { |
madcowswe | 15:9c5aaeda36dc | 101 | os << IndentLevel(l) |
madcowswe | 15:9c5aaeda36dc | 102 | << "MatrixConstReference[O=" << ops << "]<" |
madcowswe | 15:9c5aaeda36dc | 103 | << "T=" << typeid(value_type).name() << ">," |
madcowswe | 15:9c5aaeda36dc | 104 | << std::endl; |
madcowswe | 15:9c5aaeda36dc | 105 | } |
madcowswe | 15:9c5aaeda36dc | 106 | |
madcowswe | 15:9c5aaeda36dc | 107 | private: |
madcowswe | 15:9c5aaeda36dc | 108 | const_pointer _tvmet_restrict m_data; |
madcowswe | 15:9c5aaeda36dc | 109 | }; |
madcowswe | 15:9c5aaeda36dc | 110 | |
madcowswe | 15:9c5aaeda36dc | 111 | |
madcowswe | 15:9c5aaeda36dc | 112 | /** |
madcowswe | 15:9c5aaeda36dc | 113 | * \class Matrix Matrix.h "tvmet/Matrix.h" |
madcowswe | 15:9c5aaeda36dc | 114 | * \brief A tiny matrix class. |
madcowswe | 15:9c5aaeda36dc | 115 | * |
madcowswe | 15:9c5aaeda36dc | 116 | * The array syntax A[j][j] isn't supported here. The reason is that |
madcowswe | 15:9c5aaeda36dc | 117 | * operator[] always takes exactly one parameter, but operator() can |
madcowswe | 15:9c5aaeda36dc | 118 | * take any number of parameters (in the case of a rectangular matrix, |
madcowswe | 15:9c5aaeda36dc | 119 | * two paramters are needed). Therefore the cleanest way to do it is |
madcowswe | 15:9c5aaeda36dc | 120 | * with operator() rather than with operator[]. \see C++ FAQ Lite 13.8 |
madcowswe | 15:9c5aaeda36dc | 121 | */ |
madcowswe | 15:9c5aaeda36dc | 122 | template<class T, std::size_t NRows, std::size_t NCols> |
madcowswe | 15:9c5aaeda36dc | 123 | class Matrix |
madcowswe | 15:9c5aaeda36dc | 124 | { |
madcowswe | 15:9c5aaeda36dc | 125 | public: |
madcowswe | 15:9c5aaeda36dc | 126 | /** Data type of the tvmet::Matrix. */ |
madcowswe | 15:9c5aaeda36dc | 127 | typedef T value_type; |
madcowswe | 15:9c5aaeda36dc | 128 | |
madcowswe | 15:9c5aaeda36dc | 129 | /** Reference type of the tvmet::Matrix data elements. */ |
madcowswe | 15:9c5aaeda36dc | 130 | typedef T& reference; |
madcowswe | 15:9c5aaeda36dc | 131 | |
madcowswe | 15:9c5aaeda36dc | 132 | /** const reference type of the tvmet::Matrix data elements. */ |
madcowswe | 15:9c5aaeda36dc | 133 | typedef const T& const_reference; |
madcowswe | 15:9c5aaeda36dc | 134 | |
madcowswe | 15:9c5aaeda36dc | 135 | /** STL iterator interface. */ |
madcowswe | 15:9c5aaeda36dc | 136 | typedef T* iterator; |
madcowswe | 15:9c5aaeda36dc | 137 | |
madcowswe | 15:9c5aaeda36dc | 138 | /** STL const_iterator interface. */ |
madcowswe | 15:9c5aaeda36dc | 139 | typedef const T* const_iterator; |
madcowswe | 15:9c5aaeda36dc | 140 | |
madcowswe | 15:9c5aaeda36dc | 141 | /** STL reverse iterator interface. */ |
madcowswe | 15:9c5aaeda36dc | 142 | typedef std::reverse_iterator<iterator> reverse_iterator; |
madcowswe | 15:9c5aaeda36dc | 143 | |
madcowswe | 15:9c5aaeda36dc | 144 | /** STL const reverse iterator interface. */ |
madcowswe | 15:9c5aaeda36dc | 145 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
madcowswe | 15:9c5aaeda36dc | 146 | |
madcowswe | 15:9c5aaeda36dc | 147 | public: |
madcowswe | 15:9c5aaeda36dc | 148 | /** Dimensions. */ |
madcowswe | 15:9c5aaeda36dc | 149 | enum { |
madcowswe | 15:9c5aaeda36dc | 150 | Rows = NRows, /**< Number of rows. */ |
madcowswe | 15:9c5aaeda36dc | 151 | Cols = NCols, /**< Number of cols. */ |
madcowswe | 15:9c5aaeda36dc | 152 | Size = Rows * Cols /**< Complete Size of Matrix. */ |
madcowswe | 15:9c5aaeda36dc | 153 | }; |
madcowswe | 15:9c5aaeda36dc | 154 | |
madcowswe | 15:9c5aaeda36dc | 155 | public: |
madcowswe | 15:9c5aaeda36dc | 156 | /** Complexity counter. */ |
madcowswe | 15:9c5aaeda36dc | 157 | enum { |
madcowswe | 15:9c5aaeda36dc | 158 | ops_assign = Rows * Cols, |
madcowswe | 15:9c5aaeda36dc | 159 | ops = ops_assign, |
madcowswe | 15:9c5aaeda36dc | 160 | use_meta = ops < TVMET_COMPLEXITY_M_ASSIGN_TRIGGER ? true : false |
madcowswe | 15:9c5aaeda36dc | 161 | }; |
madcowswe | 15:9c5aaeda36dc | 162 | |
madcowswe | 15:9c5aaeda36dc | 163 | public: // STL interface |
madcowswe | 15:9c5aaeda36dc | 164 | /** STL iterator interface. */ |
madcowswe | 15:9c5aaeda36dc | 165 | iterator begin() { return m_data; } |
madcowswe | 15:9c5aaeda36dc | 166 | |
madcowswe | 15:9c5aaeda36dc | 167 | /** STL iterator interface. */ |
madcowswe | 15:9c5aaeda36dc | 168 | iterator end() { return m_data + Size; } |
madcowswe | 15:9c5aaeda36dc | 169 | |
madcowswe | 15:9c5aaeda36dc | 170 | /** STL const_iterator interface. */ |
madcowswe | 15:9c5aaeda36dc | 171 | const_iterator begin() const { return m_data; } |
madcowswe | 15:9c5aaeda36dc | 172 | |
madcowswe | 15:9c5aaeda36dc | 173 | /** STL const_iterator interface. */ |
madcowswe | 15:9c5aaeda36dc | 174 | const_iterator end() const { return m_data + Size; } |
madcowswe | 15:9c5aaeda36dc | 175 | |
madcowswe | 15:9c5aaeda36dc | 176 | /** STL reverse iterator interface reverse begin. */ |
madcowswe | 15:9c5aaeda36dc | 177 | reverse_iterator rbegin() { return reverse_iterator( end() ); } |
madcowswe | 15:9c5aaeda36dc | 178 | |
madcowswe | 15:9c5aaeda36dc | 179 | /** STL const reverse iterator interface reverse begin. */ |
madcowswe | 15:9c5aaeda36dc | 180 | const_reverse_iterator rbegin() const { |
madcowswe | 15:9c5aaeda36dc | 181 | return const_reverse_iterator( end() ); |
madcowswe | 15:9c5aaeda36dc | 182 | } |
madcowswe | 15:9c5aaeda36dc | 183 | |
madcowswe | 15:9c5aaeda36dc | 184 | /** STL reverse iterator interface reverse end. */ |
madcowswe | 15:9c5aaeda36dc | 185 | reverse_iterator rend() { return reverse_iterator( begin() ); } |
madcowswe | 15:9c5aaeda36dc | 186 | |
madcowswe | 15:9c5aaeda36dc | 187 | /** STL const reverse iterator interface reverse end. */ |
madcowswe | 15:9c5aaeda36dc | 188 | const_reverse_iterator rend() const { |
madcowswe | 15:9c5aaeda36dc | 189 | return const_reverse_iterator( begin() ); |
madcowswe | 15:9c5aaeda36dc | 190 | } |
madcowswe | 15:9c5aaeda36dc | 191 | |
madcowswe | 15:9c5aaeda36dc | 192 | /** The size of the matrix. */ |
madcowswe | 15:9c5aaeda36dc | 193 | static std::size_t size() { return Size; } |
madcowswe | 15:9c5aaeda36dc | 194 | |
madcowswe | 15:9c5aaeda36dc | 195 | /** STL vector max_size() - returns allways rows()*cols(). */ |
madcowswe | 15:9c5aaeda36dc | 196 | static std::size_t max_size() { return Size; } |
madcowswe | 15:9c5aaeda36dc | 197 | |
madcowswe | 15:9c5aaeda36dc | 198 | /** STL vector empty() - returns allways false. */ |
madcowswe | 15:9c5aaeda36dc | 199 | static bool empty() { return false; } |
madcowswe | 15:9c5aaeda36dc | 200 | |
madcowswe | 15:9c5aaeda36dc | 201 | public: |
madcowswe | 15:9c5aaeda36dc | 202 | /** The number of rows of matrix. */ |
madcowswe | 15:9c5aaeda36dc | 203 | static std::size_t rows() { return Rows; } |
madcowswe | 15:9c5aaeda36dc | 204 | |
madcowswe | 15:9c5aaeda36dc | 205 | /** The number of columns of matrix. */ |
madcowswe | 15:9c5aaeda36dc | 206 | static std::size_t cols() { return Cols; } |
madcowswe | 15:9c5aaeda36dc | 207 | |
madcowswe | 15:9c5aaeda36dc | 208 | public: |
madcowswe | 15:9c5aaeda36dc | 209 | /** Default Destructor */ |
madcowswe | 15:9c5aaeda36dc | 210 | ~Matrix() { |
madcowswe | 15:9c5aaeda36dc | 211 | #if defined(TVMET_DYNAMIC_MEMORY) |
madcowswe | 15:9c5aaeda36dc | 212 | delete [] m_data; |
madcowswe | 15:9c5aaeda36dc | 213 | #endif |
madcowswe | 15:9c5aaeda36dc | 214 | } |
madcowswe | 15:9c5aaeda36dc | 215 | |
madcowswe | 15:9c5aaeda36dc | 216 | /** Default Constructor. The allocated memory region isn't cleared. If you want |
madcowswe | 15:9c5aaeda36dc | 217 | a clean use the constructor argument zero. */ |
madcowswe | 15:9c5aaeda36dc | 218 | explicit Matrix() |
madcowswe | 15:9c5aaeda36dc | 219 | #if defined(TVMET_DYNAMIC_MEMORY) |
madcowswe | 15:9c5aaeda36dc | 220 | : m_data( new value_type[Size] ) |
madcowswe | 15:9c5aaeda36dc | 221 | #endif |
madcowswe | 15:9c5aaeda36dc | 222 | { } |
madcowswe | 15:9c5aaeda36dc | 223 | |
madcowswe | 15:9c5aaeda36dc | 224 | /** Copy Constructor, not explicit! */ |
madcowswe | 15:9c5aaeda36dc | 225 | Matrix(const Matrix& rhs) |
madcowswe | 15:9c5aaeda36dc | 226 | #if defined(TVMET_DYNAMIC_MEMORY) |
madcowswe | 15:9c5aaeda36dc | 227 | : m_data( new value_type[Size] ) |
madcowswe | 15:9c5aaeda36dc | 228 | #endif |
madcowswe | 15:9c5aaeda36dc | 229 | { |
madcowswe | 15:9c5aaeda36dc | 230 | *this = XprMatrix<ConstReference, Rows, Cols>(rhs.const_ref()); |
madcowswe | 15:9c5aaeda36dc | 231 | } |
madcowswe | 15:9c5aaeda36dc | 232 | |
madcowswe | 15:9c5aaeda36dc | 233 | /** |
madcowswe | 15:9c5aaeda36dc | 234 | * Constructor with STL iterator interface. The data will be copied into the matrix |
madcowswe | 15:9c5aaeda36dc | 235 | * self, there isn't any stored reference to the array pointer. |
madcowswe | 15:9c5aaeda36dc | 236 | */ |
madcowswe | 15:9c5aaeda36dc | 237 | template<class InputIterator> |
madcowswe | 15:9c5aaeda36dc | 238 | explicit Matrix(InputIterator first, InputIterator last) |
madcowswe | 15:9c5aaeda36dc | 239 | #if defined(TVMET_DYNAMIC_MEMORY) |
madcowswe | 15:9c5aaeda36dc | 240 | : m_data( new value_type[Size] ) |
madcowswe | 15:9c5aaeda36dc | 241 | #endif |
madcowswe | 15:9c5aaeda36dc | 242 | { |
madcowswe | 15:9c5aaeda36dc | 243 | TVMET_RT_CONDITION(static_cast<std::size_t>(std::distance(first, last)) <= Size, |
madcowswe | 15:9c5aaeda36dc | 244 | "InputIterator doesn't fits in size" ) |
madcowswe | 15:9c5aaeda36dc | 245 | std::copy(first, last, m_data); |
madcowswe | 15:9c5aaeda36dc | 246 | } |
madcowswe | 15:9c5aaeda36dc | 247 | |
madcowswe | 15:9c5aaeda36dc | 248 | /** |
madcowswe | 15:9c5aaeda36dc | 249 | * Constructor with STL iterator interface. The data will be copied into the matrix |
madcowswe | 15:9c5aaeda36dc | 250 | * self, there isn't any stored reference to the array pointer. |
madcowswe | 15:9c5aaeda36dc | 251 | */ |
madcowswe | 15:9c5aaeda36dc | 252 | template<class InputIterator> |
madcowswe | 15:9c5aaeda36dc | 253 | explicit Matrix(InputIterator first, std::size_t sz) |
madcowswe | 15:9c5aaeda36dc | 254 | #if defined(TVMET_DYNAMIC_MEMORY) |
madcowswe | 15:9c5aaeda36dc | 255 | : m_data( new value_type[Size] ) |
madcowswe | 15:9c5aaeda36dc | 256 | #endif |
madcowswe | 15:9c5aaeda36dc | 257 | { |
madcowswe | 15:9c5aaeda36dc | 258 | TVMET_RT_CONDITION(sz <= Size, "InputIterator doesn't fits in size" ) |
madcowswe | 15:9c5aaeda36dc | 259 | std::copy(first, first + sz, m_data); |
madcowswe | 15:9c5aaeda36dc | 260 | } |
madcowswe | 15:9c5aaeda36dc | 261 | |
madcowswe | 15:9c5aaeda36dc | 262 | /** Construct the matrix by value. */ |
madcowswe | 15:9c5aaeda36dc | 263 | explicit Matrix(value_type rhs) |
madcowswe | 15:9c5aaeda36dc | 264 | #if defined(TVMET_DYNAMIC_MEMORY) |
madcowswe | 15:9c5aaeda36dc | 265 | : m_data( new value_type[Size] ) |
madcowswe | 15:9c5aaeda36dc | 266 | #endif |
madcowswe | 15:9c5aaeda36dc | 267 | { |
madcowswe | 15:9c5aaeda36dc | 268 | typedef XprLiteral<value_type> expr_type; |
madcowswe | 15:9c5aaeda36dc | 269 | *this = XprMatrix<expr_type, Rows, Cols>(expr_type(rhs)); |
madcowswe | 15:9c5aaeda36dc | 270 | } |
madcowswe | 15:9c5aaeda36dc | 271 | |
madcowswe | 15:9c5aaeda36dc | 272 | /** Construct a matrix by expression. */ |
madcowswe | 15:9c5aaeda36dc | 273 | template<class E> |
madcowswe | 15:9c5aaeda36dc | 274 | explicit Matrix(const XprMatrix<E, Rows, Cols>& e) |
madcowswe | 15:9c5aaeda36dc | 275 | #if defined(TVMET_DYNAMIC_MEMORY) |
madcowswe | 15:9c5aaeda36dc | 276 | : m_data( new value_type[Size] ) |
madcowswe | 15:9c5aaeda36dc | 277 | #endif |
madcowswe | 15:9c5aaeda36dc | 278 | { |
madcowswe | 15:9c5aaeda36dc | 279 | *this = e; |
madcowswe | 15:9c5aaeda36dc | 280 | } |
madcowswe | 15:9c5aaeda36dc | 281 | |
madcowswe | 15:9c5aaeda36dc | 282 | /** assign a value_type on array, this can be used for a single value |
madcowswe | 15:9c5aaeda36dc | 283 | or a comma separeted list of values. */ |
madcowswe | 15:9c5aaeda36dc | 284 | CommaInitializer<Matrix, Size> operator=(value_type rhs) { |
madcowswe | 15:9c5aaeda36dc | 285 | return CommaInitializer<Matrix, Size>(*this, rhs); |
madcowswe | 15:9c5aaeda36dc | 286 | } |
madcowswe | 15:9c5aaeda36dc | 287 | |
madcowswe | 15:9c5aaeda36dc | 288 | public: // access operators |
madcowswe | 15:9c5aaeda36dc | 289 | value_type* _tvmet_restrict data() { return m_data; } |
madcowswe | 15:9c5aaeda36dc | 290 | const value_type* _tvmet_restrict data() const { return m_data; } |
madcowswe | 15:9c5aaeda36dc | 291 | |
madcowswe | 15:9c5aaeda36dc | 292 | public: // index access operators |
madcowswe | 15:9c5aaeda36dc | 293 | value_type& _tvmet_restrict operator()(std::size_t i, std::size_t j) { |
madcowswe | 15:9c5aaeda36dc | 294 | // Note: g++-2.95.3 does have problems on typedef reference |
madcowswe | 15:9c5aaeda36dc | 295 | TVMET_RT_CONDITION((i < Rows) && (j < Cols), "Matrix Bounce Violation") |
madcowswe | 15:9c5aaeda36dc | 296 | return m_data[i * Cols + j]; |
madcowswe | 15:9c5aaeda36dc | 297 | } |
madcowswe | 15:9c5aaeda36dc | 298 | |
madcowswe | 15:9c5aaeda36dc | 299 | value_type operator()(std::size_t i, std::size_t j) const { |
madcowswe | 15:9c5aaeda36dc | 300 | TVMET_RT_CONDITION((i < Rows) && (j < Cols), "Matrix Bounce Violation") |
madcowswe | 15:9c5aaeda36dc | 301 | return m_data[i * Cols + j]; |
madcowswe | 15:9c5aaeda36dc | 302 | } |
madcowswe | 15:9c5aaeda36dc | 303 | |
madcowswe | 15:9c5aaeda36dc | 304 | public: // ET interface |
madcowswe | 15:9c5aaeda36dc | 305 | typedef MatrixConstReference<T, Rows, Cols> ConstReference; |
madcowswe | 15:9c5aaeda36dc | 306 | |
madcowswe | 15:9c5aaeda36dc | 307 | typedef MatrixSliceConstReference< |
madcowswe | 15:9c5aaeda36dc | 308 | T, |
madcowswe | 15:9c5aaeda36dc | 309 | 0, Rows, 0, Cols, |
madcowswe | 15:9c5aaeda36dc | 310 | Rows, 1 |
madcowswe | 15:9c5aaeda36dc | 311 | > SliceConstReference; |
madcowswe | 15:9c5aaeda36dc | 312 | |
madcowswe | 15:9c5aaeda36dc | 313 | /** Return a const Reference of the internal data */ |
madcowswe | 15:9c5aaeda36dc | 314 | ConstReference const_ref() const { return ConstReference(*this); } |
madcowswe | 15:9c5aaeda36dc | 315 | |
madcowswe | 15:9c5aaeda36dc | 316 | /** |
madcowswe | 15:9c5aaeda36dc | 317 | * Return a sliced const Reference of the internal data. |
madcowswe | 15:9c5aaeda36dc | 318 | * \note Doesn't work since isn't implemented, but it is in |
madcowswe | 15:9c5aaeda36dc | 319 | * progress. Therefore this is a placeholder. */ |
madcowswe | 15:9c5aaeda36dc | 320 | ConstReference const_sliceref() const { return SliceConstReference(*this); } |
madcowswe | 15:9c5aaeda36dc | 321 | |
madcowswe | 15:9c5aaeda36dc | 322 | /** Return the vector as const expression. */ |
madcowswe | 15:9c5aaeda36dc | 323 | XprMatrix<ConstReference, Rows, Cols> as_expr() const { |
madcowswe | 15:9c5aaeda36dc | 324 | return XprMatrix<ConstReference, Rows, Cols>(this->const_ref()); |
madcowswe | 15:9c5aaeda36dc | 325 | } |
madcowswe | 15:9c5aaeda36dc | 326 | |
madcowswe | 15:9c5aaeda36dc | 327 | private: |
madcowswe | 15:9c5aaeda36dc | 328 | /** Wrapper for meta assign. */ |
madcowswe | 15:9c5aaeda36dc | 329 | template<class Dest, class Src, class Assign> |
madcowswe | 15:9c5aaeda36dc | 330 | static inline |
madcowswe | 15:9c5aaeda36dc | 331 | void do_assign(dispatch<true>, Dest& dest, const Src& src, const Assign& assign_fn) { |
madcowswe | 15:9c5aaeda36dc | 332 | meta::Matrix<Rows, Cols, 0, 0>::assign(dest, src, assign_fn); |
madcowswe | 15:9c5aaeda36dc | 333 | } |
madcowswe | 15:9c5aaeda36dc | 334 | |
madcowswe | 15:9c5aaeda36dc | 335 | /** Wrapper for loop assign. */ |
madcowswe | 15:9c5aaeda36dc | 336 | template<class Dest, class Src, class Assign> |
madcowswe | 15:9c5aaeda36dc | 337 | static inline |
madcowswe | 15:9c5aaeda36dc | 338 | void do_assign(dispatch<false>, Dest& dest, const Src& src, const Assign& assign_fn) { |
madcowswe | 15:9c5aaeda36dc | 339 | loop::Matrix<Rows, Cols>::assign(dest, src, assign_fn); |
madcowswe | 15:9c5aaeda36dc | 340 | } |
madcowswe | 15:9c5aaeda36dc | 341 | |
madcowswe | 15:9c5aaeda36dc | 342 | private: |
madcowswe | 15:9c5aaeda36dc | 343 | /** assign this to a matrix of a different type T2 using |
madcowswe | 15:9c5aaeda36dc | 344 | the functional assign_fn. */ |
madcowswe | 15:9c5aaeda36dc | 345 | template<class T2, class Assign> |
madcowswe | 15:9c5aaeda36dc | 346 | void assign_to(Matrix<T2, Rows, Cols>& dest, const Assign& assign_fn) const { |
madcowswe | 15:9c5aaeda36dc | 347 | do_assign(dispatch<use_meta>(), dest, *this, assign_fn); |
madcowswe | 15:9c5aaeda36dc | 348 | } |
madcowswe | 15:9c5aaeda36dc | 349 | |
madcowswe | 15:9c5aaeda36dc | 350 | public: // assign operations |
madcowswe | 15:9c5aaeda36dc | 351 | /** assign a given matrix of a different type T2 element wise |
madcowswe | 15:9c5aaeda36dc | 352 | to this matrix. The operator=(const Matrix&) is compiler |
madcowswe | 15:9c5aaeda36dc | 353 | generated. */ |
madcowswe | 15:9c5aaeda36dc | 354 | template<class T2> |
madcowswe | 15:9c5aaeda36dc | 355 | Matrix& operator=(const Matrix<T2, Rows, Cols>& rhs) { |
madcowswe | 15:9c5aaeda36dc | 356 | rhs.assign_to(*this, Fcnl_assign<value_type, T2>()); |
madcowswe | 15:9c5aaeda36dc | 357 | return *this; |
madcowswe | 15:9c5aaeda36dc | 358 | } |
madcowswe | 15:9c5aaeda36dc | 359 | |
madcowswe | 15:9c5aaeda36dc | 360 | /** assign a given XprMatrix element wise to this matrix. */ |
madcowswe | 15:9c5aaeda36dc | 361 | template <class E> |
madcowswe | 15:9c5aaeda36dc | 362 | Matrix& operator=(const XprMatrix<E, Rows, Cols>& rhs) { |
madcowswe | 15:9c5aaeda36dc | 363 | rhs.assign_to(*this, Fcnl_assign<value_type, typename E::value_type>()); |
madcowswe | 15:9c5aaeda36dc | 364 | return *this; |
madcowswe | 15:9c5aaeda36dc | 365 | } |
madcowswe | 15:9c5aaeda36dc | 366 | |
madcowswe | 15:9c5aaeda36dc | 367 | private: |
madcowswe | 15:9c5aaeda36dc | 368 | template<class Obj, std::size_t LEN> friend class CommaInitializer; |
madcowswe | 15:9c5aaeda36dc | 369 | |
madcowswe | 15:9c5aaeda36dc | 370 | /** This is a helper for assigning a comma separated initializer |
madcowswe | 15:9c5aaeda36dc | 371 | list. It's equal to Matrix& operator=(value_type) which does |
madcowswe | 15:9c5aaeda36dc | 372 | replace it. */ |
madcowswe | 15:9c5aaeda36dc | 373 | Matrix& assign_value(value_type rhs) { |
madcowswe | 15:9c5aaeda36dc | 374 | typedef XprLiteral<value_type> expr_type; |
madcowswe | 15:9c5aaeda36dc | 375 | *this = XprMatrix<expr_type, Rows, Cols>(expr_type(rhs)); |
madcowswe | 15:9c5aaeda36dc | 376 | return *this; |
madcowswe | 15:9c5aaeda36dc | 377 | } |
madcowswe | 15:9c5aaeda36dc | 378 | |
madcowswe | 15:9c5aaeda36dc | 379 | public: // math operators with scalars |
madcowswe | 15:9c5aaeda36dc | 380 | // NOTE: this meaning is clear - element wise ops even if not in ns element_wise |
madcowswe | 15:9c5aaeda36dc | 381 | Matrix& operator+=(value_type) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 382 | Matrix& operator-=(value_type) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 383 | Matrix& operator*=(value_type) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 384 | Matrix& operator/=(value_type) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 385 | |
madcowswe | 15:9c5aaeda36dc | 386 | Matrix& operator%=(std::size_t) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 387 | Matrix& operator^=(std::size_t) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 388 | Matrix& operator&=(std::size_t) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 389 | Matrix& operator|=(std::size_t) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 390 | Matrix& operator<<=(std::size_t) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 391 | Matrix& operator>>=(std::size_t) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 392 | |
madcowswe | 15:9c5aaeda36dc | 393 | public: // math operators with matrizes |
madcowswe | 15:9c5aaeda36dc | 394 | // NOTE: access using the operators in ns element_wise, since that's what is does |
madcowswe | 15:9c5aaeda36dc | 395 | template <class T2> Matrix& M_add_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 396 | template <class T2> Matrix& M_sub_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 397 | template <class T2> Matrix& M_mul_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 398 | template <class T2> Matrix& M_div_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 399 | template <class T2> Matrix& M_mod_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 400 | template <class T2> Matrix& M_xor_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 401 | template <class T2> Matrix& M_and_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 402 | template <class T2> Matrix& M_or_eq (const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 403 | template <class T2> Matrix& M_shl_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 404 | template <class T2> Matrix& M_shr_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 405 | |
madcowswe | 15:9c5aaeda36dc | 406 | public: // math operators with expressions |
madcowswe | 15:9c5aaeda36dc | 407 | // NOTE: access using the operators in ns element_wise, since that's what is does |
madcowswe | 15:9c5aaeda36dc | 408 | template <class E> Matrix& M_add_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 409 | template <class E> Matrix& M_sub_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 410 | template <class E> Matrix& M_mul_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 411 | template <class E> Matrix& M_div_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 412 | template <class E> Matrix& M_mod_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 413 | template <class E> Matrix& M_xor_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 414 | template <class E> Matrix& M_and_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 415 | template <class E> Matrix& M_or_eq (const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 416 | template <class E> Matrix& M_shl_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 417 | template <class E> Matrix& M_shr_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 418 | |
madcowswe | 15:9c5aaeda36dc | 419 | public: // aliased math operators with expressions |
madcowswe | 15:9c5aaeda36dc | 420 | template <class T2> Matrix& alias_assign(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 421 | template <class T2> Matrix& alias_add_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 422 | template <class T2> Matrix& alias_sub_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 423 | template <class T2> Matrix& alias_mul_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 424 | template <class T2> Matrix& alias_div_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 425 | |
madcowswe | 15:9c5aaeda36dc | 426 | template <class E> Matrix& alias_assign(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 427 | template <class E> Matrix& alias_add_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 428 | template <class E> Matrix& alias_sub_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 429 | template <class E> Matrix& alias_mul_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 430 | template <class E> Matrix& alias_div_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE; |
madcowswe | 15:9c5aaeda36dc | 431 | |
madcowswe | 15:9c5aaeda36dc | 432 | public: // io |
madcowswe | 15:9c5aaeda36dc | 433 | /** Structure for info printing as Matrix<T, Rows, Cols>. */ |
madcowswe | 15:9c5aaeda36dc | 434 | struct Info : public TvmetBase<Info> { |
madcowswe | 15:9c5aaeda36dc | 435 | std::ostream& print_xpr(std::ostream& os) const { |
madcowswe | 15:9c5aaeda36dc | 436 | os << "Matrix<T=" << typeid(value_type).name() |
madcowswe | 15:9c5aaeda36dc | 437 | << ", R=" << Rows << ", C=" << Cols << ">"; |
madcowswe | 15:9c5aaeda36dc | 438 | return os; |
madcowswe | 15:9c5aaeda36dc | 439 | } |
madcowswe | 15:9c5aaeda36dc | 440 | }; |
madcowswe | 15:9c5aaeda36dc | 441 | |
madcowswe | 15:9c5aaeda36dc | 442 | /** Get an info object of this matrix. */ |
madcowswe | 15:9c5aaeda36dc | 443 | static Info info() { return Info(); } |
madcowswe | 15:9c5aaeda36dc | 444 | |
madcowswe | 15:9c5aaeda36dc | 445 | /** Member function for expression level printing. */ |
madcowswe | 15:9c5aaeda36dc | 446 | std::ostream& print_xpr(std::ostream& os, std::size_t l=0) const; |
madcowswe | 15:9c5aaeda36dc | 447 | |
madcowswe | 15:9c5aaeda36dc | 448 | /** Member function for printing internal data. */ |
madcowswe | 15:9c5aaeda36dc | 449 | std::ostream& print_on(std::ostream& os) const; |
madcowswe | 15:9c5aaeda36dc | 450 | |
madcowswe | 15:9c5aaeda36dc | 451 | private: |
madcowswe | 15:9c5aaeda36dc | 452 | /** The data of matrix self. */ |
madcowswe | 15:9c5aaeda36dc | 453 | #if defined(TVMET_DYNAMIC_MEMORY) |
madcowswe | 15:9c5aaeda36dc | 454 | value_type* m_data; |
madcowswe | 15:9c5aaeda36dc | 455 | #else |
madcowswe | 15:9c5aaeda36dc | 456 | value_type m_data[Size]; |
madcowswe | 15:9c5aaeda36dc | 457 | #endif |
madcowswe | 15:9c5aaeda36dc | 458 | }; |
madcowswe | 15:9c5aaeda36dc | 459 | |
madcowswe | 15:9c5aaeda36dc | 460 | |
madcowswe | 15:9c5aaeda36dc | 461 | } // namespace tvmet |
madcowswe | 15:9c5aaeda36dc | 462 | |
madcowswe | 15:9c5aaeda36dc | 463 | #include <tvmet/MatrixImpl.h> |
madcowswe | 15:9c5aaeda36dc | 464 | #include <tvmet/MatrixFunctions.h> |
madcowswe | 15:9c5aaeda36dc | 465 | #include <tvmet/MatrixBinaryFunctions.h> |
madcowswe | 15:9c5aaeda36dc | 466 | #include <tvmet/MatrixUnaryFunctions.h> |
madcowswe | 15:9c5aaeda36dc | 467 | #include <tvmet/MatrixOperators.h> |
madcowswe | 15:9c5aaeda36dc | 468 | #include <tvmet/MatrixEval.h> |
madcowswe | 15:9c5aaeda36dc | 469 | #include <tvmet/AliasProxy.h> |
madcowswe | 15:9c5aaeda36dc | 470 | |
madcowswe | 15:9c5aaeda36dc | 471 | #endif // TVMET_MATRIX_H |
madcowswe | 15:9c5aaeda36dc | 472 | |
madcowswe | 15:9c5aaeda36dc | 473 | // Local Variables: |
madcowswe | 15:9c5aaeda36dc | 474 | // mode:C++ |
madcowswe | 15:9c5aaeda36dc | 475 | // tab-width:8 |
madcowswe | 15:9c5aaeda36dc | 476 | // End: |