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, 20 Jul 2016 01:15:28 -0500Unable to create a contour_plot of a system of inequalitieshttp://ask.sagemath.org/question/34111/unable-to-create-a-contour_plot-of-a-system-of-inequalities/I am trying to plot a system of inequalities, dependent on a matrix H. Here is my function I am planning to contour_plot: <br>
def reg(x, y):
f1 = H[0,0] * H[0,0] * x + H[1,0] * H[1,0] * y
f2 = H[0,0] * H[0,1] * x + H[1,0] * H[1,1] * y
f3 = H[0,1] * H[0,1] * x + H[1,1] * H[1,1] * y
if f1 < 0 or f2 < 0 or f3 < 0:
return 0
else:
return 1
I then have H be
> H
> [2.220446049250313e-16 -0.9999999999999998]
> [ -0.9999999999999998 2.220446049250313e-16]
However
contour_plot(reg, (x,-Integer(5),Integer(5)), (y,-Integer(5),Integer(5)))
yields an error. It says
zero-size array to reduction operation minimum which has no identity
The strange part is that when
> H
> [-1 0]
> [ 0 1]
the same contour_plot yields exactly what I want without any errors <br>
Help would be much appreciated, I have just picked up SAGE this week and have much to learn
Sat, 16 Jul 2016 14:30:45 -0500http://ask.sagemath.org/question/34111/unable-to-create-a-contour_plot-of-a-system-of-inequalities/Comment by tmonteil for <p>I am trying to plot a system of inequalities, dependent on a matrix H. Here is my function I am planning to contour_plot: <br/></p>
<pre><code>def reg(x, y):
f1 = H[0,0] * H[0,0] * x + H[1,0] * H[1,0] * y
f2 = H[0,0] * H[0,1] * x + H[1,0] * H[1,1] * y
f3 = H[0,1] * H[0,1] * x + H[1,1] * H[1,1] * y
if f1 < 0 or f2 < 0 or f3 < 0:
return 0
else:
return 1
</code></pre>
<p>I then have H be</p>
<pre><code>> H
> [2.220446049250313e-16 -0.9999999999999998]
> [ -0.9999999999999998 2.220446049250313e-16]
</code></pre>
<p>However </p>
<pre><code>contour_plot(reg, (x,-Integer(5),Integer(5)), (y,-Integer(5),Integer(5)))
</code></pre>
<p>yields an error. It says </p>
<pre><code>zero-size array to reduction operation minimum which has no identity
</code></pre>
<p>The strange part is that when </p>
<pre><code>> H
> [-1 0]
> [ 0 1]
</code></pre>
<p>the same contour_plot yields exactly what I want without any errors <br/>
Help would be much appreciated, I have just picked up SAGE this week and have much to learn</p>
http://ask.sagemath.org/question/34111/unable-to-create-a-contour_plot-of-a-system-of-inequalities/?comment=34121#post-id-34121Thanks for reporting !Mon, 18 Jul 2016 06:08:27 -0500http://ask.sagemath.org/question/34111/unable-to-create-a-contour_plot-of-a-system-of-inequalities/?comment=34121#post-id-34121Answer by paulmasson for <p>I am trying to plot a system of inequalities, dependent on a matrix H. Here is my function I am planning to contour_plot: <br/></p>
<pre><code>def reg(x, y):
f1 = H[0,0] * H[0,0] * x + H[1,0] * H[1,0] * y
f2 = H[0,0] * H[0,1] * x + H[1,0] * H[1,1] * y
f3 = H[0,1] * H[0,1] * x + H[1,1] * H[1,1] * y
if f1 < 0 or f2 < 0 or f3 < 0:
return 0
else:
return 1
</code></pre>
<p>I then have H be</p>
<pre><code>> H
> [2.220446049250313e-16 -0.9999999999999998]
> [ -0.9999999999999998 2.220446049250313e-16]
</code></pre>
<p>However </p>
<pre><code>contour_plot(reg, (x,-Integer(5),Integer(5)), (y,-Integer(5),Integer(5)))
</code></pre>
<p>yields an error. It says </p>
<pre><code>zero-size array to reduction operation minimum which has no identity
</code></pre>
<p>The strange part is that when </p>
<pre><code>> H
> [-1 0]
> [ 0 1]
</code></pre>
<p>the same contour_plot yields exactly what I want without any errors <br/>
Help would be much appreciated, I have just picked up SAGE this week and have much to learn</p>
http://ask.sagemath.org/question/34111/unable-to-create-a-contour_plot-of-a-system-of-inequalities/?answer=34119#post-id-34119The cause of the error message is that your function `reg` is returning all zero values for the first matrix `H`. Somewhere down the line Sage tries to take an inverse of zero and that returns an error. I've created a [ticket](https://trac.sagemath.org/ticket/21042) to report this problem.
One workaround for you is to have your function return a small nonzero value rather than exactly zero to avoid the error. Here's a live [example](http://sagecell.sagemath.org/?z=eJxtkMsKgzAURPeC_3B3ahslNz6opd37D0FKaWMRrCmplvj3TVDpg8xqMuQkw7zOKgw0TEHke1fRgBK3UBOYor3vgVGDcISKU0Jr2HyMhq054JouZloY9s-gk8FvJl0YdDLoYtrG1jsABansp6tLrVv6WykxjKoHFDEWcyq6p3BcmBPfq0yX-3lQrQ45ZwljNMsKmpUspymm9hkS06T81a4m3JESJ1_XZm_fu8h-kKM6PTo5hGZ6Amb8OCd5ZNw0u-gNMJ9eNg==&lang=sage).Sun, 17 Jul 2016 20:49:32 -0500http://ask.sagemath.org/question/34111/unable-to-create-a-contour_plot-of-a-system-of-inequalities/?answer=34119#post-id-34119Comment by petkus for <p>The cause of the error message is that your function <code>reg</code> is returning all zero values for the first matrix <code>H</code>. Somewhere down the line Sage tries to take an inverse of zero and that returns an error. I've created a <a href="https://trac.sagemath.org/ticket/21042">ticket</a> to report this problem.</p>
<p>One workaround for you is to have your function return a small nonzero value rather than exactly zero to avoid the error. Here's a live <a href="http://sagecell.sagemath.org/?z=eJxtkMsKgzAURPeC_3B3ahslNz6opd37D0FKaWMRrCmplvj3TVDpg8xqMuQkw7zOKgw0TEHke1fRgBK3UBOYor3vgVGDcISKU0Jr2HyMhq054JouZloY9s-gk8FvJl0YdDLoYtrG1jsABansp6tLrVv6WykxjKoHFDEWcyq6p3BcmBPfq0yX-3lQrQ45ZwljNMsKmpUspymm9hkS06T81a4m3JESJ1_XZm_fu8h-kKM6PTo5hGZ6Amb8OCd5ZNw0u-gNMJ9eNg==&lang=sage">example</a>.</p>
http://ask.sagemath.org/question/34111/unable-to-create-a-contour_plot-of-a-system-of-inequalities/?comment=34146#post-id-34146Thanks!
It turns out the problem wasn't with what the function returned but with the inequalities.
I set f1 < -1e-3, f2 < -1e-3, f3 < -1e-3 and it works how I wanted.
This is a very strange bug that really makes sage a black box to me :(Wed, 20 Jul 2016 01:15:28 -0500http://ask.sagemath.org/question/34111/unable-to-create-a-contour_plot-of-a-system-of-inequalities/?comment=34146#post-id-34146Comment by tmonteil for <p>The cause of the error message is that your function <code>reg</code> is returning all zero values for the first matrix <code>H</code>. Somewhere down the line Sage tries to take an inverse of zero and that returns an error. I've created a <a href="https://trac.sagemath.org/ticket/21042">ticket</a> to report this problem.</p>
<p>One workaround for you is to have your function return a small nonzero value rather than exactly zero to avoid the error. Here's a live <a href="http://sagecell.sagemath.org/?z=eJxtkMsKgzAURPeC_3B3ahslNz6opd37D0FKaWMRrCmplvj3TVDpg8xqMuQkw7zOKgw0TEHke1fRgBK3UBOYor3vgVGDcISKU0Jr2HyMhq054JouZloY9s-gk8FvJl0YdDLoYtrG1jsABansp6tLrVv6WykxjKoHFDEWcyq6p3BcmBPfq0yX-3lQrQ45ZwljNMsKmpUspymm9hkS06T81a4m3JESJ1_XZm_fu8h-kKM6PTo5hGZ6Amb8OCd5ZNw0u-gNMJ9eNg==&lang=sage">example</a>.</p>
http://ask.sagemath.org/question/34111/unable-to-create-a-contour_plot-of-a-system-of-inequalities/?comment=34122#post-id-34122Thanks for tracking !Mon, 18 Jul 2016 06:08:58 -0500http://ask.sagemath.org/question/34111/unable-to-create-a-contour_plot-of-a-system-of-inequalities/?comment=34122#post-id-34122