Automatic Differentiation
 
Loading...
Searching...
No Matches
unit_vector_constrain.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_UNIT_VECTOR_CONSTRAIN_HPP
2#define STAN_MATH_PRIM_FUN_UNIT_VECTOR_CONSTRAIN_HPP
3
8#include <cmath>
9
10namespace stan {
11namespace math {
12
25template <typename T, require_eigen_col_vector_t<T>* = nullptr,
26 require_not_vt_autodiff<T>* = nullptr>
28 using std::sqrt;
29 check_nonzero_size("unit_vector_constrain", "y", y);
30 auto&& y_ref = to_ref(y);
31 value_type_t<T> SN = dot_self(y_ref);
32 check_positive_finite("unit_vector_constrain", "norm", SN);
33 return y_ref.array() / sqrt(SN);
34}
35
47template <typename T1, typename T2, require_eigen_col_vector_t<T1>* = nullptr,
48 require_all_not_vt_autodiff<T1, T2>* = nullptr>
49inline plain_type_t<T1> unit_vector_constrain(const T1& y, T2& lp) {
50 using std::sqrt;
51 check_nonzero_size("unit_vector_constrain", "y", y);
52 auto&& y_ref = to_ref(y);
53 value_type_t<T1> SN = dot_self(y_ref);
54 check_positive_finite("unit_vector_constrain", "norm", SN);
55 lp -= 0.5 * SN;
56 return y_ref.array() / sqrt(SN);
57}
58
75template <bool Jacobian, typename T, require_not_std_vector_t<T>* = nullptr>
76inline auto unit_vector_constrain(const T& y, return_type_t<T>& lp) {
77 if (Jacobian) {
78 return unit_vector_constrain(y, lp);
79 } else {
80 return unit_vector_constrain(y);
81 }
82}
83
100template <bool Jacobian, typename T, require_std_vector_t<T>* = nullptr>
101inline auto unit_vector_constrain(const T& y, return_type_t<T>& lp) {
102 return apply_vector_unary<T>::apply(
103 y, [&lp](auto&& v) { return unit_vector_constrain<Jacobian>(v, lp); });
104}
105
106} // namespace math
107} // namespace stan
108
109#endif
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.
auto unit_vector_constrain(const EigMat &y)
fvar< T > sqrt(const fvar< T > &x)
Definition sqrt.hpp:17
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
void check_nonzero_size(const char *function, const char *name, const T_y &y)
Check if the specified matrix/vector is of non-zero size.
auto dot_self(const T &a)
Returns squared norm of a vector or matrix.
Definition dot_self.hpp:21
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9