Automatic Differentiation
 
Loading...
Searching...
No Matches
check_unit_vector.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_ERR_CHECK_UNIT_VECTOR_HPP
2#define STAN_MATH_PRIM_ERR_CHECK_UNIT_VECTOR_HPP
3
13#include <sstream>
14#include <string>
15#include <cmath>
16
17namespace stan {
18namespace math {
19
35template <typename Vec, require_vector_t<Vec>* = nullptr,
36 require_not_std_vector_t<Vec>* = nullptr>
37void check_unit_vector(const char* function, const char* name,
38 const Vec& theta) {
39 check_nonzero_size(function, name, theta);
40 using std::fabs;
41 scalar_type_t<Vec> ssq = value_of_rec(theta).squaredNorm();
42 if (!(fabs(1.0 - ssq) <= CONSTRAINT_TOLERANCE)) {
43 [&]() STAN_COLD_PATH {
44 std::stringstream msg;
45 msg << "is not a valid unit vector."
46 << " The sum of the squares of the elements should be 1, but is ";
47 std::string msg_str(msg.str());
48 throw_domain_error(function, name, ssq, msg_str.c_str());
49 }();
50 }
51}
52
66template <typename StdVec, require_std_vector_t<StdVec>* = nullptr>
67void check_unit_vector(const char* function, const char* name,
68 const StdVec& theta) {
69 for (size_t i = 0; i < theta.size(); ++i) {
70 check_unit_vector(function, internal::make_iter_name(name, i).c_str(),
71 theta[i]);
72 }
73}
74
75} // namespace math
76} // namespace stan
77#endif
#define STAN_COLD_PATH
auto make_iter_name(const char *name)
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
void check_unit_vector(const char *function, const char *name, const Vec &theta)
Throw an exception if the specified vector does not have unit Euclidiean length.
void throw_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.
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.
const double CONSTRAINT_TOLERANCE
The tolerance for checking arithmetic bounds in rank and in simplexes.
fvar< T > fabs(const fvar< T > &x)
Definition fabs.hpp:16
typename scalar_type< T >::type scalar_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...