Compute the derivative of a polynomial.
A polynomial can be represented by a dictionary as explained in Chapter 6.2.3. Write a function diff for differentiating such a polynomial. The diff function takes the polynomial as a dictionary argument and returns the dictionary representation of the derivative. Recall the formula for differentiation of polynomials:
This means that the coefficient of the xj−1term in the derivative equals j times the coefficient of x j term of the original polynomial. With p as the polynomial dictionary and dp as the dictionary representing the derivative, we then have dp[j-1] = j*p[j] for j running over all keys in p, except when j equals 0.
Here is an example of the use of the function diff:
>>> p = {0: -3, 3: 2, 5: -1} # -3 + 2*x**3 - x**5
>>> diff(p) # should be 6*x**2 - 5*x**4
{2: 6, 4: -5}
Name of program file: poly_diff.py.
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here