Stan Math Library  2.20.0
reverse mode automatic differentiation
unit_vector_constrain.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_UNIT_VECTOR_CONSTRAIN_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_UNIT_VECTOR_CONSTRAIN_HPP
3 
9 #include <cmath>
10 
11 namespace stan {
12 namespace math {
13 
25 template <typename T, int R, int C>
26 Eigen::Matrix<T, R, C> unit_vector_constrain(const Eigen::Matrix<T, R, C>& y) {
27  using std::sqrt;
28  check_vector("unit_vector_constrain", "y", y);
29  check_nonzero_size("unit_vector_constrain", "y", y);
30  T SN = dot_self(y);
31  check_positive_finite("unit_vector_constrain", "norm", SN);
32  return y / sqrt(SN);
33 }
34 
44 template <typename T, int R, int C>
45 Eigen::Matrix<T, R, C> unit_vector_constrain(const Eigen::Matrix<T, R, C>& y,
46  T& lp) {
47  using std::sqrt;
48  check_vector("unit_vector_constrain", "y", y);
49  check_nonzero_size("unit_vector_constrain", "y", y);
50  T SN = dot_self(y);
51  check_positive_finite("unit_vector_constrain", "norm", SN);
52  lp -= 0.5 * SN;
53  return y / sqrt(SN);
54 }
55 
56 } // namespace math
57 } // namespace stan
58 #endif
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.
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:13
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.
fvar< T > dot_self(const Eigen::Matrix< fvar< T >, R, C > &v)
Definition: dot_self.hpp:13
Eigen::Matrix< fvar< T >, R, C > unit_vector_constrain(const Eigen::Matrix< fvar< T >, R, C > &y)
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.

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