cdeterman
cdeterman

Reputation: 19960

Store pointer to Eigen Vector 'segment' without copy?

I have an Eigen Vector that I would like to refer to a segment at a later time (e.g. pass between functions) instead of modifying immediately.

Eigen::Matrix<float, Eigen::Dynamic, 1> vec(10);

// initialize
vec << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10;

I would like to create a pointer to a segment that I can refer to later. The following works but it creates a copy so any changes made to the segment are not reflected in the original vector.

const int start = 2;
const int end = 8

Eigen::Matrix<float, Eigen::Dynamic, 1> *block = new Eigen::Matrix<float, Eigen::Dynamic, 1>(end - start + 1, 1);
*block = vec.segment(start-1,end-1);

How can I keep a reference to the segment without copying?

Upvotes: 4

Views: 3421

Answers (1)

Avi Ginsburg
Avi Ginsburg

Reputation: 10596

You can use an Eigen::Map to wrap an existing segment of memory without copying. I'm not sure why you're allocating the *block object and not just using block. Using a Map it would look like

Eigen::Map<Eigen::VectorXf> block(&vec(start - 1), end - start + 1);

You then use the Map as you would a normal VectorXd, sans resizing and stuff. Simpler yet (at least according to @ggael), you can use an Eigen:Ref to refer to part of an Eigen object without inducing a copy. For example:

void times2(Eigen::Ref< Eigen::VectorXf> rf)
{
    rf *= 2.f;
}
int main()
{

    Eigen::Matrix<float, Eigen::Dynamic, 1> vec(10);

    // initialize
    vec << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10;

    const int start = 2;
    const int end = 8;

    // This would work as well
    //Eigen::Map<Eigen::VectorXf> block(&vec(start - 1), end - start + 1);

    Eigen::Ref<Eigen::VectorXf> block = vec.segment(start, end - start + 1);

    std::cout << block << "\n\n";

    times2(block);

    std::cout << vec << "\n";
    return 0;
}

P.S. I think you're misusing the segment function. It takes a beginning position an the number of elements, i.e. (start, end-start+1).

Upvotes: 6

Related Questions