Summary of matrix and vector block operations in Eigen
Eigen Version: 3.3.4
This semester I am taking a course where all programming assignments are in C++ using the library Eigen^{1}. 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 topic^{2}. 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 thei
th row as vector. 
A.col(i)
^{4}: Returns thei
th column as vector. 
A.diagonal()
^{5}: Returns the diagonal as a vector. 
A.diagonal(k)
: Given a positive numberk
this returns thek
th diagonal above the main one, also called thek
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 ofm
rows andn
columns whose upper left corner is at(i,j)
. 
A.bottomRows(n)
^{7}: Returns a matrix containing the lastn
rows. 
A.topRows(n)
^{8}: Returns a matrix containing the firstn
rows. 
A.leftCols(n)
^{9}: Returns a matrix containing the leftmostn
columns. 
A.rightCols(n)
^{10}: Returns a matrix containing the rightmostn
columns.
Vector operations

v.segment(i,n)
^{11}: Returns a vector containing the part of lengthn
starting from indexi
. 
v.head(n)
^{12}: Returns a vector containing the firstn
elements. 
v.tail(n)
^{13}: Returns a vector containing the lastn
elements. 
v.asDiagonal()
^{14}: Returns a diagonal matrix whose diagonal is set tov
.
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!

https://eigen.tuxfamily.org/dox/group__TutorialBlockOperations.html ↩

https://eigen.tuxfamily.org/dox/classEigen_1_1DenseBase.html#a09bc4a61cdf4ad219db0e70de12c2ae2 ↩

https://eigen.tuxfamily.org/dox/classEigen_1_1DenseBase.html#a27f3b8aeaf37cb1decc9413ffceafa0f ↩

https://eigen.tuxfamily.org/dox/classEigen_1_1MatrixBase.html#ab5768147536273eb2dbdfa389cfd26a3 ↩

https://eigen.tuxfamily.org/dox/classEigen_1_1DenseBase.html#a0cc046747cc9329cc02e208a122e002e ↩

https://eigen.tuxfamily.org/dox/classEigen_1_1DenseBase.html#a36137187863fe127fe88e7388eacabaa ↩

https://eigen.tuxfamily.org/dox/classEigen_1_1DenseBase.html#aafb8bf8b8d6d2648f52dadd0e4cba68e ↩

https://eigen.tuxfamily.org/dox/classEigen_1_1DenseBase.html#a4c7c4e7a7264c0acde4632d35a9a2635 ↩

https://eigen.tuxfamily.org/dox/classEigen_1_1DenseBase.html#a5a43920b4d907a7edba59c26fba7b43d ↩

https://eigen.tuxfamily.org/dox/classEigen_1_1DenseBase.html#a6212eee4095a0ab9ac3a2202274ea8d7 ↩

https://eigen.tuxfamily.org/dox/classEigen_1_1DenseBase.html#ae458ead2be3576e80ffdfa5616778cd5 ↩

https://eigen.tuxfamily.org/dox/classEigen_1_1DenseBase.html#a70abb2369391a6fe66df2af56e1c2d1e ↩

https://eigen.tuxfamily.org/dox/classEigen_1_1MatrixBase.html#ab757d5801a0e038f8555635f06456352 ↩