1 #ifndef STAN_MATH_PRIM_FUN_DOT_SELF_HPP
2 #define STAN_MATH_PRIM_FUN_DOT_SELF_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/fun/Eigen.hpp>
7 #include <cstddef>
8 #include <vector>
9 
10 namespace stan {
11 namespace math {
12 
dot_self(const std::vector<double> & x)13 inline double dot_self(const std::vector<double>& x) {
14   double sum = 0.0;
15   for (double i : x) {
16     sum += i * i;
17   }
18   return sum;
19 }
20 
21 /**
22  * Returns squared norm of a vector or matrix. For vectors that equals the dot
23  * product of the specified vector with itself.
24  *
25  * @tparam T type of the vector (must be derived from \c Eigen::MatrixBase)
26  * @param v Vector.
27  */
28 template <typename T, require_eigen_t<T>* = nullptr,
29           require_not_eigen_vt<is_var, T>* = nullptr>
dot_self(const T & v)30 inline value_type_t<T> dot_self(const T& v) {
31   return v.squaredNorm();
32 }
33 
34 }  // namespace math
35 }  // namespace stan
36 
37 #endif
38