Reputation: 478
Is there an easy way to implement a symbolic Hilbert transform in sympy such that inputting cos(x), for example, would spit out sin(x)?
Sympy does not seem to define the Hilbert transform as a symbolic integral (docs).
I tried defining it according to its limit form (from Wikipedia). I would add the equation here but I don't have enough rep to do so.
My attempt in sympy gives me a "Not implemented error":
from sympy import *
t, eps, tau = symbols('t eps tau')
limit(integrate( (sin(t+tau) - sin(t-tau))/tau, (tau, eps, oo) ), eps, 0)
Is there a way to do this in sympy? Alternatively, are there other open-source alternatives that could accomplish this?
Update:
Adding constraints to the symbolic variables as per Stelios does solve the trivial case of sin(x), but runs into problems for anything more complex.
import sympy as sp
t, tau = sp.symbols('t, tau', real = True)
eps = sp.symbols('epsilon', positive = True)
u1 = lambda x: sp.sin(x)/x
u2 = lambda x: 1/(x**2 + 1)
sp.limit(sp.integrate( (u1(t+tau) - u1(t-tau))/tau, (tau, eps, sp.oo) ), eps, 0)
sp.limit(sp.integrate( (u2(t+tau) - u2(t-tau))/tau, (tau, eps, sp.oo) ), eps, 0)
With u1, the computations hangs for a while before issuing the same "Not implemented error" and with u2, it outputs 0. Is there a more generic way to implement the transform?
Upvotes: 1
Views: 430
Reputation: 5531
Apparently, sympy fails to perform the integration for a "general" eps
. However, if you specify eps
as a positive variable, the integral can be evaluated (at least for the specific function you are considering)
import sympy as sp
t, tau = sp.symbols('t, tau', real = True)
eps = sp.symbols('epsilon', positive = True)
u = sp.sin
sp.limit(sp.integrate( (u(t+tau) - u(t-tau))/tau, (tau, eps, sp.oo) ), eps, 0)
pi*cos(t)
Upvotes: 1