Skip to content

Commit

Permalink
add test for sin
Browse files Browse the repository at this point in the history
  • Loading branch information
TimSiebert1 committed Jan 4, 2025
1 parent 9ab0571 commit 6d37ee5
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 0 deletions.
95 changes: 95 additions & 0 deletions ADOL-C/boost-test/uni5_for/hov_forward.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,4 +998,99 @@ BOOST_AUTO_TEST_CASE(TanOperator_HOV_Forward) {
myfree3(X);
myfree3(Y);
}

BOOST_AUTO_TEST_CASE(SinOperator_HOV_Forward) {
const int16_t tag = 0;
const size_t dim_out = 1;
const size_t dim_in = 1;
const size_t degree = 3;
const size_t num_dirs = 2;
std::vector<double> in{0.5};
std::vector<adouble> indep(dim_in);
std::vector<double> out(dim_out);

// sin(x1^2)
trace_on(tag);
indep[0] <<= in[0];
adouble dep = sin(pow(indep[0], 2));
dep >>= out[0];
trace_off();

double ***X = myalloc3(dim_in, num_dirs, degree);
double ***Y = myalloc3(dim_out, num_dirs, degree);

X[0][0][0] = 1.0;
X[0][0][1] = -1.0;
X[0][0][2] = 1.1;

X[0][1][0] = 2.0;
X[0][1][1] = -2.1;
X[0][1][2] = -2.0;

std::vector<double> test_in{0.5};

// sin(x1^2)
double test_out = std::sin(std::pow(test_in[0], 2));

hov_forward(tag, dim_out, dim_in, degree, num_dirs, test_in.data(), X,
out.data(), Y);

BOOST_TEST(out[0] == test_out, tt::tolerance(tol));

// first derivative
BOOST_TEST(Y[0][0][0] == 2.0 * std::cos(std::pow(test_in[0], 2)) *
test_in[0] * X[0][0][0],
tt::tolerance(tol));
BOOST_TEST(Y[0][1][0] == 2.0 * std::cos(std::pow(test_in[0], 2)) *
test_in[0] * X[0][1][0],
tt::tolerance(tol));

// second derivative
BOOST_TEST(Y[0][0][1] == 2.0 * std::cos(std::pow(test_in[0], 2)) *
test_in[0] * X[0][0][1] +
1.0 / 2.0 *
(-4.0 * std::sin(std::pow(test_in[0], 2)) *
std::pow(test_in[0], 2) +
2.0 * std::cos(std::pow(test_in[0], 2))) *
X[0][0][0] * X[0][0][0],
tt::tolerance(tol));
BOOST_TEST(Y[0][1][1] == 2.0 * std::cos(std::pow(test_in[0], 2)) *
test_in[0] * X[0][1][1] +
1.0 / 2.0 *
(-4.0 * std::sin(std::pow(test_in[0], 2)) *
std::pow(test_in[0], 2) +
2.0 * std::cos(std::pow(test_in[0], 2))) *
X[0][1][0] * X[0][1][0],
tt::tolerance(tol));

// third derivative
BOOST_TEST(
Y[0][0][2] ==
2.0 * std::cos(std::pow(test_in[0], 2)) * test_in[0] * X[0][0][2] +
(-4.0 * std::sin(std::pow(test_in[0], 2)) *
std::pow(test_in[0], 2) +
2.0 * std::cos(std::pow(test_in[0], 2))) *
X[0][0][0] * X[0][0][1] +
1.0 / 6.0 *
(-12.0 * test_in[0] * std::sin(std::pow(test_in[0], 2)) -
8.0 * std::pow(test_in[0], 3) *
std::cos(std::pow(test_in[0], 2))) *
std::pow(X[0][0][0], 3),
tt::tolerance(tol));
BOOST_TEST(
Y[0][1][2] ==
2.0 * std::cos(std::pow(test_in[0], 2)) * test_in[0] * X[0][1][2] +
(-4.0 * std::sin(std::pow(test_in[0], 2)) *
std::pow(test_in[0], 2) +
2.0 * std::cos(std::pow(test_in[0], 2))) *
X[0][1][0] * X[0][1][1] +
1.0 / 6.0 *
(-12.0 * test_in[0] * std::sin(std::pow(test_in[0], 2)) -
8.0 * std::pow(test_in[0], 3) *
std::cos(std::pow(test_in[0], 2))) *
std::pow(X[0][1][0], 3),
tt::tolerance(tol));
myfree3(X);
myfree3(Y);
}
BOOST_AUTO_TEST_SUITE_END()
96 changes: 96 additions & 0 deletions ADOL-C/boost-test/uni5_for/hov_wk_forward.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1005,4 +1005,100 @@ BOOST_AUTO_TEST_CASE(TanOperator_HOV_Forward) {
myfree3(X);
myfree3(Y);
}

