On Sun, Nov 7, 2010 at 2:11 PM, Larry Evans
On 11/07/10 13:07, Alfredo Correa wrote: [snip]
template
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
(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.
[snip] What about just creating a fusion::vector for tuple containing an initial value, then store that into a templated functor taking the free variable index as one of the template arguments, and then the operator()(double value) would use value to set the tuple at the template index and then call the function. For example:
Hi Larry,
I used your free_at.hpp to implement numerical derivative on named
parameters of a function object.
This post has the final solution that I was looking for:
1) First the "derivable" functor with named parameters, it has this general
shape:
struct p{};
struct q{};
struct H{
double operator()(double p, double q){
return p*p + q*q;
}
typedef result_of::make_map<
p , q ,
double, double
>::type mapped_arguments;
double operator()(mapped_arguments const& args){
return operator()(at_key<p>(args), at_key<q>(args));
}
};
The operator()( .. map ..) part is not very elegant. I don't know if it can
be improved (suggestions?).
2) Now, how to use the derivative with respect to ONE particular named
parameter, this is the notation I wanted to achieve. I think it is decent
but I accept suggestions. So, the usage:
H h;
double uno = 1., dos = 2.;
clog << h( uno, dos) << endl; // value at 1., 2., prints 5.
clog << d<q>(h)(uno, dos) << endl; // value of derivative at 1.,2. prints
4.
// more specific name should be
// partial<q>(h) where partial stands for partial derivative
Now the internal code that makes the magic in "d", that administers the
derivation parameter and ultimately takes the numerical derivative on that
function:
using namespace boost::fusion;
template<
class FusionMap, // e.g. fusion::map
struct bind_free_at : Functor, FusionMap{
bind_free_at(Functor const& f, FusionMap const& fm) : Functor(f),
FusionMap(fm){}
double operator()(typename result_of::value_at_key