Eurobot2012_Primary

Dependencies:   mbed Eurobot_2012_Primary

Committer:
narshu
Date:
Wed Oct 17 22:22:47 2012 +0000
Revision:
26:0995f61cb7b8
Parent:
25:143b19c1fb05
Eurobot 2012 Primary;

Who changed what in which revision?

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