1#ifndef STAN_MATH_REV_FUN_ELT_MULTIPLY_HPP
2#define STAN_MATH_REV_FUN_ELT_MULTIPLY_HPP
25template <
typename Mat1,
typename Mat2,
26 require_all_matrix_t<Mat1, Mat2>* =
nullptr,
27 require_any_rev_matrix_t<Mat1, Mat2>* =
nullptr>
31 using ret_type = return_var_matrix_t<inner_ret_type, Mat1, Mat2>;
32 if (!is_constant<Mat1>::value && !is_constant<Mat2>::value) {
33 arena_t<promote_scalar_t<var, Mat1>> arena_m1 = m1;
34 arena_t<promote_scalar_t<var, Mat2>> arena_m2 = m2;
35 arena_t<ret_type> ret(arena_m1.val().cwiseProduct(arena_m2.val()));
37 for (Eigen::Index j = 0; j < arena_m2.cols(); ++j) {
38 for (Eigen::Index i = 0; i < arena_m2.rows(); ++i) {
39 const auto ret_adj = ret.adj().coeffRef(i, j);
40 arena_m1.adj().coeffRef(i, j) += arena_m2.val().coeff(i, j) * ret_adj;
41 arena_m2.adj().coeffRef(i, j) += arena_m1.val().coeff(i, j) * ret_adj;
46 }
else if (!is_constant<Mat1>::value) {
47 arena_t<promote_scalar_t<var, Mat1>> arena_m1 = m1;
48 arena_t<promote_scalar_t<double, Mat2>> arena_m2 =
value_of(m2);
49 arena_t<ret_type> ret(arena_m1.val().cwiseProduct(arena_m2));
51 arena_m1.adj().array() += arena_m2.array() * ret.adj().array();
54 }
else if (!is_constant<Mat2>::value) {
55 arena_t<promote_scalar_t<double, Mat1>> arena_m1 =
value_of(m1);
56 arena_t<promote_scalar_t<var, Mat2>> arena_m2 = m2;
57 arena_t<ret_type> ret(arena_m1.cwiseProduct(arena_m2.val()));
59 arena_m2.adj().array() += arena_m1.array() * ret.adj().array();
elt_multiply_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > elt_multiply(T_a &&a, T_b &&b)
void reverse_pass_callback(F &&functor)
Puts a callback on the autodiff stack to be called in reverse pass.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
void check_matching_dims(const char *function, const char *name1, const T1 &y1, const char *name2, const T2 &y2)
Check if the two containers have the same dimensions.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...