-6

If I want to create a program which calculate the derivative of functions.

For the simple case, consider that the acceptable functions which our program can deal with are only polynomials.

My question are:

  • What is the background needed to design such program using Python?
  • Would this project be a difficult one for a beginner in Python?
  • Is Python a suitable language which could be used to design such a program or would it be easier to accomplish in another language?
joce
  • 9,624
  • 19
  • 56
  • 74
Fawzy Hegab
  • 137
  • 1
  • 2
  • 7
  • 1
    I'm sorry, but are you mixing up mathematical functions and Python functions? – Torxed Apr 09 '13 at 14:35
  • 2
    So you want to take a string containing a mathematical expression, parse it, transform it, and return the stringified transformation? – Silas Ray Apr 09 '13 at 14:35
  • Please put more effort into your question. Spelling! Grammar! Formatting! – John Kugelman Apr 09 '13 at 14:38
  • -1. I agree with @Torxed, before asking a question make sure that you have enough acquaintance upon what you're asking. – etuardu Apr 09 '13 at 14:39
  • It's cool that you are a high school student who loves math, and it is great that you are taking initiative to learn some programming so you can solve problems in a domain that interests you, but you really need to learn the basics before trying to do something like this. There are many python tutorials online that can get you up to speed on how the language and programming in general works. I suggest you start there. – Silas Ray Apr 09 '13 at 14:40
  • @sr2222 , can you plz give me some of those links for online tutorials ? – Fawzy Hegab Apr 09 '13 at 14:50
  • 1
    Lots of people recommend http://learnpythonthehardway.org/ but the tutorial (http://docs.python.org/3.3/tutorial/) directly from the horse's mouth, so to speak, is also quite serviceable. If neither of those work for you, there's plenty of stuff you can find via Google and Wikipedia. – Silas Ray Apr 09 '13 at 14:55
  • @MathsLover Here are a couple of links on SO that address your question: http://stackoverflow.com/questions/9876290/how-do-i-compute-derivative-using-numpy and http://stackoverflow.com/questions/627055/compute-a-derivative-using-discrete-methods – joce Apr 10 '13 at 23:28
  • @MathsLover Also, Python might not be the easiest language to write what you have in mind. IMO, Lisp is what you want. You can write a function that compute a derivative in very little code in Lisp: http://www.cs.sfu.ca/CourseCentral/310/pwfong/Lisp/3/tutorial3.html – joce Apr 10 '13 at 23:31

2 Answers2

2

Taking a function is easy... it's just like any other argument.

def example(somefunc):
  somefunc() 

example(someFunction)
example(lambda x: x ** 2)

Returning one is a little trickier, but not much.

You can return a lambda:

def example2():
  return lambda x: x + 1

Or you can build an inner function, and return that

def example3():
  def rf(x):
    return x + 2
  return rf

myfunc = example3()
myfunc(2) #returns 4
Tyler Eaves
  • 12,879
  • 1
  • 32
  • 39
1

math.sqrt(x) is NOT a (Python) function, math.sqrt is a function. Notice the missing (x). In the same vein, 1/(2 * math.sqrt(x)) is not a function but (lambda x: 1/(2 * math.sqrt(x))) is a function.

The issue is that

a = lambda x: x

and

b = lambda x: x

will yield two different python functions that are equivalent from a mathmatical point of view. So it is not that helpful to test functions for equality. You would need to actually "parse" them in order to find mathmatical derivatives.

Parsing the inner definitions is possible but not even close to simple.

Udo Klein
  • 6,784
  • 1
  • 36
  • 61