ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 13 Feb 2018 09:57:02 -0600piecewise defined function via defhttps://ask.sagemath.org/question/41060/piecewise-defined-function-via-def/I've made the following experiment with Sage:
def f(x):
if (0<= x<= 1/2):
return 1
else:
return 0
assume(0<= x<= 1/2)
show(f(x))
show(f(1/3))
However I get outputs 0 and 1 respectively. Can someone clarify please? Thanks.Sat, 10 Feb 2018 20:39:46 -0600https://ask.sagemath.org/question/41060/piecewise-defined-function-via-def/Comment by tmonteil for <p>I've made the following experiment with Sage:</p>
<pre><code> def f(x):
if (0<= x<= 1/2):
return 1
else:
return 0
assume(0<= x<= 1/2)
show(f(x))
show(f(1/3))
</code></pre>
<p>However I get outputs 0 and 1 respectively. Can someone clarify please? Thanks.</p>
https://ask.sagemath.org/question/41060/piecewise-defined-function-via-def/?comment=41084#post-id-41084Note: duplicate of https://ask.sagemath.org/question/41061/piecewise-defined-function-via-def/Tue, 13 Feb 2018 04:33:02 -0600https://ask.sagemath.org/question/41060/piecewise-defined-function-via-def/?comment=41084#post-id-41084Answer by nbruin for <p>I've made the following experiment with Sage:</p>
<pre><code> def f(x):
if (0<= x<= 1/2):
return 1
else:
return 0
assume(0<= x<= 1/2)
show(f(x))
show(f(1/3))
</code></pre>
<p>However I get outputs 0 and 1 respectively. Can someone clarify please? Thanks.</p>
https://ask.sagemath.org/question/41060/piecewise-defined-function-via-def/?answer=41081#post-id-41081Looks like double inequalities aren't parsed properly by "assume":
sage: assume(0<= x<= 1/2)
sage: bool(0<=x<=1/2)
False
sage: assume(0<=x)
sage: assume(x<=1/2)
sage: bool(0<=x<=1/2)
True
Mon, 12 Feb 2018 11:51:58 -0600https://ask.sagemath.org/question/41060/piecewise-defined-function-via-def/?answer=41081#post-id-41081Comment by tmonteil for <p>Looks like double inequalities aren't parsed properly by "assume":</p>
<pre><code>sage: assume(0<= x<= 1/2)
sage: bool(0<=x<=1/2)
False
sage: assume(0<=x)
sage: assume(x<=1/2)
sage: bool(0<=x<=1/2)
True
</code></pre>
https://ask.sagemath.org/question/41060/piecewise-defined-function-via-def/?comment=41095#post-id-41095Note that `assume` does not parse anything, it takes a symbolic expression that silently fails, see [my answer to the duplicate question](https://ask.sagemath.org/question/41061/piecewise-defined-function-via-def/?answer=41094#post-id-41094)Tue, 13 Feb 2018 09:57:02 -0600https://ask.sagemath.org/question/41060/piecewise-defined-function-via-def/?comment=41095#post-id-41095