Automatic Differentiation
 
Loading...
Searching...
No Matches
is_symmetric.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_ERR_IS_SYMMETRIC_HPP
2#define STAN_MATH_PRIM_ERR_IS_SYMMETRIC_HPP
3
10#include <cmath>
11
12namespace stan {
13namespace math {
14
23template <typename EigMat, require_eigen_matrix_dynamic_t<EigMat>* = nullptr>
24inline bool is_symmetric(const EigMat& y) {
25 const auto& y_ref = to_ref(y);
26 if (!is_square(y_ref)) {
27 return false;
28 }
29 Eigen::Index k = y_ref.rows();
30 if (k == 1) {
31 return true;
32 }
33 for (Eigen::Index m = 0; m < k; ++m) {
34 for (Eigen::Index n = m + 1; n < k; ++n) {
35 if (!(fabs(value_of(y_ref(m, n)) - value_of(y_ref(n, m)))
37 return false;
38 }
39 }
40 }
41 return true;
42}
43
44} // namespace math
45} // namespace stan
46#endif
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
bool is_square(const EigMat &y)
Return true if the matrix is square.
Definition is_square.hpp:19
bool is_symmetric(const EigMat &y)
Return true if the matrix is square, and no element not on the main diagonal is NaN.
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
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...