Adding two Eigen Matrices of vars


#1

So the following code is segfaulting for me:

  Eigen::Matrix<var, -1, -1> dxm1(2, 2);
  dxm1 << var(4.0), var(5.0), var(6.0), var(7.0);
  Eigen::Matrix<var, -1, -1> dxm2(2, 2);
  dxm2 << var(1.0), var(2.0), var(3.0), var(4.0);
  dxm2 += dxm1;

But if I replace the += with + it works fine. I suspect this might have something to do with the way varis are allocated on the custom memory arena, and perhaps there’s a copy constructor we need to override for this to work…?

Thanks,
Sean


#2

Run in what context? I’d have thought that would work. We haven’t tried to specialize the Eigen operators like operator+=.


#3

Hey @seantalts, try initializing the matrices like this:

Eigen::Matrix<var, -1, -1> dxm1(2, 2);
dxm1 << 4.0, 5.0, 6.0, 7.0;
Eigen::Matrix<var, -1, -1> dxm2(2, 2);
dxm2 << 1.0, 2.0, 3.0, 4.0;
dxm2 + dxm1;

#4

Rob nailed it! This was in my new mixed mode test for operands and partials.


#5

Out of curiosity, what’s a quick breakdown on the solution?


#6

I still don’t undertand why what you did first didn’t work. Was something going out of scope wrong somewhere else?


#7

@rtrangucci’s example didn’t have operator+=. Does his work with operator+=?


#8

It does work with += (and I have no idea why)