From 59bb3fb78c6278ef851b1283c1b8605c40e5fda8 Mon Sep 17 00:00:00 2001 From: Antonin Portelli Date: Tue, 7 Jul 2015 18:49:22 +0100 Subject: [PATCH] Functions: more binding utilities --- lib/Function.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++- lib/Function.hpp | 8 ++++++-- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/lib/Function.cpp b/lib/Function.cpp index 65a6660..a42b7c4 100644 --- a/lib/Function.cpp +++ b/lib/Function.cpp @@ -101,7 +101,8 @@ double DoubleFunction::operator()(void) const } // bind //////////////////////////////////////////////////////////////////////// -DoubleFunction DoubleFunction::bind(const Index argIndex, const double val) +DoubleFunction DoubleFunction::bind(const Index argIndex, + const double val) const { Index nArg = getNArg(); shared_ptr buf(new DVec(nArg)); @@ -133,6 +134,26 @@ DoubleFunction DoubleFunction::bind(const Index argIndex, const double val) return bindFunc; } +DoubleFunction DoubleFunction::bind(const Index argIndex, + const DVec &x) const +{ + Index nArg = getNArg(); + shared_ptr buf(new DVec(nArg)); + DoubleFunction copy(*this), bindFunc; + + auto func = [copy, buf, argIndex, x](const double *arg) + { + *buf = x; + (*buf)(argIndex) = arg[0]; + + return copy(*buf); + }; + + bindFunc.setFunction(func, 1); + + return bindFunc; +} + // arithmetic operators //////////////////////////////////////////////////////// DoubleFunction DoubleFunction::operator-(void) const { @@ -222,3 +243,29 @@ DSample DoubleFunctionSample::operator()(const vector &arg) const return (*this)(arg.data()); } +// bind //////////////////////////////////////////////////////////////////////// +DoubleFunctionSample DoubleFunctionSample::bind(const Index argIndex, + const double val) const +{ + DoubleFunctionSample bindFunc(size()); + + FOR_STAT_ARRAY(bindFunc, s) + { + bindFunc[s] = (*this)[s].bind(argIndex, val); + } + + return bindFunc; +} + +DoubleFunctionSample DoubleFunctionSample::bind(const Index argIndex, + const DVec &x) const +{ + DoubleFunctionSample bindFunc(size()); + + FOR_STAT_ARRAY(bindFunc, s) + { + bindFunc[s] = (*this)[s].bind(argIndex, x); + } + + return bindFunc; +} diff --git a/lib/Function.hpp b/lib/Function.hpp index dd1fc2f..18049f0 100644 --- a/lib/Function.hpp +++ b/lib/Function.hpp @@ -54,7 +54,8 @@ public: template double operator()(const double arg0, const Ts... args) const; // bind - DoubleFunction bind(const Index argIndex, const double val); + DoubleFunction bind(const Index argIndex, const double val) const; + DoubleFunction bind(const Index argIndex, const DVec &x) const; // arithmetic operators DoubleFunction operator-(void) const; DoubleFunction & operator+=(const DoubleFunction &f); @@ -170,13 +171,16 @@ public: DSample operator()(const std::vector &arg) const; template DSample operator()(const double arg0, const Ts... args) const; + // bind + DoubleFunctionSample bind(const Index argIndex, const double val) const; + DoubleFunctionSample bind(const Index argIndex, const DVec &x) const ; }; template DSample DoubleFunctionSample::operator()(const double arg0, const Ts... args) const { - const double arg[] = {arg0, args...}; + const double arg[] = {arg0, static_cast(args)...}; return (*this)(arg); }