top

Modules and Header files

The Eigen library is divided in a Core module and several additional modules. Each module has a corresponding header file which has to be included in order to use the module. The Dense and Eigen header files are provided to conveniently gain access to several modules at once.

ModuleHeader fileContents
Core
#include <Eigen/Core>
Matrix and Array classes, basic linear algebra (including triangular and selfadjoint products), array manipulation
Geometry Transform, Translation, Scaling, Rotation2D and 3D rotations (Quaternion, AngleAxis)
LU
#include <Eigen/LU>
Inverse, determinant, LU decompositions with solver (FullPivLU, PartialPivLU)
Cholesky LLT and LDLT Cholesky factorization with solver
Householder Householder transformations; this module is used by several linear algebra modules
SVD
#include <Eigen/SVD>
SVD decompositions with least-squares solver (JacobiSVD, BDCSVD)
QR
#include <Eigen/QR>
QR decomposition with solver (HouseholderQR, ColPivHouseholderQR, FullPivHouseholderQR)
Eigenvalues Eigenvalue, eigenvector decompositions (EigenSolver, SelfAdjointEigenSolver, ComplexEigenSolver)
Sparse
#include <Eigen/Sparse>
Sparse matrix storage and related basic linear algebra (SparseMatrix, SparseVector)
(see Quick reference guide for sparse matrices for details on sparse modules)
#include <Eigen/Dense>
Includes Core, Geometry, LU, Cholesky, SVD, QR, and Eigenvalues header files
#include <Eigen/Eigen>
Includes Dense and Sparse header files (the whole Eigen library)

top

Array, matrix and vector types

Recall: Eigen provides two kinds of dense objects: mathematical matrices and vectors which are both represented by the template class Matrix, and general 1D and 2D arrays represented by the template class Array:

typedef Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime, Options> MyMatrixType;
typedef Array<Scalar, RowsAtCompileTime, ColsAtCompileTime, Options> MyArrayType;

All combinations are allowed: you can have a matrix with a fixed number of rows and a dynamic number of columns, etc. The following are all valid:

Matrix<double, 6, Dynamic> // Dynamic number of columns (heap allocation)
Matrix<double, Dynamic, 2> // Dynamic number of rows (heap allocation)
Matrix<double, Dynamic, Dynamic, RowMajor> // Fully dynamic, row major (heap allocation)
Matrix<double, 13, 3> // Fully fixed (usually allocated on stack)

In most cases, you can simply use one of the convenience typedefs for matrices and arrays. Some examples:

MatricesArrays
Matrix<float,Dynamic,Dynamic> <=> MatrixXf
Matrix<double,Dynamic,1> <=> VectorXd
Matrix<int,1,Dynamic> <=> RowVectorXi
Matrix<float,3,3> <=> Matrix3f
Matrix<float,4,1> <=> Vector4f
Matrix< float, 3, 3 > Matrix3f
3×3 matrix of type float.
Definition: Matrix.h:501
Matrix< float, Dynamic, Dynamic > MatrixXf
Dynamic×Dynamic matrix of type float.
Definition: Matrix.h:501
Matrix< int, 1, Dynamic > RowVectorXi
1×Dynamic vector of type int.
Definition: Matrix.h:500
Matrix< double, Dynamic, 1 > VectorXd
Dynamic×1 vector of type double.
Definition: Matrix.h:502
Matrix< float, 4, 1 > Vector4f
4×1 vector of type float.
Definition: Matrix.h:501
Array<float,Dynamic,Dynamic> <=> ArrayXXf
Array<double,Dynamic,1> <=> ArrayXd
Array<int,1,Dynamic> <=> RowArrayXi
Array<float,3,3> <=> Array33f
Array<float,4,1> <=> Array4f
Array< float, 4, 1 > Array4f
Definition: Array.h:345
Array< float, 3, 3 > Array33f
Definition: Array.h:345
Array< double, Dynamic, 1 > ArrayXd
Definition: Array.h:346
Array< float, Dynamic, Dynamic > ArrayXXf
Definition: Array.h:345

Conversion between the matrix and array worlds:

