From 6f54b856bd312a798e5d26784984d83ac7eb178d Mon Sep 17 00:00:00 2001 From: Elias Pipping <elias.pipping@fu-berlin.de> Date: Tue, 6 Sep 2011 17:12:38 +0200 Subject: [PATCH] Hide some functions --- src/bisection-example.cc | 60 +++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/src/bisection-example.cc b/src/bisection-example.cc index e268cf1d..1ae3c95e 100644 --- a/src/bisection-example.cc +++ b/src/bisection-example.cc @@ -45,35 +45,6 @@ template <int dimension> class SampleFunctional { return MinusGrad(x) * dir; } - SmallVector ModifiedGradient(const SmallVector x) const { - if (x == SmallVector(0.0)) - // TODO - DUNE_THROW(Dune::Exception, "The case x = 0 is not yet handled."); - - SmallVector const pg = PlusGrad(x); - SmallVector const mg = MinusGrad(x); - SmallVector ret; - // TODO: collinearity checks suck - if (pg * x == pg.two_norm() * x.two_norm() && - -(mg * x) == mg.two_norm() * x.two_norm()) { - return SmallVector(0); - } else if (pg * x >= 0 && mg * x >= 0) { - ret = pg; - } else if (pg * x <= 0 && mg * x <= 0) { - ret = mg; - } else { - ret = project(SmoothGrad(x), x); - } - ret *= -1; - return ret; - } - - SmallVector project(const SmallVector z, const SmallVector x) const { - SmallVector y = z; - y.axpy(-(z * x) / x.two_norm2(), x); - return y; - } - SmallVector minimise(const SmallVector x, unsigned int iterations) const { SmallVector descDir = ModifiedGradient(x); @@ -158,6 +129,35 @@ template <int dimension> class SampleFunctional { y.axpy(HPrimeMinus(x.two_norm()) / x.two_norm(), x); return y; } + + SmallVector ModifiedGradient(const SmallVector x) const { + if (x == SmallVector(0.0)) + // TODO + DUNE_THROW(Dune::Exception, "The case x = 0 is not yet handled."); + + SmallVector const pg = PlusGrad(x); + SmallVector const mg = MinusGrad(x); + SmallVector ret; + // TODO: collinearity checks suck + if (pg * x == pg.two_norm() * x.two_norm() && + -(mg * x) == mg.two_norm() * x.two_norm()) { + return SmallVector(0); + } else if (pg * x >= 0 && mg * x >= 0) { + ret = pg; + } else if (pg * x <= 0 && mg * x <= 0) { + ret = mg; + } else { + ret = project(SmoothGrad(x), x); + } + ret *= -1; + return ret; + } + + SmallVector project(const SmallVector z, const SmallVector x) const { + SmallVector y = z; + y.axpy(-(z * x) / x.two_norm2(), x); + return y; + } }; int main() { @@ -179,8 +179,6 @@ int main() { std::cout << J.directionalDerivative(b, b) << std::endl; assert(J.directionalDerivative(b, b) == 10 + 2 * sqrt(5)); - SampleFunctional::SmallVector descDir = J.ModifiedGradient(b); - SampleFunctional::SmallVector start = b; start *= 17; SampleFunctional::SmallVector correction = J.minimise(start, 20); -- GitLab