Most efficient way of writing specific quadratic form


A simple question, hopefully. What is the most efficient way to write B' * diag(A) * B?

I have been using B' * diag_pre_multiply(A, B) but suspect there might be something more efficient.

Would quad_form(diag(A), B) be more efficient?


That probably has a simple answer that depends on the Stan Math library and the underlying Eigen libraries and any others that may go into linear algebra calculations.

Unfortunately I don’t know what that answer is without looking into them, so unless someone who is can give you the technical answer or you want to spend time looking into it yourself, the practical answer is probably “try both” and see if you notice any differences, often the model calculations are not the limiting factor compared to gradient calculations and they don’t make a difference, or the speed up is not really necessary for most applications.

So if any of those is you case, it may not be worth spending time with hardcore optimization of all computations.