Page 1 of 2

calculus inside a programming language

Posted: Wed Jun 03, 2009 4:15 am
by redoktober
hey guys!
i've just started studying calculus. now, i'm intrigued by the idea, of writing a function in, say, c#, or any language for that matter, which could calculate the derivative of a given mathematical expression, say, x^2. of course, i would use the rule nx^n-1. but how owuld i go about writing a function in a programming language, to take care of limits and all??

thanks!

Re: calculus inside a programming language

Posted: Wed Jun 03, 2009 5:11 am
by Solar
"How would I go about implementing a function DoWhatIMean()?"

First thing, you write down exactly what it is that you want to do. What is your input? What is your intended output? What are possible errors in the input, and how would you have your function react to it?

This, you have to do for any programming language. And once you've done that, the rest is comparatively easy.

If you don't specify the input / processing / output - like, "how would I go about writing a function calculating the derivative of a mathematical expression" is mighty blurred as far as requirements go - it's effectively impossible to satisfy your demand, because the programmer doesn't know what your demands are.

Re: calculus inside a programming language

Posted: Wed Jun 03, 2009 9:46 am
by Colonel Kernel
Solar's advice is good, if not very specific. :)

What you want to do is easiest to accomplish in a functional language where code can be easily treated as data, like Lisp or Scheme. Finding the derivative of a function is itself a higher-order function: A function that takes a function as input and returns a function as output. More importantly, the actual transformation that takes place in your "derivative" function is based on recognizing patterns in the input expression (e.g. in pseudocode: derivative(x^n) = nx^(n-1)).

If you use a lower-level language like C++, C#, Java, etc., you will have to define your own classes to represent symbolic expressions that you can manipulate. Think of it kind of like writing a compiler -- your code will get some kind of syntax tree with the input expression, then try to recognize patterns in the tree and apply transformation rules to get a tree for the output expression. If you actually want to use your expressions to calculate stuff (otherwise, what's the point? :)) you will need to write more code to evaluate the trees. This is all a big pain in the butt, and AFAIK Lisp/Scheme does a lot of this stuff for you, since code is just lists, sub-lists and so on. E.g.: (derivative (expt x n)) => magic happens here => (* n (expt x (- n 1)))

Perhaps someone with more experience with these languages can comment further...

Re: calculus inside a programming language

Posted: Wed Jun 03, 2009 9:50 am
by skyking
First of all I think by the time you finish studying calculus you'll realize that there are situation where you can't end up with an expression. Fx the antiderivates is only in some situations possible to be given an expression.

For calculating limits you could use l'Hosptals rule, but that would require to determine if an expression evaluates to zero for the given argument (exactly). This may not always be possible to determine by an algorithm.

Only the derivative would be somewhat reasonable straight forward if you don't require the result be of the most simplified form. You will need to represent expressions as data you can manipulate.

Re: calculus inside a programming language

Posted: Wed Jun 03, 2009 10:00 am
by Creature
Solar is right, you have to write down the exact steps that are needed to calculate the derivative (or at least have them in your head). I would probably start by writing something that can interpret a normal mathematical expression, such as:

Code: Select all

