Automatic Differentiation
 
Loading...
Searching...
No Matches
check_symmetric.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_ERR_CHECK_SYMMETRIC_HPP
2#define STAN_MATH_PRIM_ERR_CHECK_SYMMETRIC_HPP
3
12#include <sstream>
13#include <string>
14#include <cmath>
15
16namespace stan {
17namespace math {
18
31template <typename EigMat, require_matrix_t<EigMat>* = nullptr>
32inline void check_symmetric(const char* function, const char* name,
33 const EigMat& y) {
34 check_square(function, name, y);
35 using stan::math::fabs;
36 using std::fabs;
37
38 Eigen::Index k = y.rows();
39 if (k <= 1) {
40 return;
41 }
42 const auto& y_ref = to_ref(y);
43 for (Eigen::Index m = 0; m < k; ++m) {
44 for (Eigen::Index n = m + 1; n < k; ++n) {
45 if (!(fabs(value_of(y_ref(m, n)) - value_of(y_ref(n, m)))
47 [&]() STAN_COLD_PATH {
48 std::ostringstream msg1;
49 msg1 << "is not symmetric. " << name << "["
50 << stan::error_index::value + m << ","
51 << stan::error_index::value + n << "] = ";
52 std::string msg1_str(msg1.str());
53 std::ostringstream msg2;
54 msg2 << ", but " << name << "[" << stan::error_index::value + n << ","
55 << stan::error_index::value + m << "] = " << y_ref(n, m);
56 std::string msg2_str(msg2.str());
57 throw_domain_error(function, name, y_ref(m, n), msg1_str.c_str(),
58 msg2_str.c_str());
59 }();
60 }
61 }
62 }
63}
64
65} // namespace math
66} // namespace stan
67#endif
#define STAN_COLD_PATH
void check_symmetric(const char *function, const char *name, const matrix_cl< T > &y)
Check if the matrix_cl is symmetric.
void check_square(const char *function, const char *name, const T_y &y)
Check if the specified matrix is square.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
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.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
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:15
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...