Stan Math Library  2.10.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  // dot_product for vec (in matrix) * vec (in matrix);
16  // does all combos of row row, col col, row col, col row
17  template<typename T, int R1, int C1, int R2, int C2>
18  inline
19  fvar<T>
20  dot_product(const Eigen::Matrix<fvar<T>, R1, C1>& v1,
21  const Eigen::Matrix<fvar<T>, R2, C2>& v2) {
22  stan::math::check_vector("dot_product", "v1", v1);
23  stan::math::check_vector("dot_product", "v2", v2);
25  "v1", v1,
26  "v2", v2);
27 
28  fvar<T> ret(0, 0);
29  for (size_type i = 0; i < v1.size(); i++)
30  ret += v1(i) * v2(i);
31  return ret;
32  }
33 
34  template<typename T, int R1, int C1, int R2, int C2>
35  inline
36  fvar<T>
37  dot_product(const Eigen::Matrix<fvar<T>, R1, C1>& v1,
38  const Eigen::Matrix<double, R2, C2>& v2) {
39  stan::math::check_vector("dot_product", "v1", v1);
40  stan::math::check_vector("dot_product", "v2", v2);
42  "v1", v1,
43  "v2", v2);
44 
45  fvar<T> ret(0, 0);
46  for (size_type i = 0; i < v1.size(); i++)
47  ret += v1(i) * v2(i);
48  return ret;
49  }
50 
51  template<typename T, int R1, int C1, int R2, int C2>
52  inline
53  fvar<T>
54  dot_product(const Eigen::Matrix<double, R1, C1>& v1,
55  const Eigen::Matrix<fvar<T>, R2, C2>& v2) {
56  stan::math::check_vector("dot_product", "v1", v1);
57  stan::math::check_vector("dot_product", "v2", v2);
59  "v1", v1,
60  "v2", v2);
61 
62  fvar<T> ret(0, 0);
63  for (size_type i = 0; i < v1.size(); i++)
64  ret += v1(i) * v2(i);
65  return ret;
66  }
67 
68  template<typename T, int R1, int C1, int R2, int C2>
69  inline
70  fvar<T>
71  dot_product(const Eigen::Matrix<fvar<T>, R1, C1>& v1,
72  const Eigen::Matrix<fvar<T>, R2, C2>& v2,
73  size_type& length) {
74  stan::math::check_vector("dot_product", "v1", v1);
75  stan::math::check_vector("dot_product", "v2", v2);
76 
77  fvar<T> ret(0, 0);
78  for (size_type i = 0; i < length; i++)
79  ret += v1(i) * v2(i);
80  return ret;
81  }
82 
83  template<typename T, int R1, int C1, int R2, int C2>
84  inline
85  fvar<T>
86  dot_product(const Eigen::Matrix<fvar<T>, R1, C1>& v1,
87  const Eigen::Matrix<double, R2, C2>& v2,
88  size_type& length) {
89  stan::math::check_vector("dot_product", "v1", v1);
90  stan::math::check_vector("dot_product", "v2", v2);
91 
92  fvar<T> ret(0, 0);
93  for (size_type i = 0; i < length; i++)
94  ret += v1(i) * v2(i);
95  return ret;
96  }
97 
98  template<typename T, int R1, int C1, int R2, int C2>
99  inline
100  fvar<T>
101  dot_product(const Eigen::Matrix<double, R1, C1>& v1,
102  const Eigen::Matrix<fvar<T>, R2, C2>& v2,
103  size_type& length) {
104  stan::math::check_vector("dot_product", "v1", v1);
105  stan::math::check_vector("dot_product", "v2", v2);
106 
107  fvar<T> ret(0, 0);
108  for (size_type i = 0; i < length; i++)
109  ret += v1(i) * v2(i);
110  return ret;
111  }
112 
113  template<typename T>
114  inline
115  fvar<T>
116  dot_product(const std::vector<fvar<T> >& v1,
117  const std::vector<fvar<T> >& v2) {
118  stan::math::check_matching_sizes("dot_product",
119  "v1", v1,
120  "v2", v2);
121  fvar<T> ret(0, 0);
122  for (size_t i = 0; i < v1.size(); i++)
123  ret += v1.at(i) * v2.at(i);
124  return ret;
125  }
126 
127  template<typename T>
128  inline
129  fvar<T>
130  dot_product(const std::vector<double>& v1,
131  const std::vector<fvar<T> >& v2) {
132  stan::math::check_matching_sizes("dot_product",
133  "v1", v1,
134  "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<double>& v2) {
146  stan::math::check_matching_sizes("dot_product",
147  "v1", v1,
148  "v2", v2);
149  fvar<T> ret(0, 0);
150  for (size_t i = 0; i < v1.size(); i++)
151  ret += v1.at(i) * v2.at(i);
152  return ret;
153  }
154 
155  template<typename T>
156  inline
157  fvar<T>
158  dot_product(const std::vector<fvar<T> >& v1,
159  const std::vector<fvar<T> >& v2,
160  size_type& length) {
161  fvar<T> ret(0, 0);
162  for (size_type i = 0; i < length; i++)
163  ret += v1.at(i) * v2.at(i);
164  return ret;
165  }
166 
167  template<typename T>
168  inline
169  fvar<T>
170  dot_product(const std::vector<double>& v1,
171  const std::vector<fvar<T> >& v2,
172  size_type& length) {
173  fvar<T> ret(0, 0);
174  for (size_type i = 0; i < length; i++)
175  ret += v1.at(i) * v2.at(i);
176  return ret;
177  }
178 
179  template<typename T>
180  inline
181  fvar<T>
182  dot_product(const std::vector<fvar<T> >& v1,
183  const std::vector<double>& v2,
184  size_type& length) {
185  fvar<T> ret(0, 0);
186  for (size_type i = 0; i < length; i++)
187  ret += v1.at(i) * v2.at(i);
188  return ret;
189  }
190  }
191 }
192 #endif
bool check_vector(const char *function, const char *name, const Eigen::Matrix< T, R, C > &x)
Return true 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
bool check_matching_sizes(const char *function, const char *name1, const T_y1 &y1, const char *name2, const T_y2 &y2)
Return true if two structures at the same size.
fvar< T > dot_product(const Eigen::Matrix< fvar< T >, R1, C1 > &v1, const Eigen::Matrix< fvar< T >, R2, C2 > &v2)
Definition: dot_product.hpp:20

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