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