((2 * 5)+ 8
You will probably need a tokenizer, unless you want to put restrictions on how the equations are formatted. Then you can write a way for your interpreter to take variables into account, such as 'x', you can upgrade it so it can read expressions like:

Code: Select all

2x^2 - 2^(x-1) + 5
Now that you can interpret the equations, you can think about calculating the derivative. You will probably want to make use of the different formula's such as:

Code: Select all

D[x^n] = [nx ^ (n - 1)]
D[num] = 1

D[f ^ n] = (n * f^(n - 1) * D[f])
D[f * g] = (D[f] * g + D[g] * f)
D[f / g] = ((D[f] * g - D[g] * f) / 2)
...
Where:

Code: Select all

D = Derivative
x = Unknown variable 'x'
num = Known variable 'num'
f = Function 'f'
g = Function 'g'
n = Natural number 'n' not equal to 0
I hope my basic maths are still OK, feel free to correct :P. I also didn't bother to mention anything too much in detail.

Anyhow, you'll need to deal with most of the cases. You could do all the calculations internally, or you could evaluate the given expression, then perform some operations (such as removing paranthesis where applicable, calculating derivatives) and create a new expression from it, evalulate that expression, etc... until all the necessary operations have been performed.

Re: calculus inside a programming language

Posted: Wed Jun 03, 2009 2:23 pm
by whowhatwhere

Code: Select all

(define (cube x)
  (* x x x)

(define *dx* 0.00000001)

(define (derivative f)
  (lambda (x)
    (/ (- (f (+ x *dx*))
           (f x))
        dx)))

(display
  (map (lambda (x) ((derivative cube) x))
           '(0 1 2 3 4 5 6 7 8 9)))
(newline)
Close enough for you?

EDIT: For all you pedantic fools out there, yes, I know this doesn't cover everything.

Re: calculus inside a programming language

Posted: Wed Jun 03, 2009 9:58 pm
by redoktober
okay.
looks cool!

i think this would be fun, to implement in code...thanks, guys!
:D

Re: calculus inside a programming language

Posted: Wed Jun 03, 2009 10:44 pm
by Solar
Note how Creature assumed a textual representation of a function to be derived, while Colonel Kernel and syntropy (I think, my Lisp is weak) assumed a function in code to be derived.

You didn't specify which you want, so any specific answers will be all over the place...

Re: calculus inside a programming language

Posted: Thu Jun 04, 2009 1:01 am
by Colonel Kernel
Solar wrote:Note how Creature assumed a textual representation of a function to be derived, while Colonel Kernel and syntropy (I think, my Lisp is weak) assumed a function in code to be derived.

You didn't specify which you want, so any specific answers will be all over the place...
However, the Lisp way is probably way easier. :mrgreen:

Re: calculus inside a programming language

Posted: Thu Jun 04, 2009 1:24 am
by Solar
Yes, but do we know if Lisp is an option for him? Do we know if he actually wants it to be done in textual representation?

That's what I wanted to point out - garbage in (no specs), garbage out (best guess suggestions).

Re: calculus inside a programming language

Posted: Thu Jun 04, 2009 10:51 pm
by redoktober
point.
i should have been specific.
i'm after textual representation, yeah.
so it seems lisp does it good and easy!
thanks, guys!
:)

Re: calculus inside a programming language

Posted: Fri Jun 05, 2009 6:44 pm
by mathematician
redoktober wrote:hey guys!
i've just started studying calculus. now, i'm intrigued by the idea, of writing a function in, say, c#, or any language for that matter, which could calculate the derivative of a given mathematical expression, say, x^2. of course, i would use the rule nx^n-1. but how owuld i go about writing a function in a programming language, to take care of limits and all??

thanks!
Rather you than me. You would also need to write a program which could differentiate sin(x), log(x) etc, or any combination thereof.

In the simple case you mention, you would need to read the string one character at a time, identifying terms such as 6x^3, and applying the rule you mentioned.

Re: calculus inside a programming language

Posted: Fri Jun 05, 2009 8:12 pm
by NickJohnson
Although that Lisp code you posted would definitely work if you just need a really close answer, I think the more interesting question is whether it is possible to algorithmically find the limit of an expression, which you could really use. It seems like something that would, because of the way some functions work, not be able to return an answer, which seems like a halting problem sort of deal; of course I haven't done any sort of analysis on it, so I have no clue. Is there any research into the actual *computability* of a limit (and effectively, the rest of calculus)?

Re: calculus inside a programming language

Posted: Fri Jun 05, 2009 11:04 pm
by whowhatwhere
NickJohnson wrote:Although that Lisp code you posted would definitely work if you just need a really close answer, I think the more interesting question is whether it is possible to algorithmically find the limit of an expression, which you could really use. It seems like something that would, because of the way some functions work, not be able to return an answer, which seems like a halting problem sort of deal; of course I haven't done any sort of analysis on it, so I have no clue. Is there any research into the actual *computability* of a limit (and effectively, the rest of calculus)?
Yeah there is. Don't ask me to do it though.

Re: calculus inside a programming language

Posted: Sat Jun 06, 2009 10:01 am
by NickJohnson
Are you sure about that? I know it's possible to finitely approximate a limit to an arbitrary position, but I'm not sure there is a way to compute it precisely. From the stuff I've been reading it seems like you can't always do that and can't compute whether it's possible for a specific formula or not. I think calculus is not computable by a Turing machine.