Eigen  5.0.1-dev+7c7d8473
 
Loading...
Searching...
No Matches
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// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20template <typename Scalar_, int Rows_, int Cols_, int Options_, int MaxRows_, int MaxCols_>
21struct traits<Matrix<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_>> {
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 &&
33 (EIGEN_UNALIGNED_VECTORIZE || (int(actual_alignment) >= int(required_alignment))))
35 : 0
36 };
37
38 public:
39 typedef Scalar_ Scalar;
40 typedef Dense StorageKind;
41 typedef Eigen::Index StorageIndex;
42 typedef MatrixXpr XprKind;
43 enum {
44 RowsAtCompileTime = Rows_,
45 ColsAtCompileTime = Cols_,
46 MaxRowsAtCompileTime = MaxRows_,
47 MaxColsAtCompileTime = MaxCols_,
48 Flags = compute_matrix_flags(Options_),
49 Options = Options_,
50 InnerStrideAtCompileTime = 1,
51 OuterStrideAtCompileTime = (int(Options) & int(RowMajor)) ? ColsAtCompileTime : RowsAtCompileTime,
52
53 // FIXME, the following flag in only used to define NeedsToAlign in PlainObjectBase
54 EvaluatorFlags = LinearAccessBit | DirectAccessBit | packet_access_bit | row_major_bit,
55 Alignment = actual_alignment
56 };
57};
58} // namespace internal
59
184
185template <typename Scalar_, int Rows_, int Cols_, int Options_, int MaxRows_, int MaxCols_>
186class Matrix : public PlainObjectBase<Matrix<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_>> {
187 public:
191 typedef PlainObjectBase<Matrix> Base;
192
193 enum { Options = Options_ };
194
195 EIGEN_DENSE_PUBLIC_INTERFACE(Matrix)
196
197 typedef typename Base::PlainObject PlainObject;
198
199 using Base::base;
200 using Base::coeffRef;
201
210 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Matrix& operator=(const Matrix& other) { return Base::_set(other); }
211
222 template <typename OtherDerived>
223 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Matrix& operator=(const DenseBase<OtherDerived>& other) {
224 return Base::_set(other);
225 }
226
231 template <typename OtherDerived>
232 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Matrix& operator=(const EigenBase<OtherDerived>& other) {
233 return Base::operator=(other);
234 }
235
236 template <typename OtherDerived>
237 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Matrix& operator=(const ReturnByValue<OtherDerived>& func) {
238 return Base::operator=(func);
239 }
240
251#if defined(EIGEN_INITIALIZE_COEFFS)
252 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Matrix() { EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED }
253#else
254 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Matrix() = default;
255#endif
257 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Matrix(Matrix&&) = default;
261 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Matrix& operator=(Matrix&& other) noexcept(
262 std::is_nothrow_move_assignable<Scalar>::value) {
263 Base::operator=(std::move(other));
264 return *this;
265 }
266
282 template <typename... ArgTypes>
283 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Matrix(const Scalar& a0, const Scalar& a1, const Scalar& a2, const Scalar& a3,
284 const ArgTypes&... args)
285 : Base(a0, a1, a2, a3, args...) {}
286
311 EIGEN_DEVICE_FUNC explicit constexpr EIGEN_STRONG_INLINE Matrix(
312 const std::initializer_list<std::initializer_list<Scalar>>& list)
313 : Base(list) {}
314
315#ifndef EIGEN_PARSED_BY_DOXYGEN
316
317 // This constructor is for both 1x1 matrices and dynamic vectors
318 template <typename T>
319 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit Matrix(const T& x) {
320 Base::template _init1<T>(x);
321 }
322
323 template <typename T0, typename T1>
324 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Matrix(const T0& x, const T1& y) {
325 Base::template _init2<T0, T1>(x, y);
326 }
327
328#else
330 EIGEN_DEVICE_FUNC explicit Matrix(const Scalar* data);
331
344 EIGEN_STRONG_INLINE explicit Matrix(Index dim);
347 Matrix(const Scalar& x);
360 EIGEN_DEVICE_FUNC Matrix(Index rows, Index cols);
361
364 Matrix(const Scalar& x, const Scalar& y);
365#endif // end EIGEN_PARSED_BY_DOXYGEN
366
370 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Matrix(const Scalar& x, const Scalar& y, const Scalar& z) {
371 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Matrix, 3)
372 m_storage.data()[0] = x;
373 m_storage.data()[1] = y;
374 m_storage.data()[2] = z;
375 }
376
379 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Matrix(const Scalar& x, const Scalar& y, const Scalar& z, const Scalar& w) {
380 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Matrix, 4)
381 m_storage.data()[0] = x;
382 m_storage.data()[1] = y;
383 m_storage.data()[2] = z;
384 m_storage.data()[3] = w;
385 }
386
388 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Matrix(const Matrix&) = default;
389
393 template <typename OtherDerived>
394 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Matrix(const EigenBase<OtherDerived>& other) : Base(other.derived()) {}
395
396 EIGEN_DEVICE_FUNC constexpr Index innerStride() const noexcept { return 1; }
397 EIGEN_DEVICE_FUNC constexpr Index outerStride() const noexcept { return this->innerSize(); }
398
400
401 template <typename OtherDerived>
402 EIGEN_DEVICE_FUNC explicit Matrix(const RotationBase<OtherDerived, ColsAtCompileTime>& r);
403 template <typename OtherDerived>
405
406// allow to extend Matrix outside Eigen
407#ifdef EIGEN_MATRIX_PLUGIN
408#include EIGEN_MATRIX_PLUGIN
409#endif
410
411 protected:
412 template <typename Derived, typename OtherDerived, bool IsVector>
413 friend struct internal::conservative_resize_like_impl;
414
415 using Base::m_storage;
416};
417
448
449#define EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \
450 \
451 \
452 typedef Matrix<Type, Size, Size> Matrix##SizeSuffix##TypeSuffix; \
453 \
454 \
455 typedef Matrix<Type, Size, 1> Vector##SizeSuffix##TypeSuffix; \
456 \
457 \
458 typedef Matrix<Type, 1, Size> RowVector##SizeSuffix##TypeSuffix;
459
460#define EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, Size) \
461 \
462 \
463 typedef Matrix<Type, Size, Dynamic> Matrix##Size##X##TypeSuffix; \
464 \
465 \
466 typedef Matrix<Type, Dynamic, Size> Matrix##X##Size##TypeSuffix;
467
468#define EIGEN_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \
469 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \
470 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 3, 3) \
471 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 4, 4) \
472 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Dynamic, X) \
473 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 2) \
474 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 3) \
475 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 4)
476
477EIGEN_MAKE_TYPEDEFS_ALL_SIZES(int, i)
478EIGEN_MAKE_TYPEDEFS_ALL_SIZES(float, f)
479EIGEN_MAKE_TYPEDEFS_ALL_SIZES(double, d)
480EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<float>, cf)
481EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<double>, cd)
482
483#undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES
484#undef EIGEN_MAKE_TYPEDEFS
485#undef EIGEN_MAKE_FIXED_TYPEDEFS
486
487#define EIGEN_MAKE_TYPEDEFS(Size, SizeSuffix) \
488 \
489 \
490 template <typename Type> \
491 using Matrix##SizeSuffix = Matrix<Type, Size, Size>; \
492 \
493 \
494 template <typename Type> \
495 using Vector##SizeSuffix = Matrix<Type, Size, 1>; \
496 \
497 \
498 template <typename Type> \
499 using RowVector##SizeSuffix = Matrix<Type, 1, Size>;
500
501#define EIGEN_MAKE_FIXED_TYPEDEFS(Size) \
502 \
503 \
504 template <typename Type> \
505 using Matrix##Size##X = Matrix<Type, Size, Dynamic>; \
506 \
507 \
508 template <typename Type> \
509 using Matrix##X##Size = Matrix<Type, Dynamic, Size>;
510
511EIGEN_MAKE_TYPEDEFS(2, 2)
512EIGEN_MAKE_TYPEDEFS(3, 3)
513EIGEN_MAKE_TYPEDEFS(4, 4)
514EIGEN_MAKE_TYPEDEFS(Dynamic, X)
515EIGEN_MAKE_FIXED_TYPEDEFS(2)
516EIGEN_MAKE_FIXED_TYPEDEFS(3)
517EIGEN_MAKE_FIXED_TYPEDEFS(4)
518
521template <typename Type, int Size>
522using Vector = Matrix<Type, Size, 1>;
523
526template <typename Type, int Size>
527using RowVector = Matrix<Type, 1, Size>;
528
529#undef EIGEN_MAKE_TYPEDEFS
530#undef EIGEN_MAKE_FIXED_TYPEDEFS
531
532} // end namespace Eigen
533
534#endif // EIGEN_MATRIX_H
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:44
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:186
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:311
Matrix(const Scalar &x, const Scalar &y, const Scalar &z)
Constructs an initialized 3D vector with given coefficients.
Definition Matrix.h:370
Matrix(Index dim)
Constructs a vector or row-vector with given dimension. This is only for vectors (either row-vectors ...
constexpr Matrix()=default
Default constructor.
Matrix(const EigenBase< OtherDerived > &other)
Copy constructor for generic expressions.
Definition Matrix.h:394
constexpr Matrix(Matrix &&)=default
Move constructor.
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)
Construct a row of column vector with fixed size from an arbitrary number of coefficients.
Definition Matrix.h:283
Matrix(const Scalar &x, const Scalar &y, const Scalar &z, const Scalar &w)
Constructs an initialized 4D vector with given coefficients.
Definition Matrix.h:379
constexpr Matrix & operator=(Matrix &&other) noexcept(std::is_nothrow_move_assignable< Scalar >::value)
Moves the matrix into the other one.
Definition Matrix.h:261
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.
Matrix(const RotationBase< OtherDerived, ColsAtCompileTime > &r)
Constructs a Dim x Dim rotation matrix from the rotation r.
Definition RotationBase.h:148
PlainObjectBase< Matrix > Base
Definition Matrix.h:191
constexpr Matrix & operator=(const Matrix &other)
Assigns matrices to each other.
Definition Matrix.h:210
Matrix(const Scalar &x, const Scalar &y)
Constructs an initialized 2D vector with given coefficients.
Matrix & operator=(const EigenBase< OtherDerived > &other)
Copies the generic expression other into *this.
Definition Matrix.h:232
constexpr Matrix(const Matrix &)=default
Copy constructor.
constexpr Matrix & _set(const DenseBase< OtherDerived > &other)
Definition PlainObjectBase.h:717
constexpr const Scalar * data() const
Definition PlainObjectBase.h:247
constexpr Matrix & operator=(const PlainObjectBase &other)
Definition PlainObjectBase.h:429
Common base class for compact rotation representations.
Definition RotationBase.h:32
@ DontAlign
Definition Constants.h:324
@ RowMajor
Definition Constants.h:320
const unsigned int PacketAccessBit
Definition Constants.h:97
const unsigned int LinearAccessBit
Definition Constants.h:133
const unsigned int DirectAccessBit
Definition Constants.h:159
const unsigned int RowMajorBit
Definition Constants.h:70
Matrix< Type, 1, Size > RowVector
[c++11] 1×Size vector of type Type.
Definition Matrix.h:527
Matrix< Type, Size, 1 > Vector
[c++11] Size×1 vector of type Type.
Definition Matrix.h:522
Namespace containing all symbols from the Eigen library.
Definition B01_Experimental.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:82
const int Dynamic
Definition Constants.h:25
Definition EigenBase.h:33