Stan Math Library  2.20.0
reverse mode automatic differentiation
check_unit_vector.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_ERR_CHECK_UNIT_VECTOR_HPP
2 #define STAN_MATH_PRIM_MAT_ERR_CHECK_UNIT_VECTOR_HPP
3 
9 #include <sstream>
10 #include <string>
11 
12 namespace stan {
13 namespace math {
14 
31 template <typename T_prob>
32 void check_unit_vector(const char* function, const char* name,
33  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
34  check_nonzero_size(function, name, theta);
35  T_prob ssq = theta.squaredNorm();
36  if (!(fabs(1.0 - ssq) <= CONSTRAINT_TOLERANCE)) {
37  std::stringstream msg;
38  msg << "is not a valid unit vector."
39  << " The sum of the squares of the elements should be 1, but is ";
40  std::string msg_str(msg.str());
41  domain_error(function, name, ssq, msg_str.c_str());
42  }
43 }
44 
45 } // namespace math
46 } // namespace stan
47 #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 > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
void check_unit_vector(const char *function, const char *name, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)
Check if the specified vector is unit vector.
const double CONSTRAINT_TOLERANCE
The tolerance for checking arithmetic bounds In rank and in simplexes.
void domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.

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