Derivatives and C++

Announcements, comments, ideas, feedback, and "How do I... ?" questions
Post Reply
User avatar
Alfredwolf
Posts: 3
Joined: Sun Dec 23, 2007 8:29 pm

Derivatives and C++

Post by Alfredwolf »

well recently I wanted to find some way to efficiently calculate the derivative in C++, now note I'm not all that great with the language began just a while back,
and if anyone could possibly give me some sort of hint as to do such a thing, and not just numerical derivative but the expression.
User avatar
arif
Posts: 111
Joined: Tue Oct 09, 2007 4:35 am
Location: Lahore, Pakistan

Re: Derivatives and C++

Post by arif »

If you don't want a numerical derivative, then you need to have a formula that can be differentiated. Unfortunately C++ doesn't really have a data type that can represent formula's, or manipulate them. Even then, writing a symbolic derivative program is often a common exercise given in CS courses to teach recursion. You simply have to program in every differentiation rule from a calculus book: addition, subtraction, multiplication, division, power, chain rule, trig functions, etc. You can either make some kind of a tree data type to contain formulas, or simply use strings, with a lot of parsing.

Languages like Maple, Mathematica and others have built-in symbolic derivatives.

Languages that can manipulate strings easily, eg. perl, are relatively easy to write such programs in.

On the other hand, there is a brilliant idea (search for "automatic differentiation" or "algorithmic differentiation"), where one can differentiate a subroutine (yes!) by using overloading, and special data types. Let me explain so that this doesn't sound like mumbo-jumbo:

For any variables which used to be defined as "float x,y,z", instead define them as a new data type eg: "varfloat x,y,z". The varfloat data type keeps track of two numbers x.value and x.derivative. When you say z=x*y, the multiplication operator is overloaded to compute the value and the derivative, so that what really happens is:

Code: Select all

z.value = x.value + y.value
z.derivative = x.value * y.derivative + y.value *x.derivative
which is really an implementation of the multiplication rule. Similar overloading of all operators makes it possible to compute an exact derivative simply by changing the data types of your variables.
The i-th root of i = 4.810477381
User avatar
daniel.is.fischer
Posts: 2400
Joined: Sun Sep 02, 2007 11:15 pm
Location: Bremen, Germany

Re: Derivatives and C++

Post by daniel.is.fischer »

It's also fairly easy to do that in functional languages. But of course, you have to encode the differentiation rules by hand and the special cases you need there, too.
Il faut respecter la montagne -- c'est pourquoi les gypaètes sont là.
User avatar
Alfredwolf
Posts: 3
Joined: Sun Dec 23, 2007 8:29 pm

Re: Derivatives and C++

Post by Alfredwolf »

thanks for that one arif, helped a lot.
Post Reply