Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


What is a templated class matrix in Eigen?

Educative Team


Eigen is a library designed especially for linear algebra-related computations in C++. It is defined only in the header files and is a templated library. We may use its header files directly instead of requiring them to be compiled first.

Templated class matrix

Eigen provides a templated class matrix. In Eigen, all matrices and vectors are the matrix template class objects. Even the vectors are a special case of matrices, with either 11 row or 11 column for horizontal or vertical vectors.


Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>


The above syntax contains the three mandatory template parameters for the matrix class. They can be described as follows:

  • Scalar stands for the data type. For example, if we want a float data type, we need to mention float here.

  • RowsAtCompileTime is the known number of rows of the matrix at the compile time.

  • ColsAtCompileTime is the known number of columns of the matrix at the compile time.


By default, Eigen defines many typedef shortcuts for the common matrix types.

It has the syntax Matrix SizeType, where the size is 22, 33, and 44 for square matrices, or X for the dynamic size. Whereas, its types can be as follows:

  • i for integer
  • f for float
  • d for double
  • c for complex float
  • cd for complex double

This offers a lot of ease to typedef to cover the ordinary cases. For example, Matrix3f is a 333 * 3 matrix of the data type float data type. It is defined by Eigen as follows:

typedef Matrix<float, 3, 3> Matrix3f;

Code example

Let’s have a look at a sample matrix code in RcppEigen.

#include <RcppEigen.h>
// [[Rcpp::depends(RcppEigen)]]

using namespace Rcpp;
using Eigen::MatrixXd;
// [[Rcpp::export]]

MatrixXd mat_A(MatrixXd A) {
Eigen-based templated class matrix


The above code creates a matrix in Rcpp using the Eigen library. Let’s discuss the code written in main.cpp in detail:

  • Line 1: We include the RcppEigen library to the code.

  • Line 2: We show that this code file depends upon RcppEigen.

  • Line 6: We export Rcpp to the code file.

  • Line 8: We add the header for the function. This takes a single parameter.

  • Line 9: We return the matrix.

Let’s now discuss the code written in the main.r code file:

  • Line 1: We bind both the code files using sourceCpp.

  • Lines 3–5: We pass the values to matrix A, so that it can then be passed as a parameter to the function.

  • Line 8: We call the function and provide the appropriate parameter. It catches the return value in R.

  • Line 9: We print the matrix that was returned by the function call in the above line of code.

Note: There are two files in our code. One is an .r file named main.r, and the other is a .cpp file named main.cpp. The major part of the code is written in the .cpp file, while the .r file calls the function. Both the files are mutually integrated with each other using sourceCpp.


Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring