""" Exercise 7.11 from "A primer on..." Modify the class from ex 7.1 to support the given usage. We see that the class needs a __call__ method to support calls like f(x=pi), as well as a __str__ method that returns the function expression, for pretty print. """ from math import pi import numpy as np class F: def __init__(self, a, w): self.a = a self.w = w def __call__(self, x): a = self.a w = self.w return np.exp(-a * x) * np.sin(w * x) def __str__(self): return f"exp(-{self.a}*x)*sin({self.w}*x)" f = F(a=1.0, w=0.1) print(f(x = pi)) f.a = 2.0 print(f(x = pi)) print(f) #output: exp(-a*x)*sin(w*x) """ Terminal> python F2.py 0.01335383513703555 0.0004931861122731331 exp(-2.05*x)*sin(0.1*x) """