[Boost-users] boost::lambda and math functions

0 views
Skip to first unread message

Peter Foelsche

unread,
Nov 23, 2009, 8:46:37 PM11/23/09
to boost...@lists.boost.org
I've the following template function:


template<typename T>
T exp2(const T T0_d)
{ return exp(0.5*T0_d) + 2*exp(T0_d);
}


I want to define this as a lambda function in place (but don't know how).

It will be used with T=double and T=MyAutomaticDerivativeClass.
I've already defined the return type of arithmetic operators applied to
MyAutomaticDerivativeClass:


namespace boost {
namespace lambda {


template<class Act, typename X, typename Y>
struct plain_return_type_2<arithmetic_action<Act>, X, Y>
{ typedef ... type;
};


}
}

What else is missing?

Thanks
Peter


_______________________________________________
Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost


_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Roman Perepelitsa

unread,
Nov 24, 2009, 5:33:18 AM11/24/09
to boost...@lists.boost.org
2009/11/24 Peter Foelsche <peter_f...@agilent.com>

I've the following template function:


template<typename T>
T exp2(const T T0_d)
{ return exp(0.5*T0_d) + 2*exp(T0_d);
}


I want to define this as a lambda function in place (but don't know how).

It will be used with T=double and T=MyAutomaticDerivativeClass.

#include <cmath>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

int main() {
  using namespace boost::lambda;
  double res = (bind(exp, 0.5 * _1) + 2 * bind(exp, _1))(0.5);
}

If exp is overloaded (and I guess it is, because you are going to pass both double and MyAutomaticDerivativeClass as arguments to exp), you'll need to wrap it in a polymorphic function object.

struct Exp {
  template <class Args> 
  struct sig : boost::tuples::element<1, Args> {};

  template <class T>
  T operator()(T arg) const {
    return exp(arg);
  }
};

int main() {
  using namespace boost::lambda;
  double res = (bind(Exp(), 0.5 * _1) + 2 * bind(Exp(), _1))(0.5);
}

By the way, if you have a choice, I recommend using phoenix instead of lambda. It can do everything lambda can do and even more.

Roman Perepelitsa. 
Reply all
Reply to author
Forward
0 new messages