fixed test and folder structures

This commit is contained in:
Jérôme 2019-03-25 21:09:11 +01:00
parent a60cac7f56
commit 066abc4659
6 changed files with 51 additions and 24 deletions

3
.gitignore vendored
View file

@ -1,3 +1,6 @@
# executables
run
# Prerequisites # Prerequisites
*.d *.d

View file

@ -186,6 +186,10 @@ template<typename Scalar> Scalar heaviside(const Scalar & x) {
return Scalar(x >= Scalar(0.)); return Scalar(x >= Scalar(0.));
} }
template<typename Scalar> Scalar abs(const Scalar & x) {
return (x >= Scalar(0.)) ? x : -x;
}
// Basic mathematical functions for Dual numbers // Basic mathematical functions for Dual numbers
// f(a + b*d) = f(a) + b*f'(a)*d // f(a + b*d) = f(a) + b*f'(a)*d
@ -328,7 +332,7 @@ template<typename Scalar> Dual<Scalar> sign(const Dual<Scalar> & x) {
} }
template<typename Scalar> Dual<Scalar> abs(const Dual<Scalar> & x) { template<typename Scalar> Dual<Scalar> abs(const Dual<Scalar> & x) {
return Dual<Scalar>(fabs(x.a), x.b*sign(x.a)); return Dual<Scalar>(abs(x.a), x.b*sign(x.a));
} }
template<typename Scalar> Dual<Scalar> fabs(const Dual<Scalar> & x) { template<typename Scalar> Dual<Scalar> fabs(const Dual<Scalar> & x) {

View file

@ -39,7 +39,7 @@ class DualVector
DualVector(const Scalar & _a, const Scalar & _b) DualVector(const Scalar & _a, const Scalar & _b)
: a(_a) : a(_a)
{ {
b.fill(_b); b = VectorT(_b, N);
} }
/// Use this function to set what variable is to be derived : x + DualVector::d(i) /// Use this function to set what variable is to be derived : x + DualVector::d(i)
@ -193,6 +193,10 @@ template<typename Scalar> Scalar heaviside(const Scalar & x) {
return Scalar(x >= Scalar(0.)); return Scalar(x >= Scalar(0.));
} }
template<typename Scalar> Scalar abs(const Scalar & x) {
return (x >= Scalar(0.)) ? x : -x;
}
// Basic mathematical functions for DualVector numbers // Basic mathematical functions for DualVector numbers
// f(a + b*d) = f(a) + b*f'(a)*d // f(a + b*d) = f(a) + b*f'(a)*d
@ -335,6 +339,9 @@ template<typename Scalar, int N> DualVector<Scalar, N> sign(const DualVector<Sca
} }
template<typename Scalar, int N> DualVector<Scalar, N> abs(const DualVector<Scalar, N> & x) { template<typename Scalar, int N> DualVector<Scalar, N> abs(const DualVector<Scalar, N> & x) {
return DualVector<Scalar, N>(abs(x.a), x.b*sign(x.a));
}
template<typename Scalar, int N> DualVector<Scalar, N> fabs(const DualVector<Scalar, N> & x) {
return DualVector<Scalar, N>(fabs(x.a), x.b*sign(x.a)); return DualVector<Scalar, N>(fabs(x.a), x.b*sign(x.a));
} }

View file

