1#ifndef STAN_MATH_REV_FUN_COLUMNS_DOT_PRODUCT_HPP
2#define STAN_MATH_REV_FUN_COLUMNS_DOT_PRODUCT_HPP
31template <
typename Mat1,
typename Mat2,
32 require_all_eigen_t<Mat1, Mat2>* =
nullptr,
33 require_any_eigen_vt<is_var, Mat1, Mat2>* =
nullptr>
34inline Eigen::Matrix<return_type_t<Mat1, Mat2>, 1,
35 std::decay_t<Mat1>::ColsAtCompileTime>
38 Eigen::Matrix<var, 1, std::decay_t<Mat1>::ColsAtCompileTime> ret(1,
40 decltype(
auto) v1_ref =
to_ref(std::forward<Mat1>(v1));
41 decltype(
auto) v2_ref =
to_ref(std::forward<Mat2>(v2));
42 for (
size_type j = 0; j < v1.cols(); ++j) {
43 ret.coeffRef(j) =
dot_product(v1_ref.col(j), v2_ref.col(j));
65template <
typename Mat1,
typename Mat2,
70 using inner_return_t =
decltype(
74 if constexpr (is_autodiff_v<Mat1> && is_autodiff_v<Mat2>) {
79 = (arena_v1.val().array() * arena_v2.val().array()).colwise().sum();
83 arena_v1.adj().noalias() += arena_v2.val() * res.adj().asDiagonal();
85 arena_v1.adj() += arena_v2.val() * res.adj().asDiagonal();
87 if constexpr (is_var_matrix<Mat2>::value) {
88 arena_v2.adj().noalias() += arena_v1.val() * res.adj().asDiagonal();
90 arena_v2.adj() += arena_v1.val() * res.adj().asDiagonal();
95 }
else if constexpr (is_autodiff_v<Mat2>) {
96 arena_t<Mat1> arena_v1(std::forward<Mat1>(v1));
97 arena_t<Mat2> arena_v2(std::forward<Mat2>(v2));
99 return_t res = (arena_v1.array() * arena_v2.val().array()).colwise().sum();
102 if constexpr (is_var_matrix<Mat2>::value) {
103 arena_v2.adj().noalias() += arena_v1 * res.adj().asDiagonal();
105 arena_v2.adj() += arena_v1 * res.adj().asDiagonal();
111 arena_t<Mat1> arena_v1(std::forward<Mat1>(v1));
112 arena_t<Mat2> arena_v2(std::forward<Mat2>(v2));
114 return_t res = (arena_v1.val().array() * arena_v2.array()).colwise().sum();
117 if constexpr (is_var_matrix<Mat2>::value) {
118 arena_v1.adj().noalias() += arena_v2 * res.adj().asDiagonal();
120 arena_v1.adj() += arena_v2 * res.adj().asDiagonal();
require_all_t< is_matrix< std::decay_t< Types > >... > require_all_matrix_t
Require all of the types satisfy is_matrix.
require_any_t< is_var_matrix< std::decay_t< Types > >... > require_any_var_matrix_t
Require any of the types satisfy is_var_matrix.
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.
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Type for sizes and indexes in an Eigen matrix with double elements.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
auto dot_product(const T_a &a, const T_b &b)
Returns the dot product of the specified vectors.
auto columns_dot_product(const T_a &a, const T_b &b)
Returns the dot product of columns of the specified matrices.
typename internal::arena_type_impl< std::decay_t< T > >::type arena_t
Determines a type that can be used in place of T that does any dynamic allocations on the AD stack.
std::conditional_t< is_any_var_matrix< ReturnType, Types... >::value, stan::math::var_value< stan::math::promote_scalar_t< double, plain_type_t< ReturnType > > >, stan::math::promote_scalar_t< stan::math::var_value< double >, plain_type_t< ReturnType > > > return_var_matrix_t
Given an Eigen type and several inputs, determine if a matrix should be var<Matrix> or Matrix<var>.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Check if a type is a var_value whose value_type is derived from Eigen::EigenBase