Boost logo

Boost Users :

Subject: Re: [Boost-users] [fusion] named parameter technique with fusion::map
From: Alfredo Correa (alfredo.correa_at_[hidden])
Date: 2010-11-07 14:07:11


On Sat, Nov 6, 2010 at 2:19 PM, Dave Abrahams <dave_at_[hidden]> wrote:

> On Sat, Nov 6, 2010 at 5:19 AM, alfC <alfredo.correa_at_[hidden]> wrote:
> > (Note, named parameter boost library is not the proper way to go here
> > because the of the level of instrospection I need in the functions,
> > and because the use of named parameters is not to omit passing
> > parameters but to recognize 'externally' certain parameters).
>
> I would be *very* surprised if there was some kind of introspection
> possible that Boost.Parameter didn't allow. And I don't understand
> what you mean about omitting passing parameters or external
> recognition. I think you're reinventing the wheel; a Boost.Parameter
> ArgumentPack is essentially a fusion map.
>

ok, since I am probably missing the point, I am going to be one order of
magnitude more specific in defining what I want to achieve.
In the following example I will mix C++ and mathematical language to make
the point clear.

In "math", one can define a function of parameters and with some abuse of
notation one can use the names of parameters to define functionals (e.g.
derivatives), so for example

H(p,q) = p*p + q;

which can be evaluated, e.g. H(1.,2.) == 3. but one can also "invoke" the
derivatives of H,

dH/dq(1.,2.) == 4.

To be consistent with the abuse of notation one can also say H(p=1., q=2.)
or even H(q=2.,p=1), etc. It is this degree of notational flexibility what I
would like to achieve.

Let's switch to C++

double H(double p, double q){
  return p*p+q;
}

if I call H(1.,2.) I get of course the desired result. I would also like to
call something like
d<H, q>(1.,2.) which is the numerical derivative of H with respect to q
evaluated at p=1., q=2.

That is not possible with the naive definition of H because "q" means
nothing in the template parameter of "d".

So the next step is to inform C++ of the naming of the parameters, so I did:

struct p{};
struct q{};
double H(map<pair<p, double>, pair<q, double> > const& args){
       return at_key<p>(args)*at_key<p>(args) + at_key<q>(args);
      // same as return H(at_key<p>(args), at_key<q>(args));
 }

great, now the compiler has some idea of what the parameters are.
I can call this function as

H(make_map<p,q>(1.,2.));
H(make_set(1.,2.));
or even as H(1.,2.)

now the derivative problem:

suppose I already have a magical templated function, called 'derivative'
that returns the numerical derivative of a function or function object, when
this function has only one argument.

template<double(f)(double)>
double derivative(double const& x){ ...already defined... }

double f(double x){return x*x;}

used as
derivative<f>(1.);

the question is how to define a function "d" that takes a function like H
with fusion container arguments, bind it against all arguments except one
and calls derivative on that function. For example

d<H, q>(1.,2.);

internally, the code should bind all the parameters except q to its
corresponding values in the argument. in this case it is H binded with p=1.
and then *derivative* of H( __, 2.) is called with argument 1.

Christofere pointed example
libs/fusion/example/cookbook/do_the_bind.cpp

seems to be in the right direction but the code is hard to read, not to
mention that it doesn't compile.

Thank you,
Alfredo

> --
> Dave Abrahams
> BoostPro Computing
> http://www.boostpro.com
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
> --
> Sie haben diese Nachricht erhalten, da Sie der Google Groups-Gruppe Boost
> Users beigetreten sind.
> Wenn Sie Nachrichten in dieser Gruppe posten möchten, senden Sie eine
> E-Mail an boostusers_at_googlegroups.com.
> Wenn Sie aus dieser Gruppe austreten möchten, senden Sie eine E-Mail an
> boostusers+unsubscribe_at_[hidden]<boostusers%2Bunsubscribe_at_[hidden]>
> .
> Besuchen Sie die Gruppe unter
> http://groups.google.com/group/boostusers?hl=de, um weitere Optionen zu
> erhalten.
>
>



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net