Stan Math Library  2.20.0
reverse mode automatic differentiation
dot_product.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_DOT_PRODUCT_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_DOT_PRODUCT_HPP
3 
7 #include <vector>
8 
9 namespace stan {
10 namespace math {
20 template <int R1, int C1, int R2, int C2>
21 inline double dot_product(const Eigen::Matrix<double, R1, C1> &v1,
22  const Eigen::Matrix<double, R2, C2> &v2) {
23  check_vector("dot_product", "v1", v1);
24  check_vector("dot_product", "v2", v2);
25  check_matching_sizes("dot_product", "v1", v1, "v2", v2);
26  return v1.dot(v2);
27 }
34 inline double dot_product(const double *v1, const double *v2, size_t length) {
35  double result = 0;
36  for (size_t i = 0; i < length; i++)
37  result += v1[i] * v2[i];
38  return result;
39 }
46 inline double dot_product(const std::vector<double> &v1,
47  const std::vector<double> &v2) {
48  check_matching_sizes("dot_product", "v1", v1, "v2", v2);
49  return dot_product(&v1[0], &v2[0], v1.size());
50 }
51 } // namespace math
52 } // namespace stan
53 #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
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.

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