Stan Math Library  2.10.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 
8 #include <sstream>
9 #include <string>
10 
11 namespace stan {
12 
13  namespace math {
35  template <typename T_prob>
36  bool check_unit_vector(const char* function,
37  const char* name,
38  const Eigen::Matrix<T_prob,
39  Eigen::Dynamic, 1>& theta) {
40  check_nonzero_size(function, name, theta);
41  T_prob ssq = theta.squaredNorm();
42  if (!(fabs(1.0 - ssq) <= CONSTRAINT_TOLERANCE)) {
43  std::stringstream msg;
44  msg << "is not a valid unit vector."
45  << " The sum of the squares of the elements should be 1, but is ";
46  std::string msg_str(msg.str());
47  domain_error(function, name, ssq, msg_str.c_str());
48  }
49  return true;
50  }
51 
52  }
53 }
54 #endif
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:14
const double CONSTRAINT_TOLERANCE
The tolerance for checking arithmetic bounds In rank and in simplexes.
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.
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.
bool check_unit_vector(const char *function, const char *name, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)
Return true if the specified vector is unit vector.

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