Stan  1.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs 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>
9 
10 namespace stan {
11  namespace math {
12 
25  template <typename T_prob,
26  typename T_result,
27  class Policy>
28  bool check_unit_vector(const char* function,
29  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
30  const char* name,
31  T_result* result,
32  const Policy&) {
35  if (theta.size() == 0) {
36  std::string message(name);
37  message += " is not a valid unit vector. %1% elements in the vector.";
38  T_result tmp = raise_domain_error<size_t, size_t>(function,
39  message.c_str(),
40  0,
41  Policy());
42  if (result != 0)
43  *result = tmp;
44  return false;
45  }
46  T_prob ssq = theta.squaredNorm();
47  if (fabs(1.0 - ssq) > CONSTRAINT_TOLERANCE) {
48  std::stringstream msg;
49  msg << "in function check_unit_vector(%1%), ";
50  msg << name << " is not a valid unit vector.";
51  msg << " The sum of the squares of the elements should be 1, but is " << ssq;
52  T_result tmp = raise_domain_error<T_result,T_prob>(function,
53  msg.str().c_str(),
54  ssq,
55  Policy());
56  if (result != 0)
57  *result = tmp;
58  return false;
59  }
60  return true;
61  }
62  template <typename T_y,
63  typename T_result> // = typename T_prob_vector::value_type,
64  inline bool check_unit_vector(const char* function,
65  const Eigen::Matrix<T_y,Eigen::Dynamic,1>& theta,
66  const char* name,
67  T_result* result) {
68  return check_unit_vector(function,theta,name,result,default_policy());
69  }
70  template <typename T>
71  inline bool check_unit_vector(const char* function,
72  const Eigen::Matrix<T,Eigen::Dynamic,1>& theta,
73  const char* name,
74  T* result = 0) {
75  return check_unit_vector(function,theta,name,result,default_policy());
76  }
77 
78  }
79 }
80 #endif

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