template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
Matrix class
The matrix class, also used for vectors and row-vectors.
Template parameters | |
---|---|
_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 |
_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). |
Contents
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: The remaining template parameters are optional – in most cases you don't have to worry about them. 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:
Eigen::VectorXd v(10); v[0] = 0.1; v[1] = 0.2; v(0) = 0.3; v(1) = 0.4; Eigen::MatrixXi m(10, 10); m(0, 1) = 1; m(0, 2) = 2; m(0, 3) = 3;
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 are 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.
- In most cases, one just leaves these parameters to the default values. These parameters mean the maximum size of rows and columns that the matrix may have. They are useful in cases when the exact numbers of rows and columns are not known are compile-time, but it is known at compile-time that they cannot exceed a certain value. This happens when taking dynamic-size blocks inside fixed-size matrices: in this case _MaxRows and _MaxCols are the dimensions of the original matrix, while _Rows and _Cols are Dynamic.
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 |
---|---|
Matrix<T,Dynamic,Dynamic> | struct { T *data; // with (size_t(data)%EIGEN_MAX_ALIGN_BYTES)==0 Eigen::Index rows, cols; }; |
Matrix<T,Dynamic,1> Matrix<T,1,Dynamic> | struct { T *data; // with (size_t(data)%EIGEN_MAX_ALIGN_BYTES)==0 Eigen::Index size; }; |
Matrix<T,Rows,Cols> | struct { T data[Rows*Cols]; // with (size_t(data)%A(Rows*Cols*sizeof(T)))==0 }; |
Matrix<T,Dynamic,Dynamic,0,MaxRows,MaxCols> | 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.
Base classes
-
template<typename Derived>class PlainObjectBase
- Dense storage base class for matrices and arrays.
Public types
- using Base = PlainObjectBase<Matrix>
- Base class typedef.
Constructors, destructors, conversion operators
- Matrix()
- Default constructor.
- Matrix(const Scalar* data) explicit
- Constructs a fixed-sized matrix initialized with coefficients starting at data.
- Matrix(Index dim) explicit
- 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(const Scalar& x)
- Constructs an initialized 1x1 matrix with the given coefficient.
- Matrix(Index rows, Index cols)
- Constructs an uninitialized matrix with rows rows and cols columns.
- 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 Matrix& other)
- Copy constructor.
-
template<typename OtherDerived>Matrix(const EigenBase<OtherDerived>& other)
- Copy constructor for generic expressions.
-
template<typename OtherDerived>Matrix(const RotationBase<OtherDerived, ColsAtCompileTime>& r) explicit
- Constructs a Dim x Dim rotation matrix from the rotation r.
Public functions
-
template<typename OtherDerived>auto operator=(const RotationBase<OtherDerived, ColsAtCompileTime>& r) -> Matrix<_Scalar, _Rows, _Cols, _Storage, _MaxRows, _MaxCols>&
- Set a Dim x Dim rotation matrix from the rotation r.
- auto operator=(const Matrix& other) -> Matrix&
- Assigns matrices to each other.
-
template<typename OtherDerived>auto operator=(const EigenBase<OtherDerived>& other) -> Matrix&
- Copies the generic expression other into *this.
Typedef documentation
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
typedef PlainObjectBase<Matrix> Eigen:: Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>:: Base
Base class typedef.
Function documentation
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
Eigen:: Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>:: Matrix()
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.
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
Eigen:: Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>:: Matrix(Index dim) explicit
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.
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
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.
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
template<typename OtherDerived>
Eigen:: Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>:: Matrix(const RotationBase<OtherDerived, ColsAtCompileTime>& r) explicit
Constructs a Dim x Dim rotation matrix from the rotation r.
This is defined in the Geometry module. #include <Eigen/Geometry>
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
template<typename OtherDerived>
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. #include <Eigen/Geometry>
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
template<typename OtherDerived>
Matrix& Eigen:: Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>:: operator=(const EigenBase<OtherDerived>& other)
Copies the generic expression other into *this.
Returns | a reference to *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