Eigen Version: 3.3.4

This semester I am taking a course where all programming assignments are in C++ using the library Eigen1. For some reason I keep looking up methods to extract a certain part from a matrix or a vector. The library calls these block operations and has a dedicated documentation page on the topic2. If you prefer something shorter or just want a list of documentation links, here is a short overview of the ones that got me up and running:

Matrix operations

  • A.row(i)3: Returns the i-th row as vector.

  • A.col(i)4: Returns the i-th column as vector.

  • A.diagonal()5: Returns the diagonal as a vector.

  • A.diagonal(k): Given a positive number k this returns the k-th diagonal above the main one, also called the k-th super diagonal. The same thing works with negative numbers for sub diagonals which are below the main one.

  • A.block(i,j,m,n)6: Returns a matrix containing the block of m rows and n columns whose upper left corner is at (i,j).

  • A.bottomRows(n)7: Returns a matrix containing the last n rows.

  • A.topRows(n)8: Returns a matrix containing the first n rows.

  • A.leftCols(n)9: Returns a matrix containing the leftmost n columns.

  • A.rightCols(n)10: Returns a matrix containing the rightmost n columns.

Vector operations

  • v.segment(i,n)11: Returns a vector containing the part of length n starting from index i.

  • v.head(n)12: Returns a vector containing the first n elements.

  • v.tail(n)13: Returns a vector containing the last n elements.

  • v.asDiagonal()14: Returns a diagonal matrix whose diagonal is set to v.

As you can see, almost all of these boil down to special cases of A.block() and v.segment(). However, the Eigen documentation says that the most specific method available will have the most potential for optimizations. In addition to that more specific methods also make more readable code. I hope this either helps someone find a method they need or at least help me finally memorize them!