I'm developing a scientific library where I would define vector functions in the time and frequency domain (linked by FFT). I created a class for vector formulas in the freq domain, and now I'd want to define an identical class for the time domain.
I want that in the time domain, the class functions - although being identical to their frequency-domain twin - have one parameter named t
instead of omega
. Is there an easier way of achieving this instead of repeated definition of every single method, while maintaining readibility?
My code:
(Note: my classes are much more complicated, and one can't just use the functions as formula.x_func(...)
- some checking and etc are included. Also, there are actually 6 components.)
class VecFormula(object):
pass
class FreqFormula(VecFormula):
def __init__(self, x_func, y_func, z_func):
self.x_func = x_func
self.y_func = y_func
self.z_func = z_func
def x(self, x, y, z, omega, params):
return self.x_func(x, y, z, omega, params)
def y(self, x, y, z, omega, params):
return self.y_func(x, y, z, omega, params)
def z(self, x, y, z, omega, params):
return self.z_func(x, y, z, omega, params)
def component(self, comp, x, y, z, omega, params):
if comp == 'x':
return self.x(x, y, z, omega, params)
elif comp == 'y':
return self.y(x, y, z, omega, params)
elif comp == 'z':
return self.z(x, y, z, omega, params)
else:
raise ValueError(f'invalid component: {comp}')
class TimeFormula(FreqFormula):
"same as FreqFormula, but the omega parameter is renamed to t"
def x(self, x, y, z, t, params):
return super(TimeFormula, self).x(x, y, z, t, params)
def y(self, x, y, z, t, params):
return super(TimeFormula, self).y(x, y, z, t, params)
def z(self, x, y, z, t, params):
return super(TimeFormula, self).z(x, y, z, t, params)
def component(self, comp, x, y, z, t, params):
return super(TimeFormula, self).component(x, y, z, t, params)