""" Exercise 9.3 from "A primer on..." Simple extension of the Parabola class, with one additional term. The point of the exercise is to represent the new function as a subclass with minimal code, rusing as much as possible from the Parabola class. """ import numpy as np class Line: def __init__(self, c0, c1): self.c0, self.c1 = c0, c1 def __call__(self, x): return self.c0 + self.c1 * x def table(self, L, R, n): """Returner en tabell med n punkter for L <= x <= R.""" s = '' for x in np.linspace(L, R, n): y = self(x) s += f'{x:12g} {y:12g}\n' return s class Parabola(Line): def __init__(self, c0, c1, c2): super().__init__(c0, c1) self.c2 = c2 def __call__(self, x): return super().__call__(x) + self.c2 * x**2 p = Parabola(1,1,1) print(p(3)) class SinPlusQuadratic(Parabola): def __init__(self, A, w, a, b, c): super().__init__(c, b, a) self.A = A self.w = w def __call__(self, x): return super().__call__(x) + self.A * np.sin(self.w * x) f = SinPlusQuadratic(1, np.pi, 1, 1, 1) print(f(2.0)) print(f.table(0, 1, 5))