Stan Math Library  2.14.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 
10 #include <vector>
11 
12 namespace stan {
13  namespace math {
14 
15  template<typename T, int R1, int C1, int R2, int C2>
16  inline
17  fvar<T>
18  dot_product(const Eigen::Matrix<fvar<T>, R1, C1>& v1,
19  const Eigen::Matrix<fvar<T>, R2, C2>& v2) {
20  check_vector("dot_product", "v1", v1);
21  check_vector("dot_product", "v2", v2);
22  check_matching_sizes("dot_product", "v1", v1, "v2", v2);
23 
24  fvar<T> ret(0, 0);
25  for (size_type i = 0; i < v1.size(); i++)
26  ret += v1(i) * v2(i);
27  return ret;
28  }
29 
30  template<typename T, int R1, int C1, int R2, int C2>
31  inline
32  fvar<T>
33  dot_product(const Eigen::Matrix<fvar<T>, R1, C1>& v1,
34  const Eigen::Matrix<double, R2, C2>& v2) {
35  check_vector("dot_product", "v1", v1);
36  check_vector("dot_product", "v2", v2);
37  check_matching_sizes("dot_product", "v1", v1, "v2", v2);
38 
39  fvar<T> ret(0, 0);
40  for (size_type i = 0; i < v1.size(); i++)
41  ret += v1(i) * v2(i);
42  return ret;
43  }
44 
45  template<typename T, int R1, int C1, int R2, int C2>
46  inline
47  fvar<T>
48  dot_product(const Eigen::Matrix<double, R1, C1>& v1,
49  const Eigen::Matrix<fvar<T>, R2, C2>& v2) {
50  check_vector("dot_product", "v1", v1);
51  check_vector("dot_product", "v2", v2);
52  check_matching_sizes("dot_product", "v1", v1, "v2", v2);
53 
54  fvar<T> ret(0, 0);
55  for (size_type i = 0; i < v1.size(); i++)
56  ret += v1(i) * v2(i);
57  return ret;
58  }
59 
60  template<typename T, int R1, int C1, int R2, int C2>
61  inline
62  fvar<T>
63  dot_product(const Eigen::Matrix<fvar<T>, R1, C1>& v1,
64  const Eigen::Matrix<fvar<T>, R2, C2>& v2,
65  size_type& length) {
66  check_vector("dot_product", "v1", v1);
67  check_vector("dot_product", "v2", v2);
68 
69  fvar<T> ret(0, 0);
70  for (size_type i = 0; i < length; i++)
71  ret += v1(i) * v2(i);
72  return ret;
73  }
74 
75  template<typename T, int R1, int C1, int R2, int C2>
76  inline
77  fvar<T>
78  dot_product(const Eigen::Matrix<fvar<T>, R1, C1>& v1,
79  const Eigen::Matrix<double, R2, C2>& v2,
80  size_type& length) {
81  check_vector("dot_product", "v1", v1);
82  check_vector("dot_product", "v2", v2);
83 
84  fvar<T> ret(0, 0);
85  for (size_type i = 0; i < length; i++)
86  ret += v1(i) * v2(i);
87  return ret;
88  }
89 
90  template<typename T, int R1, int C1, int R2, int C2>
91  inline
92  fvar<T>
93  dot_product(const Eigen::Matrix<double, R1, C1>& v1,
94  const Eigen::Matrix<fvar<T>, R2, C2>& v2,
95  size_type& length) {
96  check_vector("dot_product", "v1", v1);
97  check_vector("dot_product", "v2", v2);
98 
99  fvar<T> ret(0, 0);
100  for (size_type i = 0; i < length; i++)
101  ret += v1(i) * v2(i);
102  return ret;
103  }
104 
105  template<typename T>
106  inline
107  fvar<T>
108  dot_product(const std::vector<fvar<T> >& v1,
109  const std::vector<fvar<T> >& v2) {
110  check_matching_sizes("dot_product", "v1", v1, "v2", v2);
111  fvar<T> ret(0, 0);
112  for (size_t i = 0; i < v1.size(); i++)
113  ret += v1.at(i) * v2.at(i);
114  return ret;
115  }
116 
117  template<typename T>
118  inline
119  fvar<T>
120  dot_product(const std::vector<double>& v1,
121  const std::vector<fvar<T> >& v2) {
122  check_matching_sizes("dot_product", "v1", v1, "v2", v2);
123  fvar<T> ret(0, 0);
124  for (size_t i = 0; i < v1.size(); i++)
125  ret += v1.at(i) * v2.at(i);
126  return ret;
127  }
128 
129  template<typename T>
130  inline
131  fvar<T>
132  dot_product(const std::vector<fvar<T> >& v1,
133  const std::vector<double>& v2) {
134  check_matching_sizes("dot_product", "v1", v1, "v2", v2);
135  fvar<T> ret(0, 0);
136  for (size_t i = 0; i < v1.size(); i++)
137  ret += v1.at(i) * v2.at(i);
138  return ret;
139  }
140 
141  template<typename T>
142  inline
143  fvar<T>
144  dot_product(const std::vector<fvar<T> >& v1,
145  const std::vector<fvar<T> >& v2,
146  size_type& length) {
147  fvar<T> ret(0, 0);
148  for (size_type i = 0; i < length; i++)
149  ret += v1.at(i) * v2.at(i);
150  return ret;
151  }
152 
153  template<typename T>
154  inline
155  fvar<T>
156  dot_product(const std::vector<double>& v1,
157  const std::vector<fvar<T> >& v2,
158  size_type& length) {
159  fvar<T> ret(0, 0);
160  for (size_type i = 0; i < length; i++)
161  ret += v1.at(i) * v2.at(i);
162  return ret;
163  }
164 
165  template<typename T>
166  inline
167  fvar<T>
168  dot_product(const std::vector<fvar<T> >& v1,
169  const std::vector<double>& v2,
170  size_type& length) {
171  fvar<T> ret(0, 0);
172  for (size_type i = 0; i < length; i++)
173  ret += v1.at(i) * v2.at(i);
174  return ret;
175  }
176 
177  }
178 }
179 #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)
Definition: length.hpp:10
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:13
fvar< T > dot_product(const Eigen::Matrix< fvar< T >, R1, C1 > &v1, const Eigen::Matrix< fvar< T >, R2, C2 > &v2)
Definition: dot_product.hpp:18
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.

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