# How can I Integrate the dirac_delta and heaviside functions in sage?

Is it possible to integrate the dirac_delta and the heaviside function in sage. I can't seem to do it. For the dirac_delta I've tried the following code:

reset()
var('x,a')
integral(x^2*dirac_delta(-a + x), x, -infinity, +infinity)


from which, after evaluating, I get:

integrate(x^2*dirac_delta(-a + x), x, -Infinity, +Infinity)


i.e. the integration is not performed. I am expecting a^2.

For the heaviside fucntion I've tried:

reset()
var('x,x1,x2')
k(x,x1,x2)=heaviside(x-x1)*heaviside(x2-x)
integrate(k(x,x1,x2),x,-infinity,infinity)


from which, after evaluating, I get:

integrate(heaviside(-x + x2)*heaviside(x - x1), x, -Infinity, +Infinity)


I am expecting x2-x1. Am I doing something wrong or is it just not possible to do these integrations in sage?

edit retag close merge delete

Sort by » oldest newest most voted

Maxima doesn't know how to integrate the Dirac function, so neither does Sage. From their docs:

Currently only laplace knows about the delta function.

Sage actually even mentions this use case in the manual.

But after all, it's not a function. You can integrate against it is all, but there is no symbolic integral, I guess.

I think that something similar is true for the Heaviside function.

That doesn't mean it couldn't be implemented, but there are no tickets open for this.

more Sympy can handle the delta but not the Heaviside product:

sage: import sympy
sage: x,a = sympy.var("x a")
sage: sympy.integrate(x^2*sympy.DiracDelta(x -a), (x,-sympy.oo, sympy.oo))
a**2
sage:
sage: x,x1,x2 = sympy.var('x,x1,x2')
sage: k=sympy.Heaviside(x-x1)*sympy.Heaviside(x2-x)
sage: sympy.integrate(k,(x, -sympy.oo, sympy.oo))
Integral(Heaviside(x2 - x)*Heaviside(x - x1), (x, -oo, oo))


Unfortunately because of interface gaps algorithm='sympy' doesn't work:

sage: reset()
sage: var("a x")
(a, x)
sage: integral(x^2*dirac_delta(-a + x), x, -infinity, +infinity,algorithm='sympy')
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (534, 0))
[...]
NotImplementedError: SymPy function 'dirac_delta' doesn't exist


The monkeypatch doesn't take much, though:

sage: reset()
sage: import sympy
sage: sympy.dirac_delta = sympy.DiracDelta
sage: sage.rings.infinity.MinusInfinity._sympy_ = lambda self: -sympy.oo
sage: var("a x")
(a, x)
sage: integral(x^2*dirac_delta(-a + x), x, -infinity, +infinity,algorithm='sympy')
a^2


A better fix would be to improve sage.symbolic.expression_conversions.sympy and add more functions to the translation table. But I have the impression that sympy-style integration is considered a dead end, which is strange to me because at least I can understand the code, whereas I wouldn't know where to begin to fix a maxima issue.

more

No, we should totally support that if it helps improve our stuff! Please feel free to add more functions to the tables and cc: me to review the ticket.

It is (now) possible to integrate these special functions in Sage as follows:

Dirac: using the algorithm keyword:

sage: var('x,a')
sage: integral(x^2*dirac_delta(-a + x), x, -infinity, +infinity, algorithm='sympy')
a^2
sage: integral(x^2*dirac_delta(-a + x), x, -infinity, +infinity, algorithm='giac')  # >= v.8.0.beta5
a^2


Heaviside: in this case the Maxima interface returns the expected result but using unit_step instead:

sage: var('x,x1,x2')
sage: assume(x1<x2)
sage: k(x,x1,x2) = unit_step(x-x1) * unit_step(x2-x)
sage: integrate(k(x,x1,x2), x, -infinity, infinity)
-x1 + x2


cf. #22850

more