0

Beginner programmer here looking to write a function that can simply derive a mathematical function.

The function should run like this:

f(x) = x ** 2 + 2 * x <--- user input

f'(x) = 2 * x + 2

I know there's Wolfram and Maple, but I want to write my own actual derivative program. I would like to know if this is possible.

Community
  • 1
  • 1
Keith Yong
  • 1,026
  • 1
  • 10
  • 18
  • see [this question](http://stackoverflow.com/questions/9876290/how-do-i-compute-derivative-using-numpy) – Andy Hayden Oct 10 '12 at 22:30
  • 1
    @hayden note that that's a numerical derivative, while the OP is asking about symbolic derivatives. Related but different. – ernie Oct 10 '12 at 22:32
  • 3
    If you want to just play with something (that has source code) look at [Sympy](http://scipy-lectures.github.com/advanced/sympy.html) –  Oct 10 '12 at 22:33
  • @ernie but the accepted answer includes symbolic derivatives. – Andy Hayden Oct 10 '12 at 22:33
  • Is it possible? Of course it is, as there are many such tools already written. Depending on the capability and breadth of expressions you will work with, it may be difficult though. –  Oct 11 '12 at 01:44
  • 1
    We expect you to have attempted to solve this problem by yourself rather than asking the community to arrive at a complete solution for you. When you've got some code to show us that demonstrates some effort by you (even if it's wrong) please update your question and flag to re-open. Thanks. – Kev Oct 11 '12 at 23:44

5 Answers5

4

It's obviously possible, as there are plenty of programs out there that do symbolic differentiation. That being said, it's non trivial. For your simple example above, you'd need to write a parser that would:

  1. split up each of the terms of polynomial
  2. parse the term and break it down to coefficient, variable, and exponent
  3. Apply the power rule
  4. String together the outputs

That would only handle this very basic type of derivative - no chain rule, product rule, etc, and you'd have to implement each of those separately.

So yes, definitely doable, but also non-trivial.

ernie
  • 6,356
  • 23
  • 28
3

This is called symbolic differentation.

You need to parse the equation into tree of expressions and operations, then apply the normal rules of differentation (from Calculus I) to the tree.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
3

of course you can spend some days to write your own differentiate program (and fortunately differentiation is quite simple), but if this is not an exercise, you can actually use something ready, for example you can use sympy:

import sympy
x = sympy.Symbol('x')
sympy.diff(x**2+2*x, x)
# return: 2*x + 2
Ruggero Turra
  • 16,929
  • 16
  • 85
  • 141
2

There's a good basic symbolic differentiation example in SICP. It's scheme, not python, but should be easy enough to translate once you've dealt with parsing your input.

abeyer
  • 735
  • 4
  • 12
0

It's absolutely possible. You'll first need to parse the user's input string to get a representation of the function that you can work with. You'll then need to process the various terms in the function according the the differentiation rules you want to support.

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116