# Eigen Intro

## Matrix and Vector sizes

All matrix and vector classes are subclasses of the `Matrix` class. There are also built-in types for commonly used matrix and vector types.

``````Eigen::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``````

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.

## Initializing a matrix

Use the `<<` and `,` operators:

``````MatrixXd A(2,4); // 2x4 matrix
A << 1, 2, 3, 4,
5, 6, 7, 8;``````

The elements are specified row by row.

You can also initialize matrices to the zero and identity matrices.

``````Matrix4d B = Matrix4d::Zero();     // 4x4 zero matrix
Matrix3d C = Matrix3d::Identity(); // 3x3 identity matrix
...
B.setZero();                       // back to zero
...
B.setIdentity();                   // back to identity``````

## Printing a matrix

You can print the matrix to the screen by using `cout`:

``cout << A << endl;``

will print out

``````1 2 3 4
5 6 7 8``````

## Element access

You can access an element for read/write with the `()` operator. Rows and columns start with 0, not 1.

``````A(0,0) = 9;
double foo = A(1,3);``````

## Arithmetic operations

Eigen overloads the common operators, so you can add, subtract, and multiply matrices easily.

``````Matrix4d A, B;      // 4x4 matrices
A << ...;
B << ...;
Matrix4d C = A + B; // 4x4 matrix
double s = 2.0;
Matrix4d D = s * C; // 4x4 matrix
MatrixXd E(4,3);    // 4x3 matrix
E << ...
MatrixXd F(4,3);    // 4x3 matrix
F = C * E;``````

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 http://eigen.tuxfamily.org/dox/TopicPitfalls.html. In general, it is a good idea to compile and run your code often so that Eigen errors are easier to find.

## Block access

You can read/write blocks of elements with the `block` function.

``````Matrix2d B; // 2x2 matrix
B << -1, -2,
-3, -4;
A.block<2,2>(0,0) = B;``````

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.

``Matrix2d C = A.block<2,2>(0,2); // 2x2 matrix``

This sets `C` to be the right half of `A`.

For vectors, use the `segment` function.

``````Vector4d a; // 4x1 vector
a << 1, 2, 3, 4;
Vector2d b = a.segment<2>(0); // 2x1 vector``````

This sets `b` to be the first two elements of `a`.

## Rotation matrix

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;
Vector3f axis(1.0f, 1.0f, 1.0f);
axis.normalize();
Matrix3f R(AngleAxisf(angle, axis));
Matrix4f M;
M.setIdentity();
M.block<3,3>(0,0) = R;
cout << M << endl;``````

Similarly, you can create a rotation matrix from a unit quaternion.

``````#include <Eigen/Geometry>
...
float x, y, z, w;
x = y = z = w = 1.0f;
Quaternionf q(w, x, y, z);
q.normalize();
Matrix3f R(q);
Matrix4f M;
M.setIdentity();
M.block<3,3>(0,0) = R;
cout << M << endl;``````

## Compilation Hints

• If you have structures/classes that include Eigen members, put the following inside your class: `public: EIGEN_MAKE_ALIGNED_OPERATOR_NEW`.
• If you’re having trouble running your application with Eigen, try `#define EIGEN_DONT_ALIGN_STATICALLY` before you `#include <Eigen/Dense>`.

For more hints, see http://eigen.tuxfamily.org/dox/group__DenseMatrixManipulation__Alignement.html.

Generated on Sun Aug 16 14:53:54 CDT 2020