Array44f a1, a2;
m1 = a1 * a2; // coeffwise product, implicit conversion from array to matrix.
a1 = m1 * m2; // matrix product, implicit conversion from matrix to array.
a2 = a1 + m1.array(); // mixing array and matrix is forbidden
m2 = a1.matrix() + m1; // and explicit conversion is required.
ArrayWrapper<Matrix4f> m1a(m1); // m1a is an alias for m1.array(), they share the same coefficients
MatrixWrapper<Array44f> a1m(a1);
Matrix3d m1
Definition: IOFormat.cpp:2
MatrixType m2(n_dims)
ArrayWrapper< Derived > array()
Definition: MatrixBase.h:324
Array< float, 4, 4 > Array44f
Definition: Array.h:345
Matrix< float, 4, 4 > Matrix4f
4×4 matrix of type float.
Definition: Matrix.h:501

In the rest of this document we will use the following symbols to emphasize the features which are specifics to a given kind of object:

Basic matrix manipulation

1D objects2D objectsNotes
Constructors
Array3i v2(x, y, z);
Vector4d v3(x, y, z, w);
VectorXf v5; // empty object
RowVector3d w
Map< RowVectorXf > v2(M2.data(), M2.size())
M1<< 1, 2, 3, 4, 5, 6, 7, 8, 9;Map< RowVectorXf > v1(M1.data(), M1.size())
Array< float, Dynamic, 1 > ArrayXf
Definition: Array.h:345
Array< int, 3, 1 > Array3i
Definition: Array.h:344
Matrix< float, 2, 1 > Vector2f
2×1 vector of type float.
Definition: Matrix.h:501
Matrix< float, Dynamic, 1 > VectorXf
Dynamic×1 vector of type float.
Definition: Matrix.h:501
Matrix< double, 4, 1 > Vector4d
4×1 vector of type double.
Definition: Matrix.h:502
const Scalar & y
MatrixXf m5; // empty object
MatrixXf m6(nb_rows, nb_columns);
By default, the coefficients
are left uninitialized
Comma initializer
Vector3f v1; v1 << x, y, z;
ArrayXf v2(4); v2 << 1, 2, 3, 4;
Matrix< float, 3, 1 > Vector3f
3×1 vector of type float.
Definition: Matrix.h:501
Matrix3f m1; m1 << 1, 2, 3,
4, 5, 6,
7, 8, 9;

Comma initializer (bis)
int rows=5, cols=5;
m << (Matrix3f() << 1, 2, 3, 4, 5, 6, 7, 8, 9).finished(),
cout << m;
Matrix3f m
static const ConstantReturnType Zero()
static const IdentityReturnType Identity()

output:

1 2 3 0 0
4 5 6 0 0
7 8 9 0 0
0 0 0 1 0
0 0 0 0 1

Runtime info
vector.size();
vector.innerStride();
vector.data();
matrix.rows(); matrix.cols();
matrix.innerSize(); matrix.outerSize();
matrix.innerStride(); matrix.outerStride();
matrix.data();
Inner/Outer* are storage order dependent
Compile-time info
ObjectType::Scalar ObjectType::RowsAtCompileTime
ObjectType::RealScalar ObjectType::ColsAtCompileTime
ObjectType::Index ObjectType::SizeAtCompileTime
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
Resizing
vector.resize(size);
vector.resizeLike(other_vector);
vector.conservativeResize(size);
matrix.resize(nb_rows, nb_cols);
matrix.resize(Eigen::NoChange, nb_cols);
matrix.resize(nb_rows, Eigen::NoChange);
matrix.resizeLike(other_matrix);
matrix.conservativeResize(nb_rows, nb_cols);
@ NoChange
Definition: Constants.h:362

no-op if the new sizes match,
otherwise data are lost

resizing with data preservation

Coeff access with
range checking
vector(i) vector.x()
vector[i] vector.y()
vector.z()
vector.w()
matrix(i,j)
std::ptrdiff_t j

Range checking is disabled if
NDEBUG or EIGEN_NO_DEBUG is defined

