Matrix.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
5 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_MATRIX_H
12 #define EIGEN_MATRIX_H
13 
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 template<typename Scalar_, int Rows_, int Cols_, int Options_, int MaxRows_, int MaxCols_>
20 struct traits<Matrix<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_> >
21 {
22 private:
23  constexpr static int size = internal::size_at_compile_time(Rows_,Cols_);
24  typedef typename find_best_packet<Scalar_,size>::type PacketScalar;
25  enum {
26  row_major_bit = Options_&RowMajor ? RowMajorBit : 0,
27  is_dynamic_size_storage = MaxRows_==Dynamic || MaxCols_==Dynamic,
28  max_size = is_dynamic_size_storage ? Dynamic : MaxRows_*MaxCols_,
29  default_alignment = compute_default_alignment<Scalar_,max_size>::value,
30  actual_alignment = ((Options_&DontAlign)==0) ? default_alignment : 0,
31  required_alignment = unpacket_traits<PacketScalar>::alignment,
32  packet_access_bit = (packet_traits<Scalar_>::Vectorizable && (EIGEN_UNALIGNED_VECTORIZE || (actual_alignment>=required_alignment))) ? PacketAccessBit : 0
33  };
34 
35 public:
36  typedef Scalar_ Scalar;
37  typedef Dense StorageKind;
38  typedef Eigen::Index StorageIndex;
39  typedef MatrixXpr XprKind;
40  enum {
41  RowsAtCompileTime = Rows_,
42  ColsAtCompileTime = Cols_,
43  MaxRowsAtCompileTime = MaxRows_,
44  MaxColsAtCompileTime = MaxCols_,
45  Flags = compute_matrix_flags(Options_),
46  Options = Options_,
47  InnerStrideAtCompileTime = 1,
48  OuterStrideAtCompileTime = (Options&RowMajor) ? ColsAtCompileTime : RowsAtCompileTime,
49 
50  // FIXME, the following flag in only used to define NeedsToAlign in PlainObjectBase
51  EvaluatorFlags = LinearAccessBit | DirectAccessBit | packet_access_bit | row_major_bit,
52  Alignment = actual_alignment
53  };
54 };
55 }
56 
179 template<typename Scalar_, int Rows_, int Cols_, int Options_, int MaxRows_, int MaxCols_>
180 class Matrix
181  : public PlainObjectBase<Matrix<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_> >
182 {
183  public:
184 
189 
190  enum { Options = Options_ };
191 
193 
194  typedef typename Base::PlainObject PlainObject;
195 
196  using Base::base;
197  using Base::coeffRef;
198 
208  EIGEN_STRONG_INLINE Matrix& operator=(const Matrix& other)
209  {
210  return Base::_set(other);
211  }
212 
223  template<typename OtherDerived>
225  EIGEN_STRONG_INLINE Matrix& operator=(const DenseBase<OtherDerived>& other)
226  {
227  return Base::_set(other);
228  }
229 
230  /* Here, doxygen failed to copy the brief information when using \copydoc */
231 
236  template<typename OtherDerived>
238  EIGEN_STRONG_INLINE Matrix& operator=(const EigenBase<OtherDerived> &other)
239  {
240  return Base::operator=(other);
241  }
242 
243  template<typename OtherDerived>
245  EIGEN_STRONG_INLINE Matrix& operator=(const ReturnByValue<OtherDerived>& func)
246  {
247  return Base::operator=(func);
248  }
249 
260  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
261  Matrix() : Base()
262  {
264  }
265 
266  // FIXME is it still needed
267  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
268  explicit Matrix(internal::constructor_without_unaligned_array_assert)
269  : Base(internal::constructor_without_unaligned_array_assert())
271 
272  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
273  Matrix(Matrix&& other) EIGEN_NOEXCEPT_IF(std::is_nothrow_move_constructible<Scalar>::value)
274  : Base(std::move(other)) {}
275  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
276  Matrix& operator=(Matrix&& other) EIGEN_NOEXCEPT_IF(std::is_nothrow_move_assignable<Scalar>::value)
277  {
278  Base::operator=(std::move(other));
279  return *this;
280  }
281 
289  template <typename... ArgTypes>
290  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
291  Matrix(const Scalar& a0, const Scalar& a1, const Scalar& a2, const Scalar& a3, const ArgTypes&... args)
292  : Base(a0, a1, a2, a3, args...) {}
293 
315  EIGEN_DEVICE_FUNC explicit constexpr EIGEN_STRONG_INLINE Matrix(
316  const std::initializer_list<std::initializer_list<Scalar>>& list)
317  : Base(list) {}
318 
319 #ifndef EIGEN_PARSED_BY_DOXYGEN
320 
321  // This constructor is for both 1x1 matrices and dynamic vectors
322  template<typename T>
323  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
324  explicit Matrix(const T& x)
325  {
326  Base::template _init1<T>(x);
327  }
328 
329  template<typename T0, typename T1>
330  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
331  Matrix(const T0& x, const T1& y)
332  {
333  Base::template _init2<T0,T1>(x, y);
334  }
335 
336 
337 #else
340  explicit Matrix(const Scalar *data);
341 
354  EIGEN_STRONG_INLINE explicit Matrix(Index dim);
357  Matrix(const Scalar& x);
372 
375  Matrix(const Scalar& x, const Scalar& y);
376  #endif // end EIGEN_PARSED_BY_DOXYGEN
377 
382  EIGEN_STRONG_INLINE Matrix(const Scalar& x, const Scalar& y, const Scalar& z)
383  {
385  m_storage.data()[0] = x;
386  m_storage.data()[1] = y;
387  m_storage.data()[2] = z;
388  }
393  EIGEN_STRONG_INLINE Matrix(const Scalar& x, const Scalar& y, const Scalar& z, const Scalar& w)
394  {
396  m_storage.data()[0] = x;
397  m_storage.data()[1] = y;
398  m_storage.data()[2] = z;
399  m_storage.data()[3] = w;
400  }
401 
402 
405  EIGEN_STRONG_INLINE Matrix(const Matrix& other) : Base(other)
406  { }
407 
411  template<typename OtherDerived>
413  EIGEN_STRONG_INLINE Matrix(const EigenBase<OtherDerived> &other)
414  : Base(other.derived())
415  { }
416 
418  inline Index innerStride() const EIGEN_NOEXCEPT { return 1; }
420  inline Index outerStride() const EIGEN_NOEXCEPT { return this->innerSize(); }
421 
423 
424  template<typename OtherDerived>
427  template<typename OtherDerived>
430 
431  // allow to extend Matrix outside Eigen
432  #ifdef EIGEN_MATRIX_PLUGIN
433  #include EIGEN_MATRIX_PLUGIN
434  #endif
435 
436  protected:
437  template <typename Derived, typename OtherDerived, bool IsVector>
438  friend struct internal::conservative_resize_like_impl;
439 
440  using Base::m_storage;
441 };
442 
472 #define EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \
473  \
474  \
475 typedef Matrix<Type, Size, Size> Matrix##SizeSuffix##TypeSuffix; \
476  \
477  \
478 typedef Matrix<Type, Size, 1> Vector##SizeSuffix##TypeSuffix; \
479  \
480  \
481 typedef Matrix<Type, 1, Size> RowVector##SizeSuffix##TypeSuffix;
482 
483 #define EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, Size) \
484  \
485  \
486 typedef Matrix<Type, Size, Dynamic> Matrix##Size##X##TypeSuffix; \
487  \
488  \
489 typedef Matrix<Type, Dynamic, Size> Matrix##X##Size##TypeSuffix;
490 
491 #define EIGEN_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \
492 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \
493 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 3, 3) \
494 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 4, 4) \
495 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Dynamic, X) \
496 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 2) \
497 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 3) \
498 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 4)
499 
503 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<float>, cf)
504 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<double>, cd)
505 
506 #undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES
507 #undef EIGEN_MAKE_TYPEDEFS
508 #undef EIGEN_MAKE_FIXED_TYPEDEFS
509 
510 #define EIGEN_MAKE_TYPEDEFS(Size, SizeSuffix) \
511  \
512  \
513 template <typename Type> \
514 using Matrix##SizeSuffix = Matrix<Type, Size, Size>; \
515  \
516  \
517 template <typename Type> \
518 using Vector##SizeSuffix = Matrix<Type, Size, 1>; \
519  \
520  \
521 template <typename Type> \
522 using RowVector##SizeSuffix = Matrix<Type, 1, Size>;
523 
524 #define EIGEN_MAKE_FIXED_TYPEDEFS(Size) \
525  \
526  \
527 template <typename Type> \
528 using Matrix##Size##X = Matrix<Type, Size, Dynamic>; \
529  \
530  \
531 template <typename Type> \
532 using Matrix##X##Size = Matrix<Type, Dynamic, Size>;
533 
541 
544 template <typename Type, int Size>
545 using Vector = Matrix<Type, Size, 1>;
546 
549 template <typename Type, int Size>
550 using RowVector = Matrix<Type, 1, Size>;
551 
552 #undef EIGEN_MAKE_TYPEDEFS
553 #undef EIGEN_MAKE_FIXED_TYPEDEFS
554 
555 } // end namespace Eigen
556 
557 #endif // EIGEN_MATRIX_H
#define EIGEN_UNALIGNED_VECTORIZE
#define EIGEN_NOEXCEPT_IF(x)
Definition: Macros.h:1261
#define EIGEN_NOEXCEPT
Definition: Macros.h:1260
#define EIGEN_CONSTEXPR
Definition: Macros.h:747
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1168
#define EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix)
Definition: Matrix.h:510
#define EIGEN_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix)
Definition: Matrix.h:491
#define EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, Size)
Definition: Matrix.h:524
#define EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
#define EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(TYPE, SIZE)
Definition: StaticAssert.h:51
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:42
std::conditional_t< internal::is_same< typename internal::traits< Derived >::XprKind, MatrixXpr >::value, PlainMatrix, PlainArray > PlainObject
The plain matrix or array type corresponding to this expression.
Definition: DenseBase.h:204
EIGEN_CONSTEXPR Index innerSize() const
Definition: DenseBase.h:224
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:61
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:182
Matrix(internal::constructor_without_unaligned_array_assert)
Definition: Matrix.h:268
Matrix & operator=(Matrix &&other) EIGEN_NOEXCEPT_IF(std
Definition: Matrix.h:276
Matrix(const Scalar &x, const Scalar &y, const Scalar &z)
Constructs an initialized 3D vector with given coefficients.
Definition: Matrix.h:382
Matrix(Index dim)
Constructs a vector or row-vector with given dimension. This is only for vectors (either row-vectors ...
Matrix & operator=(const EigenBase< OtherDerived > &other)
Copies the generic expression other into *this.
Definition: Matrix.h:238
Matrix(const EigenBase< OtherDerived > &other)
Copy constructor for generic expressions.
Definition: Matrix.h:413
Matrix()
Default constructor.
Definition: Matrix.h:261
Matrix & operator=(const DenseBase< OtherDerived > &other)
Definition: Matrix.h:225
EIGEN_CONSTEXPR Index outerStride() const EIGEN_NOEXCEPT
Definition: Matrix.h:420
Matrix(Index rows, Index cols)
Constructs an uninitialized matrix with rows rows and cols columns.
Matrix(const Scalar &a0, const Scalar &a1, const Scalar &a2, const Scalar &a3, const ArgTypes &... args)
Definition: Matrix.h:291
Matrix(const Scalar &x, const Scalar &y, const Scalar &z, const Scalar &w)
Constructs an initialized 4D vector with given coefficients.
Definition: Matrix.h:393
Matrix(const Matrix &other)
Copy constructor.
Definition: Matrix.h:405
constexpr Matrix(const std::initializer_list< std::initializer_list< Scalar >> &list)
Constructs a Matrix and initializes it from the coefficients given as initializer-lists grouped by ro...
Definition: Matrix.h:315
Matrix(const Scalar &x)
Constructs an initialized 1x1 matrix with the given coefficient.
Matrix(const Scalar *data)
Constructs a fixed-sized matrix initialized with coefficients starting at data.
PlainObjectBase< Matrix > Base
Base class typedef.
Definition: Matrix.h:188
Matrix(Matrix &&other) EIGEN_NOEXCEPT_IF(std
Definition: Matrix.h:273
Matrix & operator=(const RotationBase< OtherDerived, ColsAtCompileTime > &r)
EIGEN_CONSTEXPR Index innerStride() const EIGEN_NOEXCEPT
Definition: Matrix.h:418
Matrix(const Scalar &x, const Scalar &y)
Constructs an initialized 2D vector with given coefficients.
Matrix & operator=(const ReturnByValue< OtherDerived > &func)
Definition: Matrix.h:245
Dense storage base class for matrices and arrays.
Derived & _set(const DenseBase< OtherDerived > &other)
Copies the value of the expression other into *this with automatic resizing.
DenseStorage< Scalar, Base::MaxSizeAtCompileTime, Base::RowsAtCompileTime, Base::ColsAtCompileTime, Options > m_storage
Derived & operator=(const PlainObjectBase &other)
Common base class for compact rotation representations.
Definition: RotationBase.h:32
@ DontAlign
Definition: Constants.h:327
@ RowMajor
Definition: Constants.h:323
const unsigned int PacketAccessBit
Definition: Constants.h:96
const unsigned int LinearAccessBit
Definition: Constants.h:132
const unsigned int DirectAccessBit
Definition: Constants.h:157
const unsigned int RowMajorBit
Definition: Constants.h:68
constexpr unsigned compute_matrix_flags(int Options)
Definition: XprHelper.h:305
constexpr int size_at_compile_time(int rows, int cols)
Definition: XprHelper.h:313
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const int Dynamic
Definition: Constants.h:24
Definition: BFloat16.h:222
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41