[math] Does any one interesting in calculating derivative for a expression automatically?

Hi, 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. by the way, I am a new face, and English is not my native language, so if I did not explain some thing clearly, please do ask me. I will do my best to clarify.

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

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
Good idea, and this is all very neat, but I see a couple of issues: 1) Compile times for a compile time derivative is likely to be slow? 2) Isn't it easier to just calculate the derivative within a symbolic math package and then cut and paste the result into your code (Yacas is free for example)? 3) What happens for very complex expressions? Can the compile time code simplify the resulting derivative so that it is optimally evaluated? Most symbolic math packages will have a "simplify" function that does just that, without it, code might be very inefficient to evaluate, with it, compile times might be way too long :-( Regards, John.

Hi, Funnily, I was thinking about writing something like that those last days, to implement some dimension-agnostic linear algebra expressions with dimension known at compile-time. Obviously it wouldn't only involve derivatives, but what you show is quite close to what I had in mind in terms of syntax. So I'm interested :-) BTW Steven's right: proto is definitely the right tool to do that. On Tue, Mar 3, 2009 at 7:41 PM, John Maddock <john@johnmaddock.co.uk> wrote:
2) Isn't it easier to just calculate the derivative within a symbolic math package and then cut and paste the result into your code (Yacas is free for example)?
From my point of view, the main interest would be to compute expressions containing some compile-time values provided by the user. For example I want to calculate the volume of a hypersphere which is: (pi^(N/2)*r^N) / gamma(N/2 + 1), with dimension N known at compile-time. If the user happens to work only with N=2, that is circles, I want the resulting expression to be as effective as a hand-written pi*r^2 (the volume of a hypersphere<2> is the area of a circle). It's technically feasible.
Bruno

Dongfei Yin <yin.dongfei <at> gmail.com> writes:
Hi, 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 it is not so perfect now. I need some suggestion to make it better.
... HI Dongfei, I am interested in it. But what the plan is exactly ? How you are going to do it for say expressions involving trigonometric terms ? For polynomial expressions things are manageable, how you are going to parse other complex expressions ? BTW how you are going to take care of algebra at many places ? With Regards, Reetesh Mukul
participants (5)
-
Bruno Lalande
-
Dongfei Yin
-
John Maddock
-
Reetesh Mukul
-
Steven Watanabe