Menu:

OPTIONS: Download this file, Download all Python files, Go back

# Filename: polyclass.py
# Author: John Stachurski
# Date: December 2008
# Corresponds to: Listing 2.2

class Polynomial:

    def __init__(self, coef):
        """Creates an instance p of the Polynomial class,
        where p(x) = coef[0] x^0 + ... + coef[N] x^N."""
        self.coef = coef

    def evaluate(self, x):
        y = sum(a*x**i for i, a in enumerate(self.coef))
        return y

    def differentiate(self):
        new_coef = [i*a for i, a in enumerate(self.coef)]
        # Remove the first element, which is zero
        del new_coef[0]  
        # And reset coefficients data to new values
        self.coef = new_coef