aboutsummaryrefslogtreecommitdiffhomepage
path: root/tvmet-1.7.1/include/tvmet/Matrix.h
blob: 73e3d1a8cc28826f24bc49d8f8df24a16201cf5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
 * Tiny Vector Matrix Library
 * Dense Vector Matrix Libary of Tiny size using Expression Templates
 *
 * Copyright (C) 2001 - 2003 Olaf Petzold <opetzold@users.sourceforge.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * lesser General Public License for more details.
 *
 * You should have received a copy of the GNU lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: Matrix.h,v 1.54 2005/03/02 12:12:51 opetzold Exp $
 */

#ifndef TVMET_MATRIX_H
#define TVMET_MATRIX_H

#include <iterator> // reverse_iterator
#include <cassert>

#include <tvmet/tvmet.h>
#include <tvmet/TypePromotion.h>
#include <tvmet/CommaInitializer.h>

#include <tvmet/xpr/Matrix.h>
#include <tvmet/xpr/MatrixRow.h>
#include <tvmet/xpr/MatrixCol.h>
#include <tvmet/xpr/MatrixDiag.h>

namespace tvmet {


/* forwards */
template<class T, int Rows, int Cols> class Matrix;
template<class T,
	 int RowsBgn, int RowsEnd,
	 int ColsBgn, int ColsEnd,
	 int RowStride, int ColStride /*=1*/>
class MatrixSliceConstReference; // unused here; for me only


/**
 * \class MatrixConstReference Matrix.h "tvmet/Matrix.h"
 * \brief value iterator for ET
 */
template<class T, int NRows, int NCols>
class MatrixConstReference
  : public TvmetBase < MatrixConstReference<T, NRows, NCols> >
{
public:
  typedef T						value_type;
  typedef T*						pointer;
  typedef const T*					const_pointer;

  /** Dimensions. */
  enum {
    Rows = NRows,			/**< Number of rows. */
    Cols = NCols,			/**< Number of cols. */
    Size = Rows * Cols			/**< Complete Size of Matrix. */
  };

public:
  /** Complexity counter. */
  enum {
    ops       = Rows * Cols
  };

private:
  MatrixConstReference();
  MatrixConstReference& operator=(const MatrixConstReference&);

public:
  /** Constructor. */
  explicit MatrixConstReference(const Matrix<T, Rows, Cols>& rhs)
    : m_data(rhs.data())
  { }

  /** Constructor by a given memory pointer. */
  explicit MatrixConstReference(const_pointer data)
    : m_data(data)
  { }

public: // access operators
  /** access by index. */
  value_type operator()(int i, int j) const {
    assert((i < Rows) && (j < Cols));
    return m_data[i * Cols + j];
  }

public: // debugging Xpr parse tree
  void print_xpr(std::ostream& os, int l=0) const {
    os << IndentLevel(l)
       << "MatrixConstReference[O=" << ops << "]<"
       << "T=" << typeid(value_type).name() << ">,"
       << std::endl;
  }

private:
  const_pointer _tvmet_restrict 			m_data;
};


/**
 * \class Matrix Matrix.h "tvmet/Matrix.h"
 * \brief A tiny matrix class.
 *
 * The array syntax A[j][j] isn't supported here. The reason is that
 * operator[] always takes exactly one parameter, but operator() can
 * take any number of parameters (in the case of a rectangular matrix,
 * two paramters are needed). Therefore the cleanest way to do it is
 * with operator() rather than with operator[]. \see C++ FAQ Lite 13.8
 */
template<class T, int NRows, int NCols>
class Matrix
{
public:
  /** Data type of the tvmet::Matrix. */
  typedef T						value_type;

  /** Reference type of the tvmet::Matrix data elements. */
  typedef T&     					reference;

  /** const reference type of the tvmet::Matrix data elements. */
  typedef const T&     					const_reference;

  /** STL iterator interface. */
  typedef T*     					iterator;

  /** STL const_iterator interface. */
  typedef const T*     					const_iterator;

  /** STL reverse iterator interface. */
  typedef std::reverse_iterator<iterator> 		reverse_iterator;

  /** STL const reverse iterator interface. */
  typedef std::reverse_iterator<const_iterator> 	const_reverse_iterator;

public:
  /** Dimensions. */
  enum {
    Rows = NRows,			/**< Number of rows. */
    Cols = NCols,			/**< Number of cols. */
    Size = Rows * Cols			/**< Complete Size of Matrix. */
  };

public:
  /** Complexity counter. */
  enum {
    ops_assign = Rows * Cols,
    ops        = ops_assign,
    use_meta   = ops < TVMET_COMPLEXITY_M_ASSIGN_TRIGGER ? true : false
  };

public: // STL  interface
  /** STL iterator interface. */
  iterator begin() { return m_data; }

  /** STL iterator interface. */
  iterator end() { return m_data + Size; }

  /** STL const_iterator interface. */
  const_iterator begin() const { return m_data; }

  /** STL const_iterator interface. */
  const_iterator end() const { return m_data + Size; }

