First time here? Check out the FAQ!

Ask Your Question
4

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

asked 14 years ago

rtrwalker gravatar image

updated 13 years ago

Kelvin Li gravatar image

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?

Preview: (hide)

3 Answers

Sort by » oldest newest most voted
2

answered 7 years ago

mforets gravatar image

updated 7 years ago

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

Preview: (hide)
link
2

answered 14 years ago

DSM gravatar image

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.

Preview: (hide)
link

Comments

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.

kcrisman gravatar imagekcrisman ( 14 years ago )
1

answered 14 years ago

kcrisman gravatar image

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.

Preview: (hide)
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 14 years ago

Seen: 4,845 times

Last updated: May 07 '17