From 066abc46597db86bc80d774704264c9372b1bfd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me?= Date: Mon, 25 Mar 2019 21:09:11 +0100 Subject: [PATCH] fixed test and folder structures --- .gitignore | 3 +++ AutomaticDifferentiation.hpp | 6 ++++- AutomaticDifferentiationVector.hpp | 9 ++++++- Makefile | 19 ++++++------- test_AutomaticDifferentiation_vector.cpp | 34 ++++++++++++++++-------- test_vector_version_additions_manual.cpp | 4 +-- 6 files changed, 51 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index 259148f..175b764 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# executables +run + # Prerequisites *.d diff --git a/AutomaticDifferentiation.hpp b/AutomaticDifferentiation.hpp index a8af999..62afc19 100644 --- a/AutomaticDifferentiation.hpp +++ b/AutomaticDifferentiation.hpp @@ -186,6 +186,10 @@ template Scalar heaviside(const Scalar & x) { return Scalar(x >= Scalar(0.)); } +template Scalar abs(const Scalar & x) { + return (x >= Scalar(0.)) ? x : -x; +} + // Basic mathematical functions for Dual numbers // f(a + b*d) = f(a) + b*f'(a)*d @@ -328,7 +332,7 @@ template Dual sign(const Dual & x) { } template Dual abs(const Dual & x) { - return Dual(fabs(x.a), x.b*sign(x.a)); + return Dual(abs(x.a), x.b*sign(x.a)); } template Dual fabs(const Dual & x) { diff --git a/AutomaticDifferentiationVector.hpp b/AutomaticDifferentiationVector.hpp index 40be24f..c94dfb4 100644 --- a/AutomaticDifferentiationVector.hpp +++ b/AutomaticDifferentiationVector.hpp @@ -39,7 +39,7 @@ class DualVector DualVector(const Scalar & _a, const Scalar & _b) : a(_a) { - b.fill(_b); + b = VectorT(_b, N); } /// Use this function to set what variable is to be derived : x + DualVector::d(i) @@ -193,6 +193,10 @@ template Scalar heaviside(const Scalar & x) { return Scalar(x >= Scalar(0.)); } +template Scalar abs(const Scalar & x) { + return (x >= Scalar(0.)) ? x : -x; +} + // Basic mathematical functions for DualVector numbers // f(a + b*d) = f(a) + b*f'(a)*d @@ -335,6 +339,9 @@ template DualVector sign(const DualVector DualVector abs(const DualVector & x) { + return DualVector(abs(x.a), x.b*sign(x.a)); +} +template DualVector fabs(const DualVector & x) { return DualVector(fabs(x.a), x.b*sign(x.a)); } diff --git a/Makefile b/Makefile index 9558a74..c504ed9 100644 --- a/Makefile +++ b/Makefile @@ -1,31 +1,32 @@ -CXX = g++ +CXX = clang++ INCLUDE = -I../Catch2-master/single_include CFLAGS = -Wall -std=c++11 -O0 -fprofile-arcs -ftest-coverage $(INCLUDE) LDFLAGS = -lgcov LDFLAGS_ALL = $(LDFLAGS) OUTPUT_NAME = test_AutomaticDifferentiation +OBJ = obj/ # scalar version -test_AutomaticDifferentiation:test_AutomaticDifferentiation.o test_AutomaticDifferentiation_main.o +test_AutomaticDifferentiation: $(OBJ)/test_AutomaticDifferentiation.o $(OBJ)/test_AutomaticDifferentiation_main.o $(CXX) -o $(OUTPUT_NAME) test_AutomaticDifferentiation.o test_AutomaticDifferentiation_main.o $(LDFLAGS_ALL) -test_AutomaticDifferentiation_manual:test_AutomaticDifferentiation_manual.o +test_AutomaticDifferentiation_manual: $(OBJ)/test_AutomaticDifferentiation_manual.o $(CXX) -o $(OUTPUT_NAME) test_AutomaticDifferentiation_manual.o $(LDFLAGS_ALL) -test_AutomaticDifferentiation_main.o: test_AutomaticDifferentiation_main.cpp +$(OBJ)/test_AutomaticDifferentiation_main.o: test_AutomaticDifferentiation_main.cpp $(CXX) $(CFLAGS) $(LDFLAGS) -c test_AutomaticDifferentiation_main.cpp -test_AutomaticDifferentiation.o: test_AutomaticDifferentiation.cpp +$(OBJ)/test_AutomaticDifferentiation.o: test_AutomaticDifferentiation.cpp $(CXX) $(CFLAGS) $(LDFLAGS) -c test_AutomaticDifferentiation.cpp -test_AutomaticDifferentiation_manual.o: test_AutomaticDifferentiation_manual.cpp +$(OBJ)/test_AutomaticDifferentiation_manual.o: test_AutomaticDifferentiation_manual.cpp $(CXX) $(CFLAGS) $(LDFLAGS) -c test_AutomaticDifferentiation_manual.cpp # Vector version -test_AutomaticDifferentiation_vector:test_AutomaticDifferentiation_vector.o - $(CXX) -o test_AutomaticDifferentiation_vector.exe test_AutomaticDifferentiation_vector.o $(LDFLAGS_ALL) +test_AutomaticDifferentiation_vector: $(OBJ)/test_AutomaticDifferentiation_vector.o + $(CXX) -o test_AutomaticDifferentiation_vector test_AutomaticDifferentiation_vector.o $(LDFLAGS_ALL) -test_AutomaticDifferentiation_vector.o: test_AutomaticDifferentiation_vector.cpp +$(OBJ)/test_AutomaticDifferentiation_vector.o: test_AutomaticDifferentiation_vector.cpp $(CXX) $(CFLAGS) $(LDFLAGS) -c test_AutomaticDifferentiation_vector.cpp clean: diff --git a/test_AutomaticDifferentiation_vector.cpp b/test_AutomaticDifferentiation_vector.cpp index e967687..f39dcae 100644 --- a/test_AutomaticDifferentiation_vector.cpp +++ b/test_AutomaticDifferentiation_vector.cpp @@ -6,16 +6,17 @@ using std::cout; using std::cout; using std::setw; +using std::abs; #define PRINT_VAR(x) std::cout << #x << "\t= " << std::setprecision(16) << (x) << std::endl -#define PRINT_DUALVECTOR(x) std::cout << #x << "\t= " << std::fixed << std::setprecision(4) << std::setw(10) << (x).a << ", " << std::setw(10) << (x).b.transpose() << std::endl +#define PRINT_DUALVECTOR(x) std::cout << #x << "\t= " << std::fixed << std::setprecision(4) << std::setw(10) << (x).a << ", " << std::setw(10) << (x).b << std::endl #define TEST_EQ_TOL 1e-9 // #define TEST_FUNCTION_ON_DualVector_DOUBLE(fct, x) assert(fct(DualVector(x)).a == fct(x)) // #define TEST_FUNCTION_ON_DualVector_DOUBLE(fct, x) assert(abs((fct(DualVector((x))).a) - (fct((x)))) <= TEST_EQ_TOL) -#define TEST_EQ_DOUBLE(a, b) assert(abs((a) - (b)) <= TEST_EQ_TOL) +#define TEST_EQ_DOUBLE(a, b) assert(std::abs((a) - (b)) <= TEST_EQ_TOL) #define TEST_FUNCTION_ON_DualVector_DOUBLE(fct, x) \ - if(abs((fct(DualVector((x))).a) - fct((x))) > TEST_EQ_TOL) { \ + if(fabs((fct(DualVector((x))).a) - fct((x))) > TEST_EQ_TOL) { \ std::cerr << "Assertion failed at " << __FILE__ << ":" << __LINE__ << " : " << #fct << ">(" << (x) << ") != " << #fct << "(" << (x) << ")" << "\n";\ std::cerr << "Got " << (fct(DualVector(x)).a) << " ; expected " << (fct((x))) << "\n";\ exit(1);\ @@ -106,15 +107,19 @@ void test_basic() using D = DualVector; cout.precision(16); + DualVector::VectorT zeroVec(0., 3); + double x = 2; double y = 5; D X(x), Y(y); - + assert(X.a == x); - assert(X.b == D::VectorT::Zero()); + for(size_t i = 0 ; i < 3 ; i++) + assert(X.b[i] == 0.); assert(Y.a == y); - assert(Y.b == D::VectorT::Zero()); + for(size_t i = 0 ; i < 3 ; i++) + assert(Y.b[i] == 0.); assert((X+Y).a == (x+y)); assert((X-Y).a == (x-y)); assert((X*Y).a == (x*y)); @@ -278,19 +283,26 @@ void test_scalar_functions() TEST_EQ_DOUBLE(acsch(1.62), 0.5835891509960214); } -#define TEST_DERIVATIVE_NUM(fct, x, DX_NUM_DIFF, tol); \ +#define TEST_DERIVATIVE_NUM(fct, x, DX_NUM_DIFF, tol) \ {\ double dfdx = fct((DualVector((x))+DualVector::d())).b[0];\ - double dfdx_num = ((fct((x)+DX_NUM_DIFF)) - (fct((x)-DX_NUM_DIFF)))/(2*DX_NUM_DIFF);\ - bool ok = (abs(dfdx - dfdx_num) <= tol) ? true : false;\ + double dfdx_num = double(-1.)/double(60.) * fct(x-3*dx)\ + + double( 3.)/double(20.) * fct(x-2*dx)\ + + double(-3.)/double(4. ) * fct(x-1*dx)\ + + double( 3.)/double(4. ) * fct(x+1*dx)\ + + double(-3.)/double(20.) * fct(x+2*dx)\ + + double( 1.)/double(60.) * fct(x+3*dx);\ + dfdx_num /= dx;\ + bool ok = (std::abs(dfdx - dfdx_num) <= tol) ? true : false;\ cout << setw(10) << #fct << "(" << (x) << ") : " << setw(25) << dfdx << " " << setw(25) << dfdx_num << " " << setw(25) << (dfdx-dfdx_num) << "\t" << ok << "\n";\ + assert(ok);\ } void test_derivative_all() { cout << "\ntest_derivative_all()\n"; // test all derivatives numerically and check that they add up - double x = 0.5, x2 = 1.5, x3 = -x, dx = 1e-6, tol = 1e-9; + double x = 0.5, x2 = 1.5, x3 = -x, dx = 1e-3, tol = 1e-9; TEST_DERIVATIVE_NUM(cos, x, dx, tol); TEST_DERIVATIVE_NUM(sin, x, dx, tol); TEST_DERIVATIVE_NUM(tan, x, dx, tol); @@ -426,7 +438,7 @@ void test_derivative_simple_3() D L = f3(x, y, z); PRINT_DUALVECTOR(L); - PRINT_VAR(f3(x+D::d(0), y+D::d(1), z+D::d(2)).b.transpose()); + PRINT_VAR(f3(x+D::d(0), y+D::d(1), z+D::d(2)).b); PRINT_VAR(df3x(x.a, y.a, z.a)); PRINT_VAR(df3y(x.a, y.a, z.a)); diff --git a/test_vector_version_additions_manual.cpp b/test_vector_version_additions_manual.cpp index 3c27c61..f0c25e8 100644 --- a/test_vector_version_additions_manual.cpp +++ b/test_vector_version_additions_manual.cpp @@ -59,7 +59,7 @@ int main() x.diff(0); PRINT_DUAL(x); - auto y = f1(x); + DualVector y = f1(x); PRINT_VAR(x); PRINT_DUAL(y); @@ -75,7 +75,7 @@ int main() x.diff(0); y.diff(1); z.diff(2); - auto res = f3(x, y, z); + DualVector res = f3(x, y, z); PRINT_DUAL(res); PRINT_VAR(res.b[0]);