Skip to content

Commit

Permalink
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
Browse files Browse the repository at this point in the history
  • Loading branch information
stan-buildbot committed Jan 4, 2024
1 parent 068c38b commit 5fc064d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 32 deletions.
50 changes: 27 additions & 23 deletions test/unit/math/expect_near_rel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ namespace internal {
template <typename T1, typename T2, require_all_stan_scalar_t<T1, T2>...>
void expect_near_rel_finite(const std::string& msg, const T1& x1, const T2& x2,
const relative_tolerance tol = relative_tolerance(),
const char* x1_name = "x1", const char* x2_name = "x2") {
const char* x1_name = "x1",
const char* x2_name = "x2") {
double tol_val = tol.inexact(x1, x2);
EXPECT_NEAR(x1, x2, tol_val)
<< "expect_near_rel_finite in: " << msg << " for " << x1_name << " vs " << x2_name << std::endl;
<< "expect_near_rel_finite in: " << msg << " for " << x1_name << " vs "
<< x2_name << std::endl;
}

template <typename EigMat1, typename EigMat2,
require_all_eigen_t<EigMat1, EigMat2>...>
void expect_near_rel_finite(const std::string& msg, const EigMat1& x1,
const EigMat2& x2,
const char* x1_name = "x1", const char* x2_name = "x2") {
const EigMat2& x2, const char* x1_name = "x1",
const char* x2_name = "x2") {
EXPECT_EQ(x1.rows(), x2.rows());
EXPECT_EQ(x1.cols(), x2.cols());
auto x1_eval = x1.eval();
Expand All @@ -51,7 +53,8 @@ void expect_near_rel_finite(const std::string& msg, const EigMat1& x1,
template <typename T1, typename T2>
void expect_near_rel_finite(const std::string& msg, const std::vector<T1>& x1,
const std::vector<T2>& x2,
const char* x1_name = "x1", const char* x2_name = "x2") {
const char* x1_name = "x1",
const char* x2_name = "x2") {
EXPECT_EQ(x1.size(), x2.size());
for (size_t i = 0; i < x1.size(); ++i) {
expect_near_rel_finite(x1[i], x2[i], x1_name, x2_name);
Expand Down Expand Up @@ -85,7 +88,8 @@ void expect_near_rel(const std::string& msg, const T1& x1, const T2& x2,
} else if (stan::math::is_inf(x1) || stan::math::is_inf(x2)) {
EXPECT_EQ(x1, x2) << "expect_near_rel(" << x1 << ", " << x2 << ")"
<< std::endl
<< msg << " for " << x1_name << " vs " << x2_name << std::endl;
<< msg << " for " << x1_name << " vs " << x2_name
<< std::endl;
} else {
internal::expect_near_rel_finite(msg, x1, x2, tol, x1_name, x2_name);
}
Expand All @@ -111,16 +115,15 @@ template <typename EigMat1, typename EigMat2,
void expect_near_rel(const std::string& msg, EigMat1&& x1, EigMat2&& x2,
relative_tolerance tol = relative_tolerance(),
const char* x1_name = "x1", const char* x2_name = "x2") {
EXPECT_EQ(x1.rows(), x2.rows()) << "expect_near_rel (Eigen::Matrix)"
<< " rows must be same size."
<< x1_name << ".rows() = " << x1.rows()
<< "; " << x2_name << ".rows() = " << x2.rows() << std::endl
<< msg << std::endl;
EXPECT_EQ(x1.rows(), x2.rows())
<< "expect_near_rel (Eigen::Matrix)"
<< " rows must be same size." << x1_name << ".rows() = " << x1.rows()
<< "; " << x2_name << ".rows() = " << x2.rows() << std::endl
<< msg << std::endl;

EXPECT_EQ(x1.cols(), x2.cols())
<< "expect_near_rel:"
<< "cols must be same size."
<< x1_name << ".cols() = " << x1.cols()
<< "cols must be same size." << x1_name << ".cols() = " << x1.cols()
<< "; " << x2_name << ".cols() = " << x2.cols() << std::endl
<< msg << std::endl;
auto x1_eval = x1.eval();
Expand All @@ -133,10 +136,11 @@ void expect_near_rel(const std::string& msg, EigMat1&& x1, EigMat2&& x2,
msg2 += std::to_string(sentinal_val) + ") = x2("
+ std::to_string(sentinal_val) + "): " + msg;
} else {
msg2 += std::to_string(i) + ", " + std::to_string(j) + ") = x2(" +
std::to_string(i) + ", " + std::to_string(j) + "): " + msg;
msg2 += std::to_string(i) + ", " + std::to_string(j) + ") = x2("
+ std::to_string(i) + ", " + std::to_string(j) + "): " + msg;
}
expect_near_rel(msg2, x1_eval(sentinal_val), x2_eval(sentinal_val), tol, x1_name, x2_name);
expect_near_rel(msg2, x1_eval(sentinal_val), x2_eval(sentinal_val), tol,
x1_name, x2_name);
sentinal_val++;
}
}
Expand Down Expand Up @@ -166,13 +170,13 @@ void expect_near_rel(const std::string& msg, const std::vector<T1>& x1,
const std::vector<T2>& x2,
relative_tolerance tol = relative_tolerance(),
const char* x1_name = "x1", const char* x2_name = "x2") {
EXPECT_EQ(x1.size(), x2.size()) << "expect_near_rel (std::vector):"
<< " vectors must be same size. "
<< x1_name << ".size() = " << x1.size()
<< "; " << x2_name << ".size() = " << x2.size() << std::endl
<< msg << std::endl;
std::string msg2 = std::string("expect_near_rel; require items ") +
"x1[i] = " + "x2[i]: " + msg + " for " + x1_name + " vs " + x2_name;
EXPECT_EQ(x1.size(), x2.size())
<< "expect_near_rel (std::vector):"
<< " vectors must be same size. " << x1_name << ".size() = " << x1.size()
<< "; " << x2_name << ".size() = " << x2.size() << std::endl
<< msg << std::endl;
std::string msg2 = std::string("expect_near_rel; require items ") + "x1[i] = "
+ "x2[i]: " + msg + " for " + x1_name + " vs " + x2_name;
for (size_t i = 0; i < x1.size(); ++i)
expect_near_rel(msg2, x1[i], x2[i], tol, x1_name, x2_name);
}
Expand Down
27 changes: 18 additions & 9 deletions test/unit/math/test_ad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ void test_gradient(const ad_tolerances& tols, const F& f,
Eigen::VectorXd grad_ad;
double fx_ad = fx;
stan::math::gradient<F>(f, x, fx_ad, grad_ad);
expect_near_rel("gradient() val", fx, fx_ad, tols.gradient_val_, "double", "var");
expect_near_rel("gradient() val", fx, fx_ad, tols.gradient_val_, "double",
"var");
if (!test_derivs || !is_finite(x) || !is_finite(fx))
return;
Eigen::VectorXd grad_fd;
Expand Down Expand Up @@ -169,7 +170,8 @@ void test_gradient_fvar(const ad_tolerances& tols, const F& f,
Eigen::VectorXd grad_ad;
double fx_ad = fx;
stan::math::gradient<double, F>(f, x, fx_ad, grad_ad);
expect_near_rel("gradient_fvar() val", fx, fx_ad, tols.gradient_fvar_val_, "double", "fvar<double>");
expect_near_rel("gradient_fvar() val", fx, fx_ad, tols.gradient_fvar_val_,
"double", "fvar<double>");
if (!test_derivs || !is_finite(x) || !is_finite(fx))
return;
Eigen::VectorXd grad_fd;
Expand Down Expand Up @@ -210,7 +212,8 @@ void test_hessian_fvar(const ad_tolerances& tols, const F& f,
Eigen::VectorXd grad_ad;
Eigen::MatrixXd H_ad;
stan::math::hessian<double, F>(f, x, fx_ad, grad_ad, H_ad);
expect_near_rel("hessian_fvar() val", fx, fx_ad, tols.hessian_fvar_val_, "double", "fvar<fvar<double>>");
expect_near_rel("hessian_fvar() val", fx, fx_ad, tols.hessian_fvar_val_,
"double", "fvar<fvar<double>>");
if (!test_derivs || !is_finite(x) || !is_finite(fx))
return;
double fx_fd;
Expand All @@ -220,7 +223,8 @@ void test_hessian_fvar(const ad_tolerances& tols, const F& f,
expect_near_rel("hessian_fvar() grad", grad_fd, grad_ad,
tols.hessian_fvar_grad_, "finite_diff", "fvar<fvar<double>>");
expect_near_rel("hessian_fvar() Hessian", H_fd, H_ad,
tols.hessian_fvar_hessian_, "finite_diff", "fvar<fvar<double>>");
tols.hessian_fvar_hessian_, "finite_diff",
"fvar<fvar<double>>");
}

/**
Expand Down Expand Up @@ -254,15 +258,18 @@ void test_hessian(const ad_tolerances& tols, const F& f,
Eigen::VectorXd grad_ad;
Eigen::MatrixXd H_ad;
stan::math::hessian<F>(f, x, fx_ad, grad_ad, H_ad);
expect_near_rel("hessian val", fx, fx_ad, tols.hessian_val_, "double", "fvar<var>");
expect_near_rel("hessian val", fx, fx_ad, tols.hessian_val_, "double",
"fvar<var>");
if (!test_derivs || !is_finite(x) || !is_finite(fx))
return;
double fx_fd;
Eigen::VectorXd grad_fd;
Eigen::MatrixXd H_fd;
stan::math::internal::finite_diff_hessian_auto(f, x, fx_fd, grad_fd, H_fd);
expect_near_rel("hessian() grad", grad_fd, grad_ad, tols.hessian_grad_, "finite_diff", "fvar<var>");
expect_near_rel("hessian() Hessian", H_fd, H_ad, tols.hessian_hessian_, "finite_diff", "fvar<var>");
expect_near_rel("hessian() grad", grad_fd, grad_ad, tols.hessian_grad_,
"finite_diff", "fvar<var>");
expect_near_rel("hessian() Hessian", H_fd, H_ad, tols.hessian_hessian_,
"finite_diff", "fvar<var>");
}

/**
Expand Down Expand Up @@ -296,7 +303,8 @@ void test_grad_hessian(const ad_tolerances& tols, const F& f,
Eigen::MatrixXd H_ad;
std::vector<Eigen::MatrixXd> grad_H_ad;
stan::math::grad_hessian(f, x, fx_ad, H_ad, grad_H_ad);
expect_near_rel("grad_hessian() val", fx, fx_ad, tols.grad_hessian_val_, "double", "fvar<fvar<var>>");
expect_near_rel("grad_hessian() val", fx, fx_ad, tols.grad_hessian_val_,
"double", "fvar<fvar<var>>");
if (!test_derivs || !is_finite(x) || !is_finite(fx))
return;
double fx_fd;
Expand All @@ -308,7 +316,8 @@ void test_grad_hessian(const ad_tolerances& tols, const F& f,
EXPECT_EQ(x.size(), grad_H_fd.size());
for (size_t i = 0; i < grad_H_fd.size(); ++i)
expect_near_rel("grad_hessian() grad Hessian", grad_H_fd[i], grad_H_ad[i],
tols.grad_hessian_grad_hessian_, "finite_diff", "fvar<fvar<var>>");
tols.grad_hessian_grad_hessian_, "finite_diff",
"fvar<fvar<var>>");
}
#endif

Expand Down

0 comments on commit 5fc064d

Please sign in to comment.