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.Wed, 31 Jul 2013 14:56:13 +0200convolution got the wrong resulthttps://ask.sagemath.org/question/8534/convolution-got-the-wrong-result/Hi, English is not my mother language, so I am trying to make myself clear. And sorry for my grammer mistakes.
I am new to sage and trying to do convolution:f(x) * g(x)?
>f(x) = 2, -2 < x < 2; f(x) = 0, x < -2 or x > 2
>g(x) = 3/4, 0 < x < 2; g(x) = 0, x < 0 or x > 2
I use the following code to do the job:
x = PolynomialRing(QQ,'x').gen()
f = Piecewise([[(-2, 2), 2 * x^0]])
show(f)
g = Piecewise([[(0, 2), 3/4 * x^0]])
show(g)
m = g.convolution(f)
show(m)
I think I am sure the result should be:
>3/2 * x + 3, -2 < x < 0;
>3, 0 < x < 2
>-3/2 * x + 6, 2 < x < 4
>0, x < -2 or x > 4
but it comes out that when 0 < x < 2, the result is 6, which should be 3. There must be something wrong, but I can not figure out. So I am here to ask for help.
Thank you.Tue, 06 Dec 2011 09:39:30 +0100https://ask.sagemath.org/question/8534/convolution-got-the-wrong-result/Comment by kcrisman for <p>Hi, English is not my mother language, so I am trying to make myself clear. And sorry for my grammer mistakes.</p>
<p>I am new to sage and trying to do convolution:f(x) * g(x)?</p>
<blockquote>
<p>f(x) = 2, -2 < x < 2; f(x) = 0, x < -2 or x > 2</p>
<p>g(x) = 3/4, 0 < x < 2; g(x) = 0, x < 0 or x > 2</p>
</blockquote>
<p>I use the following code to do the job:</p>
<pre><code>x = PolynomialRing(QQ,'x').gen()
f = Piecewise([[(-2, 2), 2 * x^0]])
show(f)
g = Piecewise([[(0, 2), 3/4 * x^0]])
show(g)
m = g.convolution(f)
show(m)
</code></pre>
<p>I think I am sure the result should be:</p>
<blockquote>
<p>3/2 * x + 3, -2 < x < 0;</p>
<p>3, 0 < x < 2</p>
<p>-3/2 * x + 6, 2 < x < 4</p>
<p>0, x < -2 or x > 4</p>
</blockquote>
<p>but it comes out that when 0 < x < 2, the result is 6, which should be 3. There must be something wrong, but I can not figure out. So I am here to ask for help.</p>
<p>Thank you.</p>
https://ask.sagemath.org/question/8534/convolution-got-the-wrong-result/?comment=20741#post-id-20741@yideey: By the way, you can fix this yourself by opening the file at devel/sage/sage/functions/piecewise.py and making this switch of `fg2` and `fg4`, then running `sage -b`, which should work okay since it uses the Python included in Sage.Tue, 06 Dec 2011 20:40:23 +0100https://ask.sagemath.org/question/8534/convolution-got-the-wrong-result/?comment=20741#post-id-20741Comment by kcrisman for <p>Hi, English is not my mother language, so I am trying to make myself clear. And sorry for my grammer mistakes.</p>
<p>I am new to sage and trying to do convolution:f(x) * g(x)?</p>
<blockquote>
<p>f(x) = 2, -2 < x < 2; f(x) = 0, x < -2 or x > 2</p>
<p>g(x) = 3/4, 0 < x < 2; g(x) = 0, x < 0 or x > 2</p>
</blockquote>
<p>I use the following code to do the job:</p>
<pre><code>x = PolynomialRing(QQ,'x').gen()
f = Piecewise([[(-2, 2), 2 * x^0]])
show(f)
g = Piecewise([[(0, 2), 3/4 * x^0]])
show(g)
m = g.convolution(f)
show(m)
</code></pre>
<p>I think I am sure the result should be:</p>
<blockquote>
<p>3/2 * x + 3, -2 < x < 0;</p>
<p>3, 0 < x < 2</p>
<p>-3/2 * x + 6, 2 < x < 4</p>
<p>0, x < -2 or x > 4</p>
</blockquote>
<p>but it comes out that when 0 < x < 2, the result is 6, which should be 3. There must be something wrong, but I can not figure out. So I am here to ask for help.</p>
<p>Thank you.</p>
https://ask.sagemath.org/question/8534/convolution-got-the-wrong-result/?comment=20744#post-id-20744Tough nut! I think you are right about the answer being 3 there. You can see the code by typing `g.convolution??` and seeing what you get at the end. I'm wondering whether the wrong branch of the code is being taken at `if a1-b1<a2-b2:` but unfortunately have no definite answer for you yet on how to fix this.Tue, 06 Dec 2011 14:01:34 +0100https://ask.sagemath.org/question/8534/convolution-got-the-wrong-result/?comment=20744#post-id-20744Comment by yideey for <p>Hi, English is not my mother language, so I am trying to make myself clear. And sorry for my grammer mistakes.</p>
<p>I am new to sage and trying to do convolution:f(x) * g(x)?</p>
<blockquote>
<p>f(x) = 2, -2 < x < 2; f(x) = 0, x < -2 or x > 2</p>
<p>g(x) = 3/4, 0 < x < 2; g(x) = 0, x < 0 or x > 2</p>
</blockquote>
<p>I use the following code to do the job:</p>
<pre><code>x = PolynomialRing(QQ,'x').gen()
f = Piecewise([[(-2, 2), 2 * x^0]])
show(f)
g = Piecewise([[(0, 2), 3/4 * x^0]])
show(g)
m = g.convolution(f)
show(m)
</code></pre>
<p>I think I am sure the result should be:</p>
<blockquote>
<p>3/2 * x + 3, -2 < x < 0;</p>
<p>3, 0 < x < 2</p>
<p>-3/2 * x + 6, 2 < x < 4</p>
<p>0, x < -2 or x > 4</p>
</blockquote>
<p>but it comes out that when 0 < x < 2, the result is 6, which should be 3. There must be something wrong, but I can not figure out. So I am here to ask for help.</p>
<p>Thank you.</p>
https://ask.sagemath.org/question/8534/convolution-got-the-wrong-result/?comment=20740#post-id-20740@kcrisman: Thanks for your help. And I am glad that we find the bug.Tue, 06 Dec 2011 22:23:05 +0100https://ask.sagemath.org/question/8534/convolution-got-the-wrong-result/?comment=20740#post-id-20740Answer by LostTrolley for <p>Hi, English is not my mother language, so I am trying to make myself clear. And sorry for my grammer mistakes.</p>
<p>I am new to sage and trying to do convolution:f(x) * g(x)?</p>
<blockquote>
<p>f(x) = 2, -2 < x < 2; f(x) = 0, x < -2 or x > 2</p>
<p>g(x) = 3/4, 0 < x < 2; g(x) = 0, x < 0 or x > 2</p>
</blockquote>
<p>I use the following code to do the job:</p>
<pre><code>x = PolynomialRing(QQ,'x').gen()
f = Piecewise([[(-2, 2), 2 * x^0]])
show(f)
g = Piecewise([[(0, 2), 3/4 * x^0]])
show(g)
m = g.convolution(f)
show(m)
</code></pre>
<p>I think I am sure the result should be:</p>
<blockquote>
<p>3/2 * x + 3, -2 < x < 0;</p>
<p>3, 0 < x < 2</p>
<p>-3/2 * x + 6, 2 < x < 4</p>
<p>0, x < -2 or x > 4</p>
</blockquote>
<p>but it comes out that when 0 < x < 2, the result is 6, which should be 3. There must be something wrong, but I can not figure out. So I am here to ask for help.</p>
<p>Thank you.</p>
https://ask.sagemath.org/question/8534/convolution-got-the-wrong-result/?answer=15287#post-id-15287Agreed that it is wrong. After very odd results with something more complicated, I tried convolving two rectangular distributions of unequal width as a test. It is easy enough to show that the result should be a trapezoidal distribution. The result of the convolution in Sage was kind-of the right shape, but the top of the trapezium was detached, and too high by a factor of (exactly) 4.
I'm not a hacker, but would be happy to provide some test cases to somebody who wants to have a go at fixing the code.Tue, 30 Jul 2013 07:02:30 +0200https://ask.sagemath.org/question/8534/convolution-got-the-wrong-result/?answer=15287#post-id-15287Answer by kcrisman for <p>Hi, English is not my mother language, so I am trying to make myself clear. And sorry for my grammer mistakes.</p>
<p>I am new to sage and trying to do convolution:f(x) * g(x)?</p>
<blockquote>
<p>f(x) = 2, -2 < x < 2; f(x) = 0, x < -2 or x > 2</p>
<p>g(x) = 3/4, 0 < x < 2; g(x) = 0, x < 0 or x > 2</p>
</blockquote>
<p>I use the following code to do the job:</p>
<pre><code>x = PolynomialRing(QQ,'x').gen()
f = Piecewise([[(-2, 2), 2 * x^0]])
show(f)
g = Piecewise([[(0, 2), 3/4 * x^0]])
show(g)
m = g.convolution(f)
show(m)
</code></pre>
<p>I think I am sure the result should be:</p>
<blockquote>
<p>3/2 * x + 3, -2 < x < 0;</p>
<p>3, 0 < x < 2</p>
<p>-3/2 * x + 6, 2 < x < 4</p>
<p>0, x < -2 or x > 4</p>
</blockquote>
<p>but it comes out that when 0 < x < 2, the result is 6, which should be 3. There must be something wrong, but I can not figure out. So I am here to ask for help.</p>
<p>Thank you.</p>
https://ask.sagemath.org/question/8534/convolution-got-the-wrong-result/?answer=12978#post-id-12978This is apparently indeed a bug. Aside from the example yideey gives, we have the following in the code:
cmd1 = "integrate((%s)*(%s),%s,%s,%s)"%(i1,i2, uu, a1, tt-b1) ## if a1+b1 < tt < a2+b1
cmd2 = "integrate((%s)*(%s),%s,%s,%s)"%(i1,i2, uu, tt-b2, tt-b1) ## if a1+b2 < tt < a2+b1
cmd3 = "integrate((%s)*(%s),%s,%s,%s)"%(i1,i2, uu, tt-b2, a2) ## if a1+b2 < tt < a2+b2
cmd4 = "integrate((%s)*(%s),%s,%s,%s)"%(i1,i2, uu, a1, a2) ## if a2+b1 < tt < a1+b2
<snip>
if a1-b1<a2-b2:
if a2+b1!=a1+b2:
h = Piecewise([[(a1+b1,a1+b2),fg1],[(a1+b2,a2+b1),fg4],[(a2+b1,a2+b2),fg3]])
Note how the `cmd2` (which becomes `fg2`) is what is supposed to end up between `a1+b2` and `a2+b1`, but instead the result of `cmd4` does (which is `fg4`).
This is now [this bug report on Trac, #12123](http://trac.sagemath.org/sage_trac/ticket/12123).Tue, 06 Dec 2011 14:36:08 +0100https://ask.sagemath.org/question/8534/convolution-got-the-wrong-result/?answer=12978#post-id-12978Comment by kcrisman for <p>This is apparently indeed a bug. Aside from the example yideey gives, we have the following in the code:</p>
<pre><code>cmd1 = "integrate((%s)*(%s),%s,%s,%s)"%(i1,i2, uu, a1, tt-b1) ## if a1+b1 < tt < a2+b1
cmd2 = "integrate((%s)*(%s),%s,%s,%s)"%(i1,i2, uu, tt-b2, tt-b1) ## if a1+b2 < tt < a2+b1
cmd3 = "integrate((%s)*(%s),%s,%s,%s)"%(i1,i2, uu, tt-b2, a2) ## if a1+b2 < tt < a2+b2
cmd4 = "integrate((%s)*(%s),%s,%s,%s)"%(i1,i2, uu, a1, a2) ## if a2+b1 < tt < a1+b2
<snip>
if a1-b1<a2-b2:
if a2+b1!=a1+b2:
h = Piecewise([[(a1+b1,a1+b2),fg1],[(a1+b2,a2+b1),fg4],[(a2+b1,a2+b2),fg3]])
</code></pre>
<p>Note how the <code>cmd2</code> (which becomes <code>fg2</code>) is what is supposed to end up between <code>a1+b2</code> and <code>a2+b1</code>, but instead the result of <code>cmd4</code> does (which is <code>fg4</code>).</p>
<p>This is now <a href="http://trac.sagemath.org/sage_trac/ticket/12123">this bug report on Trac, #12123</a>.</p>
https://ask.sagemath.org/question/8534/convolution-got-the-wrong-result/?comment=17211#post-id-17211Also note we may be (finally) redoing piecewise functions soon, which may change some of this.Wed, 31 Jul 2013 14:56:13 +0200https://ask.sagemath.org/question/8534/convolution-got-the-wrong-result/?comment=17211#post-id-17211