1c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include <Eigen/Core>
2c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include <iostream>
3c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathusing namespace Eigen;
4c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathusing namespace std;
5c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
6c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Derived>
7c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathEigen::VectorBlock<Derived>
8c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathsegmentFromRange(MatrixBase<Derived>& v, int start, int end)
9c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
10c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return Eigen::VectorBlock<Derived>(v.derived(), start, end-start);
11c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
12c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
13c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Derived>
14c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathconst Eigen::VectorBlock<const Derived>
15c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathsegmentFromRange(const MatrixBase<Derived>& v, int start, int end)
16c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
17c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return Eigen::VectorBlock<const Derived>(v.derived(), start, end-start);
18c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
19c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
20c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathint main(int, char**)
21c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
22c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  Matrix<int,1,6> v; v << 1,2,3,4,5,6;
23c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  cout << segmentFromRange(2*v, 2, 4) << endl; // calls the const version
24c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  segmentFromRange(v, 1, 3) *= 5;              // calls the non-const version
25c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  cout << "Now the vector v is:" << endl << v << endl;
26c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return 0;
27c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
28