calculus inside a programming language

Programming, for all ages and all languages.
redoktober
Member
Member
Posts: 109
Joined: Thu Feb 26, 2009 12:58 am
Location: Gurgaon/New Delhi, India
Contact:

calculus inside a programming language

Post 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!
"Do you program in Assembly?" she asked. "NOP," he said.

"Intel Inside" is a Government Warning required by Law.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: calculus inside a programming language

Post 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.
Every good solution is obvious once you've found it.
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re: calculus inside a programming language

Post 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...
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
skyking
Member
Member
Posts: 174
Joined: Sun Jan 06, 2008 8:41 am

Re: calculus inside a programming language

Post 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.
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: calculus inside a programming language

Post 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.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
whowhatwhere
Member
Member
Posts: 199
Joined: Sat Jun 28, 2008 6:44 pm

Re: calculus inside a programming language

Post 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.
redoktober
Member
Member
Posts: 109
Joined: Thu Feb 26, 2009 12:58 am
Location: Gurgaon/New Delhi, India
Contact:

Re: calculus inside a programming language

Post by redoktober »

okay.
looks cool!

i think this would be fun, to implement in code...thanks, guys!
:D
"Do you program in Assembly?" she asked. "NOP," he said.

"Intel Inside" is a Government Warning required by Law.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: calculus inside a programming language

Post 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...
Every good solution is obvious once you've found it.
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re: calculus inside a programming language

Post 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:
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: calculus inside a programming language

Post 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).
Every good solution is obvious once you've found it.
redoktober
Member
Member
Posts: 109
Joined: Thu Feb 26, 2009 12:58 am
Location: Gurgaon/New Delhi, India
Contact:

Re: calculus inside a programming language

Post 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!
:)
"Do you program in Assembly?" she asked. "NOP," he said.

"Intel Inside" is a Government Warning required by Law.
User avatar
mathematician
Member
Member
Posts: 437
Joined: Fri Dec 15, 2006 5:26 pm
Location: Church Stretton Uk

Re: calculus inside a programming language

Post 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.
The continuous image of a connected set is connected.
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: calculus inside a programming language

Post 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)?
whowhatwhere
Member
Member
Posts: 199
Joined: Sat Jun 28, 2008 6:44 pm

Re: calculus inside a programming language

Post 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.
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: calculus inside a programming language

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