Skip to content

Commit

Permalink
Fix tests (#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
dannys4 authored Jun 17, 2024
1 parent ef9955b commit fd4c013
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
23 changes: 9 additions & 14 deletions tests/Test_InnerMarginalAffineMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,18 @@ TEST_CASE( "Test InnerMarginalAffineMap", "[InnerMarginalAffineMap]") {
}

// LogDeterminantCoeffGrad test
logdet = map->LogDeterminant(pts);
StridedMatrix<double, MemorySpace> logdet_coeff_grad = map->LogDeterminantCoeffGrad(pts);
Kokkos::View<double**, MemorySpace> exp_logdet_coeff_grad_map("exp_logdet_coeff_grad_map", trimap->numCoeffs, numPts);
for(int k = 0; k < trimap->numCoeffs; k++) {
Kokkos::View<double*, MemorySpace> 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<double,MemorySpace> 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; k<trimap->numCoeffs; ++k){
for(unsigned int j=0; j<numPts; ++j){
REQUIRE_THAT(logdet_coeff_grad(k,j), WithinRel(exp_logdet_coeff_grad_map(k,j), 20*fd_step));
double grad_fd_j = (logdet_out_fd(j) - logdet(j))/fd_step;
REQUIRE_THAT(logdet_coeff_grad(k,j), WithinRel(grad_fd_j, 20*fd_step));
}
map->Coeffs()(k) -= fd_step;
}

Kokkos::View<double**, MemorySpace> sens("sens", outputDim, numPts);
Expand Down
54 changes: 33 additions & 21 deletions tests/Test_UnivariateExpansion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ TEST_CASE("UnivariateExpansion") {
}
}

TEST_CASE("UnivariateExpansion Inverse") {
TEST_CASE("UnivariateExpansion: Sigmoid", "[SigmoidUnivariateExpansion]") {
// Set up Sigmoid basis
using Basis_T = Sigmoid1d<MemorySpace>;
unsigned int num_sigmoid = 3;
Expand All @@ -142,26 +142,38 @@ TEST_CASE("UnivariateExpansion Inverse") {
UnivariateExpansion<MemorySpace, Basis_T> expansion(maxOrder, basis);
Kokkos::deep_copy(coeffs, 1.0);
expansion.WrapCoeffs(coeffs);
unsigned int numPts = 100;
Kokkos::View<double**, MemorySpace> 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<double**, MemorySpace> 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<double, MemorySpace> eval = expansion.Evaluate(points);
Kokkos::View<double**, MemorySpace> prefix ("inverse prefix", 0, numPts);
StridedMatrix<double, MemorySpace> 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<double, MemorySpace> eval = expansion.Evaluate(points);
Kokkos::View<double**, MemorySpace> prefix ("inverse prefix", 0, numPts);
StridedMatrix<double, MemorySpace> 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<double*, Kokkos::HostSpace> 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<double>::infinity());
CHECK(ub(0) == std::numeric_limits<double>::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<double>::infinity());
}
}
Kokkos::View<double*, Kokkos::HostSpace> 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<double>::infinity());
}

0 comments on commit fd4c013

Please sign in to comment.