From 2f1b9c5b29e9297a4a79d8b9b18c6155f24f7c71 Mon Sep 17 00:00:00 2001 From: Elias Pipping <elias.pipping@fu-berlin.de> Date: Sat, 10 Sep 2011 22:57:27 +0200 Subject: [PATCH] Make minimise() write to a reference --- src/bisection-example-new.cc | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/bisection-example-new.cc b/src/bisection-example-new.cc index b9f09477..606fde01 100644 --- a/src/bisection-example-new.cc +++ b/src/bisection-example-new.cc @@ -94,15 +94,15 @@ class SampleFunctional { } }; -// TODO: take a reference to a correction - template <class Functional> -typename Functional::SmallVector minimise( - const Functional J, const typename Functional::SmallVector x) { +void minimise(const Functional J, const typename Functional::SmallVector x, + typename Functional::SmallVector &corr) { typename Functional::SmallVector descDir = J.descentDirection(x); - if (descDir == typename Functional::SmallVector(0.0)) - return typename Functional::SmallVector(0.0); + if (descDir == typename Functional::SmallVector(0.0)) { + corr = typename Functional::SmallVector(0.0); + return; + } // {{{ Construct a restriction of J to the line x + t * descDir @@ -137,9 +137,8 @@ typename Functional::SmallVector minimise( << std::endl; ; - typename Functional::SmallVector middle = descDir; - middle *= m; - return middle; + corr = descDir; + corr *= m; } template <int dim, class Function> @@ -149,7 +148,7 @@ void functionTester( typename SampleFunctional<dim, Function>::SmallVector correction; std::cout << "Old value: J(...) = " << J(start) << std::endl; for (size_t i = 1; i <= runs; ++i) { - correction = minimise(J, start); + minimise(J, start, correction); start += correction; if (i != runs) std::cout << "New value: J(...) = " << J(start) << std::endl; -- GitLab