Adding two Eigen Matrices of vars

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…?


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

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;

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

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

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

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

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