
AMDG Dongfei Yin wrote:
Recently, I am working on some mathematical issues, most of my working time, I am calculating the derivative of function manually, it is such a tedious work that I write a piece of code to do it for me. now I want to share it with others. Let me show it for you. it it something like lambda expression, I mean boost::lambda. but the main purpose is to calculate derivative. As lambda do, I defined some variables like _x, _y in advance. A function named "d" can calculate derivative. then we can use it like this. d(_x * _x , _x)(1.0); // f(x)=x*x, f '(x) = 2x. so the result will be f '(1.0) = 2.0. d(d(_x * _x, _x), _x)(4.0); // f(x)=x*x, f ''(x) = 2, so the result will be f ''(4.0) = 2.0; this argument is just used to determine the return type. of cause it can work with more than one variables, such as, d(_x * _y, _x)(2.0, 1.0); // f(x,y)=x*y, f 'x(x,y)=y, so the result will be f 'x(x,y)=1.0; // the first argument will be ignored. d(d(_x * _y, _x), _y)(1.0); // f(x,y)=x*y, f ''xy(x,y)=0, so here one or two argument are all OK. and it can calculate derivative for some complicated expressions. theoretically, it can calculate high derivative for a expression as high as you want until the expression become zero. I want to share it with others, but it is not so perfect now. I need some suggestion to make it better.
Take a look at Proto. http://www.boost.org/libs/proto In Christ, Steven Watanabe