Coeff access without
range checking
vector.coeff(i)
vector.coeffRef(i)
matrix.coeff(i,j)
matrix.coeffRef(i,j)

Assignment/copy
object = expression;
object_of_float = expression_of_double.cast<float>();

the destination is automatically resized (if possible)

Predefined Matrices

Fixed-size matrix or vector Dynamic-size matrix Dynamic-size vector
typedef {Matrix3f|Array33f} FixedXD;
FixedXD x;
x = FixedXD::Zero();
x = FixedXD::Ones();
x = FixedXD::Constant(value);
x = FixedXD::Random();
x = FixedXD::LinSpaced(size, low, high);
x.setZero();
x.setOnes();
x.setConstant(value);
x.setRandom();
x.setLinSpaced(size, low, high);
typedef {MatrixXf|ArrayXXf} Dynamic2D;
Dynamic2D x;
x = Dynamic2D::Zero(rows, cols);
x = Dynamic2D::Ones(rows, cols);
x = Dynamic2D::Constant(rows, cols, value);
x = Dynamic2D::Random(rows, cols);
N/A
x.setZero(rows, cols);
x.setOnes(rows, cols);
x.setConstant(rows, cols, value);
x.setRandom(rows, cols);
N/A
MatrixXcf A
typedef {VectorXf|ArrayXf} Dynamic1D;
Dynamic1D x;
x = Dynamic1D::Zero(size);
x = Dynamic1D::Ones(size);
x = Dynamic1D::Constant(size, value);
x = Dynamic1D::Random(size);
x = Dynamic1D::LinSpaced(size, low, high);
x.setZero(size);
x.setOnes(size);
x.setConstant(size, value);
x.setRandom(size);
x.setLinSpaced(size, low, high);

Identity and basis vectors *
x = FixedXD::Identity();
x.setIdentity();
Vector3f::UnitX() // 1 0 0
Vector3f::UnitY() // 0 1 0
Vector3f::UnitZ() // 0 0 1
Vector4f::Unit(i)
x.setUnit(i);
static const BasisReturnType UnitX()
x = Dynamic2D::Identity(rows, cols);
x.setIdentity(rows, cols);
N/A
N/A
x.setUnit(size,i);
VectorXf::Unit(4,1) == Vector4f(0,1,0,0)
static const BasisReturnType UnitY()
static const BasisReturnType Unit(Index size, Index i)

Note that it is allowed to call any of the set* functions to a dynamic-sized vector or matrix without passing new sizes. For instance:

MatrixXi M(3,3);
Matrix4Xd M
Derived & setIdentity()
Matrix< int, Dynamic, Dynamic > MatrixXi
Dynamic×Dynamic matrix of type int.
Definition: Matrix.h:500

Mapping external arrays

Contiguous
memory
float data[] = {1,2,3,4};
Map<Vector3f> v1(data); // uses v1 as a Vector3f object
Map<ArrayXf> v2(data,3); // uses v2 as a ArrayXf object
Map<Array22f> m1(data); // uses m1 as a Array22f object
Map<MatrixXf> m2(data,2,2); // uses m2 as a MatrixXf object
int data[]
Typical usage
of strides
float data[] = {1,2,3,4,5,6,7,8,9};
Map<VectorXf,0,InnerStride<2> > v1(data,3); // = [1,3,5]
Map<VectorXf,0,InnerStride<> > v2(data,3,InnerStride<>(3)); // = [1,4,7]
Map<MatrixXf,0,OuterStride<3> > m2(data,2,3); // both lines |1,4,7|
Map<MatrixXf,0,OuterStride<> > m1(data,2,3,OuterStride<>(3)); // are equal to: |2,5,8|

top

Arithmetic Operators

