Skip to content

Inconsistent arena_matrix/varmat interaction between = and += #3407

Description

@andrjohns

Opening this one as an issue since I'm not sure what the correct/intentional behaviour should be!

The arena_matrix <-> var_value<Eigen::MatrixXd> interaction currently handles inplace writes differently to assignment.

Where modifying an arena_matrix after creating a var_value will also update the value of the var_value if an inplace write is used, but not if an assignment is used:

#include <stan/math/rev.hpp>
#include <iostream>

int main() {
  Eigen::MatrixXd m = Eigen::MatrixXd::Constant(1, 1, 1);

  stan::arena_t<Eigen::MatrixXd> a(m);
  stan::math::var_value<Eigen::MatrixXd> v(a);  // shares a's memory
  std::cout << v.val() << "\n";

  a += m;  // writes in place, v sees the new values
  std::cout << v.val() << "\n";

  a = a + m;  // rebinds a, v keeps the old memory
  std::cout << v.val() << std::endl;
}

Returns:

$ ./arena 
1
2
2

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions