v.segment(first, size)= some_other_vector;
v.segment<3>(position1)= v.segment<3>(position2);// optimized variant when the size is known at compile-time
v.head(n).setConstant(12);// writes 12 in the n first coefficients of v
m.diagonal().tail(n)*= lambda;// multiplies by lambda the n last diagonal coefficients of a matrix m

Computing sums

result = m.sum();// returns the sum of all coefficients in m
result = m.row(i).sum();
result = m.rowwise().sum();// returns a vector of the sums in each row

Here is how you would compute the sum of the cubes of the i-th column of a matrix:

Comma-initializer

Like other libraries, Eigen has a comma-initializer allowing to construct a matrix like this:

Matrix3f m;
m <<1, 2, 3,
4, 5, 6,
7, 8, 9;

Unlike other libraries, Eigen's comma-initializer can be combined at will with expressions, which makes it very powerful. See our tutorial on this subject.

Linear solving

Just choose the matrix decomposition that you want, the solve() API is the same everywhere. Thus, switching decompositions is very easy. In just one line of code, you can decompose and solve.

result = m.lu().solve(right_hand_side);// using partial-pivoting LU
result = m.fullPivLu().solve(right_hand_side);// using full-pivoting LU
result = m.householderQr().solve(right_hand_side);// using Householder QR
result = m.ldlt().solve(right_hand_side);// using LDLt Cholesky