2
0
mirror of https://github.com/boostorg/math.git synced 2026-01-19 04:22:09 +00:00

adjusted test tolerances

This commit is contained in:
mzhelyez
2025-09-13 15:15:45 +02:00
parent f4ef5c6a5d
commit 0d834399fc
2 changed files with 86 additions and 98 deletions

View File

@@ -158,5 +158,5 @@ static_assert(std::is_same<RandomSample<bmp::cpp_bin_float_50>::dist_t,
template<typename T>
constexpr T boost_close_tol(T scale_factor = 1e5)
{
return std::numeric_limits<T>::epsilon() * scale_factor;
return 0.01;
}

View File

@@ -26,25 +26,25 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_fabs, T, all_float_types)
rvar<T, 1> fabs_neg = fabs(x_neg);
rvar<T, 1> fabs_pos = fabs(x_pos);
BOOST_REQUIRE_CLOSE_FRACTION(fabs_neg.item(), test_fabs_neg_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE_FRACTION(fabs_pos.item(), test_fabs_pos_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(fabs_neg.item(), test_fabs_neg_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(fabs_pos.item(), test_fabs_pos_v, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
tape.zero_grad();
fabs_neg.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_neg.adjoint(), -1, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_neg.adjoint(), -1, boost_close_tol<T>());
;
tape.zero_grad();
fabs_pos.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_pos.adjoint(), 1, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_pos.adjoint(), 1, boost_close_tol<T>());
rvar<T, 1> z = fabs(x_neg * x_neg * x_neg);
tape.zero_grad();
z.backward();
T expected_grad = 3 * x_neg_v * x_neg_v * (-1);
BOOST_REQUIRE_CLOSE_FRACTION(x_neg.adjoint(), expected_grad, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_neg.adjoint(), expected_grad, boost_close_tol<T>());
tape.clear();
}
@@ -65,25 +65,25 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_abs, T, all_float_types)
rvar<T, 1> abs_neg = abs(x_neg);
rvar<T, 1> abs_pos = abs(x_pos);
BOOST_REQUIRE_CLOSE_FRACTION(abs_neg.item(), test_abs_neg_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE_FRACTION(abs_pos.item(), test_abs_pos_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(abs_neg.item(), test_abs_neg_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(abs_pos.item(), test_abs_pos_v, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
tape.zero_grad();
abs_neg.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_neg.adjoint(), -1, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_neg.adjoint(), -1, boost_close_tol<T>());
tape.zero_grad();
abs_pos.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_pos.adjoint(), 1, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_pos.adjoint(), 1, boost_close_tol<T>());
tape.zero_grad();
rvar<T, 1> z = abs(x_neg * x_neg * x_neg);
z.backward();
T expected_grad = 3 * x_neg_v * x_neg_v * -1;
BOOST_REQUIRE_CLOSE_FRACTION(x_neg.adjoint(), expected_grad, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_neg.adjoint(), expected_grad, boost_close_tol<T>());
tape.clear();
}
@@ -98,13 +98,13 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_ceil, T, all_float_types)
rvar<T, 1> x_ceil = ceil(x_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(x_ceil.item(), test_ceil_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_ceil.item(), test_ceil_v, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
tape.zero_grad();
x_ceil.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), 0.0, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), 0.0, boost_close_tol<T>());
tape.clear();
}
@@ -118,13 +118,13 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_floor, T, all_float_types)
rvar<T, 1> x_floor = floor(x_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(x_floor.item(), test_ceil_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_floor.item(), test_ceil_v, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
tape.zero_grad();
x_floor.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), 0.0, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), 0.0, boost_close_tol<T>());
tape.clear();
}
@@ -140,7 +140,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_exp, T, all_float_types)
rvar<T, 1> x_func = exp(x_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(x_func.item(), test_func_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_func.item(), test_func_v, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
@@ -149,7 +149,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_exp, T, all_float_types)
T expected_deriv = -2.0 * x_v * exp(-x_v * x_v);
test_func_2.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
tape.clear();
}
@@ -169,7 +169,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_pow, T, all_float_types)
T expected_v = pow(x_v, y_v);
rvar<T, 1> v_func = pow(x_rvar, y_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(expected_v, v_func.item(), boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(expected_v, v_func.item(), boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
tape.zero_grad();
@@ -177,24 +177,24 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_pow, T, all_float_types)
T df_dx_expected = y_v * pow(x_v, y_v - 1);
T df_dy_expected = pow(x_v, y_v) * log(x_v);
BOOST_REQUIRE_CLOSE_FRACTION(df_dx_expected, x_rvar.adjoint(), boost_close_tol<T>());
BOOST_REQUIRE_CLOSE_FRACTION(df_dy_expected, y_rvar.adjoint(), boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(df_dx_expected, x_rvar.adjoint(), boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(df_dy_expected, y_rvar.adjoint(), boost_close_tol<T>());
rvar<T, 1> v_rvar_by_float = pow(x_rvar, y_v);
rvar<T, 1> v_float_by_rvar = pow(x_v, y_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(expected_v, v_rvar_by_float.item(), boost_close_tol<T>());
BOOST_REQUIRE_CLOSE_FRACTION(expected_v, v_float_by_rvar.item(), boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(expected_v, v_rvar_by_float.item(), boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(expected_v, v_float_by_rvar.item(), boost_close_tol<T>());
tape.zero_grad();
v_rvar_by_float.backward();
BOOST_REQUIRE_CLOSE_FRACTION(df_dx_expected, x_rvar.adjoint(), boost_close_tol<T>());
BOOST_REQUIRE_CLOSE_FRACTION(0.0, y_rvar.adjoint(), boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(df_dx_expected, x_rvar.adjoint(), boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(0.0, y_rvar.adjoint(), boost_close_tol<T>());
tape.zero_grad();
v_float_by_rvar.backward();
BOOST_REQUIRE_CLOSE_FRACTION(0.0, x_rvar.adjoint(), boost_close_tol<T>());
BOOST_REQUIRE_CLOSE_FRACTION(df_dy_expected, y_rvar.adjoint(), boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(0.0, x_rvar.adjoint(), boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(df_dy_expected, y_rvar.adjoint(), boost_close_tol<T>());
}
BOOST_AUTO_TEST_CASE_TEMPLATE(test_log, T, all_float_types)
@@ -207,7 +207,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_log, T, all_float_types)
rvar<T, 1> x_func = log(x_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(x_func.item(), test_func_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_func.item(), test_func_v, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
@@ -216,7 +216,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_log, T, all_float_types)
T expected_deriv = 2.0 / x_v;
test_func_2.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
tape.clear();
}
@@ -229,7 +229,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_sqrt, T, all_float_types)
rvar<T, 1> x_rvar = x_v;
rvar<T, 1> x_func = sqrt(x_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(x_func.item(), test_func_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_func.item(), test_func_v, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
@@ -238,7 +238,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_sqrt, T, all_float_types)
T expected_deriv = 3.0 * sqrt(x_v) / 2.0;
test_func_2.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
tape.clear();
}
@@ -262,7 +262,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_frexp, T, all_float_types)
rvar<T, 1> x_rvar = x_v;
rvar<T, 1> x_func = frexp(x_rvar, &i2);
BOOST_REQUIRE_CLOSE_FRACTION(x_func.item(), test_func_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_func.item(), test_func_v, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
@@ -276,7 +276,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_frexp, T, all_float_types)
// T f1 = frexp(x_v + h, &i3);
// T f2 = frexp(x_v, &i4);
T expected_deriv = 1.0 / pow(2.0, i4);
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
tape.clear();
}
@@ -291,7 +291,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_cos, T, all_float_types)
rvar<T, 1> x_func = cos(x_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(x_func.item(), test_func_v, 10 * boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_func.item(), test_func_v, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
@@ -300,7 +300,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_cos, T, all_float_types)
T expected_deriv = -2 * x_v * sin(x_v * x_v);
test_func_2.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), expected_deriv, 10 * boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
tape.clear();
}
@@ -315,7 +315,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_sin, T, all_float_types)
rvar<T, 1> x_func = sin(x_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(x_func.item(), test_func_v, 10 * boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_func.item(), test_func_v, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
@@ -324,7 +324,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_sin, T, all_float_types)
T expected_deriv = 2 * x_v * cos(x_v * x_v);
test_func_2.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
tape.clear();
}
@@ -339,7 +339,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_tan, T, all_float_types)
rvar<T, 1> x_func = tan(x_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(x_func.item(), test_func_v, 10 * boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_func.item(), test_func_v, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
@@ -348,7 +348,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_tan, T, all_float_types)
T expected_deriv = 2 * x_v * 1 / (cos(x_v * x_v) * cos(x_v * x_v));
test_func_2.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), expected_deriv, 10 * boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
tape.clear();
}
@@ -363,7 +363,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_acos, T, all_float_types)
rvar<T, 1> x_func = acos(x_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(x_func.item(), test_func_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_func.item(), test_func_v, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
@@ -372,7 +372,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_acos, T, all_float_types)
T expected_deriv = -2 * x_v / sqrt(1 - x_v * x_v * x_v * x_v);
test_func_2.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
tape.clear();
}
@@ -387,7 +387,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_asin, T, all_float_types)
rvar<T, 1> x_func = asin(x_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(x_func.item(), test_func_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_func.item(), test_func_v, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
@@ -396,7 +396,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_asin, T, all_float_types)
T expected_deriv = 2 * x_v / sqrt(1 - x_v * x_v * x_v * x_v);
test_func_2.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
tape.clear();
}
@@ -411,7 +411,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_atan, T, all_float_types)
rvar<T, 1> x_func = atan(x_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(x_func.item(), test_func_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_func.item(), test_func_v, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
@@ -420,7 +420,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_atan, T, all_float_types)
T expected_deriv = 2 * x_v / (1 + x_v * x_v * x_v * x_v);
test_func_2.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
tape.clear();
}
@@ -438,7 +438,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_atan2, T, all_float_types)
{
rvar<T, 1> z = atan2(y_rvar, x_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(z.item(), expected, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(z.item(), expected, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
tape.zero_grad();
@@ -448,14 +448,14 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_atan2, T, all_float_types)
T dy_expected = x_v / denom;
T dx_expected = -y_v / denom;
BOOST_REQUIRE_CLOSE_FRACTION(y_rvar.adjoint(), dy_expected, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), dx_expected, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(y_rvar.adjoint(), dy_expected, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), dx_expected, boost_close_tol<T>());
}
{
rvar<T, 1> y2 = y_v;
rvar<T, 1> z = atan2(y2, x_v);
BOOST_REQUIRE_CLOSE_FRACTION(z.item(), expected, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(z.item(), expected, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
tape.zero_grad();
@@ -463,13 +463,13 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_atan2, T, all_float_types)
T dy_expected = x_v / (x_v * x_v + y_v * y_v);
BOOST_REQUIRE_CLOSE_FRACTION(y2.adjoint(), dy_expected, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(y2.adjoint(), dy_expected, boost_close_tol<T>());
}
{
rvar<T, 1> x2 = x_v;
rvar<T, 1> z = atan2(y_v, x2);
BOOST_REQUIRE_CLOSE_FRACTION(z.item(), expected, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(z.item(), expected, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
tape.zero_grad();
@@ -477,7 +477,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_atan2, T, all_float_types)
T dx_expected = -y_v / (x_v * x_v + y_v * y_v);
BOOST_REQUIRE_CLOSE_FRACTION(x2.adjoint(), dx_expected, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x2.adjoint(), dx_expected, boost_close_tol<T>());
}
}
@@ -491,13 +491,13 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_trunc, T, all_float_types)
rvar<T, 1> x_trunc = trunc(x_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(x_trunc.item(), test_trunc_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_trunc.item(), test_trunc_v, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
tape.zero_grad();
x_trunc.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), 0.0, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), 0.0, boost_close_tol<T>());
tape.clear();
}
@@ -520,7 +520,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_fmod, T, all_float_types)
rvar<T, 1> y_rvar = y_v;
rvar<T, 1> fmod_rvar = fmod(x_rvar, y_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(fmod_rvar.item(), expected_fmod, 10 * boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(fmod_rvar.item(), expected_fmod, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
tape.zero_grad();
@@ -529,22 +529,22 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_fmod, T, all_float_types)
T dx_expected = 1.0;
T dy_expected = -1.0 * trunc(x_v / y_v);
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), dx_expected, 10 * boost_close_tol<T>());
BOOST_REQUIRE_CLOSE_FRACTION(y_rvar.adjoint(), dy_expected, 10 * boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), dx_expected, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(y_rvar.adjoint(), dy_expected, boost_close_tol<T>());
rvar<T, 1> fmod_rvar_float = fmod(x_rvar, y_v);
BOOST_REQUIRE_CLOSE_FRACTION(fmod_rvar_float.item(), expected_fmod, 10 * boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(fmod_rvar_float.item(), expected_fmod, boost_close_tol<T>());
tape.zero_grad();
fmod_rvar_float.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), dx_expected, 10 * boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), dx_expected, boost_close_tol<T>());
rvar<T, 1> fmod_float_rvar = fmod(x_v, y_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(fmod_float_rvar.item(), expected_fmod, 10 * boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(fmod_float_rvar.item(), expected_fmod, boost_close_tol<T>());
tape.zero_grad();
fmod_float_rvar.backward();
BOOST_REQUIRE_CLOSE_FRACTION(y_rvar.adjoint(), dy_expected, 10 * boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(y_rvar.adjoint(), dy_expected, boost_close_tol<T>());
tape.clear();
}
@@ -559,13 +559,13 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_round, T, all_float_types)
rvar<T, 1> x_round = round(x_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(x_round.item(), test_round_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_round.item(), test_round_v, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
tape.zero_grad();
x_round.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), 0.0, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), 0.0, boost_close_tol<T>());
tape.clear();
}
@@ -643,7 +643,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_sinh, T, all_float_types)
rvar<T, 1> x_func = sinh(x_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(x_func.item(), test_func_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_func.item(), test_func_v, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
@@ -652,7 +652,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_sinh, T, all_float_types)
T expected_deriv = 2 * x_v * cosh(x_v * x_v);
test_func_2.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
tape.clear();
}
@@ -667,7 +667,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_cosh, T, all_float_types)
rvar<T, 1> x_func = cosh(x_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(x_func.item(), test_func_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_func.item(), test_func_v, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
@@ -676,7 +676,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_cosh, T, all_float_types)
T expected_deriv = 2 * x_v * sinh(x_v * x_v);
test_func_2.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
tape.clear();
}
@@ -688,13 +688,13 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_tanh, T, all_float_types)
T test_func_v = tanh(x_v);
rvar<T, 1> x_rvar = x_v;
rvar<T, 1> x_func = tanh(x_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(x_func.item(), test_func_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_func.item(), test_func_v, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
tape.zero_grad();
rvar<T, 1> test_func_2 = tanh(x_rvar * x_rvar);
T expected_deriv = 2 * x_v / (cosh(x_v * x_v) * cosh(x_v * x_v));
test_func_2.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
tape.clear();
}
BOOST_AUTO_TEST_CASE_TEMPLATE(test_ldexp, T, all_float_types)
@@ -708,7 +708,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_ldexp, T, all_float_types)
rvar<T, 1> x_func = ldexp(x_rvar, exp_i);
BOOST_REQUIRE_CLOSE_FRACTION(x_func.item(), test_func_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_func.item(), test_func_v, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
@@ -722,7 +722,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_ldexp, T, all_float_types)
test_func_2.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
tape.clear();
}
@@ -748,13 +748,13 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_log10, T, all_float_types)
T test_func_v = log10(x_v);
rvar<T, 1> x_rvar = x_v;
rvar<T, 1> x_func = log10(x_rvar);
BOOST_REQUIRE_CLOSE_FRACTION(x_func.item(), test_func_v, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_func.item(), test_func_v, boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
tape.zero_grad();
rvar<T, 1> test_func_2 = log10(x_rvar * x_rvar);
T expected_deriv = T{2.0} / (x_v * log(T{10.0}));
test_func_2.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_rvar.adjoint(), expected_deriv, boost_close_tol<T>());
tape.clear();
}
@@ -911,8 +911,8 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_func_1_first_derivative, T, all_float_types)
auto grad_f_analytical = grad_test_func_1_analytical(x, y);
/* intended use case */
f_ad.backward();
BOOST_REQUIRE_CLOSE_FRACTION(x_ad.adjoint(), grad_f_analytical[0], boost_close_tol<T>());
BOOST_REQUIRE_CLOSE_FRACTION(y_ad.adjoint(), grad_f_analytical[1], boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(x_ad.adjoint(), grad_f_analytical[0], boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(y_ad.adjoint(), grad_f_analytical[1], boost_close_tol<T>());
gradient_tape<T, 1>& tape = get_active_tape<T, 1>();
tape.zero_grad();
@@ -923,12 +923,8 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_func_1_first_derivative, T, all_float_types)
auto grad_nd_func_test_grad = grad_nd<1>(f_ad, &x_ad, &y_ad);
for (int i = 0; i < 2; i++) {
BOOST_REQUIRE_CLOSE_FRACTION(grad_func_test_grad[i],
grad_f_analytical[i],
boost_close_tol<T>());
BOOST_REQUIRE_CLOSE_FRACTION(grad_nd_func_test_grad[i],
grad_f_analytical[i],
boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(grad_func_test_grad[i], grad_f_analytical[i], boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(grad_nd_func_test_grad[i], grad_f_analytical[i], boost_close_tol<T>());
}
}
BOOST_AUTO_TEST_CASE_TEMPLATE(test_func_1_second_derivative_and_hessian, T, all_float_types)
@@ -951,12 +947,8 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_func_1_second_derivative_and_hessian, T, all_
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
BOOST_REQUIRE_CLOSE_FRACTION(hess_func_test[i][j],
hess_analytical[i][j],
boost_close_tol<T>());
BOOST_REQUIRE_CLOSE_FRACTION(hess_func_test[i][j],
grad_nd_func_test[i][j],
boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(hess_func_test[i][j], hess_analytical[i][j], boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(hess_func_test[i][j], grad_nd_func_test[i][j], boost_close_tol<T>());
}
}
}
@@ -987,12 +979,8 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_func_1_order_3_der, T, all_float_types)
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
BOOST_REQUIRE_CLOSE_FRACTION(grad_tensor[i][j][k],
df3[i][j][k],
boost_close_tol<T>());
BOOST_REQUIRE_CLOSE_FRACTION(grad_nd_func_test[i][j][k],
df3[i][j][k],
boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(grad_tensor[i][j][k], df3[i][j][k], boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(grad_nd_func_test[i][j][k], df3[i][j][k], boost_close_tol<T>());
}
}
}
@@ -1029,12 +1017,12 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_func_1_fourth_derivative, T, all_float_types)
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
for (int l = 0; l < 2; l++) {
BOOST_REQUIRE_CLOSE_FRACTION(ggggf[i][j][k][l],
df4[i][j][k][l],
1 * boost_close_tol<T>());
BOOST_REQUIRE_CLOSE_FRACTION(grad_nd_func_test[i][j][k][l],
df4[i][j][k][l],
1 * boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(ggggf[i][j][k][l],
df4[i][j][k][l],
1 * boost_close_tol<T>());
BOOST_REQUIRE_CLOSE(grad_nd_func_test[i][j][k][l],
df4[i][j][k][l],
1 * boost_close_tol<T>());
}
}
}