I am new using function generators. I am working with the following function:
def math_model(n: float):
def crrcn(x, t0: float, amp: float, tau: float, offset: float, dt: float, k: float):
crrcn = np.zeros(np.shape(x))
for i, a in enumerate(x):
if a < (t0 + dt):
crrcn[i] = offset
else:
crrcn[i] = offset + k * amp * (math.exp(n) / (n ** n)) * ((a - (t0 + dt)) / tau) ** n * math.exp( - (a - (t0 + dt)) / tau)
return crrcn
return crrcn
which defines a mathematical model I will use later to fit some data with scipy curve_fit. I wanted to use this same model to build a more complicated one with the following line.
model = partial(math_model(N), dt = 0, k = 1) + math_model(N)
which gives me:
TypeError: unsupported operand type(s) for +: 'functools.partial' and 'function'
From which I understand that I cannot build a function from two functions using this operator and as far as I know there are no function operands in python. How can one build a function from other functions without explicitly evaluating them?
PREVIOUS ANSWER:
This seems like a misunderstanding of partial
.
partial(...)
returns a new function. It does not execute it, it just creates it.
So your line model = partial(math_model(N), dt = 0, k = 1) + math_model(N)
is invalid, because you are basically doing a a + b
operation, where a
is ... a function :)
What you may wish to do is simply applying the model. That can be done using math_model(N)(dt = 0, k = 1)
.
So
model = math_model(N)(dt = 0, k = 1) + math_model(N)
may do the trick
NEW EDIT: It seems that I misunderstood you. You actually wish to create a function by combining two functions. This is therefore some kind of symbolic reasoning. There are a few libraries out there for this, the most advanced that I know is SymPy
. Or if your functions have only one argument, you can use my mini_lambda
For example with mini-lambda:
from mini_lambda import x
f1 = x ** 2
f2 = 5 * x
f3 = f1 + f2
print(f3.to_string())
print(f3.evaluate(1))
yields
x ** 2 + 5 * x
6