@ -1,31 +1,32 @@
CXX = g++ CXX = clang++
INCLUDE = -I../Catch2-master/single_include INCLUDE = -I../Catch2-master/single_include
CFLAGS = -Wall -std=c++11 -O0 -fprofile-arcs -ftest-coverage $(INCLUDE) CFLAGS = -Wall -std=c++11 -O0 -fprofile-arcs -ftest-coverage $(INCLUDE)
LDFLAGS = -lgcov LDFLAGS = -lgcov
LDFLAGS_ALL = $(LDFLAGS) LDFLAGS_ALL = $(LDFLAGS)
OUTPUT_NAME = test_AutomaticDifferentiation OUTPUT_NAME = test_AutomaticDifferentiation
OBJ = obj/
# scalar version # 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) $(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) $(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 $(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 $(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 $(CXX) $(CFLAGS) $(LDFLAGS) -c test_AutomaticDifferentiation_manual.cpp
# Vector version # Vector version
test_AutomaticDifferentiation_vector:test_AutomaticDifferentiation_vector.o test_AutomaticDifferentiation_vector: $(OBJ)/test_AutomaticDifferentiation_vector.o
$(CXX) -o test_AutomaticDifferentiation_vector.exe test_AutomaticDifferentiation_vector.o $(LDFLAGS_ALL) $(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 $(CXX) $(CFLAGS) $(LDFLAGS) -c test_AutomaticDifferentiation_vector.cpp
clean: clean:

View file

@ -6,16 +6,17 @@
using std::cout; using std::cout;
using std::cout; using std::cout;
using std::setw; using std::setw;
using std::abs;
#define PRINT_VAR(x) std::cout << #x << "\t= " << std::setprecision(16) << (x) << std::endl #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_EQ_TOL 1e-9
// #define TEST_FUNCTION_ON_DualVector_DOUBLE(fct, x) assert(fct(DualVector<double,1>(x)).a == fct(x)) // #define TEST_FUNCTION_ON_DualVector_DOUBLE(fct, x) assert(fct(DualVector<double,1>(x)).a == fct(x))
// #define TEST_FUNCTION_ON_DualVector_DOUBLE(fct, x) assert(abs((fct(DualVector<double,1>((x))).a) - (fct((x)))) <= TEST_EQ_TOL) // #define TEST_FUNCTION_ON_DualVector_DOUBLE(fct, x) assert(abs((fct(DualVector<double,1>((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) \ #define TEST_FUNCTION_ON_DualVector_DOUBLE(fct, x) \
if(abs((fct(DualVector<double,1>((x))).a) - fct((x))) > TEST_EQ_TOL) { \ if(fabs((fct(DualVector<double,1>((x))).a) - fct((x))) > TEST_EQ_TOL) { \
std::cerr << "Assertion failed at " << __FILE__ << ":" << __LINE__ << " : " << #fct << "<DualVector<double,1>>(" << (x) << ") != " << #fct << "(" << (x) << ")" << "\n";\ std::cerr << "Assertion failed at " << __FILE__ << ":" << __LINE__ << " : " << #fct << "<DualVector<double,1>>(" << (x) << ") != " << #fct << "(" << (x) << ")" << "\n";\
std::cerr << "Got " << (fct(DualVector<double,1>(x)).a) << " ; expected " << (fct((x))) << "\n";\ std::cerr << "Got " << (fct(DualVector<double,1>(x)).a) << " ; expected " << (fct((x))) << "\n";\
exit(1);\ exit(1);\
@ -106,15 +107,19 @@ void test_basic()
using D = DualVector<double, 3>; using D = DualVector<double, 3>;
cout.precision(16); cout.precision(16);
DualVector<double, 3>::VectorT zeroVec(0., 3);
double x = 2; double x = 2;
double y = 5; double y = 5;
D X(x), Y(y); D X(x), Y(y);
assert(X.a == x); 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.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)); 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); 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<double,1>((x))+DualVector<double,1>::d())).b[0];\ double dfdx = fct((DualVector<double,1>((x))+DualVector<double,1>::d())).b[0];\
double dfdx_num = ((fct((x)+DX_NUM_DIFF)) - (fct((x)-DX_NUM_DIFF)))/(2*DX_NUM_DIFF);\ double dfdx_num = double(-1.)/double(60.) * fct(x-3*dx)\
bool ok = (abs(dfdx - dfdx_num) <= tol) ? true : false;\ + 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";\ 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() void test_derivative_all()
{ {
cout << "\ntest_derivative_all()\n"; cout << "\ntest_derivative_all()\n";
// test all derivatives numerically and check that they add up // 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(cos, x, dx, tol);
TEST_DERIVATIVE_NUM(sin, x, dx, tol); TEST_DERIVATIVE_NUM(sin, x, dx, tol);
TEST_DERIVATIVE_NUM(tan, 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); D L = f3(x, y, z);
PRINT_DUALVECTOR(L); 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(df3x(x.a, y.a, z.a));
PRINT_VAR(df3y(x.a, y.a, z.a)); PRINT_VAR(df3y(x.a, y.a, z.a));

View file

@ -59,7 +59,7 @@ int main()
x.diff(0); x.diff(0);
PRINT_DUAL(x); PRINT_DUAL(x);
auto y = f1(x); DualVector<double, 1> y = f1(x);
PRINT_VAR(x); PRINT_VAR(x);
PRINT_DUAL(y); PRINT_DUAL(y);
@ -75,7 +75,7 @@ int main()
x.diff(0); x.diff(0);
y.diff(1); y.diff(1);
z.diff(2); z.diff(2);
auto res = f3(x, y, z); DualVector<double,3> res = f3(x, y, z);
PRINT_DUAL(res); PRINT_DUAL(res);
PRINT_VAR(res.b[0]); PRINT_VAR(res.b[0]);