# Demonstration code for Scientific Computing class, # http://www.math.nyu.edu/faculty/goodman/teaching/SciComp2022/ # IntegrandClass.py # uses Python 3 and Numpy # Demonstrate a software package, with IntegrandDemo.py # and IntegrationPackage.py import numpy as np class Integrand: """define a polynomial function. """ def __init__( self, deg, coeffs): """Remember the degree of the polynomial and its coefficients""" self.deg = deg if ( deg != ( coeffs.size - 1 )): print("YOU IDIOT") print(str(coeffs)) else: local_coeffs = np.array(coeffs) # construct a new array with the # given coefficients, to avoid # implicit dependency. self.coeffs = local_coeffs def f( self, x): # Evaluate the polynomial at x xk = 1. # will be x^k sum = 0. # will be f(x) deg = self.deg # "remember" the information that... coeffs = self.coeffs # ... defines the polynomial for k in range(deg+1): # there are n+1 terms in a degree n poly sum = sum + coeffs[k]*xk xk = xk * x return sum # but it should be.