Automatic Differentiation
 
Loading...
Searching...
No Matches
dot_self.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_DOT_SELF_HPP
2#define STAN_MATH_PRIM_FUN_DOT_SELF_HPP
3
7#include <cstddef>
8#include <vector>
9
10namespace stan {
11namespace math {
12
13template <typename T, require_stan_scalar_t<T>* = nullptr>
14inline T dot_self(const T& x) {
15 return x * x;
16}
17
18inline double dot_self(const std::vector<double>& x) {
19 double sum = 0.0;
20 for (double i : x) {
21 sum += i * i;
22 }
23 return sum;
24}
25
33template <typename T, require_eigen_t<T>* = nullptr,
34 require_not_eigen_vt<is_var, T>* = nullptr>
36 return make_holder([](auto&& v_) { return v_.squaredNorm(); },
37 to_ref(std::forward<T>(v)));
38}
39
40} // namespace math
41} // namespace stan
42
43#endif
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
auto make_holder(F &&func, Args &&... args)
Calls given function with given arguments.
Definition holder.hpp:481
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
auto dot_self(const T &a)
Returns squared norm of a vector or matrix.
Definition dot_self.hpp:21
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...