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