BOOST_AUTO_TEST_CASE(SinOperator_HOV_Forward) {
const int16_t tag = 0;
const size_t dim_out = 1;
const size_t dim_in = 1;
const size_t degree = 3;
const size_t num_dirs = 2;
const short keep = 1;
std::vector<double> in{0.5};
std::vector<adouble> indep(dim_in);
std::vector<double> out(dim_out);

// sin(x1^2)
trace_on(tag);
indep[0] <<= in[0];
adouble dep = sin(pow(indep[0], 2));
dep >>= out[0];
trace_off();

double ***X = myalloc3(dim_in, num_dirs, degree);
double ***Y = myalloc3(dim_out, num_dirs, degree);

X[0][0][0] = 1.0;
X[0][0][1] = -1.0;
X[0][0][2] = 1.1;

X[0][1][0] = 2.0;
X[0][1][1] = -2.1;
X[0][1][2] = -2.0;

std::vector<double> test_in{0.5};

// sin(x1^2)
double test_out = std::sin(std::pow(test_in[0], 2));

hov_wk_forward(tag, dim_out, dim_in, degree, keep, num_dirs, test_in.data(),
X, out.data(), Y);

BOOST_TEST(out[0] == test_out, tt::tolerance(tol));

// first derivative
BOOST_TEST(Y[0][0][0] == 2.0 * std::cos(std::pow(test_in[0], 2)) *
test_in[0] * X[0][0][0],
tt::tolerance(tol));
BOOST_TEST(Y[0][1][0] == 2.0 * std::cos(std::pow(test_in[0], 2)) *
test_in[0] * X[0][1][0],
tt::tolerance(tol));

// second derivative
BOOST_TEST(Y[0][0][1] == 2.0 * std::cos(std::pow(test_in[0], 2)) *
test_in[0] * X[0][0][1] +
1.0 / 2.0 *
(-4.0 * std::sin(std::pow(test_in[0], 2)) *
std::pow(test_in[0], 2) +
2.0 * std::cos(std::pow(test_in[0], 2))) *
X[0][0][0] * X[0][0][0],
tt::tolerance(tol));
BOOST_TEST(Y[0][1][1] == 2.0 * std::cos(std::pow(test_in[0], 2)) *
test_in[0] * X[0][1][1] +
1.0 / 2.0 *
(-4.0 * std::sin(std::pow(test_in[0], 2)) *
std::pow(test_in[0], 2) +
2.0 * std::cos(std::pow(test_in[0], 2))) *
X[0][1][0] * X[0][1][0],
tt::tolerance(tol));

// third derivative
BOOST_TEST(
Y[0][0][2] ==
2.0 * std::cos(std::pow(test_in[0], 2)) * test_in[0] * X[0][0][2] +
(-4.0 * std::sin(std::pow(test_in[0], 2)) *
std::pow(test_in[0], 2) +
2.0 * std::cos(std::pow(test_in[0], 2))) *
X[0][0][0] * X[0][0][1] +
1.0 / 6.0 *
(-12.0 * test_in[0] * std::sin(std::pow(test_in[0], 2)) -
8.0 * std::pow(test_in[0], 3) *
std::cos(std::pow(test_in[0], 2))) *
std::pow(X[0][0][0], 3),
tt::tolerance(tol));
BOOST_TEST(
Y[0][1][2] ==
2.0 * std::cos(std::pow(test_in[0], 2)) * test_in[0] * X[0][1][2] +
(-4.0 * std::sin(std::pow(test_in[0], 2)) *
std::pow(test_in[0], 2) +
2.0 * std::cos(std::pow(test_in[0], 2))) *
X[0][1][0] * X[0][1][1] +
1.0 / 6.0 *
(-12.0 * test_in[0] * std::sin(std::pow(test_in[0], 2)) -
8.0 * std::pow(test_in[0], 3) *
std::cos(std::pow(test_in[0], 2))) *
std::pow(X[0][1][0], 3),
tt::tolerance(tol));
myfree3(X);
myfree3(Y);
}
BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 6d37ee5

Please sign in to comment.