Assignment operator for var class

Hi Dev team,
I am digging around in the stan math library to use in some software that I work on. The program does non-linear optimisation and already has multiple autodiff libraries e.g. adolc (

We use boost’s lexical cast to assign values to objects (I guess in the case for Stan that is var.vi_->val_) from files. I see in ( you have overloaded the ostream << operator, I was wondering if there is a reason why you haven’t overloaded the istream >> operator? would this be easy? I am not having much luck.

My C++ isn’t great but I am hoping to get Stan into this software for my PhD so any advice would be greatly appreciated.

The thing to read for a top-level overview is the arXiv paper on our autodiff.

I couldn’t quite follow what you wanted to do with initialization. You will get this:

stan::math::var x = 12;
double xv = x.vi_->val_;  // xv == 12 now

Did you try something like this:

std::ostream& operator<<(std::isstream& is, const var& v) {
  double x;
  is >> x;
  v = x;
  return is;

I’m not sure if is >> v.vi_->val_ would work.

You have to be careful with something like this as it’s not going to do anything to the adjoints. That’s usually what you want.

That was what I was after, thanks for that. If I want to maintain a version of stan with some modified classes, like this example. Do you have any tips or advice on how to best achieve it so that the repo keeps up to date but has modifications that don’t want to be included to the master?

Maintaining a branch is easiest. Then git pull brings in the latest branch. I’d branch from master so you only get official updates, but you could also branch from develop if you want to keep up with the development branch (which eventually gets promoted to master).

If it’s just things like >> opertors to math << operators, we can just add those to Stan.