Alex Rivera | Logout

finding the derivative of a polynomial

Asked 2012-06-22T11:31:39.100
11

I wondering symbolically how you would parse a polynomial into a function and return the derivative. What data structure would I use or method to parse the polynomial? Preferably without using any libraries, as this question could pop up in a technical interview.

polynomial-> of nth degree

def derivative(polynomial):
    return derivative

Example:

f(x)  = 2x^2+3x+1
f'(x) = 4x+3

I don't want a solution, this is not homework but a hint of where I would start.

Edit
Report

2 Answers

5

In my experience matrices are often very useful in representing polynomials

answered 2012-06-22T11:35:26.510
3

Well, I believe the starting point would be to define the basic components of the expression.

When you look at a function and want to process it like that, it's basically a grammar, it can be a bit complicated depending on how much detail you want to allow.

Your grammar has the form of EXPRESSION.

EXPRESSION can be either: TERM (where your function is basically something like nx^y) or TERM OPERATOR EXPRESSION

If you can parse a function like this, you simply need to define the rules for handling a TERM, and then recursively apply the same method for the rest of the expression.

Your polynomial will always have the form nx^y, with some simplifications for cases where y is 0 or 1, or when n is 1 and is omitted.

This is how I would approach it without using any additional libs. If you want, I can try and explain my point further.

By the way, I know my answer doesn't exactly address Python or the data structures to use, but it is one possible way of approaching this kind of problems.

answered 2012-06-22T11:40:39.377

Your Answer