""" Ex. 9.3 from "A primer on...". Create a class for a mathematical function as a subclass of the Parabola class used earlier. """ 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) # Line stores c0, c1 self.c2 = c2 def __call__(self, x): return super().__call__(x) + self.c2 * x**2 class SineQuadratic(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) if __name__ == "__main__": #create a couple of instance with simple parameters, #for a quick check that the results are as expected: sq1 = SineQuadratic(1, 1, 0, 0, 0) print(sq1(np.pi/2)) sq2 = SineQuadratic(0.5, np.pi/4, 1, 1, 1) print(sq2(2.0)) """ Terminal> python sin_plus_quadratic.py 1.0 7.5 """