ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 07 May 2017 22:19:15 +0200How can I Integrate the dirac_delta and heaviside functions in sage?https://ask.sagemath.org/question/8041/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?Thu, 31 Mar 2011 20:56:34 +0200https://ask.sagemath.org/question/8041/how-can-i-integrate-the-dirac_delta-and-heaviside-functions-in-sage/Answer by mforets for <p>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:</p>
<pre><code>reset()
var('x,a')
integral(x^2*dirac_delta(-a + x), x, -infinity, +infinity)
</code></pre>
<p>from which, after evaluating, I get:</p>
<pre><code>integrate(x^2*dirac_delta(-a + x), x, -Infinity, +Infinity)
</code></pre>
<p>i.e. the integration is not performed. I am expecting a^2. </p>
<p>For the heaviside fucntion I've tried:</p>
<pre><code>reset()
var('x,x1,x2')
k(x,x1,x2)=heaviside(x-x1)*heaviside(x2-x)
integrate(k(x,x1,x2),x,-infinity,infinity)
</code></pre>
<p>from which, after evaluating, I get:</p>
<pre><code>integrate(heaviside(-x + x2)*heaviside(x - x1), x, -Infinity, +Infinity)
</code></pre>
<p>I am expecting x2-x1. Am I doing something wrong or is it just not possible to do these integrations in sage?</p>
https://ask.sagemath.org/question/8041/how-can-i-integrate-the-dirac_delta-and-heaviside-functions-in-sage/?answer=37536#post-id-37536It 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](https://trac.sagemath.org/ticket/22850)Sun, 07 May 2017 22:19:15 +0200https://ask.sagemath.org/question/8041/how-can-i-integrate-the-dirac_delta-and-heaviside-functions-in-sage/?answer=37536#post-id-37536Answer by DSM for <p>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:</p>
<pre><code>reset()
var('x,a')
integral(x^2*dirac_delta(-a + x), x, -infinity, +infinity)
</code></pre>
<p>from which, after evaluating, I get:</p>
<pre><code>integrate(x^2*dirac_delta(-a + x), x, -Infinity, +Infinity)
</code></pre>
<p>i.e. the integration is not performed. I am expecting a^2. </p>
<p>For the heaviside fucntion I've tried:</p>
<pre><code>reset()
var('x,x1,x2')
k(x,x1,x2)=heaviside(x-x1)*heaviside(x2-x)
integrate(k(x,x1,x2),x,-infinity,infinity)
</code></pre>
<p>from which, after evaluating, I get:</p>
<pre><code>integrate(heaviside(-x + x2)*heaviside(x - x1), x, -Infinity, +Infinity)
</code></pre>
<p>I am expecting x2-x1. Am I doing something wrong or is it just not possible to do these integrations in sage?</p>
https://ask.sagemath.org/question/8041/how-can-i-integrate-the-dirac_delta-and-heaviside-functions-in-sage/?answer=12242#post-id-12242Sympy 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.Fri, 01 Apr 2011 03:04:06 +0200https://ask.sagemath.org/question/8041/how-can-i-integrate-the-dirac_delta-and-heaviside-functions-in-sage/?answer=12242#post-id-12242Comment by kcrisman for <p>Sympy can handle the delta but not the Heaviside product:</p>
<pre><code>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))
</code></pre>
<p>Unfortunately because of interface gaps algorithm='sympy' doesn't work:</p>
<pre><code>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
</code></pre>
<p>The monkeypatch doesn't take much, though:</p>
<pre><code>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
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/8041/how-can-i-integrate-the-dirac_delta-and-heaviside-functions-in-sage/?comment=21932#post-id-21932No, 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.Fri, 01 Apr 2011 11:28:45 +0200https://ask.sagemath.org/question/8041/how-can-i-integrate-the-dirac_delta-and-heaviside-functions-in-sage/?comment=21932#post-id-21932Answer by kcrisman for <p>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:</p>
<pre><code>reset()
var('x,a')
integral(x^2*dirac_delta(-a + x), x, -infinity, +infinity)
</code></pre>
<p>from which, after evaluating, I get:</p>
<pre><code>integrate(x^2*dirac_delta(-a + x), x, -Infinity, +Infinity)
</code></pre>
<p>i.e. the integration is not performed. I am expecting a^2. </p>
<p>For the heaviside fucntion I've tried:</p>
<pre><code>reset()
var('x,x1,x2')
k(x,x1,x2)=heaviside(x-x1)*heaviside(x2-x)
integrate(k(x,x1,x2),x,-infinity,infinity)
</code></pre>
<p>from which, after evaluating, I get:</p>
<pre><code>integrate(heaviside(-x + x2)*heaviside(x - x1), x, -Infinity, +Infinity)
</code></pre>
<p>I am expecting x2-x1. Am I doing something wrong or is it just not possible to do these integrations in sage?</p>
https://ask.sagemath.org/question/8041/how-can-i-integrate-the-dirac_delta-and-heaviside-functions-in-sage/?answer=12241#post-id-12241Maxima doesn't know how to integrate the Dirac function, so neither does Sage. From [their docs](http://maxima.sourceforge.net/docs/manual/en/maxima_19.html):
> Currently only laplace knows about the delta function.
Sage actually even [mentions](http://www.sagemath.org/doc/reference/sage/interfaces/maxima.html) 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.Thu, 31 Mar 2011 23:06:09 +0200https://ask.sagemath.org/question/8041/how-can-i-integrate-the-dirac_delta-and-heaviside-functions-in-sage/?answer=12241#post-id-12241