add
subtract
mat3 = mat1 + mat2; mat3 += mat1;
mat3 = mat1 - mat2; mat3 -= mat1;
MatrixXd mat1(size, size)
scalar product
mat3 = mat1 * s1; mat3 *= s1; mat3 = s1 * mat1;
mat3 = mat1 / s1; mat3 /= s1;
matrix/vector
products *
col2 = mat1 * col1;
row2 = row1 * mat1; row1 *= mat1;
mat3 = mat1 * mat2; mat3 *= mat1;
transposition
adjoint *
TransposeReturnType transpose()
Definition: Transpose.h:184
void transposeInPlace()
Definition: Transpose.h:346
void adjointInPlace()
Definition: Transpose.h:377
const AdjointReturnType adjoint() const
Definition: Transpose.h:223
dot product
inner product *
scalar = vec1.dot(vec2);
scalar = col1.adjoint() * col2;
scalar = (col1.adjoint() * col2).value();
RowVectorXd vec1(3)
ScalarBinaryOpTraits< typename internal::traits< Derived >::Scalar, typename internal::traits< OtherDerived >::Scalar >::ReturnType dot(const MatrixBase< OtherDerived > &other) const
Definition: Dot.h:69
outer product *

norm
normalization *
scalar = vec1.norm(); scalar = vec1.squaredNorm()
vec2 = vec1.normalized(); vec1.normalize(); // inplace
RealScalar norm() const
Definition: Dot.h:106
const PlainObject normalized() const
Definition: Dot.h:122
RealScalar squaredNorm() const
Definition: Dot.h:94
void normalize()
Definition: Dot.h:143

cross product *
#include <Eigen/Geometry>
v3c = v3a.cross(v3b); // size-3 vectors
scalar = v2a.cross(v2b); // size-2 vectors

top

Coefficient-wise & Array operators

In addition to the aforementioned operators, Eigen supports numerous coefficient-wise operator and functions. Most of them unambiguously makes sense in array-world*. The following operators are readily available for arrays, or available through .array() for vectors and matrices:

