From fd4c013944836994067190866c675eecdde03901 Mon Sep 17 00:00:00 2001 From: Daniel <43151183+dannys4@users.noreply.github.com> Date: Mon, 17 Jun 2024 12:03:15 -0400 Subject: [PATCH] Fix tests (#424) --- tests/Test_InnerMarginalAffineMap.cpp | 23 +++++------- tests/Test_UnivariateExpansion.cpp | 54 ++++++++++++++++----------- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/tests/Test_InnerMarginalAffineMap.cpp b/tests/Test_InnerMarginalAffineMap.cpp index a41b504f..0c59c2be 100644 --- a/tests/Test_InnerMarginalAffineMap.cpp +++ b/tests/Test_InnerMarginalAffineMap.cpp @@ -152,23 +152,18 @@ TEST_CASE( "Test InnerMarginalAffineMap", "[InnerMarginalAffineMap]") { } // LogDeterminantCoeffGrad test + logdet = map->LogDeterminant(pts); StridedMatrix logdet_coeff_grad = map->LogDeterminantCoeffGrad(pts); - Kokkos::View exp_logdet_coeff_grad_map("exp_logdet_coeff_grad_map", trimap->numCoeffs, numPts); - for(int k = 0; k < trimap->numCoeffs; k++) { + Kokkos::View logdet_out_fd("memory for logdet", numPts); + for(int k = 0; k < map->numCoeffs; k++) { + map->Coeffs()(k) += fd_step; + Kokkos::deep_copy(logdet_out_fd, 0.); + map->LogDeterminantImpl(pts, logdet_out_fd); for(int j = 0; j < numPts; j++) { - trimap->Coeffs()(k) += fd_step; - StridedVector exp_logdet_map_fd = trimap->LogDeterminant(appliedPts); - for(int j = 0; j < numPts; j++) { - exp_logdet_coeff_grad_map(k,j) = (exp_logdet_map_fd(j) - exp_logdet_map(j))/fd_step; - } - trimap->Coeffs()(k) -= fd_step; - } - } - - for(unsigned int k=0; knumCoeffs; ++k){ - for(unsigned int j=0; jCoeffs()(k) -= fd_step; } Kokkos::View sens("sens", outputDim, numPts); diff --git a/tests/Test_UnivariateExpansion.cpp b/tests/Test_UnivariateExpansion.cpp index f57d96a6..e395e3fb 100644 --- a/tests/Test_UnivariateExpansion.cpp +++ b/tests/Test_UnivariateExpansion.cpp @@ -119,7 +119,7 @@ TEST_CASE("UnivariateExpansion") { } } -TEST_CASE("UnivariateExpansion Inverse") { +TEST_CASE("UnivariateExpansion: Sigmoid", "[SigmoidUnivariateExpansion]") { // Set up Sigmoid basis using Basis_T = Sigmoid1d; unsigned int num_sigmoid = 3; @@ -142,26 +142,38 @@ TEST_CASE("UnivariateExpansion Inverse") { UnivariateExpansion expansion(maxOrder, basis); Kokkos::deep_copy(coeffs, 1.0); expansion.WrapCoeffs(coeffs); - unsigned int numPts = 100; - Kokkos::View points("points", 1, numPts); - double grid = double(numPts)/2; - for(int i = 0; i < numPts; i++) { - points(0, i) = (bound+0.05)*double(i-grid)/double(grid); + SECTION("Inverse") { + unsigned int numPts = 100; + Kokkos::View points("points", 1, numPts); + double grid = double(numPts)/2; + for(int i = 0; i < numPts; i++) { + points(0, i) = (bound+0.05)*double(i-grid)/double(grid); + } + StridedMatrix eval = expansion.Evaluate(points); + Kokkos::View prefix ("inverse prefix", 0, numPts); + StridedMatrix inv = expansion.Inverse(prefix, eval); + REQUIRE(inv.extent(0) == 1); + REQUIRE(inv.extent(1) == numPts); + double tol = 5e-6; // Comes from rootfinding + for(int i = 0; i < numPts; i++) { + if(std::abs(points(0,i)) > tol*1e-2) CHECK_THAT(inv(0, i), WithinRel(points(0, i), tol)); + else CHECK_THAT(inv(0, i), WithinAbs(points(0, i), 1e-10)); + } } - StridedMatrix eval = expansion.Evaluate(points); - Kokkos::View prefix ("inverse prefix", 0, numPts); - StridedMatrix inv = expansion.Inverse(prefix, eval); - REQUIRE(inv.extent(0) == 1); - REQUIRE(inv.extent(1) == numPts); - double tol = 5e-6; // Comes from rootfinding - for(int i = 0; i < numPts; i++) { - if(std::abs(points(0,i)) > tol*1e-2) CHECK_THAT(inv(0, i), WithinRel(points(0, i), tol)); - else CHECK_THAT(inv(0, i), WithinAbs(points(0, i), 1e-10)); + SECTION("CoeffBounds") { + Kokkos::View lb,ub; + std::tie(lb,ub) = expansion.CoeffBounds(); + REQUIRE(lb.size()==expansion.numCoeffs); + REQUIRE(ub.size()==expansion.numCoeffs); + + // Constant can be anything + CHECK(lb(0) == -std::numeric_limits::infinity()); + CHECK(ub(0) == std::numeric_limits::infinity()); + + // All other coefficients must be positive + for(int i = 1; i < expansion.numCoeffs; i++) { + CHECK(lb(i) == 0.); + CHECK(ub(i) == std::numeric_limits::infinity()); + } } - Kokkos::View lb,ub; - std::tie(lb,ub) = expansion.CoeffBounds(); - REQUIRE(lb.size()==expansion.numCoeffs); - REQUIRE(ub.size()==expansion.numCoeffs); - CHECK(lb(0)==0.0); - CHECK(ub(0)==std::numeric_limits::infinity()); } \ No newline at end of file