ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 16 Feb 2011 00:54:10 -0600Strange behviour when trying to integrate gaussian function. bug?http://ask.sagemath.org/question/7943/strange-behviour-when-trying-to-integrate-gaussian-function-bug/I was trying integrate the following function
y=var('y')
integrate(x*exp(-(x-y)*(x-y)*2.0),x)
$$\frac{1}{8} \, {\left(\frac{2 \, {\left(\text{erf}\left(\sqrt{2} \sqrt{{\left(x - y\right)}^{2}}\right) - 1\right)} {\left(x - y\right)} \sqrt{\pi} y}{\sqrt{{\left(x - y\right)}^{2}}} - \sqrt{2} e^{\left(-2 \, {\left(x - y\right)}^{2}\right)}\right)} \sqrt{2}$$
The integral can easily be done by redefining variables and does not have to be expressed in terms of error functions. Also the behavior of sage becomes even strange when I change the coefficient from 2.0 to 2.1. Sage just refuses to do the integral
integrate(x*exp(-(x-y)*(x-y)*2.1),x)
$$\int x e^{\left(-2.1 \, {\left(x - y\right)}^{2}\right)}\,{d x}$$
Any ideas on how to make sage give an answer in the usual exponential form?
Edit: Sorry. My bad. It is an error function. However, I expect error function even for the second example, when I replace 2 by 2.1. Any ideas why that is the case?
Mon, 14 Feb 2011 14:36:02 -0600http://ask.sagemath.org/question/7943/strange-behviour-when-trying-to-integrate-gaussian-function-bug/Comment by kcrisman for <p>I was trying integrate the following function</p>
<pre><code>y=var('y')
integrate(x*exp(-(x-y)*(x-y)*2.0),x)
</code></pre>
<p>$$\frac{1}{8} \, {\left(\frac{2 \, {\left(\text{erf}\left(\sqrt{2} \sqrt{{\left(x - y\right)}^{2}}\right) - 1\right)} {\left(x - y\right)} \sqrt{\pi} y}{\sqrt{{\left(x - y\right)}^{2}}} - \sqrt{2} e^{\left(-2 \, {\left(x - y\right)}^{2}\right)}\right)} \sqrt{2}$$
The integral can easily be done by redefining variables and does not have to be expressed in terms of error functions. Also the behavior of sage becomes even strange when I change the coefficient from 2.0 to 2.1. Sage just refuses to do the integral</p>
<pre><code>integrate(x*exp(-(x-y)*(x-y)*2.1),x)
</code></pre>
<p>$$\int x e^{\left(-2.1 \, {\left(x - y\right)}^{2}\right)}\,{d x}$$</p>
<p>Any ideas on how to make sage give an answer in the usual exponential form?</p>
<p>Edit: Sorry. My bad. It is an error function. However, I expect error function even for the second example, when I replace 2 by 2.1. Any ideas why that is the case?</p>
http://ask.sagemath.org/question/7943/strange-behviour-when-trying-to-integrate-gaussian-function-bug/?comment=22120#post-id-22120@DSM - no need to make comments vanish, that's part of the open development process, to keep a record of everyone's thoughts. "Stupid" comments may later turn out to be prescient.Tue, 15 Feb 2011 02:13:43 -0600http://ask.sagemath.org/question/7943/strange-behviour-when-trying-to-integrate-gaussian-function-bug/?comment=22120#post-id-22120Comment by DSM for <p>I was trying integrate the following function</p>
<pre><code>y=var('y')
integrate(x*exp(-(x-y)*(x-y)*2.0),x)
</code></pre>
<p>$$\frac{1}{8} \, {\left(\frac{2 \, {\left(\text{erf}\left(\sqrt{2} \sqrt{{\left(x - y\right)}^{2}}\right) - 1\right)} {\left(x - y\right)} \sqrt{\pi} y}{\sqrt{{\left(x - y\right)}^{2}}} - \sqrt{2} e^{\left(-2 \, {\left(x - y\right)}^{2}\right)}\right)} \sqrt{2}$$
The integral can easily be done by redefining variables and does not have to be expressed in terms of error functions. Also the behavior of sage becomes even strange when I change the coefficient from 2.0 to 2.1. Sage just refuses to do the integral</p>
<pre><code>integrate(x*exp(-(x-y)*(x-y)*2.1),x)
</code></pre>
<p>$$\int x e^{\left(-2.1 \, {\left(x - y\right)}^{2}\right)}\,{d x}$$</p>
<p>Any ideas on how to make sage give an answer in the usual exponential form?</p>
<p>Edit: Sorry. My bad. It is an error function. However, I expect error function even for the second example, when I replace 2 by 2.1. Any ideas why that is the case?</p>
http://ask.sagemath.org/question/7943/strange-behviour-when-trying-to-integrate-gaussian-function-bug/?comment=22121#post-id-22121Could you be specific about the transformation you have in mind? As usual, I'm missing something-- I can't see how to do it without an erf popping up. (And you should expect this comment to vanish after I realize how stupid I was. :^)Mon, 14 Feb 2011 16:44:46 -0600http://ask.sagemath.org/question/7943/strange-behviour-when-trying-to-integrate-gaussian-function-bug/?comment=22121#post-id-22121Answer by kcrisman for <p>I was trying integrate the following function</p>
<pre><code>y=var('y')
integrate(x*exp(-(x-y)*(x-y)*2.0),x)
</code></pre>
<p>$$\frac{1}{8} \, {\left(\frac{2 \, {\left(\text{erf}\left(\sqrt{2} \sqrt{{\left(x - y\right)}^{2}}\right) - 1\right)} {\left(x - y\right)} \sqrt{\pi} y}{\sqrt{{\left(x - y\right)}^{2}}} - \sqrt{2} e^{\left(-2 \, {\left(x - y\right)}^{2}\right)}\right)} \sqrt{2}$$
The integral can easily be done by redefining variables and does not have to be expressed in terms of error functions. Also the behavior of sage becomes even strange when I change the coefficient from 2.0 to 2.1. Sage just refuses to do the integral</p>
<pre><code>integrate(x*exp(-(x-y)*(x-y)*2.1),x)
</code></pre>
<p>$$\int x e^{\left(-2.1 \, {\left(x - y\right)}^{2}\right)}\,{d x}$$</p>
<p>Any ideas on how to make sage give an answer in the usual exponential form?</p>
<p>Edit: Sorry. My bad. It is an error function. However, I expect error function even for the second example, when I replace 2 by 2.1. Any ideas why that is the case?</p>
http://ask.sagemath.org/question/7943/strange-behviour-when-trying-to-integrate-gaussian-function-bug/?answer=12104#post-id-12104I assume this is still not what you want, as it doesn't use any substitution as you suggest, but it does seem to work:
sage: integrate(x*exp(-(x-y)*(x-y)*2.1),x)
-1/882*I*(21*I*(erf(sqrt(21/10)*sqrt((x - y)^2)) - 1)*(x - y)*sqrt(pi)*y/sqrt((x - y)^2) - I*sqrt(10)*sqrt(21)*e^(-21/10*(x - y)^2))*sqrt(10)*sqrt(21)
This is coming from Maxima, of course.
$$-\frac{1}{882} i {\left(\frac{21 i {\left(\text{erf}\left(\sqrt{\frac{21}{10}} \sqrt{{\left(x - y\right)}^{2}}\right) - 1\right)} {\left(x - y\right)} \sqrt{\pi} y}{\sqrt{{\left(x - y\right)}^{2}}} - i \sqrt{10} \sqrt{21} e^{\left(-\frac{21}{10} \, {\left(x - y\right)}^{2}\right)}\right)} \sqrt{10} \sqrt{21}$$
If I do `simplify_full()` to the answer, I get something that looks like
$$-\frac{1}{882} {\left(10 \sqrt{3} \sqrt{7} e^{\left(\frac{21}{5} x y\right)} + 21 {\left(\sqrt{2} \sqrt{5} y e^{\left(\frac{21}{10} x^{2} + \frac{21}{10} y^{2}\right)} \text{erf}\left(-\frac{1}{10} {\left(\sqrt{3} \sqrt{7} x - \sqrt{3} \sqrt{7} y\right)} \sqrt{2} \sqrt{5}\right) - \sqrt{2} \sqrt{5} y e^{\left(\frac{21}{10} x^{2} + \frac{21}{10} y^{2}\right)}\right)} \sqrt{\pi}\right)} \sqrt{3} \sqrt{7} e^{\left(-\frac{21}{10}x^{2} - \frac{21}{10} y^{2}\right)}$$
which is still messy but at least doesn't have any imaginary components. Is this not sufficient?
Mon, 14 Feb 2011 15:06:59 -0600http://ask.sagemath.org/question/7943/strange-behviour-when-trying-to-integrate-gaussian-function-bug/?answer=12104#post-id-12104Comment by Shashank for <p>I assume this is still not what you want, as it doesn't use any substitution as you suggest, but it does seem to work:</p>
<pre><code>sage: integrate(x*exp(-(x-y)*(x-y)*2.1),x)
-1/882*I*(21*I*(erf(sqrt(21/10)*sqrt((x - y)^2)) - 1)*(x - y)*sqrt(pi)*y/sqrt((x - y)^2) - I*sqrt(10)*sqrt(21)*e^(-21/10*(x - y)^2))*sqrt(10)*sqrt(21)
</code></pre>
<p>This is coming from Maxima, of course.</p>
<p>$$-\frac{1}{882} i {\left(\frac{21 i {\left(\text{erf}\left(\sqrt{\frac{21}{10}} \sqrt{{\left(x - y\right)}^{2}}\right) - 1\right)} {\left(x - y\right)} \sqrt{\pi} y}{\sqrt{{\left(x - y\right)}^{2}}} - i \sqrt{10} \sqrt{21} e^{\left(-\frac{21}{10} \, {\left(x - y\right)}^{2}\right)}\right)} \sqrt{10} \sqrt{21}$$</p>
<p>If I do <code>simplify_full()</code> to the answer, I get something that looks like</p>
<p>$$-\frac{1}{882} {\left(10 \sqrt{3} \sqrt{7} e^{\left(\frac{21}{5} x y\right)} + 21 {\left(\sqrt{2} \sqrt{5} y e^{\left(\frac{21}{10} x^{2} + \frac{21}{10} y^{2}\right)} \text{erf}\left(-\frac{1}{10} {\left(\sqrt{3} \sqrt{7} x - \sqrt{3} \sqrt{7} y\right)} \sqrt{2} \sqrt{5}\right) - \sqrt{2} \sqrt{5} y e^{\left(\frac{21}{10} x^{2} + \frac{21}{10} y^{2}\right)}\right)} \sqrt{\pi}\right)} \sqrt{3} \sqrt{7} e^{\left(-\frac{21}{10}x^{2} - \frac{21}{10} y^{2}\right)}$$</p>
<p>which is still messy but at least doesn't have any imaginary components. Is this not sufficient?</p>
http://ask.sagemath.org/question/7943/strange-behviour-when-trying-to-integrate-gaussian-function-bug/?comment=22116#post-id-22116Can you elaborate on how you got the quoted answer? I am still getting this as the output \int x e^{\left(-2.1 \, {\left(x - y\right)}^{2}\right)}\,{d x} Tue, 15 Feb 2011 16:35:49 -0600http://ask.sagemath.org/question/7943/strange-behviour-when-trying-to-integrate-gaussian-function-bug/?comment=22116#post-id-22116Comment by kcrisman for <p>I assume this is still not what you want, as it doesn't use any substitution as you suggest, but it does seem to work:</p>
<pre><code>sage: integrate(x*exp(-(x-y)*(x-y)*2.1),x)
-1/882*I*(21*I*(erf(sqrt(21/10)*sqrt((x - y)^2)) - 1)*(x - y)*sqrt(pi)*y/sqrt((x - y)^2) - I*sqrt(10)*sqrt(21)*e^(-21/10*(x - y)^2))*sqrt(10)*sqrt(21)
</code></pre>
<p>This is coming from Maxima, of course.</p>
<p>$$-\frac{1}{882} i {\left(\frac{21 i {\left(\text{erf}\left(\sqrt{\frac{21}{10}} \sqrt{{\left(x - y\right)}^{2}}\right) - 1\right)} {\left(x - y\right)} \sqrt{\pi} y}{\sqrt{{\left(x - y\right)}^{2}}} - i \sqrt{10} \sqrt{21} e^{\left(-\frac{21}{10} \, {\left(x - y\right)}^{2}\right)}\right)} \sqrt{10} \sqrt{21}$$</p>
<p>If I do <code>simplify_full()</code> to the answer, I get something that looks like</p>
<p>$$-\frac{1}{882} {\left(10 \sqrt{3} \sqrt{7} e^{\left(\frac{21}{5} x y\right)} + 21 {\left(\sqrt{2} \sqrt{5} y e^{\left(\frac{21}{10} x^{2} + \frac{21}{10} y^{2}\right)} \text{erf}\left(-\frac{1}{10} {\left(\sqrt{3} \sqrt{7} x - \sqrt{3} \sqrt{7} y\right)} \sqrt{2} \sqrt{5}\right) - \sqrt{2} \sqrt{5} y e^{\left(\frac{21}{10} x^{2} + \frac{21}{10} y^{2}\right)}\right)} \sqrt{\pi}\right)} \sqrt{3} \sqrt{7} e^{\left(-\frac{21}{10}x^{2} - \frac{21}{10} y^{2}\right)}$$</p>
<p>which is still messy but at least doesn't have any imaginary components. Is this not sufficient?</p>
http://ask.sagemath.org/question/7943/strange-behviour-when-trying-to-integrate-gaussian-function-bug/?comment=22115#post-id-22115I literally typed integrate(x*exp(-(x-y)*(x-y)*2.1),x). If you do A=integrate(x*exp(-(x-y)*(x-y)*2.1),x), and then A.simplify_full(), you get the last thing. What version of Sage are you using? Maxima has steadily improved its integration ability, and we try to keep up with it.Wed, 16 Feb 2011 00:54:10 -0600http://ask.sagemath.org/question/7943/strange-behviour-when-trying-to-integrate-gaussian-function-bug/?comment=22115#post-id-22115