  /** STL reverse iterator interface reverse begin. */
  reverse_iterator rbegin() { return reverse_iterator( end() ); }

  /** STL const reverse iterator interface reverse begin. */
  const_reverse_iterator rbegin() const {
    return const_reverse_iterator( end() );
  }

  /** STL reverse iterator interface reverse end. */
  reverse_iterator rend() { return reverse_iterator( begin() ); }

  /** STL const reverse iterator interface reverse end. */
  const_reverse_iterator rend() const {
    return const_reverse_iterator( begin() );
  }

  /** The size of the matrix. */
  static int size() { return Size; }

  /** STL vector max_size() - returns allways rows()*cols(). */
  static int max_size() { return Size; }

  /** STL vector empty() - returns allways false. */
  static bool empty() { return false; }

public:
  /** The number of rows of matrix. */
  static int rows() { return Rows; }

  /** The number of columns of matrix. */
  static int cols() { return Cols; }

public:
  /** Default Destructor */
  ~Matrix() {}

  /** Default Constructor. The allocated memory region isn't cleared. If you want
   a clean use the constructor argument zero. */
  explicit Matrix() {}

  /** Copy Constructor, not explicit! */
  Matrix(const Matrix& rhs)
  {
    *this = XprMatrix<ConstReference, Rows, Cols>(rhs.const_ref());
  }

  /**
   * Constructor with STL iterator interface. The data will be copied into the matrix
   * self, there isn't any stored reference to the array pointer.
   */
  template<class InputIterator>
  explicit Matrix(InputIterator first, InputIterator last)
  {
    assert(static_cast<int>(std::distance(first, last)) <= Size);
    std::copy(first, last, m_data);
  }

  /**
   * Constructor with STL iterator interface. The data will be copied into the matrix
   * self, there isn't any stored reference to the array pointer.
   */
  template<class InputIterator>
  explicit Matrix(InputIterator first, int sz)
  {
    assert(sz <= Size);
    std::copy(first, first + sz, m_data);
  }

  /** Construct the matrix by value. */
  explicit Matrix(value_type rhs)
  {
    typedef XprLiteral<value_type> expr_type;
    *this = XprMatrix<expr_type, Rows, Cols>(expr_type(rhs));
  }

  /** Construct a matrix by expression. */
  template<class E>
  explicit Matrix(const XprMatrix<E, Rows, Cols>& e)
  {
    *this = e;
  }

  /** assign a value_type on array, this can be used for a single value
      or a comma separeted list of values. */
  CommaInitializer<Matrix, Size> operator=(value_type rhs) {
    return CommaInitializer<Matrix, Size>(*this, rhs);
  }

public: // access operators
  value_type* _tvmet_restrict data() { return m_data; }
  const value_type* _tvmet_restrict data() const { return m_data; }

public: // index access operators
  value_type& _tvmet_restrict operator()(int i, int j) {
    // Note: g++-2.95.3 does have problems on typedef reference
    assert((i < Rows) && (j < Cols));
    return m_data[i * Cols + j];
  }

  value_type operator()(int i, int j) const {
    assert((i < Rows) && (j < Cols));
    return m_data[i * Cols + j];
  }

public: // ET interface
  typedef MatrixConstReference<T, Rows, Cols>   	ConstReference;

  typedef MatrixSliceConstReference<
    T,
    0, Rows, 0, Cols,
    Rows, 1
  >							SliceConstReference;

  /** Return a const Reference of the internal data */
  ConstReference const_ref() const { return ConstReference(*this); }

  /**
   * Return a sliced const Reference of the internal data.
   * \note Doesn't work since isn't implemented, but it is in
   * progress. Therefore this is a placeholder. */
  ConstReference const_sliceref() const { return SliceConstReference(*this); }

  /** Return the vector as const expression. */
  XprMatrix<ConstReference, Rows, Cols> as_expr() const {
    return XprMatrix<ConstReference, Rows, Cols>(this->const_ref());
  }

private:
  /** Wrapper for meta assign. */
  template<class Dest, class Src, class Assign>
  static inline
  void do_assign(dispatch<true>, Dest& dest, const Src& src, const Assign& assign_fn) {
    meta::Matrix<Rows, Cols, 0, 0>::assign(dest, src, assign_fn);
  }

  /** Wrapper for loop assign. */
  template<class Dest, class Src, class Assign>
  static inline
  void do_assign(dispatch<false>, Dest& dest, const Src& src, const Assign& assign_fn) {
    loop::Matrix<Rows, Cols>::assign(dest, src, assign_fn);
  }

private:
  /** assign this to a matrix  of a different type T2 using
      the functional assign_fn. */
  template<class T2, class Assign>
  void assign_to(Matrix<T2, Rows, Cols>& dest, const Assign& assign_fn) const {
    do_assign(dispatch<use_meta>(), dest, *this, assign_fn);
  }

public:  // assign operations
  /** assign a given matrix of a different type T2 element wise
      to this matrix. The operator=(const Matrix&) is compiler
      generated. */
  template<class T2>
  Matrix& operator=(const Matrix<T2, Rows, Cols>& rhs) {
    rhs.assign_to(*this, Fcnl_assign<value_type, T2>());
    return *this;
  }

