Automatic Differentiation
 
Loading...
Searching...
No Matches
sum_to_zero_constrain.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CONSTRAINT_SUM_TO_ZERO_CONSTRAIN_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_SUM_TO_ZERO_CONSTRAIN_HPP
3
9#include <cmath>
10
11namespace stan {
12namespace math {
13
38template <typename Vec, require_eigen_col_vector_t<Vec>* = nullptr,
39 require_not_st_var<Vec>* = nullptr>
41 const auto N = y.size();
42
43 plain_type_t<Vec> z = Eigen::VectorXd::Zero(N + 1);
44 if (unlikely(N == 0)) {
45 return z;
46 }
47
48 auto&& y_ref = to_ref(y);
49
50 value_type_t<Vec> sum_w(0);
51 for (int i = N; i > 0; --i) {
52 double n = static_cast<double>(i);
53 auto w = y_ref(i - 1) * inv_sqrt(n * (n + 1));
54 sum_w += w;
55
56 z.coeffRef(i - 1) += sum_w;
57 z.coeffRef(i) -= w * n;
58 }
59
60 return z;
61}
62
88template <typename Vec, require_eigen_col_vector_t<Vec>* = nullptr,
89 require_not_st_var<Vec>* = nullptr>
92 return sum_to_zero_constrain(y);
93}
94
123template <bool Jacobian, typename Vec, require_not_std_vector_t<Vec>* = nullptr>
125 return_type_t<Vec>& lp) {
126 return sum_to_zero_constrain(y);
127}
128
157template <bool Jacobian, typename T, require_std_vector_t<T>* = nullptr>
158inline auto sum_to_zero_constrain(const T& y, return_type_t<T>& lp) {
160 y, [](auto&& v) { return sum_to_zero_constrain(v); });
161}
162
163} // namespace math
164} // namespace stan
165
166#endif
#define unlikely(x)
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
plain_type_t< Vec > sum_to_zero_constrain(const Vec &y)
Return a vector with sum zero corresponding to the specified free vector.
fvar< T > inv_sqrt(const fvar< T > &x)
Definition inv_sqrt.hpp:12
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...