# Summary of matrix and vector block operations in Eigen

November 27, 2017*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 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)`

^{6}: 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)`

^{7}: Returns a matrix containing the block of`m`

rows and`n`

columns whose upper left corner is at`(i,j)`

.`A.bottomRows(n)`

^{8}: Returns a matrix containing the last`n`

rows.`A.topRows(n)`

^{9}: Returns a matrix containing the first`n`

rows.`A.leftCols(n)`

^{10}: Returns a matrix containing the leftmost`n`

columns.`A.rightCols(n)`

^{11}: Returns a matrix containing the rightmost`n`

columns.

## Vector operations

`v.segment(i,n)`

^{12}: Returns a vector containing the part of length`n`

starting from index`i`

.`v.head(n)`

^{13}: Returns a vector containing the first`n`

elements.`v.tail(n)`

^{14}: Returns a vector containing the last`n`

elements.`v.asDiagonal()`

^{15}: 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!

### References

- http://eigen.tuxfamily.org
- 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_1MatrixBase.html#a8a13d4b8efbd7797ee8efd3dd988a7f7
- 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