Boost logo

Boost :

Subject: [boost] [math] Does any one interesting in calculating derivative for a expression automatically?
From: Dongfei Yin (yin.dongfei_at_[hidden])
Date: 2009-03-03 08:05:41

    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
    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.

Boost list run by bdawes at, gregod at, cpdaniel at, john at