here's my problem:
given any two functions, eg. f(x,a) and g(x,b), I want to build a new function, say F(f,g), which returns the product of the f and g. So:
F(f,g) = f*g = f(x, a) * g(x, b) = F(x, a, b)
I want to do this hardcoding the least possible. So, for h(x, c, d), I would get F(f,h) = F(x, a, c, d). Given that then I want to minimize F, I thought of building a class. Here's a MWE:
import numpy as np
from inspect import getargspec
def f(x, a):
return np.tanh(x*a)
def g(x, b):
return np.power(x,b)
def h(x, c, d):
return x*c+np.log(x)
class fit_func(object):
def __init__(self, data, *args):
self.data = data
self.func_a = args[0]
self.func_b = args[1]
self.args_a = getargspec(args[0])[0][1:]
self.args_b = getargspec(args[1])[0][1:]
at this point, I thought of including the following __call__
method:
def __call__(self, *self.args_a, *self.args_b):
return self.func_a(self.data,self.args_a)*self.func_b(data,self.args_b)
I thought: this way an instance of the class, say F = fit_func(some_data_array,f,g)
, would be callable as F(a,b)
. However, python doesn't like the self.args_a
and self.args_b
among the arguments of __call__
and I understand why.
Does anybody know a clever way to obtain this? Thank you very much in advance