Eigen  5.0.1
 
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// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_MATRIX_H
13#define EIGEN_MATRIX_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
20namespace internal {
21template <typename Scalar_, int Rows_, int Cols_, int Options_, int MaxRows_, int MaxCols_>
22struct traits<Matrix<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_>> {
23 private:
24 constexpr static int size = internal::size_at_compile_time(Rows_, Cols_);
25 using PacketScalar = typename find_best_packet<Scalar_, size>::type;
26 enum {
27 row_major_bit = Options_ & RowMajor ? RowMajorBit : 0,
28 is_dynamic_size_storage = MaxRows_ == Dynamic || MaxCols_ == Dynamic,
29 max_size = is_dynamic_size_storage ? Dynamic : MaxRows_ * MaxCols_,
30 default_alignment = compute_default_alignment<Scalar_, max_size>::value,
31 actual_alignment = ((Options_ & DontAlign) == 0) ? default_alignment : 0,
32 required_alignment = unpacket_traits<PacketScalar>::alignment,
33 packet_access_bit = (packet_traits<Scalar_>::Vectorizable &&
34 (EIGEN_UNALIGNED_VECTORIZE || (int(actual_alignment) >= int(required_alignment))))
36 : 0
37 };
38
39 public:
40 using Scalar = Scalar_;
41 using StorageKind = Dense;
42 using StorageIndex = Eigen::Index;
43 using XprKind = MatrixXpr;
44 enum {
45 RowsAtCompileTime = Rows_,
46 ColsAtCompileTime = Cols_,
47 MaxRowsAtCompileTime = MaxRows_,
48 MaxColsAtCompileTime = MaxCols_,
49 Flags = compute_matrix_flags(Options_),
50 Options = Options_,
51 InnerStrideAtCompileTime = 1,
52 OuterStrideAtCompileTime = (int(Options) & int(RowMajor)) ? ColsAtCompileTime : RowsAtCompileTime,
53
54 // FIXME, the following flag in only used to define NeedsToAlign in PlainObjectBase
55 EvaluatorFlags = LinearAccessBit | DirectAccessBit | packet_access_bit | row_major_bit,
56 Alignment = actual_alignment
57 };
58};
59} // namespace internal
60
186
187template <typename Scalar_, int Rows_, int Cols_, int Options_, int MaxRows_, int MaxCols_>
188class Matrix : public PlainObjectBase<Matrix<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_>> {
189 public:
193 using Base = PlainObjectBase<Matrix>;
194
195 enum { Options = Options_ };
196
197 EIGEN_DENSE_PUBLIC_INTERFACE(Matrix)
198
199 using PlainObject = typename Base::PlainObject;
200
201 using Base::base;
202 using Base::coeffRef;
203
212 EIGEN_DEVICE_FUNC constexpr Matrix& operator=(const Matrix& other) { return Base::_set(other); }
213
224 template <typename OtherDerived>
225 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Matrix& operator=(const DenseBase<OtherDerived>& other) {
226 return Base::_set(other);
227 }
228
233 template <typename OtherDerived>
234 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Matrix& operator=(const EigenBase<OtherDerived>& other) {
235 return Base::operator=(other);
236 }
237
238 template <typename OtherDerived>
239 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Matrix& operator=(const ReturnByValue<OtherDerived>& func) {
240 return Base::operator=(func);
241 }
242
257#if defined(EIGEN_INITIALIZE_COEFFS)
258 EIGEN_DEVICE_FUNC constexpr Matrix() { EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED }
259#else
260 EIGEN_DEVICE_FUNC constexpr Matrix() = default;
261#endif
263 EIGEN_DEVICE_FUNC constexpr Matrix(Matrix&&) = default;
267 EIGEN_DEVICE_FUNC constexpr Matrix& operator=(Matrix&& other) noexcept(
268 std::is_nothrow_move_assignable<Scalar>::value) {
269 this->m_storage = std::move(other.m_storage);
270 return *this;
271 }
272
288 template <typename... ArgTypes>
289 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Matrix(const Scalar& a0, const Scalar& a1, const Scalar& a2, const Scalar& a3,
290 const ArgTypes&... args)
291 : Base(a0, a1, a2, a3, args...) {}
292
316 EIGEN_DEVICE_FUNC explicit constexpr EIGEN_STRONG_INLINE Matrix(
317 const std::initializer_list<std::initializer_list<Scalar>>& list)
318 : Base(list) {}
319
320#ifndef EIGEN_PARSED_BY_DOXYGEN
321
322 // This constructor is for both 1x1 matrices and dynamic vectors
323 template <typename T>
324 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE explicit Matrix(const T& x) {
325 Base::template _init1<T>(x);
326 }
327
328 template <typename T0, typename T1>
329 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Matrix(const T0& x, const T1& y) {
330 Base::template _init2<T0, T1>(x, y);
331 }
332
333#else
335 EIGEN_DEVICE_FUNC explicit Matrix(const Scalar* data);
336
349 EIGEN_STRONG_INLINE explicit Matrix(Index dim);
352 Matrix(const Scalar& x);
365 EIGEN_DEVICE_FUNC Matrix(Index rows, Index cols);
366
369 Matrix(const Scalar& x, const Scalar& y);
370#endif // end EIGEN_PARSED_BY_DOXYGEN
371
375 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Matrix(const Scalar& x, const Scalar& y, const Scalar& z) {
376 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Matrix, 3)
377 m_storage.data()[0] = x;
378 m_storage.data()[1] = y;
379 m_storage.data()[2] = z;
380 }
381
384 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Matrix(const Scalar& x, const Scalar& y, const Scalar& z,
385 const Scalar& w) {
386 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Matrix, 4)
387 m_storage.data()[0] = x;
388 m_storage.data()[1] = y;
389 m_storage.data()[2] = z;
390 m_storage.data()[3] = w;
391 }
392
394 EIGEN_DEVICE_FUNC constexpr Matrix(const Matrix&) = default;
395
401 template <typename OtherDerived,
402 std::enable_if_t<internal::is_valid_dense_conversion<Scalar, OtherDerived>::value, int> = 0>
403 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Matrix(const EigenBase<OtherDerived>& other)
404 : Base(other.derived()) {}
405
406 EIGEN_DEVICE_FUNC constexpr Index innerStride() const noexcept { return 1; }
407 EIGEN_DEVICE_FUNC constexpr Index outerStride() const noexcept { return this->innerSize(); }
408
410
411 template <typename OtherDerived>
412 EIGEN_DEVICE_FUNC explicit Matrix(const RotationBase<OtherDerived, ColsAtCompileTime>& r);
413 template <typename OtherDerived>
415
416// allow to extend Matrix outside Eigen
417#ifdef EIGEN_MATRIX_PLUGIN
418#include EIGEN_MATRIX_PLUGIN
419#endif
420
421 protected:
422 template <typename Derived, typename OtherDerived, bool IsVector>
423 friend struct internal::conservative_resize_like_impl;
424
425 using Base::m_storage;
426};
427
458
459#define EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \
460 \
461 \
462 typedef Matrix<Type, Size, Size> Matrix##SizeSuffix##TypeSuffix; \
463 \
464 \
465 typedef Matrix<Type, Size, 1> Vector##SizeSuffix##TypeSuffix; \
466 \
467 \
468 typedef Matrix<Type, 1, Size> RowVector##SizeSuffix##TypeSuffix;
469
470#define EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, Size) \
471 \
472 \
473 typedef Matrix<Type, Size, Dynamic> Matrix##Size##X##TypeSuffix; \
474 \
475 \
476 typedef Matrix<Type, Dynamic, Size> Matrix##X##Size##TypeSuffix;
477
478#define EIGEN_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \
479 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \
480 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 3, 3) \
481 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 4, 4) \
482 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Dynamic, X) \
483 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 2) \
484 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 3) \
485 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 4)
486
487EIGEN_MAKE_TYPEDEFS_ALL_SIZES(int, i)
488EIGEN_MAKE_TYPEDEFS_ALL_SIZES(float, f)
489EIGEN_MAKE_TYPEDEFS_ALL_SIZES(double, d)
490EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<float>, cf)
491EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<double>, cd)
492
493#undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES
494#undef EIGEN_MAKE_TYPEDEFS
495#undef EIGEN_MAKE_FIXED_TYPEDEFS
496
497#define EIGEN_MAKE_TYPEDEFS(Size, SizeSuffix) \
498 \
499 \
500 template <typename Type> \
501 using Matrix##SizeSuffix = Matrix<Type, Size, Size>; \
502 \
503 \
504 template <typename Type> \
505 using Vector##SizeSuffix = Matrix<Type, Size, 1>; \
506 \
507 \
508 template <typename Type> \
509 using RowVector##SizeSuffix = Matrix<Type, 1, Size>;
510
511#define EIGEN_MAKE_FIXED_TYPEDEFS(Size) \
512 \
513 \
514 template <typename Type> \
515 using Matrix##Size##X = Matrix<Type, Size, Dynamic>; \
516 \
517 \
518 template <typename Type> \
519 using Matrix##X##Size = Matrix<Type, Dynamic, Size>;
520
521EIGEN_MAKE_TYPEDEFS(2, 2)
522EIGEN_MAKE_TYPEDEFS(3, 3)
523EIGEN_MAKE_TYPEDEFS(4, 4)
524EIGEN_MAKE_TYPEDEFS(Dynamic, X)
525EIGEN_MAKE_FIXED_TYPEDEFS(2)
526EIGEN_MAKE_FIXED_TYPEDEFS(3)
527EIGEN_MAKE_FIXED_TYPEDEFS(4)
528
531template <typename Type, int Size>
532using Vector = Matrix<Type, Size, 1>;
533
536template <typename Type, int Size>
537using RowVector = Matrix<Type, 1, Size>;
538
539#undef EIGEN_MAKE_TYPEDEFS
540#undef EIGEN_MAKE_FIXED_TYPEDEFS
541
542} // end namespace Eigen
543
544#endif // EIGEN_MATRIX_H
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:45
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
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:316
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.
constexpr Matrix(Matrix &&)=default
Move constructor.
constexpr Matrix(const Scalar &x, const Scalar &y, const Scalar &z, const Scalar &w)
Constructs an initialized 4D vector with given coefficients.
Definition Matrix.h:384
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 or column vector with fixed size from an arbitrary number of coefficients.
Definition Matrix.h:289
constexpr Matrix & operator=(Matrix &&other) noexcept(std::is_nothrow_move_assignable< Scalar >::value)
Moves the matrix into the other one.
Definition Matrix.h:267
constexpr Matrix(const EigenBase< OtherDerived > &other)
Copy constructor for generic expressions. For coefficient-wise dense expressions, implicit conversion...
Definition Matrix.h:403
constexpr Matrix(const Scalar &x, const Scalar &y, const Scalar &z)
Constructs an initialized 3D vector with given coefficients.
Definition Matrix.h:375
PlainObjectBase< Matrix > Base
Definition Matrix.h:193
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:149
constexpr Matrix & operator=(const Matrix &other)
Assigns matrices to each other.
Definition Matrix.h:212
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:234
constexpr Matrix(const Matrix &)=default
Copy constructor.
constexpr Matrix & _set(const DenseBase< OtherDerived > &other)
Definition PlainObjectBase.h:734
constexpr const Scalar * data() const
Definition PlainObjectBase.h:261
constexpr Matrix & operator=(const PlainObjectBase &other)
Definition PlainObjectBase.h:445
Common base class for compact rotation representations.
Definition RotationBase.h:33
@ DontAlign
Definition Constants.h:325
@ RowMajor
Definition Constants.h:321
constexpr unsigned int PacketAccessBit
Definition Constants.h:98
constexpr unsigned int DirectAccessBit
Definition Constants.h:160
constexpr unsigned int LinearAccessBit
Definition Constants.h:134
constexpr unsigned int RowMajorBit
Definition Constants.h:71
Matrix< Type, 1, Size > RowVector
1×Size vector of type Type.
Definition Matrix.h:537
Matrix< Type, Size, 1 > Vector
Size×1 vector of type Type.
Definition Matrix.h:532
Definition EigenBase.h:34