Arithmetic operators
array1 * array2 array1 / array2 array1 *= array2 array1 /= array2
array1 + scalar array1 - scalar array1 += scalar array1 -= scalar
Comparisons
array1 < array2 array1 > array2 array1 < scalar array1 > scalar
array1 <= array2 array1 >= array2 array1 <= scalar array1 >= scalar
array1 == array2 array1 != array2 array1 == scalar array1 != scalar
array1.min(array2) array1.max(array2) array1.min(scalar) array1.max(scalar)
Trigo, power, and
misc functions
and the STL-like variants
array1.abs2()
array1.abs() abs(array1)
array1.sqrt() sqrt(array1)
array1.log() log(array1)
array1.log10() log10(array1)
array1.exp() exp(array1)
array1.pow(array2) pow(array1,array2)
array1.pow(scalar) pow(array1,scalar)
pow(scalar,array2)
array1.square()
array1.cube()
array1.inverse()
array1.sin() sin(array1)
array1.cos() cos(array1)
array1.tan() tan(array1)
array1.asin() asin(array1)
array1.acos() acos(array1)
array1.atan() atan(array1)
array1.sinh() sinh(array1)
array1.cosh() cosh(array1)
array1.tanh() tanh(array1)
array1.arg() arg(array1)
array1.floor() floor(array1)
array1.ceil() ceil(array1)
array1.round() round(aray1)
array1.isFinite() isfinite(array1)
array1.isInf() isinf(array1)
array1.isNaN() isnan(array1)
bfloat16 pow(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:626
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_tanh_op< typename Derived::Scalar >, const Derived > tanh(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_isinf_op< typename Derived::Scalar >, const Derived > isinf(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_atan_op< typename Derived::Scalar >, const Derived > atan(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_log10_op< typename Derived::Scalar >, const Derived > log10(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_cosh_op< typename Derived::Scalar >, const Derived > cosh(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_tan_op< typename Derived::Scalar >, const Derived > tan(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_acos_op< typename Derived::Scalar >, const Derived > acos(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_asin_op< typename Derived::Scalar >, const Derived > asin(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_isnan_op< typename Derived::Scalar >, const Derived > isnan(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_arg_op< typename Derived::Scalar >, const Derived > arg(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_ceil_op< typename Derived::Scalar >, const Derived > ceil(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_isfinite_op< typename Derived::Scalar >, const Derived > isfinite(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_floor_op< typename Derived::Scalar >, const Derived > floor(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_cos_op< typename Derived::Scalar >, const Derived > cos(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_round_op< typename Derived::Scalar >, const Derived > round(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_exp_op< typename Derived::Scalar >, const Derived > exp(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sin_op< typename Derived::Scalar >, const Derived > sin(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_log_op< typename Derived::Scalar >, const Derived > log(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sinh_op< typename Derived::Scalar >, const Derived > sinh(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)

The following coefficient-wise operators are available for all kind of expressions (matrices, vectors, and arrays), and for both real or complex scalar types:

Eigen's APISTL-like APIs* Comments
mat1.real()
mat1.imag()
mat1.conjugate()
real(array1)
imag(array1)
conj(array1)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_imag_op< typename Derived::Scalar >, const Derived > imag(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_conjugate_op< typename Derived::Scalar >, const Derived > conj(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_real_op< typename Derived::Scalar >, const Derived > real(const Eigen::ArrayBase< Derived > &x)
// read-write, no-op for real expressions
// read-only for real, read-write for complexes
// no-op for real expressions

Some coefficient-wise operators are readily available for for matrices and vectors through the following cwise* methods:

Matrix API *Via Array conversions
mat1.cwiseMin(mat2) mat1.cwiseMin(scalar)
mat1.cwiseMax(mat2) mat1.cwiseMax(scalar)
mat1.cwiseAbs2()
mat1.cwiseAbs()
mat1.cwiseSqrt()
mat1.cwiseInverse()
mat1.cwiseQuotient(mat2)
mat1.cwiseEqual(mat2) mat1.cwiseEqual(scalar)
mat1.cwiseNotEqual(mat2)
const SparseMatrixBase< OtherDerived >::template CwiseProductDenseReturnType< Derived >::Type cwiseProduct(const SparseMatrixBase< OtherDerived > &other) const
Definition: MatrixBase.h:457
mat1.array().min(mat2.array()) mat1.array().min(scalar)
mat1.array().max(mat2.array()) mat1.array().max(scalar)
mat1.array().abs2()
mat1.array().abs()
mat1.array().sqrt()
mat1.array().inverse()
mat1.array() * mat2.array()
mat1.array() / mat2.array()
mat1.array() == mat2.array() mat1.array() == scalar
mat1.array() != mat2.array()

The main difference between the two API is that the one based on cwise* methods returns an expression in the matrix world, while the second one (based on .array()) returns an array expression. Recall that .array() has no cost, it only changes the available API and interpretation of the data.

It is also very simple to apply any user defined function foo using DenseBase::unaryExpr together with std::ptr_fun (c++03, deprecated or removed in newer C++ versions), std::ref (c++11), or lambdas (c++11):

mat1.unaryExpr(std::ptr_fun(foo));
mat1.unaryExpr(std::ref(foo));
mat1.unaryExpr([](double x) { return foo(x); });

Please note that it's not possible to pass a raw function pointer to unaryExpr, so please warp it as shown above.

top

Reductions

Eigen provides several reduction methods such as: minCoeff() , maxCoeff() , sum() , prod() , trace() *, norm() *, squaredNorm() *, all() , and any() . All reduction operations can be done matrix-wise, column-wise or row-wise . Usage example:

5 3 1
mat = 2 7 8
9 4 6
internal::traits< Derived >::Scalar minCoeff() const
Definition: Redux.h:518
1
ConstColwiseReturnType colwise() const
Definition: DenseBase.h:560
const MinCoeffReturnType minCoeff() const
Definition: VectorwiseOp.h:377
2 3 1
ConstRowwiseReturnType rowwise() const
Definition: DenseBase.h:548
1
2
4

Special versions of minCoeff and maxCoeff :

int i, j;
s = vector.minCoeff(&i); // s == vector[i]
s = matrix.maxCoeff(&i, &j); // s == matrix(i,j)

Typical use cases of all() and any():

if((array1 > 0).all()) ... // if all coefficients of array1 are greater than 0 ...
if((array1 < array2).any()) ... // if there exist a pair i,j such that array1(i,j) < array2(i,j) ...
static const Eigen::internal::all_t all

top

Sub-matrices

PLEASE HELP US IMPROVING THIS SECTION. Eigen 3.4 supports a much improved API for sub-matrices, including, slicing and indexing from arrays: Slicing and Indexing

Read-write access to a column or a row of a matrix (or array):

mat1.row(i) = mat2.col(j);
mat1.col(j1).swap(mat1.col(j2));
void swap(const DenseBase< OtherDerived > &other)
Definition: DenseBase.h:418

Read-write access to sub-vectors:

Default versions Optimized versions when the size
is known at compile time

vec1.head(n)
int n
vec1.head<n>()
the first n coeffs
vec1.tail(n)
vec1.tail<n>()
the last n coeffs
vec1.segment(pos,n)
vec1.segment<n>(pos)
the n coeffs in the
range [pos : pos + n - 1]

Read-write access to sub-matrices:

mat1.block(i,j,rows,cols)
(more)
mat1.block<rows,cols>(i,j)
(more)
the rows x cols sub-matrix
starting from position (i,j)
mat1.topLeftCorner(rows,cols)
mat1.topRightCorner(rows,cols)
mat1.bottomLeftCorner(rows,cols)
mat1.bottomRightCorner(rows,cols)
mat1.topLeftCorner<rows,cols>()
mat1.topRightCorner<rows,cols>()
mat1.bottomLeftCorner<rows,cols>()
mat1.bottomRightCorner<rows,cols>()
the rows x cols sub-matrix
taken in one of the four corners
mat1.topRows(rows)
mat1.bottomRows(rows)
mat1.leftCols(cols)
mat1.rightCols(cols)
mat1.topRows<rows>()
mat1.bottomRows<rows>()
mat1.leftCols<cols>()
mat1.rightCols<cols>()
specialized versions of block()
when the block fit two corners

top

Miscellaneous operations

PLEASE HELP US IMPROVING THIS SECTION. Eigen 3.4 supports a new API for reshaping: Reshape

Reverse

Vectors, rows, and/or columns of a matrix can be reversed (see DenseBase::reverse(), DenseBase::reverseInPlace(), VectorwiseOp::reverse()).

vec.reverse() mat.colwise().reverse() mat.rowwise().reverse()
vec.reverseInPlace()
const ConstReverseReturnType reverse() const
Definition: VectorwiseOp.h:542

Replicate

Vectors, matrices, rows, and/or columns can be replicated in any direction (see DenseBase::replicate(), VectorwiseOp::replicate())

vec.replicate(times) vec.replicate<Times>
mat.replicate(vertical_times, horizontal_times) mat.replicate<VerticalTimes, HorizontalTimes>()
mat.colwise().replicate(vertical_times, horizontal_times) mat.colwise().replicate<VerticalTimes, HorizontalTimes>()
mat.rowwise().replicate(vertical_times, horizontal_times) mat.rowwise().replicate<VerticalTimes, HorizontalTimes>()
const Replicate< Derived, RowFactor, ColFactor > replicate() const
Definition: Replicate.h:121
const ReplicateReturnType replicate(Index factor) const
Definition: Replicate.h:136

top

Diagonal, Triangular, and Self-adjoint matrices

(matrix world *)

Diagonal matrices

OperationCode
view a vector as a diagonal matrix
const DiagonalWrapper< const Derived > asDiagonal() const
Declare a diagonal matrix
DiagonalMatrix<Scalar,SizeAtCompileTime> diag1(size);
diag1.diagonal() = vector;
Access the diagonal and super/sub diagonals of a matrix as a vector (read/write)
vec1 = mat1.diagonal(); mat1.diagonal() = vec1; // main diagonal
vec1 = mat1.diagonal(+n); mat1.diagonal(+n) = vec1; // n-th super diagonal
vec1 = mat1.diagonal(-n); mat1.diagonal(-n) = vec1; // n-th sub diagonal
vec1 = mat1.diagonal<1>(); mat1.diagonal<1>() = vec1; // first super diagonal
vec1 = mat1.diagonal<-2>(); mat1.diagonal<-2>() = vec1; // second sub diagonal
DiagonalReturnType diagonal()
Definition: Diagonal.h:189

Optimized products and inverse
mat3 = scalar * diag1 * mat1;
mat3 += scalar * mat1 * vec1.asDiagonal();
mat3 = vec1.asDiagonal().inverse() * mat1
mat3 = mat1 * diag1.inverse()
const Inverse< Derived > inverse() const
Definition: InverseImpl.h:350

Triangular views

TriangularView gives a view on a triangular part of a dense matrix and allows to perform optimized operations on it. The opposite triangular part is never referenced and can be used to store other information.

Note
The .triangularView() template member function requires the template keyword if it is used on an object of a type that depends on a template parameter; see The template and typename keywords in C++ for details.
OperationCode
Reference to a triangular with optional
unit or null diagonal (read/write):
TriangularViewReturnType< Mode >::Type triangularView()

Xxx = Upper, Lower, StrictlyUpper, StrictlyLower, UnitUpper, UnitLower
Writing to a specific triangular part:
(only the referenced triangular part is evaluated)
Conversion to a dense matrix setting the opposite triangular part to zero:
Products:
m3 += s1 * m1.adjoint().triangularView<Eigen::UnitUpper>() * m2
m3 -= s1 * m2.conjugate() * m1.adjoint().triangularView<Eigen::Lower>()
Solving linear equations:
\( M_2 := L_1^{-1} M_2 \)
\( M_3 := {L_1^*}^{-1} M_3 \)
\( M_4 := M_4 U_1^{-1} \)

L1.triangularView<Eigen::UnitLower>().solveInPlace(M2)
L1.triangularView<Eigen::Lower>().adjoint().solveInPlace(M3)
U1.triangularView<Eigen::Upper>().solveInPlace<OnTheRight>(M4)
M1<< 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12;Map< MatrixXf > M2(M1.data(), 6, 2)
RowMajorMatrixXf M3(M1)
cout<< "Row major input:"<< endl<< M3<< "\n";Map< RowMajorMatrixXf, 0, Stride< Dynamic, 3 > > M4(M3.data(), M3.rows(),(M3.cols()+2)/3, Stride< Dynamic, 3 >(M3.outerStride(), 3))
@ UnitLower
Definition: Constants.h:219
@ Upper
Definition: Constants.h:213

Symmetric/selfadjoint views

Just as for triangular matrix, you can reference any triangular part of a square matrix to see it as a selfadjoint matrix and perform special and optimized operations. Again the opposite triangular part is never referenced and can be used to store other information.

Note
The .selfadjointView() template member function requires the template keyword if it is used on an object of a type that depends on a template parameter; see The template and typename keywords in C++ for details.
OperationCode
Conversion to a dense matrix:
SelfAdjointViewReturnType< UpLo >::Type selfadjointView()
Product with another general matrix or vector:
m3 = s1 * m1.conjugate().selfadjointView<Eigen::Upper>() * m3;
m3 -= s1 * m3.adjoint() * m1.selfadjointView<Eigen::Lower>();
Rank 1 and rank K update:
\( upper(M_1) \mathrel{{+}{=}} s_1 M_2 M_2^* \)
\( lower(M_1) \mathbin{{-}{=}} M_2^* M_2 \)

M1.selfadjointView<Eigen::Upper>().rankUpdate(M2,s1);
M1.selfadjointView<Eigen::Lower>().rankUpdate(M2.adjoint(),-1);
MatrixXf M1
Rank 2 update: ( \( M \mathrel{{+}{=}} s u v^* + s v u^* \))
M.selfadjointView<Eigen::Upper>().rankUpdate(u,v,s);
Array< int, Dynamic, 1 > v
Solving linear equations:
( \( M_2 := M_1^{-1} M_2 \))
// via a standard Cholesky factorization
m2 = m1.selfadjointView<Eigen::Upper>().llt().solve(m2);
// via a Cholesky factorization with pivoting
m2 = m1.selfadjointView<Eigen::Lower>().ldlt().solve(m2);