![]() |
Eigen
3.4.90 (git rev 9589cc4e7fd8e4538bedef80dd36c7738977a8be)
|
#include <Eigen/src/Core/Matrix.h>
The matrix class, also used for vectors and row-vectors.
The Matrix class is the work-horse for all dense (note) matrices and vectors within Eigen. Vectors are matrices with one column, and row-vectors are matrices with one row.
The Matrix class encompasses both fixed-size and dynamic-size objects (note).
The first three template parameters are required:
Scalar_ | Numeric type, e.g. float, double, int or std::complex<float>. User defined scalar types are supported as well (see here). |
Rows_ | Number of rows, or Dynamic |
Cols_ | Number of columns, or Dynamic |
The remaining template parameters are optional – in most cases you don't have to worry about them.
Options_ | A combination of either RowMajor or ColMajor, and of either AutoAlign or DontAlign. The former controls storage order, and defaults to column-major. The latter controls alignment, which is required for vectorization. It defaults to aligning matrices except for fixed sizes that aren't a multiple of the packet size. |
MaxRows_ | Maximum number of rows. Defaults to Rows_ (note). |
MaxCols_ | Maximum number of columns. Defaults to Cols_ (note). |
Eigen provides a number of typedefs covering the usual cases. Here are some examples:
Matrix2d
is a 2x2 square matrix of doubles (Matrix<double, 2, 2>
) Vector4f
is a vector of 4 floats (Matrix<float, 4, 1>
) RowVector3i
is a row-vector of 3 ints (Matrix<int, 1, 3>
)MatrixXf
is a dynamic-size matrix of floats (Matrix<float, Dynamic, Dynamic>
) VectorXf
is a dynamic-size vector of floats (Matrix<float, Dynamic, 1>
)Matrix2Xf
is a partially fixed-size (dynamic-size) matrix of floats (Matrix<float, 2, Dynamic>
) MatrixX3d
is a partially dynamic-size (fixed-size) matrix of double (Matrix<double, Dynamic, 3>
)See this page for a complete list of predefined Matrix and Vector typedefs.
You can access elements of vectors and matrices using normal subscripting:
This class can be extended with the help of the plugin mechanism described on the page Extending MatrixBase (and other classes) by defining the preprocessor symbol EIGEN_MATRIX_PLUGIN
.
Some notes:
This Matrix class handles dense, not sparse matrices and vectors. For sparse matrices and vectors, see the Sparse module.
Dense matrices and vectors are plain usual arrays of coefficients. All the coefficients are stored, in an ordinary contiguous array. This is unlike Sparse matrices and vectors where the coefficients are stored as a list of nonzero coefficients.
Fixed-size means that the numbers of rows and columns are known at compile-time. In this case, Eigen allocates the array of coefficients as a fixed-size array, as a class member. This makes sense for very small matrices, typically up to 4x4, sometimes up to 16x16. Larger matrices should be declared as dynamic-size even if one happens to know their size at compile-time.
Dynamic-size means that the numbers of rows or columns are not necessarily known at compile-time. In this case they are runtime variables, and the array of coefficients is allocated dynamically on the heap.
Note that dense matrices, be they Fixed-size or Dynamic-size, do not expand dynamically in the sense of a std::map. If you want this behavior, see the Sparse module.
ABI and storage layout
The table below summarizes the ABI of some possible Matrix instances which is fixed thorough the lifetime of Eigen 3.
Matrix type | Equivalent C structure |
---|---|
struct {
T *data; // with (size_t(data)%EIGEN_MAX_ALIGN_BYTES)==0
Eigen::Index rows, cols;
};
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index The Index type as used for the API. Definition Meta.h:82 | |
struct {
T data[MaxRows*MaxCols]; // with (size_t(data)%A(MaxRows*MaxCols*sizeof(T)))==0
Eigen::Index rows, cols;
};
|
Note that in this table Rows, Cols, MaxRows and MaxCols are all positive integers. A(S) is defined to the largest possible power-of-two smaller to EIGEN_MAX_STATIC_ALIGN_BYTES.
Public Types | |
typedef PlainObjectBase< Matrix > | Base |
Base class typedef. | |
Public Member Functions | |
constexpr Scalar & | coeffRef (Index index) |
constexpr const Scalar & | coeffRef (Index index) const |
constexpr Scalar & | coeffRef (Index rowId, Index colId) |
constexpr const Scalar & | coeffRef (Index rowId, Index colId) const |
constexpr | Matrix ()=default |
Default constructor. | |
template<typename OtherDerived> | |
Matrix (const EigenBase< OtherDerived > &other) | |
Copy constructor for generic expressions. | |
constexpr | Matrix (const Matrix &)=default |
Copy constructor. | |
template<typename OtherDerived> | |
Matrix (const RotationBase< OtherDerived, ColsAtCompileTime > &r) | |
Constructs a Dim x Dim rotation matrix from the rotation r. | |
template<typename... ArgTypes> | |
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. | |
Matrix (const Scalar &x) | |
Constructs an initialized 1x1 matrix with the given coefficient. | |
Matrix (const Scalar &x, const Scalar &y) | |
Constructs an initialized 2D vector with given coefficients. | |
Matrix (const Scalar &x, const Scalar &y, const Scalar &z) | |
Constructs an initialized 3D vector with given coefficients. | |
Matrix (const Scalar &x, const Scalar &y, const Scalar &z, const Scalar &w) | |
Constructs an initialized 4D vector with given coefficients. | |
Matrix (const Scalar *data) | |
Constructs a fixed-sized matrix initialized with coefficients starting at data. | |
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 row. [c++11] . | |
Matrix (Index dim) | |
Constructs a vector or row-vector with given dimension. This is only for vectors (either row-vectors or column-vectors), i.e. matrices which are known at compile-time to have either one row or one column. | |
Matrix (Index rows, Index cols) | |
Constructs an uninitialized matrix with rows rows and cols columns. | |
constexpr | Matrix (Matrix &&)=default |
Move constructor. | |
template<typename OtherDerived> | |
Matrix & | operator= (const EigenBase< OtherDerived > &other) |
Copies the generic expression other into *this. | |
constexpr Matrix & | operator= (const Matrix &other) |
Assigns matrices to each other. | |
template<typename OtherDerived> | |
Matrix< Scalar_, Rows_, Cols_, Storage_, MaxRows_, MaxCols_ > & | operator= (const RotationBase< OtherDerived, ColsAtCompileTime > &r) |
Set a Dim x Dim rotation matrix from the rotation r. | |
constexpr Matrix & | operator= (Matrix &&other) EIGEN_NOEXCEPT_IF(std |
Moves the matrix into the other one. | |
![]() | |
constexpr const Scalar & | coeff (Index index) const |
constexpr const Scalar & | coeff (Index rowId, Index colId) const |
constexpr Scalar & | coeffRef (Index index) |
constexpr const Scalar & | coeffRef (Index index) const |
constexpr Scalar & | coeffRef (Index rowId, Index colId) |
constexpr const Scalar & | coeffRef (Index rowId, Index colId) const |
void | conservativeResize (Index rows, Index cols) |
void | conservativeResize (Index rows, NoChange_t) |
void | conservativeResize (Index size) |
void | conservativeResize (NoChange_t, Index cols) |
template<typename OtherDerived> | |
void | conservativeResizeLike (const DenseBase< OtherDerived > &other) |
constexpr Scalar * | data () |
constexpr const Scalar * | data () const |
template<typename OtherDerived> | |
Derived & | lazyAssign (const DenseBase< OtherDerived > &other) |
template<typename OtherDerived> | |
Derived & | operator= (const EigenBase< OtherDerived > &other) |
Copies the generic expression other into *this. | |
constexpr Derived & | operator= (const PlainObjectBase &other) |
constexpr void | resize (Index rows, Index cols) |
constexpr void | resize (Index rows, NoChange_t) |
constexpr void | resize (Index size) |
constexpr void | resize (NoChange_t, Index cols) |
template<typename OtherDerived> | |
void | resizeLike (const EigenBase< OtherDerived > &_other) |
Derived & | setConstant (Index rows, Index cols, const Scalar &val) |
Derived & | setConstant (Index rows, NoChange_t, const Scalar &val) |
Derived & | setConstant (Index size, const Scalar &val) |
Derived & | setConstant (NoChange_t, Index cols, const Scalar &val) |
Derived & | setOnes (Index rows, Index cols) |
Derived & | setOnes (Index rows, NoChange_t) |
Derived & | setOnes (Index size) |
Derived & | setOnes (NoChange_t, Index cols) |
Derived & | setRandom (Index rows, Index cols) |
Derived & | setRandom (Index rows, NoChange_t) |
Derived & | setRandom (Index size) |
Derived & | setRandom (NoChange_t, Index cols) |
Derived & | setZero (Index rows, Index cols) |
Derived & | setZero (Index rows, NoChange_t) |
Derived & | setZero (Index size) |
Derived & | setZero (NoChange_t, Index cols) |
Additional Inherited Members | |
![]() | |
template<typename OtherDerived> | |
constexpr Derived & | _set (const DenseBase< OtherDerived > &other) |
Copies the value of the expression other into *this with automatic resizing. | |
constexpr PlainObjectBase & | operator= (PlainObjectBase &&other) EIGEN_NOEXCEPT |
Move assignment operator. | |
template<typename OtherDerived> | |
PlainObjectBase (const DenseBase< OtherDerived > &other) | |
template<typename OtherDerived> | |
PlainObjectBase (const EigenBase< OtherDerived > &other) | |
constexpr | PlainObjectBase (const PlainObjectBase &)=default |
template<typename OtherDerived> | |
PlainObjectBase (const ReturnByValue< OtherDerived > &other) | |
Copy constructor with in-place evaluation. | |
template<typename... ArgTypes> | |
PlainObjectBase (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. | |
constexpr | PlainObjectBase (const std::initializer_list< std::initializer_list< Scalar > > &list) |
Constructs a Matrix or Array and initializes it by elements given by an initializer list of initializer lists. | |
constexpr | PlainObjectBase (PlainObjectBase &&)=default |
Move constructor. | |
typedef PlainObjectBase<Matrix> Eigen::Matrix< Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_ >::Base |
Base class typedef.
|
inlineconstexprdefault |
Default constructor.
For fixed-size matrices, does nothing.
For dynamic-size matrices, creates an empty matrix of size 0. Does not allocate any array. Such a matrix is called a null matrix. This constructor is the unique way to create null matrices: resizing a matrix to 0 is not supported.
|
inline |
Construct a row of column vector with fixed size from an arbitrary number of coefficients.
This is only for vectors (either row-vectors or column-vectors), i.e. matrices which are known at compile-time to have either one row or one column.
This constructor is for 1D array or vectors with more than 4 coefficients.
*this
.Example:
Output:
1 2 3 4 5 6 1 2 3
|
inlineexplicitconstexpr |
Constructs a Matrix and initializes it from the coefficients given as initializer-lists grouped by row. [c++11] .
In the general case, the constructor takes a list of rows, each row being represented as a list of coefficients:
Example:
Output:
1 2 3 4 5 6
Each of the inner initializer lists must contain the exact same number of elements, otherwise an assertion is triggered.
In the case of a compile-time column vector, implicit transposition from a single row is allowed. Therefore VectorXd{{1,2,3,4,5}}
is legal and the more verbose syntax RowVectorXd{{1},{2},{3},{4},{5}}
can be avoided:
Example:
Output:
1 2
In the case of fixed-sized matrices, the initializer list sizes must exactly match the matrix sizes, and implicit transposition is allowed for compile-time vectors only.
|
inlineexplicit |
Constructs a vector or row-vector with given dimension. This is only for vectors (either row-vectors or column-vectors), i.e. matrices which are known at compile-time to have either one row or one column.
This is useful for dynamic-size vectors. For fixed-size vectors, it is redundant to pass these parameters, so one should use the default constructor Matrix() instead.
1x1
matrices. For instance, calling Matrix<double,1,1>(1) will call the initialization constructor: Matrix(const Scalar&). For fixed-size 1x1
matrices it is therefore recommended to use the default constructor Matrix() instead, especially when using one of the non standard EIGEN_INITIALIZE_MATRICES_BY_{ZERO
,NAN}
macros (see Preprocessor directives). Eigen::Matrix< Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_ >::Matrix | ( | const Scalar & | x | ) |
Constructs an initialized 1x1 matrix with the given coefficient.
Eigen::Matrix< Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_ >::Matrix | ( | Index | rows, |
Index | cols ) |
Constructs an uninitialized matrix with rows rows and cols columns.
This is useful for dynamic-size matrices. For fixed-size matrices, it is redundant to pass these parameters, so one should use the default constructor Matrix() instead.
1x2
and 2x1
vectors. For instance, calling Matrix2f(2,1) will call the initialization constructor: Matrix(const Scalar& x, const Scalar& y). For fixed-size 1x2
or 2x1
vectors it is therefore recommended to use the default constructor Matrix() instead, especially when using one of the non standard EIGEN_INITIALIZE_MATRICES_BY_{ZERO
,NAN}
macros (see Preprocessor directives). Eigen::Matrix< Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_ >::Matrix | ( | const Scalar & | x, |
const Scalar & | y ) |
Constructs an initialized 2D vector with given coefficients.
|
inline |
Constructs an initialized 3D vector with given coefficients.
|
inline |
Constructs an initialized 4D vector with given coefficients.
|
inline |
Copy constructor for generic expressions.
|
explicit |
Constructs a Dim x Dim rotation matrix from the rotation r.
This is defined in the Geometry module.
|
inlineconstexpr |
This is an overloaded version of DenseCoeffsBase<Derived,WriteAccessors>::coeffRef(Index) const provided to by-pass the creation of an evaluator of the expression, thus saving compilation efforts.
See DenseCoeffsBase<Derived,WriteAccessors>::coeffRef(Index) const for details.
|
inlineconstexpr |
This is the const version of coeffRef(Index) which is thus synonym of coeff(Index). It is provided for convenience.
|
inlineconstexpr |
This is an overloaded version of DenseCoeffsBase<Derived,WriteAccessors>::coeffRef(Index,Index) const provided to by-pass the creation of an evaluator of the expression, thus saving compilation efforts.
See DenseCoeffsBase<Derived,WriteAccessors>::coeffRef(Index,Index) const for details.
|
inlineconstexpr |
This is the const version of coeffRef(Index,Index) which is thus synonym of coeff(Index,Index). It is provided for convenience.
|
inline |
Copies the generic expression other into *this.
The expression must provide a (templated) evalTo(Derived& dst) const function which does the actual job. In practice, this allows any user to write its own special matrix without having to modify MatrixBase
|
inlineconstexpr |
Assigns matrices to each other.
Matrix< Scalar_, Rows_, Cols_, Storage_, MaxRows_, MaxCols_ > & Eigen::Matrix< Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_ >::operator= | ( | const RotationBase< OtherDerived, ColsAtCompileTime > & | r | ) |
Set a Dim x Dim rotation matrix from the rotation r.
This is defined in the Geometry module.