From a5024f3b62949a9885d7d1b71b8dd189b1b149e5 Mon Sep 17 00:00:00 2001 From: Elias Pipping <elias.pipping@fu-berlin.de> Date: Mon, 10 Oct 2011 18:05:57 +0200 Subject: [PATCH] Move evaluate from octave to dune --- src/Makefile.am | 1 + src/duneevaluate.cc | 39 +++++++++++++++++++++++++++++++++++++++ src/duneevaluate.mk | 10 ++++++++++ src/foo.m | 8 ++------ 4 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 src/duneevaluate.cc create mode 100644 src/duneevaluate.mk diff --git a/src/Makefile.am b/src/Makefile.am index dabe8d36..c0c893ce 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -49,3 +49,4 @@ run-octave: duneevaluate.oct duneminimise.oct octave --path $(abs_builddir) --path $(abs_srcdir) include $(srcdir)/duneminimise.mk +include $(srcdir)/duneevaluate.mk diff --git a/src/duneevaluate.cc b/src/duneevaluate.cc new file mode 100644 index 00000000..c5583749 --- /dev/null +++ b/src/duneevaluate.cc @@ -0,0 +1,39 @@ +/* -*- mode:c++; mode:semantic -*- */ + +#include <octave/oct.h> + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "samplefunctional.hh" + +#include <cassert> + +DEFUN_DLD(duneevaluate, args, nargout, "the help string") { + assert(args.length() == 1); + ColumnVector point(args(0).matrix_value()); + // FIXME: We're only working with dimension two for now + assert(point.length() == 2); + + int const dim = 2; + typedef Dune::SampleFunctional<dim> SampleFunctional; + + SampleFunctional::SmallMatrix A; + A[0][0] = 3; + A[0][1] = 1.5; + A[1][0] = 1.5; + A[1][1] = 4; + SampleFunctional::SmallVector b; + b[0] = 1; + b[1] = 2; + + SampleFunctional::SmallVector our_point; + our_point[0] = point(0); + our_point[1] = point(1); + + Dune::SampleFunction f; + SampleFunctional J(A, b, Dune::MyNonlinearity<dim>(f)); + + return octave_value(J(our_point)); +} diff --git a/src/duneevaluate.mk b/src/duneevaluate.mk new file mode 100644 index 00000000..047d72a1 --- /dev/null +++ b/src/duneevaluate.mk @@ -0,0 +1,10 @@ +bin_PROGRAMS += duneminimise.oct + +# this would work if shared libraries were only passed via -L and -l, not directly +#duneminimise_LINK = libtool --tag=CXX --mode link $(MKOCTFILE) $(AM_LDFLAGS) -o $@ + +duneevaluate.oct: duneevaluate.o + $(MKOCTFILE) -o $@ $< -ldunecommon + +duneevaluate.o: duneevaluate.cc + $(MKOCTFILE) $(AM_CPPFLAGS) -c -o $@ $< diff --git a/src/foo.m b/src/foo.m index 1a696309..15e74ba6 100644 --- a/src/foo.m +++ b/src/foo.m @@ -8,15 +8,11 @@ y = (-300:10:300)'; v = ones(length(x),1); X = v * x; Y = y * v'; -A = [3 1.5; 1.5 4]; -b = [1; 2]; - -myfunc = @(x)(1/2 * (A * x)' * x - b' * x + H(norm(x))); for i=1:length(x) for j=1:length(y) vec = [ X(i,j); Y(i,j) ]; - val = myfunc(vec); + val = duneevaluate(vec); f(i,j) = val; end end @@ -30,7 +26,7 @@ for i=1:6 newvec=duneminimise(oldvec); line([oldvec(1) newvec(1)], ... [oldvec(2) newvec(2)], ... - [myfunc(oldvec) myfunc(newvec)], ... + [duneevaluate(oldvec) duneevaluate(newvec)], ... 'color', 'r'); norm(oldvec-newvec) oldvec=newvec; -- GitLab