Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
check_unit_vector.hpp
Go to the documentation of this file.
1 #ifndef STAN__MATH__ERROR_HANDLING__MATRIX__CHECK_UNIT_VECTOR_HPP
2 #define STAN__MATH__ERROR_HANDLING__MATRIX__CHECK_UNIT_VECTOR_HPP
3 
4 #include <sstream>
8 
9 namespace stan {
10 
11  namespace math {
12 
26  template <typename T_prob, typename T_result>
27  bool check_unit_vector(const char* function,
28  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
29  const char* name,
30  T_result* result) {
31  if (theta.size() == 0) {
32  std::string message(name);
33  message += " is not a valid unit vector. %1% elements in the vector.";
34  return dom_err(function,0,name,
35  message.c_str(),"",
36  result);
37  }
38  T_prob ssq = theta.squaredNorm();
39  if (!(fabs(1.0 - ssq) <= CONSTRAINT_TOLERANCE)) {
40  std::stringstream msg;
41  msg << "in function check_unit_vector(%1%), ";
42  msg << name << " is not a valid unit vector.";
43  msg << " The sum of the squares of the elements should be 1, but is "
44  << ssq;
45  std::string tmp(msg.str());
46  return dom_err(function,ssq,name,
47  tmp.c_str(),"",
48  result);
49  }
50  return true;
51  }
52 
53 
54  template <typename T>
55  inline bool check_unit_vector(const char* function,
56  const Eigen::Matrix<T,Eigen::Dynamic,1>& theta,
57  const char* name,
58  T* result = 0) {
59  return check_unit_vector<T,T>(function,theta,name,result);
60  }
61 
62 
63  }
64 }
65 #endif
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:18
bool dom_err(const char *function, const T &y, const char *name, const char *error_msg, const T_msg error_msg2, T_result *result)
Definition: dom_err.hpp:36
bool check_unit_vector(const char *function, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta, const char *name, T_result *result)
Return true if the specified vector is unit vector.
const double CONSTRAINT_TOLERANCE
The tolerance for checking arithmetic bounds In rank and in simplexes.

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