Stan Math Library  2.20.0
reverse mode automatic differentiation
dot_product.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_MAT_FUN_DOT_PRODUCT_HPP
2 #define STAN_MATH_FWD_MAT_FUN_DOT_PRODUCT_HPP
3 
8 #include <vector>
9 
10 namespace stan {
11 namespace math {
12 
13 template <typename T, int R1, int C1, int R2, int C2>
14 inline fvar<T> dot_product(const Eigen::Matrix<fvar<T>, R1, C1>& v1,
15  const Eigen::Matrix<fvar<T>, R2, C2>& v2) {
16  check_vector("dot_product", "v1", v1);
17  check_vector("dot_product", "v2", v2);
18  check_matching_sizes("dot_product", "v1", v1, "v2", v2);
19 
20  fvar<T> ret(0, 0);
21  for (size_type i = 0; i < v1.size(); i++)
22  ret += v1(i) * v2(i);
23  return ret;
24 }
25 
26 template <typename T, int R1, int C1, int R2, int C2>
27 inline fvar<T> dot_product(const Eigen::Matrix<fvar<T>, R1, C1>& v1,
28  const Eigen::Matrix<double, R2, C2>& v2) {
29  check_vector("dot_product", "v1", v1);
30  check_vector("dot_product", "v2", v2);
31  check_matching_sizes("dot_product", "v1", v1, "v2", v2);
32 
33  fvar<T> ret(0, 0);
34  for (size_type i = 0; i < v1.size(); i++)
35  ret += v1(i) * v2(i);
36  return ret;
37 }
38 
39 template <typename T, int R1, int C1, int R2, int C2>
40 inline fvar<T> dot_product(const Eigen::Matrix<double, R1, C1>& v1,
41  const Eigen::Matrix<fvar<T>, R2, C2>& v2) {
42  check_vector("dot_product", "v1", v1);
43  check_vector("dot_product", "v2", v2);
44  check_matching_sizes("dot_product", "v1", v1, "v2", v2);
45 
46  fvar<T> ret(0, 0);
47  for (size_type i = 0; i < v1.size(); i++)
48  ret += v1(i) * v2(i);
49  return ret;
50 }
51 
52 template <typename T, int R1, int C1, int R2, int C2>
53 inline fvar<T> dot_product(const Eigen::Matrix<fvar<T>, R1, C1>& v1,
54  const Eigen::Matrix<fvar<T>, R2, C2>& v2,
55  size_type& length) {
56  check_vector("dot_product", "v1", v1);
57  check_vector("dot_product", "v2", v2);
58 
59  fvar<T> ret(0, 0);
60  for (size_type i = 0; i < length; i++)
61  ret += v1(i) * v2(i);
62  return ret;
63 }
64 
65 template <typename T, int R1, int C1, int R2, int C2>
66 inline fvar<T> dot_product(const Eigen::Matrix<fvar<T>, R1, C1>& v1,
67  const Eigen::Matrix<double, R2, C2>& v2,
68  size_type& length) {
69  check_vector("dot_product", "v1", v1);
70  check_vector("dot_product", "v2", v2);
71 
72  fvar<T> ret(0, 0);
73  for (size_type i = 0; i < length; i++)
74  ret += v1(i) * v2(i);
75  return ret;
76 }
77 
78 template <typename T, int R1, int C1, int R2, int C2>
79 inline fvar<T> dot_product(const Eigen::Matrix<double, R1, C1>& v1,
80  const Eigen::Matrix<fvar<T>, R2, C2>& v2,
81  size_type& length) {
82  check_vector("dot_product", "v1", v1);
83  check_vector("dot_product", "v2", v2);
84 
85  fvar<T> ret(0, 0);
86  for (size_type i = 0; i < length; i++)
87  ret += v1(i) * v2(i);
88  return ret;
89 }
90 
91 template <typename T>
92 inline fvar<T> dot_product(const std::vector<fvar<T> >& v1,
93  const std::vector<fvar<T> >& v2) {
94  check_matching_sizes("dot_product", "v1", v1, "v2", v2);
95  fvar<T> ret(0, 0);
96  for (size_t i = 0; i < v1.size(); i++)
97  ret += v1.at(i) * v2.at(i);
98  return ret;
99 }
100 
101 template <typename T>
102 inline fvar<T> dot_product(const std::vector<double>& v1,
103  const std::vector<fvar<T> >& v2) {
104  check_matching_sizes("dot_product", "v1", v1, "v2", v2);
105  fvar<T> ret(0, 0);
106  for (size_t i = 0; i < v1.size(); i++)
107  ret += v1.at(i) * v2.at(i);
108  return ret;
109 }
110 
111 template <typename T>
112 inline fvar<T> dot_product(const std::vector<fvar<T> >& v1,
113  const std::vector<double>& v2) {
114  check_matching_sizes("dot_product", "v1", v1, "v2", v2);
115  fvar<T> ret(0, 0);
116  for (size_t i = 0; i < v1.size(); i++)
117  ret += v1.at(i) * v2.at(i);
118  return ret;
119 }
120 
121 template <typename T>
122 inline fvar<T> dot_product(const std::vector<fvar<T> >& v1,
123  const std::vector<fvar<T> >& v2, size_type& length) {
124  fvar<T> ret(0, 0);
125  for (size_type i = 0; i < length; i++)
126  ret += v1.at(i) * v2.at(i);
127  return ret;
128 }
129 
130 template <typename T>
131 inline fvar<T> dot_product(const std::vector<double>& v1,
132  const std::vector<fvar<T> >& v2, size_type& length) {
133  fvar<T> ret(0, 0);
134  for (size_type i = 0; i < length; i++)
135  ret += v1.at(i) * v2.at(i);
136  return ret;
137 }
138 
139 template <typename T>
140 inline fvar<T> dot_product(const std::vector<fvar<T> >& v1,
141  const std::vector<double>& v2, size_type& length) {
142  fvar<T> ret(0, 0);
143  for (size_type i = 0; i < length; i++)
144  ret += v1.at(i) * v2.at(i);
145  return ret;
146 }
147 
148 } // namespace math
149 } // namespace stan
150 #endif
void check_vector(const char *function, const char *name, const Eigen::Matrix< T, R, C > &x)
Check if the matrix is either a row vector or column vector.
size_t length(const std::vector< T > &x)
Returns the length of the provided std::vector.
Definition: length.hpp:16
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Type for sizes and indexes in an Eigen matrix with double e.
Definition: typedefs.hpp:11
fvar< T > dot_product(const Eigen::Matrix< fvar< T >, R1, C1 > &v1, const Eigen::Matrix< fvar< T >, R2, C2 > &v2)
Definition: dot_product.hpp:14
void check_matching_sizes(const char *function, const char *name1, const T_y1 &y1, const char *name2, const T_y2 &y2)
Check if two structures at the same size.
This template class represents scalars used in forward-mode automatic differentiation, which consist of values and directional derivatives of the specified template type.
Definition: fvar.hpp:41

     [ Stan Home Page ] © 2011–2018, Stan Development Team.