From e17447d6cd059b4645e110f180eb78e24f0b1c66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A5rd=20Skaflestad?= Date: Tue, 30 Apr 2013 00:08:02 +0200 Subject: [PATCH] Add further test cases - Polynomials (including mixed mode arithmetic) - Cosines (including mixed mode arithmethic) - Square roots (including mixed mode arithmetic) --- test_syntax.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/test_syntax.cpp b/test_syntax.cpp index 3cc1b1e37..48f9a946d 100644 --- a/test_syntax.cpp +++ b/test_syntax.cpp @@ -6,6 +6,7 @@ #include "AutoDiff.hpp" +#include #include BOOST_AUTO_TEST_CASE(Initialisation) @@ -171,3 +172,60 @@ BOOST_AUTO_TEST_CASE(Division) BOOST_CHECK_CLOSE(otb.val(), 2 / b.val(), atol); BOOST_CHECK_CLOSE(otb.der(), -2*b.der() / (b.val() * b.val()), atol); } + + +BOOST_AUTO_TEST_CASE(Polynomial) +{ + typedef AutoDiff::Forward AdFW; + + const double atol = 1.0e-14; + + const AdFW x(1.234e-1); + + const AdFW p0 = x * x; + BOOST_CHECK_CLOSE(p0.val(), x.val() * x.val(), atol); + BOOST_CHECK_CLOSE(p0.der(), 2*x.val()*x.der(), atol); + + const AdFW p = 10*x*x - x/2.0 + 3.0; + BOOST_CHECK_CLOSE(p.val(), 10 *x.val()*x.val() - x.val()/2.0 + 3.0, atol); + BOOST_CHECK_CLOSE(p.der(), 10*2*x.val()*x.der() - x.der()/2.0 , atol); +} + + +BOOST_AUTO_TEST_CASE(Cosine) +{ + typedef AutoDiff::Forward AdFW; + + const double atol = 1.0e-14; + + const AdFW x(3.14159265358979323846264338327950288); + + const AdFW f = std::cos(x); + BOOST_CHECK_CLOSE(f.val(), std::cos(x.val()), atol); + BOOST_CHECK_CLOSE(f.der(), - std::sin(x.val()), atol); + + const AdFW p = 10*x*x - x/2.0 + 3.0; + const AdFW g = std::cos(p); + BOOST_CHECK_CLOSE(g.val(), std::cos(p.val()) , atol); + BOOST_CHECK_CLOSE(g.der(), - std::sin(p.val())*p.der(), atol); +} + + +BOOST_AUTO_TEST_CASE(SquareRoot) +{ + typedef AutoDiff::Forward AdFW; + + const double atol = 1.0e-14; + + const AdFW x(1.234e-5); + + const AdFW x2 = x * x; + const AdFW g = std::cos(x2) + x; + const AdFW f = std::sqrt(g) - 1.2; + + BOOST_CHECK_CLOSE(g.val(), std::cos(x2.val()) + x.val(), atol); + BOOST_CHECK_CLOSE(g.der(), -std::sin(x2.val())*x2.der() + x.der(), atol); + + BOOST_CHECK_CLOSE(f.val(), std::sqrt(g.val()) - 1.2, atol); + BOOST_CHECK_CLOSE(f.der(), 1.0/(2.0 * std::sqrt(g.val())) * g.der(), atol); +}