0

I have some Eigen vectors which are concatenated from parts which I want to access. For this I use the block method. However, I need to access the same part over and over in different parts of the function. Therefore, I defined a temporary Eigen vector but I am not sure if this adds too much overhead or is optimized out anyway?

auto vec_part = vec.block<NUM_ELEMS,1>(start,0);

Any suggestions for an efficient implementation?

Regards Max

2
  • 1
    That's not a vector, that is a Block object that references the original vector. So it is not a copy and the compiler can optimize it freely. However, note this chapter about using auto in the documentation: eigen.tuxfamily.org/dox/TopicPitfalls.html Basically, auto is only really safe to use with Eigen when you don't nest or chain expressions (or you are lucky with their implementation and interface). So your use is okay, but vec.block(...).normalized() may not be. Use separate auto variables for that Commented Nov 24, 2022 at 9:15
  • 1
    If vec is a "safe" object (e.g., a plain Matrix or Array), then auto vec_part = vec.block<...>() is safe as well. If vec is known at compile-time to be a vector, you can also write vec.segment<NUM_ELEMS>(start), btw. This should not make any difference after compilation, but could be easier to read. Commented Nov 24, 2022 at 13:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.