  /** assign a given XprMatrix element wise to this matrix. */
  template <class E>
  Matrix& operator=(const XprMatrix<E, Rows, Cols>& rhs) {
    rhs.assign_to(*this, Fcnl_assign<value_type, typename E::value_type>());
    return *this;
  }

private:
  template<class Obj, int LEN> friend class CommaInitializer;

  /** This is a helper for assigning a comma separated initializer
      list. It's equal to Matrix& operator=(value_type) which does
      replace it. */
  Matrix& assign_value(value_type rhs) {
    typedef XprLiteral<value_type> 			expr_type;
    *this = XprMatrix<expr_type, Rows, Cols>(expr_type(rhs));
    return *this;
  }

public: // math operators with scalars
  // NOTE: this meaning is clear - element wise ops even if not in ns element_wise
  Matrix& operator+=(value_type) TVMET_CXX_ALWAYS_INLINE;
  Matrix& operator-=(value_type) TVMET_CXX_ALWAYS_INLINE;
  Matrix& operator*=(value_type) TVMET_CXX_ALWAYS_INLINE;
  Matrix& operator/=(value_type) TVMET_CXX_ALWAYS_INLINE;

  Matrix& operator%=(int) TVMET_CXX_ALWAYS_INLINE;
  Matrix& operator^=(int) TVMET_CXX_ALWAYS_INLINE;
  Matrix& operator&=(int) TVMET_CXX_ALWAYS_INLINE;
  Matrix& operator|=(int) TVMET_CXX_ALWAYS_INLINE;
  Matrix& operator<<=(int) TVMET_CXX_ALWAYS_INLINE;
  Matrix& operator>>=(int) TVMET_CXX_ALWAYS_INLINE;

public: // math operators with matrizes
  // NOTE: access using the operators in ns element_wise, since that's what is does
  template <class T2> Matrix& M_add_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class T2> Matrix& M_sub_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class T2> Matrix& M_mul_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class T2> Matrix& M_div_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class T2> Matrix& M_mod_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class T2> Matrix& M_xor_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class T2> Matrix& M_and_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class T2> Matrix& M_or_eq (const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class T2> Matrix& M_shl_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class T2> Matrix& M_shr_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;

public: // math operators with expressions
  // NOTE: access using the operators in ns element_wise, since that's what is does
  template <class E> Matrix& M_add_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class E> Matrix& M_sub_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class E> Matrix& M_mul_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class E> Matrix& M_div_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class E> Matrix& M_mod_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class E> Matrix& M_xor_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class E> Matrix& M_and_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class E> Matrix& M_or_eq (const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class E> Matrix& M_shl_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class E> Matrix& M_shr_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;

public: // aliased math operators with expressions
  template <class T2> Matrix& alias_assign(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class T2> Matrix& alias_add_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class T2> Matrix& alias_sub_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class T2> Matrix& alias_mul_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class T2> Matrix& alias_div_eq(const Matrix<T2, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;

  template <class E> Matrix& alias_assign(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class E> Matrix& alias_add_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class E> Matrix& alias_sub_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class E> Matrix& alias_mul_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;
  template <class E> Matrix& alias_div_eq(const XprMatrix<E, Rows, Cols>&) TVMET_CXX_ALWAYS_INLINE;

public: // io
  /** Structure for info printing as Matrix<T, Rows, Cols>. */
  struct Info : public TvmetBase<Info> {
    std::ostream& print_xpr(std::ostream& os) const {
      os << "Matrix<T=" << typeid(value_type).name()
	 << ", R=" << Rows << ", C=" << Cols << ">";
      return os;
    }
  };

  /** Get an info object of this matrix. */
  static Info info() { return Info(); }

  /** Member function for expression level printing. */
  std::ostream& print_xpr(std::ostream& os, int l=0) const;

  /** Member function for printing internal data. */
  std::ostream& print_on(std::ostream& os) const;

private:
  /** The data of matrix self. */
  value_type m_data[Size];
};

typedef Matrix<int, 2, 2> Matrix2i;
typedef Matrix<int, 3, 3> Matrix3i;
typedef Matrix<int, 4, 4> Matrix4i;
typedef Matrix<float, 2, 2> Matrix2f;
typedef Matrix<float, 3, 3> Matrix3f;
typedef Matrix<float, 4, 4> Matrix4f;
typedef Matrix<double, 2, 2> Matrix2d;
typedef Matrix<double, 3, 3> Matrix3d;
typedef Matrix<double, 4, 4> Matrix4d;

} // namespace tvmet

#include <tvmet/MatrixImpl.h>
#include <tvmet/MatrixFunctions.h>
#include <tvmet/MatrixBinaryFunctions.h>
#include <tvmet/MatrixUnaryFunctions.h>
#include <tvmet/MatrixOperators.h>
#include <tvmet/MatrixEval.h>
#include <tvmet/AliasProxy.h>

#endif // TVMET_MATRIX_H

// Local Variables:
// mode:C++
// End: