alashworth / test-issue-import

0 stars 0 forks source link

improve scalar * matrix efficiency #51

Open alashworth opened 5 years ago

alashworth commented 5 years ago

Issue by bob-carpenter Monday Dec 22, 2014 at 21:13 GMT Originally opened as https://github.com/stan-dev/stan/issues/1184


Right now, the scalar * matrix definition promotes the matrix and scalar to autodiff variables. It should just go through and do element-by-element multiplications with a promoted return type. There can be a single definition in math, which would just template out the return type to be promoted. Here's what it current looks like (there was already a relevant FIXME note in the code):

   template<typename T1,typename T2,int R2,int C2>
    inline Eigen::Matrix<var,R2,C2> multiply(const T1& c, 
                                             const Eigen::Matrix<T2, R2, C2>& m) {
      // FIXME:  pull out to eliminate overpromotion of one side
      // move to matrix.hpp w. promotion?
      return to_var(m) * to_var(c);
    }

There may be other cases like this one, but I think a single implementation can replace the first three definitions of multiply() currently in stan/agrad/rev/matrix/multiply.hpp.