Polynomial Calculus

The questions below are due on Friday June 21, 2024; 10:00:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.

In this problem, we'll explore methods for computing symbolic derivatives and integrals of polynomials.

For this question, let's represent Polynomials in x as lists of coefficients, starting with the zeroth-order coefficient. For example, we would represent x^2 as [0, 0, 1] and 4x^3 + 7x - 8 as [-8, 7, 0, 4].

Write a function calc_deriv that computes the symbolic derivative of a polynomial given in this form. This problem relies heavily on the power rule, or the fact that the derivative is x^n is nx^{n-1}. We can perform the derivative on each term of the polynomial independently.

Also write a function calc_integral to do the symbolic integration of polynomials. We'll represent polynomials in the same form we used above, and like before we can use the power rule for integration on each term where x^n would have an integral of \frac{1}{n+1}x^{n+1}

Notes

  • In order to return the proper value in the end, you'll want to construct a new list. Here are two possible strategies for this:
    • Figure out the length that the output list will be. Make a list of that length which contains only zeros, or only Nones (or some othe default value). Then, replace the entries in that list with the appropriate values.
    • Start with an empty list, [], and add the appropriate values one by one using append.
  • In some cases, you may find it helpful to loop over the indices of a list, rather than over the elements in the list. A nice way to loop over those indices is to write, for example, for index in range(len(my_list)) instead of for value in my_list. This allows you to use the index in computation, and you can still get the associated element via, for example, my_list[index].
  • You should choose some of your favorite polynomials and use those as test cases on your own machine.

Link to starter code file

def calc_deriv(polynomial):
    """
    Inputs:
        polynomial (list<float,int>) - current polynomial coefficients

    Returns:
        A new list of polynomial coefficients resulting from taking the 
        derivative of the polynomial expression.

    """
    pass # your code here

def calc_integral(polynomial, constant):
    """
    Inputs:
        polynomial (list<float,int>) - current polynomial coefficients
        constant (float, int) - constant term in the new polynomial

    Returns:
        The list of polynomial coefficients resulting from taking the integral
        of the expression.
    """
    pass # your code here

if __name__ == "__main__":    
    # Local testing -- feel free to add your own tests as well!

    poly = [0, 0, 1/2, 4] # represents 1/2 x^2 + 4 x^3
    print(f"Got {calc_deriv(poly)=}, Expected=[0, 1.0, 12]")
    # Expected represents x  + 12 x^2

    poly2 = [3, 2] # represents polynomial 2x + 3
    print(f"Got {calc_integral(poly2, 15)=}, Expected=[15, 3.0, 1.0]")
    # Expected represents 15 + 3x + x^2

Once you have simulated by hand and tested on your own machine, and are convinced that your program will do the right thing, upload your file below for testing:

  No file selected

Next Exercise: 2-D Arrays

Back to exercises