You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
775 B

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