Eigen  3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)
Matrix.h
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 
192  EIGEN_DENSE_PUBLIC_INTERFACE(Matrix)
193 
194  typedef typename Base::PlainObject PlainObject;
195 
196  using Base::base;
197  using Base::coeffRef;
198 
207  EIGEN_DEVICE_FUNC
208  EIGEN_STRONG_INLINE Matrix& operator=(const Matrix& other)
209  {
210  return Base::_set(other);
211  }
212 
223  template<typename OtherDerived>
224  EIGEN_DEVICE_FUNC
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>
237  EIGEN_DEVICE_FUNC
238  EIGEN_STRONG_INLINE Matrix& operator=(const EigenBase<OtherDerived> &other)
239  {
240  return Base::operator=(other);
241  }
242 
243  template<typename OtherDerived>
244  EIGEN_DEVICE_FUNC
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  {
263  EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
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())
270  { EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED }
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
316  explicit EIGEN_STRONG_INLINE Matrix(const std::initializer_list<std::initializer_list<Scalar>>& list) : Base(list) {}
317 
318 #ifndef EIGEN_PARSED_BY_DOXYGEN
319 
320  // This constructor is for both 1x1 matrices and dynamic vectors
321  template<typename T>
322  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
323  explicit Matrix(const T& x)
324  {
325  Base::template _init1<T>(x);
326  }
327 
328  template<typename T0, typename T1>
329  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
330  Matrix(const T0& x, const T1& y)
331  {
332  Base::template _init2<T0,T1>(x, y);
333  }
334 
335 
336 #else
338  EIGEN_DEVICE_FUNC
339  explicit Matrix(const Scalar *data);
340 
353  EIGEN_STRONG_INLINE explicit Matrix(Index dim);
356  Matrix(const Scalar& x);
369  EIGEN_DEVICE_FUNC
370  Matrix(Index rows, Index cols);
371 
374  Matrix(const Scalar& x, const Scalar& y);
375  #endif // end EIGEN_PARSED_BY_DOXYGEN
376 
380  EIGEN_DEVICE_FUNC
381  EIGEN_STRONG_INLINE Matrix(const Scalar& x, const Scalar& y, const Scalar& z)
382  {
383  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Matrix, 3)
384  m_storage.data()[0] = x;
385  m_storage.data()[1] = y;
386  m_storage.data()[2] = z;
387  }
391  EIGEN_DEVICE_FUNC
392  EIGEN_STRONG_INLINE Matrix(const Scalar& x, const Scalar& y, const Scalar& z, const Scalar& w)
393  {
394  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Matrix, 4)
395  m_storage.data()[0] = x;
396  m_storage.data()[1] = y;
397  m_storage.data()[2] = z;
398  m_storage.data()[3] = w;
399  }
400 
401 
403  EIGEN_DEVICE_FUNC
404  EIGEN_STRONG_INLINE Matrix(const Matrix& other) : Base(other)
405  { }
406 
410  template<typename OtherDerived>
411  EIGEN_DEVICE_FUNC
412  EIGEN_STRONG_INLINE Matrix(const EigenBase<OtherDerived> &other)
413  : Base(other.derived())
414  { }
415 
416  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
417  inline Index innerStride() const EIGEN_NOEXCEPT { return 1; }
418  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
419  inline Index outerStride() const EIGEN_NOEXCEPT { return this->innerSize(); }
420 
422 
423  template<typename OtherDerived>
424  EIGEN_DEVICE_FUNC
425  explicit Matrix(const RotationBase<OtherDerived,ColsAtCompileTime>& r);
426  template<typename OtherDerived>
427  EIGEN_DEVICE_FUNC
428  Matrix& operator=(const RotationBase<OtherDerived,ColsAtCompileTime>& r);
429 
430  // allow to extend Matrix outside Eigen
431  #ifdef EIGEN_MATRIX_PLUGIN
432  #include EIGEN_MATRIX_PLUGIN
433  #endif
434 
435  protected:
436  template <typename Derived, typename OtherDerived, bool IsVector>
437  friend struct internal::conservative_resize_like_impl;
438 
439  using Base::m_storage;
440 };
441 
471 #define EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \
472  \
473  \
474 typedef Matrix<Type, Size, Size> Matrix##SizeSuffix##TypeSuffix; \
475  \
476  \
477 typedef Matrix<Type, Size, 1> Vector##SizeSuffix##TypeSuffix; \
478  \
479  \
480 typedef Matrix<Type, 1, Size> RowVector##SizeSuffix##TypeSuffix;
481 
482 #define EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, Size) \
483  \
484  \
485 typedef Matrix<Type, Size, Dynamic> Matrix##Size##X##TypeSuffix; \
486  \
487  \
488 typedef Matrix<Type, Dynamic, Size> Matrix##X##Size##TypeSuffix;
489 
490 #define EIGEN_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \
491 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \
492 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 3, 3) \
493 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 4, 4) \
494 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Dynamic, X) \
495 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 2) \
496 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 3) \
497 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 4)
498 
499 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(int, i)
500 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(float, f)
501 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(double, d)
502 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<float>, cf)
503 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<double>, cd)
504 
505 #undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES
506 #undef EIGEN_MAKE_TYPEDEFS
507 #undef EIGEN_MAKE_FIXED_TYPEDEFS
508 
509 #define EIGEN_MAKE_TYPEDEFS(Size, SizeSuffix) \
510  \
511  \
512 template <typename Type> \
513 using Matrix##SizeSuffix = Matrix<Type, Size, Size>; \
514  \
515  \
516 template <typename Type> \
517 using Vector##SizeSuffix = Matrix<Type, Size, 1>; \
518  \
519  \
520 template <typename Type> \
521 using RowVector##SizeSuffix = Matrix<Type, 1, Size>;
522 
523 #define EIGEN_MAKE_FIXED_TYPEDEFS(Size) \
524  \
525  \
526 template <typename Type> \
527 using Matrix##Size##X = Matrix<Type, Size, Dynamic>; \
528  \
529  \
530 template <typename Type> \
531 using Matrix##X##Size = Matrix<Type, Dynamic, Size>;
532 
533 EIGEN_MAKE_TYPEDEFS(2, 2)
534 EIGEN_MAKE_TYPEDEFS(3, 3)
535 EIGEN_MAKE_TYPEDEFS(4, 4)
536 EIGEN_MAKE_TYPEDEFS(Dynamic, X)
537 EIGEN_MAKE_FIXED_TYPEDEFS(2)
538 EIGEN_MAKE_FIXED_TYPEDEFS(3)
539 EIGEN_MAKE_FIXED_TYPEDEFS(4)
540 
543 template <typename Type, int Size>
544 using Vector = Matrix<Type, Size, 1>;
545 
548 template <typename Type, int Size>
549 using RowVector = Matrix<Type, 1, Size>;
550 
551 #undef EIGEN_MAKE_TYPEDEFS
552 #undef EIGEN_MAKE_FIXED_TYPEDEFS
553 
554 } // end namespace Eigen
555 
556 #endif // EIGEN_MATRIX_H
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:42
EIGEN_CONSTEXPR Index innerSize() const
Definition: DenseBase.h:224
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:61
Derived & derived()
Definition: EigenBase.h:48
Scalar & z()
Definition: DenseCoeffsBase.h:453
Scalar & x()
Definition: DenseCoeffsBase.h:437
Scalar & w()
Definition: DenseCoeffsBase.h:463
Scalar & y()
Definition: DenseCoeffsBase.h:443
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:182
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:316
Matrix(const Scalar &x, const Scalar &y, const Scalar &z)
Constructs an initialized 3D vector with given coefficients.
Definition: Matrix.h:381
Matrix(Index dim)
Constructs a vector or row-vector with given dimension. This is only for vectors (either row-vectors ...
Matrix & operator=(const Matrix &other)
Assigns matrices to each other.
Definition: Matrix.h:208
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:412
Matrix()
Default constructor.
Definition: Matrix.h:261
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:392
Matrix(const Matrix &other)
Copy constructor.
Definition: Matrix.h:404
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(const Scalar &x, const Scalar &y)
Constructs an initialized 2D vector with given coefficients.
Dense storage base class for matrices and arrays.
Definition: PlainObjectBase.h:102
Derived & _set(const DenseBase< OtherDerived > &other)
Copies the value of the expression other into *this with automatic resizing.
Definition: PlainObjectBase.h:775
Scalar & coeffRef(Index rowId, Index colId)
Definition: PlainObjectBase.h:187
Derived & operator=(const PlainObjectBase &other)
Definition: PlainObjectBase.h:461
@ 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
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const int Dynamic
Definition: Constants.h:24
Definition: EigenBase.h:32
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41