Automatic Differentiation
 
Loading...
Searching...
No Matches
columns_dot_product.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_COLUMNS_DOT_PRODUCT_HPP
2#define STAN_MATH_REV_FUN_COLUMNS_DOT_PRODUCT_HPP
3
11
12#include <type_traits>
13
14namespace stan {
15namespace math {
16
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>
36columns_dot_product(Mat1&& v1, Mat2&& v2) {
37 check_matching_dims("check_matching_dims", "v1", v1, "v2", v2);
38 Eigen::Matrix<var, 1, std::decay_t<Mat1>::ColsAtCompileTime> ret(1,
39 v1.cols());
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));
44 }
45 return ret;
46}
47
65template <typename Mat1, typename Mat2,
68inline auto columns_dot_product(Mat1&& v1, Mat2&& v2) {
69 check_matching_dims("columns_dot_product", "v1", v1, "v2", v2);
70 using inner_return_t = decltype(
71 (value_of(v1).array() * value_of(v2).array()).colwise().sum().matrix());
73
74 if constexpr (is_autodiff_v<Mat1> && is_autodiff_v<Mat2>) {
75 arena_t<Mat1> arena_v1(std::forward<Mat1>(v1));
76 arena_t<Mat2> arena_v2(std::forward<Mat2>(v2));
77
78 return_t res
79 = (arena_v1.val().array() * arena_v2.val().array()).colwise().sum();
80
81 reverse_pass_callback([arena_v1, arena_v2, res]() mutable {
82 if constexpr (is_var_matrix<Mat1>::value) {
83 arena_v1.adj().noalias() += arena_v2.val() * res.adj().asDiagonal();
84 } else {
85 arena_v1.adj() += arena_v2.val() * res.adj().asDiagonal();
86 }
87 if constexpr (is_var_matrix<Mat2>::value) {
88 arena_v2.adj().noalias() += arena_v1.val() * res.adj().asDiagonal();
89 } else {
90 arena_v2.adj() += arena_v1.val() * res.adj().asDiagonal();
91 }
92 });
93
94 return res;
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));
98
99 return_t res = (arena_v1.array() * arena_v2.val().array()).colwise().sum();
100
101 reverse_pass_callback([arena_v1, arena_v2, res]() mutable {
102 if constexpr (is_var_matrix<Mat2>::value) {
103 arena_v2.adj().noalias() += arena_v1 * res.adj().asDiagonal();
104 } else {
105 arena_v2.adj() += arena_v1 * res.adj().asDiagonal();
106 }
107 });
108
109 return res;
110 } else {
111 arena_t<Mat1> arena_v1(std::forward<Mat1>(v1));
112 arena_t<Mat2> arena_v2(std::forward<Mat2>(v2));
113
114 return_t res = (arena_v1.val().array() * arena_v2.array()).colwise().sum();
115
116 reverse_pass_callback([arena_v1, arena_v2, res]() mutable {
117 if constexpr (is_var_matrix<Mat2>::value) {
118 arena_v1.adj().noalias() += arena_v2 * res.adj().asDiagonal();
119 } else {
120 arena_v1.adj() += arena_v2 * res.adj().asDiagonal();
121 }
122 });
123
124 return res;
125 }
126}
127
128} // namespace math
129} // namespace stan
130#endif
require_all_t< is_matrix< std::decay_t< Types > >... > require_all_matrix_t
Require all of the types satisfy is_matrix.
Definition is_matrix.hpp:38
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.
Definition value_of.hpp:18
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.
Definition typedefs.hpp:11
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
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