Operands and partials with more than five edges



Hi all,

I’m working on some code for an efficient (lme4-like) linear mixed model for the Stan library. I’m piggybacking quite heavily off normal_id_glm_lpdf.hpp.

I’ve got most of the details worked out, although it’s got a few complications (mainly due to Eigen’s limited sparse matrix features).

But my main problem right now is that I have 8 potential autodiff parameters (5 vector-like parameters, 2 dense matrix-like parameters, and one scalar parameter), which is more than the five assumed by the implementation of operands_and_partials. Is there an obvious way to get around this?

  • In almost every application, at most three of these will be vars, but following convention, I’m making sure every double can be a var.
  • I’m assuming the ops_partials.edge1_.partials_ syntax is correct rather than the operands_and_partials_d_x1_ syntax from the arXiv paper on the math library. Please let me know if that’s wrong.

Edit: I’m probably going to hit similar road blocks with the traits meta-programs. Let me know if the solution is different here.
Edit 2: Actually, would this work for the metaprograms?

return_type<T1, T2, return_type<T3,T4>::type >::type




Probably the easiest solution is just to add more template arguments to operands and partials? It’s probably just some copy paste.

Here are the files!

bbales2@tadpole:~/math-1d$ find stan/math -name "operands_and_partials*"

This is what happens to the edge1_.partials in the end https://github.com/stan-dev/math/blob/develop/stan/math/rev/scal/meta/operands_and_partials.hpp#L113

I think @seantalts would have the most precise answers on this.

This is the same as return_type<T1, T2, T3,T4>::type, right? Was that template meta-program not accepting enough arguments? If so it’d be worth an issue and should be an easy fix.


We only have 5 because when we only needed 4, Matthijs went and made some of these GLM functions that needed 5 so he expanded it. You should be able to find a PR with that in it and copy it (I can look a bit later tonight). I don’t think there’s a reason not to expand it further.


Easy! Thanks.


Right. I found the PR Feature/Normal_Id_GLM #665. There’s nothing in the operands_and_partials bit that’s particularly intimidating, so I’ll do that and when I eventually get everything else running I’ll make a PR.

Thanks for your help!