I’m new to sympy and I’m trying to perform some convolution with it.
I’m working on Jupyter Notebook.
My convolution includes two exponentials (f1, f2) with two different tau values (tau1, tau2).
When solving the convolution, sympy returns a Piecewise function with two parts:
- one for tau1 != tau2
- the other for ‘otherwise’
import sympy as sp
from sympy.plotting import plot
def convolve(f, g, t, lim_low, lim_up):
tau = sp.symbols('tau', real=True, positive=True)
h = sp.integrate(f.subs(t, tau) * g.subs(t, t - tau),
(tau, lim_low, lim_up))
return h
tau_1, tau_2, tau_3 = sp.symbols('tau_1, tau_2, tau_3', real=True, positive=True)
t = sp.symbols('t', real=True)
f1 = sp.exp( -t/tau_1 )
f2 = sp.exp( -t/tau_2 )
f3 = sp.exp( -t/tau_3 )
h12 = convolve(f1, f2, t, lim_low=0, lim_up=t).simplify()
print('First convolution:')
display(h12)
h12(t)
In my case, I know that tau1 and tau2 are not equal, so I wish to put that constraint before calculating the convolution – but I could not find how to do so.
Is there a way to do it?
I’ve tried getting the part of the calculation where tau1 != tau2 like this:
h12_TausNotEqaul = h12.args[0][0]
But when I use this in a second convolution with f3 I’m pacing with the same problem, where I only care for the case of tau1 != tau2 != tau3.
h123_TausNotEqaul = convolve(h12_TausNotEqaul, f3, t, lim_low=0, lim_up=t).simplify()
display(h123_TausNotEqaul)
h123_TausNotEqaul(t)
Thanks.
ofer frank is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.