All matrix and vector classes are subclasses of the
Matrix
class. There are also built-in types for commonly
used matrix and vector types.
::MatrixXd A(2,4); // 2x4 double matrix
Eigen::Matrix4d B; // 4x4 double matrix
Eigen::Matrix3f C; // 3x3 float matrix
Eigen::VectorXf d(9); // 9x1 float vector
Eigen::Vector4f e; // 4x1 float vector Eigen
If you declare using namespace Eigen;
beforehand in your
code, you don’t need to say Eigen::
in front of these and
other Eigen types. If you do this, it is highly recommended that you do
this in each .cpp
file and not in a .h
file,
where it could potentially pollute multiple other files.
Use the <<
and ,
operators:
(2,4); // 2x4 matrix
MatrixXd A<< 1, 2, 3, 4,
A 5, 6, 7, 8;
The elements are specified row by row.
You can also initialize matrices to the zero and identity matrices.
= Matrix4d::Zero(); // 4x4 zero matrix
Matrix4d B = Matrix3d::Identity(); // 3x3 identity matrix
Matrix3d C ...
.setZero(); // back to zero
B...
.setIdentity(); // back to identity B
See this tutorial for more information.
You can print the matrix to the screen by using
cout
:
<< A << endl; cout
will print out
1 2 3 4
5 6 7 8
You can access an element for read/write with the ()
operator. Rows and columns start with 0, not 1.
(0,0) = 9;
Adouble foo = A(1,3);
Eigen overloads the common operators, so you can add, subtract, and multiply matrices easily.
, B; // 4x4 matrices
Matrix4d A<< ...;
A << ...;
B = A + B; // 4x4 matrix
Matrix4d C double s = 2.0;
= s * C; // 4x4 matrix
Matrix4d D (4,3); // 4x3 matrix
MatrixXd E<< ...
E (4,3); // 4x3 matrix
MatrixXd F= C * E; F
The matrix sizes aren’t checked at compile time. If there is a
mismatch, you’ll get a very complex looking runtime error - one drawback
of template programming. Eigen warns against using the
auto
keyword. See the list of
pitfalls. In general, it is a good idea to compile and run your code
often so that Eigen errors are easier to find.
You can read/write blocks of elements with the block
function.
; // 2x2 matrix
Matrix2d B<< -1, -2,
B -3, -4;
.block<2,2>(0,0) = B; A
The code above writes B
into the upper 2x2 block of
A
. The numbers in the angled bracket indicate the size of
the block, and the numbers in parenthesis indicate the starting
location. You can also read a block in the same way.
= A.block<2,2>(0,2); // 2x2 matrix Matrix2d C
This sets C
to be the right half of A
.
For vectors, use the segment
function.
; // 4x1 vector
Vector4d a<< 1, 2, 3, 4;
a = a.segment<2>(0); // 2x1 vector Vector2d b
This sets b
to be the first two elements of
a
.
You can create a rotation matrix from an axis-angle as follows. The
input axis to the AngleAxis
constructor
must be normalized.
#include <Eigen/Geometry>
...
float angle = M_PI/6.0f;
(1.0f, 1.0f, 1.0f);
Vector3f axis.normalize();
axis(AngleAxisf(angle, axis));
Matrix3f R;
Matrix4f M.setIdentity();
M.block<3,3>(0,0) = R;
M<< M << endl; cout
Similarly, you can create a rotation matrix from a unit quaternion.
#include <Eigen/Geometry>
...
float x, y, z, w;
= y = z = w = 1.0f;
x (w, x, y, z);
Quaternionf q.normalize();
q(q);
Matrix3f R;
Matrix4f M.setIdentity();
M.block<3,3>(0,0) = R;
M<< M << endl; cout
public: EIGEN_MAKE_ALIGNED_OPERATOR_NEW
.#define EIGEN_DONT_ALIGN_STATICALLY
before you
#include <Eigen/Dense>
.For more hints, see the alignment page.