24 template <
typename DerivedA,
typename DerivedB>
26 const Eigen::MatrixBase<DerivedB>& m2,
29 std::string* explanation =
nullptr) {
32 if (m1.rows() != m2.rows() || m1.cols() != m2.cols()) {
33 if (explanation !=
nullptr) {
34 std::stringstream msg;
35 msg <<
"Matrix size mismatch: (" << m1.rows() <<
" x " << m1.cols()
36 <<
" vs. " << m2.rows() <<
" x " << m2.rows() <<
")";
37 *explanation = msg.str();
42 for (
int ii = 0; result && ii < m1.rows(); ii++) {
43 for (
int jj = 0; result && jj < m1.cols(); jj++) {
46 bool both_positive_infinity =
47 m1(ii, jj) == std::numeric_limits<double>::infinity() &&
48 m2(ii, jj) == std::numeric_limits<double>::infinity();
50 bool both_negative_infinity =
51 m1(ii, jj) == -std::numeric_limits<double>::infinity() &&
52 m2(ii, jj) == -std::numeric_limits<double>::infinity();
54 bool both_nan = std::isnan(m1(ii, jj)) && std::isnan(m2(ii, jj));
56 if (both_positive_infinity || both_negative_infinity || both_nan)
60 if ((std::isnan(m1(ii, jj)) && !std::isnan(m2(ii, jj))) ||
61 (!std::isnan(m1(ii, jj)) && std::isnan(m2(ii, jj)))) {
62 if (explanation !=
nullptr) {
63 std::stringstream msg;
64 msg <<
"NaN missmatch at (" << ii <<
", " << jj <<
"):\nm1 =\n" 67 *explanation = msg.str();
75 double delta =
std::abs(m1(ii, jj) - m2(ii, jj));
80 if (delta > tolerance) {
81 if (explanation !=
nullptr) {
82 std::stringstream msg;
83 msg <<
"Values at (" << ii <<
", " << jj
84 <<
") exceed tolerance: " << m1(ii, jj) <<
" vs. " << m2(ii, jj)
85 <<
", diff = " << delta <<
", tolerance = " << tolerance
91 *explanation = msg.str();
99 double relative_tolerance = tolerance * std::max(1.0, max_value);
101 if (delta > relative_tolerance) {
102 if (explanation !=
nullptr) {
103 std::stringstream msg;
104 msg <<
"Values at (" << ii <<
", " << jj
105 <<
") exceed tolerance: " << m1(ii, jj) <<
" vs. " << m2(ii, jj)
106 <<
", diff = " << delta <<
", tolerance = " << tolerance
107 <<
", relative tolerance = " << relative_tolerance <<
"\nm1 =\n" 109 << m2 <<
"\ndelta=\n" 112 *explanation = msg.str();
Definition: eigen_matrix_compare.h:9
Definition: constants.h:3
FunctionalForm abs(FunctionalForm const &x)
Definition: functional_form.cc:183
Definition: eigen_matrix_compare.h:9
std::vector< Number > result
Definition: IpoptSolver.cpp:170
MatrixCompareType
Definition: eigen_matrix_compare.h:9
bool CompareMatrices(const Eigen::MatrixBase< DerivedA > &m1, const Eigen::MatrixBase< DerivedB > &m2, double tolerance, MatrixCompareType compare_type, std::string *explanation=nullptr)
Compares two matrices to determine whether they are equal to within a certain threshold.
Definition: eigen_matrix_compare.h:25