""" This file implements a class for a Linear function, very similar to the Quadratic class of exercise 7.5. There are two versions; one using a tuple to represent the coefficients and one using two float variables (commented out code). The point is to illustrate that if all such internal data are accessed through functions such as get_coefficients, instead of being accessed directly, the users of the class will not be affected of a change in internal data structure. """ from numpy import linspace class Linear: #class for linear functions f(x) = ax+b def __init__(self,a,b): #self.a = a #self.b = b self.coeff = (a,b) def value(self,x): a,b = self.coeff #a = self.a; b = self.b return a*x+ b def table(self,n,L,R): x_values = linspace(L,R,n) for x in x_values: print(x,value(x)) def root(self): return -self.coeff[1]/self.coeff[0] #return -self.b/self.a def get_coefficients(self): return self.coeff #return self.a,self.b line1 = Linear(1,0) print(line1.root()) print(line1.get_coefficients())