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 namespace math {
13 /**
14  * Check if the specified vector is unit vector.
15  *
16  * A valid unit vector is one where the square of the elements
17  * summed is equal to 1. This function tests that the sum is within the
18  * tolerance specified by <code>CONSTRAINT_TOLERANCE</code>. This
19  * function only accepts Eigen vectors, statically typed vectors,
20  * not general matrices with 1 column.
21  *
22  * @tparam T_prob Scalar type of the vector
23  *
24  * @param function Function name (for error messages)
25  * @param name Variable name (for error messages)
26  * @param theta Vector to test.
27  *
28  * @throw <code>std::invalid_argument</code> if <code>theta</code>
29  * is a 0-vector.
30  * @throw <code>std::domain_error</code> if the vector is not a unit
31  * vector or if any element is <code>NaN</code>.
32  */
33 template <typename T_prob>
34 void check_unit_vector(const char* function, const char* name,
35  const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
36  check_nonzero_size(function, name, theta);
37  T_prob ssq = theta.squaredNorm();
38  if (!(fabs(1.0 - ssq) <= CONSTRAINT_TOLERANCE)) {
39  std::stringstream msg;
40  msg << "is not a valid unit vector."
41  << " The sum of the squares of the elements should be 1, but is ";
42  std::string msg_str(msg.str());
43  domain_error(function, name, ssq, msg_str.c_str());
44  }
45 }
46 
47 } // namespace math
48 } // namespace stan
49 #endif
const XML_Char * name
Definition: expat.h:151
void check_nonzero_size(const char *function, const char *name, const T_y &y)
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)
const double CONSTRAINT_TOLERANCE
void domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
enum BeamMode string