From 5164c4292912619c8017fc8da6e640331a93a126 Mon Sep 17 00:00:00 2001 From: Elias Pipping <elias.pipping@fu-berlin.de> Date: Sun, 29 Jul 2012 17:12:34 +0200 Subject: [PATCH] Add and use two_distance --- src/test-gradient-method-helper.hh | 9 +++++++++ src/test-gradient-sample-nonsmooth.cc | 24 +++++++++++------------- src/test-gradient-sample.cc | 13 ++++++------- src/test-gradient-trivial.cc | 21 ++++++++------------- 4 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/test-gradient-method-helper.hh b/src/test-gradient-method-helper.hh index 757336e5..aab0b4a8 100644 --- a/src/test-gradient-method-helper.hh +++ b/src/test-gradient-method-helper.hh @@ -24,4 +24,13 @@ double functionTester(Dune::SampleFunctional<dim> J, std::cout << boost::format("%8g -> %e") % original % final << std::endl; return final; } + +template <int dim> +double two_distance( + typename Dune::SampleFunctional<dim>::SmallVector const &x, + typename Dune::SampleFunctional<dim>::SmallVector const &y) { + typename Dune::SampleFunctional<dim>::SmallVector tmp = x; + tmp -= y; + return tmp.two_norm(); +} #endif diff --git a/src/test-gradient-sample-nonsmooth.cc b/src/test-gradient-sample-nonsmooth.cc index eb4517b7..b21e3a79 100644 --- a/src/test-gradient-sample-nonsmooth.cc +++ b/src/test-gradient-sample-nonsmooth.cc @@ -31,7 +31,7 @@ int main() { Functional J(A, b, phi); Functional::SmallVector start; - Functional::SmallVector error; + Functional::SmallVector analytic_descent; /* for x = b/|b|: @@ -50,12 +50,11 @@ int main() { start /= (b.two_norm() + 1e-12); assert(start.two_norm() < 1); - Functional::SmallVector returned; - J.descentDirection(start, returned); - error[0] = -(7 / sqrt(5) - 1); - error[1] = -(11.5 / sqrt(5) - 2); - error -= returned; - assert(error.two_norm() < 1e-10); + Functional::SmallVector numerical_descent; + J.descentDirection(start, numerical_descent); + analytic_descent[0] = -(7 / sqrt(5) - 1); + analytic_descent[1] = -(11.5 / sqrt(5) - 2); + assert(two_distance<dim>(analytic_descent, numerical_descent) < 1e-10); functionTester(J, start, 6); } @@ -64,12 +63,11 @@ int main() { start /= (b.two_norm() - 1e-12); // Make sure the norm is above 1; assert(start.two_norm() > 1); - Functional::SmallVector returned; - J.descentDirection(start, returned); - error[0] = -(8 / sqrt(5) - 1); - error[1] = -(13.5 / sqrt(5) - 2); - error -= returned; - assert(error.two_norm() < 1e-10); + Functional::SmallVector numerical_descent; + J.descentDirection(start, numerical_descent); + analytic_descent[0] = -(8 / sqrt(5) - 1); + analytic_descent[1] = -(13.5 / sqrt(5) - 2); + assert(two_distance<dim>(analytic_descent, numerical_descent) < 1e-10); functionTester(J, start, 6); } diff --git a/src/test-gradient-sample.cc b/src/test-gradient-sample.cc index 8d8d451d..43f42781 100644 --- a/src/test-gradient-sample.cc +++ b/src/test-gradient-sample.cc @@ -40,13 +40,12 @@ int main() { = 17*(6, 9.5) - (1, 2) + 2/sqrt(5) (1, 2) = (102 - 1 + 2/sqrt(5), 161.5 - 2 + 4/sqrt(5)) */ - Functional::SmallVector error; - error[0] = -(102 - 1 + 2 / sqrt(5)); - error[1] = -(161.5 - 2 + 4 / sqrt(5)); - Functional::SmallVector returned; - J.descentDirection(start, returned); - error -= returned; - assert(error.two_norm() < 1e-10); + Functional::SmallVector analytic_descent; + analytic_descent[0] = -(102 - 1 + 2 / sqrt(5)); + analytic_descent[1] = -(161.5 - 2 + 4 / sqrt(5)); + Functional::SmallVector numerical_descent; + J.descentDirection(start, numerical_descent); + assert(two_distance<dim>(analytic_descent, numerical_descent) < 1e-10); double const ret1 = functionTester(J, start, 6); diff --git a/src/test-gradient-trivial.cc b/src/test-gradient-trivial.cc index 895fdb5e..b14efe04 100644 --- a/src/test-gradient-trivial.cc +++ b/src/test-gradient-trivial.cc @@ -44,27 +44,22 @@ int main() { = 17*(6, 9.5) - (1, 2) = (102 - 1, 161.5 - 2) */ - Functional::SmallVector error; - error[0] = -101; - error[1] = -159.5; - Functional::SmallVector returned; - J.descentDirection(start, returned); - error -= returned; - assert(error.two_norm() < 1e-10); + Functional::SmallVector analytic_descent; + analytic_descent[0] = -101; + analytic_descent[1] = -159.5; + Functional::SmallVector numerical_descent; + J.descentDirection(start, numerical_descent); + assert(two_distance<dim>(numerical_descent, analytic_descent) < 1e-10); double const ret1 = functionTester(J, start, 6); - error = solution; - error -= start; - assert(error.two_norm() < 1e-5); + assert(two_distance<dim>(start, solution) < 1e-5); // Something random start[0] = 279; start[1] = -96; double const ret2 = functionTester(J, start, 25); - error = solution; - error -= start; - assert(error.two_norm() < 1e-6); + assert(two_distance<dim>(start, solution) < 1e-6); assert(std::abs(ret1 - ret2) < 1e-5); } -- GitLab