From 85fead620edb4dfc6e0ce4de4f3f69cb21eb7550 Mon Sep 17 00:00:00 2001 From: Daniel Sharp Date: Sun, 10 Sep 2023 14:16:55 -0400 Subject: [PATCH] Add plenty of rootfinding tests --- tests/Test_RootFinding.cpp | 59 ++++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/tests/Test_RootFinding.cpp b/tests/Test_RootFinding.cpp index 369a237a..5bfd4b0c 100644 --- a/tests/Test_RootFinding.cpp +++ b/tests/Test_RootFinding.cpp @@ -22,39 +22,74 @@ TEST_CASE( "RootFindingUtils", "[RootFindingUtils]") { } auto identity = [](double x){return x;}; auto sigmoid = [](double x){return 1./(std::exp(-x)+1);}; + auto sigmoid_combo = [sigmoid](double x){return sigmoid(2*(x-1)) + 3*sigmoid(x*0.5) + 0.5*sigmoid(1.5*(x+1));}; SECTION("FindBound lower linear") { - double xd = -1.; + double xd = -1.1; double yd = identity(xd); double xub = 2., yub = 2.; double xlb = 0., ylb = 0.; - FindBound(false, yd, identity, xlb, ylb, xub, yub, 10'000); + FindBound(false, yd, identity, xub, yub, xlb, ylb, 10'000); CheckFoundBounds(identity, xlb, xd, xub, ylb, yd, yub); } SECTION("FindBound upper linear") { - double xd = 1.; + double xd = 1.1; double yd = identity(xd); double xub = 0., yub = 0.; double xlb = -2., ylb = -2.; - FindBound(true, yd, identity, xub, yub, xlb, ylb, 10'000); + FindBound(true, yd, identity, xlb, ylb, xub, yub, 10'000); CheckFoundBounds(identity, xlb, xd, xub, ylb, yd, yub); } SECTION("FindBound lower sigmoid") { double xd = -0.5; double yd = sigmoid(xd); - double xub = 2., yub = 2.; - double xlb = 0., ylb = 0.; - FindBound(false, yd, sigmoid, xlb, ylb, xub, yub, 10'000); + double xub = 2., yub = sigmoid(xub); + double xlb = 0., ylb = sigmoid(xlb); + FindBound(false, yd, sigmoid, xub, yub, xlb, ylb, 10'000); CheckFoundBounds(sigmoid, xlb, xd, xub, ylb, yd, yub); } SECTION("FindBound upper sigmoid") { double xd = 0.5; double yd = sigmoid(xd); - double xub = 0., yub = 0.; - double xlb = -2., ylb = -2.; - FindBound(true, yd, sigmoid, xub, yub, xlb, ylb, 10'000); - std::cout << "ylb = " << ylb << ", f(xlb) = " << sigmoid(xlb) << std::endl; + double xub = 0., yub = sigmoid(xub); + double xlb = -2., ylb = sigmoid(xlb); + FindBound(true, yd, sigmoid, xlb, ylb, xub, yub, 10'000); CheckFoundBounds(sigmoid, xlb, xd, xub, ylb, yd, yub); } - + SECTION("Test Inverse Linear, low x0") { + double xd = 0.5, yd = identity(xd); + double x0 = 0.0, xtol = 1e-5, ftol = 1e-5; + double xd_found = InverseSingleBracket(yd, identity, x0, xtol, ftol); + CHECK( xd_found == Approx(xd).epsilon(2*xtol)); + } + SECTION("Test Inverse Linear, high x0") { + double xd = 0.5, yd = identity(xd); + double x0 = 1.0, xtol = 1e-5, ftol = 1e-5; + double xd_found = InverseSingleBracket(yd, identity, x0, xtol, ftol); + CHECK( xd_found == Approx(xd).epsilon(2*xtol)); + } + SECTION("Test Inverse Sigmoid, low x0") { + double xd = 0.5, yd = sigmoid(xd); + double x0 = 0.0, xtol = 1e-5, ftol = 1e-5; + double xd_found = InverseSingleBracket(yd, sigmoid, x0, xtol, ftol); + CHECK( xd_found == Approx(xd).epsilon(2*xtol)); + } + SECTION("Test Inverse Sigmoid, high x0") { + double xd = 0.5, yd = sigmoid(xd); + double x0 = 1.0, xtol = 1e-5, ftol = 1e-5; + double xd_found = InverseSingleBracket(yd, sigmoid, x0, xtol, ftol); + CHECK( xd_found == Approx(xd).epsilon(2*xtol)); + } + SECTION("Test Inverse Sigmoid Combo, low x0") { + double xd = 0.5, yd = sigmoid_combo(xd); + double x0 = -5.0, xtol = 1e-5, ftol = 1e-5; + double xd_found = InverseSingleBracket(yd, sigmoid_combo, x0, xtol, ftol); + CHECK( xd_found == Approx(xd).epsilon(2*xtol)); + } + SECTION("Test Inverse Sigmoid Combo, high x0") { + double xd = 0.5, yd = sigmoid_combo(xd); + double x0 = 5.0, xtol = 1e-5, ftol = 1e-5; + double xd_found = InverseSingleBracket(yd, sigmoid_combo, x0, xtol, ftol); + CHECK( xd_found == Approx(xd).epsilon(2*xtol)); + } } \ No newline at end of file