Stan Math Library  2.11.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 
10 #include <cmath>
11 
12 namespace stan {
13  namespace math {
14 
23  template <typename T, int R, int C>
24  Eigen::Matrix<T, R, C>
25  unit_vector_constrain(const Eigen::Matrix<T, R, C>& y) {
26  using std::sqrt;
27  check_vector("unit_vector_constrain", "y", y);
28  check_nonzero_size("unit_vector_constrain", "y", y);
29  const T SN = dot_self(y);
30  check_positive_finite("unit_vector_constrain", "norm", SN);
31  return y / sqrt(SN);
32  }
33 
43  template <typename T, int R, int C>
44  Eigen::Matrix<T, R, C>
45  unit_vector_constrain(const Eigen::Matrix<T, R, C>& y, T& lp) {
46  using std::sqrt;
47  check_vector("unit_vector_constrain", "y", y);
48  check_nonzero_size("unit_vector_constrain", "y", y);
49  const T SN = dot_self(y);
50  check_positive_finite("unit_vector_constrain", "norm", SN);
51  lp -= 0.5 * SN;
52  return y / sqrt(SN);
53  }
54 
55  }
56 
57 }
58 
59 #endif
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:15
bool check_vector(const char *function, const char *name, const Eigen::Matrix< T, R, C > &x)
Return true 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:16
bool check_nonzero_size(const char *function, const char *name, const T_y &y)
Return true if the specified matrix/vector is of non-zero size.
Eigen::Matrix< fvar< T >, R, C > unit_vector_constrain(const Eigen::Matrix< fvar< T >, R, C > &y)
bool check_positive_finite(const char *function, const char *name, const T_y &y)
Return true if y is positive and finite.

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