stan-dev / math

The Stan Math Library is a C++ template library for automatic differentiation of any order using forward, reverse, and mixed modes. It includes a range of built-in functions for probabilistic modeling, linear algebra, and equation solving.
https://mc-stan.org
BSD 3-Clause "New" or "Revised" License
754 stars 188 forks source link

operator* does not compile for one input matrix with double and other with var scalars #2103

Open t4c1 opened 4 years ago

t4c1 commented 4 years ago

Description

operator* (matrix multiplication) does not compile for one input matrix with double and other with var scalars.

Both stan and tests always use multiply function, so this went undetected.

Example

  int N = 2;
  int M = 3;
  int K = 4;

  Eigen::MatrixXd a(N, M);
  a << 1, 2, 3, 4, 5, 6;
  stan::math::matrix_v b(M, K);
  b << 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1;
  stan::math::matrix_v c = a * b;

Does not compile.

Expected Output

operator* compiles and does the same as multiply function.

Current Version:

v3.3.0

SteveBronder commented 4 years ago

This was brought up by Bob a while ago when we were doing the complex stuff.

https://github.com/stan-dev/math/pull/1838#issuecomment-616135990

Is there a benefit to using Eigen's multiply vs. our multiply? I had a branch up that fixed this but it was a lot of pretty goofy things

https://github.com/stan-dev/math/compare/feature/eigen-specializations-products#diff-989932cf1f16ca6deb20162d5b764e7fR178

t4c1 commented 4 years ago

This is not about ours vs Eigen. We are using c++ operators to do most operations on matrices. It is mildly strange to have to use a function call instead of the operator for just one of them (especially since this special case is not documented). If the Eigen implementation does not work with our types we should specialize operator* for these types to use our implementation.